I’d like both of the queries below to return 0.5
.
using Turing
@model function foo(w)
w ~ Categorical([0.5, 0.5])
x = w + 1
end
model = foo(missing)
println(prob"w = 2 | model = model")
println(prob"x = 3 | model = model")
I get
0.5
┌ Warning: Argument w is not defined. A value of `nothing` is used.
└ @ DynamicPPL ~/.julia/packages/DynamicPPL/WBmMU/src/prob_macro.jl:172
ERROR: MethodError: no method matching +(::Nothing, ::Int64)
What’s the right way to do this? I’d also appreciate an example using Gen.jl
.
prob"x = 3 | model = model"
This is not possible in Turing.jl, unfortuately. =
isn’t a special statement; it’s just a standard Julia assignment, so Turing.jl doesn’t actualy touch this piece of code. Only stuff on the left-hand side of ~
can be referenced in prob
.
And in general, I’d advice against using the prob
macro, but instead just make use of logjoint(model, (w = 3,))
, etc.
Thanks. Is there a way to achieve what I want? I tried
using Turing
@model function foo(w)
w ~ Categorical([0.5, 0.5])
one_ = Categorical([1.0])
x ~ w + one_
end
model = foo(missing)
@show exp(logjoint(model, (w = 2,)))
@show exp(logjoint(model, (x = 3,)))
which gives
ERROR: type NamedTuple has no field x
The x
version won’t work here either; that’s what I meant with the following part of my message:
This is not possible in Turing.jl, unfortuately. =
isn’t a special statement; it’s just a standard Julia assignment, so Turing.jl doesn’t actualy touch this piece of code.
Unfortunately that’s a limitation of Turing.jl as it requires automatic marginalization of _one
and w
.
Sorry, I had meant to avoid all =
. I had intended to write
using Turing
@model function foo(w)
w ~ Categorical([0.5, 0.5])
one_ ~ Categorical([1.0])
x ~ (w + one_)
end
model = foo(missing)
@show exp(logjoint(model, (w = 2,)))
@show exp(logjoint(model, (x = 3,)))
which still gives the same error. Anyway, it sounds like this isn’t possible with Turing.jl
. Is there an alternative that would allow this simple example to work as I would expect? I suppose I would like a system that can do symbolic reasoning where possible, and use a sampling / MC approach where necessary.
I suppose I would like a system that can do symbolic reasoning where possible, and use a sampling / MC approach where necessary.
Yeah that seems more in line with what you’re trying to do. There are other PPLs in Julia, some which might be able to do stuff like this but I’m not entirely sure unfortunately