# gizml:svm

support vector machines. The SMO solver of LIBSVM (second-order working set selection, Fan, Chen and Lin 2005) for C-SVC, nu-SVC, epsilon-SVR and nu-SVR, one-vs-one for more than two classes, and the dual coordinate descent of LIBLINEAR for linear SVC (squared hinge) and linear SVR (epsilon-insensitive). Matrices are row-major.

```gizlang
import * as Svm from "gizml:svm";
```

## Types

### `Kernel`

```gizlang
type Kernel = { kind: string; gamma: f64; coef0: f64; degree: i64; }
```

A kernel: rbf exp(-gamma |x - z|²), linear x·z, poly (gamma x·z + coef0)^degree, sigmoid tanh(gamma x·z + coef0).

- `kind`: `string`
- `gamma`: `f64`
- `coef0`: `f64`
- `degree`: `i64`

### `Svm`

```gizlang
type Svm = { kernel: Kernel; features: i64; support: Vec<f64>; count: i64; coef: Vec<f64>; rho: Vec<f64>; classes: i64; }
```

The support vectors (count × features), and for each decision function its coefficient of each support vector and its
rho: f(x) = Σ coef K(sv, x) - rho. classes: 0 for regression (one function), else the class count (one function for
each pair i < j, positive for class i).

- `kernel`: `M31.Kernel`
- `features`: `i64`
- `support`: `Vec<f64>`
- `count`: `i64`
- `coef`: `Vec<f64>`
- `rho`: `Vec<f64>`
- `classes`: `i64`

### `SvmParams`

```gizlang
type SvmParams = { kernel: Kernel; cost: f64; nu: f64; epsilon: f64; tol: f64; maxIter: i64; }
```

cost: C. nu: the nu of nu-SVC and nu-SVR (0 for the C variants). epsilon: the tube of epsilon-SVR. maxIter < 0: none.

- `kernel`: `M31.Kernel`
- `cost`: `f64`
- `nu`: `f64`
- `epsilon`: `f64`
- `tol`: `f64`
- `maxIter`: `i64`

## Functions

### `fitSvc`

```gizlang
function fitSvc(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, k: i64, p: SvmParams): Svm throws string
```

SVC or nu-SVC on class indexes y (0..k-1): one-vs-one, the support vectors shared by the functions.

Effects: fail.

### `fitSvr`

```gizlang
function fitSvr(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, p: SvmParams): Svm
```

epsilon-SVR or nu-SVR on one target.

Effects: fail.

### `linearSvc`

```gizlang
function linearSvc(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, cost: f64, tol: f64, maxIter: i64, rng: mut Random): Vec<f64>
```

LIBLINEAR's dual coordinate descent with a bias feature of 1 (intercept_scaling 1), as scikit-learn's LinearSVC
(squared hinge, one-vs-rest; y ±1) and LinearSVR (epsilon-insensitive). The weights: d values, then the intercept.

Effects: fail.

### `linearSvr`

```gizlang
function linearSvr(x: Vec<f64>, n: i64, d: i64, y: Vec<f64>, cost: f64, epsilon: f64, tol: f64, maxIter: i64, rng: mut Random): Vec<f64>
```

The weights and bias (last) of a linear support vector regression of the N × D matrix X onto Y, by dual coordinate
descent with COST, EPSILON and TOL.

Effects: fail.

## Methods

### `Svm.decisions`

```gizlang
function Svm.decisions(self: Svm, row: Vec<f64>): Vec<f64>
```

The decision values of one row: one for regression or two classes, one for each pair otherwise.

Effects: fail.

### `Svm.scores`

```gizlang
function Svm.scores(self: Svm, row: Vec<f64>): Vec<f64>
```

Regression: the prediction. Two classes: minus the decision value (positive for class 1, as scikit-learn's
decision_function). More classes: the votes of each class, the lowest class winning a tie.

Effects: fail.

