--- a/progs/drumb_sol.scala Wed Nov 08 13:57:13 2017 +0000
+++ b/progs/drumb_sol.scala Wed Nov 08 13:58:11 2017 +0000
@@ -25,9 +25,6 @@
Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
-get_trading_data("FB", 2015)
-get_trading_data("GOOG", 2010).head
-
def get_first_price(symbol: String, year: Int) : Option[Double] = {
val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None
data.map(_.split(",").toList(1).toDouble)
@@ -51,7 +48,7 @@
val p_fb = get_prices(List("FB"), 2012 to 2014)
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
-val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008)
+val tt = get_prices(List("BIDU"), 2004 to 2008)
// (2) The first function below calculates the change factor (delta) between
// a price in year n and a price in year n+1. The second function calculates
@@ -91,29 +88,6 @@
}
}
-yearly_yield(d, 100, 0)
-yearly_yield(ttd, 100, 1)
-yearly_yield(ttd, 100, 2)
-yearly_yield(ttd, 100, 3)
-
-assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2)
-assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2)
-assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2)
-assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2)
-
-
-
-//test case
-yearly_yield(d, 100, 0)
-yearly_yield(d, 225, 1)
-yearly_yield(d, 246, 2)
-yearly_yield(d, 466, 3)
-yearly_yield(d, 218, 4)
-yearly_yield(d, 469, 5)
-yearly_yield(d, 587, 6)
-//yearly_yield(d, 100, 0)
-//yearly_yield(d, 125, 1)
-
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
if (year >= data.length) balance else {
val new_balance = yearly_yield(data, balance, year)
@@ -121,72 +95,19 @@
}
}
-compound_yield(d.take(6), 100, 0)
+yearly_yield(d, 100, 0)
+//compound_yield(d.take(6), 100, 0)
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
}
-investment(List("GOOG", "AAPL"), 2005 to 2009, 100)
+
//test cases for the two portfolios given above
-println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
-println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
+//println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
+//println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
-investment(List("IBM", "BIDU"), 2004 to 2008, 100)
-
-val p_fb = get_prices(List("FB"), 2011 to 2016)
-
-// prices 2011 - 2016
-//
-List(List(None), List(None), List(Some(28.0)),
- List(Some(54.709999)), List(Some(78.449997)),
- List(Some(102.220001)))
-
-// deltas 2011 - 2016
-val d_fb = get_deltas(p_fb)
-
-List(List(None), List(None), List(Some(0.9539285357142858)),
- List(Some(0.4339242996513305)), List(Some(0.30299560113431234)))
-
-yearly_yield(d_fb, 100, 0) //2011 => 100
-yearly_yield(d_fb, 100, 1) //2012 => 100
-yearly_yield(d_fb, 100, 2) //2013 => 195
-yearly_yield(d_fb, 195, 3) //2014 => 279
-yearly_yield(d_fb, 279, 4) //2015 => 363
-
-investment(List("FB"), 2011 to 2012, 100) // => 100
-investment(List("FB"), 2011 to 2013, 100) // => 100
-investment(List("FB"), 2011 to 2014, 100) // => 195
-investment(List("FB"), 2011 to 2015, 100) // => 279
-investment(List("FB"), 2011 to 2016, 100) // => 363
-
-
-val rs_p = get_prices(rstate_portfolio, 1978 to 2016)
-val bl_p = get_prices(blchip_portfolio, 1978 to 2016)
-
-val rs_d = get_deltas(rs_p)
-val bl_d = get_deltas(bl_p)
-
-rs_p(0)
- <-
-rs_p(1)
- <-
-rs_p(2)
- <-
-rs_p(3)
-
-rs_d(0)
-rs_d(1)
-rs_d(2)
-
-yearly_yield(rs_d, 100, 0)
-yearly_yield(rs_d, 96, 1)
-yearly_yield(rs_d, 95, 2)
-yearly_yield(rs_d, 134, 3)
-yearly_yield(rs_d, 126, 4)
-yearly_yield(rs_d, 169, 5)
-
}