Skip to content

ENH/REF/BUG Discrete models llnull improvements - #3921

Merged
josef-pkt merged 10 commits into
statsmodels:masterfrom
josef-pkt:discrete_llnull
Sep 22, 2017
Merged

josef-pkt merged 10 commits into
statsmodels:masterfrom
josef-pkt:discrete_llnull

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Sep 21, 2017 •

Copy link
Copy Markdown
Member

This PR refactors llnull to use start_params provide by model._get_start_params_null.
The start_params for the null model can be obtained by moment estimators, which should make it more robust.
closes related issues:
#3533 second part is for llnull, first part is convergence failure with defaults (where users can work around)
#3476 Poisson llnull, mailing list thread has also discussion on MNLogit
#3572 NB2 llnull

related issue #3751 (is more general, e.g. penalization, constraints. I don't know yet what should/needs to be done)

This also includes now refactoring and fixes that I ran into:

#3919 avoid integer overflow in GP hessian, REF set endog, exog dtypes to at least float64
#3918 start_params needed to to provide transformed params if transformparams is used internally

closes #3533, closes #3476, closes#3572, closes #3919 ("closes" is needed each time)

@josef-pkt

Copy link
Copy Markdown
Member Author

Bonus: I'm writing moment estimators for the dispersion coefficient, NB1, NB2, NBP, GPP.
Those are currently designed for being hidden inside _get_start_params_null, but might be useful standalone (based on resid and mu).
similar to chisquare overdispersion scale in GLM, and score_tests for excess dispersion.

aside: count models don't have resid_pearson (discrete binary models have it)

@josef-pkt

Copy link
Copy Markdown
Member Author

all red, problem in statsmodels.discrete.tests.test_sandwich_cov which I didn't run locally.
some failing llf comparisons and some shape mismatches.

my new tests (locally) still have convergence problems or create a lot of warnings, test case is definitely not nice for exp. (NB1 dispersion coefficient is above 2000)
One problem is convergence conditions are not very strict to test different start_params at high tolerance.
The second problem is that we get warnings in corner cases, that should be silenced if those are "expected".

@josef-pkt

Copy link
Copy Markdown
Member Author

test error py 2.7: AttributeError: 'list' object has no attribute 'copy'
start_params is not converted to ndarray

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 81.506% when pulling f7143a2 on josef-pkt:discrete_llnull into 56aa49e on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

The last commit should finish with the main count models.

I'm not sure I will figure out the remaining null start_params for this PR, binary, MNLogit and ZeroInflated.

a possible additional test is to check that null start_params are close to fit params, which should be fine if one-step moment estimators are close to MLE.

another open issue is better preliminary fit to get the start_params for the final model.

In the unit tests in this PR, I avoided problems by hardcoding the start_params for the full model. The test classes could check generically for convergence in this case, future task when we have a better way to get default start_params. In my initial script for this PR I used method='nm' to get reasonable start_params that work with "bfgs" or "newton".

@josef-pkt

Copy link
Copy Markdown
Member Author

2 widespread test failure
TestNegativeBinomialNBP2Null nan in first llf null (w/ start_params_null)
TestGeneralizedPoissonNull nan in second llf null (comparison)

test pass in one python version on travis, and they pass locally for me

@coveralls

coveralls commented Sep 21, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 81.525% when pulling 9d25878 on josef-pkt:discrete_llnull into 56aa49e on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

on appveyor 2 out of 3 are green, one has the same two failures as travis

right now I don't see a reason why bfgs should converge in one case but not the other with similar starting values. From what I can see locally there shouldn't be a difference in the model or model parameters.
It fails with scipy as recent as 0.17 on travis, the two passing on appveyor and the one passing on travis use scipy 0.19. I'm using scipy 0.18.1. So maybe the fixes in bfgs are not old enough to use it for this difficult test case.

needs more "unit" tests for intermediate results.

@josef-pkt

Copy link
Copy Markdown
Member Author

related to this: I did not add an option to change the optimizer arguments in the llnull fit

Maybe I need to add it after all if start_params_null are not enough.

@josef-pkt

Copy link
Copy Markdown
Member Author

New commit uses nm and newton instead of bfgs.

This is mainly to check the outcome across versions. I don't think it's a good default. "bfgs" looks better for recent enough scipy versions.

