[PYTHON] How to use PyMC3 (Bayesian inference)

I will forget it when I try to use it for the first time in a long time, so make a note of it.

What is PyMC3?

--Probabilistic programming library for Python --Can be used for Bayesian inference

Installation

pip install pymc3

Basic writing


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.

Visualization of sample (posterior distribution)

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.

Displaying summary statistics

with model:
  # HDI(highest density interval)As 95%Set HDI
  print(pm.summary(trace, hdi_prob=0.95)

Visualize sample (posterior distribution) with summary statistics

with model:
  pm.plot_posterior(trace, hdi_prob=0.95)

Reference material

Recommended Posts

How to use PyMC3 (Bayesian inference)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use the zip function
How to use the optparse module
How to use SWIG from waf
Summary of how to use pandas.DataFrame.loc
How to install and use Tesseract-OCR