main_solution1/drumb.scala
changeset 431 ef68136b9a96
parent 418 fa7f7144f2bb
equal deleted inserted replaced
430:274c865b3878 431:ef68136b9a96
   121 //     calculates the yearly yield, i.e. new balance, according to our dumb investment 
   121 //     calculates the yearly yield, i.e. new balance, according to our dumb investment 
   122 //     strategy. Index points to a year in the data list.
   122 //     strategy. Index points to a year in the data list.
   123 
   123 
   124 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
   124 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
   125   val somes = data(index).flatten
   125   val somes = data(index).flatten
   126   println(s"somes: $somes")
       
   127   val somes_length = somes.length
   126   val somes_length = somes.length
   128   if (somes_length == 0) balance
   127   if (somes_length == 0) balance
   129   else {
   128   else {
   130     val portion: Double = balance.toDouble / somes_length.toDouble
   129     val portion: Double = balance.toDouble / somes_length.toDouble
   131     println(s"portion: $portion")
   130     balance + (for (x <- somes) yield (x * portion)).sum.toLong
   132     val b = balance + (for (x <- somes) yield (x * portion)).sum.toLong
       
   133     println(s"balance $b")
       
   134     println(s"profit ${(for (x <- somes) yield (x * portion)).sum}")
       
   135     b
       
   136   }
   131   }
   137 }
   132 }
   138 
       
   139 val dd = List(List(Some(0.9)), List(Some(0.9)))
       
   140 yearly_yield(dd, 100, 0)
       
   141 
       
   142 def yearly_yield2(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
       
   143 
       
   144     val share = balance/data(index).flatten.size.toDouble
       
   145     println(s"portion: $share")
       
   146     def count(data: List[Option[Double]], prof: Double, share: Double) : Double = data match{
       
   147         case Nil => prof
       
   148         case x::rest  => count(rest,prof + (x.getOrElse(0.0) * share), share)
       
   149     }
       
   150     val balance_double = count(data(index), 0.0, share)
       
   151     println(balance_double)
       
   152     println(s"balance ${balance_double + balance}")
       
   153     println(s"profit $balance_double")
       
   154     //if (balance_double >= balance_double.toLong.toDouble+0.5) (balance_double + 1).toLong else balance_double.toLong
       
   155     (balance_double.toLong + balance)
       
   156 }
       
   157 
       
   158 yearly_yield2(dd, 100, 0)
       
   159 
   133 
   160 // test case using the deltas calculated above
   134 // test case using the deltas calculated above
   161 //println("Task 6 yield from Google and Apple in 2010 with  balance 100")
   135 //println("Task 6 yield from Google and Apple in 2010 with  balance 100")
   162 
   136 
   163 //val d0 = goog_aapl_deltas(0)(0)
   137 //val d0 = goog_aapl_deltas(0)(0)
   164 //val d1 = goog_aapl_deltas(0)(1)
   138 //val d1 = goog_aapl_deltas(0)(1)
   165 //println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}")
   139 //println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}")
   166 
   140 
   167 
   141 
   168 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
   142 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
   169 //val goog_aapl_yield = yearly_yield2(goog_aapl_deltas, 100, 0)
       
   170 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
   143 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
   171 
   144 
   172 
   145 
   173 //yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) 
   146 //yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) 
   174 //get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum
   147 //get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum
   186     val new_balance = yearly_yield(data, balance, index)
   159     val new_balance = yearly_yield(data, balance, index)
   187     compound_yield(data, new_balance, index + 1)
   160     compound_yield(data, new_balance, index + 1)
   188   }
   161   }
   189 }
   162 }
   190 
   163 
   191 def compound_yield2(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
       
   192   if (index >= data.length) balance else {
       
   193     val new_balance = yearly_yield2(data, balance, index)
       
   194     compound_yield2(data, new_balance, index + 1)
       
   195   }
       
   196 }
       
   197 
       
   198 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   164 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   199   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   165   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   200 }
   166 }
   201 
   167 
   202 def investment2(portfolio: List[String], years: Range, start_balance: Long): Long = {
       
   203   compound_yield2(get_deltas(get_prices(portfolio, years)), start_balance, 0)
       
   204 }
       
   205 
   168 
   206 
   169 
   207 //test cases for the two portfolios given above
   170 //test cases for the two portfolios given above
   208 
   171 
   209   println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
   172   println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
   210   println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
   173   println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
   211 
   174 
   212 
   175 
   213 }
   176 }
   214 
       
   215 
       
   216 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100)
       
   217 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100)
       
   218 
       
   219 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100)
       
   220 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100)
       
   221 
       
   222 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100)
       
   223 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) 
       
   224 
       
   225 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100)
       
   226 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) 
       
   227 
       
   228 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) 
       
   229 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) 
       
   230 
       
   231 investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) 
       
   232 investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) 
       
   233 
   177 
   234 
   178 
   235 
   179 
   236 
   180 
   237 
   181 
   246 
   190 
   247 
   191 
   248 
   192 
   249 
   193 
   250 
   194 
       
   195 
       
   196