---
title: "SEPTEMBER 2018"
author: "Solver"
date: "September 6, 2018"
output:
html_document:
toc: yes
html_notebook: default
pdf_document:
toc: yes
word_document:
toc: yes
---
```{r, echo=FALSE}
# SECTION 0
dataff <- read.table("ffdata.csv", stringsAsFactors = F, header = T, sep = ",")
# packages used
listofpackages <- c("aod","plm", "foreign", "lmtest", "systemfit", "dygraphs", "car", "sandwich","dplyr", "stats", "rugarch", "forecast", "dygraphs", "Rsolnp", "nloptr", "numDeriv", "spd", "xts", "zoo", "chron", "SkewHyperbolic", "expm", "Rcpp", "graphics", "grDevices", "utils", "rugarch", "urca")
for (j in listofpackages){
if(sum(installed.packages()[, 1] == j) == 0) {
install.packages(j)
}
library(j, character.only = T)
}
```
##(a) Please describe the purpose of SECTION 1 of the programme.
The first two lines of code in SECTION 1 generate two new columns called "port" and "port_m" in the dataset "dataff" through the function "array". To ensure that these two new columns can fit the existing dataset, they have a number of rows equal to the number of rows of "dataff" (this is the purpose of "dim = nrow(dataff)". For the moment, the new columns are filled with "NA". Then, the code initializes three new variables corresponding to the row numbers of "1962", "2000" and "2016" in the column "date". Finally, the code fills the columns "port" and "port_m" between 1962 and 2016 (excluded) through two "for" loops. Specifically, the column "port" collects the cumulated returns of a buy and hold portfolio which is invested in PR11 until "t1-1" and in PR15 until "t2-1". Such cumulated returns are then plotted against the cumulated retruns of the market over the same period.
----------------------------------------------------------------------------------
##(b) Modify the code to generate a graph in which all the values for the relevant variables are visible for the full sample.
By drawing a dynamic graph, we can see all the values (dygraph)
```{r}
dataff$port <- array(data = NA, dim = nrow(dataff))
dataff$port_m<- array(data = NA, dim = nrow(dataff))
t0 <- which(dataff$date == 1962)
t1 <- which(dataff$date == 2000)
t2 <- which(dataff$date == 2016)
dataff[t0, c("port","port_m")] <- 1 # don't need to repeat the value to make the array being assigned be of the same length. be careful though as it is one of the few cases of exception
for (i in (t0+1):(t1-1)) {
dataff$port[i] <- dataff$port[i-1] * (1 + dataff$PR11[i])
dataff$port_m[i] <- dataff$port_m[i-1] * (1 + dataff$r_mkt[i])
}
for (i in (t1):(t2-1)) {
dataff$port[i] <- dataff$port[i-1] * (1 + dataff$PR15[i])
dataff$port_m[i] <- dataff$port_m[i-1] * (1 + dataff$r_mkt[i])
}
plot(dataff$date[t0:(t2-1)],dataff$port[t0:(t2-1)],
type = "l", col = "red", ylim = c(0, 200),
ylab = "cumulative return", xlab="year")
lines(y =dataff$port_m[t0:(t2-1)] , x = dataff$date[t0:(t2-1)], col = "blue")
## ------------------------------------------------------------------------
```
##(c) Generate a single graph in which you plot two series: the value of a buy and hold investment in the market over the period 1962:1-2015:12 and the value of a a buy and hold investment in " the small and value FF portfolio ".
PR15
```{r}
dataff$port <- array(data = NA, dim = nrow(dataff))
dataff$port_m<- array(data = NA, dim = nrow(dataff))
t0 <- which(dataff$date == 1962)
t1 <- which(dataff$date == 2000)
t2 <- which(dataff$date == 2016)
dataff[t0, c("port","port_m")] <- 1
for (i in (t0+1):(t2-1)) {
dataff$port[i] <- dataff$port[i-1] * (1 + dataff$PR15[i])
dataff$port_m[i] <- dataff$port_m[i-1] * (1 + dataff$r_mkt[i])
}
plot(dataff$date[t0:(t2-1)],dataff$port[t0:(t2-1)],
type = "l", col = "red", ylim = c(0, 4500),
ylab = "cumulative return", xlab="year")
lines(y =dataff$port_m[t0:(t2-1)] , x = dataff$date[t0:(t2-1)], col = "blue")
```
##(d) Generate a single graph in which you plot two series: the monthly returns on the market over the period 1962:1-2015:12 and the monthly returns from investment in " the small and value FF portfolio "
```{r}
plot(dataff$date[t0:(t2-1)],dataff$PR15[t0:(t2-1)],
type = "l", col = "purple", ylim = c(-0.5, 0.5),
ylab = "cumulative return", xlab="year")
lines(y =dataff$r_mkt[t0:(t2-1)] , x = dataff$date[t0:(t2-1)], col = "green")
```
##(e) Compute
(a) the first two moments of the distribution of monthly returns of the market portfolio and the "small and value portfolio"
```{r}
mean_15=mean(dataff$PR15[t0:(t2-1)])
var_15=var(dataff$PR15[t0:(t2-1)])
mean_15
var_15
mean_mkt=mean(dataff$r_mkt[t0:(t2-1)])
mean_mkt
var_mkt=var(dataff$r_mkt[t0:(t2-1)])
var_mkt
```
##(b) the first two moments of the distribution of annualized monthly returns on the market portfolio and on the "small and value portfolio"
```{r}
mean_15_a=mean(dataff$r_pr15_a[t0:(t2-1)])
mean_15_a
var_15_a=var(dataff$r_pr15_a[t0:(t2-1)])
var_15_a
mean_mkt_a=mean(dataff$r_mkt_a[t0:(t2-1)])
mean_mkt_a
var_mkt_a=var(dataff$r_mkt_a[t0:(t2-1)])
var_mkt_a
```
##(c) the covariance and correlation matrices of the monthly returns on the two assets
```{r}
cov(dataff$r_mkt[t0:(t2-1)],dataff$PR15[t0:(t2-1)])
cor(dataff$r_mkt[t0:(t2-1)],dataff$PR15[t0:(t2-1)])
```
-----------------------------------------------------------------------------------------
##(a) Please describe the purpose of SECTION2 of the programme.
The purpose of the code is to test the statistical significance of the parameters in a CAPM model used to described the returns of "small" portfolios. Specifically, the "for" loop defines xmkt as the excess returns of the market and xasset as the returns of each small portfolio starting from PR11 in the first loop until PR15 in the last loop. Finally, a wald test is performed with null hypothesis H0: alpha=0 and beta=0 and alternative hypothesis H1: at least one of the two parameters is different from zero.
##(b) Provide a test of the null hypothesis that beta is equal to 1 in the CAPM for portfolio 15 (see code below)
H0: beta = 1
P(> W) = 0.024 is less than 0.05 significance level. Therefore we reject H0 and conclude that Beta in the CAPM is not equal to 1.
##(c) Provide a test of the null hypothesis that the average idiosyncratic risk for portfolio 15 is zero (see code below)
In order to test wehther the average idiosyncratic risk is zero (i.e. the average of estimated residuals is zero) it is sufficient to perform a traditional T-test on the intercept of the regression xcapm, the t-value of 6.77 leads to a rejection of the null.
```{r}
# Point (b)
xmkt <- subset(dataff, select = exret_mkt, date >= 1962 & date <= 2014 + 5/12 + 0.001)[, 1]
xasset <- subset(dataff, select = PR15, date >= 1962 & date <= 2014 + 5/12 + 0.001)[, 1]
xcapm <- lm(xasset ~ xmkt)
print("PR15")
print(summary(xcapm)$coefficients)
cat("\n")
print(wald.test(vcov(xcapm), coef(xcapm), H0 = c(1), df = 630-2,
L = matrix(c(0,1),ncol=2), verbose = T))
cat("------------------------------------------------------------")
cat("\n")
```
## (d) Compute the correlation matrix of the idiosyncratic risks associated to the five "small" FF portfolios
```{r}
modelchoice <- c(11, 12, 13, 14, 15)
for (i in modelchoice){
xmkt <- subset(dataff, select = exret_mkt, date >= 1962 & date <= 2014 + 5/12 + 0.001)[, 1]
xasset <- subset(dataff, select = paste("PR", i, sep = ''), date >= 1962 & date <= 2014 + 5/12 + 0.001)[, 1]
xcapm <- lm(xasset ~ xmkt)
if (i == 11) {
residuals = xcapm$residuals
} else {
residuals = cbind(residuals, xcapm$residuals)
}
}
print(cor(residuals))
```
##(e) Consider a risk-neutral investor who, using the full info available from 1962:1 and 2015:12, builds a portfolio with two risky assets: the market and the "small and value FF portfolio". Can you indicate the optimal weights of the two assets in this portfolio ?
```{r}
xmkt <- subset(dataff, select = r_mkt, date > 1962 & date < 2016)[, 1]
xasset <- subset(dataff, select = PR15, date > 1962 & date < 2016)[, 1]
print(mean(xmkt))
print(mean(xasset))
```
The risk neutral investor should invest only asset with highest expected returns, i.e. PR15