diff --git a/packages/squiggle-lang/__tests__/Distributions/Mixture_test.res b/packages/squiggle-lang/__tests__/Distributions/Mixture_test.res index 1993feb1..537a657f 100644 --- a/packages/squiggle-lang/__tests__/Distributions/Mixture_test.res +++ b/packages/squiggle-lang/__tests__/Distributions/Mixture_test.res @@ -6,10 +6,8 @@ let env: DistributionOperation.env = { xyPointLength: 100, } -let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output) -let {run} = module(DistributionOperation) -let {fmap} = module(DistributionOperation.Output) -let run = run(~env) +let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output) +let run = DistributionOperation.run(~env) let outputMap = fmap(~env) let toExt: option<'a> => 'a = E.O.toExt( "Should be impossible to reach (This error is in test file)", @@ -39,8 +37,8 @@ describe("mixture", () => { let theMean = { run(Mixture( [ - (mkBeta(alpha, beta), 0.25), - (mkExponential(rate), 0.75) + (mkBeta(alpha, beta), 0.25), + (mkExponential(rate), 0.75) ] )) -> outputMap(FromDist(ToFloat(#Mean))) } diff --git a/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res b/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res index 8e48b3df..ae5d17c4 100644 --- a/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res +++ b/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res @@ -1,7 +1,7 @@ open Jest open Expect -let pdfImage = (thePdf, inps) => Js.Array.map(thePdf, inps) +let fnImage = (theFn, inps) => Js.Array.map(theFn, inps) let env: DistributionOperation.env = { sampleCount: 100, @@ -10,8 +10,7 @@ let env: DistributionOperation.env = { let mkNormal = (mean, stdev) => GenericDist_Types.Symbolic(#Normal({mean: mean, stdev: stdev})) let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output) -let {run} = module(DistributionOperation) -let run = run(~env) +let run = DistributionOperation.run(~env) let outputMap = fmap(~env) let toExtFloat: option => float = E.O.toExt( "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 => { run(FromDist(ToFloat(#Mean), mkNormal(mean, 4.0))) -> unpackFloat @@ -41,15 +40,33 @@ describe("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 => { 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 }) -// test("of a cauchy distribution", () => { -// let result = 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.")) -// }) + test("of a cauchy distribution", () => { + let theMean = run(FromDist(ToFloat(#Mean), GenericDist_Types.Symbolic(#Cauchy({local: 1.0, scale: 1.0})))) + theMean + -> unpackFloat + -> expect + -> toBeCloseTo(2.01868297874546) + //-> toBe(GenDistError(Other("Cauchy distributions may have no mean value."))) + }) test("of a triangular distribution", () => { // should be property 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/ }) - 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( ToFloat(#Mean), - GenericDist_Types.Symbolic(#Beta({alpha: 2e-4, beta: 64.0})) + GenericDist_Types.Symbolic(#Beta({alpha: alpha, beta: beta})) )) theMean -> unpackFloat -> 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( ToFloat(#Mean), - GenericDist_Types.Symbolic(#Beta({alpha: 128.0, beta: 1.0})) + GenericDist_Types.Symbolic(#Beta({alpha: 0.0, beta: 0.0})) )) - theMean - -> unpackFloat - -> expect - -> toBeCloseTo(1.0 /. (1.0 +. (1.0 /. 128.0))) // https://en.wikipedia.org/wiki/Beta_distribution#Mean + theMean + -> unpackFloat + -> expect + -> 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( ToFloat(#Mean), - GenericDist_Types.Symbolic(#Lognormal({mu: 2.0, sigma: 4.0})) + GenericDist_Types.Symbolic(#Lognormal({mu: mu, sigma: sigma})) )) theMean -> unpackFloat -> 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( ToFloat(#Mean), - GenericDist_Types.Symbolic(#Uniform({low: 1e-5, high: 12.345})) + GenericDist_Types.Symbolic(#Uniform({low: low, high: high})) )) theMean -> unpackFloat -> 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", () => { @@ -117,7 +137,7 @@ describe("mean", () => { 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 normalDistAtSumMean: SymbolicDistTypes.normal = switch normalDistAtSumMeanConstr { | #Normal(params) => params @@ -129,17 +149,26 @@ describe("Normal distribution with sparklines", () => { 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 pdfNormalDistAtMean5 = x => SymbolicDist.Normal.pdf(x, normalDistAtMean5) - let sparklineMean5 = pdfImage(pdfNormalDistAtMean5, range20Float) - test("mean=5", () => { + test("mean=5 pdf", () => { + let pdfNormalDistAtMean5 = x => SymbolicDist.Normal.pdf(x, normalDistAtMean5) + let sparklineMean5 = fnImage(pdfNormalDistAtMean5, range20Float) Sparklines.create(sparklineMean5, ()) -> expect -> 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, ()) -> expect -> toEqual(`▁▁▁▁▁▁▁▁▁▁▂▃▅▇███▇▅▃▂`) }) + + test("mean=5 cdf", () => { + let cdfNormalDistAtMean10 = x => SymbolicDist.Normal.cdf(x, normalDistAtMean10) + let sparklineMean10 = fnImage(cdfNormalDistAtMean10, range20Float) + Sparklines.create(sparklineMean10, ()) + -> expect + -> toEqual(`▁▁▁▁▁▁▁▁▂▃▅▆▇████████`) + }) })