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')
mcmc1.run(rng_key_,x=x)
mcmc1.print_summary()
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')
mcmc2.run(rng_key_, x=x)
mcmc2.print_summary()
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!
Best
N