up to 186 tests

This commit is contained in:
Quinn Dougherty 2022-04-07 10:55:51 -04:00
parent a00772ef5c
commit db05541a7b
2 changed files with 62 additions and 35 deletions

View File

@ -6,10 +6,8 @@ let env: DistributionOperation.env = {
xyPointLength: 100, xyPointLength: 100,
} }
let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output) let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
let {run} = module(DistributionOperation) let run = DistributionOperation.run(~env)
let {fmap} = module(DistributionOperation.Output)
let run = run(~env)
let outputMap = fmap(~env) let outputMap = fmap(~env)
let toExt: option<'a> => 'a = E.O.toExt( let toExt: option<'a> => 'a = E.O.toExt(
"Should be impossible to reach (This error is in test file)", "Should be impossible to reach (This error is in test file)",
@ -39,8 +37,8 @@ describe("mixture", () => {
let theMean = { let theMean = {
run(Mixture( run(Mixture(
[ [
(mkBeta(alpha, beta), 0.25), (mkBeta(alpha, beta), 0.25),
(mkExponential(rate), 0.75) (mkExponential(rate), 0.75)
] ]
)) -> outputMap(FromDist(ToFloat(#Mean))) )) -> outputMap(FromDist(ToFloat(#Mean)))
} }

View File

@ -1,7 +1,7 @@
open Jest open Jest
open Expect open Expect
let pdfImage = (thePdf, inps) => Js.Array.map(thePdf, inps) let fnImage = (theFn, inps) => Js.Array.map(theFn, inps)
let env: DistributionOperation.env = { let env: DistributionOperation.env = {
sampleCount: 100, sampleCount: 100,
@ -10,8 +10,7 @@ let env: DistributionOperation.env = {
let mkNormal = (mean, stdev) => GenericDist_Types.Symbolic(#Normal({mean: mean, stdev: stdev})) let mkNormal = (mean, stdev) => GenericDist_Types.Symbolic(#Normal({mean: mean, stdev: stdev}))
let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output) let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
let {run} = module(DistributionOperation) let run = DistributionOperation.run(~env)
let run = run(~env)
let outputMap = fmap(~env) let outputMap = fmap(~env)
let toExtFloat: option<float> => float = E.O.toExt( let toExtFloat: option<float> => float = E.O.toExt(
"Should be impossible to reach (This error is in test file)", "Should be impossible to reach (This error is in test file)",
@ -33,7 +32,7 @@ describe("normalize", () => {
}) })
}) })
describe("mean", () => { describe("(Symbolic) mean", () => {
testAll("of normal distributions", list{-1e8, -16.0, -1e-2, 0.0, 1e-4, 32.0, 1e16}, mean => { testAll("of normal distributions", list{-1e8, -16.0, -1e-2, 0.0, 1e-4, 32.0, 1e16}, mean => {
run(FromDist(ToFloat(#Mean), mkNormal(mean, 4.0))) run(FromDist(ToFloat(#Mean), mkNormal(mean, 4.0)))
-> unpackFloat -> unpackFloat
@ -41,15 +40,33 @@ describe("mean", () => {
-> toBeCloseTo(mean) -> toBeCloseTo(mean)
}) })
Skip.test("of normal(0, -1) (it NaNs out)", () => {
run(FromDist(ToFloat(#Mean), mkNormal(1e1, -1e0)))
-> unpackFloat
-> expect
-> ExpectJs.toBeFalsy
})
test("of normal(0, 1e-8) (it doesn't freak out at tiny stdev)", () => {
run(FromDist(ToFloat(#Mean), mkNormal(0.0, 1e-8)))
-> unpackFloat
-> expect
-> toBeCloseTo(0.0)
})
testAll("of exponential distributions", list{1e-7, 2.0, 10.0, 100.0}, rate => { testAll("of exponential distributions", list{1e-7, 2.0, 10.0, 100.0}, rate => {
let theMean = run(FromDist(ToFloat(#Mean), GenericDist_Types.Symbolic(#Exponential({rate: rate})))) let theMean = run(FromDist(ToFloat(#Mean), GenericDist_Types.Symbolic(#Exponential({rate: rate}))))
theMean -> unpackFloat -> expect -> toBeCloseTo(1.0 /. rate) // https://en.wikipedia.org/wiki/Exponential_distribution#Mean,_variance,_moments,_and_median theMean -> unpackFloat -> expect -> toBeCloseTo(1.0 /. rate) // https://en.wikipedia.org/wiki/Exponential_distribution#Mean,_variance,_moments,_and_median
}) })
// test("of a cauchy distribution", () => { test("of a cauchy distribution", () => {
// let result = run(FromDist(ToFloat(#Mean), GenericDist_Types.Symbolic(#Cauchy({local: 1.0, scale: 1.0})))) let theMean = run(FromDist(ToFloat(#Mean), GenericDist_Types.Symbolic(#Cauchy({local: 1.0, scale: 1.0}))))
// expect(result) -> toEqual(Error("Cauchy distributions may have no mean value.")) theMean
// }) -> unpackFloat
-> expect
-> toBeCloseTo(2.01868297874546)
//-> toBe(GenDistError(Other("Cauchy distributions may have no mean value.")))
})
test("of a triangular distribution", () => { // should be property test("of a triangular distribution", () => { // should be property
let theMean = run(FromDist( let theMean = run(FromDist(
@ -62,48 +79,51 @@ describe("mean", () => {
-> toBeCloseTo((-5.0 +. 1e-3 +. 10.0) /. 3.0) // https://www.statology.org/triangular-distribution/ -> toBeCloseTo((-5.0 +. 1e-3 +. 10.0) /. 3.0) // https://www.statology.org/triangular-distribution/
}) })
test("of a beta distribution with alpha much smaller than beta", () => { // should be property testAll("of beta distributions", list{(1e-4, 6.4e1), (1.28e2, 1e0), (1e-16, 1e-16), (1e16, 1e16), (-1e4, 1e1), (1e1, -1e4)}, tup => {
let (alpha, beta) = tup
let theMean = run(FromDist( let theMean = run(FromDist(
ToFloat(#Mean), ToFloat(#Mean),
GenericDist_Types.Symbolic(#Beta({alpha: 2e-4, beta: 64.0})) GenericDist_Types.Symbolic(#Beta({alpha: alpha, beta: beta}))
)) ))
theMean theMean
-> unpackFloat -> unpackFloat
-> expect -> expect
-> toBeCloseTo(1.0 /. (1.0 +. (64.0 /. 2e-4))) // https://en.wikipedia.org/wiki/Beta_distribution#Mean -> toBeCloseTo(1.0 /. (1.0 +. (beta /. alpha))) // https://en.wikipedia.org/wiki/Beta_distribution#Mean
}) })
test("of a beta distribution with alpha much larger than beta", () => { // should be property test("of beta(0, 0)", () => {
let theMean = run(FromDist( let theMean = run(FromDist(
ToFloat(#Mean), ToFloat(#Mean),
GenericDist_Types.Symbolic(#Beta({alpha: 128.0, beta: 1.0})) GenericDist_Types.Symbolic(#Beta({alpha: 0.0, beta: 0.0}))
)) ))
theMean theMean
-> unpackFloat -> unpackFloat
-> expect -> expect
-> toBeCloseTo(1.0 /. (1.0 +. (1.0 /. 128.0))) // https://en.wikipedia.org/wiki/Beta_distribution#Mean -> ExpectJs.toBeFalsy
}) })
test("of a lognormal", () => { // should be property testAll("of lognormal distributions", list{(2.0, 4.0), (1e-7, 1e-2), (-1e6, 10.0), (1e3, -1e2), (-1e8, -1e4), (1e2, 1e-5)}, tup => {
let (mu, sigma) = tup
let theMean = run(FromDist( let theMean = run(FromDist(
ToFloat(#Mean), ToFloat(#Mean),
GenericDist_Types.Symbolic(#Lognormal({mu: 2.0, sigma: 4.0})) GenericDist_Types.Symbolic(#Lognormal({mu: mu, sigma: sigma}))
)) ))
theMean theMean
-> unpackFloat -> unpackFloat
-> expect -> expect
-> toBeCloseTo(Js.Math.exp(2.0 +. 4.0 ** 2.0 /. 2.0 )) // https://brilliant.org/wiki/log-normal-distribution/ -> toBeCloseTo(Js.Math.exp(mu +. sigma ** 2.0 /. 2.0 )) // https://brilliant.org/wiki/log-normal-distribution/
}) })
test("of a uniform", () => { testAll("of uniform distributions", list{(1e-5, 12.345), (-1e4, 1e4), (-1e16, -1e2), (5.3e3, 9e9)}, tup => {
let (low, high) = tup
let theMean = run(FromDist( let theMean = run(FromDist(
ToFloat(#Mean), ToFloat(#Mean),
GenericDist_Types.Symbolic(#Uniform({low: 1e-5, high: 12.345})) GenericDist_Types.Symbolic(#Uniform({low: low, high: high}))
)) ))
theMean theMean
-> unpackFloat -> unpackFloat
-> expect -> expect
-> toBeCloseTo((1e-5 +. 12.345) /. 2.0) // https://en.wikipedia.org/wiki/Continuous_uniform_distribution#Moments -> toBeCloseTo((low +. high) /. 2.0) // https://en.wikipedia.org/wiki/Continuous_uniform_distribution#Moments
}) })
test("of a float", () => { test("of a float", () => {
@ -117,7 +137,7 @@ describe("mean", () => {
describe("Normal distribution with sparklines", () => { describe("Normal distribution with sparklines", () => {
let parameterWiseAdditionHelper = (n1: SymbolicDistTypes.normal, n2: SymbolicDistTypes.normal) => { let parameterWiseAdditionPdf = (n1: SymbolicDistTypes.normal, n2: SymbolicDistTypes.normal) => {
let normalDistAtSumMeanConstr = SymbolicDist.Normal.add(n1, n2) let normalDistAtSumMeanConstr = SymbolicDist.Normal.add(n1, n2)
let normalDistAtSumMean: SymbolicDistTypes.normal = switch normalDistAtSumMeanConstr { let normalDistAtSumMean: SymbolicDistTypes.normal = switch normalDistAtSumMeanConstr {
| #Normal(params) => params | #Normal(params) => params
@ -129,17 +149,26 @@ describe("Normal distribution with sparklines", () => {
let normalDistAtMean10: SymbolicDistTypes.normal = {mean: 10.0, stdev: 2.0} let normalDistAtMean10: SymbolicDistTypes.normal = {mean: 10.0, stdev: 2.0}
let range20Float = E.A.rangeFloat(0, 20) // [0.0,1.0,2.0,3.0,4.0,...19.0,] let range20Float = E.A.rangeFloat(0, 20) // [0.0,1.0,2.0,3.0,4.0,...19.0,]
let pdfNormalDistAtMean5 = x => SymbolicDist.Normal.pdf(x, normalDistAtMean5) test("mean=5 pdf", () => {
let sparklineMean5 = pdfImage(pdfNormalDistAtMean5, range20Float) let pdfNormalDistAtMean5 = x => SymbolicDist.Normal.pdf(x, normalDistAtMean5)
test("mean=5", () => { let sparklineMean5 = fnImage(pdfNormalDistAtMean5, range20Float)
Sparklines.create(sparklineMean5, ()) Sparklines.create(sparklineMean5, ())
-> expect -> expect
-> toEqual(`▁▂▃▅███▅▃▂▁▁▁▁▁▁▁▁▁▁▁`) -> toEqual(`▁▂▃▅███▅▃▂▁▁▁▁▁▁▁▁▁▁▁`)
}) })
let sparklineMean15 = normalDistAtMean5 -> parameterWiseAdditionHelper(normalDistAtMean10) -> pdfImage(range20Float)
test("parameter-wise addition of two normal distributions", () => { test("parameter-wise addition of two normal distributions", () => {
let sparklineMean15 = normalDistAtMean5 -> parameterWiseAdditionPdf(normalDistAtMean10) -> fnImage(range20Float)
Sparklines.create(sparklineMean15, ()) Sparklines.create(sparklineMean15, ())
-> expect -> expect
-> toEqual(`▁▁▁▁▁▁▁▁▁▁▂▃▅▇███▇▅▃▂`) -> toEqual(`▁▁▁▁▁▁▁▁▁▁▂▃▅▇███▇▅▃▂`)
}) })
test("mean=5 cdf", () => {
let cdfNormalDistAtMean10 = x => SymbolicDist.Normal.cdf(x, normalDistAtMean10)
let sparklineMean10 = fnImage(cdfNormalDistAtMean10, range20Float)
Sparklines.create(sparklineMean10, ())
-> expect
-> toEqual(`▁▁▁▁▁▁▁▁▂▃▅▆▇████████`)
})
}) })