Skip to content

Commit

Permalink
Calc stds_X and Y only once
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeda committed Jan 12, 2024
1 parent 1ac9d52 commit 5307bae
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/iterative_ensemble_smoother/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,10 @@ def compute_cross_covariance_multiplier(
def _cov_to_corr_inplace(
self,
cov_XY: npt.NDArray[np.double],
X: npt.NDArray[np.double],
Y: npt.NDArray[np.double],
stds_X: npt.NDArray[np.double],
stds_Y: npt.NDArray[np.double],
) -> None:
"""Convert a covariance matrix to a correlation matrix in-place."""
assert cov_XY.shape == (X.shape[0], Y.shape[0])

stds_Y = np.std(Y, axis=1, ddof=1)
stds_X = np.std(X, axis=1, ddof=1)

# Divide each element of cov_XY by the corresponding standard deviations
cov_XY /= stds_X[:, np.newaxis]
Expand All @@ -167,15 +163,10 @@ def _cov_to_corr_inplace(
def _corr_to_cov_inplace(
self,
corr_XY: npt.NDArray[np.double],
X: npt.NDArray[np.double],
Y: npt.NDArray[np.double],
stds_X: npt.NDArray[np.double],
stds_Y: npt.NDArray[np.double],
) -> None:
"""Convert a correlation matrix to a covariance matrix in-place."""
assert corr_XY.shape == (X.shape[0], Y.shape[0])

stds_Y = np.std(Y, axis=1, ddof=1)
stds_X = np.std(X, axis=1, ddof=1)

# Multiply each element of corr_XY by the corresponding standard deviations
corr_XY *= stds_X[:, np.newaxis]
corr_XY *= stds_Y[np.newaxis, :]
Expand Down Expand Up @@ -284,14 +275,17 @@ def correlation_threshold(ensemble_size: int) -> float:
cov_XY = empirical_cross_covariance(X, Y)
assert cov_XY.shape == (X.shape[0], Y.shape[0])

corr_XY = self._cov_to_corr_inplace(cov_XY, X, Y)
stds_X = np.std(X, axis=1, ddof=1)
stds_Y = np.std(Y, axis=1, ddof=1)

corr_XY = self._cov_to_corr_inplace(cov_XY, stds_X, stds_Y)

# Determine which elements in the cross covariance matrix that will
# be set to zero
threshold = correlation_threshold(X.shape[1])
significant_corr_XY = np.abs(corr_XY) > threshold

cov_XY = self._corr_to_cov_inplace(corr_XY, X, Y)
cov_XY = self._corr_to_cov_inplace(corr_XY, stds_X, stds_Y)

# Pre-compute the covariance cov(Y, Y) here, and index on it later
if cov_YY is None:
Expand Down

0 comments on commit 5307bae

Please sign in to comment.