templates1/drumb.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 08 Nov 2018 23:42:03 +0000
changeset 199 54befaf23648
parent 145 d306102fd33b
child 266 ca48ac1d3c3e
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: 145
diff changeset
     1
// Part 2 and 3 about a really dumb investment strategy
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     2
//======================================================
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
//two test portfolios
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     7
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     8
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
     9
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    12
// (1) The function below takes a stock symbol and a year as arguments.
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    13
//     It should read the corresponding CSV-file and reads the January 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    14
//     data from the given year. The data should be collected in a list of
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    15
//     strings for each line in the CSV-file.
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    16
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    17
import io.Source
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    18
import scala.util._
132
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    19
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    20
//def get_january_data(symbol: String, year: Int) : List[String] = ...
2ca6db82d207 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    21
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    22
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    23
// (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
    24
//     should extract the first line (if it exists) and the corresponding
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    25
//     first trading price in that year with type Option[Double]. If no line 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    26
//     is generated by get_january_data then the result is None; Some if 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    27
//     there is a price.
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    28
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    33
// (3) Complete the function below that obtains all first prices
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    34
//     for the stock symbols from a portfolio (list of strings) and 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    35
//     for the given range of years. The inner lists are for the
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    36
//     stock symbols and the outer list for the years.
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    39
//def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ...
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    40
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    43
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    44
//==============================================
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    45
// Do not change anything below, unless you want 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    46
// to submit the file for the advanced part 3!
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    47
//==============================================
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    48
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
// (4) The function below calculates the change factor (delta) between
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    51
//     a price in year n and a price in year n + 1. 
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    53
//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
    54
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    55
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    56
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    57
// (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
    58
//     portfolio). The input to this function are the nested lists created by 
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    59
//     get_prices above.
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    60
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    61
//def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = ...
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    62
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    65
// (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
    66
//     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
    67
//     strategy. Index points to a year in the data list.
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    68
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    69
//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
    70
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    72
// (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
    73
//     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
    74
//     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
    75
//     results generated under (6). The function investment calls compound_yield
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    76
//     with the appropriate deltas and the first index.
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    77
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    78
//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
    79
135
077e63e96287 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    80
//def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ...
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
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
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    85
//Test cases for the two portfolios given above
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    87
//println("Real data: " + investment(rstate_portfolio, 1978 to 2018, 100))
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    88
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2018, 100))
129
b1a51285de7e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    90