sampling
JavaScript for sampling from a handful of discrete probability distributions
Setup
This is now a node module on npm. To install, go to your project's directory in
a terminal and enter
npm install discrete-sampling
To load the library in your own node module, use
var SJS = Sampling = require('discrete-sampling');
To load the module in the browser, you can load the script with
<script src="node_modules/discrete-sampling/dist/discrete.js"></script>
and then the library will be accessible through the global variables SJS
and
Sampling
.
Currently Supported
- Bernoulli
- Binomial
- Discrete
- Multinomial
- Poisson
- Negative Binomial
Examples
var bern = Sampling.Bernoulli(.5);
bern.draw();
bern.sample(10);
var binom = SJS.Binomial(10, 0.5);
binom.draw();
binom.sample(10);
var probabilities = [.1, .2, .3, .05, .15];
var disc = SJS.Discrete(probabilities);
disc.draw();
disc.sample(10);
var probs = [1,1,1,1,1,1,1,1,1,1,1,1,1,1]
var mult = SJS.Multinomial(5, probs);
mult.draw();
mult.sample(10);
var myArray = [0,1,2,3,4,5]
sample_from_array(myArray, 5, true)
sample_from_array(myArray, 5)