sample

static LinearRegression.sample(model: Model, n_iterations: int, burn_in_iterations: int, n_chains: int, seed: int | None = None) None

Samples a sequence of observations from the full posterior distribution of regressors’ parameters \(\beta_j\) and variance \(\sigma^2\). First burn_in_iterations are discarded since they may not accurately represent the desired distribution. For each variable, it generates n_chain Markov chains.

Parameters

modelbaypy.model.model.Model

The model with data, regressors, response variable and priors to be solved through Monte Carlo sampling.

n_iterationsint

Number of total sampling iteration for each chain. It must be a strictly positive integer.

burn_in_iterationsint

Number of burn-in iteration for each chain. It must be a positive integer or 0.

n_chainsint

Number of chains. It must be a strictly positive integer.

seedint, optional

Random seed to use for reproducibility of the sampling.

Raises

TypeError
  • If model is not a baypy.model.model.Model,

  • if n_iterations is not a int,

  • if burn_in_iterations is not a int,

  • if n_chains is not a int,

  • if seed is not a int.

ValueError
  • If model.data is None,

  • if model.response_variable is None,

  • if model.response_variable is not a column of model.data

  • if model.priors is None,

  • if a model.priors key is not a column of model.data,

  • If n_iterations is equal to or less than 0,

  • if burn_in_iterations is less than 0,

  • if n_chains is equal to or less than 0,

  • if seed is not between 0 and 2**32 - 1.

Notes

The linear regression model of the response variable \(y\) with respect to regressors \(X\) is:

\[y \sim N(\mu, \sigma^2)\]
\[\mu = \beta_0 + B X = \beta_0 + \sum_{j = 1}^m \beta_j x_j\]

and the likelihood is:

\[p \left( y \left\vert B,\sigma^2 \right. \right) = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp{- \frac{\left(y - \mu \right)^2}{2 \sigma^2}} .\]