Skip to content

Commit ef41108

Browse files
authored
Merge pull request #51 from JesseLivezey/eig_logger
rectify check moved to logger.
2 parents f68b40a + 7f5fb78 commit ef41108

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

dca/cov_util.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import numpy as np
23
import scipy as sp
34
import collections
@@ -9,6 +10,9 @@
910
from sklearn.utils import check_random_state
1011

1112

13+
logging.basicConfig()
14+
15+
1216
def form_lag_matrix(X, T, stride=1, stride_tricks=True, rng=None, writeable=False):
1317
"""Form the data matrix with `T` lags.
1418
@@ -69,7 +73,7 @@ def form_lag_matrix(X, T, stride=1, stride_tricks=True, rng=None, writeable=Fals
6973
return X_with_lags
7074

7175

72-
def rectify_spectrum(cov, epsilon=1e-6, verbose=False):
76+
def rectify_spectrum(cov, epsilon=1e-6, logger=None):
7377
"""Rectify the spectrum of a covariance matrix.
7478
7579
Parameters
@@ -81,11 +85,13 @@ def rectify_spectrum(cov, epsilon=1e-6, verbose=False):
8185
verbose : bool
8286
Whethere to print when the spectrum needs to be rectified.
8387
"""
84-
min_eig = np.min(sp.linalg.eigvalsh(cov))
85-
if min_eig < 0:
86-
cov += (-min_eig + epsilon) * np.eye(cov.shape[0])
87-
if verbose:
88-
print("Warning: non-PSD matrix (had to increase eigenvalues)")
88+
eigvals = sp.linalg.eigvalsh(cov)
89+
n_neg = np.sum(eigvals <= 0.)
90+
if n_neg > 0:
91+
cov += (-np.min(eigvals) + epsilon) * np.eye(cov.shape[0])
92+
if logger is not None:
93+
string = 'Non-PSD matrix, {} of {} eigenvalues were not positive.'
94+
logger.info(string.format(n_neg, eigvals.size))
8995

9096

9197
def toeplitzify(cov, T, N, symmetrize=True):
@@ -171,7 +177,7 @@ def calc_chunked_cov(X, T, stride, chunks, cov_est=None, rng=None, stride_tricks
171177

172178
def calc_cross_cov_mats_from_data(X, T, mean=None, chunks=None, stride=1,
173179
rng=None, regularization=None, reg_ops=None,
174-
stride_tricks=True):
180+
stride_tricks=True, logger=None):
175181
"""Compute the N-by-N cross-covariance matrix, where N is the data dimensionality,
176182
for each time lag up to T-1.
177183
@@ -271,7 +277,7 @@ def calc_cross_cov_mats_from_data(X, T, mean=None, chunks=None, stride=1,
271277
else:
272278
raise ValueError
273279

274-
rectify_spectrum(cov_est, verbose=True)
280+
rectify_spectrum(cov_est, logger=logger)
275281
cross_cov_mats = calc_cross_cov_mats_from_cov(cov_est, T, N)
276282
return cross_cov_mats
277283

dca/dca.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ def estimate_cross_covariance(self, X, T=None, regularization=None, reg_ops=None
308308
stride=self.stride,
309309
rng=self.rng,
310310
regularization=regularization,
311-
reg_ops=reg_ops)
311+
reg_ops=reg_ops,
312+
logger=self._logger)
312313
self.cross_covs = torch.tensor(cross_covs, device=self.device, dtype=self.dtype)
313314
delta_time = round((time.time() - start) / 60., 1)
314315
self._logger.info('Cross covariance estimate took {:0.1f} minutes.'.format(delta_time))

0 commit comments

Comments
 (0)