Turing.jl icon indicating copy to clipboard operation
Turing.jl copied to clipboard

Strange behavior for transformed data.

Open itsdfish opened this issue 5 years ago • 6 comments

An error was reported in this Discourse forum.

If heads ~ Binomial(length(y), p) is replaced with 2 ~ Binomial(length(y), p), the example below works. It seems like the problem is that heads is not treated like data.

using Turing, Random

@model function coinflip_binomial_broken(y::Array{Int64})
    # prior on p
    p ~ Beta(1, 1)
    
    # updates on p
    heads = sum(y)
    heads ~ Binomial(length(y), p)
end

Random.seed!(5484)
y = [1,0,0,1]
chain = sample(coinflip_binomial_broken(y), NUTS(1000,.65), 2000)

Output:

Summary Statistics
  parameters      mean       std   naive_se      mcse       ess      rhat 
      Symbol   Float64   Float64    Float64   Float64   Float64   Float64 

       heads    2.0000    0.0000     0.0000    0.0000       NaN       NaN
           p    0.2672    0.0000     0.0000    0.0000    7.1985    1.0187

Quantiles
  parameters      2.5%     25.0%     50.0%     75.0%     97.5% 
      Symbol   Float64   Float64   Float64   Float64   Float64 

       heads    2.0000    2.0000    2.0000    2.0000    2.0000
           p    0.2672    0.2672    0.2672    0.2672    0.2672

Version: Julia 1.5.3

(@v1.5) pkg> st Turing
Status `~/.julia/environments/v1.5/Project.toml`
  [fce5fe82] Turing v0.15.8

itsdfish avatar Dec 31 '20 15:12 itsdfish

This is a design choice (see https://github.com/TuringLang/DynamicPPL.jl/blob/1244b98b1b95d8f0f54bde3a08c161b0456556ae/src/compiler.jl#L223 and https://github.com/TuringLang/DynamicPPL.jl/blob/1244b98b1b95d8f0f54bde3a08c161b0456556ae/src/compiler.jl#L246): observations, i.e., variables on the left hand side of ~ that should not be sampled, have to be passed in as arguments to the model or be literals. I.e., the model has to be written as

@model function coinflip_binomial(heads::Int, n::Int)
    # prior on p
    p ~ Beta(1, 1)
    
    # updates on p
    heads ~ Binomial(n, p)
end

instead. For convenience, you can define

coinflip_binomial(y::Vector{Int}) = coinflip_binomial(sum(y), length(y))

I am wondering if the design should be changed and if the model in the OP should work as well. Mostly, I am wondering if and how existing models would break if one would not check if the LHS is an argument to the model but e.g. would only check if it exists (i.e., something like @isdefined heads and isassigned(...) for arrays?) or is missing.

devmotion avatar Dec 31 '20 16:12 devmotion

Thanks for the explanation. One argument against changing the current setup is that it is more efficient to process data outside of the model function. I'm trying to think whether there is a good reason to allow data processing inside the model function, but I cannot think of one.

itsdfish avatar Dec 31 '20 16:12 itsdfish

Thanks for clarifying. I wrote the original post on the discourse. The purpose behind writing this way was for edification: I was writing a series of simple models to get myself familiar with Turing and Julia. This model was a simple modification of a previous model that worked using a Bernoulli variable instead (so it was looping over the argument y instead).

If I understand the limitation here, this is implicitly somewhat like stan's transformed data which is evaluated only once before the sampling steps of the parameters and model blocks. Since @model has no distinct "blocks", the end user should do the transformations beforehand. Is that about right?

bwgroff avatar Dec 31 '20 19:12 bwgroff

I believe that is correct. You can verify by putting a print statement in the model definition. It will print many times.

itsdfish avatar Dec 31 '20 23:12 itsdfish

Yes, this is right. The @model macro does not simplify the model and therefore every model execution will perform the data transformations again, which slows down inference and is probably not what you want. However, since the @model macro just defines a regular Julia function, you can move the transformations to a separate function, possibly using multiple dispatch as in my example above. In this case, the transformation is only performed once. You can check this by e.g. putting print statements in the function that transforms the data.

devmotion avatar Jan 02 '21 00:01 devmotion

Closing now since it seems the question has been answered.

yebai avatar Jan 27 '21 17:01 yebai