272 have become out of our \$100. |
273 have become out of our \$100. |
273 \end{itemize} |
274 \end{itemize} |
274 |
275 |
275 \noindent |
276 \noindent |
276 Until Yahoo was bought by Altaba this summer, historical stock market |
277 Until Yahoo was bought by Altaba this summer, historical stock market |
277 data was available online for free, but nowadays this kind of data is |
278 data for such back-of-the-envelope calculations was available online |
278 difficult to obtain unless you are prepared to pay extortionate prices |
279 for free, but nowadays this kind of data is difficult to obtain, unless |
279 or be severely rate-limited. Therefore this coursework comes with a |
280 you are prepared to pay extortionate prices or be severely |
280 number of files containing CSV-lists about stock prices of |
281 rate-limited. Therefore this coursework comes with a number of files |
281 various companies. Use these files for the following tasks.\bigskip |
282 containing CSV-lists about historical stock prices for companies of |
|
283 our portfolio. Use these files for the following tasks.\bigskip |
282 |
284 |
283 \noindent |
285 \noindent |
284 \textbf{Tasks (file drumb.scala):} |
286 \textbf{Tasks (file drumb.scala):} |
285 |
287 |
286 \begin{itemize} |
288 \begin{itemize} |
287 \item[(1.a)] Write a function \texttt{get\_january\_data} that takes a |
289 \item[(1.a)] Write a function \texttt{get\_january\_data} that takes a |
288 stock symbol and a year as argument. The function reads the |
290 stock symbol and a year as arguments. The function reads the |
289 corresponding CSV-file and returns the list of strings that start |
291 corresponding CSV-file and returns the list of strings that start |
290 with the given year (each line in the CSV-list is of the form |
292 with the given year (each line in the CSV-list is of the form |
291 \texttt{year-01-someday,someprice}. |
293 \texttt{year-01-someday,someprice}). |
292 |
294 |
293 \item[(1.b)] Write a function \texttt{get\_first\_price} that takes |
295 \item[(1.b)] Write a function \texttt{get\_first\_price} that takes |
294 again stock symbol and a year as arguments. It should return the |
296 again a stock symbol and a year as arguments. It should return the |
295 first January price for the stock symbol in the year. For this it |
297 first January price for the stock symbol in given the year. For this |
296 obtains the list of strings generated by |
298 it uses the list of strings generated by |
297 \texttt{get\_january\_data}. A problem is that normally a stock |
299 \texttt{get\_january\_data}. A problem is that normally a stock |
298 exchange is not open on 1st of January, but depending on the day of |
300 exchange is not open on 1st of January, but depending on the day of |
299 the week on a later day (maybe 3rd or 4th). The easiest way to solve |
301 the week on a later day (maybe 3rd or 4th). The easiest way to solve |
300 this problem is to obtain the whole January data for a stock symbol |
302 this problem is to obtain the whole January data for a stock symbol |
301 and then select the earliest entry in this list. This entry should |
303 and then select the earliest, or first, entry in this list. The |
302 be converted into a double. Such a price might not exist, if the |
304 stock price of this entry should be converted into a double. Such a |
303 company does not exist in the given year. For example, if you query |
305 price might not exist, in case the company does not exist in the given |
304 for Google in January of 1980, then clearly Google did not exists |
306 year. For example, if you query for Google in January of 1980, then |
305 yet. Therefore you are asked to return a trade price as |
307 clearly Google did not exists yet. Therefore you are asked to |
306 \texttt{Option[Double]}. |
308 return a trade price as \texttt{Option[Double]}\ldots\texttt{None} |
|
309 will be the value for when no price exists. |
307 |
310 |
308 \item[(1.c)] Write a function \texttt{get\_prices} that takes a |
311 \item[(1.c)] Write a function \texttt{get\_prices} that takes a |
309 portfolio (a list of stock symbols), a years range and gets all the |
312 portfolio (a list of stock symbols), a years range and gets all the |
310 first trading prices for each year. You should organise this as a |
313 first trading prices for each year in the range. You should organise |
311 list of lists of \texttt{Option[Double]}'s. The inner lists are for |
314 this as a list of lists of \texttt{Option[Double]}'s. The inner |
312 all stock symbols from the portfolio and the outer list for the |
315 lists are for all stock symbols from the portfolio and the outer |
313 years. For example for Google and Apple in years 2010 (first line), |
316 list for the years. For example for Google and Apple in years 2010 |
314 2011 (second line) and 2012 (third line) you obtain: |
317 (first line), 2011 (second line) and 2012 (third line) you obtain: |
315 |
318 |
316 \begin{verbatim} |
319 \begin{verbatim} |
317 List(List(Some(311.349976), Some(27.505054)), |
320 List(List(Some(311.349976), Some(27.505054)), |
318 List(Some(300.222351), Some(42.357094)), |
321 List(Some(300.222351), Some(42.357094)), |
319 List(Some(330.555054), Some(52.852215))) |
322 List(Some(330.555054), Some(52.852215))) |
338 List(List(Some(-0.03573992567129673), Some(0.5399749442411563)) |
341 List(List(Some(-0.03573992567129673), Some(0.5399749442411563)) |
339 List(Some(0.10103412653643493), Some(0.2477771728154912))) |
342 List(Some(0.10103412653643493), Some(0.2477771728154912))) |
340 \end{verbatim} |
343 \end{verbatim} |
341 |
344 |
342 That means Google did a bit badly in 2010, while Apple did very well. |
345 That means Google did a bit badly in 2010, while Apple did very well. |
343 Both did OK in 2011.\hfill\mbox{[1 Mark]} |
346 Both did OK in 2011.\\ |
|
347 \mbox{}\hfill\mbox{[1 Mark]} |
344 |
348 |
345 \item[(3.a)] Write a function that calculates the ``yield'', or |
349 \item[(3.a)] Write a function that calculates the ``yield'', or |
346 balance, for one year for our portfolio. This function takes the |
350 balance, for one year for our portfolio. This function takes the |
347 change factors, the starting balance and the year as arguments. If |
351 change factors, the starting balance and the year as arguments. If |
348 no company from our portfolio existed in that year, the balance is |
352 no company from our portfolio existed in that year, the balance is |
349 unchanged. Otherwise we invest in each existing company an equal |
353 unchanged. Otherwise we invest in each existing company an equal |
350 amount of our balance. Using the change factors computed under Task |
354 amount of our balance. Using the change factors computed under Task |
351 2, calculate the new balance. Say we had \$100 in 2010, we would have |
355 2, calculate the new balance. Say we had \$100 in 2010, we would have |
352 received in our running example |
356 received in our running example involving Google and Apple: |
353 |
357 |
354 \begin{verbatim} |
358 \begin{verbatim} |
355 $50 * -0.03573992567129673 + $50 * 0.5399749442411563 |
359 $50 * -0.03573992567129673 + $50 * 0.5399749442411563 |
356 = $25.21175092849298 |
360 = $25.21175092849298 |
357 \end{verbatim} |
361 \end{verbatim} |
359 as profit for that year, and our new balance for 2011 is \$125 when |
363 as profit for that year, and our new balance for 2011 is \$125 when |
360 converted to a \texttt{Long}. |
364 converted to a \texttt{Long}. |
361 |
365 |
362 \item[(3.b)] Write a function that calculates the overall balance |
366 \item[(3.b)] Write a function that calculates the overall balance |
363 for a range of years where each year the yearly profit is compounded to |
367 for a range of years where each year the yearly profit is compounded to |
364 the new balances and then re-invested into our portfolio.\mbox{}\hfill\mbox{[1 Mark]} |
368 the new balances and then re-invested into our portfolio.\\ |
|
369 \mbox{}\hfill\mbox{[1 Mark]} |
365 \end{itemize}\medskip |
370 \end{itemize}\medskip |
366 |
371 |
367 \noindent |
372 \noindent |
368 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios |
373 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios |
369 collected from the S\&P 500, one for blue-chip companies, including |
374 collected from the S\&P 500, one for blue-chip companies, including |
370 Facebook, Amazon and Baidu; and another for listed real-estate |
375 Facebook, Amazon and Baidu; and another for listed real-estate |
371 companies, whose names I have never heard of. Following the dumb |
376 companies, whose names I have never heard of. Following the dumb |
372 investment strategy from 1978 until 2017 would have turned a starting |
377 investment strategy from 1978 until 2017 would have turned a starting |
373 balance of \$100 into roughly \$30,895 for real estate and a whopping |
378 balance of \$100 into roughly \$30,839 for real estate and a whopping |
374 \$188,172 for blue chips. Note when comparing these results with your |
379 \$349,597 for blue chips. Note when comparing these results with your |
375 own results: there might be some small rounding errors, which when |
380 own calculations: there might be some small rounding errors, which |
376 compounded, lead to moderately different values.\medskip |
381 when compounded, lead to moderately different values.\bigskip |
|
382 |
|
383 \noindent |
|
384 \textbf{Hints:} useful string functions: \texttt{startsWith(...)} for |
|
385 testing a string having a given prefix, \texttt{\_ + \_} for |
|
386 concatenating twop strings; useful option functions: \texttt{.flatten} |
|
387 flattens a list of options such that it filters way all |
|
388 \texttt{None}'s, \texttt{Try(...) getOrElse ...} runs some code that |
|
389 might raise an exception, if yes, then a default value can be given; |
|
390 useful list functions: \texttt{.head} for obtaining the first element |
|
391 in a non-empty list, \texttt{.length} for the length of a |
|
392 list.\bigskip |
|
393 |
377 |
394 |
378 \noindent |
395 \noindent |
379 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating |
396 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating |
380 our yield in many ways: first, who can know in 1978 about what will |
397 our yield in many ways: first, who can know in 1978 about what will |
381 turn out to be a blue chip company. Also, since the portfolios are |
398 turn out to be a blue chip company. Also, since the portfolios are |