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
x.summary() and x3.summary() give about the same results. x2.summary() gives you something really different... the params and z-scores are much different.
I don't fully understand the following code, but if you use the scipy optimizers to fit the GLM, and the hessian is not positive-definate, the code refuses to attempt to invert the matrix.
I think this behavior is correct (when you override this logic, the z-scores you get are just, well, weird.) However, the code sets Hinv = None, and this line of code then tries to divide None / scale, which returns an error.
Is there a reason why the optimizers use the observed information matrix and not the expected for GLM?
When I changed this line to append observed=False, I was able to get newton to match irls pretty much perfectly. The math appears to be (essentially) identical in that case.
From an examination with my tweedie data, the first few iterations of the 'newton' scipy method yield a non-positive definite Hessian... and its breaks down from there. When using the expected hessian, it was positive definite all the way and I was able to fit (and was about 20 seconds faster on my large-ish data.)
expected information matrix has more restrictive assumptions, and observed, i.e. Hessian is the appropiate default method used with the optimizers.
However, a missing feature is to allow a choice of an alternative to the hessian, either EIM or OPG (outer product of gradient). Both EIM and OPG are usually numerically more stable, but might not be and estimate of the hessian when there is misspecification.
Specific to GLM: OIM = EIM with canonical links and correctly specified model. I'm not sure what the relationship betwe EIM and OPG is in GLM or whether they are the same in this case.
This is still an unfinished area, currently irls uses the implied EIM and gradient optimization uses hessian.
(The only place where this is a choice in statsmodels is in the statespace models where OPG works much better than hessian which can often fail, among other reasons because there are extra parameters that might be more difficult to estimate)
about the convergence problem with newton: Newton often needs to be in the neighborhood of the solution to work reliably. That's why we use irls as an initial estimator to get the start_params for newton.
scipy has now and is getting better newton methods that should be more stable than our own newton.
Optimization speed: hessian is also more expensive to compute that OPG and if available EIM. Stata or maybe some other packages switch optimizers during optimization, e.g. OPG at the beginning and Hessian at the end. (I don't remember where I saw this.)
Essentially, this is another area where we need options for improvements.
Why do you use newton early on in your optimization?
For example, using lbfgsb and then newton, or irls and then newton will be faster and more robust in many cases.
Looking at the code for #3548, I just saw that hessian has observed keyword, but AFAIR we don't use EIM.
It looks like it just needs to be connected to fit and cov_type options.
In base.LikelihoodModel.fit we have hess = lambda params, *args: -self.hessian(params, *args) / nobs which allows for args but not for keywords to be transmitted to loglike, score and hessian.
For reparameterization we use an internal attribute instead of a keyword.
(aside: the fit method as a unqualified except
model = smf.glm('affairs ~ age + rate_marriage', data=data.data, exposure=data.data['Exposure'],
family=sm.families.Tweedie(link=sm.families.links.Power(1), var_power=1.5))
raises an exception in current master that exposure is only allowed with log linkg
the last part is
m:\...\statsmodels\genmod\generalized_linear_model.py in _check_inputs(self, family, offset, exposure, endog, freq_weights, var_weights)
382 if exposure is not None:
383 if not isinstance(self.family.link, families.links.Log):
--> 384 raise ValueError("exposure can only be used with the log "
385 "link function")
386 elif exposure.shape[0] != endog.shape[0]:
ValueError: exposure can only be used with the log link function
using offset = np.log(exposure) fails partially in optimization
Activity
thequackdaddy commentedon Nov 6, 2016
Part 1. is me being stupid. loglike is not defined for tweedie... Ignore part 1
thequackdaddy commentedon Feb 26, 2017
Another question with this.
Is there a reason why the optimizers use the observed information matrix and not the expected for GLM?
When I changed this line to append
observed=False, I was able to getnewtonto matchirlspretty much perfectly. The math appears to be (essentially) identical in that case.From an examination with my tweedie data, the first few iterations of the 'newton' scipy method yield a non-positive definite Hessian... and its breaks down from there. When using the expected hessian, it was positive definite all the way and I was able to fit (and was about 20 seconds faster on my large-ish data.)
josef-pkt commentedon Feb 26, 2017
expected information matrix has more restrictive assumptions, and observed, i.e. Hessian is the appropiate default method used with the optimizers.
However, a missing feature is to allow a choice of an alternative to the hessian, either EIM or OPG (outer product of gradient). Both EIM and OPG are usually numerically more stable, but might not be and estimate of the hessian when there is misspecification.
Specific to GLM: OIM = EIM with canonical links and correctly specified model. I'm not sure what the relationship betwe EIM and OPG is in GLM or whether they are the same in this case.
This is still an unfinished area, currently irls uses the implied EIM and gradient optimization uses hessian.
(The only place where this is a choice in statsmodels is in the statespace models where OPG works much better than hessian which can often fail, among other reasons because there are extra parameters that might be more difficult to estimate)
about the convergence problem with newton: Newton often needs to be in the neighborhood of the solution to work reliably. That's why we use irls as an initial estimator to get the start_params for newton.
scipy has now and is getting better newton methods that should be more stable than our own newton.
Optimization speed: hessian is also more expensive to compute that OPG and if available EIM. Stata or maybe some other packages switch optimizers during optimization, e.g. OPG at the beginning and Hessian at the end. (I don't remember where I saw this.)
Essentially, this is another area where we need options for improvements.
Why do you use newton early on in your optimization?
For example, using lbfgsb and then newton, or irls and then newton will be faster and more robust in many cases.
josef-pkt commentedon Mar 10, 2017
Looking at the code for #3548, I just saw that hessian has
observedkeyword, but AFAIR we don't use EIM.It looks like it just needs to be connected to fit and cov_type options.
In base.LikelihoodModel.fit we have
hess = lambda params, *args: -self.hessian(params, *args) / nobswhich allows for args but not for keywords to be transmitted to loglike, score and hessian.For reparameterization we use an internal attribute instead of a keyword.
(aside: the fit method as a unqualified
exceptjosef-pkt commentedon Oct 9, 2017
I don't remember or know where all this ended up
using the updated signature using Power link
raises an exception in current master that exposure is only allowed with log linkg
the last part is
using offset = np.log(exposure) fails partially in optimization