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