templates/drumb.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 14 Nov 2017 13:14:47 +0000
changeset 144 716042628398
parent 135 077e63e96287
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
     1
// Advanced Part 3 about a really dumb investment strategy
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================================
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW6c {
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//two test portfolios
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     9
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
    10
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    14
// (1.a) The function below takes a stock symbol and a year as arguments.
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    15
//       It should read the corresponding CSV-file and read the January 
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    16
//       data from the given year. The data should be collected in a list of
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    17
//       strings for each line in the CSV-file.
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    18
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    19
import io.Source
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    20
import scala.util._
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    21
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    22
//def get_january_data(symbol: String, year: Int) : List[String] = ...
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    23
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    24
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    25
// (1.b) From the output of the get_january_data function, the next function 
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    26
//       should extract the first line (if it exists) and the corresponding
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    27
//       first trading price in that year as Option[Double]. If no line is 
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    28
//       generated by get_january_data then the result is None
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    29
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    34
// (1.c) Complete the function below that obtains all first prices
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    35
//       for the stock symbols from a portfolio (list of strings) and 
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    36
//       for the given range of years. The inner lists are for the
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    37
//       stock symbols and the outer list for the years.
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    40
//def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ...
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    41
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// (2) The first function below calculates the change factor (delta) between
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    45
//     a price in year n and a price in year n + 1. The second function calculates
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    46
//     all change factors for all prices (from a portfolio). The input to this
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    47
//     function are the nested lists created by get_prices above.
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    49
//def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ...
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    51
//def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = ...
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    52
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// (3) Write a function that given change factors, a starting balance and a year
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    56
//     calculates the yearly yield, i.e. new balance, according to our dump investment 
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    57
//     strategy. Another function calculates given the same data calculates the
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    58
//     compound yield up to a given year. Finally a function combines all 
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    59
//     calculations by taking a portfolio, a range of years and a start balance
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    60
//     as arguments.
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    61
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    63
//def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int) : Long = ... 
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    64
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    65
//def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int) : Long = ... 
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    67
//def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ...
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//test cases for the two portfolios given above
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    73
//investment(rstate_portfolio, 1978 to 2017, 100)
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    74
//investment(blchip_portfolio, 1978 to 2017, 100)
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}