wbspatialstats 0.1.1

Unified spatial statistics library: kriging, spatial autocorrelation, spatial regression, point process analysis
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Spatial autocorrelation measures (Phase A & Phase A+: Permutation Testing)
//
// Provides global and local indicators of spatial association:
// - Global Moran's I: Overall spatial autocorrelation
// - Local Moran's I (LISA): Local clustering indicators
// - Getis-Ord G and G*: Hot/cold spot analysis
// - Nearest Neighbor Index (NNI): CSR hypothesis testing
// - Quadrat Analysis: Count-based spatial pattern analysis
//
// Phase A+ adds permutation-based inference (robust, distribution-free):
// - Permutation testing for Moran's I (global and local)
// - Permutation testing for Getis-Ord statistics
// - FDR-BH multiple testing correction

pub mod permutation;

use crate::weights::SpatialWeightsGraph;
use rayon::prelude::*;

// Re-export key permutation testing types for convenience
pub use permutation::{
    apply_fdr_bh_correction, getis_ord_gi_star_permutation, local_morans_i_permutation,
    morans_i_permutation, LocalPermutationTestResult, PermutationTestResult,
};

/// Result of global spatial autocorrelation analysis
#[derive(Debug, Clone)]
pub struct GlobalAutocorrelationResult {
    /// Statistic value (e.g., Moran's I)
    pub statistic: f64,
    /// Expected value under null hypothesis
    pub expected_value: f64,
    /// Variance of the statistic
    pub variance: f64,
    /// Standardized z-score
    pub z_score: f64,
    /// Two-tailed p-value
    pub p_value: f64,
    /// Number of features used in computation
    pub n_features: usize,
}

/// Result of local spatial association analysis (LISA)
#[derive(Debug, Clone)]
pub struct LocalAssociationResult {
    /// Local statistic value for each feature
    pub local_statistics: Vec<f64>,
    /// Expected value for each feature
    pub expected_values: Vec<f64>,
    /// Variance for each feature
    pub variances: Vec<f64>,
    /// Z-scores for each feature
    pub z_scores: Vec<f64>,
    /// P-values for each feature
    pub p_values: Vec<f64>,
    /// Cluster classification: "HH", "LL", "HL", "LH", "insignificant"
    pub cluster_types: Vec<String>,
}

/// Result of Getis-Ord G statistic analysis
#[derive(Debug, Clone)]
pub struct GetisOrdResult {
    /// G or G* statistic value
    pub statistic: f64,
    /// Expected value under null hypothesis
    pub expected_value: f64,
    /// Variance of the statistic
    pub variance: f64,
    /// Z-score
    pub z_score: f64,
    /// Two-tailed p-value
    pub p_value: f64,
}

/// Result of local Getis-Ord G* analysis (per-feature)
#[derive(Debug, Clone)]
pub struct LocalGetisOrdResult {
    /// Local G* statistic for each feature
    pub local_statistics: Vec<f64>,
    /// Expected values for each feature
    pub expected_values: Vec<f64>,
    /// Variances for each feature
    pub variances: Vec<f64>,
    /// Z-scores for each feature
    pub z_scores: Vec<f64>,
    /// P-values for each feature
    pub p_values: Vec<f64>,
    /// Cluster type: "HotSpot", "ColdSpot", "insignificant"
    pub cluster_types: Vec<String>,
}

/// Result of Nearest Neighbor Index analysis
#[derive(Debug, Clone)]
pub struct NearestNeighborIndexResult {
    /// Observed mean nearest neighbor distance
    pub observed_distance: f64,
    /// Expected mean nearest neighbor distance under CSR
    pub expected_distance: f64,
    /// Nearest Neighbor Index (ratio of observed to expected)
    pub nni: f64,
    /// Z-score for spatial clustering test
    pub z_score: f64,
    /// Two-tailed p-value
    pub p_value: f64,
    /// Interpretation: "Clustered", "Random", "Dispersed"
    pub interpretation: String,
}

/// Result of Quadrat analysis
#[derive(Debug, Clone)]
pub struct QuadratAnalysisResult {
    /// Chi-square test statistic
    pub chi_square: f64,
    /// Degrees of freedom
    pub degrees_of_freedom: usize,
    /// P-value
    pub p_value: f64,
    /// Variance-to-mean ratio (dispersion index)
    pub variance_mean_ratio: f64,
    /// Number of quadrats
    pub n_quadrats: usize,
}

/// Compute Global Moran's I with asymptotic inference
///
/// # Arguments
/// - `values`: Data values at features
/// - `weights`: Spatial weights graph
///
/// # Returns
/// Global Moran's I statistic and inference results
pub fn morans_i(values: &[f64], weights: &SpatialWeightsGraph) -> Result<GlobalAutocorrelationResult, String> {
    if values.len() != weights.n_features() {
        return Err("Values and weights must have same number of features".to_string());
    }

    if values.len() < 3 {
        return Err("At least 3 features required for Moran's I".to_string());
    }

    let n = values.len() as f64;
    let mean = values.iter().sum::<f64>() / n;
    let deviations: Vec<f64> = values.iter().map(|v| v - mean).collect();

    // Numerator: sum of cross-products of neighboring deviations
    let mut numerator = 0.0;
    let mut neighbor_count = 0usize;
    
    for (i, neighbors) in weights.neighbors.iter().enumerate() {
        for (j, weight) in neighbors {
            numerator += weight * deviations[i] * deviations[*j];
            neighbor_count += 1;
        }
    }

    // Denominator: sum of squared deviations
    let denominator: f64 = deviations.iter().map(|d| d * d).sum();

    if denominator == 0.0 {
        return Err("Deviations are zero; cannot compute Moran's I".to_string());
    }

    // Moran's I
    let sum_weights: f64 = weights.neighbors.iter().flatten().map(|(_, w)| w).sum();
    let i_stat = (n / sum_weights) * (numerator / denominator);

    // Expected value under null hypothesis (no autocorrelation)
    let expected_i = -1.0 / (n - 1.0);

    // Variance approximation (simplified for now)
    // A full computation would involve higher-order moments and more careful numerical handling
    let variance = if neighbor_count > 0 {
        // Simple approximation: variance is roughly proportional to n
        (1.0 + (sum_weights / n)) / ((n - 1.0) * sum_weights)
    } else {
        1.0
    };

    let z_score = if variance > 0.0 {
        (i_stat - expected_i) / variance.sqrt()
    } else {
        0.0
    };
    let p_value = 2.0 * (1.0 - crate::weights::normal_cdf(z_score.abs()));

    Ok(GlobalAutocorrelationResult {
        statistic: i_stat,
        expected_value: expected_i,
        variance: variance.max(0.0),
        z_score,
        p_value,
        n_features: values.len(),
    })
}

/// Compute Local Moran's I (LISA) with cluster classification
///
/// # Arguments
/// - `values`: Data values at features
/// - `weights`: Spatial weights graph
/// - `alpha`: Significance threshold (e.g., 0.05)
///
/// # Returns
/// Per-feature LISA statistics with cluster types ("HH", "LL", "HL", "LH", "insignificant")
pub fn local_morans_i_lisa(values: &[f64], weights: &SpatialWeightsGraph, alpha: f64) -> Result<LocalAssociationResult, String> {
    if values.len() != weights.n_features() {
        return Err("Values and weights must have same number of features".to_string());
    }

    if values.len() < 3 {
        return Err("At least 3 features required".to_string());
    }

    let n = values.len() as f64;
    let mean = values.iter().sum::<f64>() / n;
    let centered: Vec<f64> = values.iter().map(|v| v - mean).collect();
    let s2: f64 = centered.iter().map(|z| z * z).sum::<f64>() / n;
    
    if s2 <= 0.0 {
        return Err("Data variance is zero; LISA is undefined".to_string());
    }

    let s = s2.sqrt();
    let z: Vec<f64> = centered.iter().map(|c| c / s).collect();
    let b2 = z.iter().map(|zi| zi.powi(4)).sum::<f64>() / n;

    let mut local_i = vec![0.0; values.len()];
    let mut expected_vals = vec![0.0; values.len()];
    let mut variances = vec![0.0; values.len()];
    let mut z_scores = vec![0.0; values.len()];
    let mut p_values = vec![0.0; values.len()];
    let mut cluster_types = vec!["insignificant".to_string(); values.len()];

    // Parallel computation of per-feature LISA statistics
    let results: Vec<_> = (0..values.len())
        .into_par_iter()
        .map(|i| {
            let mut lag_z = 0.0;
            let mut wi = 0.0;
            let mut wi2 = 0.0;

            for (j, w) in &weights.neighbors[i] {
                lag_z += w * z[*j];
                wi += w;
                wi2 += w * w;
            }

            if wi == 0.0 {
                return (
                    0.0, 0.0, 0.0, 0.0, 0.0, "insignificant".to_string(),
                );
            }

            let i_stat = z[i] * lag_z;
            let expected = -wi / (n - 1.0);
            let var_raw = ((n - b2) / (n - 1.0)) * wi2 + ((2.0 * b2 - n) / ((n - 1.0) * (n - 2.0))) * (wi * wi - wi2) - expected * expected;
            
            if var_raw.is_finite() && var_raw > 1.0e-12 {
                let zscore = (i_stat - expected) / var_raw.sqrt();
                let p = crate::weights::two_tailed_normal_p(zscore);
                let cluster = if p <= alpha {
                    if z[i] >= 0.0 && lag_z >= 0.0 {
                        "HH".to_string()
                    } else if z[i] < 0.0 && lag_z < 0.0 {
                        "LL".to_string()
                    } else if z[i] >= 0.0 && lag_z < 0.0 {
                        "HL".to_string()
                    } else {
                        "LH".to_string()
                    }
                } else {
                    "insignificant".to_string()
                };
                (i_stat, expected, var_raw, zscore, p, cluster)
            } else {
                (i_stat, expected, 0.0, 0.0, 1.0, "insignificant".to_string())
            }
        })
        .collect();

    // Unpack parallel results into output vectors
    for (i, (i_stat, expected, var_raw, zscore, p, cluster)) in results.into_iter().enumerate() {
        local_i[i] = i_stat;
        expected_vals[i] = expected;
        variances[i] = var_raw;
        z_scores[i] = zscore;
        p_values[i] = p;
        cluster_types[i] = cluster;
    }

    Ok(LocalAssociationResult {
        local_statistics: local_i,
        expected_values: expected_vals,
        variances,
        z_scores,
        p_values,
        cluster_types,
    })
}

