Skip to content
Prev 7402 / 15274 Next

portfolio.optim and error in solve.QP: matrix D not positive definite

Hi Lui,

Without seeing the data this is just speculation but...

Are you sure you want t(x)? If you're mixing up your observations versus 
your assets this may explain the error.

The first parameter of portfolio.optim (in the tseries package) is a 
returns matrix, one column for each asset and one row for each day 
(assuming daily returns).  If you have this wrong then for your small 
datasets you'd have more columns than rows and this could produce that 
error.

Also, you don't have to pass the entire returns matrix to 
portfolio.optim, you could pass just the covariance matrix you calculate 
yourself and a vector (1-row matrix) of mean returns as follows:


library(tseries)
set.seed(2)
R <- matrix(rnorm(100*10),nrow=100,ncol=10) # 10 assets, 100 observations
averet <- matrix(apply(R,2,mean),nrow=1)
rcov <- cov(R)
current_er <- 0.05
(op <- portfolio.optim(x=averet,pm=current_er,covmat=rcov,riskless = 
FALSE,shorts = FALSE, rf = 0.0))

Hope this helps.

Best,

Guy
On 1/26/2011 7:51 PM, Lui ## wrote: