Invert GMLscripts.com

ds_list_variance

Wikipedia:

In probability theory and statistics, variance measures how far a set of numbers is spread out. A variance of zero indicates that all the values are identical. Variance is always non-negative: a small variance indicates that the data points tend to be very close to the mean (expected value) and hence to each other, while a high variance indicates that the data points are very spread out around the mean and from each other.

Population Variance

In general, the population variance of a finite population of size \(N\) with values \(x_i\) is given by

\( \sigma^2 = \frac 1N \sum_{i=1}^N \left(x_i - \mu \right)^2 \)

where

\( \mu = \frac 1N \sum_{i=1}^N x_i \)

is the population mean.

ds_list_variance(list, sample)
Returns the variance of the values in a given list.
COPY/// @func   ds_list_variance(list, sample)
///
/// @desc   Returns the variance of the values in a given list.
///         Computes for a sample or entire population (default).
///
/// @param  {list}      list        list data structure
/// @param  {bool}      sample      true for sample, false for population
///
/// @return {real}      variance of values
///
/// GMLscripts.com/license

function ds_list_variance(list, sample=false)
{
    var n = ds_list_size(list);
    if (n == 0) return undefined;

    var avg = 0;
    var sum = 0;

    for (var i=0; i<n; i++) avg += ds_list_find_value(list, i);
    avg /= n;

    for (var i=0; i<n; i++) sum += sqr(ds_list_find_value(list, i) - avg);

    return sum / (n - real(sample));
}

Contributors: Quimp

GitHub: View · Commits · Blame · Raw