Git Product home page Git Product logo

rethinking-pyro's Introduction

Statistical Rethinking with PyTorch and Pyro

Status: Codes in this repository have not been updated to the latest Pyro versions. Please check out https://github.com/fehiepsi/rethinking-numpyro repository for the updated codes of 2nd edition in NumPyro.

Statistical Rethinking is an excellent book for applied Bayesian data analysis. The accompanying codes for the book are written in R and Stan. They are then ported to Python language using PyMC3. Recently, Pyro emerges as a scalable and flexible Bayesian modeling tool (see its tutorial page), so to attract statisticians to this new library, I decided to make a Pyronic version for the codes in this repository. Inspired by the PyMC3onic version, I keep the codes in this repository as close as possible to the original codes in the book.

To say a bit more about Pyro, it is a universal probabilistic programming language which is built on top of PyTorch, a very popular platform for deep learning. If you are familiar with numpy, the transition from numpy.array to torch.tensor is rather straightforward (as demonstrated in this tutorial).

How to read the notebooks

Installation

pip install jupyter pandas pyro-ppl seaborn torch

rethinking-pyro's People

Contributors

fehiepsi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rethinking-pyro's Issues

flist does not run with large (20) sigma

Hello,
I am new to pyro and I try to explore the different implementations. When I try to run the given code I am having problems at Code 4.25.
Essentially the MAP function doesn´t run and 4.1 is not createt during (m4_1 = MAP(flist).run(d2_height))
Therefore running:
precis(m4_1)
gives me following Error.
"NameError: name 'm4_1' is not defined"
Apparently when I changed the sigma of:
mu = pyro.sample("mu", dist.Normal(178, 20))
to 1 or smaller
mu = pyro.sample("mu", dist.Normal(178, 1))
I am able to run the code and get an answer running
precis(m4_1)

I am intrigued by this behaviour and would definately appreaciate it if an expert can give an answer to this question...

Very best!

LinAlgError for MAP in code 4.26 (chapter 4)

Dear reader,

I am working through the Statistical Rethinking book and running the code in Pyro using this great repository. I encountered the following error when during the model with MAP:

(4.24)

howell1 = pd.read_csv("../data/Howell1.csv", sep=";")

(4.25)

def flist(height):
    mu = pyro.sample("mu", dist.Normal(178, 20))
    sigma = pyro.sample("sigma", dist.Uniform(0, 50))
    with pyro.plate("plate"):
        pyro.sample("height", dist.Normal(mu, sigma), obs=height)

Thus far it worked well. Yet with (4.26) I run into the following error:

d2_height = torch.tensor(d2["height"], dtype=torch.float)
m4_1 = MAP(flist).run(d2_height)
---------------------------------------------------------------------------
_LinAlgError                              Traceback (most recent call last)
Cell In[38], line 2
      1 d2_height = torch.tensor(d2["height"], dtype=torch.float)
----> 2 m4_1 = MAP(flist).run(d2_height)

File ~/learning_pyro/notebooks/rethinking.py:101, in MAP.run(self, *args, **kwargs)
     99         except Exception as e:
    100             last_error = e
--> 101 raise last_error

File ~/learning_pyro/notebooks/rethinking.py:98, in MAP.run(self, *args, **kwargs)
     96 for i in range(10):
     97     try:
---> 98         return super(MAP, self).run(*args, **kwargs)
     99     except Exception as e:
    100         last_error = e

File ~/anaconda3/envs/PYRO/lib/python3.10/site-packages/pyro/infer/abstract_infer.py:251, in TracePosterior.run(self, *args, **kwargs)
    249 self._reset()
    250 with poutine.block():
--> 251     for i, vals in enumerate(self._traces(*args, **kwargs)):
    252         if len(vals) == 2:
    253             chain_id = 0

File ~/learning_pyro/notebooks/rethinking.py:85, in MAP._traces(self, *args, **kwargs)
     82     return loss
     84 optimizer.step(closure)
---> 85 guide = delta_guide.laplace_approximation(*args, **kwargs)
     87 # get posterior
     88 for i in range(self.num_samples):

File ~/anaconda3/envs/PYRO/lib/python3.10/site-packages/pyro/infer/autoguide/guides.py:1181, in AutoLaplaceApproximation.laplace_approximation(self, *args, **kwargs)
   1179     cov /= scale[:, None]
   1180     cov /= scale[None, :]
-> 1181     scale_tril = torch.linalg.cholesky(cov)
   1183 gaussian_guide = AutoMultivariateNormal(self.model)
   1184 gaussian_guide._setup_prototype(*args, **kwargs)

_LinAlgError: linalg.cholesky: The factorization could not be completed because the input is not positive-definite (the leading minor of order 1 is not positive-definite).

Notably, continuing to work with the MAP class for code snippet 4.29 does work again and gives me a similar output to the one described online.

What could be the underlying issue here?

Update to be consistent with second edition?

Are there any plans to update the rethinking.py code and code examples to be consistent with the second edition of the book? For example is there going to be an implementation of quap?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.