Skip to content

Commit 84cd2c0

Browse files
committed
Remove instances of extra covmat_to_artunc
1 parent 01c33b2 commit 84cd2c0

4 files changed

Lines changed: 2 additions & 141 deletions

File tree

nnpdf_data/nnpdf_data/commondata/DYE906_Z0_120GEV_DW/filter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import pandas as pd
66
import yaml
77

8-
from nnpdf_data.filter_utils.correlations import covmat_to_artunc
98
from nnpdf_data.filter_utils.hera_utils import commondata
10-
from nnpdf_data.filter_utils.utils import prettify_float
9+
from nnpdf_data.filter_utils.utils import prettify_float, covmat_to_artunc
1110

1211
yaml.add_representer(float, prettify_float)
1312

nnpdf_data/nnpdf_data/commondata/LHCB_Z0J_13TEV_2022/filter.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,6 @@
4747

4848
# Utility functions
4949
# ________________________________________________________
50-
def OuterDecomposition(matrix):
51-
r"""Compute the single value decomposition of the matrix A.
52-
53-
Returns the set of vector \sigma_{i}^{(k)} defined as
54-
55-
\sigma_{i}^{(k)} = v_{i}^{(k)} * \lambda^{(k)},
56-
57-
where v_{i}^{(k)} is the k-th eigenvector of the matrix A
58-
associated to the k-th eigenvalue \lambda^{(k)}. The vectors
59-
\sigma_{i}^{(k)} are defined such that
60-
61-
A_{ij} = \sum_{k=1}^{dim(A)} \sigma_{i}^{(k)} \sigma_{j}^{(k)}.
62-
63-
If A is a correlation matrix, for each data point i there will be
64-
an associated vector \sigma_{i} = (sigma_{i}^{(1)}, ... , sigma_{i}^{(dim(A))}).
65-
66-
Parameters
67-
----------
68-
A: the matrix to be decomposed.
69-
70-
Returns
71-
-------
72-
sigma: A matrix containing the set of vectors whose outer product reconstruct the original matrix.
73-
The matrix has two indices sigma[i,k] - the first one refers to the component of the k-th
74-
eigenvector, whereas the other one selects the eigenvalue.
75-
"""
76-
eigenvalues, eigenvectors = np.linalg.eig(matrix)
77-
sigma = np.zeros_like(matrix, dtype=float)
78-
for i in range(np.size(eigenvalues)):
79-
for j in range(np.size(eigenvalues)):
80-
sigma[i][j] = eigenvectors[i][j] * np.sqrt(eigenvalues[j])
81-
return sigma
82-
83-
8450
def ExtractCorrelation(yaml_file, N):
8551
"""Convert a list of values into a matrix
8652

nnpdf_data/nnpdf_data/commondata/LHCB_Z0_13TEV_2022/filter.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,6 @@
4545

4646
# Utility functions
4747
# ________________________________________________________
48-
def OuterDecomposition(matrix):
49-
r"""Compute the single value decomposition of the matrix A.
50-
51-
Returns the set of vector \sigma_{i}^{(k)} defined as
52-
53-
\sigma_{i}^{(k)} = v_{i}^{(k)} * \lambda^{(k)},
54-
55-
where v_{i}^{(k)} is the k-th eigenvector of the matrix A
56-
associated to the k-th eigenvalue \lambda^{(k)}. The vectors
57-
\sigma_{i}^{(k)} are defined such that
58-
59-
A_{ij} = \sum_{k=1}^{dim(A)} \sigma_{i}^{(k)} \sigma_{j}^{(k)}.
60-
61-
If A is a correlation matrix, for each data point i there will be
62-
an associated vector \sigma_{i} = (sigma_{i}^{(1)}, ... , sigma_{i}^{(dim(A))}).
63-
64-
Parameters
65-
----------
66-
A: the matrix to be decomposed.
67-
68-
Returns
69-
-------
70-
sigma: A matrix containing the set of vectors whose outer product reconstruct the original matrix.
71-
The matrix has two indices sigma[i,k] - the first one refers to the component of the k-th
72-
eigenvector, whereas the other one selects the eigenvalue.
73-
"""
74-
eigenvalues, eigenvectors = np.linalg.eig(matrix)
75-
sigma = np.zeros_like(matrix, dtype=float)
76-
for i in range(np.size(eigenvalues)):
77-
for j in range(np.size(eigenvalues)):
78-
sigma[i][j] = eigenvectors[i][j] * np.sqrt(eigenvalues[j])
79-
return sigma
80-
81-
8248
def ExtractCorrelation(yaml_file, N):
8349
"""Convert a list of values into a matrix
8450
Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22
from numpy.linalg import eig
3-
from nnpdf_data.filter_utils.utils import sort_eigenvalues
3+
from nnpdf_data.filter_utils.utils import covmat_to_artunc
44

