415
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Main Part 1 about a really dumb investment strategy
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//===================================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
object M1 {
|
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 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
// (1) The function below takes a stock symbol and a year as arguments.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
// It should read the corresponding CSV-file and then extract the January
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
// data from the given year. The data should be collected in a list of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
// strings (one entry for each line in the CSV-file).
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
import io.Source
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
import scala.util._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
def get_january_data(symbol: String, year: Int) : List[String] = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
// (2) From the output of the get_january_data function, the next function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
// should extract the first line (if it exists) and the corresponding
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
// first trading price in that year with type Option[Double]. If no line
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
// is generated by get_january_data then the result is None; and Some if
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
// there is a price.
|
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 |
def get_first_price(symbol: String, year: Int) : Option[Double] = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
// (3) Complete the function below that obtains all first prices
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
// for the stock symbols from a portfolio (list of strings) and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
// for the given range of years. The inner lists are for the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
// stock symbols and the outer list for the years.
|
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 |
def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
// (4) The function below calculates the change factor (delta) between
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
// a price in year n and a price in year n + 1.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
// (5) The next function calculates all change factors for all prices (from a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
// portfolio). The input to this function are the nested lists created by
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
// get_prices above.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
// (6) Write a function that given change factors, a starting balance and an index,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
// calculates the yearly yield, i.e. new balance, according to our dumb investment
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
// strategy. Index points to a year in the data list.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
// (7) Write a function compound_yield that calculates the overall balance for a
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
// range of years where in each year the yearly profit is compounded to the new
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
// balances and then re-invested into our portfolio. For this use the function and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
// results generated under (6). The function investment calls compound_yield
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
// with the appropriate deltas and the first index.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
//Test cases for the two portfolios given above
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
//println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
//println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
}
|