Skip to content

Commit ec8f2bb

Browse files
committed
Merge branch 'cross-cov'
2 parents dca71e2 + a743364 commit ec8f2bb

File tree

4 files changed

+221
-143
lines changed

4 files changed

+221
-143
lines changed

vega/build_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ def _build_main_config(self, fit_type, fit_info, parameters, git_hash):
392392
config['data sets']['zeff'] = str(self.zeff_in)
393393
corr_paths = [str(path) for path in self.corr_paths]
394394
config['data sets']['ini files'] = ' '.join(corr_paths)
395+
if 'global_cov_file' in fit_info:
396+
config['data sets']['global-cov-file'] = fit_info.get('global_cov_file')
395397

396398
# Write the scale parameters functions
397399
config['cosmo-fit type'] = {}

vega/data.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from scipy import sparse
55
from scipy.sparse import csr_matrix
66

7-
from vega.utils import find_file
7+
from vega.utils import find_file, compute_masked_invcov, compute_log_cov_det
88
from vega.coordinates import Coordinates
99

1010
BLINDING_STRATEGIES = ['desi_m2', 'desi_y1']
@@ -159,7 +159,8 @@ def inv_masked_cov(self):
159159
"""
160160
if self._inv_masked_cov is None:
161161
# Compute inverse of the covariance matrix
162-
self.compute_masked_invcov()
162+
self._inv_masked_cov = compute_masked_invcov(
163+
self.cov_mat, self.data_mask, self.invert_full_cov)
163164

164165
return self._inv_masked_cov
165166

@@ -173,13 +174,7 @@ def log_cov_det(self):
173174
Logarithm of the determinant of the covariance matrix
174175
"""
175176
if self._log_cov_det is None:
176-
# Compute the log determinant using and LDL^T decomposition
177-
# |C| = Product of Diagonal components of D
178-
masked_cov = self.cov_mat[:, self.data_mask]
179-
masked_cov = masked_cov[self.data_mask, :]
180-
_, d, __ = linalg.ldl(masked_cov)
181-
self._log_cov_det = np.log(d.diagonal()).sum()
182-
assert isinstance(self.log_cov_det, float)
177+
self._log_cov_det = compute_log_cov_det(self.cov_mat, self.data_mask)
183178

184179
return self._log_cov_det
185180

@@ -619,32 +614,3 @@ def create_monte_carlo(self, fiducial_model, scale=None, seed=None, forecast=Fal
619614
self.masked_mc_mock = self.mc_mock[self.data_mask]
620615

621616
return self.mc_mock
622-
623-
def compute_masked_invcov(self):
624-
"""Compute the masked inverse of the covariance matrix.
625-
"""
626-
masked_cov = self.cov_mat[:, self.data_mask]
627-
masked_cov = masked_cov[self.data_mask, :]
628-
629-
try:
630-
linalg.cholesky(self.cov_mat)
631-
print('LOG: Full matrix is positive definite')
632-
except linalg.LinAlgError:
633-
if self.invert_full_cov:
634-
raise ValueError('Full matrix is not positive definite. '
635-
'Use invert-full-cov = False to work with the masked covariance')
636-
else:
637-
print('WARNING: Full matrix is not positive definite')
638-
639-
try:
640-
linalg.cholesky(masked_cov)
641-
print('LOG: Reduced matrix is positive definite')
642-
except linalg.LinAlgError:
643-
print('WARNING: Reduced matrix is not positive definite')
644-
645-
if self.invert_full_cov:
646-
inv_cov = linalg.inv(self.cov_mat)
647-
self._inv_masked_cov = inv_cov[:, self.data_mask]
648-
self._inv_masked_cov = self._inv_masked_cov[self.data_mask, :]
649-
else:
650-
self._inv_masked_cov = linalg.inv(masked_cov)

vega/utils.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,66 @@ def find_file(path):
228228
raise RuntimeError('The path/file does not exists: ', input_path)
229229

230230

231+
def compute_masked_invcov(cov_mat, data_mask, invert_full_cov=False):
232+
"""Compute the masked inverse of the covariance matrix
233+
234+
Parameters
235+
----------
236+
cov_mat : Array
237+
Covariance matrix
238+
data_mask : Array
239+
Mask of the data
240+
invert_full_cov : bool, optional
241+
Flag to invert the full covariance matrix, by default False
242+
"""
243+
masked_cov = cov_mat[:, data_mask]
244+
masked_cov = masked_cov[data_mask, :]
245+
246+
try:
247+
np.linalg.cholesky(cov_mat)
248+
print('LOG: Full matrix is positive definite')
249+
except np.linalg.LinAlgError:
250+
if invert_full_cov:
251+
raise ValueError('Full matrix is not positive definite. '
252+
'Use invert-full-cov = False to work with the masked covariance')
253+
else:
254+
print('WARNING: Full matrix is not positive definite')
255+
256+
try:
257+
np.linalg.cholesky(masked_cov)
258+
print('LOG: Reduced matrix is positive definite')
259+
except np.linalg.LinAlgError:
260+
print('WARNING: Reduced matrix is not positive definite')
261+
262+
if invert_full_cov:
263+
inv_cov = np.linalg.inv(cov_mat)
264+
inv_masked_cov = inv_cov[:, data_mask]
265+
inv_masked_cov = inv_masked_cov[data_mask, :]
266+
else:
267+
inv_masked_cov = np.linalg.inv(masked_cov)
268+
269+
return inv_masked_cov
270+
271+
272+
def compute_log_cov_det(cov_mat, data_mask):
273+
"""Compute the log of the determinant of the covariance matrix
274+
275+
Parameters
276+
----------
277+
cov_mat : Array
278+
Covariance matrix
279+
data_mask : Array
280+
Mask of the data
281+
282+
Returns
283+
-------
284+
float
285+
Log of the determinant of the covariance matrix
286+
"""
287+
masked_cov = cov_mat[:, data_mask]
288+
masked_cov = masked_cov[data_mask, :]
289+
return np.linalg.slogdet(masked_cov)[1]
290+
291+
231292
class VegaBoundsError(Exception):
232293
pass

0 commit comments

Comments
 (0)