说明
pymc算是多年的老朋友了,中间失联了好几年。
内容
1 安装
安装更加麻烦了,不能很好的和其他的环境兼容。在官网上,也是建议用conda的方式安装。
conda create -c conda-forge -n pymc_env "pymc>=5"
conda activate pymc_env
2 测试
import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pymc as pm
import xarray as xr
from pymc import HalfCauchy, Model, Normal, sample
print(f"Running on PyMC v{pm.__version__}")
Running on PyMC v5.16.2
跑跑看
with Model() as model: # model specifications in PyMC are wrapped in a with-statement
# Define priors
sigma = HalfCauchy("sigma", beta=10)
intercept = Normal("Intercept", 0, sigma=20)
slope = Normal("slope", 0, sigma=20)
# Define likelihood
likelihood = Normal("y", mu=intercept + slope * x, sigma=sigma, observed=y)
# Inference!
# draw 3000 posterior samples using NUTS sampling
idata = sample(3000)
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [sigma, Intercept, slope]
Sampling 4 chains, 0 divergences ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 / 0:00:05
Sampling 4 chains for 1_000 tune and 3_000 draw iterations (4_000 + 12_000 draws total) took 6 seconds.
这样应该是可以了
3 资料
- Installation
- gallery
- books
先这些吧,后面还有很多事需要做才能完全捡起来。
这块算是一条通往近似计算、贝叶斯推断的一条路,是更好的机器学习方法。