Skip to content

WIP: fit_constrained - #1714

Merged
josef-pkt merged 24 commits into
statsmodels:masterfrom
josef-pkt:fit_constrained
Jun 5, 2014
Merged

josef-pkt merged 24 commits into
statsmodels:masterfrom
josef-pkt:fit_constrained

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

add constrained fitting to models

starting with generic setup and Poisson as example.
OLS was test example for transformation, but has separate RestrictedGLS implementation, PR #1673

  • needs offset for all models except linear, see ENH: add offset to all non-linear models #1640
    currently only count models and GLM have offset
  • add method to models
    • Poisson
    • other discrete, NegativeBinomial has offset but an extra parameter that needs to be handled
    • GLM
    • ? linear models, has other implementation, that avoids auxiliary model creation, delegate
    • RLM I'm pretty sure it works almost out of the box, but haven't tried yet,
      using correction to endog instead of offset is not implemented in the function
      (I have a separate example code for that, linear case is more related to RestrictedGLS PR)
  • does not return results instance, currently returns params and bse, calculates cov_params
    Those two need to be overwritten in the creation of the results instance.
    I would prefer generic results creation see REF DRYing fit methods #1615, but will start with a full test case for Poisson.
  • unit test currently only verify params and bse, tvalues, pvalues, confint, llf, and llnull (if available) are verified
  • start_params for transformed model: I got convergence failures sometimes in bfgs, sometimes in newton. I didn't get them in both at the same time for the poisson test cases. Needed to add start_params for TravisCI to converge in test cases 1a and 2a
  • still unclear (waiting for results instance to inherit all results to be verified):
    • df_resid, df_model, small sample correction, I assume those need to be adjusted unless we only use asymptotic normal version, no use_t=True
    • aic, bic, ... ? not yet in results module, works without changes but see GLM aic bic inconsistency #1733
  • problem: functions or results methods that want to access score_obs, hessian, get the wrong method, doesn't take constraints into account, ???
    maybe no problem: Likelihood function doesn't change, we just evaluate it at the constrained parameters.
    need to check what kshedden did for score test in GEE.
  • summary, other
    • summary shows converged=False from the wrong fit, need to adjust mle_fit results
    • summary doesn't mention constrained estimation

code duplication:
base.TransformRestriction has the same purpose as Kerby's version in GEE. I haven't checked if the result is identical.

  • API decisions, naming, ...
  • bonus question: Can I get the robust covariances just from the transformed model without any extra work? Need to be wired up.

Unit tests
I have added more test results from Stata that haven't been use so far, unconstraint, and constraint with vce(robust) option
I don't know what vce(robust) does in Stata, and I haven't figured out yet how to add sandwich robust covariances to GLM. Stata also allows HAC and cluster robust vce in GLM.

follwow-up

  • extension to other classes
  • clean up and find pattern and naming for Transformation classes (missing get_offset(exog) method)
  • unused transform_params_constraint that obtains restricted parameters from the unrestricted parameters (approximate in non-linear models, similar to Wald test)
  • development script examples/try_fit_constrained.py contains example with application to linear model (OLS) and example for transform_params_constraint

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling 7cff3eb on josef-pkt:fit_constrained into e4326b7 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.06%) when pulling b62b36b on josef-pkt:fit_constrained into e4326b7 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

test failures: python 3: print in junk function (draft for another example based on randhi)

TestPoissonConstrained 1a and 2a look like convergence failures on one python 2.7
still need start_params

@josef-pkt

Copy link
Copy Markdown
Member Author

The last commit added start_params, but used it only in one of the two non-converging test cases.
Still need to figure out why two examples don't converge on TravisCI. Tests pass on my current version of dependencies, but optimizer was chosen so they pass for me.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) when pulling b583394 on josef-pkt:fit_constrained into e4326b7 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

no StringIO import on python 3
setting the start_params for the constant worked in the first example,
if we want to look at convergence issues later, link to #1649

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) when pulling edb986f on josef-pkt:fit_constrained into e4326b7 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.09%) when pulling 7c901b9 on josef-pkt:fit_constrained into e4326b7 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

TravisCI failure: I attached the auxiliary, transformed results to the regular results instance.
pickle_shrink test fails on all python.

I didn't attach it in Poisson.fit_regularized

@josef-pkt

Copy link
Copy Markdown
Member Author

No, unrelated to fit_regularized (unit tests don't check that case)
adding llnull triggers the calculation of null which is cached.
However, why wasn't null already called before adding llnull?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.11%) when pulling d332246 on josef-pkt:fit_constrained into e4326b7 on statsmodels:master.

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.

don't keep monkeys in the unit tests :)

@josef-pkt

Copy link
Copy Markdown
Member Author

It took me a while to find out why my changes don't work anymore for the GLM-logit test case.

@josef-pkt

Copy link
Copy Markdown
Member Author

How can they put a close button where there is supposed to be a cancel comment button.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.16%) when pulling 502e959 on josef-pkt:fit_constrained into 8f7d124 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.16%) when pulling 7db959e on josef-pkt:fit_constrained into 8f7d124 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

@jseabold @kshedden I'm about done here, or as far as I want to go in this PR

only Poisson and GLM for now
included prototype for "pre_fix" function base._constraints.fit_constrained_wrap
still some open issues, but the two new methods look stable enough, I think

If the _wrap functions that modifies an existing model work out, then I think we can add more of those that without needing to incorporate them in every model. Pinv/PCR for non-linear models (Poisson and GLM again) would be my next transform candidate.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.17%) when pulling 7db959e on josef-pkt:fit_constrained into 8f7d124 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.18%) when pulling dd66f64 on josef-pkt:fit_constrained into 8f7d124 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

Ready for the green button, when TravisCI comes back green
(only one merge behind master)

Follow-up will be extension to other models, and getting a pattern and naming for Transformation classes and better support for "prefix" functions.

@josef-pkt josef-pkt added this to the 0.6 milestone Jun 5, 2014
@josef-pkt

Copy link
Copy Markdown
Member Author

missing: adding to release notes

josef-pkt added a commit that referenced this pull request Jun 5, 2014
ENH: fit_constrained for Poisson and GLM, and standalone
@josef-pkt
josef-pkt merged commit 2c3eb6d into statsmodels:master Jun 5, 2014
@josef-pkt
josef-pkt deleted the fit_constrained branch June 5, 2014 01:56
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
ENH: fit_constrained for Poisson and GLM, and standalone
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.

2 participants