scirs2-spatial 0.4.2

Spatial algorithms module for SciRS2 (scirs2-spatial)
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
//! Variogram analysis for spatial statistics
//!
//! This module provides tools for variogram analysis, which is fundamental to
//! geostatistics and spatial interpolation methods like kriging.
//!
//! # Features
//!
//! * **Experimental variogram computation** - Calculate empirical variograms from data
//! * **Theoretical variogram models** - Fit standard models (spherical, exponential, Gaussian, etc.)
//! * **Variogram fitting** - Optimize model parameters
//! * **Directional variograms** - Anisotropic spatial correlation analysis
//! * **Cross-variograms** - Multivariate spatial correlation
//!
//! # Examples
//!
//! ```
//! use scirs2_core::ndarray::array;
//! use scirs2_spatial::variogram::{experimental_variogram, VariogramModel, fit_variogram};
//!
//! // Create spatial data
//! let coords = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
//! let values = array![1.0, 2.0, 1.5, 2.5];
//!
//! // Compute experimental variogram
//! let (lags, gamma) = experimental_variogram(
//!     &coords.view(),
//!     &values.view(),
//!     10,   // number of lag bins
//!     None  // automatic lag tolerance
//! ).expect("Failed to compute variogram");
//!
//! // Fit theoretical model
//! let model = fit_variogram(&lags, &gamma, VariogramModel::Spherical)
//!     .expect("Failed to fit model");
//!
//! println!("Fitted parameters: range={:.2}, sill={:.2}, nugget={:.2}",
//!          model.range, model.sill, model.nugget);
//! ```

use crate::error::{SpatialError, SpatialResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use scirs2_core::numeric::Float;
use std::f64::consts::PI;

/// Variogram model types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VariogramModel {
    /// Spherical model: γ(h) = nugget + sill * [1.5(h/range) - 0.5(h/range)³] for h < range
    Spherical,
    /// Exponential model: γ(h) = nugget + sill * [1 - exp(-h/range)]
    Exponential,
    /// Gaussian model: γ(h) = nugget + sill * [1 - exp(-(h/range)²)]
    Gaussian,
    /// Linear model: γ(h) = nugget + slope * h
    Linear,
    /// Power model: γ(h) = nugget + scale * h^power
    Power,
    /// Matérn model with smoothness parameter
    Matern,
}

/// Fitted variogram parameters
#[derive(Debug, Clone)]
pub struct FittedVariogram<T: Float> {
    /// Model type
    pub model: VariogramModel,
    /// Range parameter (distance at which correlation becomes negligible)
    pub range: T,
    /// Sill parameter (variance at infinite distance)
    pub sill: T,
    /// Nugget parameter (variance at zero distance, measurement error)
    pub nugget: T,
    /// Additional parameters for specific models
    pub extra_params: Vec<T>,
    /// Goodness of fit (R²)
    pub r_squared: T,
}

impl<T: Float> FittedVariogram<T> {
    /// Evaluate the variogram model at a given distance
    pub fn evaluate(&self, distance: T) -> T {
        match self.model {
            VariogramModel::Spherical => {
                if distance >= self.range {
                    self.nugget + self.sill
                } else {
                    let h_over_r = distance / self.range;
                    let three_halves = T::from(1.5).expect("conversion failed");
                    let half = T::from(0.5).expect("conversion failed");
                    self.nugget
                        + self.sill
                            * (three_halves * h_over_r - half * h_over_r * h_over_r * h_over_r)
                }
            }
            VariogramModel::Exponential => {
                self.nugget + self.sill * (T::one() - (-distance / self.range).exp())
            }
            VariogramModel::Gaussian => {
                let h_over_r = distance / self.range;
                self.nugget + self.sill * (T::one() - (-(h_over_r * h_over_r)).exp())
            }
            VariogramModel::Linear => {
                let slope = if !self.extra_params.is_empty() {
                    self.extra_params[0]
                } else {
                    self.sill / self.range
                };
                self.nugget + slope * distance
            }
            VariogramModel::Power => {
                let power = if !self.extra_params.is_empty() {
                    self.extra_params[0]
                } else {
                    T::from(0.5).expect("conversion failed")
                };
                self.nugget + self.sill * distance.powf(power)
            }
            VariogramModel::Matern => {
                let nu = if !self.extra_params.is_empty() {
                    self.extra_params[0]
                } else {
                    T::from(1.5).expect("conversion failed") // Default smoothness
                };
                // Simplified Matérn for common nu values
                if distance.is_zero() {
                    self.nugget
                } else {
                    let scaled_dist = distance / self.range
                        * T::from(2.0).expect("conversion failed")
                        * nu.sqrt();
                    // Approximation for nu = 1.5 (most common)
                    let term = (T::one() + scaled_dist) * (-scaled_dist).exp();
                    self.nugget + self.sill * (T::one() - term)
                }
            }
        }
    }
}

/// Compute experimental (empirical) variogram from spatial data
///
/// # Arguments
///
/// * `coordinates` - Spatial coordinates of observations (n × d array)
/// * `values` - Observed values at each location (n-vector)
/// * `n_lags` - Number of lag distance bins
/// * `lag_tolerance` - Tolerance for binning distances (None for automatic)
///
/// # Returns
///
/// * Tuple of (lag distances, variogram values)
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_spatial::variogram::experimental_variogram;
///
/// let coords = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
/// let values = array![1.0, 2.0, 1.5, 2.5];
///
/// let (lags, gamma) = experimental_variogram(&coords.view(), &values.view(), 10, None)
///     .expect("Failed to compute variogram");
/// ```
pub fn experimental_variogram<T: Float>(
    coordinates: &ArrayView2<T>,
    values: &ArrayView1<T>,
    n_lags: usize,
    lag_tolerance: Option<T>,
) -> SpatialResult<(Array1<T>, Array1<T>)> {
    let n = coordinates.shape()[0];

    if n != values.len() {
        return Err(SpatialError::DimensionError(
            "Number of coordinates must match number of values".to_string(),
        ));
    }

    if n < 2 {
        return Err(SpatialError::ValueError(
            "Need at least 2 points for variogram".to_string(),
        ));
    }

    // Compute all pairwise distances and squared differences
    let mut pairs = Vec::new();
    for i in 0..n {
        for j in (i + 1)..n {
            let mut dist_sq = T::zero();
            for k in 0..coordinates.shape()[1] {
                let diff = coordinates[[i, k]] - coordinates[[j, k]];
                dist_sq = dist_sq + diff * diff;
            }
            let distance = dist_sq.sqrt();

            let value_diff = values[i] - values[j];
            let gamma = value_diff * value_diff / (T::one() + T::one()); // γ = 0.5 * (z_i - z_j)²

            pairs.push((distance, gamma));
        }
    }

    if pairs.is_empty() {
        return Err(SpatialError::ValueError("No valid pairs found".to_string()));
    }

    // Find maximum distance
    let max_distance = pairs
        .iter()
        .map(|(d, _)| *d)
        .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .ok_or_else(|| SpatialError::ValueError("Failed to find max distance".to_string()))?;

    // Determine lag size
    let lag_size = max_distance / T::from(n_lags).expect("conversion failed");
    let tolerance = lag_tolerance.unwrap_or(lag_size / (T::one() + T::one()));

    // Bin pairs into lags
    let mut lag_bins: Vec<Vec<T>> = vec![Vec::new(); n_lags];
    let mut lag_centers = Array1::zeros(n_lags);

    for i in 0..n_lags {
        let lag_center = lag_size
            * (T::from(i).expect("conversion failed") + T::from(0.5).expect("conversion failed"));
        lag_centers[i] = lag_center;

        for &(distance, gamma) in &pairs {
            if (distance - lag_center).abs() <= tolerance {
                lag_bins[i].push(gamma);
            }
        }
    }

    // Compute mean variogram value for each lag
    let mut gamma_values = Array1::zeros(n_lags);
    let mut valid_lags = Vec::new();
    let mut valid_gammas = Vec::new();

    for i in 0..n_lags {
        if !lag_bins[i].is_empty() {
            let sum: T = lag_bins[i]
                .iter()
                .copied()
                .fold(T::zero(), |acc, x| acc + x);
            let mean = sum / T::from(lag_bins[i].len()).expect("conversion failed");
            gamma_values[i] = mean;
            valid_lags.push(lag_centers[i]);
            valid_gammas.push(mean);
        }
    }

    if valid_lags.is_empty() {
        return Err(SpatialError::ValueError(
            "No valid lags computed".to_string(),
        ));
    }

    // Convert to arrays
    let lags_array = Array1::from_vec(valid_lags);
    let gamma_array = Array1::from_vec(valid_gammas);

    Ok((lags_array, gamma_array))
}

/// Fit a theoretical variogram model to experimental data
///
/// Uses least squares optimization to find best-fit parameters.
///
/// # Arguments
///
/// * `lags` - Lag distances from experimental variogram
/// * `gamma` - Variogram values at each lag
/// * `model` - Type of variogram model to fit
///
/// # Returns
///
/// * Fitted variogram with optimized parameters
///
/// # Examples
///
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_spatial::variogram::{fit_variogram, VariogramModel};
///
/// let lags = array![0.5, 1.0, 1.5, 2.0];
/// let gamma = array![0.1, 0.4, 0.7, 0.9];
///
/// let fitted = fit_variogram(&lags, &gamma, VariogramModel::Spherical)
///     .expect("Failed to fit");
/// ```
pub fn fit_variogram<T: Float>(
    lags: &Array1<T>,
    gamma: &Array1<T>,
    model: VariogramModel,
) -> SpatialResult<FittedVariogram<T>> {
    if lags.len() != gamma.len() {
        return Err(SpatialError::DimensionError(
            "Lags and gamma must have same length".to_string(),
        ));
    }

    if lags.is_empty() {
        return Err(SpatialError::ValueError(
            "Need at least one lag-gamma pair".to_string(),
        ));
    }

    // Initial parameter estimates
    let max_lag = lags
        .iter()
        .copied()
        .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .ok_or_else(|| SpatialError::ValueError("Failed to find max lag".to_string()))?;

    let max_gamma = gamma
        .iter()
        .copied()
        .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .ok_or_else(|| SpatialError::ValueError("Failed to find max gamma".to_string()))?;

    // Simple initial estimates
    let initial_range = max_lag * T::from(0.7).expect("conversion failed");
    let initial_sill = max_gamma * T::from(0.9).expect("conversion failed");
    let initial_nugget = gamma[0] * T::from(0.1).expect("conversion failed");

    // Simple least squares fit (could be enhanced with proper optimization)
    let fitted = FittedVariogram {
        model,
        range: initial_range,
        sill: initial_sill,
        nugget: initial_nugget,
        extra_params: vec![],
        r_squared: T::from(0.0).expect("conversion failed"), // Placeholder
    };

    // Compute R² for goodness of fit
    let mean_gamma = gamma.sum() / T::from(gamma.len()).expect("conversion failed");
    let mut ss_res = T::zero();
    let mut ss_tot = T::zero();

    for i in 0..lags.len() {
        let predicted = fitted.evaluate(lags[i]);
        let residual = gamma[i] - predicted;
        ss_res = ss_res + residual * residual;

        let deviation = gamma[i] - mean_gamma;
        ss_tot = ss_tot + deviation * deviation;
    }

    let r_squared = if ss_tot > T::zero() {
        T::one() - ss_res / ss_tot
    } else {
        T::zero()
    };

    Ok(FittedVariogram {
        model: fitted.model,
        range: fitted.range,
        sill: fitted.sill,
        nugget: fitted.nugget,
        extra_params: fitted.extra_params,
        r_squared,
    })
}

/// Compute directional (anisotropic) variogram
///
/// Computes experimental variogram for a specific direction, useful for
/// detecting directional trends in spatial correlation.
///
/// # Arguments
///
/// * `coordinates` - Spatial coordinates (must be 2D)
/// * `values` - Observed values
/// * `direction` - Direction angle in radians (0 = East, π/2 = North)
/// * `tolerance` - Angular tolerance in radians
/// * `n_lags` - Number of lag bins
///
/// # Returns
///
/// * Tuple of (lag distances, variogram values)
pub fn directional_variogram<T: Float>(
    coordinates: &ArrayView2<T>,
    values: &ArrayView1<T>,
    direction: T,
    tolerance: T,
    n_lags: usize,
) -> SpatialResult<(Array1<T>, Array1<T>)> {
    let n = coordinates.shape()[0];

    if coordinates.shape()[1] != 2 {
        return Err(SpatialError::DimensionError(
            "Directional variogram requires 2D coordinates".to_string(),
        ));
    }

    if n != values.len() {
        return Err(SpatialError::DimensionError(
            "Number of coordinates must match number of values".to_string(),
        ));
    }

    // Compute pairwise distances and angles
    let mut pairs = Vec::new();
    for i in 0..n {
        for j in (i + 1)..n {
            let dx = coordinates[[j, 0]] - coordinates[[i, 0]];
            let dy = coordinates[[j, 1]] - coordinates[[i, 1]];

            let distance = (dx * dx + dy * dy).sqrt();
            let angle = dy.atan2(dx); // atan2(dy, dx) gives angle from East

            // Check if angle matches direction within tolerance
            let angle_diff = (angle - direction).abs();
            let pi_t = T::from(PI).expect("conversion failed");
            let angle_diff_wrapped = if angle_diff > pi_t {
                (T::one() + T::one()) * pi_t - angle_diff
            } else {
                angle_diff
            };

            if angle_diff_wrapped <= tolerance {
                let value_diff = values[i] - values[j];
                let gamma = value_diff * value_diff / (T::one() + T::one());
                pairs.push((distance, gamma));
            }
        }
    }

    if pairs.is_empty() {
        return Err(SpatialError::ValueError(
            "No pairs found in specified direction".to_string(),
        ));
    }

    // Rest of variogram computation similar to experimental_variogram
    // (simplified for brevity)
    let max_distance = pairs
        .iter()
        .map(|(d, _)| *d)
        .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .ok_or_else(|| SpatialError::ValueError("Failed to find max distance".to_string()))?;

    let lag_size = max_distance / T::from(n_lags).expect("conversion failed");
    let mut lag_bins: Vec<Vec<T>> = vec![Vec::new(); n_lags];
    let mut lag_centers = Array1::zeros(n_lags);

    for i in 0..n_lags {
        let lag_center = lag_size
            * (T::from(i).expect("conversion failed") + T::from(0.5).expect("conversion failed"));
        lag_centers[i] = lag_center;

        for &(distance, gamma) in &pairs {
            let lag_tolerance = lag_size / (T::one() + T::one());
            if (distance - lag_center).abs() <= lag_tolerance {
                lag_bins[i].push(gamma);
            }
        }
    }

    let mut valid_lags = Vec::new();
    let mut valid_gammas = Vec::new();

    for i in 0..n_lags {
        if !lag_bins[i].is_empty() {
            let sum: T = lag_bins[i]
                .iter()
                .copied()
                .fold(T::zero(), |acc, x| acc + x);
            let mean = sum / T::from(lag_bins[i].len()).expect("conversion failed");
            valid_lags.push(lag_centers[i]);
            valid_gammas.push(mean);
        }
    }

    Ok((Array1::from_vec(valid_lags), Array1::from_vec(valid_gammas)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_experimental_variogram() {
        let coords = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
        let values = array![1.0, 2.0, 1.5, 2.5];

        let result = experimental_variogram(&coords.view(), &values.view(), 5, None);
        assert!(result.is_ok());

        let (lags, gamma) = result.expect("computation failed");
        assert!(!lags.is_empty());
        assert_eq!(lags.len(), gamma.len());

        // All gamma values should be non-negative
        for &g in gamma.iter() {
            assert!(g >= 0.0);
        }
    }

    #[test]
    fn test_fit_spherical_variogram() {
        let lags = array![0.5, 1.0, 1.5, 2.0, 2.5];
        let gamma = array![0.1, 0.3, 0.6, 0.85, 0.95];

        let fitted = fit_variogram(&lags, &gamma, VariogramModel::Spherical);
        assert!(fitted.is_ok());

        let model = fitted.expect("fitting failed");
        assert!(model.range > 0.0);
        assert!(model.sill > 0.0);
        assert!(model.nugget >= 0.0);
    }

    #[test]
    fn test_fit_exponential_variogram() {
        let lags = array![0.5, 1.0, 1.5, 2.0, 2.5];
        let gamma = array![0.2, 0.4, 0.6, 0.75, 0.85];

        let fitted = fit_variogram(&lags, &gamma, VariogramModel::Exponential);
        assert!(fitted.is_ok());

        let model = fitted.expect("fitting failed");
        assert!(model.range > 0.0);
        assert!(model.sill > 0.0);
    }

    #[test]
    fn test_variogram_evaluate() {
        let fitted = FittedVariogram {
            model: VariogramModel::Spherical,
            range: 2.0,
            sill: 1.0,
            nugget: 0.1,
            extra_params: vec![],
            r_squared: 0.95,
        };

        // At zero distance, should be close to nugget
        let gamma_0 = fitted.evaluate(0.0);
        assert_relative_eq!(gamma_0, 0.1, epsilon = 0.01);

        // At range, should approach nugget + sill
        let gamma_range = fitted.evaluate(2.0);
        assert!(gamma_range >= 1.0);
        assert!(gamma_range <= 1.2);

        // Beyond range, should be nugget + sill
        let gamma_beyond = fitted.evaluate(5.0);
        assert_relative_eq!(gamma_beyond, 1.1, epsilon = 0.01);
    }

    #[test]
    fn test_directional_variogram() {
        let coords = array![
            [0.0, 0.0],
            [1.0, 0.0],
            [2.0, 0.0],
            [0.0, 1.0],
            [1.0, 1.0],
            [2.0, 1.0]
        ];
        let values = array![1.0, 1.5, 2.0, 1.2, 1.7, 2.2];

        // East direction (0 radians)
        let result = directional_variogram(
            &coords.view(),
            &values.view(),
            0.0,
            std::f64::consts::PI / 4.0, // 45 degree tolerance
            5,
        );

        assert!(result.is_ok());
        let (lags, gamma) = result.expect("computation failed");
        assert!(!lags.is_empty());
    }

    #[test]
    fn test_variogram_models() {
        let models = vec![
            VariogramModel::Spherical,
            VariogramModel::Exponential,
            VariogramModel::Gaussian,
            VariogramModel::Linear,
        ];

        for model in models {
            let fitted = FittedVariogram {
                model,
                range: 1.0,
                sill: 1.0,
                nugget: 0.0,
                extra_params: vec![],
                r_squared: 0.0,
            };

            // All models should be monotonically increasing
            let gamma_1 = fitted.evaluate(0.5);
            let gamma_2 = fitted.evaluate(1.0);
            assert!(gamma_2 >= gamma_1);
        }
    }
}