Add other distributions and operations into benchmarking

This commit is contained in:
Sam Nolan 2022-04-26 16:05:16 -04:00
parent 592bdbb9e9
commit 752f2a1ea5
4 changed files with 67 additions and 8 deletions

View File

@ -1,9 +1,29 @@
import { generateNormal } from "./generators";
import { distributions, generateFloat, generateFloatRange } from "./generators";
import { test, expectEqual } from "./lib";
test("mean is the same under point set transform", () => {
let normal = generateNormal();
let symbolicMean = `mean(${normal})`;
let pointSetMean = `mean(toPointSet(${normal}))`;
return expectEqual(symbolicMean, pointSetMean);
let checkDistributionSame = (distribution: string, operation: (arg: string) => string): void => {
expectEqual(operation(distribution), operation(`toPointSet(${distribution})`));
expectEqual(operation(distribution), operation(`toSampleSet(${distribution})`));
}
Object.entries(distributions).map(([key, generator]) => {
let distribution = generator();
test(`mean is the same for ${key} distribution under all distribution types`, () =>
checkDistributionSame(distribution, (d: string) => `mean(${d})`)
)
test(`cdf is the same for ${key} distribution under all distribution types`, () => {
let cdf_value = generateFloat();
checkDistributionSame(distribution, (d: string) => `cdf(${d}, ${cdf_value})`)
})
test(`pdf is the same for ${key} distribution under all distribution types`, () => {
let pdf_value = generateFloat();
checkDistributionSame(distribution, (d: string) => `pdf(${d}, ${pdf_value})`)
})
test(`inv is the same for ${key} distribution under all distribution types`, () => {
let inv_value = generateFloatRange(0, 1);
checkDistributionSame(distribution, (d: string) => `inv(${d}, ${inv_value})`)
})
});

View File

@ -1,4 +1,6 @@
let generateFloat = (): number => Math.random() * 200 - 100;
export let generateFloat = (): number => Math.random() * 200 - 100;
export let generateFloatMin = (min:number): number => Math.random() * (100 - min) + min;
export let generateFloatRange = (min:number, max: number): number => Math.random() * (max - min) + min;
let generatePositive = (): number => Math.random() * 100;
@ -10,3 +12,33 @@ export let generateBeta = (): string =>
export let generateLognormal = (): string =>
`lognormal(${generateFloat()}, ${generatePositive()})`;
export let generateExponential = (): string =>
`exponential(${generatePositive()})`;
export let generateUniform = (): string => {
let a = generateFloat()
let b = generateFloatMin(a)
return `uniform(${a}, ${b})`
}
export let generateCauchy = (): string => {
return `cauchy(${generateFloat()}, ${generatePositive()})`
}
export let generateTriangular = (): string => {
let a = generateFloat()
let b = generateFloatMin(a)
let c = generateFloatMin(b)
return `triangular(${a}, ${b}, ${c})`
}
export let distributions : {[key: string]: () => string} = {
normal: generateNormal,
beta: generateBeta,
lognormal: generateLognormal,
exponential: generateExponential,
triangular: generateTriangular ,
cauchy: generateCauchy,
uniform: generateUniform
}

View File

@ -26,7 +26,11 @@ export function expectEqual(expression1: string, expression2: string) {
console.log(`${expression1} === ${expression2}`);
console.log(`${result1.value} === ${result2.value}`);
console.log(`loss = ${loss}`);
console.log(`logloss = ${Math.log(loss)}`);
console.log(`logloss = ${Math.abs(Math.log(result1.value) - Math.log(result2.value))}`);
console.log()
}
else {
throw Error(`Expected both to be number, but got ${result1.tag} and ${result2.tag}`)
}
}

View File

@ -31,6 +31,9 @@ let makeSampleSetDist = SampleSetDist.make
@genType
let evaluate = Reducer.evaluate
@genType
let evaluateUsingExternalBindings = Reducer.evaluateUsingExternalBindings
@genType
type expressionValue = ReducerInterface_ExpressionValue.expressionValue