Skip to content

distributions-io/uniform

Repository files navigation

uniform

![NPM version][npm-image] Build Status Coverage Status Dependencies

Continuous uniform distribution.

Installation

$ npm install distributions-uniform

For use in the browser, use browserify.

Usage

var createDist = require( 'distributions-uniform' );

To create a uniform distribution,

var dist = createDist();

The constructor function takes two input arguments, a and b, the lower and upper endpoints of the distribution. By default, a standard uniform distribution over the interval [0,1] is created.

The distribution is configurable and has the following methods...

dist.support()

Returns the distribution support, which is all numbers in the interval [a,b].

var support = dist.support();
// returns [ a, b ]

dist.a( [value] )

This method is a setter/getter. If no value is provided, returns the minimum value. To set a,

dist.a( 10 );

The default minimum value is 1.

dist.b( [value] )

This method is a setter/getter. If no value is provided, returns the maximum value. To set b,

dist.b( 100 );

The default maximum value is 1.

dist.mean()

Returns the distribution mean.

var mean = dist.mean();
// returns 0.5 * ( a + b )

dist.variance()

Returns the distribution variance.

var variance = dist.variance();
// returns (1/12) * (b - a)^2

dist.median()

Returns the distribution median.

var median = dist.median();
// returns 0.5 * ( a + b )

dist.skewness()

Returns the distribution skewness.

var skewness = dist.skewness();
// returns 0

dist.ekurtosis()

Returns the distribution excess kurtosis.

var excess = dist.ekurtosis();
// returns -6/5

dist.entropy()

Returns the distribution's differential entropy.

var entropy = dist.entropy();
// returns ln( b - a )

dist.pdf( [x] )

If no argument is provided, returns the probability density function (PDF). If a number, an array, a typed array, or a matrix is provided, evaluates the PDF for each element.

var data = [ 0, 0.2, 0.5, 0.8 ];

var pdf = dist.pdf( data );
// returns [...]

dist.cdf( [x] )

If no argument is provided, returns the cumulative distribution function (CDF). If a number, an array, a typed array, or a matrix is provided, evaluates the CDF for each element.

var data = [ 0, 0.2, 0.5, 0.8 ];

var cdf = dist.cdf( data );
// returns [...]

dist.quantile( [p] )

If no argument is provided, returns the quantileerse cumulative distribution (quantile) function. If a number, an array, a typed array, or a matrix of probabilities is provided, evaluates the quantile function for each element.

var probs = [ 0.025, 0.5, 0.975 ];

var quantiles = dist.quantile( probs );
// returns [...]

Note: all values must exist on the interval [0, 1]. The function returns NaN for a value not satisfying this condition.

dist.mgf( [t] )

If no argument is provided, returns the moment generating function (MGF) of the distribution. If a number, an array, a typed array, or a matrix is provided, evaluates the MGF for each input element.

Examples

var createDist = require( 'distributions-uniform' );

// Define the distribution parameters...
var a = 10,
	b = 20;

// Create a vector...
var vec = new Array( 1000 ),
	len = vec.length;

for ( var i = 0; i < len; i++ ) {
	vec[ i ] = a + ( b - a ) * Math.random();
}

// Create a uniform distribution and configure...
var dist = createDist( a, b );

// Evaluate the probability density function over the vector...
var pdf = dist.pdf( vec );

var arr = new Array( 100 );
for ( var j = 0; j < arr.length; j++ ) {
	arr[ j ] = [ vec[j], pdf[j] ];
}
console.log( arr );

// Evaluate the quantile function for canonical cumulative probability values...
var quantiles = dist.quantile( [ 0.025, 0.5, 0.975 ] );

console.log( quantiles );

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,

$ open reports/coverage/lcov-report/index.html

License

MIT license.

Copyright

Copyright © 2015. The Distributions.io Authors.

[npm-image]: http://img.shields.io/npm/v/distributions-uniform.svg

About

Uniform distribution.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published