Bookmark this: tool.io/probability/gamma-sampler/source-code
Gamma Distribution Sampler Source Code
Browse the implementation files for Gamma Distribution Sampler.tool.ts typescript
import {
sampleRandomSource,
type RandomSource
} from '../../../tool-runtime/randomSource';
import { parseInteger } from '../../parse/integer/tool';
import { parseNumber } from '../../parse/number/tool';
import { randomNormal } from '../normal-box-muller-sampler/tool';
import { createSeededRandom } from '../../random/seeded-number/tool';
export const randomGamma = (
shape: number,
random: RandomSource
): number | null => {
if (!Number.isFinite(shape) || shape <= 0) return null;
if (shape < 1) {
const boosted = randomGamma(shape + 1, random);
const uniform = sampleRandomSource(random);
return boosted === null || uniform === null
? null
: boosted * uniform ** (1 / shape);
}
const offset = shape - 1 / 3;
const scale = 1 / Math.sqrt(9 * offset);
while (true) {
const normal = randomNormal(random);
if (normal === null) return null;
const base = 1 + scale * normal;
if (base <= 0) continue;
const value = base ** 3;
const uniform = sampleRandomSource(random);
if (uniform === null) return null;
if (
uniform < 1 - 0.0331 * normal ** 4 ||
Math.log(uniform) <
0.5 * normal ** 2 +
offset * (1 - value + Math.log(value))
) {
return offset * value;
}
}
};
export const calculateGammaSamples = (
shape: number,
scale: number,
count: number,
random: RandomSource
): number[] | null => {
if (
!Number.isFinite(shape) ||
shape <= 0 ||
!Number.isFinite(scale) ||
scale <= 0 ||
!Number.isSafeInteger(count) ||
count < 1 ||
count > 100_000
) {
return null;
}
const samples: number[] = [];
for (let index = 0; index < count; index += 1) {
const sample = randomGamma(shape, random);
if (sample === null) return null;
samples.push(sample * scale);
}
return samples;
};
export const sampleGamma = (
shapeInput: unknown,
scaleInput: unknown,
countInput: unknown,
seedInput: unknown
): string => {
const shape = parseNumber(shapeInput);
const scale = parseNumber(scaleInput);
const count = parseInteger(countInput);
if (
shape === null ||
scale === null ||
count === null ||
typeof seedInput !== 'string' ||
seedInput.trim() === ''
) {
return '';
}
const samples = calculateGammaSamples(
shape,
scale,
count,
createSeededRandom(seedInput)
);
return samples === null
? ''
: JSON.stringify(
{ count, samples, scale, seed: seedInput, shape },
null,
2
);
};
export const Tool: TypeTool = {
name: 'Gamma Distribution Sampler',
slug: 'probability/gamma-sampler',
intro:
'Generate reproducible gamma-distributed samples from a positive shape and scale.',
tool: sampleGamma,
i: [
{
example: '2',
label: 'Shape',
placeholder: 'Positive shape',
type: 'number'
},
{
example: '1',
label: 'Scale',
placeholder: 'Positive scale',
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": [1.2, 2.4] }',
label: 'Gamma Samples',
placeholder: 'Samples will appear here...',
render: 'textarea',
type: 'text'
}
],
rerunnable: true,
alternative: [
{
title: 'Poisson–Gamma Mixture Sampler',
url: '/probability/poisson-gamma-mixture-sampler'
},
{
title: 'Normal Distribution Box–Muller Sampler',
url: '/probability/normal-box-muller-sampler'
}
]
};
export default Tool;
Description
Browse the implementation files for Gamma Distribution Sampler.