* AKA Bayesian statistics, Probabilistic modeling, probabilistic programming, probabilistic machine learning...
Bayesian modeling is conceptually simple :-)
Bayesian modeling is mathematically difficult :-(
The promise: clear separation of modeling and inference. Practitioners should focus on modeling, not computational details
with pm.Model() as model_g:
μ = pm.Normal('μ', 0, 10) # Prior
σ = pm.HalfNormal('σ', 25) # Prior
y = pm.Normal('y', μ, σ, observed=data) # Likelihood
trace_g = pm.sample(1000)
az.plot_joint(trace_g, kind='kde', fill_last=False, textsize=16);
Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Multiprocess sampling (2 chains in 2 jobs) NUTS: [σ, μ] Sampling 2 chains: 100%|██████████| 3000/3000 [00:01<00:00, 2684.83draws/s]
with pm.Model() as model_am:
# Priors
cp = pm.DiscreteUniform('cp', lower=years.min(), upper=years.max(), testval=1900)
rate_0 = pm.Exponential('rate_0', 1)
rate_1 = pm.Exponential('rate_1', 1)
rates = pm.math.switch(pc >= years, rate_0, rate_1)
# Likelihood
dissasters = pm.Poisson('dissasters', rates, observed=data)
# Inference
trace_am = pm.sample(1000)
with pm.Model() as linear_regression:
# Priors
α = pm.Normal('α', mu=0, sd=10)
β = pm.Normal('β', mu=0, sd=1, shape=K)
ϵ = pm.HalfNormal('ϵ', 10)
μ = α + pm.math.dot(X, β) # linear model
#Likelihood
y_pred = pm.Normal('y_pred', mu=μ, sd=ϵ, observed=y_obs)
# Inference
trace = pm.Sample(1000)
with pm.Model() as model_gh:
# hyper-priors
μ_μ = pm.Normal('μ_μ', 0, 10)
σ_μ = pm.HalfNormal('σ_μ', 25)
# Priors
μ = pm.Normal('μ', μ_μ, σ_μ, shape=K)
σ = pm.HalfNormal('σ', 25)
# Likelihood
y = pm.Normal('y', μ, σ, observed=data)
# Inference
trace_gh = pm.sample(1000)
N-dimensional labeled arrays and datasets in Python
xarray.Dataset is an in-memory representation of a netCDF file
inference_data = az.load_arviz_data('centered_eight')
inference_data
Inference data with groups: > posterior > sample_stats > posterior_predictive > prior > observed_data
az.plot_forest(inference_data, var_names=['mu', 'theta'], combined=True, figsize=(10, 8));
Styles
az.style.use('arviz-darkgrid')
Support for PyMC3 and PyStan objects