Skip to content

Main content

Normal Distribution Box–Muller Sampler Source Code

Browse the implementation files for Normal Distribution Box–Muller 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';

export const randomNormal = (
	random: RandomSource
): number | null => {
	const firstSample = sampleRandomSource(random);
	const second = sampleRandomSource(random);
	if (firstSample === null || second === null) return null;

	const first = Math.max(Number.EPSILON, firstSample);
	return (
		Math.sqrt(-2 * Math.log(first)) *
		Math.cos(2 * Math.PI * second)
	);
};

export const sampleNormalBoxMuller = (
	meanInput: unknown,
	standardDeviationInput: unknown,
	countInput: unknown,
	seedInput: unknown
): string => {
	const mean = parseFiniteNumber(meanInput);
	const standardDeviation = parseFiniteNumber(
		standardDeviationInput
	);
	const count = parseInteger(countInput);
	if (
		mean === null ||
		standardDeviation === null ||
		standardDeviation < 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 standardNormal = randomNormal(random);
		if (standardNormal === null) return '';
		const sample = mean + standardDeviation * standardNormal;
		if (!Number.isFinite(sample)) return '';
		samples.push(Number(sample.toPrecision(10)));
	}
	return JSON.stringify(
		{
			count,
			mean,
			method: 'Box–Muller',
			samples,
			seed: seedInput,
			standardDeviation
		},
		null,
		2
	);
};

export const Tool: TypeTool = {
	name: 'Normal Distribution Box–Muller Sampler',
	slug: 'probability/normal-box-muller-sampler',
	intro:
		'Generate reproducible normally distributed samples with the Box–Muller transform.',
	tool: sampleNormalBoxMuller,
	i: [
		{
			example: '0',
			label: 'Mean',
			placeholder: 'Distribution mean',
			type: 'number'
		},
		{
			example: '1',
			label: 'Standard Deviation',
			placeholder: 'Nonnegative standard deviation',
			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": [0.4, -1.2] }',
			label: 'Normal Samples',
			placeholder: 'Samples will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	cite: [
		{
			title: 'Box–Muller transform - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform'
		}
	]
};
export default Tool;

Description

Browse the implementation files for Normal Distribution Box–Muller Sampler.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools