我如何给一个协方差矩阵scipy.odr公司?

2024-09-28 23:23:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试对x和y数据进行相关拟合,但是当我传入x和y测量的协方差矩阵时,我得到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-173-273ef42c6f27> in <module>()
----> 1 odrout = theodr.run()

/Users/anaconda/lib/python2.7/site-packages/scipy/odr/odrpack.pyc in run(self)
   1098         for attr in kwd_l:
   1099             obj = getattr(self, attr)
-> 1100             if obj is not None:
   1101                 kwds[attr] = obj
   1102 

ValueError: could not convert we to a suitable array

下面是在我的机器上触发此错误的最小不工作示例:

import numpy as np
import scipy.odr as spodr

# make x and y data for a function
xx = np.linspace(0, 2*np.pi, 100)
yy = 2.*np.sin(3*xx) - 1
# randomize both variables a bit, and make 10 measurements
# of each data point
xdat = xx + np.random.normal(scale=0.3, size=(10,100))
ydat = yy + np.random.normal(scale=0.3, size=(10, 100))
# the function I will fit to
sin = lambda beta, x: beta[0]*np.sin(beta[1] * x) + beta[2]
# the covariance matrices for both data sets, here I summed over
# the 10 measurements I made for both my x and y data
xcov = np.cov(xdat.transpose())
ycov = np.cov(ydat.transpose())
# setup the odr data
odrdat = spodr.RealData(np.mean(xdat, axis=0),
                        np.mean(ydat, axis=0), covx=xcov, covy=ycov)
# set up the odr model
model = spodr.Model(sin)
# make the odr object
theodr = spodr.ODR(odrdat, model, beta0=[2,3,-1])
# run the odr object
odrout = theodr.run()

我似乎不明白为什么我传递的矩阵不是合适的数组。从文档中:

Covariance of x covx is an array of covariance matrices of x and are converted to weights by performing a matrix inversion on each observation’s covariance matrix.

这让我觉得我应该为每个数据点传递一个协方差矩阵,但是我没有那种类型的信息,我认为我不需要它。对于相关拟合,所有数据之间的协方差就足够了。例如,在scipy.curve_fit中,可以将二维数组作为y数据的协方差矩阵传入,而不是每个点都需要一个协方差矩阵。你知道吗

有什么特殊的方法可以传递这些协方差矩阵吗?你知道吗


Tags: andofthe数据runinfordata