models.BayesGlmFit
A sampled Bayesian GLM, from BayesGlm.fit.
Usage
models.BayesGlmFit()Attributes
| Name | Description |
|---|---|
| chains | Number of chains. |
| coefficient_draws | Coefficient draws, one row per draw (chain by chain). |
| dispersion_draws | Dispersion draws, one per draw (constant when fixed). |
| divergences | Divergent transitions among the kept draws. |
| names | Coefficient names. |
| posterior_mean | Posterior means of the coefficients. |
chains
Number of chains.
chains: int
coefficient_draws
Coefficient draws, one row per draw (chain by chain).
coefficient_draws: list[list[float]]
dispersion_draws
Dispersion draws, one per draw (constant when fixed).
dispersion_draws: list[float]
divergences
Divergent transitions among the kept draws.
divergences: int
names
Coefficient names.
names: list[str]
posterior_mean
Posterior means of the coefficients.
posterior_mean: list[float]
Methods
| Name | Description |
|---|---|
| from_json() | Reads an artifact written by to_json. |
| log_likelihood() |
Pointwise log-likelihood of y given design: one row per draw,
|
| loo() |
PSIS-LOO of y given design, with each observation’s relative
|
| loo_pit() |
The leave-one-out PIT of y given design, by Pareto-smoothed
|
| posterior_predictive_check() |
Posterior predictive checks of y given design: each test
|
| predict() | Posterior mean of each row’s mean. |
| predict_distribution() |
Posterior predictive draws across the rows, keyed row = 0, 1, ....
|
| replicate() | One replicated dataset per posterior draw: one row per draw, one |
| summary() | Posterior summary: one dict per parameter with name, mean, |
| to_json() | The fit as a versioned JSON artifact: the specification with its |
from_json()
Reads an artifact written by to_json.
Usage
from_json(text)Parameters
text: str
Returns
BayesGlmFit
Raises
ValueError- For malformed JSON, another format, a newer format version or inconsistent fields.
log_likelihood()
Pointwise log-likelihood of y given design: one row per draw,
Usage
log_likelihood(design, y)Returns
list of list of float
loo()
PSIS-LOO of y given design, with each observation’s relative
Usage
loo(design, y)efficiency estimated from the chains.
Returns
Elpd
loo_pit()
The leave-one-out PIT of y given design, by Pareto-smoothed
Usage
loo_pit(design, y, seed=0)importance weights on replicate’s datasets, randomized over ties; uniform for a calibrated model.
Parameters
design: Designy: list of floatseed: int = 0
Returns
posterior_predictive_check()
Posterior predictive checks of y given design: each test
Usage
posterior_predictive_check(design, y, statistics=None, seed=0)statistic of y against its distribution over replicate’s datasets, with the p-value P(T(y_rep) >= T(y)) (ties count half). A p-value near 0 or 1 says the model cannot reproduce that feature.
Parameters
design: Designy: list of floatstatistics: list of str or float = ["mean", "sd", "min", "max"]-
"mean","sd","min","max","proportion_zero", or a probability for that quantile. seed: int = 0
Returns
list of dict-
name,
observed,replicatedandp_valueper statistic.
predict()
Posterior mean of each row’s mean.
Usage
predict(design)Returns
list of float
predict_distribution()
Posterior predictive draws across the rows, keyed row = 0, 1, ....
Usage
predict_distribution(design, n_sims, seed)Returns
PredictiveDistribution
replicate()
One replicated dataset per posterior draw: one row per draw, one
Usage
replicate(design, seed=0)column per row of design, each response drawn from the family at that draw’s coefficients and dispersion.
Parameters
design: Designseed: int = 0
Returns
list of list of float
summary()
Usage
summary()sd, q05, q50, q95, rhat, ess_bulk and ess_tail; the dispersion last when it was sampled.
Returns
list of dict
to_json()
The fit as a versioned JSON artifact: the specification with its
Usage
to_json()priors and sampler settings, the names, and every kept draw of the coefficients and the dispersion, numbers bit for bit (#978), so a loaded fit summarizes and predicts as this one. from_json reads it back; pickle uses the same document.
Returns
str
Examples
>>> import pickle
>>> from prospicio.models import BayesGlm, BayesGlmFit, Design
>>> x = [(i % 4) - 1.5 for i in range(20)]
>>> y = [[1.0, 2.0, 3.0, 5.0][i % 4] for i in range(20)]
>>> d = Design([[1.0] * 20, x], ["(Intercept)", "x"])
>>> fit = BayesGlm("poisson", chains=1, tune=50, draws=20).fit(d, y)
>>> BayesGlmFit.from_json(fit.to_json()).posterior_mean == fit.posterior_meanTrue
>>> pickle.loads(pickle.dumps(fit)).predict(d) == fit.predict(d)True