linreg-core 0.8.1

Lightweight regression library (OLS, Ridge, Lasso, Elastic Net, WLS, LOESS, Polynomial) with 14 diagnostic tests, cross validation, and prediction intervals. Pure Rust - no external math dependencies. WASM, Python, FFI, and Excel XLL bindings.
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
// ============================================================================
// Data Splitting for Cross Validation
// ============================================================================

//! Utilities for creating K-Fold train/test splits.
//!
//! This module provides functions to split data into K folds for cross-validation,
//! with optional shuffling for reproducibility.

use crate::error::{Error, Result};

/// Creates K-Fold train/test splits for cross-validation.
///
/// # Arguments
///
/// * `n_samples` — Total number of observations
/// * `n_folds` — Number of folds to create
/// * `shuffle` — Whether to shuffle indices before splitting
/// * `seed` — Optional random seed for reproducible shuffling
///
/// # Returns
///
/// A vector of tuples where each tuple contains:
/// - `train_indices` — Indices for training data
/// - `test_indices` — Indices for test data
///
/// # Algorithm
///
/// 1. Create indices 0..n_samples
/// 2. If shuffle is enabled, apply Fisher-Yates shuffle with the provided seed
/// 3. Partition indices into n_folds groups
///    - First `n_samples % n_folds` folds get one extra sample
/// 4. For each fold, test = fold indices, train = all other indices
///
/// # Example
///
/// ```rust
/// use linreg_core::cross_validation::splits::create_kfold_splits;
///
/// let splits = create_kfold_splits(10, 3, false, None)?;
/// assert_eq!(splits.len(), 3);
///
/// // First fold: test indices are [0, 1, 2, 3], train is the rest
/// let (train, test) = &splits[0];
/// assert_eq!(test.len(), 4);  // First fold gets extra sample (10 % 3 = 1)
/// assert_eq!(train.len(), 6);
/// # Ok::<(), linreg_core::Error>(())
/// ```
///
/// # Arguments
///
/// * `n_samples` - Total number of samples to split
/// * `n_folds` - Number of folds to create (must be >= 2)
/// * `shuffle` - Whether to shuffle indices before splitting
/// * `seed` - Optional random seed for reproducibility (ignored if shuffle=false)
///
/// # Returns
///
/// A vector of `(train_indices, test_indices)` tuples, one per fold.
///
/// # Errors
///
/// Returns `Error::InvalidInput` if `n_folds < 2`.
/// Returns `Error::InsufficientData` if `n_samples < n_folds`.
pub fn create_kfold_splits(
    n_samples: usize,
    n_folds: usize,
    shuffle: bool,
    seed: Option<u64>,
) -> Result<Vec<(Vec<usize>, Vec<usize>)>> {
    if n_folds < 2 {
        return Err(Error::InvalidInput("n_folds must be at least 2".to_string()));
    }
    if n_samples < n_folds {
        return Err(Error::InsufficientData {
            required: n_folds,
            available: n_samples,
        });
    }

    // Create indices 0..n_samples
    let mut indices: Vec<usize> = (0..n_samples).collect();

    // Shuffle if requested
    if shuffle {
        let seed_val = seed.unwrap_or(42);
        fisher_yates_shuffle(&mut indices, seed_val);
    }

    // Partition into folds
    let fold_size = n_samples / n_folds;
    let remainder = n_samples % n_folds;

    let mut folds: Vec<Vec<usize>> = Vec::with_capacity(n_folds);
    let mut start = 0;

    for fold_idx in 0..n_folds {
        let size = fold_size + if fold_idx < remainder { 1 } else { 0 };
        let end = start + size;
        folds.push(indices[start..end].to_vec());
        start = end;
    }

    // Create train/test splits
    let mut splits: Vec<(Vec<usize>, Vec<usize>)> = Vec::with_capacity(n_folds);

    for test_indices in folds {
        let train_indices: Vec<usize> = indices
            .iter()
            .filter(|&i| !test_indices.contains(i))
            .copied()
            .collect();

        splits.push((train_indices, test_indices));
    }

    Ok(splits)
}

/// Fisher-Yates shuffle with a simple LCG for reproducibility.
///
/// This is a self-contained implementation that doesn't rely on external RNG.
/// The LCG uses the constants from glibc: a = 1103515245, c = 12345, m = 2^32.
///
/// # Arguments
///
/// * `indices` — The slice to shuffle in-place
/// * `seed` — Random seed for the LCG
///
/// # Algorithm
///
/// The Fisher-Yates shuffle works by iterating from the last element to the first,
/// and swapping each element with a randomly chosen element from the remaining
/// unshuffled portion.
///
/// # Example
///
/// ```rust
/// use linreg_core::cross_validation::splits::fisher_yates_shuffle;
///
/// let mut indices = vec![0, 1, 2, 3, 4];
/// fisher_yates_shuffle(&mut indices, 42);
/// // Indices are now in a deterministic but shuffled order
/// ```
///
/// # Returns
///
/// Always returns `()`. The shuffle is performed in-place on `indices`.
///
/// # Panics
///
/// Never panics. An empty slice is returned unchanged.
pub fn fisher_yates_shuffle(indices: &mut [usize], seed: u64) {
    if indices.is_empty() {
        return;
    }

    let mut rng = Lcg::new(seed);
    let n = indices.len();

    for i in (1..n).rev() {
        // Generate random index in [0, i]
        let j = rng.next_usize(0, i);
        indices.swap(i, j);
    }
}

/// Simple Linear Congruential Generator for reproducible randomness.
///
/// Uses glibc constants:
/// - a = 1103515245
/// - c = 12345
/// - m = 2^32 (implicit due to u32 wrapping)
///
/// This provides sufficient randomness for shuffling while remaining
/// completely self-contained.
struct Lcg {
    state: u64,
}

impl Lcg {
    /// Creates a new LCG with the given seed.
    fn new(seed: u64) -> Self {
        Lcg { state: seed.wrapping_add(1) }
    }

    /// Generates the next random number in the sequence.
    #[inline]
    fn next(&mut self) -> u32 {
        // glibc LCG: state = (a * state + c) mod 2^32
        const A: u64 = 1103515245;
        const C: u64 = 12345;

        self.state = self.state.wrapping_mul(A).wrapping_add(C);
        (self.state & 0xFFFFFFFF) as u32
    }

    /// Generates a random usize in the range [min, max] inclusive.
    #[inline]
    fn next_usize(&mut self, min: usize, max: usize) -> usize {
        let range = max - min + 1;

        if range <= 1 {
            return min;
        }

        // Use a unbiased approach: reject samples outside the largest
        // multiple of `range` that fits in u32
        let threshold = (u32::MAX / range as u32) * range as u32;

        loop {
            let val = self.next();
            if val < threshold {
                return min + (val as usize) % range;
            }
        }
    }
}

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

    #[test]
    fn test_create_kfold_splits_basic() {
        let splits = create_kfold_splits(10, 3, false, None).unwrap();
        assert_eq!(splits.len(), 3);

        // Check that each split has correct sizes
        // First fold gets extra sample (10 % 3 = 1)
        assert_eq!(splits[0].1.len(), 4); // test
        assert_eq!(splits[0].0.len(), 6); // train

        // Other folds have 3 samples each
        assert_eq!(splits[1].1.len(), 3);
        assert_eq!(splits[1].0.len(), 7);

        assert_eq!(splits[2].1.len(), 3);
        assert_eq!(splits[2].0.len(), 7);
    }

    #[test]
    fn test_create_kfold_splits_no_remainder() {
        let splits = create_kfold_splits(9, 3, false, None).unwrap();
        assert_eq!(splits.len(), 3);

        // All folds have equal size
        for (train, test) in &splits {
            assert_eq!(test.len(), 3);
            assert_eq!(train.len(), 6);
        }
    }

    #[test]
    fn test_create_kfold_splits_all_samples_used() {
        let n_samples = 20;
        let splits = create_kfold_splits(n_samples, 5, false, None).unwrap();

        let mut all_test_indices: Vec<usize> = Vec::new();
        for (_, test) in &splits {
            all_test_indices.extend(test);
        }

        all_test_indices.sort();
        let expected: Vec<usize> = (0..n_samples).collect();
        assert_eq!(all_test_indices, expected);
    }

    #[test]
    fn test_create_kfold_splits_no_overlap() {
        let splits = create_kfold_splits(15, 5, false, None).unwrap();

        for (train, test) in &splits {
            // No index should be in both train and test
            for &t in test {
                assert!(!train.contains(&t));
            }
        }
    }

    #[test]
    fn test_create_kfold_splits_with_shuffle() {
        let splits1 = create_kfold_splits(20, 5, true, Some(42)).unwrap();
        let splits2 = create_kfold_splits(20, 5, true, Some(42)).unwrap();

        // Same seed should produce identical splits
        assert_eq!(splits1.len(), splits2.len());
        for (s1, s2) in splits1.iter().zip(splits2.iter()) {
            assert_eq!(s1.0, s2.0);
            assert_eq!(s1.1, s2.1);
        }

        // Different seed should produce different splits (very likely)
        let splits3 = create_kfold_splits(20, 5, true, Some(123)).unwrap();
        assert_ne!(splits1[0].1, splits3[0].1);
    }

    #[test]
    fn test_create_kfold_splits_no_shuffle_reproducible() {
        let splits1 = create_kfold_splits(10, 3, false, None).unwrap();
        let splits2 = create_kfold_splits(10, 3, false, None).unwrap();

        // Without shuffling, indices should be in order
        // First fold test indices: [0, 1, 2, 3] (with remainder)
        assert_eq!(splits1[0].1, vec![0, 1, 2, 3]);
        assert_eq!(splits1, splits2);
    }

    #[test]
    fn test_create_kfold_splits_invalid_folds() {
        let result = create_kfold_splits(10, 1, false, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_create_kfold_splits_insufficient_samples() {
        let result = create_kfold_splits(5, 10, false, None);
        assert!(result.is_err());

        match result {
            Err(Error::InsufficientData { required, available }) => {
                assert_eq!(required, 10);
                assert_eq!(available, 5);
            }
            _ => panic!("Expected InsufficientData error"),
        }
    }

    #[test]
    fn test_fisher_yates_shuffle_deterministic() {
        let mut indices1 = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let mut indices2 = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        fisher_yates_shuffle(&mut indices1, 42);
        fisher_yates_shuffle(&mut indices2, 42);

        assert_eq!(indices1, indices2);
    }

    #[test]
    fn test_fisher_yates_shuffle_different_seeds() {
        let mut indices1 = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let mut indices2 = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        fisher_yates_shuffle(&mut indices1, 42);
        fisher_yates_shuffle(&mut indices2, 123);

        assert_ne!(indices1, indices2);
    }

    #[test]
    fn test_fisher_yates_shuffle_permutation() {
        let mut indices = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let original = indices.clone();

        fisher_yates_shuffle(&mut indices, 42);

        // Should contain same elements, just reordered
        let mut sorted1 = indices.clone();
        let mut sorted2 = original.clone();
        sorted1.sort();
        sorted2.sort();
        assert_eq!(sorted1, sorted2);
    }

    #[test]
    fn test_fisher_yates_shuffle_empty() {
        let mut indices: Vec<usize> = vec![];
        fisher_yates_shuffle(&mut indices, 42);
        assert!(indices.is_empty());
    }

    #[test]
    fn test_fisher_yates_shuffle_single() {
        let mut indices = vec![42];
        fisher_yates_shuffle(&mut indices, 42);
        assert_eq!(indices, vec![42]);
    }

    #[test]
    fn test_lcg_range() {
        let mut rng = Lcg::new(42);

        // Test that values are within expected range
        for _ in 0..1000 {
            let val = rng.next_usize(0, 99);
            assert!(val <= 99);
        }
    }

    #[test]
    fn test_lcg_uniform_distribution() {
        let mut rng = Lcg::new(42);
        const N: usize = 10000;
        const RANGE: usize = 10;

        let mut counts = [0usize; RANGE];

        for _ in 0..N {
            let val = rng.next_usize(0, RANGE - 1);
            counts[val] += 1;
        }

        // Each bucket should have roughly N/RANGE counts
        // Allow for some variation (20% tolerance)
        let expected = N / RANGE;
        for count in counts {
            assert!(
                count > expected * 80 / 100 && count < expected * 120 / 100,
                "Count {} is outside expected range {}",
                count,
                expected
            );
        }
    }
}