/// Compute Getis-Ord G statistic for global hot/cold spot analysis
///
/// # Arguments
/// - `values`: Data values at features
/// - `weights`: Spatial weights graph (usually distance-based, symmetric)
///
/// # Returns
/// Global G statistic with inference
pub fn getis_ord_g(values: &[f64], weights: &SpatialWeightsGraph) -> Result<GetisOrdResult, String> {
    if values.len() != weights.n_features() {
        return Err("Values and weights must have same number of features".to_string());
    }

    if values.len() < 3 {
        return Err("At least 3 features required".to_string());
    }

    let n = values.len() as f64;
    let mut numerator = 0.0;
    let mut sum_weights = 0.0;

    for (i, neighbors) in weights.neighbors.iter().enumerate() {
        for (j, weight) in neighbors {
            if i != *j {  // Getis-Ord G excludes self
                numerator += weight * values[i] * values[*j];
                sum_weights += weight;
            }
        }
    }

    let sum_val: f64 = values.iter().sum();
    let sum_sq: f64 = values.iter().map(|v| v * v).sum();

    if sum_val == 0.0 || sum_weights == 0.0 {
        return Err("Cannot compute G: zero sum or weights".to_string());
    }

    let g = numerator / (sum_val * sum_val);
    let expected_g = sum_weights / (n * (n - 1.0));

    let b2 = values.iter().map(|v| v.powi(4)).sum::<f64>();
    let _s2 = sum_sq / n - (sum_val / n).powi(2);

    let var_numerator = (n * (n - 3.0) * sum_weights.powi(2) + sum_weights.powi(2) - 2.0 * (n - 1.0) * sum_weights) * sum_sq - (n - 1.0) * b2 * sum_weights.powi(2);
    let var_denominator = (n * (n - 1.0) * (sum_val / n).powi(4)).powi(2);

    let variance = if var_denominator > 0.0 { var_numerator / var_denominator } else { 0.0 };

    let z_score = if variance > 0.0 { (g - expected_g) / variance.sqrt() } else { 0.0 };
    let p_value = crate::weights::two_tailed_normal_p(z_score);

    Ok(GetisOrdResult {
        statistic: g,
        expected_value: expected_g,
        variance: variance.max(0.0),
        z_score,
        p_value,
    })
}

