Hi all,
I am trying to use the SharpeRatio Optimizer for the MeanVarianceOptimization constructor model. It works mostly fine, but every now and then I got the following error:
Runtime Error: ValueError : MaximumSharpeRatioPortfolioOptimizer.portfolio_variance: Volatility cannot be zero. This is caused by the covariance matrix, which is weird because I am trading always the same assets (at least 2 every time) and it is computed over 60 days for each of them, so it is very unlikely that the variance is actually zero.
Any suggestions?
Thank you for your help!
Alexandre Catarino
Hi Darthagnan ,
Could you please attach a backtest that we can use to reproduce the issue?
Note: This thread is the continuation of:
Problem with MeanVarianceOptimization #6811
Lorenzo Palmieri
Hi Alexandre,
Thank you very much for your reply. I think I figured the problem out, but I'de be grateful if you could give me your opinion about this.
Assuming that the variance is computed based on the magnitude prediction, I think the proble is that I am using Machine Learning models to forecast the price movements of the assets, and if there are not many variations the prediction is not updated, which gives raise to the error.
I guess that in orderto fix this I can just set the variance to an arbitraty small number or simply remove the error flag - I haven't check if var=0 could give raise to some divergence though, do you think it could create problems during the optimization procedure?
Thank you for your help!
Alexandre Catarino
Hi Darthagnan ,
If the variance is zero, maybe the optimizer should not be called the positions should be maintained.
Why is variance zero? Is it because covariance is a zero matrix? If so, the algorithm can add a test for zero covariance before the optimizer call, since variance minimum optimization can not be used in this case.
Lorenzo Palmieri
Hi Alexandre,
Thank you for your reply. Yes, the problem is with the covariance matrix. Am I right in saying that the covariance is calculated from the history of the predicted magnitudes rather than the mean of the historical prices?
Thank you for your help.
Alexandre Catarino
Hi Darthagnan ,
Sorry about the wait.
You are right, but it can be more general than that.
In the BlackLittermanOptimizationPortfolioConstructionModel, we use the covariance of predicted magnitudes.
However, we could the covariance of historical returns in other construction models.
Perhaps the algorithm should use the covariance of historical returns along with the predicted magnitude calculated by the machine learning model as the expected returns.
Lorenzo Palmieri
Dear Alexandre,
weights = self.optimizer.Optimize(returns) ----> weights = self.optimizer.Optimize(returns, Insight_Magnitudes)Thank you very much for your reply. Following your suggestion I have added a check that returns zero targets in the event of a zero covariance matrix. Thank you also for the suggestion about using the predicted magnitude as expected return in the optimizer, it sounds like a very sensible idea!
Just one last question, at the moment I am using the MeanVarianceOptimizationPortfolioConstructionModel (MVO) together with the MaximumSharpeRatioPortfolioOptimizer. So, If I change line 88 in MVO
The SharpeRatioOptimizer should use the covariance of the historical returns along with the predicted magnitude in the optimizatoin process. Is this correct?
Thank you very much for your help!
Alexandre Catarino
Hi Darthagnan ,
Yes, that is correct. In MaximumSharpeRatioPortfolioOptimizer.Optimize, the first argument is used to calculate the expected returns and the covariance unless those are provided in the second and third arguments respectively.
Lorenzo Palmieri
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!