| 276 |      1 | % !TEX program = xelatex
 | 
| 6 |      2 | \documentclass{article}
 | 
| 423 |      3 | \usepackage{../styles/style}
 | 
| 195 |      4 | \usepackage{disclaimer}
 | 
| 423 |      5 | \usepackage{../styles/langs}
 | 
| 6 |      6 | 
 | 
| 335 |      7 | 
 | 
|  |      8 | 
 | 
| 6 |      9 | \begin{document}
 | 
|  |     10 | 
 | 
| 399 |     11 | \section*{Main Part 1 (Scala, 6 Marks)}
 | 
| 199 |     12 | 
 | 
| 460 |     13 | 
 | 
|  |     14 | \mbox{}\hfill\textit{``No matter what language you work in, programming in a}\\
 | 
|  |     15 | \mbox{}\hfill\textit{functional style provides benefits. You should do it}\\
 | 
|  |     16 | \mbox{}\hfill\textit{whenever it is convenient, and you should think hard}\\
 | 
|  |     17 | \mbox{}\hfill\textit{about the decision when it isn't convenient.''}\smallskip\\
 | 
|  |     18 | \mbox{}\hfill\textit{ --- John Carmack (\"Uber-developer of many computer games)}\bigskip\bigskip\\
 | 
|  |     19 | 
 | 
| 408 |     20 | \IMPORTANTNONE{}
 | 
| 195 |     21 | 
 | 
| 127 |     22 | \noindent
 | 
| 195 |     23 | Also note that the running time of each part will be restricted to a
 | 
| 199 |     24 | maximum of 30 seconds on my laptop.
 | 
| 192 |     25 | 
 | 
| 196 |     26 | \DISCLAIMER{}
 | 
| 6 |     27 | 
 | 
| 201 |     28 | \subsection*{Reference Implementation}
 | 
| 6 |     29 | 
 | 
| 199 |     30 | Like the C++ assignments, the Scala assignments will work like this: you
 | 
|  |     31 | push your files to GitHub and receive (after sometimes a long delay) some
 | 
|  |     32 | automated feedback. In the end we take a snapshot of the submitted files and
 | 
| 266 |     33 | apply an automated marking script to them.\medskip
 | 
| 199 |     34 | 
 | 
| 266 |     35 | \noindent
 | 
| 306 |     36 | In addition, the Scala coursework comes with a reference implementation
 | 
|  |     37 | in form of \texttt{jar}-files. This allows you to run any test cases
 | 
| 199 |     38 | on your own computer. For example you can call Scala on the command
 | 
| 335 |     39 | line with the option \texttt{-cp drumb.jar} and then query any
 | 
| 199 |     40 | function from the template file. Say you want to find out what
 | 
| 345 |     41 | the function \code{get_january_data}
 | 
|  |     42 | produces: for this you just need to prefix them with the object name
 | 
| 396 |     43 | \texttt{M1} and call them with some arguments: 
 | 
| 199 |     44 | 
 | 
|  |     45 | \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
| 345 |     46 | $ scala -cp drumb.jar
 | 
| 199 |     47 |   
 | 
| 396 |     48 | scala> M1.get_january_data("FB", 2014)
 | 
| 345 |     49 | val res2: List[String] = List(2014-01-02,54.709999,....)
 | 
| 199 |     50 | \end{lstlisting}%$
 | 
|  |     51 | 
 | 
| 201 |     52 | \subsection*{Hints}
 | 
|  |     53 | 
 | 
|  |     54 | \noindent
 | 
| 396 |     55 | \textbf{For Main Part 1:} useful string functions:
 | 
| 266 |     56 | \texttt{.startsWith(...)} for checking whether a string has a given
 | 
|  |     57 | prefix, \texttt{\_ ++ \_} for concatenating two strings; useful option
 | 
|  |     58 | functions: \texttt{.flatten} flattens a list of options such that it
 | 
|  |     59 | filters way all \texttt{None}'s, \texttt{Try(...).getOrElse ...} runs
 | 
|  |     60 | some code that might raise an exception---if yes, then a default value
 | 
|  |     61 | can be given; useful list functions: \texttt{.head} for obtaining the
 | 
|  |     62 | first element in a non-empty list, \texttt{.length} for the length of
 | 
|  |     63 | a list; \texttt{.filter(...)} for filtering out elements in a list;
 | 
|  |     64 | \texttt{.getLines.toList} for obtaining a list of lines from a file;
 | 
|  |     65 | \texttt{.split(",").toList} for splitting strings according to a
 | 
|  |     66 | comma.\bigskip
 | 
| 201 |     67 | 
 | 
| 396 |     68 | \noindent\alert
 | 
| 266 |     69 | \textbf{Note!} Fortunately Scala supports operator overloading. But
 | 
|  |     70 | make sure you understand the difference between \texttt{100 / 3} and
 | 
| 201 |     71 | \texttt{100.0 / 3}!
 | 
|  |     72 | 
 | 
|  |     73 | \newpage
 | 
| 399 |     74 | \subsection*{Main Part 1 (6 Marks, file drumb.scala)}
 | 
| 199 |     75 | 
 | 
|  |     76 | A purely fictional character named Mr T.~Drumb inherited in 1978
 | 
|  |     77 | approximately 200 Million Dollar from his father. Mr Drumb prides
 | 
|  |     78 | himself to be a brilliant business man because nowadays it is
 | 
|  |     79 | estimated he is 3 Billion Dollar worth (one is not sure, of course,
 | 
|  |     80 | because Mr Drumb refuses to make his tax records public).
 | 
|  |     81 | 
 | 
|  |     82 | Since the question about Mr Drumb's business acumen remains open,
 | 
|  |     83 | let's do a quick back-of-the-envelope calculation in Scala whether his
 | 
|  |     84 | claim has any merit. Let's suppose we are given \$100 in 1978 and we
 | 
|  |     85 | follow a really dumb investment strategy, namely:
 | 
|  |     86 | 
 | 
|  |     87 | \begin{itemize}
 | 
|  |     88 | \item We blindly choose a portfolio of stocks, say some Blue-Chip stocks
 | 
|  |     89 |   or some Real Estate stocks.
 | 
|  |     90 | \item If some of the stocks in our portfolio are traded in January of
 | 
|  |     91 |   a year, we invest our money in equal amounts in each of these
 | 
|  |     92 |   stocks.  For example if we have \$100 and there are four stocks that
 | 
|  |     93 |   are traded in our portfolio, we buy \$25 worth of stocks
 | 
| 306 |     94 |   from each. (Be careful to also test cases where you trade with 3 stocks.) 
 | 
| 199 |     95 | \item Next year in January, we look at how our stocks did, liquidate
 | 
|  |     96 |   everything, and re-invest our (hopefully) increased money in again
 | 
|  |     97 |   the stocks from our portfolio (there might be more stocks available,
 | 
|  |     98 |   if companies from our portfolio got listed in that year, or less if
 | 
|  |     99 |   some companies went bust or were de-listed).
 | 
| 276 |    100 | \item We do this for 41 years until January 2019 and check what would
 | 
| 199 |    101 |   have become out of our \$100.
 | 
|  |    102 | \end{itemize}
 | 
|  |    103 | 
 | 
|  |    104 | \noindent
 | 
| 266 |    105 | Until Yahoo was bought by Altaba a few years ago, historical stock market
 | 
| 199 |    106 | data for such back-of-the-envelope calculations was freely available
 | 
| 201 |    107 | online. Unfortunately nowadays this kind of data is more difficult to
 | 
| 199 |    108 | obtain, unless you are prepared to pay extortionate prices or be
 | 
| 306 |    109 | severely rate-limited.  Therefore this part comes with a number
 | 
| 199 |    110 | of files containing CSV-lists with the historical stock prices for the
 | 
|  |    111 | companies in our portfolios. Use these files for the following
 | 
| 396 |    112 | tasks.\medskip
 | 
|  |    113 | 
 | 
|  |    114 | \noindent\alert
 | 
|  |    115 | \textbf{Note:} Do not hardcode the path to the CSV-files. The testing
 | 
|  |    116 | framework will assume that these files are in the same directory as the
 | 
|  |    117 | drumb.scala file.
 | 
|  |    118 | \bigskip
 | 
| 199 |    119 | 
 | 
| 201 |    120 | \newpage
 | 
| 199 |    121 | \noindent
 | 
|  |    122 | \textbf{Tasks}
 | 
|  |    123 | 
 | 
|  |    124 | \begin{itemize}
 | 
|  |    125 | \item[(1)] Write a function \texttt{get\_january\_data} that takes a
 | 
|  |    126 |   stock symbol and a year as arguments. The function reads the
 | 
|  |    127 |   corresponding CSV-file and returns the list of strings that start
 | 
|  |    128 |   with the given year (each line in the CSV-list is of the form
 | 
| 399 |    129 |   \texttt{someyear-01-someday,someprice}).\hfill[0.5 Marks]
 | 
| 199 |    130 | 
 | 
|  |    131 | \item[(2)] Write a function \texttt{get\_first\_price} that takes
 | 
|  |    132 |   again a stock symbol and a year as arguments. It should return the
 | 
|  |    133 |   first January price for the stock symbol in the given year. For this
 | 
|  |    134 |   it uses the list of strings generated by
 | 
|  |    135 |   \texttt{get\_january\_data}.  A problem is that normally a stock
 | 
|  |    136 |   exchange is not open on 1st of January, but depending on the day of
 | 
|  |    137 |   the week on a later day (maybe 3rd or 4th). The easiest way to solve
 | 
|  |    138 |   this problem is to obtain the whole January data for a stock symbol
 | 
|  |    139 |   and then select the earliest, or first, entry in this list. The
 | 
|  |    140 |   stock price of this entry should be converted into a double.  Such a
 | 
|  |    141 |   price might not exist, in case the company does not exist in the given
 | 
|  |    142 |   year. For example, if you query for Google in January of 1980, then
 | 
|  |    143 |   clearly Google did not exist yet.  Therefore you are asked to
 | 
|  |    144 |   return a trade price with type \texttt{Option[Double]}\ldots\texttt{None}
 | 
|  |    145 |   will be the value for when no price exists; \texttt{Some} if  there is a
 | 
|  |    146 |   price.\hfill[1 Mark]
 | 
|  |    147 | 
 | 
|  |    148 | \item[(3)] Write a function \texttt{get\_prices} that takes a
 | 
|  |    149 |   portfolio (a list of stock symbols), a years range and gets all the
 | 
|  |    150 |   first trading prices for each year in the range. You should organise
 | 
|  |    151 |   this as a list of lists of \texttt{Option[Double]}'s. The inner
 | 
|  |    152 |   lists are for all stock symbols from the portfolio and the outer
 | 
|  |    153 |   list for the years.  For example for Google and Apple in years 2010
 | 
|  |    154 |   (first line), 2011 (second line) and 2012 (third line) you obtain:
 | 
|  |    155 | 
 | 
|  |    156 | \begin{verbatim}
 | 
| 314 |    157 |   List(List(Some(312.204773), Some(26.782711)), 
 | 
| 315 |    158 |        List(Some(301.0466),   Some(41.244694)), 
 | 
| 314 |    159 |        List(Some(331.462585), Some(51.464207))))
 | 
| 201 |    160 | \end{verbatim}\hfill[1 Mark]
 | 
| 266 |    161 | 
 | 
|  |    162 | 
 | 
|  |    163 | %\end{itemize}
 | 
| 199 |    164 | 
 | 
| 266 |    165 | %\subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)}
 | 
