[R-SIG-Finance] portfolio.optim and error in solve.QP: matrix D not positive definite

krishna kriskumar at earthlink.net
Fri Jan 28 21:51:55 CET 2011


Also the link below might help with a large number of assets this is a  
common problem.

https://stat.ethz.ch/pipermail/r-sig-finance/2008q3/002854.html


Cheers
Krishna


On Jan 27, 2011, at 12:03 PM, Guy Yollin <gyollin at r-programming.org>  
wrote:

> 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:
>> Dear Group,
>>
>> I have  a large set of stocks and want to determine the efficient
>> frontier. The data set covers approx. 1.5 years and S&P 500 companies
>> (nothing weird). portfolio.optim from the PerformanceAnalytics  
>> package
>> works very well and fast. However, whenever I decrease the number of
>> stocks in the portfolio (to 10 or 400), I receive an error message:
>>
>> "solve.QP(Dmat, dvec, Amat, bvec = b0, meq = 2) :
>>   matrix D in quadratic function is not positive definite!"
>>
>> My command settings for portfolio.optim were:
>>
>> seed<- portfolio.optim(t(x), pm = current_er, riskless = FALSE,
>> shorts = FALSE, rf = 0.0)
>>
>> Even when I tried it with shorts = TRUE the error would still remain.
>> x is the set of stocks (stocks in columns, time in rows), current_er
>> is the target return (lies between the minimal mean and the maximum
>> mean of a long only portfolio).
>> I can not post the stock data here - so maybe you have some general
>> suggestions for me of what could have gone wrong... The covariance
>> matrix is positive definite. What could cause the problem? It works
>> fine with the large data set but does not work at all with the small
>> one...
>> Thanks a lot for your suggestions!
>>
>> Lui
>>
>> _______________________________________________
>> R-SIG-Finance at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R  
>> questions should go.
>>
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R  
> questions should go.



More information about the R-SIG-Finance mailing list