cws/cw01.tex
changeset 196 c50b074b3047
parent 195 fc3ac7b70a06
child 197 c3e39fdeea3b
equal deleted inserted replaced
195:fc3ac7b70a06 196:c50b074b3047
     6 \begin{document}
     6 \begin{document}
     7 
     7 
     8 \section*{Coursework 6 (Scala)}
     8 \section*{Coursework 6 (Scala)}
     9  
     9  
    10 This coursework is about Scala and is worth 10\%. The first and second
    10 This coursework is about Scala and is worth 10\%. The first and second
    11 part are due on 15 November at 11pm, and the third part on 20 December
    11 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
    12 at 11pm. You are asked to implement three programs about list
    13 processing and recursion. The third part is more advanced and might
    13 processing and recursion. The third part is more advanced and might
    14 include material you have not yet seen in the lecture.
    14 include material you have not yet seen in the first lecture.
    15 Make sure the files you submit can be processed by just calling
    15 \bigskip
    16 \texttt{scala <<filename.scala>>}.\bigskip
       
    17 
    16 
    18 \IMPORTANT{}
    17 \IMPORTANT{}
    19 
    18 
    20 
       
    21 \noindent
    19 \noindent
    22 Also note that the running time of each part will be restricted to a
    20 Also note that the running time of each part will be restricted to a
    23 maximum of 60 seconds on my laptop.
    21 maximum of 360 seconds on my laptop.
    24 
    22 
    25 
    23 \DISCLAIMER{}
    26 \subsection*{Disclaimer}
       
    27 
       
    28 It should be understood that the work you submit represents
       
    29 your own effort. You have not copied from anyone else. An
       
    30 exception is the Scala code I showed during the lectures or
       
    31 uploaded to KEATS, which you can freely use. If you want to
       
    32 look up anything from the Scala library, the link to the api
       
    33 is
       
    34 
       
    35 \begin{center}
       
    36 \url{http://www.scala-lang.org/api/current/}
       
    37 \end{center}  
       
    38 
       
    39 \noindent
       
    40 I usually google for things like \texttt{scala} \texttt{library} \texttt{groupBy}
       
    41 and click on the link about the Scala Standard Library for more information.
       
    42 \bigskip
       
    43 
    24 
    44 
    25 
    45 \subsection*{Part 1 (3 Marks)}
    26 \subsection*{Part 1 (3 Marks)}
    46 
    27 
    47 This part is about recursion. You are asked to implement a Scala
    28 This part is about recursion. You are asked to implement a Scala
    71 As you can see, the numbers go up and down like a roller-coaster, but
    52 As you can see, the numbers go up and down like a roller-coaster, but
    72 curiously they seem to always terminate in $1$. The conjecture is that
    53 curiously they seem to always terminate in $1$. The conjecture is that
    73 this will \emph{always} happen for every number greater than
    54 this will \emph{always} happen for every number greater than
    74 0.\footnote{While it is relatively easy to test this conjecture with
    55 0.\footnote{While it is relatively easy to test this conjecture with
    75   particular numbers, it is an interesting open problem to
    56   particular numbers, it is an interesting open problem to
    76   \emph{prove} that the conjecture is true for \emph{all} numbers
    57   \emph{prove} that the conjecture is true for \emph{all} numbers ($>
    77   ($> 0$). Paul Erd\"o{}s, a famous mathematician you might have hard
    58   0$). Paul Erd\"o{}s, a famous mathematician you might have hard
    78   about, is quoted to have said about this conjecture: ``Mathematics
    59   about, said about this conjecture: ``Mathematics may not be ready
    79   is not yet ripe enough for such questions.''  and also offered a
    60   for such problems.'' and also offered a \$500 cash prize for its
    80   \$500 cash prize for its solution. Jeffrey Lagarias, another
    61   solution. Jeffrey Lagarias, another mathematician, claimed that
    81   mathematician, claimed that based only on known information about
    62   based only on known information about this problem, ``this is an
    82   this problem, ``this is an extraordinarily difficult problem,
    63   extraordinarily difficult problem, completely out of reach of
    83   completely out of reach of present day mathematics.'' There is also
    64   present day mathematics.'' There is also a
    84   a \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
    65   \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture
    85   (click \href{https://xkcd.com/710/}{here}). If you are able to solve
    66   (click \href{https://xkcd.com/710/}{here}). If you are able to solve
    86   this conjecture, you will definitely get famous.}\bigskip
    67   this conjecture, you will definitely get famous.}\bigskip
    87 
    68 
    88 \newpage
       
    89 \noindent
    69 \noindent
    90 \textbf{Tasks (file collatz.scala):}
    70 \textbf{Tasks (file collatz.scala):}
    91 
    71 
    92 \begin{itemize}
    72 \begin{itemize}
    93 \item[(1)] You are asked to implement a recursive function that
    73 \item[(1)] You are asked to implement a recursive function that
    95   with $1$. In case of starting with $6$, it takes $9$ steps and in
    75   with $1$. In case of starting with $6$, it takes $9$ steps and in
    96   case of starting with $9$, it takes $20$ (see above). In order to
    76   case of starting with $9$, it takes $20$ (see above). In order to
    97   try out this function with large numbers, you should use
    77   try out this function with large numbers, you should use
    98   \texttt{Long} as argument type, instead of \texttt{Int}.  You can
    78   \texttt{Long} as argument type, instead of \texttt{Int}.  You can
    99   assume this function will be called with numbers between $1$ and
    79   assume this function will be called with numbers between $1$ and
   100   $1$ million. \hfill[2 Marks]
    80   $1$ Million. \hfill[2 Marks]
   101 
    81 
   102 \item[(2)] Write a second function that takes an upper bound as
    82 \item[(2)] Write a second function that takes an upper bound as
   103   argument and calculates the steps for all numbers in the range from
    83   argument and calculates the steps for all numbers in the range from
   104   1 up to this bound. It returns the maximum number of steps and the
    84   1 up to this bound. It returns the maximum number of steps and the
   105   corresponding number that needs that many steps.  More precisely
    85   corresponding number that needs that many steps.  More precisely
   115 \item 1 to 10 where $9$ takes 20 steps 
    95 \item 1 to 10 where $9$ takes 20 steps 
   116 \item 1 to 100 where $97$ takes 119 steps,
    96 \item 1 to 100 where $97$ takes 119 steps,
   117 \item 1 to 1,000 where $871$ takes 179 steps,
    97 \item 1 to 1,000 where $871$ takes 179 steps,
   118 \item 1 to 10,000 where $6,171$ takes 262 steps,
    98 \item 1 to 10,000 where $6,171$ takes 262 steps,
   119 \item 1 to 100,000 where $77,031$ takes 351 steps, 
    99 \item 1 to 100,000 where $77,031$ takes 351 steps, 
   120 \item 1 to 1 million where $837,799$ takes 525 steps
   100 \item 1 to 1 Million where $837,799$ takes 525 steps
   121   %%\item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 686 steps
   101   %%\item[$\bullet$] $1 - 10$ million where $8,400,511$ takes 686 steps
   122 \end{itemize}\bigskip
   102 \end{itemize}
   123   
   103   
   124 
   104 \noindent
   125 
   105 \textbf{Hints:} useful math operators: \texttt{\%} for modulo; useful
   126 \subsection*{Part 2 (4 Marks)}
   106 functions: \mbox{\texttt{(1\,to\,10)}} for ranges, \texttt{.toInt},
   127 
   107 \texttt{.toList} for conversions, \texttt{List(...).max} for the
   128 This part is about list processing---it's a variant of
   108 maximum of a list, \texttt{List(...).indexOf(...)} for the first index of
   129 ``buy-low-sell-high'' in Scala. It uses the online financial data
   109 a value in a list.
   130 service from Yahoo.\bigskip 
   110 
   131 
   111 
   132 \noindent
   112 
   133 \textbf{Tasks (file trade.scala):}
   113 \subsection*{Part 2 (3 Marks)}
   134 
   114 
   135 \begin{itemize}
   115 This part is about web-scraping and list-processing in Scala. It uses
   136 \item[(1)] Given a list of prices for a commodity, for example
   116 online data about the per-capita alcohol consumption for each country
   137 
   117 (per year?), and a file containing the data about the population size of
   138 \[
   118 each country.  From this data you are supposed to estimate how many
   139 \texttt{List(28.0, 18.0, 20.0, 26.0, 24.0)}
   119 litres of pure alcohol are consumed worldwide.\bigskip
   140 \]
   120 
   141 
   121 \noindent
   142 \noindent
   122 \textbf{Tasks (file alcohol.scala):}
   143 you need to write a function that returns a pair of indices for when
   123 
   144 to buy and when to sell this commodity. In the example above it should
   124 \begin{itemize}
   145 return the pair $\texttt{(1, 3)}$ because at index $1$ the price is lowest and
   125 \item[(1)] Write a function that given an URL requests a
   146 then at index $3$ the price is highest. Note the prices are given as
   126   comma-separated value (CSV) list.  We are interested in the list
   147 lists of \texttt{Double}s.\newline \mbox{} \hfill[1 Mark]
   127   from the following URL
   148 
       
   149 \item[(2)] Write a function that requests a comma-separated value (CSV) list
       
   150   from the Yahoo websevice that provides historical data for stock
       
   151   indices. For example if you query the URL
       
   152 
   128 
   153 \begin{center}
   129 \begin{center}
   154 \url{http://ichart.yahoo.com/table.csv?s=GOOG}
   130   \url{https://raw.githubusercontent.com/fivethirtyeight/data/master/alcohol-consumption/drinks.csv}
   155 \end{center}
   131 \end{center}
   156 
   132 
   157 \noindent where \texttt{GOOG} stands for Google's stock market symbol,
   133 \noindent Your function should take a string (the URL) as input, and
   158 then you will receive a CSV-list of the daily stock prices since
   134 produce a list of strings as output, where each string is one line in
   159 Google was listed. You can also try this with other stock market
   135 the corresponding CSV-list.  This list from the URL above should
   160 symbols, for instance AAPL, MSFT, IBM, FB, YHOO, AMZN, BIDU and so
   136 contain 194 lines.\medskip
   161 on. 
   137 
   162 
   138 \noindent
   163 This function should return a List of strings, where each string
   139 Write another function that can read the file \texttt{population.csv}
   164 is one line in this CVS-list (representing one day's
   140 from disk (the file is distributed with the coursework). This
   165 data). Note that Yahoo generates its answer such that the newest data
   141 function should take a string as argument, the file name, and again
   166 is at the front of this list, and the oldest data is at the end.
   142 return a list of strings corresponding to each entry in the
   167 \hfill[1 Mark]
   143 CSV-list. For \texttt{population.csv}, this list should contain 216
   168 
   144 lines.\hfill[1 Mark]
   169 \item[(3)] As you can see, the financial data from Yahoo is organised in 7 columns,
   145 
   170 for example
   146 
   171 
   147 \item[(2)] Unfortunately, the CSV-lists contain a lot of ``junk'' and we
   172 {\small\begin{verbatim}
   148   need to extract the data that interests us.  From the header of the
   173 Date,Open,High,Low,Close,Volume,Adj Close
   149   alcohol list, you can see there are 5 columns
   174 2016-11-04,750.659973,770.359985,750.560974,762.02002,2126900,762.02002
   150   
   175 2016-11-03,767.25,769.950012,759.030029,762.130005,1914000,762.130005
   151   \begin{center}
   176 2016-11-02,778.200012,781.650024,763.450012,768.700012,1872400,768.700012
   152     \begin{tabular}{l}
   177 2016-11-01,782.890015,789.48999,775.539978,783.609985,2404500,783.609985
   153       \texttt{country (name),}\\
   178 ....
   154       \texttt{beer\_servings,}\\
   179 \end{verbatim}}
   155       \texttt{spirit\_servings,}\\
   180 
   156       \texttt{wine\_servings,}\\
   181 \noindent
   157       \texttt{total\_litres\_of\_pure\_alcohol}
   182 Write a function that ignores the first line (the header) and then
   158     \end{tabular}  
   183 extracts from each line the date (first column) and the Adjusted Close
   159   \end{center}
   184 price (last column). The Adjusted Close price should be converted into
   160 
   185 a \texttt{Double}. So the result of this function is a list of pairs where the
   161   \noindent
   186 first components are strings (the dates) and the second are doubles
   162   Write a function that extracts the data from the first column,
   187 (the adjusted close prices).\newline\mbox{}\hfill\mbox{[1 Mark]}
   163   the country name, and the data from the fifth column (converted into
   188 
   164   a \texttt{Double}). For this go through each line of the CSV-list
   189 \item[(4)] Write a function that takes a stock market symbol as
   165   (except the first line), use the \texttt{split(",")} function to
   190   argument (you can assume it is a valid one, like GOOG or AAPL). The
   166   divide each line into an array of 5 elements. Keep the data from the
   191   function calculates the \underline{dates} when you should have
   167   first and fifth element in these arrays.\medskip
   192   bought the corresponding shares (lowest price) and when you should
   168 
   193   have sold them (highest price).\hfill\mbox{[1 Mark]}
   169   \noindent
   194 \end{itemize}
   170   Write another function that processes the population size list. This
   195 
   171   is already of the form country name and population size.\footnote{Your
   196 \noindent
   172     friendly lecturer already did the messy processing for you from the
   197 \textbf{Test Data:}
   173   Worldbank database, see \url{https://github.com/datasets/population/tree/master/data} for the original.} Again, split the
   198 In case of Google, the financial data records 3077 entries starting
   174   strings according to the commas. However, this time generate a
   199 from 2004-08-19 until 2016-11-04 (which is the last entry on the day
   175   \texttt{Map} from country names to population sizes.\hfill[1 Mark]
   200 when I prepared the course work...namely on 6 November; remember stock
   176 
   201 markets are typically closed on weekends and no financial data is
   177 \item[(3)] In (2) you generated the data about the alcohol consumption
   202 produced then; also I did not count the header line). The lowest
   178   per capita for each country, and also the population size for each
   203 shareprice for Google was on 2004-09-03 with \$49.95513 per share and the
   179   country. From this generate next a sorted(!) list of the overall
   204 highest on 2016-10-24 with \$813.109985 per share.\bigskip
   180   alcohol consumption for each country. The list should be sorted from
   205 
   181   highest alcohol consumption to lowest. The difficulty is that the
   206 \subsection*{Advanced Part 3 (3 Marks)}
   182   data is scraped off from ``random'' sources on the Internet and
       
   183   annoyingly the spelling of some country names does not always agree in both
       
   184   lists. For example the alcohol list contains
       
   185   \texttt{Bosnia-Herzegovina}, while the population writes this country as
       
   186   \texttt{Bosnia and Herzegovina}. In your sorted
       
   187   overall list include only countries from the alcohol list, whose
       
   188   exact country name is also in the population size list. This means
       
   189   you can ignore countries like Bosnia-Herzegovina from the overall
       
   190   alcohol consumption. There are 177 countries where the names
       
   191   agree. The UK is ranked 10th on this list by
       
   192   consuming 671,976,864 Litres of pure alcohol each year.\medskip
       
   193   
       
   194   \noindent
       
   195   Finally, write another function that takes an integer, say
       
   196   \texttt{n}, as argument. You can assume this integer is between 0
       
   197   and 177 (the number of countries in the sorted list above).  The
       
   198   function should return a triple, where the first component is the
       
   199   sum of the alcohol consumption in all countries (on the list); the
       
   200   second component is the sum of the \texttt{n}-highest alcohol
       
   201   consumers on the list; and the third component is the percentage the
       
   202   \texttt{n}-highest alcohol consumers drink with respect to the
       
   203   the world consumption. You will see that according to our data, 164
       
   204   countries (out of 177) gobble up 100\% of the World alcohol
       
   205   consumption.\hfill\mbox{[1 Mark]}
       
   206 \end{itemize}
       
   207 
       
   208 \noindent
       
   209 \textbf{Hints:} useful list functions: \texttt{.drop(n)},
       
   210 \texttt{.take(n)} for dropping or taking some elements in a list,
       
   211 \texttt{.getLines} for separating lines in a string;
       
   212 \texttt{.sortBy(\_.\_2)} sorts a list of pairs according to the second
       
   213 elements in the pairs---the sorting is done from smallest to highest;
       
   214 useful \texttt{Map} functions: \texttt{.toMap} converts a list of
       
   215 pairs into a \texttt{Map}, \texttt{.isDefinedAt(k)} tests whether the
       
   216 map is defined at that key, that is would produce a result when
       
   217 called with this key; useful data functions: \texttt{Source.fromURL},
       
   218 \texttt{Source.fromFile} for obtaining a webpage and reading a file.
       
   219 
       
   220 \newpage
       
   221 
       
   222 \subsection*{Advanced Part 3 (4 Marks)}
   207 
   223 
   208 A purely fictional character named Mr T.~Drumb inherited in 1978
   224 A purely fictional character named Mr T.~Drumb inherited in 1978
   209 approximately 200 Million Dollar from his father. Mr Drumb prides
   225 approximately 200 Million Dollar from his father. Mr Drumb prides
   210 himself to be a brilliant business man because nowadays it is
   226 himself to be a brilliant business man because nowadays it is
   211 estimated he is 3 Billion Dollar worth (one is not sure, of course,
   227 estimated he is 3 Billion Dollar worth (one is not sure, of course,
   212 because Mr Drumb refuses to make his tax records public).
   228 because Mr Drumb refuses to make his tax records public).
   213 
   229 
   214 Since the question about Mr Drumb's business acumen remains, let's do a
   230 Since the question about Mr Drumb's business acumen remains open,
   215 quick back-of-the-envelope calculation in Scala whether his claim has
   231 let's do a quick back-of-the-envelope calculation in Scala whether his
   216 any merit. Let's suppose we are given \$100 in 1978 and we follow a
   232 claim has any merit. Let's suppose we are given \$100 in 1978 and we
   217 really dumb investment strategy, namely:
   233 follow a really dumb investment strategy, namely:
   218 
   234 
   219 \begin{itemize}
   235 \begin{itemize}
   220 \item We blindly choose a portfolio of stocks, say some Blue-Chip stocks
   236 \item We blindly choose a portfolio of stocks, say some Blue-Chip stocks
   221   or some Real Estate stocks.
   237   or some Real Estate stocks.
   222 \item If some of the stocks in our portfolio are traded in January of
   238 \item If some of the stocks in our portfolio are traded in January of
   226   from each.
   242   from each.
   227 \item Next year in January, we look how our stocks did, liquidate
   243 \item Next year in January, we look how our stocks did, liquidate
   228   everything, and re-invest our (hopefully) increased money in again
   244   everything, and re-invest our (hopefully) increased money in again
   229   the stocks from our portfolio (there might be more stocks available,
   245   the stocks from our portfolio (there might be more stocks available,
   230   if companies from our portfolio got listed in that year, or less if
   246   if companies from our portfolio got listed in that year, or less if
   231   some companies went bust or de-listed).
   247   some companies went bust or were de-listed).
   232 \item We do this for 38 years until January 2016 and check what would
   248 \item We do this for 39 years until January 2017 and check what would
   233   have become out of our \$100.
   249   have become out of our \$100.
   234 \end{itemize}\medskip  
   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
   235 
   261 
   236 \noindent
   262 \noindent
   237 \textbf{Tasks (file drumb.scala):}
   263 \textbf{Tasks (file drumb.scala):}
   238 
   264 
   239 \begin{itemize}
   265 \begin{itemize}
   240 \item[(1.a)] Write a function that queries the Yahoo financial data
   266 \item[(1.a)] Write a function \texttt{get\_january\_data} that takes a
   241   service and obtains the first trade (adjusted close price) of a
   267   stock symbol and a year as arguments. The function reads the
   242   stock symbol and a year. A problem is that normally a stock exchange
   268   corresponding CSV-file and returns the list of strings that start
   243   is not open on 1st of January, but depending on the day of the week
   269   with the given year (each line in the CSV-list is of the form
   244   on a later day (maybe 3rd or 4th). The easiest way to solve this
   270   \texttt{year-01-someday,someprice}).
   245   problem is to obtain the whole January data for a stock symbol as
   271 
   246   CSV-list and then select the earliest entry in this list. For this
   272 \item[(1.b)] Write a function \texttt{get\_first\_price} that takes
   247   you can specify a date range with the Yahoo service. For example if
   273   again a stock symbol and a year as arguments. It should return the
   248   you want to obtain all January data for Google in 2000, you can form
   274   first January price for the stock symbol in given the year. For this
   249   the query:\mbox{}\\[-8mm]
   275   it uses the list of strings generated by
   250 
   276   \texttt{get\_january\_data}.  A problem is that normally a stock
   251   \begin{center}\small
   277   exchange is not open on 1st of January, but depending on the day of
   252     \mbox{\url{http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=1&e=1&f=2000}}
   278   the week on a later day (maybe 3rd or 4th). The easiest way to solve
   253   \end{center}
   279   this problem is to obtain the whole January data for a stock symbol
   254 
   280   and then select the earliest, or first, entry in this list. The
   255   For other companies and years, you need to change the stock symbol
   281   stock price of this entry should be converted into a double.  Such a
   256   (\texttt{GOOG}) and the year \texttt{2000} (in the \texttt{c} and
   282   price might not exist, in case the company does not exist in the given
   257   \texttt{f} argument of the query). Such a request might fail, if the
   283   year. For example, if you query for Google in January of 1980, then
   258   company does not exist during this period. For example, if you query
   284   clearly Google did not exist yet.  Therefore you are asked to
   259   for Google in January of 1980, then clearly Google did not exists yet.
   285   return a trade price as \texttt{Option[Double]}\ldots\texttt{None}
   260   Therefore you are asked to return a trade price as
   286   will be the value for when no price exists.
   261   \texttt{Option[Double]}.
   287 
   262 
   288 \item[(1.c)] Write a function \texttt{get\_prices} that takes a
   263 \item[(1.b)] Write a function that takes a portfolio (a list of stock symbols),
   289   portfolio (a list of stock symbols), a years range and gets all the
   264   a years range and gets all the first trading prices for each year. You should
   290   first trading prices for each year in the range. You should organise
   265   organise this as a list of lists of \texttt{Option[Double]}'s. The inner lists
   291   this as a list of lists of \texttt{Option[Double]}'s. The inner
   266   are for all stock symbols from the portfolio and the outer list for the years.
   292   lists are for all stock symbols from the portfolio and the outer
   267   For example for Google and Apple in years 2010 (first line), 2011
   293   list for the years.  For example for Google and Apple in years 2010
   268   (second line) and 2012 (third line) you obtain:
   294   (first line), 2011 (second line) and 2012 (third line) you obtain:
   269 
   295 
   270 \begin{verbatim}
   296 \begin{verbatim}
   271   List(List(Some(313.062468), Some(27.847252)), 
   297   List(List(Some(311.349976), Some(27.505054)), 
   272        List(Some(301.873641), Some(42.884065)),
   298        List(Some(300.222351), Some(42.357094)),
   273        List(Some(332.373186), Some(53.509768)))
   299        List(Some(330.555054), Some(52.852215)))
   274 \end{verbatim}\hfill[1 Mark]
   300 \end{verbatim}\hfill[2 Marks]
   275  
   301  
   276 \item[(2.a)] Write a function that calculates the \emph{change factor} (delta)
   302 \item[(2.a)] Write a function that calculates the \emph{change factor} (delta)
   277   for how a stock price has changed from one year to the next. This is
   303   for how a stock price has changed from one year to the next. This is
   278   only well-defined, if the corresponding company has been traded in both
   304   only well-defined, if the corresponding company has been traded in both
   279   years. In this case you can calculate
   305   years. In this case you can calculate
   280 
   306 
   281   \[
   307   \[
   282   \frac{price_{new} - price_{old}}{price_{old}}
   308   \frac{price_{new} - price_{old}}{price_{old}}
   283   \]
   309   \]
   284 
   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}.
   285   
   314   
   286 \item[(2.b)] Write a function that calculates all change factors
   315 \item[(2.b)] Write a function that calculates all change factors
   287   (deltas) for the prices we obtained under Task 1. For the running
   316   (deltas) for the prices we obtained under Task 1. For the running
   288   example of Google and Apple for the years 2010 to 2012 you should
   317   example of Google and Apple for the years 2010 to 2012 you should
   289   obtain 4 change factors:
   318   obtain 4 change factors:
   290 
   319 
   291 \begin{verbatim}  
   320 \begin{verbatim}  
   292   List(List(Some(-0.03573991820699504), Some(0.5399747522663995))
   321   List(List(Some(-0.03573992567129673), Some(0.5399749442411563))
   293           List(Some(0.10103414428290529), Some(0.24777742035415723)))
   322         List(Some(0.10103412653643493), Some(0.2477771728154912)))
   294 \end{verbatim}
   323 \end{verbatim}
   295 
   324 
   296   That means Google did a bit badly in 2010, while Apple did very well.
   325   That means Google did a bit badly in 2010, while Apple did very well.
   297   Both did OK in 2011.\hfill\mbox{[1 Mark]}
   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]}
   298 
   330 
   299 \item[(3.a)] Write a function that calculates the ``yield'', or
   331 \item[(3.a)] Write a function that calculates the ``yield'', or
   300   balance, for one year for our portfolio.  This function takes the
   332   balance, for one year for our portfolio.  This function takes the
   301   change factors, the starting balance and the year as arguments. If
   333   change factors, the starting balance and the year as arguments. If
   302   no company from our portfolio existed in that year, the balance is
   334   no company from our portfolio existed in that year, the balance is
   303   unchanged. Otherwise we invest in each existing company an equal
   335   unchanged. Otherwise we invest in each existing company an equal
   304   amount of our balance. Using the change factors computed under Task
   336   amount of our balance. Using the change factors computed under Task
   305   2, calculate the new balance. Say we had \$100 in 2010, we would have
   337   2, calculate the new balance. Say we had \$100 in 2010, we would have
   306   received in our running example
   338   received in our running example involving Google and Apple:
   307 
   339 
   308   \begin{verbatim}
   340   \begin{verbatim}
   309   $50 * -0.03573991820699504 + $50 * 0.5399747522663995
   341   $50 * -0.03573992567129673 + $50 * 0.5399749442411563
   310                                          = $25.211741702970222
   342                                        = $25.21175092849298
   311   \end{verbatim}
   343   \end{verbatim}
   312 
   344 
   313   as profit for that year, and our new balance for 2011 is \$125 when
   345   as profit for that year, and our new balance for 2011 is \$125 when
   314   converted to a \texttt{Long}.
   346   converted to a \texttt{Long}.
   315   
   347   
   316 \item[(3.b)] Write a function that calculates the overall balance
   348 \item[(3.b)] Write a function that calculates the overall balance
   317   for a range of years where each year the yearly profit is compounded to
   349   for a range of years where each year the yearly profit is compounded to
   318   the new balances and then re-invested into our portfolio.\mbox{}\hfill\mbox{[1 Mark]}
   350   the new balances and then re-invested into our portfolio.\\
       
   351   \mbox{}\hfill\mbox{[1 Mark]}
   319 \end{itemize}\medskip  
   352 \end{itemize}\medskip  
   320 
   353 
   321 \noindent
   354 \noindent
   322 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
   355 \textbf{Test Data:} File \texttt{drumb.scala} contains two portfolios
   323 collected from the S\&P 500, one for blue-chip companies, including
   356 collected from the S\&P 500, one for blue-chip companies, including
   324 Facebook, Amazon and Baidu; and another for listed real-estate companies, whose
   357 Facebook, Amazon and Baidu; and another for listed real-estate
   325 names I have never heard of. Following the dumb investment strategy
   358 companies, whose names I have never heard of. Following the dumb
   326 from 1978 until 2016 would have turned a starting balance of \$100
   359 investment strategy from 1978 until 2017 would have turned a starting
   327 into \$23,794 for real estate and a whopping \$524,609 for blue chips.\medskip
   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 
   328 
   376 
   329 \noindent
   377 \noindent
   330 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
   378 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
   331 our yield in many ways: first, who can know in 1978 about what will
   379 our yield in many ways: first, who can know in 1978 about what will
   332 turn out to be a blue chip company.  Also, since the portfolios are
   380 turn out to be a blue chip company.  Also, since the portfolios are
   333 chosen from the current S\&P 500, they do not include the myriad
   381 chosen from the current S\&P 500, they do not include the myriad
   334 of companies that went bust or were de-listed over the years.
   382 of companies that went bust or were de-listed over the years.
   335 So where does this leave our fictional character Mr T.~Drumb? Well, given
   383 So where does this leave our fictional character Mr T.~Drumb? Well, given
   336 his inheritance, a really dumb investment strategy would have done
   384 his inheritance, a really dumb investment strategy would have done
   337 equally well, if not much better.
   385 equally well, if not much better.\medskip
   338 
   386 
   339 
   387 
   340 About rounding errors: \url{https://www.youtube.com/watch?v=pQs_wx8eoQ8}
       
   341 (PBS Infinity Series).
       
   342 \end{document}
   388 \end{document}
   343 
   389 
   344 %%% Local Variables: 
   390 %%% Local Variables: 
   345 %%% mode: latex
   391 %%% mode: latex
   346 %%% TeX-master: t
   392 %%% TeX-master: t