rustyml 0.13.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! Integration tests for SVC (Support Vector Classifier)
//!
//! Label-domain contract: fit requires labels in {+1.0, -1.0}, predict emits
//! labels in {+1.0, -1.0}, and sign(decision_function[i]) >= 0 <-> predict[i] == +1.0

use approx::assert_abs_diff_eq;
use ndarray::{Array1, Array2, array};
use rustyml::error::Error;
use rustyml::machine_learning::{Gamma, KernelType, SVC};

// helpers

/// Linearly separable 2-feature dataset in the +1/-1 label domain
///
/// Class +1 sits in the upper-right quadrant, class -1 in the lower-left, so a
/// large-margin linear-kernel SVC classifies every point with zero error
fn linearly_separable_data() -> (Array2<f64>, Array1<f64>) {
    let x = Array2::from_shape_vec(
        (8, 2),
        vec![
            2.0, 2.0, 3.0, 2.0, 2.0, 3.0, 3.0, 3.0, // class +1
            -2.0, -2.0, -3.0, -2.0, -2.0, -3.0, -3.0, -3.0, // class -1
        ],
    )
    .unwrap();
    let y = array![1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0];
    (x, y)
}

/// Concentric-rings dataset (inner ring class +1, outer ring class -1) that a
/// linear kernel cannot separate but an RBF kernel with gamma=0.5 can
fn concentric_rings_data() -> (Array2<f64>, Array1<f64>) {
    let x = Array2::from_shape_vec(
        (8, 2),
        vec![
            1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0, -1.0, // inner ring (radius 1), class +1
            // Radius 5 (not 3) keeps every inner-inner distance strictly below every inner-outer
            // distance, so RBF can separate the rings (at radius 3 some points are equidistant)
            5.0, 0.0, -5.0, 0.0, 0.0, 5.0, 0.0, -5.0, // outer ring (radius 5), class -1
        ],
    )
    .unwrap();
    let y = array![1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0];
    (x, y)
}

// constructor validation

/// C = 0.0 is non-positive -> InvalidParameter
#[test]
fn new_rejects_zero_c() {
    let result = SVC::new(KernelType::Linear, 0.0, 1e-3, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// C = -1.0 is negative -> InvalidParameter
#[test]
fn new_rejects_negative_c() {
    let result = SVC::new(KernelType::Linear, -1.0, 1e-3, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// C = NaN -> InvalidParameter (non-finite check)
#[test]
fn new_rejects_nan_c() {
    let result = SVC::new(KernelType::Linear, f64::NAN, 1e-3, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// C = +Inf -> InvalidParameter (non-finite check)
#[test]
fn new_rejects_inf_c() {
    let result = SVC::new(KernelType::Linear, f64::INFINITY, 1e-3, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// tol = 0.0 is non-positive -> InvalidParameter
#[test]
fn new_rejects_zero_tol() {
    let result = SVC::new(KernelType::Linear, 1.0, 0.0, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// tol = -1e-3 is negative -> InvalidParameter
#[test]
fn new_rejects_negative_tol() {
    let result = SVC::new(KernelType::Linear, 1.0, -1e-3, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// tol = NaN -> InvalidParameter
#[test]
fn new_rejects_nan_tol() {
    let result = SVC::new(KernelType::Linear, 1.0, f64::NAN, 100);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// max_iter = 0 -> InvalidParameter
#[test]
fn new_rejects_zero_max_iter() {
    let result = SVC::new(KernelType::Linear, 1.0, 1e-3, 0);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "expected InvalidParameter, got {:?}",
        result
    );
}

/// Valid parameters succeed and getters echo them back
#[test]
fn new_valid_parameters_round_trip() {
    let svc = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        2.0,
        1e-3,
        500,
    )
    .expect("valid params must succeed")
    .with_random_state(7);

    assert_abs_diff_eq!(svc.get_regularization_parameter(), 2.0, epsilon = 1e-10);
    assert_abs_diff_eq!(svc.get_tolerance(), 1e-3, epsilon = 1e-12);
    assert_eq!(svc.get_max_iterations(), 500);
    assert_eq!(svc.get_random_state(), Some(7));
}

/// Default constructor exposes the documented field values
#[test]
fn default_has_expected_params() {
    let svc = SVC::default();
    assert_abs_diff_eq!(svc.get_regularization_parameter(), 1.0, epsilon = 1e-10);
    assert_abs_diff_eq!(svc.get_tolerance(), 0.001, epsilon = 1e-12);
    assert_eq!(svc.get_max_iterations(), 1000);
    // not yet fitted
    assert!(svc.get_support_vectors().is_none());
    assert!(svc.get_alphas().is_none());
    assert!(svc.get_bias().is_none());
    assert!(svc.get_actual_iterations().is_none());
}

// fit label-domain validation

/// fit rejects labels that are not exactly +/-1.0 (e.g. 0.0/1.0 domain)
#[test]
fn fit_rejects_labels_not_plus_minus_one() {
    let x = Array2::from_shape_vec((4, 2), vec![1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0, -1.0]).unwrap();
    // 0-based binary labels, not valid for SVC
    let y = array![0.0, 0.0, 1.0, 1.0];
    let mut svc = SVC::new(KernelType::Linear, 1.0, 1e-3, 100)
        .unwrap()
        .with_random_state(42);
    let result = svc.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::InvalidInput(_))),
        "expected InvalidInput for non-±1 labels, got {:?}",
        result
    );
}

/// fit rejects labels containing fractional values (e.g. 0.5)
#[test]
fn fit_rejects_fractional_labels() {
    let x = Array2::from_shape_vec((4, 2), vec![1.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0, -1.0]).unwrap();
    let y = array![1.0, -1.0, 0.5, -0.5];
    let mut svc = SVC::new(KernelType::Linear, 1.0, 1e-3, 100)
        .unwrap()
        .with_random_state(42);
    let result = svc.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::InvalidInput(_))),
        "expected InvalidInput for fractional labels, got {:?}",
        result
    );
}

// predict / decision_function before fit

/// predict before fit -> NotFitted
#[test]
fn predict_before_fit_returns_not_fitted() {
    let svc = SVC::new(KernelType::Linear, 1.0, 1e-3, 100)
        .unwrap()
        .with_random_state(42);
    let x = Array2::from_shape_vec((2, 2), vec![1.0, 0.0, -1.0, 0.0]).unwrap();
    let result = svc.predict(&x);
    assert!(
        matches!(result, Err(Error::NotFitted(_))),
        "expected NotFitted, got {:?}",
        result
    );
}

/// decision_function before fit -> NotFitted
#[test]
fn decision_function_before_fit_returns_not_fitted() {
    let svc = SVC::new(KernelType::Linear, 1.0, 1e-3, 100)
        .unwrap()
        .with_random_state(42);
    let x = Array2::from_shape_vec((2, 2), vec![1.0, 0.0, -1.0, 0.0]).unwrap();
    let result = svc.decision_function(&x);
    assert!(
        matches!(result, Err(Error::NotFitted(_))),
        "expected NotFitted, got {:?}",
        result
    );
}

// predict / decision_function dimension checks

/// predict with wrong feature dimension -> DimensionMismatch
#[test]
fn predict_wrong_feature_dim_returns_dimension_mismatch() {
    let (x_train, y_train) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 500)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x_train, &y_train).expect("fit must succeed");

    // training used 2 features; pass 3-feature input
    let x_bad = Array2::from_shape_vec((2, 3), vec![1.0, 0.0, 0.0, -1.0, 0.0, 0.0]).unwrap();
    let result = svc.predict(&x_bad);
    assert!(
        matches!(result, Err(Error::DimensionMismatch { .. })),
        "expected DimensionMismatch, got {:?}",
        result
    );
}

/// decision_function with wrong feature dimension -> DimensionMismatch
#[test]
fn decision_function_wrong_feature_dim_returns_dimension_mismatch() {
    let (x_train, y_train) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 500)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x_train, &y_train).expect("fit must succeed");

    let x_bad = Array2::from_shape_vec((2, 3), vec![1.0, 0.0, 0.0, -1.0, 0.0, 0.0]).unwrap();
    let result = svc.decision_function(&x_bad);
    assert!(
        matches!(result, Err(Error::DimensionMismatch { .. })),
        "expected DimensionMismatch, got {:?}",
        result
    );
}

// linear kernel: linearly separable data

/// A linear-kernel SVC on well-separated data classifies every training point
/// correctly against the by-construction labels
#[test]
fn linear_kernel_classifies_separable_data_perfectly() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y)
        .expect("fit must succeed on linearly separable data");

    let preds = svc.predict(&x).expect("predict must succeed");

    // by construction the correct labels are exactly y
    for (i, (&pred, &true_label)) in preds.iter().zip(y.iter()).enumerate() {
        assert_eq!(
            pred, true_label,
            "sample {i}: predicted {pred} but true label is {true_label}"
        );
    }
}

/// predict output is strictly within the label domain {+1.0, -1.0}
#[test]
fn predict_output_domain_is_plus_minus_one() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let preds = svc.predict(&x).expect("predict must succeed");
    for &p in preds.iter() {
        assert!(
            p == 1.0 || p == -1.0,
            "predict returned {p}, which is not in {{+1.0, -1.0}}"
        );
    }
}

/// sign(decision_function[i]) >= 0 <-> predict[i] == +1.0 for the linear kernel
#[test]
fn sign_consistency_linear_kernel() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let df = svc
        .decision_function(&x)
        .expect("decision_function must succeed");
    let preds = svc.predict(&x).expect("predict must succeed");

    for (i, (&dv, &p)) in df.iter().zip(preds.iter()).enumerate() {
        let expected_pred = if dv >= 0.0 { 1.0_f64 } else { -1.0_f64 };
        assert_eq!(
            p, expected_pred,
            "sample {i}: decision_value={dv}, predict={p} disagrees with sign"
        );
    }
}

/// After fit, getters for support vectors, alphas, and bias are Some
#[test]
fn linear_kernel_fit_populates_state() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    assert!(
        svc.get_support_vectors().is_some(),
        "support_vectors must be Some after fit"
    );
    assert!(svc.get_alphas().is_some(), "alphas must be Some after fit");
    assert!(svc.get_bias().is_some(), "bias must be Some after fit");
    assert!(
        svc.get_actual_iterations().is_some(),
        "n_iter must be Some after fit"
    );
}

/// n_iter after fit is in [1, max_iter]
#[test]
fn actual_iterations_in_valid_range() {
    let (x, y) = linearly_separable_data();
    let max_iter = 1000_usize;
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, max_iter)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let n_iter = svc
        .get_actual_iterations()
        .as_ref()
        .copied()
        .expect("n_iter must be Some after fit");
    assert!(
        n_iter >= 1 && n_iter <= max_iter,
        "n_iter={n_iter} is outside [1, {max_iter}]"
    );
}

// RBF kernel: non-linearly separable concentric rings

/// An RBF-kernel SVC classifies every concentric-ring point into its known ring
/// (class +1 inner, class -1 outer), which a linear kernel cannot
#[test]
fn rbf_kernel_classifies_concentric_rings_perfectly() {
    let (x, y) = concentric_rings_data();
    let mut svc = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        10.0,
        1e-3,
        5000,
    )
    .unwrap()
    .with_random_state(42);
    svc.fit(&x, &y)
        .expect("fit must succeed on concentric rings");

    let preds = svc.predict(&x).expect("predict must succeed");
    for (i, (&pred, &true_label)) in preds.iter().zip(y.iter()).enumerate() {
        assert_eq!(
            pred, true_label,
            "sample {i}: RBF SVC predicted {pred} but true label is {true_label}"
        );
    }
}

/// sign consistency holds for the RBF kernel too
#[test]
fn sign_consistency_rbf_kernel() {
    let (x, y) = concentric_rings_data();
    let mut svc = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        10.0,
        1e-3,
        2000,
    )
    .unwrap()
    .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let df = svc
        .decision_function(&x)
        .expect("decision_function must succeed");
    let preds = svc.predict(&x).expect("predict must succeed");

    for (i, (&dv, &p)) in df.iter().zip(preds.iter()).enumerate() {
        let expected_pred = if dv >= 0.0 { 1.0_f64 } else { -1.0_f64 };
        assert_eq!(
            p, expected_pred,
            "sample {i}: RBF decision_value={dv}, predict={p} disagrees with sign"
        );
    }
}

// all five kernels: construct, fit, predict

/// All five kernel variants construct, train, and predict on the
/// linearly-separable data without error
#[test]
fn all_kernels_fit_and_predict_without_error() {
    let kernels: &[KernelType] = &[
        KernelType::Linear,
        KernelType::Poly {
            degree: 2,
            gamma: Gamma::Value(1.0),
            coef0: 1.0,
        },
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        KernelType::Sigmoid {
            gamma: Gamma::Value(0.1),
            coef0: 0.0,
        },
        KernelType::Cosine,
    ];

    let (x, y) = linearly_separable_data();

    for kernel in kernels {
        let mut svc = SVC::new(*kernel, 5.0, 1e-3, 1000)
            .expect("constructor must succeed for all kernel variants")
            .with_random_state(42);
        svc.fit(&x, &y)
            .unwrap_or_else(|e| panic!("fit failed for kernel {:?}: {e}", kernel));
        let preds = svc
            .predict(&x)
            .unwrap_or_else(|e| panic!("predict failed for kernel {:?}: {e}", kernel));
        // predictions must be in the correct domain
        for &p in preds.iter() {
            assert!(
                p == 1.0 || p == -1.0,
                "kernel {:?} returned label {p} outside {{±1.0}}",
                kernel
            );
        }
    }
}

// Polynomial kernel correctness

/// A degree-2 polynomial kernel separates the linearly-separable data and
/// classifies every training point into its known true class
#[test]
fn poly_kernel_classifies_separable_data_correctly() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(
        KernelType::Poly {
            degree: 2,
            gamma: Gamma::Value(1.0),
            coef0: 1.0,
        },
        10.0,
        1e-3,
        1000,
    )
    .unwrap()
    .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");
    let preds = svc.predict(&x).expect("predict must succeed");
    for (i, (&pred, &true_label)) in preds.iter().zip(y.iter()).enumerate() {
        assert_eq!(
            pred, true_label,
            "Poly sample {i}: predicted {pred} but true label is {true_label}"
        );
    }
}

// Cosine kernel: zero-vector robustness

/// The Cosine kernel tolerates a zero vector in the data without panic or
/// non-finite values; no specific prediction value is asserted
#[test]
fn cosine_kernel_zero_vector_does_not_panic() {
    // zero vector in the training set, labelled +1 (arbitrary but must be +/-1)
    let x = Array2::from_shape_vec(
        (6, 2),
        vec![
            2.0, 2.0, 3.0, 3.0, // class +1
            0.0, 0.0, // class +1, zero vector
            -2.0, -2.0, -3.0, -3.0, -4.0, -4.0, // class -1
        ],
    )
    .unwrap();
    let y = array![1.0, 1.0, 1.0, -1.0, -1.0, -1.0];

    let mut svc = SVC::new(KernelType::Cosine, 5.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    // fit may converge or not; what matters is that it does not panic
    let _ = svc.fit(&x, &y);
}

// Reproducibility with fixed random_state

/// Two runs with the same seed produce identical predictions, decision values,
/// and bias
#[test]
fn same_seed_produces_identical_results() {
    let (x, y) = linearly_separable_data();

    let mut svc1 = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        5.0,
        1e-3,
        1000,
    )
    .unwrap()
    .with_random_state(42);
    svc1.fit(&x, &y).expect("first fit must succeed");

    let mut svc2 = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        5.0,
        1e-3,
        1000,
    )
    .unwrap()
    .with_random_state(42);
    svc2.fit(&x, &y).expect("second fit must succeed");

    // predictions must be identical
    let preds1 = svc1.predict(&x).expect("predict1 must succeed");
    let preds2 = svc2.predict(&x).expect("predict2 must succeed");
    assert_eq!(preds1, preds2, "same seed must yield identical predictions");

    // decision function values must be identical
    let df1 = svc1.decision_function(&x).expect("df1 must succeed");
    let df2 = svc2.decision_function(&x).expect("df2 must succeed");
    for (a, b) in df1.iter().zip(df2.iter()) {
        assert_abs_diff_eq!(a, b, epsilon = 1e-12);
    }

    // bias must be identical
    assert_abs_diff_eq!(
        svc1.get_bias().unwrap(),
        svc2.get_bias().unwrap(),
        epsilon = 1e-12
    );
}

// fit_predict convenience method

/// fit_predict returns the same array as fit then predict on the same data
#[test]
fn fit_predict_agrees_with_fit_then_predict() {
    let (x, y) = linearly_separable_data();

    let mut svc_a = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    let fp_preds = svc_a.fit_predict(&x, &y).expect("fit_predict must succeed");

    let mut svc_b = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc_b.fit(&x, &y).expect("fit must succeed");
    let preds = svc_b.predict(&x).expect("predict must succeed");

    assert_eq!(fp_preds, preds, "fit_predict must match fit+predict");
}

// save / load round-trip

/// Saving a fitted model and loading it back yields identical predictions and
/// decision-function values
#[test]
fn save_load_round_trip_yields_identical_predictions() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(0.5),
        },
        5.0,
        1e-3,
        1000,
    )
    .unwrap()
    .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let original_preds = svc.predict(&x).expect("predict must succeed before save");
    let original_df = svc
        .decision_function(&x)
        .expect("df must succeed before save");

    // write to a temporary path
    let path = "/tmp/rustyml_svc_test_roundtrip.json";
    svc.save_to_path(path).expect("save_to_path must succeed");

    let loaded = SVC::load_from_path(path).expect("load_from_path must succeed");

    let loaded_preds = loaded.predict(&x).expect("predict must succeed after load");
    let loaded_df = loaded
        .decision_function(&x)
        .expect("df must succeed after load");

    assert_eq!(
        original_preds, loaded_preds,
        "predictions must match after round-trip"
    );
    for (a, b) in original_df.iter().zip(loaded_df.iter()) {
        assert_abs_diff_eq!(a, b, epsilon = 1e-10);
    }

    // hyperparameters must survive round-trip
    assert_abs_diff_eq!(
        svc.get_regularization_parameter(),
        loaded.get_regularization_parameter(),
        epsilon = 1e-12
    );
    assert_abs_diff_eq!(
        svc.get_bias().unwrap(),
        loaded.get_bias().unwrap(),
        epsilon = 1e-12
    );

    // clean up the temp file; failure to delete is non-fatal for test correctness
    let _ = std::fs::remove_file(path);
}

/// A linear-kernel model round-trips identically through save/load, exercising
/// kernel serialization
#[test]
fn save_load_round_trip_linear_kernel() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let path = "/tmp/rustyml_svc_test_linear_roundtrip.json";
    svc.save_to_path(path).expect("save_to_path must succeed");
    let loaded = SVC::load_from_path(path).expect("load_from_path must succeed");

    let preds_orig = svc.predict(&x).expect("original predict");
    let preds_load = loaded.predict(&x).expect("loaded predict");
    assert_eq!(
        preds_orig, preds_load,
        "linear kernel round-trip must match"
    );

    let _ = std::fs::remove_file(path);
}

// EmptyInput guard

/// predict on an empty input matrix -> EmptyInput
#[test]
fn predict_empty_input_returns_error() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let x_empty = Array2::<f64>::zeros((0, 2));
    let result = svc.predict(&x_empty);
    assert!(
        matches!(result, Err(Error::EmptyInput(_))),
        "expected EmptyInput for zero-row input, got {:?}",
        result
    );
}

/// decision_function on an empty input matrix -> EmptyInput
#[test]
fn decision_function_empty_input_returns_error() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-3, 1000)
        .unwrap()
        .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let x_empty = Array2::<f64>::zeros((0, 2));
    let result = svc.decision_function(&x_empty);
    assert!(
        matches!(result, Err(Error::EmptyInput(_))),
        "expected EmptyInput for zero-row input, got {:?}",
        result
    );
}

// Sigmoid kernel: construct, fit, predict

/// Sigmoid-kernel SVC on linearly-separable data: each predicted label agrees
/// with the sign of its decision value
#[test]
fn sigmoid_kernel_sign_consistency() {
    let (x, y) = linearly_separable_data();
    let mut svc = SVC::new(
        KernelType::Sigmoid {
            gamma: Gamma::Value(0.1),
            coef0: 0.0,
        },
        5.0,
        1e-3,
        1000,
    )
    .unwrap()
    .with_random_state(42);
    svc.fit(&x, &y).expect("fit must succeed");

    let df = svc
        .decision_function(&x)
        .expect("decision_function must succeed");
    let preds = svc.predict(&x).expect("predict must succeed");

    for (i, (&dv, &p)) in df.iter().zip(preds.iter()).enumerate() {
        let expected_pred = if dv >= 0.0 { 1.0_f64 } else { -1.0_f64 };
        assert_eq!(
            p, expected_pred,
            "sample {i}: Sigmoid decision_value={dv}, predict={p} disagrees"
        );
    }
}

// fit: no support vectors (single-class data) -> NotConverged

/// Single-class training data yields an empty support set, so fit surfaces
/// NotConverged rather than a degenerate all-zero model
#[test]
fn fit_single_class_data_returns_not_converged() {
    let x = array![[0.0, 0.0], [1.0, 1.0], [2.0, 0.5], [0.5, 2.0]];
    let y = array![1.0, 1.0, 1.0, 1.0]; // one class only
    let mut svc = SVC::new(KernelType::Linear, 1.0, 1e-3, 100)
        .unwrap()
        .with_random_state(42);
    let result = svc.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::NotConverged(_))),
        "single-class data must yield no support vectors → NotConverged, got {result:?}"
    );
}

// decision_function / bias: closed-form ground truth (linear kernel)

/// On a 1-D separable set whose max-margin solution is f(x) = x - 1, bias and
/// decision values match the independently derived non-zero ground truth
#[test]
fn decision_function_and_bias_match_closed_form_linear_kernel() {
    let x = array![[0.0], [-1.0], [2.0], [3.0]];
    let y = array![-1.0, -1.0, 1.0, 1.0];
    let mut svc = SVC::new(KernelType::Linear, 10.0, 1e-5, 5000)
        .unwrap()
        .with_random_state(7);
    svc.fit(&x, &y).expect("separable data must fit");

    // asymmetric placement forces a non-zero intercept: b = -1
    assert_abs_diff_eq!(svc.get_bias().unwrap(), -1.0, epsilon = 1e-2);

    // f(x) = x - 1 at the two support vectors (the +/-1 margins) and the boundary
    let probe = array![[0.0], [2.0], [1.0]];
    let df = svc
        .decision_function(&probe)
        .expect("decision_function must succeed");
    assert_abs_diff_eq!(df[0], -1.0, epsilon = 1e-2); // SV on the -1 margin
    assert_abs_diff_eq!(df[1], 1.0, epsilon = 1e-2); // SV on the +1 margin
    assert_abs_diff_eq!(df[2], 0.0, epsilon = 1e-2); // decision boundary
}

// data-dependent gamma (Scale / Auto)

/// RBF `Gamma::Scale` is resolved at fit time to 1/(n_features * X.var()) and produces the
/// same model as constructing the kernel with that explicit value
#[test]
fn rbf_gamma_scale_resolves_and_matches_explicit_equivalent() {
    let x = Array2::from_shape_vec(
        (6, 2),
        vec![
            0.0, 0.0, 0.5, 0.2, 0.1, 0.4, // class -1
            3.0, 3.0, 3.2, 2.8, 2.9, 3.1, // class +1
        ],
    )
    .unwrap();
    let y = array![-1.0, -1.0, -1.0, 1.0, 1.0, 1.0];

    // Expected scale gamma: X.var() is the population variance of all matrix entries
    let n_features = x.ncols() as f64;
    let mean = x.iter().sum::<f64>() / x.len() as f64;
    let var = x.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / x.len() as f64;
    let expected_gamma = 1.0 / (n_features * var);

    let mut scale_model = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Scale,
        },
        1.0,
        1e-3,
        200,
    )
    .unwrap()
    .with_random_state(0);
    scale_model.fit(&x, &y).unwrap();

    // After fit, gamma is a resolved Value approximately equal to the scale formula
    let resolved_gamma = match scale_model.get_kernel() {
        KernelType::RBF {
            gamma: Gamma::Value(g),
        } => g,
        other => panic!("expected resolved RBF Value after fit, got {other:?}"),
    };
    assert_abs_diff_eq!(resolved_gamma, expected_gamma, epsilon = 1e-9);

    // A model built with the exact resolved gamma must predict identically
    let mut explicit_model = SVC::new(
        KernelType::RBF {
            gamma: Gamma::Value(resolved_gamma),
        },
        1.0,
        1e-3,
        200,
    )
    .unwrap()
    .with_random_state(0);
    explicit_model.fit(&x, &y).unwrap();

    assert_eq!(
        scale_model.predict(&x).unwrap(),
        explicit_model.predict(&x).unwrap(),
        "Scale must behave identically to its resolved explicit gamma"
    );
}

/// RBF `Gamma::Auto` is resolved at fit time to exactly 1/n_features
#[test]
fn rbf_gamma_auto_resolves_to_inverse_n_features() {
    let x = Array2::from_shape_vec(
        (4, 3),
        vec![
            0.0, 0.0, 0.0, 0.1, 0.2, 0.1, // class -1
            3.0, 3.0, 3.0, 3.1, 2.9, 3.0, // class +1
        ],
    )
    .unwrap();
    let y = array![-1.0, -1.0, 1.0, 1.0];

    let mut model = SVC::new(KernelType::RBF { gamma: Gamma::Auto }, 1.0, 1e-3, 200)
        .unwrap()
        .with_random_state(0);
    model.fit(&x, &y).unwrap();

    match model.get_kernel() {
        KernelType::RBF {
            gamma: Gamma::Value(g),
        } => assert_abs_diff_eq!(g, 1.0 / 3.0, epsilon = 1e-12),
        other => panic!("expected resolved RBF Value, got {other:?}"),
    }
}