Sidechair

array.variance()

Browse the reference

Returns how widely the numbers in an array spread around their mean, as the average squared distance.

Try it in the editor

By default it divides by the number of items, which treats the array as the whole population. Pass biased = false to divide by one less, the sample estimate.

na items are skipped. With a single number the sample form has nothing to divide by and returns na.

Syntax

Pine Script
array.variance(id: array<int/float>, biased: series bool (optional)) -> series float
array.variance(id: array<int>, biased: series bool (optional)) -> series int
array.variance(biased: series bool (optional)) -> series float
array.variance(biased: series bool (optional)) -> series int

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to measure the spread of. na items are skipped.
biasedseries booltruetrue divides by the count (population); false divides by the count minus one (sample).

Returns

series float or series int: The variance of the numbers.

Remarks

Example

Pine ScriptPopulation and sample varianceOpen in editor
//@version=6
indicator("array.variance: both forms", overlay = false)
var win = array.new_float()
array.push(win, close)
if array.size(win) > 10
    array.shift(win)
plot(array.variance(win), "Population")
plot(array.variance(win, false), "Sample", color.orange)

See also

Runs on Sidechair