squiggle/packages/squiggle-lang/__tests__/GenericDist/GenericOperation__Test.res

77 lines
2.3 KiB
Plaintext
Raw Normal View History

2022-03-28 19:14:39 +00:00
open Jest
open Expect
let env: DistributionOperation.env = {
2022-03-28 19:14:39 +00:00
sampleCount: 100,
xyPointLength: 100,
}
let normalDist: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 5.0, stdev: 2.0}))
let normalDist10: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 10.0, stdev: 2.0}))
let normalDist20: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 20.0, stdev: 2.0}))
let uniformDist: GenericDist_Types.genericDist = Symbolic(#Uniform({low: 9.0, high: 10.0}))
2022-03-28 19:14:39 +00:00
let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output)
let {run} = module(DistributionOperation)
let {fmap} = module(DistributionOperation.Output)
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("normalize", () => {
test("has no impact on normal dist", () => {
let result = run(FromDist(ToDist(Normalize), normalDist))
expect(result)->toEqual(Dist(normalDist))
2022-03-28 19:14:39 +00:00
})
})
describe("mean", () => {
test("for a normal distribution", () => {
let result = DistributionOperation.run(~env, FromDist(ToFloat(#Mean), normalDist))
expect(result)->toEqual(Float(5.0))
2022-03-28 19:14:39 +00:00
})
})
describe("mixture", () => {
test("on two normal distributions", () => {
let result =
run(Mixture([(normalDist10, 0.5), (normalDist20, 0.5)]))
->outputMap(FromDist(ToFloat(#Mean)))
->toFloat
->toExt
2022-03-28 19:14:39 +00:00
expect(result)->toBeCloseTo(15.28)
})
})
describe("toPointSet", () => {
test("on symbolic normal distribution", () => {
let result =
run(FromDist(ToDist(ToPointSet), normalDist))
->outputMap(FromDist(ToFloat(#Mean)))
->toFloat
->toExt
2022-03-28 19:14:39 +00:00
expect(result)->toBeCloseTo(5.09)
})
test("on sample set distribution with under 4 points", () => {
let result =
run(FromDist(ToDist(ToPointSet), SampleSet([0.0, 1.0, 2.0, 3.0])))->outputMap(
FromDist(ToFloat(#Mean)),
2022-03-28 19:14:39 +00:00
)
expect(result)->toEqual(GenDistError(Other("Converting sampleSet to pointSet failed")))
2022-03-28 19:14:39 +00:00
})
2022-03-31 12:37:04 +00:00
Skip.test("on sample set", () => {
2022-03-28 19:14:39 +00:00
let result =
run(FromDist(ToDist(ToPointSet), normalDist))
->outputMap(FromDist(ToDist(ToSampleSet(1000))))
->outputMap(FromDist(ToDist(ToPointSet)))
->outputMap(FromDist(ToFloat(#Mean)))
->toFloat
->toExt
2022-03-28 19:14:39 +00:00
expect(result)->toBeCloseTo(5.09)
})
})