scirs2-stats 0.4.1

Statistical functions module for SciRS2 (scirs2-stats)
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
//! Memory-efficient statistical operations
//!
//! This module provides memory-optimized implementations of statistical functions
//! that minimize allocations and use streaming/chunked processing for large datasets.

use crate::error::{StatsError, StatsResult};
use crate::error_standardization::ErrorMessages;
#[cfg(feature = "memmap")]
use memmap2::Mmap;
use scirs2_core::ndarray::{s, ArrayBase, ArrayViewMut1, Data, Ix1, Ix2};
use scirs2_core::numeric::{Float, NumCast};
use std::cmp::Ordering;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::path::Path;

/// Chunk size for streaming operations (tuned for cache efficiency)
const CHUNK_SIZE: usize = 8192;

/// Streaming mean calculation that processes data in chunks
///
/// This function computes the mean without loading the entire dataset into memory
/// at once, making it suitable for very large datasets.
///
/// # Arguments
///
/// * `data_iter` - Iterator over data chunks
/// * `total_count` - Total number of elements across all chunks
///
/// # Returns
///
/// The arithmetic mean
#[allow(dead_code)]
pub fn streaming_mean<F, I>(mut data_iter: I, totalcount: usize) -> StatsResult<F>
where
    F: Float + NumCast,
    I: Iterator<Item = F> + std::fmt::Display,
{
    if totalcount == 0 {
        return Err(ErrorMessages::empty_array("dataset"));
    }

    let mut sum = F::zero();
    let mut _count = 0;

    // Process in chunks to maintain precision
    while _count < totalcount {
        let chunk_sum = data_iter
            .by_ref()
            .take(CHUNK_SIZE)
            .fold(F::zero(), |acc, val| acc + val);

        sum = sum + chunk_sum;
        _count += CHUNK_SIZE.min(totalcount - _count);
    }

    Ok(sum / F::from(totalcount).expect("Failed to convert to float"))
}

/// Welford's online algorithm for variance computation
///
/// This algorithm computes variance in a single pass with minimal memory usage
/// and improved numerical stability.
///
/// # Arguments
///
/// * `x` - Input data array
/// * `ddof` - Delta degrees of freedom
///
/// # Returns
///
/// * Tuple of (mean, variance)
#[allow(dead_code)]
pub fn welford_variance<F, D>(x: &ArrayBase<D, Ix1>, ddof: usize) -> StatsResult<(F, F)>
where
    F: Float + NumCast,
    D: Data<Elem = F>,
{
    let n = x.len();
    if n <= ddof {
        return Err(StatsError::InvalidArgument(
            "Not enough data points for the given degrees of freedom".to_string(),
        ));
    }

    let mut mean = F::zero();
    let mut m2 = F::zero();
    let mut count = 0;

    for &value in x.iter() {
        count += 1;
        let delta = value - mean;
        mean = mean + delta / F::from(count).expect("Failed to convert to float");
        let delta2 = value - mean;
        m2 = m2 + delta * delta2;
    }

    let variance = m2 / F::from(n - ddof).expect("Failed to convert to float");
    Ok((mean, variance))
}

/// In-place normalization (standardization) of data
///
/// This function normalizes data in-place to have zero mean and unit variance,
/// avoiding the need for additional memory allocation.
///
/// # Arguments
///
/// * `data` - Mutable array to normalize
/// * `ddof` - Delta degrees of freedom for variance calculation
#[allow(dead_code)]
pub fn normalize_inplace<F>(data: &mut ArrayViewMut1<F>, ddof: usize) -> StatsResult<()>
where
    F: Float + NumCast + std::fmt::Display,
{
    let (mean, variance) = welford_variance(&data.to_owned(), ddof)?;

    if variance <= F::epsilon() {
        return Err(StatsError::InvalidArgument(
            "Cannot normalize data with zero variance".to_string(),
        ));
    }

    let std_dev = variance.sqrt();

    // Normalize in-place
    for val in data.iter_mut() {
        *val = (*val - mean) / std_dev;
    }

    Ok(())
}

