Bookmark this: tool.io/probability/pareto-sampler/source-code
Pareto Distribution Sampler Source Code
Browse the implementation files for Pareto Distribution Sampler.tool.ts typescript
import { parseFiniteNumber } from '../../parse/number/tool';
import { parseInteger } from '../../parse/integer/tool';
import { createSeededRandom } from '../../random/seeded-number/tool';
export const samplePareto = (
scaleInput: unknown,
shapeInput: unknown,
countInput: unknown,
seedInput: unknown
): string => {
const scale = parseFiniteNumber(scaleInput);
const shape = parseFiniteNumber(shapeInput);
const count = parseInteger(countInput);
if (
scale === null ||
scale <= 0 ||
shape === null ||
shape <= 0 ||
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 =
scale /
Math.max(Number.EPSILON, 1 - random()) ** (1 / shape);
if (!Number.isFinite(sample)) return '';
samples.push(Number(sample.toPrecision(10)));
}
return JSON.stringify(
{
count,
method: 'inverse transform',
samples,
scale,
seed: seedInput,
shape
},
null,
2
);
};
export const Tool: TypeTool = {
name: 'Pareto Distribution Sampler',
slug: 'probability/pareto-sampler',
intro:
'Generate reproducible Pareto Type I samples using inverse-transform sampling.',
tool: samplePareto,
i: [
{
example: '1',
label: 'Minimum Scale',
placeholder: 'Positive scale xm',
type: 'number'
},
{
example: '2',
label: 'Shape',
placeholder: 'Positive alpha',
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, 3.4] }',
label: 'Pareto Samples',
placeholder: 'Samples will appear here...',
render: 'textarea',
type: 'text'
}
],
rerunnable: true,
cite: [
{
title: 'Pareto distribution - Wikipedia',
url: 'https://en.wikipedia.org/wiki/Pareto_distribution'
}
]
};
export default Tool;
Description
Browse the implementation files for Pareto Distribution Sampler.