cws/cw01.tex
changeset 24 66b97f9a40f8
parent 20 07860dd35c2b
child 26 a7afc2540a88
child 28 f50c6a61a3a2
equal deleted inserted replaced
23:8ac886bb0c15 24:66b97f9a40f8
     5 \begin{document}
     5 \begin{document}
     6 
     6 
     7 \section*{Coursework 6 (Scala)}
     7 \section*{Coursework 6 (Scala)}
     8 
     8 
     9 This coursework is about Scala and is worth 10\%. The first and second
     9 This coursework is about Scala and is worth 10\%. The first and second
    10 part is due on 16 November at 11:00, and the third part on 23 November
    10 part are due on 16 November at 11:00, and the third part on 23 November
    11 at 11:00. You are asked to implement three programs about list
    11 at 11:00. You are asked to implement three programs about list
    12 processing and recursion. The third part is more advanced and might
    12 processing and recursion. The third part is more advanced and might
    13 include material you have not yet heard about in the first lecture.
    13 include material you have not yet seen in the first lecture.
    14 
    14 
    15 \subsection*{Disclaimer}
    15 \subsection*{Disclaimer}
    16 
    16 
    17 It should be understood that the work you submit represents
    17 It should be understood that the work you submit represents
    18 your own effort. You have not copied from anyone else. An
    18 your own effort. You have not copied from anyone else. An
    78   $1$ million. \hfill[2 Marks]
    78   $1$ million. \hfill[2 Marks]
    79 
    79 
    80 \item[(2)] Write a second function that takes an upper bound as
    80 \item[(2)] Write a second function that takes an upper bound as
    81   argument and calculates the steps for all numbers in the range from
    81   argument and calculates the steps for all numbers in the range from
    82   1 up to this bound. It returns the maximum number of steps and the
    82   1 up to this bound. It returns the maximum number of steps and the
    83   corresponding number that needs that many steps.  The first
    83   corresponding number that needs that many steps.  More precisely
    84   component of the pair is the number of steps and the second is the
    84   it returns a pair where the first
    85   corresponding number. \hfill[1 Mark]
    85   component is the number of steps and the second is the
       
    86   corresponding number. \hfill\mbox{[1 Mark]}
    86 \end{itemize}
    87 \end{itemize}
    87 
    88 
    88 \noindent
    89 \noindent
    89 \textbf{Test Data:} Some test ranges are:
    90 \textbf{Test Data:} Some test ranges are:
    90 
    91 
   119 \noindent
   120 \noindent
   120 you need to write a function that returns a pair of indices for when
   121 you need to write a function that returns a pair of indices for when
   121 to buy and when to sell this commodity. In the example above it should
   122 to buy and when to sell this commodity. In the example above it should
   122 return the pair $\texttt{(1, 3)}$ because at index $1$ the price is lowest and
   123 return the pair $\texttt{(1, 3)}$ because at index $1$ the price is lowest and
   123 then at index $3$ the price is highest. Note the prices are given as
   124 then at index $3$ the price is highest. Note the prices are given as
   124 lists of \texttt{Float}s. \hfill[1 Mark]
   125 lists of \texttt{Float}s.\newline \mbox{} \hfill[1 Mark]
   125 
   126 
   126 \item[(2)] Write a function that requests a comma-separated value (CSV) list
   127 \item[(2)] Write a function that requests a comma-separated value (CSV) list
   127   from the Yahoo websevice that provides historical data for stock
   128   from the Yahoo websevice that provides historical data for stock
   128   indices. For example if you query the URL
   129   indices. For example if you query the URL
   129 
   130 
   130 \begin{center}
   131 \begin{center}
   131 \url{http://ichart.yahoo.com/table.csv?s=GOOG}
   132 \url{http://ichart.yahoo.com/table.csv?s=GOOG}
   132 \end{center}
   133 \end{center}
   133 
   134 
   134 \noindent where \texttt{GOOG} stands for Google's stock market symbol
   135 \noindent where \texttt{GOOG} stands for Google's stock market symbol,
   135 then you will receive a CSV-list of the daily stock prices since
   136 then you will receive a CSV-list of the daily stock prices since
   136 Google was listed. You can also try this with other stock market
   137 Google was listed. You can also try this with other stock market
   137 symbols, for instance AAPL, MSFT, IBM, FB, YHOO, AMZN, BIDU and so
   138 symbols, for instance AAPL, MSFT, IBM, FB, YHOO, AMZN, BIDU and so
   138 on. 
   139 on. 
   139 
   140 
   186 approximately 200 Million Dollar from his father. Mr Drump prides
   187 approximately 200 Million Dollar from his father. Mr Drump prides
   187 himself to be a brilliant business man because nowadays it is
   188 himself to be a brilliant business man because nowadays it is
   188 estimated he is 3 Billion Dollar worth (one is not sure, of course,
   189 estimated he is 3 Billion Dollar worth (one is not sure, of course,
   189 because Mr Drump refuses to make his tax records public).
   190 because Mr Drump refuses to make his tax records public).
   190 
   191 
   191 The question about Mr Drump's business acumen remains. So let's do a
   192 Since the question about Mr Drump's business acumen remains, let's do a
   192 quick back-of-the-envelope calculation in Scala whether his claim has
   193 quick back-of-the-envelope calculation in Scala whether his claim has
   193 any merit. Let's suppose we are given \$100 in 1978 and we follow a
   194 any merit. Let's suppose we are given \$100 in 1978 and we follow a
   194 really dump investment strategy, namely:
   195 really dump investment strategy, namely:
   195 
   196 
   196 \begin{itemize}
   197 \begin{itemize}
   197 \item We blindly choose a portfolio of stocks, say some Blue-Chips stocks
   198 \item We blindly choose a portfolio of stocks, say some Blue-Chip stocks
   198   or some Real Estate stocks.
   199   or some Real Estate stocks.
   199 \item If some of the stocks in our portfolio are traded in January of
   200 \item If some of the stocks in our portfolio are traded in January of
   200   a year, we invest our money to equal amounts in each of these
   201   a year, we invest our money in equal amounts in each of these
   201   stocks.  For example if we have \$100 and there are four stocks that
   202   stocks.  For example if we have \$100 and there are four stocks that
   202   are traded in our portfolio, we buy in January \$25 worth of stocks
   203   are traded in our portfolio, we buy \$25 worth of stocks
   203   from each.
   204   from each.
   204 \item Next year in January, we look how our stocks did, liquidate
   205 \item Next year in January, we look how our stocks did, liquidate
   205   everything, and re-invest our (hopefully) increased money in again
   206   everything, and re-invest our (hopefully) increased money in again
   206   the stocks from our portfolio (there might be more stocks available,
   207   the stocks from our portfolio (there might be more stocks available,
   207   if companies from our portfolio got listed in that year, or less if
   208   if companies from our portfolio got listed in that year, or less if
   215 
   216 
   216 \begin{itemize}
   217 \begin{itemize}
   217 \item[(1.a)] Write a function that queries the Yahoo financial data
   218 \item[(1.a)] Write a function that queries the Yahoo financial data
   218   service and obtains the first trade (adjusted close price) of a
   219   service and obtains the first trade (adjusted close price) of a
   219   stock symbol and a year. A problem is that normally a stock exchange
   220   stock symbol and a year. A problem is that normally a stock exchange
   220   is not open on 1st of January, but depending a the day of the week
   221   is not open on 1st of January, but depending on the day of the week
   221   on a later day (maybe 3rd or 4th). The easiest way to solve this
   222   on a later day (maybe 3rd or 4th). The easiest way to solve this
   222   problem is to obtain the whole January data for a stock symbol as
   223   problem is to obtain the whole January data for a stock symbol as
   223   CSV-list and then select the earliest entry in this list. For this
   224   CSV-list and then select the earliest entry in this list. For this
   224   you can specify a date range with the Yahoo service. For example if
   225   you can specify a date range with the Yahoo service. For example if
   225   you want to obtain all January data for Google in 2000, you can form
   226   you want to obtain all January data for Google in 2000, you can form
   232   For other companies and years, you need to change the stock symbol
   233   For other companies and years, you need to change the stock symbol
   233   (\texttt{GOOG}) and the year \texttt{2000} (in the \texttt{c} and
   234   (\texttt{GOOG}) and the year \texttt{2000} (in the \texttt{c} and
   234   \texttt{f} argument of the query). Such a request might fail, if the
   235   \texttt{f} argument of the query). Such a request might fail, if the
   235   company does not exist during this period. For example, if you query
   236   company does not exist during this period. For example, if you query
   236   for Google in January of 1980, then clearly Google did not exists yet.
   237   for Google in January of 1980, then clearly Google did not exists yet.
   237   Therefore you need to return a trade price as
   238   Therefore you are asked to return a trade price as
   238   \texttt{Option[Double]}.
   239   \texttt{Option[Double]}.
   239 
   240 
   240 \item[(1.b)] Write a function that takes a portfolio (a List of stock symbols),
   241 \item[(1.b)] Write a function that takes a portfolio (a list of stock symbols),
   241   a years range and gets all the first trading prices for a year. You should
   242   a years range and gets all the first trading prices for each year. You should
   242   organise this as a list of lists of \texttt{Option[Double]}. The inner lists
   243   organise this as a list of lists of \texttt{Option[Double]}'s. The inner lists
   243   are for all stock symbols from the portfolio and the outer list for the years.
   244   are for all stock symbols from the portfolio and the outer list for the years.
   244   For example for Google and Apple in years 2010 (first line), 2011
   245   For example for Google and Apple in years 2010 (first line), 2011
   245   (second line) and 2012 (third line) you obtain:
   246   (second line) and 2012 (third line) you obtain:
   246 
   247 
   247 \begin{verbatim}
   248 \begin{verbatim}
   260   \]
   261   \]
   261 
   262 
   262   
   263   
   263 \item[(2.b)] Write a function that calculates all change factors
   264 \item[(2.b)] Write a function that calculates all change factors
   264   (deltas) for the prices we obtained under Task 1. For the running
   265   (deltas) for the prices we obtained under Task 1. For the running
   265   example of Google and Apple for the years 2010 to 2012 gives
   266   example of Google and Apple for the years 2010 to 2012 you should
   266   the 4 change factors:
   267   obtain 4 change factors:
   267 
   268 
   268 \begin{verbatim}  
   269 \begin{verbatim}  
   269   List(List(Some(-0.03573991820699504), Some(0.5399747522663995))
   270   List(List(Some(-0.03573991820699504), Some(0.5399747522663995))
   270           List(Some(0.10103414428290529), Some(0.24777742035415723)))
   271           List(Some(0.10103414428290529), Some(0.24777742035415723)))
   271 \end{verbatim}
   272 \end{verbatim}
   272 
   273 
   273   That is Google did a bit badly in 2010, while Apple did very well.
   274   That means Google did a bit badly in 2010, while Apple did very well.
   274   Both did OK in 2011.\hfill\mbox{[1 Mark]}
   275   Both did OK in 2011.\hfill\mbox{[1 Mark]}
   275 
   276 
   276 \item[(3.a)] Write a function that calculates the ``yield'', or
   277 \item[(3.a)] Write a function that calculates the ``yield'', or
   277   balance, for one year for our portfolio.  This function takes the
   278   balance, for one year for our portfolio.  This function takes the
   278   change factors, the starting balance and the year as arguments. If
   279   change factors, the starting balance and the year as arguments. If
   302 names I have never heard of. Following the dumb investment strategy
   303 names I have never heard of. Following the dumb investment strategy
   303 from 1978 until 2016 would have turned a starting balance of \$100
   304 from 1978 until 2016 would have turned a starting balance of \$100
   304 into \$23,794 for real estate and a whopping \$524,609 for blue chips.\medskip
   305 into \$23,794 for real estate and a whopping \$524,609 for blue chips.\medskip
   305 
   306 
   306 \noindent
   307 \noindent
   307 \textbf{Moral:} Reflecting on our assumptions, we are overestimating
   308 \textbf{Moral:} Reflecting on our assumptions, we are over-estimating
   308 our yield in many ways: first, who can know in 1978 about what will
   309 our yield in many ways: first, who can know in 1978 about what will
   309 turn out to be a blue chip company.  Also, since the portfolios are
   310 turn out to be a blue chip company.  Also, since the portfolios are
   310 chosen from the current S\&P 500, they do not include the myriad
   311 chosen from the current S\&P 500, they do not include the myriad
   311 of companies that went bust or were de-listed over the years.
   312 of companies that went bust or were de-listed over the years.
   312 So where does this leave our fictional character Mr T.~Drump? Well, given
   313 So where does this leave our fictional character Mr T.~Drump? Well, given