main_marking1/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 11:32:10 +0000
changeset 472 6a77c260c8a5
parent 463 0315d9983cd0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 388
diff changeset
     1
// Main Part 1 about a really dumb investment strategy
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     2
//===================================================
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     4
object M1 {
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//two test portfolios
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
import io.Source
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
import scala.util._
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    15
// ADD YOUR CODE BELOW
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    16
//======================
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    19
// (1) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    20
def get_january_data(symbol: String, year: Int) : List[String] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    21
  Try(Source.fromFile(s"${symbol}.csv").getLines.toList.filter(_.startsWith(year.toString))).getOrElse(Nil)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    22
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    23
// (2) 
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
def get_first_price(symbol: String, year: Int) : Option[Double] = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    25
  val list = get_january_data(symbol, year)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    26
  if (list == Nil) None else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    27
    Some(list.head.split(",").toList(1).toDouble)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    28
  }
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    32
// (3) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    33
def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    34
  for (n <- years.toList) yield{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    35
    for (m <- portfolio) yield{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    36
      get_first_price(m, n)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    37
    }
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    43
// (4) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    44
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    45
  if (price_old != None && price_new != None) Some((price_new.get - price_old.get) / price_old.get) else None
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    50
// (5) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    51
def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    52
  for (n <- data.tail) yield{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    53
    for (m <- n) yield{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    54
      get_delta(data(data.tail.indexOf(n))(n.indexOf(m)),m)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    55
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    56
  }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    57
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    58
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    59
// (6) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    60
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    61
  if(data.length == 0) balance else{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    62
    val equal = balance / data(index).flatten.length
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    63
    val list = for(n <- data(index).flatten) yield n * equal
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    64
    (balance + list.sum).toLong
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    65
  }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    66
}
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    68
// (7) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    69
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    70
  val deltas = get_deltas(data)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    71
  if (deltas.length == 0) balance else{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    72
      if(deltas.length - 1 == index) yearly_yield(deltas, balance, index) else compound_yield(data, yearly_yield(deltas, balance, index), index + 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    73
  }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    74
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    75
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    76
def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    77
  val list = get_prices(portfolio, years)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    78
  compound_yield(list, start_balance, 0)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    79
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    80
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    81
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    82
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    83
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    84
//Test cases for the two portfolios given above
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    85
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    86
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    87
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 388
diff changeset
    88
388
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 388
diff changeset
    94
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    95
// This template code is subject to copyright 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    96
// by King's College London, 2022. Do not 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    97
// make the template code public in any shape 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    98
// or form, and do not exchange it with other 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    99
// students under any circumstance.