Search code examples
pythonrandomsympyexpectations

Expectation of a product in python sympy


In python sympy I have the following code

from sympy.stats import P, E, variance, covariance, Normal

mu, sigma = symbols('mu sigma')
epsilon_t = Normal('epsilon_t', 0, sigma)
epsilon_t1 = Normal('epsilon_t+1', 0, sigma)

which defines two (presumably independent) normal random variables. Now, I would expect that

E(epsilon_t1*epsilon_t)

is equal to zero, but when I run this through sympy, I get something a lot more complicated. It tells me it is zero for pi/2 > 2|larg(sigma)| and something much more complicated otherwise. I don't care about the second part, I just want it to be zero. How can I do that?


Solution

  • If you look at help(arg) you will get a hint that is helpful: the arg(x) is 0 if its x is positive. So if you define your parameters as positive instead of real, arg will be able to evaluate:

    >>> mu, sigma = symbols('mu sigma',positive=True)
    >>> epsilon_t = Normal('epsilon_t', 0, sigma)
    >>> epsilon_t1 = Normal('epsilon_t+1', 0, sigma)
    >>> E(epsilon_t*epsilon_t1)
    0