2022-04-12 05:41:36 +00:00
|
|
|
import {
|
|
|
|
run,
|
|
|
|
Distribution,
|
|
|
|
resultMap,
|
|
|
|
squiggleExpression,
|
2022-04-12 06:21:32 +00:00
|
|
|
errorValueToString,
|
2022-04-20 00:52:53 +00:00
|
|
|
} from "../../src/js/index";
|
2022-01-29 23:56:44 +00:00
|
|
|
|
2022-04-11 03:16:31 +00:00
|
|
|
let testRun = (x: string): squiggleExpression => {
|
2022-04-12 05:41:36 +00:00
|
|
|
let result = run(x, { sampleCount: 100, xyPointLength: 100 });
|
|
|
|
expect(result.tag).toEqual("Ok");
|
|
|
|
if (result.tag === "Ok") {
|
|
|
|
return result.value;
|
2022-04-12 06:21:32 +00:00
|
|
|
} else {
|
|
|
|
throw Error(
|
|
|
|
"Expected squiggle expression to evaluate but got error: " +
|
|
|
|
errorValueToString(result.value)
|
|
|
|
);
|
2022-03-22 02:33:28 +00:00
|
|
|
}
|
2022-04-08 23:48:53 +00:00
|
|
|
};
|
2022-03-22 02:33:28 +00:00
|
|
|
|
2022-04-11 01:06:13 +00:00
|
|
|
function Ok<b>(x: b) {
|
2022-04-11 01:08:34 +00:00
|
|
|
return { tag: "Ok", value: x };
|
2022-04-11 01:06:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 02:12:20 +00:00
|
|
|
describe("Simple calculations and results", () => {
|
2022-04-08 23:48:53 +00:00
|
|
|
test("mean(normal(5,2))", () => {
|
|
|
|
expect(testRun("mean(normal(5,2))")).toEqual({
|
2022-04-11 03:16:31 +00:00
|
|
|
tag: "number",
|
2022-04-12 05:41:36 +00:00
|
|
|
value: 5,
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
test("10+10", () => {
|
|
|
|
let foo = testRun("10 + 10");
|
2022-04-11 03:16:31 +00:00
|
|
|
expect(foo).toEqual({ tag: "number", value: 20 });
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-23 02:12:20 +00:00
|
|
|
describe("Log function", () => {
|
2022-04-08 23:48:53 +00:00
|
|
|
test("log(1) = 0", () => {
|
|
|
|
let foo = testRun("log(1)");
|
2022-04-11 03:16:31 +00:00
|
|
|
expect(foo).toEqual({ tag: "number", value: 0 });
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-11 01:06:13 +00:00
|
|
|
describe("Distribution", () => {
|
2022-04-09 01:31:08 +00:00
|
|
|
//It's important that sampleCount is less than 9. If it's more, than that will create randomness
|
2022-04-10 01:56:05 +00:00
|
|
|
//Also, note, the value should be created using makeSampleSetDist() later on.
|
2022-04-09 01:31:08 +00:00
|
|
|
let env = { sampleCount: 8, xyPointLength: 100 };
|
2022-04-26 16:22:31 +00:00
|
|
|
let dist1Samples = [3, 4, 5, 6, 6, 7, 10, 15, 30];
|
|
|
|
let dist1SampleCount = dist1Samples.length;
|
2022-04-11 01:06:13 +00:00
|
|
|
let dist = new Distribution(
|
2022-04-08 23:48:53 +00:00
|
|
|
{ tag: "SampleSet", value: [3, 4, 5, 6, 6, 7, 10, 15, 30] },
|
2022-04-09 01:31:08 +00:00
|
|
|
env
|
|
|
|
);
|
2022-04-11 01:06:13 +00:00
|
|
|
let dist2 = new Distribution(
|
2022-04-09 01:31:08 +00:00
|
|
|
{ tag: "SampleSet", value: [20, 22, 24, 29, 30, 35, 38, 44, 52] },
|
|
|
|
env
|
2022-04-08 23:48:53 +00:00
|
|
|
);
|
2022-04-09 01:31:08 +00:00
|
|
|
|
2022-04-08 23:48:53 +00:00
|
|
|
test("mean", () => {
|
2022-04-26 16:22:31 +00:00
|
|
|
expect(dist.mean().value).toBeCloseTo(8.704375514292865);
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
test("pdf", () => {
|
2022-04-26 16:22:31 +00:00
|
|
|
expect(dist.pdf(5.0).value).toBeCloseTo(0.052007455285386944, 1);
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
test("cdf", () => {
|
2022-04-26 16:22:31 +00:00
|
|
|
expect(dist.cdf(5.0).value).toBeCloseTo(
|
|
|
|
dist1Samples.filter((x) => x <= 5).length / dist1SampleCount
|
|
|
|
);
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
test("inv", () => {
|
2022-04-26 16:22:31 +00:00
|
|
|
expect(dist.inv(0.5).value).toBeCloseTo(6);
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
test("toPointSet", () => {
|
|
|
|
expect(
|
2022-04-11 06:31:54 +00:00
|
|
|
resultMap(dist.toPointSet(), (r: Distribution) => r.toString())
|
2022-04-11 01:06:13 +00:00
|
|
|
).toEqual(Ok("Point Set Distribution"));
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
test("toSparkline", () => {
|
2022-04-26 15:59:07 +00:00
|
|
|
expect(dist.toSparkline(20).value).toEqual("▁▁▃▇█▇▄▂▂▂▁▁▁▁▁▂▂▁▁▁");
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
2022-04-09 01:31:08 +00:00
|
|
|
test("algebraicAdd", () => {
|
|
|
|
expect(
|
2022-04-11 01:08:34 +00:00
|
|
|
resultMap(dist.algebraicAdd(dist2), (r: Distribution) =>
|
|
|
|
r.toSparkline(20)
|
|
|
|
).value
|
2022-04-11 01:06:13 +00:00
|
|
|
).toEqual(Ok("▁▁▂▄▆████▇▆▄▄▃▃▃▂▁▁▁"));
|
2022-04-09 01:31:08 +00:00
|
|
|
});
|
|
|
|
test("pointwiseAdd", () => {
|
|
|
|
expect(
|
2022-04-11 01:08:34 +00:00
|
|
|
resultMap(dist.pointwiseAdd(dist2), (r: Distribution) =>
|
|
|
|
r.toSparkline(20)
|
|
|
|
).value
|
2022-04-26 16:22:31 +00:00
|
|
|
).toEqual(Ok("▁▂▅██▅▅▅▆▆▇▅▄▃▃▂▂▁▁▁"));
|
2022-04-09 01:31:08 +00:00
|
|
|
});
|
2022-02-18 01:50:32 +00:00
|
|
|
});
|