Compute the conditional expectation with sympy

32 views Asked by At

How can I calculate the conditional expectation of a multivariate random variable in sympy? In particular, I'm trying to figure out the computation of the conditional expected value (Conditional distributions ) of two random variables which are jointly normal.

I tried to create a multivariate normal random variable X1, X2 ~ N(mu, Sigma)

import sympy as sy

# Join normal random variable [X1,X2 ~ N2(mu, Sigma)]
X = sy.stats.MultivariateNormal('X', [1, 2], [[3, 1], [1, 2]])

then I compute the Conditional density (X1 | X2 = x), but an exception arise

# Conditional density 
x = sy.symbols('x')
d = sy.stat.density(X[0],X[1] = x)

>>> SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Finally, to compute the conditional expected values do:

# Conditioal expected value
st.stats.E(d)

# or st.stats.E(X[0],X[1] = x)

What am I doing wrong?

0

There are 0 answers