Skip to content

Commit 0ffaab6

Browse files
committed
order eigenvalues and eigenvectors
1 parent 726d1d6 commit 0ffaab6

9 files changed

Lines changed: 46 additions & 28 deletions

File tree

nnpdf_data/nnpdf_data/commondata/ATLAS_WJ_8TEV/filter_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pandas as pd
66
import yaml
77

8-
from nnpdf_data.filter_utils.utils import matlist_to_matrix, prettify_float, symmetrize_errors
8+
from nnpdf_data.filter_utils.utils import matlist_to_matrix, prettify_float, symmetrize_errors, decompose_covmat
99

1010
yaml.add_representer(float, prettify_float)
1111

@@ -406,8 +406,8 @@ def generate_data(self, variant='default', save_to_yaml=False, path='./'):
406406

407407
# Get statistical (artidicial uncertainties)
408408
stat_covmat = self.__build_abs_stat_covmat()
409-
eigvals, eigvecs = np.linalg.eig(stat_covmat)
410-
art_stat = np.sqrt(eigvals) * eigvecs
409+
410+
art_stat = decompose_covmat(stat_covmat)
411411

412412
sys_artificial = [] # Initialize vector of artificial uncertainties
413413

nnpdf_data/nnpdf_data/commondata/ATLAS_Z0_8TEV_ZMASS/filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33
import yaml
44

5-
from nnpdf_data.filter_utils.utils import prettify_float
5+
from nnpdf_data.filter_utils.utils import prettify_float, decompose_covmat
66

77
yaml.add_representer(float, prettify_float)
88

@@ -50,8 +50,8 @@ def filter_ATLAS_Z0_8TEV_uncertainties():
5050

5151
# compute decomposition of covariance matrix so as to get artificial systematics
5252
# TODO: use utils once merged in master
53-
lamb, mat = np.linalg.eig(cov_matrix)
54-
art_sys = np.multiply(np.sqrt(lamb), mat)
53+
54+
art_sys = decompose_covmat(cov_matrix)
5555

5656
uncertainties = []
5757

nnpdf_data/nnpdf_data/commondata/CMS_WCHARM_7TEV/filter_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import yaml
77

8-
from nnpdf_data.filter_utils.utils import prettify_float
8+
from nnpdf_data.filter_utils.utils import prettify_float, decompose_covmat
99

1010
yaml.add_representer(float, prettify_float)
1111

@@ -211,8 +211,8 @@ def generate_data(self):
211211
if self.observable == 'WPWM-TOT':
212212
# Generate covmat and perform eigen decomposition
213213
covmat = self._generate_covmat(sys_unc)
214-
eigvals, eigvecs = np.linalg.eig(covmat)
215-
art_unc = np.sqrt(eigvals) * eigvecs
214+
215+
art_unc = decompose_covmat(covmat)
216216

217217
# Loop over bins
218218
for data_idx in range(len(central_data)):

nnpdf_data/nnpdf_data/commondata/CMS_WPWM_13TEV_ETA/filter_utils.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import uproot
33
import yaml
44

5-
from nnpdf_data.filter_utils.utils import prettify_float
5+
from nnpdf_data.filter_utils.utils import prettify_float, decompose_covmat
66

77
yaml.add_representer(float, prettify_float)
88

@@ -83,16 +83,6 @@ def get_data_values(version, figure):
8383
return data_central
8484

8585

