metrics #
VSL Metrics Module
The vsl.metrics module provides evaluation metrics for machine learning models, including classification and regression metrics.
Features
Classification Metrics
- accuracy_score: Classification accuracy
- precision_score: Precision (positive predictive value)
- recall_score: Recall (sensitivity, true positive rate)
- f1_score: F1 score (harmonic mean of precision and recall)
- confusion_matrix: Confusion matrix for binary classification
- multiclass_confusion_matrix: Class-label-aware multiclass confusion matrix
- precision_score_multiclass, recall_score_multiclass, f1_score_multiclass: Macro, micro, and support-weighted scores
- roc_curve: Receiver Operating Characteristic curve
- roc_auc_score: Area Under the ROC Curve
- precision_recall_curve: Precision-Recall curve
- average_precision_score: Average precision score
- log_loss: Logistic loss (cross-entropy)
- gini_coefficient: Gini coefficient
- ks_statistic: Kolmogorov-Smirnov statistic
Regression Metrics
- mean_squared_error: Mean Squared Error (MSE)
- root_mean_squared_error: Root Mean Squared Error (RMSE)
- mean_absolute_error: Mean Absolute Error (MAE)
- r2_score: Coefficient of determination (R²)
- mean_absolute_percentage_error: MAPE
- explained_variance_score: Explained variance
- max_error: Maximum absolute error
- median_absolute_error: Median absolute error
- mean_squared_log_error: Mean squared logarithmic error
Quick Start
Classification Metrics
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]
y_pred := [1.0, 0.0, 0.0, 0.0, 1.0, 1.0]
// Basic metrics
acc := metrics.accuracy_score(y_true, y_pred)!
prec := metrics.precision_score(y_true, y_pred)!
rec := metrics.recall_score(y_true, y_pred)!
f1 := metrics.f1_score(y_true, y_pred)!
println('Accuracy: ${acc}')
println('Precision: ${prec}')
println('Recall: ${rec}')
println('F1 Score: ${f1}')
// Confusion matrix
cm := metrics.confusion_matrix(y_true, y_pred)!
println('Confusion Matrix: ${cm}')
// [[TN, FP], [FN, TP]]
ROC Curve and AUC
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0]
y_score := [0.9, 0.8, 0.3, 0.1] // prediction probabilities
// Compute ROC curve
roc := metrics.roc_curve(y_true, y_score)!
println('FPR: ${roc.fpr}')
println('TPR: ${roc.tpr}')
// Compute AUC
auc_score := metrics.roc_auc_score(y_true, y_score)!
println('AUC: ${auc_score}')
Precision-Recall Curve
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0]
y_score := [0.9, 0.8, 0.3, 0.1]
pr := metrics.precision_recall_curve(y_true, y_score)!
println('Precision: ${pr.precision}')
println('Recall: ${pr.recall}')
ap := metrics.average_precision_score(y_true, y_score)!
println('Average Precision: ${ap}')
Gini and KS Statistics
import vsl.metrics
y_true := [1.0, 1.0, 0.0, 0.0]
y_score := [0.9, 0.8, 0.2, 0.1]
gini := metrics.gini_coefficient(y_true, y_score)!
println('Gini: ${gini}')
ks := metrics.ks_statistic(y_true, y_score)!
println('KS Statistic: ${ks}')
Multiclass classification
Multiclass labels are integers. Confusion-matrix rows are true labels and columns are predicted labels; the result includes the sorted label list.
import vsl.metrics
y_true := [0, 0, 0, 1, 2, 2, 2, 2]
y_pred := [0, 1, 0, 1, 2, 2, 1, 2]
cm := metrics.multiclass_confusion_matrix(y_true, y_pred)!
macro_f1 := metrics.f1_score_multiclass(y_true, y_pred, .macro_avg)!
weighted_f1 := metrics.f1_score_multiclass(y_true, y_pred, .weighted_avg)!
println('labels: ${cm.labels}, rows: ${cm.values}')
println('macro F1: ${macro_f1}; weighted F1: ${weighted_f1}')
Regression Metrics
import vsl.metrics
y_true := [3.0, -0.5, 2.0, 7.0]
y_pred := [2.5, 0.0, 2.0, 8.0]
mse := metrics.mean_squared_error(y_true, y_pred)!
rmse := metrics.root_mean_squared_error(y_true, y_pred)!
mae := metrics.mean_absolute_error(y_true, y_pred)!
r2 := metrics.r2_score(y_true, y_pred)!
println('MSE: ${mse}')
println('RMSE: ${rmse}')
println('MAE: ${mae}')
println('R²: ${r2}')
API Reference
Classification Functions
| Function | Description |
|---|---|
confusion_matrix(y_true, y_pred) |
Returns [[TN, FP], [FN, TP]] |
multiclass_confusion_matrix(y_true, y_pred) |
Sorted integer labels and a true-row/predicted-column matrix |
accuracy_score(y_true, y_pred) |
(TP + TN) / total |
precision_score(y_true, y_pred) |
TP / (TP + FP) |
recall_score(y_true, y_pred) |
TP / (TP + FN) |
f1_score(y_true, y_pred) |
2 * P * R / (P + R) |
precision_score_multiclass(y_true, y_pred, average) |
Macro, micro, or support-weighted precision |
recall_score_multiclass(y_true, y_pred, average) |
Macro, micro, or support-weighted recall |
f1_score_multiclass(y_true, y_pred, average) |
Macro, micro, or support-weighted F1 |
roc_curve(y_true, y_score) |
Returns FPR, TPR, thresholds |
roc_auc_score(y_true, y_score) |
Area under ROC curve |
precision_recall_curve(y_true, y_score) |
Returns precision, recall, thresholds |
average_precision_score(y_true, y_score) |
AP from predictions |
log_loss(y_true, y_pred) |
Cross-entropy loss |
gini_coefficient(y_true, y_score) |
2 * AUC - 1 |
ks_statistic(y_true, y_score) |
Max difference between CDFs |
auc(x, y) |
Area under curve (trapezoidal) |
Regression Functions
| Function | Description |
|---|---|
mean_squared_error(y_true, y_pred) |
Σ(y - ŷ)² / n |
root_mean_squared_error(y_true, y_pred) |
√MSE |
mean_absolute_error(y_true, y_pred) |
Σ|y - ŷ| / n |
r2_score(y_true, y_pred) |
1 - SS_res / SS_tot |
mean_absolute_percentage_error(y_true, y_pred) |
MAPE in % |
explained_variance_score(y_true, y_pred) |
1 - Var(residuals) / Var(y) |
max_error(y_true, y_pred) |
max(|y - ŷ|) |
median_absolute_error(y_true, y_pred) |
median(|y - ŷ|) |
mean_squared_log_error(y_true, y_pred) |
MSLE (for non-negative) |
Integration with ML Models
import vsl.metrics
import vsl.ml
// Train a model
mut model := ml.LogReg.new(mut data, 'my_model')
model.train(ml.LogRegTrainConfig{})
// Make predictions
mut predictions := []f64{}
for i in 0 .. test_data.nb_samples {
x := test_data.x.get_row(i)
predictions << model.predict(x)
}
// Evaluate
accuracy := metrics.accuracy_score(test_data.y, predictions)!
auc_score := metrics.roc_auc_score(test_data.y, predictions)!
println('Test Accuracy: ${accuracy}')
println('Test AUC: ${auc_score}')
Visualization with Metrics
import vsl.metrics
import vsl.plot
// Compute ROC curve
roc := metrics.roc_curve(y_true, y_score)!
auc_val := metrics.roc_auc_score(y_true, y_score)!
// Plot ROC curve
mut plt := plot.Plot.new()
plt.scatter(
x: roc.fpr
y: roc.tpr
mode: 'lines'
name: 'ROC (AUC = ${auc_val:.3f})'
)
plt.scatter(
x: [0.0, 1.0]
y: [0.0, 1.0]
mode: 'lines'
name: 'Random'
)
plt.layout(
title: 'ROC Curve'
xaxis: plot.Axis{
title: plot.AxisTitle{
text: 'False Positive Rate'
}
}
yaxis: plot.Axis{
title: plot.AxisTitle{
text: 'True Positive Rate'
}
}
)
plt.show()!
Calibration
SigmoidCalibrator fits a Platt-style logistic map from decision scores to probabilities. IsotonicCalibrator fits a non-decreasing map with the pool-adjacent-violators algorithm; predictions are interpolated between pooled knots and clamped outside the training score range. Both expose fit and predict and require binary labels with matching finite input lengths.
import vsl.metrics
mut calibrator := metrics.IsotonicCalibrator{}
calibrator.fit([f64(0.0), 1.0, 0.0, 1.0], [f64(0.1), 0.9, 0.3, 0.6])!
probabilities := calibrator.predict([f64(0.2), 0.8])!
println(probabilities)
cross_calibrate(method, y_true, y_score, folds) fits on the complement of each caller-supplied fold and predicts the held-out samples, so validation labels cannot leak into fitting. CalibrationMethod.sigmoid and .isotonic are supported. Folds must be non-empty and partition the samples exactly once; otherwise an error is returned.
Notes
- For binary classification, labels should be 0.0 or 1.0 (or values that threshold at 0.5)
y_scoreparameters expect probability scores, not class labels- R² can be negative for predictions worse than the mean
- MAPE and MSLE have specific requirements for input values
See Also
fn accuracy_score #
fn accuracy_score(y_true []f64, y_pred []f64) !f64
accuracy_score computes the accuracy classification score
fn auc #
fn auc(x []f64, y []f64) !f64
auc computes Area Under the Curve using the trapezoidal rule
fn average_precision_score #
fn average_precision_score(y_true []f64, y_score []f64) !f64
average_precision_score computes average precision from prediction scores
fn brier_score #
fn brier_score(y_true []f64, y_prob []f64) !f64
brier_score computes the mean squared probability error for binary labels. Labels must be exactly 0 or 1 and probabilities must be finite in [0, 1].
fn calibration_curve #
fn calibration_curve(y_true []f64, y_prob []f64, n_bins int) !CalibrationCurveResult
calibration_curve computes reliability-diagram points for binary probabilities using uniform bins over [0, 1].
fn confusion_matrix #
fn confusion_matrix(y_true []f64, y_pred []f64) ![][]int
confusion_matrix computes the confusion matrix for binary classification. Returns a 2x2 matrix: [[TN, FP], [FN, TP]]
fn cross_calibrate #
fn cross_calibrate(method CalibrationMethod, y_true []f64, y_score []f64, folds [][]int) ![]f64
cross_calibrate fits a calibrator on all samples outside each fold and predicts the held-out samples. Folds are supplied by the caller, so no validation label can be used during fitting. Each sample must appear in exactly one fold.
fn expected_calibration_error #
fn expected_calibration_error(y_true []f64, y_prob []f64, n_bins int) !f64
expected_calibration_error computes the count-weighted absolute gap between predicted probability and observed positive rate across uniform bins.
fn explained_variance_score #
fn explained_variance_score(y_true []f64, y_pred []f64) !f64
explained_variance_score computes the explained variance regression score
fn f1_score #
fn f1_score(y_true []f64, y_pred []f64) !f64
f1_score computes the F1 score: 2 * (precision * recall) / (precision + recall)
fn f1_score_multiclass #
fn f1_score_multiclass(y_true []int, y_pred []int, average ClassificationAverage) !f64
f1_score_multiclass computes multiclass F1 with macro, micro, or support-weighted averaging.
fn gini_coefficient #
fn gini_coefficient(y_true []f64, y_score []f64) !f64
gini_coefficient computes the Gini coefficient from predictions
fn ks_statistic #
fn ks_statistic(y_true []f64, y_score []f64) !f64
ks_statistic computes the Kolmogorov-Smirnov statistic
fn log_loss #
fn log_loss(y_true []f64, y_pred []f64) !f64
log_loss computes the logistic loss (cross-entropy loss)
fn max_error #
fn max_error(y_true []f64, y_pred []f64) !f64
max_error computes the maximum absolute error
fn mean_absolute_error #
fn mean_absolute_error(y_true []f64, y_pred []f64) !f64
mean_absolute_error computes the mean absolute error (MAE)
fn mean_absolute_percentage_error #
fn mean_absolute_percentage_error(y_true []f64, y_pred []f64) !f64
mean_absolute_percentage_error computes MAPE MAPE = (1/n) * Σ|y_true - y_pred| / |y_true| * 100
fn mean_squared_error #
fn mean_squared_error(y_true []f64, y_pred []f64) !f64
mean_squared_error computes the mean squared error (MSE)
fn mean_squared_log_error #
fn mean_squared_log_error(y_true []f64, y_pred []f64) !f64
mean_squared_log_error computes the mean squared logarithmic error Only valid for non-negative values
fn median_absolute_error #
fn median_absolute_error(y_true []f64, y_pred []f64) !f64
median_absolute_error computes the median absolute error
fn multiclass_confusion_matrix #
fn multiclass_confusion_matrix(y_true []int, y_pred []int) !MulticlassConfusionMatrix
multiclass_confusion_matrix builds a confusion matrix for arbitrary integer class labels. Labels are the sorted union of true and predicted classes.
fn optimal_threshold #
fn optimal_threshold(y_true []f64, y_score []f64) !f64
optimal_threshold returns the score threshold maximizing Youden's J statistic (true-positive rate minus false-positive rate).
fn precision_recall_curve #
fn precision_recall_curve(y_true []f64, y_score []f64) !PrecisionRecallResult
precision_recall_curve computes precision-recall pairs for different thresholds
fn precision_score #
fn precision_score(y_true []f64, y_pred []f64) !f64
precision_score computes the precision: tp / (tp + fp)
fn precision_score_multiclass #
fn precision_score_multiclass(y_true []int, y_pred []int, average ClassificationAverage) !f64
precision_score_multiclass computes multiclass precision with macro, micro, or support-weighted averaging.
fn r2_score #
fn r2_score(y_true []f64, y_pred []f64) !f64
r2_score computes the coefficient of determination (R² score) R² = 1 - SS_res / SS_tot where SS_res = Σ(y_true - y_pred)² and SS_tot = Σ(y_true - mean(y_true))²
fn recall_score #
fn recall_score(y_true []f64, y_pred []f64) !f64
recall_score computes the recall: tp / (tp + fn)
fn recall_score_multiclass #
fn recall_score_multiclass(y_true []int, y_pred []int, average ClassificationAverage) !f64
recall_score_multiclass computes multiclass recall with macro, micro, or support-weighted averaging.
fn roc_auc_score #
fn roc_auc_score(y_true []f64, y_score []f64) !f64
roc_auc_score computes Area Under the ROC Curve using the trapezoidal rule
fn roc_curve #
fn roc_curve(y_true []f64, y_score []f64) !RocCurveResult
roc_curve computes Receiver Operating Characteristic (ROC) curve
fn root_mean_squared_error #
fn root_mean_squared_error(y_true []f64, y_pred []f64) !f64
root_mean_squared_error computes the root mean squared error (RMSE)
fn CalibrationMethod.from #
fn CalibrationMethod.from[W](input W) !CalibrationMethod
from returns the CalibrationMethod value of an integer or of a field name, or an error if there is none.
fn ClassificationAverage.from #
fn ClassificationAverage.from[W](input W) !ClassificationAverage
from returns the ClassificationAverage value of an integer or of a field name, or an error if there is none.
enum CalibrationMethod #
enum CalibrationMethod {
sigmoid
isotonic
}
CalibrationMethod selects the model used to map decision scores to probabilities in cross_calibrate.
enum ClassificationAverage #
enum ClassificationAverage {
macro_avg
micro_avg
weighted_avg
}
ClassificationAverage chooses an aggregation for multiclass metrics.
struct CalibrationCurveResult #
struct CalibrationCurveResult {
pub:
mean_predicted_probability []f64
fraction_positive []f64
counts []int
}
CalibrationCurveResult contains observed positive rates and mean predicted probabilities for non-empty uniform probability bins.
struct IsotonicCalibrator #
struct IsotonicCalibrator {
pub mut:
knots []f64
values []f64
mut:
fitted bool
}
IsotonicCalibrator fits a non-decreasing step mapping from decision scores to probabilities with the pool-adjacent-violators algorithm. Predictions are linearly interpolated between the fitted knots and clamped outside the training score range.
fn (IsotonicCalibrator) fit #
fn (mut calibrator IsotonicCalibrator) fit(y_true []f64, y_score []f64) !
fit estimates the isotonic regression of the binary labels on the scores.
fn (IsotonicCalibrator) predict #
fn (calibrator &IsotonicCalibrator) predict(y_score []f64) ![]f64
predict returns calibrated probabilities for decision scores.
struct MulticlassConfusionMatrix #
struct MulticlassConfusionMatrix {
pub:
labels []int
values [][]int
}
MulticlassConfusionMatrix stores sorted class labels and counts, where rows are true labels and columns are predicted labels.
struct PrecisionRecallResult #
struct PrecisionRecallResult {
pub:
precision []f64 // precision values
recall []f64 // recall values
thresholds []f64 // thresholds used
}
PrecisionRecallResult holds the result of precision-recall curve computation
struct RocCurveResult #
struct RocCurveResult {
pub:
fpr []f64 // false positive rates
tpr []f64 // true positive rates
thresholds []f64 // thresholds used
}
RocCurveResult holds the result of ROC curve computation
struct SigmoidCalibrator #
struct SigmoidCalibrator {
pub mut:
coef_ f64
intercept_ f64
mut:
fitted bool
}
SigmoidCalibrator maps decision scores to probabilities using a fitted logistic (Platt-style) calibration curve.
fn (SigmoidCalibrator) fit #
fn (mut calibrator SigmoidCalibrator) fit(y_true []f64, y_score []f64) !
fit estimates the sigmoid parameters from binary labels and decision scores.
fn (SigmoidCalibrator) predict #
fn (calibrator &SigmoidCalibrator) predict(y_score []f64) ![]f64
predict returns calibrated probabilities for decision scores.
- README
- fn accuracy_score
- fn auc
- fn average_precision_score
- fn brier_score
- fn calibration_curve
- fn confusion_matrix
- fn cross_calibrate
- fn expected_calibration_error
- fn explained_variance_score
- fn f1_score
- fn f1_score_multiclass
- fn gini_coefficient
- fn ks_statistic
- fn log_loss
- fn max_error
- fn mean_absolute_error
- fn mean_absolute_percentage_error
- fn mean_squared_error
- fn mean_squared_log_error
- fn median_absolute_error
- fn multiclass_confusion_matrix
- fn optimal_threshold
- fn precision_recall_curve
- fn precision_score
- fn precision_score_multiclass
- fn r2_score
- fn recall_score
- fn recall_score_multiclass
- fn roc_auc_score
- fn roc_curve
- fn root_mean_squared_error
- fn CalibrationMethod.from
- fn ClassificationAverage.from
- enum CalibrationMethod
- enum ClassificationAverage
- struct CalibrationCurveResult
- struct IsotonicCalibrator
- struct MulticlassConfusionMatrix
- struct PrecisionRecallResult
- struct RocCurveResult
- struct SigmoidCalibrator