squiggle/packages/squiggle-lang/__tests__/TS/SampleSet_test.ts

209 lines
6.8 KiB
TypeScript
Raw Normal View History

2022-04-20 03:42:24 +00:00
import { Distribution } from "../../src/js/index";
2022-04-20 22:48:04 +00:00
// import { expectErrorToBeBounded, failDefault } from "./TestHelpers";
import * as fc from "fast-check";
// Beware: float64Array makes it appear in an infinite loop.
let arrayGen = () =>
fc.float32Array({
minLength: 10,
maxLength: 10000,
noDefaultInfinity: true,
noNaN: true,
});
2022-04-20 22:48:04 +00:00
describe("cumulative density function", () => {
let n = 10000;
2022-04-20 22:48:04 +00:00
// // 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)
// })
// );
// })
test("'s codomain is bounded below", () => {
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;
expect(cdfValue).toBeGreaterThanOrEqual(0);
})
);
});
test("at the highest number in the sample is close to 1", () => {
fc.assert(
2022-04-20 22:48:04 +00:00
fc.property(arrayGen(), (xs_) => {
let xs = Array.from(xs_);
let max = Math.max(...xs);
// Should compute with squiggle strings once interpreter has `sample`
let dist = new Distribution(
2022-04-20 22:48:04 +00:00
{ tag: "SampleSet", value: xs },
{ sampleCount: n, xyPointLength: 100 }
);
let cdfValue = dist.cdf(max).value;
2022-04-20 22:48:04 +00:00
let min = Math.min(...xs);
let epsilon = 5e-3;
if (max - min < epsilon) {
expect(cdfValue).toBeLessThan(1 - epsilon);
} else {
expect(dist.cdf(max).value).toBeGreaterThan(1 - epsilon);
}
})
);
});
// I may simply be mistaken about the math here.
// test("at the lowest number in the distribution is within epsilon of 0", () => {
// fc.assert(
2022-04-20 22:48:04 +00:00
// 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(
2022-04-20 22:48:04 +00:00
// { tag: "SampleSet", value: xs },
// { sampleCount: n, xyPointLength: 100 }
// );
// let cdfValue = dist.cdf(min).value;
2022-04-20 22:48:04 +00:00
// 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(
2022-04-20 22:48:04 +00:00
// fc.property(arrayGen(), fc.float(), (xs_, x) => {
// let xs = Array.from(xs_);
// let dist = new Distribution(
2022-04-20 22:48:04 +00:00
// { tag: "SampleSet", value: xs },
// { sampleCount: n, xyPointLength: 100 }
// );
// let cdfValue = dist.cdf(x).value;
2022-04-20 22:48:04 +00:00
// 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()
// }
// })
// );
// });
2022-04-20 22:48:04 +00:00
test("is non-negative everywhere with zero when x is lower than the min", () => {
fc.assert(
2022-04-20 22:48:04 +00:00
fc.property(arrayGen(), fc.float(), (xs_, x) => {
let xs = Array.from(xs_);
let dist = new Distribution(
2022-04-20 22:48:04 +00:00
{ tag: "SampleSet", value: xs },
{ sampleCount: n, xyPointLength: 100 }
);
let cdfValue = dist.cdf(x).value;
2022-04-20 22:48:04 +00:00
if (x < Math.min(...xs)) {
expect(cdfValue).toEqual(0);
} else {
expect(cdfValue).toBeGreaterThan(0);
}
})
);
});
});
// // I no longer believe this is true.
2022-04-20 22:48:04 +00:00
// describe("probability density function", () => {
// let n = 1000;
//
// test("assigns to the max at most the weight of the mean", () => {
// fc.assert(
2022-04-20 22:48:04 +00:00
// 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(
2022-04-20 22:48:04 +00:00
// { 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);
// }
// })
// );
// });
// });
2022-04-20 04:50:46 +00:00
// This should be true, but I can't get it to work.
2022-04-20 22:48:04 +00:00
// describe("mean is mean", () => {
// test("when sampling twice as widely as the input", () => {
2022-04-20 04:50:46 +00:00
// fc.assert(
// fc.property(
// fc.float64Array({ minLength: 10, maxLength: 100000 }),
2022-04-20 22:48:04 +00:00
// (xs_) => {
// let xs = Array.from(xs_);
// let n = xs.length;
2022-04-20 04:50:46 +00:00
// let dist = new Distribution(
2022-04-20 22:48:04 +00:00
// { tag: "SampleSet", value: xs },
2022-04-20 04:50:46 +00:00
// { sampleCount: 2 * n, xyPointLength: 4 * n }
// );
// let mean = dist.mean()
// if (typeof mean.value == "number") {
2022-04-20 22:48:04 +00:00
// expectErrorToBeBounded(mean.value, xs.reduce((a, b) => a + b, 0.0) / n, 5e-1, 1)
2022-04-20 04:50:46 +00:00
// } else {
// failDefault()
// }
// }
// )
// );
// });
//
2022-04-20 22:48:04 +00:00
// test("when sampling half as widely as the input", () => {
2022-04-20 04:50:46 +00:00
// fc.assert(
// fc.property(
// fc.float64Array({ minLength: 10, maxLength: 100000 }),
2022-04-20 22:48:04 +00:00
// (xs_) => {
// let xs = Array.from(xs_);
// let n = xs.length;
2022-04-20 04:50:46 +00:00
// let dist = new Distribution(
2022-04-20 22:48:04 +00:00
// { tag: "SampleSet", value: xs },
2022-04-20 04:50:46 +00:00
// { sampleCount: Math.floor(n / 2), xyPointLength: 4 * n }
// );
// let mean = dist.mean()
// if (typeof mean.value == "number") {
2022-04-20 22:48:04 +00:00
// expectErrorToBeBounded(mean.value, xs.reduce((a, b) => a + b, 0.0) / n, 5e-1, 1)
2022-04-20 04:50:46 +00:00
// } else {
// failDefault()
// }
// }
// )
// );
// });
// });