7 |
7 |
8 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU") |
8 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU") |
9 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", |
9 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", |
10 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") |
10 "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") |
11 |
11 |
12 |
|
13 // (1) The function below takes a stock symbol and a year as arguments. |
|
14 // It should read the corresponding CSV-file and then extract the January |
|
15 // data from the given year. The data should be collected in a list of |
|
16 // strings (one entry for each line in the CSV-file). |
|
17 |
|
18 import io.Source |
12 import io.Source |
19 import scala.util._ |
13 import scala.util._ |
20 |
14 |
|
15 // ADD YOUR CODE BELOW |
|
16 //====================== |
|
17 |
|
18 |
|
19 // (1) |
21 def get_january_data(symbol: String, year: Int) : List[String] = ??? |
20 def get_january_data(symbol: String, year: Int) : List[String] = ??? |
22 |
21 |
23 |
22 |
24 // (2) From the output of the get_january_data function, the next function |
23 // (2) |
25 // should extract the first line (if it exists) and the corresponding |
|
26 // first trading price in that year with type Option[Double]. If no line |
|
27 // is generated by get_january_data then the result is None; and Some if |
|
28 // there is a price. |
|
29 |
|
30 |
|
31 def get_first_price(symbol: String, year: Int) : Option[Double] = ??? |
24 def get_first_price(symbol: String, year: Int) : Option[Double] = ??? |
32 |
25 |
33 |
26 |
34 // (3) Complete the function below that obtains all first prices |
27 // (3) |
35 // for the stock symbols from a portfolio (list of strings) and |
|
36 // for the given range of years. The inner lists are for the |
|
37 // stock symbols and the outer list for the years. |
|
38 |
|
39 |
|
40 def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ??? |
28 def get_prices(portfolio: List[String], years: Range) : List[List[Option[Double]]] = ??? |
41 |
29 |
42 |
30 |
43 |
31 |
44 // (4) The function below calculates the change factor (delta) between |
32 // (4) |
45 // a price in year n and a price in year n + 1. |
|
46 |
|
47 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ??? |
33 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = ??? |
48 |
34 |
49 |
35 |
50 |
36 |
51 // (5) The next function calculates all change factors for all prices (from a |
37 // (5) |
52 // portfolio). The input to this function are the nested lists created by |
|
53 // get_prices above. |
|
54 |
|
55 def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = ??? |
38 def get_deltas(data: List[List[Option[Double]]]) : List[List[Option[Double]]] = ??? |
56 |
39 |
57 |
40 // (6) |
58 |
|
59 // (6) Write a function that given change factors, a starting balance and an index, |
|
60 // calculates the yearly yield, i.e. new balance, according to our dumb investment |
|
61 // strategy. Index points to a year in the data list. |
|
62 |
|
63 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ??? |
41 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ??? |
64 |
42 |
65 |
43 |
66 // (7) Write a function compound_yield that calculates the overall balance for a |
44 // (7) |
67 // range of years where in each year the yearly profit is compounded to the new |
|
68 // balances and then re-invested into our portfolio. For this use the function and |
|
69 // results generated under (6). The function investment calls compound_yield |
|
70 // with the appropriate deltas and the first index. |
|
71 |
|
72 def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ??? |
45 def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = ??? |
73 |
46 |
74 def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ??? |
47 def investment(portfolio: List[String], years: Range, start_balance: Long) : Long = ??? |
75 |
48 |
76 |
49 |