.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\2-examples\_aux_func.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_2-examples__aux_func.py: Example of a Pyro model and inference ===================================== .. GENERATED FROM PYTHON SOURCE LINES 6-8 .. code-block:: default # sphinx_gallery_thumbnail_number = -1 .. GENERATED FROM PYTHON SOURCE LINES 9-46 .. code-block:: default import pyro import pyro.distributions as dist from pyro.infer import MCMC, NUTS, Predictive import arviz as az def model(distributions_family, data): if distributions_family == "normal_distribution": mu = pyro.sample('$\mu$', dist.Normal(2.07, 0.07)) elif distributions_family in "uniform_distribution": mu = pyro.sample('$\mu$', dist.Uniform(0, 10)) else: raise ValueError("distributions_family must be either 'normal_distribution' or 'uniform_distribution'") sigma = pyro.sample('$\sigma$', dist.Gamma(0.3, 3)) y = pyro.sample('$y$', dist.Normal(mu, sigma), obs=data) return y def infer_model(distributions_family, data): # 1. Prior Sampling prior = Predictive(model, num_samples=100)(distributions_family, data) # 2. MCMC Sampling nuts_kernel = NUTS(model) mcmc = MCMC(nuts_kernel, num_samples=1000, warmup_steps=100) # Assuming 1000 warmup steps mcmc.run(distributions_family, data) # Get posterior samples posterior_samples = mcmc.get_samples(1100) # 3. Sample from Posterior Predictive posterior_predictive = Predictive(model, posterior_samples)(distributions_family, data) # %% az_data = az.from_pyro( posterior=mcmc, prior=prior, posterior_predictive=posterior_predictive ) return az_data .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_examples_2-examples__aux_func.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _aux_func.py <_aux_func.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _aux_func.ipynb <_aux_func.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_