Algorithmcore
Saltelli sampling scheme
Also called Saltelli estimator.
Estimates all first order and total Sobol indices from two sample matrices and their column swaps, at N(d + 2) model runs, or N(2d + 2) with second order indices.
- Time
- O(N d F + N d^2) worst
- Space
- O(N d) extra
- Where
- F = the cost of one model run; N = the number of rows of each sample matrix; d = the number of inputs
- Model
- Real RAM
- Properties
- randomizednot exactnot iterativerandomized
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Andrea Saltelli, Ilya M. Sobol, Michiel J. W. Jansen, Toshimitsu Homma, 2002
The Saltelli sampling scheme estimates all first order and all total Sobol indices of a model with independent inputs from one set of model runs. It draws two independent sample matrices and with rows, and builds more matrices : the matrix with its column taken from . Two rows that share all inputs but tell how much the output moves with alone, and two rows that share only tell how much of the output is fixed by . Averages of products and squared differences of the outputs then give unbiased estimates of the conditional variances in the definitions. The scheme is a design of model runs. It is not the Sobol sequence, a low discrepancy point set by the same author, although the rows of and are often taken from one, which turns the plain Monte Carlo estimate into quasi-Monte Carlo integration.
In plain words
You want to know which inputs of a model matter most. A simple way is slow: fix one input, vary all the others many times, and repeat for many values of the fixed input. The Saltelli scheme gets the same numbers with far fewer runs.
Make two random tables of inputs. Then, for each input, make a mixed table: the first table with that one column copied from the second. Run the model on every row of every table. If changing one column changes the output a lot, the input matters a lot in total. If the second table and the mixed table, which share only that column, give outputs that rise and fall together, the input matters a lot on its own. A few averages turn these comparisons into shares of the output spread.
The problem
The input is a model with independent inputs of known laws, and . The output is estimates of the first order and the total indices
where is the vector of all inputs except . The cost is the number of model runs; everything else is cheap.
The algorithm
SALTELLI(f, d, N, DRAW)- 1
A, B ← two independent N × d matrices; each row is a draw of ξ by DRAW - 2
y_A ← f(rows of A), y_B ← f(rows of B) ▷ 2N runs - 3
D ← sample variance of the 2N values y_A, y_B - 4
for i = 1..d do - 5
AB_i ← A with its column i replaced by column i of B - 6
y_ABi ← f(rows of AB_i) ▷ N runs - 7
t1_j ← y_B[j] (y_ABi[j] - y_A[j]) for j = 1..N - 8
t2_j ← (y_A[j] - y_ABi[j])^2 / 2 for j = 1..N - 9
S_i ← mean(t1) / D, S_Ti ← mean(t2) / D - 10
return S_1..S_d, S_T1..S_Td and the run count N(d + 2)
Line L5 is the column swap. A row of shares with the same row of and only with the same row of . Line L7 is the first order estimator of Saltelli et al. (2010): given , the outputs of and are independent with the common mean , so the mean of their product is , and subtracting removes . Line L8 is the total estimator of Jansen (1999): given , the outputs of and are two independent draws, and half the mean squared difference of two such draws is the conditional variance. Line L3 uses all independent outputs for the denominator.
Example
The Ishigami function with inputs uniform on is a standard test. Its exact values are , and
| input | exact | exact |
|---|---|---|
| 0.3139 | 0.5576 | |
| 0.4424 | 0.4424 | |
| 0 | 0.2437 |
They follow from , and the interaction . The input has no effect alone, but acts through its product with ; has no interaction.
A run with random.Random(2002) gives (a measurement):
| runs | |||
|---|---|---|---|
| 1000 | 5000 | 0.3074, 0.4637, -0.0141 | 0.5011, 0.4624, 0.2546 |
| 10000 | 50000 | 0.3092, 0.4503, 0.0118 | 0.5562, 0.4425, 0.2414 |
At the standard errors of the first order estimates are about 0.0145, 0.0124 and 0.0112, and those of the total estimates about 0.0151, 0.0050 and 0.0049, so all six estimates are within one or two standard errors of the exact values. The estimate at is a negative estimate of a zero index, which is a normal sampling error. The gap shows the interaction of at both sizes.
Correctness
The claim is that the means in line L9 are unbiased for the numerators and , that is unbiased for the variance, and that the ratios converge almost surely to and as . The key fact is that a row of has the law of , because the inputs are independent; it shares exactly the right inputs with the rows of and , and the two conditional independence arguments above then give the two numerators. The strong law of large numbers gives convergence, and the central limit theorem gives standard errors of order . The full proof is in the proof file. The ratios themselves have a bias of order .
Proof: The Saltelli estimators of the first order and total variances are unbiasedfull
We assume exact arithmetic and exact random numbers. The input has independent components, and is square integrable with . For a set of indices, is the vector of the components in , and is the set of all indices except . The Sobol indices are and with
One row of the sample matrices gives two independent draws and of and, for each , the mixed point : the row of with its component taken from . Different rows are independent.
Lemma 1 (a mixed point has the input law). has the law of . The pair shares the component and nothing else, and the pair shares all components except .
Proof. The components of are for and . They are independent, because they come from different components or from different draws, and each has the law of . As the components of are independent, the joint law is the law of . The other two claims hold by construction.
Lemma 2 (the first order numerator). .
Proof. Let . Given , the values and depend on the independent vectors and , so they are conditionally independent with the same conditional mean . So . The draws and are independent, so . The difference is .
Lemma 3 (the total numerator of Jansen). .
Proof. Given , the values and depend on the independent components and , which have the same law. So they are conditionally independent and identically distributed with the variance . For two such variables , . Take the expectation over .
Theorem (unbiased numerators, consistent indices). Lines L7 and L8 give the terms and of rows . Their means and are unbiased for and . Line L3 gives an unbiased estimate of . As , and almost surely.
Proof. The rows are independent, so the means are unbiased by Lemmas 2 and 3. The values and are independent draws of , so their sample variance with the divisor is unbiased for . The terms of each row are independent and identically distributed with a finite mean (the products and squares of square integrable variables are integrable), so the strong law of large numbers gives almost sure convergence of each mean. A ratio of two convergent sequences converges to the ratio of the limits when the limit of the denominator is .
The ratios are not unbiased for finite , because the expectation of a ratio is not the ratio of the expectations. The bias is of order , smaller than the standard error of order .
Corollary (an input that the model ignores). If does not depend on , then for every row, so both estimates of input are exactly zero, for every .
Corollary (the run count). The model runs on the rows of , and the mixed matrices, which is runs. All first order and total estimates use these same runs.
Error bars. If is square integrable, the terms and have finite variance, and by the central limit theorem is approximately normal with the standard error , where is the sample standard deviation of the terms. The code returns this standard error divided by , and a relative standard error of . The tests compare the estimates with the exact values within four times the sum of the two parts. These error bars are asymptotic, not bounds.
What is not proved. Lemma 1 needs independent inputs. If the components of are dependent, a mixed point does not have the law of , the model is evaluated at inputs that the real system cannot take, and the estimates are not estimates of conditional variances.
Complexity
The scheme makes runs in line L2 and in each of the passes of line L6, which is runs, or time for a run cost . Building each copies rows of length , which is arithmetic in the real RAM model; a version that changes one entry of a row in place needs only . Lines L7 to L9 cost per input. The matrices and take space, and the outputs per pass. A double loop that estimates each by an inner and an outer sample needs runs per input instead.
Properties
- Unbiased numerators. Both numerators are unbiased for every ; the indices are ratios and are only consistent.
- Exact zeros. If does not depend on , then row by row, and both estimates of input are exactly 0.
- One design for all indices. The same runs give all indices; adding an input costs more runs.
- Randomized. The estimates are random; negative estimates of small indices and first order estimates above the total estimate happen at small .
- Independent inputs only. The mixed rows are draws of only if the inputs are independent.
Variants and improvements
- Second order indices. Add the matrices , the matrix with its column from . A row of and the same row of share exactly and , so the mean of minus estimates , and subtracting and gives . This costs runs for all first order, second order and total indices (Saltelli 2002).
- Other estimators. Sobol's original first order estimator is the mean of the products of two outputs that share only , minus the squared sample mean; it loses precision when is large against the standard deviation. Other total estimators exist, for example that of Homma and Saltelli; Saltelli et al. (2010) compare several and recommend Jansen's.
- Quasi-random rows. Take the rows of and from one -dimensional Sobol sequence, the first coordinates for and the last for . For smooth models the error then falls faster than .
- Error bars by bootstrap. Resample the rows with replacement and recompute the indices, to get confidence intervals that include the error of .
- Surrogate models. For an expensive model, compute the indices of a polynomial chaos expansion of it instead; they follow from its coefficients without sampling.
When to use it
Use the Saltelli scheme when you need quantitative first order and total indices with error bars, the inputs are independent, and you can afford runs with in the hundreds to thousands. Use the Morris method first when is large and runs are expensive, to screen out the inputs that do not matter. Use the Fourier amplitude sensitivity test when the model is smooth and the cost per index must be lower, and a bias without error bars is acceptable. Use a polynomial chaos expansion when the model is smooth in few inputs and a surrogate fits it well. For dependent inputs, none of these variance splits holds; use a method built for dependence, for example indices based on the Shapley value.
Pitfalls
- Dependent inputs. Line L5 builds rows with from one draw and from another. With dependent inputs these rows can be inputs that the real system never takes, and the estimates are not conditional variances. Transform to independent inputs first, if the model allows it.
- The variance of one matrix. Line L3 can use alone, but then comes from values instead of , and its sampling error is larger at no saving in runs. Use the pooled values.
- The original first order estimator with a large mean. Replacing line L7 by the mean of minus the squared sample mean subtracts two large numbers when is large, and the estimate loses digits. Line L7 subtracts inside the sum, which is robust to a shift of .
- Swapping the roles of A and B. Line L7 pairs with (shared ) and line L8 pairs it with (shared ). If the matrix is built as with column from , the two pairings must swap too, or the first order estimator estimates the total one and the other way round.
- Reading noise as a result. A small negative , or , is sampling error at small . Report the indices with their standard errors.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/saltelli-sampling-scheme/. The authors are listed in prov/SEW99Y.jsonl.
"""First order and total Sobol indices by the Saltelli sampling scheme.
Two independent N by d sample matrices A and B give d more matrices AB_i:
the matrix A with its column i taken from B. The model runs on all rows of A,
B and AB_1, ..., AB_d, which is N (d + 2) runs. The estimators are those of
Saltelli et al. 2010: the first order numerator
V_i = (1/N) sum_j f(B)_j (f(AB_i)_j - f(A)_j)
and the total numerator of Jansen 1999
VT_i = (1/(2N)) sum_j (f(A)_j - f(AB_i)_j)^2,
both divided by the sample variance of the 2N values f(A) and f(B).
The inputs must be independent. The caller gives the map from the unit cube
to the input law, one coordinate at a time.
"""
import math
import random
from typing import Callable, Sequence
Model = Callable[[Sequence[float]], float]
def ishigami(x: Sequence[float], a: float = 7.0, b: float = 0.1) -> float:
"""The Ishigami function sin x1 + a sin^2 x2 + b x3^4 sin x1, inputs uniform on [-pi, pi]^3."""
return math.sin(x[0]) + a * math.sin(x[1]) ** 2 + b * x[2] ** 4 * math.sin(x[0])
def ishigami_exact(a: float = 7.0, b: float = 0.1) -> dict:
"""The exact mean, variance, first order and total indices of the Ishigami function."""
pi = math.pi
v1 = 0.5 * (1 + b * pi**4 / 5) ** 2
v2 = a * a / 8
v13 = b * b * pi**8 * (1 / 18 - 1 / 50)
v = v1 + v2 + v13
return {
"mean": a / 2,
"variance": v,
"S": [v1 / v, v2 / v, 0.0],
"ST": [(v1 + v13) / v, v2 / v, v13 / v],
}
def saltelli(f: Model, d: int, n: int, rng: random.Random, to_input: Callable[[float, int], float]) -> dict:
"""Estimate S_i and ST_i for i = 1..d with N (d + 2) model runs.
to_input(u, i) maps a uniform number u in [0, 1) to coordinate i of the input.
Returns the indices, their standard errors (with the variance taken as exact)
and the number of model runs.
"""
a = [[to_input(rng.random(), i) for i in range(d)] for _ in range(n)] # @line L1
b = [[to_input(rng.random(), i) for i in range(d)] for _ in range(n)]
fa = [f(row) for row in a] # @line L2
fb = [f(row) for row in b]
runs = 2 * n
both = fa + fb
mean = sum(both) / (2 * n)
var = sum((y - mean) ** 2 for y in both) / (2 * n - 1) # @line L3
s, st, s_err, st_err = [], [], [], []
for i in range(d): # @line L4
fab = []
for j in range(n):
row = list(a[j]) # @line L5
row[i] = b[j][i]
fab.append(f(row)) # @line L6
runs += n
t1 = [fb[j] * (fab[j] - fa[j]) for j in range(n)] # @line L7
t2 = [0.5 * (fa[j] - fab[j]) ** 2 for j in range(n)] # @line L8
s.append(sum(t1) / n / var) # @line L9
st.append(sum(t2) / n / var)
s_err.append(_std_error(t1) / var)
st_err.append(_std_error(t2) / var)
sq = [((y - mean) ** 2 - var) ** 2 for y in both]
var_rel_err = math.sqrt(sum(sq) / (2 * n - 1) / (2 * n)) / var
return {"S": s, "ST": st, "S_err": s_err, "ST_err": st_err, "runs": runs, "variance": var, "variance_rel_err": var_rel_err} # @line L10
def _std_error(t: Sequence[float]) -> float:
n = len(t)
m = sum(t) / n
return math.sqrt(sum((x - m) ** 2 for x in t) / (n - 1) / n)
def brute_force_first_order(f: Model, d: int, i: int, n_outer: int, n_inner: int, rng: random.Random, to_input) -> float:
"""Var(E[Q | x_i]) by a double loop: n_outer values of x_i, n_inner samples of the rest for each.
The inner means carry noise of variance about Var(Q | x_i) / n_inner, which this subtracts.
"""
cond_means, cond_vars = [], []
for _ in range(n_outer):
xi = to_input(rng.random(), i)
ys = []
for _ in range(n_inner):
x = [to_input(rng.random(), k) for k in range(d)]
x[i] = xi
ys.append(f(x))
m = sum(ys) / n_inner
cond_means.append(m)
cond_vars.append(sum((y - m) ** 2 for y in ys) / (n_inner - 1))
mm = sum(cond_means) / n_outer
var_of_means = sum((m - mm) ** 2 for m in cond_means) / (n_outer - 1)
return var_of_means - sum(cond_vars) / n_outer / n_inner
test_saltelli_sampling_scheme.py 64 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/saltelli-sampling-scheme/. The authors are listed in prov/SEW99Y.jsonl.
import math
import random
from saltelli_sampling_scheme import brute_force_first_order, ishigami, ishigami_exact, saltelli
def unif_pi(u, i):
return -math.pi + 2 * math.pi * u
def unit(u, i):
return u
def tolerance(res, key, i):
"""Four standard errors of the numerator, plus four of the variance in the denominator."""
return 4 * (res[key + "_err"][i] + abs(res[key][i]) * res["variance_rel_err"])
def test_ishigami_exact_values():
ex = ishigami_exact()
assert abs(ex["variance"] - 13.84459) < 1e-5
assert [round(x, 4) for x in ex["S"]] == [0.3139, 0.4424, 0.0]
assert [round(x, 4) for x in ex["ST"]] == [0.5576, 0.4424, 0.2437]
def test_ishigami_indices_within_four_standard_errors():
ex = ishigami_exact()
res = saltelli(ishigami, 3, 10000, random.Random(2002), unif_pi)
assert res["runs"] == 10000 * (3 + 2)
for i in range(3):
assert abs(res["S"][i] - ex["S"][i]) < tolerance(res, "S", i)
assert abs(res["ST"][i] - ex["ST"][i]) < tolerance(res, "ST", i)
# x3 acts only through its interaction with x1
assert res["ST"][2] - res["S"][2] > 0.15
def test_linear_model_has_equal_first_order_and_total_indices():
c = [1.0, 2.0, 3.0]
f = lambda x: sum(ci * xi for ci, xi in zip(c, x)) # noqa: E731
res = saltelli(f, 3, 5000, random.Random(5), unit)
total = sum(ci * ci for ci in c)
for i in range(3):
exact = c[i] ** 2 / total
assert abs(res["S"][i] - exact) < tolerance(res, "S", i)
assert abs(res["ST"][i] - exact) < tolerance(res, "ST", i)
def test_an_input_that_the_model_ignores_gets_exactly_zero():
f = lambda x: x[0] ** 2 + math.sin(x[0]) # noqa: E731
res = saltelli(f, 3, 500, random.Random(8), unit)
assert res["S"][1] == 0.0 and res["ST"][1] == 0.0
assert res["S"][2] == 0.0 and res["ST"][2] == 0.0
assert abs(res["ST"][0] - 1.0) < tolerance(res, "ST", 0)
def test_first_order_agrees_with_the_double_loop_estimate():
ex = ishigami_exact()
res = saltelli(ishigami, 3, 4000, random.Random(13), unif_pi)
v1 = brute_force_first_order(ishigami, 3, 0, 300, 100, random.Random(17), unif_pi)
s1_brute = v1 / ex["variance"]
assert abs(res["S"][0] - s1_brute) < 0.08
History
Sobol published the Monte Carlo estimate of first order indices with two independent samples in Russian in 1990; the English translation appeared in 1993. Homma and Saltelli introduced the total sensitivity index in 1996, with its estimate. Jansen gave the estimator of the total variance from squared differences in 1999. Saltelli showed in 2002 how one design of runs gives all first order and total indices, and runs also all second order ones. Saltelli and coauthors compared the estimators in 2010 and recommended the forms of lines L7 and L8. Ishigami and Homma had proposed the test function of the example in 1990.
References
- Andrea Saltelli, Making best use of model evaluations to compute sensitivity indices, Computer Physics Communications 145(2), 2002, 280-297, the design with the matrices A, B and the column swaps, N(d + 2) runs for first order and total indices and N(2d + 2) runs with second order indices
- Andrea Saltelli, Paola Annoni, Ivano Azzini, Francesca Campolongo, Marco Ratto and Stefano Tarantola, Variance based sensitivity analysis of model output. Design and estimator for the total sensitivity index, Computer Physics Communications 181(2), 2010, 259-270, the comparison of estimators, the first order estimator f(B)(f(AB_i) - f(A)) and the recommendation of the Jansen total estimator
- Michiel J. W. Jansen, Analysis of variance designs for model output, Computer Physics Communications 117(1-2), 1999, 35-43, the estimator of the total variance from squared differences of runs that differ in one input
- Toshimitsu Homma and Andrea Saltelli, Importance measures in global sensitivity analysis of nonlinear models, Reliability Engineering and System Safety 52(1), 1996, 1-17, the total sensitivity index and its Monte Carlo estimate
- Ilya M. Sobol, Sensitivity estimates for nonlinear mathematical models, Mathematical Modeling and Computational Experiment 1(4), 1993, 407-414, the Monte Carlo estimate of the first order indices with two independent samples (translation of the 1990 Russian paper)
- Andrea Saltelli, Marco Ratto, Terry Andres, Francesca Campolongo, Jessica Cariboni, Debora Gatelli, Michaela Saisana and Stefano Tarantola, Global Sensitivity Analysis, The Primer, Wiley, 2008, Chapter 4 (variance-based methods and the computation of first order and total indices)
- Tsutomu Ishigami and Toshimitsu Homma, An importance quantification technique in uncertainty analysis for computer models, Proceedings of the First International Symposium on Uncertainty Modeling and Analysis (ISUMA 1990), IEEE, 1990, 398-403, the test function sin x1 + a sin^2 x2 + b x3^4 sin x1
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag SEW99Y Edit this page History Report an error Markdown