fdars-core 0.13.0

Functional Data Analysis algorithms in Rust
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Elastic shape analysis: quotient space operations and orbit representatives.
//!
//! Extends elastic alignment to work in quotient spaces where curves are
//! considered equivalent up to reparameterization, translation, and/or scaling.

use super::karcher::karcher_mean;
use super::pairwise::{elastic_align_pair, elastic_self_distance_matrix};
use super::srsf::srsf_single;
use super::{AlignmentResult, KarcherMeanResult};
use crate::error::FdarError;
use crate::helpers::simpsons_weights;
use crate::matrix::FdMatrix;
use crate::warping::l2_norm_l2;

// ─── Types ──────────────────────────────────────────────────────────────────

/// Quotient space for shape analysis.
///
/// Determines which transformations are factored out when comparing curves.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ShapeQuotient {
    /// Quotient by reparameterization only (elastic distance).
    #[default]
    Reparameterization,
    /// Quotient by reparameterization and vertical translation.
    ReparameterizationTranslation,
    /// Quotient by reparameterization, translation, and scale.
    ReparameterizationTranslationScale,
}

/// A canonical representative of a shape orbit.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OrbitRepresentative {
    /// The pre-processed curve (centered, scaled, etc.).
    pub representative: Vec<f64>,
    /// SRSF of the representative curve.
    pub representative_srsf: Vec<f64>,
    /// Warping function applied (identity for orbit_representative).
    pub gamma: Vec<f64>,
    /// Vertical translation removed.
    pub translation: f64,
    /// Scale factor removed.
    pub scale: f64,
}

/// Result of computing the elastic shape distance between two curves.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ShapeDistanceResult {
    /// Shape distance in the quotient space.
    pub distance: f64,
    /// Optimal warping function (length m).
    pub gamma: Vec<f64>,
    /// Second curve aligned to the first.
    pub f2_aligned: Vec<f64>,
}

/// Result of computing the shape mean of a set of curves.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ShapeMeanResult {
    /// Shape mean curve.
    pub mean: Vec<f64>,
    /// SRSF of the shape mean.
    pub mean_srsf: Vec<f64>,
    /// Warping functions (n x m).
    pub gammas: FdMatrix,
    /// Curves aligned to the mean (n x m).
    pub aligned_data: FdMatrix,
    /// Number of iterations used.
    pub n_iter: usize,
    /// Whether the algorithm converged.
    pub converged: bool,
}

// ─── Pre-processing Helpers ─────────────────────────────────────────────────

/// Compute the integral mean of a curve using Simpson's weights.
fn integral_mean(f: &[f64], argvals: &[f64]) -> f64 {
    let w = simpsons_weights(argvals);
    let total_w: f64 = w.iter().sum();
    if total_w <= 0.0 {
        return 0.0;
    }
    let wsum: f64 = f.iter().zip(w.iter()).map(|(&fi, &wi)| fi * wi).sum();
    wsum / total_w
}

/// Pre-process a curve according to the quotient type.
///
/// Returns `(processed_curve, translation, scale)`.
fn preprocess_curve(f: &[f64], argvals: &[f64], quotient: ShapeQuotient) -> (Vec<f64>, f64, f64) {
    let mut curve = f.to_vec();
    let mut translation = 0.0;
    let mut scale = 1.0;

    match quotient {
        ShapeQuotient::Reparameterization => {
            // No pre-processing needed.
        }
        ShapeQuotient::ReparameterizationTranslation => {
            // Subtract integral mean.
            let mean_val = integral_mean(&curve, argvals);
            translation = mean_val;
            for v in &mut curve {
                *v -= mean_val;
            }
        }
        ShapeQuotient::ReparameterizationTranslationScale => {
            // Subtract integral mean, then scale by SRSF L2 norm.
            let mean_val = integral_mean(&curve, argvals);
            translation = mean_val;
            for v in &mut curve {
                *v -= mean_val;
            }

            // Compute L2 norm of the SRSF for scale normalization.
            let q = srsf_single(&curve, argvals);
            // Use a uniform [0,1] time grid for the L2 norm computation.
            let m = argvals.len();
            let time: Vec<f64> = (0..m).map(|i| i as f64 / (m - 1).max(1) as f64).collect();
            let norm = l2_norm_l2(&q, &time);

            if norm > 1e-10 {
                scale = norm;
                for v in &mut curve {
                    *v /= norm;
                }
            }
        }
    }

    (curve, translation, scale)
}

