Using Posterior Samples From First in Second Model

Hi everyone!

I’m trying to use the posterior samples of a model during the sampling of model2; Currently I simply “glue” both models together, using 2 sampling statements with different obs= but there should be a better way, right?

A simple example would be the following:

import numpyro
import numpyro.distributions as dist
import numpy as np

# Gen Data
x = np.random.normal(0, 1, 100)
m = 5
b = 2
y = m * x + b + np.random.normal(0, 1, 100)

scale = 2.
y2 = y * scale

def model(x):
  m = numpyro.sample("m", dist.Normal(0,1))
  b = numpyro.sample("b", dist.Normal(0,1))
  with numpyro.plate("data", len(x)):
    numpyro.sample("y", dist.Normal(m * x + b, 1), obs=y)

kernel = NUTS(model)
mcmc1 = MCMC(kernel, num_warmup=1000, num_samples=1000, num_chains=4, progress_bar=True, chain_method='parallel'),x=x)
def model2(x):
    m = numpyro.sample("m", dist.Normal(0,1))
    b = numpyro.sample("b", dist.Normal(0,1))

    projection = (m * x + b)
    numpyro.sample("y", dist.Normal(projection, 1), obs=y)

    scale = numpyro.sample("scale", dist.Normal(0,1))
    numpyro.sample("y_maxed", dist.Normal(projection * scale, 1), obs=y2)

kernel2 = NUTS(model2)
mcmc2 = MCMC(kernel2, num_warmup=1000, num_samples=1000, num_chains=4, progress_bar=True, chain_method='parallel'), x=x)

While model2 works (in the sense that the mean parameter values of m and b are correct, I noticed that the effective number of samples in model_2 for m and b is lower (which makes sense) and this kind of double-sampling seems error-prone.

Happy to get any hints!


what is the generative process? why are you effectively observing the same thing twice?

Thanks for the reply!

The idea is that I observe the data on y2 and I have an assumption on how this y2 comes into being, namely as a function of some intermediate value y (here very simply just y2 = f(y) = 2*y) for which is also observe data; in other terms, modeling y could be called a “measurement model”, whereas I’m ultimately interested in the data of y2.

i’m afraid i don’t understand. are both y and y2 observed/measured in the sense that you have concrete values for each of them?

Yes exactly, the idea is that I can measure parts of the whole system and the lets say final outcome and want to model everything jointly.

if you have two measurements that are independent in some sense—one taken with a microscope, one with a scale, say—then having two observe statements would generally be appropriate