Attic/drumb_sol.scala
changeset 468 0587ef444547
parent 140 ecec79b9ab25
equal deleted inserted replaced
467:9b5165b8a762 468:0587ef444547
       
     1 // Advanvced Part 3 about a really dumb investment strategy
       
     2 //==========================================================
       
     3 
       
     4 object CW6c {
       
     5 
       
     6 
       
     7 //two test portfolios
       
     8 
       
     9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
       
    10 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
       
    11                             "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
       
    12 
       
    13 import io.Source
       
    14 import scala.util._
       
    15 
       
    16 def get_january_data(symbol: String, year: Int) : List[String] = 
       
    17   Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
       
    18 
       
    19 
       
    20 def get_first_price(symbol: String, year: Int) : Option[Double] = {
       
    21   val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
       
    22   data.map(_.split(",").toList(1).toDouble)
       
    23 }
       
    24 
       
    25 get_first_price("GOOG", 1980)
       
    26 get_first_price("GOOG", 2010)
       
    27 get_first_price("FB", 2014)
       
    28 
       
    29 
       
    30 def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
       
    31   for (year <- years.toList) yield
       
    32     for (symbol <- portfolio) yield get_first_price(symbol, year)
       
    33 
       
    34 
       
    35 // test case
       
    36 val p_fb = get_prices(List("FB"), 2012 to 2014)
       
    37 val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
       
    38 
       
    39 val tt = get_prices(List("BIDU"), 2004 to 2008)
       
    40 
       
    41 
       
    42 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
       
    43   (price_old, price_new) match {
       
    44     case (Some(x), Some(y)) => Some((y - x) / x)
       
    45     case _ => None
       
    46   }
       
    47 }
       
    48 
       
    49 def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
       
    50   for (i <- (0 until (data.length - 1)).toList) yield 
       
    51     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
       
    52 
       
    53 
       
    54 // test case using the prices calculated above
       
    55 val d = get_deltas(p)
       
    56 val ttd = get_deltas(tt)
       
    57 
       
    58 
       
    59 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
       
    60   val somes = data(year).flatten
       
    61   val somes_length = somes.length
       
    62   if (somes_length == 0) balance
       
    63   else {
       
    64     val portion: Double = balance.toDouble / somes_length.toDouble
       
    65     balance + (for (x <- somes) yield (x * portion)).sum.toLong
       
    66   }
       
    67 }
       
    68 
       
    69 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
       
    70   if (year >= data.length) balance else {
       
    71     val new_balance = yearly_yield(data, balance, year)
       
    72     compound_yield(data, new_balance, year + 1)
       
    73   }
       
    74 }
       
    75 
       
    76 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
       
    77   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
       
    78 }
       
    79 
       
    80 
       
    81 
       
    82 //test cases for the two portfolios given above
       
    83 
       
    84 println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
       
    85 println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
       
    86 
       
    87 
       
    88 }