# Lecture 5 Analysis
# Author: Chris Hansman
# Email: chansman@imperial.ac.uk
# Date : 06/02/21
# Libraries
library(tidyverse)
library(broom)
# Loading Returns
all_returns <- read_csv("returns.csv")
#---------------------------------------------------
# CAPM Beta: Two Ways
#---------------------------------------------------
# Calculating Beta
#The Hard Way
#The time series for asset 1 (SPY)
returns_SPY <- all_returns %>%
filter(asset==”SPY”)
#Beta for SPY
beta_spy <- lm(returns ~ mkt_returns, data=returns_SPY)
summary(beta_spy)
#The time series for asset 2 (EFA)
returns_efa <- all_returns %>%
filter(asset==”EFA”)
#Beta for EFY
beta_efa <- lm(returns ~ mkt_returns, data=returns_efa)
summary(beta_efa)
tidy(beta_efa)
# And so on...
# A trick from the tidyverse:
returns_forbeta <- all_returns %>%
nest(-asset)
#Calculating all the betas
beta_assets <-map(returns_forbeta$data, ~ lm(returns ~ mkt_returns, data =.))
beta_assets
#Using the Nest Approach to Make Nice Looking Output
beta_assets <-
all_returns %>%
nest(-asset) %>%
mutate(model = map(data, ~ lm(returns ~ mkt_returns, data = .))) %>%
unnest(model%>% map(tidy)) %>%
filter(term == “mkt_returns”) %>%
select(-term)
#A similar approach to recover Residual SD by Group
residual_sd <- all_returns %>%
nest(-asset) %>%
mutate(fit = map(data, ~ lm(returns ~ mkt_returns, data = .)), results = map(fit, augment)) %>%
unnest(results) %>%
select(asset, returns, mkt_returns, .resid) %>%
group_by(asset)%>%
summarize(sd_r = sd(.resid), sd_m=sd(mkt_returns)) %>%
mutate(sd_r=sd_r*sqrt(59/58))
# Combining the Two
beta_assets <- beta_assets %>%
left_join(residual_sd, by=”asset”)
#—————————————————
# Creating Covariance Matrix
#—————————————————
#Extracting Elements
betas <- as.matrix(beta_assets$estimate)
var_r <- diag(as.vector(beta_assets$sd_r)^2)
var_m <- as.matrix(beta_assets$sd_m[1])^2
#Variance Covariance Matrix
sigma=betas%*%var_m%*%t(betas)+var_r
#---------------------------------------------------
# FF Betas:
#---------------------------------------------------
###....Your work here...
#---------------------------------------------------
# BARRA Industry Model
#---------------------------------------------------
X<-read.table("m-barra-9003.txt", header=TRUE)
# Demeaning Data
xbar = colMeans(X)
X<- X - rep(xbar, rep.int(nrow(X), ncol(X)))
#Creating Dummies: Finance, Tech and Other
fin = c(rep(1,4),rep(0,6))
tech = c(rep(0,4),rep(1,3),rep(0,3))
oth = c(rep(0,7),rep(1,3))
#The Actual B matrix
B=cbind(fin, tech,oth)
#Raw Covariance Matrix:
cov_return <- var(X)
corr_return <- cor(X)
#OLS By Hand:
F_hat<-solve(t(B)%*%B)%*%t(B)%*%t(X)
#Residuals
R_hat<-t(X)-B%*%F_hat
# Estimating Variance of Residuals
Psi <- apply(R_hat,1,var)
Psi_inv <- diag(Psi^(-1))
# GLS by Hand
F_hat_gls<-solve(t(B)%*%Psi_inv%*%B)%*%t(B)%*%Psi_inv%*%t(X)
#Residuals
e_hat_gls<-t(X)-B%*%F_hat_gls
# Estimating Variance of Residuals
Psi_hat <- apply(e_hat_gls,1,var)
# Estimating Covariance Matrix
cov_barra <- B%*%var(t(F_hat_gls))%*%t(B)+diag(Psi_hat)
#Correlation Matrix
corr_barra <- cov2cor(cov_barra)
# Porfolio Weights
omega<-solve(t(B)%*%Psi_inv%*%B)%*%t(B)%*%Psi_inv
#--------------------------------------------------#
#A bit of Plotting
#--------------------------------------------------#
#Converting to Tibble for ggplot
Factors <- as_tibble(t(F_hat_gls)) %>%
mutate(t=1)%>%
mutate(t=cumsum(t))
# Plotting
ggplot(data = Factors ) +
geom_line(aes(x = t, y = fin))