Bobo - 1 year ago 116

Python Question

I have to calculate the covariance between 2 parameters from a fit function. I found this package in Python called iminuit that did a good fit and also calculate the covariance matrix of the parameters. I tested the package on a simple function. This is the code:

`from iminuit import Minuit, describe, Struct`

def func(x,y):

f=x**2+y**2

return f

m = Minuit(func,pedantic=False,print_level=0)

m.migrad()

print("Covariance:")

print(m.matrix())

and this is the output:

Covariance:

((1.0, 0.0),

(0.0, 1.0))

However if i replace x^2+y^2 with (x-y)^2 I obtain

Covariance:

((250.24975024975475, 249.75024975025426),

(249.75024975025426, 250.24975024975475))

I am confused why do I get covariance bigger than 1 (I am not good at statistics but from what I understood it has to be between -1 and 1), so someone who knows iminuit can help me? And also, in the first case, what does the matrix means? Why there is 0 correlation between x and y and what 1 on the diagonal means?

Answer Source

You are confusing covariance with correlation. Correlation is the normalised version of the covariance, which is indeed always between -1 and 1.

To obtain the corellation from the covariance matrix, calculate:

```
correlation = cov[0, 1] / np.sqrt(cov[0, 0] * cov[1, 1])
```