121 // calculates the yearly yield, i.e. new balance, according to our dumb investment |
121 // calculates the yearly yield, i.e. new balance, according to our dumb investment |
122 // strategy. Index points to a year in the data list. |
122 // strategy. Index points to a year in the data list. |
123 |
123 |
124 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = { |
124 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = { |
125 val somes = data(index).flatten |
125 val somes = data(index).flatten |
|
126 println(s"somes: $somes") |
126 val somes_length = somes.length |
127 val somes_length = somes.length |
127 if (somes_length == 0) balance |
128 if (somes_length == 0) balance |
128 else { |
129 else { |
129 val portion: Double = balance.toDouble / somes_length.toDouble |
130 val portion: Double = balance.toDouble / somes_length.toDouble |
130 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
131 println(s"portion: $portion") |
131 } |
132 val b = balance + (for (x <- somes) yield (x * portion)).sum.toLong |
132 } |
133 println(s"balance $b") |
|
134 println(s"profit ${(for (x <- somes) yield (x * portion)).sum}") |
|
135 b |
|
136 } |
|
137 } |
|
138 |
|
139 val dd = List(List(Some(0.9)), List(Some(0.9))) |
|
140 yearly_yield(dd, 100, 0) |
|
141 |
|
142 def yearly_yield2(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = { |
|
143 |
|
144 val share = balance/data(index).flatten.size.toDouble |
|
145 println(s"portion: $share") |
|
146 def count(data: List[Option[Double]], prof: Double, share: Double) : Double = data match{ |
|
147 case Nil => prof |
|
148 case x::rest => count(rest,prof + (x.getOrElse(0.0) * share), share) |
|
149 } |
|
150 val balance_double = count(data(index), 0.0, share) |
|
151 println(balance_double) |
|
152 println(s"balance ${balance_double + balance}") |
|
153 println(s"profit $balance_double") |
|
154 //if (balance_double >= balance_double.toLong.toDouble+0.5) (balance_double + 1).toLong else balance_double.toLong |
|
155 (balance_double.toLong + balance) |
|
156 } |
|
157 |
|
158 yearly_yield2(dd, 100, 0) |
133 |
159 |
134 // test case using the deltas calculated above |
160 // test case using the deltas calculated above |
135 //println("Task 6 yield from Google and Apple in 2010 with balance 100") |
161 //println("Task 6 yield from Google and Apple in 2010 with balance 100") |
136 |
162 |
137 //val d0 = goog_aapl_deltas(0)(0) |
163 //val d0 = goog_aapl_deltas(0)(0) |
138 //val d1 = goog_aapl_deltas(0)(1) |
164 //val d1 = goog_aapl_deltas(0)(1) |
139 //println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}") |
165 //println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}") |
140 |
166 |
141 |
167 |
142 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0) |
168 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0) |
|
169 //val goog_aapl_yield = yearly_yield2(goog_aapl_deltas, 100, 0) |
143 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n") |
170 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n") |
144 |
171 |
145 |
172 |
146 //yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) |
173 //yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) |
147 //get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum |
174 //get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum |
159 val new_balance = yearly_yield(data, balance, index) |
186 val new_balance = yearly_yield(data, balance, index) |
160 compound_yield(data, new_balance, index + 1) |
187 compound_yield(data, new_balance, index + 1) |
161 } |
188 } |
162 } |
189 } |
163 |
190 |
|
191 def compound_yield2(data: List[List[Option[Double]]], balance: Long, index: Int): Long = { |
|
192 if (index >= data.length) balance else { |
|
193 val new_balance = yearly_yield2(data, balance, index) |
|
194 compound_yield2(data, new_balance, index + 1) |
|
195 } |
|
196 } |
|
197 |
164 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
198 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
165 compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
199 compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
166 } |
200 } |
167 |
201 |
|
202 def investment2(portfolio: List[String], years: Range, start_balance: Long): Long = { |
|
203 compound_yield2(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
|
204 } |
168 |
205 |
169 |
206 |
170 //test cases for the two portfolios given above |
207 //test cases for the two portfolios given above |
171 |
208 |
172 println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100)) |
209 println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100)) |
174 |
211 |
175 |
212 |
176 } |
213 } |
177 |
214 |
178 |
215 |
179 |
216 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100) |
180 |
217 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100) |
181 |
218 |
182 |
219 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100) |
183 |
220 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100) |
184 |
221 |
185 |
222 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) |
186 |
223 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) |
187 |
224 |
188 |
225 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) |
189 |
226 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) |
190 |
227 |
191 |
228 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) |
192 |
229 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) |
193 |
230 |
194 |
231 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) |
195 |
232 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) |
196 |
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |