| 
199
 | 
     1  | 
// Part 2 and 3 about a really dumb investment strategy
  | 
| 
 | 
     2  | 
//======================================================
  | 
| 
130
 | 
     3  | 
  | 
| 
199
 | 
     4  | 
//object CW6b { // for purposes of generating a jar
 | 
| 
130
 | 
     5  | 
  | 
| 
 | 
     6  | 
  | 
| 
 | 
     7  | 
//two test portfolios
  | 
| 
 | 
     8  | 
  | 
| 
160
 | 
     9  | 
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
 | 
| 
166
 | 
    10  | 
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
 | 
| 
199
 | 
    11  | 
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
  | 
| 
130
 | 
    12  | 
  | 
| 
 | 
    13  | 
import io.Source
  | 
| 
 | 
    14  | 
import scala.util._
  | 
| 
 | 
    15  | 
  | 
| 
199
 | 
    16  | 
// (1) The function below takes a stock symbol and a year as arguments.
  | 
| 
 | 
    17  | 
//     It should read the corresponding CSV-file and reads the January 
  | 
| 
 | 
    18  | 
//     data from the given year. The data should be collected in a list of
  | 
| 
 | 
    19  | 
//     strings for each line in the CSV-file.
  | 
| 
 | 
    20  | 
  | 
| 
166
 | 
    21  | 
def get_january_data(symbol: String, year: Int) : List[String] = 
  | 
| 
199
 | 
    22  | 
  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
 | 
| 
 | 
    23  | 
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
//test cases
  | 
| 
 | 
    26  | 
//blchip_portfolio.map(get_january_data(_, 2018))
  | 
| 
 | 
    27  | 
//rstate_portfolio.map(get_january_data(_, 2018))
  | 
| 
 | 
    28  | 
  | 
| 
 | 
    29  | 
//get_january_data("GOOG", 1980)
 | 
| 
 | 
    30  | 
//get_january_data("GOOG", 2010)
 | 
| 
 | 
    31  | 
//get_january_data("FB", 2014)
 | 
| 
130
 | 
    32  | 
  | 
| 
199
 | 
    33  | 
//get_january_data("PLD", 1980)
 | 
| 
 | 
    34  | 
//get_january_data("EQIX", 2010)
 | 
| 
 | 
    35  | 
//get_january_data("ESS", 2014)
 | 
| 
 | 
    36  | 
  | 
| 
 | 
    37  | 
  | 
| 
 | 
    38  | 
// (2) From the output of the get_january_data function, the next function 
  | 
| 
 | 
    39  | 
//     should extract the first line (if it exists) and the corresponding
  | 
| 
 | 
    40  | 
//     first trading price in that year with type Option[Double]. If no line 
  | 
| 
 | 
    41  | 
//     is generated by get_january_data then the result is None; Some if 
  | 
| 
 | 
    42  | 
//     there is a price.
  | 
| 
130
 | 
    43  | 
  | 
| 
 | 
    44  | 
def get_first_price(symbol: String, year: Int) : Option[Double] = {
 | 
| 
166
 | 
    45  | 
  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
  | 
| 
 | 
    46  | 
  data.map(_.split(",").toList(1).toDouble)
 | 
| 
130
 | 
    47  | 
}
  | 
| 
 | 
    48  | 
  | 
| 
199
 | 
    49  | 
//test cases
  | 
| 
 | 
    50  | 
//get_first_price("GOOG", 1980)
 | 
| 
 | 
    51  | 
//get_first_price("GOOG", 2010)
 | 
| 
 | 
    52  | 
//get_first_price("FB", 2014)
 | 
| 
 | 
    53  | 
  | 
| 
 | 
    54  | 
/*
  | 
| 
 | 
    55  | 
for (i <- 1978 to 2018) {
 | 
| 
 | 
    56  | 
  println(blchip_portfolio.map(get_first_price(_, i)))
  | 
| 
 | 
    57  | 
}
  | 
| 
 | 
    58  | 
  | 
| 
 | 
    59  | 
for (i <- 1978 to 2018) {
 | 
| 
 | 
    60  | 
  println(rstate_portfolio.map(get_first_price(_, i)))
  | 
| 
 | 
    61  | 
}
  | 
| 
 | 
    62  | 
*/ 
  | 
| 
130
 | 
    63  | 
  | 
| 
166
 | 
    64  | 
  | 
| 
199
 | 
    65  | 
// (3) Complete the function below that obtains all first prices
  | 
| 
 | 
    66  | 
//     for the stock symbols from a portfolio (list of strings) and 
  | 
| 
 | 
    67  | 
//     for the given range of years. The inner lists are for the
  | 
| 
 | 
    68  | 
//     stock symbols and the outer list for the years.
  | 
| 
166
 | 
    69  | 
  | 
| 
 | 
    70  | 
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
  | 
| 
 | 
    71  | 
  for (year <- years.toList) yield
  | 
| 
 | 
    72  | 
    for (symbol <- portfolio) yield get_first_price(symbol, year)
  | 
| 
130
 | 
    73  | 
  | 
| 
 | 
    74  | 
  | 
| 
199
 | 
    75  | 
//test cases
  | 
| 
 | 
    76  | 
//val p_fb = get_prices(List("FB"), 2012 to 2014)
 | 
| 
 | 
    77  | 
//val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
 | 
| 
 | 
    78  | 
  | 
| 
 | 
    79  | 
//val tt = get_prices(List("BIDU"), 2004 to 2008)
 | 
| 
 | 
    80  | 
  | 
| 
167
 | 
    81  | 
  | 
| 
199
 | 
    82  | 
//==============================================
  | 
| 
 | 
    83  | 
// Do not change anything below, unless you want 
  | 
