添加两个泊松计算在sympy上是如此慢

发布于 2025-02-13 03:49:11 字数 227 浏览 0 评论 0原文

我试图在Sympy上添加两个泊松随机变量。 但是我的计算没有任何回应。

有什么可以算出答案吗?

import sympy as sp
import sympy.stats as ss
x= ss.Poisson("x", 3)
y = ss.Poisson("y", 6)
mixed = x+y
d = ss.density(mixed)
print(sp.N(d(0)))

I tried to add two poisson random variables on sympy.
But my calculation didn't response anything.

Is there anything to calc the answer?

import sympy as sp
import sympy.stats as ss
x= ss.Poisson("x", 3)
y = ss.Poisson("y", 6)
mixed = x+y
d = ss.density(mixed)
print(sp.N(d(0)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坚持沉默 2025-02-20 03:49:11

有两件事正在减慢评估:

  1. 如果您查看表达式,您会发现它包含一个从0到Infinity开始的求和。计算永远不会停止!
  2. 该表达式包含一个阶乘,该阶乘将产生很大的数字!

这就是我的方式:用符号替换Infinity,用lambdify生成函数并进行评估。

import sympy as sp
import sympy.stats as ss
x= ss.Poisson("x", 3)
y = ss.Poisson("y", 6)
mixed = x+y
d = ss.density(mixed)

# substitute infinity with a symbol
n = sp.symbols("n")
expr = d(0).subs(sp.oo, n)

# generate a function to be evaluate with Numpy/Scipy
f = sp.lambdify([n], expr)

# evaluate the function: provide a relatively high value of `n`
print(f(100))
# out: 0.00012340980408667956

如果您选择n太大,则会得到以下错误(由于阶乘),例如:

print(f(400))
# OverflowError: int too large to convert to float

There are two things that are slowing down the evaluation:

  1. If you look at the expression you will see that it contains a summation starting from 0 going to infinity. The computation will never stop!
  2. The expression contains a factorial, which is going to generate very large numbers!

This is how I would proceed: replace infinity with a symbol, generate a function with lambdify and evaluate it.

import sympy as sp
import sympy.stats as ss
x= ss.Poisson("x", 3)
y = ss.Poisson("y", 6)
mixed = x+y
d = ss.density(mixed)

# substitute infinity with a symbol
n = sp.symbols("n")
expr = d(0).subs(sp.oo, n)

# generate a function to be evaluate with Numpy/Scipy
f = sp.lambdify([n], expr)

# evaluate the function: provide a relatively high value of `n`
print(f(100))
# out: 0.00012340980408667956

If you choose n too large you will get the following error (because of factorial), for example:

print(f(400))
# OverflowError: int too large to convert to float
别闹i 2025-02-20 03:49:11

在这种特殊情况下,您有一个无限的总和,但只有第一项是非零的:

In [25]: d(0)
Out[25]: 
  ∞                          
______                       
╲                            
 ╲                           
  ╲    ⎧ -y  y  -9           
   ╲   ⎪3  ⋅6 ⋅ℯ             
    ╲  ⎪──────────  for y ≤ 0
    ╱  ⎨ (-y)!⋅y!            
   ╱   ⎪                     
  ╱    ⎪    0       otherwise
 ╱     ⎩                     
╱                            
‾‾‾‾‾‾                       
y = 0 

理​​想情况下,Sympy只能计算出来,但似乎没有明确的处理程序来对这样的零件进行总结。

因此,我们只需要评估第一项:

In [29]: d(0).args[0].subs(y.symbol, 0)
Out[29]: 
 -9
ℯ  

这等同于要求X和Y都为零的概率:

In [30]: ss.density(x)(0) * ss.density(y)(0)
Out[30]: 
 -9
ℯ  

In [31]: ss.P(Eq(x, 0) & Eq(y, 0))
Out[31]: 
 -9
ℯ 

In [32]: N(_)
Out[32]: 0.000123409804086680

In this particular case you have an infinite sum but only the first term is nonzero:

In [25]: d(0)
Out[25]: 
  ∞                          
______                       
╲                            
 ╲                           
  ╲    ⎧ -y  y  -9           
   ╲   ⎪3  ⋅6 ⋅ℯ             
    ╲  ⎪──────────  for y ≤ 0
    ╱  ⎨ (-y)!⋅y!            
   ╱   ⎪                     
  ╱    ⎪    0       otherwise
 ╱     ⎩                     
╱                            
‾‾‾‾‾‾                       
y = 0 

Ideally SymPy would just be able to compute that but it does not seem to have explicit handlers for a summation over a Piecewise like this.

We therefore just need to evaluate the first term:

In [29]: d(0).args[0].subs(y.symbol, 0)
Out[29]: 
 -9
ℯ  

This is equivalent to asking for the probability that x and y are both zero:

In [30]: ss.density(x)(0) * ss.density(y)(0)
Out[30]: 
 -9
ℯ  

In [31]: ss.P(Eq(x, 0) & Eq(y, 0))
Out[31]: 
 -9
ℯ 

In [32]: N(_)
Out[32]: 0.000123409804086680
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文