From 14189fd8484cae8d6484f7d46755a090a6c19ab9 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Mon, 27 Jun 2022 14:31:35 -0400 Subject: [PATCH 1/7] #497 (no tests or examples) --- .../Distributions/SymbolicDist/SymbolicDist.res | 13 +++++++++++++ .../FunctionRegistry/FunctionRegistry_Library.res | 8 ++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index f33fc450..762e5fd5 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -124,6 +124,19 @@ module Beta = { let sample = (t: t) => Jstat.Beta.sample(t.alpha, t.beta) let mean = (t: t) => Ok(Jstat.Beta.mean(t.alpha, t.beta)) let toString = ({alpha, beta}: t) => j`Beta($alpha,$beta)` + + let fromMeanAndSampleSize = (mean, sampleSize) => { + // https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_sample_size + let alpha = mean *. sampleSize + let beta = (1.0 -. mean) *. sampleSize + alpha->make(beta) + } + + let fromMeanAndStdev = (mean, stdev) => { + let var = stdev *. stdev + let sampleSize = mean *. (1.0 -. mean) /. var + mean->fromMeanAndSampleSize(sampleSize) + } } module Lognormal = { diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 56315860..a37b8dc4 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -117,8 +117,12 @@ lognormal({mean: 5, stdev: 2})`, ), Function.make( ~name="Beta", - ~examples=`beta(20, 25)`, - ~definitions=[TwoArgDist.make("beta", twoArgs(SymbolicDist.Beta.make))], + ~examples=`beta(20, 25) +beta({mean: 0.39, stdev: 0.1})`, + ~definitions=[ + TwoArgDist.make("beta", twoArgs(SymbolicDist.Beta.make)), + TwoArgDist.makeRecordMeanStdev("beta", twoArgs(SymbolicDist.Beta.fromMeanAndStdev)), + ], (), ), Function.make( From 54cd6365241e63671189a70c5cdbe8cb40e1bb77 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Mon, 27 Jun 2022 14:37:12 -0400 Subject: [PATCH 2/7] added comment for more direct wikipedia citation --- .../src/rescript/Distributions/SymbolicDist/SymbolicDist.res | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index 762e5fd5..61470d08 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -133,6 +133,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 mean->fromMeanAndSampleSize(sampleSize) From 9dcac8dd481b3099cf2fe0b73705248c8e74f5b2 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Thu, 30 Jun 2022 12:20:50 -0400 Subject: [PATCH 3/7] 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) } } From e0289ed23fea20006e5b89f8914f7a2a97efdc58 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Thu, 30 Jun 2022 12:29:09 -0400 Subject: [PATCH 4/7] documented addition to `beta` api --- packages/website/docs/Api/DistGeneric.mdx | 2 ++ packages/website/docs/Guides/DistributionCreation.mdx | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/packages/website/docs/Api/DistGeneric.mdx b/packages/website/docs/Api/DistGeneric.mdx index e49eec1c..42f464df 100644 --- a/packages/website/docs/Api/DistGeneric.mdx +++ b/packages/website/docs/Api/DistGeneric.mdx @@ -70,12 +70,14 @@ uniform(10, 12); ``` beta: (distribution|number, distribution|number) => distribution +beta: (dict<{mean: distribution|number, stdev: distribution|number}>) => distribution ``` **Examples** ```javascript beta(20, 25); +beta({ mean: 0.39, stdev: 0.1 }); ``` ### cauchy diff --git a/packages/website/docs/Guides/DistributionCreation.mdx b/packages/website/docs/Guides/DistributionCreation.mdx index 5e84b47f..1ad69960 100644 --- a/packages/website/docs/Guides/DistributionCreation.mdx +++ b/packages/website/docs/Guides/DistributionCreation.mdx @@ -259,6 +259,7 @@ with values at 1 and 2. Therefore, this is the same as `mixture(pointMass(1),poi ## Beta `beta(alpha:number, beta:number)` +`beta({mean: number, stdev: number})` Creates a [beta distribution](https://en.wikipedia.org/wiki/Beta_distribution) with the given `alpha` and `beta` values. For a good summary of the beta distribution, see [this explanation](https://stats.stackexchange.com/a/47782) on Stack Overflow. @@ -278,6 +279,12 @@ Creates a [beta distribution](https://en.wikipedia.org/wiki/Beta_distribution) w + + + ### Arguments From 5b41019c04d4b5137af7776bc21b84e784635371 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Thu, 30 Jun 2022 14:38:59 -0400 Subject: [PATCH 5/7] hotfix: relative path --- packages/website/docs/Introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/website/docs/Introduction.md b/packages/website/docs/Introduction.md index 35ff63b6..e04157c2 100644 --- a/packages/website/docs/Introduction.md +++ b/packages/website/docs/Introduction.md @@ -10,7 +10,7 @@ Squiggle is an _estimation language_, and a syntax for _calculating and expressi - [Gallery](./Discussions/Gallery) - [Squiggle playground](/playground) - [Language basics](./Guides/Language) -- [Squiggle functions source of truth](./docs/Guides/Functions) +- [Squiggle functions source of truth](./Guides/Functions) - [Known bugs](./Discussions/Bugs) - [Original lesswrong sequence](https://www.lesswrong.com/s/rDe8QE5NvXcZYzgZ3) - [Author your squiggle models as Observable notebooks](https://observablehq.com/@hazelfire/squiggle) From 7a419742ad5f91afd07ff6a7deb828ad8c50632a Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Mon, 4 Jul 2022 09:57:00 -0400 Subject: [PATCH 6/7] moved error message sooner for invalid mean and stdev --- .../Distributions/SymbolicDist/SymbolicDist.res | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index 0b192b5f..e7471ffa 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -129,14 +129,18 @@ module Beta = { // https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_sample_size let alpha = mean *. sampleSize let beta = (1.0 -. mean) *. sampleSize - alpha->make(beta) + make(alpha, 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 -. 1.0 - mean->fromMeanAndSampleSize(sampleSize) + if !(0.0 < stdev && stdev <= 0.5) || !(0.0 <= mean && mean <= 1.0) { + "Beta mean must be in [0,1] and stdev must be in (1,0.5]"->Error + } else { + let var = stdev *. stdev + let sampleSize = mean *. (1.0 -. mean) /. var -. 1.0 + fromMeanAndSampleSize(mean, sampleSize) + } } } From 4054f7dbfffc91e059da6c4fd509d9721969daef Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Tue, 5 Jul 2022 19:48:41 -0400 Subject: [PATCH 7/7] Small beta error message change --- .../rescript/Distributions/SymbolicDist/SymbolicDist.res | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index e7471ffa..249deb02 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -134,8 +134,10 @@ module Beta = { let fromMeanAndStdev = (mean, stdev) => { // https://en.wikipedia.org/wiki/Beta_distribution#Mean_and_variance - if !(0.0 < stdev && stdev <= 0.5) || !(0.0 <= mean && mean <= 1.0) { - "Beta mean must be in [0,1] and stdev must be in (1,0.5]"->Error + if !(0.0 < stdev && stdev <= 0.5) { + Error("Stdev must be in in between 0 and 0.5.") + } else if !(0.0 <= mean && mean <= 1.0) { + Error("Mean must be in between 0 and 1.0.") } else { let var = stdev *. stdev let sampleSize = mean *. (1.0 -. mean) /. var -. 1.0