# MoonBit Kernel SVM

`Yuxiao-Bot/kernel-svm` is a deterministic support-vector classification toolkit implemented in MoonBit. It covers validated dense datasets, binary and multiclass training, model evaluation, diagnostics, explanations, and stable reports without requiring a runtime service.

## Installation

```bash
moon add Yuxiao-Bot/kernel-svm@0.1.0
```

Import the package in `moon.pkg`:

```moonbit
import {
  "Yuxiao-Bot/kernel-svm" @svm,
}
```

## Minimal Example

```moonbit
fn main {
  let data = @svm.dataset(
    [[-2.0], [-1.0], [1.0], [2.0]],
    [0, 0, 1, 1],
    "minimal classification",
  ).unwrap()
  let config = @svm.default_binary_config(@svm.Linear).unwrap()
  let model = @svm.train_binary(data, config).unwrap()
  println("prediction=\{model.predict([1.5]).unwrap()}")
}
```

Run the checked repository examples:

```bash
moon run examples/binary
moon run examples/kernels
moon run examples/multiclass
moon run examples/validation
moon run examples/search
```

## Implemented Scope

- Validated, immutable dense datasets with finite numeric features and integer labels.
- Linear, radial-basis-function, and polynomial kernels with checked parameters.
- Deterministic binary C-SVC training using sequential minimal optimization.
- Positive sample weights, class weights, and per-observation box constraints.
- One-vs-rest multiclass training with sorted classes and stable score ties.
- Standard and min-max scaling with inverse transforms.
- Confusion matrices; class, macro, micro, weighted, and balanced metrics.
- Deterministic stratified folds, fold-local preprocessing, cross-validation, repeated validation, and ordered configuration search.
- ROC, precision-recall, AUC, average precision, support-vector contributions, permutation importance, and row-level prediction audits.
- Kernel, margin, support-vector, convergence, dataset, and weight diagnostics.
- Stable text and JSON reports with explicit field order and JSON string escaping.

All fallible public operations return `Result[..., SvmError]`. Public array accessors return copies. Training and selection use deterministic iteration and tie rules; they do not use random state.

## Input Contract

Feature matrices must be nonempty, rectangular, finite, and contain at least one column. Labels must match the row count. Binary training requires exactly two distinct labels. Multiclass training requires at least two labels. Kernel/configuration parameters and all effective weights must be finite and valid. Stratified validation rejects folds that cannot contain each class.

## Verification

From the repository root:

```bash
moon fmt --check
moon info
moon check --deny-warn
moon build
moon test
powershell -ExecutionPolicy Bypass -File scripts/source-audit.ps1
```

The source audit checks production-code scale, automated tests, five executable examples, required documentation, and placeholder markers. GitHub Actions runs the same build, test, example, and audit path.

## Ecosystem Context

The MoonBit ecosystem already includes a compact linear binary SVM implementation in [`Juwan-Hwang/moon-certified`](https://github.com/Juwan-Hwang/moon-certified/tree/main/ml/svm). This repository is an independent implementation and does not copy that source. Its separate contribution is a standalone Mooncakes package with nonlinear kernels, weighted deterministic training, multiclass composition, preprocessing, validation/search, diagnostics, explanations, reports, CI, and public examples.

Algorithm and license references are recorded in [docs/references.md](docs/references.md).

## Version 0.1.0 Boundaries

Version 0.1.0 is a dense numeric classification toolkit. It does not claim regression, calibrated probabilities, sparse matrices, missing-value imputation, categorical encoding, model deserialization, parallel/GPU training, formal verification, or performance guarantees. These are possible later directions and are not part of the current release or application claims.

## License

Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
