Repository navigation
Description
It looks like the current GenericLikelihoodModel does not set df_resid, df_model correctly.
see also #7767 and #7759 for df and #4679 for more refactoring
I converted an old gist from a blog article to work with current version.
It uses GenericLikelihoodModel for ZIP (without regressors)
https://www.austinrochford.com/posts/2015-03-03-mle-python-statsmodels.html
my notebook for checking this is mle_custom_zip.ipynb
fitting the model emits warnings
...statsmodels\base\model.py:2733: UserWarning: df_model + k_constant differs from nparams
warnings.warn("df_model + k_constant differs from nparams")
...statsmodels\statsmodels\base\model.py:2735: UserWarning: df_resid differs from nobs - nparams
warnings.warn("df_resid differs from nobs - nparams")
and df_resid, df_model are wrong
N, model.df_resid, model.df_model, results.df_resid, results.df_model
(1000, 1000.0, -1.0, 1000.0, -1.0)
The model code does not set anything related to df_xxx,
class ZeroInflatedPoisson(GenericLikelihoodModel):
def __init__(self, endog, exog=None, **kwds):
if exog is None:
exog = np.zeros_like(endog)
super(ZeroInflatedPoisson, self).__init__(endog, exog, **kwds)
def nloglikeobs(self, params):
pi = params[0]
lambda_ = params[1]
return -np.log(zip_pmf(self.endog, pi=pi, lambda_=lambda_))
def fit(self, start_params=None, maxiter=10000, maxfun=5000, **kwds):
if start_params is None:
lambda_start = self.endog.mean()
excess_zeros = (self.endog == 0).mean() - stats.poisson.pmf(0, lambda_start)
start_params = np.array([excess_zeros, lambda_start])
return super(ZeroInflatedPoisson, self).fit(start_params=start_params,
maxiter=maxiter, maxfun=maxfun, **kwds)
One guess for the bug is that the model code specifies neither n_params/k_params, nor a start_params with correct len.
using explicit start_params does not help. The __init__ does not seem to have an option to set k_params.
I guess there is some problem also if exog=None.
fit sets the _set_extra_params_names correctly, but does not change df_xxx.
Activity
josef-pkt commentedon Oct 28, 2022
likely problem
GenericLikelihoodModelResults has
hasattr might never apply,
creating the model will always set df_resid, df_model, but they could be nan if exog is None.
However exog is a column of zeros even though it is not used (except for k_params, df_xxx setting)
josef-pkt commentedon Oct 28, 2022
more experimenting:
If df_resid depends on exog, then it is correct:
using extra_params_names does not help for df_resid
josef-pkt commentedon Oct 28, 2022
back to hasattr in Generic Results class:
I just realized that setting exog to column of zeros is in custom code, i.e. user defined subclass.
When I drop this, then I get the nan in model.df_resid and model.df_model
This means that the hasattr check needs to be replaced or combined with a
nancheck inGenericLikelihoodModelResults.__init__To confirm that the hasattr check works, if I
delthe two df_xxx, then the df are correctly set.One remaining (old) problem, df_model and hasconst.
by default the code often assumes hasconst is true and we have one constant.
see df_model should be excess compared to df_null. issue ?
josef-pkt commentedon Oct 28, 2022
adding the nan check, df_resid works correctly in the example case
However, df_model will be 2 in this case, i.e. the same as k_params.
However, if we add df_null or k_null, and define df_model as number of "excess" parameters,
df_model = k_params - k_null, then df_model should be zero in this case.BetaReg seems to define df_model currently as
self.df_model = self.nparams - 1see #7584 (comment) and related comments
BUG: GenericLikelihood Results hasattr for df_resid is always true, see
josef-pkt commentedon Oct 28, 2022
case 1 exog, 1 extra parameter:
df_resid subtracts only 1 parameter, i.e. the exog parameters
AFAICS, there is no
k_extrahandling in generic MLE as in discrete negbin, GPP, ...Asides:
base.modelhas k_extra only inLikelihoodModel._fit_zerosLikelihoodModel also has
_fit_collinearwhich I have not use in years and is, I guess, not advertised at all.compared to discrete count models: NBP, GPP
AFAICS, exog_extra is handled in individual results methods like aic, but does not affect df_model.
That means extra params are correctly excluded from the model excess over the Null model, e.g. in llr_pvalue.
But we don't have generic code based on k_params, i.e. the number of all estimated parameters.
I don't see (yet) in the code if or where k_extra is included in df_resid computation.
josef-pkt commentedon Oct 28, 2022
nparamsis set inGenericLikelihoodModel._set_extra_params_namesbased onlen(self.exog_names). It only applies if the user provides extra_params_namesHowever, AFAIR,
nparamsis not really used anymore. (It was an idea that didn't take hold yet. Newer convention is k_params.)example previous model has correct nparams
we can add setting k_extra in the same or similar way.
BUG: GenericLikelihood Results hasattr for df_resid is always true, see
[-]BUG: default df_resid in GenericLikelihoodModel[/-][+]BUG: default df_resid, df_model in GenericLikelihoodModel[/+]josef-pkt commentedon Dec 5, 2022
one more
unit test run on azur has warnings for copula models
bashtage commentedon Jul 30, 2026
Fixed by PR #8476 (same PR as #8482): added NaN checks and k_extra bookkeeping, with a regression test in test_generic_mle.py::TestTwoPeakLLHNoExog::test_fit confirming correct df_resid/df_model values. Closing as fixed.