python输入不确定个参数_如何使用Python确定拟合参数的不确定性?

I have the following data for x and y:

x y

1.71 0.0

1.76 5.0

1.81 10.0

1.86 15.0

1.93 20.0

2.01 25.0

2.09 30.0

2.20 35.0

2.32 40.0

2.47 45.0

2.65 50.0

2.87 55.0

3.16 60.0

3.53 65.0

4.02 70.0

4.69 75.0

5.64 80.0

7.07 85.0

9.35 90.0

13.34 95.0

21.43 100.0

For the above data, I am trying to fit the data in the form:

However, there are certain uncertainties associated with x and y, where x has uncertainty of 50% of x and y has a fixed uncertainty. I am trying to determine the uncertainty in the fit parameters with this uncertainties package. But, I am having issues with curve fitting with scipy optimize's curve fit function. I get the following error:

minpack.error: Result from function call is not a proper array of

floats.

How do I fix the following error and determine the uncertainty of the fit parameters (a,b and n)?

MWE

from __future__ import division

import numpy as np

import re

from scipy import optimize, interpolate, spatial

from scipy.interpolate import UnivariateSpline

from uncertainties import unumpy

def linear_fit(x, a, b):

return a * x + b

uncertainty = 0.5

y_error = 1.2

x = np.array([1.71, 1.76, 1.81, 1.86, 1.93, 2.01, 2.09, 2.20, 2.32, 2.47, 2.65, 2.87, 3.16, 3.53, 4.02, 4.69, 5.64, 7.07, 9.35, 13.34, 21.43])

x_uncertainty = x * uncertainty

x = unumpy.uarray(x, x_uncertainty)

y = np.array([0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0])

y = unumpy.uarray(y, y_error)

n = np.arange(0, 5, 0.005)

coefficient_determination_on = np.empty(shape = (len(n),))

for j in range(len(n)):

n_correlation = n[j]

x_fit = 1 / ((x) ** n_correlation)

y_fit = y

fit_a_raw, fit_b_raw = optimize.curve_fit(linear_fit, x_fit, y_fit)[0]

x_prediction = (fit_a_raw / ((x) ** n_correlation)) + fit_b_raw

y_residual_squares = np.sum((x_prediction - y) ** 2)

y_total_squares = np.sum((y - np.mean(y)) ** 2)

coefficient_determination_on[j] = 1 - (y_residual_squares / y_total_squares)

解决方案

Let me first preface this with this problem being impossible to solve "nicely" given that you want to solve for a, b and n. This is because for a fixed n, your problem admits a closed form solution, while if you let n be free, it does not, and in fact the problem may have multiple solutions. Hence classical error analysis (such as that used by uncertanities) breaks down and you have to resort to other methods.

The case n fixed

If n is fixed, your problem is that the libraries you call do not support uarray, so you have to make a workaround. Thankfully, linear fitting (under the l2-distance) is simply Linear least squares which admits a closed form solution, requiring only padding the values with ones and then solving the normal equations.

Where:

You could do it like this:

import numpy as np

from uncertainties import unumpy

uncertainty = 0.5

y_error = 1.2

n = 1.0

# Define x and y

x = np.array([1.71, 1.76, 1.81, 1.86, 1.93, 2.01, 2.09, 2.20, 2.32, 2.47, 2.65,

2.87, 3.16, 3.53, 4.02, 4.69, 5.64, 7.07, 9.35, 13.34, 21.43])

# Take power of x values according to n

x_pow = x ** n

x_uncertainty = x_pow * uncertainty

x_fit = unumpy.uarray(np.c_[x_pow, np.ones_like(x)],

np.c_[x_uncertainty, np.zeros_like(x_uncertainty)])

y = np.array([0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0,

55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0])

y_fit = unumpy.uarray(y, y_error)

# Use normal equations to find coefficients

inv_mat = unumpy.ulinalg.pinv(x_fit.T.dot(x_fit))

fit_a, fit_b = inv_mat.dot(x_fit.T.dot(y_fit))

print('fit_a={}, fit_b={}'.format(fit_a, fit_b))

Result:

