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
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
// ============================================================================
// Input Validation Unit Tests
// ============================================================================
//
// Tests for validating edge cases and invalid inputs across the library.
// These tests ensure the library handles malformed data gracefully.

use linreg_core::{core::ols_regression, diagnostics::*, Error};

// ============================================================================
// NaN Value Tests
// ============================================================================

#[test]
fn test_ols_rejects_nan_in_y() {
    let y = vec![1.0, 2.0, f64::NAN, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    // Should fail gracefully - either with InvalidInput or through QR decomposition
    assert!(result.is_err(), "OLS should reject NaN values in y");
}

#[test]
fn test_ols_rejects_nan_in_x() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, f64::NAN, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_err(), "OLS should reject NaN values in x");
}

#[test]
fn test_jarque_bera_rejects_nan_in_y() {
    let y = vec![1.0, 2.0, f64::NAN, 4.0, 5.0, 6.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];

    let result = jarque_bera_test(&y, &[x1]);

    assert!(result.is_err(), "Jarque-Bera should reject NaN values");
}

#[test]
fn test_shapiro_wilk_raw_rejects_nan() {
    let sample = vec![1.0, 2.0, f64::NAN, 4.0, 5.0];

    let result = shapiro_wilk_test_raw(&sample);

    // NaN values should cause the test to fail
    // The sort will place NaN at the end, but the range check should fail
    assert!(result.is_err(), "Shapiro-Wilk should reject NaN values");
}

#[test]
fn test_anderson_darling_raw_rejects_nan() {
    let sample = vec![1.0, 2.0, f64::NAN, 4.0, 5.0, 6.0, 7.0, 8.0];

    let result = anderson_darling_test_raw(&sample);

    assert!(result.is_err(), "Anderson-Darling should reject NaN values");
}

// ============================================================================
// Infinite Value Tests
// ============================================================================

#[test]
fn test_ols_rejects_infinity_in_y() {
    let y = vec![1.0, 2.0, f64::INFINITY, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_err(), "OLS should reject infinite values in y");
}

#[test]
fn test_ols_rejects_negative_infinity_in_y() {
    let y = vec![1.0, 2.0, f64::NEG_INFINITY, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(
        result.is_err(),
        "OLS should reject negative infinite values in y"
    );
}

#[test]
fn test_durbin_watson_rejects_infinity() {
    let y = vec![1.0, 2.0, f64::INFINITY, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = durbin_watson_test(&y, &[x1]);

    assert!(
        result.is_err(),
        "Durbin-Watson should reject infinite values"
    );
}

// ============================================================================
// Dimension Mismatch Tests
// ============================================================================

#[test]
fn test_ols_rejects_mismatched_lengths() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0]; // Different length

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    // May fail with IndexOutOfBounds during QR or with DimensionMismatch
    assert!(
        result.is_err(),
        "OLS should reject mismatched vector lengths"
    );
}

#[test]
fn test_ols_rejects_multiple_x_with_mismatched_lengths() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x2 = vec![2.0, 4.0, 6.0]; // Different length

    let result = ols_regression(
        &y,
        &[x1, x2],
        &["Intercept".to_string(), "X1".to_string(), "X2".to_string()],
    );

    assert!(
        result.is_err(),
        "OLS should reject mismatched x variable lengths"
    );
}

#[test]
fn test_diagnostics_rejects_mismatched_lengths() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0]; // Different length

    let test_results: Vec<Result<DiagnosticTestResult, Error>> = vec![
        jarque_bera_test(&y, &[x1.clone()]),
        breusch_pagan_test(&y, &[x1.clone()]),
    ];

    for result in test_results {
        assert!(
            result.is_err(),
            "Diagnostic tests should reject mismatched lengths"
        );
    }

    // Durbin-Watson returns a different result type, test separately
    let dw_result = durbin_watson_test(&y, &[x1]);
    assert!(
        dw_result.is_err(),
        "Durbin-Watson should reject mismatched lengths"
    );
}

// ============================================================================
// Empty Vector Tests
// ============================================================================

#[test]
fn test_ols_rejects_empty_y() {
    let y: Vec<f64> = vec![];
    let x1: Vec<f64> = vec![];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    match result {
        Err(Error::InsufficientData { .. }) => (),
        _ => panic!(
            "Expected InsufficientData error for empty y, got {:?}",
            result
        ),
    }
}

#[test]
fn test_ols_rejects_single_observation() {
    let y = vec![5.0];
    let x1 = vec![2.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    match result {
        Err(Error::InsufficientData { .. }) => (),
        _ => panic!("Expected InsufficientData error for single observation"),
    }
}

#[test]
fn test_rainbow_rejects_insufficient_data() {
    let y = vec![1.0, 2.0];
    let x1 = vec![1.0, 2.0];

    let result = rainbow_test(&y, &[x1], 0.5, linreg_core::diagnostics::RainbowMethod::R);

    assert!(
        result.is_err(),
        "Rainbow test should reject insufficient data"
    );
}

// ============================================================================
// Constant Value Tests (Zero Variance)
// ============================================================================

#[test]
fn test_ols_with_constant_y() {
    let y = vec![5.0; 10];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    // Should work - constant y with varying x gives flat regression
    assert!(
        result.is_ok(),
        "OLS should handle constant y with varying x"
    );

    let r = result.unwrap();
    // X1 coefficient should be near zero since y is constant
    assert!(r.coefficients[1].abs() < 1e-10);
}

#[test]
fn test_ols_with_constant_x() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![5.0; 5]; // Constant x

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    // Constant x creates perfect multicollinearity with intercept
    // The library may use ridge fallback or return SingularMatrix error
    match result {
        Ok(_) => {},                      // Ridge fallback succeeded
        Err(Error::SingularMatrix) => {}, // Correctly detected multicollinearity
        Err(other) => panic!("Unexpected error: {:?}", other),
    }
}

#[test]
fn test_shapiro_wilk_raw_rejects_constant_sample() {
    let sample = vec![5.0; 100];

    let result = shapiro_wilk_test_raw(&sample);

    assert!(
        result.is_err(),
        "Shapiro-Wilk should reject constant sample"
    );
}

#[test]
fn test_anderson_darling_raw_rejects_constant_sample() {
    let sample = vec![5.0; 100];

    let result = anderson_darling_test_raw(&sample);

    assert!(
        result.is_err(),
        "Anderson-Darling should reject constant sample"
    );
}

// ============================================================================
// Very Small Sample Tests
// ============================================================================

#[test]
fn test_shapiro_wilk_minimum_sample_size() {
    let sample = vec![1.0, 2.0, 3.0]; // Minimum valid size

    let result = shapiro_wilk_test_raw(&sample);

    assert!(result.is_ok(), "Shapiro-Wilk should accept n=3");

    let r = result.unwrap();
    assert!(r.statistic > 0.0 && r.statistic <= 1.0);
}

#[test]
fn test_shapiro_wilk_below_minimum() {
    let sample = vec![1.0, 2.0];

    let result = shapiro_wilk_test_raw(&sample);

    match result {
        Err(Error::InsufficientData {
            required,
            available,
        }) => {
            assert_eq!(required, 3);
            assert_eq!(available, 2);
        },
        _ => panic!("Expected InsufficientData error for n=2"),
    }
}

#[test]
fn test_anderson_darling_minimum_sample_size() {
    let sample = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]; // Minimum valid size

    let result = anderson_darling_test_raw(&sample);

    assert!(result.is_ok(), "Anderson-Darling should accept n=8");
}

#[test]
fn test_anderson_darling_below_minimum() {
    let sample = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];

    let result = anderson_darling_test_raw(&sample);

    match result {
        Err(Error::InsufficientData {
            required,
            available,
        }) => {
            assert_eq!(required, 8);
            assert_eq!(available, 7);
        },
        _ => panic!("Expected InsufficientData error for n=7"),
    }
}

// ============================================================================
// Extreme Value Tests (Large but Valid)
// ============================================================================

#[test]
fn test_ols_handles_large_values() {
    let y = vec![1e10, 2e10, 3e10, 4e10, 5e10];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_ok(), "OLS should handle large values");

    let r = result.unwrap();
    // Coefficient should be approximately 1e10
    assert!((r.coefficients[1] - 1e10).abs() < 1e8);
}

#[test]
fn test_ols_handles_small_values() {
    let y = vec![1e-10, 2e-10, 3e-10, 4e-10, 5e-10];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_ok(), "OLS should handle small values");

    let r = result.unwrap();
    // Coefficient should be approximately 1e-10
    assert!((r.coefficients[1] - 1e-10).abs() < 1e-12);
}

#[test]
fn test_shapiro_wilk_maximum_sample_size() {
    // Create sample at the maximum allowed size (5000)
    let sample: Vec<f64> = (0..5000).map(|i| (i as f64) / 5000.0).collect();

    let result = shapiro_wilk_test_raw(&sample);

    assert!(result.is_ok(), "Shapiro-Wilk should accept n=5000");
}

#[test]
fn test_shapiro_wilk_above_maximum() {
    // Create sample exceeding the maximum allowed size
    let sample: Vec<f64> = (0..6000).map(|i| i as f64).collect();

    let result = shapiro_wilk_test_raw(&sample);

    assert!(result.is_err(), "Shapiro-Wilk should reject n > 5000");
}

// ============================================================================
// Zero Value Tests
// ============================================================================

#[test]
fn test_ols_handles_all_zeros_in_x() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![0.0; 5];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    // All zeros in x creates perfect multicollinearity with intercept
    // This should return SingularMatrix error (correct behavior)
    // The library may use ridge fallback, so we accept either outcome
    match result {
        Ok(_) => {},                      // Ridge fallback succeeded
        Err(Error::SingularMatrix) => {}, // Correctly detected multicollinearity
        Err(other) => panic!("Unexpected error: {:?}", other),
    }
}

#[test]
fn test_ols_handles_all_zeros_in_y() {
    let y = vec![0.0; 5];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_ok(), "OLS should handle all zeros in y");

    let r = result.unwrap();
    // All coefficients should be near zero
    assert!(r.coefficients.iter().all(|c| c.abs() < 1e-10));
}

// ============================================================================
// Perfect Multicollinearity Tests
// ============================================================================

#[test]
fn test_ols_detects_perfect_multicollinearity() {
    let y = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let x2 = vec![2.0, 4.0, 6.0, 8.0, 10.0]; // Exactly 2 * x1

    let result = ols_regression(
        &y,
        &[x1, x2],
        &["Intercept".to_string(), "X1".to_string(), "X2".to_string()],
    );

    // Should detect multicollinearity
    match result {
        Err(Error::SingularMatrix) => {
            // Expected - perfect multicollinearity
        },
        Ok(r) => {
            // Some implementations may use ridge fallback
            // Check that VIF is high for collinear variables
            assert!(r.vif.iter().any(|v| v.vif > 100.0));
        },
        _ => panic!("Unexpected result: {:?}", result),
    }
}

// ============================================================================
// Mixed Valid/Invalid Data Tests
// ============================================================================

#[test]
fn test_ols_with_one_zero_value() {
    let y = vec![1.0, 2.0, 0.0, 4.0, 5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_ok(), "OLS should handle data with zero values");
}

#[test]
fn test_ols_with_negative_values() {
    let y = vec![-1.0, -2.0, -3.0, -4.0, -5.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let result = ols_regression(&y, &[x1], &["Intercept".to_string(), "X1".to_string()]);

    assert!(result.is_ok(), "OLS should handle negative values");

    let r = result.unwrap();
    // Coefficient should be negative
    assert!(r.coefficients[1] < 0.0);
}