Skip to content

Instantly share code, notes, and snippets.

View krishnanraman's full-sized avatar

Krishnan Raman krishnanraman

View GitHub Profile
@krishnanraman
krishnanraman / weightedSum
Created November 21, 2012 20:02
How to use weightedSum(..) in Scalding to perform elementary Portfolio Mgmt
How many ways can you invest $1000 in facebook, microsoft, hp ?
https://github.com/krishnanraman/scalding/blob/develop/src/main/scala/com/twitter/scalding/mathematics/Combinatorics.scala
Using the Combinatorics package in a Scalding job, its absolutely trivial.
------ACTUAL CODE --------
val cash = 1000
val error = 5 // max error $5, so its ok if we cannot invest the last $5 or less
val (FB, MSFT, HP) = (23,27,51) // share prices
val stocks = IndexedSeq( FB,MSFT,HP )
@krishnanraman
krishnanraman / gist:4166029
Created November 29, 2012 01:09
Oscar movember challenge
// Compute a cayley table for a finite abelian group given its zero
def zero(x:Int, group:Vector[Int]) = {
val n = group.size
val pos = group.indexOf(x)
val base = group.slice(pos, n) ++ group.slice(0, pos)
Vector.tabulate(n,n)((x,y)=> {
val row = base.slice(x,n)++base.slice(0,x)
row(y)
})
@krishnanraman
krishnanraman / gist:4199415
Created December 4, 2012 00:36
IPGetter.scala
def ip(host:String)=java.net.InetAddress.getAllByName(host)(0).getHostAddress
var map = Map[String,String]()
val hosts = List("google.com","twitter.com","facebook.com")
hosts.par.map(host => (host,ip(host))).foreach(hostip => map+=(hostip._1->hostip._2))
scala> map
res1: scala.collection.immutable.Map[String,String] = Map(google.com -> 74.125.224.71, twitter.com -> 199.59.150.7, facebook.com -> 69.171.229.16)
@krishnanraman
krishnanraman / gist:4210467
Created December 5, 2012 00:01
Portfolio Mgmt using Scalding
import com.twitter.scalding._
import cern.colt.matrix.{DoubleFactory2D, DoubleFactory1D }
import cern.colt.matrix.linalg.Algebra
import java.util.StringTokenizer
class Portfolios(args : Args) extends Job(args) {
val cash = 1000.0 // money at hand
val error = 1 // its ok if we cannot invest the last dollar
@krishnanraman
krishnanraman / gist:4221584
Created December 6, 2012 03:27
CombinatoricsJob
package com.twitter.scalding.mathematics
import org.specs._
import com.twitter.scalding._
class CombinatoricsJob(args : Args) extends Job(args) {
val C = Combinatorics
C.permutations( 5,2 ).write(Tsv("perms.txt"))
C.combinations( 5,2 ).write(Tsv("combs.txt"))
@krishnanraman
krishnanraman / gist:4255840
Created December 11, 2012 04:18
parallel pathfinding
import scala.actors._
import scala.actors.Actor._
import scala.math.{pow,abs}
import collection.mutable.HashMap
class CoordinatesActor extends Actor {
def act = {
react {
@krishnanraman
krishnanraman / gist:4333595
Created December 19, 2012 01:10
rsq in scala repl
val data = List((1,2),(2,3),(3,4),(4,5),(5,7))
def line(x:Int) = x+1 // guess based on data
def mean(xy:List[(Int,Int)]) = xy.map(a=>a._2).sum/(0.0+xy.size) // average the y's
def sstot(xy:List[(Int,Int)]) = { val mu = mean(data); xy.map(a=>(a._2-mu)*(a._2-mu)).sum } // total sum of squares
def sserr(xy:List[(Int,Int)]) = { xy.map(a=>(a._2-line(a._1))*(a._2-line(a._1))).sum } // sum of squares of residuals
def rsq(xy:List[(Int,Int)]) = 1.0 - sserr(xy)/sstot(xy)
scala> rsq(data)
res5: Double = 0.9324324324324325 // 93% fit, not bad for a guess.
@krishnanraman
krishnanraman / EmployeeMonoid.scala
Created January 15, 2013 01:11
Employee Monoid
case class Employee(age:Int, tenure:Int, salary:Int, name:String)
def rnd(min:Int,max:Int) = math.round(min+math.random*(max-min)).toInt
val empDB = for(employees<-1 to 10000) yield Employee( rnd(25,35),rnd(1,6),rnd(85000,150000),"emp"+employees)
case class Monoid(e:Set[Employee]) {
def plus(a:Employee,b:Employee)= {
if(a.age > b.age) a else
if(b.age > a.age) b else
@krishnanraman
krishnanraman / gist:4535154
Last active December 11, 2015 03:08
Employee DB
case class Employee(age:Int, tenure:Int, salary:Int, name:String)
def rnd(min:Int,max:Int) = math.round(min+math.random*(max-min)).toInt
val empDB = for(employees<-1 to 10000) yield Employee( rnd(25,35),rnd(1,6),rnd(85000,150000),"emp"+employees)
@krishnanraman
krishnanraman / gist:4535169
Created January 15, 2013 01:21
Nested Sort
scala> empDB.sortBy(e=>(e.tenure,e.age,-e.salary)).reverse.head
res0: Employee = Employee(35,6,85079,emp7435)