fit_a=4.8+/-2.6, fit_b=28+/-10

The case n unknown

With n unknown, you really are in some trouble since the problem is non-convex. Here, linear error analysis (as performed by uncertainties) will not work.

One solution is to perform Bayesian inference, using some package like pymc. If you are interested in this, I could try to make a writeup, but it would not be as clean as above.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 首先,你需要准备你的数据,这通常是一个包含两列的数据帧,其中一列是自变量,另一列是因变量。 然后,你需要安装一个名为`scipy`的库,这是一个用于科学计算的 Python 库。 接下来,你可以使用 `scipy.optimize.curve_fit` 函数来进行拟合。它需要你提供一个函数,该函数描述了你想要拟合的模型,以及你的数据。 下面是一个使用参数拟合的例子: ``` from scipy.optimize import curve_fit import numpy as np # 定义四参数模型 def four_parameter_model(x, a, b, c, d): return a / (1 + np.exp(-b * (x - c))) + d # 使用 curve_fit 函数进行拟合 params, _ = curve_fit(four_parameter_model, x_data, y_data) a, b, c, d = params # 得到拟合后的结果 fitted_y = four_parameter_model(x_data, a, b, c, d) ``` 五参数拟合的方法与此类似,你只需要定义一个带有五个参数的函数,然后使用 `curve_fit` 函数进行拟合即可。 请注意,这些拟合方法只能在你的模型与数据相匹配的情况下才能得到可靠的结果。 ### 回答2: 四参数拟合和五参数拟合都是一种统计方法,用于拟合一组数据。Python提供了一些库和函数可以实现这两种拟合。 对于四参数拟合,我们可以使用SciPy库中的curve_fit函数。首先,我们需要导入相关库和模块: ```python import numpy as np from scipy.optimize import curve_fit ``` 然后,我们定义一个待拟合的函数,例如四参数的指数函数: ```python def four_parameter(x, a, b, c, d): return a * np.exp(-b * x) + c * np.exp(-d * x) ``` 接下来,我们准备好待拟合的数据,构成两个NumPy数组,分别表示自变量和因变量: ```python x = np.array([1, 2, 3, 4, 5]) y = np.array([2.5, 1.6, 1.1, 0.6, 0.35]) ``` 然后,我们使用curve_fit函数进行拟合: ```python params, covariance = curve_fit(four_parameter, x, y) ``` 最后,我们得到了拟合参数和协方差矩阵。参数保存在params数组中,协方差矩阵保存在covariance中。你可以根据具体问题进行后续处理和分析。 同样地,对于五参数拟合,我们可以定义一个待拟合的五参数函数,准备好待拟合的数据,然后使用curve_fit函数进行拟合。只需要修改函数的定义和参数个数即可。 需要注意的是,在使用拟合方法时,数据的选择和拟合模型的确定都十分重要。合理的数据和模型选择可以提高拟合的准确性和可靠性。 ### 回答3: 对一组数据进行四参数和五参数拟合,可以使用Python编程语言来实现。 首先,需要导入相关的库,如numpy和scipy中的optimize模块。然后,定义一个自定义函数,用于计算拟合后的函数值与实际数据间的误差。此处暂不详细展开。 接下来,准备好需要拟合的数据。可以先创建两个数组,一个用于存储自变量的数值,另一个用于存储因变量的数值。可以直接将数据写在代码内部,或者从外部文件中读取。 然后,使用scipy库中的curve_fit()函数来进行四参数和五参数拟合。该函数具有以下参数:自定义的函数、自变量数组、因变量数组、拟合参数初始猜测值。 接着,分别对四参数和五参数拟合进行计算和绘图。使用numpy库中的linspace()函数生成一系列自变量的数值,然后将其代入拟合后得到的函数表达式中,计算得到因变量的数值。 最后,可以使用matplotlib库中的plot()函数将原始数据和拟合后的曲线绘制在同一张图上,以便对比。 总结说,使用Python中的numpy和scipy库,可以对一组数据进行四参数和五参数拟合。需要提前导入所需库,准备好数据,自定义函数,拟合并绘制曲线。具体步骤可以参照上述内容进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值