diff -r 3350cc06804b -r ecec79b9ab25 progs/drumb_sol.scala --- a/progs/drumb_sol.scala Thu Nov 09 16:34:08 2017 +0000 +++ b/progs/drumb_sol.scala Fri Nov 10 00:56:12 2017 +0000 @@ -10,14 +10,6 @@ val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") -// (1) The function below should obtain the first trading price -// for a stock symbol by using the query -// -// http://ichart.yahoo.com/table.csv?s=<>&a=0&b=1&c=<>&d=1&e=1&f=<> -// -// and extracting the first January Adjusted Close price in a year. - - import io.Source import scala.util._ @@ -35,10 +27,6 @@ get_first_price("FB", 2014) -// Complete the function below that obtains all first prices -// for the stock symbols from a portfolio for the given -// range of years - def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = for (year <- years.toList) yield for (symbol <- portfolio) yield get_first_price(symbol, year) @@ -50,9 +38,6 @@ val tt = get_prices(List("BIDU"), 2004 to 2008) -// (2) The first function below calculates the change factor (delta) between -// a price in year n and a price in year n+1. The second function calculates -// all change factors for all prices (from a portfolio). def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = { (price_old, price_new) match { @@ -70,13 +55,6 @@ val d = get_deltas(p) val ttd = get_deltas(tt) -// (3) Write a function that given change factors, a starting balance and a year -// calculates the yearly yield, i.e. new balanace, according to our dump investment -// strategy. Another function calculates given the same data calculates the -// compound yield up to a given year. Finally a function combines all -// calculations by taking a portfolio, a range of years and a start balance -// as arguments. - def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { val somes = data(year).flatten @@ -95,9 +73,6 @@ } } -yearly_yield(d, 100, 0) -//compound_yield(d.take(6), 100, 0) - def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) } @@ -106,8 +81,8 @@ //test cases for the two portfolios given above -//println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100)) -//println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100)) +println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100)) +println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100)) }