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
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 ) |
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
// 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) | |
}) |
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
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) |
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 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 |
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
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")) |
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 scala.actors._ | |
import scala.actors.Actor._ | |
import scala.math.{pow,abs} | |
import collection.mutable.HashMap | |
class CoordinatesActor extends Actor { | |
def act = { | |
react { |
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
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. |
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
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 |
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
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) |
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> empDB.sortBy(e=>(e.tenure,e.age,-e.salary)).reverse.head | |
res0: Employee = Employee(35,6,85079,emp7435) |
OlderNewer