From 89e07dad864b815c4764c9b85da45ae1e1765ad4 Mon Sep 17 00:00:00 2001 From: Sebastian Kosch Date: Mon, 6 Jul 2020 21:08:56 -0700 Subject: [PATCH] Fix truncation --- src/components/DistBuilder.re | 2 +- .../expressionTree/ExpressionTreeEvaluator.re | 21 ++++++++----------- .../expressionTree/ExpressionTypes.re | 6 ++++++ src/distPlus/symbolic/SymbolicDist.re | 5 ----- src/distPlus/symbolic/SymbolicTypes.re | 8 ++++++- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/components/DistBuilder.re b/src/components/DistBuilder.re index 77c64aab..f087a6bf 100644 --- a/src/components/DistBuilder.re +++ b/src/components/DistBuilder.re @@ -172,7 +172,7 @@ let make = () => { ~onSubmit=({state}) => {None}, ~initialState={ //guesstimatorString: "mm(normal(-10, 2), uniform(18, 25), lognormal({mean: 10, stdev: 8}), triangular(31,40,50))", - guesstimatorString: "uniform(0, 1) * normal(1, 2) - 1", + guesstimatorString: "truncate(normal(100, 60), 50, 100)", domainType: "Complete", xPoint: "50.0", xPoint2: "60.0", diff --git a/src/distPlus/expressionTree/ExpressionTreeEvaluator.re b/src/distPlus/expressionTree/ExpressionTreeEvaluator.re index 2325a134..23853db8 100644 --- a/src/distPlus/expressionTree/ExpressionTreeEvaluator.re +++ b/src/distPlus/expressionTree/ExpressionTreeEvaluator.re @@ -118,16 +118,17 @@ module PointwiseCombination = { }; module Truncate = { - let trySimplification = (leftCutoff, rightCutoff, t) => { + let trySimplification = (leftCutoff, rightCutoff, t): simplificationResult => { switch (leftCutoff, rightCutoff, t) { - | (None, None, t) => Ok(t) + | (None, None, t) => `Solution(t) + | (Some(lc), Some(rc), t) when lc > rc => `Error("Left truncation bound must be smaller than right bound.") | (lc, rc, `SymbolicDist(`Uniform(u))) => // just create a new Uniform distribution let nu: SymbolicTypes.uniform = u; let newLow = max(E.O.default(neg_infinity, lc), nu.low); let newHigh = min(E.O.default(infinity, rc), nu.high); - Ok(`SymbolicDist(`Uniform({low: newLow, high: newHigh}))); - | (_, _, t) => Ok(t) + `Solution(`SymbolicDist(`Uniform({low: newLow, high: newHigh}))); + | _ => `NoSolution }; }; @@ -135,7 +136,6 @@ module Truncate = { // TODO: use named args in renderToShape; if we're lucky we can at least get the tail // of a distribution we otherwise wouldn't get at all let renderedShape = toLeaf(renderParams, `Render(t)); - switch (renderedShape) { | Ok(`RenderedDist(rs)) => let truncatedShape = @@ -157,13 +157,10 @@ module Truncate = { : result(node, string) => { t |> trySimplification(leftCutoff, rightCutoff) - |> E.R.bind( - _, - fun - | `SymbolicDist(d) as t => Ok(t) - | _ => - truncateAsShape(toLeaf, renderParams, leftCutoff, rightCutoff, t), - ); + |> fun + | `Solution(t) => Ok(t) + | `Error(e) => Error(e) + | `NoSolution => truncateAsShape(toLeaf, renderParams, leftCutoff, rightCutoff, t); }; }; diff --git a/src/distPlus/expressionTree/ExpressionTypes.re b/src/distPlus/expressionTree/ExpressionTypes.re index 06be9967..8b6ece67 100644 --- a/src/distPlus/expressionTree/ExpressionTypes.re +++ b/src/distPlus/expressionTree/ExpressionTypes.re @@ -18,3 +18,9 @@ module ExpressionTree = { | `FloatFromDist(distToFloatOperation, node) ]; }; + +type simplificationResult = [ + | `Solution(ExpressionTree.node) + | `Error(string) + | `NoSolution +]; diff --git a/src/distPlus/symbolic/SymbolicDist.re b/src/distPlus/symbolic/SymbolicDist.re index 96ecf0c1..3b4fb065 100644 --- a/src/distPlus/symbolic/SymbolicDist.re +++ b/src/distPlus/symbolic/SymbolicDist.re @@ -274,11 +274,6 @@ module T = { can still return an error if there is a serious problem, like in the case of a divide by 0. */ - type analyticalSimplificationResult = [ - | `AnalyticalSolution(SymbolicTypes.symbolicDist) - | `Error(string) - | `NoSolution - ]; let tryAnalyticalSimplification = ( d1: symbolicDist, diff --git a/src/distPlus/symbolic/SymbolicTypes.re b/src/distPlus/symbolic/SymbolicTypes.re index b372a00f..1a5dcf22 100644 --- a/src/distPlus/symbolic/SymbolicTypes.re +++ b/src/distPlus/symbolic/SymbolicTypes.re @@ -46,4 +46,10 @@ type symbolicDist = [ | `Triangular(triangular) | `ContinuousShape(continuousShape) | `Float(float) // Dirac delta at x. Practically useful only in the context of multimodals. -]; \ No newline at end of file +]; + +type analyticalSimplificationResult = [ + | `AnalyticalSolution(symbolicDist) + | `Error(string) + | `NoSolution +];