squiggle/packages/squiggle-lang/benchmark/generators.ts

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-04-26 20:38:17 +00:00
export let generateFloatRange = (min: number, max: number): number =>
2022-04-26 22:59:27 +00:00
Math.random() * (max - min) + min;
export let generateIntRange = (min: number, max: number): number =>
Math.floor(generateFloatRange(min, max));
2022-04-26 18:26:02 +00:00
2022-04-26 22:59:27 +00:00
export let generateIntMin = (min: number): number => generateIntRange(min, 100);
2022-04-26 20:38:17 +00:00
2022-04-26 22:59:27 +00:00
export let generateInt = (): number => generateIntMin(-100);
2022-04-26 20:38:17 +00:00
2022-04-26 22:59:27 +00:00
let generatePositiveInt = (): number => generateIntMin(1);
2022-04-26 18:26:02 +00:00
export let generateNormal = (): string =>
2022-04-26 22:59:27 +00:00
`normal(${generateInt()}, ${generatePositiveInt()})`;
2022-04-26 18:26:02 +00:00
export let generateBeta = (): string =>
2022-04-26 22:59:27 +00:00
`beta(${generatePositiveInt()}, ${generatePositiveInt()})`;
2022-04-26 18:26:02 +00:00
export let generateLognormal = (): string =>
2022-04-26 22:59:27 +00:00
`lognormal(${generateInt()}, ${generatePositiveInt()})`;
export let generateExponential = (): string =>
2022-04-26 22:59:27 +00:00
`exponential(${generatePositiveInt()})`;
export let generateUniform = (): string => {
2022-04-26 22:59:27 +00:00
let a = generateInt();
let b = generateIntMin(a + 1);
2022-04-26 20:38:17 +00:00
return `uniform(${a}, ${b})`;
};
export let generateCauchy = (): string => {
2022-04-26 22:59:27 +00:00
return `cauchy(${generateInt()}, ${generatePositiveInt()})`;
2022-04-26 20:38:17 +00:00
};
export let generateTriangular = (): string => {
2022-04-26 22:59:27 +00:00
let a = generateInt();
let b = generateIntMin(a + 1);
let c = generateIntMin(b + 1);
2022-04-26 20:38:17 +00:00
return `triangular(${a}, ${b}, ${c})`;
};
export let distributions: { [key: string]: () => string } = {
normal: generateNormal,
beta: generateBeta,
lognormal: generateLognormal,
exponential: generateExponential,
triangular: generateTriangular,
cauchy: generateCauchy,
2022-04-26 20:38:17 +00:00
uniform: generateUniform,
};