Skip to content

WIP/ENH Gam 2744 rebased3 - #5370

Closed
josef-pkt wants to merge 66 commits into
statsmodels:masterfrom
josef-pkt:gam_2744_rebased3
Closed

josef-pkt wants to merge 66 commits into
statsmodels:masterfrom
josef-pkt:gam_2744_rebased3

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Nov 5, 2018 •

Copy link
Copy Markdown
Member

continuation of #5296 after rebase

commits for outside gam/penalized

DonBeo and others added 30 commits November 4, 2018 21:04
refactor get_prediction, improve unit test
@josef-pkt

Copy link
Copy Markdown
Member Author

next change:

  • fit(method='irls') also uses pirls
  • max_start_irls > 0 now uses fit_pirls instead of letting super fit handle this which would use the unpenalized _fit_irls. Comparing the run times for unit test before and after this change, this might increase the time a little bit, maybe by half a second for gam/tests which takes around 11.5 seconds.
    We currently only have test cases where the unpenalized estimation overfits but doesn't have numerical problems.

docstring for fit does not list all the arguments, it mainly says "most parameters are the same as for GLM"

@josef-pkt

Copy link
Copy Markdown
Member Author

max_start_irls seems to be ignored anyway, in super fit

PenalizedMixin.fit has

        if hasattr(self, 'family'):  # assume this identifies GLM
            kwds.update({'max_start_irls' : 0})

