tensorlogic-sklears-kernels 0.1.1

Logic-derived similarity kernels for SkleaRS integration
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
//! Comprehensive tests for SVM via SMO.
//!
//! Tests cover binary/multi-class SVC, ε-SVR, error handling, KKT conditions,
//! SMO configuration defaults, and decision function correctness.

use std::sync::Arc;

use crate::error::KernelError;
use crate::tensor_kernels::types::{LinearKernel, RbfKernel};
use crate::types::{Kernel, RbfKernelConfig};

use super::smo::SmoConfig;
use super::svc::SvcModel;
use super::svr::SvrModel;

// ─── Helpers ────────────────────────────────────────────────────────────────

/// Build an `Arc<dyn Kernel>` wrapping a LinearKernel.
fn linear_kernel() -> Arc<dyn Kernel> {
    Arc::new(LinearKernel::new())
}

/// Build an `Arc<dyn Kernel>` wrapping an RBF kernel with the given gamma.
fn rbf_kernel(gamma: f64) -> Arc<dyn Kernel> {
    Arc::new(RbfKernel::new(RbfKernelConfig::new(gamma)).expect("valid gamma"))
}

// ─── SVC Tests ───────────────────────────────────────────────────────────────

/// Two clearly separated clusters in 2D — linearly separable with LinearKernel.
#[test]
fn test_linear_svc_binary_separable() {
    let x_pos = [vec![1.0, 1.0], vec![2.0, 1.0], vec![1.0, 2.0]];
    let x_neg = [vec![-1.0, -1.0], vec![-2.0, -1.0], vec![-1.0, -2.0]];
    let x: Vec<Vec<f64>> = x_pos.iter().chain(x_neg.iter()).cloned().collect();
    let y: Vec<i32> = vec![1, 1, 1, -1, -1, -1];

    let model = SvcModel::new(linear_kernel(), 10.0).expect("valid C");
    let fitted = model.fit(&x, &y).expect("fit should succeed");

    // All training points should be classified correctly.
    for (xi, &yi) in x.iter().zip(y.iter()) {
        let pred = fitted.predict(xi).expect("predict");
        assert_eq!(
            pred, yi,
            "Linear SVC misclassified training point {:?}: got {}, expected {}",
            xi, pred, yi
        );
    }
}

/// XOR dataset — 4 points, requires non-linear kernel (RBF with sufficient gamma).
#[test]
fn test_rbf_svc_xor() {
    // XOR: (+1, +1) → -1,  (-1, -1) → -1,  (+1, -1) → +1,  (-1, +1) → +1
    let x = vec![
        vec![1.0, 1.0],
        vec![-1.0, -1.0],
        vec![1.0, -1.0],
        vec![-1.0, 1.0],
    ];
    let y = vec![-1, -1, 1, 1];

    // High gamma to create tight local regions.
    let model = SvcModel::new(rbf_kernel(4.0), 100.0).expect("valid params");
    let fitted = model.fit(&x, &y).expect("fit should succeed");

    for (xi, &yi) in x.iter().zip(y.iter()) {
        let pred = fitted.predict(xi).expect("predict");
        assert_eq!(
            pred, yi,
            "RBF XOR SVC misclassified {:?}: got {}, expected {}",
            xi, pred, yi
        );
    }
}

/// Providing all the same class label should be an error (can't train binary SVC).
#[test]
fn test_svc_single_class_error() {
    let x = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
    let y = vec![1, 1, 1]; // All same class

    let model = SvcModel::new(linear_kernel(), 1.0).expect("valid C");
    let result = model.fit(&x, &y);
    assert!(
        result.is_err(),
        "Expected error when all labels are the same"
    );
    assert!(
        matches!(result, Err(KernelError::InvalidParameter { .. })),
        "Expected InvalidParameter error, got: {:?}",
        result
    );
}

/// Empty training set should produce a DimensionMismatch error.
#[test]
fn test_svc_empty_data_error() {
    let model = SvcModel::new(linear_kernel(), 1.0).expect("valid C");
    let result = model.fit(&[], &[]);
    assert!(result.is_err());
    assert!(
        matches!(result, Err(KernelError::DimensionMismatch { .. })),
        "Expected DimensionMismatch, got {:?}",
        result
    );
}

