main_solution1/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 04 Aug 2022 16:53:38 +0200
changeset 426 b51467741af2
parent 418 fa7f7144f2bb
child 431 ef68136b9a96
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
     1
// Main Part 1 about a really dumb investment strategy
282
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     2
//=====================================================
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     3
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
282
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     5
// generate jar with
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     6
//   > scala -d drumb.jar  drumb.scala
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     7
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     8
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
     9
object M1 { 
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
//two test portfolios
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
import io.Source
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
import scala.util._
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
// (1) The function below takes a stock symbol and a year as arguments.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//     It should read the corresponding CSV-file and reads the January 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//     data from the given year. The data should be collected in a list of
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
//     strings for each line in the CSV-file.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
def get_january_data(symbol: String, year: Int) : List[String] = 
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
    27
  Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines().toList.filter(_.startsWith(year.toString))
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//test cases
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
//blchip_portfolio.map(get_january_data(_, 2018))
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
//rstate_portfolio.map(get_january_data(_, 2018))
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
//get_january_data("GOOG", 1980)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//get_january_data("GOOG", 2010)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
//get_january_data("FB", 2014)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
//get_january_data("PLD", 1980)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//get_january_data("EQIX", 2010)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//get_january_data("ESS", 2014)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
// (2) From the output of the get_january_data function, the next function 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
//     should extract the first line (if it exists) and the corresponding
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
//     first trading price in that year with type Option[Double]. If no line 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
//     is generated by get_january_data then the result is None; Some if 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
//     there is a price.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
def get_first_price(symbol: String, year: Int) : Option[Double] = {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  data.map(_.split(",").toList(1).toDouble)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//test cases
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
//get_first_price("GOOG", 1980)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
//get_first_price("GOOG", 2010)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
//get_first_price("FB", 2014)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
/*
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
for (i <- 1978 to 2018) {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  println(blchip_portfolio.map(get_first_price(_, i)))
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
for (i <- 1978 to 2018) {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  println(rstate_portfolio.map(get_first_price(_, i)))
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
*/ 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// (3) Complete the function below that obtains all first prices
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//     for the stock symbols from a portfolio (list of strings) and 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
//     for the given range of years. The inner lists are for the
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//     stock symbols and the outer list for the years.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  for (year <- years.toList) yield
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
    for (symbol <- portfolio) yield get_first_price(symbol, year)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
//test cases
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    82
//println("Task 3 data from Google and Apple in 2010 to 2012")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    83
//val goog_aapl_prices = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    84
//println(goog_aapl_prices.toString ++ "\n")
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    86
//val p_fb = get_prices(List("FB"), 2012 to 2014)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
    87
//val tt = get_prices(List("BIDU"), 2004 to 2008)
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// (4) The function below calculates the change factor (delta) between
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
//     a price in year n and a price in year n + 1. 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  (price_old, price_new) match {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
    case (Some(x), Some(y)) => Some((y - x) / x)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
    case _ => None
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  }
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
// (5) The next function calculates all change factors for all prices (from a 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
//     portfolio). The input to this function are the nested lists created by 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
//     get_prices above.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  for (i <- (0 until (data.length - 1)).toList) yield 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
    for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   110
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
// test case using the prices calculated above
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   112
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   113
//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: 229
diff changeset
   114
//val goog_aapl_deltas = get_deltas(goog_aapl_prices)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   115
//println(goog_aapl_deltas.toString ++ "\n")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   116
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
//val ttd = get_deltas(tt)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
// (6) Write a function that given change factors, a starting balance and an index,
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
//     calculates the yearly yield, i.e. new balance, according to our dumb investment 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
//     strategy. Index points to a year in the data list.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  val somes = data(index).flatten
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   126
  println(s"somes: $somes")
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  val somes_length = somes.length
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  if (somes_length == 0) balance
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  else {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
    val portion: Double = balance.toDouble / somes_length.toDouble
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   131
    println(s"portion: $portion")
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   132
    val b = balance + (for (x <- somes) yield (x * portion)).sum.toLong
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   133
    println(s"balance $b")
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   134
    println(s"profit ${(for (x <- somes) yield (x * portion)).sum}")
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   135
    b
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  }
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   139
val dd = List(List(Some(0.9)), List(Some(0.9)))
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   140
yearly_yield(dd, 100, 0)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   141
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   142
def yearly_yield2(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   143
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   144
    val share = balance/data(index).flatten.size.toDouble
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   145
    println(s"portion: $share")
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   146
    def count(data: List[Option[Double]], prof: Double, share: Double) : Double = data match{
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   147
        case Nil => prof
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   148
        case x::rest  => count(rest,prof + (x.getOrElse(0.0) * share), share)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   149
    }
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   150
    val balance_double = count(data(index), 0.0, share)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   151
    println(balance_double)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   152
    println(s"balance ${balance_double + balance}")
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   153
    println(s"profit $balance_double")
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   154
    //if (balance_double >= balance_double.toLong.toDouble+0.5) (balance_double + 1).toLong else balance_double.toLong
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   155
    (balance_double.toLong + balance)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   156
}
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   157
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   158
yearly_yield2(dd, 100, 0)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   159
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   160
// test case using the deltas calculated above
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   161
//println("Task 6 yield from Google and Apple in 2010 with  balance 100")
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   163
//val d0 = goog_aapl_deltas(0)(0)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   164
//val d1 = goog_aapl_deltas(0)(1)
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   165
//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: 229
diff changeset
   166
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   167
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   168
//val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   169
//val goog_aapl_yield = yearly_yield2(goog_aapl_deltas, 100, 0)
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   170
//println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   171
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   172
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   173
//yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
   174
//get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum
229
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   175
5549016ab10f updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   176
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
// (7) Write a function compound_yield that calculates the overall balance for a 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
//     range of years where in each year the yearly profit is compounded to the new 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
//     balances and then re-invested into our portfolio. For this use the function and 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
//     results generated under (6). The function investment calls compound_yield
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
//     with the appropriate deltas and the first index.
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  if (index >= data.length) balance else {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
    val new_balance = yearly_yield(data, balance, index)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
    compound_yield(data, new_balance, index + 1)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  }
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   191
def compound_yield2(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   192
  if (index >= data.length) balance else {
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   193
    val new_balance = yearly_yield2(data, balance, index)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   194
    compound_yield2(data, new_balance, index + 1)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   195
  }
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   196
}
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   197
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   202
def investment2(portfolio: List[String], years: Range, start_balance: Long): Long = {
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   203
  compound_yield2(get_deltas(get_prices(portfolio, years)), start_balance, 0)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   204
}
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
//test cases for the two portfolios given above
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   209
  println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   210
  println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   211
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   216
investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   217
investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   218
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   219
investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   220
investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   221
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   222
investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   223
investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   224
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   225
investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100)
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   226
investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   227
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   228
investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   229
investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   230
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   231
investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   232
investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   233
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 402
diff changeset
   234
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   236
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   237
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   238
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   239
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   240
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   241
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   242
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   243
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   244
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   245
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   246
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   247
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   248
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   249
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 384
diff changeset
   250