Continuous uniform distribution.
$ npm install distributions-uniform
For use in the browser, use browserify.
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...
Returns the distribution support, which is all numbers in the interval [a,b]
.
var support = dist.support();
// returns [ a, b ]
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.
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.
Returns the distribution mean
.
var mean = dist.mean();
// returns 0.5 * ( a + b )
Returns the distribution variance
.
var variance = dist.variance();
// returns (1/12) * (b - a)^2
Returns the distribution median
.
var median = dist.median();
// returns 0.5 * ( a + b )
Returns the distribution skewness
.
var skewness = dist.skewness();
// returns 0
Returns the distribution excess kurtosis
.
var excess = dist.ekurtosis();
// returns -6/5
Returns the distribution's differential entropy.
var entropy = dist.entropy();
// returns ln( b - a )
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 [...]
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 [...]
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.
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.
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
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.
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
Copyright © 2015. The Distributions.io Authors.
[npm-image]: http://img.shields.io/npm/v/distributions-uniform.svg