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