Below is an example of how using an index model to calculate the covariance between two stock's returns can understate the actual covariance if there are industry effects unaccounted for in the model.

``````library(quantmod)
xom <- getSymbols('XOM', auto.assign = F)
bp <- getSymbols('BP', auto.assign = F)
mkt <- getSymbols('^GSPC', auto.assign = F)
reg.x <- lm(ClCl(xom['2015/'])[-1] ~ ClCl(mkt['2015/'])[-1])
reg.b <- lm(ClCl(bp['2015/'])[-1] ~ ClCl(mkt['2015/'])[-1])

## residual correlation
cor(reg.x\$resid, reg.b\$resid)
``````
``````## [1] 0.6524235
``````
``````## annual covariance by index model
reg.x\$coef[2]*reg.b\$coef[2]*(sd(ClCl(mkt['2015/'])[-1])^2)*252
``````
``````## ClCl(mkt["2015/"])[-1]
##             0.03088164
``````
``````## annual covariance
cov(ClCl(xom['2015/'])[-1], ClCl(bp['2015/'])[-1])*252
``````
``````##                   ClCl.bp["2015/"]
## ClCl.xom["2015/"]       0.05153118
``````

## Assignment:

Try the above for two stock's that are not in the same industry. Can you explain the results in relation to the above results?