If it works, then we might have to add set_null_optim_kwds or set_optim_kwds_null to get around a bfgs default

@josef-pkt

josef-pkt commented Sep 21, 2017 •

Copy link
Copy Markdown
Member Author

travis stopped on all versions

No output has been received in the last 10m0s, this potentially indicates a stalled build or something wrong with the build itself.
Check the details on how to adjust your build configuration on: https://docs.travis-ci.com/user/common-build-problems/#Build-times-out-because-no-output-was-received
The build has been terminated

maybe inf or nan in linalg when computing newton that hangs (although MKL usually raises instead of hangs)

appveyor also hangs in count model test, but didn't stop, I canceled

another puzzle: the changes use test_discrete and not test_count_model which is the last on travis and appveyor

@josef-pkt

Copy link
Copy Markdown
Member Author

amended last commit and force pushed to switch back to 'bfgs', try different settings

@josef-pkt

Copy link
Copy Markdown
Member Author

Another way to avoid spending (wasting) a lot of time fixing unit tests for extreme cases is to skip the two tests for scipy < 0.18.
Anyone who wants good optimizers needs a recent scipy.

@coveralls

coveralls commented Sep 21, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) to 81.527% when pulling 3306615 on josef-pkt:discrete_llnull into 56aa49e on statsmodels:master.

@codecov-io

codecov-io commented Sep 21, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #3921 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3921      +/-   ##
==========================================
+ Coverage   78.92%   78.96%   +0.04%     
==========================================
  Files         541      541              
  Lines       80648    80813     +165     
  Branches     9180     9187       +7     
==========================================
+ Hits        63649    63816     +167     
+ Misses      14921    14920       -1     
+ Partials     2078     2077       -1
Impacted Files Coverage Δ
statsmodels/discrete/tests/test_discrete.py 98.32% <100%> (+0.13%) ⬆️
statsmodels/discrete/discrete_model.py 87.62% <100%> (+0.79%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 56aa49e...2e91993. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member Author

everything green

However, I decided to add set_null_options to override everything in an official way.
results.set_null_options(llnull=None, attach_results=True, **kwds)
this should have _cache manipulation to reset generated cached attributes.
kwds are fit keywords that override default settings
attach_results can set the internal _attach_nullmodel flag

no computation, computation are lazily triggered if llnull is used directly or indirectly

the intention is mainly to have an official override if the defaults fail, as in the issues that triggered this refactoring

@josef-pkt

Copy link
Copy Markdown
Member Author

set_null_option added, plan is to merge tomorrow after an quick review

@denadai2 this should fix the llnull failures/nans in your issues, but I haven't tried with your dataset yet.
It's a bit later than we would hope for but it was a busy summer for me.

@coveralls

coveralls commented Sep 22, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 81.537% when pulling 2e91993 on josef-pkt:discrete_llnull into 56aa49e on statsmodels:master.

@josef-pkt

josef-pkt commented Sep 22, 2017 •

Copy link
Copy Markdown
Member Author

The llnull problem with Poisson in #3533 is fixed by this, the new null start_params allow convergence and non-nan llnull.

(In my notebook for #3533 I had used scipy '0.17.0', now I'm using 0.18.1, which might also improve convergence.)

aside: In that notebook I used null_fit_kwds as internal dictionary
res._results.null_fit_kwds = dict(start_params=np.log([model.endog.mean()]), method='bfgs', disp=1)

@josef-pkt josef-pkt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine when skimming the code

a few typos,
no doc string for internal _get_start_params_null

Comment thread statsmodels/discrete/discrete_model.py Outdated
Parameters
----------
llnull : None or float
If llnull is not None, than the value will be directly assigned to

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo than -> then

Comment thread statsmodels/discrete/discrete_model.py Outdated
If llnull is not None, than the value will be directly assigned to
the cached attribute "llnull".
attach_results : Bool
Sets and internal flag whether the results instance of the null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and -> an

Comment thread statsmodels/discrete/discrete_model.py Outdated
the cached attribute "llnull".
attach_results : Bool
Sets and internal flag whether the results instance of the null
model is attached. By default without calling this method, the

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model is attached -> model should be attached

@josef-pkt

Copy link
Copy Markdown
Member Author

using a docstring template for internal functions is not so useful, because the docstring is not next to the source and the main audience are developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants