scirs2-interpolate 0.4.3

Interpolation module for SciRS2 (scirs2-interpolate)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! ANOVA functional decomposition for sparse grids.
//!
//! Classical ANOVA decomposition:
//! ```text
//! f(x) = f₀ + Σᵢ fᵢ(xᵢ) + Σᵢ<ⱼ fᵢⱼ(xᵢ,xⱼ) + …
//! ```
//! where each term is defined by orthogonal projection (marginal integration).
//!
//! Also provides Sobol' sensitivity indices: Sᵢ = Var(fᵢ) / Var(f).
//!
//! # References
//! - Saltelli et al., "Global Sensitivity Analysis: The Primer" (2008)
//! - Sobol', "Sensitivity estimates for nonlinear mathematical models" (1993)

use scirs2_core::ndarray::Array2;

/// Configuration for ANOVA decomposition.
#[derive(Debug, Clone)]
pub struct AnovaConfig {
    /// Maximum interaction order (default 2). Orders above this are truncated.
    pub max_order: usize,
    /// Number of Gauss-Legendre quadrature points per dimension (capped at 5 internally).
    pub n_quad_points: usize,
}

impl Default for AnovaConfig {
    fn default() -> Self {
        Self {
            max_order: 2,
            n_quad_points: 10,
        }
    }
}

/// Result of an ANOVA functional decomposition.
#[derive(Debug, Clone)]
pub struct AnovaDecomposition {
    /// f₀ = E[f], the constant (mean) term.
    pub mean: f64,
    /// Variance of each main-effect term fᵢ: Var(fᵢ) for dim i.
    pub main_effects: Vec<f64>,
    /// Variance of each pairwise interaction term fᵢⱼ: `interaction_effects[[i,j]]`.
    /// Symmetric, diagonal is zero.
    pub interaction_effects: Array2<f64>,
    /// First-order Sobol' index Sᵢ = Var(fᵢ) / Var(f).
    pub sobol_indices: Vec<f64>,
    /// Total Sobol' index Tᵢ = 1 - Var(E[f|x_{−i}]) / Var(f).
    pub total_sobol_indices: Vec<f64>,
    /// Total variance Var(f).
    pub total_variance: f64,
}

// ─── Gauss-Legendre nodes and weights on [0, 1] ───────────────────────────────

/// Returns (nodes, weights) for Gauss-Legendre quadrature mapped to [0, 1].
///
/// Supports n ∈ {1, 2, 3, 4, 5}.  For any other `n` the function silently
/// falls back to n = 5 (accuracy up to degree-9 polynomials).
fn gauss_legendre_01(n: usize) -> (Vec<f64>, Vec<f64>) {
    match n {
        1 => (vec![0.5], vec![1.0]),
        2 => (
            vec![0.211_324_865_405_187_1, 0.788_675_134_594_812_9],
            vec![0.5, 0.5],
        ),
        3 => (
            vec![0.112_701_665_379_258_31, 0.5, 0.887_298_334_620_741_69],
            vec![
                0.277_777_777_777_777_79,
                0.444_444_444_444_444_42,
                0.277_777_777_777_777_79,
            ],
        ),
        4 => (
            vec![
                0.069_431_844_202_973_713,
                0.330_009_478_207_571_87,
                0.669_990_521_792_428_13,
                0.930_568_155_797_026_29,
            ],
            vec![
                0.173_927_422_568_726_93,
                0.326_072_577_431_273_07,
                0.326_072_577_431_273_07,
                0.173_927_422_568_726_93,
            ],
        ),
        // n == 5 or any unsupported n
        _ => (
            vec![
                0.046_910_077_781_151_27,
                0.230_765_345_653_031_44,
                0.5,
                0.769_234_654_346_968_56,
                0.953_089_922_218_848_73,
            ],
            vec![
                0.118_463_442_528_094_54,
                0.239_314_335_249_683_23,
                0.284_444_444_444_444_44,
                0.239_314_335_249_683_23,
                0.118_463_442_528_094_54,
            ],
        ),
    }
}

// ─── Public API ───────────────────────────────────────────────────────────────

