Evaluating a sum of a distribution and a constant

Viewed 94

I'm trying to compute the CDF of a shifted Irwin-Hall distribution in sympy. The shifting turns the expression into an Add and I don't know how to get a value out of it.

Without shifting, the code computes the CDF successfully and I can extract the numerical value:

import sympy as sp
import sympy.stats
from sympy.stats import Uniform, UniformSum

irwin_hall = UniformSum("irwin-hall", 2)
low = sympy.stats.cdf(irwin_hall)(0).evalf()
high = sympy.stats.cdf(irwin_hall)(1).evalf()
print("low", low)
print("high", high)
# prints:
#     low 0
#     high 0.500000000000000

With shifting, the code appears to compute the CDF correctly. Whether it does, I can't extract the numerical value at the high (i.e. rightmost) end of the distribution.


irwin_hall = 1 + UniformSum("irwin-hall", 2)
low = sympy.stats.cdf(irwin_hall)(0).evalf()
high = sympy.stats.cdf(irwin_hall)(1).evalf()
print("low", low)
print("high", high)
print(type(high))
# prints:
#    low 0
#    high -1.125*(-1.0)**(floor(_x) - 1.0)*(0.333333333333333*floor(_x) - 1)**2*binomial(2, floor(_x) - 1)*floor(_x) + 0.5*(-1.0)**(floor(_x) - 1.0)*(floor(_x) - 3.0)*(floor(_x) - 1.0)*binomial(2, floor(_x) - 1)*floor(_x)
#.   <class 'sympy.core.add.Add'>

I see that Uniform is able to handle adding a constant.

uniform = Uniform("uniform", 0, 1) + 1
low = sympy.stats.cdf(uniform)(0).evalf()
high = sympy.stats.cdf(uniform)(2).evalf()
print("low", low)
print("high", high)
# prints
#     low 0
#.    high 1.00000000000000

Is there a trick for making this work with UniformSum?

0 Answers
Related