regression
js/ml/metrics/regression.ts
Regression error and goodness-of-fit metrics over continuous targets.
Functions
function meanSquaredError(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Mean of the squared residuals.
Squaring makes large misses dominate, so this is the metric to optimize when one big error is worse than several small ones.
import { meanSquaredError } from 'fino:ml/metrics';
console.log(meanSquaredError([3, -0.5, 2, 7], [2.5, 0, 2, 8])); // 0.375function rootMeanSquaredError(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Square root of the mean squared error, back in the target's own units.
import { rootMeanSquaredError } from 'fino:ml/metrics';
console.log(rootMeanSquaredError([3, -0.5, 2, 7], [2.5, 0, 2, 8]).toFixed(4)); // 0.6124function meanAbsoluteError(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Mean of the absolute residuals.
Every error counts in proportion to its size, which makes this far less
sensitive to outliers than meanSquaredError.
import { meanAbsoluteError } from 'fino:ml/metrics';
console.log(meanAbsoluteError([3, -0.5, 2, 7], [2.5, 0, 2, 8])); // 0.5function medianAbsoluteError(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Median of the absolute residuals — the outlier-proof error summary.
import { medianAbsoluteError } from 'fino:ml/metrics';
console.log(medianAbsoluteError([3, -0.5, 2, 7], [2.5, 0, 2, 8])); // 0.5function maxError(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Largest absolute residual — the worst case rather than the typical one.
import { maxError } from 'fino:ml/metrics';
console.log(maxError([3, 2, 7], [3, 2, 8])); // 1function meanAbsolutePercentageError(
yTrue: ArrayLike<number>,
yPred: ArrayLike<number>,
): number
Mean absolute error as a fraction of the true value.
Scale-free, so it compares targets of different magnitudes — but it is undefined at zero and biased against under-prediction, so it throws rather than silently dividing by a zero target.
import { meanAbsolutePercentageError } from 'fino:ml/metrics';
console.log(meanAbsolutePercentageError([100, 200], [110, 180])); // 0.1function meanSquaredLogError(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Mean squared error between log1p of the targets and predictions.
Penalizes relative error, so it suits targets spanning orders of magnitude.
Both inputs must be at least -1.
import { meanSquaredLogError } from 'fino:ml/metrics';
console.log(meanSquaredLogError([3, 5, 2.5, 7], [2.5, 5, 4, 8]).toFixed(4)); // 0.0397function r2Score(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Coefficient of determination: the share of variance the model explains.
1 is exact, 0 matches always predicting the mean, and negative values
mean the model does worse than that baseline. Reports 0 when the targets
have no variance and the statistic is undefined.
import { r2Score } from 'fino:ml/metrics';
console.log(r2Score([3, -0.5, 2, 7], [2.5, 0, 2, 8]).toFixed(4)); // 0.9486function explainedVariance(yTrue: ArrayLike<number>, yPred: ArrayLike<number>): number
Share of variance explained, ignoring any constant offset in the residuals.
Differs from r2Score only when predictions are systematically biased: a
gap between the two is the signal that the model is off by a constant.
import { explainedVariance } from 'fino:ml/metrics';
console.log(explainedVariance([3, -0.5, 2, 7], [2.5, 0, 2, 8]).toFixed(4)); // 0.9572function pearsonCorrelation(a: ArrayLike<number>, b: ArrayLike<number>): number
Pearson correlation between two numeric vectors, in [-1, 1].
Reports 0 when either input is constant and the statistic is undefined.
import { pearsonCorrelation } from 'fino:ml/metrics';
console.log(pearsonCorrelation([1, 2, 3], [2, 4, 6])); // 1