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