Python sympy.stats中的反伽马分布的混合

Python sympy.stats中的反伽马分布的混合,python,statistics,sympy,gamma-distribution,Python,Statistics,Sympy,Gamma Distribution,在上一篇文章的基础上,我想使用sympy.stats对反向伽马分布进行加权混合: %matplotlib inline from matplotlib import pyplot as plt from sympy.stats import GammaInverse, density import numpy as np f1 = 0.7; f2 = 1-f1 G1 = GammaInverse("G1", 5, 120/(5.5*2.5E-7)) G2 = GammaInverse("G2"

在上一篇文章的基础上,我想使用sympy.stats对反向伽马分布进行加权混合:

%matplotlib inline
from matplotlib import pyplot as plt
from sympy.stats import GammaInverse, density
import numpy as np

f1 = 0.7; f2 = 1-f1
G1 = GammaInverse("G1", 5, 120/(5.5*2.5E-7))
G2 = GammaInverse("G2", 4, 120/(5.5*1.5E-7))
G3 = f1*G1 + f2*G2
D1 = density(G1);  
D2 = density(G2);  
D3 = density(G3);
v1 = [D1(i).evalf() for i in u]
v2 = [D2(i).evalf() for i in u]
v3 = [D3(i).evalf() for i in u]
不幸的是,此错误出现在
D3=密度(G3)
,错误以

PolynomialDivisionFailed: couldn't reduce degree in a polynomial 
division algorithm when dividing [231761.370742578/(0.0011381138741823*G2**2 -
 0.007587425827882*G2*_z + 0.0126457097131367*_z**2), 0.0]
by [263.770831541635/263.770831541635, 0.0]. 
This can happen when it's not possible to detect zero in the coefficient domain. 
The domain of computation is RR(G2,_t0,_z). Zero detection is guaranteed in this
coefficient domain. This may indicate a bug in SymPy or the domain is user defined
and doesn't implement zero detection properly.
有办法解决这个问题吗


助教

Sympy.stats生成的积分如下所示:

In [1]: from sympy.stats import *
In [2]: a, b, c, d, = symbols('a b c d', real=True, positive=True)
In [3]: G1 = GammaInverse("G1", a, b)
In [4]: G2 = GammaInverse("G2", c, d)
In [5]: G3 = S(7)/10*G1 + S(3)/10*G2
In [7]: density(G3, evaluate=False)(x)
Out[7]: 
∞                                                                            
⌠                                                                            
⎮                  ∞                                                         
⎮                  ⌠                                                         
⎮                  ⎮              -b                                         
⎮                  ⎮              ───                                        
⎮              -d  ⎮   -a - 1  a   G₁           ⎛7⋅G₁   3⋅G₂    ⎞            
⎮              ─── ⎮ G₁      ⋅b ⋅ℯ   ⋅DiracDelta⎜──── + ──── - x⎟            
⎮   -c - 1  c   G₂ ⎮                            ⎝ 10     10     ⎠            
⎮ G₂      ⋅d ⋅ℯ   ⋅⎮ ──────────────────────────────────────────── d(G₁)      
⎮                  ⎮                     Γ(a)                                
⎮                  ⌡                                                         
⎮                  0                                                         
⎮ ───────────────────────────────────────────────────────────────────── d(G₂)
⎮                                  Γ(c)                                      
⌡                                                                            
0                                                                            
所以我们可以把你的问题简化成这样的问题,“有人有没有想过Symphy如何解这个积分?”

此外,考虑到报告错误的性质,您可能遇到的终端问题是浮点数上的多项式。在SymPy中键入
5.5
之类的内容会产生Python浮点值,而这些浮点值并不十分数学化。即使输入是干净的整数,Symphy也无法解决这个问题,因此还有其他问题


也只是一个一般性的说明,在统计中很容易发现不容易解析解决的问题。在统计学中更容易发现SymPy无法分析解决的问题。我的经验法则是,我们通常可以解决你在本科教科书中发现的任何问题

考虑到积分有一个DiracDelta,这可能只是改进delta积分算法的问题。哦,实际上,内部积分是可计算的。这是SymPy不能做的外部积分。