From ee0ff39326645aa63e97a8289da5de954a3a3031 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 26 Feb 2020 10:08:37 +0000 Subject: [PATCH] Minor fix to get continuous-discrete magnitudes working --- showcase/entries/Continuous.re | 2 +- src/components/charts/DistPlusPlot.re | 18 ++++++++++++++++++ .../DistributionPlot/DistributionPlot.re | 8 ++++++++ .../charts/DistributionPlot/distPlotReact.js | 4 ++-- src/distributions/Distributions.re | 18 ++++++++++++++++++ src/interface/FormBuilder.re | 1 - 6 files changed, 47 insertions(+), 4 deletions(-) diff --git a/showcase/entries/Continuous.re b/showcase/entries/Continuous.re index a86d6004..12d34bd3 100644 --- a/showcase/entries/Continuous.re +++ b/showcase/entries/Continuous.re @@ -26,7 +26,7 @@ let distributions = () => {setup( DistPlusIngredients.make( ~guesstimatorString= - "uniform(0,1) > 0.036 ? lognormal(6.652, -0.41): 0", + "uniform(0,1) > 0.3 ? lognormal(6.652, -0.41): 0", ~domain= RightLimited({xPoint: 50.0, excludingProbabilityMass: 0.3}), (), diff --git a/src/components/charts/DistPlusPlot.re b/src/components/charts/DistPlusPlot.re index 57df8dad..38a09448 100644 --- a/src/components/charts/DistPlusPlot.re +++ b/src/components/charts/DistPlusPlot.re @@ -1,3 +1,15 @@ +let adjustBoth = discreteProbabilityMass => { + let yMaxDiscreteDomainFactor = discreteProbabilityMass; + let yMaxContinuousDomainFactor = 1.0 -. discreteProbabilityMass; + let yMax = + yMaxDiscreteDomainFactor > yMaxContinuousDomainFactor + ? yMaxDiscreteDomainFactor : yMaxContinuousDomainFactor; + ( + 1.0 /. (yMaxDiscreteDomainFactor /. yMax), + 1.0 /. (yMaxContinuousDomainFactor /. yMax), + ); +}; + module DistPlusChart = { [@react.component] let make = (~distPlus: DistTypes.distPlus, ~onHover) => { @@ -16,9 +28,15 @@ module DistPlusChart = { let maxX = T.maxX(distPlus); let timeScale = distPlus.unit |> DistTypes.DistributionUnit.toJson; + let toDiscreteProbabilityMass = + distPlus |> Distributions.DistPlus.T.toDiscreteProbabilityMass; + let (yMaxDiscreteDomainFactor, yMaxContinuousDomainFactor) = + adjustBoth(toDiscreteProbabilityMass); (), ~continuous=?, ~discrete=?, @@ -44,6 +46,8 @@ module RawPlot = { ~marginTop?, ~maxX?, ~minX?, + ~yMaxContinuousDomainFactor?, + ~yMaxDiscreteDomainFactor?, ~onHover, ~continuous?, ~discrete?, @@ -93,6 +97,8 @@ let make = ~height=200, ~maxX=?, ~minX=?, + ~yMaxDiscreteDomainFactor=?, + ~yMaxContinuousDomainFactor=?, ~onHover: float => unit, ~continuous=?, ~scale=?, @@ -105,6 +111,8 @@ let make = E.O.fmap(XYShape.toJs)} diff --git a/src/components/charts/DistributionPlot/distPlotReact.js b/src/components/charts/DistributionPlot/distPlotReact.js index 805e6417..8b5dd6e5 100644 --- a/src/components/charts/DistributionPlot/distPlotReact.js +++ b/src/components/charts/DistributionPlot/distPlotReact.js @@ -50,8 +50,8 @@ function CdfChartReact(props) { .set('container', containerRef.current) .set('scale', scale) .set('timeScale', props.timeScale) - .set('yMaxContinuousDomainFactor', 1) - .set('yMaxDiscreteDomainFactor', 1) + .set('yMaxContinuousDomainFactor', props.yMaxContinuousDomainFactor) + .set('yMaxDiscreteDomainFactor', props.yMaxDiscreteDomainFactor) .data({ continuous: props.continuous, discrete: props.discrete, diff --git a/src/distributions/Distributions.re b/src/distributions/Distributions.re index 111b6ef0..9026ff42 100644 --- a/src/distributions/Distributions.re +++ b/src/distributions/Distributions.re @@ -25,6 +25,7 @@ module type dist = { let toDiscrete: t => option(DistTypes.discreteShape); let toScaledContinuous: t => option(DistTypes.continuousShape); let toScaledDiscrete: t => option(DistTypes.discreteShape); + let toDiscreteProbabilityMass: t => float; type integral; let integral: (~cache: option(integral), t) => integral; @@ -45,6 +46,7 @@ module Dist = (T: dist) => { let pointwiseFmap = T.pointwiseFmap; let xToY = T.xToY; let toShape = T.toShape; + let toDiscreteProbabilityMass = T.toDiscreteProbabilityMass; let toContinuous = T.toContinuous; let toDiscrete = T.toDiscrete; let toScaledContinuous = T.toScaledContinuous; @@ -103,6 +105,7 @@ module Continuous = { type integral = DistTypes.continuousShape; let minX = shapeFn(XYShape.minX); let maxX = shapeFn(XYShape.maxX); + let toDiscreteProbabilityMass = t => 0.0; let pointwiseFmap = (fn, t: t) => t |> xyShape |> XYShape.pointwiseMap(fn) |> fromShape; let toShape = (t: t): DistTypes.shape => Continuous(t); @@ -160,6 +163,7 @@ module Discrete = { t |> integral(~cache) |> Continuous.lastY; let minX = XYShape.minX; let maxX = XYShape.maxX; + let toDiscreteProbabilityMass = t => 1.0; let pointwiseFmap = XYShape.pointwiseMap; let toShape = (t: t): DistTypes.shape => Discrete(t); let toContinuous = _ => None; @@ -224,6 +228,7 @@ module Mixed = { let toShape = (t: t): DistTypes.shape => Mixed(t); let toContinuous = ({continuous}: t) => Some(continuous); let toDiscrete = ({discrete}: t) => Some(discrete); + let toDiscreteProbabilityMass = ({discreteProbabilityMassFraction}: t) => discreteProbabilityMassFraction; let xToY = (f, {discrete, continuous} as t: t) => { let c = continuous @@ -356,6 +361,17 @@ module Shape = { Continuous.T.toDiscrete, ), ); + + let toDiscreteProbabilityMass = (t: t) => + mapToAll( + t, + ( + Mixed.T.toDiscreteProbabilityMass, + Discrete.T.toDiscreteProbabilityMass, + Continuous.T.toDiscreteProbabilityMass, + ), + ); + let toScaledDiscrete = (t: t) => mapToAll( t, @@ -508,6 +524,8 @@ module DistPlus = { let minX = shapeFn(Shape.T.minX); let maxX = shapeFn(Shape.T.maxX); + let toDiscreteProbabilityMass = + shapeFn(Shape.T.toDiscreteProbabilityMass); // This bit is kind of akward, could probably use rethinking. let integral = (~cache, t: t) => diff --git a/src/interface/FormBuilder.re b/src/interface/FormBuilder.re index eea420b4..0b7b85e3 100644 --- a/src/interface/FormBuilder.re +++ b/src/interface/FormBuilder.re @@ -64,7 +64,6 @@ module ModelForm = { {type_.name |> ReasonReact.string}