/// Memory-efficient quantile computation using quickselect
///
/// This function computes quantiles without fully sorting the array,
/// which saves memory and time for large datasets.
///
/// # Arguments
///
/// * `data` - Input data array (will be partially reordered)
/// * `q` - Quantile to compute (0 to 1)
///
/// # Returns
///
/// The computed quantile value
#[allow(dead_code)]
pub fn quantile_quickselect<F>(data: &mut [F], q: F) -> StatsResult<F>
where
    F: Float + NumCast + std::fmt::Display,
{
    if data.is_empty() {
        return Err(StatsError::InvalidArgument(
            "Cannot compute quantile of empty array".to_string(),
        ));
    }

    if q < F::zero() || q > F::one() {
        return Err(StatsError::domain("Quantile must be between 0 and 1"));
    }

    let n = data.len();
    let pos = q * F::from(n - 1).expect("Failed to convert to float");
    let k = NumCast::from(pos.floor()).expect("Operation failed");

    // Use quickselect to find k-th element
    quickselect(data, k);

    let lower = data[k];

    // Handle interpolation if needed
    let frac = pos - pos.floor();
    if frac > F::zero() && k + 1 < n {
        quickselect(&mut data[(k + 1)..], 0);
        let upper = data[k + 1];
        Ok(lower + frac * (upper - lower))
    } else {
        Ok(lower)
    }
}

/// Quickselect algorithm for finding k-th smallest element
#[allow(dead_code)]
fn quickselect<F: Float>(data: &mut [F], k: usize) {
    let len = data.len();
    if len <= 1 {
        return;
    }

    let mut left = 0;
    let mut right = len - 1;

    while left < right {
        let pivot_idx = partition(data, left, right);

        match k.cmp(&pivot_idx) {
            Ordering::Less => right = pivot_idx - 1,
            Ordering::Greater => left = pivot_idx + 1,
            Ordering::Equal => return,
        }
    }
}

/// Partition function for quickselect
#[allow(dead_code)]
fn partition<F: Float>(data: &mut [F], left: usize, right: usize) -> usize {
    let pivot_idx = left + (right - left) / 2;
    let pivot = data[pivot_idx];

    data.swap(pivot_idx, right);

    let mut store_idx = left;
    for i in left..right {
        if data[i] < pivot {
            data.swap(i, store_idx);
            store_idx += 1;
        }
    }

    data.swap(store_idx, right);
    store_idx
}

/// Memory-efficient covariance matrix computation
///
/// Computes covariance matrix using a streaming algorithm that processes
/// data in chunks to minimize memory usage.
///
/// # Arguments
///
/// * `data` - 2D array where columns are variables
/// * `ddof` - Delta degrees of freedom
///
/// # Returns
///
/// Covariance matrix
#[allow(dead_code)]
pub fn covariance_chunked<F, D>(
    data: &ArrayBase<D, Ix2>,
    ddof: usize,
) -> StatsResult<scirs2_core::ndarray::Array2<F>>
where
    F: Float + NumCast,
    D: Data<Elem = F>,
{
    let n_obs = data.nrows();
    let n_vars = data.ncols();

    if n_obs <= ddof {
        return Err(StatsError::InvalidArgument(
            "Not enough observations for the given degrees of freedom".to_string(),
        ));
    }

    // Compute means for each variable
    let mut means = scirs2_core::ndarray::Array1::zeros(n_vars);
    for j in 0..n_vars {
        let col = data.slice(s![.., j]);
        means[j] = col.iter().fold(F::zero(), |acc, &val| acc + val)
            / F::from(n_obs).expect("Failed to convert to float");
    }

    // Initialize covariance matrix
    let mut cov_matrix = scirs2_core::ndarray::Array2::zeros((n_vars, n_vars));

    // Process data in chunks to compute covariance
    let chunksize = CHUNK_SIZE / n_vars;
    for chunk_start in (0..n_obs).step_by(chunksize) {
        let chunk_end = (chunk_start + chunksize).min(n_obs);
        let chunk = data.slice(s![chunk_start..chunk_end, ..]);

        // Update covariance for this chunk
        for i in 0..n_vars {
            for j in i..n_vars {
                let mut sum = F::zero();
                for k in 0..chunk.nrows() {
                    let xi = chunk[(k, i)] - means[i];
                    let xj = chunk[(k, j)] - means[j];
                    sum = sum + xi * xj;
                }
                cov_matrix[(i, j)] = cov_matrix[(i, j)] + sum;
            }
        }
    }

    // Normalize and fill symmetric entries
    let factor = F::from(n_obs - ddof).expect("Failed to convert to float");
    for i in 0..n_vars {
        for j in i..n_vars {
            cov_matrix[(i, j)] = cov_matrix[(i, j)] / factor;
            if i != j {
                cov_matrix[(j, i)] = cov_matrix[(i, j)];
            }
        }
    }

    Ok(cov_matrix)
}

/// Streaming correlation computation for large datasets
///
/// Computes correlation between two variables without loading all data at once.
#[allow(dead_code)]
pub struct StreamingCorrelation<F: Float> {
    n: usize,
    sum_x: F,
    sum_y: F,
    sum_xx: F,
    sum_yy: F,
    sum_xy: F,
}

#[allow(dead_code)]
impl<F: Float + NumCast + std::fmt::Display> StreamingCorrelation<F> {
    /// Create a new streaming correlation calculator
    pub fn new() -> Self {
        Self {
            n: 0,
            sum_x: F::zero(),
            sum_y: F::zero(),
            sum_xx: F::zero(),
            sum_yy: F::zero(),
            sum_xy: F::zero(),
        }
    }

    /// Update with a new pair of values
    pub fn update(&mut self, x: F, y: F) {
        self.n += 1;
        self.sum_x = self.sum_x + x;
        self.sum_y = self.sum_y + y;
        self.sum_xx = self.sum_xx + x * x;
        self.sum_yy = self.sum_yy + y * y;
        self.sum_xy = self.sum_xy + x * y;
    }

    /// Update with arrays of values
    pub fn update_batch<D>(&mut self, x: &ArrayBase<D, Ix1>, y: &ArrayBase<D, Ix1>)
    where
        D: Data<Elem = F>,
    {
        for (&xi, &yi) in x.iter().zip(y.iter()) {
            self.update(xi, yi);
        }
    }

    /// Compute the correlation coefficient
    pub fn correlation(&self) -> StatsResult<F> {
        if self.n < 2 {
            return Err(StatsError::InvalidArgument(
                "Need at least 2 observations to compute correlation".to_string(),
            ));
        }

        let n = F::from(self.n).expect("Failed to convert to float");
        let mean_x = self.sum_x / n;
        let mean_y = self.sum_y / n;

        let cov_xy = (self.sum_xy - n * mean_x * mean_y) / (n - F::one());
        let var_x = (self.sum_xx - n * mean_x * mean_x) / (n - F::one());
        let var_y = (self.sum_yy - n * mean_y * mean_y) / (n - F::one());

        if var_x <= F::epsilon() || var_y <= F::epsilon() {
            return Err(StatsError::InvalidArgument(
                "Cannot compute correlation when one or both variables have zero variance"
                    .to_string(),
            ));
        }

        Ok(cov_xy / (var_x * var_y).sqrt())
    }

    /// Merge with another streaming correlation
    pub fn merge(&mut self, other: &Self) {
        self.n += other.n;
        self.sum_x = self.sum_x + other.sum_x;
        self.sum_y = self.sum_y + other.sum_y;
        self.sum_xx = self.sum_xx + other.sum_xx;
        self.sum_yy = self.sum_yy + other.sum_yy;
        self.sum_xy = self.sum_xy + other.sum_xy;
    }
}

/// Incremental covariance matrix computation
///
/// Updates covariance matrix incrementally as new observations arrive.
#[allow(dead_code)]
pub struct IncrementalCovariance<F: Float> {
    n: usize,
    means: scirs2_core::ndarray::Array1<F>,
    cov_matrix: scirs2_core::ndarray::Array2<F>,
    n_vars: usize,
}

#[allow(dead_code)]
impl<F: Float + NumCast + scirs2_core::ndarray::ScalarOperand + std::fmt::Display>
    IncrementalCovariance<F>
{
    /// Create a new incremental covariance calculator
    pub fn new(_nvars: usize) -> Self {
        Self {
            n: 0,
            means: scirs2_core::ndarray::Array1::zeros(_nvars),
            cov_matrix: scirs2_core::ndarray::Array2::zeros((_nvars, _nvars)),
            n_vars: _nvars,
        }
    }

    /// Update with a new observation
    pub fn update(&mut self, observation: &scirs2_core::ndarray::ArrayView1<F>) -> StatsResult<()> {
        if observation.len() != self.n_vars {
            return Err(StatsError::DimensionMismatch(
                "Observation dimension doesn't match".to_string(),
            ));
        }

        self.n += 1;
        let n = F::from(self.n).expect("Failed to convert to float");

        // Update means and covariance using Welford's algorithm
        let mut delta = scirs2_core::ndarray::Array1::zeros(self.n_vars);

        for i in 0..self.n_vars {
            delta[i] = observation[i] - self.means[i];
            self.means[i] = self.means[i] + delta[i] / n;
        }

        if self.n > 1 {
            for i in 0..self.n_vars {
                for j in i..self.n_vars {
                    let delta_new = observation[j] - self.means[j];
                    let cov_update = delta[i] * delta_new * (n - F::one()) / n;
                    self.cov_matrix[(i, j)] = self.cov_matrix[(i, j)] + cov_update;
                    if i != j {
                        self.cov_matrix[(j, i)] = self.cov_matrix[(i, j)];
                    }
                }
            }
        }

        Ok(())
    }

    /// Get current covariance matrix
    pub fn covariance(&self, ddof: usize) -> StatsResult<scirs2_core::ndarray::Array2<F>> {
        if self.n <= ddof {
            return Err(StatsError::InvalidArgument(
                "Not enough observations for the given degrees of freedom".to_string(),
            ));
        }

        let factor = F::from(self.n - ddof).expect("Failed to convert to float");
        Ok(&self.cov_matrix / factor)
    }

    /// Get current means
    pub fn means(&self) -> &scirs2_core::ndarray::Array1<F> {
        &self.means
    }
}

/// Memory-efficient rolling window statistics
///
/// Computes statistics over a sliding window without storing all data.
#[allow(dead_code)]
pub struct RollingStats<F: Float> {
    windowsize: usize,
    buffer: Vec<F>,
    position: usize,
    is_full: bool,
    sum: F,
    sum_squares: F,
}

#[allow(dead_code)]
impl<F: Float + NumCast + std::fmt::Display> RollingStats<F> {
    /// Create a new rolling statistics calculator
    pub fn new(_windowsize: usize) -> StatsResult<Self> {
        if _windowsize == 0 {
            return Err(StatsError::InvalidArgument(
                "Window size must be positive".to_string(),
            ));
        }

        Ok(Self {
            windowsize: _windowsize,
            buffer: vec![F::zero(); _windowsize],
            position: 0,
            is_full: false,
            sum: F::zero(),
            sum_squares: F::zero(),
        })
    }

    /// Add a new value to the rolling window
    pub fn push(&mut self, value: F) {
        let old_value = self.buffer[self.position];

        // Update running sums
        self.sum = self.sum - old_value + value;
        self.sum_squares = self.sum_squares - old_value * old_value + value * value;

        // Store new value
        self.buffer[self.position] = value;
        self.position = (self.position + 1) % self.windowsize;

        if !self.is_full && self.position == 0 {
            self.is_full = true;
        }
    }

    /// Get current window size
    pub fn len(&self) -> usize {
        if self.is_full {
            self.windowsize
        } else {
            self.position
        }
    }

    /// Compute mean of current window
    pub fn mean(&self) -> F {
        let n = self.len();
        if n == 0 {
            F::zero()
        } else {
            self.sum / F::from(n).expect("Failed to convert to float")
        }
    }

    /// Compute variance of current window
    pub fn variance(&self, ddof: usize) -> StatsResult<F> {
        let n = self.len();
        if n <= ddof {
            return Err(StatsError::InvalidArgument(
                "Not enough data for the given degrees of freedom".to_string(),
            ));
        }

        let mean = self.mean();
        let n_f = F::from(n).expect("Failed to convert to float");
        let variance = (self.sum_squares / n_f) - mean * mean;
        Ok(variance * n_f / F::from(n - ddof).expect("Failed to convert to float"))
    }

    /// Get current buffer as array
    pub fn as_array(&self) -> scirs2_core::ndarray::Array1<F> {
        if self.is_full {
            scirs2_core::ndarray::Array1::from_vec(self.buffer.clone())
        } else {
            scirs2_core::ndarray::Array1::from_vec(self.buffer[..self.position].to_vec())
        }
    }
}

/// Memory-efficient histogram computation
///
/// Computes histogram without storing all data, using a streaming approach.
pub struct StreamingHistogram<F: Float> {
    bins: Vec<F>,
    counts: Vec<usize>,
    min_val: F,
    max_val: F,
    total_count: usize,
}

impl<F: Float + NumCast + std::fmt::Display> StreamingHistogram<F> {
    /// Create a new streaming histogram
    pub fn new(_n_bins: usize, min_val: F, maxval: F) -> Self {
        let bin_width = (maxval - min_val) / F::from(_n_bins).expect("Failed to convert to float");
        let bins: Vec<F> = (0..=_n_bins)
            .map(|i| min_val + F::from(i).expect("Failed to convert to float") * bin_width)
            .collect();

        Self {
            bins,
            counts: vec![0; _n_bins],
            min_val,
            max_val: maxval,
            total_count: 0,
        }
    }

    /// Add a value to the histogram
    pub fn add_value(&mut self, value: F) {
        if value >= self.min_val && value <= self.max_val {
            let n_bins = self.counts.len();
            let bin_width = (self.max_val - self.min_val)
                / F::from(n_bins).expect("Failed to convert to float");
            let bin_idx = ((value - self.min_val) / bin_width).floor();
            let bin_idx: usize = NumCast::from(bin_idx).unwrap_or(n_bins - 1).min(n_bins - 1);
            self.counts[bin_idx] += 1;
            self.total_count += 1;
        }
    }

    /// Add multiple values
    pub fn add_values<D>(&mut self, values: &ArrayBase<D, Ix1>)
    where
        D: Data<Elem = F>,
    {
        for &value in values.iter() {
            self.add_value(value);
        }
    }

    /// Get the histogram results
    pub fn get_histogram(&self) -> (Vec<F>, Vec<usize>) {
        (self.bins.clone(), self.counts.clone())
    }

    /// Get normalized histogram (density)
    pub fn get_density(&self) -> (Vec<F>, Vec<F>) {
        let n_bins = self.counts.len();
        let bin_width =
            (self.max_val - self.min_val) / F::from(n_bins).expect("Failed to convert to float");
        let total = F::from(self.total_count).expect("Failed to convert to float") * bin_width;

        let density: Vec<F> = self
            .counts
            .iter()
            .map(|&count| F::from(count).expect("Failed to convert to float") / total)
            .collect();

        (self.bins.clone(), density)
    }
}

/// Out-of-core statistics for datasets larger than memory
///
/// Processes data from files in chunks without loading entire dataset.
#[allow(dead_code)]
pub struct OutOfCoreStats<F: Float> {
    chunksize: usize,
    _phantom: std::marker::PhantomData<F>,
}

#[allow(dead_code)]
impl<F: Float + NumCast + std::str::FromStr + std::fmt::Display> OutOfCoreStats<F> {
    /// Create a new out-of-core statistics processor
    pub fn new(_chunksize: usize) -> Self {
        Self {
            chunksize: _chunksize,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Compute mean from a CSV file
    pub fn mean_from_csv<P: AsRef<Path>>(
        &self,
        path: P,
        column: usize,
        has_header: bool,
    ) -> StatsResult<F> {
        let file = File::open(path)
            .map_err(|e| StatsError::InvalidArgument(format!("Failed to open file: {e}")))?;
        let mut reader = BufReader::new(file);

        let mut sum = F::zero();
        let mut count = 0;
        let mut line = String::new();
        let mut line_num = 0;

        // Skip _header if present
        if has_header {
            reader
                .read_line(&mut line)
                .map_err(|e| StatsError::InvalidArgument(format!("Failed to read header: {e}")))?;
            line.clear();
        }

        // Process file in lines
        loop {
            match reader.read_line(&mut line) {
                Ok(0) => break, // EOF
                Ok(_) => {
                    line_num += 1;
                    let fields: Vec<&str> = line.trim().split(',').collect();

                    if fields.len() > column {
                        if let Ok(value) = fields[column].parse::<F>() {
                            sum = sum + value;
                            count += 1;
                        }
                    }

                    line.clear();
                }
                Err(e) => {
                    return Err(StatsError::ComputationError(format!(
                        "Error reading line {line_num}: {e}"
                    )))
                }
            }
        }

        if count == 0 {
            return Err(StatsError::InvalidArgument(
                "No valid data found".to_string(),
            ));
        }

        Ok(sum / F::from(count).expect("Failed to convert to float"))
    }

    /// Compute variance from a CSV file using two-pass algorithm
    pub fn variance_from_csv<P: AsRef<Path>>(
        &self,
        path: P,
        column: usize,
        has_header: bool,
        ddof: usize,
    ) -> StatsResult<(F, F)> {
        // First pass: compute mean
        let mean = self.mean_from_csv(&path, column, has_header)?;

        // Second pass: compute variance
        let file = File::open(path)
            .map_err(|e| StatsError::InvalidArgument(format!("Failed to open file: {e}")))?;
        let mut reader = BufReader::new(file);

        let mut sum_sq = F::zero();
        let mut count = 0;
        let mut line = String::new();

        // Skip _header if present
        if has_header {
            reader
                .read_line(&mut line)
                .map_err(|e| StatsError::InvalidArgument(format!("Failed to read header: {e}")))?;
            line.clear();
        }

        // Process file
        loop {
            match reader.read_line(&mut line) {
                Ok(0) => break, // EOF
                Ok(_) => {
                    let fields: Vec<&str> = line.trim().split(',').collect();

                    if fields.len() > column {
                        if let Ok(value) = fields[column].parse::<F>() {
                            let diff = value - mean;
                            sum_sq = sum_sq + diff * diff;
                            count += 1;
                        }
                    }

                    line.clear();
                }
                Err(e) => {
                    return Err(StatsError::ComputationError(format!(
                        "Error reading file: {}",
                        e
                    )))
                }
            }
        }

        if count <= ddof {
            return Err(StatsError::InvalidArgument(
                "Not enough data for the given degrees of freedom".to_string(),
            ));
        }

        let variance = sum_sq
            / F::from(count - ddof).ok_or_else(|| {
                StatsError::ComputationError(
                    "Failed to convert count - ddof to target type".to_string(),
                )
            })?;
        Ok((mean, variance))
    }

    /// Process large binary file of floats
    pub fn process_binary_file<P: AsRef<Path>, G>(
        &self,
        path: P,
        mut processor: G,
    ) -> StatsResult<()>
    where
        G: FnMut(&[F]) -> StatsResult<()>,
    {
        use std::mem;

        let file = File::open(path)
            .map_err(|e| StatsError::InvalidArgument(format!("Failed to open file: {e}")))?;
        let mut reader = BufReader::new(file);

        let elementsize = mem::size_of::<F>();
        let buffersize = self.chunksize * elementsize;
        let mut buffer = vec![0u8; buffersize];

        loop {
            match reader.read(&mut buffer) {
                Ok(0) => break, // EOF
                Ok(bytes_read) => {
                    let n_elements = bytes_read / elementsize;

                    // Convert bytes to floats (unsafe but efficient)
                    let floats = unsafe {
                        std::slice::from_raw_parts(buffer.as_ptr() as *const F, n_elements)
                    };

                    processor(floats)?;
                }
                Err(e) => {
                    return Err(StatsError::ComputationError(format!(
                        "Error reading file: {}",
                        e
                    )))
                }
            }
        }

        Ok(())
    }
}

/// Memory-mapped statistics for very large files
///
/// Uses memory-mapped I/O for efficient access to large datasets.
#[cfg(feature = "memmap")]
pub struct MemoryMappedStats<F: Float> {
    _phantom: std::marker::PhantomData<F>,
}

#[cfg(feature = "memmap")]
impl<F: Float + NumCast + std::fmt::Display> MemoryMappedStats<F> {
    /// Create a new memory-mapped statistics processor
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    /// Process a memory-mapped file
    pub fn process_mmap_file<P: AsRef<Path>, G>(&self, path: P, processor: G) -> StatsResult<()>
    where
        G: FnOnce(&[F]) -> StatsResult<()>,
    {
        let file = File::open(path)
            .map_err(|e| StatsError::InvalidArgument(format!("Failed to open file: {e}")))?;

        let mmap = unsafe {
            Mmap::map(&file)
                .map_err(|e| StatsError::ComputationError(format!("Failed to mmap file: {}", e)))?
        };

        // Interpret memory-mapped data as array of floats
        let data = unsafe {
            std::slice::from_raw_parts(
                mmap.as_ptr() as *const F,
                mmap.len() / std::mem::size_of::<F>(),
            )
        };

        processor(data)
    }
}