40 for (year <- years.toList) yield |
40 for (year <- years.toList) yield |
41 for (symbol <- portfolio) yield get_first_price(symbol, year) |
41 for (symbol <- portfolio) yield get_first_price(symbol, year) |
42 |
42 |
43 |
43 |
44 // test case |
44 // test case |
45 val p = 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 |
48 |
49 // (2) The first function below calculates the change factor (delta) between |
49 // (2) The first function below calculates the change factor (delta) between |
50 // a price in year n and a price in year n+1. The second function calculates |
50 // a price in year n and a price in year n+1. The second function calculates |
77 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
77 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
78 val somes = data(year).flatten |
78 val somes = data(year).flatten |
79 val somes_length = somes.length |
79 val somes_length = somes.length |
80 if (somes_length == 0) balance |
80 if (somes_length == 0) balance |
81 else { |
81 else { |
82 val portion: Double = (balance.toDouble) / somes_length |
82 val portion: Double = balance.toDouble / somes_length.toDouble |
83 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
83 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
84 } |
84 } |
85 } |
85 } |
86 |
86 |
87 //test case |
87 //test case |
113 //test cases for the two portfolios given above |
113 //test cases for the two portfolios given above |
114 |
114 |
115 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
115 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
116 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |
116 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |
117 |
117 |
|
118 val p_fb = get_prices(List("FB"), 2011 to 2016) |
|
119 |
|
120 // prices 2011 - 2016 |
|
121 // |
|
122 List(List(None), List(None), List(Some(28.0)), |
|
123 List(Some(54.709999)), List(Some(78.449997)), |
|
124 List(Some(102.220001))) |
|
125 |
|
126 // deltas 2011 - 2016 |
|
127 val d_fb = get_deltas(p_fb) |
|
128 |
|
129 List(List(None), List(None), List(Some(0.9539285357142858)), |
|
130 List(Some(0.4339242996513305)), List(Some(0.30299560113431234))) |
|
131 |
|
132 yearly_yield(d_fb, 100, 0) //2011 => 100 |
|
133 yearly_yield(d_fb, 100, 1) //2012 => 100 |
|
134 yearly_yield(d_fb, 100, 2) //2013 => 195 |
|
135 yearly_yield(d_fb, 195, 3) //2014 => 279 |
|
136 yearly_yield(d_fb, 279, 4) //2015 => 363 |
|
137 |
|
138 investment(List("FB"), 2011 to 2012, 100) // => 100 |
|
139 investment(List("FB"), 2011 to 2013, 100) // => 100 |
|
140 investment(List("FB"), 2011 to 2014, 100) // => 195 |
|
141 investment(List("FB"), 2011 to 2015, 100) // => 279 |
|
142 investment(List("FB"), 2011 to 2016, 100) // => 363 |
|
143 |
|
144 |