squiggle/packages/squiggle-lang/__tests__/TestHelpers.res

75 lines
3.5 KiB
Plaintext
Raw Normal View History

2022-04-07 22:38:49 +00:00
open Jest
open Expect
2022-04-14 00:58:16 +00:00
/*
This encodes the expression for percent error
The test says "the percent error of received against expected is bounded by epsilon"
However, the semantics are degraded by catching some numerical instability:
when expected is too small, the return of this function might blow up to infinity.
So we capture that by taking the max of abs(expected) against a 1.
A sanity check of this function would be welcome, in general it is a better way of approaching
squiggle-lang tests than toBeSoCloseTo.
*/
2022-04-13 23:17:49 +00:00
let expectErrorToBeBounded = (received, expected, ~epsilon) => {
let distance = Js.Math.abs_float(received -. expected)
2022-04-13 23:58:36 +00:00
let expectedAbs = Js.Math.abs_float(expected)
2022-04-14 00:58:16 +00:00
let normalizingDenom = Js.Math.max_float(expectedAbs, 1e0)
2022-04-13 23:58:36 +00:00
let error = distance /. normalizingDenom
2022-04-13 23:18:08 +00:00
error->expect->toBeLessThan(epsilon)
2022-04-13 23:17:49 +00:00
}
2022-04-07 22:38:49 +00:00
let makeTest = (~only=false, str, item1, item2) =>
only
2022-04-12 23:59:40 +00:00
? Only.test(str, () => expect(item1)->toEqual(item2))
: test(str, () => expect(item1)->toEqual(item2))
2022-04-07 22:38:49 +00:00
let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
let fnImage = (theFn, inps) => Js.Array.map(theFn, inps)
let env: DistributionOperation.env = {
percentile: 0.9998,
2022-04-27 17:59:33 +00:00
sampleCount: MagicNumbers.Environment.defaultSampleCount,
xyPointLength: MagicNumbers.Environment.defaultXYPointLength,
2022-04-07 22:38:49 +00:00
}
let run = DistributionOperation.run(~env)
let outputMap = fmap(~env)
let unreachableInTestFileMessage = "Should be impossible to reach (This error is in test file)"
let toExtFloat: option<float> => float = E.O.toExt(unreachableInTestFileMessage)
2022-04-13 05:02:53 +00:00
let toExtDist: option<DistributionTypes.genericDist> => DistributionTypes.genericDist = E.O.toExt(
unreachableInTestFileMessage,
)
2022-04-07 22:38:49 +00:00
// let toExt: option<'a> => 'a = E.O.toExt(unreachableInTestFileMessage)
2022-04-13 05:02:53 +00:00
let unpackFloat = x => x->toFloat->toExtFloat
let unpackDist = y => y->toDist->toExtDist
2022-04-12 21:06:53 +00:00
let mkNormal = (mean, stdev) => DistributionTypes.Symbolic(#Normal({mean: mean, stdev: stdev}))
let mkBeta = (alpha, beta) => DistributionTypes.Symbolic(#Beta({alpha: alpha, beta: beta}))
let mkExponential = rate => DistributionTypes.Symbolic(#Exponential({rate: rate}))
2022-04-13 05:02:53 +00:00
let mkUniform = (low, high) => DistributionTypes.Symbolic(#Uniform({low: low, high: high}))
2022-04-12 21:06:53 +00:00
let mkCauchy = (local, scale) => DistributionTypes.Symbolic(#Cauchy({local: local, scale: scale}))
let mkLognormal = (mu, sigma) => DistributionTypes.Symbolic(#Lognormal({mu: mu, sigma: sigma}))
let mkDelta = x => DistributionTypes.Symbolic(#Float(x))
2022-04-12 21:06:53 +00:00
let normalMake = SymbolicDist.Normal.make
let betaMake = SymbolicDist.Beta.make
let exponentialMake = SymbolicDist.Exponential.make
let uniformMake = SymbolicDist.Uniform.make
let cauchyMake = SymbolicDist.Cauchy.make
let lognormalMake = SymbolicDist.Lognormal.make
2022-04-13 05:02:53 +00:00
let triangularMake = SymbolicDist.Triangular.make
let floatMake = SymbolicDist.Float.make
2022-05-03 14:15:48 +00:00
let fmapGenDist = symbdistres => E.R.fmap(s => DistributionTypes.Symbolic(s), symbdistres)
let normalMakeR = (mean, stdev) => fmapGenDist(SymbolicDist.Normal.make(mean, stdev))
let betaMakeR = (alpha, beta) => fmapGenDist(SymbolicDist.Beta.make(alpha, beta))
let exponentialMakeR = rate => fmapGenDist(SymbolicDist.Exponential.make(rate))
let uniformMakeR = (low, high) => fmapGenDist(SymbolicDist.Uniform.make(low, high))
let cauchyMakeR = (local, rate) => fmapGenDist(SymbolicDist.Cauchy.make(local, rate))
let lognormalMakeR = (mu, sigma) => fmapGenDist(SymbolicDist.Lognormal.make(mu, sigma))
let triangularMakeR = (low, mode, high) =>
2022-05-03 14:15:48 +00:00
fmapGenDist(SymbolicDist.Triangular.make(low, mode, high))