Condition understanding

In the tutorial, we are trying to infer weight distribution given guess and measurement.

(𝗐𝖾𝗂𝗀𝗁𝗍|π—€π—Žπ–Ύπ—Œπ—Œ,π—†π–Ύπ–Ίπ—Œπ—Žπ—‹π–Ύπ—†π–Ύπ—‡π—=9.5)∼?

And the code is below.

def scale_obs(guess):  # equivalent to conditioned_scale above
    weight = pyro.sample("weight", dist.Normal(guess, 1.))
     # here we condition on measurement == 9.5
    return pyro.sample("measurement", dist.Normal(weight, 1.), obs=9.5)

Here I don’t understand why you put observation when you are sampling measurement.

As far as I know the fact that measurement is 9.5 should be reflected when we sample weight, but it is sampled before the reflection.

So I’m confused understanding the code. Could someone help me understand it?

At first glance, it is a bit tricky. But if you take a second to think about it, it makes sense. We’ve defined a model, and if we sample from that model, then we forward sample in order from the model priors. I.e. we first sample weight, and then we sample measurement. But, the obs parameter tells the model that we actually see that measurement is 9.5. So we replace measurement samples with the value we see. And voila: we’ve sampled from the model prior, but we’ve also incorporated the explicit knowledge we have from observing a variable in the real world.

This is sampling. Note that sampling is different from inference. There are different ways to perform inference, some of which use sampling-based methods. But forward sampling from a model is not the same as inference. Intuitively, you can think of forward sampling as following the order of the model forward to get data, while inference might be seen as reasoning backward from data:

[Credit to Frank Wood]

To perform inference in Pyro, you have to explicitly define an inference scheme that tells Pyro how to make guesses about latent variables – in this case, weight.

TL;DR: what you’re describing is sampling, not inference. If you want weight to adjust for the fact that you observe measurement to be 9.5, then you need to define an inference scheme.

1 Like

Thank you for the answer! I think I kind of understood.
Then if we plugin 9.5 in measurement, is something in β€œweight” distribution changes(or disclosed?)?
I want to know what do you mean by infer weight distribution given measurement. So as far as I know, this is inspecting β€œweight” distribution. Then what do we know about β€œweight” distribution after conditioning the model?