marking1/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 28 Nov 2020 15:37:30 +0000
changeset 376 6cc36d0ef79e
parent 281 87b9e3e2c1a7
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
     1
// Main Part about a really dumb investment strategy
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
     2
//======================================================
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     4
object CW6b {
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//two test portfolios
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
import io.Source
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
import scala.util._
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    16
// (1) The function below takes a stock symbol and a year as arguments.
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    17
//     It should read the corresponding CSV-file and reads the January 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    18
//     data from the given year. The data should be collected in a list of
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    19
//     strings for each line in the CSV-file.
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    20
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def get_january_data(symbol: String, year: Int) : List[String] = 
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    22
  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    23
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    24
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    25
//test cases
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    26
//blchip_portfolio.map(get_january_data(_, 2018))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    27
//rstate_portfolio.map(get_january_data(_, 2018))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    28
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    29
//get_january_data("GOOG", 1980)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    30
//get_january_data("GOOG", 2010)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    31
//get_january_data("FB", 2014)
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    33
//get_january_data("PLD", 1980)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    34
//get_january_data("EQIX", 2010)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    35
//get_january_data("ESS", 2014)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    36
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    37
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    38
// (2) From the output of the get_january_data function, the next function 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    39
//     should extract the first line (if it exists) and the corresponding
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    40
//     first trading price in that year with type Option[Double]. If no line 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    41
//     is generated by get_january_data then the result is None; Some if 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    42
//     there is a price.
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def get_first_price(symbol: String, year: Int) : Option[Double] = {
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  data.map(_.split(",").toList(1).toDouble)
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
}
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    49
//test cases
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    50
//get_first_price("GOOG", 1980)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    51
//get_first_price("GOOG", 2010)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    52
//get_first_price("FB", 2014)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    53
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    54
/*
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    55
for (i <- 1978 to 2018) {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    56
  println(blchip_portfolio.map(get_first_price(_, i)))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    57
}
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    58
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    59
for (i <- 1978 to 2018) {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    60
  println(rstate_portfolio.map(get_first_price(_, i)))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    61
}
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    62
*/ 
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    65
// (3) Complete the function below that obtains all first prices
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    66
//     for the stock symbols from a portfolio (list of strings) and 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    67
//     for the given range of years. The inner lists are for the
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    68
//     stock symbols and the outer list for the years.
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  for (year <- years.toList) yield
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
    for (symbol <- portfolio) yield get_first_price(symbol, year)
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    75
//test cases
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    76
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    77
//println("Task 3 data from Google and Apple in 2010 to 2012")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    78
//val goog_aapl_prices = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    79
//println(goog_aapl_prices.toString ++ "\n")
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    81
//val p_fb = get_prices(List("FB"), 2012 to 2014)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    82
//val tt = get_prices(List("BIDU"), 2004 to 2008)
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    84
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    85
// (4) The function below calculates the change factor (delta) between
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    86
//     a price in year n and a price in year n + 1. 
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  (price_old, price_new) match {
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
    case (Some(x), Some(y)) => Some((y - x) / x)
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
    case _ => None
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  }
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
}
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    95
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    96
// (5) The next function calculates all change factors for all prices (from a 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    97
//     portfolio). The input to this function are the nested lists created by 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    98
//     get_prices above.
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
    99
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  for (i <- (0 until (data.length - 1)).toList) yield 
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   105
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// test case using the prices calculated above
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   108
//println("Task 5 change prices from Google and Apple in 2010 and 2011")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   109
//val goog_aapl_deltas = get_deltas(goog_aapl_prices)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   110
//println(goog_aapl_deltas.toString ++ "\n")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   111
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   112
//val ttd = get_deltas(tt)
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   115
// (6) Write a function that given change factors, a starting balance and an index,
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   116
//     calculates the yearly yield, i.e. new balance, according to our dumb investment 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   117
//     strategy. Index points to a year in the data list.
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   118
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   119
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   120
  val somes = data(index).flatten
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  val somes_length = somes.length
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  if (somes_length == 0) balance
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  else {
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
    val portion: Double = balance.toDouble / somes_length.toDouble
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
    balance + (for (x <- somes) yield (x * portion)).sum.toLong
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  }
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
}
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   129
// test case using the deltas calculated above
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   130
//println("Task 6 yield from Google and Apple in 2010 with  balance 100")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   131
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   132
//val d0 = goog_aapl_deltas(0)(0)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   133
//val d1 = goog_aapl_deltas(0)(1)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   134
//println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   135
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   136
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   137
//val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   138
//println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   139
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   140
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   141
//yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   142
//get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   143
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   144
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   145
// (7) Write a function compound_yield that calculates the overall balance for a 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   146
//     range of years where in each year the yearly profit is compounded to the new 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   147
//     balances and then re-invested into our portfolio. For this use the function and 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   148
//     results generated under (6). The function investment calls compound_yield
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   149
//     with the appropriate deltas and the first index.
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   150
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   151
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   152
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   153
  if (index >= data.length) balance else {
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   154
    val new_balance = yearly_yield(data, balance, index)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   155
    compound_yield(data, new_balance, index + 1)
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  }
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
}
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
}
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
//test cases for the two portfolios given above
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   167
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   168
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
   170
}
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 260
diff changeset
   173