/// Compute local Getis-Ord G* statistic (includes self)
///
/// # Arguments
/// - `values`: Data values at features
/// - `weights`: Spatial weights graph (includes self-loops for G*)
///
/// # Returns
/// Per-feature G* statistics with cluster types ("HotSpot", "ColdSpot", "insignificant")
pub fn getis_ord_g_star(values: &[f64], weights: &SpatialWeightsGraph, alpha: f64) -> Result<LocalGetisOrdResult, String> {
    if values.len() != weights.n_features() {
        return Err("Values and weights must have same number of features".to_string());
    }

    if values.len() < 3 {
        return Err("At least 3 features required".to_string());
    }

    let n = values.len() as f64;
    let sum_val: f64 = values.iter().sum();
    let sum_sq: f64 = values.iter().map(|v| v * v).sum();
    let _b2 = values.iter().map(|v| v.powi(4)).sum::<f64>();

    // Parallel computation of per-feature Getis-Ord G* statistics
    let results: Vec<_> = (0..values.len())
        .into_par_iter()
        .map(|i| {
            let mut sum_wy = 0.0;
            let mut wi = 0.0;
            let mut wi2 = 0.0;

            for (j, w) in &weights.neighbors[i] {
                sum_wy += w * values[*j];
                wi += w;
                wi2 += w * w;
            }

            if wi == 0.0 {
                return (
                    0.0, 0.0, 0.0, 0.0, 1.0, "insignificant".to_string(),
                );
            }

            let g_local = sum_wy / sum_val;
            let expected = wi / (n - 1.0);

            let var_numerator = (n - 1.0) * (sum_sq * wi2 - (wi * wi)) - 2.0 * (n - 2.0) * wi.powi(2) * sum_val;
            let var_denominator = (n - 1.0).powi(2) * sum_val.powi(2);
            let variance = if var_denominator > 0.0 { var_numerator / var_denominator } else { 0.0 };
            let variance = variance.max(0.0);

            if variance > 0.0 {
                let zscore = (g_local - expected) / variance.sqrt();
                let p = crate::weights::two_tailed_normal_p(zscore);
                let cluster = if p <= alpha {
                    if zscore > 0.0 { "HotSpot".to_string() } else { "ColdSpot".to_string() }
                } else {
                    "insignificant".to_string()
                };
                (g_local, expected, variance, zscore, p, cluster)
            } else {
                (g_local, expected, 0.0, 0.0, 1.0, "insignificant".to_string())
            }
        })
        .collect();

    // Unpack parallel results into output vectors
    let mut local_g = vec![0.0; values.len()];
    let mut expected_vals = vec![0.0; values.len()];
    let mut variances = vec![0.0; values.len()];
    let mut z_scores = vec![0.0; values.len()];
    let mut p_values = vec![0.0; values.len()];
    let mut cluster_types = vec!["insignificant".to_string(); values.len()];

    for (i, (g, exp, var, z, p, cluster)) in results.into_iter().enumerate() {
        local_g[i] = g;
        expected_vals[i] = exp;
        variances[i] = var;
        z_scores[i] = z;
        p_values[i] = p;
        cluster_types[i] = cluster;
    }

    Ok(LocalGetisOrdResult {
        local_statistics: local_g,
        expected_values: expected_vals,
        variances,
        z_scores,
        p_values,
        cluster_types,
    })
}

/// Compute Nearest Neighbor Index for spatial point pattern analysis
///
/// # Arguments
/// - `coordinates`: (x, y) coordinates of points
///
/// # Returns
/// NNI statistic with interpretation ("Clustered", "Random", "Dispersed")
pub fn nearest_neighbor_index(coordinates: &[(f64, f64)]) -> Result<NearestNeighborIndexResult, String> {
    if coordinates.len() < 2 {
        return Err("At least 2 points required for NNI".to_string());
    }

    let n = coordinates.len();
    let mut sum_nn_dist = 0.0;

    // Find nearest neighbor distance for each point
    for i in 0..n {
        let mut min_dist = f64::INFINITY;
        for j in 0..n {
            if i != j {
                let dx = coordinates[i].0 - coordinates[j].0;
                let dy = coordinates[i].1 - coordinates[j].1;
                let dist = (dx * dx + dy * dy).sqrt();
                if dist < min_dist {
                    min_dist = dist;
                }
            }
        }
        sum_nn_dist += min_dist;
    }

    let observed = sum_nn_dist / n as f64;

    // Compute study area bounds
    let min_x = coordinates.iter().map(|(x, _)| x).copied().fold(f64::INFINITY, f64::min);
    let max_x = coordinates.iter().map(|(x, _)| x).copied().fold(f64::NEG_INFINITY, f64::max);
    let min_y = coordinates.iter().map(|(_, y)| y).copied().fold(f64::INFINITY, f64::min);
    let max_y = coordinates.iter().map(|(_, y)| y).copied().fold(f64::NEG_INFINITY, f64::max);

    let area = (max_x - min_x) * (max_y - min_y);
    if area <= 0.0 {
        return Err("Study area has zero or negative area".to_string());
    }

    // Expected NNI under complete spatial randomness (CSR)
    let density = n as f64 / area;
    let expected = 0.5 / density.sqrt();

    let nni = observed / expected;

    // Variance of NNI under CSR
    let var_nni = (0.0703 * area + 0.0000563 * area.powi(2)) / (n as f64).powi(2);
    let se_nni = var_nni.sqrt();

    let z_score = (observed - expected) / se_nni;
    let p_value = crate::weights::two_tailed_normal_p(z_score);

    let interpretation = if nni < 0.5 {
        "Clustered".to_string()
    } else if nni > 1.5 {
        "Dispersed".to_string()
    } else {
        "Random".to_string()
    };

    Ok(NearestNeighborIndexResult {
        observed_distance: observed,
        expected_distance: expected,
        nni,
        z_score,
        p_value,
        interpretation,
    })
}

