Skip to content

Scoping for nested models #245

@cscherrer

Description

@cscherrer

This works just fine:

julia> m1 = @model a, b begin
                  p ~ Beta(a, b)
                  x ~ Normal(p, 1.0) |> iid(3)
              end;

julia> m2 = @model begin
                  a ~ Beta(0.5, 0.5)
                  b ~ Beta(1, 0.5)
                  m ~ m1(a = a, b = b)
              end;

julia> t = xform(m2() | (; m = (; x = rand(3))))
TransformVariables.TransformTuple{NamedTuple{(:b, :a, :m), Tuple{TransformVariables.ScaledShiftedLogistic{Float64}, TransformVariables.ScaledShiftedLogistic{Float64}, TransformVariables.TransformTuple{NamedTuple{(:p,), Tuple{TransformVariables.ScaledShiftedLogistic{Float64}}}}}}}((b = as𝕀, a = as𝕀, m = TransformVariables.TransformTuple{NamedTuple{(:p,), Tuple{TransformVariables.ScaledShiftedLogistic{Float64}}}}((p = as𝕀,), 1)), 3)

julia> logdensity(m2() | (; m = (; x = rand(3))), t(randn(3)))
-1.3902285607281852

But in tests I had to do

    @testset "Nested models" begin
        m1 = @model a, b begin
            p ~ Beta(a, b)
            x ~ Normal(p, 1.0) |> iid(3)
        end

        m2 = @model begin
            a ~ Beta(0.5, 0.5)
            b ~ Beta(1, 0.5)
            m ~ m1(a = a, b = b)
        end

        @test_broken let t = xform(m2() | (; m = (; x = rand(3))))
            logdensity(m2() | (; m = (; x = rand(3))), t(randn(3))) isa Float64
        end
    end

Without this, the xform call fails with

Nested models: Error During Test at REPL[24]:1
  Got exception outside of a @test
  UndefVarError: α not defined
  Stacktrace:
    [1] getproperty
      @ ./Base.jl:26 [inlined]
    [2] macro expansion
      @ ~/.julia/packages/GeneralizedGenerated/hIoV7/src/closure_conv.jl:121 [inlined]
    [3] _xform(#unused#::Type{TypeEncoding(Main)}, _m::Model{NamedTuple{(:n, :α, :β), T} where T<:Tuple, TypeEncoding(begin
      p ~ Beta(α, β)
      x ~ Binomial(n, p)
  end), TypeEncoding(Main)}, _args::NamedTuple{(:a, :b), Tuple{Float64, Float64}}, _data::NamedTuple{(:x,), Tuple{Vector{Float64}}})
      @ Soss ~/.julia/packages/GeneralizedGenerated/hIoV7/src/closure_conv.jl:121
    [4] xform(m::Soss.ConditionalModel{NamedTuple{(:n, , ), T} where T<:Tuple, TypeEncoding(begin
      p ~ Beta(α, β)
      x ~ Binomial(n, p)
  end), TypeEncoding(Main), NamedTuple{(:a, :b), Tuple{Float64, Float64}}, NamedTuple{(:x,), Tuple{Vector{Float64}}}})
      @ Soss ~/git/Soss.jl/src/primitives/xform.jl:23
    [5] xform(m::Soss.ConditionalModel{NamedTuple{(:n, , ), T} where T<:Tuple, TypeEncoding(begin
      p ~ Beta(α, β)
      x ~ Binomial(n, p)
  end), TypeEncoding(Main), NamedTuple{(:a, :b), Tuple{Float64, Float64}}, NamedTuple{(), Tuple{}}}, _data::NamedTuple{(:x,), Tuple{Vector{Float64}}})
      @ Soss ~/git/Soss.jl/src/primitives/xform.jl:20
    [6] macro expansion
      @ ~/.julia/packages/GeneralizedGenerated/hIoV7/src/closure_conv.jl:121 [inlined]
    [7] _xform(#unused#::Type{TypeEncoding(Main)}, _m::Model{NamedTuple{(), T} where T<:Tuple, TypeEncoding(begin
      b ~ Beta(1, 0.5)
      a ~ Beta(0.5, 0.5)
      m ~ m1(a = a, b = b)
  end), TypeEncoding(Main)}, _args::NamedTuple{(), Tuple{}}, _data::NamedTuple{(:m,), Tuple{NamedTuple{(:x,), Tuple{Vector{Float64}}}}})
      @ Soss ~/.julia/packages/GeneralizedGenerated/hIoV7/src/closure_conv.jl:121
    [8] xform(m::Soss.ConditionalModel{NamedTuple{(), T} where T<:Tuple, TypeEncoding(begin
      b ~ Beta(1, 0.5)
      a ~ Beta(0.5, 0.5)
      m ~ m1(a = a, b = b)
  end), TypeEncoding(Main), NamedTuple{(), Tuple{}}, NamedTuple{(:m,), Tuple{NamedTuple{(:x,), Tuple{Vector{Float64}}}}}})
      @ Soss ~/git/Soss.jl/src/primitives/xform.jl:23

It seems to be mistakenly finding this model from test/transforms.jl:

m = @model (n,α,β) begin
    p ~ Beta(α, β)
    x ~ Binomial(n, p)
    z ~ Binomial(n, α/+β))
end

@thautwarm I'm not sure what's going on here, looks like a scoping issue. Any ideas?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions