/sampling

sampling.js

Primary LanguageJavaScript

sampling

JavaScript for sampling from a handful of discrete probability distributions

Currently Supported

  • Bernoulli
  • Binomial
  • Discrete
  • Multinomial
  • Poisson
  • Negative Binomial

Examples

var bern = Sampling.Bernoulli(.5);
bern.draw();     // One random draw
bern.sample(10); // Get an array of 10 samples. 
var binom = SJS.Binomial(10, 0.5);  // alternate syntax (Sampling === SJS)
binom.draw();
binom.sample(10);
var probabilities = [.1, .2, .3, .05, .15];
var disc = SJS.Discrete(probabilities);
disc.draw(); 
disc.sample(10); 
// The probabilities will be normalized for you. 
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 five elements with replacement
sample_from_array(myArray, 5, true)
// sample five elements without replacement
sample_from_array(myArray, 5)