# Are there any libraries for discrete probability tables in order to do these type

**URL:** <https://discourse.julialang.org/t/are-there-any-libraries-for-discrete-probability-tables-in-order-to-do-these-type/54990>\
**Category:** General Usage\
**Created:** [February 10, 2021, 1:13pm UTC](https://discourse.julialang.org/t/are-there-any-libraries-for-discrete-probability-tables-in-order-to-do-these-type/54990 "2021-02-10T13:13:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 10, 2021, 1:13pm UTC](https://discourse.julialang.org/t/are-there-any-libraries-for-discrete-probability-tables-in-order-to-do-these-type/54990/1 "2021-02-10T13:13:59Z")

</div>

Are there any libraries for discrete probability tables in order to do these type of operations directly (see images)?

I’m looking for having discrete probability tables with these operations defined on them. I also want them to be sparse (see last image). I.e., I want zero probabilities (or sometimes even an arbitrary common-to-many-states probability) to have no “entry”/ take up no memory.

Is there anything that already fit these requirements? Or are there perhaps some legwork that I can pick up and enhance?

 ![image (0)](https://global.discourse-cdn.com/julialang/original/3X/8/6/8664ecc7c5e33bd4aeeaaeab8b90fc1580d00d93.png) ![image (1)](https://global.discourse-cdn.com/julialang/original/3X/c/9/c9a295a7953c7e7992d2b904a671bdca1cd2158c.png) ![image (2)](https://global.discourse-cdn.com/julialang/original/3X/d/e/de62243849749edaea5fb10409b7a1cbb9751415.png) ![image (3)](https://global.discourse-cdn.com/julialang/original/3X/c/8/c809f9eb6dc29bf9133841d2b139b0c7306173a5.png) ![image (4)](https://global.discourse-cdn.com/julialang/original/3X/3/2/321aa4a6f4a57fe4493bd825d5eaf8fbdaf27b85.png) ![image (5)](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a1d85fa83c21525aad0d32cfdea044e745a9807.png) ![image (6)](https://global.discourse-cdn.com/julialang/original/3X/d/e/de21fe39ed980542a28c3603355ec1301c9773b9.png)

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1612961520462300?thread_ts=1612961520.462300&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![heetbeet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heetbeet/32/20958_2.png) [@heetbeet](https://discourse.julialang.org/u/heetbeet)\
**Post date:** [February 11, 2021, 10:38am UTC](https://discourse.julialang.org/t/are-there-any-libraries-for-discrete-probability-tables-in-order-to-do-these-type/54990/2 "2021-02-11T10:38:34Z")

</div>

![](https://global.discourse-cdn.com/julialang/original/3X/9/0/9095247c0ae2833e76d3287797c8b8e086bab178.png)

[Boston Gelwan](https://app.slack.com/team/U019K6Q9N15) [18 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612976332477600?thread_ts=1612961520.462300&cid=C6A044SQH)

`Distributions.jl` has discrete distributions, but these operations are best represented by linear algebra. `LinearAlgebra` should suffice entirely. (edited)

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

[Heetbeet](https://app.slack.com/team/U018FJVBXPD) [16 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612981146480400?thread_ts=1612961520.462300&cid=C6A044SQH)

So I was thinking the same some years ago and got frustrated with

- matching/reshuffling dimensions to fit the “variable” labels. E.g. p(x,y,z) == p(y,x,z), and p(x,y) may be multiplied with p(y,z) with only one variable in the overlapping scope.
- I wanted a sparse implementation, and I’m not sure how the memory mapping reshaping arrays worked back in the day in Python.

In C++ I have an object that with the main data structure similar to `Dict{Vector{Int}, Any}` to represent this: (edited)

image (6).png

[  
  ![image (6).png](https://files.slack.com/files-tmb/T68168MUP-F01NDGPQNGY-e3af210ad5/image __6__ 360.png)  
 ](https://files.slack.com/files-pri/T68168MUP-F01NDGPQNGY/image__6_.png)

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

[Heetbeet](https://app.slack.com/team/U018FJVBXPD) [16 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612981457480600?thread_ts=1612961520.462300&cid=C6A044SQH)

Along with some other helper data such as the A,B,C,D labels. These are wrapped in a object `DiscreteTable` which is a subclass of `Factor` Those operations were then defined on `DiscreteTable` and allowed `DiscreteTable` to operate in other algorithms designed for `Factor` classes. But doing anything in that library is such a hassle and a big mess. I’m busy writing up my thesis, and I was wondering if it’s not better to just get the minimal stuff I did working in Julia and then be able to write my experiments much faster.

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

[Heetbeet](https://app.slack.com/team/U018FJVBXPD) [16 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612981631480900?thread_ts=1612961520.462300&cid=C6A044SQH)

So I can obviously go with the same structure as C++ in Julia, but I have the same hunch as you [@extradosages](https://julialang.slack.com/team/U019K6Q9N15):  
LinearAlgebra should be able to reach this problem. I just don’t have any idea how to do the dimension-\>variable mapping, and have everything sparse. (edited)

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

[Heetbeet](https://app.slack.com/team/U018FJVBXPD) [16 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612982150481200?thread_ts=1612961520.462300&cid=C6A044SQH)

Hmm, this might help me:  
[Implementing an equivalent of python's xarray (labeled nd-arrays) in julia](https://discourse.julialang.org/t/implementing-an-equivalent-of-pythons-xarray-labeled-nd-arrays-in-julia/1741) (edited)

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

[Heetbeet](https://app.slack.com/team/U018FJVBXPD) [16 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612982937482500?thread_ts=1612961520.462300&cid=C6A044SQH)

I might be wrong, but it seems like [GitHub - JuliaArrays/AxisArrays.jl: Performant arrays where each dimension can have a named axis with values](https://github.com/JuliaArrays/AxisArrays.jl) annotates the “indexes”, i.e. for a 2x2 array, each row can get a name and each column can get a name. But what I would need is to let each dimension have a name, like :row and :column and not each item within that dimension.

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

[Heetbeet](https://app.slack.com/team/U018FJVBXPD) [16 hours ago](https://julialang.slack.com/archives/C6A044SQH/p1612983026482700?thread_ts=1612961520.462300&cid=C6A044SQH)

This looks exactly like what I want:

> **[GitHub - davidavdav/NamedArrays.jl: Julia type that implements a drop-in...](https://github.com/davidavdav/NamedArrays.jl)**
>
> Julia type that implements a drop-in replacement of Array with named dimensions - GitHub - davidavdav/NamedArrays.jl: Julia type that implements a drop-in replacement of Array with named dimensions

---

<div class="post-metadata">

**Author:** ![heetbeet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heetbeet/32/20958_2.png) [@heetbeet](https://discourse.julialang.org/u/heetbeet)\
**Post date:** [March 31, 2021, 2:53pm UTC](https://discourse.julialang.org/t/are-there-any-libraries-for-discrete-probability-tables-in-order-to-do-these-type/54990/3 "2021-03-31T14:53:44Z")

</div>

Related: [Can NamedDims.jl make use of sparse arrays?](https://discourse.julialang.org/t/can-nameddims-jl-make-use-of-sparse-arrays/55043)
