cws/cw01.tex
changeset 199 54befaf23648
parent 197 c3e39fdeea3b
child 201 018b9c12ee1f
equal deleted inserted replaced
198:d59c7995bcb2 199:54befaf23648
     1 \documentclass{article}
     1 \documentclass{article}
     2 \usepackage{../style}
     2 \usepackage{../style}
     3 \usepackage{disclaimer}
     3 \usepackage{disclaimer}
     4 %%\usepackage{../langs}
     4 \usepackage{../langs}
     5 
     5 
     6 \begin{document}
     6 \begin{document}
     7 
     7 
     8 \section*{Coursework 6 (Scala)}
     8 \section*{Assignment 6 (Scala)}
     9  
     9 
    10 This coursework is about Scala and is worth 10\%. The first and second
    10 \mbox{}\hfill\textit{``The most effective debugging tool is still careful thought,}\\
       
    11 \mbox{}\hfill\textit{coupled with judiciously placed print statements.''}\smallskip\\
       
    12 \mbox{}\hfill\textit{ --- Brian W. Kernighan, in Unix for Beginners (1979)}\bigskip
       
    13 
       
    14 
       
    15 \noindent
       
    16 This assignemnt is about Scala and worth 10\%. The first and second
    11 part are due on 16 November at 11pm, and the third part on 21 December
    17 part are due on 16 November at 11pm, and the third part on 21 December
    12 at 11pm. You are asked to implement three programs about list
    18 at 11pm. You are asked to implement two programs about list
    13 processing and recursion. The third part is more advanced and might
    19 processing and recursion. The third part is more advanced and might
    14 include material you have not yet seen in the first lecture.
    20 include material you have not yet seen in the first lecture.
    15 \bigskip
    21 \bigskip
    16 
    22  
    17 \IMPORTANT{}
    23 \IMPORTANT{}
    18 
    24 
    19 \noindent
    25 \noindent
    20 Also note that the running time of each part will be restricted to a
    26 Also note that the running time of each part will be restricted to a
    21 maximum of 360 seconds on my laptop.
    27 maximum of 30 seconds on my laptop.
    22 
    28 
    23 \DISCLAIMER{}
    29 \DISCLAIMER{}
    24 
    30 
    25 
    31 \subsubsection*{Reference Implementation}
    26 \subsection*{Part 1 (3 Marks)}
    32 
       
    33 Like the C++ assignments, the Scala assignments will work like this: you
       
    34 push your files to GitHub and receive (after sometimes a long delay) some
       
    35 automated feedback. In the end we take a snapshot of the submitted files and
       
    36 apply an automated marking script to them.
       
    37 
       
    38 In addition, the Scala assignments come with a reference implementation
       
    39 in form of a \texttt{jar}-file. This allows you to run any test cases
       
    40 on your own computer. For example you can call Scala on the command
       
    41 line with the option \texttt{-cp collatz.jar} and then query any
       
    42 function from the template file. Say you want to find out what
       
    43 the functions \texttt{collatz} and \texttt{collatz\_max}
       
    44 produce: for this you just need to prefix them with the object name
       
    45 \texttt{CW6a} (and \texttt{CW6b} respectively for \texttt{drumb.jar}).
       
    46 If you want to find out what these functions produce for the argument
       
    47 \texttt{6}, you would type something like:
       
    48 
       
    49 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
       
    50 $ scala -cp collatz.jar
       
    51   
       
    52 scala> CW6a.collatz(6)
       
    53 ...
       
    54 scala> CW6a.collatz_max(6)
       
    55 ...
       
    56 \end{lstlisting}%$
       
    57 
       
    58 \subsection*{Part 1 (3 Marks, file collatz.scala)}
    27 
    59 
    28 This part is about recursion. You are asked to implement a Scala
    60 This part is about recursion. You are asked to implement a Scala
    29 program that tests examples of the \emph{$3n + 1$-conjecture}, also
    61 program that tests examples of the \emph{$3n + 1$-conjecture}, also
    30 called \emph{Collatz conjecture}. This conjecture can be described as
    62 called \emph{Collatz conjecture}. This conjecture can be described as
    31 follows: Start with any positive number $n$ greater than $0$:
    63 follows: Start with any positive number $n$ greater than $0$:
    36   1$.
    68   1$.
    37 \item Repeat this process and you will always end up with $1$.
    69 \item Repeat this process and you will always end up with $1$.
    38 \end{itemize}
    70 \end{itemize}
    39 
    71 
    40 \noindent
    72 \noindent
    41 For example if you start with $6$, respectively $9$, you obtain the
    73 For example if you start with $6$, or $9$, you obtain the
    42 series
    74 series
    43 
    75 
    44 \[
    76 \[
    45 \begin{array}{@{}l@{\hspace{5mm}}l@{}}
    77 \begin{array}{@{}l@{\hspace{5mm}}l@{}}
    46 6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(= 9 steps)}\\
    78 6, 3, 10, 5, 16, 8, 4, 2, 1 & \text{(= 8 steps)}\\
    47 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1  & \text{(= 20 steps)}\\
    79 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1  & \text{(= 19 steps)}\\
    48 \end{array}
    80 \end{array}
    49 \]
    81 \]
    50 
    82 
    51 \noindent
    83 \noindent
    52 As you can see, the numbers go up and down like a roller-coaster, but
    84 As you can see, the numbers go up and down like a roller-coaster, but
    65   \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
    97   \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
    66   (click \href{https://xkcd.com/710/}{here}). If you are able to solve
    98   (click \href{https://xkcd.com/710/}{here}). If you are able to solve
    67   this conjecture, you will definitely get famous.}\bigskip
    99   this conjecture, you will definitely get famous.}\bigskip
    68 
   100 
    69 \noindent
   101 \noindent
    70 \textbf{Tasks (file collatz.scala):}
   102 \textbf{Tasks}
    71 
   103 
    72 \begin{itemize}
   104 \begin{itemize}
    73 \item[(1)] You are asked to implement a recursive function that
   105 \item[(1)] You are asked to implement a recursive function that
    74   calculates the number of steps needed until a series ends
   106   calculates the number of steps needed until a series ends
    75   with $1$. In case of starting with $6$, it takes $9$ steps and in
   107   with $1$. In case of starting with $6$, it takes $8$ steps and in
    76   case of starting with $9$, it takes $20$ (see above). In order to
   108   case of starting with $9$, it takes $19$ (see above). In order to
    77   try out this function with large numbers, you should use
   109   try out this function with large numbers, you should use
    78   \texttt{Long} as argument type, instead of \texttt{Int}.  You can
   110   \texttt{Long} as argument type, instead of \texttt{Int}.  You can
    79   assume this function will be called with numbers between $1$ and
   111   assume this function will be called with numbers between $1$ and
    80   $1$ Million. \hfill[2 Marks]
   112   $1$ Million. \hfill[2 Marks]
    81 
   113 
    90 
   122 
    91 \noindent
   123 \noindent
    92 \textbf{Test Data:} Some test ranges are:
   124 \textbf{Test Data:} Some test ranges are:
    93 
   125 
    94 \begin{itemize}
   126 \begin{itemize}
    95 \item 1 to 10 where $9$ takes 20 steps 
   127 \item 1 to 10 where $9$ takes 19 steps 
    96 \item 1 to 100 where $97$ takes 119 steps,
   128 \item 1 to 100 where $97$ takes 118 steps,
    97 \item 1 to 1,000 where $871$ takes 179 steps,
   129 \item 1 to 1,000 where $871$ takes 178 steps,
    98 \item 1 to 10,000 where $6,171$ takes 262 steps,
   130 \item 1 to 10,000 where $6,171$ takes 261 steps,
    99 \item 1 to 100,000 where $77,031$ takes 351 steps, 
   131 \item 1 to 100,000 where $77,031$ takes 350 steps, 
   100 \item 1 to 1 Million where $837,799$ takes 525 steps
   132 \item 1 to 1 Million where $837,799$ takes 524 steps
   101   %%\item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 686 steps
   133   %% runs out of stack space
       
   134   %% \item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 685 steps
   102 \end{itemize}
   135 \end{itemize}
   103   
   136   
   104 \noindent
   137 \noindent
   105 \textbf{Hints:} useful math operators: \texttt{\%} for modulo; useful
   138 \textbf{Hints:} useful math operators: \texttt{\%} for modulo; useful
   106 functions: \mbox{\texttt{(1\,to\,10)}} for ranges, \texttt{.toInt},
   139 functions: \mbox{\texttt{(1\,to\,10)}} for ranges, \texttt{.toInt},
   108 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of
   141 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of
   109 a value in a list.
   142 a value in a list.
   110 
   143 
   111 
   144 
   112 
   145 
   113 \subsection*{Part 2 (3 Marks)}
   146 \subsection*{Part 2 (3 Marks, file drumb.scala)}
       
   147 
       
   148 A purely fictional character named Mr T.~Drumb inherited in 1978
       
   149 approximately 200 Million Dollar from his father. Mr Drumb prides
       
   150 himself to be a brilliant business man because nowadays it is
       
   151 estimated he is 3 Billion Dollar worth (one is not sure, of course,
       
   152 because Mr Drumb refuses to make his tax records public).
       
   153 
       
   154 Since the question about Mr Drumb's business acumen remains open,
       
   155 let's do a quick back-of-the-envelope calculation in Scala whether his
       
   156 claim has any merit. Let's suppose we are given \$100 in 1978 and we
       
   157 follow a really dumb investment strategy, namely:
       
   158 
       
   159 \begin{itemize}
       
   160 \item We blindly choose a portfolio of stocks, say some Blue-Chip stocks
       
   161   or some Real Estate stocks.
       
   162 \item If some of the stocks in our portfolio are traded in January of
       
   163   a year, we invest our money in equal amounts in each of these
       
   164   stocks.  For example if we have \$100 and there are four stocks that
       
   165   are traded in our portfolio, we buy \$25 worth of stocks
       
   166   from each. Be careful to also test cases where you trade with 3 stocks, for example. 
       
   167 \item Next year in January, we look at how our stocks did, liquidate
       
   168   everything, and re-invest our (hopefully) increased money in again
       
   169   the stocks from our portfolio (there might be more stocks available,
       
   170   if companies from our portfolio got listed in that year, or less if
       
   171   some companies went bust or were de-listed).
       
   172 \item We do this for 40 years until January 2018 and check what would
       
   173   have become out of our \$100.
       
   174 \end{itemize}
       
   175 
       
   176 \noindent
       
   177 Until Yahoo was bought by Altaba this summer, historical stock market
       
   178 data for such back-of-the-envelope calculations was freely available
       
   179 online. Unfortuantely nowadays this kind of data is difficult to
       
   180 obtain, unless you are prepared to pay extortionate prices or be
       
   181 severely rate-limited.  Therefore this coursework comes with a number
       
   182 of files containing CSV-lists with the historical stock prices for the
       
   183 companies in our portfolios. Use these files for the following
       
   184 tasks.\bigskip
       
   185 
       
   186 \noindent
       
   187 \textbf{Tasks}
       
   188 
       
   189 \begin{itemize}
       
   190 \item[(1)] Write a function \texttt{get\_january\_data} that takes a
       
   191   stock symbol and a year as arguments. The function reads the
       
   192   corresponding CSV-file and returns the list of strings that start
       
   193   with the given year (each line in the CSV-list is of the form
       
   194   \texttt{someyear-01-someday,someprice}).\hfill[1 Mark]
       
   195 
       
   196 \item[(2)] Write a function \texttt{get\_first\_price} that takes
       
   197   again a stock symbol and a year as arguments. It should return the
       
   198   first January price for the stock symbol in the given year. For this
       
   199   it uses the list of strings generated by
       
   200   \texttt{get\_january\_data}.  A problem is that normally a stock
       
   201   exchange is not open on 1st of January, but depending on the day of
       
   202   the week on a later day (maybe 3rd or 4th). The easiest way to solve
       
   203   this problem is to obtain the whole January data for a stock symbol
       
   204   and then select the earliest, or first, entry in this list. The
       
   205   stock price of this entry should be converted into a double.  Such a
       
   206   price might not exist, in case the company does not exist in the given
       
   207   year. For example, if you query for Google in January of 1980, then
       
   208   clearly Google did not exist yet.  Therefore you are asked to
       
   209   return a trade price with type \texttt{Option[Double]}\ldots\texttt{None}
       
   210   will be the value for when no price exists; \texttt{Some} if  there is a
       
   211   price.\hfill[1 Mark]
       
   212 
       
   213 \item[(3)] Write a function \texttt{get\_prices} that takes a
       
   214   portfolio (a list of stock symbols), a years range and gets all the
       
   215   first trading prices for each year in the range. You should organise
       
   216   this as a list of lists of \texttt{Option[Double]}'s. The inner
       
   217   lists are for all stock symbols from the portfolio and the outer
       
   218   list for the years.  For example for Google and Apple in years 2010
       
   219   (first line), 2011 (second line) and 2012 (third line) you obtain:
       
   220 
       
   221 \begin{verbatim}
       
   222   List(List(Some(311.349976), Some(20.544939)), 
       
   223        List(Some(300.222351), Some(31.638695)),
       
   224        List(Some(330.555054), Some(39.478039)))
       
   225 \end{verbatim}\hfill[1 Marks]
       
   226 \end{itemize}
       
   227 
       
   228 \subsection*{Advanced Part 3 (4 Marks, continue in file drumb.scala)}
       
   229 
       
   230 \noindent
       
   231 \textbf{Tasks}
       
   232 
       
   233 \begin{itemize}  
       
   234 \item[(4)] Write a function that calculates the \emph{change factor} (delta)
       
   235   for how a stock price has changed from one year to the next. This is
       
   236   only well-defined, if the corresponding company has been traded in both
       
   237   years. In this case you can calculate
       
   238 
       
   239   \[
       
   240   \frac{price_{new} - price_{old}}{price_{old}}
       
   241   \]
       
   242 
       
   243   If the change factor is defined, you should return it
       
   244   as \texttt{Some(change\_factor)}; if not, you should return
       
   245   \texttt{None}.\mbox{}\hfill\mbox{[1 Mark]}
       
   246   
       
   247 \item[(5)] Write a function that calculates all change factors
       
   248   (deltas) for the prices we obtained under Part 2. For the running
       
   249   example of Google and Apple for the years 2010 to 2012 you should
       
   250   obtain 4 change factors:
       
   251 
       
   252 \begin{verbatim}  
       
   253   List(List(Some(-0.03573992567129673), Some(0.539975124774038))
       
   254        List(Some(0.10103412653643493), Some(0.24777709700099845)))
       
   255 \end{verbatim}
       
   256 
       
   257   That means Google did a bit badly in 2010, while Apple did very well.
       
   258   Both did OK in 2011. Make sure you handle the cases where a company is
       
   259   not listed in a year. In such cases the change factor should be \texttt{None}
       
   260   (see~(4)).
       
   261   \mbox{}\hfill\mbox{[1 Mark]}
       
   262 
       
   263 \item[(6)] Write a function that calculates the ``yield'', or
       
   264   balance, for one year for our portfolio.  This function takes the
       
   265   change factors, the starting balance and the year as arguments. If
       
   266   no company from our portfolio existed in that year, the balance is
       
   267   unchanged. Otherwise we invest in each existing company an equal
       
   268   amount of our balance. Using the change factors computed under Task
       
   269   2, calculate the new balance. Say we had \$100 in 2010, we would have
       
   270   received in our running example involving Google and Apple:
       
   271 
       
   272   \begin{verbatim}
       
   273   $50 * -0.03573992567129673 + $50 * 0.539975124774038
       
   274                                        = $25.21175995513706
       
   275   \end{verbatim}
       
   276 
       
   277   as profit for that year, and our new balance for 2011 is \$125 when
       
   278   converted to a \texttt{Long}.\mbox{}\hfill\mbox{[1 Mark]}
       
   279   
       
   280 \item[(7)] Write a function that calculates the overall balance
       
   281   for a range of years where each year the yearly profit is compounded to
       
   282   the new balances and then re-invested into our portfolio.
       
   283   For this use the function and results generated under (6).\\
       
   284   \mbox{}\hfill\mbox{[1 Mark]}
       
   285 \end{itemize}\medskip  
       
   286 
       
   287 
       
   288 
       
   289 \noindent
       
   290 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
       
   291 collected from the S\&P 500, one for blue-chip companies, including
       
   292 Facebook, Amazon and Baidu; and another for listed real-estate
       
   293 companies, whose names I have never heard of. Following the dumb
       
   294 investment strategy from 1978 until 2018 would have turned a starting
       
   295 balance of \$100 into roughly \$101,589 for real estate and a whopping
       
   296 \$1,587,528 for blue chips.  Note when comparing these results with your
       
   297 own calculations: there might be some small rounding errors, which
       
   298 when compounded lead to moderately different values.\bigskip
       
   299 
       
   300 \noindent
       
   301 \textbf{Hints:} useful string functions: \texttt{.startsWith(...)} for
       
   302 checking whether a string has a given prefix, \texttt{\_ ++ \_} for
       
   303 concatenating two strings; useful option functions: \texttt{.flatten}
       
   304 flattens a list of options such that it filters way all
       
   305 \texttt{None}'s, \texttt{Try(...) getOrElse ...} runs some code that
       
   306 might raise an exception---if yes, then a default value can be given;
       
   307 useful list functions: \texttt{.head} for obtaining the first element
       
   308 in a non-empty list, \texttt{.length} for the length of a
       
   309 list.\bigskip
       
   310 
       
   311 
       
   312 \noindent
       
   313 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
       
   314 our yield in many ways: first, who can know in 1978 about what will
       
   315 turn out to be a blue chip company.  Also, since the portfolios are
       
   316 chosen from the current S\&P 500, they do not include the myriad
       
   317 of companies that went bust or were de-listed over the years.
       
   318 So where does this leave our fictional character Mr T.~Drumb? Well, given
       
   319 his inheritance, a really dumb investment strategy would have done
       
   320 equally well, if not much better.\medskip
       
   321 
       
   322 \end{document}
       
   323 
       
   324 \newpage
   114 
   325 
   115 This part is about web-scraping and list-processing in Scala. It uses
   326 This part is about web-scraping and list-processing in Scala. It uses
   116 online data about the per-capita alcohol consumption for each country
   327 online data about the per-capita alcohol consumption for each country
   117 (per year?), and a file containing the data about the population size of
   328 (per year?), and a file containing the data about the population size of
   118 each country.  From this data you are supposed to estimate how many
   329 each country.  From this data you are supposed to estimate how many
   217 called with this key; useful data functions: \texttt{Source.fromURL},
   428 called with this key; useful data functions: \texttt{Source.fromURL},
   218 \texttt{Source.fromFile} for obtaining a webpage and reading a file.
   429 \texttt{Source.fromFile} for obtaining a webpage and reading a file.
   219 
   430 
   220 \newpage
   431 \newpage
   221 
   432 
   222 \subsection*{Advanced Part 3 (4 Marks)}
   433 
   223 
   434 
   224 A purely fictional character named Mr T.~Drumb inherited in 1978
   435 
   225 approximately 200 Million Dollar from his father. Mr Drumb prides
   436 
   226 himself to be a brilliant business man because nowadays it is
       
   227 estimated he is 3 Billion Dollar worth (one is not sure, of course,
       
   228 because Mr Drumb refuses to make his tax records public).
       
   229 
       
   230 Since the question about Mr Drumb's business acumen remains open,
       
   231 let's do a quick back-of-the-envelope calculation in Scala whether his
       
   232 claim has any merit. Let's suppose we are given \$100 in 1978 and we
       
   233 follow a really dumb investment strategy, namely:
       
   234 
       
   235 \begin{itemize}
       
   236 \item We blindly choose a portfolio of stocks, say some Blue-Chip stocks
       
   237   or some Real Estate stocks.
       
   238 \item If some of the stocks in our portfolio are traded in January of
       
   239   a year, we invest our money in equal amounts in each of these
       
   240   stocks.  For example if we have \$100 and there are four stocks that
       
   241   are traded in our portfolio, we buy \$25 worth of stocks
       
   242   from each.
       
   243 \item Next year in January, we look how our stocks did, liquidate
       
   244   everything, and re-invest our (hopefully) increased money in again
       
   245   the stocks from our portfolio (there might be more stocks available,
       
   246   if companies from our portfolio got listed in that year, or less if
       
   247   some companies went bust or were de-listed).
       
   248 \item We do this for 39 years until January 2017 and check what would
       
   249   have become out of our \$100.
       
   250 \end{itemize}
       
   251 
       
   252 \noindent
       
   253 Until Yahoo was bought by Altaba this summer, historical stock market
       
   254 data for such back-of-the-envelope calculations was freely available
       
   255 online. Unfortuantely nowadays this kind of data is difficult to
       
   256 obtain, unless you are prepared to pay extortionate prices or be
       
   257 severely rate-limited.  Therefore this coursework comes with a number
       
   258 of files containing CSV-lists with the historical stock prices for the
       
   259 companies in our portfolios. Use these files for the following
       
   260 tasks.\bigskip
       
   261 
       
   262 \noindent
       
   263 \textbf{Tasks (file drumb.scala):}
       
   264 
       
   265 \begin{itemize}
       
   266 \item[(1.a)] Write a function \texttt{get\_january\_data} that takes a
       
   267   stock symbol and a year as arguments. The function reads the
       
   268   corresponding CSV-file and returns the list of strings that start
       
   269   with the given year (each line in the CSV-list is of the form
       
   270   \texttt{year-01-someday,someprice}).
       
   271 
       
   272 \item[(1.b)] Write a function \texttt{get\_first\_price} that takes
       
   273   again a stock symbol and a year as arguments. It should return the
       
   274   first January price for the stock symbol in the given year. For this
       
   275   it uses the list of strings generated by
       
   276   \texttt{get\_january\_data}.  A problem is that normally a stock
       
   277   exchange is not open on 1st of January, but depending on the day of
       
   278   the week on a later day (maybe 3rd or 4th). The easiest way to solve
       
   279   this problem is to obtain the whole January data for a stock symbol
       
   280   and then select the earliest, or first, entry in this list. The
       
   281   stock price of this entry should be converted into a double.  Such a
       
   282   price might not exist, in case the company does not exist in the given
       
   283   year. For example, if you query for Google in January of 1980, then
       
   284   clearly Google did not exist yet.  Therefore you are asked to
       
   285   return a trade price as \texttt{Option[Double]}\ldots\texttt{None}
       
   286   will be the value for when no price exists.
       
   287 
       
   288 \item[(1.c)] Write a function \texttt{get\_prices} that takes a
       
   289   portfolio (a list of stock symbols), a years range and gets all the
       
   290   first trading prices for each year in the range. You should organise
       
   291   this as a list of lists of \texttt{Option[Double]}'s. The inner
       
   292   lists are for all stock symbols from the portfolio and the outer
       
   293   list for the years.  For example for Google and Apple in years 2010
       
   294   (first line), 2011 (second line) and 2012 (third line) you obtain:
       
   295 
       
   296 \begin{verbatim}
       
   297   List(List(Some(311.349976), Some(27.505054)), 
       
   298        List(Some(300.222351), Some(42.357094)),
       
   299        List(Some(330.555054), Some(52.852215)))
       
   300 \end{verbatim}\hfill[2 Marks]
       
   301  
       
   302 \item[(2.a)] Write a function that calculates the \emph{change factor} (delta)
       
   303   for how a stock price has changed from one year to the next. This is
       
   304   only well-defined, if the corresponding company has been traded in both
       
   305   years. In this case you can calculate
       
   306 
       
   307   \[
       
   308   \frac{price_{new} - price_{old}}{price_{old}}
       
   309   \]
       
   310 
       
   311   If the change factor is defined, you should return it
       
   312   as \texttt{Some(change factor)}; if not, you should return
       
   313   \texttt{None}.
       
   314   
       
   315 \item[(2.b)] Write a function that calculates all change factors
       
   316   (deltas) for the prices we obtained under Task 1. For the running
       
   317   example of Google and Apple for the years 2010 to 2012 you should
       
   318   obtain 4 change factors:
       
   319 
       
   320 \begin{verbatim}  
       
   321   List(List(Some(-0.03573992567129673), Some(0.5399749442411563))
       
   322         List(Some(0.10103412653643493), Some(0.2477771728154912)))
       
   323 \end{verbatim}
       
   324 
       
   325   That means Google did a bit badly in 2010, while Apple did very well.
       
   326   Both did OK in 2011. Make sure you handle the cases where a company is
       
   327   not listed in a year. In such cases the change factor should be \texttt{None}
       
   328   (see 2.a).\\
       
   329   \mbox{}\hfill\mbox{[1 Mark]}
       
   330 
       
   331 \item[(3.a)] Write a function that calculates the ``yield'', or
       
   332   balance, for one year for our portfolio.  This function takes the
       
   333   change factors, the starting balance and the year as arguments. If
       
   334   no company from our portfolio existed in that year, the balance is
       
   335   unchanged. Otherwise we invest in each existing company an equal
       
   336   amount of our balance. Using the change factors computed under Task
       
   337   2, calculate the new balance. Say we had \$100 in 2010, we would have
       
   338   received in our running example involving Google and Apple:
       
   339 
       
   340   \begin{verbatim}
       
   341   $50 * -0.03573992567129673 + $50 * 0.5399749442411563
       
   342                                        = $25.21175092849298
       
   343   \end{verbatim}
       
   344 
       
   345   as profit for that year, and our new balance for 2011 is \$125 when
       
   346   converted to a \texttt{Long}.
       
   347   
       
   348 \item[(3.b)] Write a function that calculates the overall balance
       
   349   for a range of years where each year the yearly profit is compounded to
       
   350   the new balances and then re-invested into our portfolio.\\
       
   351   \mbox{}\hfill\mbox{[1 Mark]}
       
   352 \end{itemize}\medskip  
       
   353 
       
   354 \noindent
       
   355 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
       
   356 collected from the S\&P 500, one for blue-chip companies, including
       
   357 Facebook, Amazon and Baidu; and another for listed real-estate
       
   358 companies, whose names I have never heard of. Following the dumb
       
   359 investment strategy from 1978 until 2017 would have turned a starting
       
   360 balance of \$100 into roughly \$30,895 for real estate and a whopping
       
   361 \$349,597 for blue chips.  Note when comparing these results with your
       
   362 own calculations: there might be some small rounding errors, which
       
   363 when compounded lead to moderately different values.\bigskip
       
   364 
       
   365 \noindent
       
   366 \textbf{Hints:} useful string functions: \texttt{.startsWith(...)} for
       
   367 checking whether a string has a given prefix, \texttt{\_ ++ \_} for
       
   368 concatenating two strings; useful option functions: \texttt{.flatten}
       
   369 flattens a list of options such that it filters way all
       
   370 \texttt{None}'s, \texttt{Try(...) getOrElse ...} runs some code that
       
   371 might raise an exception---if yes, then a default value can be given;
       
   372 useful list functions: \texttt{.head} for obtaining the first element
       
   373 in a non-empty list, \texttt{.length} for the length of a
       
   374 list.\bigskip
       
   375 
       
   376 
       
   377 \noindent
       
   378 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
       
   379 our yield in many ways: first, who can know in 1978 about what will
       
   380 turn out to be a blue chip company.  Also, since the portfolios are
       
   381 chosen from the current S\&P 500, they do not include the myriad
       
   382 of companies that went bust or were de-listed over the years.
       
   383 So where does this leave our fictional character Mr T.~Drumb? Well, given
       
   384 his inheritance, a really dumb investment strategy would have done
       
   385 equally well, if not much better.\medskip
       
   386 
       
   387 
       
   388 \end{document}
       
   389 
   437 
   390 %%% Local Variables: 
   438 %%% Local Variables: 
   391 %%% mode: latex
   439 %%% mode: latex
   392 %%% TeX-master: t
   440 %%% TeX-master: t
   393 %%% End: 
   441 %%% End: