Added pointwiseCombinationFloat to sci.res

This commit is contained in:
Ozzie Gooen 2022-03-26 13:25:47 -04:00
parent 1a2ce5bfa0
commit d490af38f0
2 changed files with 211 additions and 188 deletions

View File

@ -3,6 +3,7 @@ type error =
| InputsNeedPointSetConversion | InputsNeedPointSetConversion
| NotYetImplemented | NotYetImplemented
| ImpossiblePath | ImpossiblePath
| DistributionVerticalShiftIsInvalid
| Other(string) | Other(string)
type genericDist = [ type genericDist = [
@ -11,209 +12,220 @@ type genericDist = [
| #Symbolic(SymbolicDistTypes.symbolicDist) | #Symbolic(SymbolicDistTypes.symbolicDist)
] ]
type outputType = [ module OperationType = {
| #Dist(genericDist) type direction = [
| #Error(error) | #Algebraic
| #Float(float) | #Pointwise
] ]
let fromResult = (r: result<outputType, error>): outputType => type combination = [
switch r { | #Add
| Ok(o) => o | #Multiply
| Error(e) => #Error(e) | #Subtract
| #Divide
| #Exponentiate
| #Log
]
let combinationToFn = (combination: combination) =>
switch combination {
| #Add => \"+."
| #Multiply => \"*."
| #Subtract => \"-."
| #Exponentiate => \"**"
| #Divide => \"/."
| #Log => (a, b) => log(a) /. log(b)
}
type toFloat = [
| #Cdf(float)
| #Inv(float)
| #Mean
| #Pdf(float)
| #Sample
]
type toDist = [
| #normalize
| #toPointSet
| #toSampleSet(int)
]
type toFloatArray = [
| #Sample(int)
]
type scale = [
| #Multiply
| #Exponentiate
| #Log
]
type t = [
| #toFloat(toFloat)
| #toDist(toDist)
| #toDistCombination(direction, combination, [#Dist(genericDist) | #Float(float)])
]
}
type operation = OperationType.t
module T = {
type t = genericDist
type toPointSetFn = genericDist => result<PointSetTypes.pointSetDist, error>
let sampleN = (n, t: t) => {
switch t {
| #PointSet(r) => Ok(PointSetDist.sampleNRendered(n, r))
| #Symbolic(r) => Ok(SymbolicDist.T.sampleN(n, r))
| #SampleSet(_) => Error(NotYetImplemented)
}
} }
type direction = [ let toFloat = (toPointSet: toPointSetFn, fnName, t: genericDist) => {
| #Algebraic switch t {
| #Pointwise | #Symbolic(r) if Belt.Result.isOk(SymbolicDist.T.operate(fnName, r)) =>
] switch SymbolicDist.T.operate(fnName, r) {
| Ok(float) => Ok(float)
type combination = [ | Error(_) => Error(ImpossiblePath)
| #Add
| #Multiply
| #Subtract
| #Divide
| #Exponentiate
]
let combinationToFn = (combination: combination) =>
switch combination {
| #Add => \"+."
| #Multiply => \"*."
| #Subtract => \"-."
| #Exponentiate => \"**"
| #Divide => \"/."
}
type toFloat = [
| #Cdf(float)
| #Inv(float)
| #Mean
| #Pdf(float)
| #Sample
]
type toDist = [
| #normalize
| #toPointSet
| #toSampleSet(int)
]
type toFloatArray = [
| #Sample(int)
]
type operation = [
| #toFloat(toFloat)
| #toDist(toDist)
| #toDistCombination(direction, combination, genericDist)
]
type params = {
sampleCount: int,
xyPointLength: int,
}
let genericParams = {
sampleCount: 1000,
xyPointLength: 1000,
}
type wrapped = (genericDist, params)
type wrappedOutput = (outputType, params)
let wrapWithParams = (g: genericDist, f: params): wrapped => (g, f)
let exampleDist: genericDist = #PointSet(
Discrete(Discrete.make(~integralSumCache=Some(1.0), {xs: [3.0], ys: [1.0]})),
)
let defaultSamplingInputs: SamplingInputs.samplingInputs = {
sampleCount: 10000,
outputXYPoints: 10000,
pointSetDistLength: 1000,
kernelWidth: None,
}
/* 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).
In general, this is implemented via convolution. */
module AlgebraicCombination = {
let tryAnalyticalSimplification = (operation, t1: genericDist, t2: genericDist) =>
switch (operation, t1, t2) {
| (operation, #Symbolic(d1), #Symbolic(d2)) =>
switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, operation) {
| #AnalyticalSolution(symbolicDist) => Ok(#Symbolic(symbolicDist))
| #Error(er) => Error(er)
| #NoSolution => Ok(#NoSolution)
} }
| _ => Ok(#NoSolution) | _ =>
} switch toPointSet(t) {
} | Ok(r) => Ok(PointSetDist.operate(fnName, r))
| Error(r) => Error(r)
// let toSampleSet = (r)
let sampleN = (n, genericDist) => {
switch genericDist {
| #PointSet(r) => Ok(PointSetDist.sampleNRendered(n, r))
| #Symbolic(r) => Ok(SymbolicDist.T.sampleN(n, r))
| #SampleSet(_) => Error(NotYetImplemented)
}
}
let toFloat = (
toPointSet: genericDist => result<PointSetTypes.pointSetDist, error>,
fnName,
value,
) => {
switch value {
| #Symbolic(r) if Belt.Result.isOk(SymbolicDist.T.operate(fnName, r)) =>
switch SymbolicDist.T.operate(fnName, r) {
| Ok(float) => Ok(float)
| Error(_) => Error(ImpossiblePath)
}
| #PointSet(r) => Ok(PointSetDist.operate(fnName, r))
| _ =>
switch toPointSet(value) {
| Ok(r) => Ok(PointSetDist.operate(fnName, r))
| Error(r) => Error(r)
}
}
}
let distToPointSet = (sampleCount, dist: genericDist) => {
switch dist {
| #PointSet(pointSet) => Ok(pointSet)
| #Symbolic(r) => Ok(SymbolicDist.T.toPointSetDist(sampleCount, r))
| #SampleSet(r) => {
let response = SampleSet.toPointSetDist(
~samples=r,
~samplingInputs=defaultSamplingInputs,
(),
).pointSetDist
switch response {
| Some(r) => Ok(r)
| None => Error(Other("Converting sampleSet to pointSet failed"))
} }
} }
} }
}
let rec applyFnInternal = (wrapped: wrapped, fnName: operation): wrappedOutput => { //TODO: Refactor this bit.
let (value, {sampleCount, xyPointLength} as extra) = wrapped let defaultSamplingInputs: SamplingInputs.samplingInputs = {
let reCall = (~value=value, ~extra=extra, ~fnName=fnName, ()) => { sampleCount: 10000,
applyFnInternal((value, extra), fnName) outputXYPoints: 10000,
pointSetDistLength: 1000,
kernelWidth: None,
} }
let reCallUnwrapped = (~value=value, ~extra=extra, ~fnName=fnName, ()) => {
let (value, _) = applyFnInternal((value, extra), fnName) let toPointSet = (xyPointLength, t: t) => {
value switch t {
} | #PointSet(pointSet) => Ok(pointSet)
let toPointSet = r => { | #Symbolic(r) => Ok(SymbolicDist.T.toPointSetDist(xyPointLength, r))
switch reCallUnwrapped(~value=r, ~fnName=#toDist(#toPointSet), ()) { | #SampleSet(r) => {
| #Dist(#PointSet(p)) => Ok(p) let response = SampleSet.toPointSetDist(
| #Error(r) => Error(r) ~samples=r,
| _ => Error(Other("Impossible error")) ~samplingInputs=defaultSamplingInputs,
(),
).pointSetDist
switch response {
| Some(r) => Ok(r)
| None => Error(Other("Converting sampleSet to pointSet failed"))
}
}
} }
} }
let toPointSetAndReCall = v =>
toPointSet(v) |> E.R.fmap(r => reCallUnwrapped(~value=#PointSet(r), ())) let algebraicCombination = (operation, sampleCount, dist1: t, dist2: t) => {
let newVal: outputType = switch (fnName, value) { let dist1 = sampleN(sampleCount, dist1)
// | (#toFloat(n), v) => toFloat(toPointSet, v, n) let dist2 = sampleN(sampleCount, dist2)
| (#toFloat(fnName), _) => let samples = E.R.merge(dist1, dist2) |> E.R.fmap(((d1, d2)) => {
toFloat(toPointSet, fnName, value) |> E.R.fmap(r => #Float(r)) |> fromResult Belt.Array.zip(d1, d2) |> E.A.fmap(((a, b)) => Operation.Algebraic.toFn(operation, a, b))
| (#toDist(#normalize), #PointSet(r)) => #Dist(#PointSet(PointSetDist.T.normalize(r))) })
| (#toDist(#normalize), #Symbolic(_)) => #Dist(value) samples |> E.R.fmap(r => #SampleSet(r))
| (#toDist(#normalize), #SampleSet(_)) => #Dist(value) }
| (#toDist(#toPointSet), _) =>
value |> distToPointSet(sampleCount) |> E.R.fmap(r => #Dist(#PointSet(r))) |> fromResult let pointwiseCombination = (toPointSet: toPointSetFn, operation, t1: t, t2: t) => {
| (#toDist(#toSampleSet(n)), _) => E.R.merge(toPointSet(t1), toPointSet(t2))
value |> sampleN(n) |> E.R.fmap(r => #Dist(#SampleSet(r))) |> fromResult |> E.R.fmap(((t1, t2)) =>
| (#toDistCombination(#Algebraic, operation, p2), p1) => { PointSetDist.combinePointwise(OperationType.combinationToFn(operation), t1, t2)
// TODO: This could be more complex, to get possible simplification and similar. )
let dist1 = sampleN(sampleCount, p1) |> E.R.fmap(r => #PointSet(r))
let dist2 = sampleN(sampleCount, p2) }
let samples = E.R.merge(dist1, dist2) |> E.R.fmap(((d1, d2)) => {
Belt.Array.zip(d1, d2) |> E.A.fmap(((a, b)) => Operation.Algebraic.toFn(operation, a, b)) let pointwiseCombinationFloat = (
toPointSet: toPointSetFn,
operation: OperationType.combination,
t: t,
f: float,
) => {
switch operation {
| #Add | #Subtract => Error(DistributionVerticalShiftIsInvalid)
| (#Multiply | #Divide | #Exponentiate | #Log) as operation =>
toPointSet(t) |> E.R.fmap(t => {
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),
t,
)
}) })
switch samples { }
| Ok(r) => #Dist(#SampleSet(r)) }
| Error(e) => #Error(e) }
module OmniRunner = {
type params = {
sampleCount: int,
xyPointLength: int,
}
let genericParams = {
sampleCount: 1000,
xyPointLength: 1000,
}
type wrapped = (genericDist, params)
let wrapWithParams = (g: genericDist, f: params): wrapped => (g, f)
type outputType = [
| #Dist(genericDist)
| #Error(error)
| #Float(float)
]
let fromResult = (r: result<outputType, error>): outputType =>
switch r {
| Ok(o) => o
| Error(e) => #Error(e)
}
let rec applyFnInternal = (wrapped: wrapped, fnName: operation): outputType => {
let (value, {sampleCount, xyPointLength} as extra) = wrapped
let reCall = (~value=value, ~extra=extra, ~fnName=fnName, ()) => {
applyFnInternal((value, extra), fnName)
}
let toPointSet = r => {
switch reCall(~value=r, ~fnName=#toDist(#toPointSet), ()) {
| #Dist(#PointSet(p)) => Ok(p)
| #Error(r) => Error(r)
| _ => Error(Other("Impossible error"))
} }
} }
| (#toDistCombination(#Pointwise, operation, p2), p1) => let toPointSetAndReCall = v => toPointSet(v) |> E.R.fmap(r => reCall(~value=#PointSet(r), ()))
switch ( let newVal: outputType = switch (fnName, value) {
toPointSet(p1), // | (#toFloat(n), v) => toFloat(toPointSet, v, n)
toPointSet(p2) | (#toFloat(fnName), _) =>
) { T.toFloat(toPointSet, fnName, value) |> E.R.fmap(r => #Float(r)) |> fromResult
| (Ok(p1), Ok(p2)) => | (#toDist(#normalize), #PointSet(r)) => #Dist(#PointSet(PointSetDist.T.normalize(r)))
// TODO: If the dist is symbolic, then it doesn't need to be converted into a pointSet | (#toDist(#normalize), #Symbolic(_)) => #Dist(value)
#Dist(#PointSet(PointSetDist.combinePointwise(combinationToFn(operation), p1, p2))) | (#toDist(#normalize), #SampleSet(_)) => #Dist(value)
| (_, _) => #Error(Other("No Match or not supported")) | (#toDist(#toPointSet), _) =>
value |> T.toPointSet(xyPointLength) |> E.R.fmap(r => #Dist(#PointSet(r))) |> fromResult
| (#toDist(#toSampleSet(n)), _) =>
value |> T.sampleN(n) |> E.R.fmap(r => #Dist(#SampleSet(r))) |> fromResult
| (#toDistCombination(#Algebraic, _, #Float(_)), _) => #Error(NotYetImplemented)
| (#toDistCombination(#Algebraic, operation, #Dist(p2)), p1) =>
T.algebraicCombination(operation, sampleCount, p1, p2)
|> E.R.fmap(r => #Dist(r))
|> fromResult
| (#toDistCombination(#Pointwise, operation, #Dist(p2)), p1) =>
T.pointwiseCombination(toPointSet, operation, p1, p2) |> E.R.fmap(r => #Dist(r)) |> fromResult
| (#toDistCombination(#Pointwise, operation, #Float(f)), _) =>
T.pointwiseCombinationFloat(toPointSet, operation, value, f)
|> E.R.fmap(r => #Dist(#PointSet(r)))
|> fromResult
} }
| _ => #Error(Other("No Match or not supported")) newVal
} }
(newVal, {sampleCount: sampleCount, xyPointLength: xyPointLength})
} }
// let applyFn = (wrapped, fnName): wrapped => { // let applyFn = (wrapped, fnName): wrapped => {
@ -237,4 +249,8 @@ let rec applyFnInternal = (wrapped: wrapped, fnName: operation): wrappedOutput =
// } // }
// } // }
// let exampleDist: genericDist = #PointSet(
// Discrete(Discrete.make(~integralSumCache=Some(1.0), {xs: [3.0], ys: [1.0]})),
// )
// let foo = exampleDist->wrapWithParams(genericParams)->applyFn(#toDist(#normalize)) // let foo = exampleDist->wrapWithParams(genericParams)->applyFn(#toDist(#normalize))

View File

@ -7,10 +7,11 @@ type algebraicOperation = [
| #Subtract | #Subtract
| #Divide | #Divide
| #Exponentiate | #Exponentiate
| #Log
] ]
@genType @genType
type pointwiseOperation = [#Add | #Multiply | #Exponentiate] type pointwiseOperation = [#Add | #Multiply | #Exponentiate]
type scaleOperation = [#Multiply | #Exponentiate | #Log] type scaleOperation = [#Multiply | #Exponentiate | #Log | #Divide]
type distToFloatOperation = [ type distToFloatOperation = [
| #Pdf(float) | #Pdf(float)
| #Cdf(float) | #Cdf(float)
@ -28,6 +29,7 @@ module Algebraic = {
| #Multiply => \"*." | #Multiply => \"*."
| #Exponentiate => \"**" | #Exponentiate => \"**"
| #Divide => \"/." | #Divide => \"/."
| #Log => (a, b) => log(a) /. log(b)
} }
let applyFn = (t, f1, f2) => let applyFn = (t, f1, f2) =>
@ -43,6 +45,7 @@ module Algebraic = {
| #Multiply => "*" | #Multiply => "*"
| #Exponentiate => "**" | #Exponentiate => "**"
| #Divide => "/" | #Divide => "/"
| #Log => "log"
} }
let format = (a, b, c) => b ++ (" " ++ (toString(a) ++ (" " ++ c))) let format = (a, b, c) => b ++ (" " ++ (toString(a) ++ (" " ++ c)))
@ -79,6 +82,7 @@ module Scale = {
let toFn = x => let toFn = x =>
switch x { switch x {
| #Multiply => \"*." | #Multiply => \"*."
| #Divide => \"/."
| #Exponentiate => \"**" | #Exponentiate => \"**"
| #Log => (a, b) => log(a) /. log(b) | #Log => (a, b) => log(a) /. log(b)
} }
@ -86,6 +90,7 @@ module Scale = {
let format = (operation: t, value, scaleBy) => let format = (operation: t, value, scaleBy) =>
switch operation { switch operation {
| #Multiply => j`verticalMultiply($value, $scaleBy) ` | #Multiply => j`verticalMultiply($value, $scaleBy) `
| #Divide => j`verticalDivide($value, $scaleBy) `
| #Exponentiate => j`verticalExponentiate($value, $scaleBy) ` | #Exponentiate => j`verticalExponentiate($value, $scaleBy) `
| #Log => j`verticalLog($value, $scaleBy) ` | #Log => j`verticalLog($value, $scaleBy) `
} }
@ -93,6 +98,7 @@ module Scale = {
let toIntegralSumCacheFn = x => let toIntegralSumCacheFn = x =>
switch x { switch x {
| #Multiply => (a, b) => Some(a *. b) | #Multiply => (a, b) => Some(a *. b)
| #Divide => (a, b) => Some(a /. b)
| #Exponentiate => (_, _) => None | #Exponentiate => (_, _) => None
| #Log => (_, _) => None | #Log => (_, _) => None
} }
@ -100,6 +106,7 @@ module Scale = {
let toIntegralCacheFn = x => let toIntegralCacheFn = x =>
switch x { switch x {
| #Multiply => (_, _) => None // TODO: this could probably just be multiplied out (using Continuous.scaleBy) | #Multiply => (_, _) => None // TODO: this could probably just be multiplied out (using Continuous.scaleBy)
| #Divide => (_, _) => None
| #Exponentiate => (_, _) => None | #Exponentiate => (_, _) => None
| #Log => (_, _) => None | #Log => (_, _) => None
} }