Cleanup to return more from DistPlusRenderer

This commit is contained in:
Ozzie Gooen 2020-04-05 12:00:25 +01:00
parent 990f01b8d6
commit 82595ae167
12 changed files with 132 additions and 105 deletions

View File

@ -5,41 +5,44 @@
// floor(3 to 4)
// uniform(0,1) > 0.3 ? lognormal(6.652, -0.41): 0
let timeDist =
DistPlusIngredients.make(
let timeDist ={
let ingredients = RenderTypes.DistPlusRenderer.Ingredients.make(
~guesstimatorString="(floor(10 to 15))",
~domain=RightLimited({xPoint: 50.0, excludingProbabilityMass: 0.3}),
~unit=
DistTypes.TimeDistribution({zero: MomentRe.momentNow(), unit: `years}),
(),
);
());
let inputs = RenderTypes.DistPlusRenderer.make(~distPlusIngredients=ingredients,())
inputs |> DistPlusRenderer.run
}
let setup = dist =>
RenderTypes.DistPlusRenderer.make(~distPlusIngredients=dist,())
|> DistPlusIngredients.toDistPlus
|> DistPlusRenderer.run
|> RenderTypes.DistPlusRenderer.Outputs.distplus
|> E.O.React.fmapOrNull(distPlus => <DistPlusPlot distPlus />);
let simpleExample = (name, guesstimatorString) =>
<>
<h3 className="text-gray-600 text-lg font-bold">
{name |> ReasonReact.string}
</h3>
{setup(DistPlusIngredients.make(~guesstimatorString, ()))}
</>;
// let simpleExample = (name, guesstimatorString) =>
// <>
// <h3 className="text-gray-600 text-lg font-bold">
// {name |> ReasonReact.string}
// </h3>
// {setup(DistPlusIngredients.make(~guesstimatorString, ()))}
// </>;
let timeExample = (name, guesstimatorString) =>
<>
<h3 className="text-gray-600 text-lg font-bold">
{name |> ReasonReact.string}
</h3>
{setup(
DistPlusIngredients.make(
~guesstimatorString,
~unit=TimeDistribution({zero: MomentRe.momentNow(), unit: `years}),
(),
),
)}
</>;
// let timeExample = (name, guesstimatorString) =>
// <>
// <h3 className="text-gray-600 text-lg font-bold">
// {name |> ReasonReact.string}
// </h3>
// {setup(
// DistPlusIngredients.make(
// ~guesstimatorString,
// ~unit=TimeDistribution({zero: MomentRe.momentNow(), unit: `years}),
// (),
// ),
// )}
// </>;
let distributions = () =>
<div>
@ -47,37 +50,37 @@ let distributions = () =>
<h2 className="text-gray-800 text-xl font-bold">
{"Initial Section" |> ReasonReact.string}
</h2>
{simpleExample("Continuous", "5 to 20")}
{simpleExample("Continuous, wide range", "1 to 1000000")}
{simpleExample("Continuous, tiny values", "0.000000001 to 0.00000001")}
{simpleExample(
"Continuous large values",
"50000000000000 to 200000000000000000",
)}
{simpleExample("Discrete", "floor(10 to 20)")}
{simpleExample(
"Discrete and below 0, normal(10,30)",
"floor(normal(10,30))",
)}
{simpleExample("Discrete, wide range", "floor(10 to 200000)")}
{simpleExample("Mixed", "mm(5 to 20, floor(20 to 30), [.5,.5])")}
{simpleExample("Mixed, Early-Discrete Point", "mm(1, 5 to 20, [.5,.5])")}
{simpleExample(
"Mixed, Two-Discrete Points",
"mm(0,10, 5 to 20, [.5,.5,.5])",
)}
<h2 className="text-gray-800 text-xl font-bold">
{"Over Time" |> ReasonReact.string}
</h2>
{timeExample("Continuous", "5 to 20")}
{timeExample("Continuous Over Long Period", "500 to 200000")}
{timeExample("Continuous Over Short Period", "0.0001 to 0.001")}
{timeExample(
"Continuous Over Very Long Period",
"500 to 20000000000000",
)}
{timeExample("Discrete", "floor(5 to 20)")}
{timeExample("Mixed", "mm(5 to 20, floor(5 to 20), [.5,.5])")}
// {simpleExample("Continuous", "5 to 20")}
// {simpleExample("Continuous, wide range", "1 to 1000000")}
// {simpleExample("Continuous, tiny values", "0.000000001 to 0.00000001")}
// {simpleExample(
// "Continuous large values",
// "50000000000000 to 200000000000000000",
// )}
// {simpleExample("Discrete", "floor(10 to 20)")}
// {simpleExample(
// "Discrete and below 0, normal(10,30)",
// "floor(normal(10,30))",
// )}
// {simpleExample("Discrete, wide range", "floor(10 to 200000)")}
// {simpleExample("Mixed", "mm(5 to 20, floor(20 to 30), [.5,.5])")}
// {simpleExample("Mixed, Early-Discrete Point", "mm(1, 5 to 20, [.5,.5])")}
// {simpleExample(
// "Mixed, Two-Discrete Points",
// "mm(0,10, 5 to 20, [.5,.5,.5])",
// )}
// <h2 className="text-gray-800 text-xl font-bold">
// {"Over Time" |> ReasonReact.string}
// </h2>
// {timeExample("Continuous", "5 to 20")}
// {timeExample("Continuous Over Long Period", "500 to 200000")}
// {timeExample("Continuous Over Short Period", "0.0001 to 0.001")}
// {timeExample(
// "Continuous Over Very Long Period",
// "500 to 20000000000000",
// )}
// {timeExample("Discrete", "floor(5 to 20)")}
// {timeExample("Mixed", "mm(5 to 20, floor(5 to 20), [.5,.5])")}
</div>
</div>;

View File

@ -127,7 +127,7 @@ module DemoDist = {
{switch (domain, unit, options) {
| (Some(domain), Some(unit), Some(options)) =>
let distPlusIngredients =
DistPlusIngredients.make(
RenderTypes.DistPlusRenderer.Ingredients.make(
~guesstimatorString,
~domain,
~unit,
@ -141,10 +141,12 @@ module DemoDist = {
kernelWidth: options.kernelWidth,
},
~distPlusIngredients,
~shouldTruncate=options.truncateTo |> E.O.isSome,
~recommendedLength=options.truncateTo |> E.O.default(10000),
(),
);
let distPlus = DistPlusIngredients.toDistPlus(inputs);
switch (distPlus) {
let response = DistPlusRenderer.run(inputs);
switch (RenderTypes.DistPlusRenderer.Outputs.distplus(response)) {
| Some(distPlus) => <DistPlusPlot distPlus />
| _ =>
"Correct Guesstimator string input to show a distribution."

View File

@ -60,12 +60,6 @@ type distPlus = {
guesstimatorString: option(string),
};
type distPlusIngredients = {
guesstimatorString: string,
domain,
unit: distributionUnit,
};
module DistributionUnit = {
let toJson = (distributionUnit: distributionUnit) =>
switch (distributionUnit) {

View File

@ -1,20 +1,14 @@
open DistTypes;
let make =
(~guesstimatorString, ~domain=Complete, ~unit=UnspecifiedDistribution, ())
: distPlusIngredients => {
guesstimatorString,
domain,
unit,
let truncateIfShould =
(
{recommendedLength, shouldTruncate}: RenderTypes.DistPlusRenderer.inputs,
{sampling:shape}: RenderTypes.ShapeRenderer.Combined.outputs,
dist,
) => {
(shouldTruncate && (shape |> E.O.isSome))
? dist |> Distributions.DistPlus.T.truncate(recommendedLength) : dist;
};
let truncateIfShould = (inputs: RenderTypes.DistPlusRenderer.inputs, dist) => {
inputs.shouldTruncate
? dist
: dist |> Distributions.DistPlus.T.truncate(inputs.recommendedLength);
};
let toDistPlus = (inputs: RenderTypes.DistPlusRenderer.inputs): option(distPlus) => {
let run = (inputs: RenderTypes.DistPlusRenderer.inputs): RenderTypes.DistPlusRenderer.outputs => {
let toDist = shape =>
Distributions.DistPlus.make(
~shape,
@ -24,16 +18,16 @@ let toDistPlus = (inputs: RenderTypes.DistPlusRenderer.inputs): option(distPlus)
(),
)
|> Distributions.DistPlus.T.scaleToIntegralSum(~intendedSum=1.0);
let shape =
GuesstimatorToShape.run({
let outputs =
ShapeRenderer.run({
samplingInputs: inputs.samplingInputs,
guesstimatorString: inputs.distPlusIngredients.guesstimatorString,
symbolicInputs: {
length: inputs.recommendedLength,
},
})
|> GuesstimatorToShape.getShape;
let shape = outputs |> RenderTypes.ShapeRenderer.Combined.getShape
let dist =
shape |> E.O.fmap(toDist) |> E.O.fmap(truncateIfShould(inputs));
dist;
shape |> E.O.fmap(toDist) |> E.O.fmap(truncateIfShould(inputs, outputs));
RenderTypes.DistPlusRenderer.Outputs.make(outputs, dist)
};

View File

@ -59,18 +59,43 @@ module ShapeRenderer = {
symbolic: option(Belt.Result.t(Symbolic.outputs, string)),
sampling: option(Sampling.outputs),
};
let getShape = (r: outputs) =>
switch (r.symbolic, r.sampling) {
| (Some(Ok({shape})), _) => Some(shape)
| (_, Some({shape})) => shape
| _ => None
};
};
};
module DistPlusRenderer = {
let defaultRecommendedLength = 10000;
let defaultShouldTruncate = true;
type ingredients = {
guesstimatorString: string,
domain: DistTypes.domain,
unit: DistTypes.distributionUnit,
};
type inputs = {
distPlusIngredients: DistTypes.distPlusIngredients,
distPlusIngredients: ingredients,
samplingInputs: ShapeRenderer.Sampling.inputs,
recommendedLength: int,
shouldTruncate: bool,
};
module Ingredients = {
let make =
(
~guesstimatorString,
~domain=DistTypes.Complete,
~unit=DistTypes.UnspecifiedDistribution,
(),
)
: ingredients => {
guesstimatorString,
domain,
unit,
};
};
let make =
(
~samplingInputs=ShapeRenderer.Sampling.Inputs.empty,
@ -85,4 +110,13 @@ module DistPlusRenderer = {
recommendedLength,
shouldTruncate,
};
type outputs = {
shapeRenderOutputs: ShapeRenderer.Combined.outputs,
distPlus: option(DistTypes.distPlus)
}
module Outputs = {
let distplus = (t:outputs) => t.distPlus
let shapeRenderOutputs = (t:outputs) => t.shapeRenderOutputs
let make = (shapeRenderOutputs, distPlus) => {shapeRenderOutputs, distPlus};
}
};

View File

@ -4,13 +4,6 @@ let runSymbolic =
graph |> E.R.fmap(g => RenderTypes.ShapeRenderer.Symbolic.make(g, SymbolicDist.toShape(length,g)))
}
let getShape = (r: RenderTypes.ShapeRenderer.Combined.outputs) =>
switch (r.symbolic, r.sampling) {
| (Some(Ok({shape})), _) => Some(shape)
| (_, Some({shape})) => shape
| _ => None
};
let run =
(
inputs: RenderTypes.ShapeRenderer.Combined.inputs
@ -19,8 +12,8 @@ let run =
let symbolic = runSymbolic(inputs.guesstimatorString, inputs.symbolicInputs.length);
let sampling =
switch (symbolic) {
| Ok(r) => None
| Error(r) =>
| Ok(_) => None
| Error(_) =>
Samples.T.fromGuesstimatorString(
~guesstimatorString=inputs.guesstimatorString,
~samplingInputs=inputs.samplingInputs,

View File

@ -17,10 +17,16 @@ let propValue = (t: Prop.Value.t) => {
switch (t) {
| SelectSingle(r) => r |> ReasonReact.string
| ConditionalArray(r) => "Array" |> ReasonReact.string
| DistPlusIngredients((r: DistTypes.distPlusIngredients)) =>
| DistPlusIngredients((r: RenderTypes.DistPlusRenderer.ingredients)) =>
let newDistribution =
RenderTypes.DistPlusRenderer.make(~distPlusIngredients=r, ~recommendedLength=1000, ~shouldTruncate=true,())
|> DistPlusIngredients.toDistPlus
RenderTypes.DistPlusRenderer.make(
~distPlusIngredients=r,
~recommendedLength=10000,
~shouldTruncate=true,
(),
)
|> DistPlusRenderer.run
|> RenderTypes.DistPlusRenderer.Outputs.distplus;
switch (newDistribution) {
| Some(distribution) =>
<div> <DistPlusPlot distPlus=distribution /> </div>

View File

@ -9,7 +9,7 @@ module Value = {
| DateTime(MomentRe.Moment.t)
| FloatPoint(float)
| Probability(float)
| DistPlusIngredients(DistTypes.distPlusIngredients)
| DistPlusIngredients(RenderTypes.DistPlusRenderer.ingredients)
| ConditionalArray(array(conditional))
| FloatCdf(string);
@ -85,7 +85,7 @@ module ValueCluster = {
[ | `combination(range(MomentRe.Moment.t)) | `item(string)],
)
| Probability([ | `item(string)])
| DistPlusIngredients([ | `item(DistTypes.distPlusIngredients)])
| DistPlusIngredients([ | `item(RenderTypes.DistPlusRenderer.ingredients)])
| ConditionalArray([ | `item(array(conditional))])
| FloatCdf([ | `item(string)]);
};

View File

@ -111,7 +111,8 @@ module Model = {
let getGlobalCatastropheChance = dateTime => {
GlobalCatastrophe.makeI(MomentRe.momentNow())
|> RenderTypes.DistPlusRenderer.make(~distPlusIngredients=_, ())
|> DistPlusIngredients.toDistPlus
|> DistPlusRenderer.run
|> RenderTypes.DistPlusRenderer.Outputs.distplus
|> E.O.bind(_, Distributions.DistPlusTime.Integral.xToY(Time(dateTime)));
};
@ -152,7 +153,7 @@ module Model = {
};
let distPlusIngredients =
DistPlusIngredients.make(
RenderTypes.DistPlusRenderer.Ingredients.make(
~guesstimatorString=str,
~domain=Complete,
~unit=UnspecifiedDistribution,
@ -162,7 +163,7 @@ module Model = {
| CHANCE_OF_EXISTENCE =>
Prop.Value.DistPlusIngredients(
DistPlusIngredients.make(
RenderTypes.DistPlusRenderer.Ingredients.make(
~guesstimatorString=
GuesstimatorDist.min(
GlobalCatastrophe.guesstimatorString,

View File

@ -1,7 +1,7 @@
let guesstimatorString = "uniform(1, 100)";
let makeI = (currentDateTime: MomentRe.Moment.t) => {
DistPlusIngredients.make(
RenderTypes.DistPlusRenderer.Ingredients.make(
~guesstimatorString,
~unit=TimeDistribution({zero: currentDateTime, unit: `years}),
~domain=RightLimited({xPoint: 300.0, excludingProbabilityMass: 0.3}),

View File

@ -2,7 +2,7 @@ let guesstimatorString = age =>
GuesstimatorDist.normal(72.0 -. age, 5.0 -. age *. 0.01);
let makeI = (age: float) => {
DistPlusIngredients.make(
RenderTypes.DistPlusRenderer.Ingredients.make(
~guesstimatorString=guesstimatorString(age),
~unit=TimeDistribution({zero: MomentRe.momentNow(), unit: `years}),
~domain=RightLimited({xPoint: 300.0, excludingProbabilityMass: 0.3}),