|  |    166 | %
 | 
|  |    167 | %\noindent
 | 
|  |    168 | %\textbf{Tasks}
 | 
| 199 |    169 | 
 | 
| 266 |    170 | %\begin{itemize}  
 | 
| 199 |    171 | 
 | 
|  |    172 | \item[(4)] Write a function that calculates the \emph{change factor} (delta)
 | 
|  |    173 |   for how a stock price has changed from one year to the next. This is
 | 
|  |    174 |   only well-defined, if the corresponding company has been traded in both
 | 
|  |    175 |   years. In this case you can calculate
 | 
|  |    176 | 
 | 
|  |    177 |   \[
 | 
|  |    178 |   \frac{price_{new} - price_{old}}{price_{old}}
 | 
|  |    179 |   \]
 | 
|  |    180 | 
 | 
|  |    181 |   If the change factor is defined, you should return it
 | 
|  |    182 |   as \texttt{Some(change\_factor)}; if not, you should return
 | 
|  |    183 |   \texttt{None}.\mbox{}\hfill\mbox{[1 Mark]}
 | 
|  |    184 |   
 | 
|  |    185 | \item[(5)] Write a function that calculates all change factors
 | 
| 266 |    186 |   (deltas) for the prices we obtained in Task (2). For the running
 | 
