testing1/drumb.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 15 May 2018 10:57:20 +0100
changeset 175 526542d8d700
parent 171 4c9497ab5caa
child 199 54befaf23648
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
     1
// Advanvced Part 3 about a really dumb investment strategy
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================================
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW6c {
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", 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    13
// (1) The function below should obtain the first trading price
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    14
// for a stock symbol by using the query
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    15
//
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    16
//    http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    17
// 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    18
// and extracting the first January Adjusted Close price in a year.
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    19
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
import io.Source
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
import scala.util._
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    24
def get_january_data(symbol: String, year: Int) : List[String] = 
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    25
  Source.fromFile(symbol ++ ".csv").getLines.toList.filter(_.startsWith(year.toString))
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
def get_first_price(symbol: String, year: Int) : Option[Double] = {
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    29
  val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    30
  data.map(_.split(",").toList(1).toDouble)
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    33
get_first_price("GOOG", 1980)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    34
get_first_price("GOOG", 2010)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    35
get_first_price("FB", 2014)
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    37
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    38
// Complete the function below that obtains all first prices
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    39
// for the stock symbols from a portfolio for the given
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    40
// range of years
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    41
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    42
def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    43
  for (year <- years.toList) yield
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    44
    for (symbol <- portfolio) yield get_first_price(symbol, year)
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    47
// test case
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    48
val p_fb = get_prices(List("FB"), 2012 to 2014)
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    49
val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    50
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    51
val tt = get_prices(List("BIDU"), 2004 to 2008)
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    52
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    53
// (2) The first function below calculates the change factor (delta) between
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    54
// a price in year n and a price in year n+1. The second function calculates
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    55
// all change factors for all prices (from a portfolio).
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    56
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    57
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
    58
  (price_old, price_new) match {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    59
    case (Some(x), Some(y)) => Some((y - x) / x)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    60
    case _ => None
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    61
  }
161
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    62
}
6ea450e999e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    63
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    64
def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    65
  for (i <- (0 until (data.length - 1)).toList) yield 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    66
    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
    67
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    69
// test case using the prices calculated above
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    70
val d = get_deltas(p)
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    71
val ttd = get_deltas(tt)
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// (3) Write a function that given change factors, a starting balance and a year
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    74
// calculates the yearly yield, i.e. new balanace, according to our dump investment 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    75
// strategy. Another function calculates given the same data calculates the
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    76
// compound yield up to a given year. Finally a function combines all 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    77
// calculations by taking a portfolio, a range of years and a start balance
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    78
// as arguments.
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    81
def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    82
  val somes = data(year).flatten
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    83
  val somes_length = somes.length
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    84
  if (somes_length == 0) balance
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    85
  else {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    86
    val portion: Double = balance.toDouble / somes_length.toDouble
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    87
    balance + (for (x <- somes) yield (x * portion)).sum.toLong
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    88
  }
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    89
}
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 compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    92
  if (year >= data.length) balance else {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    93
    val new_balance = yearly_yield(data, balance, year)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    94
    compound_yield(data, new_balance, year + 1)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    95
  }
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    96
}
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    97
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    98
//yearly_yield(d, 100, 0)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
    99
//compound_yield(d.take(6), 100, 0)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   100
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   101
//test case
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   102
//yearly_yield(d, 100, 0)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   103
//yearly_yield(d, 225, 1)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   104
//yearly_yield(d, 246, 2)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   105
//yearly_yield(d, 466, 3)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   106
//yearly_yield(d, 218, 4)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   107
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   108
//yearly_yield(d, 100, 0)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   109
//yearly_yield(d, 125, 1)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   110
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   111
def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   112
  compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
130
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
}
7f3f01dfe738 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   115
/*
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   116
val q1 = get_deltas(get_prices(List("GOOG", "AAPL", "BIDU"), 2000 to 2017))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   117
yearly_yield(q1, 100, 0)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   118
yearly_yield(q1, 100, 1)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   119
yearly_yield(q1, 100, 2)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   120
yearly_yield(q1, 100, 3)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   121
yearly_yield(q1, 100, 4)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   122
yearly_yield(q1, 100, 5)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   123
yearly_yield(q1, 100, 6)
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
investment(List("GOOG", "AAPL", "BIDU"), 2004 to 2017, 100)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   126
val one = get_deltas(get_prices(rstate_portfolio, 1978 to 1984))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   127
val two = get_deltas(get_prices(blchip_portfolio, 1978 to 1984))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   128
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   129
val one_full = get_deltas(get_prices(rstate_portfolio, 1978 to 2017))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   130
val two_full = get_deltas(get_prices(blchip_portfolio, 1978 to 2017))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   131
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   132
one_full.map(_.flatten).map(_.sum).sum
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   133
two_full.map(_.flatten).map(_.sum).sum
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   134
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   135
//test cases for the two portfolios given above
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   136
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   137
//println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   138
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
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
for (i <- 2000 to 2017) {
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   141
  println("Year " + i)
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   142
  //println("Real data: " + investment(rstate_portfolio, 1978 to i, 100))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   143
  //println("Blue data: " + investment(blchip_portfolio, 1978 to i, 100))
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   144
  println("test: " + investment(List("GOOG", "AAPL", "BIDU"), 2000 to i, 100))
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 138
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
*/ 
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   149
//1984
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   150
//1992
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   151
}