Skip to content

Latest commit

 

History

History
129 lines (82 loc) · 3.56 KB

File metadata and controls

129 lines (82 loc) · 3.56 KB

snanstdev

Compute the standard deviation of a one-dimensional single-precision floating-point ndarray, ignoring NaN values.

Usage

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

snanstdev( arrays )

Computes the standard deviation of a one-dimensional single-precision floating-point ndarray, ignoring NaN values.

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

var xbuf = new Float32Array( [ 1.0, 3.0, NaN, 2.0 ] );
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );

var c = scalar2ndarray( 1.0, {
    'dtype': 'float32'
});

var v = snanstdev( [ x, c ] );
// returns ~1.0

The function has the following parameters:

  • arrays: array-like object containing two ndarrays:
    • x: a one-dimensional input ndarray.
    • c: a zero-dimensional ndarray specifying the degrees of freedom adjustment. Providing a non-zero degrees of freedom adjustment has the effect of adjusting the divisor during the calculation of the standard deviation according to N-c, where N is the number of non-NaN elements in the input ndarray and c corresponds to the provided degrees of freedom adjustment. When computing the standard deviation of a population, setting this parameter to 0 is the standard choice. When computing the corrected sample standard deviation, setting this parameter to 1 is the standard choice (commonly referred to as Bessel's correction).

Notes

  • If provided an empty one-dimensional ndarray, the function returns NaN.

Examples

var uniform = require( '@stdlib/random/base/uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var snanstdev = require( '@stdlib/stats/base/ndarray/snanstdev' );

function rand() {
    if ( bernoulli( 0.8 ) < 1 ) {
        return NaN;
    }
    return uniform( -50.0, 50.0 );
}

var xbuf = filledarrayBy( 10, 'float32', rand );
var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );

var c = scalar2ndarray( 1.0, {
    'dtype': 'float32'
});

var v = snanstdev( [ x, c ] );
console.log( v );