Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax assertion on correlation matrix #189

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/iterative_ensemble_smoother/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
features of iterative_ensemble_smoother
"""
import numbers
import warnings
from typing import List, Optional, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -149,9 +150,16 @@ def _correlation_matrix(self, cov_XY, X, Y):
# Compute the correlation matrix from the covariance matrix
corr_XY = (cov_XY / stds_X[:, np.newaxis]) / stds_Y[np.newaxis, :]

# Perform checks
assert corr_XY.max() <= 1
assert corr_XY.min() >= -1
# Perform checks. There appears to be occasional numerical issues in
# the equation. With 2 ensemble members, we get e.g. a max value of
# 1.0000000000016778. We allow some leeway and clip the results.
eps = 1e-8
if not ((corr_XY.max() <= 1 + eps) and (corr_XY.min() >= -1 - eps)):
msg = "Cross-correlation matrix has entries not in [-1, 1]."
msg += f"The min and max values are: {corr_XY.min()} and {corr_XY.max()}"
warnings.warn(msg)

corr_XY = np.clip(corr_XY, a_min=-1, a_max=1)
return corr_XY

def assimilate(self, X, Y, D, alpha, correlation_threshold=None, verbose=False):
Expand Down