templates1/drumb.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 21 Nov 2017 16:31:11 +0000
changeset 152 16dbc95d7d77
parent 145 155a7e41615e
child 199 2e13dedd922e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
     1
// Advanced Part 3 about a really dumb investment strategy
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==========================================================
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW6c {
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//two test portfolios
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
144
41a2b4f2c30c updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
     9
val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
41a2b4f2c30c updated
Christian Urban <urbanc@in.tum.de>
parents: 135
diff changeset
    10
val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
                            "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
135
e59aebee9770 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.
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    15
//       It should read the corresponding CSV-file and read the January 
e59aebee9770 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
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    17
//       strings for each line in the CSV-file.
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    18
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    19
import io.Source
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    20
import scala.util._
132
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    21
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    22
//def get_january_data(symbol: String, year: Int) : List[String] = ...
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    23
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    24
132
d91f907b6ed3 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 
d91f907b6ed3 updated
Christian Urban <urbanc@in.tum.de>
parents: 129
diff changeset
    26
//       should extract the first line (if it exists) and the corresponding
d91f907b6ed3 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
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    28
//       generated by get_january_data then the result is None
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    29
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
//def get_first_price(symbol: String, year: Int) : Option[Double] = ...
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    34
// (1.c) Complete the function below that obtains all first prices
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    35
//       for the stock symbols from a portfolio (list of strings) and 
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    36
//       for the given range of years. The inner lists are for the
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    37
//       stock symbols and the outer list for the years.
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    40
//def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ...
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    41
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// (2) The first function below calculates the change factor (delta) between
135
e59aebee9770 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
e59aebee9770 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
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    47
//     function are the nested lists created by get_prices above.
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
135
e59aebee9770 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
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    51
//def get_deltas(data: List[List[Option[Double]]]) :  List[List[Option[Double]]] = ...
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    52
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
1b0f1573c27c 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
e59aebee9770 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 
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    57
//     strategy. Another function calculates given the same data calculates the
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    58
//     compound yield up to a given year. Finally a function combines all 
e59aebee9770 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
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    60
//     as arguments.
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    61
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
135
e59aebee9770 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 = ... 
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    64
e59aebee9770 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
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    67
//def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ...
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//test cases for the two portfolios given above
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
135
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    73
//investment(rstate_portfolio, 1978 to 2017, 100)
e59aebee9770 updated
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
    74
//investment(blchip_portfolio, 1978 to 2017, 100)
129
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
1b0f1573c27c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}