Using the pyLFI Prior class

Example usage of the Prior class.

import matplotlib.pyplot as plt
import numpy as np
import pylfi

We initialize a Gaussian prior over the parameter \(\theta\). The first positional argument can be any scipy.stats distribution passed as str. Following positional and keyword arguments are distribution specific (see scipy.stats documentation). The name keyword argument is required and expects the name of the parameter passed as str. The optional tex keyword argument can be used to provide LaTeX typesetting for the parameter name, which is used as axis label in pyLFI’s plotting procedures if provided.

theta_prior = pylfi.Prior('norm',
                          loc=0,
                          scale=1,
                          name='theta',
                          tex=r'$\theta$'
                          )

Sampling from the prior is done through the rvs method. The size keyword can be used to set the output size of the sample. The sampling can also be seeded through the seed keyword argument.

theta_prior.rvs(size=10, seed=42)

Out:

array([ 0.30471708, -1.03998411,  0.7504512 ,  0.94056472, -1.95103519,
       -1.30217951,  0.1278404 , -0.31624259, -0.01680116, -0.85304393])

The plot_prior method plots the prior pdf or pmf, depending on whether the distribution is continuous or discrete, respectively, evaluated at points \(x\).

x = np.linspace(-4, 4, 1000)
theta_prior.plot_prior(x)
plot prior

Total running time of the script: ( 0 minutes 0.348 seconds)

Gallery generated by Sphinx-Gallery