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

时间:2014-03-31 21:38:33

标签: python statistics sympy gamma-distribution

继续前一个post,我想做以下事情,使用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 = density(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.

有没有办法解决这个问题?

的Ta。

1 个答案:

答案 0 :(得分:0)

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                                                                            

因此,人们可以将您的问题简化为一个问题,例如“有没有人对SymPy如何解决这个问题有任何想法?”

另外,鉴于报告错误的性质,听起来可能是您遇到的终端问题是多项式超过浮点数。在{SympPy中输入5.5这样的东西会产生Python浮点数,这些浮点数不是很大。即使输入是干净的整数,SymPy也无法解决这个问题,因此存在其他问题。

另外,一般情况下,很容易发现统计数据中难以解析的问题。在SymPy无法解析的统计数据中发现问题更容易。我的经验法则是,我们通常可以解决您在本科课本中可以找到的任何问题。

相关问题