Skip to content

Instantly share code, notes, and snippets.

View krishnanraman's full-sized avatar

Krishnan Raman krishnanraman

View GitHub Profile
@krishnanraman
krishnanraman / script.scala
Last active August 29, 2015 13:55
Number of Times an Average American has sex per year, based on their age, gender & political affiliation.
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
@krishnanraman
krishnanraman / RegressionMonoid.scala
Last active August 29, 2015 13:56
$ scala RegressionMonoid , Result: Alpha 3.000 Beta 2.000
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)
}
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
@krishnanraman
krishnanraman / gist:9147382
Last active August 29, 2015 13:56
Scalding vs TrivialHadoop
https://github.com/krishnanraman/trivialhadoop
// 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] = {
$ 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
@krishnanraman
krishnanraman / gershgorin.scala
Last active August 29, 2015 14:03
Approximation of L2 norm of matrix using gershgorin's circles
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")
@krishnanraman
krishnanraman / gist:0693b397291001f97280
Last active August 29, 2015 14:04
Gershgorin using the Scalding Typed API !!!
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.
@krishnanraman
krishnanraman / gist:c17397667ea9090a5bc1
Last active August 29, 2015 14:04
Alfred Brauer's Ovals of Cassini, using Scalding Typed API
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.
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