Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Created November 21, 2012 20:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krishnanraman/4127299 to your computer and use it in GitHub Desktop.
Save krishnanraman/4127299 to your computer and use it in GitHub Desktop.
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 )
val (minShares,maxShares) = (1,1000/23) // about 50 shares max, since min stock price = 23, 23*50 > 1000
weightedSum( (minShares,maxShares), stocks, cash, error).write( Tsv("invest.txt"))
------
Results: Exactly 70 ways
------
FB MSFT HP
2 22 7
14 8 9
9 18 6
21 4 8
6 11 11
1 21 8
13 7 10
27 12 1
8 17 7
20 3 9
5 10 12
12 6 11
26 11 2
33 7 1
4 9 13
6 28 2
18 14 4
11 5 12
13 24 1
25 10 3
32 6 2
3 8 14
5 27 3
17 13 5
12 23 2
24 9 4
9 16 7
21 2 9
2 7 15
4 26 4
16 12 6
11 22 3
23 8 5
8 15 8
20 1 10
3 25 5
15 11 7
12 4 12
7 14 9
33 5 2
2 24 6
14 10 8
40 1 1
11 3 13
6 13 10
20 18 1
32 4 3
3 6 15
10 2 14
12 21 3
24 7 5
5 12 11
19 17 2
31 3 4
26 13 1
2 5 16
9 1 15
11 20 4
23 6 6
6 30 1
18 16 3
30 2 5
1 4 17
3 23 6
15 9 8
10 19 5
22 5 7
5 29 2
17 15 4
29 1 6
---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment