Skip to content

Forcing all LHS variables of tilde to be part of model arguments #965

@yebai

Description

@yebai

Currently, LHS variables of tilde statements can be optionally included in the model arguments. If they do appear, and non-nothing values are passed, these variables are viewed as observed.

Turing.@model function demo(y)
   x ~ Normal()
   y ~ Normal(x, 1)
end 

demo(1.) # y is data, x is parameter
demo(nothing) # both x and y are parameters
demo() # this will error

This was conceived as intuitive when I initially introduced it. However, it always felt a bit "magical" and not satisfactory to me, since the variable x is treated differently from y and involves complex code in DynamicPPL to handle it reasonably well. Even so, there is the well-known issue explained in #519. In addition, if x is an Array, it will get repeatedly re-allocated for every model evaluation, which is wasteful. These considerations motivate the following proposal:

Turing.@model function demo((;x, y)::NamedTuple, s)
   x ~ Normal()
   y ~ Normal(x, s)
end 

demo((;), 1.0) # option 1, both x and y are parameters
demo((;x=1.), 1.0) # option 2, x is observed, y is parameter
demo((;y=1.), 1.0) # option 3, y is observed, x is parameter
demo((;x=1., y=1.), 1.0) # option 4, both x and y are data
demo() # this will error
demo(1.0) # this will error since the first argument is not a namedtuple

Here, we explicitly require all LHS variables of tilde statements to be part of the model arguments. However, during the model construction step, the first argument could be a namedtuple containing a subset of LHS variables. These variables will be treated as data, similar to the current Turing syntax. If a variable is missing in the namedtuple, it can either be conditioned via the condition(...) method or left as a parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions