用标量因子拟合Python中的Gamma分布

时间:2019-05-21 04:15:39

标签: python-3.x least-squares model-fitting gamma-distribution

我已经进行了优化以最小化结果模型的误差,但是R平方仍然很低。我尝试将标量与结果模型相乘以获得更好的拟合度。标量也将通过优化来生成。

我已经在Excel中使用求解器工具完成了此操作,并获得了令人满意的输出,但是Python给了我不同的结果。

import numpy as np
import scipy.stats as stats 
from scipy.optimize import leastsq
import sklearn.metrics as metrics

def my_res( params, yData ):
    a, b = params
    xList= range( 1, len(yData) + 1 )
    th = np.fromiter( ( stats.gamma.pdf( x, a, loc=0, scale=b ) for x in  xList ), np.float )
    diff = np.array(th) - np.array( yData )
    return diff

data = [0.030235821,    0.039199333,    0.029218791,    0.017308671]

sol, err = leastsq( my_res, [.4, 1 ], args=( data, ) )

datath = [ stats.gamma.pdf( x, sol[0], loc=0, scale=sol[1]) for x in range(1,5) ]

### the result showed low R-squared
ll=[1,2,3,4,5,6,7,8,9,10]
plop=stats.gamma.pdf(ll, sol[0], loc=0, scale=sol[1])
print ('expected values:')
print(plop)
print(sol)
rscored=metrics.r2_score(data,np.array(plop[0:4]))
print("R-squared without scalar: ",rscored)
##############################################################################
#Code with scalar
def my_res( params, yData ):
    a, b, c = params
    xList= range( 1, len(yData) + 1 )
    th = np.fromiter( ( stats.gamma.pdf( x, a, loc=0, scale=b ) for x in  xList ), np.float )
    diff = np.array(th)*c - np.array( yData )
    return diff

data = [0.030235821,    0.039199333,    0.029218791,    0.017308671]

sol, err = leastsq( my_res, [.4, 1, 0.1 ], args=( data, ) )

datath = [ stats.gamma.pdf( x, sol[0], loc=0, scale=sol[1]) for x in range(1,5) ]

### the result gives the expected answer
ll=[1,2,3,4,5,6,7,8,9,10]
plop=stats.gamma.pdf(ll, sol[0], loc=0, scale=sol[1])
print ('expected values:')
print(plop)
print(sol)
rscored=metrics.r2_score(data,np.array(plop[0:4]))
print("R-squared with scalar: ",rscored)

标量0.133922081 阿尔法2.928877362 Beta 0.928544745 R ^ 2 0.999996716 期望值: [0.030232481 0.039211057 0.029197912 0.0173232 0.009074871 0.004393961 0.002014991 0.000888009 0.000379637 0.000158458]

0 个答案:

没有答案