/// Compute an ANOVA functional decomposition of a black-box function
/// `f: [0,1]^d → ℝ`.
///
/// # Arguments
/// * `f_fn` – The function to decompose.
/// * `d`    – Number of input dimensions.
/// * `config` – Quadrature and truncation settings.
///
/// # Returns
/// [`AnovaDecomposition`] with mean, main-effect variances, pairwise interaction
/// variances, and first-order / total Sobol' indices.
pub fn anova_decompose<F>(
    f_fn: &F,
    d: usize,
    config: &AnovaConfig,
) -> Result<AnovaDecomposition, AnovaError>
where
    F: Fn(&[f64]) -> f64 + Sync,
{
    if d == 0 {
        return Err(AnovaError::ZeroDimension);
    }

    // Cap quadrature points to the table size (max 5).
    let q = config.n_quad_points.clamp(1, 5);
    let (nodes, weights) = gauss_legendre_01(q);

    // For large d reduce the effective grid to avoid n^d explosion.
    let effective_q = if d <= 3 { q } else { 3.min(q) };
    let nodes = &nodes[..effective_q];
    let weights = &weights[..effective_q];

    // f₀ = E[f]
    let mean = compute_mean(f_fn, d, nodes, weights);

    // E[f²] → total variance
    let mean_sq = compute_mean_of_square(f_fn, d, nodes, weights);
    let total_variance = mean_sq - mean * mean;

    // Main-effect variances: Var(fᵢ) for each i
    let main_effects: Vec<f64> = (0..d)
        .map(|i| compute_main_effect_variance(f_fn, d, i, mean, nodes, weights))
        .collect();

    // First-order Sobol' indices
    let sobol_indices: Vec<f64> = if total_variance > 1e-12 {
        main_effects.iter().map(|v| v / total_variance).collect()
    } else {
        vec![0.0; d]
    };

    // Total Sobol' indices: Tᵢ = 1 − Var(E[f | x_{−i}]) / Var(f)
    let total_sobol_indices: Vec<f64> = (0..d)
        .map(|i| {
            let complement_var =
                compute_complement_effect_variance(f_fn, d, i, mean, nodes, weights);
            if total_variance > 1e-12 {
                1.0 - complement_var / total_variance
            } else {
                0.0
            }
        })
        .collect();

    // Pairwise interaction variances (max_order >= 2 and d ≤ 8 to stay tractable)
    let mut interaction_effects = Array2::zeros((d, d));
    if config.max_order >= 2 && d <= 8 {
        for i in 0..d {
            for j in (i + 1)..d {
                let v_ij = compute_interaction_variance(
                    f_fn,
                    d,
                    i,
                    j,
                    mean,
                    main_effects[i],
                    main_effects[j],
                    nodes,
                    weights,
                );
                interaction_effects[[i, j]] = v_ij;
                interaction_effects[[j, i]] = v_ij;
            }
        }
    }

    Ok(AnovaDecomposition {
        mean,
        main_effects,
        interaction_effects,
        sobol_indices,
        total_sobol_indices,
        total_variance,
    })
}

// ─── Quadrature helpers ───────────────────────────────────────────────────────

/// E[f] via full tensor-product Gauss-Legendre quadrature.
fn compute_mean<F: Fn(&[f64]) -> f64>(f_fn: &F, d: usize, nodes: &[f64], weights: &[f64]) -> f64 {
    let nq = nodes.len();
    let total_pts = nq.pow(d as u32);
    let mut x = vec![0.0f64; d];
    let mut total = 0.0f64;
    for idx in 0..total_pts {
        let mut w = 1.0f64;
        let mut tmp = idx;
        for xi in x.iter_mut().take(d) {
            let k = tmp % nq;
            tmp /= nq;
            *xi = nodes[k];
            w *= weights[k];
        }
        total += w * f_fn(&x);
    }
    total
}

/// E[f²] via full tensor-product Gauss-Legendre quadrature.
fn compute_mean_of_square<F: Fn(&[f64]) -> f64>(
    f_fn: &F,
    d: usize,
    nodes: &[f64],
    weights: &[f64],
) -> f64 {
    let nq = nodes.len();
    let total_pts = nq.pow(d as u32);
    let mut x = vec![0.0f64; d];
    let mut total = 0.0f64;
    for idx in 0..total_pts {
        let mut w = 1.0f64;
        let mut tmp = idx;
        for xi in x.iter_mut().take(d) {
            let k = tmp % nq;
            tmp /= nq;
            *xi = nodes[k];
            w *= weights[k];
        }
        let val = f_fn(&x);
        total += w * val * val;
    }
    total
}

/// Var(fᵢ) = E[(E[f | xᵢ] − f₀)²].
fn compute_main_effect_variance<F: Fn(&[f64]) -> f64>(
    f_fn: &F,
    d: usize,
    dim_i: usize,
    mean: f64,
    nodes: &[f64],
    weights: &[f64],
) -> f64 {
    nodes
        .iter()
        .zip(weights.iter())
        .map(|(&xi, &wi)| {
            let cond_mean = compute_conditional_mean(f_fn, d, dim_i, xi, nodes, weights);
            wi * (cond_mean - mean).powi(2)
        })
        .sum()
}

/// E[f | x_{fixed_dim} = fixed_val] — integrate out all other dimensions.
fn compute_conditional_mean<F: Fn(&[f64]) -> f64>(
    f_fn: &F,
    d: usize,
    fixed_dim: usize,
    fixed_val: f64,
    nodes: &[f64],
    weights: &[f64],
) -> f64 {
    let nq = nodes.len();
    let d_rest = d - 1;
    let total_pts = nq.pow(d_rest as u32);
    let mut x = vec![0.0f64; d];
    x[fixed_dim] = fixed_val;

    let other_dims: Vec<usize> = (0..d).filter(|&dim| dim != fixed_dim).collect();

    let mut total = 0.0f64;
    for idx in 0..total_pts {
        let mut w = 1.0f64;
        let mut tmp = idx;
        for &dim in &other_dims {
            let k = tmp % nq;
            tmp /= nq;
            x[dim] = nodes[k];
            w *= weights[k];
        }
        total += w * f_fn(&x);
    }
    total
}

/// Var(E[f | x_{−i}]) — variance of the conditional expectation with dim i
/// integrated out.  Used for total Sobol' indices.
fn compute_complement_effect_variance<F: Fn(&[f64]) -> f64>(
    f_fn: &F,
    d: usize,
    dim_i: usize,
    mean: f64,
    nodes: &[f64],
    weights: &[f64],
) -> f64 {
    let nq = nodes.len();
    let d_rest = d - 1;
    let total_complement_pts = nq.pow(d_rest as u32);

    let other_dims: Vec<usize> = (0..d).filter(|&dim| dim != dim_i).collect();
    let mut x = vec![0.0f64; d];
    let mut variance = 0.0f64;

    for idx in 0..total_complement_pts {
        let mut w_complement = 1.0f64;
        let mut tmp = idx;
        for &dim in &other_dims {
            let k = tmp % nq;
            tmp /= nq;
            x[dim] = nodes[k];
            w_complement *= weights[k];
        }
        // Integrate out dim_i
        let cond_mean_complement: f64 = nodes
            .iter()
            .zip(weights.iter())
            .map(|(&xi, &wi)| {
                x[dim_i] = xi;
                wi * f_fn(&x)
            })
            .sum();
        variance += w_complement * (cond_mean_complement - mean).powi(2);
    }
    variance
}

/// Pure second-order interaction variance:
/// Var(fᵢⱼ) = Var(E[f | xᵢ, xⱼ]) − Var(fᵢ) − Var(fⱼ), clamped to ≥ 0.
fn compute_interaction_variance<F: Fn(&[f64]) -> f64>(
    f_fn: &F,
    d: usize,
    dim_i: usize,
    dim_j: usize,
    mean: f64,
    var_i: f64,
    var_j: f64,
    nodes: &[f64],
    weights: &[f64],
) -> f64 {
    let nq = nodes.len();
    let d_rest = if d >= 2 { d - 2 } else { 0 };
    let mut x = vec![0.0f64; d];
    let other_dims: Vec<usize> = (0..d).filter(|&dim| dim != dim_i && dim != dim_j).collect();

    let mut v_ij_plus = 0.0f64;
    for (ki, &xi) in nodes.iter().enumerate() {
        for (kj, &xj) in nodes.iter().enumerate() {
            x[dim_i] = xi;
            x[dim_j] = xj;
            // E[f | xᵢ, xⱼ] — integrate out remaining dims
            let total_rest = nq.pow(d_rest as u32);
            let mut acc = 0.0f64;
            for idx in 0..total_rest {
                let mut w = 1.0f64;
                let mut tmp = idx;
                for &dim in &other_dims {
                    let k = tmp % nq;
                    tmp /= nq;
                    x[dim] = nodes[k];
                    w *= weights[k];
                }
                acc += w * f_fn(&x);
            }
            v_ij_plus += weights[ki] * weights[kj] * (acc - mean).powi(2);
        }
    }
    (v_ij_plus - var_i - var_j).max(0.0)
}

// ─── Error type ───────────────────────────────────────────────────────────────

/// Error returned by [`anova_decompose`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnovaError {
    /// `d = 0` is not supported.
    ZeroDimension,
}

impl std::fmt::Display for AnovaError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AnovaError::ZeroDimension => write!(f, "ANOVA requires d ≥ 1"),
        }
    }
}

impl std::error::Error for AnovaError {}

// ─── Unit tests ───────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn gauss_legendre_weights_sum_to_one() {
        for n in 1..=5 {
            let (_, weights) = gauss_legendre_01(n);
            let sum: f64 = weights.iter().sum();
            assert!((sum - 1.0).abs() < 1e-12, "n={n}: weights sum = {sum}");
        }
    }

    #[test]
    fn constant_function_mean_correct() {
        let result =
            anova_decompose(&|_x: &[f64]| 7.0_f64, 2, &AnovaConfig::default()).expect("anova");
        assert!((result.mean - 7.0).abs() < 1e-8, "mean={}", result.mean);
        assert!(result.total_variance < 1e-8);
    }
}