Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use randomized sobol sampling #167

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ KernelDensity = "0.6.4"
LinearAlgebra = "1.10"
OrdinaryDiffEq = "6.62"
Parameters = "0.12"
QuasiMonteCarlo = "0.2.3, 0.3"
QuasiMonteCarlo = "0.3.3"
Random = "1.10"
RecursiveArrayTools = "3.2"
SafeTestsets = "0.1"
Expand Down
10 changes: 5 additions & 5 deletions docs/src/tutorials/juliacon21.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ We showcase how to use multiple GSA methods, analyze their results and leverage
perform Global Sensitivity analysis at scale.

```@example lv
using GlobalSensitivity, QuasiMonteCarlo, OrdinaryDiffEq, Statistics, CairoMakie
using GlobalSensitivity, QuasiMonteCarlo, OrdinaryDiffEq, Statistics, CairoMakie, Random

function f(du, u, p, t)
du[1] = p[1] * u[1] - p[2] * u[1] * u[2] #prey
Expand Down Expand Up @@ -54,7 +54,7 @@ fig
```

```@example lv
sobol_sens = gsa(f1, Sobol(), bounds, samples = 500)
sobol_sens = gsa(f1, Sobol(), bounds, samples = 512)
efast_sens = gsa(f1, eFAST(), bounds, samples = 500)
```

Expand Down Expand Up @@ -94,10 +94,10 @@ fig

```@example lv
using QuasiMonteCarlo
samples = 500
samples = 512
lb = [1.0, 1.0, 1.0, 1.0]
ub = [5.0, 5.0, 5.0, 5.0]
sampler = SobolSample()
sampler = SobolSample(; R = QuasiMonteCarlo.OwenScramble(; base = 2, pad = 9, rng = _rng))
A, B = QuasiMonteCarlo.generate_design_matrices(samples, lb, ub, sampler)
sobol_sens_desmat = gsa(f1, Sobol(), A, B)

Expand Down Expand Up @@ -129,7 +129,7 @@ f1 = function (p)
prob1 = remake(prob; p = p)
sol = solve(prob1, Tsit5(); saveat = t)
end
sobol_sens = gsa(f1, Sobol(nboot = 20), bounds, samples = 500)
sobol_sens = gsa(f1, Sobol(nboot = 20), bounds, samples = 512)
fig = Figure(resolution = (600, 400))
ax, hm = CairoMakie.scatter(
fig[1, 1], sobol_sens.S1[1][1, 2:end], label = "Prey", markersize = 4)
Expand Down
6 changes: 3 additions & 3 deletions docs/src/tutorials/parallelized_gsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ scatter(
For the Sobol method, we can similarly do:

```@example ode
m = gsa(f1, Sobol(), [[1, 5], [1, 5], [1, 5], [1, 5]], samples = 1000)
m = gsa(f1, Sobol(), [[1, 5], [1, 5], [1, 5], [1, 5]], samples = 1024)
```

## Direct Use of Design Matrices
Expand All @@ -76,10 +76,10 @@ we use [QuasiMonteCarlo.jl](https://docs.sciml.ai/QuasiMonteCarlo/stable/) to ge
as follows:

```@example ode
samples = 500
samples = 512
lb = [1.0, 1.0, 1.0, 1.0]
ub = [5.0, 5.0, 5.0, 5.0]
sampler = SobolSample()
sampler = SobolSample(; R = QuasiMonteCarlo.OwenScramble(; base = 2, pad = 9, rng = _rng))
A, B = QuasiMonteCarlo.generate_design_matrices(samples, lb, ub, sampler)
```

Expand Down
8 changes: 4 additions & 4 deletions docs/src/tutorials/shapley.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ barplot(
xticklabelrotation = 1,
xticks = (1:54, ["θ$i" for i in 1:54]),
ylabel = "Shapley Indices",
limits = (nothing, (0.0, 0.2)),
),
limits = (nothing, (0.0, 0.2))
)
)
```

Expand Down Expand Up @@ -160,7 +160,7 @@ barplot(
xticklabelrotation = 1,
xticks = (1:54, ["θ$i" for i in 1:54]),
ylabel = "Shapley Indices",
limits = (nothing, (0.0, 0.2)),
),
limits = (nothing, (0.0, 0.2))
)
)
```
5 changes: 5 additions & 0 deletions src/shapley_sensitivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ function gsa(f, method::Shapley, input_distribution::SklarDist; batch = false)
sample_complement = rand(
Copulas.subsetdims(input_distribution, idx_minus), n_outer)

if size(sample_complement, 2) == 1
sample_complement = reshape(
sample_complement, (1, length(sample_complement)))
end

Vaibhavdixit02 marked this conversation as resolved.
Show resolved Hide resolved
for l in 1:n_outer
curr_sample = @view sample_complement[:, l]
# Sampling of the set conditionally to the complementary element
Expand Down
18 changes: 15 additions & 3 deletions src/sobol_sensitivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ by dividing other terms in the variance decomposition by `` Var(Y) ``.
- `:Jansen1999` - [M.J.W. Jansen, 1999, Analysis of variance designs for model output, Computer Physics Communi- cation, 117, 35–43.](https://www.sciencedirect.com/science/article/abs/pii/S0010465598001544)
- `:Janon2014` - [Janon, A., Klein, T., Lagnoux, A., Nodet, M., & Prieur, C. (2014). Asymptotic normality and efficiency of two Sobol index estimators. ESAIM: Probability and Statistics, 18, 342-364.](https://arxiv.org/abs/1303.6451)

!!! note

Sobol sampling should be done with $2^k$ points and randomization, take a look at the docs for [QuasiMonteCarlo](https://docs.sciml.ai/QuasiMonteCarlo/stable/randomization/). If the number of samples is not a power of 2, the number of sample points will be changed to the next power of 2.

### Example

```julia
Expand All @@ -57,7 +61,7 @@ function ishi(X)
sin(X[1]) + A*sin(X[2])^2+ B*X[3]^4 *sin(X[1])
end

samples = 600000
samples = 524288
lb = -ones(4)*π
ub = ones(4)*π
sampler = SobolSample()
Expand Down Expand Up @@ -342,10 +346,18 @@ function gsa_sobol_all_y_analysis(method, all_y::AbstractArray{T}, d, n, Ei_esti
nboot > 1 ? reshape(ST_CI, size_...) : nothing)
end

