Reputation: 37
The example data is provided below.
data <- data.frame(normal_return = c(0.02012, 0.03488, 0.00418, 0.01780, 0.00563, -0.00187),
log_return = c(0.01992, 0.03429, 0.00418, 0.01764, 0.00562, -0.00187))
How could I calculate the covariance matrix for the log return series, using both the returns and returns squared
Is that just simply using cov() function?
Upvotes: 0
Views: 824
Reputation: 1456
Making a call to cov()
is the appropriate way to obtain the variance-covariance matrix. The variances are on the diagonal and the covariances are represented by the off-diagonal elements.
my_cov <- cov(data)
> my_cov
normal_return log_return
normal_return 0.0001808887 0.0001779709
log_return 0.0001779709 0.0001751051
If you want to obtain the sd
, you should use the square root of the diagonal of this matrix.
> sqrt(diag(my_cov))
normal_return log_return
0.01344949 0.01323273
Edit
If you would like to also include the square of normal_return
in the covariance matrix, the easiest way to go about is creating a new column, which includes these values.
# new column: square of normal_return
data$sq_return <- data$normal_return^2
my_cov <- cov(data)
> my_cov
normal_return log_return sq_return
normal_return 1.808887e-04 0.0001779709 5.934746e-06
log_return 1.779709e-04 0.0001751051 5.828500e-06
sq_return 5.934746e-06 0.0000058285 2.170196e-07
If you would like to have the output in fixed instead of exponential/scientific notation, you can run options(scipen = 999)
.
One final note, if you would like to convert the covariance matrix into a correlation matrix, you may call to the cov2cor()
function and use a covariance matrix as its argument.
my_corr <- cov2cor(my_cov)
> my_corr
normal_return log_return sq_return
normal_return 1.0000000 0.9999858 0.9472112
log_return 0.9999858 1.0000000 0.9454920
sq_return 0.9472112 0.9454920 1.0000000
## cov2cor() scales a covariance matrix by its diagonal
## to become the correlation matrix.
Upvotes: 1