How Does the Sampler Use Covariate Observed Data?

I am not sure how the sampler uses the observed value in conjunction with regression downstream but one suggested way to add noise to such a variable is a bit different than what you are doing. It is to create a latent variable, see for instance the answer here:

So in your case it would be:

import pymc as pm
import arviz as az

x_observed = [17, 7, 2, 21, 42]
y_observed = [21, 23, 45, 31, 101]

with pm.Model() as model:
  x_mu = pm.Normal("x_mu",0,1)
  x_sigma = pm.HalfNormal("x_sigma",1)
  x_err = pm.HalfNormal("x_err", 1)
  x_lat = pm.Normal("x_lat", x_mu, x_sigma)
  x = pm.Normal("x",x_lat, x_err, observed=x_observed)

  a = pm.Normal("alpha",0,1)
  b = pm.Normal("beta",0,1)

  y_mu = pm.Deterministic("y_mu",a+b*x_lat)
  y_sigma = pm.HalfNormal("y_sigma",1)
  y = pm.Normal("y",y_mu,y_sigma,observed=y_observed)

  trace = pm.sample()

The posterior I get from here is
test1

where as if I run this

with pm.Model() as model:
  x_mu = pm.Normal("x_mu",0,1)
  x_sigma = pm.HalfNormal("x_sigma",1)
  x_err = pm.HalfNormal("x_err", 1)
  x_lat = pm.Normal("x_lat", x_mu, x_sigma)
  x = pm.Normal("x",x_lat, x_err, observed=x_observed)

  trace = pm.sample()

the posterior I get is
test2

3 Likes