Skip to content

Instantly share code, notes, and snippets.

@mrocklin
Created February 18, 2013 23:48
Show Gist options
  • Save mrocklin/4981811 to your computer and use it in GitHub Desktop.
Save mrocklin/4981811 to your computer and use it in GitHub Desktop.
Example showing densities of random variables in conditional joint probability space.
from sympy.stats import *
from sympy import Tuple, Eq, simplify, Symbol, pprint
X = Normal('x', 3, 4)
Y = Normal('y', 5, 6)
Z = Normal('z', 0, 1)
# X, Y, and Z given that Z+Y == X
XX, YY, ZZ = given(Tuple(X, Y, Z), Eq(Z+Y, X))
w = Symbol('w')
pprint(simplify(density(XX)(w)))
# 2
# 53⋅w 191⋅w 36481
# - ───── + ───── - ─────
# ______ 1184 592 62752
# ╲╱ 3922 ⋅ℯ
# ─────────────────────────────────
# ___
# 296⋅╲╱ π
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment