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
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
//! GLMNET Algorithm Tests
//!
//! These tests validate that the core elastic net algorithm components match glmnet output.
//! The behavior was determined through validation against paper descriptions and reference outputs.

use linreg_core::linalg::Matrix;
use linreg_core::regularized::{
    make_lambda_path, LambdaPathOptions,
    elastic_net_path, ElasticNetOptions,
    elastic_net_fit,
};
use linreg_core::regularized::path::compute_lambda_max;
use linreg_core::regularized::preprocess::{standardize_xy, StandardizeOptions};

// =============================================================================
// Lambda Max Computation Tests
// Based on glmnet output
//
// Formula (glmnet):
//   lambda_max = max_j |X_j^T y| / max(alpha, 1e-3)
//
// where X is standardized (unit norm columns) and y is at unit norm
// =============================================================================

#[test]
fn test_compute_lambda_max_lasso_finite_positive() {
    // For lasso (alpha = 1.0), lambda_max should be finite and positive
    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![5.0, 4.0, 3.0, 2.0, 1.0];

    let n = 5;
    let p = 2;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
        x_data[i * (p + 1) + 2] = x2[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let standardization_options = StandardizeOptions {
        intercept: true,
        standardize_x: true,
        standardize_y: true,
        weights: None,
    };
    let (x_standardized, y_standardized, _) = standardize_xy(&x, &y, &standardization_options);

    let lambda_max = compute_lambda_max(&x_standardized, &y_standardized, 1.0, None, Some(0));

    assert!(lambda_max > 0.0, "lambda_max should be positive for lasso");
    assert!(lambda_max.is_finite(), "lambda_max should be finite for lasso");
}

#[test]
fn test_compute_lambda_max_ridge_returns_infinity() {
    // For alpha = 0 (ridge), glmnet uses max(alpha, 1e-3) in denominator
    // This produces a large finite value, not infinity
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let standardization_options = StandardizeOptions {
        intercept: true,
        standardize_x: true,
        standardize_y: true,
        weights: None,
    };
    let (x_standardized, y_standardized, _) = standardize_xy(&x, &y, &standardization_options);

    // For alpha=0, the formula uses max(0, 1e-3) = 1e-3 as denominator
    let lambda_max = compute_lambda_max(&x_standardized, &y_standardized, 0.0, None, Some(0));

    // Result should be finite (large value), not infinite
    assert!(lambda_max.is_finite(), "lambda_max should be finite for ridge (alpha=0) using max(alpha, 1e-3)");
    // The value should be quite large since we're dividing by 1e-3
    assert!(lambda_max > 100.0, "lambda_max should be large for ridge");
}

// =============================================================================
// Y Standardization Tests
//
// glmnet scales y to unit norm:
//   y = v * (y - ym)  where v = sqrt(w_normalized)
//   ys = sqrt(sum(y^2))
//   y = y / ys  -> ||y|| = 1
// =============================================================================

#[test]
fn test_standardize_y_unit_norm() {
    // Verify y is standardized to unit norm (||y|| = 1)
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let standardization_options = StandardizeOptions {
        intercept: true,
        standardize_x: true,
        standardize_y: true,
        weights: None,
    };
    let (_, y_standardized, info) = standardize_xy(&x, &y, &standardization_options);

    // Verify y is at unit norm
    let y_norm: f64 = y_standardized.iter().map(|&v| v * v).sum::<f64>().sqrt();
    assert!((y_norm - 1.0).abs() < 1e-10, "y should be at unit norm after standardization");

    // y_scale should be the L2 norm of the centered y (before v scaling)
    let y_centered: Vec<f64> = y.iter().map(|&yi| yi - 3.0).collect();
    // After v = 1/sqrt(n) scaling: y_standardized = (y - ym) / sqrt(n)
    let expected_scale: f64 = y_centered.iter().map(|&v| v * v).sum::<f64>().sqrt() / (n as f64).sqrt();
    if let Some(scale) = info.y_scale {
        assert!((scale - expected_scale).abs() < 1e-10, "y_scale should match L2 norm");
    }
}

#[test]
fn test_standardize_preserves_intercept_column() {
    // Verify that intercept column (col 0) is NOT standardized
    let y = vec![10.0, 20.0, 30.0, 40.0, 50.0];
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let standardization_options = StandardizeOptions {
        intercept: true,
        standardize_x: true,
        standardize_y: true,
        weights: None,
    };
    let (x_standardized, _, _) = standardize_xy(&x, &y, &standardization_options);

    // Check that first column is still all ones
    for i in 0..n {
        assert!((x_standardized.get(i, 0) - 1.0).abs() < 1e-10,
               "Intercept column should remain as ones when intercept=true");
    }
}

// =============================================================================
// Lambda Path Generation Tests
//
// Lambda path follows geometric progression (glmnet style):
//   lambda[0] = LAMBDA_EFFECTIVE_INFINITY = infinity (effectively infinite)
//   lambda[1] = lambda_decay_factor * lambda_max (first real lambda)
//   lambda[k] = lambda[k-1] * lambda_decay_factor (geometric decay for k >= 2)
//
// Where:
//   lambda_decay_factor = max(lambda_min_ratio, eps)^(1/(nlam-1)), eps = 1e-6
//   lambda_max = max(|X^T y|) / max(alpha, 1e-3)
// =============================================================================

#[test]
fn test_lambda_path_geometric_decay() {
    // Lambda path should follow glmnet's geometric progression
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let standardization_options = StandardizeOptions {
        intercept: true,
        standardize_x: true,
        standardize_y: true,
        weights: None,
    };
    let (x_standardized, y_standardized, _) = standardize_xy(&x, &y, &standardization_options);

    let nlambda = 10;
    let lambda_min_ratio_val = 0.01;
    let path_options = LambdaPathOptions {
        nlambda,
        lambda_min_ratio: Some(lambda_min_ratio_val),
        alpha: 1.0,
        eps_for_ridge: 1e-3,
    };

    let lambdas = make_lambda_path(&x_standardized, &y_standardized, &path_options, None, Some(0));

    assert_eq!(lambdas.len(), nlambda, "Should generate exactly nlambda lambdas");

    // First lambda should be LAMBDA_EFFECTIVE_INFINITY (effectively infinite)
    assert_eq!(lambdas[0], f64::INFINITY, "First lambda should be LAMBDA_EFFECTIVE_INFINITY (infinity)");

    // Verify geometric progression for k >= 2
    // lambda_decay_factor = max(lambda_min_ratio, 1e-6)^(1/(nlam-1))
    const EPS: f64 = 1.0e-6;
    let lambda_min_ratio_clamped = lambda_min_ratio_val.max(EPS);
    let lambda_decay_factor = lambda_min_ratio_clamped.powf(1.0 / (nlambda - 1) as f64);

    for i in 2..lambdas.len() {
        let expected_ratio = lambdas[i] / lambdas[i - 1];
        assert!((expected_ratio - lambda_decay_factor).abs() < 1e-10,
               "Lambda path should follow geometric progression at index {}: expected {}, got {}",
               i, lambda_decay_factor, expected_ratio);
    }

    // Verify decreasing sequence
    for i in 1..lambdas.len() {
        assert!(lambdas[i] < lambdas[i - 1],
               "Lambda path should be strictly decreasing at index {}", i);
    }
}

#[test]
fn test_lambda_path_first_lambda_is_lambda_max() {
    // First lambda in path should equal lambda_max
    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, 1.0, 0.5, 0.2, 0.1];

    let n = 5;
    let p = 2;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
        x_data[i * (p + 1) + 2] = x2[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let standardization_options = StandardizeOptions {
        intercept: true,
        standardize_x: true,
        standardize_y: true,
        weights: None,
    };
    let (x_standardized, y_standardized, _) = standardize_xy(&x, &y, &standardization_options);

    let lambda_max = compute_lambda_max(&x_standardized, &y_standardized, 1.0, None, Some(0));

    let path_options = LambdaPathOptions {
        nlambda: 5,
        lambda_min_ratio: Some(0.1),
        alpha: 1.0,
        eps_for_ridge: 1e-3,
    };
    let lambdas = make_lambda_path(&x_standardized, &y_standardized, &path_options, None, Some(0));

    // glmnet style: first lambda is LAMBDA_EFFECTIVE_INFINITY, second is lambda_decay_factor * lambda_max
    assert_eq!(lambdas[0], f64::INFINITY, "First lambda should be LAMBDA_EFFECTIVE_INFINITY (infinity)");
    // Second lambda should be: lambda_decay_factor * lambda_max where lambda_decay_factor = 0.1^(1/4)
    let expected_second = 0.1_f64.powf(1.0 / 4.0) * lambda_max;
    assert!((lambdas[1] - expected_second).abs() < 1e-10,
           "Second lambda should equal lambda_decay_factor * lambda_max");
}

// =============================================================================
// Soft Threshold (Coordinate Descent) Tests
//
// Update formula (glmnet):
//   u = g_k + a_k * xv(k)
//   v = |u| - vp(k) * lambda * alpha
//   a(k) = 0 if v <= 0
//   a(k) = sign(u) * v / (xv(k) + vp(k) * lambda * (1 - alpha)) if v > 0
//
// For standardized x: xv(k) = 1.0
// =============================================================================

#[test]
fn test_soft_threshold_formula() {
    // Verify soft-threshold operator matches glmnet formula
    // S(z, lambda * alpha) = sign(z) * max(|z| - lambda * alpha, 0)

    let test_cases: [(f64, f64, f64); 6] = [
        (1.5, 0.5, 1.0),   // |1.5| - 0.5 > 0, so 1.5 - 0.5 = 1.0
        (0.3, 0.5, 0.0),   // |0.3| - 0.5 <= 0, so 0
        (-1.2, 1.0, -0.2), // |-1.2| - 1.0 = 0.2, sign is negative
        (-0.8, 0.3, -0.5),  // |-0.8| - 0.3 = 0.5 > 0, sign is negative
        (0.0, 0.5, 0.0),   // |0| - 0.5 <= 0, so 0
        (5.0, 0.0, 5.0),   // |5| - 0 > 0, so 5
    ];

    for (z, threshold, expected) in test_cases {
        let result = if z.abs() > threshold {
            z.signum() * (z.abs() - threshold)
        } else {
            0.0_f64
        };
        assert!((result - expected).abs() < 1e-10,
               "Soft threshold failed for z={}, threshold={}: got {}, expected {}",
               z, threshold, result, expected);
    }
}

#[test]
fn test_elastic_net_denominator() {
    // Verify elastic net denominator matches glmnet:
    // denom = xv(k) + vp(k) * lambda * (1 - alpha)
    // For standardized x: xv(k) = 1.0

    let lambda: f64 = 0.5;
    let alpha: f64 = 0.7;  // Elastic net
    let penalty_factor_value: f64 = 1.0;     // Default penalty factor

    // Expected: 1.0 + 1.0 * 0.5 * (1.0 - 0.7) = 1.0 + 0.15 = 1.15
    let expected = 1.0_f64 + penalty_factor_value * lambda * (1.0 - alpha);
    assert!((expected - 1.15_f64).abs() < 1e-10, "Elastic net denominator calculation");

    // For lasso (alpha = 1): denom = 1.0 + lambda * 0 = 1.0
    let lasso_denom = 1.0_f64 + lambda * (1.0 - 1.0);
    assert!((lasso_denom - 1.0_f64).abs() < 1e-10, "Lasso denominator should be 1.0");

    // For ridge (alpha = 0): denom = 1.0 + lambda
    let ridge_denom = 1.0_f64 + lambda * (1.0 - 0.0);
    assert!((ridge_denom - 1.5_f64).abs() < 1e-10, "Ridge denominator should be 1.5");
}

// =============================================================================
// Integration Test: Full Elastic Net Path
// =============================================================================

#[test]
fn test_elastic_net_path_convergence() {
    // Full integration test: verify elastic net path converges
    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![0.5, 1.0, 1.5, 2.0, 2.5];

    let n = 5;
    let p = 2;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
        x_data[i * (p + 1) + 2] = x2[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    let path_options = LambdaPathOptions {
        nlambda: 5,
        lambda_min_ratio: Some(0.1),
        alpha: 1.0,
        eps_for_ridge: 1e-3,
    };

    let fit_options = ElasticNetOptions {
        lambda: 0.0,  // Not used in path
        alpha: 1.0,
        intercept: true,
        standardize: true,
        max_iter: 1000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: None,
    };

    let result = elastic_net_path(&x, &y, &path_options, &fit_options);

    assert!(result.is_ok(), "Elastic net path should converge");

    let fits = result.unwrap();
    assert_eq!(fits.len(), 5, "Should generate 5 fits");

    // Verify all fits converged
    for (i, fit) in fits.iter().enumerate() {
        assert!(fit.converged, "Fit {} should converge", i);
    }

    // Verify lambda values are decreasing
    for i in 1..fits.len() {
        assert!(fits[i].lambda < fits[i - 1].lambda,
               "Lambda values should be decreasing: {} < {}",
               fits[i].lambda, fits[i - 1].lambda);
    }

    // At high lambda (first fit), most coefficients should be zero
    let first_fit = &fits[0];
    let nonzero_count = first_fit.coefficients.iter().filter(|&&c| c.abs() > 0.0).count();
    assert!(nonzero_count <= 1, "At high lambda, most coefficients should be zero");
}

// =============================================================================
// Coefficient Bounds Tests
//
// Bounds clamping formula (glmnet):
//   a(k) = max(cl(1,k), min(cl(2,k), calculated_value))
// where cl(1,k) is lower bound, cl(2,k) is upper bound
// =============================================================================

#[test]
fn test_coefficient_bounds_non_negative() {
    // Test non-negative constraint: all coefficients >= 0
    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, -1.0, 0.0, 1.0, 2.0];

    let n = 5;
    let p = 2;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
        x_data[i * (p + 1) + 2] = x2[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Non-negative bounds for both predictors
    let bounds = vec![(0.0, f64::INFINITY); p];

    let options = ElasticNetOptions {
        lambda: 0.1,
        alpha: 1.0, // Lasso
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: Some(bounds),
    };

    let result = elastic_net_fit(&x, &y, &options);

    assert!(result.is_ok(), "Fit with non-negative bounds should succeed");

    let fit = result.unwrap();
    assert!(fit.converged, "Should converge with bounds");

    // All coefficients should be >= 0
    for (i, &coef) in fit.coefficients.iter().enumerate() {
        assert!(coef >= 0.0, "Coefficient {} should be non-negative, got {}", i, coef);
    }
}

#[test]
fn test_coefficient_bounds_upper_limit() {
    // Test upper bound constraint: coefficients <= 1.0
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Upper bound of 1.0
    let bounds = vec![(-f64::INFINITY, 1.0)];

    let options = ElasticNetOptions {
        lambda: 0.01,  // Small lambda so coefficient would normally be > 1
        alpha: 1.0,
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: Some(bounds),
    };

    let result = elastic_net_fit(&x, &y, &options);

    assert!(result.is_ok(), "Fit with upper bound should succeed");

    let fit = result.unwrap();
    assert!(fit.converged, "Should converge with upper bound");

    // Coefficient should be <= 1.0
    assert!(fit.coefficients[0] <= 1.0,
           "Coefficient should be <= 1.0, got {}", fit.coefficients[0]);
}

#[test]
fn test_coefficient_bounds_both_limits() {
    // Test both lower and upper bounds
    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![0.5, 1.0, 1.5, 2.0, 2.5];

    let n = 5;
    let p = 2;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
        x_data[i * (p + 1) + 2] = x2[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Different bounds for each predictor
    let bounds = vec![
        (-1.0, 1.0),   // Predictor 1: between -1 and 1
        (0.0, 2.0),    // Predictor 2: between 0 and 2
    ];

    let options = ElasticNetOptions {
        lambda: 0.01,
        alpha: 0.5, // Elastic net
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: Some(bounds),
    };

    let result = elastic_net_fit(&x, &y, &options);

    assert!(result.is_ok(), "Fit with both bounds should succeed");

    let fit = result.unwrap();
    assert!(fit.converged, "Should converge with both bounds");

    // Check coefficients are within bounds
    assert!(fit.coefficients[0] >= -1.0 && fit.coefficients[0] <= 1.0,
           "Coefficient 0 should be in [-1, 1], got {}", fit.coefficients[0]);
    assert!(fit.coefficients[1] >= 0.0 && fit.coefficients[1] <= 2.0,
           "Coefficient 1 should be in [0, 2], got {}", fit.coefficients[1]);
}

#[test]
fn test_coefficient_bounds_validation_wrong_length() {
    // Test that bounds with wrong length are rejected
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Wrong number of bounds (2 instead of 1)
    let bounds = vec![(0.0, f64::INFINITY), (0.0, f64::INFINITY)];

    let options = ElasticNetOptions {
        lambda: 0.1,
        alpha: 1.0,
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: Some(bounds),
    };

    let result = elastic_net_fit(&x, &y, &options);

    assert!(result.is_err(), "Should reject bounds with wrong length");
}

#[test]
fn test_coefficient_bounds_validation_inverted_bounds() {
    // Test that inverted bounds (lower > upper) are rejected
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Inverted bounds
    let bounds = vec![(1.0, 0.0)];

    let options = ElasticNetOptions {
        lambda: 0.1,
        alpha: 1.0,
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: Some(bounds),
    };

    let result = elastic_net_fit(&x, &y, &options);

    assert!(result.is_err(), "Should reject inverted bounds");
}

#[test]
fn test_coefficient_bounds_no_bounds_same_result() {
    // Verify that None bounds produces same result as unbounded
    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 n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Fit without bounds (None)
    let options_no_bounds = ElasticNetOptions {
        lambda: 0.1,
        alpha: 1.0,
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: None,
    };

    // Fit with explicit unbounded bounds
    let options_unbounded = ElasticNetOptions {
        coefficient_bounds: Some(vec![(-f64::INFINITY, f64::INFINITY)]),
        ..options_no_bounds.clone()
    };

    let result_no_bounds = elastic_net_fit(&x, &y, &options_no_bounds);
    let result_unbounded = elastic_net_fit(&x, &y, &options_unbounded);

    assert!(result_no_bounds.is_ok());
    assert!(result_unbounded.is_ok());

    let fit_no_bounds = result_no_bounds.unwrap();
    let fit_unbounded = result_unbounded.unwrap();

    // Results should be very similar (may have tiny numerical differences)
    assert!((fit_no_bounds.coefficients[0] - fit_unbounded.coefficients[0]).abs() < 1e-10,
           "None bounds and explicit unbounded should produce same result");
}

#[test]
fn test_coefficient_bounds_at_convergence() {
    // Test behavior when solution is at a bound
    let y = vec![2.0, 4.0, 6.0, 8.0, 10.0];  // y = 2*x
    let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];

    let n = 5;
    let p = 1;
    let mut x_data = vec![1.0; n * (p + 1)];
    for i in 0..n {
        x_data[i * (p + 1) + 1] = x1[i];
    }
    let x = Matrix::new(n, p + 1, x_data);

    // Set upper bound below the true solution (coefficient should be ~2)
    let bounds = vec![(0.0, 1.5)];

    let options = ElasticNetOptions {
        lambda: 0.0,  // No regularization, true OLS solution would be 2
        alpha: 1.0,
        intercept: true,
        standardize: true,
        max_iter: 10000,
        tol: 1e-7,
        penalty_factor: None,
        weights: None,
        warm_start: None,
        coefficient_bounds: Some(bounds),
    };

    let result = elastic_net_fit(&x, &y, &options);

    assert!(result.is_ok(), "Fit should succeed");

    let fit = result.unwrap();
    assert!(fit.converged, "Should converge even when at bound");

    // Coefficient should be at the upper bound (or very close)
    assert!(fit.coefficients[0] <= 1.5 + 1e-6,
           "Coefficient should be clamped to upper bound, got {}", fit.coefficients[0]);
}