From d9141f6b2bafbac79efe0950e92dc5ac527376cb Mon Sep 17 00:00:00 2001 From: Sebastian Kosch Date: Sat, 18 Jul 2020 21:43:27 -0700 Subject: [PATCH] Change extrapolation handling in Continuous.combinePointwise to explicit distributionType param --- .../distribution/AlgebraicShapeCombination.re | 27 +++---------------- src/distPlus/distribution/Continuous.re | 7 ++++- src/distPlus/distribution/DistTypes.re | 5 ++++ .../expressionTree/ExpressionTreeEvaluator.re | 2 +- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/distPlus/distribution/AlgebraicShapeCombination.re b/src/distPlus/distribution/AlgebraicShapeCombination.re index c12e96c7..edcf00ad 100644 --- a/src/distPlus/distribution/AlgebraicShapeCombination.re +++ b/src/distPlus/distribution/AlgebraicShapeCombination.re @@ -209,30 +209,9 @@ let toDiscretePointMassesFromDiscrete = (s: DistTypes.xyShape): pointMassesWithM let {xs, ys}: XYShape.T.t = s; let n = E.A.length(xs); - let masses: array(float) = Belt.Array.makeUninitializedUnsafe(n); // doesn't include the fake first and last points - let means: array(float) = Belt.Array.makeUninitializedUnsafe(n); - let variances: array(float) = Belt.Array.makeUninitializedUnsafe(n); - - for (i in 0 to n - 1) { - Belt.Array.set( - masses, - i, - ys[i] - ) |> ignore; - - Belt.Array.set( - means, - i, - xs[i] - ) |> ignore; - - Belt.Array.set( - variances, - i, - 0.0 - ) |> ignore; - (); - }; + let masses: array(float) = Belt.Array.makeBy(n, i => ys[i]); + let means: array(float) = Belt.Array.makeBy(n, i => xs[i]); + let variances: array(float) = Belt.Array.makeBy(n, i => 0.0); {n, masses, means, variances}; }; diff --git a/src/distPlus/distribution/Continuous.re b/src/distPlus/distribution/Continuous.re index 939c88aa..1adb60c0 100644 --- a/src/distPlus/distribution/Continuous.re +++ b/src/distPlus/distribution/Continuous.re @@ -41,7 +41,7 @@ let combinePointwise = ( ~integralSumCachesFn=(_, _) => None, ~integralCachesFn: (t, t) => option(t) =(_, _) => None, - ~extrapolation=`UseZero, + ~distributionType: DistTypes.distributionType = `PDF, fn: (float, float) => float, t1: DistTypes.continuousShape, t2: DistTypes.continuousShape, @@ -68,6 +68,11 @@ let combinePointwise = | (`Stepwise, `Linear) => (stepwiseToLinear(t1), t2); }; + let extrapolation = switch (distributionType) { + | `PDF => `UseZero + | `CDF => `UseOutermostPoints + }; + let interpolator = XYShape.XtoY.continuousInterpolator(t1.interpolation, extrapolation); make( diff --git a/src/distPlus/distribution/DistTypes.re b/src/distPlus/distribution/DistTypes.re index 62afdc22..92f03ee6 100644 --- a/src/distPlus/distribution/DistTypes.re +++ b/src/distPlus/distribution/DistTypes.re @@ -9,6 +9,11 @@ type domain = | RightLimited(domainLimit) | LeftAndRightLimited(domainLimit, domainLimit); +type distributionType = [ + | `PDF + | `CDF +]; + type xyShape = { xs: array(float), ys: array(float), diff --git a/src/distPlus/expressionTree/ExpressionTreeEvaluator.re b/src/distPlus/expressionTree/ExpressionTreeEvaluator.re index 0af87e56..0a4b7070 100644 --- a/src/distPlus/expressionTree/ExpressionTreeEvaluator.re +++ b/src/distPlus/expressionTree/ExpressionTreeEvaluator.re @@ -131,7 +131,7 @@ module PointwiseCombination = { `RenderedDist( Shape.combinePointwise( ~integralSumCachesFn=(a, b) => Some(a +. b), - ~integralCachesFn=(a, b) => Some(Continuous.combinePointwise(~extrapolation=`UseOutermostPoints, (+.), a, b)), + ~integralCachesFn=(a, b) => Some(Continuous.combinePointwise(~distributionType=`CDF, (+.), a, b)), (+.), rs1, rs2,