Added todo list to GenericDist repo

This commit is contained in:
Ozzie Gooen 2022-03-28 07:56:20 -04:00
parent 80b33fcd84
commit 04abeb7cd1
4 changed files with 36 additions and 12 deletions

View File

@ -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<genericDist, error>,
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)),
)
})
}
}

View File

@ -29,4 +29,17 @@ graph TD
D --> Float(Float)
D --> Exponential(Exponential)
D --> Cauchy(Cauchy)
```
```
## 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.

View File

@ -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)) =>

View File

@ -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}