strapdown-core 0.5.0

A toolbox for building and analyzing strapdown inertial navigation systems as well as simulating various scenarios of GNSS signal degradation.
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
//! Particle filter implementation for nonlinear Bayesian state estimation.
//!
//! This module provides a particle filter (also known as Sequential Monte Carlo) implementation
//! for strapdown inertial navigation. Unlike Kalman filters which assume Gaussian distributions,
//! particle filters can represent arbitrary probability distributions through weighted samples.
//! This makes them well-suited for highly nonlinear systems and non-Gaussian noise characteristics.
//! However, they typically require more computational resources due to the need for many particles
//! to accurately approximate the posterior distribution. As such, they are often used in scenarios
//! where Kalman filters struggle, such as in the presence of multimodal distributions or severe
//! nonlinearities with state dynamics and dimensions that are manageable.
//!
//! A full 15-state canonical strapdown inertial navigation system is feasible with particle filters
//! for moderate particle counts (e.g., 1000-5000 particles) on modern hardware with *reasonably
//! good* IMU quality. For more challenging scenarios, such as low-quality IMUs or more complex
//! motion dynamics, hybrid approaches like Rao-Blackwellized Particle Filters (RBPF) can
//! be employed. RBPFs use particles to represent a subset of the state (e.g., position and velocity)
//! while using Kalman filters for the remaining states (e.g., attitude and sensor biases), reducing
//! the dimensionality of the particle filter and improving efficiency.
//!
//! # Library Design Philosophy
//!
//! This module is designed as a **template-style library** that provides building blocks for
//! implementing custom particle filter-based navigation systems. Since there is no canonical
//! form of a particle filter-based INS, we provide generic components that users can adapt:
//!
//! ## Key Components
//!
//! - [`Particle`]: A trait defining the required methods for particle state representation
//! - [`ParticleFilter`]: A trait for marking navigation systems as particle filter-based
//! - **Standalone resampling functions**: Generic algorithms that operate on particle weights
//!   - [`multinomial_resample`]: Independent sampling from weighted distribution
//!   - [`systematic_resample`]: Deterministic selection with random offset
//!   - [`stratified_resample`]: Stratified sampling for lower variance
//!   - [`residual_resample`]: Hybrid deterministic/random sampling

use std::any::Any;

use nalgebra::DVector;
use rand::prelude::*;

/// Trait defining the interface for particle state representation
///
/// Users must implement this trait for their custom particle types to use with
/// the generic ParticleFilter. The trait provides methods for particle initialization,
/// state extraction, and weight management.
pub trait Particle: Any {
    /// Downcast helper method to allow for type-safe downcasting
    fn as_any(&self) -> &dyn Any;
    /// Downcast helper method for mutable references
    fn as_any_mut(&mut self) -> &mut dyn Any;

    /// Creates a new particle with the given initial state and weight
    ///
    /// # Arguments
    /// * `initial_state` - The initial state vector for the particle
    /// * `weight` - The initial weight (typically 1/N for N particles)
    fn new(initial_state: &DVector<f64>, weight: f64) -> Self
    where
        Self: Sized;

    /// Returns the state vector of the particle
    fn state(&self) -> DVector<f64>;

    /// Sets the state vector of the particle
    ///
    /// The default implementation will panic if called. Implementors of this
    /// trait should override this method to update their internal state
    /// representation.
    fn set_state(&mut self, _state: DVector<f64>) {
        panic!(
            "Particle::set_state is not implemented for this type. \
             Please provide an implementation in your Particle impl."
        );
    }

    /// Returns the current weight of the particle
    fn weight(&self) -> f64;

    /// Sets the weight of the particle
    fn set_weight(&mut self, weight: f64);
}

/// Strategy for extracting state estimates from particles
///
/// Different averaging strategies can be used depending on the application:
/// - Mean: Simple arithmetic mean of particle states
/// - WeightedMean: Weighted average using particle weights
/// - HighestWeight: State of the particle with highest weight
#[derive(Clone, Copy, Debug, Default)]
pub enum ParticleAveragingStrategy {
    /// Arithmetic mean of all particle states (ignores weights)
    Mean,
    /// Weighted mean using particle weights
    #[default]
    WeightedMean,
    /// State of the particle with the highest weight (maximum a posteriori estimate)
    HighestWeight,
}

/// Strategy for resampling particles to combat degeneracy
///
/// Particle filters suffer from degeneracy where most particles have negligible weights.
/// Resampling addresses this by generating new particles from the current set based on
/// their weights.
#[derive(Clone, Copy, Debug, Default)]
pub enum ParticleResamplingStrategy {
    /// Multinomial resampling: draw N independent samples from weighted distribution
    Multinomial,
    /// Systematic resampling: deterministic selection with random offset
    #[default]
    Systematic,
    /// Stratified resampling: divide [0,1] into N strata and sample once per stratum
    Stratified,
    /// Residual resampling: deterministic selection of high-weight particles, then random sampling for remainder
    Residual,
}

// ============= Generic Resampling Functions ===================================

/// Multinomial resampling algorithm
///
/// Draws N independent samples from the weighted particle distribution.
/// This is the simplest resampling method but has highest variance.
///
/// # Arguments
/// * `weights` - Non-negative weights for each particle. Callers are responsible
///   for ensuring the weights sum to 1.0 before calling this function.
/// * `num_samples` - Number of samples to draw
/// * `rng` - Random number generator
///
/// # Returns
/// Vector of indices representing which particles to keep/duplicate
///
/// # Example
/// ```
/// use strapdown::particle::multinomial_resample;
/// use rand::SeedableRng;
///
/// let weights = vec![0.1, 0.3, 0.4, 0.2];
/// let mut rng = rand::rngs::StdRng::seed_from_u64(42);
/// let indices = multinomial_resample(&weights, 4, &mut rng);
/// assert_eq!(indices.len(), 4);
/// ```
pub fn multinomial_resample<R: Rng>(
    weights: &[f64],
    num_samples: usize,
    rng: &mut R,
) -> Vec<usize> {
    let mut indices = Vec::with_capacity(num_samples);
    let cumsum: Vec<f64> = weights
        .iter()
        .scan(0.0, |acc, &w| {
            *acc += w;
            Some(*acc)
        })
        .collect();

    for _ in 0..num_samples {
        let u: f64 = rng.random();
        let idx = cumsum
            .iter()
            .position(|&c| c >= u)
            .unwrap_or(weights.len() - 1);
        indices.push(idx);
    }

    indices
}

/// Systematic resampling algorithm
///
/// More efficient and lower variance than multinomial resampling.
/// Uses a single random number and deterministic spacing to select particles.
///
/// # Arguments
/// * `weights` - Non-negative weights for each particle. Callers are responsible
///   for ensuring the weights sum to 1.0 before calling this function.
/// * `num_samples` - Number of samples to draw
/// * `rng` - Random number generator
///
/// # Returns
/// Vector of indices representing which particles to keep/duplicate
///
/// # Example
/// ```
/// use strapdown::particle::systematic_resample;
/// use rand::SeedableRng;
///
/// let weights = vec![0.1, 0.3, 0.4, 0.2];
/// let mut rng = rand::rngs::StdRng::seed_from_u64(42);
/// let indices = systematic_resample(&weights, 4, &mut rng);
/// assert_eq!(indices.len(), 4);
/// ```
pub fn systematic_resample<R: Rng>(weights: &[f64], num_samples: usize, rng: &mut R) -> Vec<usize> {
    let mut indices = Vec::with_capacity(num_samples);
    let step = 1.0 / num_samples as f64;
    let start: f64 = rng.random::<f64>() * step;

    let mut cumsum = 0.0;
    let mut i = 0;

    for j in 0..num_samples {
        let u = start + (j as f64) * step;

        while cumsum < u && i < weights.len() {
            cumsum += weights[i];
            i += 1;
        }

        // Ensure i is at least 1 before subtracting, and clamp to valid range
        let idx = if i > 0 { i - 1 } else { 0 };
        indices.push(idx.min(weights.len() - 1));
    }

    indices
}

/// Stratified resampling algorithm
///
/// Divides [0,1] into N equal strata and samples once per stratum.
/// Provides lower variance than multinomial resampling while maintaining
/// randomness compared to systematic resampling.
///
/// # Arguments
/// * `weights` - Non-negative weights for each particle. Callers are responsible
///   for ensuring the weights sum to 1.0 before calling this function.
/// * `num_samples` - Number of samples to draw
/// * `rng` - Random number generator
///
/// # Returns
/// Vector of indices representing which particles to keep/duplicate
///
/// # Example
/// ```
/// use strapdown::particle::stratified_resample;
/// use rand::SeedableRng;
///
/// let weights = vec![0.1, 0.3, 0.4, 0.2];
/// let mut rng = rand::rngs::StdRng::seed_from_u64(42);
/// let indices = stratified_resample(&weights, 4, &mut rng);
/// assert_eq!(indices.len(), 4);
/// ```
pub fn stratified_resample<R: Rng>(weights: &[f64], num_samples: usize, rng: &mut R) -> Vec<usize> {
    let mut indices = Vec::with_capacity(num_samples);
    let step = 1.0 / num_samples as f64;

    let mut cumsum = 0.0;
    let mut i = 0;

    for j in 0..num_samples {
        let u = (j as f64 + rng.random::<f64>()) * step;

        while cumsum < u && i < weights.len() {
            cumsum += weights[i];
            i += 1;
        }

        // Ensure i is at least 1 before subtracting, and clamp to valid range
        let idx = if i > 0 { i - 1 } else { 0 };
        indices.push(idx.min(weights.len() - 1));
    }

    indices
}

/// Residual resampling algorithm
///
/// Deterministically selects particles based on integer parts of N*weights,
/// then randomly samples remaining particles. This minimizes variance while
/// ensuring particles with high weights are always included.
///
/// # Arguments
/// * `weights` - Non-negative weights for each particle. Callers are responsible
///   for ensuring the weights sum to 1.0 before calling this function.
/// * `num_samples` - Number of samples to draw
/// * `rng` - Random number generator
///
/// # Returns
/// Vector of indices representing which particles to keep/duplicate
///
/// # Example
/// ```
/// use strapdown::particle::residual_resample;
/// use rand::SeedableRng;
///
/// let weights = vec![0.1, 0.3, 0.4, 0.2];
/// let mut rng = rand::rngs::StdRng::seed_from_u64(42);
/// let indices = residual_resample(&weights, 4, &mut rng);
/// assert_eq!(indices.len(), 4);
/// ```
pub fn residual_resample<R: Rng>(weights: &[f64], num_samples: usize, rng: &mut R) -> Vec<usize> {
    let mut indices = Vec::with_capacity(num_samples);

    // Calculate expected number of copies for each particle
    let n_weights: Vec<f64> = weights.iter().map(|&w| w * num_samples as f64).collect();

    // Deterministic selection: take integer part of each weight
    let mut residual_weights = Vec::with_capacity(weights.len());
    for (i, &nw) in n_weights.iter().enumerate() {
        let n_copies = nw.floor() as usize;
        for _ in 0..n_copies {
            indices.push(i);
        }
        // Store fractional part for residual sampling
        residual_weights.push(nw - nw.floor());
    }

    // Normalize residual weights
    let residual_sum: f64 = residual_weights.iter().sum();
    if residual_sum > 0.0 {
        for w in &mut residual_weights {
            *w /= residual_sum;
        }
    }

    // Random sampling for remaining particles
    let remaining = num_samples - indices.len();
    if remaining > 0 {
        // Use systematic resampling for the residuals
        let step = 1.0 / remaining as f64;
        let start: f64 = rng.random::<f64>() * step;

        let mut cumsum = 0.0;
        let mut i = 0;

        for j in 0..remaining {
            let u = start + (j as f64) * step;

            while cumsum < u && i < residual_weights.len() {
                cumsum += residual_weights[i];
                i += 1;
            }

            let idx = if i > 0 { i - 1 } else { 0 };
            indices.push(idx.min(weights.len() - 1));
        }
    }

    indices
}

// ============= ParticleFilter Trait ==========================================

/// Trait for marking a navigation system as particle filter-based
///
/// This trait provides a common interface for all particle filter implementations.
/// It is designed to be a thin wrapper around the generic resampling functions
/// provided by this module.
pub trait ParticleFilter {
    /// Resample particles based on their weights
    ///
    /// This method should implement resampling logic to combat particle degeneracy.
    /// Implementations should use the generic resampling functions provided by this
    /// module (systematic_resample, multinomial_resample, etc.) as appropriate.
    ///
    /// Note: Weights should be normalized before resampling so that they
    /// sum to 1.0. Call weight normalization first if needed.
    fn resample(&mut self);
}

// ==================== Unit Tests =============================================
#[cfg(test)]
mod tests {
    use super::*;

    // ============= Tests for standalone resampling functions ==================

    #[test]
    fn test_standalone_multinomial_resample() {
        let weights = vec![0.1, 0.3, 0.4, 0.2];
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);
        let indices = multinomial_resample(&weights, 100, &mut rng);

        assert_eq!(indices.len(), 100);

        // All indices should be valid
        for &idx in &indices {
            assert!(idx < weights.len());
        }

        // With 100 samples, distribution should roughly match weights
        let mut counts = vec![0; weights.len()];
        for &idx in &indices {
            counts[idx] += 1;
        }

        // Check that higher weight particles are sampled more often
        assert!(counts[2] > counts[0]); // 0.4 > 0.1
        assert!(counts[1] > counts[0]); // 0.3 > 0.1
    }

    #[test]
    fn test_standalone_systematic_resample() {
        let weights = vec![0.25, 0.25, 0.25, 0.25]; // Uniform weights
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);
        let indices = systematic_resample(&weights, 100, &mut rng);

        assert_eq!(indices.len(), 100);

        // With uniform weights, each particle should be selected roughly equally
        let mut counts = vec![0; weights.len()];
        for &idx in &indices {
            counts[idx] += 1;
        }

        // Each should be selected about 25 times
        for count in counts {
            let diff = if count > 25 { count - 25 } else { 25 - count };
            assert!(diff <= 5); // Allow some variance
        }
    }

    #[test]
    fn test_standalone_stratified_resample() {
        let weights = vec![0.1, 0.2, 0.3, 0.4];
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);
        let indices = stratified_resample(&weights, 50, &mut rng);

        assert_eq!(indices.len(), 50);

        // All indices should be valid
        for &idx in &indices {
            assert!(idx < weights.len());
        }
    }

    #[test]
    fn test_standalone_residual_resample() {
        let weights = vec![0.1, 0.3, 0.4, 0.2];
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);
        let indices = residual_resample(&weights, 10, &mut rng);

        assert_eq!(indices.len(), 10);

        // Count occurrences
        let mut counts = vec![0; weights.len()];
        for &idx in &indices {
            counts[idx] += 1;
        }

        // Particle with weight 0.4 should appear at least 4 times (deterministic part)
        // Particle with weight 0.3 should appear at least 3 times
        assert!(counts[2] >= 4); // 0.4 * 10 = 4.0
        assert!(counts[1] >= 3); // 0.3 * 10 = 3.0
    }

    #[test]
    fn test_resampling_functions_preserve_particle_count() {
        let weights = vec![0.1, 0.2, 0.3, 0.4];
        let num_samples = 100;
        let mut rng = rand::rngs::StdRng::seed_from_u64(42);

        // Test all resampling methods
        let multinomial_indices = multinomial_resample(&weights, num_samples, &mut rng);
        assert_eq!(multinomial_indices.len(), num_samples);

        let systematic_indices = systematic_resample(&weights, num_samples, &mut rng);
        assert_eq!(systematic_indices.len(), num_samples);

        let stratified_indices = stratified_resample(&weights, num_samples, &mut rng);
        assert_eq!(stratified_indices.len(), num_samples);

        let residual_indices = residual_resample(&weights, num_samples, &mut rng);
        assert_eq!(residual_indices.len(), num_samples);
    }
}