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