在Python中使用Scipy进行卡方分布的参数优化
发布时间:2023-12-26 00:50:51
在Python中使用Scipy进行卡方分布的参数优化可以通过使用scipy.stats模块中的chi2类来实现。该类提供了计算和拟合卡方分布的功能。
首先,需要导入相关的库和模块:
import numpy as np from scipy import stats from scipy.optimize import curve_fit import matplotlib.pyplot as plt
然后,定义一个目标函数来拟合卡方分布参数:
def chi2_dist(x, k):
return stats.chi2.pdf(x, k)
这里的目标函数chi2_dist接受两个参数,x和k,其中k为卡方分布的自由度,x为自变量。
接下来,我们需要生成一些模拟的数据来进行拟合。可以使用numpy的random模块的chisquare函数来生成符合卡方分布的数据:
np.random.seed(0) x_data = np.random.chisquare(10, 100) hist, bin_edges = np.histogram(x_data, bins=10, density=True)
这里生成了一个包含100个符合自由度为10的卡方分布的样本数据。
然后,使用scipy的curve_fit函数来进行参数优化:
params, params_covariance = curve_fit(chi2_dist, bin_edges[:-1], hist)
这里的curve_fit函数接受两个参数, 个参数为目标函数,第二个参数为样本数据。返回值为参数估计值和参数的协方差矩阵。
最后,可以用拟合得到的参数值来绘制拟合曲线:
x = np.linspace(0, 20, 100) y = chi2_dist(x, params[0]) plt.plot(x, y, label='Fit: k=%5.3f' % tuple(params)) plt.hist(x_data, bins=10, density=True, alpha=0.7, label='data') plt.legend() plt.show()
这段代码生成了一个自变量范围从0到20,包含100个点的均匀分布。然后使用刚才拟合得到的参数值来计算拟合曲线,并绘制拟合曲线和原始数据的直方图。
完整的代码如下:
import numpy as np
from scipy import stats
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def chi2_dist(x, k):
return stats.chi2.pdf(x, k)
np.random.seed(0)
x_data = np.random.chisquare(10, 100)
hist, bin_edges = np.histogram(x_data, bins=10, density=True)
params, params_covariance = curve_fit(chi2_dist, bin_edges[:-1], hist)
x = np.linspace(0, 20, 100)
y = chi2_dist(x, params[0])
plt.plot(x, y, label='Fit: k=%5.3f' % tuple(params))
plt.hist(x_data, bins=10, density=True, alpha=0.7, label='data')
plt.legend()
plt.show()
运行这段代码可以得到一个包含拟合曲线和原始数据的直方图,其中拟合曲线根据样本数据自动计算了卡方分布的参数。