function gsa(f, method::Sobol, p_range::AbstractVector; samples, kwargs...)
function gsa(f, method::Sobol, p_range::AbstractVector; samples,
rng::AbstractRNG = Random.default_rng(), kwargs...)
samples2n = nextpow(2, samples)
if samples2n != samples
samples = samples2n
@warn "Passed samples is not a power of 2, number of sample points changed to $samples"
end
log2num = round(Int, log2(samples))
AB = QuasiMonteCarlo.generate_design_matrices(samples, [i[1] for i in p_range],
[i[2] for i in p_range],
QuasiMonteCarlo.SobolSample(),
QuasiMonteCarlo.SobolSample(;
R = QuasiMonteCarlo.OwenScramble(; base = 2, pad = log2num, rng)),
Vaibhavdixit02 marked this conversation as resolved.
Show resolved Hide resolved
2 * method.nboot)
A = reduce(hcat, @view(AB[1:(method.nboot)]))
B = reduce(hcat, @view(AB[(method.nboot + 1):end]))
Expand Down
12 changes: 6 additions & 6 deletions test/shapley_method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ n_perms = -1;
n_var = 10_000;
n_outer = 1000;
n_inner = 3;
dim = 3;
margins = (Uniform(-pi, pi), Uniform(-pi, pi), Uniform(-pi, pi));
dim = 4;
margins = (Uniform(-pi, pi), Uniform(-pi, pi), Uniform(-pi, pi), Uniform(-pi, pi));
dependency_matrix = Matrix(4 * I, dim, dim);
C = GaussianCopula(dependency_matrix);
input_distribution = SklarDist(C, margins);
Expand All @@ -41,17 +41,17 @@ method = Shapley(n_perms = n_perms,

@test result.shapley_effects[1]≈0.43813841765976547 atol=1e-1
@test result.shapley_effects[2]≈0.44673952698721386 atol=1e-1
@test result.shapley_effects[3]≈0.23144736934254417 atol=1e-1
# @test result.shapley_effects[4]≈0.0 atol=1e-1
@test result.shapley_effects[3]≈0.11855122481995543 atol=1e-1
@test result.shapley_effects[4]≈0.0 atol=1e-1
#<---- non batch

#---> batch
result = gsa(ishi_batch, method, input_distribution, batch = true);

@test result.shapley_effects[1]≈0.44080027198796035 atol=1e-1
@test result.shapley_effects[2]≈0.43029987176805085 atol=1e-1
@test result.shapley_effects[3]≈0.23144736934254417 atol=1e-1
# @test result.shapley_effects[4]≈0.0 atol=1e-1
@test result.shapley_effects[3]≈0.11855122481995543 atol=1e-1
@test result.shapley_effects[4]≈0.0 atol=1e-1
#<--- batch

d = 3
Expand Down
9 changes: 3 additions & 6 deletions test/sobol_method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function linear(X)
A * X[1] + B * X[2]
end

n = 600000
n = 524288
lb = -ones(4) * π
ub = ones(4) * π
sampler = SobolSample()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed as well, shouldn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the comment from the PR that added RQMC that you had put in the issue. The OP mentions

To nuance my words, for the use case M=2 of sensitivity analysis this is probably not as bad as I show for small enough cases.

So it makes sense to enforce the randomization only for bootstrap cases, what do you think? Changing this is significantly affecting the results of these trivial tests and the indices values for this are pretty commonly accepted, though to be honest I guess the new values could be justified a bit better I am just worried about users from R trying to match the values and then having to defend this

Copy link
Member

@devmotion devmotion May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless of what this PR ends up doing: I think the tests, examples and recommendations should consistently do the same thing as the gsa implementations with samples kwarg (is this function tested at all if the changes do not affect the tests?).

My main concern is that generally doing non-standard things with low discrepancy sequences is a bad idea. E.g., even just omitting the initial point of zeros (as done in Sobol.jl) is a bad idea, as discussed in the issue in Sobol.jl and shown in https://arxiv.org/pdf/2008.08051. What's currently done in GlobalSensitivity - and what IMO rightfully shows a warning to users - is that the samples * num_mats samples are generated by taking a single consecutive subset of samples * num_mats and partitioning it: https://github.com/SciML/QuasiMonteCarlo.jl/blob/5c5483565d5b6a083256861bcf7e00cf7075c5f4/src/RandomizedQuasiMonteCarlo/iterators.jl#L268-L271 In general this won't preserve any of the distribution properties of the original sequence.

Switching to a proper RQMC approach would be much more sound. However, as far as I know, generally it shouldn't lead to (much) different/worse estimates of integrals, so something seems off or some other part of the algorithm seems to interact badly with the randomized sequences. A quick search reveals quite a few papers though regarding global sensitivity and RQMC, so I think it should work and be an improvement in general.

Regardless of the mathematical details, IMO just sticking with the current implementation is not a good idea for another reason: Users might get the same results as with other (R) packages - but every time they run gsa without pre-computed design matrices they will see a warning.

So in case it's too unclear yet how to fix the RQMC issues, I suggest as a temporary workaround to just do a single sample call (as in the implementation of generate_design_matrices that's currently used) in tests, examples, and gsa that at least makes it apparent how the samples are generated and fixes the warning (and should even lead to a more efficient gsa implementation since some of the partitioning and hcating can be avoided, only a single split is needed). Probably it would be good to still add a comment about this choice and links to RQMC in the docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was pretty helpful, I need to get to it @devmotion though sorry for the delay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something blocking or making your work harder?

Expand All @@ -48,10 +48,7 @@ res1 = gsa(ishi, Sobol(order = [0, 1, 2], nboot = 20), A, B)
0.0 0.0 4.279200031668718e-5 1.2542212940962112e-5;
0.0 0.0 0.0 -7.998213172266514e-7; 0.0 0.0 0.0 0.0] atol=1e-4
@test res1.S1_Conf_Int≈[
0.00013100970128286063,
0.00014730548523359544,
7.398816006175431e-5,
0.0
0.00018057916212640867, 0.0002327582551601999, 9.116874912775071e-5, 0.0
] atol=1e-4
@test res1.ST_Conf_Int≈[
5.657364947147881e-5,
Expand Down Expand Up @@ -94,7 +91,7 @@ ishigami.fun <- function(X) {
B <- 0.1
A * X[, 1] + B * X[, 2]
}
n <- 6000000
n <- 524288
X1 <- data.frame(matrix(runif(4 * n,-pi,pi), nrow = n))
X2 <- data.frame(matrix(runif(4 * n,-pi,pi), nrow = n))
sobol2007(ishigami.fun, X1, X2)
Expand Down
Loading