From 9dcac8dd481b3099cf2fe0b73705248c8e74f5b2 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Thu, 30 Jun 2022 12:20:50 -0400 Subject: [PATCH] fixed \`-1\` I had forgotten; added basic mean tests --- .../__tests__/Distributions/Symbolic_test.res | 17 +++++++++++++++++ .../Distributions/SymbolicDist/SymbolicDist.res | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res b/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res index 6ee9ffc8..152b580d 100644 --- a/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res +++ b/packages/squiggle-lang/__tests__/Distributions/Symbolic_test.res @@ -77,6 +77,23 @@ describe("(Symbolic) mean", () => { meanValue->unpackFloat->expect->ExpectJs.toBeFalsy }) + testAll( + "of beta distributions from mean and standard dev", + list{(0.39, 0.1), (0.08, 0.1), (0.8, 0.3)}, + tup => { + let (mean, stdev) = tup + let betaDistribution = SymbolicDist.Beta.fromMeanAndStdev(mean, stdev) + let meanValue = + betaDistribution->E.R2.fmap(d => + run(FromDist(ToFloat(#Mean), d->DistributionTypes.Symbolic)) + ) + switch meanValue { + | Ok(value) => value->unpackFloat->expect->toBeCloseTo(mean) + | Error(err) => err->expect->toBe("shouldn't happen") + } + }, + ) + testAll( "of lognormal distributions", list{(2.0, 4.0), (1e-7, 1e-2), (-1e6, 10.0), (1e3, -1e2), (-1e8, -1e4), (1e2, 1e-5)}, diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index 61470d08..0b192b5f 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -135,7 +135,7 @@ module Beta = { let fromMeanAndStdev = (mean, stdev) => { // https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_variance let var = stdev *. stdev - let sampleSize = mean *. (1.0 -. mean) /. var + let sampleSize = mean *. (1.0 -. mean) /. var -. 1.0 mean->fromMeanAndSampleSize(sampleSize) } }