Added scalePower and scaleLog

This commit is contained in:
Ozzie Gooen 2022-04-28 21:14:03 -04:00
parent 5f78399760
commit 282fa7726b
8 changed files with 58 additions and 11 deletions

View File

@ -154,6 +154,16 @@ let rec run = (~env, functionCallInfo: functionCallInfo): outputType => {
->GenericDist.toPointSet(~xyPointLength, ~sampleCount, ()) ->GenericDist.toPointSet(~xyPointLength, ~sampleCount, ())
->E.R2.fmap(r => Dist(PointSet(r))) ->E.R2.fmap(r => Dist(PointSet(r)))
->OutputLocal.fromResult ->OutputLocal.fromResult
| ToDist(Scale(#Logarithm, f)) =>
dist
->GenericDist.pointwiseCombinationFloat(~toPointSetFn, ~algebraicCombination=#Logarithm, ~f)
->E.R2.fmap(r => Dist(r))
->OutputLocal.fromResult
| ToDist(Scale(#Power, f)) =>
dist
->GenericDist.pointwiseCombinationFloat(~toPointSetFn, ~algebraicCombination=#Power, ~f)
->E.R2.fmap(r => Dist(r))
->OutputLocal.fromResult
| ToDistCombination(Algebraic(_), _, #Float(_)) => GenDistError(NotYetImplemented) | ToDistCombination(Algebraic(_), _, #Float(_)) => GenDistError(NotYetImplemented)
| ToDistCombination(Algebraic(strategy), arithmeticOperation, #Dist(t2)) => | ToDistCombination(Algebraic(strategy), arithmeticOperation, #Dist(t2)) =>
dist dist

View File

@ -70,10 +70,16 @@ module DistributionOperation = {
| #Sample | #Sample
] ]
type toScaleFn = [
| #Power
| #Logarithm
]
type toDist = type toDist =
| Normalize | Normalize
| ToPointSet | ToPointSet
| ToSampleSet(int) | ToSampleSet(int)
| Scale(toScaleFn, float)
| Truncate(option<float>, option<float>) | Truncate(option<float>, option<float>)
| Inspect | Inspect
@ -113,6 +119,8 @@ module DistributionOperation = {
| ToDist(ToSampleSet(r)) => `toSampleSet(${E.I.toString(r)})` | ToDist(ToSampleSet(r)) => `toSampleSet(${E.I.toString(r)})`
| ToDist(Truncate(_, _)) => `truncate` | ToDist(Truncate(_, _)) => `truncate`
| ToDist(Inspect) => `inspect` | ToDist(Inspect) => `inspect`
| ToDist(Scale(#Power, r)) => `scalePower(${E.Float.toFixed(r)})`
| ToDist(Scale(#Logarithm, r)) => `scaleLog(${E.Float.toFixed(r)})`
| ToString(ToString) => `toString` | ToString(ToString) => `toString`
| ToString(ToSparkline(n)) => `toSparkline(${E.I.toString(n)})` | ToString(ToSparkline(n)) => `toSparkline(${E.I.toString(n)})`
| ToBool(IsNormalized) => `isNormalized` | ToBool(IsNormalized) => `isNormalized`
@ -142,6 +150,8 @@ module Constructors = {
let toSampleSet = (dist, r): t => FromDist(ToDist(ToSampleSet(r)), dist) let toSampleSet = (dist, r): t => FromDist(ToDist(ToSampleSet(r)), dist)
let truncate = (dist, left, right): t => FromDist(ToDist(Truncate(left, right)), dist) let truncate = (dist, left, right): t => FromDist(ToDist(Truncate(left, right)), dist)
let inspect = (dist): t => FromDist(ToDist(Inspect), dist) let inspect = (dist): t => FromDist(ToDist(Inspect), dist)
let scalePower = (dist, n): t => FromDist(ToDist(Scale(#Power, n)), dist)
let scaleLogarithm = (dist, n): t => FromDist(ToDist(Scale(#Logarithm, n)), dist)
let toString = (dist): t => FromDist(ToString(ToString), dist) let toString = (dist): t => FromDist(ToString(ToString), dist)
let toSparkline = (dist, n): t => FromDist(ToString(ToSparkline(n)), dist) let toSparkline = (dist, n): t => FromDist(ToString(ToSparkline(n)), dist)
let algebraicAdd = (dist1, dist2: genericDist): t => FromDist( let algebraicAdd = (dist1, dist2: genericDist): t => FromDist(

View File

@ -366,6 +366,7 @@ let pointwiseCombination = (
~algebraicCombination: Operation.algebraicOperation, ~algebraicCombination: Operation.algebraicOperation,
~t2: t, ~t2: t,
): result<t, error> => { ): result<t, error> => {
Js.log2("PointwiseCombination", algebraicCombination);
E.R.merge(toPointSetFn(t1), toPointSetFn(t2))->E.R.bind(((t1, t2)) => E.R.merge(toPointSetFn(t1), toPointSetFn(t2))->E.R.bind(((t1, t2)) =>
PointSetDist.combinePointwise(Operation.Algebraic.toFn(algebraicCombination), t1, t2) PointSetDist.combinePointwise(Operation.Algebraic.toFn(algebraicCombination), t1, t2)
->E.R2.fmap(r => DistributionTypes.PointSet(r)) ->E.R2.fmap(r => DistributionTypes.PointSet(r))

View File

@ -156,8 +156,10 @@ let reduce = (
~integralSumCachesFn: (float, float) => option<float>=(_, _) => None, ~integralSumCachesFn: (float, float) => option<float>=(_, _) => None,
fn: (float, float) => result<float, 'e>, fn: (float, float) => result<float, 'e>,
continuousShapes, continuousShapes,
): result<t, 'e> => ): result<t, 'e> => {
continuousShapes |> E.A.R.foldM(combinePointwise(~integralSumCachesFn, fn), empty) let merge = combinePointwise(~integralSumCachesFn, fn)
continuousShapes |> E.A.R.foldM(merge, empty)
}
let mapYResult = ( let mapYResult = (
~integralSumCacheFn=_ => None, ~integralSumCacheFn=_ => None,

View File

@ -34,9 +34,10 @@ let lastY = (t: t) => t |> getShape |> XYShape.T.lastY
let combinePointwise = ( let combinePointwise = (
~integralSumCachesFn=(_, _) => None, ~integralSumCachesFn=(_, _) => None,
fn,
t1: PointSetTypes.discreteShape, t1: PointSetTypes.discreteShape,
t2: PointSetTypes.discreteShape, t2: PointSetTypes.discreteShape,
): PointSetTypes.discreteShape => { ): result<PointSetTypes.discreteShape, 'e> => {
let combinedIntegralSum = Common.combineIntegralSums( let combinedIntegralSum = Common.combineIntegralSums(
integralSumCachesFn, integralSumCachesFn,
t1.integralSumCache, t1.integralSumCache,
@ -49,16 +50,22 @@ let combinePointwise = (
make( make(
~integralSumCache=combinedIntegralSum, ~integralSumCache=combinedIntegralSum,
XYShape.PointwiseCombination.combine( XYShape.PointwiseCombination.combine(
(a, b) => Ok(a +. b), fn,
XYShape.XtoY.discreteInterpolator, XYShape.XtoY.discreteInterpolator,
t1.xyShape, t1.xyShape,
t2.xyShape, t2.xyShape,
)->E.R.toExn("Addition operation should never fail", _), )->E.R.toExn("Addition operation should never fail", _),
) )->Ok
} }
let reduce = (~integralSumCachesFn=(_, _) => None, discreteShapes): PointSetTypes.discreteShape => let reduce = (
discreteShapes |> E.A.fold_left(combinePointwise(~integralSumCachesFn), empty) ~integralSumCachesFn=(_, _) => None,
fn: (float, float) => result<float, 'e>,
discreteShapes: array<PointSetTypes.discreteShape>,
): result<t, 'e> => {
let merge = combinePointwise(~integralSumCachesFn, fn)
discreteShapes |> E.A.R.foldM(merge, empty)
}
let updateIntegralSumCache = (integralSumCache, t: t): t => { let updateIntegralSumCache = (integralSumCache, t: t): t => {
...t, ...t,

View File

@ -316,7 +316,10 @@ let combinePointwise = (
t2: t, t2: t,
): result<t, 'e> => { ): result<t, 'e> => {
let reducedDiscrete = let reducedDiscrete =
[t1, t2] |> E.A.fmap(toDiscrete) |> E.A.O.concatSomes |> Discrete.reduce(~integralSumCachesFn) [t1, t2]
|> E.A.fmap(toDiscrete)
|> E.A.O.concatSomes
|> Discrete.reduce(~integralSumCachesFn, fn)
let reducedContinuous = let reducedContinuous =
[t1, t2] [t1, t2]
@ -335,11 +338,11 @@ let combinePointwise = (
t1.integralCache, t1.integralCache,
t2.integralCache, t2.integralCache,
) )
reducedContinuous->E.R2.fmap(continuous => E.R.merge(reducedContinuous, reducedDiscrete)->E.R2.fmap(((continuous, discrete)) =>
make( make(
~integralSumCache=combinedIntegralSum, ~integralSumCache=combinedIntegralSum,
~integralCache=combinedIntegral, ~integralCache=combinedIntegral,
~discrete=reducedDiscrete, ~discrete,
~continuous, ~continuous,
) )
) )

View File

@ -84,7 +84,12 @@ let combinePointwise = (
m2, m2,
)->E.R2.fmap(x => PointSetTypes.Continuous(x)) )->E.R2.fmap(x => PointSetTypes.Continuous(x))
| (Discrete(m1), Discrete(m2)) => | (Discrete(m1), Discrete(m2)) =>
Ok(PointSetTypes.Discrete(Discrete.combinePointwise(~integralSumCachesFn, m1, m2))) Discrete.combinePointwise(
~integralSumCachesFn,
fn,
m1,
m2,
)->E.R2.fmap(x => PointSetTypes.Discrete(x))
| (m1, m2) => | (m1, m2) =>
Mixed.combinePointwise( Mixed.combinePointwise(
~integralSumCachesFn, ~integralSumCachesFn,

View File

@ -211,6 +211,15 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
| ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist) | ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist)
| ("isNormalized", [EvDistribution(dist)]) => Helpers.toBoolFn(IsNormalized, dist) | ("isNormalized", [EvDistribution(dist)]) => Helpers.toBoolFn(IsNormalized, dist)
| ("toPointSet", [EvDistribution(dist)]) => Helpers.toDistFn(ToPointSet, dist) | ("toPointSet", [EvDistribution(dist)]) => Helpers.toDistFn(ToPointSet, dist)
| ("scaleLog", [EvDistribution(dist)]) =>
Helpers.toDistFn(Scale(#Logarithm, MagicNumbers.Math.e), dist)
| ("scaleLog10", [EvDistribution(dist)]) => Helpers.toDistFn(Scale(#Logarithm, 10.0), dist)
| ("scaleLog", [EvDistribution(dist), EvNumber(float)]) =>
Helpers.toDistFn(Scale(#Logarithm, float), dist)
| ("scalePow", [EvDistribution(dist), EvNumber(float)]) =>
Helpers.toDistFn(Scale(#Power, float), dist)
| ("scaleExp", [EvDistribution(dist)]) =>
Helpers.toDistFn(Scale(#Power, MagicNumbers.Math.e), dist)
| ("cdf", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Cdf(float), dist) | ("cdf", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Cdf(float), dist)
| ("pdf", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Pdf(float), dist) | ("pdf", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Pdf(float), dist)
| ("inv", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Inv(float), dist) | ("inv", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Inv(float), dist)