From bd3f2c99d1165ae509d07d859ae25fa795f1e735 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Thu, 14 Apr 2022 16:22:21 -0400 Subject: [PATCH] tweak: add explanation for magic number --- .../SymbolicDist/SymbolicDist.res | 7 +++-- .../Internal/ProcessingConfidenceIntervals.md | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 packages/website/docs/Internal/ProcessingConfidenceIntervals.md diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index 28119e57..eb6ea136 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -1,5 +1,8 @@ open SymbolicDistTypes +let normal95confidencePoint = 1.6448536269514722 +// explained in website/docs/internal/ProcessingConfidenceIntervals + module Normal = { type t = normal let make = (mean: float, stdev: float): result => @@ -11,7 +14,7 @@ module Normal = { let from90PercentCI = (low, high) => { let mean = E.A.Floats.mean([low, high]) - let stdev = (high -. low) /. (2. *. 1.6448536269514722) + let stdev = (high -. low) /. (2. *. normal95confidencePoint) #Normal({mean: mean, stdev: stdev}) } let inv = (p, t: t) => Jstat.Normal.inv(p, t.mean, t.stdev) @@ -120,7 +123,7 @@ module Lognormal = { let logLow = Js.Math.log(low) let logHigh = Js.Math.log(high) let mu = E.A.Floats.mean([logLow, logHigh]) - let sigma = (logHigh -. logLow) /. (2.0 *. 1.6448536269514722) + let sigma = (logHigh -. logLow) /. (2.0 *. normal95confidencePoint) #Lognormal({mu: mu, sigma: sigma}) } let fromMeanAndStdev = (mean, stdev) => { diff --git a/packages/website/docs/Internal/ProcessingConfidenceIntervals.md b/packages/website/docs/Internal/ProcessingConfidenceIntervals.md new file mode 100644 index 00000000..202c291c --- /dev/null +++ b/packages/website/docs/Internal/ProcessingConfidenceIntervals.md @@ -0,0 +1,31 @@ +# Processing confidence intervals + +This page explains what we are doing when we take a 95% confidence interval, and we get a mean and a standard deviation from it + +## For normals + +```js +module Normal = { + //... + let from90PercentCI = (low, high) => { + let mean = E.A.Floats.mean([low, high]) + let stdev = (high -. low) /. (2. *. 1.6448536269514722) + #Normal({mean: mean, stdev: stdev}) + } + //... +} +``` + +We know that for a normal with mean $\mu$ and standard deviation $\sigma$, + +$$ + +a \cdot Normal(\mu, \sigma) = Normal(a\cdot \mu, |a|\cdot \sigma) + +$$ + +We can now look at the inverse cdf of a $Normal(0,1)$. We find that the 95% point is reached at $1.6448536269514722$. ([source](https://stackoverflow.com/questions/20626994/how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p)) This means that the 90% confidence interval is $[-1.6448536269514722, 1.6448536269514722]$, which has a width of $2 \cdot 1.6448536269514722$. + +So then, if we take a $Normal(0,1)$ and we multiply it by $\frac{(high -. low)}{(2. *. 1.6448536269514722)}$, it's 90% confidence interval will be multiplied by the same amount. Then we just have to shift it by the mean to get our target normal. + +## For lognormals \ No newline at end of file