# How to mimick a Discrete Bayes Net in Turing.jl?

**URL:** https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319
**Category:** Probabilistic Programming
**Tags:** question, turing, bayesian-inference
**Created:** [June 29, 2025, 9:25pm UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319 "2025-06-29T21:25:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![nicolasg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicolasg/32/217493_2.png) [@nicolasg](https://discourse.julialang.org/u/nicolasg)
#### Post date: [June 29, 2025, 9:25pm UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319/1 "2025-06-29T21:25:48Z")

</div>

Hi,

I’ve been using Turing.jl for various purposes, and recently I thought it might be easy to mimick inference from a basic discrete bayesian network like I could do with BayesNets.jl like this for example:

```julia
bn = DiscreteBayesNet()
push!(
    bn, 
    DiscreteCPD(
        :a, # our root node
        [0.6, 0.4], # prior
    )
)
push!(
    bn,
    DiscreteCPD(
        :b,
        [:a], # :a is :b's parent node
        [2],
        [
            Categorical([0.9, 0.1]),
            Categorical([0.1, 0.9]),
        ],
    )
)

infer_df = DataFrame(infer(
    bn,
    :a,
    evidence = Assignment(
        :b => 1, # observing a specific state of :b, infer :a's new distribution
    ),
))

```

Which gives updated probabilities for `:a`:

```julia
2×2 DataFrame
 Row │ a potential 
     │ Int64 Float64   
─────┼──────────────────
   1 │ 1 0.931034
   2 │ 2 0.0689655

```

I’ve been toying around with a Turing model for a while now, trying to combine Dirichlet and Categorical distributions etc, but I can’t seem to figure out a way to infer the correct posterior probabilities from some evidence (here, a single observation of `:b`). I’ve been able to get the probabilities to go in the correct direction, but not to approximate the exact inference result through sampling with Turing.

I guess I’m going at it wrong, probably. I’m not an expert with probabilistic programming. On the other hand, Turing seems so flexible that I guess it should be possible, though I’m not sure. Would you have an example of how to do something like this in Turing?

---

<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: [July 1, 2025, 7:51am UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319/2 "2025-07-01T07:51:20Z")

</div>

Welcome to the forum! This is an interesting question, I’ve been thinking about a similar thing. Could you share your attempts?

---

<div class="post-metadata">

### Author: ![Red-Portal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/red-portal/32/9102_2.png) [@Red-Portal](https://discourse.julialang.org/u/Red-Portal)
#### Post date: [September 21, 2025, 5:00pm UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319/3 "2025-09-21T17:00:41Z")

</div>

Most contemporary PPLs are developed with continuous variables in mind. Therefore, for pure Bayesian networks, it’s best to use frameworks specialized to those.

---

<div class="post-metadata">

### Author: ![PedroPizarro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pedropizarro/32/216074_2.png) [@PedroPizarro](https://discourse.julialang.org/u/PedroPizarro)
#### Post date: [September 22, 2025, 8:09pm UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319/4 "2025-09-22T20:09:28Z")

</div>

Are there any Discrete Bayesian network frameworks in Julia?

---

<div class="post-metadata">

### Author: ![Red-Portal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/red-portal/32/9102_2.png) [@Red-Portal](https://discourse.julialang.org/u/Red-Portal)
#### Post date: [September 22, 2025, 8:44pm UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319/5 "2025-09-22T20:44:49Z")

</div>

I think so? A quick google search revaled [`BayesNets.jl`](https://github.com/sisl/BayesNets.jl?tab=readme-ov-file). I am sure there is more, but I am more of a general PPL man myself, so not much experience with Bayesian networks.

---

<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: [September 23, 2025, 1:16pm UTC](https://discourse.julialang.org/t/how-to-mimick-a-discrete-bayes-net-in-turing-jl/130319/6 "2025-09-23T13:16:40Z")

</div>

Also see the RxInfer.jl tutorial on [Bayesian networks](https://examples.rxinfer.com/categories/basic_examples/bayesian_networks/).
