Skip to content

Gam gsoc2015 - #2435

Closed
DonBeo wants to merge 94 commits into
statsmodels:masterfrom
DonBeo:gam_gsoc2015
Closed

DonBeo wants to merge 94 commits into
statsmodels:masterfrom
DonBeo:gam_gsoc2015

Conversation

@DonBeo

@DonBeo DonBeo commented Jun 5, 2015

Copy link
Copy Markdown
Contributor

@josef-pkt I am starting a PR . At the moment there is the gam file that contains the gam penalty class. Smooth_basis contains some functions to get bsplines and polynomial basis. This file will be removed when we will be able to use directly patsy.

There are also 2 files that contains examples or small scripts. We will remove them later.

Let me know what do you think about that.

Todo

  • predict errors (stateful transform, patsy ?), Note fittedvalues are available
    only a problem in pirls example, predict after fit works, requires spline basis values
  • get_prediction also errors, maybe consequence of predict error
    currently errors because weights is None
  • check test coverage for offset and exposure

Interface

  • partial_values plot_partial has inconvenient arguments (smoother and mask) instead of column index or term name or similar
  • formula-like interface for predict (create spline basis values internally)
  • adjust inherited methods like plot_partial_residuals (after GSOC?)
  • default param names for splines (should be more informative than "xi" (i is range(k)), but less (?) verbose than patsy's), need them for test_terms

@josef-pkt

Copy link
Copy Markdown
Member

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 83.91% when pulling 3d5985c on DonBeo:gam_gsoc2015 into 4b55fa4 on statsmodels:master.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

in general, we use from numpy.testing import assert_allclose and other related asserts
No plain python asserts

@josef-pkt

Copy link
Copy Markdown
Member

@DonBeo Do you have the GLM example so I can look at it?

@DonBeo

DonBeo commented Aug 19, 2015

Copy link
Copy Markdown
Contributor Author

Cubic splines can be fitted only with PIRLS. This is because they do not have the der_basis and der2_basis attribute. They should work fine if method is PIRLS

@josef-pkt

Copy link
Copy Markdown
Member

the predict problem is pirls only

here is the result with bsplines estimated with nm/bfgs in first case and pirls in second case

res_glm_gam.fittedvalues[:10] - res_glm_gam.predict()[:10]
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

res_glm_gam2.fittedvalues[:10] - res_glm_gam2.predict()[:10]
array([ 0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1,  0.1])

The differences are not visible in some of the graphs, because 0.1 is small compared to the overall scale and variation in the fit, e.g. the quadratic notebook example.

@DonBeo

DonBeo commented Aug 19, 2015

Copy link
Copy Markdown
Contributor Author

What if you use np.dot(basis, params) instead of predict? Does it work?

@josef-pkt

Copy link
Copy Markdown
Member

What if you use np.dot(basis, params) instead of predict?

In the cases that I checked np.dot(basis, params) and predict() produce the same result. The problem is that pirls fits a model that has 0.1 as offset (corresponds to subtracting 0.1 from y in the Gaussian case. So the estimated params are for a slightly different model, AFAICS.

So if you compare np.dot(basis, params) to the results in R, then you should be 0.1 off in prediction, if the model was estimated with pirls.

@DonBeo

DonBeo commented Aug 19, 2015

Copy link
Copy Markdown
Contributor Author

In pirls you should also check self.scaletype = 'dev' . I am not sure that this value is right

@josef-pkt

Copy link
Copy Markdown
Member

I just saw self.scaletype. setting offset_exposure to zero instead of one if it doesn't exist yet, fixes the predict versus fittedvalues problem in pirls.

I will check these two things. I thought both should be available in the model already as attributes, so we shouldn't have to define it in pirls. (need to compare with _fit_irls in GLM.

Another unit test case would be to compare that GLMGAM produces identical results to GLM when alpha=0 (within optimization precision).

@DonBeo

DonBeo commented Aug 19, 2015

Copy link
Copy Markdown
Contributor Author

ok I can add the test tomorrow. Should I wait your push ?

On Thu, Aug 20, 2015 at 1:17 AM, Josef Perktold [email protected]
wrote:

I just saw self.scaletype. setting offset_exposure to zero instead of one
if it doesn't exist yet, fixes the predict versus fittedvalues problem in
pirls.

I will check these two things. I thought both should be available in the
model already as attributes, so we shouldn't have to define it in pirls.
(need to compare with _fit_irls in GLM.

Another unit test case would be to compare that GLMGAM produces identical
results to GLM when alpha=0 (within optimization precision).

—
Reply to this email directly or view it on GitHub
#2435 (comment)
.

@josef-pkt

Copy link
Copy Markdown
Member

right now the only change I have is to set the _offset_exposure to zero, which you could add to your changes, so you can work on this and have consistent predict

        if not hasattr(self, '_offset_exposure'):
            self._offset_exposure = 0

0 should broadcast correctly.

instead of

self._offset_exposure = np.array([.1] * n_samples)

I still have to figure out how to set _offset_exposure and scaletype correctly.

@josef-pkt

Copy link
Copy Markdown
Member

another cross-check:

It looks like pirls and nm/bfgs use different scaling of alpha.
I get close matches in the params and the prediction if alpha=1 in nm/bfgs and alpha=0.35 in pirls.

manual search: I get agreement in sum of squared difference in prediction of 1e-11 with alpha=0.3545 in pirls, i.e. essentially exactly the same prediction/fittedvalues but with different alpha.

This needs to be fixed, otherwise we are not able to switch between the optimizers.

@josef-pkt

Copy link
Copy Markdown
Member

Overall PIRLS looks like a good optimizer, it seems to be more robust to starting values and faster than using the scipy optimizers.

Once the two fit methods produce consistent results, we could use a pirls step as an initial step for the scipy optimizers similar to what we do with GLM, if that works as well in the GLMGAM case as in the GLM case.

@josef-pkt

Copy link
Copy Markdown
Member

random numbers with no seed set makes replication difficult. current run (no seed either) alpha=1 in nm/bfgs corresponds to 0.40865 in pirls.
The penal function uses a scaling of the penalization weight proportional to nobs, and in the Gaussian case, the relative weight depends on the variance (scale estimate), AFAIR.

The factor in this case looks like

len(res_glm_gam.fittedvalues) * res_glm_gam.scale / 100
0.40867633530278336

which gives glm_gam = GLMGam(y, bsplines, alpha=0.40867633530278333) fit with pirls, agrees with glm_gam = GLMGam(y, bsplines, alpha=1) fit with `nm/bfgs' at 1e-13 for sum of squared diff.

That seems to be correct, running several cases and changing alpha, I get for alpha in scipy optimizer, the corresponding alpha for pirls is
alpha_pirls = alpha * len(res_glm_gam.fittedvalues) * res_glm_gam.scale / 100

no idea why divided by 100, I'm reverse engineering and not looking at code.

@DonBeo

DonBeo commented Aug 20, 2015

Copy link
Copy Markdown
Contributor Author

in _fit_pirls the variable self.scale is not defined.

@josef-pkt

Copy link
Copy Markdown
Member

in _fit_pirls the variable self.scale is not defined.

It's not because we don't need it in GLM for estimating the params. We wouldn't need it if we interpret alpha as penalization weight relative to the scale. (There is a similar issue in penalized OLS/GLS in my PR).

The problem is that I don't know what the impact of estimating the scale at each iteration of the pirls loop is on the convergence behavior. Does the explanation in Wood for pirls mention anything about scale estimates?

For now you could ignore the scale issue for converting alpha, and just use binomial or poisson as a test case where we don't need to include the scale because it's set to 1.

The nobs correction for alpha should be in penal.pen_weight or something like this.

(I don't manage to wake up my monitor this morning and won't be able to look into it right now.)

@DonBeo

DonBeo commented Aug 20, 2015

Copy link
Copy Markdown
Contributor Author

I haven't read anything about scale in Wood's . Personally I do not even know the statistical meaning of scale in this contest. I have updated some of the tests but a couple are still not working

@springcoil

Copy link
Copy Markdown

Does this need more code-review? What is stopping this being released?

@DonBeo

DonBeo commented Aug 27, 2015

Copy link
Copy Markdown
Contributor Author

I think this is almost ready. I will be busy until the 6th of September. Then we will probably merge.

@DonBeo

DonBeo commented Nov 11, 2015

Copy link
Copy Markdown
Contributor Author

Hi @josef-pkt,
I am wondering if you have any update on this pull.
I haven't had the time to work on it yet.
It is still in my todo list tough.
Hopefully during Christmas I should have more free time. Otherwise this summer.

Let me know!!

@josef-pkt

Copy link
Copy Markdown
Member

@DonBeo Sorry, I have been neglecting this since GSOC ended. I wanted to get some "easier" things done and merged before going back to penalization.

Using git surgery to clean up this branch is pretty difficult, so I was thinking that the best way forward would be to start a new branch, copy everything over, moving modules to their proper location, and keep this PR for the history and discussion. Then we should also get TravisCI testing, I never figured out why this PR is stuck at appveyor and doesn't use Travis. When Travis runs the test, we will also get the test coverage report, to see how good that is.

Overall, I think the code is in good shape, I still have some unfinished notebooks where I tried it out. But we might have to add some Future or Experimental warnings at the places where we expect future changes with reasonably high probability.
(patsy integration won't happen in the first round, AFAICS)

@DonBeo

DonBeo commented Dec 23, 2015

Copy link
Copy Markdown
Contributor Author

Hi Josef,
I had some free time and I have improved the partial_values and partial_plot of gam. There is something wrong with the SE value obtained with partial_values. There is a failing test for it.

I am afraid that I can not do too much for the other elements in the TODO list as they require a good knowledge of the statsmodels' structure.

Let me know.
All the best,
Luca

@josef-pkt

Copy link
Copy Markdown
Member

@DonBeo

Luca, Can you create a new clean branch without any extra files?
Create a new PR and leave this one as is, so we have the discussion and history.
That should then also let TravisCI do it's testing job. There shouldn't be any conflicts with current master, AFAIK.

I think we can just put this in a new directory statsmodels.gam, then you can just remame the directory and move it up out of the sandbox.
We could put the access path for GAM into genmod.generalized_additive_model, but your code is more general and will be more widely applicable than just generalized linear models.

@springcoil

Copy link
Copy Markdown

Hey @DonBeo have you opened up another branch for this yet? If so can you reference it here. I could do a code review on the code so far. I think this was really close to being accepted if I understood correctly?

@DonBeo

DonBeo commented Mar 19, 2016

Copy link
Copy Markdown
Contributor Author

I think you may refer to this PR #2744
I am quiet busy this period so I did not have time to do much

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