Capital Asset Pricing Model

Factor Model

Factor Models in Matrix Notation

Benefit of Diversification

data<-read.csv("stock_treasury.csv")
# Risk Free Rate is in percentage and annualised. 
# So the following conversion is required.
Rf<-data$UST_Yr_1/(100*250)
plot(ts(Rf),ylab="US Treasury 1 Year Yield")

n<-nrow(data)

## Compute log-return
ln_rt_snp500<-diff(log(data$SnP500))-Rf[2:n]
ln_rt_ibm<-diff(log(data$IBM_AdjClose))-Rf[2:n]
ln_rt_apple<-diff(log(data$Apple_AdjClose))-Rf[2:n]
ln_rt_msft<-diff(log(data$MSFT_AdjClose))-Rf[2:n]
ln_rt_intel<-diff(log(data$Intel_AdjClose))-Rf[2:n]

## log-return of the portfolio
ln_r <- cbind(ln_rt_ibm,ln_rt_apple,ln_rt_msft,ln_rt_intel)
head(ln_r)
##         ln_rt_ibm   ln_rt_apple  ln_rt_msft  ln_rt_intel
## [1,] -0.015870443 -2.858644e-02 -0.00924877 -0.011350577
## [2,] -0.021811910  8.413819e-05 -0.01479610 -0.018822896
## [3,] -0.006567005  1.391479e-02  0.01261529  0.020748139
## [4,]  0.021492855  3.769331e-02  0.02898454  0.018420655
## [5,]  0.004337269  1.063127e-03 -0.00844949  0.001897247
## [6,] -0.016930825 -2.495693e-02 -0.01258907 -0.004369651
Company IBM Apple Microsoft Intel
Weights 20% 30% 25% 25%
——– —— ——- ———– ——-
w = c(0.2,0.3,0.25,0.25)
ln_rt_portf = ln_r%*%w

capm_ibm<-lm(ln_rt_ibm~ln_rt_snp500)
capm_ibm_analysis<-coefficients(summary(capm_ibm))
capm_ibm_analysis <- round(capm_ibm_analysis,digits = 5)
rownames(capm_ibm_analysis)<-c("alpha","beta")
## Result of capm using lm() for IBM
capm_ibm_analysis
##       Estimate Std. Error  t value Pr(>|t|)
## alpha -0.00050    0.00058 -0.86605   0.3873
## beta   1.01601    0.05863 17.32801   0.0000
plot(ln_rt_snp500,ln_rt_ibm,xlab="S&P 500",ylab="ibm")
abline(capm_ibm,col="blue")
grid(col="red")
## Adjusted R-Squareed
summary(capm_ibm)$adj.r.squared
## [1] 0.5468333
rse<-summary(capm_ibm)$sigma
al <-capm_ibm$coefficients[1]-2*rse
b <- capm_ibm$coefficients[2]
abline(a= al,b= b,col=3,lty=2)
au <-capm_ibm$coefficients[1]+2*rse
abline(a=au,b=b,col=3,lty=2)