Home SymPy Supports Compound Distributions
Post
Cancel

SymPy Supports Compound Distributions

Compound distributions allow us to compose probability distributions together. In other words, it allows us to have parameters in our model that are themselves random variables. This facilities including a hierarchical structure (e.g. like in a BHM) in our models.

A few weeks ago I learned that SymPy supports compound distributions.

Here is an example. Suppose

\[X \sim \text{Normal}(2, 4)\] \[L \sim \text{Exponential}(100)\] \[N \sim \text{Normal}(X, L)\]

In SymPy we can represent this model as follows:

1
2
3
4
5
6
7
8
from sympy.stats.compound_rv import CompoundDistribution
from sympy.stats.crv_types import NormalDistribution
from sympy.stats import Exponential, Normal

X = Normal('X', 2, 4)
L = Exponential('L', 100)
N = NormalDistribution(X, L)
C = CompoundDistribution(N)

I’m looking forward to seeing more advancements in SymPy for statistics. Mathematical statistics can get particularly difficult when performed by hand. Having specialized classes with handy methods for distributions or random variables could be a huge benefit to bringing in reproducible mathematical derivations of moments (e.g. expectations) and distributions.

This post is licensed under CC BY 4.0 by the author.

Paul Grogan's DES Example and Its Implementation in Ciw

Expressing the Agnesian Equation of Order One of the Lorenz System Using SymPy