python - 如何在 PyMC3 中处理数据标准化/去标准化
问题描述
我从贝叶斯统计和 PyMC3 开始,沿着 Statistical Rethinking 书工作。
我对变量标准化有两个(可能是基本的)问题:
- 当我对预测变量和结果变量都进行标准化时,我如何才能在原始尺度上获得先前的可视化?
示例模型:
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")
当我尝试使用上述代码绘制先验图时,权重变量以标准化形式显示,高度变量以原始比例显示。为什么会这样?有没有办法在原始/标准化尺度上显示两个变量?
- 检查模型的预测检查后验预测分布的预测给了我标准化的值:
height_hat=np.mean(posterior_pred_1['height_hat'],axis=0)
我应该如何将它们重新缩放到原始比例?这应该发生在模型内部还是之后?是否有处理可变(去)标准化的标准流程?
很抱歉初学者的问题,我希望有人可以帮助我:)
解决方案
推荐阅读
- c++ - 如何修复错误:C++ 中的大小写重复?
- amazon-web-services - 服务到服务名称无法解析 ECS Fargate
- javascript - 对原始状态的更改副本做出反应状态更改?
- multithreading - 如何使使用 Fortran 模块在 Abaqus 子例程之间共享变量成为线程安全的?
- swift - SwiftUI 为什么不能在视图之间传递发布者?
- jquery - jQuery:如何在这种特定情况下恢复点击事件的初始状态?
- php - 当我尝试从数据库中提取数据时 PHP 未定义变量
- oracle - 从 PL/SQL 调用 HTTPS 站点的参数错误
- powerbi - 用 dax 计算 power bi 的总方差
- javascript - req.body 在 Node.js 中是未定义的,即使当我记录 req 时它会显示正文及其内容