### Function Reference: `var`

: v = var (x)
: v = var (x, w)
: v = var (x, w, dim)
: v = var (x, w, vecdim)
: v = var (x, w, `"all"`)
: v = var (…, nanflag)
: [v, m] = var (…)

Compute the variance of the elements of the vector x.

The variance is defined as $$ {\rm var}(x) = {1\over N-1} \sum_{i=1}^N (x_i - \bar x )^2 $$

If x is an array, compute the variance along the first non-singleton dimensions of x.

The optional argument w determines the weighting scheme to use. Valid values are:

0 [default]:

Normalize with N-1 (population variance). This provides the square root of the best unbiased estimator of the variance.

1:

Normalize with N (sample variance). This provides the square root of the second moment around the mean.

a vector:

Compute the weighted variance with non-negative weights. The length of w must equal the size of x in the operating dimension. NaN values are permitted in w, will be multiplied with the associated values in x, and can be excluded by the nanflag option.

an array:

Similar to vector weights, but w must be the same size as x. If the operating dimension is supplied as vecdim or `"all"` and w is not a scalar, w must be an same-sized array.

Note: w must always be specified before specifying any of the following dimension options. To use the default value for w you may pass an empty input argument [].

The optional variable dim forces `var` to operate over the specified dimension, which must be a positive integer-valued number. Specifying any singleton dimension in x, including any dimension exceeding `ndims (x)`, will result in a variance of 0.

Specifying the dimensions as vecdim, a vector of non-repeating dimensions, will return the variance calculated over the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option `"all"`. Any dimension in vecdim greater than `ndims (x)` is ignored.

Specifying the dimension as `"all"` will force `var` to operate on all elements of x, and is equivalent to `var (x(:))`.

The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is `"includenan"` which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to `"omitnan"`. The output will still contain NaN values if x consists of all NaN values in the operating dimension.

The optional second output variable m contains the mean of the elements of x used to calculate the variance. If v is the weighted variance, then m is also the weighted mean.

See also: std, mean, cov, skewness, kurtosis, moment

Source Code: var