Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.


This module contains base statistical algorithms.
Note that used specialized summing algorithms execute more primitive operations than vanilla summation. Therefore, if in certain cases maximum speed is required at expense of precision, one can use .
Ilya Yaroshenko
template mean(Summation summation = Summation.appropriate)

template mean(string summation)
Computes the average of r, which must be a finite iterable.
The average of all the elements in the range r.
assert(mean([1.0, 2, 3]) == 2);
assert(mean([1.0 + 3i, 2, 3]) == 2 + 1i);
@safe sumType!Range mean(Range)(Range r)
if (hasLength!Range || summation == Summation.appropriate || summation == || summation == Summation.naive);
template simpleLinearRegression(Summation summation = Summation.kbn)

template simpleLinearRegression(string summation)
A linear regression model with a single explanatory variable.
import mir.math.common: approxEqual;
static immutable x = [0, 1, 2, 3];
static immutable y = [-1, 0.2, 0.9, 2.1];
auto params = x.simpleLinearRegression(y);
assert(params[0].approxEqual(-0.95)); // shift
assert(params[1].approxEqual(1)); // slope
@safe sumType!YRange[2] simpleLinearRegression(XRange, YRange)(XRange x, YRange y)
if (isInputRange!XRange && isInputRange!YRange && !(isArray!XRange && isArray!YRange) && isFloatingPoint!(sumType!YRange));

@safe sumType!(Y[])[2] simpleLinearRegression(X, Y)(scope const X[] x, scope const Y[] y);
XRange x x[i] points
YRange y f(x[i]) values
The pair of shift and slope of the linear curve.