| 
 | 
    84  | 
// to submit the file for the advanced part 3!
  | 
| 
 | 
    85  | 
//==============================================
  | 
| 
166
 | 
    86  | 
  | 
| 
199
 | 
    87  | 
  | 
| 
 | 
    88  | 
// (4) The function below calculates the change factor (delta) between
  | 
| 
 | 
    89  | 
//     a price in year n and a price in year n + 1. 
  | 
| 
166
 | 
    90  | 
  | 
| 
 | 
    91  | 
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
 | 
| 
 | 
    92  | 
  (price_old, price_new) match {
 | 
| 
 | 
    93  | 
    case (Some(x), Some(y)) => Some((y - x) / x)
  | 
| 
 | 
    94  | 
    case _ => None
  | 
| 
 | 
    95  | 
  }
  | 
| 
161
 | 
    96  | 
}
  | 
| 
 | 
    97  | 
  | 
| 
199
 | 
    98  | 
  | 
| 
 | 
    99  | 
// (5) The next function calculates all change factors for all prices (from a 
  | 
| 
 | 
   100  | 
//     portfolio). The input to this function are the nested lists created by 
  | 
| 
 | 
   101  | 
//     get_prices above.
  | 
| 
 | 
   102  | 
  | 
| 
166
 | 
   103  | 
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
  | 
| 
 | 
   104  | 
  for (i <- (0 until (data.length - 1)).toList) yield 
  | 
| 
 | 
   105  | 
    for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
  | 
| 
130
 | 
   106  | 
  | 
| 
 | 
   107  | 
  | 
| 
166
 | 
   108  | 
// test case using the prices calculated above
  | 
| 
199
 | 
   109  | 
//val d = get_deltas(p)
  | 
| 
 | 
   110  | 
//val ttd = get_deltas(tt)
  | 
| 
130
 | 
   111  | 
  | 
| 
 | 
   112  | 
  | 
| 
199
 | 
   113  | 
// (6) Write a function that given change factors, a starting balance and an index,
  | 
| 
 | 
   114  | 
//     calculates the yearly yield, i.e. new balance, according to our dumb investment 
  | 
| 
 | 
   115  | 
//     strategy. Index points to a year in the data list.
  | 
| 
 | 
   116  | 
  | 
| 
 | 
   117  | 
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
 | 
| 
 | 
   118  | 
  val somes = data(index).flatten
  | 
| 
166
 | 
   119  | 
  val somes_length = somes.length
  | 
| 
 | 
   120  | 
  if (somes_length == 0) balance
  | 
| 
 | 
   121  | 
  else {
 | 
| 
 | 
   122  | 
    val portion: Double = balance.toDouble / somes_length.toDouble
  | 
| 
167
 | 
   123  | 
    balance + (for (x <- somes) yield (x * portion)).sum.toLong
  | 
| 
166
 | 
   124  | 
  }
  | 
| 
 | 
   125  | 
}
  | 
| 
 | 
   126  | 
  | 
| 
199
 | 
   127  | 
  | 
| 
 | 
   128  | 
// (7) Write a function compound_yield that calculates the overall balance for a 
  | 
| 
 | 
   129  | 
//     range of years where in each year the yearly profit is compounded to the new 
  | 
| 
 | 
   130  | 
//     balances and then re-invested into our portfolio. For this use the function and 
  | 
| 
 | 
   131  | 
//     results generated under (6). The function investment calls compound_yield
  | 
| 
 | 
   132  | 
//     with the appropriate deltas and the first index.
  | 
| 
 | 
   133  | 
  | 
| 
 | 
   134  | 
  | 
| 
 | 
   135  | 
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
 | 
| 
 | 
   136  | 
  if (index >= data.length) balance else {
 | 
| 
 | 
   137  | 
    val new_balance = yearly_yield(data, balance, index)
  | 
| 
 | 
   138  | 
    compound_yield(data, new_balance, index + 1)
  | 
| 
166
 | 
   139  | 
  }
  | 
| 
 | 
   140  | 
}
  | 
| 
 | 
   141  | 
  | 
| 
 | 
   142  | 
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
 | 
| 
 | 
   143  | 
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
  | 
| 
130
 | 
   144  | 
}
  | 
| 
 | 
   145  | 
  | 
| 
166
 | 
   146  | 
  | 
| 
 | 
   147  | 
  | 
| 
 | 
   148  | 
//test cases for the two portfolios given above
  | 
| 
 | 
   149  | 
  | 
| 
199
 | 
   150  | 
//println("Real data: " + investment(rstate_portfolio, 1978 to 2018, 100))
 | 
| 
 | 
   151  | 
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2018, 100))
 | 
| 
166
 | 
   152  | 
  | 
| 
199
 | 
   153  | 
//}
  | 
| 
166
 | 
   154  | 
  | 
| 
 | 
   155  | 
  | 
| 
248
 | 
   156  | 
//val ds = get_deltas(get_prices(List("GOOG", "AAPL"), 2010 to 2012))
 | 
| 
 | 
   157  | 
//yearly_yield(ds, 100, 0)  => 125
  | 
| 
 | 
   158  | 
//yearly_yield(ds, 100, 1)  => 117
  | 
| 
199
 | 
   159  | 
  | 
| 
248
 | 
   160  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100) // => 100
 | 
| 
 | 
   161  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100) // => 27
 | 
| 
 | 
   162  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) // => 42
 | 
| 
 | 
   163  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) // => 27
 | 
| 
 | 
   164  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) // => 38
 | 
| 
 | 
   165  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) // => 113
 | 
| 
 | 
   166  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2006, 100) // => 254
 | 
| 
 | 
   167  | 
//investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2007, 100) // => 349
 |