How do I handle data standardization/destandardization in PyMC3

Viewed 99

I am starting with Bayesian Statistics and and PyMC3, working along the Statistical Rethinking book.

I have two (probably basic) questions to variable standardization:

  1. When I standardize both predictor and outcome variable, how can I still get prior visualization on the original scale?

Example model:

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)

Plotting the prior:

#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")

When I try to plot the priors using the above code, the weight variable is shown in the standardized form, the height variable on original scale. Why is it so? Is there a way to show both variables on the original / both on the standardized scale?

  1. Checking predictions from the model Checking the predictions from the posterior predictive distributions gives me standardized values:
height_hat=np.mean(posterior_pred_1['height_hat'],axis=0)

How should I rescale them to the original scale? Should this happen within the model or afterwards? Is there any standard process for dealing with variable (de)standardization?

Sorry for the beginner questions, I hope someone can help me :)

0 Answers
Related