| 
161
 | 
     1  | 
// Advanced Part 3 about a really dumb investment strategy
  | 
| 
130
 | 
     2  | 
//==========================================================
  | 
| 
 | 
     3  | 
  | 
| 
 | 
     4  | 
object CW6c {
 | 
| 
 | 
     5  | 
  | 
| 
 | 
     6  | 
  | 
| 
 | 
     7  | 
//two test portfolios
  | 
| 
 | 
     8  | 
  | 
| 
160
 | 
     9  | 
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
 | 
| 
161
 | 
    10  | 
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI","DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
 | 
| 
130
 | 
    11  | 
  | 
| 
161
 | 
    12  | 
// (1.a) The function below takes a stock symbol and a year as arguments.
  | 
| 
 | 
    13  | 
//       It should read the corresponding CSV-file and read 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.
  | 
| 
130
 | 
    16  | 
  | 
| 
 | 
    17  | 
import io.Source
  | 
| 
 | 
    18  | 
import scala.util._
  | 
| 
 | 
    19  | 
  | 
| 
161
 | 
    20  | 
def get_january_data(symbol: String, year: Int) : List[String] = {
 | 
| 
 | 
    21  | 
	val file = symbol + ".csv"
  | 
| 
 | 
    22  | 
	val list = scala.io.Source.fromFile(file).mkString.split("\n").toList
 | 
| 
 | 
    23  | 
	val rx = (year.toString + ".*")
  | 
| 
 | 
    24  | 
	(for(n <- 1 to list.length -1 if(list(n) matches rx)) yield list(n)).toList
  | 
| 
 | 
    25  | 
}
  | 
| 
 | 
    26  | 
  | 
| 
 | 
    27  | 
  | 
| 
 | 
    28  | 
// (1.b) From the output of the get_january_data function, the next function 
  | 
| 
 | 
    29  | 
//       should extract the first line (if it exists) and the corresponding
  | 
| 
 | 
    30  | 
//       first trading price in that year as Option[Double]. If no line is 
  | 
| 
 | 
    31  | 
//       generated by get_january_data then the result is None
  | 
| 
130
 | 
    32  | 
  | 
| 
 | 
    33  | 
  | 
| 
 | 
    34  | 
def get_first_price(symbol: String, year: Int) : Option[Double] = {
 | 
| 
161
 | 
    35  | 
	val first_line = get_january_data(symbol, year)
  | 
| 
 | 
    36  | 
  | 
| 
 | 
    37  | 
	if(first_line.length == 0 ){
 | 
| 
 | 
    38  | 
		None
  | 
| 
 | 
    39  | 
	} else {
 | 
| 
 | 
    40  | 
	Option((first_line(0).split(",")(1)).toDouble)
 | 
| 
 | 
    41  | 
	}
  | 
| 
130
 | 
    42  | 
}
  | 
| 
 | 
    43  | 
  | 
| 
 | 
    44  | 
  | 
| 
161
 | 
    45  | 
// (1.c) Complete the function below that obtains all first prices
  | 
| 
 | 
    46  | 
//       for the stock symbols from a portfolio (list of strings) and 
  | 
| 
 | 
    47  | 
//       for the given range of years. The inner lists are for the
  | 
| 
 | 
    48  | 
//       stock symbols and the outer list for the years.
  | 
| 
130
 | 
    49  | 
  | 
| 
 | 
    50  | 
  | 
| 
161
 | 
    51  | 
def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] ={
 | 
| 
 | 
    52  | 
	(for(y <- years) yield (for(n <- 0 to portfolio.length-1) yield get_first_price(portfolio(n), y)).toList).toList
  | 
| 
 | 
    53  | 
}
  | 
| 
 | 
    54  | 
  | 
| 
130
 | 
    55  | 
  | 
| 
 | 
    56  | 
  | 
| 
161
 | 
    57  | 
// (2) The first function below calculates the change factor (dta) between
  | 
| 
 | 
    58  | 
//     a price in year n and a price in year n + 1. The second function calculates
  | 
| 
 | 
    59  | 
//     all change factors for all prices (from a portfolio). The input to this
  | 
| 
 | 
    60  | 
//     function are the nested lists created by get_prices above.
  | 
| 
130
 | 
    61  | 
  | 
| 
 | 
    62  | 
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
 | 
