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
//! Support-vector machine evaluation — RBF kernel dot product.
//!
//! Implements `SupportVectorMachineModel` with an RBF (`exp(-γ‖x - sv‖²)`) kernel.
//! Inputs are the ordered `VectorFields` (`VectorDictionary/VectorFields/@field`), each
//! must be `Continuous` (otherwise `Missing`). The raw score is `absoluteValue + Σ coeff[i] * K(input, sv_i)`
//! where `sv_i` is the dense vector from `VectorDictionary/VectorInstances` looked up by
//! `SupportVector/@vectorId` and `coeff` is `Coefficients/@value`. For regression the raw sum is returned
//! as `Continuous`; classification threshold mapping is not yet implemented.
//!
//! # What belongs here
//!
//! - [`evaluate_support_vector_machine`] — the single public entry point.
//!
//! # Performance
//!
//! `O(support_vectors * dims)` where `dims = vector_fields.len()`. No allocation beyond `HashMap` for `vectorId → array`.
use crateValue;
use crateSupportVectorMachineIr;
use HashMap;
/// Evaluate a [`SupportVectorMachineIr`] against a dense `values` array.
///
/// Builds the input vector `x` from `svm.vector_fields` (all must be `Continuous`; any `Missing`
/// or `Discrete` yields `Missing`), then computes `sum = absoluteValue + Σ coeff[i] * exp(-γ ‖x - sv_i‖²)`.
/// Dimensionality mismatches (`sv_i.len() != dims`) are skipped. `γ` is `SupportVectorMachine/@gamma`
/// (RBF kernel width).
///
/// # Parameters
///
/// - `svm`: Lowered SVM model (`SupportVectorMachineIr`) with `vector_fields`, `vector_instances` (`id → array`),
/// `support_vectors` (ordered `vectorId`s), `coefficients`, `absolute_value`, `kernel_gamma`.
/// - `values`: Dense `&[Value]` indexed by [`FieldId`](crate::base::FieldId). Out-of-bounds → `Missing`.
///
/// # Returns
///
/// `Continuous(sum)` on success, `Missing` when `vector_fields` or `support_vectors` is empty,
/// or when any required input is `Missing`/`Discrete`.
///
/// # Panics
///
/// Never panics. All `FieldId` indexing and `support_vectors`/`coefficients` length mismatches are guarded.
///
/// # Performance
///
/// `O(support_vectors * dims)` with one `exp` per support vector. Hash lookup for `vectorId → array`.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{FieldId, Value};
/// use pmmlruntime::ir::*;
/// use pmmlruntime::engine::models::evaluate_support_vector_machine;
///
/// let f = FieldId(0);
/// let svm = SupportVectorMachineIr {
/// function_name: "regression".into(),
/// mining_schema: MiningSchemaIr { active_fields: vec![f], target_field: None, field_metas: vec![], missing_value_replacement: None },
/// output: vec![],
/// vector_fields: vec![f],
/// vector_instances: vec![("sv1".into(), vec![0.0]), ("sv2".into(), vec![1.0])],
/// support_vectors: vec!["sv1".into(), "sv2".into()],
/// coefficients: vec![1.0, -1.0],
/// absolute_value: 0.0,
/// kernel_gamma: 1.0,
/// };
/// let pred = evaluate_support_vector_machine(&svm, &[Value::Continuous(0.0)]);
/// match pred { Value::Continuous(v) => assert!(v.is_finite()), _ => panic!("expected continuous") }
/// ```