marking1/drumb.scala
changeset 266 ca48ac1d3c3e
parent 260 b4812c877b05
child 281 87b9e3e2c1a7
equal deleted inserted replaced
265:59779ce322a6 266:ca48ac1d3c3e
     1 // Advanvced Part 3 about a really dumb investment strategy
     1 // Main Part about a really dumb investment strategy
     2 //==========================================================
     2 //======================================================
     3 
     3 
     4 //object CW6c {
     4 //object CW6b { // for purposes of generating a jar
     5 
     5 
     6 
     6 
     7 //two test portfolios
     7 //two test portfolios
     8 
     8 
     9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
     9 val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU")
    10 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
    10 val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", 
    11                             "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") 
    11                             "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "HCP") 
    12 
       
    13 // (1) The function below should obtain the first trading price
       
    14 // for a stock symbol by using the query
       
    15 //
       
    16 //    http://ichart.yahoo.com/table.csv?s=<<symbol>>&a=0&b=1&c=<<year>>&d=1&e=1&f=<<year>> 
       
    17 // 
       
    18 // and extracting the first January Adjusted Close price in a year.
       
    19 
       
    20 
    12 
    21 import io.Source
    13 import io.Source
    22 import scala.util._
    14 import scala.util._
    23 
    15 
       
    16 // (1) The function below takes a stock symbol and a year as arguments.
       
    17 //     It should read the corresponding CSV-file and reads the January 
       
    18 //     data from the given year. The data should be collected in a list of
       
    19 //     strings for each line in the CSV-file.
       
    20 
    24 def get_january_data(symbol: String, year: Int) : List[String] = 
    21 def get_january_data(symbol: String, year: Int) : List[String] = 
    25   Source.fromFile(symbol ++ ".csv").getLines.toList.filter(_.startsWith(year.toString))
    22   Source.fromFile(symbol ++ ".csv")("ISO-8859-1").getLines.toList.filter(_.startsWith(year.toString))
    26 
    23 
       
    24 
       
    25 //test cases
       
    26 //blchip_portfolio.map(get_january_data(_, 2018))
       
    27 //rstate_portfolio.map(get_january_data(_, 2018))
       
    28 
       
    29 //get_january_data("GOOG", 1980)
       
    30 //get_january_data("GOOG", 2010)
       
    31 //get_january_data("FB", 2014)
       
    32 
       
    33 //get_january_data("PLD", 1980)
       
    34 //get_january_data("EQIX", 2010)
       
    35 //get_january_data("ESS", 2014)
       
    36 
       
    37 
       
    38 // (2) From the output of the get_january_data function, the next function 
       
    39 //     should extract the first line (if it exists) and the corresponding
       
    40 //     first trading price in that year with type Option[Double]. If no line 
       
    41 //     is generated by get_january_data then the result is None; Some if 
       
    42 //     there is a price.
    27 
    43 
    28 def get_first_price(symbol: String, year: Int) : Option[Double] = {
    44 def get_first_price(symbol: String, year: Int) : Option[Double] = {
    29   val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
    45   val data = Try(Some(get_january_data(symbol, year).head)) getOrElse None 
    30   data.map(_.split(",").toList(1).toDouble)
    46   data.map(_.split(",").toList(1).toDouble)
    31 }
    47 }
    32 
    48 
    33 get_first_price("GOOG", 1980)
    49 //test cases
    34 get_first_price("GOOG", 2010)
    50 //get_first_price("GOOG", 1980)
    35 get_first_price("FB", 2014)
    51 //get_first_price("GOOG", 2010)
       
    52 //get_first_price("FB", 2014)
       
    53 
       
    54 /*
       
    55 for (i <- 1978 to 2018) {
       
    56   println(blchip_portfolio.map(get_first_price(_, i)))
       
    57 }
       
    58 
       
    59 for (i <- 1978 to 2018) {
       
    60   println(rstate_portfolio.map(get_first_price(_, i)))
       
    61 }
       
    62 */ 
    36 
    63 
    37 
    64 
    38 // Complete the function below that obtains all first prices
    65 // (3) Complete the function below that obtains all first prices
    39 // for the stock symbols from a portfolio for the given
    66 //     for the stock symbols from a portfolio (list of strings) and 
    40 // range of years
    67 //     for the given range of years. The inner lists are for the
       
    68 //     stock symbols and the outer list for the years.
    41 
    69 
    42 def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
    70 def get_prices(portfolio: List[String], years: Range): List[List[Option[Double]]] = 
    43   for (year <- years.toList) yield
    71   for (year <- years.toList) yield
    44     for (symbol <- portfolio) yield get_first_price(symbol, year)
    72     for (symbol <- portfolio) yield get_first_price(symbol, year)
    45 
    73 
    46 
    74 
    47 // test case
    75 //test cases
    48 val p_fb = get_prices(List("FB"), 2012 to 2014)
       
    49 val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
       
    50 
    76 
    51 val tt = get_prices(List("BIDU"), 2004 to 2008)
    77 //println("Task 3 data from Google and Apple in 2010 to 2012")
       
    78 //val goog_aapl_prices = get_prices(List("GOOG", "AAPL"), 2010 to 2012)
       
    79 //println(goog_aapl_prices.toString ++ "\n")
    52 
    80 
    53 // (2) The first function below calculates the change factor (delta) between
    81 //val p_fb = get_prices(List("FB"), 2012 to 2014)
    54 // a price in year n and a price in year n+1. The second function calculates
    82 //val tt = get_prices(List("BIDU"), 2004 to 2008)
    55 // all change factors for all prices (from a portfolio).
    83 
       
    84 
       
    85 // (4) The function below calculates the change factor (delta) between
       
    86 //     a price in year n and a price in year n + 1. 
    56 
    87 
    57 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
    88 def get_delta(price_old: Option[Double], price_new: Option[Double]) : Option[Double] = {
    58   (price_old, price_new) match {
    89   (price_old, price_new) match {
    59     case (Some(x), Some(y)) => Some((y - x) / x)
    90     case (Some(x), Some(y)) => Some((y - x) / x)
    60     case _ => None
    91     case _ => None
    61   }
    92   }
    62 }
    93 }
    63 
    94 
       
    95 
       
    96 // (5) The next function calculates all change factors for all prices (from a 
       
    97 //     portfolio). The input to this function are the nested lists created by 
       
    98 //     get_prices above.
       
    99 
    64 def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
   100 def get_deltas(data: List[List[Option[Double]]]):  List[List[Option[Double]]] =
    65   for (i <- (0 until (data.length - 1)).toList) yield 
   101   for (i <- (0 until (data.length - 1)).toList) yield 
    66     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
   102     for (j <- (0 until (data(0).length)).toList) yield get_delta(data(i)(j), data(i + 1)(j))
    67 
   103 
    68 
   104 
       
   105 
    69 // test case using the prices calculated above
   106 // test case using the prices calculated above
    70 val d = get_deltas(p)
       
    71 val ttd = get_deltas(tt)
       
    72 
   107 
    73 // (3) Write a function that given change factors, a starting balance and a year
   108 //println("Task 5 change prices from Google and Apple in 2010 and 2011")
    74 // calculates the yearly yield, i.e. new balanace, according to our dump investment 
   109 //val goog_aapl_deltas = get_deltas(goog_aapl_prices)
    75 // strategy. Another function calculates given the same data calculates the
   110 //println(goog_aapl_deltas.toString ++ "\n")
    76 // compound yield up to a given year. Finally a function combines all 
   111 
    77 // calculations by taking a portfolio, a range of years and a start balance
   112 //val ttd = get_deltas(tt)
    78 // as arguments.
       
    79 
   113 
    80 
   114 
    81 def yearly_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
   115 // (6) Write a function that given change factors, a starting balance and an index,
    82   val somes = data(year).flatten
   116 //     calculates the yearly yield, i.e. new balance, according to our dumb investment 
       
   117 //     strategy. Index points to a year in the data list.
       
   118 
       
   119 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
       
   120   val somes = data(index).flatten
    83   val somes_length = somes.length
   121   val somes_length = somes.length
    84   if (somes_length == 0) balance
   122   if (somes_length == 0) balance
    85   else {
   123   else {
    86     val portion: Double = balance.toDouble / somes_length.toDouble
   124     val portion: Double = balance.toDouble / somes_length.toDouble
    87     balance + (for (x <- somes) yield (x * portion)).sum.toLong
   125     balance + (for (x <- somes) yield (x * portion)).sum.toLong
    88   }
   126   }
    89 }
   127 }
    90 
   128 
    91 def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = {
   129 // test case using the deltas calculated above
    92   if (year >= data.length) balance else {
   130 //println("Task 6 yield from Google and Apple in 2010 with  balance 100")
    93     val new_balance = yearly_yield(data, balance, year)
   131 
    94     compound_yield(data, new_balance, year + 1)
   132 //val d0 = goog_aapl_deltas(0)(0)
       
   133 //val d1 = goog_aapl_deltas(0)(1)
       
   134 //println(s"50 * ${d0.get} + 50 * ${d1.get} = ${50.toDouble * d0.get + 50.toDouble * d1.get}")
       
   135 
       
   136 
       
   137 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
       
   138 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
       
   139 
       
   140 
       
   141 //yearly_yield(get_prices(rstate_portfolio, 2016 to 2018), 100, 2) 
       
   142 //get_prices(rstate_portfolio, 2016 to 2018)(2).flatten.sum
       
   143 
       
   144 
       
   145 // (7) Write a function compound_yield that calculates the overall balance for a 
       
   146 //     range of years where in each year the yearly profit is compounded to the new 
       
   147 //     balances and then re-invested into our portfolio. For this use the function and 
       
   148 //     results generated under (6). The function investment calls compound_yield
       
   149 //     with the appropriate deltas and the first index.
       
   150 
       
   151 
       
   152 def compound_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
       
   153   if (index >= data.length) balance else {
       
   154     val new_balance = yearly_yield(data, balance, index)
       
   155     compound_yield(data, new_balance, index + 1)
    95   }
   156   }
    96 }
   157 }
    97 
       
    98 //yearly_yield(d, 100, 0)
       
    99 //compound_yield(d.take(6), 100, 0)
       
   100 
       
   101 //test case
       
   102 //yearly_yield(d, 100, 0)
       
   103 //yearly_yield(d, 225, 1)
       
   104 //yearly_yield(d, 246, 2)
       
   105 //yearly_yield(d, 466, 3)
       
   106 //yearly_yield(d, 218, 4)
       
   107 
       
   108 //yearly_yield(d, 100, 0)
       
   109 //yearly_yield(d, 125, 1)
       
   110 
   158 
   111 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   159 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   112   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   160   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
   113 }
   161 }
   114 
   162 
   115 /*
       
   116 val q1 = get_deltas(get_prices(List("GOOG", "AAPL", "BIDU"), 2000 to 2017))
       
   117 yearly_yield(q1, 100, 0)
       
   118 yearly_yield(q1, 100, 1)
       
   119 yearly_yield(q1, 100, 2)
       
   120 yearly_yield(q1, 100, 3)
       
   121 yearly_yield(q1, 100, 4)
       
   122 yearly_yield(q1, 100, 5)
       
   123 yearly_yield(q1, 100, 6)
       
   124 
   163 
   125 investment(List("GOOG", "AAPL", "BIDU"), 2004 to 2017, 100)
       
   126 val one = get_deltas(get_prices(rstate_portfolio, 1978 to 1984))
       
   127 val two = get_deltas(get_prices(blchip_portfolio, 1978 to 1984))
       
   128 
       
   129 val one_full = get_deltas(get_prices(rstate_portfolio, 1978 to 2017))
       
   130 val two_full = get_deltas(get_prices(blchip_portfolio, 1978 to 2017))
       
   131 
       
   132 one_full.map(_.flatten).map(_.sum).sum
       
   133 two_full.map(_.flatten).map(_.sum).sum
       
   134 
   164 
   135 //test cases for the two portfolios given above
   165 //test cases for the two portfolios given above
   136 
   166 
   137 //println("Real data: " + investment(rstate_portfolio, 1978 to 2017, 100))
   167 //println("Real data: " + investment(rstate_portfolio, 1978 to 2019, 100))
   138 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2017, 100))
   168 //println("Blue data: " + investment(blchip_portfolio, 1978 to 2019, 100))
   139 
   169 
   140 for (i <- 2000 to 2017) {
   170 //}
   141   println("Year " + i)
       
   142   //println("Real data: " + investment(rstate_portfolio, 1978 to i, 100))
       
   143   //println("Blue data: " + investment(blchip_portfolio, 1978 to i, 100))
       
   144   println("test: " + investment(List("GOOG", "AAPL", "BIDU"), 2000 to i, 100))
       
   145 }
       
   146 
   171 
   147 
   172 
   148 */ 
   173 
   149 //1984
       
   150 //1992
       
   151 //}