main_templates1/drumb.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 08 Nov 2021 01:39:00 +0000
changeset 403 ffce7b61b446
parent 396 3ffe978a5664
child 428 cdfa6a293453
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     1
// Main Part 1 about a really dumb investment strategy
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     2
//===================================================
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     4
object M1 {
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
//two test portfolios
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     8
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     9
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    10
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    13
// (1) The function below takes a stock symbol and a year as arguments.
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    14
//     It should read the corresponding CSV-file and then extract the January 
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    15
//     data from the given year. The data should be collected in a list of
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    16
//     strings (one entry for each line in the CSV-file).
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    17
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    18
import io.Source
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    19
import scala.util._
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    20
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    21
def get_january_data(symbol: String, year: Int) : List[String] = ???
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    22
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    23
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    24
// (2) From the output of the get_january_data function, the next function 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    25
//     should extract the first line (if it exists) and the corresponding
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    26
//     first trading price in that year with type Option[Double]. If no line 
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    27
//     is generated by get_january_data then the result is None; and Some if 
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    28
//     there is a price.
135
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
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    31
def get_first_price(symbol: String, year: Int) : Option[Double] = ???
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    34
// (3) Complete the function below that obtains all first prices
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    35
//     for the stock symbols from a portfolio (list of strings) and 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    36
//     for the given range of years. The inner lists are for the
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
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
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    40
def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ???
135
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
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    44
// (4) The function below calculates the change factor (delta) between
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    45
//     a price in year n and a price in year n + 1. 
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    47
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
    48
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    49
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    50
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    51
// (5) The next function calculates all change factors for all prices (from a 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    52
//     portfolio). The input to this function are the nested lists created by 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    53
//     get_prices above.
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    54
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    55
def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = ???
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    56
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    59
// (6) Write a function that given change factors, a starting balance and an index,
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    60
//     calculates the yearly yield, i.e. new balance, according to our dumb investment 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    61
//     strategy. Index points to a year in the data list.
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    62
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    63
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ???
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    64
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    66
// (7) Write a function compound_yield that calculates the overall balance for a 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    67
//     range of years where in each year the yearly profit is compounded to the new 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    68
//     balances and then re-invested into our portfolio. For this use the function and 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    69
//     results generated under (6). The function investment calls compound_yield
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    70
//     with the appropriate deltas and the first index.
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    71
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    72
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ???
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
347
4de31fdc0d67 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    74
def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ???
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
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    78
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    79
//Test cases for the two portfolios given above
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    81
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    82
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    84
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
    85
}