From 04abeb7cd10d5081d72d93de0a7b97cbae4908b7 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Mon, 28 Mar 2022 07:56:20 -0400 Subject: [PATCH] Added todo list to GenericDist repo --- .../src/rescript/GenericDist/GenericDist.res | 31 ++++++++++++------- .../src/rescript/GenericDist/README.md | 15 ++++++++- .../rescript/pointSetDist/PointSetDist.res | 1 + .../rescript/symbolicDist/SymbolicDist.res | 1 + 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/GenericDist/GenericDist.res b/packages/squiggle-lang/src/rescript/GenericDist/GenericDist.res index 84259de3..7cb714e6 100644 --- a/packages/squiggle-lang/src/rescript/GenericDist/GenericDist.res +++ b/packages/squiggle-lang/src/rescript/GenericDist/GenericDist.res @@ -100,6 +100,8 @@ module Truncate = { } } +let truncate = Truncate.run + /* Given two random variables A and B, this returns the distribution of a new variable that is the result of the operation on A and B. For instance, normal(0, 1) + normal(1, 1) -> normal(1, 2). @@ -136,8 +138,9 @@ module AlgebraicCombination = { t1: t, t2: t, ) => { + let operation = Operation.Algebraic.toFn(operation) E.R.merge(toSampleSet(t1), toSampleSet(t2)) |> E.R.fmap(((a, b)) => { - Belt.Array.zip(a, b) |> E.A.fmap(((a, b)) => Operation.Algebraic.toFn(operation, a, b)) + Belt.Array.zip(a, b) |> E.A.fmap(((a, b)) => operation(a, b)) }) } @@ -178,6 +181,8 @@ module AlgebraicCombination = { } } +let algebraicCombination = AlgebraicCombination.run + //TODO: Add faster pointwiseCombine fn let pointwiseCombination = (toPointSet: toPointSetFn, operation, t2: t, t1: t): result< t, @@ -219,14 +224,18 @@ let mixture = ( pointwiseAdd: (genericDist, genericDist) => result, values: array<(genericDist, float)>, ) => { - let properlyWeightedValues = - values |> E.A.fmap(((dist, weight)) => scaleMultiply(dist, weight)) |> E.A.R.firstErrorOrOpen - properlyWeightedValues |> E.R.bind(_, values => { - values - |> Js.Array.sliceFrom(1) - |> E.A.fold_left( - (acc, x) => E.R.bind(acc, acc => pointwiseAdd(acc, x)), - Ok(E.A.unsafe_get(values, 0)), - ) - }) + if E.A.length(values) == 0 { + Error(GenericDist_Types.Other("mixture must have at least 1 element")) + } else { + let properlyWeightedValues = + values |> E.A.fmap(((dist, weight)) => scaleMultiply(dist, weight)) |> E.A.R.firstErrorOrOpen + properlyWeightedValues |> E.R.bind(_, values => { + values + |> Js.Array.sliceFrom(1) + |> E.A.fold_left( + (acc, x) => E.R.bind(acc, acc => pointwiseAdd(acc, x)), + Ok(E.A.unsafe_get(values, 0)), + ) + }) + } } diff --git a/packages/squiggle-lang/src/rescript/GenericDist/README.md b/packages/squiggle-lang/src/rescript/GenericDist/README.md index 9a3ca6ce..382de2b9 100644 --- a/packages/squiggle-lang/src/rescript/GenericDist/README.md +++ b/packages/squiggle-lang/src/rescript/GenericDist/README.md @@ -29,4 +29,17 @@ graph TD D --> Float(Float) D --> Exponential(Exponential) D --> Cauchy(Cauchy) -``` \ No newline at end of file +``` + +## Todo +- [ ] Lots of cleanup +- [ ] Add interface files +- [ ] Simple test story +- [ ] Provide decent stack traces for key calls in GenericOperation. This could be very useful for debugging. +- [ ] Cleanup Sample Set library +- [ ] Add memoization for calculations +- [ ] Performance bechmarking reports +- [ ] Remove most of DistPlus, much of which is not needed anymore +- [ ] More functions for Sample Set, which is very minimal now +- [ ] Allow these functions to be run on web workers +- [ ] Refactor interpreter to use GenericDist. This might not be necessary, as the new reducer-inspired interpreter is integrated. \ No newline at end of file diff --git a/packages/squiggle-lang/src/rescript/pointSetDist/PointSetDist.res b/packages/squiggle-lang/src/rescript/pointSetDist/PointSetDist.res index 59bead6b..834b244f 100644 --- a/packages/squiggle-lang/src/rescript/pointSetDist/PointSetDist.res +++ b/packages/squiggle-lang/src/rescript/pointSetDist/PointSetDist.res @@ -34,6 +34,7 @@ let toMixed = mapToAll(( ), )) +//TODO WARNING: The combineAlgebraicallyWithDiscrete will break for subtraction and division, like, discrete - continous let combineAlgebraically = (op: Operation.algebraicOperation, t1: t, t2: t): t => switch (t1, t2) { | (Continuous(m1), Continuous(m2)) => diff --git a/packages/squiggle-lang/src/rescript/symbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/symbolicDist/SymbolicDist.res index 3da6bd02..d31a8393 100644 --- a/packages/squiggle-lang/src/rescript/symbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/symbolicDist/SymbolicDist.res @@ -145,6 +145,7 @@ module Uniform = { let mean = (t: t) => Ok(Jstat.Uniform.mean(t.low, t.high)) let toString = ({low, high}: t) => j`Uniform($low,$high)` let truncate = (low, high, t: t): t => { +//todo: add check let newLow = max(E.O.default(neg_infinity, low), t.low) let newHigh = min(E.O.default(infinity, high), t.high) {low: newLow, high: newHigh}