43 |
43 |
44 // test case |
44 // test case |
45 val p_fb = get_prices(List("FB"), 2012 to 2014) |
45 val p_fb = get_prices(List("FB"), 2012 to 2014) |
46 val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012) |
46 val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012) |
47 |
47 |
48 get_prices(List("BIDU"), 2004 to 2008) |
48 val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008) |
49 |
49 |
50 // (2) The first function below calculates the change factor (delta) between |
50 // (2) The first function below calculates the change factor (delta) between |
51 // a price in year n and a price in year n+1. The second function calculates |
51 // a price in year n and a price in year n+1. The second function calculates |
52 // all change factors for all prices (from a portfolio). |
52 // all change factors for all prices (from a portfolio). |
53 |
53 |
63 for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j)) |
63 for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j)) |
64 |
64 |
65 |
65 |
66 // test case using the prices calculated above |
66 // test case using the prices calculated above |
67 val d = get_deltas(p) |
67 val d = get_deltas(p) |
68 |
68 val ttd = get_deltas(tt) |
69 |
69 |
70 // (3) Write a function that given change factors, a starting balance and a year |
70 // (3) Write a function that given change factors, a starting balance and a year |
71 // calculates the yearly yield, i.e. new balanace, according to our dump investment |
71 // calculates the yearly yield, i.e. new balanace, according to our dump investment |
72 // strategy. Another function calculates given the same data calculates the |
72 // strategy. Another function calculates given the same data calculates the |
73 // compound yield up to a given year. Finally a function combines all |
73 // compound yield up to a given year. Finally a function combines all |
82 else { |
82 else { |
83 val portion: Double = balance.toDouble / somes_length.toDouble |
83 val portion: Double = balance.toDouble / somes_length.toDouble |
84 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
84 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
85 } |
85 } |
86 } |
86 } |
|
87 |
|
88 yearly_yield(ttd, 100, 0) |
|
89 yearly_yield(ttd, 100, 1) |
|
90 yearly_yield(ttd, 100, 2) |
|
91 yearly_yield(ttd, 100, 3) assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2) |
|
92 assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2) |
|
93 assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2) |
|
94 assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2) |
|
95 |
|
96 |
87 |
97 |
88 //test case |
98 //test case |
89 yearly_yield(d, 100, 0) |
99 yearly_yield(d, 100, 0) |
90 yearly_yield(d, 225, 1) |
100 yearly_yield(d, 225, 1) |
91 yearly_yield(d, 246, 2) |
101 yearly_yield(d, 246, 2) |
113 |
123 |
114 //test cases for the two portfolios given above |
124 //test cases for the two portfolios given above |
115 |
125 |
116 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
126 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
117 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |
127 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |
|
128 |
|
129 |
|
130 investment(List("IBM", "BIDU"), 2004 to 2008, 100) |
118 |
131 |
119 val p_fb = get_prices(List("FB"), 2011 to 2016) |
132 val p_fb = get_prices(List("FB"), 2011 to 2016) |
120 |
133 |
121 // prices 2011 - 2016 |
134 // prices 2011 - 2016 |
122 // |
135 // |