89 val portion: Double = balance.toDouble / somes_length.toDouble |
86 val portion: Double = balance.toDouble / somes_length.toDouble |
90 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
87 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
91 } |
88 } |
92 } |
89 } |
93 |
90 |
94 yearly_yield(d, 100, 0) |
|
95 yearly_yield(ttd, 100, 1) |
|
96 yearly_yield(ttd, 100, 2) |
|
97 yearly_yield(ttd, 100, 3) |
|
98 |
|
99 assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2) |
|
100 assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2) |
|
101 assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2) |
|
102 assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2) |
|
103 |
|
104 |
|
105 |
|
106 //test case |
|
107 yearly_yield(d, 100, 0) |
|
108 yearly_yield(d, 225, 1) |
|
109 yearly_yield(d, 246, 2) |
|
110 yearly_yield(d, 466, 3) |
|
111 yearly_yield(d, 218, 4) |
|
112 yearly_yield(d, 469, 5) |
|
113 yearly_yield(d, 587, 6) |
|
114 //yearly_yield(d, 100, 0) |
|
115 //yearly_yield(d, 125, 1) |
|
116 |
|
117 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
91 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
118 if (year >= data.length) balance else { |
92 if (year >= data.length) balance else { |
119 val new_balance = yearly_yield(data, balance, year) |
93 val new_balance = yearly_yield(data, balance, year) |
120 compound_yield(data, new_balance, year + 1) |
94 compound_yield(data, new_balance, year + 1) |
121 } |
95 } |
122 } |
96 } |
123 |
97 |
124 compound_yield(d.take(6), 100, 0) |
98 yearly_yield(d, 100, 0) |
|
99 //compound_yield(d.take(6), 100, 0) |
125 |
100 |
126 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
101 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
127 compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
102 compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
128 } |
103 } |
129 |
104 |
130 investment(List("GOOG", "AAPL"), 2005 to 2009, 100) |
105 |
131 |
106 |
132 //test cases for the two portfolios given above |
107 //test cases for the two portfolios given above |
133 |
108 |
134 println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100)) |
109 //println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100)) |
135 println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100)) |
110 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100)) |
136 |
111 |
137 |
112 |
138 investment(List("IBM", "BIDU"), 2004 to 2008, 100) |
|
139 |
|
140 val p_fb = get_prices(List("FB"), 2011 to 2016) |
|
141 |
|
142 // prices 2011 - 2016 |
|
143 // |
|
144 List(List(None), List(None), List(Some(28.0)), |
|
145 List(Some(54.709999)), List(Some(78.449997)), |
|
146 List(Some(102.220001))) |
|
147 |
|
148 // deltas 2011 - 2016 |
|
149 val d_fb = get_deltas(p_fb) |
|
150 |
|
151 List(List(None), List(None), List(Some(0.9539285357142858)), |
|
152 List(Some(0.4339242996513305)), List(Some(0.30299560113431234))) |
|
153 |
|
154 yearly_yield(d_fb, 100, 0) //2011 => 100 |
|
155 yearly_yield(d_fb, 100, 1) //2012 => 100 |
|
156 yearly_yield(d_fb, 100, 2) //2013 => 195 |
|
157 yearly_yield(d_fb, 195, 3) //2014 => 279 |
|
158 yearly_yield(d_fb, 279, 4) //2015 => 363 |
|
159 |
|
160 investment(List("FB"), 2011 to 2012, 100) // => 100 |
|
161 investment(List("FB"), 2011 to 2013, 100) // => 100 |
|
162 investment(List("FB"), 2011 to 2014, 100) // => 195 |
|
163 investment(List("FB"), 2011 to 2015, 100) // => 279 |
|
164 investment(List("FB"), 2011 to 2016, 100) // => 363 |
|
165 |
|
166 |
|
167 val rs_p = get_prices(rstate_portfolio, 1978 to 2016) |
|
168 val bl_p = get_prices(blchip_portfolio, 1978 to 2016) |
|
169 |
|
170 val rs_d = get_deltas(rs_p) |
|
171 val bl_d = get_deltas(bl_p) |
|
172 |
|
173 rs_p(0) |
|
174 <- |
|
175 rs_p(1) |
|
176 <- |
|
177 rs_p(2) |
|
178 <- |
|
179 rs_p(3) |
|
180 |
|
181 rs_d(0) |
|
182 rs_d(1) |
|
183 rs_d(2) |
|
184 |
|
185 yearly_yield(rs_d, 100, 0) |
|
186 yearly_yield(rs_d, 96, 1) |
|
187 yearly_yield(rs_d, 95, 2) |
|
188 yearly_yield(rs_d, 134, 3) |
|
189 yearly_yield(rs_d, 126, 4) |
|
190 yearly_yield(rs_d, 169, 5) |
|
191 |
|
192 } |
113 } |