testing1/drumb.scala
changeset 281 87b9e3e2c1a7
parent 248 1616d06a0893
equal deleted inserted replaced
280:a057dc4457fc 281:87b9e3e2c1a7
     1 // Part 2 and 3 about a really dumb investment strategy
     1 // Main Part about a really dumb investment strategy
     2 //======================================================
     2 //======================================================
     3 
     3 
     4 //object CW6b { // for purposes of generating a jar
     4 object CW6b {
     5 
     5 
     6 
     6 
     7 //two test portfolios
     7 //two test portfolios
     8 
     8 
     9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
     9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
    71   for (year <- years.toList) yield
    71   for (year <- years.toList) yield
    72     for (symbol <- portfolio) yield get_first_price(symbol, year)
    72     for (symbol <- portfolio) yield get_first_price(symbol, year)
    73 
    73 
    74 
    74 
    75 //test cases
    75 //test cases
       
    76 
       
    77 //println("Task 3 data from Google and Apple in 2010 to 2012")
       
    78 //val goog_aapl_prices = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
       
    79 //println(goog_aapl_prices.toString ++ "\n")
       
    80 
    76 //val p_fb = get_prices(List("FB"), 2012 to 2014)
    81 //val p_fb = get_prices(List("FB"), 2012 to 2014)
    77 //val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
       
    78 
       
    79 //val tt = get_prices(List("BIDU"), 2004 to 2008)
    82 //val tt = get_prices(List("BIDU"), 2004 to 2008)
    80 
       
    81 
       
    82 //==============================================
       
    83 // Do not change anything below, unless you want 
       
    84 // to submit the file for the advanced part 3!
       
    85 //==============================================
       
    86 
    83 
    87 
    84 
    88 // (4) The function below calculates the change factor (delta) between
    85 // (4) The function below calculates the change factor (delta) between
    89 //     a price in year n and a price in year n + 1. 
    86 //     a price in year n and a price in year n + 1. 
    90 
    87 
   103 def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
   100 def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
   104   for (i <- (0 until (data.length - 1)).toList) yield 
   101   for (i <- (0 until (data.length - 1)).toList) yield 
   105     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
   102     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
   106 
   103 
   107 
   104 
       
   105 
   108 // test case using the prices calculated above
   106 // test case using the prices calculated above
   109 //val d = get_deltas(p)
   107 
       
   108 //println("Task 5 change prices from Google and Apple in 2010 and 2011")
       
   109 //val goog_aapl_deltas = get_deltas(goog_aapl_prices)
       
   110 //println(goog_aapl_deltas.toString ++ "\n")
       
   111 
   110 //val ttd = get_deltas(tt)
   112 //val ttd = get_deltas(tt)
   111 
   113 
   112 
   114 
   113 // (6) Write a function that given change factors, a starting balance and an index,
   115 // (6) Write a function that given change factors, a starting balance and an index,
   114 //     calculates the yearly yield, i.e. new balance, according to our dumb investment 
   116 //     calculates the yearly yield, i.e. new balance, according to our dumb investment 
   121   else {
   123   else {
   122     val portion: Double = balance.toDouble / somes_length.toDouble
   124     val portion: Double = balance.toDouble / somes_length.toDouble
   123     balance + (for (x <- somes) yield (x * portion)).sum.toLong
   125     balance + (for (x <- somes) yield (x * portion)).sum.toLong
   124   }
   126   }
   125 }
   127 }
       
   128 
       
   129 // test case using the deltas calculated above
       
   130 //println("Task 6 yield from Google and Apple in 2010 with  balance 100")
       
   131 
       
   132 //val d0 = goog_aapl_deltas(0)(0)
       
   133 //val d1 = goog_aapl_deltas(0)(1)
       
   134 //println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}")
       
   135 
       
   136 
       
   137 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
       
   138 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
       
   139 
       
   140 
       
   141 //yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) 
       
   142 //get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum
   126 
   143 
   127 
   144 
   128 // (7) Write a function compound_yield that calculates the overall balance for a 
   145 // (7) Write a function compound_yield that calculates the overall balance for a 
   129 //     range of years where in each year the yearly profit is compounded to the new 
   146 //     range of years where in each year the yearly profit is compounded to the new 
   130 //     balances and then re-invested into our portfolio. For this use the function and 
   147 //     balances and then re-invested into our portfolio. For this use the function and 
   145 
   162 
   146 
   163 
   147 
   164 
   148 //test cases for the two portfolios given above
   165 //test cases for the two portfolios given above
   149 
   166 
   150 //println("Real data: " + investment(rstate_portfolio, 1978 to 2018, 100))
   167 //println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
   151 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2018, 100))
   168 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
   152 
   169 
   153 //}
   170 }
   154 
   171 
   155 
   172 
   156 //val ds = get_deltas(get_prices(List("GOOG", "AAPL"), 2010 to 2012))
       
   157 //yearly_yield(ds, 100, 0)  => 125
       
   158 //yearly_yield(ds, 100, 1)  => 117
       
   159 
   173 
   160 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100) // => 100
       
   161 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100) // => 27
       
   162 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) // => 42
       
   163 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) // => 27
       
   164 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) // => 38
       
   165 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) // => 113
       
   166 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2006, 100) // => 254
       
   167 //investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2007, 100) // => 349