| 
161
 | 
    63  | 
	for( x <- price_old; y <- price_new) yield (y-x)/x
  | 
| 
130
 | 
    64  | 
}
  | 
| 
 | 
    65  | 
  | 
| 
161
 | 
    66  | 
def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = {
 | 
| 
 | 
    67  | 
	(for( n <- 1 to data.length-1) yield (for(i <- 0 to data(n).length-1) yield  get_delta(data(n-1)(i), data(n)(i))).toList).toList
  | 
| 
 | 
    68  | 
}
  | 
| 
130
 | 
    69  | 
  | 
| 
 | 
    70  | 
  | 
| 
 | 
    71  | 
  | 
| 
 | 
    72  | 
// (3) Write a function that given change factors, a starting balance and a year
  | 
| 
161
 | 
    73  | 
//     calculates the yearly yield, i.e. new balance, according to our dump investment 
  | 
| 
 | 
    74  | 
//     strategy. Another function calculates given the same data calculates the
  | 
| 
 | 
    75  | 
//     compound yield up to a given year. Finally a function combines all 
  | 
| 
 | 
    76  | 
//     calculations by taking a portfolio, a range of years and a start balance
  | 
| 
 | 
    77  | 
//     as arguments.
  | 
| 
130
 | 
    78  | 
  | 
| 
 | 
    79  | 
  | 
| 
161
 | 
    80  | 
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int) : Long = {
 | 
| 
 | 
    81  | 
	val increments = (for(n <- 0 to data(year).length-1 if(!(data(year)(n) == None))) yield (data(year)(n).getOrElse(0.0))).toList
  | 
| 
 | 
    82  | 
	val sumi = (increments.sum).toDouble
  | 
| 
 | 
    83  | 
	if(increments.length == 0){
 | 
| 
 | 
    84  | 
		balance
  | 
| 
 | 
    85  | 
	}else{
 | 
| 
 | 
    86  | 
		val il = (increments.length).toDouble
  | 
| 
 | 
    87  | 
		val averag = sumi/il
  | 
| 
 | 
    88  | 
		val i = (balance + (balance*averag))
  | 
| 
 | 
    89  | 
		i.toLong
  | 
| 
 | 
    90  | 
	}
  | 
| 
130
 | 
    91  | 
}
  | 
| 
 | 
    92  | 
  | 
| 
161
 | 
    93  | 
def compound_yield(data: List[List[Option[Double]]], balance: Long, ye: Int) : Long = {//if(year == 0) yearly_yield(data, balance, 0) else compound_yield(data, yearly_yield(data, balance, year), year-1)
 | 
| 
 | 
    94  | 
	val increments_py = (for(year <- 0 to ye) yield {
 | 
| 
 | 
    95  | 
		val increments = (for(n <- 0 to data(year).length-1 if(!(data(year)(n) == None))) yield (data(year)(n).getOrElse(0.0))).toList
  | 
| 
 | 
    96  | 
		val sum_of = (increments.sum).toDouble
  | 
| 
 | 
    97  | 
		val number_of = (increments.length).toDouble
  | 
| 
 | 
    98  | 
		sum_of/number_of + 1.0
  | 
| 
 | 
    99  | 
	}).toList
  | 
| 
 | 
   100  | 
	val mul_factor = increments_py.reduceLeft(_*_)
  | 
| 
 | 
   101  | 
	(balance*mul_factor).toLong
  | 
| 
 | 
   102  | 
}
  | 
| 
 | 
   103  | 
def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = {
 | 
| 
 | 
   104  | 
	val p = get_prices(portfolio, years)
  | 
| 
 | 
   105  | 
	val d = get_deltas(p)
  | 
| 
 | 
   106  | 
	compound_yield(d, start_balance, d.length-1)
  | 
| 
130
 | 
   107  | 
}
  | 
| 
 | 
   108  | 
  | 
| 
 | 
   109  | 
  | 
| 
 | 
   110  | 
  | 
| 
 | 
   111  | 
//test cases for the two portfolios given above
  | 
| 
 | 
   112  | 
  | 
| 
161
 | 
   113  | 
investment(rstate_portfolio, 1978 to 2017, 100)
  | 
| 
 | 
   114  | 
investment(blchip_portfolio, 1978 to 2017, 100)
  | 
| 
130
 | 
   115  | 
  | 
| 
144
 | 
   116  | 
}
  |