New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method for simulating from the posterior (or just add an example to the documentation) #113
Comments
@cbrummitt Awesome code sample :) i'm impressed you were able to dig that up. We should definitely add that functionality to the models. For linear GAMs your example looks solid. (ignoring the question of estimating the scale parameter... pg 68 section 2.1.5 of Wood's book) I see your point about each distribution needing its own sampler... I suspect that we can go a long way with numpy's Perhaps adding the link functions could be similar to the way the
Does that look right to you? Bootstrapping makes sense to me for getting a distribution of (B, lam). Do you have an intuition of how many bootstrap samples is enough? I will do some reading on the properties of the bootstrap. |
That new method is beautiful 😍 Just one edit: An alternative to using numpy's I don't have good intuition right now for how many bootstrap samples would be a good default value. Wood simulates the coefficients |
I'm also not sure about where to transpose. The last line may need to be Because the first axis tends to be samples, I chose to make the shape of the output to be |
Nice. yeah something like The scipy RV sampling looks cool. ill play around with that a bit and compare to numpy. let's see which one has the most distributions/easiest parameterization/fastest/etc. @cbrummitt TBH I would love for you to contribute to this code base (even if it's just a few lines!) I can then branch off of your branch, and collaborate on the PR that way. |
Sure! I started working on it. For one point of reference, in PyMC3's API the
|
|
I forked the repo and made a new branch • Normal: The I also made I haven't yet tried implementing bootstrap samples to get random samples of the smoothing parameters, too. Update: Fixed the arguments to the numpy methods in the list above. |
😍😭 love it. im planning on working on pygam today and tomorrow. ive got some more work on the other PR. push youre work whenever you want and i that way i can add to it! |
@cbrummitt so awesome man!! thanks for this :) |
Estimating the mean and confidence intervals (using
prediction_intervals
) is great. In some cases, it can be useful to simulate from the posterior distribution of the model's coefficients. An example is given in pages 242–243 of [1].I think the following code snippet does the trick for a
LinearGAM
:I'm not sure if this should be added as an example in the documentation or added to the code (or both).
To implement this in general, I think we'd want to add a method for each
Distribution
that draws a certain number of samples (calledsample
orrandom_variate
?), so we'd have aNormalDist.sample
,BinomialDist.sample
, and so on. Then theGAM.simulate
could just callself.dist.sample(self.coef_, self.statistics['cov'], size=n_simulations)
? I'm not sure yet how to best handle the link functions for these simulations...As pointed out on pages 256–257 of [1], this procedure simulates the coefficients conditioned on the smoothing parameters, lambda (
lam
). To actually simulate from the coefficients, one may use bootstrap samples to get simulations of the coefficients and of the smoothing parameters; an example is given on page 257 of [1].[1] S. Wood. Generalized Additive Models: An Introduction with R (First Edition). Chapman & Hall/CRC Texts in Statistical Science. Taylor & Francis, 2006.
The text was updated successfully, but these errors were encountered: