author | Christian Urban <urbanc@in.tum.de> |
Tue, 07 Nov 2017 13:08:18 +0000 | |
changeset 127 | b4def82f3f9f |
parent 125 | dcaab8068baa |
child 129 | b1a51285de7e |
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. |
127 | 14 |
\bigskip |
79 | 15 |
|
16 |
\noindent |
|
127 | 17 |
\textbf{Important:} |
18 |
||
19 |
\begin{itemize} |
|
20 |
\item Make sure the files you submit can be processed by just calling\\ |
|
21 |
\mbox{\texttt{scala <<filename.scala>>}} on the commandline. |
|
22 |
||
23 |
\item Do not use any mutable data structures in your |
|
110 | 24 |
submissions! They are not needed. This means you cannot use |
127 | 25 |
\texttt{ListBuffer}s, for example. |
26 |
||
27 |
\item Do not use \texttt{return} in your code! It has a different |
|
28 |
meaning in Scala, than in Java. |
|
29 |
||
30 |
\item Do not use \texttt{var}! This declares a mutable variable. Only |
|
31 |
use \texttt{val}! |
|
32 |
||
33 |
\item Do not use any parallel collections! No \texttt{.par} therefore! |
|
34 |
Our testing and marking infrastructure is not set up for it. |
|
35 |
\end{itemize} |
|
36 |
||
37 |
\noindent |
|
38 |
Also note that the running time of each part will be restricted to a |
|
39 |
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
|
40 |
|
6 | 41 |
|
18 | 42 |
\subsection*{Disclaimer} |
6 | 43 |
|
44 |
It should be understood that the work you submit represents |
|
127 | 45 |
your \textbf{own} effort. You have not copied from anyone else. An |
6 | 46 |
exception is the Scala code I showed during the lectures or |
47 |
uploaded to KEATS, which you can freely use.\bigskip |
|
48 |
||
49 |
||
18 | 50 |
\subsection*{Part 1 (3 Marks)} |
6 | 51 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
52 |
This part is about recursion. You are asked to implement a Scala |
18 | 53 |
program that tests examples of the \emph{$3n + 1$-conjecture}, also |
54 |
called \emph{Collatz conjecture}. This conjecture can be described as |
|
55 |
follows: Start with any positive number $n$ greater than $0$: |
|
56 |
||
57 |
\begin{itemize} |
|
58 |
\item If $n$ is even, divide it by $2$ to obtain $n / 2$. |
|
59 |
\item If $n$ is odd, multiply it by $3$ and add $1$ to obtain $3n + |
|
60 |
1$. |
|
61 |
\item Repeat this process and you will always end up with $1$. |
|
62 |
\end{itemize} |
|
63 |
||
64 |
\noindent |
|
65 |
For example if you start with $6$, respectively $9$, you obtain the |
|
66 |
series |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
67 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
68 |
\[ |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
69 |
\begin{array}{@{}l@{\hspace{5mm}}l@{}} |
18 | 70 |
6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(= 9 steps)}\\ |
71 |
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
|
72 |
\end{array} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
73 |
\] |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
74 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
75 |
\noindent |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
76 |
As you can see, the numbers go up and down like a roller-coaster, but |
18 | 77 |
curiously they seem to always terminate in $1$. The conjecture is that |
78 |
this will \emph{always} happen for every number greater than |
|
79 |
0.\footnote{While it is relatively easy to test this conjecture with |
|
80 |
particular numbers, it is an interesting open problem to |
|
81 |
\emph{prove} that the conjecture is true for \emph{all} numbers ($> |
|
82 |
0$). Paul Erd\"o{}s, a famous mathematician you might have hard |
|
83 |
about, said about this conjecture: ``Mathematics may not be ready |
|
84 |
for such problems.'' and also offered a \$500 cash prize for its |
|
85 |
solution. Jeffrey Lagarias, another mathematician, claimed that |
|
86 |
based only on known information about this problem, ``this is an |
|
87 |
extraordinarily difficult problem, completely out of reach of |
|
88 |
present day mathematics.'' There is also a |
|
89 |
\href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture |
|
90 |
(click \href{https://xkcd.com/710/}{here}). If you are able to solve |
|
91 |
this conjecture, you will definitely get famous.}\bigskip |
|
92 |
||
93 |
\noindent |
|
94 |
\textbf{Tasks (file collatz.scala):} |
|
95 |
||
96 |
\begin{itemize} |
|
97 |
\item[(1)] You are asked to implement a recursive function that |
|
98 |
calculates the number of steps needed until a series ends |
|
99 |
with $1$. In case of starting with $6$, it takes $9$ steps and in |
|
100 |
case of starting with $9$, it takes $20$ (see above). In order to |
|
101 |
try out this function with large numbers, you should use |
|
102 |
\texttt{Long} as argument type, instead of \texttt{Int}. You can |
|
103 |
assume this function will be called with numbers between $1$ and |
|
127 | 104 |
$1$ Million. \hfill[2 Marks] |
18 | 105 |
|
106 |
\item[(2)] Write a second function that takes an upper bound as |
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
it returns a pair where the first |
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
111 |
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
|
112 |
corresponding number. \hfill\mbox{[1 Mark]} |
18 | 113 |
\end{itemize} |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
114 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
115 |
\noindent |
18 | 116 |
\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
|
117 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
118 |
\begin{itemize} |
18 | 119 |
\item 1 to 10 where $9$ takes 20 steps |
120 |
\item 1 to 100 where $97$ takes 119 steps, |
|
121 |
\item 1 to 1,000 where $871$ takes 179 steps, |
|
122 |
\item 1 to 10,000 where $6,171$ takes 262 steps, |
|
123 |
\item 1 to 100,000 where $77,031$ takes 351 steps, |
|
127 | 124 |
\item 1 to 1 Million where $837,799$ takes 525 steps |
18 | 125 |
%%\item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 686 steps |
127 | 126 |
\end{itemize} |
18 | 127 |
|
127 | 128 |
\noindent |
129 |
\textbf{Hints:} useful math operators: \texttt{\%} for modulo; useful |
|
130 |
functions: \mbox{\texttt{(1\,to\,10)}} for ranges, \texttt{.toInt}, |
|
131 |
\texttt{.toList} for conversions, \texttt{List(...).max} for the |
|
132 |
maximum of a list, \texttt{List(...).indexOf(...)} for the first index of |
|
133 |
a value in a list. |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
134 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
135 |
|
127 | 136 |
|
137 |
\subsection*{Part 2 (3 Marks)} |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
138 |
|
127 | 139 |
This part is about web-scraping and list-processing in Scala. It uses |
140 |
online data about the per-capita alcohol consumption for each country |
|
141 |
(per year?), and a file with the data about the population size of |
|
142 |
each country. From this data you are supposed to estimate how many |
|
143 |
litres of pure alcohol are consumed worldwide.\bigskip |
|
18 | 144 |
|
145 |
\noindent |
|
127 | 146 |
\textbf{Tasks (file alcohol.scala):} |
18 | 147 |
|
148 |
\begin{itemize} |
|
127 | 149 |
\item[(1)] Write a function that given an URL requests a |
150 |
comma-separated value (CSV) list. We are interested in the list |
|
151 |
from the following URL |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
152 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
153 |
\begin{center} |
127 | 154 |
\url{https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv} |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
155 |
\end{center} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
156 |
|
127 | 157 |
\noindent Your function should take a string (the URL) as input, and |
158 |
produce a list of strings as output, where each string is one line in |
|
159 |
the corresponding CSV-list. This list should contain 194 lines.\medskip |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
160 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
161 |
\noindent |
127 | 162 |
Write another function that can read the file \texttt{population.csv} |
163 |
from disk (the file is distributed with the coursework). This |
|
164 |
function should take a string as argument, the file name, and again |
|
165 |
return a list of strings corresponding to each entry in the |
|
166 |
CSV-list. For \texttt{population.csv}, this list should contain 216 |
|
167 |
lines.\hfill[1 Mark] |
|
168 |
||
169 |
||
170 |
\item[(2)] Unfortunately, the CSV-lists contain a lot of ``junk'' and we |
|
171 |
need to extract the data that interests us. From the header of the |
|
172 |
alcohol list, you can see there are 5 columns |
|
173 |
||
174 |
\begin{center} |
|
175 |
\begin{tabular}{l} |
|
176 |
\texttt{country (name),}\\ |
|
177 |
\texttt{beer\_servings,}\\ |
|
178 |
\texttt{spirit\_servings,}\\ |
|
179 |
\texttt{wine\_servings,}\\ |
|
180 |
\texttt{total\_litres\_of\_pure\_alcohol} |
|
181 |
\end{tabular} |
|
182 |
\end{center} |
|
183 |
||
184 |
\noindent |
|
185 |
Write a function that extracts the data from the first column, |
|
186 |
the country name, and the data from the fifth column (converted into |
|
187 |
a \texttt{Double}). For this go through each line of the CSV-list |
|
188 |
(except the first line), use the \texttt{split(",")} function to |
|
189 |
divide each line into an array of 5 elements. Keep the data from the |
|
190 |
first and fifth element in these arrays.\medskip |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
191 |
|
127 | 192 |
\noindent |
193 |
Write another function that processes the population size list. This |
|
194 |
is already of the form country name and population size.\footnote{Your |
|
195 |
friendly lecturer already did the messy processing for you from the |
|
196 |
Worldbank database, see \url{https://github.com/datasets/population/tree/master/data}.} Again, split the |
|
197 |
strings according to the commas. However, this time generate a |
|
198 |
\texttt{Map} from country names to population sizes.\hfill[1 Mark] |
|
199 |
||
200 |
\item[(3)] In (2) you generated the data about the alcohol consumption |
|
201 |
per capita for each country, and also the population size for each |
|
202 |
country. From this generate next a sorted(!) list of the overall |
|
203 |
alcohol consumption for each country. The list should be sorted from |
|
204 |
highest alcohol consumption to lowest. The difficulty is that the |
|
205 |
data is scrapped off from ``random'' sources on the Internet and |
|
206 |
annoyingly the spelling of some country names does not always agree in the |
|
207 |
lists. For example the alcohol list contains |
|
208 |
\texttt{Bosnia-Herzegovina}, while the population writes this country as |
|
209 |
\texttt{Bosnia and Herzegovina}. In your sorted |
|
210 |
overall list include only countries from the alcohol list, whose |
|
211 |
exact country name is also in the population size list. This means |
|
212 |
you can ignore countries like Bosnia-Herzegovina from the overall |
|
213 |
alcohol consumption. There are 177 countries where the names |
|
214 |
agree. The UK is ranked 10th on this list with |
|
215 |
consuming 671,976,864 Litres of pure alcohol each year.\medskip |
|
216 |
||
217 |
\noindent |
|
218 |
Finally, write another function that takes an integer, say |
|
219 |
\texttt{n}, as argument. You can assume this integer is between 0 |
|
220 |
and 177. The function should use the sorted list from above. It returns |
|
221 |
a triple, where the first component is the sum of the alcohol |
|
222 |
consumption in all countries (on the list); the second component is |
|
223 |
the sum of the \texttt{n}-highest alcohol consumers on the list; and |
|
224 |
the third component is the percentage the \texttt{n}-highest alcohol |
|
225 |
consumers feast on with respect to the the world consumption. You will |
|
226 |
see that according to our data, 164 countries (out of 177) gobble up 100\% |
|
227 |
of the world alcohol consumption.\hfill\mbox{[1 Mark]} |
|
18 | 228 |
\end{itemize} |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
229 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
230 |
\noindent |
127 | 231 |
\textbf{Hints:} useful list functions: \texttt{.drop(n)}, |
232 |
\texttt{.take(n)} for dropping or taking some elements in a list, |
|
233 |
\texttt{.getLines} for separating lines in a string; |
|
234 |
\texttt{.sortBy(\_.\_2)} sorts a list of pairs according to the second |
|
235 |
elements in the pairs---the sorting is done from smallest to highest; |
|
236 |
useful \texttt{Map} functions: \texttt{.toMap} converts a list of |
|
237 |
pairs into a \texttt{Map}, \texttt{.isDefinedAt(k)} tests whether the |
|
238 |
map is defined at that key, that is would produce a result when |
|
239 |
called with this key. |
|
240 |
||
241 |
\newpage |
|
18 | 242 |
|
243 |
\subsection*{Advanced Part 3 (3 Marks)} |
|
244 |
||
35
9fea5f751be4
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
31
diff
changeset
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
because Mr Drumb refuses to make his tax records public). |
18 | 250 |
|
127 | 251 |
Since the question about Mr Drumb's business acumen remains open, |
252 |
let's do a quick back-of-the-envelope calculation in Scala whether his |
|
253 |
claim has any merit. Let's suppose we are given \$100 in 1978 and we |
|
254 |
follow a really dumb investment strategy, namely: |
|
18 | 255 |
|
256 |
\begin{itemize} |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
257 |
\item We blindly choose a portfolio of stocks, say some Blue-Chip stocks |
18 | 258 |
or some Real Estate stocks. |
259 |
\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
|
260 |
a year, we invest our money in equal amounts in each of these |
18 | 261 |
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
|
262 |
are traded in our portfolio, we buy \$25 worth of stocks |
18 | 263 |
from each. |
264 |
\item Next year in January, we look how our stocks did, liquidate |
|
265 |
everything, and re-invest our (hopefully) increased money in again |
|
266 |
the stocks from our portfolio (there might be more stocks available, |
|
267 |
if companies from our portfolio got listed in that year, or less if |
|
268 |
some companies went bust or de-listed). |
|
127 | 269 |
\item We do this for 38 years until January 2017 and check what would |
18 | 270 |
have become out of our \$100. |
127 | 271 |
\end{itemize} |
272 |
||
273 |
||
274 |
\medskip |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
9
diff
changeset
|
275 |
|
18 | 276 |
\noindent |
42
a5106bc13db6
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
35
diff
changeset
|
277 |
\textbf{Tasks (file drumb.scala):} |
18 | 278 |
|
279 |
\begin{itemize} |
|
280 |
\item[(1.a)] Write a function that queries the Yahoo financial data |
|
281 |
service and obtains the first trade (adjusted close price) of a |
|
282 |
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
|
283 |
is not open on 1st of January, but depending on the day of the week |
18 | 284 |
on a later day (maybe 3rd or 4th). The easiest way to solve this |
285 |
problem is to obtain the whole January data for a stock symbol as |
|
286 |
CSV-list and then select the earliest entry in this list. For this |
|
287 |
you can specify a date range with the Yahoo service. For example if |
|
288 |
you want to obtain all January data for Google in 2000, you can form |
|
289 |
the query:\mbox{}\\[-8mm] |
|
290 |
||
291 |
\begin{center}\small |
|
292 |
\mbox{\url{http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=1&e=1&f=2000}} |
|
293 |
\end{center} |
|
294 |
||
295 |
For other companies and years, you need to change the stock symbol |
|
296 |
(\texttt{GOOG}) and the year \texttt{2000} (in the \texttt{c} and |
|
297 |
\texttt{f} argument of the query). Such a request might fail, if the |
|
298 |
company does not exist during this period. For example, if you query |
|
299 |
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
|
300 |
Therefore you are asked to return a trade price as |
18 | 301 |
\texttt{Option[Double]}. |
302 |
||
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
303 |
\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
|
304 |
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
|
305 |
organise this as a list of lists of \texttt{Option[Double]}'s. The inner lists |
18 | 306 |
are for all stock symbols from the portfolio and the outer list for the years. |
307 |
For example for Google and Apple in years 2010 (first line), 2011 |
|
308 |
(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
|
309 |
|
18 | 310 |
\begin{verbatim} |
311 |
List(List(Some(313.062468), Some(27.847252)), |
|
312 |
List(Some(301.873641), Some(42.884065)), |
|
313 |
List(Some(332.373186), Some(53.509768))) |
|
314 |
\end{verbatim}\hfill[1 Mark] |
|
315 |
||
316 |
\item[(2.a)] Write a function that calculates the \emph{change factor} (delta) |
|
317 |
for how a stock price has changed from one year to the next. This is |
|
318 |
only well-defined, if the corresponding company has been traded in both |
|
319 |
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
|
320 |
|
18 | 321 |
\[ |
322 |
\frac{price_{new} - price_{old}}{price_{old}} |
|
323 |
\] |
|
324 |
||
325 |
||
326 |
\item[(2.b)] Write a function that calculates all change factors |
|
327 |
(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
|
328 |
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
|
329 |
obtain 4 change factors: |
18 | 330 |
|
331 |
\begin{verbatim} |
|
332 |
List(List(Some(-0.03573991820699504), Some(0.5399747522663995)) |
|
333 |
List(Some(0.10103414428290529), Some(0.24777742035415723))) |
|
334 |
\end{verbatim} |
|
335 |
||
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
336 |
That means Google did a bit badly in 2010, while Apple did very well. |
18 | 337 |
Both did OK in 2011.\hfill\mbox{[1 Mark]} |
338 |
||
339 |
\item[(3.a)] Write a function that calculates the ``yield'', or |
|
340 |
balance, for one year for our portfolio. This function takes the |
|
341 |
change factors, the starting balance and the year as arguments. If |
|
342 |
no company from our portfolio existed in that year, the balance is |
|
343 |
unchanged. Otherwise we invest in each existing company an equal |
|
344 |
amount of our balance. Using the change factors computed under Task |
|
345 |
2, calculate the new balance. Say we had \$100 in 2010, we would have |
|
346 |
received in our running example |
|
6 | 347 |
|
18 | 348 |
\begin{verbatim} |
349 |
$50 * -0.03573991820699504 + $50 * 0.5399747522663995 |
|
350 |
= $25.211741702970222 |
|
351 |
\end{verbatim} |
|
352 |
||
353 |
as profit for that year, and our new balance for 2011 is \$125 when |
|
354 |
converted to a \texttt{Long}. |
|
355 |
||
356 |
\item[(3.b)] Write a function that calculates the overall balance |
|
357 |
for a range of years where each year the yearly profit is compounded to |
|
358 |
the new balances and then re-invested into our portfolio.\mbox{}\hfill\mbox{[1 Mark]} |
|
359 |
\end{itemize}\medskip |
|
360 |
||
361 |
\noindent |
|
362 |
\textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios |
|
363 |
collected from the S\&P 500, one for blue-chip companies, including |
|
364 |
Facebook, Amazon and Baidu; and another for listed real-estate companies, whose |
|
365 |
names I have never heard of. Following the dumb investment strategy |
|
366 |
from 1978 until 2016 would have turned a starting balance of \$100 |
|
367 |
into \$23,794 for real estate and a whopping \$524,609 for blue chips.\medskip |
|
368 |
||
369 |
\noindent |
|
24
66b97f9a40f8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
20
diff
changeset
|
370 |
\textbf{Moral:} Reflecting on our assumptions, we are over-estimating |
18 | 371 |
our yield in many ways: first, who can know in 1978 about what will |
372 |
turn out to be a blue chip company. Also, since the portfolios are |
|
373 |
chosen from the current S\&P 500, they do not include the myriad |
|
374 |
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
|
375 |
So where does this leave our fictional character Mr T.~Drumb? Well, given |
18 | 376 |
his inheritance, a really dumb investment strategy would have done |
377 |
equally well, if not much better. |
|
6 | 378 |
\end{document} |
379 |
||
380 |
%%% Local Variables: |
|
381 |
%%% mode: latex |
|
382 |
%%% TeX-master: t |
|
383 |
%%% End: |