Skip to content

distributions-io/t-skewness

Repository files navigation

Skewness

NPM version Build Status Coverage Status Dependencies

Student's t distribution skewness.

The skewness for a Student's t random variable is

Skewness for Student's t distribution.

for v > 3, where v is the degrees of freedom of the distribution. For any v <= 3, the skewness is undefined. In this case, NaN is returned by this module.

Installation

$ npm install distributions-t-skewness

For use in the browser, use browserify.

Usage

var skewness = require( 'distributions-t-skewness' );

skewness( v[, opts] )

Computes the skewness for a Student's t distribution with parameter v. v may be either a number, an array, a typed array, or a matrix.

var matrix = require( 'dstructs-matrix' ),
	data,
	mat,
	out,
	i;

out = skewness( 2 );
// returns NaN

v = [ 2, 4, 8, 16 ];
out = skewness( v );

// returns [ NaN, 0, 0, 0 ]

v = new Float32Array( v );
out = skewness( v );
// returns Float64Array( [NaN,0,0,0] )

v =  matrix( [ 2, 4, 8, 16 ], [2,2] );
/*
	[ 2 4
	  8 16 ]
*/

out = skewness( v );
/*
	[ NaN 0
	  0 0 ]
*/

The function accepts the following options:

  • accessor: accessor function for accessing array values.
  • dtype: output typed array or matrix data type. Default: float64.
  • copy: boolean indicating if the function should return a new data structure. Default: true.
  • path: deepget/deepset key path.
  • sep: deepget/deepset key path separator. Default: '.'.

For non-numeric arrays, provide an accessor function for accessing array values.

var v = [
	[0,2],
	[1,4],
	[2,8],
	[3,16]
];

function getValue( d, i ) {
	return d[ 1 ];
}

var out = skewness( v, {
	'accessor': getValue
});
// returns [ NaN, 0, 0, 0 ]

To deepset an object array, provide a key path and, optionally, a key path separator.

var v = [
	{'x':[9,2]},
	{'x':[9,4]},
	{'x':[9,8]},
	{'x':[9,16]}
];

var out = skewness( v, 'x|1', '|' );
/*
	[
		{'x':[9,NaN]},
		{'x':[9,0]},
		{'x':[9,0]},
		{'x':[9,0]},
	]
*/

var bool = ( data === out );
// returns true

By default, when provided a typed array or matrix, the output data structure is float64 in order to preserve precision. To specify a different data type, set the dtype option (see matrix for a list of acceptable data types).

var v, out;

v = new Float64Array( [ 2,4,8,16 ] );

out = skewness( v, {
	'dtype': 'int32'
});
// returns Int32Array( [ 0,0,0,0 ] )

// Works for plain arrays, as well...
out = skewness( [2,4,8,16], {
	'dtype': 'int32'
});
// returns Int32Array( [ 0,0,0,0 ] )

By default, the function returns a new data structure. To mutate the input data structure (e.g., when input values can be discarded or when optimizing memory usage), set the copy option to false.

var v,
	bool,
	mat,
	out,
	i;

v = [ 2, 4, 8, 16 ];

out = skewness( v, {
	'copy': false
});
// returns [ NaN, 0, 0, 0 ]

bool = ( data === out );
// returns true

mat = matrix( [ 2, 4, 8, 16 ], [2,2] );
/*
	[ 2 4
	  8 16 ]
*/

out = skewness( mat, {
	'copy': false
});
/*
	[ NaN 0
	  0 0 ]
*/

bool = ( mat === out );
// returns true

Notes

  • If an element is not a positive number, the skewness is NaN.

    var v, out;
    
    out = skewness( -1 );
    // returns NaN
    
    out = skewness( 0 );
    // returns NaN
    
    out = skewness( null );
    // returns NaN
    
    out = skewness( true );
    // returns NaN
    
    out = skewness( {'a':'b'} );
    // returns NaN
    
    out = skewness( [ true, null, [] ] );
    // returns [ NaN, NaN, NaN ]
    
    function getValue( d, i ) {
    	return d.x;
    }
    v = [
    	{'x':true},
    	{'x':[]},
    	{'x':{}},
    	{'x':null}
    ];
    
    out = skewness( v, {
    	'accessor': getValue
    });
    // returns [ NaN, NaN, NaN, NaN ]
    
    out = skewness( v, {
    	'path': 'x'
    });
    /*
    	[
    		{'x':NaN},
    		{'x':NaN},
    		{'x':NaN,
    		{'x':NaN}
    	]
    */
  • Be careful when providing a data structure which contains non-numeric elements and specifying an integer output data type, as NaN values are cast to 0.

    var out = skewness( [ true, null, [] ], {
    	'dtype': 'int8'
    });
    // returns Int8Array( [0,0,0] );

Examples

var matrix = require( 'dstructs-matrix' ),
	skewness = require( 'distributions-t-skewness' );

var v,
	mat,
	out,
	tmp,
	i;

// Plain arrays...
v = new Array( 10 );
for ( i = 0; i < v.length; i++ ) {
	v[ i ] = i;
}
out = skewness( v );

// Object arrays (accessors)...
function getValue( d ) {
	return d.x;
}
for ( i = 0; i < v.length; i++ ) {
	v[ i ] = {
		'x': v[ i ]
	};
}
out = skewness( v, {
	'accessor': getValue
});

// Deep set arrays...
for ( i = 0; i < v.length; i++ ) {
	v[ i ] = {
		'x': [ i, v[ i ].x ]
	};
}
out = skewness( v, {
	'path': 'x/1',
	'sep': '/'
});

// Typed arrays...
v = new Float64Array( 10 );
for ( i = 0; i < v.length; i++ ) {
	v[ i ] = i;
}
out = skewness( v );

// Matrices...
mat = matrix( v, [5,2], 'float64' );
out = skewness( mat );

// Matrices (custom output data type)...
out = skewness( mat, {
	'dtype': 'uint8'
});

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2015. The Compute.io Authors.

About

Student's t distribution skewness.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published