Have ideas to improve npm?Join in the discussion! »

    discrete-sampling

    1.0.3 • Public • Published

    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();     // 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)

    Install

    npm i discrete-sampling

    DownloadsWeekly Downloads

    55

    Version

    1.0.3

    License

    ISC

    Last publish

    Collaborators

    • avatar