I will forget it when I try to use it for the first time in a long time, so make a note of it.
--Probabilistic programming library for Python --Can be used for Bayesian inference
pip install pymc3
import numpy as np
from matplotlib import pyplot as plt
#Number of trials and observation results(Number of times)
N = 100
a = 5
with pm.Model() as model:
#Prior distribution
#Uniform distribution with a range of 0 to 1
theta = pm.Uniform('theta', lower=0, upper=1)
#Likelihood function
#Set the binomial distribution as the distribution followed by the number of observations a when the Bernoulli trial is performed N times.
obs = pm.Binomial('a', p=theta, n=N, observed=a)
#Perform inference and from posterior distribution 5000*2 Get a sample
trace = pm.sample(5000, chains=2)
--In pm.sample
, the sample is obtained by MCMC and the result is stored in trace
.
with model:
pm.traceplot(trace)
--The figure on the left shows the estimated posterior distribution for the random variable of interest. --On the right side, the trajectory of the sample obtained after burn-in is displayed. --Burn-in: The period during which the sample is discarded to remove the effect of the initial value of the search.
with model:
# HDI(highest density interval)As 95%Set HDI
print(pm.summary(trace, hdi_prob=0.95)
with model:
pm.plot_posterior(trace, hdi_prob=0.95)
Recommended Posts