如何在考虑两条曲线的 x 值的同时对它们进行卡方检验?

问题描述 投票:0回答:1

我有两条曲线;一张显示理论曲线,一张显示实验数据。

曲线1(理论曲线): x 值:

lambdaDiffuse*1e9
y 值:
y_th

曲线2(实验数据): x 值:

redresse_x
y 值:
coupe

我正在进行卡方检验,以找到与实验结果最相符的参数 ne、Te 和 m。当我使用上面显示的 x 值绘制曲线时(每条曲线不同),我得到以下图:

The curves as they are meant to be compared.

我用于卡方测试的代码相当简单:

def chi_square(params, lambdaDiffuse, coupe):
    ne, Te, m = params
    y_th = P2(lambdaDiffuse, ne, Te, m)
    chi_square_value = np.sum((coupe - y_th)**2)

其中 P2 是计算给定一组 ne、Te 和 m 值的理论曲线的函数。

问题在于,通过将卡方值计算为

chi_square_value = np.sum((coupe - y_th)**2)
,不会考虑 x 值。这意味着 y_th 和 coupe 位于 x 轴的不同部分,并且 y 值没有按应有的方式分布,如下所示。因此,结果不正确,因为它们没有对齐并且缩放比例不同。

The curves that are used for the chi square test. Notice how they are not centered around the same value (~532 nm), as well as the different scaling of the two curves compared to the ones shown in the first picture.

我尝试使用

scipy.interpolate
,如下所示,但无济于事。

def chi_square(params, lambdaDiffuse, coupe):
    ne, Te, m = params
    y_theoretical = P2(lambdaDiffuse, ne, Te, m)
    interpolate_exp = interp1d(redresse_x, coupe, kind='linear', fill_value='extrapolate')
    coupe_interp = interpolate_exp(lambdaDiffuse*1e9)
    chi_square_value = np.sum((coupe_interp - y_theoretical)**2)

如何执行卡方检验并确保两条曲线的缩放比例与使用适当的 x 值绘制它们时所显示的一致?

python scipy curve-fitting chi-squared
1个回答
0
投票

让我们构建一个 MCVE 来展示如何使用简单的模型找到平移和缩放。

import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize

def model(x, a, b, c):
    return a * x**2 + b * x + c

我们从模型创建数据并在

x
轴上应用一些移位和缩放:

np.random.seed(12345)
x0 = np.linspace(-1, 1, 200)
x1 = x0 * 0.5  + 1.
p0 = (1, 2, 3)
y = model(x0, *p0)
n = 0.05 * np.random.normal(size=y.size)
yn = y + n

现在我们创建一个适应模型,在使用基础模型之前缩放和移动

x
轴:

def adapted_model(x, scale, loc):
    x0 = (x - loc) / scale
    return model(x0, *p0)

我们针对真实数据优化wrt:

popt, pcov = optimize.curve_fit(adapted_model, x1, yn, p0=[1, -1])
# (array([0.49895806, 1.00052535]),
#  array([[ 3.90617479e-06, -1.97083511e-06],
#         [-1.97083511e-06,  1.58383376e-06]]))

现在我们可以将转换应用于实验数据以适应模型。

fig, axe = plt.subplots()
axe.plot(x0, y, label="Model")
axe.plot(x1, yn, label="Data")
axe.plot(x1, adapted_model(x1, *popt), label="Adapted Model")
axe.legend()
axe.grid()

您可以将模型和数据对齐到

x
轴的比例和位移。

© www.soinside.com 2019 - 2024. All rights reserved.