This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Age Female,Conservative Female,Liberal MALE | |
18 133 124 128 | |
19 128 119 124 | |
20 124 115 120 | |
21 120 112 116 | |
22 116 108 113 | |
23 112 104 109 | |
24 108 100 106 | |
25 104 97 102 | |
26 100 93 99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collection.parallel.ParSeq | |
object RegressionMonoid extends App { | |
// Use monoidal addition as opposed to def mean(x:Seq[Double, size:Int) = x.sum/size | |
def mean(x:ParSeq[Double], size:Int):Double = { | |
def monoid(ab:Seq[Double]) = ab.size match { case 2 => ab.head + ab.last; case _ => ab.head } | |
val pairs = x.toList.sliding(2,2).toSeq | |
val sumpar = pairs.par.map(monoid) | |
if (sumpar.size == 1 ) sumpar.head/size else mean(sumpar, size) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def foo[T](x:T, y:T)(implicit n:Numeric[T]) = n.toDouble(x)+n.toDouble(y) | |
foo: [T](x: T, y: T)(implicit n: Numeric[T])Double | |
scala> foo(2,3.0) | |
res4: Double = 5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/krishnanraman/trivialhadoop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://news.ycombinator.com/item?id=7856135 | |
object zsb extends App { | |
type T = (Int,Int,Int) | |
def ok(zsb:T) = { val (z,s,b) = zsb; z>=0 && s>=0 && b>=0 } | |
def parse(s:String) = { val arr = s.split(",").map(s=> s.toInt); (arr(0), arr(1), arr(2)) } | |
def buyout(zsb:T, set:Set[T]):Set[T] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scala lsh | |
From time to time this submerged or latent theater in becomes almost overt. It is close to the surface in Hamlet?s pretense of madness, the ?antic disposition? he puts on to protect himself and prevent his antagonists from plucking out the heart of his mystery. It is even closer to the surface when Hamlet enters his mother?s room and holds up, side by side, the pictures of the two kings, Old Hamlet and Claudius, and proceeds to describe for her the true nature of the choice she has made, presenting truth by means of a show. Similarly, when he leaps into the open grave at Ophelia?s funeral, ranting in high heroic terms, he is acting out for Laertes, and perhaps for himself as well, the folly of excessive, melodramatic expressions of grief. | |
is 13.52 % similar to | |
Almost all of Shakespeare?s Hamlet can be understood as a play about acting and the theater. For example, there is Hamlet?s pretense of madness, the ?antic disposition? that he puts on to protect himself and prevent his antagonists fro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object gershgorin extends App { | |
type Matrix = Seq[Seq[Double]] | |
def gershgorin(square: Matrix) = { | |
val rows = (0 until square.size) | |
rows.map(i => square(i)(i)) zip | |
rows.map(i => (0 until i).map(j=> math.abs(square(i)(j))) ++ (i+1 until square.size).map(j=> math.abs(square(i)(j)))).map(_.sum) | |
} | |
def eigenValues(square: Matrix) = gershgorin(square).map{ cr => val (center,radius) = cr; (center - radius, center + radius)} | |
def spectralRadius(square: Matrix) = eigenValues(square).map(_._2).max | |
def mkString(m:Matrix) = m.map{r => r.mkString(",")}.mkString("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given a symmetric matrix, one can compute the approximate L2 Norm using a dated 1970s technique nobody uses anymore. | |
(Its a very good approximation, btw. ) | |
For geezers like myself, who learnt math in the 1970s using abacus & log tables & such, | |
the standard technique to computing approximate eigen values in your head | |
was to mentally draw out the Gershgorin circles. | |
The eigen values would lie in them circles. | |
The max eigen value is the spectral radius. | |
L2 Norms of symmetric matrices are simply their spectral radius. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Idea: | |
The chief intuition behind Gershgorin: Simply examine matrix rows, one at a time. | |
But what if you look at matrix rows, two at a time ? | |
That is the chief intuition behind the Cassini Oval ! | |
Details: | |
To obtain ballpark estimates of eigen values of matrices (especially diagonally-dominant ones) | |
one usually resorts to Gerschgorin disks. | |
The idea is that eigens lie in the union of Gershgorin disks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object VonMises extends App { | |
type Matrix = Seq[Seq[Double]] | |
type Vector = Seq[Double] | |
def dot(a:Vector, b:Vector) = a.zip(b).map(i=>i._1 * i._2).sum // dot product aka inner product | |
def mult(m:Matrix, v:Vector) = m.map{ row => dot(row,v) } // matrix vector multiplication | |
def l2(v:Vector) = math.sqrt(v.map{i=> i*i}.sum) // l2 norm of vector | |
def scale(v:Vector, c:Double) = v.map{i=> i*c } | |
// computes the dominant eigen value and associated eigen vector via Von Mises Iteration aka Power Method |