# Lecture 5 Analysis
# Author: Chris Hansman
# Email: [email protected]
# 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 Outputbeta_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 Matrixsigma=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 Dataxbar = colMeans(X)X<- X – rep(xbar, rep.int(nrow(X), ncol(X)))#Creating Dummies: Finance, Tech and Otherfin = 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 matrixB=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)#ResidualsR_hat<-t(X)-B%*%F_hat# Estimating Variance of ResidualsPsi <- apply(R_hat,1,var)Psi_inv <- diag(Psi^(-1))# GLS by HandF_hat_gls<-solve(t(B)%*%Psi_inv%*%B)%*%t(B)%*%Psi_inv%*%t(X)#Residualse_hat_gls<-t(X)-B%*%F_hat_gls# Estimating Variance of ResidualsPsi_hat <- apply(e_hat_gls,1,var)# Estimating Covariance Matrixcov_barra <- B%*%var(t(F_hat_gls))%*%t(B)+diag(Psi_hat)#Correlation Matrixcorr_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 ggplotFactors <- as_tibble(t(F_hat_gls)) %>%
mutate(t=1)%>%
mutate(t=cumsum(t))
# Plotting
ggplot(data = Factors ) +
geom_line(aes(x = t, y = fin))
Reviews
There are no reviews yet.