Skip to content

Commit

Permalink
Merge pull request #203 from sdmccabe/merge-cmr-rcmr
Browse files Browse the repository at this point in the history
Merge regularized CM into CM reconstructor
  • Loading branch information
tlarock committed May 22, 2019
2 parents 5685097 + 41e4728 commit 720ae06
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 98 deletions.
2 changes: 0 additions & 2 deletions netrd/reconstruction/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .base import BaseReconstructor
from .random import RandomReconstructor
from .correlation_matrix import CorrelationMatrixReconstructor
from .regularized_correlation_matrix import RegularizedCorrelationMatrixReconstructor
from .partial_correlation_matrix import PartialCorrelationMatrixReconstructor
from .partial_correlation_influence import PartialCorrelationInfluenceReconstructor
from .free_energy_minimization import FreeEnergyMinimizationReconstructor
Expand All @@ -22,7 +21,6 @@
__all__ = [
'RandomReconstructor',
'CorrelationMatrixReconstructor',
'RegularizedCorrelationMatrixReconstructor',
'PartialCorrelationMatrixReconstructor',
'PartialCorrelationInfluenceReconstructor',
'FreeEnergyMinimizationReconstructor',
Expand Down
58 changes: 49 additions & 9 deletions netrd/reconstruction/correlation_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@


class CorrelationMatrixReconstructor(BaseReconstructor):
def fit(self, TS, threshold_type='range', **kwargs):
"""Uses an unregularized form of the precision matrix.
def fit(self, TS, num_eigs=None, threshold_type='range', **kwargs):
"""Uses the correlation matrix.
The results dictionary also stores the raw correlation matrix as
`'weights_matrix'` and the thresholded version of the correlation
matrix as `'thresholded_matrix'`. For details see [1].
If ``num_eigs`` is `None`, perform the reconstruction using the
unregularized correlation matrix. Otherwise, construct a regularized
precision matrix using ``num_eigs`` eigenvectors and eigenvalues of the
correlation matrix. For details on the regularization method, see [1].
The results dictionary also stores the raw correlation matrix
(potentially regularized) as `'weights_matrix'` and the thresholded
version of the correlation matrix as `'thresholded_matrix'`. For
details see [2].
Parameters
----------
TS (np.ndarray)
Array consisting of $L$ observations from $N$ sensors
Array consisting of :math:`L` observations from :math:`N` sensors
num_eigs (int)
The number of eigenvalues to use. (This corresponds to the
amount of regularization.) The number of eigenvalues used must
be less than :math:`N`.
threshold_type (str)
Which thresholding function to use on the matrix of
Expand All @@ -43,16 +54,45 @@ def fit(self, TS, threshold_type='range', **kwargs):
References
----------
[1] https://github.com/valeria-io/visualising_stocks_correlations/blob/master/corr_matrix_viz.ipynb
[1] https://bwlewis.github.io/correlation-regularization/
[2] https://github.com/valeria-io/visualising_stocks_correlations/blob/master/corr_matrix_viz.ipynb
"""

# get the correlation matrix
cor = np.corrcoef(TS)
self.results['weights_matrix'] = cor

if num_eigs:
N = TS.shape[0]
if num_eigs > N:
raise ValueError(
"The number of eigenvalues used must be less "
"than the number of sensors."
)

# get eigenvalues and eigenvectors of the correlation matrix
vals, vecs = np.linalg.eigh(cor)
idx = vals.argsort()[::-1]
vals = vals[idx]
vecs = vecs[:, idx]

# construct the precision matrix and store it
P = (vecs[:, :num_eigs]) @ (
1 / (vals[:num_eigs]).reshape(num_eigs, 1) * (vecs[:, :num_eigs]).T
)
P = P / (
np.sqrt(np.diag(P)).reshape(N, 1) @ np.sqrt(np.diag(P)).reshape(1, N)
)
mat = P
else:
mat = cor

# store the appropriate source matrix
self.results['weights_matrix'] = mat

# threshold the correlation matrix
A = threshold(cor, threshold_type, **kwargs)
A = threshold(mat, threshold_type, **kwargs)
self.results['thresholded_matrix'] = A

# construct the network
Expand Down
87 changes: 0 additions & 87 deletions netrd/reconstruction/regularized_correlation_matrix.py

This file was deleted.

0 comments on commit 720ae06

Please sign in to comment.