/// Pre-process all rows of a data matrix according to the quotient type.
fn preprocess_data(data: &FdMatrix, argvals: &[f64], quotient: ShapeQuotient) -> FdMatrix {
    let (n, m) = data.shape();
    let mut result = FdMatrix::zeros(n, m);
    for i in 0..n {
        let row = data.row(i);
        let (processed, _, _) = preprocess_curve(&row, argvals, quotient);
        for j in 0..m {
            result[(i, j)] = processed[j];
        }
    }
    result
}

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

/// Compute the canonical orbit representative of a curve.
///
/// Applies the quotient transformations (centering, scaling) and computes
/// the SRSF of the result. The warping function is the identity.
///
/// # Arguments
/// * `f`        — Curve values (length m).
/// * `argvals`  — Evaluation points (length m).
/// * `quotient` — Which transformations to factor out.
///
/// # Errors
/// Returns [`FdarError::InvalidDimension`] if lengths do not match or `m < 2`.
pub fn orbit_representative(
    f: &[f64],
    argvals: &[f64],
    quotient: ShapeQuotient,
) -> Result<OrbitRepresentative, FdarError> {
    let m = f.len();
    if m != argvals.len() {
        return Err(FdarError::InvalidDimension {
            parameter: "f",
            expected: format!("length {}", argvals.len()),
            actual: format!("length {m}"),
        });
    }
    if m < 2 {
        return Err(FdarError::InvalidDimension {
            parameter: "f",
            expected: "length >= 2".to_string(),
            actual: format!("length {m}"),
        });
    }

    let (representative, translation, scale) = preprocess_curve(f, argvals, quotient);
    let representative_srsf = srsf_single(&representative, argvals);
    let gamma = argvals.to_vec(); // identity warp

    Ok(OrbitRepresentative {
        representative,
        representative_srsf,
        gamma,
        translation,
        scale,
    })
}

/// Compute the elastic shape distance between two curves.
///
/// Pre-processes both curves according to the quotient type, then computes
/// the elastic distance after optimal alignment.
///
/// # Arguments
/// * `f1`       — First curve (length m).
/// * `f2`       — Second curve (length m).
/// * `argvals`  — Evaluation points (length m).
/// * `quotient` — Which transformations to factor out.
/// * `lambda`   — Roughness penalty for alignment.
///
/// # Errors
/// Returns [`FdarError::InvalidDimension`] if lengths do not match or `m < 2`.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn shape_distance(
    f1: &[f64],
    f2: &[f64],
    argvals: &[f64],
    quotient: ShapeQuotient,
    lambda: f64,
) -> Result<ShapeDistanceResult, FdarError> {
    let m = f1.len();
    if m != f2.len() || m != argvals.len() {
        return Err(FdarError::InvalidDimension {
            parameter: "f1/f2",
            expected: format!("matching lengths == argvals.len() ({})", argvals.len()),
            actual: format!("f1.len()={}, f2.len()={}", f1.len(), f2.len()),
        });
    }
    if m < 2 {
        return Err(FdarError::InvalidDimension {
            parameter: "f1",
            expected: "length >= 2".to_string(),
            actual: format!("length {m}"),
        });
    }

    let (f1_pre, _, _) = preprocess_curve(f1, argvals, quotient);
    let (f2_pre, _, _) = preprocess_curve(f2, argvals, quotient);

    let AlignmentResult {
        gamma,
        f_aligned,
        distance,
    } = elastic_align_pair(&f1_pre, &f2_pre, argvals, lambda);

    Ok(ShapeDistanceResult {
        distance,
        gamma,
        f2_aligned: f_aligned,
    })
}

