testing1/drumb.scala
changeset 167 349d706586ef
parent 166 780c40aaad27
child 171 4c9497ab5caa
equal deleted inserted replaced
166:780c40aaad27 167:349d706586ef
    43   for (year <- years.toList) yield
    43   for (year <- years.toList) yield
    44     for (symbol <- portfolio) yield get_first_price(symbol, year)
    44     for (symbol <- portfolio) yield get_first_price(symbol, year)
    45 
    45 
    46 
    46 
    47 // test case
    47 // test case
    48 val prices_fb    = get_prices(List("FB"), 2012 to 2014)
    48 val p_fb = get_prices(List("FB"), 2012 to 2014)
    49 val prices_goap = get_prices(List("GOOG", "AAPL"), 2010 to 2014)
    49 val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
    50 val prices_bidu = get_prices(List("BIDU"), 2004 to 2008)
    50 
    51 val prices_goapfb = get_prices(List("GOOG", "AAPL", "FB"), 2010 to 2016)
    51 val tt = get_prices(List("BIDU"), 2004 to 2008)
    52 
    52 
    53 // (2) The first function below calculates the change factor (delta) between
    53 // (2) The first function below calculates the change factor (delta) between
    54 // 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
    55 // all change factors for all prices (from a portfolio).
    55 // all change factors for all prices (from a portfolio).
    56 
    56 
    65   for (i <- (0 until (data.length - 1)).toList) yield 
    65   for (i <- (0 until (data.length - 1)).toList) yield 
    66     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
    66     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
    67 
    67 
    68 
    68 
    69 // test case using the prices calculated above
    69 // test case using the prices calculated above
    70 val deltas_fb   = get_deltas(prices_fb)
    70 val d = get_deltas(p)
    71 val deltas_goap = get_deltas(prices_goap)
    71 val ttd = get_deltas(tt)
    72 val deltas_bidu = get_deltas(prices_bidu)
       
    73 val deltas_goapfb = get_deltas(prices_goapfb)
       
    74 
       
    75 
    72 
    76 // (3) Write a function that given change factors, a starting balance and a year
    73 // (3) Write a function that given change factors, a starting balance and a year
    77 // calculates the yearly yield, i.e. new balanace, according to our dump investment 
    74 // calculates the yearly yield, i.e. new balanace, according to our dump investment 
    78 // strategy. Another function calculates given the same data calculates the
    75 // strategy. Another function calculates given the same data calculates the
    79 // compound yield up to a given year. Finally a function combines all 
    76 // compound yield up to a given year. Finally a function combines all 
    85   val somes = data(year).flatten
    82   val somes = data(year).flatten
    86   val somes_length = somes.length
    83   val somes_length = somes.length
    87   if (somes_length == 0) balance
    84   if (somes_length == 0) balance
    88   else {
    85   else {
    89     val portion: Double = balance.toDouble / somes_length.toDouble
    86     val portion: Double = balance.toDouble / somes_length.toDouble
    90     (for (x <- somes) yield ((1.0 + x) * portion)).sum.toLong
    87     balance + (for (x <- somes) yield (x * portion)).sum.toLong
    91     //balance + (for (x <- somes) yield (x * portion)).sum.toLong
       
    92   }
       
    93 }
       
    94 
       
    95 def yearly_yield_double(data: List[List[Option[Double]]], balance: Double, year: Int): Double = {
       
    96   val somes = data(year).flatten
       
    97   val somes_length = somes.length
       
    98   if (somes_length == 0) balance
       
    99   else {
       
   100     val portion: Double = balance / somes_length.toDouble
       
   101     balance + (for (x <- somes) yield (x * portion)).sum
       
   102   }
    88   }
   103 }
    89 }
   104 
    90 
   105 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 = {
   106   println(balance)
       
   107   if (year >= data.length) balance else {
    92   if (year >= data.length) balance else {
   108     val new_balance = yearly_yield(data, balance, year)
    93     val new_balance = yearly_yield(data, balance, year)
   109     compound_yield(data, new_balance, year + 1)
    94     compound_yield(data, new_balance, year + 1)
   110   }
    95   }
   111 }
    96 }
   112 
       
   113 def compound_yield_double(data: List[List[Option[Double]]], balance: Double, year: Int): Long = {
       
   114   if (year >= data.length) balance.toLong else {
       
   115     val new_balance = yearly_yield_double(data, balance.toDouble, year)
       
   116     compound_yield_double(data, new_balance, year + 1)
       
   117   }
       
   118 }
       
   119 
       
   120 val prices_fb    = get_prices(List("FB"), 2012 to 2014)
       
   121 val prices_goap = get_prices(List("GOOG", "AAPL"), 2010 to 2014)
       
   122 val prices_bidu = get_prices(List("BIDU"), 2004 to 2008)
       
   123 val prices_goapfb = get_prices(List("GOOG", "AAPL", "FB"), 2010 to 2016)
       
   124 
       
   125 val deltas_fb   = get_deltas(prices_fb)
       
   126 val deltas_goap = get_deltas(prices_goap)
       
   127 val deltas_bidu = get_deltas(prices_bidu)
       
   128 val deltas_goapfb = get_deltas(prices_goapfb)
       
   129 
       
   130 
       
   131 yearly_yield(deltas_bidu, 100, 0)
       
   132 yearly_yield(deltas_bidu, 100, 1)
       
   133 yearly_yield(deltas_bidu, 100, 2)
       
   134 yearly_yield(deltas_bidu, 192, 3)
       
   135 //598
       
   136 
       
   137 yearly_yield(deltas_fb, 100, 0)
       
   138 yearly_yield(deltas_fb, 100, 1)
       
   139 //yearly_yield(deltas_fb, 195, 2)
       
   140 
       
   141 yearly_yield(deltas_goap, 100, 0)
       
   142 yearly_yield(deltas_goap, 125, 1)
       
   143 yearly_yield(deltas_goap, 146, 2)
       
   144 yearly_yield(deltas_goap, 177, 3)
       
   145 //yearly_yield(deltas_goap, 227, 4)
       
   146 
       
   147 yearly_yield(deltas_goapfb, 100, 0)
       
   148 yearly_yield(deltas_goapfb, 125, 1)
       
   149 yearly_yield(deltas_goapfb, 146, 2)
       
   150 yearly_yield(deltas_goapfb, 177, 3)
       
   151 yearly_yield(deltas_goapfb, 267, 4)
       
   152 yearly_yield(deltas_goapfb, 337, 5)
       
   153 //416
       
   154 
       
   155 compound_yield(deltas_goapfb.take(1), 100, 0)
       
   156 compound_yield(deltas_goapfb.take(2), 100, 0)
       
   157 compound_yield(deltas_goapfb.take(3), 100, 0)
       
   158 compound_yield(deltas_goapfb.take(4), 100, 0)
       
   159 compound_yield(deltas_goapfb.take(5), 100, 0)
       
   160 compound_yield(deltas_goapfb.take(6), 100, 0)
       
   161 
       
   162 compound_yield_double(deltas_goapfb.take(1), 100.0, 0)
       
   163 compound_yield_double(deltas_goapfb.take(2), 100.0, 0)
       
   164 compound_yield_double(deltas_goapfb.take(3), 100.0, 0)
       
   165 compound_yield_double(deltas_goapfb.take(4), 100.0, 0)
       
   166 compound_yield_double(deltas_goapfb.take(5), 100.0, 0)
       
   167 compound_yield_double(deltas_goapfb.take(6), 100.0, 0)
       
   168 
    97 
   169 //yearly_yield(d, 100, 0)
    98 //yearly_yield(d, 100, 0)
   170 //compound_yield(d.take(6), 100, 0)
    99 //compound_yield(d.take(6), 100, 0)
   171 
   100 
   172 //test case
   101 //test case
   180 //yearly_yield(d, 125, 1)
   109 //yearly_yield(d, 125, 1)
   181 
   110 
   182 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   111 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   183   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   112   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   184 }
   113 }
   185 
       
   186 def investment_double(portfolio: List[String], years: Range, start_balance: Long): Long = {
       
   187   compound_yield_double(get_deltas(get_prices(portfolio, years)), start_balance.toDouble, 0)
       
   188 }
       
   189 
       
   190 
       
   191 investment(rstate_portfolio, 1978 to 2017, 100)
       
   192 investment(blchip_portfolio, 1978 to 2017, 100)
       
   193 
       
   194 investment_double(rstate_portfolio, 1978 to 2017, 100)
       
   195 investment_double(blchip_portfolio, 1978 to 2017, 100)
       
   196 
       
   197 
   114 
   198 /*
   115 /*
   199 val q1 = get_deltas(get_prices(List("GOOG", "AAPL", "BIDU"), 2000 to 2017))
   116 val q1 = get_deltas(get_prices(List("GOOG", "AAPL", "BIDU"), 2000 to 2017))
   200 yearly_yield(q1, 100, 0)
   117 yearly_yield(q1, 100, 0)
   201 yearly_yield(q1, 100, 1)
   118 yearly_yield(q1, 100, 1)