[SciPy-User] [SciPy-user] scipy.odr - Correct form of covx, covy matrix

Robert Kern robert.kern@gmail....
Mon Feb 13 09:59:35 CST 2012

```On Sun, Feb 12, 2012 at 23:11, Elfnor <elfnor@gmail.com> wrote:
>
> I'm having trouble using covariance matrices with odrpack RealData
>
> I expected the following adaptation of the test in test_odr.py to work but
> get the error
>
>  File "C:\Python27\lib\site-packages\scipy\odr\odrpack.py", line 1080, in
> run
>    self.output = Output(apply(odr, args, kwds))
> ValueError: could not convert we to a suitable array
> _________________________
> import numpy as np
> from  scipy.odr.odrpack import *
>
> p_x = np.array([0.,.9,1.8,2.6,3.3,4.4,5.2,6.1,6.5,7.4])
> p_y = np.array([5.9,5.4,4.4,4.6,3.5,3.7,2.8,2.8,2.4,1.5])
> p_sx = np.array([.03,.03,.04,.035,.07,.11,.13,.22,.74,1.])
> p_sy = np.array([1.,.74,.5,.35,.22,.22,.12,.12,.1,.04])
>
> def pearson_fcn(B, x):
>        return B[0] + B[1]*x
>
> covp_x = np.diag(p_sx**2)
> covp_y = np.diag(p_sy**2)
>
> p_dat = RealData(p_x, p_y, covx=covp_x, covy=covp_y)

covx and covy aren't covariance matrices across observations, i.e. not
the 10x10 matrices that you have here. Rather, what you need to
provide is a length-10 array of covariance matrices, one for each
observation. For the case where each of your observations are
1-dimensional, you don't construct covariance matrices at all, just
provide the standard deviations:

p_dat = RealData(p_x, p_y, sx=p_sx, sy=p_sy)

https://github.com/scipy/scipy/blob/master/scipy/odr/odrpack.py#L27

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
```