2013-11-25 69 views
3

我需要适合泊松分布到一组数据:泊松分布适合

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) # Target function 
errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function 
p0 = [1., 2.] # Initial guess for the parameters 
p1, success = optimize.leastsq(errfunc, p0[:], args=(bins_mean, n)) 

我提出,使用在SciPy documentation给出的例子。

如果我对伽玛函数部分发表评论,它就像一个魅力,所以问题在那里,但我不知道如何解决它。

我得到以下错误:

TypeError: can only concatenate list (not "int") to list 

拟合的输入参数是plt.hist的输出,我检查和类型是numpy的ndarray

+0

你会得到什么错误? –

+2

如果你有数据(不只是直方图),这里没有必要进行优化。对于泊松分布,只需通过“np.mean(data)”就可以分析找到最合适的参数(lambda,你的'p [1]')。 'p [0]'只是标准化。 – askewchan

+1

是'bins_mean'和'n' numpy数组吗?错误使我相信你正在通过列表。 –

回答

4
fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) # Target function 
errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function 
p0 = [1., 2.] # Initial guess for the parameters 
p1, success = optimize.leastsq(errfunc, p0[:], args=(bins_mean, n)) 

既然你说它工作没有math.gamma(x + 1)的一部分,我猜测它会工作,如果你改变

fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/math.gamma(x+1) 

from scipy.misc import factorial 
fitfunc = lambda p, x: p[0]*pow(p[1],x)*pow(e,-p[1])/factorial(x) 

因为math.gamma不喜欢列表(或任何不是我猜的浮动),而阶乘可以正常使用列表吗?

旁边的问题:你为什么要使用pow,而不是仅仅使用**?