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