86-
def decompose_covmat(covmat):
87-
"""Given a covmat it return an array sys with shape (ndat,ndat)
88-
giving ndat correlated systematics for each of the ndat point.
89-
The original covmat is obtained by doing sys@sys.T"""
90-
91-
lamb, mat = np.linalg.eig(covmat)
92-
sys = np.multiply(np.sqrt(lamb), mat)
93-
return sys
94-
95-
9686
def get_systematics(observable, version, figure):
9787
"""
9888
Following the CMS advice we take the covariance matrix from

nnpdf_data/nnpdf_data/commondata/CMS_Z0J_8TEV/filter_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import yaml
77

8-
from nnpdf_data.filter_utils.utils import prettify_float
8+
from nnpdf_data.filter_utils.utils import prettify_float, decompose_covmat
99

1010
yaml.add_representer(float, prettify_float)
1111

@@ -229,8 +229,8 @@ def generate_data(self, variant='default'):
229229
# eigenvector basis, hence they are called "artificial uncertainties".
230230
# The original covmat can be reconstruted as covat = art_stat.T @ art_stat
231231
covmat = self._build_covmat()
232-
eigvals, eigvecs = np.linalg.eig(covmat)
233-
art_stat = np.sqrt(eigvals) * eigvecs * self.mult_factor
232+
233+
art_stat = decompose_covmat(covmat) * self.mult_factor
234234

235235
unc_vals = [] # Initialize vector of uncertainties
236236
for data_idx, data in enumerate(central_data):

nnpdf_data/nnpdf_data/commondata/LHCB_Z0J_13TEV_2022/filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import yaml
55

6-
from nnpdf_data.filter_utils.utils import prettify_float
6+
from nnpdf_data.filter_utils.utils import prettify_float, decompose_covmat
77

88
yaml.add_representer(float, prettify_float)
99

@@ -386,7 +386,7 @@ def processData():
386386
cov = ComputeCovariance(cormat, v)
387387

388388
# Single value decomposition
389-
sigma = OuterDecomposition(cov)
389+
sigma = decompose_covmat(cov)
390390

391391
# Loop over the bins
392392
for k in range(len(error_diag)):

nnpdf_data/nnpdf_data/commondata/LHCB_Z0_13TEV_2022/filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import yaml
55

6-
from nnpdf_data.filter_utils.utils import prettify_float
6+
from nnpdf_data.filter_utils.utils import prettify_float, decompose_covmat
77

88
yaml.add_representer(float, prettify_float)
99

@@ -313,7 +313,7 @@ def processData():
313313
cov = ComputeCovariance(cormat, v)
314314

315315
# Single value decomposition
316-
sigma = OuterDecomposition(cov)
316+
sigma = decompose_covmat(cov)
317317

318318
# Loop over the bins
319319
for k in range(len(error_diag)):

nnpdf_data/nnpdf_data/filter_utils/correlations.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from numpy.linalg import eig
3+
from nnpdf_data.filter_utils.utils import sort_eigenvalues
34

45

56
def upper_triangular_to_symmetric(ut, dim):
@@ -69,6 +70,7 @@ def covmat_to_artunc(ndata, covmat_list, no_of_norm_mat=0):
6970
b = i % ndata
7071
covmat[a][b] = covmat_list[i]
7172
eigval, eigvec = eig(covmat)
73+
eigval, eigvec = sort_eigenvalues(eigval, eigvec)
7274
for j in range(len(eigval)):
7375
if eigval[j] < epsilon:
7476
psd_check = False

nnpdf_data/nnpdf_data/filter_utils/utils.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ def cormat_to_covmat(err_list, cormat_list):
106106
covmat_list.append(cormat_list[i] * err_list[a] * err_list[b])
107107
return covmat_list
108108

109+
def sort_eigenvalues(evals, evecs):
110+
r"""Defines ordering of the eigenvalues and eigenvectors such
111+
that eigenvalues are given in decreasing order and the first
112+
non-zero entry of each eigenvector is positive.
113+
Parameters
114+
----------
115+
evals, evecs : output of np.linalg.eig
116+
117+
Returns
118+
-------
119+
evacs_sorted, evecs_sorted : ordered eigen- values and vectors,
120+
as defined above
121+
122+
"""
123+
idx = evals.argsort()[::-1]
124+
evals_sorted = evals[idx]
125+
evecs_sorted = evecs[:,idx]
126+
for i in range(len(evecs_sorted)):
127+
j = 0
128+
while evecs_sorted[j,i] == 0:
129+
j += 1
130+
if evecs_sorted[j,i] < 0:
131+
evecs_sorted[:, i] *= -1
132+
return evals_sorted, evecs_sorted
109133

110134
def covmat_to_artunc(ndata, covmat_list, no_of_norm_mat=0):
111135
r"""Convert the covariance matrix to a matrix of
@@ -152,6 +176,7 @@ def covmat_to_artunc(ndata, covmat_list, no_of_norm_mat=0):
152176
b = i % ndata
153177
covmat[a][b] = covmat_list[i]
154178
eigval, eigvec = eig(covmat)
179+
eigval, eigvec = sort_eigenvalues(eigval, eigvec)
155180
for j in range(len(eigval)):
156181
if eigval[j] < epsilon:
157182
psd_check = False
@@ -397,7 +422,8 @@ def decompose_covmat(covmat):
397422
giving ndat correlated systematics for each of the ndat point.
398423
The original covmat is obtained by doing sys@sys.T"""
399424

400-
lamb, mat = np.linalg.eig(covmat)
425+
lamb, mat = eig(covmat)
426+
lamb, mat = sort_eigenvalues(lamb, mat)
401427
sys = np.multiply(np.sqrt(lamb), mat)
402428
return sys
403429

0 commit comments

Comments
 (0)