/// Perform Quadrat Analysis for testing spatial randomness
///
/// # Arguments
/// - `coordinates`: (x, y) coordinates of points
/// - `rows`: Number of rows in quadrat grid
/// - `cols`: Number of columns in quadrat grid
///
/// # Returns
/// Chi-square test statistic with variance-to-mean ratio
pub fn quadrat_analysis(coordinates: &[(f64, f64)], rows: usize, cols: usize) -> Result<QuadratAnalysisResult, String> {
    if coordinates.is_empty() {
        return Err("At least one point required".to_string());
    }

    if rows == 0 || cols == 0 {
        return Err("rows and cols must be > 0".to_string());
    }

    let n_points = coordinates.len();
    let min_x = coordinates.iter().map(|(x, _)| x).copied().fold(f64::INFINITY, f64::min);
    let max_x = coordinates.iter().map(|(x, _)| x).copied().fold(f64::NEG_INFINITY, f64::max);
    let min_y = coordinates.iter().map(|(_, y)| y).copied().fold(f64::INFINITY, f64::min);
    let max_y = coordinates.iter().map(|(_, y)| y).copied().fold(f64::NEG_INFINITY, f64::max);

    let dx = (max_x - min_x) / cols as f64;
    let dy = (max_y - min_y) / rows as f64;

    if !dx.is_finite() || !dy.is_finite() || dx <= 0.0 || dy <= 0.0 {
        return Err("Study area bounds invalid".to_string());
    }

    let mut counts = vec![0usize; rows * cols];
    for (x, y) in coordinates {
        let col = ((x - min_x) / dx) as usize;
        let row = ((y - min_y) / dy) as usize;
        let col_clamped = col.min(cols - 1);
        let row_clamped = row.min(rows - 1);
        counts[row_clamped * cols + col_clamped] += 1;
    }

    let expected = n_points as f64 / (rows * cols) as f64;
    let mut chi_square = 0.0;
    let mut sum_sq = 0.0;

    for count in &counts {
        let diff = *count as f64 - expected;
        chi_square += diff * diff / expected;
        sum_sq += (*count as f64) * (*count as f64);
    }

    let variance = sum_sq / (rows * cols) as f64 - expected * expected;
    let vmr = variance / expected;

    let df = (rows * cols - 1) as usize;
    let p_value = chi_square_cdf(chi_square, df as f64);

    Ok(QuadratAnalysisResult {
        chi_square,
        degrees_of_freedom: df,
        p_value: 1.0 - p_value,
        variance_mean_ratio: vmr,
        n_quadrats: rows * cols,
    })
}

/// Approximate chi-square CDF using Cornish-Fisher expansion
fn chi_square_cdf(x: f64, k: f64) -> f64 {
    if x < 0.0 {
        return 0.0;
    }
    if x == 0.0 {
        return 0.0;
    }

    // Approximate: for large k, χ²(k) ≈ N(k, 2k)
    let mean = k;
    let std = (2.0 * k).sqrt();
    let z = (x - mean) / std;
    crate::weights::normal_cdf(z)
}

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

    fn simple_weights() -> SpatialWeightsGraph {
        SpatialWeightsGraph {
            neighbors: vec![
                vec![(1, 1.0)],
                vec![(0, 1.0), (2, 1.0)],
                vec![(1, 1.0)],
            ],
            diagnostics: crate::weights::SpatialWeightsDiagnostics {
                n_features: 3,
                n_islands: 0,
                neighbor_count_min: 1,
                neighbor_count_mean: 1.33,
                neighbor_count_max: 2,
                connected_component_count: 1,
                row_standardized: false,
                dropped_feature_count: 0,
            },
            warnings: vec![],
        }
    }

    #[test]
    fn test_morans_i_requires_enough_features() {
        let weights = SpatialWeightsGraph {
            neighbors: vec![vec![(1, 1.0)], vec![(0, 1.0)]],
            diagnostics: crate::weights::SpatialWeightsDiagnostics {
                n_features: 2,
                n_islands: 0,
                neighbor_count_min: 1,
                neighbor_count_mean: 1.0,
                neighbor_count_max: 1,
                connected_component_count: 1,
                row_standardized: false,
                dropped_feature_count: 0,
            },
            warnings: vec![],
        };
        let values = vec![1.0, 2.0];
        assert!(morans_i(&values, &weights).is_err());
    }

    #[test]
    fn test_morans_i_basic() {
        let weights = simple_weights();
        let values = vec![1.0, 2.0, 3.0];
        let result = morans_i(&values, &weights);
        assert!(result.is_ok());
        let r = result.unwrap();
        assert!(r.statistic.is_finite());
        assert!(r.z_score.is_finite());
        assert!(r.p_value > 0.0 && r.p_value <= 1.0);
    }

    #[test]
    fn test_lisa_basic() {
        let weights = simple_weights();
        let values = vec![1.0, 2.0, 1.0];
        let result = local_morans_i_lisa(&values, &weights, 0.05);
        assert!(result.is_ok());
        let r = result.unwrap();
        assert_eq!(r.local_statistics.len(), 3);
        assert_eq!(r.cluster_types.len(), 3);
        for p in &r.p_values {
            assert!(*p > 0.0 && *p <= 1.0 || !p.is_finite());
        }
    }

    #[test]
    fn test_lisa_zero_variance() {
        let weights = simple_weights();
        let values = vec![5.0, 5.0, 5.0];  // constant
        let result = local_morans_i_lisa(&values, &weights, 0.05);
        assert!(result.is_err());
    }

    #[test]
    fn test_getis_ord_g_basic() {
        let weights = simple_weights();
        let values = vec![1.0, 5.0, 2.0];
        let result = getis_ord_g(&values, &weights);
        assert!(result.is_ok());
        let r = result.unwrap();
        assert!(r.statistic.is_finite());
        assert!(r.z_score.is_finite());
    }

    #[test]
    fn test_getis_ord_g_star_basic() {
        let weights = simple_weights();
        let values = vec![1.0, 5.0, 2.0];
        let result = getis_ord_g_star(&values, &weights, 0.05);
        assert!(result.is_ok());
        let r = result.unwrap();
        assert_eq!(r.local_statistics.len(), 3);
        assert_eq!(r.cluster_types.len(), 3);
    }

    #[test]
    fn test_nni_basic() {
        let coords = vec![(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (0.0, 1.0)];
        let result = nearest_neighbor_index(&coords);
        assert!(result.is_ok());
        let r = result.unwrap();
        assert!(r.observed_distance > 0.0);
        assert!(r.expected_distance > 0.0);
        assert!(r.nni.is_finite());
        assert!(!r.interpretation.is_empty());
    }

    #[test]
    fn test_nni_insufficient_points() {
        let coords = vec![(0.0, 0.0)];
        let result = nearest_neighbor_index(&coords);
        assert!(result.is_err());
    }

    #[test]
    fn test_quadrat_basic() {
        let coords = vec![(0.1, 0.1), (0.5, 0.5), (0.9, 0.9), (0.2, 0.8), (0.7, 0.3)];
        let result = quadrat_analysis(&coords, 2, 2);
        assert!(result.is_ok());
        let r = result.unwrap();
        assert!(r.chi_square >= 0.0);
        assert!(r.variance_mean_ratio >= 0.0);
        assert_eq!(r.n_quadrats, 4);
    }

    #[test]
    fn test_quadrat_empty() {
        let coords: Vec<(f64, f64)> = vec![];
        let result = quadrat_analysis(&coords, 2, 2);
        assert!(result.is_err());
    }
}