首页 > 解决方案 > 如何在 PyMC3 中处理数据标准化/去标准化

问题描述

我从贝叶斯统计和 PyMC3 开始,沿着 Statistical Rethinking 书工作。

我对变量标准化有两个(可能是基本的)问题:

  1. 当我对预测变量和结果变量都进行标准化时,我如何才能在原始尺度上获得先前的可视化?

示例模型:

weight_s = standardize(df1.weight)
height_s = standardize(df1.height)


with pm.Model() as model_adults:
    # Data
    weight = pm.Data('weight', weight_s)
    height = pm.Data('height', height_s)
    
    # Priors
    alpha = pm.Normal('alpha', mu=178, sd=20)
    beta = pm.Lognormal('beta', mu=0, sd=1)
    sigma = pm.Uniform('sigma', lower=0, upper=50)
    
    # Regression
    mu = alpha + beta * weight
    height_hat = pm.Normal('height_hat', mu=mu, sd=sigma, observed=height)
    
    # Prior sampling, trace definition and posterior sampling
    prior = pm.sample_prior_predictive()
    posterior_1 = pm.sample(draws=1000, tune=1000)
    posterior_pred_1 = pm.sample_posterior_predictive(posterior_1)

绘制先验:

#check priors
_, ax = plt.subplots()

x = np.linspace(-3, 3, 50)

for a, b in zip(prior["alpha"], prior["beta"]):
    y = a + b * x
    ax.plot(x, y, c="k", alpha=0.1)

ax.set_xlabel("Weight")
ax.set_ylabel("Height")

当我尝试使用上述代码绘制先验图时,权重变量以标准化形式显示,高度变量以原始比例显示。为什么会这样?有没有办法在原始/标准化尺度上显示两个变量?

  1. 检查模型的预测检查后验预测分布的预测给了我标准化的值:
height_hat=np.mean(posterior_pred_1['height_hat'],axis=0)

我应该如何将它们重新缩放到原始比例?这应该发生在模型内部还是之后?是否有处理可变(去)标准化的标准流程?

很抱歉初学者的问题,我希望有人可以帮助我:)

标签: pythonregressionbayesianpymc3

解决方案


推荐阅读