Skip to content

Commit

Permalink
Simplify draw_values_uniform and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeda authored Oct 28, 2024
1 parent 1862268 commit 7b913a0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
31 changes: 16 additions & 15 deletions src/fmu/tools/sensitivities/design_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,24 @@ def draw_values_uniform(dist_parameters, numreals, normalscoresamples=None):
Returns:
list of values
"""
distribution = None
status, msg = _check_dist_params_uniform(dist_parameters)
if status:
low = float(dist_parameters[0])
high = float(dist_parameters[1])
uscale = high - low
if normalscoresamples is not None:
values = scipy.stats.uniform.ppf(
scipy.stats.norm.cdf(normalscoresamples), loc=low, scale=uscale
)
if numreals < 0:
raise ValueError("numreal must be a positive integer")

else:
distribution = scipy.stats.uniform(loc=low, scale=uscale)
values = distribution.rvs(size=numreals)
else:
status, msg = _check_dist_params_uniform(dist_parameters)
if not status:
raise ValueError(msg)
return values

low = float(dist_parameters[0])
high = float(dist_parameters[1])
uscale = high - low

if normalscoresamples is not None:
return scipy.stats.uniform.ppf(
scipy.stats.norm.cdf(normalscoresamples), loc=low, scale=uscale
)

distribution = scipy.stats.uniform(loc=low, scale=uscale)
return distribution.rvs(size=numreals)


def draw_values_triangular(dist_parameters, numreals, normalscoresamples=None):
Expand Down
38 changes: 36 additions & 2 deletions tests/sensitivities/test_design_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,49 @@ def test_draw_values_lognormal():
assert all(value > 0 for value in values)


def test_draw_uniform_with_correlation():
rng = np.random.default_rng()
correlation = 0.6
n_samples = 1000

cov_matrix = [[1.0, correlation], [correlation, 1.0]]
normal_scores = rng.multivariate_normal([0, 0], cov_matrix, size=n_samples)

values1 = dists.draw_values_uniform([0, 100], n_samples, normal_scores[:, 0])
values2 = dists.draw_values_uniform([10, 50], n_samples, normal_scores[:, 1])

actual_correlation = np.corrcoef(values1, values2)[0, 1]
assert (actual_correlation - correlation) < 0.1


def test_draw_values_uniform():
"""Test drawing uniform values"""
assert not dists.draw_values_uniform([10, 100], 0).size
values = dists.draw_values_uniform([10, 100], 0)
assert len(values) == 0

values = dists.draw_values_uniform([10, 100], 20)
assert len(values) == 20
assert all(isinstance(value, numbers.Number) for value in values)
assert all(10 <= value <= 100 for value in values)

with pytest.raises(
ValueError,
match="Uniform distribution must have 2 parameters, but had 3 parameters.",
):
values = dists.draw_values_uniform([10, 50, 100], 10)

with pytest.raises(
ValueError, match="Uniform distribution must have dist_param2 >= dist_param1"
):
values = dists.draw_values_uniform([50, 10], 10)

with pytest.raises(
ValueError, match="Parameters for uniform distribution must be numbers."
):
values = dists.draw_values_uniform(["a", 10], 10)

with pytest.raises(ValueError, match="numreal must be a positive integer"):
values = dists.draw_values_uniform([10, 50], -10)


def test_draw_values_triangular():
"""Test drawing triangular values"""
Expand Down

0 comments on commit 7b913a0

Please sign in to comment.