squiggle/packages/squiggle-lang/__tests__/Distributions/DistributionOperation_test.res

102 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2022-03-28 19:14:39 +00:00
open Jest
open Expect
2022-07-13 16:37:39 +00:00
let env: GenericDist.env = {
2022-03-28 19:14:39 +00:00
sampleCount: 100,
xyPointLength: 100,
}
let {
normalDist5,
normalDist10,
normalDist20,
normalDist,
uniformDist,
betaDist,
lognormalDist,
cauchyDist,
triangularDist,
exponentialDist,
} = module(GenericDist_Fixtures)
2022-03-28 19:14:39 +00:00
let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
let {run} = module(DistributionOperation)
let run = run(~env)
let outputMap = fmap(~env)
2022-03-28 19:14:39 +00:00
let toExt: option<'a> => 'a = E.O.toExt(
"Should be impossible to reach (This error is in test file)",
)
describe("sparkline", () => {
let runTest = (
name: string,
dist: DistributionTypes.genericDist,
expected: DistributionOperation.outputType,
) => {
test(name, () => {
let result = DistributionOperation.run(~env, FromDist(#ToString(ToSparkline(20)), dist))
expect(result)->toEqual(expected)
})
}
runTest(
"normal",
normalDist,
String(`▁▁▁▁▁▂▄▆▇██▇▆▄▂▁▁▁▁▁`),
)
runTest(
"uniform",
uniformDist,
String(`████████████████████`),
)
runTest("beta", betaDist, String(`▁▄▇████▇▆▅▄▃▃▂▁▁▁▁▁▁`))
runTest(
"lognormal",
lognormalDist,
String(`▁█▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁`),
)
runTest(
"cauchy",
cauchyDist,
String(`▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁`),
)
runTest(
"triangular",
triangularDist,
String(`▁▁▂▃▄▅▆▇████▇▆▅▄▃▂▁▁`),
)
runTest(
"exponential",
exponentialDist,
String(`█▅▄▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁`),
)
})
2022-03-28 19:14:39 +00:00
describe("toPointSet", () => {
test("on symbolic normal distribution", () => {
let result =
run(FromDist(#ToDist(ToPointSet), normalDist5))
->outputMap(FromDist(#ToFloat(#Mean)))
->toFloat
->toExt
2022-04-07 03:01:17 +00:00
expect(result)->toBeSoCloseTo(5.0, ~digits=0)
2022-03-28 19:14:39 +00:00
})
2022-04-07 03:01:17 +00:00
test("on sample set", () => {
2022-03-28 19:14:39 +00:00
let result =
run(FromDist(#ToDist(ToPointSet), normalDist5))
->outputMap(FromDist(#ToDist(ToSampleSet(1000))))
->outputMap(FromDist(#ToDist(ToPointSet)))
->outputMap(FromDist(#ToFloat(#Mean)))
->toFloat
->toExt
2022-04-07 03:01:17 +00:00
expect(result)->toBeSoCloseTo(5.0, ~digits=-1)
2022-03-28 19:14:39 +00:00
})
})