From 9b104521566ca6d1773b1072fe3266f46c7e9fc1 Mon Sep 17 00:00:00 2001 From: Sebastian Kosch Date: Fri, 12 Jun 2020 23:53:01 -0700 Subject: [PATCH] Fix division by zero --- src/distPlus/symbolic/SymbolicDist.re | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/distPlus/symbolic/SymbolicDist.re b/src/distPlus/symbolic/SymbolicDist.re index f141bf92..9743f587 100644 --- a/src/distPlus/symbolic/SymbolicDist.re +++ b/src/distPlus/symbolic/SymbolicDist.re @@ -410,6 +410,7 @@ module DistTree = { `Distribution(`Float(func(v1, v2))) } + | (`Distribution(`Normal(n2)), `Distribution(`Float(v1)), `AddOperation) | (`Distribution(`Float(v1)), `Distribution(`Normal(n2)), `AddOperation) => { let n: normal = {mean: v1 +. n2.mean, stdev: n2.stdev}; `Distribution(`Normal(n)) @@ -420,6 +421,22 @@ module DistTree = { `Distribution(`Normal(n)); } + | (`Distribution(`Normal(n1)), `Distribution(`Normal(n2)), `SubtractOperation) => { + let n: normal = {mean: n1.mean -. n2.mean, stdev: sqrt(n1.stdev ** 2. +. n2.stdev ** 2.)}; + `Distribution(`Normal(n)); + } + + | (`Distribution(`Lognormal(l1)), `Distribution(`Lognormal(l2)), `MultiplyOperation) => { + let l: lognormal = {mu: l1.mu +. l2.mu, sigma: l1.sigma +. l2.sigma}; + `Distribution(`Lognormal(l)); + } + + | (`Distribution(`Lognormal(l1)), `Distribution(`Lognormal(l2)), `DivideOperation) => { + let l: lognormal = {mu: l1.mu -. l2.mu, sigma: l1.sigma +. l2.sigma}; + `Distribution(`Lognormal(l)); + } + + /* General cases: convolve the XYShapes */ | (`Distribution(d1), `Distribution(d2), _) => { let (sc1, sd1) = renderDistributionToXYShape(d1, sampleCount); @@ -537,6 +554,9 @@ module DistTree = { let evaluateNormalize = (et: nodeResult, sampleCount: int) => { // just divide everything by the integral. switch (et) { + | `RenderedShape(sc, sd, 0.) => { + `RenderedShape(Distributions.Continuous.empty, Distributions.Discrete.empty, 0.) + } | `RenderedShape(sc, sd, i) => { // loop through all ys and divide them by i let normalize = (s: DistTypes.xyShape): DistTypes.xyShape => {xs: s.xs, ys: s.ys |> E.A.fmap(y => y /. i)};