# Ledoit-Wolf portfolio weights calculation

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:

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?

• Without showing the code for cov_shrink there is no way to answer this question. – muffin1974 Nov 25 '16 at 15:08