Skip to main content
Module

x/simplestatistics/src/poisson_distribution.js

simple statistics for node & browser javascript
Go to Latest
File
import epsilon from "./epsilon.js";
/** * The [Poisson Distribution](http://en.wikipedia.org/wiki/Poisson_distribution) * is a discrete probability distribution that expresses the probability * of a given number of events occurring in a fixed interval of time * and/or space if these events occur with a known average rate and * independently of the time since the last event. * * The Poisson Distribution is characterized by the strictly positive * mean arrival or occurrence rate, `λ`. * * @param {number} lambda location poisson distribution * @returns {number[]} values of poisson distribution at that point */function poissonDistribution(lambda) /*: ?number[] */ { // Check that lambda is strictly positive if (lambda <= 0) { return undefined; }
// our current place in the distribution let x = 0; // and we keep track of the current cumulative probability, in // order to know when to stop calculating chances. let cumulativeProbability = 0; // the calculated cells to be returned const cells = []; let factorialX = 1;
// This algorithm iterates through each potential outcome, // until the `cumulativeProbability` is very close to 1, at // which point we've defined the vast majority of outcomes do { // a [probability mass function](https://en.wikipedia.org/wiki/Probability_mass_function) cells[x] = (Math.exp(-lambda) * Math.pow(lambda, x)) / factorialX; cumulativeProbability += cells[x]; x++; factorialX *= x; // when the cumulativeProbability is nearly 1, we've calculated // the useful range of this distribution } while (cumulativeProbability < 1 - epsilon);
return cells;}
export default poissonDistribution;