/// Compute the pairwise shape distance matrix for a set of curves.
///
/// Pre-processes all curves according to the quotient type, then delegates to
/// the elastic self-distance matrix computation.
///
/// # Arguments
/// * `data`     — Functional data matrix (n x m).
/// * `argvals`  — Evaluation points (length m).
/// * `quotient` — Which transformations to factor out.
/// * `lambda`   — Roughness penalty for alignment.
///
/// # Errors
/// Returns [`FdarError::InvalidDimension`] if `argvals` length does not match `m`.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn shape_self_distance_matrix(
    data: &FdMatrix,
    argvals: &[f64],
    quotient: ShapeQuotient,
    lambda: f64,
) -> Result<FdMatrix, FdarError> {
    let (_n, m) = data.shape();
    if argvals.len() != m {
        return Err(FdarError::InvalidDimension {
            parameter: "argvals",
            expected: format!("{m}"),
            actual: format!("{}", argvals.len()),
        });
    }

    let preprocessed = preprocess_data(data, argvals, quotient);
    Ok(elastic_self_distance_matrix(&preprocessed, argvals, lambda))
}

/// Compute the Karcher (Frechet) mean in the elastic shape space.
///
/// Pre-processes all curves according to the quotient type, then computes
/// the Karcher mean on the preprocessed data.
///
/// # Arguments
/// * `data`     — Functional data matrix (n x m).
/// * `argvals`  — Evaluation points (length m).
/// * `quotient` — Which transformations to factor out.
/// * `lambda`   — Roughness penalty for alignment.
/// * `max_iter` — Maximum number of Karcher iterations.
/// * `tol`      — Convergence tolerance.
///
/// # Errors
/// Returns [`FdarError::InvalidDimension`] if `argvals` length does not match `m`
/// or `n < 1`.
#[must_use = "expensive computation whose result should not be discarded"]
pub fn shape_mean(
    data: &FdMatrix,
    argvals: &[f64],
    quotient: ShapeQuotient,
    lambda: f64,
    max_iter: usize,
    tol: f64,
) -> Result<ShapeMeanResult, FdarError> {
    let (n, m) = data.shape();
    if argvals.len() != m {
        return Err(FdarError::InvalidDimension {
            parameter: "argvals",
            expected: format!("{m}"),
            actual: format!("{}", argvals.len()),
        });
    }
    if n < 1 {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: "at least 1 row".to_string(),
            actual: format!("{n} rows"),
        });
    }

    let preprocessed = preprocess_data(data, argvals, quotient);
    let KarcherMeanResult {
        mean,
        mean_srsf,
        gammas,
        aligned_data,
        n_iter,
        converged,
        ..
    } = karcher_mean(&preprocessed, argvals, max_iter, tol, lambda);

    Ok(ShapeMeanResult {
        mean,
        mean_srsf,
        gammas,
        aligned_data,
        n_iter,
        converged,
    })
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::simulation::{sim_fundata, EFunType, EValType};
    use crate::test_helpers::uniform_grid;

    fn make_data(n: usize, m: usize) -> (FdMatrix, Vec<f64>) {
        let t = uniform_grid(m);
        let data = sim_fundata(n, &t, 3, EFunType::Fourier, EValType::Exponential, Some(99));
        (data, t)
    }

    // ── orbit_representative ──

    #[test]
    fn orbit_representative_reparam_only() {
        let t = uniform_grid(30);
        let f: Vec<f64> = t.iter().map(|&x| (x * 6.0).sin()).collect();
        let rep = orbit_representative(&f, &t, ShapeQuotient::Reparameterization).unwrap();
        // No transformation: representative should match original.
        assert_eq!(rep.representative.len(), 30);
        for i in 0..30 {
            assert!(
                (rep.representative[i] - f[i]).abs() < 1e-12,
                "reparameterization-only orbit should not change the curve"
            );
        }
        assert!((rep.translation - 0.0).abs() < f64::EPSILON);
        assert!((rep.scale - 1.0).abs() < f64::EPSILON);
        assert_eq!(rep.gamma, t);
    }

    #[test]
    fn orbit_representative_translation() {
        let t = uniform_grid(30);
        let offset = 5.0;
        let f: Vec<f64> = t.iter().map(|&x| (x * 6.0).sin() + offset).collect();
        let rep =
            orbit_representative(&f, &t, ShapeQuotient::ReparameterizationTranslation).unwrap();
        // The integral mean should be removed; representative should be roughly centered.
        let mean_after = integral_mean(&rep.representative, &t);
        assert!(
            mean_after.abs() < 1e-10,
            "translation quotient should center the curve, mean={mean_after}"
        );
    }

    #[test]
    fn orbit_representative_translation_scale() {
        let t = uniform_grid(50);
        let f: Vec<f64> = t.iter().map(|&x| 10.0 * (x * 4.0).sin() + 3.0).collect();
        let rep = orbit_representative(&f, &t, ShapeQuotient::ReparameterizationTranslationScale)
            .unwrap();
        assert!(rep.scale > 0.0, "scale factor should be positive");

        // Scaling a curve by alpha should produce the same representative (up to sign).
        let f2: Vec<f64> = t.iter().map(|&x| 20.0 * (x * 4.0).sin() + 3.0).collect();
        let rep2 = orbit_representative(&f2, &t, ShapeQuotient::ReparameterizationTranslationScale)
            .unwrap();

        // The representatives should be proportional (same shape); check correlation.
        let dot: f64 = rep
            .representative
            .iter()
            .zip(rep2.representative.iter())
            .map(|(&a, &b)| a * b)
            .sum();
        let n1: f64 = rep
            .representative
            .iter()
            .map(|&v| v * v)
            .sum::<f64>()
            .sqrt();
        let n2: f64 = rep2
            .representative
            .iter()
            .map(|&v| v * v)
            .sum::<f64>()
            .sqrt();
        let corr = if n1 > 1e-10 && n2 > 1e-10 {
            dot / (n1 * n2)
        } else {
            1.0
        };
        assert!(
            corr > 0.99,
            "scaled curves should have nearly identical representatives, corr={corr}"
        );
    }

    #[test]
    fn orbit_representative_length_mismatch() {
        let t = uniform_grid(30);
        let f = vec![1.0; 20];
        assert!(orbit_representative(&f, &t, ShapeQuotient::Reparameterization).is_err());
    }

    #[test]
    fn orbit_representative_too_short() {
        let f = vec![1.0];
        let t = vec![0.0];
        assert!(orbit_representative(&f, &t, ShapeQuotient::Reparameterization).is_err());
    }

    // ── shape_distance ──

    #[test]
    fn shape_distance_identical_curves() {
        let t = uniform_grid(30);
        let f: Vec<f64> = t.iter().map(|&x| (x * 6.0).sin()).collect();
        let result = shape_distance(&f, &f, &t, ShapeQuotient::Reparameterization, 0.0).unwrap();
        assert!(
            result.distance < 0.1,
            "distance between identical curves should be near zero, got {}",
            result.distance
        );
        assert_eq!(result.gamma.len(), 30);
        assert_eq!(result.f2_aligned.len(), 30);
    }

    #[test]
    fn shape_distance_translated_curves() {
        let t = uniform_grid(30);
        let f1: Vec<f64> = t.iter().map(|&x| (x * 6.0).sin()).collect();
        let f2: Vec<f64> = t.iter().map(|&x| (x * 6.0).sin() + 5.0).collect();

        // Without translation quotient: distance should be large.
        let d_no_trans =
            shape_distance(&f1, &f2, &t, ShapeQuotient::Reparameterization, 0.0).unwrap();
        // With translation quotient: distance should be much smaller.
        let d_trans = shape_distance(
            &f1,
            &f2,
            &t,
            ShapeQuotient::ReparameterizationTranslation,
            0.0,
        )
        .unwrap();

        assert!(
            d_trans.distance < d_no_trans.distance + 0.01,
            "translation quotient should not increase distance: d_trans={}, d_no_trans={}",
            d_trans.distance,
            d_no_trans.distance
        );
    }

    #[test]
    fn shape_distance_length_mismatch() {
        let t = uniform_grid(30);
        let f1 = vec![0.0; 30];
        let f2 = vec![0.0; 20];
        assert!(shape_distance(&f1, &f2, &t, ShapeQuotient::Reparameterization, 0.0).is_err());
    }

    // ── shape_self_distance_matrix ──

    #[test]
    fn shape_distance_matrix_smoke() {
        let (data, t) = make_data(5, 20);
        let dmat =
            shape_self_distance_matrix(&data, &t, ShapeQuotient::Reparameterization, 0.0).unwrap();
        assert_eq!(dmat.shape(), (5, 5));
        // Diagonal should be zero.
        for i in 0..5 {
            assert!(
                dmat[(i, i)].abs() < 1e-10,
                "diagonal should be zero, got {}",
                dmat[(i, i)]
            );
        }
        // Should be symmetric.
        for i in 0..5 {
            for j in (i + 1)..5 {
                assert!(
                    (dmat[(i, j)] - dmat[(j, i)]).abs() < 1e-10,
                    "distance matrix should be symmetric"
                );
            }
        }
    }

    #[test]
    fn shape_distance_matrix_argvals_mismatch() {
        let (data, _) = make_data(5, 20);
        let bad_t = uniform_grid(15);
        assert!(
            shape_self_distance_matrix(&data, &bad_t, ShapeQuotient::Reparameterization, 0.0)
                .is_err()
        );
    }

    // ── shape_mean ──

    #[test]
    fn shape_mean_smoke() {
        let (data, t) = make_data(6, 25);
        let result =
            shape_mean(&data, &t, ShapeQuotient::Reparameterization, 0.0, 5, 1e-2).unwrap();
        assert_eq!(result.mean.len(), 25);
        assert_eq!(result.mean_srsf.len(), 25);
        assert_eq!(result.gammas.shape(), (6, 25));
        assert_eq!(result.aligned_data.shape(), (6, 25));
        assert!(result.n_iter >= 1);
    }

    #[test]
    fn shape_mean_translation_quotient() {
        let (data, t) = make_data(6, 25);
        let result = shape_mean(
            &data,
            &t,
            ShapeQuotient::ReparameterizationTranslation,
            0.0,
            5,
            1e-2,
        )
        .unwrap();
        assert_eq!(result.mean.len(), 25);
    }

    #[test]
    fn shape_mean_full_quotient() {
        let (data, t) = make_data(6, 25);
        let result = shape_mean(
            &data,
            &t,
            ShapeQuotient::ReparameterizationTranslationScale,
            0.0,
            5,
            1e-2,
        )
        .unwrap();
        assert_eq!(result.mean.len(), 25);
    }

    #[test]
    fn shape_mean_argvals_mismatch() {
        let (data, _) = make_data(5, 25);
        let bad_t = uniform_grid(15);
        assert!(shape_mean(
            &data,
            &bad_t,
            ShapeQuotient::Reparameterization,
            0.0,
            5,
            1e-2
        )
        .is_err());
    }

    #[test]
    fn default_quotient() {
        assert_eq!(ShapeQuotient::default(), ShapeQuotient::Reparameterization);
    }
}