fit with gradient returns a GLMResultsWrapper
GLM.fit, _fit_gradient (super of GLMGam.fit with scipy optimizers uses the correct results class

        results_class = getattr(self, '_results_class', GLMResults)
        glm_results = results_class(self, rslt.params,

but uses hard coded wrapper
return GLMResultsWrapper(glm_results)

with method='bfgs'

type(res_glm_gamgcv), type(res_glm_gamgcv._results)
(statsmodels.genmod.generalized_linear_model.GLMResultsWrapper,
 statsmodels.gam.gam.GLMGamResults)

in discrete count models we also have the wrapper class attached for generic use
e,g, ZeroInflatedPoisson attaches the classes in __init__ and not as class attributes

        self.result_class = ZeroInflatedPoissonResults
        self.result_class_wrapper = ZeroInflatedPoissonResultsWrapper
        self.result_class_reg = L1ZeroInflatedPoissonResults
        self.result_class_reg_wrapper = L1ZeroInflatedPoissonResultsWrapper

I think or IIRC one advantage of defining it in __init__ is that it is runtime and the classes don't have to be available when the class is created. gam.py module has results class before defining model class, most other modules have this reversed.

GLM uses a leading underscore for _results_class
That's not in master, so we can still change it in GLM

@josef-pkt

Copy link
Copy Markdown
Member Author

gam_cross_validation.BasePenaltiesPathCV is unused
I think it is supposed to be the base class for MultivariateGAMCVPath but it is not subclassed and has different __init__ arguments. The only method of BasePenaltiesPathCV is a plot method.
I'm not sure how the plot method is supposed to work with more than 1 component, i.e. multi-dimensional alpha grid.

ignore for now.

MultivariateGAMCVPath works fine in a simple example (1 component and no exog_linear), but API should be improved.
_split_train_test_smoothers and call to create a model do not support exog_linear yet.

in general: Can we use a short cut and just split the existing model exog, i.e. including all components, instead of recreating smoothers?
The problem is that we need to use the signature of GLMGam, which requires smoothers.

my GLMGam.select_penweight is simpler because it doesn't resample the data.

@josef-pkt

Copy link
Copy Markdown
Member Author

naming again
GLMGam uses smoother instead of smoothers . It should be plural it's a list with one or more smoother instances.
(I just mis-typed it during refactoring.)

WRONG: GLMGam has one additive, multi-component smoother, which holds the list of individual smooth components.
However, alphas and similar are a list in GLMGam corresponding to the list of smoothers in the additive smoother.

@josef-pkt

Copy link
Copy Markdown
Member Author

folder names: too many identical names

from gam.gam import GLMGam
import gam.api as gam
from .gam_cross_validation.gam_cross_validation import MultivariateGAMCVPath

@josef-pkt

Copy link
Copy Markdown
Member Author

now k-fold cross validation works also for exog_linear (but no verifying unit tests)

That was the last missing piece of the core functionality.
I did not think hard about and refactor the design of the cross-validation, I mainly changed and checked that it works for the supported cases.
(I guess we could streamline some of this if we don't create GLMGam instance in the cross-validation and use a PenalizedMixin subclass with given full exog and given penalty matrix. But checking and implementing this can wait for an unspecified future.)

Should be ready to merge, except for another round of reviewing and fixing docstrings.

@josef-pkt

Copy link
Copy Markdown
Member Author

all red, I forgot to add the api.py module,
amended and force pushed

@josef-pkt

josef-pkt commented Jan 26, 2019 •

Copy link
Copy Markdown
Member Author

being a bit pedantic:
smooth_basis classes are not "Smoothers" but it's in their name.
nonparametric.smoother are model classes that actually estimate a smooth function, i.e. Smoothers smooth.
Here the XXXSmoother classes just define smooth basis functions and supporting characteristics.

The main user classes don't have smooth in the name: BSplines, CyclicCubicSplines, but all univariate, single component classes do.
correction univariate splines don't have smoother in the name, e.g UnivariateBSplines

one possibility would be to change the name to Base, e.g. UnivariatePolynomialBase

@josef-pkt

Copy link
Copy Markdown
Member Author

strange test failure in all CI that I don't see on my computer. It looks like exog_linear is penalized based on edf. But other parts don't fail.

I added a few asserts that select_penweight and select_penweight_kfold don't affect some of the penalization model attributes

____________________________ TestGAMMPGBS.test_edf _____________________________
self = <statsmodels.gam.tests.test_penalized.TestGAMMPGBS object at 0x7fd3c2852550>
    def test_edf(self):
        res1 = self.res1
        res2 = self.res2
>       assert_allclose(res1.edf, res2.edf_all, rtol=1e-6)
E       AssertionError: 
E       Not equal to tolerance rtol=1e-06, atol=0
E       
E       (mismatch 100.0%)
E        x: array([ 0.977614,  0.977614,  0.977614,  0.977614,  0.699172,  0.924357,
E               0.913044,  0.890399,  0.897879,  0.915949,  0.945958,  0.956598,
E               0.957664,  0.95133 ,  0.971678,  0.201906,  0.131707,  0.127122,
E               0.209266,  0.419033,  0.469991,  0.712416,  0.454576,  0.838797])
E        y: array([ 1.      ,  1.      ,  1.      ,  1.      ,  0.715183,  0.945523,
E               0.933952,  0.910788,  0.91844 ,  0.936923,  0.96762 ,  0.978503,
E               0.979594,  0.973115,  0.993929,  0.20653 ,  0.134723,  0.130033,
E               0.214058,  0.428629,  0.480753,  0.72873 ,  0.464985,  0.858005])
statsmodels/gam/tests/test_penalized.py:468: AssertionError

@josef-pkt

Copy link
Copy Markdown
Member Author

there is still the same failure in at least one of the runs, but the new asserts don't fail
https://travis-ci.org/statsmodels/statsmodels/jobs/485453073

in other news: the latest pandas release broke the VAR unit tests

@josef-pkt

Copy link
Copy Markdown
Member Author

The discrepancy in the travis failure is proportional, so most likely a problem with scale

xx = np.array([ 0.977614,  0.977614,  0.977614,  0.977614,  0.699172,  0.924357])
yy = np.array([ 1.      ,  1.      ,  1.      ,  1.      ,  0.715183,  0.945523])
yy / xx
array([ 1.02289861,  1.02289861,  1.02289861,  1.02289861,  1.02289994,
        1.02289808])

I'm not able to reproduce or reverse engineer that number yet. Possibly something changes model.scale or model.scaletype.

Also, there is potentially a circular definition

  • computing edf uses scale
  • computing scale might use edf as df_resid

I'm not sure how this currently works.
GLMGamResults.__init__

  • attach scale given by args
  • compute edf
  • set df_model, df_resid based on edf, including the one in the model self.model.df_resid = self.df_resid
  • recompute scale, self.scale = scale = self.model.estimate_scale(mu)

This sounds bad, but I think (!) there is a invariance in the edf so that it is independent of scale as long as we use the same scale in all computation.

However, the failure seems to depend on the random sequence of how the unit tests are run by pytest, so my guess is still some unintended changes somewhere.

@josef-pkt

Copy link
Copy Markdown
Member Author

Ok.
gam_bsf.select_penweight changes res_bsf.model.scale, but not res_bsf.scale and res_bsf.model.scaletype

but I don't see how it can change edf on an existing results instance given that it is used in results __init__ and cached. It doesn't when I run an example.

The edf computation depends on model scale through hessian_factor if it is not provided as argument
GLM.hessian_factor has:

        if scale is None:
            scale = self.estimate_scale(mu)

I changed it so that hessian_factor in edf uses self.scale

@josef-pkt

Copy link
Copy Markdown
Member Author

It looks like master is all red and full of deprecation warnings (based on travis for this PR).

Gam in this PR looks fine now, except for lint violations
I have one more amend to the "wrong state" in the model instance plus some uncommitted doc changes.

@josef-pkt

Copy link
Copy Markdown
Member Author

I am planning to merge this tomorrow. (Never merge a big PR late at night.)

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.

5 participants