40 for (year <- years.toList) yield |
40 for (year <- years.toList) yield |
41 for (symbol <- portfolio) yield get_first_price(symbol, year) |
41 for (symbol <- portfolio) yield get_first_price(symbol, year) |
42 |
42 |
43 |
43 |
44 // test case |
44 // test case |
45 //val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) |
45 val p = get_prices(List("FB"), 2012 to 2014) |
|
46 val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012) |
46 |
47 |
47 |
48 |
48 // (2) The first function below calculates the change factor (delta) between |
49 // (2) The first function below calculates the change factor (delta) between |
49 // a price in year n and a price in year n+1. The second function calculates |
50 // a price in year n and a price in year n+1. The second function calculates |
50 // all change factors for all prices (from a portfolio). |
51 // all change factors for all prices (from a portfolio). |
60 for (i <- (0 until (data.length - 1)).toList) yield |
61 for (i <- (0 until (data.length - 1)).toList) yield |
61 for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j)) |
62 for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j)) |
62 |
63 |
63 |
64 |
64 // test case using the prices calculated above |
65 // test case using the prices calculated above |
65 //val d = get_deltas(p) |
66 val d = get_deltas(p) |
66 |
67 |
67 |
68 |
68 // (3) Write a function that given change factors, a starting balance and a year |
69 // (3) Write a function that given change factors, a starting balance and a year |
69 // calculates the yearly yield, i.e. new balanace, according to our dump investment |
70 // calculates the yearly yield, i.e. new balanace, according to our dump investment |
70 // strategy. Another function calculates given the same data calculates the |
71 // strategy. Another function calculates given the same data calculates the |
76 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
77 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
77 val somes = data(year).flatten |
78 val somes = data(year).flatten |
78 val somes_length = somes.length |
79 val somes_length = somes.length |
79 if (somes_length == 0) balance |
80 if (somes_length == 0) balance |
80 else { |
81 else { |
81 val portion: Double = balance / somes_length |
82 val portion: Double = (balance.toDouble) / somes_length |
82 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
83 balance + (for (x <- somes) yield (x * portion)).sum.toLong |
83 } |
84 } |
84 } |
85 } |
85 |
86 |
86 //test case |
87 //test case |
|
88 yearly_yield(d, 100, 0) |
|
89 yearly_yield(d, 225, 1) |
|
90 yearly_yield(d, 246, 2) |
|
91 yearly_yield(d, 466, 3) |
|
92 yearly_yield(d, 218, 4) |
|
93 yearly_yield(d, 469, 5) |
|
94 yearly_yield(d, 587, 6) |
87 //yearly_yield(d, 100, 0) |
95 //yearly_yield(d, 100, 0) |
|
96 //yearly_yield(d, 125, 1) |
88 |
97 |
89 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
98 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
90 if (year >= data.length) balance else { |
99 if (year >= data.length) balance else { |
91 val new_balance = yearly_yield(data, balance, year) |
100 val new_balance = yearly_yield(data, balance, year) |
92 compound_yield(data, new_balance, year + 1) |
101 compound_yield(data, new_balance, year + 1) |
93 } |
102 } |
94 } |
103 } |
95 |
104 |
|
105 compound_yield(d.take(6), 100, 0) |
|
106 |
96 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
107 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
97 compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
108 compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
98 } |
109 } |
99 |
110 |
|
111 investment(List("GOOG", "AAPL"), 2005 to 2009, 100) |
100 |
112 |
101 //test cases for the two portfolios given above |
113 //test cases for the two portfolios given above |
102 |
114 |
103 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
115 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100)) |
104 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |
116 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100)) |