| 199 |    187 |   example of Google and Apple for the years 2010 to 2012 you should
 | 
|  |    188 |   obtain 4 change factors:
 | 
|  |    189 | 
 | 
| 266 |    190 | \begin{verbatim}
 | 
|  |    191 |   List(List(Some(-0.03573991804411003), Some(0.539974575389325)), 
 | 
|  |    192 |        List(Some(0.10103414222249969), Some(0.24777764141006836)))
 | 
| 199 |    193 | \end{verbatim}
 | 
|  |    194 | 
 | 
|  |    195 |   That means Google did a bit badly in 2010, while Apple did very well.
 | 
|  |    196 |   Both did OK in 2011. Make sure you handle the cases where a company is
 | 
|  |    197 |   not listed in a year. In such cases the change factor should be \texttt{None}
 | 
| 266 |    198 |   (recall Task~(4)).
 | 
| 199 |    199 |   \mbox{}\hfill\mbox{[1 Mark]}
 | 
|  |    200 | 
 | 
|  |    201 | \item[(6)] Write a function that calculates the ``yield'', or
 | 
|  |    202 |   balance, for one year for our portfolio.  This function takes the
 | 
|  |    203 |   change factors, the starting balance and the year as arguments. If
 | 
|  |    204 |   no company from our portfolio existed in that year, the balance is
 | 
