Skip to content

Commit

Permalink
Merge pull request #589 from SALib/fix-rsa-cvm-587
Browse files Browse the repository at this point in the history
Switch to Cramer-von Mises test for RSA, remove normalization (fix #587)
  • Loading branch information
ConnectedSystems committed Oct 9, 2023
2 parents 0d6d155 + 5db65f0 commit 4c47556
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Included methods

* PAWN (`Pianosi and Wagener 2018 <https://dx.doi.org/10.1016/j.envsoft.2018.07.019>`__, `Pianosi and Wagener 2015 <https://doi.org/10.1016/j.envsoft.2015.01.004>`__)

* Regional Sensitivity Analysis (based on `Saltelli et al. 2008 <https://dx.doi.org/10.1002/9780470725184>`__, `Pianosi et al., 2016 <https://dx.doi.org/10.1016/j.envsoft.2016.02.008>`__)
* Regional Sensitivity Analysis (based on `Hornberger and Spear, 1981 <https://www.osti.gov/biblio/6396608-approach-preliminary-analysis-environmental-systems>`__, `Saltelli et al. 2008 <https://dx.doi.org/10.1002/9780470725184>`__, `Pianosi et al., 2016 <https://dx.doi.org/10.1016/j.envsoft.2016.02.008>`__)


**Contributing:** see `here <CONTRIBUTING.md>`__
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Supported Methods
* PAWN
(`Pianosi and Wagener 2018 <https://dx.doi.org/10.1016/j.envsoft.2018.07.019>`__, `Pianosi and Wagener 2015 <https://doi.org/10.1016/j.envsoft.2015.01.004>`__)
* Regional Sensitivity Analysis
(based on `Saltelli et al. 2008 <https://dx.doi.org/10.1002/9780470725184>`__, `Pianosi et al., 2016 <https://dx.doi.org/10.1016/j.envsoft.2016.02.008>`__)
(based on `Hornberger and Spear, 1981 <https://www.osti.gov/biblio/6396608-approach-preliminary-analysis-environmental-systems>`__, `Saltelli et al. 2008 <https://dx.doi.org/10.1002/9780470725184>`__, `Pianosi et al., 2016 <https://dx.doi.org/10.1016/j.envsoft.2016.02.008>`__)


Getting Started
Expand Down
52 changes: 29 additions & 23 deletions src/SALib/analyze/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
import pandas as pd
from scipy.stats import anderson_ksamp
from scipy.stats import cramervonmises_2samp

from . import common_args
from ..util import read_param_file, ResultDict, extract_group_names, _check_groups
Expand Down Expand Up @@ -43,10 +43,11 @@ def analyze(
(Y|b_{\\sim i})`. This aids in answering the question "where in factor space are
outputs most sensitive to?"
The :math:`k`-sample Anderson-Darling test is used to compare distributions.
Results of the analysis are normalized so that values will be :math:`\\in [0, 1]`,
and indicate relative sensitivity across factor/output space. Larger values
indicate greater dissimilarity (thus, sensitivity).
The two-sample Cramér-von Mises (CvM) test is used to compare distributions.
Results of the analysis indicate sensitivity across factor/output space. As the
Cramér-von Mises criterion ranges from 0 to :math:`\\infty`, a value of zero will
indicates the two distributions being compared are identical, with larger values
indicating greater differences.
Notes
-----
Expand Down Expand Up @@ -86,20 +87,25 @@ def analyze(
References
----------
1. Pianosi, F., K. Beven, J. Freer, J. W. Hall, J. Rougier, D. B. Stephenson, and
T. Wagener. 2016.
Sensitivity analysis of environmental models:
A systematic review with practical workflow.
Environmental Modelling & Software 79:214-232.
https://dx.doi.org/10.1016/j.envsoft.2016.02.008
2. Saltelli, A., M. Ratto, T. Andres, F. Campolongo, J. Cariboni, D. Gatelli,
M. Saisana, and S. Tarantola. 2008.
Global Sensitivity Analysis: The Primer.
Wiley, West Sussex, U.K.
https://dx.doi.org/10.1002/9780470725184
Accessible at:
http://www.andreasaltelli.eu/file/repository/Primer_Corrected_2022.pdf
1. Hornberger, G. M., and R. C. Spear. 1981.
Approach to the preliminary analysis of environmental systems.
Journal of Environmental Management 12:1.
https://www.osti.gov/biblio/6396608-approach-preliminary-analysis-environmental-systems
2. Pianosi, F., K. Beven, J. Freer, J. W. Hall, J. Rougier, D. B. Stephenson, and
T. Wagener. 2016.
Sensitivity analysis of environmental models:
A systematic review with practical workflow.
Environmental Modelling & Software 79:214-232.
https://dx.doi.org/10.1016/j.envsoft.2016.02.008
3. Saltelli, A., M. Ratto, T. Andres, F. Campolongo, J. Cariboni, D. Gatelli,
M. Saisana, and S. Tarantola. 2008.
Global Sensitivity Analysis: The Primer.
Wiley, West Sussex, U.K.
https://dx.doi.org/10.1002/9780470725184
Accessible at:
http://www.andreasaltelli.eu/file/repository/Primer_Corrected_2022.pdf
"""
groups = _check_groups(problem)
if not groups:
Expand Down Expand Up @@ -162,16 +168,16 @@ def rsa(X: np.ndarray, y: np.ndarray, bins: int = 10, target="X") -> np.ndarray:
quants = np.quantile(t_arr, seq)
b = (quants[0] <= t_arr) & (t_arr <= quants[1])
if _has_samples(y, b):
r_s[0, d_i] = anderson_ksamp((m_arr[b], m_arr[~b])).statistic
r_s[0, d_i] = cramervonmises_2samp(m_arr[b], m_arr[~b]).statistic

# Then assess the other bins
for s in range(1, bins):
b = (quants[s] < t_arr) & (t_arr <= quants[s + 1])

if _has_samples(y, b):
r_s[s, d_i] = anderson_ksamp((m_arr[b], m_arr[~b])).statistic
r_s[s, d_i] = cramervonmises_2samp(m_arr[b], m_arr[~b]).statistic

return r_s / np.nanmax(r_s)
return r_s


def _has_samples(y, sel):
Expand Down Expand Up @@ -216,7 +222,7 @@ def plot(self, factors=None):
fig, ax = plt.subplots()

xlabel = f"${self['target']}$ (Percentile)"
ylabel = "Relative $S_{i}$"
ylabel = "$CvM_{i}$"

if factors is None:
factors = slice(None)
Expand Down

0 comments on commit 4c47556

Please sign in to comment.