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
479
480
481
482
483
484
485
486
487
// ============================================================================
// Polynomial Regression Unit Tests
// ============================================================================

use linreg_core::polynomial::{polynomial_regression, predict, PolynomialOptions};
use linreg_core::error::Error;

// ============================================================================
// Test Helpers
// ============================================================================

fn assert_close(a: f64, b: f64, tol: f64, ctx: &str) {
    let diff = (a - b).abs();
    assert!(
        diff <= tol,
        "{}: {} != {}, diff = {} (tolerance = {})",
        ctx,
        a,
        b,
        diff,
        tol
    );
}

// ============================================================================
// Basic Functionality Tests
// ============================================================================

#[test]
fn test_polynomial_degree_1_is_linear() {
    // Degree 1 should match simple linear regression: y = 1 + 2x
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi).collect();

    let options = PolynomialOptions {
        degree: 1,
        center: false,
        standardize: false,
        intercept: true,
    };

    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert!(fit.ols_output.r_squared > 0.9999, "R² should be near 1");
    assert_close(fit.ols_output.coefficients[0], 1.0, 1e-6, "intercept");
    assert_close(fit.ols_output.coefficients[1], 2.0, 1e-6, "slope");
    assert_eq!(fit.degree, 1);
    assert_eq!(fit.n_features, 1);
    assert_eq!(fit.ols_output.coefficients.len(), 2);
}

#[test]
fn test_polynomial_degree_2_perfect_fit() {
    // Perfect quadratic: y = 1 + 2x + x²
    let x = vec![0.0, 1.0, 2.0, 3.0, 4.0];
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi + xi * xi).collect();

    let options = PolynomialOptions::default(); // degree = 2
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert_close(fit.ols_output.r_squared, 1.0, 1e-10, "");
    assert_close(fit.ols_output.coefficients[0], 1.0, 1e-6, "intercept");
    assert_close(fit.ols_output.coefficients[1], 2.0, 1e-6, "x coeff");
    assert_close(fit.ols_output.coefficients[2], 1.0, 1e-6, "x² coeff");
    assert_eq!(fit.ols_output.coefficients.len(), 3);
}

#[test]
fn test_polynomial_degree_3_cubic_fit() {
    // y = 5 + 3x - 2x² + 0.5x³
    let x = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
    let y: Vec<f64> = x
        .iter()
        .map(|&xi| 5.0 + 3.0 * xi - 2.0 * xi * xi + 0.5 * xi * xi * xi)
        .collect();

    let options = PolynomialOptions {
        degree: 3,
        ..Default::default()
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert_close(fit.ols_output.r_squared, 1.0, 1e-9, "");
    assert_eq!(fit.ols_output.coefficients.len(), 4, "4 coefficients");
    assert_eq!(fit.degree, 3);
    assert_eq!(fit.n_features, 3);
}

#[test]
fn test_polynomial_feature_names_uncentered() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y = x.clone();

    let options = PolynomialOptions {
        degree: 3,
        center: false,
        ..Default::default()
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    // feature_names: Intercept, x, x^2, x^3
    assert_eq!(fit.feature_names[0], "Intercept");
    assert_eq!(fit.feature_names[1], "x");
    assert_eq!(fit.feature_names[2], "x^2");
    assert_eq!(fit.feature_names[3], "x^3");
}

#[test]
fn test_polynomial_feature_names_centered() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y = x.clone();

    let options = PolynomialOptions {
        degree: 2,
        center: true,
        ..Default::default()
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert_eq!(fit.feature_names[0], "Intercept");
    assert_eq!(fit.feature_names[1], "x_centered");
    assert_eq!(fit.feature_names[2], "x^2_centered");
}

#[test]
fn test_polynomial_centering_stores_x_mean() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y = x.clone();

    let options = PolynomialOptions {
        degree: 2,
        center: true,
        ..Default::default()
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert!(fit.centered);
    assert_close(fit.x_mean, 3.0, 1e-10, "x_mean");
}

#[test]
fn test_polynomial_no_centering_stores_zero_mean() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y = x.clone();

    let options = PolynomialOptions {
        degree: 2,
        center: false,
        ..Default::default()
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert!(!fit.centered);
    assert_close(fit.x_mean, 0.0, 1e-15, "x_mean should be 0.0");
}

#[test]
fn test_polynomial_standardize_stores_stats() {
    let x: Vec<f64> = (1..=10).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|&xi| xi * xi).collect();

    let options = PolynomialOptions {
        degree: 2,
        center: false,
        standardize: true,
        intercept: true,
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    assert!(fit.standardized);
    // Should have stored 2 means and 2 stds (one per polynomial term)
    assert_eq!(fit.feature_means.len(), 2);
    assert_eq!(fit.feature_stds.len(), 2);
    // All stds should be positive
    for std_val in &fit.feature_stds {
        assert!(*std_val > 0.0, "std must be positive");
    }
}

// ============================================================================
// Centering Reduces Multicollinearity
// ============================================================================

#[test]
fn test_polynomial_centering_reduces_max_vif() {
    // For degree 3 on a well-spaced x, centering should reduce VIF
    let x: Vec<f64> = (0..20).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + xi + 0.5 * xi * xi).collect();

    let opts_no_center = PolynomialOptions {
        degree: 3,
        center: false,
        ..Default::default()
    };
    let fit_no_center = polynomial_regression(&y, &x, &opts_no_center).unwrap();

    let opts_centered = PolynomialOptions {
        degree: 3,
        center: true,
        ..Default::default()
    };
    let fit_centered = polynomial_regression(&y, &x, &opts_centered).unwrap();

    // Both should fit well
    assert!(fit_no_center.ols_output.r_squared > 0.95);
    assert!(fit_centered.ols_output.r_squared > 0.95);

    // Centered fit should have lower maximum VIF
    let max_vif_no_center = fit_no_center
        .ols_output
        .vif
        .iter()
        .map(|v| v.vif)
        .fold(0.0f64, f64::max);

    let max_vif_centered = fit_centered
        .ols_output
        .vif
        .iter()
        .map(|v| v.vif)
        .fold(0.0f64, f64::max);

    assert!(
        max_vif_centered < max_vif_no_center,
        "Centering should reduce VIF: centered={} vs uncentered={}",
        max_vif_centered,
        max_vif_no_center
    );
}

// ============================================================================
// Prediction Tests
// ============================================================================

#[test]
fn test_polynomial_predict_quadratic() {
    // y = x² → train on 1..5, predict at 6 and 7
    let x_train = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y_train: Vec<f64> = x_train.iter().map(|&xi| xi * xi).collect();

    let options = PolynomialOptions {
        degree: 2,
        ..Default::default()
    };
    let fit = polynomial_regression(&y_train, &x_train, &options).unwrap();

    let preds = predict(&fit, &[6.0, 7.0]).unwrap();
    assert_eq!(preds.len(), 2);
    assert_close(preds[0], 36.0, 0.1, "pred at x=6");
    assert_close(preds[1], 49.0, 0.1, "pred at x=7");
}

#[test]
fn test_polynomial_predict_empty_x_returns_empty() {
    let x_train = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y_train: Vec<f64> = x_train.iter().map(|&xi| xi * xi).collect();

    let options = PolynomialOptions::default();
    let fit = polynomial_regression(&y_train, &x_train, &options).unwrap();

    let preds = predict(&fit, &[]).unwrap();
    assert!(preds.is_empty());
}

#[test]
fn test_polynomial_predict_centered() {
    // Even with centering, prediction at training points should closely match y
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi + xi * xi).collect();

    let options = PolynomialOptions {
        degree: 2,
        center: true,
        ..Default::default()
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    let preds = predict(&fit, &x).unwrap();
    for (i, (&actual, &pred)) in y.iter().zip(preds.iter()).enumerate() {
        assert_close(pred, actual, 1e-6, &format!("pred[{}]", i));
    }
}

#[test]
fn test_polynomial_predict_standardized() {
    let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let y: Vec<f64> = x.iter().map(|&xi| xi * xi).collect();

    let options = PolynomialOptions {
        degree: 2,
        center: false,
        standardize: true,
        intercept: true,
    };
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    // Predictions at training points should still match
    let preds = predict(&fit, &x).unwrap();
    for (i, (&actual, &pred)) in y.iter().zip(preds.iter()).enumerate() {
        assert_close(pred, actual, 1e-6, &format!("pred[{}] standardized", i));
    }
}

// ============================================================================
// Error Cases
// ============================================================================

#[test]
fn test_polynomial_degree_zero_returns_error() {
    let x = vec![1.0, 2.0, 3.0];
    let y = vec![2.0, 4.0, 6.0];
    let options = PolynomialOptions {
        degree: 0,
        ..Default::default()
    };
    let result = polynomial_regression(&y, &x, &options);
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::InvalidInput(_)));
}

#[test]
fn test_polynomial_mismatched_lengths_return_error() {
    let x = vec![1.0, 2.0, 3.0];
    let y = vec![2.0, 4.0]; // Different length
    let options = PolynomialOptions::default();
    let result = polynomial_regression(&y, &x, &options);
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::DimensionMismatch(_)));
}

#[test]
fn test_polynomial_insufficient_data_returns_error() {
    let x = vec![1.0];
    let y = vec![2.0];
    let options = PolynomialOptions::default();
    let result = polynomial_regression(&y, &x, &options);
    assert!(result.is_err());
    assert!(matches!(
        result.unwrap_err(),
        Error::InsufficientData { .. }
    ));
}

#[test]
fn test_polynomial_nan_x_returns_error() {
    let x = vec![1.0, f64::NAN, 3.0];
    let y = vec![2.0, 4.0, 6.0];
    let options = PolynomialOptions::default();
    let result = polynomial_regression(&y, &x, &options);
    assert!(result.is_err());
}

#[test]
fn test_polynomial_inf_x_returns_error() {
    let x = vec![1.0, f64::INFINITY, 3.0];
    let y = vec![2.0, 4.0, 6.0];
    let options = PolynomialOptions::default();
    let result = polynomial_regression(&y, &x, &options);
    assert!(result.is_err());
}

// ============================================================================
// Statistics Tests
// ============================================================================

#[test]
fn test_polynomial_r_squared_improves_with_correct_degree() {
    let x: Vec<f64> = (1..=20).map(|i| i as f64).collect();
    // True relationship is quadratic
    let y: Vec<f64> = x.iter().map(|&xi| 2.0 + xi + 0.3 * xi * xi).collect();

    let fit1 = polynomial_regression(
        &y,
        &x,
        &PolynomialOptions {
            degree: 1,
            ..Default::default()
        },
    )
    .unwrap();

    let fit2 = polynomial_regression(
        &y,
        &x,
        &PolynomialOptions {
            degree: 2,
            ..Default::default()
        },
    )
    .unwrap();

    assert!(
        fit2.ols_output.r_squared > fit1.ols_output.r_squared,
        "Degree 2 should fit better for quadratic data"
    );
    assert_close(fit2.ols_output.r_squared, 1.0, 1e-9, "Degree 2 R²");
}

#[test]
fn test_polynomial_coefficient_count() {
    for degree in 1usize..=5 {
        let x: Vec<f64> = (0..20).map(|i| i as f64).collect();
        let y = x.clone();

        let options = PolynomialOptions {
            degree,
            ..Default::default()
        };
        let fit = polynomial_regression(&y, &x, &options).unwrap();

        assert_eq!(
            fit.ols_output.coefficients.len(),
            degree + 1,
            "degree {} should produce {} coefficients",
            degree,
            degree + 1
        );
    }
}

#[test]
fn test_polynomial_fitted_values_match_y_for_perfect_fit() {
    let x = vec![0.0, 1.0, 2.0, 3.0, 4.0];
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi + xi * xi).collect();

    let options = PolynomialOptions::default();
    let fit = polynomial_regression(&y, &x, &options).unwrap();

    for (i, (&actual, &fitted)) in y.iter().zip(fit.ols_output.predictions.iter()).enumerate() {
        assert_close(fitted, actual, 1e-8, &format!("fitted[{}]", i));
    }
}

// ============================================================================
// Regularized Polynomial Tests
// ============================================================================

#[test]
fn test_polynomial_ridge_basic() {
    use linreg_core::polynomial::polynomial_ridge;

    let x: Vec<f64> = (1..=10).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi + xi * xi).collect();

    let fit = polynomial_ridge(&y, &x, 2, 0.1, false, true).unwrap();
    // Ridge shrinks but should still have positive R²
    assert!(fit.r_squared > 0.9, "Ridge polynomial R² = {}", fit.r_squared);
    // Should have 2 slope coefficients (x and x²)
    assert_eq!(fit.coefficients.len(), 2);
}

#[test]
fn test_polynomial_lasso_basic() {
    use linreg_core::polynomial::polynomial_lasso;

    let x: Vec<f64> = (1..=10).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi + xi * xi).collect();

    let fit = polynomial_lasso(&y, &x, 2, 0.01, false, true).unwrap();
    assert!(fit.r_squared > 0.9, "Lasso polynomial R² = {}", fit.r_squared);
    assert_eq!(fit.coefficients.len(), 2);
}

#[test]
fn test_polynomial_elastic_net_basic() {
    use linreg_core::polynomial::polynomial_elastic_net;

    let x: Vec<f64> = (1..=10).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|&xi| 1.0 + 2.0 * xi + xi * xi).collect();

    let fit = polynomial_elastic_net(&y, &x, 2, 0.05, 0.5, false, true).unwrap();
    assert!(fit.r_squared > 0.9, "Elastic net polynomial R² = {}", fit.r_squared);
    assert_eq!(fit.coefficients.len(), 2);
}

// ============================================================================
// Default Options Test
// ============================================================================

#[test]
fn test_polynomial_options_default() {
    let opts = PolynomialOptions::default();
    assert_eq!(opts.degree, 2);
    assert!(!opts.center);
    assert!(!opts.standardize);
    assert!(opts.intercept);
}