# [SciPy-user] negative values in diagonal of covariance matrix

David Cournapeau cournape@gmail....
Thu Dec 11 07:41:10 CST 2008

```On Thu, Dec 11, 2008 at 10:09 PM, Pavlo Shchelokovskyy
<shchelokovskyy@gmail.com> wrote:
> Hi all,
>
> I'm a moderately new user of scipy, trying to make some curve-fitting
> with it. I wanted to use cov matrix output of leastsq to estimate
> errors of fitted parameters, but stumbled upon strange discrepancy
> (for one particular dataset):
>
> On Linux (Fedora 8), using Python 2.5.1, numpy 1.2.0 and scipy 0.6.0
>
>>>> from scipy import *
>>>> from scipy import optimize
>>>> y = asarray([217, 182, 162, 170, 255])
>>>> x = linspace(0, y.size - 1, y.size)
>>>> gauss = lambda p, x: p[0] + p[1] * exp(-(x-p[2])**2/(2*p[3]**2))
>>>> errfunc = lambda p, x, y: y - gauss(p, x)
>>>> pinit = (max(y), -y.ptp(), argmin(y), y.size/4)
>>>> fit,cov,info,mesg,success = optimize.leastsq(errfunc, pinit, args=(x,y), full_output = 1)
>>>> fit
> array([  3.01602865e+05,  -3.01444487e+05,   1.83283239e+00, 8.87273494e+01])
>>>> cov
> array([[ -2.27903048e+16,   2.27903047e+16,  -4.72378743e+05, -3.35454486e+12],
>       [  2.27903047e+16,  -2.27903046e+16,   4.72378733e+05, 3.35454485e+12],
>       [ -4.72378950e+05,   4.72378947e+05,   6.38886491e-05, -6.95317302e+01],
>       [ -3.35454486e+12,   3.35454485e+12,  -6.95316881e+01, -4.93761332e+08]])
>

This kind of values suggests a badly scaled result at best. The
numerical values cannot be trusted, so discrepencies are not
surprising: you are outside the validity of the numerical methods. The
fact that the values on windows are positive are just an accident and
an implementation detail.

> Is it my error or misunderstanding somewhere, or is it really a bug in
> Linux implementation?

It is likely that both platforms do not use the same implementation -
and compilers version/options differences could explain the difference
for the same code source. Again, this is not surprising - and not a
bug - if the methods are outside their validity range (it is of course
a totally different matter if you are in the expected domain of the
algorithms: in this case, a correct implementation and installation of
numpy/scipy should hopefully only differ in the same range as machine
precision).

cheers,

David
```