Oihana PHP

variance.php

Table of Contents

Functions

variance()  : float
Computes the variance of a list of numbers.

Functions

variance()

Computes the variance of a list of numbers.

variance(array<int, int|float> $values[, bool $sample = false ]) : float

By default the population variance is returned (sum of squared deviations divided by N). When $sample is true, the sample variance is returned instead (divided by N - 1, Bessel's correction).

Parameters
$values : array<int, int|float>

A list of numeric values (at least one for population, two for sample).

$sample : bool = false

Whether to compute the sample variance (N - 1) instead of the population one (N).

Tags
throws
InvalidArgumentException

If the array is empty, or has fewer than two values when $sample is true.

example
use function oihana\core\maths\variance;

echo variance( [ 2 , 4 , 4 , 4 , 5 , 5 , 7 , 9 ] )        ; // 4.0  (population)
echo variance( [ 2 , 4 , 4 , 4 , 5 , 5 , 7 , 9 ] , true ) ; // ~4.571 (sample)
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
float

The variance of the values.

On this page

Search results