| 199 |      1 | // Part 2 and 3 about a really dumb investment strategy
 | 
|  |      2 | //======================================================
 | 
| 129 |      3 | 
 | 
|  |      4 | 
 | 
|  |      5 | //two test portfolios
 | 
|  |      6 | 
 | 
| 144 |      7 | val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
 | 
|  |      8 | val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
 | 
| 199 |      9 |                             "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
 | 
| 129 |     10 | 
 | 
|  |     11 | 
 | 
| 199 |     12 | // (1) The function below takes a stock symbol and a year as arguments.
 | 
|  |     13 | //     It should read the corresponding CSV-file and reads the January 
 | 
|  |     14 | //     data from the given year. The data should be collected in a list of
 | 
|  |     15 | //     strings for each line in the CSV-file.
 | 
| 135 |     16 | 
 | 
|  |     17 | import io.Source
 | 
|  |     18 | import scala.util._
 | 
| 132 |     19 | 
 | 
|  |     20 | //def get_january_data(symbol: String, year: Int) : List[String] = ...
 | 
|  |     21 | 
 | 
| 135 |     22 | 
 | 
| 199 |     23 | // (2) From the output of the get_january_data function, the next function 
 | 
|  |     24 | //     should extract the first line (if it exists) and the corresponding
 | 
|  |     25 | //     first trading price in that year with type Option[Double]. If no line 
 | 
|  |     26 | //     is generated by get_january_data then the result is None; Some if 
 | 
|  |     27 | //     there is a price.
 | 
| 135 |     28 | 
 | 
| 129 |     29 | 
 | 
|  |     30 | //def get_first_price(symbol: String, year: Int) : Option[Double] = ...
 | 
|  |     31 | 
 | 
|  |     32 | 
 | 
| 199 |     33 | // (3) Complete the function below that obtains all first prices
 | 
|  |     34 | //     for the stock symbols from a portfolio (list of strings) and 
 | 
|  |     35 | //     for the given range of years. The inner lists are for the
 | 
|  |     36 | //     stock symbols and the outer list for the years.
 | 
| 129 |     37 | 
 | 
|  |     38 | 
 | 
| 135 |     39 | //def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ...
 | 
|  |     40 | 
 | 
| 129 |     41 | 
 | 
|  |     42 | 
 | 
| 199 |     43 | 
 | 
|  |     44 | //==============================================
 | 
|  |     45 | // Do not change anything below, unless you want 
 | 
|  |     46 | // to submit the file for the advanced part 3!
 | 
|  |     47 | //==============================================
 | 
|  |     48 | 
 | 
|  |     49 | 
 | 
|  |     50 | // (4) The function below calculates the change factor (delta) between
 | 
|  |     51 | //     a price in year n and a price in year n + 1. 
 | 
| 129 |     52 | 
 | 
| 135 |     53 | //def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ...
 | 
| 129 |     54 | 
 | 
| 199 |     55 | 
 | 
|  |     56 | 
 | 
|  |     57 | // (5) The next function calculates all change factors for all prices (from a 
 | 
|  |     58 | //     portfolio). The input to this function are the nested lists created by 
 | 
|  |     59 | //     get_prices above.
 | 
|  |     60 | 
 | 
| 135 |     61 | //def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = ...
 | 
|  |     62 | 
 | 
| 129 |     63 | 
 | 
|  |     64 | 
 | 
| 199 |     65 | // (6) Write a function that given change factors, a starting balance and an index,
 | 
|  |     66 | //     calculates the yearly yield, i.e. new balance, according to our dumb investment 
 | 
|  |     67 | //     strategy. Index points to a year in the data list.
 | 
|  |     68 | 
 | 
|  |     69 | //def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ... 
 | 
| 135 |     70 | 
 | 
| 129 |     71 | 
 | 
| 199 |     72 | // (7) Write a function compound_yield that calculates the overall balance for a 
 | 
|  |     73 | //     range of years where in each year the yearly profit is compounded to the new 
 | 
|  |     74 | //     balances and then re-invested into our portfolio. For this use the function and 
 | 
|  |     75 | //     results generated under (6). The function investment calls compound_yield
 | 
|  |     76 | //     with the appropriate deltas and the first index.
 | 
| 135 |     77 | 
 | 
| 199 |     78 | //def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ... 
 | 
| 129 |     79 | 
 | 
| 135 |     80 | //def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ...
 | 
| 129 |     81 | 
 | 
|  |     82 | 
 | 
|  |     83 | 
 | 
| 199 |     84 | 
 | 
|  |     85 | //Test cases for the two portfolios given above
 | 
| 129 |     86 | 
 | 
| 199 |     87 | //println("Real data: " + investment(rstate_portfolio, 1978 to 2018, 100))
 | 
|  |     88 | //println("Blue data: " + investment(blchip_portfolio, 1978 to 2018, 100))
 | 
| 129 |     89 | 
 | 
| 199 |     90 | 
 |