From cc29eb33be46b810f7585cae3de13ee39400a3ef Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Wed, 20 Apr 2022 19:07:25 -0400 Subject: [PATCH] used skip instead of commenting out --- .../squiggle-lang/__tests__/TS/Parser_test.ts | 4 +- .../__tests__/TS/PointSet_test.ts | 72 +++-- .../__tests__/TS/SampleSet_test.ts | 272 +++++++++--------- .../squiggle-lang/__tests__/TS/TestHelpers.ts | 5 - 4 files changed, 177 insertions(+), 176 deletions(-) diff --git a/packages/squiggle-lang/__tests__/TS/Parser_test.ts b/packages/squiggle-lang/__tests__/TS/Parser_test.ts index 090062b8..0120ff30 100644 --- a/packages/squiggle-lang/__tests__/TS/Parser_test.ts +++ b/packages/squiggle-lang/__tests__/TS/Parser_test.ts @@ -9,9 +9,7 @@ import * as fc from "fast-check"; describe("Squiggle's parser is whitespace insensitive", () => { test("when assigning a distribution to a name and calling that name", () => { - /* - * intersperse varying amounts of whitespace in a squiggle string - */ + // intersperse varying amounts of whitespace in a squiggle string let squiggleString = ( a: string, b: string, diff --git a/packages/squiggle-lang/__tests__/TS/PointSet_test.ts b/packages/squiggle-lang/__tests__/TS/PointSet_test.ts index baba792d..3e74f039 100644 --- a/packages/squiggle-lang/__tests__/TS/PointSet_test.ts +++ b/packages/squiggle-lang/__tests__/TS/PointSet_test.ts @@ -1,39 +1,37 @@ // import { errorValueToString } from "../../src/js/index"; -// import { testRun, expectErrorToBeBounded } from "./TestHelpers"; -// import * as fc from "fast-check"; +import { testRun, expectErrorToBeBounded } from "./TestHelpers"; +import * as fc from "fast-check"; -// describe("Mean of mixture is weighted average of means", () => { -// test("mx(beta(a,b), lognormal(m,s), [x,y])", () => { -// fc.assert( -// fc.property( -// fc.float({ min: 1e-1 }), // alpha -// fc.float({ min: 1 }), // beta -// fc.float(), // mu -// fc.float({ min: 1e-1 }), // sigma -// fc.float({ min: 1e-7 }), -// fc.float({ min: 1e-7 }), -// (a, b, m, s, x, y) => { -// let squiggleString = `mean(mixture(beta(${a},${b}), lognormal(${m},${s}), [${x}, ${y}]))`; -// let res = testRun(squiggleString); -// let weightDenom = x + y; -// let betaWeight = x / weightDenom; -// let lognormalWeight = y / weightDenom; -// let betaMean = 1 / (1 + b / a); -// let lognormalMean = m + s ** 2 / 2; -// if (res.tag == "number") { -// expectErrorToBeBounded( -// res.value, -// betaWeight * betaMean + lognormalWeight * lognormalMean, -// 1, -// 2 -// ); -// } else { -// expect(res.value).toEqual("some error message"); -// } -// } -// ) -// ); -// }); -// }); - -describe("vacuous", () => test("vacuous", () => expect(true).toEqual(true))); +describe("Mean of mixture is weighted average of means", () => { + test.skip("mx(beta(a,b), lognormal(m,s), [x,y])", () => { + fc.assert( + fc.property( + fc.float({ min: 1e-1 }), // alpha + fc.float({ min: 1 }), // beta + fc.float(), // mu + fc.float({ min: 1e-1 }), // sigma + fc.float({ min: 1e-7 }), + fc.float({ min: 1e-7 }), + (a, b, m, s, x, y) => { + let squiggleString = `mean(mixture(beta(${a},${b}), lognormal(${m},${s}), [${x}, ${y}]))`; + let res = testRun(squiggleString); + let weightDenom = x + y; + let betaWeight = x / weightDenom; + let lognormalWeight = y / weightDenom; + let betaMean = 1 / (1 + b / a); + let lognormalMean = m + s ** 2 / 2; + if (res.tag == "number") { + expectErrorToBeBounded( + res.value, + betaWeight * betaMean + lognormalWeight * lognormalMean, + 1, + 2 + ); + } else { + expect(res.value).toEqual("some error message"); + } + } + ) + ); + }); +}); diff --git a/packages/squiggle-lang/__tests__/TS/SampleSet_test.ts b/packages/squiggle-lang/__tests__/TS/SampleSet_test.ts index d9dbf0d7..c4599f7c 100644 --- a/packages/squiggle-lang/__tests__/TS/SampleSet_test.ts +++ b/packages/squiggle-lang/__tests__/TS/SampleSet_test.ts @@ -1,5 +1,5 @@ import { Distribution } from "../../src/js/index"; -// import { expectErrorToBeBounded, failDefault } from "./TestHelpers"; +import { expectErrorToBeBounded, failDefault } from "./TestHelpers"; import * as fc from "fast-check"; // Beware: float64Array makes it appear in an infinite loop. @@ -14,22 +14,22 @@ let arrayGen = () => describe("cumulative density function", () => { let n = 10000; - // // We should obtain the math here. - // test("'s codomain is bounded above", () => { - // fc.assert( - // fc.property(arrayGen(), fc.float(), (xs_, x) => { - // let xs = Array.from(xs_); - // // Should compute with squiggle strings once interpreter has `sample` - // let dist = new Distribution( - // { tag: "SampleSet", value: xs }, - // { sampleCount: n, xyPointLength: 100 } - // ); - // let cdfValue = dist.cdf(x).value; - // let epsilon = 5e-7 - // expect(cdfValue).toBeLessThanOrEqual(1 + epsilon) - // }) - // ); - // }) + // We should fix this. + test.skip("'s codomain is bounded above", () => { + fc.assert( + fc.property(arrayGen(), fc.float(), (xs_, x) => { + let xs = Array.from(xs_); + // Should compute with squiggle strings once interpreter has `sample` + let dist = new Distribution( + { tag: "SampleSet", value: xs }, + { sampleCount: n, xyPointLength: 100 } + ); + let cdfValue = dist.cdf(x).value; + let epsilon = 5e-7; + expect(cdfValue).toBeLessThanOrEqual(1 + epsilon); + }) + ); + }); test("'s codomain is bounded below", () => { fc.assert( @@ -69,50 +69,50 @@ describe("cumulative density function", () => { }); // I may simply be mistaken about the math here. - // test("at the lowest number in the distribution is within epsilon of 0", () => { - // fc.assert( - // fc.property(arrayGen(), (xs_) => { - // let xs = Array.from(xs_); - // let min = Math.min(...xs); - // // Should compute with squiggle strings once interpreter has `sample` - // let dist = new Distribution( - // { tag: "SampleSet", value: xs }, - // { sampleCount: n, xyPointLength: 100 } - // ); - // let cdfValue = dist.cdf(min).value; - // let max = Math.max(...xs); - // let epsilon = 5e-3; - // if (max - min < epsilon) { - // expect(cdfValue).toBeGreaterThan(4 * epsilon); - // } else { - // expect(cdfValue).toBeLessThan(4 * epsilon); - // } - // }) - // ); - // }); + test.skip("at the lowest number in the distribution is within epsilon of 0", () => { + fc.assert( + fc.property(arrayGen(), (xs_) => { + let xs = Array.from(xs_); + let min = Math.min(...xs); + // Should compute with squiggle strings once interpreter has `sample` + let dist = new Distribution( + { tag: "SampleSet", value: xs }, + { sampleCount: n, xyPointLength: 100 } + ); + let cdfValue = dist.cdf(min).value; + let max = Math.max(...xs); + let epsilon = 5e-3; + if (max - min < epsilon) { + expect(cdfValue).toBeGreaterThan(4 * epsilon); + } else { + expect(cdfValue).toBeLessThan(4 * epsilon); + } + }) + ); + }); // I believe this is true, but due to bugs can't get the test to pass. - // test("is <= 1 everywhere with equality when x is higher than the max", () => { - // fc.assert( - // fc.property(arrayGen(), fc.float(), (xs_, x) => { - // let xs = Array.from(xs_); - // let dist = new Distribution( - // { tag: "SampleSet", value: xs }, - // { sampleCount: n, xyPointLength: 100 } - // ); - // let cdfValue = dist.cdf(x).value; - // let max = Math.max(...xs) - // if (x > max) { - // let epsilon = (x - max) / x - // expect(cdfValue).toBeGreaterThan(1 * (1 - epsilon)); - // } else if (typeof cdfValue == "number") { - // expect(Math.round(1e5 * cdfValue) / 1e5).toBeLessThanOrEqual(1); - // } else { - // failDefault() - // } - // }) - // ); - // }); + test.skip("is <= 1 everywhere with equality when x is higher than the max", () => { + fc.assert( + fc.property(arrayGen(), fc.float(), (xs_, x) => { + let xs = Array.from(xs_); + let dist = new Distribution( + { tag: "SampleSet", value: xs }, + { sampleCount: n, xyPointLength: 100 } + ); + let cdfValue = dist.cdf(x).value; + let max = Math.max(...xs); + if (x > max) { + let epsilon = (x - max) / x; + expect(cdfValue).toBeGreaterThan(1 * (1 - epsilon)); + } else if (typeof cdfValue == "number") { + expect(Math.round(1e5 * cdfValue) / 1e5).toBeLessThanOrEqual(1); + } else { + failDefault(); + } + }) + ); + }); test("is non-negative everywhere with zero when x is lower than the min", () => { fc.assert( @@ -133,76 +133,86 @@ describe("cumulative density function", () => { }); }); -// // I no longer believe this is true. -// describe("probability density function", () => { -// let n = 1000; -// -// test("assigns to the max at most the weight of the mean", () => { -// fc.assert( -// fc.property(arrayGen(), (xs_) => { -// let xs = Array.from(xs_); -// let max = Math.max(...xs); -// let mean = xs.reduce((a, b) => a + b, 0.0) / ys.length; -// // Should be from squiggleString once interpreter exposes sampleset -// let dist = new Distribution( -// { tag: "SampleSet", value: xs }, -// { sampleCount: n, xyPointLength: 100 } -// ); -// let pdfValueMean = dist.pdf(mean).value; -// let pdfValueMax = dist.pdf(max).value; -// if (typeof pdfValueMean == "number" && typeof pdfValueMax == "number") { -// expect(pdfValueMax).toBeLessThanOrEqual(pdfValueMean); -// } else { -// expect(pdfValueMax).toEqual(pdfValueMean); -// } -// }) -// ); -// }); -// }); +// I no longer believe this is true. +describe("probability density function", () => { + let n = 1000; -// This should be true, but I can't get it to work. -// describe("mean is mean", () => { -// test("when sampling twice as widely as the input", () => { -// fc.assert( -// fc.property( -// fc.float64Array({ minLength: 10, maxLength: 100000 }), -// (xs_) => { -// let xs = Array.from(xs_); -// let n = xs.length; -// let dist = new Distribution( -// { tag: "SampleSet", value: xs }, -// { sampleCount: 2 * n, xyPointLength: 4 * n } -// ); -// let mean = dist.mean() -// if (typeof mean.value == "number") { -// expectErrorToBeBounded(mean.value, xs.reduce((a, b) => a + b, 0.0) / n, 5e-1, 1) -// } else { -// failDefault() -// } -// } -// ) -// ); -// }); -// -// test("when sampling half as widely as the input", () => { -// fc.assert( -// fc.property( -// fc.float64Array({ minLength: 10, maxLength: 100000 }), -// (xs_) => { -// let xs = Array.from(xs_); -// let n = xs.length; -// let dist = new Distribution( -// { tag: "SampleSet", value: xs }, -// { sampleCount: Math.floor(n / 2), xyPointLength: 4 * n } -// ); -// let mean = dist.mean() -// if (typeof mean.value == "number") { -// expectErrorToBeBounded(mean.value, xs.reduce((a, b) => a + b, 0.0) / n, 5e-1, 1) -// } else { -// failDefault() -// } -// } -// ) -// ); -// }); -// }); + test.skip("assigns to the max at most the weight of the mean", () => { + fc.assert( + fc.property(arrayGen(), (xs_) => { + let xs = Array.from(xs_); + let max = Math.max(...xs); + let mean = xs.reduce((a, b) => a + b, 0.0) / xs.length; + // Should be from squiggleString once interpreter exposes sampleset + let dist = new Distribution( + { tag: "SampleSet", value: xs }, + { sampleCount: n, xyPointLength: 100 } + ); + let pdfValueMean = dist.pdf(mean).value; + let pdfValueMax = dist.pdf(max).value; + if (typeof pdfValueMean == "number" && typeof pdfValueMax == "number") { + expect(pdfValueMax).toBeLessThanOrEqual(pdfValueMean); + } else { + expect(pdfValueMax).toEqual(pdfValueMean); + } + }) + ); + }); +}); + +// // This should be true, but I can't get it to work. +describe("mean is mean", () => { + test.skip("when sampling twice as widely as the input", () => { + fc.assert( + fc.property( + fc.float64Array({ minLength: 10, maxLength: 100000 }), + (xs_) => { + let xs = Array.from(xs_); + let n = xs.length; + let dist = new Distribution( + { tag: "SampleSet", value: xs }, + { sampleCount: 2 * n, xyPointLength: 4 * n } + ); + let mean = dist.mean(); + if (typeof mean.value == "number") { + expectErrorToBeBounded( + mean.value, + xs.reduce((a, b) => a + b, 0.0) / n, + 5e-1, + 1 + ); + } else { + failDefault(); + } + } + ) + ); + }); + + test.skip("when sampling half as widely as the input", () => { + fc.assert( + fc.property( + fc.float64Array({ minLength: 10, maxLength: 100000 }), + (xs_) => { + let xs = Array.from(xs_); + let n = xs.length; + let dist = new Distribution( + { tag: "SampleSet", value: xs }, + { sampleCount: Math.floor(n / 2), xyPointLength: 4 * n } + ); + let mean = dist.mean(); + if (typeof mean.value == "number") { + expectErrorToBeBounded( + mean.value, + xs.reduce((a, b) => a + b, 0.0) / n, + 5e-1, + 1 + ); + } else { + failDefault(); + } + } + ) + ); + }); +}); diff --git a/packages/squiggle-lang/__tests__/TS/TestHelpers.ts b/packages/squiggle-lang/__tests__/TS/TestHelpers.ts index 3d9683c1..3d4153ef 100644 --- a/packages/squiggle-lang/__tests__/TS/TestHelpers.ts +++ b/packages/squiggle-lang/__tests__/TS/TestHelpers.ts @@ -7,11 +7,6 @@ import { // result, } from "../../src/js/index"; -export function testRunR(x: string): any { - //: result => { - return run(x, { sampleCount: 1000, xyPointLength: 100 }); -} - export function testRun(x: string): squiggleExpression { let squiggleResult = run(x, { sampleCount: 1000, xyPointLength: 100 }); // return squiggleResult.value