Bookmark this: tool.io/probability/poisson-sampler/source-code
Poisson Distribution Sampler Source Code
Browse the implementation files for Poisson Distribution Sampler.tool.ts typescript
import { parseFiniteNumber } from '../../parse/number/tool';
import { parseInteger } from '../../parse/integer/tool';
import {
sampleRandomSource,
type RandomSource
} from '../../../tool-runtime/randomSource';
import { createSeededRandom } from '../../random/seeded-number/tool';
const LANCZOS_COEFFICIENTS = [
676.5203681218851, -1259.1392167224028, 771.3234287776531,
-176.6150291621406, 12.507343278686905, -0.13857109526572012,
9.984369578019572e-6, 1.5056327351493116e-7
];
const logGamma = (value: number): number => {
const shifted = value - 1;
let series = 0.9999999999998099;
for (
let index = 0;
index < LANCZOS_COEFFICIENTS.length;
index += 1
)
series +=
LANCZOS_COEFFICIENTS[index] / (shifted + index + 1);
const scale = shifted + LANCZOS_COEFFICIENTS.length - 0.5;
return (
0.5 * Math.log(2 * Math.PI) +
(shifted + 0.5) * Math.log(scale) -
scale +
Math.log(series)
);
};
export const randomPoisson = (
rate: number,
random: RandomSource
): number | null => {
if (!Number.isFinite(rate) || rate < 0) return null;
if (rate === 0) return 0;
if (rate <= 30) {
const limit = Math.exp(-rate);
let product = 1;
let count = 0;
do {
count += 1;
const sample = sampleRandomSource(random);
if (sample === null) return null;
product *= sample;
} while (product > limit);
return count - 1;
}
const squareRoot = Math.sqrt(rate);
const b = 0.931 + 2.53 * squareRoot;
const a = -0.059 + 0.02483 * b;
const inverseAlpha = 1.1239 + 1.1328 / (b - 3.4);
const immediateAcceptance = 0.9277 - 3.6224 / (b - 2);
for (let attempt = 0; attempt < 1_000_000; attempt += 1) {
const first = sampleRandomSource(random);
const second = sampleRandomSource(random);
if (first === null || second === null) return null;
const centered = first - 0.5;
const distance = 0.5 - Math.abs(centered);
if (distance <= 0) continue;
const sample = Math.floor(
((2 * a) / distance + b) * centered + rate + 0.43
);
if (
distance >= 0.07 &&
second <= immediateAcceptance &&
sample >= 0
)
return sample;
if (sample < 0 || (distance < 0.013 && second > distance))
continue;
const left = Math.log(
(second * inverseAlpha) / (a / (distance * distance) + b)
);
const right =
-rate + sample * Math.log(rate) - logGamma(sample + 1);
if (left <= right) return sample;
}
return null;
};
export const samplePoisson = (
rateInput: unknown,
countInput: unknown,
seedInput: unknown
): string => {
const rate = parseFiniteNumber(rateInput);
const count = parseInteger(countInput);
if (
rate === null ||
rate < 0 ||
rate > 1_000_000 ||
count === null ||
count < 1 ||
count > 100_000 ||
typeof seedInput !== 'string' ||
!seedInput.trim()
)
return '';
const random = createSeededRandom(seedInput);
const samples: number[] = [];
for (let index = 0; index < count; index += 1) {
const sample = randomPoisson(rate, random);
if (sample === null) return '';
samples.push(sample);
}
return JSON.stringify(
{ count, rate, samples, seed: seedInput },
null,
2
);
};
export const Tool: TypeTool = {
name: 'Poisson Distribution Sampler',
slug: 'probability/poisson-sampler',
intro:
'Generate reproducible Poisson-distributed event-count samples.',
tool: samplePoisson,
i: [
{
example: '4',
label: 'Rate λ',
placeholder: 'Nonnegative expected count',
type: 'number'
},
{
example: '20',
label: 'Sample Count',
placeholder: 'Number of samples',
type: 'number'
},
{
example: 'tool.io',
label: 'Seed',
placeholder: 'Reproducible seed',
type: 'text'
}
],
o: [
{
example: '{ "samples": [3, 5] }',
label: 'Poisson Samples',
placeholder: 'Samples will appear here...',
render: 'textarea',
type: 'text'
}
],
rerunnable: true,
cite: [
{
title: 'Poisson distribution - Wikipedia',
url: 'https://en.wikipedia.org/wiki/Poisson_distribution'
}
]
};
export default Tool;
Description
Browse the implementation files for Poisson Distribution Sampler.