progs/drumb_sol.scala
changeset 131 1fb90f5d53ee
parent 129 b1a51285de7e
child 140 ecec79b9ab25
equal deleted inserted replaced
130:7f3f01dfe738 131:1fb90f5d53ee
    23 
    23 
    24 def get_january_data(symbol: String, year: Int) : List[String] = 
    24 def get_january_data(symbol: String, year: Int) : List[String] = 
    25   Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
    25   Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
    26 
    26 
    27 
    27 
    28 get_trading_data("FB", 2015)
       
    29 get_trading_data("GOOG", 2010).head
       
    30 
       
    31 def get_first_price(symbol: String, year: Int) : Option[Double] = {
    28 def get_first_price(symbol: String, year: Int) : Option[Double] = {
    32   val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
    29   val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
    33   data.map(_.split(",").toList(1).toDouble)
    30   data.map(_.split(",").toList(1).toDouble)
    34 }
    31 }
    35 
    32 
    49 
    46 
    50 // test case
    47 // test case
    51 val p_fb = get_prices(List("FB"), 2012 to 2014)
    48 val p_fb = get_prices(List("FB"), 2012 to 2014)
    52 val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
    49 val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
    53 
    50 
    54 val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008)
    51 val tt = get_prices(List("BIDU"), 2004 to 2008)
    55 
    52 
    56 // (2) The first function below calculates the change factor (delta) between
    53 // (2) The first function below calculates the change factor (delta) between
    57 // a price in year n and a price in year n+1. The second function calculates
    54 // a price in year n and a price in year n+1. The second function calculates
    58 // all change factors for all prices (from a portfolio).
    55 // all change factors for all prices (from a portfolio).
    59 
    56 
    89     val portion: Double = balance.toDouble / somes_length.toDouble
    86     val portion: Double = balance.toDouble / somes_length.toDouble
    90     balance + (for (x <- somes) yield (x * portion)).sum.toLong
    87     balance + (for (x <- somes) yield (x * portion)).sum.toLong
    91   }
    88   }
    92 }
    89 }
    93 
    90 
    94 yearly_yield(d, 100, 0)
       
    95 yearly_yield(ttd, 100, 1)
       
    96 yearly_yield(ttd, 100, 2)
       
    97 yearly_yield(ttd, 100, 3) 
       
    98 
       
    99 assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2)
       
   100 assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2)
       
   101 assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2)
       
   102 assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2)
       
   103 
       
   104 
       
   105 
       
   106 //test case
       
   107 yearly_yield(d, 100, 0)
       
   108 yearly_yield(d, 225, 1)
       
   109 yearly_yield(d, 246, 2)
       
   110 yearly_yield(d, 466, 3)
       
   111 yearly_yield(d, 218, 4)
       
   112 yearly_yield(d, 469, 5)
       
   113 yearly_yield(d, 587, 6)
       
   114 //yearly_yield(d, 100, 0)
       
   115 //yearly_yield(d, 125, 1)
       
   116 
       
   117 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
    91 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
   118   if (year >= data.length) balance else {
    92   if (year >= data.length) balance else {
   119     val new_balance = yearly_yield(data, balance, year)
    93     val new_balance = yearly_yield(data, balance, year)
   120     compound_yield(data, new_balance, year + 1)
    94     compound_yield(data, new_balance, year + 1)
   121   }
    95   }
   122 }
    96 }
   123 
    97 
   124 compound_yield(d.take(6), 100, 0)
    98 yearly_yield(d, 100, 0)
       
    99 //compound_yield(d.take(6), 100, 0)
   125 
   100 
   126 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   101 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   127   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   102   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   128 }
   103 }
   129 
   104 
   130 investment(List("GOOG", "AAPL"), 2005 to 2009, 100)
   105 
   131 
   106 
   132 //test cases for the two portfolios given above
   107 //test cases for the two portfolios given above
   133 
   108 
   134 println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
   109 //println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
   135 println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
   110 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
   136 
   111 
   137 
   112 
   138 investment(List("IBM", "BIDU"), 2004 to 2008, 100)
       
   139 
       
   140 val p_fb = get_prices(List("FB"), 2011 to 2016)
       
   141 
       
   142 // prices 2011 - 2016
       
   143 //    
       
   144 List(List(None), List(None), List(Some(28.0)), 
       
   145      List(Some(54.709999)), List(Some(78.449997)), 
       
   146      List(Some(102.220001)))
       
   147 
       
   148 // deltas 2011 - 2016
       
   149 val d_fb = get_deltas(p_fb)
       
   150 
       
   151 List(List(None), List(None), List(Some(0.9539285357142858)), 
       
   152      List(Some(0.4339242996513305)), List(Some(0.30299560113431234)))
       
   153 
       
   154 yearly_yield(d_fb, 100, 0)   //2011  => 100
       
   155 yearly_yield(d_fb, 100, 1)   //2012  => 100
       
   156 yearly_yield(d_fb, 100, 2)   //2013  => 195
       
   157 yearly_yield(d_fb, 195, 3)   //2014  => 279   
       
   158 yearly_yield(d_fb, 279, 4)   //2015  => 363
       
   159 
       
   160 investment(List("FB"), 2011 to 2012, 100)  // => 100
       
   161 investment(List("FB"), 2011 to 2013, 100)  // => 100
       
   162 investment(List("FB"), 2011 to 2014, 100)  // => 195
       
   163 investment(List("FB"), 2011 to 2015, 100)  // => 279
       
   164 investment(List("FB"), 2011 to 2016, 100)  // => 363
       
   165 
       
   166 
       
   167 val rs_p = get_prices(rstate_portfolio, 1978 to 2016)
       
   168 val bl_p = get_prices(blchip_portfolio, 1978 to 2016)
       
   169 
       
   170 val rs_d = get_deltas(rs_p)
       
   171 val bl_d = get_deltas(bl_p)
       
   172 
       
   173 rs_p(0)
       
   174     <-
       
   175 rs_p(1)
       
   176     <- 
       
   177 rs_p(2)
       
   178     <- 
       
   179 rs_p(3)
       
   180 
       
   181 rs_d(0)
       
   182 rs_d(1)
       
   183 rs_d(2)
       
   184 
       
   185 yearly_yield(rs_d, 100, 0)
       
   186 yearly_yield(rs_d, 96, 1)
       
   187 yearly_yield(rs_d, 95, 2)
       
   188 yearly_yield(rs_d, 134, 3)
       
   189 yearly_yield(rs_d, 126, 4)
       
   190 yearly_yield(rs_d, 169, 5)
       
   191 
       
   192 }
   113 }