Skip to content

Commit 5baed01

Browse files
rdiaz02yebai
andauthored
Fix error in example and add simpler example (#1928)
As discussed in https://discourse.julialang.org/t/turing-guide-documentation-prob-examples-throws-errors/50319 the example of the use of `prob"..."` threw errors from at least: - model was not instantiated - some queries used `s` but the model used `s²` In addition, it was unclear the role of the values in the instantiated model, which is now clarified with a simpler example that is easily checked against a standard normal. Co-authored-by: Hong Ge <[email protected]>
1 parent ad9e529 commit 5baed01

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

docs/src/using-turing/guide.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,28 +380,58 @@ Similarly, when using a particle sampler, the Julia variable used should either
380380

381381
### Querying Probabilities from Model or Chain
382382

383+
Consider first the following simplified `gdemo` model:
384+
```julia
385+
@model function gdemo0(x)
386+
s ~ InverseGamma(2, 3)
387+
m ~ Normal(0, sqrt(s))
388+
x ~ Normal(m, sqrt(s))
389+
end
390+
391+
# Instantiate three models, with different value of x
392+
model1 = gdemo0(1)
393+
model4 = gdemo0(4)
394+
model10 = gdemo0(10)
395+
```
383396

384-
Consider the following `gdemo` model:
397+
Now, query the instantiated models: compute the likelihood of `x = 1.0` given the values of `s = 1.0` and `m = 1.0` for the parameters:
398+
```julia
399+
prob"x = 1.0 | model = model1, s = 1.0, m = 1.0"
400+
prob"x = 1.0 | model = model4, s = 1.0, m = 1.0"
401+
prob"x = 1.0 | model = model10, s = 1.0, m = 1.0"
402+
```
403+
404+
Notice that even if we use three models, instantiated with three different values of `x`, we should obtain the same likelihood. We can easily verify that value in this case:
405+
406+
```julia
407+
pdf(Normal(1.0, 1.0), 1.0)
408+
```
409+
410+
411+
Let us now consider the following `gdemo` model:
385412
```julia
386413
@model function gdemo(x, y)
387414
~ InverseGamma(2, 3)
388415
m ~ Normal(0, sqrt(s²))
389416
x ~ Normal(m, sqrt(s²))
390417
y ~ Normal(m, sqrt(s²))
391418
end
419+
420+
# Instantiate the model.
421+
model = gdemo(2.0, 4.0)
392422
```
393423

394424
The following are examples of valid queries of the `Turing` model or chain:
395425

396-
- `prob"x = 1.0, y = 1.0 | model = gdemo, s = 1.0, m = 1.0"` calculates the likelihood of `x = 1` and `y = 1` given `s = 1` and `m = 1`.
426+
- `prob"x = 1.0, y = 1.0 | model = model, s = 1.0, m = 1.0"` calculates the likelihood of `x = 1` and `y = 1` given `s = 1` and `m = 1`.
397427

398-
- `prob"s = 1.0, m = 1.0 | model = gdemo, x = nothing, y = nothing"` calculates the joint probability of `s = 1` and `m = 1` ignoring `x` and `y`. `x` and `y` are ignored so they can be optionally dropped from the RHS of `|`, but it is recommended to define them.
428+
- `prob"s² = 1.0, m = 1.0 | model = model, x = nothing, y = nothing"` calculates the joint probability of `s = 1` and `m = 1` ignoring `x` and `y`. `x` and `y` are ignored so they can be optionally dropped from the RHS of `|`, but it is recommended to define them.
399429

400-
- `prob"s = 1.0, m = 1.0, x = 1.0 | model = gdemo, y = nothing"` calculates the joint probability of `s = 1`, `m = 1` and `x = 1` ignoring `y`.
430+
- `prob"s² = 1.0, m = 1.0, x = 1.0 | model = model, y = nothing"` calculates the joint probability of `s = 1`, `m = 1` and `x = 1` ignoring `y`.
401431

402-
- `prob"s = 1.0, m = 1.0, x = 1.0, y = 1.0 | model = gdemo"` calculates the joint probability of all the variables.
432+
- `prob"s² = 1.0, m = 1.0, x = 1.0, y = 1.0 | model = model"` calculates the joint probability of all the variables.
403433

404-
- After the MCMC sampling, given a `chain`, `prob"x = 1.0, y = 1.0 | chain = chain, model = gdemo"` calculates the element-wise likelihood of `x = 1.0` and `y = 1.0` for each sample in `chain`.
434+
- After the MCMC sampling, given a `chain`, `prob"x = 1.0, y = 1.0 | chain = chain, model = model"` calculates the element-wise likelihood of `x = 1.0` and `y = 1.0` for each sample in `chain`.
405435

406436
- If `save_state=true` was used during sampling (i.e., `sample(model, sampler, N; save_state=true)`), you can simply do `prob"x = 1.0, y = 1.0 | chain = chain"`.
407437

0 commit comments

Comments
 (0)