Added named paramaters to most GenericDist functions
This commit is contained in:
parent
d61f521a0e
commit
f2d03c8f11
|
@ -29,10 +29,14 @@ let normalize = (t: t) =>
|
|||
| #SampleSet(_) => t
|
||||
}
|
||||
|
||||
let operationToFloat = (t, toPointSet: toPointSetFn, fnName) => {
|
||||
let operationToFloat = (
|
||||
t,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~operation: Operation.distToFloatOperation,
|
||||
) => {
|
||||
let symbolicSolution = switch t {
|
||||
| #Symbolic(r) =>
|
||||
switch SymbolicDist.T.operate(fnName, r) {
|
||||
switch SymbolicDist.T.operate(operation, r) {
|
||||
| Ok(f) => Some(f)
|
||||
| _ => None
|
||||
}
|
||||
|
@ -41,7 +45,7 @@ let operationToFloat = (t, toPointSet: toPointSetFn, fnName) => {
|
|||
|
||||
switch symbolicSolution {
|
||||
| Some(r) => Ok(r)
|
||||
| None => toPointSet(t)->E.R2.fmap(PointSetDist.operate(fnName))
|
||||
| None => toPointSetFn(t)->E.R2.fmap(PointSetDist.operate(operation))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,9 +88,10 @@ module Truncate = {
|
|||
|
||||
let run = (
|
||||
t: t,
|
||||
toPointSet: toPointSetFn,
|
||||
leftCutoff: option<float>,
|
||||
rightCutoff: option<float>,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~leftCutoff=None: option<float>,
|
||||
~rightCutoff=None: option<float>,
|
||||
(),
|
||||
): result<t, error> => {
|
||||
let doesNotNeedCutoff = E.O.isNone(leftCutoff) && E.O.isNone(rightCutoff)
|
||||
if doesNotNeedCutoff {
|
||||
|
@ -95,7 +100,7 @@ module Truncate = {
|
|||
switch trySymbolicSimplification(leftCutoff, rightCutoff, t) {
|
||||
| Some(r) => Ok(r)
|
||||
| None =>
|
||||
toPointSet(t)->E.R2.fmap(t =>
|
||||
toPointSetFn(t)->E.R2.fmap(t =>
|
||||
#PointSet(PointSetDist.T.truncate(leftCutoff, rightCutoff, t))
|
||||
)
|
||||
}
|
||||
|
@ -168,20 +173,20 @@ module AlgebraicCombination = {
|
|||
|
||||
let run = (
|
||||
t1: t,
|
||||
toPointSet: toPointSetFn,
|
||||
toSampleSet: toSampleSetFn,
|
||||
algebraicOp,
|
||||
t2: t,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~toSampleSetFn: toSampleSetFn,
|
||||
~operation,
|
||||
~t2: t,
|
||||
): result<t, error> => {
|
||||
switch tryAnalyticalSimplification(algebraicOp, t1, t2) {
|
||||
switch tryAnalyticalSimplification(operation, t1, t2) {
|
||||
| Some(Ok(symbolicDist)) => Ok(#Symbolic(symbolicDist))
|
||||
| Some(Error(e)) => Error(Other(e))
|
||||
| None =>
|
||||
switch chooseConvolutionOrMonteCarlo(t1, t2) {
|
||||
| #CalculateWithMonteCarlo =>
|
||||
runMonteCarlo(toSampleSet, algebraicOp, t1, t2)->E.R2.fmap(r => #SampleSet(r))
|
||||
runMonteCarlo(toSampleSetFn, operation, t1, t2)->E.R2.fmap(r => #SampleSet(r))
|
||||
| #CalculateWithConvolution =>
|
||||
runConvolution(toPointSet, algebraicOp, t1, t2)->E.R2.fmap(r => #PointSet(r))
|
||||
runConvolution(toPointSetFn, operation, t1, t2)->E.R2.fmap(r => #PointSet(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -190,11 +195,11 @@ module AlgebraicCombination = {
|
|||
let algebraicCombination = AlgebraicCombination.run
|
||||
|
||||
//TODO: Add faster pointwiseCombine fn
|
||||
let pointwiseCombination = (t1: t, toPointSet: toPointSetFn, operation, t2: t): result<
|
||||
let pointwiseCombination = (t1: t, ~toPointSetFn: toPointSetFn, ~operation, ~t2: t): result<
|
||||
t,
|
||||
error,
|
||||
> => {
|
||||
E.R.merge(toPointSet(t1), toPointSet(t2))
|
||||
E.R.merge(toPointSetFn(t1), toPointSetFn(t2))
|
||||
->E.R2.fmap(((t1, t2)) =>
|
||||
PointSetDist.combinePointwise(GenericDist_Types.Operation.arithmeticToFn(operation), t1, t2)
|
||||
)
|
||||
|
@ -203,22 +208,22 @@ let pointwiseCombination = (t1: t, toPointSet: toPointSetFn, operation, t2: t):
|
|||
|
||||
let pointwiseCombinationFloat = (
|
||||
t: t,
|
||||
toPointSet: toPointSetFn,
|
||||
operation: GenericDist_Types.Operation.arithmeticOperation,
|
||||
f: float,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~operation: GenericDist_Types.Operation.arithmeticOperation,
|
||||
~float: float,
|
||||
): result<t, error> => {
|
||||
let m = switch operation {
|
||||
| #Add | #Subtract => Error(GenericDist_Types.DistributionVerticalShiftIsInvalid)
|
||||
| (#Multiply | #Divide | #Exponentiate | #Log) as operation =>
|
||||
toPointSet(t)->E.R2.fmap(t => {
|
||||
toPointSetFn(t)->E.R2.fmap(t => {
|
||||
//TODO: Move to PointSet codebase
|
||||
let fn = (secondary, main) => Operation.Scale.toFn(operation, main, secondary)
|
||||
let integralSumCacheFn = Operation.Scale.toIntegralSumCacheFn(operation)
|
||||
let integralCacheFn = Operation.Scale.toIntegralCacheFn(operation)
|
||||
PointSetDist.T.mapY(
|
||||
~integralSumCacheFn=integralSumCacheFn(f),
|
||||
~integralCacheFn=integralCacheFn(f),
|
||||
~fn=fn(f),
|
||||
~integralSumCacheFn=integralSumCacheFn(float),
|
||||
~integralCacheFn=integralCacheFn(float),
|
||||
~fn=fn(float),
|
||||
t,
|
||||
)
|
||||
})
|
||||
|
@ -230,8 +235,8 @@ let pointwiseCombinationFloat = (
|
|||
//Note: If the inputs are not normalized, this will return poor results. The weights probably refer to the post-normalized forms. It would be good to apply a catch to this.
|
||||
let mixture = (
|
||||
values: array<(t, float)>,
|
||||
scaleMultiply: scaleMultiplyFn,
|
||||
pointwiseAdd: pointwiseAddFn,
|
||||
~scaleMultiplyFn: scaleMultiplyFn,
|
||||
~pointwiseAddFn: pointwiseAddFn,
|
||||
) => {
|
||||
if E.A.length(values) == 0 {
|
||||
Error(GenericDist_Types.Other("mixture must have at least 1 element"))
|
||||
|
@ -239,13 +244,13 @@ let mixture = (
|
|||
let totalWeight = values->E.A2.fmap(E.Tuple2.second)->E.A.Floats.sum
|
||||
let properlyWeightedValues =
|
||||
values
|
||||
->E.A2.fmap(((dist, weight)) => scaleMultiply(dist, weight /. totalWeight))
|
||||
->E.A2.fmap(((dist, weight)) => scaleMultiplyFn(dist, weight /. totalWeight))
|
||||
->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)),
|
||||
(acc, x) => E.R.bind(acc, acc => pointwiseAddFn(acc, x)),
|
||||
Ok(E.A.unsafe_get(values, 0)),
|
||||
)
|
||||
})
|
||||
|
|
|
@ -13,32 +13,46 @@ let toString: t => string
|
|||
|
||||
let normalize: t => t
|
||||
|
||||
let operationToFloat: (t, toPointSetFn, Operation.distToFloatOperation) => result<float, error>
|
||||
let operationToFloat: (
|
||||
t,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~operation: Operation.distToFloatOperation,
|
||||
) => result<float, error>
|
||||
|
||||
let toPointSet: (t, int) => result<PointSetTypes.pointSetDist, error>
|
||||
|
||||
let truncate: (t, toPointSetFn, option<float>, option<float>) => result<t, error>
|
||||
let truncate: (
|
||||
t,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~leftCutoff: option<float>=?,
|
||||
~rightCutoff: option<float>=?,
|
||||
unit,
|
||||
) => result<t, error>
|
||||
|
||||
let algebraicCombination: (
|
||||
t,
|
||||
toPointSetFn,
|
||||
toSampleSetFn,
|
||||
GenericDist_Types.Operation.arithmeticOperation,
|
||||
t,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~toSampleSetFn: toSampleSetFn,
|
||||
~operation: GenericDist_Types.Operation.arithmeticOperation,
|
||||
~t2: t,
|
||||
) => result<t, error>
|
||||
|
||||
let pointwiseCombination: (
|
||||
t,
|
||||
toPointSetFn,
|
||||
GenericDist_Types.Operation.arithmeticOperation,
|
||||
t,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~operation: GenericDist_Types.Operation.arithmeticOperation,
|
||||
~t2: t,
|
||||
) => result<t, error>
|
||||
|
||||
let pointwiseCombinationFloat: (
|
||||
t,
|
||||
toPointSetFn,
|
||||
GenericDist_Types.Operation.arithmeticOperation,
|
||||
float,
|
||||
~toPointSetFn: toPointSetFn,
|
||||
~operation: GenericDist_Types.Operation.arithmeticOperation,
|
||||
~float: float,
|
||||
) => result<t, error>
|
||||
|
||||
let mixture: (array<(t, float)>, scaleMultiplyFn, pointwiseAddFn) => result<t, error>
|
||||
let mixture: (
|
||||
array<(t, float)>,
|
||||
~scaleMultiplyFn: scaleMultiplyFn,
|
||||
~pointwiseAddFn: pointwiseAddFn,
|
||||
) => result<t, error>
|
||||
|
|
|
@ -48,11 +48,17 @@ let fromResult = (r: result<outputType, error>): outputType =>
|
|||
| Error(e) => #GenDistError(e)
|
||||
}
|
||||
|
||||
//This is used to catch errors in other switch statements.
|
||||
let _errorMap = (o: outputType): error =>
|
||||
switch o {
|
||||
| #GenDistError(r) => r
|
||||
| _ => Unreachable
|
||||
}
|
||||
|
||||
let outputToDistResult = (o: outputType): result<genericDist, error> =>
|
||||
switch o {
|
||||
| #Dist(r) => Ok(r)
|
||||
| #GenDistError(r) => Error(r)
|
||||
| _ => Error(Unreachable)
|
||||
| r => Error(_errorMap(r))
|
||||
}
|
||||
|
||||
let rec run = (extra, fnName: operation): outputType => {
|
||||
|
@ -62,19 +68,17 @@ let rec run = (extra, fnName: operation): outputType => {
|
|||
run(extra, fnName)
|
||||
}
|
||||
|
||||
let toPointSet = r => {
|
||||
let toPointSetFn = r => {
|
||||
switch reCall(~fnName=#fromDist(#toDist(#toPointSet), r), ()) {
|
||||
| #Dist(#PointSet(p)) => Ok(p)
|
||||
| #GenDistError(r) => Error(r)
|
||||
| _ => Error(Unreachable)
|
||||
| r => Error(_errorMap(r))
|
||||
}
|
||||
}
|
||||
|
||||
let toSampleSet = r => {
|
||||
let toSampleSetFn = r => {
|
||||
switch reCall(~fnName=#fromDist(#toDist(#toSampleSet(sampleCount)), r), ()) {
|
||||
| #Dist(#SampleSet(p)) => Ok(p)
|
||||
| #GenDistError(r) => Error(r)
|
||||
| _ => Error(Unreachable)
|
||||
| r => Error(_errorMap(r))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -93,42 +97,50 @@ let rec run = (extra, fnName: operation): outputType => {
|
|||
let fromDistFn = (subFnName: GenericDist_Types.Operation.fromDist, dist: genericDist) =>
|
||||
switch subFnName {
|
||||
| #toFloat(fnName) =>
|
||||
GenericDist.operationToFloat(dist, toPointSet, fnName)->E.R2.fmap(r => #Float(r))->fromResult
|
||||
GenericDist.operationToFloat(dist, ~toPointSetFn, ~operation=fnName)
|
||||
->E.R2.fmap(r => #Float(r))
|
||||
->fromResult
|
||||
| #toString => dist->GenericDist.toString->(r => #String(r))
|
||||
| #toDist(#consoleLog) => {
|
||||
| #toDist(#inspect) => {
|
||||
Js.log2("Console log requested: ", dist)
|
||||
#Dist(dist)
|
||||
}
|
||||
| #toDist(#normalize) => dist->GenericDist.normalize->(r => #Dist(r))
|
||||
| #toDist(#truncate(left, right)) =>
|
||||
dist->GenericDist.truncate(toPointSet, left, right)->E.R2.fmap(r => #Dist(r))->fromResult
|
||||
| #toDist(#truncate(leftCutoff, rightCutoff)) =>
|
||||
GenericDist.truncate(~toPointSetFn, ~leftCutoff, ~rightCutoff, dist, ())
|
||||
->E.R2.fmap(r => #Dist(r))
|
||||
->fromResult
|
||||
| #toDist(#toPointSet) =>
|
||||
dist->GenericDist.toPointSet(xyPointLength)->E.R2.fmap(r => #Dist(#PointSet(r)))->fromResult
|
||||
| #toDist(#toSampleSet(n)) =>
|
||||
dist->GenericDist.sampleN(n)->E.R2.fmap(r => #Dist(#SampleSet(r)))->fromResult
|
||||
| #toDistCombination(#Algebraic, _, #Float(_)) => #GenDistError(NotYetImplemented)
|
||||
| #toDistCombination(#Algebraic, operation, #Dist(dist2)) =>
|
||||
| #toDistCombination(#Algebraic, operation, #Dist(t2)) =>
|
||||
dist
|
||||
->GenericDist.algebraicCombination(toPointSet, toSampleSet, operation, dist2)
|
||||
->GenericDist.algebraicCombination(~toPointSetFn, ~toSampleSetFn, ~operation, ~t2)
|
||||
->E.R2.fmap(r => #Dist(r))
|
||||
->fromResult
|
||||
| #toDistCombination(#Pointwise, operation, #Dist(dist2)) =>
|
||||
| #toDistCombination(#Pointwise, operation, #Dist(t2)) =>
|
||||
dist
|
||||
->GenericDist.pointwiseCombination(toPointSet, operation, dist2)
|
||||
->GenericDist.pointwiseCombination(~toPointSetFn, ~operation, ~t2)
|
||||
->E.R2.fmap(r => #Dist(r))
|
||||
->fromResult
|
||||
| #toDistCombination(#Pointwise, operation, #Float(f)) =>
|
||||
| #toDistCombination(#Pointwise, operation, #Float(float)) =>
|
||||
dist
|
||||
->GenericDist.pointwiseCombinationFloat(toPointSet, operation, f)
|
||||
->GenericDist.pointwiseCombinationFloat(~toPointSetFn, ~operation, ~float)
|
||||
->E.R2.fmap(r => #Dist(r))
|
||||
->fromResult
|
||||
}
|
||||
|
||||
switch fnName {
|
||||
| #fromDist(subFnName, dist) => fromDistFn(subFnName, dist)
|
||||
| #fromFloat(subFnName, float) => reCall(~fnName=#fromDist(subFnName, GenericDist.fromFloat(float)), ())
|
||||
| #fromFloat(subFnName, float) =>
|
||||
reCall(~fnName=#fromDist(subFnName, GenericDist.fromFloat(float)), ())
|
||||
| #mixture(dists) =>
|
||||
dists->GenericDist.mixture(scaleMultiply, pointwiseAdd)->E.R2.fmap(r => #Dist(r))->fromResult
|
||||
dists
|
||||
->GenericDist.mixture(~scaleMultiplyFn=scaleMultiply, ~pointwiseAddFn=pointwiseAdd)
|
||||
->E.R2.fmap(r => #Dist(r))
|
||||
->fromResult
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,4 +160,4 @@ let outputMap = (
|
|||
| (#fromFloat(_), _) => Error(Other("Expected float, got something else"))
|
||||
}
|
||||
newFnCall->E.R2.fmap(r => run(extra, r))->fromResult
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ module Operation = {
|
|||
| #toPointSet
|
||||
| #toSampleSet(int)
|
||||
| #truncate(option<float>, option<float>)
|
||||
| #consoleLog
|
||||
| #inspect
|
||||
]
|
||||
|
||||
type toFloatArray = [
|
||||
|
@ -85,7 +85,7 @@ module Operation = {
|
|||
| #toDist(#toPointSet) => `toPointSet`
|
||||
| #toDist(#toSampleSet(r)) => `toSampleSet(${E.I.toString(r)})`
|
||||
| #toDist(#truncate(_, _)) => `truncate`
|
||||
| #toDist(#consoleLog) => `consoleLog`
|
||||
| #toDist(#inspect) => `inspect`
|
||||
| #toString => `toString`
|
||||
| #toDistCombination(#Algebraic, _, _) => `algebraic`
|
||||
| #toDistCombination(#Pointwise, _, _) => `pointwise`
|
||||
|
|
Loading…
Reference in New Issue
Block a user