nrc-cnrc/MetroloPy

gummy.create() with covariance matrix mutates input matrix

nluetts opened this issue · 2 comments

Hi,

I noticed that gummy.create() mutates its input covariance matrix.
This leads to unexpected behavior:

from metrolopy import gummy
import numpy as np

val = [1., 2.]
cov = np.array([[0.3, 0.1], [0.1, 0.2]])

print("cov before gummy creation:\n", cov)
print()

a, b = gummy.create(val, covariance_matrix=cov)

print("cov after gummy creation:\n", cov)
print()
print("a + b after first gummy creation ", a + b)

a, b = gummy.create(val, covariance_matrix=cov)

print("a + b after second gummy creation ", a + b)
cov before gummy creation:
 [[0.3 0.1]
 [0.1 0.2]]

cov after gummy creation:
 [[1.         0.40824829]
 [0.40824829 1.        ]]

a + b after first gummy creation  3.00(84)
a + b after second gummy creation  3.0(17)

I think this line is the problem:

matrix[i][j] /= gummys[i]._u*gummys[j]._u

Is the mutation necessary?
A workaround is to pass a deepcopy of the covariance matrix.

Good catch, the matrix should not be mutated. I will change this line to create a new matrix instead.

Version 0.5.6 has now been released which fixes this bug.