/// Negative C should produce an InvalidParameter error.
#[test]
fn test_svc_negative_c_error() {
    let result = SvcModel::new(linear_kernel(), -1.0);
    assert!(result.is_err());
    assert!(
        matches!(result, Err(KernelError::InvalidParameter { .. })),
        "Expected InvalidParameter, got {:?}",
        result
    );
}

/// Zero C should also produce an InvalidParameter error.
#[test]
fn test_svc_zero_c_error() {
    let result = SvcModel::new(linear_kernel(), 0.0);
    assert!(result.is_err());
    assert!(
        matches!(result, Err(KernelError::InvalidParameter { .. })),
        "Expected InvalidParameter for C=0, got {:?}",
        result
    );
}

/// After fitting a linearly separable dataset, the KKT conditions should hold
/// for all support vectors: |y_i * f(x_i) - 1| < 2 * tol.
///
/// KKT condition for a support vector (0 < α_i < C):
///   y_i * f(x_i) = 1  (exactly on the margin)
///   => |y_i * f(x_i) - 1| ≈ 0 within numerical tolerance.
#[test]
fn test_svc_kkt_conditions() {
    // Tightly separated data so the margin is well-defined.
    let x = vec![
        vec![2.0, 0.0],
        vec![3.0, 0.0],
        vec![-2.0, 0.0],
        vec![-3.0, 0.0],
    ];
    let y = vec![1, 1, -1, -1];

    let tol = 1e-3;
    let config = SmoConfig {
        c: 1.0,
        tol,
        max_iter: 50_000,
        ..SmoConfig::default()
    };
    let model = SvcModel::new_with_config(linear_kernel(), config).expect("valid");
    let fitted = model.fit(&x, &y).expect("fit");

    // For support vectors: |y_i * f(x_i) - 1| < 2 * tol.
    // We use the public decision_function which only works for binary SVC.
    for (xi, &yi) in x.iter().zip(y.iter()) {
        let df = fitted.decision_function(xi).expect("decision function");
        let functional_margin = (yi as f64) * df;
        // Training points that ended up as support vectors must satisfy KKT.
        // For non-support vectors (alpha = 0 or C), the margin may exceed 1.
        // All training data should have functional_margin >= 1 - tol (feasibility).
        assert!(
            functional_margin >= 1.0 - 2.0 * tol,
            "KKT feasibility violated at {:?}: y*f(x) = {}, expected >= {}",
            xi,
            functional_margin,
            1.0 - 2.0 * tol
        );
    }
}

/// Three-class linearly separable problem (well-separated blobs).
#[test]
fn test_svc_multiclass_ovr() {
    // Three clusters in 2D, each far from the others.
    // Class 0: around (5, 0),  Class 1: around (-5, 0),  Class 2: around (0, 5).
    let x: Vec<Vec<f64>> = vec![
        // Class 0 cluster
        vec![5.0, 0.0],
        vec![5.5, 0.5],
        vec![4.5, -0.5],
        // Class 1 cluster
        vec![-5.0, 0.0],
        vec![-5.5, 0.5],
        vec![-4.5, -0.5],
        // Class 2 cluster
        vec![0.0, 5.0],
        vec![0.5, 5.5],
        vec![-0.5, 4.5],
    ];
    let y: Vec<i32> = vec![0, 0, 0, 1, 1, 1, 2, 2, 2];

    let model = SvcModel::new(linear_kernel(), 1.0).expect("valid C");
    let fitted = model.fit(&x, &y).expect("fit 3-class");

    for (xi, &yi) in x.iter().zip(y.iter()) {
        let pred = fitted.predict(xi).expect("predict");
        assert_eq!(
            pred, yi,
            "Multiclass SVC misclassified {:?}: got {}, expected {}",
            xi, pred, yi
        );
    }
}

/// For well-separated linearly separable data with C not too large,
/// the number of support vectors should be small (exactly 2 for 1D data).
#[test]
fn test_svc_num_support_vectors() {
    // Perfectly linearly separable with large margin; SVs are just the boundary points.
    let x = vec![
        vec![1.0, 0.0],
        vec![2.0, 0.0],
        vec![3.0, 0.0],
        vec![-1.0, 0.0],
        vec![-2.0, 0.0],
        vec![-3.0, 0.0],
    ];
    let y = vec![1, 1, 1, -1, -1, -1];

    let model = SvcModel::new(linear_kernel(), 1.0).expect("valid C");
    let fitted = model.fit(&x, &y).expect("fit");

    let n_sv = fitted.num_support_vectors();
    // For well-separated linear data, we expect 2 support vectors (one per class boundary).
    // With larger C or overlapping data, this could be more. We use a generous bound.
    assert!(
        n_sv <= 4,
        "Expected at most 4 support vectors for well-separated linear data, got {}",
        n_sv
    );
    assert!(
        n_sv >= 1,
        "Expected at least 1 support vector, got {}",
        n_sv
    );
}

/// The decision function should have correct sign for all training points.
#[test]
fn test_svc_decision_function_signs() {
    let x = vec![vec![3.0], vec![4.0], vec![-3.0], vec![-4.0]];
    let y = vec![1, 1, -1, -1];

    let model = SvcModel::new(linear_kernel(), 10.0).expect("valid C");
    let fitted = model.fit(&x, &y).expect("fit");

    for (xi, &yi) in x.iter().zip(y.iter()) {
        let df = fitted.decision_function(xi).expect("decision function");
        let sign = if df >= 0.0 { 1_i32 } else { -1_i32 };
        assert_eq!(
            sign, yi,
            "decision_function sign wrong at {:?}: df={}, expected label {}",
            xi, df, yi
        );
    }
}

// ─── SVR Tests ───────────────────────────────────────────────────────────────

/// Fit a linear regression y = 2x + 1 with LinearKernel.
/// Using x values starting at 1 to avoid degenerate x=0 for LinearKernel.
/// Predict at x=5.0, expect result within 0.5 of 11.0.
#[test]
fn test_svr_linear_recovery() {
    let x_vals: Vec<f64> = (1..=10).map(|i| i as f64).collect();
    let y_vals: Vec<f64> = x_vals.iter().map(|&xi| 2.0 * xi + 1.0).collect();
    let x: Vec<Vec<f64>> = x_vals.iter().map(|&xi| vec![xi]).collect();

    let model = SvrModel::new(linear_kernel(), 10.0, 0.1).expect("valid params");
    let fitted = model.fit(&x, &y_vals).expect("fit linear");

    let pred = fitted.predict(&[5.0]).expect("predict");
    let expected = 11.0;
    assert!(
        (pred - expected).abs() < 1.5,
        "SVR linear recovery: predicted {}, expected {} (within 1.5)",
        pred,
        expected
    );
}

/// Fit SVR on a simple 1D quadratic with RBF kernel.
/// Use 6 carefully chosen points from y = x^2 and check training MAE.
#[test]
fn test_svr_rbf_regression() {
    // Small, simple dataset: y = x^2 for x ∈ {-2,-1,0,1,2}.
    // RBF with high gamma (tight kernel), large C (low regularization), small epsilon.
    let x: Vec<Vec<f64>> = vec![vec![-2.0], vec![-1.0], vec![0.0], vec![1.0], vec![2.0]];
    let y: Vec<f64> = x.iter().map(|xi| xi[0] * xi[0]).collect(); // [4, 1, 0, 1, 4]

    // Use moderate C and gamma with larger epsilon for stability.
    let config = super::smo::SmoConfig {
        c: 10.0,
        tol: 1e-3,
        max_iter: 50_000,
        ..Default::default()
    };
    let model = SvrModel::new_with_config(rbf_kernel(1.0), 0.5, config).expect("valid params");
    let fitted = model.fit(&x, &y).expect("fit RBF SVR");

    let preds = fitted.predict_batch(&x).expect("predict_batch");
    let mae: f64 = preds
        .iter()
        .zip(y.iter())
        .map(|(&p, &t)| (p - t).abs())
        .sum::<f64>()
        / x.len() as f64;

    // MAE should be reasonably small (within the ε-tube).
    // With ε=0.5, the maximum possible MAE without any penalization is 0.5,
    // so we use 0.8 as a conservative bound to account for finite C.
    assert!(
        mae < 0.8,
        "SVR RBF: training MAE = {} (expected < 0.8)",
        mae
    );
}

/// Empty training set should produce an error.
#[test]
fn test_svr_empty_error() {
    let model = SvrModel::new(linear_kernel(), 1.0, 0.1).expect("valid params");
    let result = model.fit(&[], &[]);
    assert!(result.is_err(), "Expected error for empty training set");
    assert!(
        matches!(result, Err(KernelError::DimensionMismatch { .. })),
        "Expected DimensionMismatch, got {:?}",
        result
    );
}

