2022-03-28 19:14:39 +00:00
|
|
|
open Jest
|
|
|
|
open Expect
|
|
|
|
|
2022-04-04 15:59:14 +00:00
|
|
|
let env: DistributionOperation.env = {
|
2022-03-28 19:14:39 +00:00
|
|
|
sampleCount: 100,
|
|
|
|
xyPointLength: 100,
|
|
|
|
}
|
|
|
|
|
2022-04-08 12:53:37 +00:00
|
|
|
let {
|
|
|
|
normalDist5,
|
|
|
|
normalDist10,
|
|
|
|
normalDist20,
|
|
|
|
normalDist,
|
|
|
|
uniformDist,
|
|
|
|
betaDist,
|
|
|
|
lognormalDist,
|
|
|
|
cauchyDist,
|
|
|
|
triangularDist,
|
|
|
|
exponentialDist,
|
|
|
|
} = module(GenericDist_Fixtures)
|
2022-04-06 22:57:51 +00:00
|
|
|
let mkNormal = (mean, stdev) => GenericDist_Types.Symbolic(#Normal({mean: mean, stdev: stdev}))
|
2022-03-28 19:14:39 +00:00
|
|
|
|
2022-04-04 15:59:14 +00:00
|
|
|
let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output)
|
|
|
|
let {run} = module(DistributionOperation)
|
|
|
|
let {fmap} = module(DistributionOperation.Output)
|
2022-03-31 18:07:39 +00:00
|
|
|
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)",
|
|
|
|
)
|
|
|
|
|
2022-04-08 12:53:37 +00:00
|
|
|
describe("sparkline", () => {
|
|
|
|
let runTest = (
|
|
|
|
name: string,
|
|
|
|
dist: GenericDist_Types.genericDist,
|
|
|
|
expected: DistributionOperation.outputType,
|
|
|
|
) => {
|
|
|
|
test(name, () => {
|
2022-04-08 13:08:49 +00:00
|
|
|
let result = DistributionOperation.run(~env, FromDist(ToString(ToSparkline(20)), dist))
|
2022-04-08 12:53:37 +00:00
|
|
|
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 =
|
2022-04-06 19:19:27 +00:00
|
|
|
run(FromDist(ToDist(ToPointSet), normalDist5))
|
2022-03-31 18:51:42 +00:00
|
|
|
->outputMap(FromDist(ToFloat(#Mean)))
|
2022-03-29 18:36:54 +00:00
|
|
|
->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
|
|
|
})
|
|
|
|
|
|
|
|
test("on sample set distribution with under 4 points", () => {
|
2022-04-09 22:10:06 +00:00
|
|
|
let sampleSet = SampleSetDist.make([0.0, 1.0, 2.0, 3.0]) -> E.R.toExn;
|
2022-03-28 19:14:39 +00:00
|
|
|
let result =
|
2022-04-09 22:10:06 +00:00
|
|
|
run(FromDist(ToDist(ToPointSet), SampleSet(sampleSet)))->outputMap(
|
2022-03-31 18:51:42 +00:00
|
|
|
FromDist(ToFloat(#Mean)),
|
2022-03-28 19:14:39 +00:00
|
|
|
)
|
2022-03-31 13:27:36 +00:00
|
|
|
expect(result)->toEqual(GenDistError(Other("Converting sampleSet to pointSet failed")))
|
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 =
|
2022-04-06 19:19:27 +00:00
|
|
|
run(FromDist(ToDist(ToPointSet), normalDist5))
|
2022-03-31 18:51:42 +00:00
|
|
|
->outputMap(FromDist(ToDist(ToSampleSet(1000))))
|
|
|
|
->outputMap(FromDist(ToDist(ToPointSet)))
|
|
|
|
->outputMap(FromDist(ToFloat(#Mean)))
|
2022-03-29 18:36:54 +00:00
|
|
|
->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
|
|
|
})
|
|
|
|
})
|