#####Libraries ##### 

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tidyr)
library(dplyr)
library(broom)
library(ggplot2)
library(tibbletime)
## 
## Attaching package: 'tibbletime'
## The following object is masked from 'package:stats':
## 
##     filter
library(fpp2)
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v forecast  8.16     v expsmooth 2.3 
## v fma       2.4
## 
library(pdfetch)

Question 1: The problem set consist of two tasks.

1 (30 points). Consider a risky asset with the following string of rates of returns in the previous 5 years. 5 years ago it returned -10%, 4 years ago -20%, 3 years ago 30%, 2 years ago 0%, and 1 year ago 5%. In addition, there is a safe asset with a constant rate of return of 1% in each year. For the risky asset please compute and provide your computations:

  1. Mean returns and returns’ standard deviation (10 points).

find the mean using the average of historic rates of return: E(r) = 1/n(Summation(r(s))). The mean returns for this example are 0.01 or 1%

We can find the STD using the formula: STD = sqrt(1/n-1(Summation(rs-rhat)^2)

#finding mean
risky<- c(-.1, -0.2, .3, 0.0, 0.05)
sum(risky)/5
## [1] 0.01
# Mean returns are 0.01 

#finding standard deviation
(sum((risky - 0.01)^2))/4
## [1] 0.0355
#Standard deviation = 0.035 

Question 2:

2 (50 points). Using the Yahoo Finance download the SPY prices for the 7/1/2007-7/1/2009 period on weekly frequency. Using the software of your choice, please do the following tasks (and provide the relevant spreadsheet or code for the computations):

  1. compute weekly returns using the column “Adj. Close” (10 points)
#getting data 
prices <- pdfetch_YAHOO("SPY", from = "1997-06-01") 

#converting to weekly pricing 

returns <- 
  prices %>% 
  to.weekly(indexAt = "firstof", OHLC = FALSE) %>%
  # convert the index to a date
  data.frame(date = index(.)) %>%
  # now remove the index because it got converted to row names
  remove_rownames() %>%
  # convert from wide to long creating new variable asset that will assign ticker, conversion needed to create returns using lagged variables
  gather(asset,prices,-date) %>%
  # compute returns using the usual way
  group_by(asset) %>%  
  mutate(returns=(prices/lag(prices))-1) %>%
  # remove prices
  select(-prices) %>% 
  # convert back to wide by asset
  spread(asset, returns) %>% 
  # remove missings
  na.omit()

# back to long from wide
asset_returns_long <- 
  returns %>% 
  gather(asset, returns, -date)

# create a dummy for dates for corono
asset_returns_long$corona <- 0
asset_returns_long$corona[asset_returns_long$date > "2020-02-01"] <- 1
  1. plot the weekly return (10 points)

ggplot was used rather than autoplot so the conversion to a timeseries data set didn’t have to be used

returns %>% 
ggplot(aes(date, SPY.adjclose))+
  geom_line()

  1. compute average returns (10 points),

The below summary statistics show the stats as a whole which gives an average return of 0.023. It then shows the returns after covid took effect and this gave an average return of 0.0213. Then the last one is the returns before covid which gave an average return of 0.0232

############### basic summary stats
summary(asset_returns_long$returns)
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -0.889925 -0.013790  0.002622  0.023001  0.017352  4.930338
summary(asset_returns_long$returns[asset_returns_long$corona==1])
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -0.806229 -0.015310  0.005055  0.021281  0.020389  2.383266
summary(asset_returns_long$returns[asset_returns_long$corona==0])
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -0.889925 -0.013529  0.002469  0.023179  0.017058  4.930338
  1. compute returns’ standard deviation (10 points),

The below computations compute the standard deviation of the adjusted close manually, using the standard deviaiton function, the std during covid, and the std before covid.

############### estimated standard deviation
sqrt(sum((asset_returns_long$returns-mean(asset_returns_long$returns))^2/(length(asset_returns_long$returns)-1)))
## [1] 0.258679
sd(asset_returns_long$returns)
## [1] 0.258679
sd(asset_returns_long$returns[asset_returns_long$corona==1])
## [1] 0.2347787
sd(asset_returns_long$returns[asset_returns_long$corona==0])
## [1] 0.2610359

It is interesting to see that the standard deviation, which is supposed to help reflect risk, is lower during covid. This is in line with the lower average adjusted close price during covid as well.