# RxInfer Domain Logic

**URL:** https://discourse.julialang.org/t/rxinfer-domain-logic/117355
**Category:** Graphs
**Tags:** question, bayesian-inference, graphs, rxinfer
**Created:** [July 22, 2024, 6:04pm UTC](https://discourse.julialang.org/t/rxinfer-domain-logic/117355 "2024-07-22T18:04:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![spolk](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@spolk](https://discourse.julialang.org/u/spolk)
#### Post date: [July 22, 2024, 6:04pm UTC](https://discourse.julialang.org/t/rxinfer-domain-logic/117355/1 "2024-07-22T18:04:04Z")

</div>

Hello!

I am trying to modify [this example](https://reactivebayes.github.io/RxInfer.jl/stable/examples/advanced_examples/Assessing%20People%20Skills/) from RxInfer.jl’s documentation and get an error saying I didn’t initialize marginals when two unobserved states influence multiple observable results in the domain logic in a Bayesian network.

Specifically, consider the following MWE that was derived from the example linked above.

```julia
using RxInfer, Random

# Create Score node
struct Score end
@node Score Stochastic [out, in]

# Adding update rule for the Score node
@rule Score(:in, Marginalisation) (q_out::PointMass,) = begin
    return Bernoulli(mean(q_out))
end
 
@model function model_a(r)

    local s
    # Priors
    s[1] ~ Bernoulli(0.5)
    s[2] ~ Bernoulli(0.5)

    # Domain logic and results
    r[1] ~ Score(s[1]) 
    r[2] ~ Score(s[1] || s[2]) 
end

test_results = [0, 1]
inference_result = infer(
    model=model_a(),
    data=(r=test_results,)
)
results = map(params, inference_result.posteriors[:s])

```

This results in no error whatsoever and yields the result `s = [0,1]`, as desired. However, consider the following model, in which I add slightly more complex logic to the Bayesian network:

```julia
# Same code as above outside of the model adjustment below: 
@model function model_b(r)

    local s
    # Priors
    s[1] ~ Bernoulli(0.5)
    s[2] ~ Bernoulli(0.5)

    # Domain logic and results
    r[1] ~ Score(s[1] && s[2]) # There is an additional dependence from s[1] & s[2] to r[1] now. 
    r[2] ~ Score(s[1] || s[2]) 
end

# Run inference with slightly more complicated model
 inference_result = infer(
           model=model_b(),
           data=(r=test_results,)
       )

```

When I try to infer the states `s` from this model, I get the following error indicating that my marginals have not been defined and hence `s` cannot be updated by RxInfer.jl.

```julia
1-element ExceptionStack:
Variables [s] have not been updated after an update event.
Therefore, make sure to initialize all required marginals and messages. See `initialization` keyword argument for the inference function.
See the official documentation for detailed information regarding the initialization.

Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] check_and_reset_updated!(updates::Dict{Symbol, RxInfer.MarginalHasBeenUpdated})
   @ RxInfer C:\Users\SA30308\.julia\packages\RxInfer\wbFg1\src\inference\inference.jl:79
 [3] batch_inference(; model::GraphPPL.ModelGenerator{typeof(model_b), @Kwargs{}, GraphPPL.PluginsCollection{Tuple{}}, RxInfer.ReactiveMPGraphPPLBackend}, data::@NamedTuple{r::Vector{Int64}}, initialization::Nothing, constraints::Nothing, meta::Nothing, options::Nothing, returnvars::Nothing, predictvars::Nothing, iterations::Nothing, free_energy::Bool, free_energy_diagnostics::Tuple{RxInfer.ObjectiveDiagnosticCheckNaNs, RxInfer.ObjectiveDiagnosticCheckInfs}, showprogress::Bool, callbacks::Nothing, addons::Nothing, postprocess::DefaultPostprocess, warn::Bool, catch_exception::Bool)
   @ RxInfer C:\Users\SA30308\.julia\packages\RxInfer\wbFg1\src\inference\batch.jl:306
 [4] batch_inference
   @ RxInfer C:\Users\SA30308\.julia\packages\RxInfer\wbFg1\src\inference\batch.jl:94 [inlined]
 [5] #infer#242
   @ RxInfer C:\Users\SA30308\.julia\packages\RxInfer\wbFg1\src\inference\inference.jl:306 [inlined]
 [6] top-level scope
   @ REPL[11]:1

```

If I set my marginals for `s` correctly for `model_a`, shouldn’t `model_b` work as well? What am I missing here? And what are the rules for modifying domain logic in RxInfer.jl that will avoid running into this sort of issue in the future?

Thank you very much for your time and help.

---

<div class="post-metadata">

### Author: ![timn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timn/32/211595_2.png) [@timn](https://discourse.julialang.org/u/timn)
#### Post date: [August 9, 2024, 11:16am UTC](https://discourse.julialang.org/t/rxinfer-domain-logic/117355/2 "2024-08-09T11:16:01Z")

</div>

Hi @spolk !

The error message “Variables […] have not been updated after an update event” does indicate that your model B has a loop as described in the [documentation](https://reactivebayes.github.io/RxInfer.jl/stable/manuals/inference/initialization/).

For your case, I drew both factor graphs (will add model A in a subsequent post):

 ![model_b](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f5c2ab5964aa4e55e77350190934f9f4bb9f5ec.jpeg)

You can see that model B has a loop from s\_1 to itself (from the s\_1 equality node, the OR node, and then over the s\_2 equality node, to the AND node, back to the s\_1 equality node), as well as from s\_2 to itself.

To resolve this circular dependency / loop, you need to tell RxInfer how to break the loop to update the variables by initializing the messages like so

```julia
init = @initialization begin           
       μ(s) = Bernoulli(0.5)                                                                 
end

[...]

inference_result = infer(
  model = model_b(),
  data = (r=test_results,),
  initialization = init,
  [...]
)

```

I hope that helps 🙂

---

<div class="post-metadata">

### Author: ![timn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timn/32/211595_2.png) [@timn](https://discourse.julialang.org/u/timn)
#### Post date: [August 9, 2024, 11:16am UTC](https://discourse.julialang.org/t/rxinfer-domain-logic/117355/3 "2024-08-09T11:16:31Z")

</div>

Factor graph for model A for comparison:

 ![model_a](https://global.discourse-cdn.com/julialang/original/3X/e/a/ea71f686cf38d2e8f4ecbb6c48248e1e4718506e.jpeg)

(As a new user, I am only allowed to add one media per post)

---

<div class="post-metadata">

### Author: ![spolk](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@spolk](https://discourse.julialang.org/u/spolk)
#### Post date: [August 27, 2024, 12:46pm UTC](https://discourse.julialang.org/t/rxinfer-domain-logic/117355/4 "2024-08-27T12:46:45Z")

</div>

Thank you!
