Skip to content

Commit

Permalink
Merge pull request #597 from joergbuchwald/loguniform
Browse files Browse the repository at this point in the history
loguniform scaling
  • Loading branch information
ConnectedSystems committed Nov 14, 2023
2 parents fef0cd6 + cfe27fe commit a563401
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/user_guide/advanced.rst
Expand Up @@ -102,6 +102,7 @@ parameter bounds but sample-specific metadata.

* unif: uniform distribution
e.g. :code:`[-np.pi, np.pi]` defines the lower and upper bounds
* logunif: logarithmic uniform with lower and upper bounds
* triang: triangular with lower and upper bounds, as well as
location of peak
The location of peak is in percentage of width
Expand All @@ -110,6 +111,7 @@ parameter bounds but sample-specific metadata.
A soon-to-be deprecated two-value format assumes the lower bound to be 0
e.g. :code:`[3, 0.5]` assumes 0 to 3, with a peak at 1.5
* norm: normal distribution with mean and standard deviation
* truncnorm: truncated normal distribution with upper and lower bounds, mean and standard deviation
* lognorm: lognormal with ln-space mean and standard deviation


Expand Down
4 changes: 4 additions & 0 deletions src/SALib/util/__init__.py
Expand Up @@ -134,6 +134,7 @@ def _nonuniform_scale_samples(params, bounds, dists):
dists : list
list of distributions, one for each parameter
unif: uniform with lower and upper bounds
logunif: logarithmic uniform with lower and upper bounds
triang: triangular with lower and upper bounds, as well as
location of peak
The location of peak is in percentage of width
Expand Down Expand Up @@ -205,6 +206,9 @@ def _nonuniform_scale_samples(params, bounds, dists):
else:
conv_params[:, i] = params[:, i] * (b2 - b1) + b1

elif dists[i] == "logunif":
conv_params[:, i] = sp.stats.loguniform.ppf(params[:, i], a=b1, b=b2)

elif dists[i] == "norm":
if b2 <= 0:
raise ValueError("""Normal distribution: stdev must be > 0""")
Expand Down
21 changes: 21 additions & 0 deletions tests/test_util.py
Expand Up @@ -2,6 +2,7 @@
from numpy.testing import assert_equal, assert_allclose

import numpy as np
from scipy import stats
import pytest

from SALib.util import (
Expand Down Expand Up @@ -203,3 +204,23 @@ def test_nonuniform_scale_samples_truncnorm():
]
)
np.testing.assert_allclose(actual, expected)


def test_nonuniform_scale_samples_logunif():
"""
Test the rescaling of samples for logarithmic uniform distribution
"""
problem = {
"num_vars": 1,
"dists": ["logunif"],
"bounds": [[1e-22, 1e-18]],
"names": ["x1"],
}
actual = latin.sample(problem, 10000)
rng = np.random.default_rng()
x = stats.loguniform.rvs(size=100000, a=1e-22, b=1e-18, random_state=rng)
res = stats.cramervonmises_2samp(actual[:, 0], x)
zero_hypothesis_rejected = True
if res.pvalue > 0.05:
zero_hypothesis_rejected = False
assert zero_hypothesis_rejected is False

0 comments on commit a563401

Please sign in to comment.