简体   繁体   English

与 scipy.odr 的正交距离

[英]orthogonal distance from fit with scipy.odr

I perform an ODR fit for given data points with x and y uncertainty with scipy.odr .我使用scipy.odr对具有 x 和 y 不确定性的给定数据点执行 ODR 拟合。 For the fit result I would like to see the orthogonal distances of each point from the fit.对于拟合结果,我想查看每个点与拟合的正交距离。 This can be easily computed for a linear fit function but it can be more complicated for an arbitrary function. Since scipy.odr is minimizing these distances it has to compute them, so maybe one can directly access them but I did not find it in the documentation.对于线性拟合 function 这可以很容易地计算出来,但对于任意 function 可能会更复杂。由于scipy.odr正在最小化这些距离,因此它必须计算它们,所以也许可以直接访问它们,但我没有在文档。 Maybe I missed it?也许我错过了? Any ideas?有任何想法吗?

So for the attached example code I would like to get an array of 100 elements containing the orthogonal distances and then eg plot a histogram.因此,对于附加的示例代码,我想获得一个包含正交距离的 100 个元素的数组,然后是一个直方图,例如 plot。

import numpy as np
from scipy.odr import *

np.random.seed(1)

N = 100
x = np.linspace(0, 10, N)
y = 3*x - 1 + np.random.random(N)
sx = np.random.random(N)
sy = np.random.random(N)

def f(B, x):
    return B[0]*x + B[1]

linear = Model(f)
mydata = RealData(x, y, sx=sx, sy=sy)

myodr = ODR(mydata, linear, beta0=[1., 2.])
myoutput = myodr.run()
myoutput.pprint()

Having a quick look in the documentation I think the model estimates for the orthogonal distance are what it brings input errors in myoutput.delta and output errors in myoutput.eps , combine the two of them we can get an estimate of the orthogonal distance.快速浏览一下文档,我认为正交距离的 model 估计值是它在myoutput.eps中带来的输入错误和myoutput.delta中的 output 错误,将两者结合起来我们可以得到正交距离的估计值。

Using the calculated y error, for this linear model we can use some geometry to find the orthogonal distance that is dy * sqrt(10/81)使用计算出的y误差,对于这个线性 model,我们可以使用一些几何来找到正交距离,即dy * sqrt(10/81)

dy = (y - f(myoutput.beta, x))
orthogonal = np.sqrt((dy / 3)**2 + (dy / 9)**2)
plt.plot(dy, np.sqrt(myoutput.delta**2 + myoutput.eps**2), '.', label='Estimated orthogonal distance')
plt.plot(dy, orthogonal, '.', label='Exact orthogonal distance')
plt.xlabel('Vertical distance')
plt.ylabel('Orthogonal distance')
plt.legend()

在此处输入图像描述

It is reasonable that the estimates are higher than the actual value, since it may be not estimating properly the closest point in the fitted curve.估计值高于实际值是合理的,因为它可能没有正确估计拟合曲线中的最近点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM