Turing Guide documentation: prob"..." examples throws errors

I’m new to Turing.jl (and quite new to Julia as well) and reading the Guide documentation ( Guide ), section “Querying Probabilities from Model or Chain”. The probability query examples are of the form

prob"x = 1.0, y = 1.0 | model = gdemo, s = 1.0, m = 1.0"

Am I correct to assume that this code should work verbatim, or should I change/transform them in some way?

The reason I ask is that the following program throws an error:

@model function gdemo(x, y)
   s ~ InverseGamma(2, 3)
   m ~ Normal(0, sqrt(s))
   x ~ Normal(m, sqrt(s))
   y ~ Normal(m, sqrt(s))
end

model = gdemo(2,4)
chains = sample(model, HMC(0.01, 5), 10000)

prob"x=1.0, y=1.0 | model=gdemo, s = 1.0, m = 1.0"

The error is:
“”"
ERROR: LoadError: AssertionError: model isa Model
Stacktrace:
[1] probtype(::NamedTuple{(:x, :y),Tuple{Float64,Float64}}, ::NamedTuple{(:model, :s, :m),Tuple{typeof(gdemo),Float64,Float64}}) at /home/hakank/.julia/packages/DynamicPPL/jOFDR/src/prob_macro.jl:63
[2] logprob(::NamedTuple{(:x, :y),Tuple{Float64,Float64}}, ::NamedTuple{(:model, :s, :m),Tuple{typeof(gdemo),Float64,Float64}}) at /home/hakank/.julia/packages/DynamicPPL/jOFDR/src/prob_macro.jl:25
[3] top-level scope at /home/hakank/julia/turing/turing_tutorial12.jl:35
[4] include(::String) at ./client.jl:457
[5] top-level scope at ./timing.jl:174 [inlined]
[6] top-level scope at ./none:0
in expression starting at /home/hakank/julia/turing/turing_tutorial12.jl:35
“”"

Here’s my versioninfo():
Julia Version 1.5.2
Commit 539f3ce943 (2020-09-23 23:17 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core™ i9-7940X CPU @ 3.10GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-9.0.1 (ORCJIT, skylake-avx512)
Environment:
JULIA_DEPOT_PATH = /home/hakank/.julia
JULIA_NUM_THREADS = 14
JULIA_PKG_SERVER = pkg.juliahub.com
JULIA_LOAD_PATH = @:@v#.#:@stdlib

I’m running via JuliaPro_v1.5.2-1 (via Atom) and Turing version v0.14.10. The same error is thrown using Julia version 1.5.3.

Best regards,

Hakan

1 Like

In this case, you want to use the query string

prob"x=1.0, y=1.0 | model=model, s = 1.0, m = 1.0"

because model is the name of the instantiated model with the data 2 and 4. gdemo isn’t actually a “model” yet, because it hasn’t been given data – model has both the model definition and the data.

2 Likes

@cpfiffer Excellent! Thanks, it works now.

This really ought to be explained in the guide doc, not just here.

I’m still confused about it though. The line model = gdemo(2,4) seems to give values of 2 and 4 to x and y, whereas the prob example gives them values 1.0 and 1.0. Which ones are used, and why do they need to be provided twice?

I also found it confusing: I was expecting the example to run, since it is given this way in the documentation, and it was not clear to me either which x and y were used. Anyway, the ones used are the ones given in the prob line; the following simplified example shows this:

@model function gdemo0(x)
    s ~ InverseGamma(2, 3)
    m ~ Normal(0, sqrt(s))
    x ~ Normal(m, sqrt(s))
end

model1 = gdemo0(1)
model4 = gdemo0(4)
model10 = gdemo0(10)

prob"x = 1.0 | model = model1, s = 1.0, m = 1.0"
prob"x = 1.0 | model = model4, s = 1.0, m = 1.0"
prob"x = 1.0 | model = model10, s = 1.0, m = 1.0"

## Compare to
pdf(Normal(1.0, 1.0), 1.0)

I can try to submit a (my first) PR to the docs, if appropriate.