Giter Club home page Giter Club logo

binomial-random's Introduction

Binomial Random Variables

NPM version Build Status Coverage Status Dependencies

Creates a matrix or array filled with draws from a binomial distribution.

Installation

$ npm install distributions-binomial-random

For use in the browser, use browserify.

Usage

var random = require( 'distributions-binomial-random' );

random( [dims][, opts] )

Creates a matrix or array filled with draws from a binomial distribution. The dims argument may either be a positive integer specifying a length or an array of positive integers specifying dimensions. If no dims argument is supplied,the function returns a single random draw from a binomial distribution.

var out;

// Set seed
random.seed = 2;

out = random( 5 );
// returns [ 1, 1, 0, 1, 1 ]

out = random( [2,1,2] );
// returns [ [ [1,0] ], [ [1,1] ] ]

The function accepts the following options:

  • n: number of trails. Default: 1.
  • p: success probability. Default: 0.5.
  • seed: positive integer used as a seed to initialize the generator. If not supplied, uniformly distributed random numbers are generated via an underlying generator seedable by setting the seed property of the exported function.
  • dtype: output data type (see matrix for a list of acceptable data types). Default: generic.

The binomial distribution is a function of two parameters: n (number of trails) and 0 <= p <= 1 (success probability). By default, n is equal to 1 and p is equal to 0.5. To adjust either parameter, set the corresponding option.

var out = random( 5, {
	'n': 200,
	'p': 0.3
});
// returns [ 65, 60, 65, 55, 53 ]

To be able to reproduce the generated random numbers, set the seed option to a positive integer.

var out = random( 3, {
	'seed': 22
});
// returns [ 1, 0, 0 ]

var out = random( 3, {
    'seed': 22
});
// returns [ 1, 0, 0 ]

If no seed option is supplied, each function call uses a common underlying uniform number generator. A positive-integer seed for this underlying generator can be supplied by setting the seed property of the exported function.

random.seed = 11;
var out = random();
// returns 1

var out = random();
// returns 1

random.seed = 11;
var out = random();
// returns 1

var out = random();
// returns 1

By default, the output data structure is a generic array. To output a typed array or matrix, set the dtype option.

var out;

out = random( 5, {
	'dtype': 'float32'
});
// returns Float32Array( [0,1,1,1,1] )

out = random( [3,2], {
	'dtype': 'float64'
});
/*
	[ 0 1
	  1 1
	  0 1 ]
*/

Notes:

  • Currently, for more than 2 dimensions, the function outputs a generic array and ignores any specified dtype.

    var out = random( [2,1,3], {
    	'dtype': 'float32'
    });
    // returns [ [ [0,0,0] ], [ [1,1,1] ] ]

Examples

var random = require( 'distributions-binomial-random' ),
	out;

// Set seed
random.seed = 11;

// Plain arrays...

// 1x10:
out = random( 10 );

// 2x1x3:
out = random( [2,1,3] );

// 5x5x5:
out = random( [5,5,5] );

// 10x5x10x20:
out = random( [10,5,10,20] );

// Typed arrays...
out = random( 10, {
	'dtype': 'float32'
});

// Matrices...
out = random( [3,2], {
	'dtype': 'float64'
});

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.