【问题标题】:How to find Chi square value of a bimodal Gaussain fitting?如何找到双峰高斯拟合的卡方值?
【发布时间】:2020-11-30 08:33:28
【问题描述】:

我在这段代码下面写;

from pylab import *
from scipy.optimize import curve_fit

data=concatenate((normal(1,.2,5000),normal(2,.2,2500)))
y,x,_=hist(data,100,alpha=.3,label='data')

x=(x[1:]+x[:-1])/2 # for len(x)==len(y)

def gauss(x,mu,sigma,A):
    return A*exp(-(x-mu)**2/2/sigma**2)

def bimodal(x,mu1,sigma1,A1,mu2,sigma2,A2):
    return gauss(x,mu1,sigma1,A1)+gauss(x,mu2,sigma2,A2)

expected=(1,.2,250,2,.2,125)
params,cov=curve_fit(bimodal,x,y,expected)
sigma=sqrt(diag(cov))
plot(x,bimodal(x,*params),color='red',lw=3,label='model')
legend()
print(params,'\n',sigma)    

我得到了这个情节:

现在,我想通过计算卡方的值来测试拟合的好坏,我如何找出卡方的值?

【问题讨论】:

    标签: python matplotlib astronomy chi-squared model-fitting


    【解决方案1】:

    您可以执行以下操作,利用 numpys 数组操作功能

    import numpy as np
    
    diff = np.array(bimodal(x,*params)) - np.array(y)
    chi2 = np.sum( diff**2 / np.array(sigma)**2 )
    
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2018-03-12
      • 2021-06-22
      • 2018-07-23
      • 1970-01-01
      • 2017-02-23
      • 2021-08-20
      • 2018-04-17
      相关资源
      最近更新 更多