RxInfer.jl beginner question re; UndefRefError

Hi all, I just started with RxInfer.jl for simulations of patient recruitment in clinical trials. The simplest model is a simple Poisson-Gamma model, where each clinical site draws the (time homogeneous) rate for the Poisson process describing patient recruitment from a Gamma distribution. I am trying to implement in RxInfer but getting ERROR: UndefRefError: access to undefined reference. I am sure this is some simple error on my part but I cannot find it. Appreciate any help.

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, )
)
2 Likes

Hey! Sorry I don’t have a definite solution but could this have something do with the conjugacy stuff? I understand that RxInfer requires it by default and if you have a non-conjugate model you need to take some extra steps (?). (Coming from medicine myself, cool application by the way!)

1 Like

Thanks for the idea, but I don’t think so. I encountered the non-conjugate error before and it was different, I’m very puzzled by the UndefRefError

Usually it helps to post the complete error message, including the stack trace.

1 Like

no problem.

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.
1 Like

I can’t quite figure out what’s happening. Seems like a malformed Vector value, with uninitialized fields, is constructed somehow, then trying to index into it throws UndefRefError. Either there’s a Julia bug, or (more likely, I guess) a bug in one of the packages here.

Try reporting a bug to RxInfer.jl, perhaps the maintainers figure it out.

1 Like

I opened an issue on GitHub: `UndefVarError` in simple Poisson-Gamma model · Issue #439 · ReactiveBayes/RxInfer.jl · GitHub

Thanks for your time in looking at it.

2 Likes