| author | Christian Urban <urbanc@in.tum.de> | 
| Wed, 08 Nov 2017 02:06:54 +0000 | |
| changeset 129 | 1b0f1573c27c | 
| parent 82 | 28c8e17ab83a | 
| child 131 | 06d8d1c1a61d | 
| permissions | -rw-r--r-- | 
| 129 | 1 | // Advanvced Part 3 about a really dumb investment strategy | 
| 2 | //========================================================== | |
| 3 | ||
| 4 | object CW6c {
 | |
| 5 | ||
| 18 | 6 | |
| 7 | //two test portfolios | |
| 8 | ||
| 129 | 9 | val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
 | 
| 10 | val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
 | |
| 18 | 11 | "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") | 
| 12 | ||
| 26 | 13 | // (1) The function below should obtain the first trading price | 
| 14 | // for a stock symbol by using the query | |
| 15 | // | |
| 16 | // http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> | |
| 17 | // | |
| 18 | // and extracting the first January Adjusted Close price in a year. | |
| 18 | 19 | |
| 129 | 20 | |
| 18 | 21 | import io.Source | 
| 22 | import scala.util._ | |
| 23 | ||
| 129 | 24 | def get_january_data(symbol: String, year: Int) : List[String] = | 
| 25 |   Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
 | |
| 26 | ||
| 27 | ||
| 28 | get_trading_data("FB", 2015)
 | |
| 29 | get_trading_data("GOOG", 2010).head
 | |
| 30 | ||
| 31 | def get_first_price(symbol: String, year: Int) : Option[Double] = {
 | |
| 32 | val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None | |
| 33 |   data.map(_.split(",").toList(1).toDouble)
 | |
| 18 | 34 | } | 
| 35 | ||
| 129 | 36 | get_first_price("GOOG", 1980)
 | 
| 37 | get_first_price("GOOG", 2010)
 | |
| 38 | get_first_price("FB", 2014)
 | |
| 18 | 39 | |
| 26 | 40 | |
| 41 | // Complete the function below that obtains all first prices | |
| 42 | // for the stock symbols from a portfolio for the given | |
| 43 | // range of years | |
| 44 | ||
| 18 | 45 | def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = | 
| 46 | for (year <- years.toList) yield | |
| 47 | for (symbol <- portfolio) yield get_first_price(symbol, year) | |
| 48 | ||
| 26 | 49 | |
| 50 | // test case | |
| 62 | 51 | val p_fb = get_prices(List("FB"), 2012 to 2014)
 | 
| 129 | 52 | val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
 | 
| 18 | 53 | |
| 82 | 54 | val tt = get_prices(List("IBM", "BIDU"), 2004 to 2008)
 | 
| 18 | 55 | |
| 26 | 56 | // (2) The first function below calculates the change factor (delta) between | 
| 57 | // a price in year n and a price in year n+1. The second function calculates | |
| 58 | // all change factors for all prices (from a portfolio). | |
| 59 | ||
| 129 | 60 | def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
 | 
| 18 | 61 |   (price_old, price_new) match {
 | 
| 62 | case (Some(x), Some(y)) => Some((y - x) / x) | |
| 63 | case _ => None | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = | |
| 68 | for (i <- (0 until (data.length - 1)).toList) yield | |
| 69 | for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j)) | |
| 70 | ||
| 26 | 71 | |
| 72 | // test case using the prices calculated above | |
| 52 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 73 | val d = get_deltas(p) | 
| 82 | 74 | val ttd = get_deltas(tt) | 
| 26 | 75 | |
| 76 | // (3) Write a function that given change factors, a starting balance and a year | |
| 77 | // calculates the yearly yield, i.e. new balanace, according to our dump investment | |
| 78 | // strategy. Another function calculates given the same data calculates the | |
| 79 | // compound yield up to a given year. Finally a function combines all | |
| 80 | // calculations by taking a portfolio, a range of years and a start balance | |
| 81 | // as arguments. | |
| 82 | ||
| 83 | ||
| 18 | 84 | def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
 | 
