:orphan:

Code Examples
=============

Examples on specifying models and doing inference in NumPyro.

`View source files on github`__

.. _github: https://github.com/pyro-ppl/numpyro/tree/master/examples

__ github_



.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example trains a Conditional Variational Autoencoder (CVAE) [1] on the MNIST data using Flax&#x27; neural network API. The implementation can be found here: https://github.com/pyro-ppl/numpyro/tree/master/examples/cvae-flax">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_cvae_thumb.png
    :alt:

  :ref:`sphx_glr_examples_cvae.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Conditional Variational Autoencoder in Flax</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example, which is adapted from [1], illustrates how to leverage non-centered parameterization using the reparam handler. We will examine the difference between two types of parameterizations on the 10-dimensional Neal&#x27;s funnel distribution. As we will see, HMC gets trouble at the neck of the funnel if centered parameterization is used. On the contrary, the problem can be solved by using non-centered parameterization.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_funnel_thumb.png
    :alt:

  :ref:`sphx_glr_examples_funnel.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Neal's Funnel</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Generative model:">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_stochastic_volatility_thumb.png
    :alt:

  :ref:`sphx_glr_examples_stochastic_volatility.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Stochastic Volatility</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example replicates the great case study [1], which leverages the Lotka-Volterra equation [2] to describe the dynamics of Canada lynx (predator) and snowshoe hare (prey) populations. We will use the dataset obtained from [3] and run MCMC to get inferences about parameters of the differential equation governing the dynamics.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_ode_thumb.png
    :alt:

  :ref:`sphx_glr_examples_ode.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Predator-Prey Model</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates the usage of the contrib class NestedSampler, which is a wrapper of jaxns library ([1]) to be used for NumPyro models.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_gaussian_shells_thumb.png
    :alt:

  :ref:`sphx_glr_examples_gaussian_shells.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Nested Sampling for Gaussian Shells</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example we show how to use jax.lax.scan to avoid writing a (slow) Python for-loop. In this toy example, with --num-data=1000, the improvement is of almost almost 3x.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_ar2_thumb.png
    :alt:

  :ref:`sphx_glr_examples_ar2.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: AR2 process</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The UCBadmit data is sourced from the study [1] of gender biased in graduate admissions at UC Berkeley in Fall 1973:">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_ucbadmit_thumb.png
    :alt:

  :ref:`sphx_glr_examples_ucbadmit.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Generalized Linear Mixed Models</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A toy mixture model to provide a simple example for implementing discrete enumeration::">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_toy_mixture_model_discrete_enumeration_thumb.png
    :alt:

  :ref:`sphx_glr_examples_toy_mixture_model_discrete_enumeration.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Toy Mixture Model with Discrete Enumeration</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="AutoDAIS constructs a guide that combines elements of Hamiltonian Monte Carlo, Annealed Importance Sampling, and Variational Inference.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_dais_demo_thumb.png
    :alt:

  :ref:`sphx_glr_examples_dais_demo.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: AutoDAIS</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example answers this question by estimating a logistic regression model where the covariates are whether the customer got called and their gender. We place a multivariate normal prior on the regression coefficients. We report the 95% highest posterior density interval for the effect of making a call.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_proportion_test_thumb.png
    :alt:

  :ref:`sphx_glr_examples_proportion_test.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Proportion Test</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example is adapted from [1]. The model in the paper estimates death rates for 6791 small areas in England for 19 age groups (0, 1-4, 5-9, 10-14, ..., 80-84, 85+ years) from 2002-19.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_mortality_thumb.png
    :alt:

  :ref:`sphx_glr_examples_mortality.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Modelling mortality over space and time</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example, we model and predict how many fish are caught by visitors to a state park. Many groups of visitors catch zero fish, either because they did not fish at all or because they were unlucky. We would like to explicitly model this bimodal behavior (zero versus non-zero) and ascertain which variables contribute to each behavior.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_zero_inflated_poisson_thumb.png
    :alt:

  :ref:`sphx_glr_examples_zero_inflated_poisson.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Zero-Inflated Poisson regression model</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates the use of data subsampling in HMC using Energy Conserving Subsampling. Data subsampling is applicable when the likelihood factorizes as a product of N terms.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_hmcecs_thumb.png
    :alt:

  :ref:`sphx_glr_examples_hmcecs.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Hamiltonian Monte Carlo with Energy Conserving Subsampling</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example we show how to implement Exponential Smoothing. This is intended to be a simple counter-part to the Time Series Forecasting notebook.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_holt_winters_thumb.png
    :alt:

  :ref:`sphx_glr_examples_holt_winters.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Holt-Winters Exponential Smoothing</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="We demonstrate how to use NUTS to do inference on a simple (small) Bayesian neural network with two hidden layers.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_bnn_thumb.png
    :alt:

  :ref:`sphx_glr_examples_bnn.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Bayesian Neural Network</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="We demonstrate how to use NUTS to do sparse regression using the Horseshoe prior [1] for both continuous- and binary-valued responses. For a more complex modeling and inference approach that also supports quadratic interaction terms in a way that is efficient in high dimensions see examples/sparse_regression.py.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_horseshoe_regression_thumb.png
    :alt:

  :ref:`sphx_glr_examples_horseshoe_regression.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Horseshoe Regression</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example, we demonstrate how to implement and perform Bayesian inference for a Vector Autoregressive process of order 2 (VAR(2)). VAR models are widely used in time series analysis, especially for capturing the dynamics between multiple variables.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_var2_thumb.png
    :alt:

  :ref:`sphx_glr_examples_var2.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: VAR(2) process</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Example: Variational Autoencoder">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_vae_thumb.png
    :alt:

  :ref:`sphx_glr_examples_vae.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Variational Autoencoder</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="We demonstrate how to use SteinVI to predict housing prices using a BNN for the Boston Housing prices dataset from the UCI regression benchmarks.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_stein_bnn_thumb.png
    :alt:

  :ref:`sphx_glr_examples_stein_bnn.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Bayesian Neural Network with SteinVI</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates how to use a trained AutoBNAFNormal autoguide to transform a posterior to a Gaussian-like one. The transform will be used to get better mixing rate for NUTS sampler.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_neutra_thumb.png
    :alt:

  :ref:`sphx_glr_examples_neutra.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Neural Transport</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example we show how to use NUTS to sample from the posterior over the hyperparameters of a gaussian process.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_gp_thumb.png
    :alt:

  :ref:`sphx_glr_examples_gp.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Gaussian Process</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Original example from Pyro: https://github.com/pyro-ppl/pyro/blob/dev/examples/baseball.py">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_baseball_thumb.png
    :alt:

  :ref:`sphx_glr_examples_baseball.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Baseball Batting Average</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates the usages of various MCMC methods which are suitable for tall data:">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_covtype_thumb.png
    :alt:

  :ref:`sphx_glr_examples_covtype.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: MCMC Methods for Tall Data</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example, we will follow [1] to construct a semi-supervised Hidden Markov Model for a generative model with observations are words and latent variables are categories. Instead of automatically marginalizing all discrete latent variables (as in [2]), we will use the &quot;forward algorithm&quot; (which exploits the conditional independent of a Markov model - see [3]) to iteratively do this marginalization.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_hmm_thumb.png
    :alt:

  :ref:`sphx_glr_examples_hmm.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Hidden Markov Model</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example we show how to implement Thompson sampling for Bayesian optimization with Gaussian processes. The implementation is based on this tutorial: https://gdmarmerola.github.io/ts-for-bayesian-optim/">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_thompson_sampling_thumb.png
    :alt:

  :ref:`sphx_glr_examples_thompson_sampling.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Thompson sampling for Bayesian Optimization with GPs</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example models the dihedral angles that occur in the backbone of a protein as a mixture of skewed directional distributions. The backbone angle pairs, called \phi and \psi, are a canonical representation for the fold of a protein. In this model, we fix the third dihedral angle (omega) as it usually only takes angles 0 and pi radian, with the latter being the most common. We model the angle pairs as a distribution on the torus using the sine distribution [1] and break point-wise (toroidal) symmetry using sine-skewing [2].">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_ssbvm_mixture_thumb.png
    :alt:

  :ref:`sphx_glr_examples_ssbvm_mixture.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Sine-skewed sine (bivariate von Mises) mixture</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example, we will follow [1] to implement the ProdLDA topic model from Autoencoding Variational Inference For Topic Models by Akash Srivastava and Charles Sutton [2]. This model returns consistently better topics than vanilla LDA and trains much more quickly. Furthermore, it does not require a custom inference algorithm that relies on complex mathematical derivations. This example also serves as an introduction to Flax and Haiku modules in NumPyro.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_prodlda_thumb.png
    :alt:

  :ref:`sphx_glr_examples_prodlda.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: ProdLDA with Flax and Haiku</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The model DMM based on reference [1][2] and the Pyro DMM example: https://pyro.ai/examples/dmm.html.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_stein_dmm_thumb.png
    :alt:

  :ref:`sphx_glr_examples_stein_dmm.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Deep Markov Model inferred using SteinVI</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example is ported from [8].">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_capture_recapture_thumb.png
    :alt:

  :ref:`sphx_glr_examples_capture_recapture.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: CJS Capture-Recapture Model for Ecological Data</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="In this example, we run MCMC for various crowdsourced annotation models in [1].">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_annotation_thumb.png
    :alt:

  :ref:`sphx_glr_examples_annotation.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Bayesian Models of Annotation</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example is ported from [1], which shows how to marginalize out discrete model variables in Pyro.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_hmm_enum_thumb.png
    :alt:

  :ref:`sphx_glr_examples_hmm_enum.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Enumerate Hidden Markov Model</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="We demonstrate how to do (fully Bayesian) sparse linear regression using the approach described in [1]. This approach is particularly suitable for situations with many feature dimensions (large P) but not too many datapoints (small N). In particular we consider a quadratic regressor of the form:">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_sparse_regression_thumb.png
    :alt:

  :ref:`sphx_glr_examples_sparse_regression.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Sparse Regression</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example replicates the model in the excellent case study by Aki Vehtari [1] (originally written using R and Stan). The case study uses approximate Gaussian processes [2] to model the relative number of births per day in the US from 1969 to 1988. The Hilbert space approximation is way faster than the exact Gaussian processes because it circumvents the need for inverting the covariance matrix.">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_hsgp_thumb.png
    :alt:

  :ref:`sphx_glr_examples_hsgp.py`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example: Hilbert space approximation for Gaussian processes.</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/cvae
   /examples/funnel
   /examples/stochastic_volatility
   /examples/ode
   /examples/gaussian_shells
   /examples/ar2
   /examples/ucbadmit
   /examples/toy_mixture_model_discrete_enumeration
   /examples/dais_demo
   /examples/proportion_test
   /examples/mortality
   /examples/zero_inflated_poisson
   /examples/hmcecs
   /examples/holt_winters
   /examples/bnn
   /examples/horseshoe_regression
   /examples/var2
   /examples/vae
   /examples/stein_bnn
   /examples/neutra
   /examples/gp
   /examples/baseball
   /examples/covtype
   /examples/hmm
   /examples/thompson_sampling
   /examples/ssbvm_mixture
   /examples/prodlda
   /examples/stein_dmm
   /examples/capture_recapture
   /examples/annotation
   /examples/hmm_enum
   /examples/sparse_regression
   /examples/hsgp


.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-gallery

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download all examples in Python source code: examples_python.zip </examples/examples_python.zip>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download all examples in Jupyter notebooks: examples_jupyter.zip </examples/examples_jupyter.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
