You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: docs/src/using-turing/guide.md
+36-6Lines changed: 36 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -380,28 +380,58 @@ Similarly, when using a particle sampler, the Julia variable used should either
380
380
381
381
### Querying Probabilities from Model or Chain
382
382
383
+
Consider first the following simplified `gdemo` model:
384
+
```julia
385
+
@modelfunctiongdemo0(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
+
```
383
396
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:
385
412
```julia
386
413
@modelfunctiongdemo(x, y)
387
414
s² ~InverseGamma(2, 3)
388
415
m ~Normal(0, sqrt(s²))
389
416
x ~Normal(m, sqrt(s²))
390
417
y ~Normal(m, sqrt(s²))
391
418
end
419
+
420
+
# Instantiate the model.
421
+
model =gdemo(2.0, 4.0)
392
422
```
393
423
394
424
The following are examples of valid queries of the `Turing` model or chain:
395
425
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`.
397
427
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.
399
429
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`.
401
431
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.
403
433
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`.
405
435
406
436
- 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"`.
0 commit comments