| 85 | val somes = data(year).flatten | |
| 86 | val somes_length = somes.length | |
| 87 | if (somes_length == 0) balance | |
| 88 |   else {
 | |
| 62 | 89 | val portion: Double = balance.toDouble / somes_length.toDouble | 
| 18 | 90 | balance + (for (x <- somes) yield (x * portion)).sum.toLong | 
| 91 | } | |
| 92 | } | |
| 93 | ||
| 129 | 94 | yearly_yield(d, 100, 0) | 
| 82 | 95 | yearly_yield(ttd, 100, 1) | 
| 96 | yearly_yield(ttd, 100, 2) | |
| 129 | 97 | yearly_yield(ttd, 100, 3) | 
| 98 | ||
| 99 | assert((yearly_yield(ttd, 100, 0) - 107).abs <= 2) | |
| 100 | assert((yearly_yield(ttd, 100, 1) - 85).abs <= 2) | |
| 101 | assert((yearly_yield(ttd, 100, 2) - 156).abs <= 2) | |
| 102 | assert((yearly_yield(ttd, 100, 3) - 210).abs <= 2) | |
| 82 | 103 | |
| 104 | ||
| 105 | ||
| 26 | 106 | //test case | 
| 52 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 107 | yearly_yield(d, 100, 0) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 108 | yearly_yield(d, 225, 1) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 109 | yearly_yield(d, 246, 2) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 110 | yearly_yield(d, 466, 3) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 111 | yearly_yield(d, 218, 4) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 112 | yearly_yield(d, 469, 5) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 113 | yearly_yield(d, 587, 6) | 
| 26 | 114 | //yearly_yield(d, 100, 0) | 
| 52 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 115 | //yearly_yield(d, 125, 1) | 
| 26 | 116 | |
| 18 | 117 | def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
 | 
| 118 |   if (year >= data.length) balance else {
 | |
| 119 | val new_balance = yearly_yield(data, balance, year) | |
| 120 | compound_yield(data, new_balance, year + 1) | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 52 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 124 | compound_yield(d.take(6), 100, 0) | 
| 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 125 | |
| 18 | 126 | def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
 | 
| 127 | compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) | |
| 128 | } | |
| 129 | ||
| 52 
7a4fe3f6b188
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
39diff
changeset | 130 | investment(List("GOOG", "AAPL"), 2005 to 2009, 100)
 | 
| 18 | 131 | |
| 26 | 132 | //test cases for the two portfolios given above | 
| 133 | ||
| 129 | 134 | println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
 | 
| 135 | println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
 | |
| 18 | 136 | |
| 82 | 137 | |
| 138 | investment(List("IBM", "BIDU"), 2004 to 2008, 100)
 | |
| 139 | ||
| 62 | 140 | val p_fb = get_prices(List("FB"), 2011 to 2016)
 | 
| 141 | ||
| 142 | // prices 2011 - 2016 | |
| 143 | // | |
| 144 | List(List(None), List(None), List(Some(28.0)), | |
| 145 | List(Some(54.709999)), List(Some(78.449997)), | |
| 146 | List(Some(102.220001))) | |
| 147 | ||
| 148 | // deltas 2011 - 2016 | |
| 149 | val d_fb = get_deltas(p_fb) | |
| 150 | ||
| 151 | List(List(None), List(None), List(Some(0.9539285357142858)), | |
| 152 | List(Some(0.4339242996513305)), List(Some(0.30299560113431234))) | |
| 153 | ||
| 154 | yearly_yield(d_fb, 100, 0) //2011 => 100 | |
| 155 | yearly_yield(d_fb, 100, 1) //2012 => 100 | |
| 156 | yearly_yield(d_fb, 100, 2) //2013 => 195 | |
| 157 | yearly_yield(d_fb, 195, 3) //2014 => 279 | |
| 158 | yearly_yield(d_fb, 279, 4) //2015 => 363 | |
| 159 | ||
| 160 | investment(List("FB"), 2011 to 2012, 100)  // => 100
 | |
| 161 | investment(List("FB"), 2011 to 2013, 100)  // => 100
 | |
| 162 | investment(List("FB"), 2011 to 2014, 100)  // => 195
 | |
| 163 | investment(List("FB"), 2011 to 2015, 100)  // => 279
 | |
| 164 | investment(List("FB"), 2011 to 2016, 100)  // => 363
 | |
| 165 | ||
| 166 | ||
| 64 | 167 | val rs_p = get_prices(rstate_portfolio, 1978 to 2016) | 
| 168 | val bl_p = get_prices(blchip_portfolio, 1978 to 2016) | |
| 169 | ||
| 170 | val rs_d = get_deltas(rs_p) | |
| 171 | val bl_d = get_deltas(bl_p) | |
| 172 | ||
| 173 | rs_p(0) | |
| 174 | <- | |
| 175 | rs_p(1) | |
| 176 | <- | |
| 177 | rs_p(2) | |
| 178 | <- | |
| 179 | rs_p(3) | |
| 180 | ||
| 181 | rs_d(0) | |
| 182 | rs_d(1) | |
| 183 | rs_d(2) | |
| 184 | ||
| 185 | yearly_yield(rs_d, 100, 0) | |
| 186 | yearly_yield(rs_d, 96, 1) | |
| 187 | yearly_yield(rs_d, 95, 2) | |
| 188 | yearly_yield(rs_d, 134, 3) | |
| 189 | yearly_yield(rs_d, 126, 4) | |
| 190 | yearly_yield(rs_d, 169, 5) | |
| 191 | ||
| 129 | 192 | } |