-
Notifications
You must be signed in to change notification settings - Fork 34
Closed
Description
Hi RxInfer team. I'm reporting a UndefVarError bug here that I found as a result of the MWE I posted in this question on the Julia Discourse (https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660), where I was encouraged to report this to the team.
I am on MacOS, Julia v1.11.3, and software versions:
[31c24e10] Distributions v0.25.117
[86711068] RxInfer v4.2.0
Here is the MWE:
using Distributions
"""
Follows 3.2.1 in the paper. Simulate data from a clinical trial enrollment process.
Note β is a rate parameter, but Distributions uses scale; we take 1/β in the function.
Note that the paper considers the "time step" to be a day.
"""
function pg_homogeneous(α, β, u, T)
N = length(α)
@assert N == length(β) == length(u)
λ = rand.(Gamma.(α, 1 ./ β)) # shape, rate parameterization
outmat = zeros(Int, N, T)
for i in 1:N
rate = [k < u[i] ? 0 : λ[i] for k in 1:T]
outmat[i, :] = rand.(Poisson.(rate))
end
return cumsum(outmat, dims=2) # return cumulative number of pts
end
# example with time-homogeneous rates
α = 1/(1.2^2)
β = α/0.02
N = 200
T = 400
u = rand(1:29*4, N)
recruitment_target = 1000
out_homogeneous = pg_homogeneous(fill(α, N), fill(β, N), u, T)
final_date = findfirst(sum(out_homogeneous, dims=1) .>= recruitment_target)[2]
out_homogeneous = out_homogeneous[:, 1:final_date]
using RxInfer
# The inference model for the homogeneous PG model.
# This assumes that Gamma(α,β) is the common distribution of the
# rate for all centers (the simulation function in the paper generalizes that somewhat
# but all the math in the paper makes this simplifying assumption).
@model function pg_homogeneous_rxinfer(data, T, N, u)
# these are quite terrible priors for the Gamma(α,β)
# parameters, but the actual conjugate prior is quite a strange non-standard
# distribution (see Gamma entry in https://en.wikipedia.org/wiki/Conjugate_prior#When_likelihood_function_is_a_continuous_distribution)
# so let's use these for testing
α ~ Uniform(0, 10)
β ~ Uniform(1, 150)
# the main stochastic model
for i in 1:N
λ[i] ~ Gamma(shape=α, scale=β) # shape, scale parameterization
for k in 1:T
if k ≥ u[i] # recruitment has started
data[i, k] ~ Poisson(λ[i]) # Poisson draw at each day
end
end
end
end
infer_homogeneous = infer(
model = pg_homogeneous_rxinfer(T=final_date, N=N, u=u),
data = (data = out_homogeneous, )
)And the stack trace:
ERROR: UndefRefError: access to undefined reference
Stacktrace:
[1] getindex
@ ./essentials.jl:917 [inlined]
[2] recursive_getindex
@ ~/.julia/packages/GraphPPL/xPNyo/src/resizable_array.jl:144 [inlined]
[3] recursive_getindex
@ ~/.julia/packages/GraphPPL/xPNyo/src/resizable_array.jl:152 [inlined]
[4] getindex
@ ~/.julia/packages/GraphPPL/xPNyo/src/resizable_array.jl:136 [inlined]
[5] iterate(array::GraphPPL.ResizableArray{GraphPPL.NodeLabel, Vector{Vector{GraphPPL.NodeLabel}}, 2})
@ GraphPPL ~/.julia/packages/GraphPPL/xPNyo/src/resizable_array.jl:180
[6] iterate
@ ./generator.jl:45 [inlined]
[7] _collect(c::GraphPPL.ResizableArray{…}, itr::Base.Generator{…}, ::Base.EltypeUnknown, isz::Base.HasShape{…})
@ Base ./array.jl:811
[8] collect_similar(cont::GraphPPL.ResizableArray{…}, itr::Base.Generator{…})
@ Base ./array.jl:720
[9] map(f::Function, A::GraphPPL.ResizableArray{GraphPPL.NodeLabel, Vector{Vector{GraphPPL.NodeLabel}}, 2})
@ Base ./abstractarray.jl:3371
[10] getvarref(model::GraphPPL.Model{…}, container::GraphPPL.ResizableArray{…})
@ RxInfer ~/.julia/packages/RxInfer/2Syro/src/model/plugins/reactivemp_inference.jl:265
[11] (::RxInfer.var"#73#74"{GraphPPL.Model{…}})(v::GraphPPL.ResizableArray{GraphPPL.NodeLabel, Vector{…}, 2})
@ RxInfer ~/.julia/packages/RxInfer/2Syro/src/model/plugins/reactivemp_inference.jl:261
[12] map!(f::RxInfer.var"#73#74"{…}, out::Dictionaries.UnorderedDictionary{…}, d::Dictionaries.UnorderedDictionary{…})
@ Dictionaries ~/.julia/packages/Dictionaries/tq7Jt/src/map.jl:57
[13] map(f::Function, d::Dictionaries.UnorderedDictionary{Symbol, Any})
@ Dictionaries ~/.julia/packages/Dictionaries/tq7Jt/src/map.jl:92
[14] map(f::Function, vardict::GraphPPL.VarDict{Any})
@ GraphPPL ~/.julia/packages/GraphPPL/xPNyo/src/graph_engine.jl:615
[15] getvardict
@ ~/.julia/packages/RxInfer/2Syro/src/model/plugins/reactivemp_inference.jl:261 [inlined]
[16] getvardict
@ ~/.julia/packages/RxInfer/2Syro/src/model/model.jl:40 [inlined]
[17] batch_inference(; model::GraphPPL.ModelGenerator{…}, data::@NamedTuple{…}, initialization::Nothing, constraints::Nothing, meta::Nothing, options::Nothing, returnvars::Nothing, predictvars::Nothing, iterations::Nothing, free_energy::Bool, free_energy_diagnostics::Tuple{…}, allow_node_contraction::Bool, showprogress::Bool, callbacks::Nothing, addons::Nothing, postprocess::DefaultPostprocess, warn::Bool, catch_exception::Bool)
@ RxInfer ~/.julia/packages/RxInfer/2Syro/src/inference/batch.jl:205
[18] batch_inference
@ ~/.julia/packages/RxInfer/2Syro/src/inference/batch.jl:96 [inlined]
[19] #288
@ ~/.julia/packages/RxInfer/2Syro/src/inference/inference.jl:532 [inlined]
[20] with_session(f::RxInfer.var"#288#290"{…}, session::RxInfer.Session, label::Symbol)
@ RxInfer ~/.julia/packages/RxInfer/2Syro/src/session.jl:253
[21] #infer#287
@ ~/.julia/packages/RxInfer/2Syro/src/inference/inference.jl:502 [inlined]
[22] top-level scope
@ ~/Desktop/misc/tmp.jl:58
Some type information was truncated. Use `show(err)` to see complete types.