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   | 
   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   | 
   108   | 
   105   | 
   109 // test case using the prices calculated above  | 
   106 // test case using the prices calculated above  | 
   110 //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   | 
   111 //val ttd = get_deltas(tt)  | 
   112 //val ttd = get_deltas(tt)  | 
   112   | 
   113   | 
   113   | 
   114   | 
   114 // (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,  | 
   115 //     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   | 
   123     val portion: Double = balance.toDouble / somes_length.toDouble  | 
   124     val portion: Double = balance.toDouble / somes_length.toDouble  | 
   124     balance + (for (x <- somes) yield (x * portion)).sum.toLong  | 
   125     balance + (for (x <- somes) yield (x * portion)).sum.toLong  | 
   125   }  | 
   126   }  | 
   126 }  | 
   127 }  | 
   127   | 
   128   | 
         | 
   129 // test case using the deltas calculated above  | 
         | 
   130 //println("Task 6 yield from Google and Apple in 2010 with  balance 100") | 
   128   | 
   131   | 
   129 yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2)   | 
   132 //val d0 = goog_aapl_deltas(0)(0)  | 
   130 get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum  | 
   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  | 
   131   | 
   143   | 
   132   | 
   144   | 
   133 // (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   | 
   134 //     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   | 
   135 //     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   | 
   150   | 
   162   | 
   151   | 
   163   | 
   152   | 
   164   | 
   153 //test cases for the two portfolios given above  | 
   165 //test cases for the two portfolios given above  | 
   154   | 
   166   | 
   155 //println("Real data: " + investment(rstate_portfolio, 1978 to 2018, 100)) | 
   167 //println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100)) | 
   156 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2018, 100)) | 
   168 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100)) | 
   157   | 
   169   | 
   158 }  | 
   170 }  | 
   159   | 
   171   | 
   160   | 
   172   | 
   161   | 
   173   |