load("dataset.RData") load("SP500.RData") #COMPUTE THE BETA OF THE STOCKS #to get the excess market return we use the average excess return of the S&P 500 SP500_ex <- SP500$SP500 - DJIA_ex$Rfree #now we compute AXP's beta #first we plot its returns against the returns of the market for illustration purposes plot(SP500_ex, DJIA_ex$AXP, pch=19, xlab="S&P 500 excess returns", ylab="AXP excess returns", main="AXP") #we compute AXP's beta (and Jensen's alpha) by regressing its excess returns on the market's excess returns AXP_reg <- lm(DJIA_ex$AXP ~ SP500_ex) AXP_alpha <- AXP_reg$coefficients[1] AXP_beta <- AXP_reg$coefficients[2] #now we plot the regressed line on the previous plot abline(AXP_reg, col = "red", lwd=2) #the line is pretty flat because the beta is low. let's try with BA plot(SP500_ex, DJIA_ex$BA, pch=19, xlab="S&P 500 excess returns", ylab="BA excess returns", main="BA") BA_reg <- lm(DJIA_ex$BA ~ SP500_ex) BA_alpha <- BA_reg$coefficients[1] BA_beta <- BA_reg$coefficients[2] abline(BA_reg, col = "red", lwd=2) #the line is steeper because BA has a higher beta. let's compare the mean return of the two stocks AXP_mean <- mean(DJIA$AXP) BA_mean <- mean(DJIA$BA) AXP_mean BA_mean #BA has a higher mean return than AXP, in line with the theory (higher beta should imply higher return) #now we compute all the betas betas_stocks <- vector() DJIA_ex_ret <- DJIA_ex[,-c(1,2)] for (i in 1:ncol(DJIA_ex_ret)) { model <- lm(DJIA_ex_ret[,i] ~ SP500_ex) betas_stocks[i] <- model$coefficients[2] } names(betas_stocks) <- names(DJIA_ex_ret) #PLOT THE CAPITAL MARKET LINE AND THE SECURITY MARKET LINE #for the first plot we need the expected return and SD of the risk-free rate, the stocks and the S&P 500 #we create a vector with the mean risk-free rate and one with the mean market excess return rfree_mean <- rep(mean(DJIA$Rfree),length(betas_stocks)) SP500_ex_mean <- rep(mean(SP500_ex),length(betas_stocks)) #then we apply the CAPM formula for the expected return E_ret_stocks <- rfree_mean + betas_stocks*SP500_ex_mean #we still need the expected return of the risk-free rate and of the SP500 #the first is equal to the mean risk-free rate E_ret_rfree <- mean(DJIA$Rfree) names(E_ret_rfree) <- "T-bills" #the second is equal to the mean return of the SP500 E_ret_SP500 <- mean(SP500$SP500) names(E_ret_SP500) <- "S&P 500" #finally we join all the expected returns in one object E_ret <- c(E_ret_rfree,E_ret_SP500,E_ret_stocks) #now we compute the SD of the stocks, T-bills and S&P 500 DJIA_sd <- apply(DJIA[,-c(1,2)], 2, sd) rfree_sd <- sd(DJIA$Rfree) names(rfree_sd) <- "T-bills" SP500_sd <- sd(SP500$SP500) names(SP500_sd) <- "S&P 500" #and we join them together sds <- c(rfree_sd,SP500_sd,DJIA_sd) plot(sds, E_ret, pch=19, xlab="Standard deviation", ylab="Expected return", main="Capital Market Line") #we color in blue the S&P 500 ("market portfolio") and in green the T-bill ("risk-free") points(SP500_sd, E_ret_SP500, pch=19, col="blue3") points(rfree_sd, E_ret_rfree, pch=19, col="green4") #now we plot the capital market line abline(coef = c(coef(lm(c(E_ret_rfree, E_ret_SP500) ~ c(rfree_sd, SP500_sd)))), lwd=2, col="orange2") #to plot the security market line we replace the standard deviation with the betas #the beta of the risk-free is 0 and the one of the one of the market is 1 betas <- c(0,1,betas_stocks) names(betas)[1] <- "T-bills" names(betas)[2] <- "S&P 500" plot(betas, E_ret, pch=19, xlab="Beta", ylab="Expected return", main="Security Market Line") #we color in blue the S&P 500 ("market portfolio") and in green the T-bill ("risk-free") points(1, E_ret_SP500, pch=19, col="blue3") points(0, E_ret_rfree, pch=19, col="green4") #now we plot the security market line abline(coef = c(coef(lm(c(E_ret_rfree, E_ret_SP500) ~ c(0, 1)))), lwd=2, col="orange2")