代码:
import numpy as np
import pymc as pm
import arviz as az
import matplotlib.pyplot as plt
print(f"PyMC version: {pm.__version__}")
print(f"ArviZ version: {az.__version__}")
# 1. 生成模拟数据
np.random.seed(42)
true_intercept = 1.0
true_slope = 2.5
sigma = 0.5
n_samples = 100
x = np.linspace(0, 1, n_samples)
y = true_intercept + true_slope * x + np.random.normal(0, sigma, size=n_samples)
# 绘制数据
plt.scatter(x, y, label="Observed data")
plt.plot(x, true_intercept + true_slope * x, 'r-', label="True regression line")
plt.legend()
plt.title("Generated Data")
plt.show()
# 2. 构建PyMC模型
with pm.Model() as linear_regression:
# 定义先验分布
intercept = pm.Normal("intercept", mu=0, sigma=10)
slope = pm.Normal("slope", mu=0, sigma=10)
sigma = pm.HalfNormal("sigma", sigma=1)
# 定义线性模型
mu = intercept + slope * x
# 定义似然函数
likelihood = pm.Normal("y", mu=mu, sigma=sigma, observed=y)
# 3. 执行MCMC采样
idata = pm.sample(2000, tune=1000, chains=4, cores=1) # 直接返回InferenceData
# 4. 查看结果摘要
print(az.summary(idata))
# 5. 绘制后验分布
az.plot_trace(idata)
plt.tight_layout()
plt.show()
# 6. 后验预测采样
with linear_regression:
# 生成后验预测样本并自动添加到idata
idata.extend(pm.sample_posterior_predictive(idata))
# 7. 绘制后验预测检查
az.plot_ppc(idata)
plt.show()
结果: