libsvm-rs 0.8.0

FFI-free Rust implementation of LIBSVM-compatible SVM training and prediction
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
//! Core LIBSVM-compatible data structures.
//!
//! These types intentionally mirror LIBSVM concepts: sparse nodes, problem
//! rows, solver parameters, and trained model fields. Values produced by
//! [`crate::io::load_problem`] and [`crate::io::load_model`] have passed the
//! loader's text-format and resource-bound checks. Values constructed manually
//! by a caller are not automatically checked; call [`check_parameter`] before
//! training, and prefer the loader APIs for external model/problem files.

/// Type of SVM formulation.
///
/// Matches the integer constants in the original LIBSVM (`svm.h`):
/// `C_SVC=0, NU_SVC=1, ONE_CLASS=2, EPSILON_SVR=3, NU_SVR=4`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum SvmType {
    /// C-Support Vector Classification.
    CSvc = 0,
    /// ν-Support Vector Classification.
    NuSvc = 1,
    /// One-class SVM (distribution estimation / novelty detection).
    OneClass = 2,
    /// ε-Support Vector Regression.
    EpsilonSvr = 3,
    /// ν-Support Vector Regression.
    NuSvr = 4,
}

/// Type of kernel function.
///
/// Matches the integer constants in the original LIBSVM (`svm.h`):
/// `LINEAR=0, POLY=1, RBF=2, SIGMOID=3, PRECOMPUTED=4`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum KernelType {
    /// `K(x,y) = x·y`
    Linear = 0,
    /// `K(x,y) = (γ·x·y + coef0)^degree`
    Polynomial = 1,
    /// `K(x,y) = exp(-γ·‖x-y‖²)`
    Rbf = 2,
    /// `K(x,y) = tanh(γ·x·y + coef0)`
    Sigmoid = 3,
    /// Kernel values supplied as a precomputed matrix.
    Precomputed = 4,
}

/// A single sparse feature: `index:value`.
///
/// In the original LIBSVM, a sentinel node with `index = -1` marks the end
/// of each instance. In this Rust port, instance length is tracked by
/// `Vec::len()` instead, so no sentinel is needed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SvmNode {
    /// 1-based feature index. Uses `i32` to match the original C `int` and
    /// preserve file-format compatibility.
    pub index: i32,
    /// Feature value.
    pub value: f64,
}

/// A training/test problem: a collection of labelled sparse instances.
///
/// `load_problem` validates that sparse feature indices are ascending and
/// within the configured [`crate::io::LoadOptions`] bounds. When constructing a
/// problem manually, keep `labels.len() == instances.len()` and use ascending
/// feature indices to match LIBSVM input assumptions.
#[derive(Debug, Clone, PartialEq)]
pub struct SvmProblem {
    /// Label (class for classification, target for regression) per instance.
    pub labels: Vec<f64>,
    /// Sparse feature vectors, one per instance.
    pub instances: Vec<Vec<SvmNode>>,
}

/// SVM parameters controlling the formulation, kernel, and solver.
///
/// Default values match the original LIBSVM defaults.
#[derive(Debug, Clone, PartialEq)]
pub struct SvmParameter {
    /// SVM formulation type.
    pub svm_type: SvmType,
    /// Kernel function type.
    pub kernel_type: KernelType,
    /// Degree for polynomial kernel.
    pub degree: i32,
    /// γ parameter for RBF, polynomial, and sigmoid kernels.
    /// Set to `1/num_features` when 0.
    pub gamma: f64,
    /// Independent term in polynomial and sigmoid kernels.
    pub coef0: f64,
    /// Cache memory size in MB.
    pub cache_size: f64,
    /// Stopping tolerance for the solver.
    pub eps: f64,
    /// Cost parameter C (for C-SVC, ε-SVR, ν-SVR).
    pub c: f64,
    /// Per-class weight overrides: `(class_label, weight)` pairs.
    pub weight: Vec<(i32, f64)>,
    /// ν parameter (for ν-SVC, one-class SVM, ν-SVR).
    pub nu: f64,
    /// ε in the ε-insensitive loss function (ε-SVR).
    pub p: f64,
    /// Whether to use the shrinking heuristic.
    pub shrinking: bool,
    /// Whether to train for probability estimates.
    pub probability: bool,
}

impl Default for SvmParameter {
    fn default() -> Self {
        Self {
            svm_type: SvmType::CSvc,
            kernel_type: KernelType::Rbf,
            degree: 3,
            gamma: 0.0, // means 1/num_features
            coef0: 0.0,
            cache_size: 100.0,
            eps: 0.001,
            c: 1.0,
            weight: Vec::new(),
            nu: 0.5,
            p: 0.1,
            shrinking: true,
            probability: false,
        }
    }
}

impl SvmParameter {
    /// Validate parameter values (independent of training data).
    ///
    /// This checks the same constraints as the original LIBSVM's
    /// `svm_check_parameter`, except for the ν-SVC feasibility check
    /// which requires the problem. Use [`check_parameter`] for the full check.
    pub fn validate(&self) -> Result<(), crate::error::SvmError> {
        use crate::error::SvmError;

        // gamma must be non-negative for kernels that use it
        if matches!(
            self.kernel_type,
            KernelType::Polynomial | KernelType::Rbf | KernelType::Sigmoid
        ) && self.gamma < 0.0
        {
            return Err(SvmError::InvalidParameter("gamma < 0".into()));
        }

        // polynomial degree must be non-negative
        if self.kernel_type == KernelType::Polynomial && self.degree < 0 {
            return Err(SvmError::InvalidParameter(
                "degree of polynomial kernel < 0".into(),
            ));
        }

        if self.cache_size <= 0.0 {
            return Err(SvmError::InvalidParameter("cache_size <= 0".into()));
        }

        if self.eps <= 0.0 {
            return Err(SvmError::InvalidParameter("eps <= 0".into()));
        }

        // C > 0 for formulations that use it
        if matches!(
            self.svm_type,
            SvmType::CSvc | SvmType::EpsilonSvr | SvmType::NuSvr
        ) && self.c <= 0.0
        {
            return Err(SvmError::InvalidParameter("C <= 0".into()));
        }

        // nu ∈ (0, 1] for formulations that use it
        if matches!(
            self.svm_type,
            SvmType::NuSvc | SvmType::OneClass | SvmType::NuSvr
        ) && (self.nu <= 0.0 || self.nu > 1.0)
        {
            return Err(SvmError::InvalidParameter("nu <= 0 or nu > 1".into()));
        }

        // p >= 0 for epsilon-SVR
        if self.svm_type == SvmType::EpsilonSvr && self.p < 0.0 {
            return Err(SvmError::InvalidParameter("p < 0".into()));
        }

        Ok(())
    }
}

/// Full parameter check including ν-SVC feasibility against training data.
///
/// Matches the original LIBSVM `svm_check_parameter()`.
pub fn check_parameter(
    problem: &SvmProblem,
    param: &SvmParameter,
) -> Result<(), crate::error::SvmError> {
    use crate::error::SvmError;

    // Run the data-independent checks first
    param.validate()?;

    if problem.labels.len() != problem.instances.len() {
        return Err(SvmError::InvalidParameter(format!(
            "labels length ({}) does not match instance length ({})",
            problem.labels.len(),
            problem.instances.len()
        )));
    }

    if problem.labels.is_empty() {
        return Err(SvmError::InvalidParameter(
            "problem has no instances".into(),
        ));
    }

    if param.kernel_type == KernelType::Precomputed {
        let upper = problem.instances.len() as f64;
        for (row, instance) in problem.instances.iter().enumerate() {
            let first = instance.first().ok_or_else(|| {
                SvmError::InvalidParameter(format!(
                    "precomputed kernel row {} is missing 0:sample_serial_number",
                    row + 1
                ))
            })?;
            if first.index != 0
                || !first.value.is_finite()
                || first.value < 1.0
                || first.value > upper
                || first.value.fract() != 0.0
            {
                return Err(SvmError::InvalidParameter(format!(
                    "precomputed kernel row {} must start with 0:sample_serial_number in [1, {}]",
                    row + 1,
                    problem.instances.len()
                )));
            }
        }
    }

    // ν-SVC feasibility: for every pair of classes (i, j),
    // nu * (count_i + count_j) / 2 must be <= min(count_i, count_j)
    //
    // Note: LIBSVM casts labels to int for class grouping. We match this
    // behavior. Classification labels must be integers (non-integer labels
    // will be truncated, matching `(int)prob->y[i]` in the C code).
    if param.svm_type == SvmType::NuSvc {
        let mut class_counts: Vec<(i32, usize)> = Vec::new();
        for &y in &problem.labels {
            let label = y as i32;
            if let Some(entry) = class_counts.iter_mut().find(|(l, _)| *l == label) {
                entry.1 += 1;
            } else {
                class_counts.push((label, 1));
            }
        }

        for (i, &(_, n1)) in class_counts.iter().enumerate() {
            for &(_, n2) in &class_counts[i + 1..] {
                if param.nu * (n1 + n2) as f64 / 2.0 > n1.min(n2) as f64 {
                    return Err(SvmError::InvalidParameter(
                        "specified nu is infeasible".into(),
                    ));
                }
            }
        }
    }

    Ok(())
}

/// A trained SVM model.
///
/// Produced by training, or loaded from a LIBSVM model file.
///
/// `load_model` validates model-file shape contracts before returning this
/// type: class counts, support-vector counts, decision-function arrays, optional
/// probability metadata, and sparse support-vector rows must be internally
/// consistent. Manually constructed values bypass those checks, so callers that
/// accept external model text should prefer [`crate::io::load_model`] or
/// [`crate::io::load_model_from_reader`].
#[derive(Debug, Clone, PartialEq)]
pub struct SvmModel {
    /// Parameters used during training.
    pub param: SvmParameter,
    /// Number of classes (2 for binary, >2 for multiclass, 2 for regression).
    pub nr_class: usize,
    /// Support vectors (sparse feature vectors).
    pub sv: Vec<Vec<SvmNode>>,
    /// Support vector coefficients. For k classes, this is a
    /// `(k-1) × num_sv` matrix stored as `Vec<Vec<f64>>`.
    pub sv_coef: Vec<Vec<f64>>,
    /// Bias terms (rho). One per class pair: `k*(k-1)/2` values.
    pub rho: Vec<f64>,
    /// Pairwise probability parameter A (Platt scaling). Empty if not trained
    /// with probability estimates.
    pub prob_a: Vec<f64>,
    /// Pairwise probability parameter B (Platt scaling). Empty if not trained
    /// with probability estimates.
    pub prob_b: Vec<f64>,
    /// Probability density marks (for one-class SVM).
    pub prob_density_marks: Vec<f64>,
    /// Original indices of support vectors in the training set (1-based).
    pub sv_indices: Vec<usize>,
    /// Class labels (in the order used internally).
    pub label: Vec<i32>,
    /// Number of support vectors per class.
    pub n_sv: Vec<usize>,
}

impl SvmModel {
    /// Return the SVM type used by the model.
    pub fn svm_type(&self) -> SvmType {
        self.param.svm_type
    }

    /// Return number of classes.
    pub fn class_count(&self) -> usize {
        self.nr_class
    }

    /// Return class labels in internal one-vs-one order.
    pub fn labels(&self) -> &[i32] {
        &self.label
    }

    /// Return original 1-based support-vector indices.
    pub fn support_vector_indices(&self) -> &[usize] {
        &self.sv_indices
    }

    /// Return total number of support vectors.
    pub fn support_vector_count(&self) -> usize {
        self.sv.len()
    }

    /// Return SVR sigma when a probability-capable SVR model is available.
    pub fn svr_probability(&self) -> Option<f64> {
        match self.param.svm_type {
            SvmType::EpsilonSvr | SvmType::NuSvr => self.prob_a.first().copied(),
            _ => None,
        }
    }

    /// Check whether the model contains probability metadata.
    pub fn has_probability_model(&self) -> bool {
        match self.param.svm_type {
            SvmType::CSvc | SvmType::NuSvc => !self.prob_a.is_empty() && !self.prob_b.is_empty(),
            SvmType::EpsilonSvr | SvmType::NuSvr => !self.prob_a.is_empty(),
            SvmType::OneClass => !self.prob_density_marks.is_empty(),
        }
    }
}

/// C-API style helper matching LIBSVM's `svm_get_svm_type`.
pub fn svm_get_svm_type(model: &SvmModel) -> SvmType {
    model.svm_type()
}

/// C-API style helper matching LIBSVM's `svm_get_nr_class`.
pub fn svm_get_nr_class(model: &SvmModel) -> usize {
    model.class_count()
}

/// C-API style helper matching LIBSVM's `svm_get_labels`.
pub fn svm_get_labels(model: &SvmModel) -> &[i32] {
    model.labels()
}

/// C-API style helper matching LIBSVM's `svm_get_sv_indices`.
pub fn svm_get_sv_indices(model: &SvmModel) -> &[usize] {
    model.support_vector_indices()
}

/// C-API style helper matching LIBSVM's `svm_get_nr_sv`.
pub fn svm_get_nr_sv(model: &SvmModel) -> usize {
    model.support_vector_count()
}

/// C-API style helper matching LIBSVM's `svm_get_svr_probability`.
pub fn svm_get_svr_probability(model: &SvmModel) -> Option<f64> {
    model.svr_probability()
}

/// C-API style helper matching LIBSVM's `svm_check_probability_model`.
pub fn svm_check_probability_model(model: &SvmModel) -> bool {
    model.has_probability_model()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::train::svm_train;
    use std::path::PathBuf;

    fn data_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("..")
            .join("..")
            .join("data")
    }

    #[test]
    fn default_params_are_valid() {
        SvmParameter::default().validate().unwrap();
    }

    #[test]
    fn negative_gamma_rejected() {
        let p = SvmParameter {
            gamma: -1.0,
            ..Default::default()
        };
        assert!(p.validate().is_err());
    }

    #[test]
    fn zero_cache_rejected() {
        let p = SvmParameter {
            cache_size: 0.0,
            ..Default::default()
        };
        assert!(p.validate().is_err());
    }

    #[test]
    fn zero_c_rejected() {
        let p = SvmParameter {
            c: 0.0,
            ..Default::default()
        };
        assert!(p.validate().is_err());
    }

    #[test]
    fn nu_out_of_range_rejected() {
        let p = SvmParameter {
            svm_type: SvmType::NuSvc,
            nu: 1.5,
            ..Default::default()
        };
        assert!(p.validate().is_err());

        let p2 = SvmParameter {
            svm_type: SvmType::NuSvc,
            nu: 0.0,
            ..Default::default()
        };
        assert!(p2.validate().is_err());
    }

    #[test]
    fn negative_p_rejected_for_svr() {
        let p = SvmParameter {
            svm_type: SvmType::EpsilonSvr,
            p: -0.1,
            ..Default::default()
        };
        assert!(p.validate().is_err());
    }

    #[test]
    fn negative_poly_degree_rejected() {
        let p = SvmParameter {
            kernel_type: KernelType::Polynomial,
            degree: -1,
            ..Default::default()
        };
        assert!(p.validate().is_err());
    }

    #[test]
    fn check_parameter_rejects_empty_problem() {
        let problem = SvmProblem {
            labels: Vec::new(),
            instances: Vec::new(),
        };
        let err = check_parameter(&problem, &SvmParameter::default()).unwrap_err();
        assert!(format!("{}", err).contains("problem has no instances"));
    }

    #[test]
    fn check_parameter_rejects_label_instance_length_mismatch() {
        let problem = SvmProblem {
            labels: vec![1.0],
            instances: Vec::new(),
        };
        let err = check_parameter(&problem, &SvmParameter::default()).unwrap_err();
        assert!(format!("{}", err).contains("does not match instance length"));
    }

    #[test]
    fn check_parameter_rejects_precomputed_rows_without_sample_serial_number() {
        let problem = SvmProblem {
            labels: vec![1.0, -1.0],
            instances: vec![
                vec![],
                vec![SvmNode {
                    index: 0,
                    value: 2.0,
                }],
            ],
        };
        let param = SvmParameter {
            kernel_type: KernelType::Precomputed,
            ..Default::default()
        };
        let err = check_parameter(&problem, &param).unwrap_err();
        assert!(format!("{}", err).contains("missing 0:sample_serial_number"));
    }

    #[test]
    fn nu_svc_feasibility_check() {
        // 2 classes with 3 samples each: nu * (3+3)/2 <= 3  →  nu <= 1
        let problem = SvmProblem {
            labels: vec![1.0, 1.0, 1.0, 2.0, 2.0, 2.0],
            instances: vec![vec![]; 6],
        };
        let ok_param = SvmParameter {
            svm_type: SvmType::NuSvc,
            nu: 0.5,
            ..Default::default()
        };
        check_parameter(&problem, &ok_param).unwrap();

        // nu = 0.9: 0.9 * 6/2 = 2.7 <= 3 → feasible
        let borderline = SvmParameter {
            svm_type: SvmType::NuSvc,
            nu: 0.9,
            ..Default::default()
        };
        check_parameter(&problem, &borderline).unwrap();
    }

    #[test]
    fn nu_svc_infeasible() {
        // 5 class-A, 1 class-B: nu*(5+1)/2 > min(5,1)=1  →  nu > 1/3
        let problem = SvmProblem {
            labels: vec![1.0, 1.0, 1.0, 1.0, 1.0, 2.0],
            instances: vec![vec![]; 6],
        };
        let param = SvmParameter {
            svm_type: SvmType::NuSvc,
            nu: 0.5, // 0.5 * 6/2 = 1.5 > 1
            ..Default::default()
        };
        let err = check_parameter(&problem, &param);
        assert!(err.is_err());
        assert!(format!("{}", err.unwrap_err()).contains("infeasible"));
    }

    #[test]
    fn c_api_style_model_helpers() {
        let problem = crate::io::load_problem(&data_dir().join("heart_scale")).unwrap();
        let param = SvmParameter {
            gamma: 1.0 / 13.0,
            ..Default::default()
        };
        let model = svm_train(&problem, &param);

        assert_eq!(svm_get_svm_type(&model), SvmType::CSvc);
        assert_eq!(svm_get_nr_class(&model), 2);
        assert_eq!(svm_get_nr_sv(&model), model.sv.len());
        assert_eq!(svm_get_labels(&model), model.label.as_slice());
        assert_eq!(svm_get_sv_indices(&model), model.sv_indices.as_slice());
        assert!(!svm_check_probability_model(&model));
        assert_eq!(svm_get_svr_probability(&model), None);
    }

    #[test]
    fn probability_helpers_by_svm_type() {
        let svm = vec![SvmNode {
            index: 1,
            value: 1.0,
        }];

        let csvc_model = SvmModel {
            param: SvmParameter {
                svm_type: SvmType::CSvc,
                ..Default::default()
            },
            nr_class: 2,
            sv: vec![svm.clone()],
            sv_coef: vec![vec![1.0]],
            rho: vec![0.0],
            prob_a: vec![1.0],
            prob_b: vec![-0.5],
            prob_density_marks: vec![],
            sv_indices: vec![1],
            label: vec![1, -1],
            n_sv: vec![1, 0],
        };
        assert!(csvc_model.has_probability_model());
        assert!(svm_check_probability_model(&csvc_model));
        assert_eq!(svm_get_svr_probability(&csvc_model), None);

        let eps_svr_model = SvmModel {
            param: SvmParameter {
                svm_type: SvmType::EpsilonSvr,
                ..Default::default()
            },
            nr_class: 2,
            sv: vec![svm.clone()],
            sv_coef: vec![vec![0.8]],
            rho: vec![0.0],
            prob_a: vec![0.123],
            prob_b: vec![],
            prob_density_marks: vec![],
            sv_indices: vec![1],
            label: vec![],
            n_sv: vec![],
        };
        assert!(eps_svr_model.has_probability_model());
        assert_eq!(svm_get_svr_probability(&eps_svr_model), Some(0.123));

        let one_class_model = SvmModel {
            param: SvmParameter {
                svm_type: SvmType::OneClass,
                ..Default::default()
            },
            nr_class: 2,
            sv: vec![svm],
            sv_coef: vec![vec![1.0]],
            rho: vec![0.0],
            prob_a: vec![],
            prob_b: vec![],
            prob_density_marks: vec![0.1; 10],
            sv_indices: vec![1],
            label: vec![],
            n_sv: vec![],
        };
        assert!(one_class_model.has_probability_model());
        assert_eq!(svm_get_svr_probability(&one_class_model), None);
    }
}