%load_ext autoreload
%autoreload 2
%matplotlib inline
import pymc3 as pm
import arviz as az
import numpy as np
import seaborn as sns
t_data = pm.StudentT.dist(nu=5).random(size=1000)
sns.distplot(t_data, kde=False);
Using the Student-T directly
with pm.Model() as m0:
ν = pm.HalfNormal('ν', sigma=5)
t = pm.StudentT('t', nu=ν, observed=t_data)
trace0 = pm.sample(1000, tune=2000, cores=2)
Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Multiprocess sampling (2 chains in 2 jobs) NUTS: [ν] Sampling 2 chains, 0 divergences: 100%|██████████| 6000/6000 [00:03<00:00, 1603.59draws/s]
az.plot_trace(trace0);
with pm.Model() as m1:
ν = pm.HalfNormal('ν', sigma=5)
σ2 = pm.InverseGamma('σ2', alpha=ν/2, beta=ν/2, shape=len(t_data))
t = pm.Normal('t', sigma=pm.math.sqrt(σ2), observed=t_data)
trace1 = pm.sample(1000, tune=1000, cores=2)
Auto-assigning NUTS sampler... Initializing NUTS using jitter+adapt_diag... Sequential sampling (2 chains in 1 job) NUTS: [σ2, ν] Sampling chain 0, 0 divergences: 100%|██████████| 2000/2000 [00:15<00:00, 126.54it/s] Sampling chain 1, 0 divergences: 100%|██████████| 2000/2000 [00:16<00:00, 119.73it/s] The number of effective samples is smaller than 25% for some parameters.
az.plot_trace(trace1, var_names=['ν']);