/// C = 0 should produce InvalidParameter error.
#[test]
fn test_svr_negative_c_error() {
    let result = SvrModel::new(linear_kernel(), 0.0, 0.1);
    assert!(result.is_err());
    assert!(
        matches!(result, Err(KernelError::InvalidParameter { .. })),
        "Expected InvalidParameter for C=0, got {:?}",
        result
    );
}

/// predict_batch should return a vector of the same length as the input batch.
#[test]
fn test_svr_predict_batch_shape() {
    let x: Vec<Vec<f64>> = (0..5).map(|i| vec![i as f64]).collect();
    let y: Vec<f64> = x.iter().map(|xi| xi[0] * 2.0).collect();

    let model = SvrModel::new(linear_kernel(), 5.0, 0.1).expect("valid");
    let fitted = model.fit(&x, &y).expect("fit");

    let test_x: Vec<Vec<f64>> = (0..8).map(|i| vec![i as f64 * 0.5]).collect();
    let preds = fitted.predict_batch(&test_x).expect("predict_batch");
    assert_eq!(
        preds.len(),
        test_x.len(),
        "predict_batch output length mismatch"
    );
}

// ─── SMO Config Tests ────────────────────────────────────────────────────────

/// SmoConfig::default() should have the specified values.
#[test]
fn test_smo_config_default() {
    let cfg = SmoConfig::default();
    assert_eq!(cfg.c, 1.0, "default C should be 1.0");
    assert_eq!(cfg.tol, 1e-3, "default tol should be 1e-3");
    assert_eq!(cfg.epsilon, 0.1, "default epsilon should be 0.1");
    assert_eq!(cfg.max_iter, 10_000, "default max_iter should be 10000");
}

/// SmoConfig should be Clone and Debug.
#[test]
fn test_smo_config_clone_debug() {
    let cfg = SmoConfig {
        c: 5.0,
        tol: 1e-4,
        epsilon: 0.5,
        max_iter: 1000,
    };
    let cloned = cfg.clone();
    assert_eq!(cloned.c, cfg.c);
    let dbg = format!("{:?}", cfg);
    assert!(dbg.contains("SmoConfig"));
}

/// SVC predict_batch returns a vector of the correct length.
#[test]
fn test_svc_predict_batch_shape() {
    let x = vec![
        vec![1.0, 0.0],
        vec![0.0, 1.0],
        vec![-1.0, 0.0],
        vec![0.0, -1.0],
    ];
    let y = vec![1, 1, -1, -1];

    let model = SvcModel::new(linear_kernel(), 1.0).expect("valid C");
    let fitted = model.fit(&x, &y).expect("fit");

    let test_x = vec![vec![2.0, 1.0], vec![-2.0, -1.0], vec![0.5, 0.5]];
    let preds = fitted.predict_batch(&test_x).expect("predict_batch");
    assert_eq!(preds.len(), 3, "predict_batch output length mismatch");
}

/// SVC with mismatched x/y lengths should return DimensionMismatch.
#[test]
fn test_svc_mismatched_lengths_error() {
    let x = vec![vec![1.0], vec![2.0], vec![3.0]];
    let y = vec![1, -1]; // wrong length

    let model = SvcModel::new(linear_kernel(), 1.0).expect("valid C");
    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(KernelError::DimensionMismatch { .. })),
        "Expected DimensionMismatch, got {:?}",
        result
    );
}

/// SVR model creation with negative epsilon should fail.
#[test]
fn test_svr_negative_epsilon_error() {
    let result = SvrModel::new(linear_kernel(), 1.0, -0.1);
    assert!(result.is_err());
    assert!(
        matches!(result, Err(KernelError::InvalidParameter { .. })),
        "Expected InvalidParameter for negative epsilon, got {:?}",
        result
    );
}

/// SVR with mismatched x/y lengths should return DimensionMismatch.
#[test]
fn test_svr_mismatched_lengths_error() {
    let x = vec![vec![1.0], vec![2.0]];
    let y = vec![1.0, 2.0, 3.0]; // wrong length

    let model = SvrModel::new(linear_kernel(), 1.0, 0.1).expect("valid");
    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(KernelError::DimensionMismatch { .. })),
        "Expected DimensionMismatch, got {:?}",
        result
    );
}