Standard library · Numbers and collections

stats

Descriptive statistics over a list of numbers: mean, median, variance, standard deviation and percentiles.

import stats
On this page
  1. Functions

The stats package works on a Vector<f64> and never changes it. Variance and standard deviation come in two forms: the ordinary one for when the data is a sample of a larger population, dividing by n−1, and the "population" one for when you have the whole population, dividing by n.

Functions

Sum

func Sum(v: Vector<f64>): f64
The sum of all the numbers.
stats-Mean.salam
import stats

func main:
    mut scores := Vector {} as Vector<f64>
    defer scores.free()
    each s in [12.0, 18.0, 15.0, 19.0, 11.0]:
        scores.push(s)
    end
    println stats.Sum(scores), stats.Mean(scores), stats.Median(scores)
    println stats.Min(scores), stats.Max(scores)
    println stats.Variance(scores), stats.PopVariance(scores)
    println stats.Stdev(scores), stats.PopStdev(scores)
    println stats.Geomean(scores), stats.Percentile(scores, 90.0)
end
Output75 15 15 11 19 12.5 10 3.5355339059327378 3.1622776601683795 14.660144778355681 18.6

Mean

func Mean(v: Vector<f64>): f64
The arithmetic mean.

The example for this item is shown with the ones above: stats-Mean.salam

Min

func Min(v: Vector<f64>): f64
The smallest number.

The example for this item is shown with the ones above: stats-Mean.salam

Max

func Max(v: Vector<f64>): f64
The largest number.

The example for this item is shown with the ones above: stats-Mean.salam

Median

func Median(v: Vector<f64>): f64
The middle number after sorting, or the mean of the middle two when there is an even count. It stands up to outliers better than the mean.

The example for this item is shown with the ones above: stats-Mean.salam

Variance

func Variance(v: Vector<f64>): f64
The sample variance, dividing by n−1.

The example for this item is shown with the ones above: stats-Mean.salam

PopVariance

func PopVariance(v: Vector<f64>): f64
The population variance, dividing by n.

The example for this item is shown with the ones above: stats-Mean.salam

Stdev

func Stdev(v: Vector<f64>): f64
The sample standard deviation, the square root of the sample variance.

The example for this item is shown with the ones above: stats-Mean.salam

PopStdev

func PopStdev(v: Vector<f64>): f64
The population standard deviation.

The example for this item is shown with the ones above: stats-Mean.salam

Geomean

func Geomean(v: Vector<f64>): f64
The geometric mean, which suits growth rates and ratios better than the arithmetic mean. Every number has to be positive.

The example for this item is shown with the ones above: stats-Mean.salam

Percentile

func Percentile(v: Vector<f64>, p: f64): f64
The pth percentile, with p between zero and a hundred; the 90th percentile, for instance, is the value 90 per cent of the data falls below.

The example for this item is shown with the ones above: stats-Mean.salam