Skip to content

Latest commit

 

History

History
178 lines (112 loc) · 6.3 KB

File metadata and controls

178 lines (112 loc) · 6.3 KB

svariance

Calculate the variance of a one-dimensional single-precision floating-point ndarray.

The population variance of a finite size population of size N is given by

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

where the population mean is given by

$$\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i$$

Often in the analysis of data, the true population variance is not known a priori and must be estimated from a sample drawn from the population distribution. If one attempts to use the formula for the population variance, the result is biased and yields an uncorrected sample variance. To compute a corrected sample variance for a sample of size n,

$$s^2 = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2$$

where the sample mean is given by

$$\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i$$

The use of the term n-1 is commonly referred to as Bessel's correction. Note, however, that applying Bessel's correction can increase the mean squared error between the sample variance and population variance. Depending on the characteristics of the population distribution, other correction factors (e.g., n-1.5, n+1, etc) can yield better estimators.

Usage

var svariance = require( '@stdlib/stats/base/ndarray/svariance' );

svariance( arrays )

Computes the variance of a one-dimensional single-precision floating-point ndarray.

var Float32Array = require( '@stdlib/array/float32' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );

var opts = {
    'dtype': 'float32'
};

var xbuf = new Float32Array( [ 1.0, -2.0, 2.0 ] );
var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' );

var correction = scalar2ndarray( 1.0, opts );

var v = svariance( [ x, correction ] );
// returns ~4.333333

The function accepts the following arguments:

  • arrays: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying a degrees of freedom adjustment.

The function assumes that the input ndarray has a single-precision floating-point data type float32.

Examples

var Float32Array = require( '@stdlib/array/float32' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var svariance = require( '@stdlib/stats/base/ndarray/svariance' );

var opts = {
    'dtype': 'float32'
};

var xbuf = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
var x = new ndarray( opts.dtype, xbuf, [ 4 ], [ 2 ], 1, 'row-major' );

var correction = scalar2ndarray( 1.0, opts );

var v = svariance( [ x, correction ] );
// returns 6.25