progs/drumb_sol.scala
changeset 26 a7afc2540a88
parent 18 87e55eb309ed
child 39 c6fe374a5fca
--- a/progs/drumb_sol.scala	Wed Nov 09 15:07:23 2016 +0000
+++ b/progs/drumb_sol.scala	Thu Nov 10 00:15:14 2016 +0000
@@ -1,5 +1,5 @@
-// Advanvced Part 3 about Mr T. Drumb investing into stocks
-//==========================================================
+// Advanvced Part 3 about really dump investing strategy
+//=======================================================
 
 //two test portfolios
 
@@ -7,6 +7,12 @@
 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CBG", "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=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> 
+// 
+// and extracting the first January Adjusted Close price in a year.
 
 import io.Source
 import scala.util._
@@ -25,13 +31,24 @@
   data.map(_.last.split(",").toList(6).toDouble)
 }
 
+
+// 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)
 
-get_prices(List("GOOG", "AAPL"), 2010 to 2012)
+
+// test case
+//val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
 
 
+// (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 {
     case (Some(x), Some(y)) => Some((y - x) / x)
@@ -43,6 +60,19 @@
   for (i <- (0 until (data.length - 1)).toList) yield 
     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
 
+
+// test case using the prices calculated above
+//val d = get_deltas(p)
+
+
+// (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
   val somes_length = somes.length
@@ -53,6 +83,9 @@
   }
 }
 
+//test case
+//yearly_yield(d, 100, 0)
+
 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
   if (year >= data.length) balance else {
     val new_balance = yearly_yield(data, balance, year)
@@ -60,15 +93,13 @@
   }
 }
 
-val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
-val d = get_deltas(p)
-yearly_yield(d, 100, 0)
-
 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
 }
 
 
+//test cases for the two portfolios given above
+
 println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100))
 println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100))