3
$\begingroup$

I am trying to implement the Ledoit-Wolf minimum variance portfolio strategy on a real-world stock dataset.

library(quadprog)
library(Rsolnp)

#first I read in the data and the corresponding market rates:

data<-read.table("https://dl.dropboxusercontent.com/u/22681355/data.txt")
market.rate <-read.table("https://dl.dropboxusercontent.com/u/22681355/market.rate.txt")



sample.data<-data[1:120, ]
sample.market.rate<-market.rate[1:120]

# I calculate the Ledoit-Wulf portfolio strategy:

Te <- nrow(sample.data)
s2<-var(sample.market.rate)
estimates<-sapply(1:N, function(x) lm(sample.data[,x]~sample.market.rate ))
slopes<-sapply(1:N,function(x) estimates[,x]$coefficients[2])
    residuals<-sapply(1:N, function(x) estimates[,x]$residuals)
B<-as.vector(slopes)
D<-diag(N)
diag(D)<-diag(cov(residuals))

Fe<- s2 * B %*% t(B) + D
S.hat<-cov_shrink(Fe)
cov.Rt<-S.hat
inv.cov<-solve(cov.Rt)  
one.vec<-rep(1,N)   
weights<-as.vector(inv.cov%*%one.vec)/( t(one.vec) %*% inv.cov %*% one.vec)
} 

However, I get the following error:

Error in solve.default(cov.Rt) : 
  system is computationally singular: reciprocal condition number = 2.08164e-18

I must be doing something wrong because according to Ledoit Wolf their covariance estimator should not be computationally singular by construction.

Any ideas?

$\endgroup$
1
  • 1
    $\begingroup$ Without showing the code for cov_shrink there is no way to answer this question. $\endgroup$ Commented Nov 25, 2016 at 15:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.