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