How to add weights to data in bayesian linear regression

Welcome!

There are various ways to incorporate weights into your model. Many will write custom logp functions (e.g., here and here). But I personally prefer to adjust the scale parameter of my observed variable, which allows for more heavily weighted observations to contribute more model’s logp. It’s simple and relatively transparent. Here’s a simple example:

s = pm.HalfNormal('error', sd = 1)
scaled_s = pm.Deterministic('scaled_s', s / y_weights)
obs = pm.Normal('observation',
                mu = y_pred,
                sd=scaled_s,
                observed=y)

In addition, I would strongly suggest using the defaults when calling pm.sample() until you are confident that they don’t work for you. I would particularly suggest avoiding the use of pm.find_MAP()

# nice and simple
trace = pm.sample(1000)
5 Likes