如何在Python中找到与相关参数拟合的不确定性?

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

我已经使用卡方和 scipy fmin 函数找到了最适合数据的参数。我试图通过查找 chi squared = chi squared min + 1 轮廓上的值来获得这些参数的不确定性,并且我知道如何对不相关的参数执行此操作。我知道如何对相关参数执行此操作,但似乎需要大量代码,所以我想知道是否缺少一些简单的东西?

def polynomial(x_value, parameters):
    a = parameters[0]
    b = parameters[1]
    return (a + b) * x_value**2 + (a - b) * x_value - 2.6

def calculate_chi_squared(data, prediction):
    return np.sum((prediction - data[:, 1])**2 / data[:, 2]**2)

data = np.genfromtxt('polynomial_data_2.csv', delimiter=',')

chi_squared = lambda parameters: calculate_chi_squared(data, polynomial_2(data[:, 0], parameters))
result = optimize.fmin(chi_squared, INITIAL_GUESS_2, full_output=True)
optimised_parameters = result[0]
python contour scipy-optimize data-fitting uncertainty
1个回答
0
投票

如果

x
上有两个变量
y
s
和不确定性
y
,你可以直接使用
curve_fit
进行操作:

import numpy as np
from scipy import optimize

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

创建综合数据集:

np.random.seed(12345)
x = np.linspace(-1, 1, 30)
p0 = np.array([3, 2])
y = model(x, *p0)
s = 0.1 * np.ones_like(y)
n = s * np.random.normal(size=y.size)
yn = y + n

拟合参数并得到正确的参数协方差矩阵:

popt, pcov = optimize.curve_fit(model, x, yn, sigma=s, absolute_sigma=True)
# (array([3.02853144, 2.02814927]),
#  array([[0.00059905, 0.00013131],
#         [0.00013131, 0.00059905]]))

您可以在

sigma
absolute_sigma
开关的文档中找到详细信息。

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