纯 MoonBit 加权保序回归、稳健分位数拟合与可解释单调校准模型
moon add kekeshuo/moonisotonic@0.1.0import {
"kekeshuo/moonisotonic" @iso,
}let fit = @iso.fit_l2([3.0, 1.0, 2.0], [1.0, 1.0, 1.0]).unwrap()
// fit.values == [2.0, 2.0, 2.0]; 块采用 [start, end) 下标。
let model = @iso.train_l2([
@iso.sample(0.0, 0.0),
@iso.sample(2.0, 4.0),
]).unwrap()
let prediction = @iso.predict(model, 1.0).unwrap() // 2.0
let saved = @iso.encode_model(model).unwrap()
let restored = @iso.decode_model(saved).unwrap()| 工作流 | 主要入口 | 结果/约定 |
|---|---|---|
| 有序数组拟合 | isotonic, fit_l2, fit_l1, fit_quantile | 权重严格为正,双方向,块与目标值 |
| 带约束拟合 | fit_bounded, fit_range, check_bounds | 逐点有限闭区间,拒绝不可行链 |
| 训练经验曲线 | prepare, train_l2, train_l1, train_quantile | 稳定按坐标排序,重复坐标共享预测 |
| 查询/移植 | predict, predict_many, inverse, threshold_domain, JSON codec | 三种插值、三种边界策略、平台逆区间 |
| 解释/导出 | audit_order, audit_projection, explain_fit, TSV | 原行恢复、违反定位、残差与块解释 |
| 下游评估 | evaluate, reliability, holdout, cross_validate | 加权误差、Brier/ECE、显式分区 |
| 描述不确定性 | train_band, band_coverage, resample_sensitivity | 不交叉经验分位数包络、调用者提供重采样计划 |
moon info && moon fmt
moon fmt --check
moon check --deny-warn --target wasm-gc
moon test --deny-warn --target wasm-gc
moon build --deny-warn --target wasm-gc
moon run examples/probability --deny-warn --target wasm-gc
# 将目标替换为 wasm、js、native 重复;native 需要 C 编译器。pub(all) struct BandCoverage {
count : Int
weight : Double
covered_weight : Double
coverage : Double
mean_width : Double
} derive(Debug)#deprecated("`BandCoverage::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn BandCoverage::to_repr(BandCoverage) -> Reprpub(all) struct BlockExplanation {
start : Int
end : Int
fitted : Double
observed_mean : Double
observed_min : Double
observed_max : Double
weight : Double
squared_error : Double
absolute_error : Double
} derive(Debug)#deprecated("`BlockExplanation::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn BlockExplanation::to_repr(BlockExplanation) -> Reprpub(all) struct CalibrationComparison {
before : Reliability
after : Reliability
brier_change : Double
ece_change : Double
} derive(Debug)#deprecated("`CalibrationComparison::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn CalibrationComparison::to_repr(CalibrationComparison) -> Reprpub(all) struct CrossValidation {
predictions : Array[Double]
aggregate : Evaluation
folds : Array[Evaluation]
} derive(Debug)#deprecated("`CrossValidation::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn CrossValidation::to_repr(CrossValidation) -> Reprpub(all) struct Evaluation {
count : Int
weight : Double
mse : Double
mae : Double
bias : Double
maximum_error : Double
loss : Double
} derive(Debug)#deprecated("`Evaluation::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn Evaluation::to_repr(Evaluation) -> Reprpub(all) struct Holdout {
model : Model
training : Evaluation
validation : Evaluation
predictions : Array[Double]
validation_indices : Array[Int]
} derive(Debug)#deprecated("`Interpolation::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn Interpolation::to_repr(Interpolation) -> Repr#deprecated("`InverseRange::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn InverseRange::to_repr(InverseRange) -> Repr#deprecated("`OrderAudit::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn OrderAudit::to_repr(OrderAudit) -> Reprpub(all) struct ProjectionAudit {
residual_sum : Double
min_prefix : Double
max_prefix : Double
max_block_balance : Double
order : OrderAudit
} derive(Debug)#deprecated("`ProjectionAudit::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ProjectionAudit::to_repr(ProjectionAudit) -> Repr#deprecated("`QuantileBand::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn QuantileBand::to_repr(QuantileBand) -> Reprpub(all) struct Reliability {
bins : Array[ReliabilityBin]
brier : Double
ece : Double
maximum_gap : Double
} derive(Debug)#deprecated("`Reliability::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn Reliability::to_repr(Reliability) -> Reprpub(all) struct ReliabilityBin {
index : Int
count : Int
weight : Double
mean_prediction : Double
event_rate : Double
gap : Double
} derive(Debug)#deprecated("`ReliabilityBin::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ReliabilityBin::to_repr(ReliabilityBin) -> Repr#deprecated("`Sensitivity::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn Sensitivity::to_repr(Sensitivity) -> Reprfn audit_order(values : Array[Double], direction? : Direction, tolerance? : Double) -> Result[OrderAudit, String]fn audit_projection(y : Array[Double], weights : Array[Double], fit : Fit) -> Result[ProjectionAudit, String]fn compare_calibration(samples : Array[Sample], result : Holdout, bin_count? : Int) -> Result[CalibrationComparison, String]fn evaluate_predictions(y : Array[Double], predictions : Array[Double], weights : Array[Double], loss? : Loss) -> Result[Evaluation, String]fn explain_fit(y : Array[Double], weights : Array[Double], fit : Fit) -> Result[Array[BlockExplanation], String]fn predict(model : Model, x : Double, interpolation? : Interpolation, boundary? : Boundary) -> Result[Double, String]fn predict_band(band : QuantileBand, queries : Array[Double]) -> Result[Array[InverseRange], String]fn predict_many(model : Model, queries : Array[Double], interpolation? : Interpolation, boundary? : Boundary) -> Result[Array[Double], String]fn reliability(labels : Array[Double], predictions : Array[Double], weights : Array[Double], bin_count? : Int) -> Result[Reliability, String]fn train_band(samples : Array[Sample], lower_tau : Double, upper_tau : Double, direction? : Direction) -> Result[QuantileBand, String]Install
Download zip纯 MoonBit 加权保序回归、稳健分位数拟合与可解释单调校准模型