55

66
def upper_triangular_to_symmetric(ut, dim):
@@ -20,73 +20,3 @@ def compute_covmat(corrmat: np.ndarray, unc: np.ndarray, ndata: int) -> list:
2020
# multiply by stat err
2121
cov_mat = np.einsum("i,ij,j->ij", unc, corrmat, unc)
2222
return covmat_to_artunc(ndata, cov_mat.flatten().tolist())
23-
24-
25-
def covmat_to_artunc(ndata, covmat_list, no_of_norm_mat=0):
26-
r"""Convert the covariance matrix to a matrix of
27-
artificial uncertainties.
28-
29-
NOTE: This function has been taken from validphys.newcommondata_utils.
30-
If those utils get merged in the future, we can replace this.
31-
32-
Parameters
33-
----------
34-
ndata : integer
35-
Number of data points
36-
covmat_list : list
37-
A one dimensional list which contains the elements of
38-
the covariance matrix row by row. Since experimental
39-
datasets provide these matrices in a list form, this
40-
simplifies the implementation for the user.
41-
no_of_norm_mat : int
42-
Normalized covariance matrices may have an eigenvalue
43-
of 0 due to the last data point not being linearly
44-
independent. To allow for this, the user should input
45-
the number of normalized matrices that are being treated
46-
in an instance. For example, if a single covariance matrix
47-
of a normalized distribution is being processed, the input
48-
would be 1. If a covariance matrix contains pertains to
49-
3 normalized datasets (i.e. cross covmat for 3
50-
distributions), the input would be 3. The default value is
51-
0 for when the covariance matrix pertains to an absolute
52-
distribution.
53-
54-
Returns
55-
-------
56-
artunc : list
57-
A two dimensional matrix (given as a list of lists)
58-
which contains artificial uncertainties to be added
59-
to the commondata. i^th row (or list) contains the
60-
artificial uncertainties of the i^th data point.
61-
62-
"""
63-
epsilon = -0.0000000001
64-
neg_eval_count = 0
65-
psd_check = True
66-
covmat = np.zeros((ndata, ndata))
67-
artunc = np.zeros((ndata, ndata))
68-
for i in range(len(covmat_list)):
69-
a = i // ndata
70-
b = i % ndata
71-
covmat[a][b] = covmat_list[i]
72-
eigval, eigvec = eig(covmat)
73-
eigval, eigvec = sort_eigenvalues(eigval, eigvec)
74-
for j in range(len(eigval)):
75-
if eigval[j] < epsilon:
76-
psd_check = False
77-
elif eigval[j] > epsilon and eigval[j] <= 0:
78-
neg_eval_count = neg_eval_count + 1
79-
if neg_eval_count == (no_of_norm_mat + 1):
80-
psd_check = False
81-
elif eigval[j] > 0:
82-
continue
83-
if psd_check == False:
84-
raise ValueError("The covariance matrix is not positive-semidefinite")
85-
else:
86-
for i in range(ndata):
87-
for j in range(ndata):
88-
if eigval[j] < 0:
89-
continue
90-
else:
91-
artunc[i][j] = eigvec[i][j] * np.sqrt(eigval[j])
92-
return artunc.tolist()

0 commit comments

Comments
 (0)