--- a/testing1/drumb.scala Sat Dec 16 23:53:28 2017 +0000
+++ b/testing1/drumb.scala Mon Jan 15 23:15:34 2018 +0000
@@ -45,10 +45,10 @@
// test case
-val prices_fb = get_prices(List("FB"), 2012 to 2014)
-val prices_goap = get_prices(List("GOOG", "AAPL"), 2010 to 2014)
-val prices_bidu = get_prices(List("BIDU"), 2004 to 2008)
-val prices_goapfb = get_prices(List("GOOG", "AAPL", "FB"), 2010 to 2016)
+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("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
@@ -67,11 +67,8 @@
// test case using the prices calculated above
-val deltas_fb = get_deltas(prices_fb)
-val deltas_goap = get_deltas(prices_goap)
-val deltas_bidu = get_deltas(prices_bidu)
-val deltas_goapfb = get_deltas(prices_goapfb)
-
+val d = get_deltas(p)
+val ttd = get_deltas(tt)
// (3) Write a function that given change factors, a starting balance and a year
// calculates the yearly yield, i.e. new balanace, according to our dump investment
@@ -87,85 +84,17 @@
if (somes_length == 0) balance
else {
val portion: Double = balance.toDouble / somes_length.toDouble
- (for (x <- somes) yield ((1.0 + x) * portion)).sum.toLong
- //balance + (for (x <- somes) yield (x * portion)).sum.toLong
- }
-}
-
-def yearly_yield_double(data: List[List[Option[Double]]], balance: Double, year: Int): Double = {
- val somes = data(year).flatten
- val somes_length = somes.length
- if (somes_length == 0) balance
- else {
- val portion: Double = balance / somes_length.toDouble
- balance + (for (x <- somes) yield (x * portion)).sum
+ balance + (for (x <- somes) yield (x * portion)).sum.toLong
}
}
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
- println(balance)
if (year >= data.length) balance else {
val new_balance = yearly_yield(data, balance, year)
compound_yield(data, new_balance, year + 1)
}
}
-def compound_yield_double(data: List[List[Option[Double]]], balance: Double, year: Int): Long = {
- if (year >= data.length) balance.toLong else {
- val new_balance = yearly_yield_double(data, balance.toDouble, year)
- compound_yield_double(data, new_balance, year + 1)
- }
-}
-
-val prices_fb = get_prices(List("FB"), 2012 to 2014)
-val prices_goap = get_prices(List("GOOG", "AAPL"), 2010 to 2014)
-val prices_bidu = get_prices(List("BIDU"), 2004 to 2008)
-val prices_goapfb = get_prices(List("GOOG", "AAPL", "FB"), 2010 to 2016)
-
-val deltas_fb = get_deltas(prices_fb)
-val deltas_goap = get_deltas(prices_goap)
-val deltas_bidu = get_deltas(prices_bidu)
-val deltas_goapfb = get_deltas(prices_goapfb)
-
-
-yearly_yield(deltas_bidu, 100, 0)
-yearly_yield(deltas_bidu, 100, 1)
-yearly_yield(deltas_bidu, 100, 2)
-yearly_yield(deltas_bidu, 192, 3)
-//598
-
-yearly_yield(deltas_fb, 100, 0)
-yearly_yield(deltas_fb, 100, 1)
-//yearly_yield(deltas_fb, 195, 2)
-
-yearly_yield(deltas_goap, 100, 0)
-yearly_yield(deltas_goap, 125, 1)
-yearly_yield(deltas_goap, 146, 2)
-yearly_yield(deltas_goap, 177, 3)
-//yearly_yield(deltas_goap, 227, 4)
-
-yearly_yield(deltas_goapfb, 100, 0)
-yearly_yield(deltas_goapfb, 125, 1)
-yearly_yield(deltas_goapfb, 146, 2)
-yearly_yield(deltas_goapfb, 177, 3)
-yearly_yield(deltas_goapfb, 267, 4)
-yearly_yield(deltas_goapfb, 337, 5)
-//416
-
-compound_yield(deltas_goapfb.take(1), 100, 0)
-compound_yield(deltas_goapfb.take(2), 100, 0)
-compound_yield(deltas_goapfb.take(3), 100, 0)
-compound_yield(deltas_goapfb.take(4), 100, 0)
-compound_yield(deltas_goapfb.take(5), 100, 0)
-compound_yield(deltas_goapfb.take(6), 100, 0)
-
-compound_yield_double(deltas_goapfb.take(1), 100.0, 0)
-compound_yield_double(deltas_goapfb.take(2), 100.0, 0)
-compound_yield_double(deltas_goapfb.take(3), 100.0, 0)
-compound_yield_double(deltas_goapfb.take(4), 100.0, 0)
-compound_yield_double(deltas_goapfb.take(5), 100.0, 0)
-compound_yield_double(deltas_goapfb.take(6), 100.0, 0)
-
//yearly_yield(d, 100, 0)
//compound_yield(d.take(6), 100, 0)
@@ -183,18 +112,6 @@
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
}
-def investment_double(portfolio: List[String], years: Range, start_balance: Long): Long = {
- compound_yield_double(get_deltas(get_prices(portfolio, years)), start_balance.toDouble, 0)
-}
-
-
-investment(rstate_portfolio, 1978 to 2017, 100)
-investment(blchip_portfolio, 1978 to 2017, 100)
-
-investment_double(rstate_portfolio, 1978 to 2017, 100)
-investment_double(blchip_portfolio, 1978 to 2017, 100)
-
-
/*
val q1 = get_deltas(get_prices(List("GOOG", "AAPL", "BIDU"), 2000 to 2017))
yearly_yield(q1, 100, 0)