author | Christian Urban <urbanc@in.tum.de> |
Sun, 05 Nov 2017 12:02:09 +0000 | |
changeset 125 | dcaab8068baa |
parent 123 | 556cd74cbba9 |
child 127 | b4def82f3f9f |
permissions | -rw-r--r-- |
6 | 1 |
\documentclass{article} |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
2 |
\usepackage{../style} |
6 | 3 |
%%\usepackage{../langs} |
4 |
||
5 |
\begin{document} |
|
6 |
||
9
48a477fdef21
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
6
diff
changeset
|
7 |
\section*{Coursework 6 (Scala)} |
31
d0caa12ab8d8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
30
diff
changeset
|
8 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
9 |
This coursework is about Scala and is worth 10\%. The first and second |
125 | 10 |
part are due on 16 November at 11pm, and the third part on 21 December |
29
fde9223a5301
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
28
diff
changeset
|
11 |
at 11pm. You are asked to implement three programs about list |
18 | 12 |
processing and recursion. The third part is more advanced and might |
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
13 |
include material you have not yet seen in the first lecture. |
28
f50c6a61a3a2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
24
diff
changeset
|
14 |
Make sure the files you submit can be processed by just calling |
79 | 15 |
\texttt{scala <<filename.scala>>}.\bigskip |
16 |
||
17 |
\noindent |
|
18 |
\textbf{Important:} Do not use any mutable data structures in your |
|
110 | 19 |
submissions! They are not needed. This means you cannot use |
79 | 20 |
\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your |
21 |
code! It has a different meaning in Scala, than in Java. |
|
123 | 22 |
Do not use \texttt{var}! This declares a mutable variable. ??? Make sure the |
79 | 23 |
functions you submit are defined on the ``top-level'' of Scala, not |
86 | 24 |
inside a class or object. Also note that the running time of |
110 | 25 |
each part will be restricted to a maximum of 360 seconds on my laptop. |
28
f50c6a61a3a2
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
24
diff
changeset
|
26 |
|
6 | 27 |
|
18 | 28 |
\subsection*{Disclaimer} |
6 | 29 |
|
30 |
It should be understood that the work you submit represents |
|
31 |
your own effort. You have not copied from anyone else. An |
|
32 |
exception is the Scala code I showed during the lectures or |
|
33 |
uploaded to KEATS, which you can freely use.\bigskip |
|
34 |
||
35 |
||
18 | 36 |
\subsection*{Part 1 (3 Marks)} |
6 | 37 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
38 |
This part is about recursion. You are asked to implement a Scala |
18 | 39 |
program that tests examples of the \emph{$3n + 1$-conjecture}, also |
40 |
called \emph{Collatz conjecture}. This conjecture can be described as |
|
41 |
follows: Start with any positive number $n$ greater than $0$: |
|
42 |
||
43 |
\begin{itemize} |
|
44 |
\item If $n$ is even, divide it by $2$ to obtain $n / 2$. |
|
45 |
\item If $n$ is odd, multiply it by $3$ and add $1$ to obtain $3n + |
|
46 |
1$. |
|
47 |
\item Repeat this process and you will always end up with $1$. |
|
48 |
\end{itemize} |
|
49 |
||
50 |
\noindent |
|
51 |
For example if you start with $6$, respectively $9$, you obtain the |
|
52 |
series |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
53 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
54 |
\[ |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
55 |
\begin{array}{@{}l@{\hspace{5mm}}l@{}} |
18 | 56 |
6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(= 9 steps)}\\ |
57 |
9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 & \text{(= 20 steps)}\\ |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
58 |
\end{array} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
59 |
\] |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
60 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
61 |
\noindent |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
62 |
As you can see, the numbers go up and down like a roller-coaster, but |
18 | 63 |
curiously they seem to always terminate in $1$. The conjecture is that |
64 |
this will \emph{always} happen for every number greater than |
|
65 |
0.\footnote{While it is relatively easy to test this conjecture with |
|
66 |
particular numbers, it is an interesting open problem to |
|
67 |
\emph{prove} that the conjecture is true for \emph{all} numbers ($> |
|
68 |
0$). Paul Erd\"o{}s, a famous mathematician you might have hard |
|
69 |
about, said about this conjecture: ``Mathematics may not be ready |
|
70 |
for such problems.'' and also offered a \$500 cash prize for its |
|
71 |
solution. Jeffrey Lagarias, another mathematician, claimed that |
|
72 |
based only on known information about this problem, ``this is an |
|
73 |
extraordinarily difficult problem, completely out of reach of |
|
74 |
present day mathematics.'' There is also a |
|
75 |
\href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture |
|
76 |
(click \href{https://xkcd.com/710/}{here}). If you are able to solve |
|
77 |
this conjecture, you will definitely get famous.}\bigskip |
|
78 |
||
79 |
\newpage |
|
80 |
\noindent |
|
81 |
\textbf{Tasks (file collatz.scala):} |
|
82 |
||
83 |
\begin{itemize} |
|
84 |
\item[(1)] You are asked to implement a recursive function that |
|
85 |
calculates the number of steps needed until a series ends |
|
86 |
with $1$. In case of starting with $6$, it takes $9$ steps and in |
|
87 |
case of starting with $9$, it takes $20$ (see above). In order to |
|
88 |
try out this function with large numbers, you should use |
|
89 |
\texttt{Long} as argument type, instead of \texttt{Int}. You can |
|
90 |
assume this function will be called with numbers between $1$ and |
|
91 |
$1$ million. \hfill[2 Marks] |
|
92 |
||
93 |
\item[(2)] Write a second function that takes an upper bound as |
|
94 |
argument and calculates the steps for all numbers in the range from |
|
20
07860dd35c2b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
18
diff
changeset
|
95 |
1 up to this bound. It returns the maximum number of steps and the |
31
d0caa12ab8d8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
30
diff
changeset
|
96 |
corresponding number that needs that many steps. More precisely |
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
97 |
it returns a pair where the first |
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
98 |
component is the number of steps and the second is the |
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
99 |
corresponding number. \hfill\mbox{[1 Mark]} |
18 | 100 |
\end{itemize} |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
101 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
102 |
\noindent |
18 | 103 |
\textbf{Test Data:} Some test ranges are: |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
104 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
105 |
\begin{itemize} |
18 | 106 |
\item 1 to 10 where $9$ takes 20 steps |
107 |
\item 1 to 100 where $97$ takes 119 steps, |
|
108 |
\item 1 to 1,000 where $871$ takes 179 steps, |
|
109 |
\item 1 to 10,000 where $6,171$ takes 262 steps, |
|
110 |
\item 1 to 100,000 where $77,031$ takes 351 steps, |
|
111 |
\item 1 to 1 million where $837,799$ takes 525 steps |
|
112 |
%%\item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 686 steps |
|
113 |
\end{itemize}\bigskip |
|
114 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
115 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
116 |
|
18 | 117 |
\subsection*{Part 2 (4 Marks)} |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
118 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
119 |
This part is about list processing---it's a variant of |
18 | 120 |
``buy-low-sell-high'' in Scala. It uses the online financial data |
121 |
service from Yahoo.\bigskip |
|
122 |
||
123 |
\noindent |
|
124 |
\textbf{Tasks (file trade.scala):} |
|
125 |
||
126 |
\begin{itemize} |
|
127 |
\item[(1)] Given a list of prices for a commodity, for example |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
128 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
129 |
\[ |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
130 |
\texttt{List(28.0, 18.0, 20.0, 26.0, 24.0)} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
131 |
\] |
6 | 132 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
133 |
\noindent |
18 | 134 |
you need to write a function that returns a pair of indices for when |
135 |
to buy and when to sell this commodity. In the example above it should |
|
136 |
return the pair $\texttt{(1, 3)}$ because at index $1$ the price is lowest and |
|
137 |
then at index $3$ the price is highest. Note the prices are given as |
|
35
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
138 |
lists of \texttt{Double}s.\newline \mbox{} \hfill[1 Mark] |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
139 |
|
18 | 140 |
\item[(2)] Write a function that requests a comma-separated value (CSV) list |
141 |
from the Yahoo websevice that provides historical data for stock |
|
142 |
indices. For example if you query the URL |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
143 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
144 |
\begin{center} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
145 |
\url{http://ichart.yahoo.com/table.csv?s=GOOG} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
146 |
\end{center} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
147 |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
148 |
\noindent where \texttt{GOOG} stands for Google's stock market symbol, |
18 | 149 |
then you will receive a CSV-list of the daily stock prices since |
20
07860dd35c2b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
18
diff
changeset
|
150 |
Google was listed. You can also try this with other stock market |
18 | 151 |
symbols, for instance AAPL, MSFT, IBM, FB, YHOO, AMZN, BIDU and so |
152 |
on. |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
153 |
|
18 | 154 |
This function should return a List of strings, where each string |
155 |
is one line in this CVS-list (representing one day's |
|
156 |
data). Note that Yahoo generates its answer such that the newest data |
|
157 |
is at the front of this list, and the oldest data is at the end. |
|
158 |
\hfill[1 Mark] |
|
159 |
||
160 |
\item[(3)] As you can see, the financial data from Yahoo is organised in 7 columns, |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
161 |
for example |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
162 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
163 |
{\small\begin{verbatim} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
164 |
Date,Open,High,Low,Close,Volume,Adj Close |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
165 |
2016-11-04,750.659973,770.359985,750.560974,762.02002,2126900,762.02002 |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
166 |
2016-11-03,767.25,769.950012,759.030029,762.130005,1914000,762.130005 |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
167 |
2016-11-02,778.200012,781.650024,763.450012,768.700012,1872400,768.700012 |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
168 |
2016-11-01,782.890015,789.48999,775.539978,783.609985,2404500,783.609985 |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
169 |
.... |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
170 |
\end{verbatim}} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
171 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
172 |
\noindent |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
173 |
Write a function that ignores the first line (the header) and then |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
174 |
extracts from each line the date (first column) and the Adjusted Close |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
175 |
price (last column). The Adjusted Close price should be converted into |
18 | 176 |
a \texttt{Double}. So the result of this function is a list of pairs where the |
177 |
first components are strings (the dates) and the second are doubles |
|
178 |
(the adjusted close prices).\newline\mbox{}\hfill\mbox{[1 Mark]} |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
179 |
|
18 | 180 |
\item[(4)] Write a function that takes a stock market symbol as |
181 |
argument (you can assume it is a valid one, like GOOG or AAPL). The |
|
182 |
function calculates the \underline{dates} when you should have |
|
183 |
bought the corresponding shares (lowest price) and when you should |
|
184 |
have sold them (highest price).\hfill\mbox{[1 Mark]} |
|
185 |
\end{itemize} |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
186 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
187 |
\noindent |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
188 |
\textbf{Test Data:} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
189 |
In case of Google, the financial data records 3077 entries starting |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
190 |
from 2004-08-19 until 2016-11-04 (which is the last entry on the day |
18 | 191 |
when I prepared the course work...namely on 6 November; remember stock |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
192 |
markets are typically closed on weekends and no financial data is |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
193 |
produced then; also I did not count the header line). The lowest |
18 | 194 |
shareprice for Google was on 2004-09-03 with \$49.95513 per share and the |
195 |
highest on 2016-10-24 with \$813.109985 per share.\bigskip |
|
196 |
||
197 |
\subsection*{Advanced Part 3 (3 Marks)} |
|
198 |
||
35
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
199 |
A purely fictional character named Mr T.~Drumb inherited in 1978 |
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
200 |
approximately 200 Million Dollar from his father. Mr Drumb prides |
20
07860dd35c2b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
18
diff
changeset
|
201 |
himself to be a brilliant business man because nowadays it is |
07860dd35c2b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
18
diff
changeset
|
202 |
estimated he is 3 Billion Dollar worth (one is not sure, of course, |
35
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
203 |
because Mr Drumb refuses to make his tax records public). |
18 | 204 |
|
35
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
205 |
Since the question about Mr Drumb's business acumen remains, let's do a |
18 | 206 |
quick back-of-the-envelope calculation in Scala whether his claim has |
207 |
any merit. Let's suppose we are given \$100 in 1978 and we follow a |
|
79 | 208 |
really dumb investment strategy, namely: |
18 | 209 |
|
210 |
\begin{itemize} |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
211 |
\item We blindly choose a portfolio of stocks, say some Blue-Chip stocks |
18 | 212 |
or some Real Estate stocks. |
213 |
\item If some of the stocks in our portfolio are traded in January of |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
214 |
a year, we invest our money in equal amounts in each of these |
18 | 215 |
stocks. For example if we have \$100 and there are four stocks that |
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
216 |
are traded in our portfolio, we buy \$25 worth of stocks |
18 | 217 |
from each. |
218 |
\item Next year in January, we look how our stocks did, liquidate |
|
219 |
everything, and re-invest our (hopefully) increased money in again |
|
220 |
the stocks from our portfolio (there might be more stocks available, |
|
221 |
if companies from our portfolio got listed in that year, or less if |
|
222 |
some companies went bust or de-listed). |
|
223 |
\item We do this for 38 years until January 2016 and check what would |
|
224 |
have become out of our \$100. |
|
225 |
\end{itemize}\medskip |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
226 |
|
18 | 227 |
\noindent |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
35
diff
changeset
|
228 |
\textbf{Tasks (file drumb.scala):} |
18 | 229 |
|
230 |
\begin{itemize} |
|
231 |
\item[(1.a)] Write a function that queries the Yahoo financial data |
|
232 |
service and obtains the first trade (adjusted close price) of a |
|
233 |
stock symbol and a year. A problem is that normally a stock exchange |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
234 |
is not open on 1st of January, but depending on the day of the week |
18 | 235 |
on a later day (maybe 3rd or 4th). The easiest way to solve this |
236 |
problem is to obtain the whole January data for a stock symbol as |
|
237 |
CSV-list and then select the earliest entry in this list. For this |
|
238 |
you can specify a date range with the Yahoo service. For example if |
|
239 |
you want to obtain all January data for Google in 2000, you can form |
|
240 |
the query:\mbox{}\\[-8mm] |
|
241 |
||
242 |
\begin{center}\small |
|
243 |
\mbox{\url{http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=1&e=1&f=2000}} |
|
244 |
\end{center} |
|
245 |
||
246 |
For other companies and years, you need to change the stock symbol |
|
247 |
(\texttt{GOOG}) and the year \texttt{2000} (in the \texttt{c} and |
|
248 |
\texttt{f} argument of the query). Such a request might fail, if the |
|
249 |
company does not exist during this period. For example, if you query |
|
250 |
for Google in January of 1980, then clearly Google did not exists yet. |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
251 |
Therefore you are asked to return a trade price as |
18 | 252 |
\texttt{Option[Double]}. |
253 |
||
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
254 |
\item[(1.b)] Write a function that takes a portfolio (a list of stock symbols), |
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
255 |
a years range and gets all the first trading prices for each year. You should |
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
256 |
organise this as a list of lists of \texttt{Option[Double]}'s. The inner lists |
18 | 257 |
are for all stock symbols from the portfolio and the outer list for the years. |
258 |
For example for Google and Apple in years 2010 (first line), 2011 |
|
259 |
(second line) and 2012 (third line) you obtain: |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
260 |
|
18 | 261 |
\begin{verbatim} |
262 |
List(List(Some(313.062468), Some(27.847252)), |
|
263 |
List(Some(301.873641), Some(42.884065)), |
|
264 |
List(Some(332.373186), Some(53.509768))) |
|
265 |
\end{verbatim}\hfill[1 Mark] |
|
266 |
||
267 |
\item[(2.a)] Write a function that calculates the \emph{change factor} (delta) |
|
268 |
for how a stock price has changed from one year to the next. This is |
|
269 |
only well-defined, if the corresponding company has been traded in both |
|
270 |
years. In this case you can calculate |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
271 |
|
18 | 272 |
\[ |
273 |
\frac{price_{new} - price_{old}}{price_{old}} |
|
274 |
\] |
|
275 |
||
276 |
||
277 |
\item[(2.b)] Write a function that calculates all change factors |
|
278 |
(deltas) for the prices we obtained under Task 1. For the running |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
279 |
example of Google and Apple for the years 2010 to 2012 you should |
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
280 |
obtain 4 change factors: |
18 | 281 |
|
282 |
\begin{verbatim} |
|
283 |
List(List(Some(-0.03573991820699504), Some(0.5399747522663995)) |
|
284 |
List(Some(0.10103414428290529), Some(0.24777742035415723))) |
|
285 |
\end{verbatim} |
|
286 |
||
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
287 |
That means Google did a bit badly in 2010, while Apple did very well. |
18 | 288 |
Both did OK in 2011.\hfill\mbox{[1 Mark]} |
289 |
||
290 |
\item[(3.a)] Write a function that calculates the ``yield'', or |
|
291 |
balance, for one year for our portfolio. This function takes the |
|
292 |
change factors, the starting balance and the year as arguments. If |
|
293 |
no company from our portfolio existed in that year, the balance is |
|
294 |
unchanged. Otherwise we invest in each existing company an equal |
|
295 |
amount of our balance. Using the change factors computed under Task |
|
296 |
2, calculate the new balance. Say we had \$100 in 2010, we would have |
|
297 |
received in our running example |
|
6 | 298 |
|
18 | 299 |
\begin{verbatim} |
300 |
$50 * -0.03573991820699504 + $50 * 0.5399747522663995 |
|
301 |
= $25.211741702970222 |
|
302 |
\end{verbatim} |
|
303 |
||
304 |
as profit for that year, and our new balance for 2011 is \$125 when |
|
305 |
converted to a \texttt{Long}. |
|
306 |
||
307 |
\item[(3.b)] Write a function that calculates the overall balance |
|
308 |
for a range of years where each year the yearly profit is compounded to |
|
309 |
the new balances and then re-invested into our portfolio.\mbox{}\hfill\mbox{[1 Mark]} |
|
310 |
\end{itemize}\medskip |
|
311 |
||
312 |
\noindent |
|
313 |
\textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios |
|
314 |
collected from the S\&P 500, one for blue-chip companies, including |
|
315 |
Facebook, Amazon and Baidu; and another for listed real-estate companies, whose |
|
316 |
names I have never heard of. Following the dumb investment strategy |
|
317 |
from 1978 until 2016 would have turned a starting balance of \$100 |
|
318 |
into \$23,794 for real estate and a whopping \$524,609 for blue chips.\medskip |
|
319 |
||
320 |
\noindent |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
321 |
\textbf{Moral:} Reflecting on our assumptions, we are over-estimating |
18 | 322 |
our yield in many ways: first, who can know in 1978 about what will |
323 |
turn out to be a blue chip company. Also, since the portfolios are |
|
324 |
chosen from the current S\&P 500, they do not include the myriad |
|
325 |
of companies that went bust or were de-listed over the years. |
|
35
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
326 |
So where does this leave our fictional character Mr T.~Drumb? Well, given |
18 | 327 |
his inheritance, a really dumb investment strategy would have done |
328 |
equally well, if not much better. |
|
6 | 329 |
\end{document} |
330 |
||
331 |
%%% Local Variables: |
|
332 |
%%% mode: latex |
|
333 |
%%% TeX-master: t |
|
334 |
%%% End: |