progs/drumb_sol.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 31 Oct 2019 11:18:37 +0000
changeset 298 51ea8891bc26
parent 140 ecec79b9ab25
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     1
// Advanvced Part 3 about a really dumb investment strategy
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     2
//==========================================================
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     3
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     4
object CW6c {
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     5
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//two test portfolios
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
     9
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    10
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import io.Source
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
import scala.util._
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    16
def get_january_data(symbol: String, year: Int) : List[String] = 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    17
  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    18
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    19
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    20
def get_first_price(symbol: String, year: Int) : Option[Double] = {
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    21
  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    22
  data.map(_.split(",").toList(1).toDouble)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    25
get_first_price("GOOG", 1980)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    26
get_first_price("GOOG", 2010)
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    27
get_first_price("FB", 2014)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    29
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  for (year <- years.toList) yield
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    for (symbol <- portfolio) yield get_first_price(symbol, year)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    34
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    35
// test case
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    36
val p_fb = get_prices(List("FB"), 2012 to 2014)
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    37
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
131
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    39
val tt = get_prices(List("BIDU"), 2004 to 2008)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    41
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    42
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  (price_old, price_new) match {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    case (Some(x), Some(y)) => Some((y - x) / x)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    case _ => None
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  for (i <- (0 until (data.length - 1)).toList) yield 
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    53
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    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
28c8e17ab83a updated
Christian Urban <urbanc@in.tum.de>
parents: 81
diff changeset
    56
val ttd = get_deltas(tt)
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    57
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    58
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  val somes = data(year).flatten
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  val somes_length = somes.length
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  if (somes_length == 0) balance
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  else {
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    64
    val portion: Double = balance.toDouble / somes_length.toDouble
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
    balance + (for (x <- somes) yield (x * portion)).sum.toLong
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  if (year >= data.length) balance else {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
    val new_balance = yearly_yield(data, balance, year)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
    compound_yield(data, new_balance, year + 1)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  }
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
131
1fb90f5d53ee updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    80
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    82
//test cases for the two portfolios given above
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    83
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
    84
println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
    85
println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
82
28c8e17ab83a updated
Christian Urban <urbanc@in.tum.de>
parents: 81
diff changeset
    87
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents: 82
diff changeset
    88
}