import numpy as np
import emcee
import matplotlib.pyplot as plt
import numba as nb
This is a tiny demo of the MCMC algorithm from the emcee library, which computes the posterior for some parameters based on the likelihood function and priors on the parameters.
= np.random.default_rng(1)
rng
= 2.0
y
@nb.njit
def model(mu):
return mu + np.random.randn()
@nb.njit
def log_prob(mu, y):
= y - model(mu)
diff return -0.5 * np.sum(diff ** 2)
= 1000
nwalkers = 1
ndim = emcee.EnsembleSampler(nwalkers, ndim, log_prob, args=[y])
sampler
= rng.normal(size=(nwalkers, ndim))
start = sampler.run_mcmc(start, 100, progress=True)
r
plt.hist(r.coords)print(np.mean(r.coords), np.std(r.coords) / np.sqrt(len(r.coords)))
100%|██████████| 100/100 [00:00<00:00, 168.91it/s]
1.962706921253352 0.04570914440079373