From ba4ee1212d7b2e6c07e2c013592ddeeb5df9f78a Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Thu, 7 Apr 2022 20:09:06 -0400 Subject: [PATCH] intermediary commit while I check out something else --- .../SymbolicDist/SymbolicDist2.res | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist2.res diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist2.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist2.res new file mode 100644 index 00000000..e1b6d352 --- /dev/null +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist2.res @@ -0,0 +1,83 @@ +open SymbolicDistTypes + +module type ValidNormal = { + let params: normal + let distribution: validated +} + +let from90PercentCI = (low, high) => { + let mean = E.A.Floats.mean([low, high]) + let stdev = (high -. low) /. (2.0 *. 1.644854) + (mean, stdev) +} + +module Normal = (Validated: ValidNormal) => { + let pdf = x => Jstat.Normal.pdf(x, Validated.params.mean, Validated.params.stdev) + let cdf = x => Jstat.Normal.cdf(x, Validated.params.mean, Validated.params.stdev) + + let from90PercentCI = (low, high) => { + let mean = E.A.Floats.mean([low, high]) + let stdev = (high -. low) /. (2.0 *. 1.644854) + normalConstr({mean: mean, stdev: stdev}) + } + + let inv = p => Jstat.Normal.inv(p, Validated.params.mean, Validated.params.stdev) + let sample = Jstat.Normal.sample(Validated.params.mean, Validated.params.stdev) + let mean = Jstat.Normal.mean(Validated.params.mean, Validated.params.stdev) + let toString = j`Normal(${Js.Float.toString(Validated.params.mean)},${Js.Float.toString(Validated.params.stdev)})` +} + +module NormalBinOps = (N1: ValidNormal, N2: ValidNormal) => { + let add = { + let mean = N1.params.mean +. N2.params.mean + let stdev = sqrt(N1.params.stdev ** 2.0 +. N2.params.stdev ** 2.0) + normalConstr({mean: mean, stdev: stdev}) + } + + let subtract = { + let mean = N1.params.mean -. N2.params.mean + let stdev = sqrt(N1.params.stdev ** 2.0 +. N2.params.stdev ** 2.0) + normalConstr({mean: mean, stdev: stdev}) + } + + let operate: Operation.Algebraic.t => option> = operation => { + switch operation { + | #Add => Some(add) + | #Subtract => Some(subtract) + | _ => None + } + } +} + +module From90thPercentile = { + let constr: (float, float) => validated = (low, high) => { + let (mean, stdev) = from90PercentCI(low, high) + module NormalValueValidated: ValidNormal = { + let params = {mean: mean, stdev: stdev} + let distribution = normalConstr(params) + } + module NormalValue = Normal(NormalValueValidated) + NormalValue.from90PercentCI(low, high) + } +} + +module T = { + let minCdfValue = 1e-4 + let maxCdfValue = 1.0 -. 1e-4 + + let pdf = (x, dist) => { + switch dist { + | #Normal(_params) => { + module NormalValueValidated: ValidNormal = { + let params = _params + let distribution = normalConstr(params) + } + module NormalValue = Normal(NormalValueValidated) + switch NormalValueValidated.distribution { + | Ok(symbdist) => NormalValue.pdf(x) + | Error(invalidNormal) => 0.0 + } + } + } + } +} \ No newline at end of file