diff --git a/packages/squiggle-lang/benchmark/conversion_tests.ts b/packages/squiggle-lang/benchmark/conversion_tests.ts index 52804c0b..c2f9dcd7 100644 --- a/packages/squiggle-lang/benchmark/conversion_tests.ts +++ b/packages/squiggle-lang/benchmark/conversion_tests.ts @@ -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})`) + }) }); diff --git a/packages/squiggle-lang/benchmark/generators.ts b/packages/squiggle-lang/benchmark/generators.ts index 6d5d7854..9e134a0b 100644 --- a/packages/squiggle-lang/benchmark/generators.ts +++ b/packages/squiggle-lang/benchmark/generators.ts @@ -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 +} + diff --git a/packages/squiggle-lang/benchmark/lib.ts b/packages/squiggle-lang/benchmark/lib.ts index 7049d62c..ab63c317 100644 --- a/packages/squiggle-lang/benchmark/lib.ts +++ b/packages/squiggle-lang/benchmark/lib.ts @@ -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}`) } } diff --git a/packages/squiggle-lang/src/rescript/TypescriptInterface.res b/packages/squiggle-lang/src/rescript/TypescriptInterface.res index 8704bf5e..fc381729 100644 --- a/packages/squiggle-lang/src/rescript/TypescriptInterface.res +++ b/packages/squiggle-lang/src/rescript/TypescriptInterface.res @@ -31,6 +31,9 @@ let makeSampleSetDist = SampleSetDist.make @genType let evaluate = Reducer.evaluate +@genType +let evaluateUsingExternalBindings = Reducer.evaluateUsingExternalBindings + @genType type expressionValue = ReducerInterface_ExpressionValue.expressionValue