author | Christian Urban <christian.urban@kcl.ac.uk> |
Sat, 11 Mar 2023 22:42:09 +0000 | |
changeset 464 | 73ced118f73d |
parent 140 | ecec79b9ab25 |
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 |
||
13 |
import io.Source |
|
14 |
import scala.util._ |
|
15 |
||
129 | 16 |
def get_january_data(symbol: String, year: Int) : List[String] = |
17 |
Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString)) |
|
18 |
||
19 |
||
20 |
def get_first_price(symbol: String, year: Int) : Option[Double] = { |
|
21 |
val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None |
|
22 |
data.map(_.split(",").toList(1).toDouble) |
|
18 | 23 |
} |
24 |
||
129 | 25 |
get_first_price("GOOG", 1980) |
26 |
get_first_price("GOOG", 2010) |
|
27 |
get_first_price("FB", 2014) |
|
18 | 28 |
|
26 | 29 |
|
18 | 30 |
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = |
31 |
for (year <- years.toList) yield |
|
32 |
for (symbol <- portfolio) yield get_first_price(symbol, year) |
|
33 |
||
26 | 34 |
|
35 |
// test case |
|
62 | 36 |
val p_fb = get_prices(List("FB"), 2012 to 2014) |
129 | 37 |
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) |
18 | 38 |
|
131 | 39 |
val tt = get_prices(List("BIDU"), 2004 to 2008) |
18 | 40 |
|
26 | 41 |
|
129 | 42 |
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = { |
18 | 43 |
(price_old, price_new) match { |
44 |
case (Some(x), Some(y)) => Some((y - x) / x) |
|
45 |
case _ => None |
|
46 |
} |
|
47 |
} |
|
48 |
||
49 |
def get_deltas(data: List[List[Option[Double]]]): List[List[Option[Double]]] = |
|
50 |
for (i <- (0 until (data.length - 1)).toList) yield |
|
51 |
for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j)) |
|
52 |
||
26 | 53 |
|
54 |
// test case using the prices calculated above |
|
52
7a4fe3f6b188
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
39
diff
changeset
|
55 |
val d = get_deltas(p) |
82 | 56 |
val ttd = get_deltas(tt) |
26 | 57 |
|
58 |
||
18 | 59 |
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
60 |
val somes = data(year).flatten |
|
61 |
val somes_length = somes.length |
|
62 |
if (somes_length == 0) balance |
|
63 |
else { |
|
62 | 64 |
val portion: Double = balance.toDouble / somes_length.toDouble |
18 | 65 |
balance + (for (x <- somes) yield (x * portion)).sum.toLong |
66 |
} |
|
67 |
} |
|
68 |
||
69 |
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { |
|
70 |
if (year >= data.length) balance else { |
|
71 |
val new_balance = yearly_yield(data, balance, year) |
|
72 |
compound_yield(data, new_balance, year + 1) |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { |
|
77 |
compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) |
|
78 |
} |
|
79 |
||
131 | 80 |
|
18 | 81 |
|
26 | 82 |
//test cases for the two portfolios given above |
83 |
||
140 | 84 |
println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100)) |
85 |
println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100)) |
|
18 | 86 |
|
82 | 87 |
|
129 | 88 |
} |