From b2c4c02a080a2be0472b8a565003e7b230e888c6 Mon Sep 17 00:00:00 2001 From: Blunde1 Date: Tue, 26 Sep 2023 16:59:50 +0200 Subject: [PATCH] Rewrite trans_derrf to intended behaviour --- src/ert/config/gen_kw_config.py | 7 ++++++ tests/unit_tests/config/test_gen_kw_config.py | 5 ---- .../config/test_transfer_functions.py | 23 ++++++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/ert/config/gen_kw_config.py b/src/ert/config/gen_kw_config.py index 32f4c9bacad..881ea2e03a7 100644 --- a/src/ert/config/gen_kw_config.py +++ b/src/ert/config/gen_kw_config.py @@ -427,6 +427,13 @@ def trans_errf(x: float, arg: List[float]) -> float: """ _min, _max, _skew, _width = arg[0], arg[1], arg[2], arg[3] y = 0.5 * (1 + math.erf((x + _skew) / (_width * math.sqrt(2.0)))) + if np.isnan(y): + raise ValueError( + ( + "Output is nan, likely from triplet (x, skewness, width) " + "leading to low/high-probability in normal CDF." + ) + ) return _min + y * (_max - _min) @staticmethod diff --git a/tests/unit_tests/config/test_gen_kw_config.py b/tests/unit_tests/config/test_gen_kw_config.py index 964960e6b06..c0e0f3f31e5 100644 --- a/tests/unit_tests/config/test_gen_kw_config.py +++ b/tests/unit_tests/config/test_gen_kw_config.py @@ -358,11 +358,6 @@ def test_gen_kw_params_parsing(tmpdir, params, error): ("MYNAME ERRF 1 2 0.1 0.1", 0.3, 1.99996832875816688002), ("MYNAME ERRF 1 2 0.1 0.1", 0.7, 1.99999999999999933387), ("MYNAME ERRF 1 2 0.1 0.1", 1.0, 2.00000000000000000000), - ("MYNAME DERRF 10 1 2 0.1 0.1", -1.0, 1.00000000000000000000), - ("MYNAME DERRF 10 1 2 0.1 0.1", 0.0, 1.00000000000000000000), - ("MYNAME DERRF 10 1 2 0.1 0.1", 0.3, 2.00000000000000000000), - ("MYNAME DERRF 10 1 2 0.1 0.1", 0.7, 2.00000000000000000000), - ("MYNAME DERRF 10 1 2 0.1 0.1", 1.0, 2.00000000000000000000), ], ) def test_gen_kw_trans_func(tmpdir, params, xinput, expected): diff --git a/tests/unit_tests/config/test_transfer_functions.py b/tests/unit_tests/config/test_transfer_functions.py index 54d38be6d00..e29cb33f9dc 100644 --- a/tests/unit_tests/config/test_transfer_functions.py +++ b/tests/unit_tests/config/test_transfer_functions.py @@ -1,4 +1,5 @@ import numpy as np +from scipy.stats import norm from hypothesis import given from hypothesis import strategies as st @@ -114,7 +115,7 @@ def test_that_derrf_is_within_bounds(x, arg): st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=2), valid_derrf_parameters(), ) -def test_that_derrf_creates_at_least_steps_number_of_distinct_values(xlist, arg): +def test_that_derrf_creates_at_least_steps_or_less_distinct_values(xlist, arg): """derrf cannot create more than steps distinct values""" res = [] for x in xlist: @@ -122,6 +123,26 @@ def test_that_derrf_creates_at_least_steps_number_of_distinct_values(xlist, arg) assert len(set(res)) <= arg[0] +@given(st.floats(allow_nan=False, allow_infinity=False), valid_derrf_parameters()) +def test_that_derrf_corresponds_scaled_binned_normal_cdf(x, arg): + """Check correspondance to normal cdf with -mu=_skew and sd=_width""" + _steps, _min, _max, _skew, _width = arg + q_values = np.linspace(start=0, stop=1, num=_steps) + q_checks = np.linspace(start=0, stop=1, num=_steps + 1)[1:] + p = norm.cdf(x, loc=-_skew, scale=_width) + expected = q_values[-1] # last + for i in range(_steps - 1): + if p < q_checks[i]: + expected = q_values[i] + break + # scale and ensure ok numerics + expected = _min + expected * (_max - _min) + if expected > _max or expected < _min: + np.clip(expected, _min, _max) + result = TransferFunction.trans_derrf(x, arg) + assert np.isclose(result, expected) + + @given( st.tuples( st.floats(allow_nan=False, allow_infinity=False),