pyro-ppl / pyro

Deep universal probabilistic programming with Python and PyTorch
http://pyro.ai
Apache License 2.0
8.52k stars 984 forks source link

[FR]: Example of Predictive with MCMC sampling #2803

Closed fonnesbeck closed 3 years ago

fonnesbeck commented 3 years ago

There are currently no examples in the docs and tutorials of Predictive being used in MCMC sampling. Given its importance in returning deterministic values of interest from models, it would probably be useful for users to be able to see how they are used.

mtsokol commented 3 years ago

Hi! Is it open as a good-first-issue? If yes then I would like to try it. I haven't used pyro (only TFP) so I would be grateful if you could point out which examples from http://pyro.ai/examples I can refer to and follow while learning and writing.

fritzo commented 3 years ago

Hi @mtsokol, yes this would be a good first issue.

But wait, it looks like the baseball example and the Bayesian Regression II tutorial use MCMC and Predictive. @fonnesbeck can you suggest how we might improve these tutorials and examples to make Predictive easier to understand?

EDIT my mistake, it looks like Bayesian Regression II uses Predictive with SVI rather than with MCMC. @mtsokol WDYT of making a simple tutorial kind of like Bayesian Regression but with a model with pyro.deterministic sites in it? For example I'm working with a regression model that looks like

```py def model(features, counts): N, P = features.shape assert counts.shape == (N,) scale = pyro.sample("scale", dist.LogNormal(0, 1)) coef = pyro.sample("coef", dist.Normal(0, scale).expand([P]).to_event(1)) rate = pyro.deterministic("rate", torch.nn.functional.softplus(coef @ features.T)) concentration = pyro.sample("concentration", dist.LogNormal(0, 1)) with pyro.plate("bins", N): pyro.sample("counts", dist.GammaPoisson(concentration, rate), obs=counts) ```

where you could examine samples from the deterministic rate variable.

fonnesbeck commented 3 years ago

My mistake--did not notice it in the baseball example. One minor suggestion is for important classes like Predictive to have links in the API docs to where it is used in the examples. I think Tensorflow does this (not sure if its done automatically or manually).

mtsokol commented 3 years ago

Hi @fritzo! Sorry for the delay and thank you for a guidance!

I did a short (draft with possible errors and misunderstanding) notebook in a "tutorial" way that utilizes deterministic, Predictive and MCMC with the model you provided. But it's pretty short and it repeats content from existing tutorials so I don't think it's worth it. (However writing it was a great way to familiarize myself with the library!)

I can search for another issue in pyro or numpyro or continue this notebook according to your tips if you consider it worthwhile. What do you think?

Links to the notebook: https://nbviewer.jupyter.org/gist/mtsokol/fe00f78bb0b925f986d241622c2ec019 https://gist.github.com/mtsokol/fe00f78bb0b925f986d241622c2ec019

(I focused on explaining deterministic, rate variable and GammaPoisson distribution)

fritzo commented 3 years ago

Hi @mtsokol your notebook looks great! Even though it is simple, I still think it is worth publishing it and adding to the index, so feel free to submit a PR.

I think the following additions would also help users:

Anyway, don't hesitate to put up a PR even for a small notebook, and thanks for writing this!

khsibr commented 3 years ago

Hi @mtsokol Don't you need to add a bias term in the regression? Also, it would nice to add a line explaining why GammaPoisson is appropriate for this example.

Thanks!

mtsokol commented 3 years ago

Hi @khsibr!

The model I used here was suggested by @fritzo a few comments before so I can't really elaborate why it's designed that way.

I've just opened a PR with updated notebook #2852 where we can continue discussion.

fritzo commented 3 years ago

Completed in #2857