|  |    205 |   unchanged. Otherwise we invest in each existing company an equal
 | 
|  |    206 |   amount of our balance. Using the change factors computed under Task
 | 
| 266 |    207 |   (2), calculate the new balance. Say we had \$100 in 2010, we would have
 | 
| 199 |    208 |   received in our running example involving Google and Apple:
 | 
|  |    209 | 
 | 
|  |    210 |   \begin{verbatim}
 | 
| 266 |    211 |   $50 * -0.03573991804411003 + $50 * 0.539974575389325
 | 
|  |    212 |                                        = $25.21173286726075
 | 
| 199 |    213 |   \end{verbatim}
 | 
|  |    214 | 
 | 
|  |    215 |   as profit for that year, and our new balance for 2011 is \$125 when
 | 
| 428 |    216 |   converted to a \texttt{Long}. Since \texttt{yearly\_yield} should
 | 
|  |    217 |   produce a \texttt{Long}, there are a number of ways how to round
 | 
|  |    218 |   doubles.  One way to do the calculation is to calculate the profit
 | 
|  |    219 |   first as \texttt{Double}, and then round the result down to a \texttt{Long}
 | 
|  |    220 |   (using \texttt{.toLong}) and add it to the balance (which is also a
 | 
|  |    221 |   \texttt{Long}).\\
 | 
|  |    222 |   \mbox{}\hfill\mbox{[1 Mark]}
 | 
| 199 |    223 |   
 | 
|  |    224 | \item[(7)] Write a function that calculates the overall balance
 | 
|  |    225 |   for a range of years where each year the yearly profit is compounded to
 | 
|  |    226 |   the new balances and then re-invested into our portfolio.
 | 
|  |    227 |   For this use the function and results generated under (6).\\
 | 
| 399 |    228 |   \mbox{}\hfill\mbox{[0.5 Marks]}
 | 
| 199 |    229 | \end{itemize}\medskip  
 | 
|  |    230 | 
 | 
|  |    231 | 
 | 
|  |    232 | 
 | 
|  |    233 | \noindent
 | 
|  |    234 | \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
 | 
|  |    235 | collected from the S\&P 500, one for blue-chip companies, including
 | 
|  |    236 | Facebook, Amazon and Baidu; and another for listed real-estate
 | 
|  |    237 | companies, whose names I have never heard of. Following the dumb
 | 
| 266 |    238 | investment strategy from 1978 until 2019 would have turned a starting
 | 
|  |    239 | balance of \$100 into roughly \$39,162 for real estate and a whopping
 | 
|  |    240 | \$462,199 for blue chips.  Note when comparing these results with your
 | 
| 199 |    241 | own calculations: there might be some small rounding errors, which
 | 
|  |    242 | when compounded lead to moderately different values.\bigskip
 | 
|  |    243 | 
 | 
|  |    244 | 
 | 
|  |    245 | \noindent
 | 
|  |    246 | \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
 | 
|  |    247 | our yield in many ways: first, who can know in 1978 about what will
 | 
|  |    248 | turn out to be a blue chip company.  Also, since the portfolios are
 | 
|  |    249 | chosen from the current S\&P 500, they do not include the myriad
 | 
|  |    250 | of companies that went bust or were de-listed over the years.
 | 
|  |    251 | So where does this leave our fictional character Mr T.~Drumb? Well, given
 | 
|  |    252 | his inheritance, a really dumb investment strategy would have done
 | 
| 428 |    253 | equally well, if not much better. Anyhow, one would assume that this
 | 
|  |    254 | guy is by now locked up in a prison and the key thrown away, but alas he
 | 
|  |    255 | is still around annoying commonsense people. What a pity.\medskip
 | 
| 199 |    256 | 
 | 
|  |    257 | \end{document}
 | 
|  |    258 | 
 |