You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
try:
Hinv=np.linalg.inv(-1*self.hessian(xopt))
except:
#might want custom warning ResultsWarning? NumericalWarning?fromwarningsimportwarnwarndoc= ('Inverting hessian failed, no bse or ''cov_params available')
warn(warndoc, RuntimeWarning)
Hinv=None
is inadequate and only wil have issues when eigenvalues are very close to zero. It doesn't verify that all eigenvalues have the same sign (positive, for -1 * self.hessian(xopt)). A better check is to ensure that the minimum eigenvale of -1 * self.hessian(xopt) is sufficiently positive and to remove the try-catch block since it should not be hit when using a PD hessian.
Given the large positive and negative eigenvalues, this is not just a numerical precision problem, but seems to be more serious convergence problems.
My suggestion is to add a Warning in 0.9 and then investigate some cases.
As solution for users, I would eventually set invalid hessians to nans or None similar to the case when linalg.inv fails. The users or developers can always call the hessian method again to compute it for further investigation. But we don't want to raise an exception with convergence failures so users and we can investigate where and why it gets stuck.
Two special cases:
IIRC, I have seen a messed up hessian for extra parameters while the linear part parameters had a proper positive definite submatrix, e.g. NegativeBinomial (IIRC).
In some cases, we have singular hessian by design. With a numerical precision problem this might result in slightly negative eigenvalues of the hessian, e.g. -1e-15. I don't remember seeing a case with small negative, but I think that's likely to show up. cov_params reduced rank - consequences #1717singular exog - check #146 (has example with -1.9e-15)
Chad had problems in statespace models with the hessian and added OPG (outer product of gradient) for the cov_params which is by definition positive (semi) definite.
Related aside (I don't find an issue that mentions it): Stata uses in some cases the rank of cov_params instead of the rank of exog.
I think we could replace inv with an svd or eig to be able to calculate and inv and get the information about eigenvalues/rank at the same time.
This would allow us to get the rank and work towards more consistency in using pinv versus inv for cov_params. (possibly with keyword like singular_ok = True, which is used in some cases in Stata, IIRC with different use cases.)
Related: several models calculate the cov_params in fit directly instead of doing it in base fit.
Activity
bashtage commentedon Jul 5, 2016
When PD is enforced, there are at least 131 failing tests:
https://travis-ci.org/bashtage/statsmodels/jobs/142324304
bashtage commentedon Jul 5, 2016
Most failures are in
duration,discreteandmiscmodelskshedden commentedon Jul 5, 2016
I wasn't immediately able to observe any problems in duration with Hessian inversion. May be numpy version or platform dependent?
bashtage commentedon Jul 5, 2016
The estimated Hessian in these tests is indefinite:
The two numbers on each line are the maximum and minimum eigenvalue. The minimum should be > 0 if
josef-pkt commentedon Jul 5, 2016
as reference: above test failures are produced by
#3084 (comment)
josef-pkt commentedon Jul 5, 2016
Given the large positive and negative eigenvalues, this is not just a numerical precision problem, but seems to be more serious convergence problems.
My suggestion is to add a Warning in 0.9 and then investigate some cases.
As solution for users, I would eventually set invalid hessians to nans or None similar to the case when linalg.inv fails. The users or developers can always call the hessian method again to compute it for further investigation. But we don't want to raise an exception with convergence failures so users and we can investigate where and why it gets stuck.
Two special cases:
cov_params reduced rank - consequences #1717 singular exog - check #146 (has example with -1.9e-15)
Chad had problems in statespace models with the hessian and added OPG (outer product of gradient) for the cov_params which is by definition positive (semi) definite.
Related aside (I don't find an issue that mentions it): Stata uses in some cases the rank of cov_params instead of the rank of exog.
kshedden commentedon Jul 6, 2016
Don't you mean like below? In that case, I get no errors.
bashtage commentedon Jul 6, 2016
Yes, indeed. I was checking wrong output. Face palm is the correct expression I think.
kshedden commentedon Jul 6, 2016
We might still want to include the check, but I think a failed Cholesky factorization is a more efficient way to go (compared to calling eig).
josef-pkt commentedon Jul 6, 2016
I think we could replace inv with an svd or eig to be able to calculate and inv and get the information about eigenvalues/rank at the same time.
This would allow us to get the rank and work towards more consistency in using pinv versus inv for cov_params. (possibly with keyword like singular_ok = True, which is used in some cases in Stata, IIRC with different use cases.)
Related: several models calculate the cov_params in fit directly instead of doing it in base fit.
TST: Add test for PD hessian
TST: Add test for PD hessian
TST: Add test for PD hessian
add basic distributed_estimation notebook