# RxInfer.jl beginner question re; UndefRefError

**URL:** <https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660>\
**Category:** Probabilistic Programming\
**Tags:** rxinfer\
**Created:** [March 7, 2025, 1:49am UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660 "2025-03-07T01:49:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)\
**Post date:** [March 7, 2025, 1:49am UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/1 "2025-03-07T01:49:59Z")

</div>

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.

```julia
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, )
)

```

---

<div class="post-metadata">

**Author:** ![eteppo](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@eteppo](https://discourse.julialang.org/u/eteppo)\
**Post date:** [March 9, 2025, 2:16pm UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/2 "2025-03-09T14:16:31Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)\
**Post date:** [March 9, 2025, 3:34pm UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/3 "2025-03-09T15:34:38Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 9, 2025, 4:06pm UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/4 "2025-03-09T16:06:53Z")

</div>

> [@slwu89](#):
>
> getting `ERROR: UndefRefError: access to undefined reference`.

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

---

<div class="post-metadata">

**Author:** ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)\
**Post date:** [March 9, 2025, 4:18pm UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/5 "2025-03-09T16:18:05Z")

</div>

no problem.

```julia
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.

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 9, 2025, 7:27pm UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/6 "2025-03-09T19:27:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slwu89](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slwu89/32/217323_2.png) [@slwu89](https://discourse.julialang.org/u/slwu89)\
**Post date:** [March 9, 2025, 8:29pm UTC](https://discourse.julialang.org/t/rxinfer-jl-beginner-question-re-undefreferror/126660/7 "2025-03-09T20:29:21Z")

</div>

I opened an issue on GitHub: [`UndefVarError` in simple Poisson-Gamma model · Issue #439 · ReactiveBayes/RxInfer.jl · GitHub](https://github.com/ReactiveBayes/RxInfer.jl/issues/439)

Thanks for your time in looking at it.
