Adding render options and params

This commit is contained in:
Ozzie Gooen 2020-04-04 21:37:58 +01:00
parent db78bc07f3
commit e84cfd4c92
11 changed files with 251 additions and 68 deletions

View File

@ -15,12 +15,8 @@ let timeDist =
); );
let setup = dist => let setup = dist =>
dist RenderTypes.DistPlus.make(~distPlusIngredients=dist,())
|> DistPlusIngredients.toDistPlus( |> DistPlusIngredients.toDistPlus
~sampleCount=2000,
~outputXYPoints=1000,
~truncateTo=Some(1000),
)
|> E.O.React.fmapOrNull(distPlus => <DistPlusPlot distPlus />); |> E.O.React.fmapOrNull(distPlus => <DistPlusPlot distPlus />);
let simpleExample = (name, guesstimatorString) => let simpleExample = (name, guesstimatorString) =>

View File

@ -22,6 +22,7 @@ module KDE = {
|> JS.jsToDist; |> JS.jsToDist;
}; };
// Note: This was an experiment, but it didn't actually work that well.
let inGroups = (samples, outputXYPoints, kernelWidth, ~cuttoff=0.9, ()) => { let inGroups = (samples, outputXYPoints, kernelWidth, ~cuttoff=0.9, ()) => {
let partitionAt = let partitionAt =
samples samples
@ -88,33 +89,94 @@ module T = {
(continuous, discrete); (continuous, discrete);
}; };
let kde = (~samples, ~outputXYPoints) => { let xWidthToUnitWidth = (samples, outputXYPoints, xWidth) => {
let width = Bandwidth.nrd0(samples);
let xyPointRange = E.A.Sorted.range(samples) |> E.O.default(0.0); let xyPointRange = E.A.Sorted.range(samples) |> E.O.default(0.0);
let xyPointWidth = xyPointRange /. float_of_int(outputXYPoints); let xyPointWidth = xyPointRange /. float_of_int(outputXYPoints);
let kernelWidth = int_of_float(Jstat.max([|(width /. xyPointWidth), 1.0 |])); xWidth /. xyPointWidth;
KDE.normalSampling(samples, outputXYPoints, kernelWidth); };
let formatUnitWidth = w => Jstat.max([|w, 1.0|]) |> int_of_float;
let suggestedUnitWidth = (samples, outputXYPoints) => {
let suggestedXWidth = Bandwidth.nrd0(samples);
xWidthToUnitWidth(samples, outputXYPoints, suggestedXWidth);
};
let kde = (~samples, ~outputXYPoints, width) => {
KDE.normalSampling(samples, outputXYPoints, width);
}; };
// todo: Figure out some way of doing this without having to integrate so many times. // todo: Figure out some way of doing this without having to integrate so many times.
let toShape = (~samples: t, ~outputXYPoints=3000, ~kernelWidth=10, ()) => { let toShape =
(~samples: t, ~samplingInputs: RenderTypes.Sampling.Inputs.fInputs, ()) => {
Array.fast_sort(compare, samples); Array.fast_sort(compare, samples);
let (continuousPart, discretePart) = E.A.Sorted.Floats.split(samples); let (continuousPart, discretePart) = E.A.Sorted.Floats.split(samples);
let length = samples |> E.A.length; let length = samples |> E.A.length |> float_of_int;
let lengthFloat = float_of_int(length);
let discrete: DistTypes.xyShape = let discrete: DistTypes.xyShape =
discretePart discretePart
|> E.FloatFloatMap.fmap(r => r /. lengthFloat) |> E.FloatFloatMap.fmap(r => r /. length)
|> E.FloatFloatMap.toArray |> E.FloatFloatMap.toArray
|> XYShape.T.fromZippedArray; |> XYShape.T.fromZippedArray;
let pdf: DistTypes.xyShape =
let pdf =
continuousPart |> E.A.length > 5 continuousPart |> E.A.length > 5
? { ? {
continuousPart |> kde(~samples=_, ~outputXYPoints) let _suggestedXWidth = Bandwidth.nrd0(continuousPart);
let _suggestedUnitWidth =
suggestedUnitWidth(continuousPart, samplingInputs.outputXYPoints);
let usedWidth =
samplingInputs.kernelWidth |> E.O.default(_suggestedXWidth);
let usedUnitWidth =
xWidthToUnitWidth(
samples,
samplingInputs.outputXYPoints,
usedWidth,
);
let foo: RenderTypes.Sampling.samplingStats = {
sampleCount: samplingInputs.sampleCount,
outputXYPoints: samplingInputs.outputXYPoints,
bandwidthXSuggested: _suggestedXWidth,
bandwidthUnitSuggested: _suggestedUnitWidth,
bandwidthXImplemented: usedWidth,
bandwidthUnitImplemented: usedUnitWidth,
};
continuousPart
|> kde(
~samples=_,
~outputXYPoints=samplingInputs.outputXYPoints,
formatUnitWidth(usedUnitWidth),
)
|> Distributions.Continuous.make(`Linear)
|> (r => Some((r, foo)));
} }
: {xs: [||], ys: [||]}; : None;
let continuous = pdf |> Distributions.Continuous.make(`Linear); let shape =
let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete); MixedShapeBuilder.buildSimple(
shape; ~continuous=pdf |> E.O.fmap(fst),
~discrete,
);
let samplesParse: RenderTypes.Sampling.outputs = {
continuousParseParams: pdf |> E.O.fmap(snd),
shape,
};
samplesParse;
};
let fromGuesstimatorString =
(
~guesstimatorString,
~samplingInputs=RenderTypes.Sampling.Inputs.empty,
(),
) => {
let hasValidSamples =
Guesstimator.stringToSamples(guesstimatorString, 10) |> E.A.length > 0;
let samplingInputs = RenderTypes.Sampling.Inputs.toF(samplingInputs);
switch (hasValidSamples) {
| false => None
| true =>
let samples =
Guesstimator.stringToSamples(guesstimatorString, samplingInputs.sampleCount);
Some(toShape(~samples, ~samplingInputs, ()));
};
}; };
}; };

View File

@ -26,7 +26,7 @@ type options = {
sampleCount: int, sampleCount: int,
outputXYPoints: int, outputXYPoints: int,
truncateTo: option(int), truncateTo: option(int),
kernelWidth: int, kernelWidth: option(float),
}; };
module Form = ReForm.Make(FormConfig); module Form = ReForm.Make(FormConfig);
@ -111,6 +111,13 @@ module Styles = {
]); ]);
}; };
type inputs = {
samplingInputs: RenderTypes.Sampling.inputs,
guesstimatorString: string,
length: int,
shouldTruncateSampledDistribution: int,
};
module DemoDist = { module DemoDist = {
[@react.component] [@react.component]
let make = (~guesstimatorString, ~domain, ~unit, ~options) => { let make = (~guesstimatorString, ~domain, ~unit, ~options) => {
@ -119,14 +126,24 @@ module DemoDist = {
<div> <div>
{switch (domain, unit, options) { {switch (domain, unit, options) {
| (Some(domain), Some(unit), Some(options)) => | (Some(domain), Some(unit), Some(options)) =>
let distPlus = let distPlusIngredients =
DistPlusIngredients.make(~guesstimatorString, ~domain, ~unit, ()) DistPlusIngredients.make(
|> DistPlusIngredients.toDistPlus( ~guesstimatorString,
~sampleCount=options.sampleCount, ~domain,
~outputXYPoints=options.outputXYPoints, ~unit,
~truncateTo=options.truncateTo, (),
~kernelWidth=options.kernelWidth,
); );
let inputs =
RenderTypes.DistPlus.make(
~samplingInputs={
sampleCount: Some(options.sampleCount),
outputXYPoints: Some(options.outputXYPoints),
kernelWidth: options.kernelWidth,
},
~distPlusIngredients,
(),
);
let distPlus = DistPlusIngredients.toDistPlus(inputs);
switch (distPlus) { switch (distPlus) {
| Some(distPlus) => <DistPlusPlot distPlus /> | Some(distPlus) => <DistPlusPlot distPlus />
| _ => | _ =>
@ -160,9 +177,9 @@ let make = () => {
unitType: "UnspecifiedDistribution", unitType: "UnspecifiedDistribution",
zero: MomentRe.momentNow(), zero: MomentRe.momentNow(),
unit: "days", unit: "days",
sampleCount: "10000", sampleCount: "30000",
outputXYPoints: "500", outputXYPoints: "10000",
truncateTo: "100", truncateTo: "1000",
kernelWidth: "5", kernelWidth: "5",
}, },
(), (),
@ -246,7 +263,7 @@ let make = () => {
truncateTo: truncateTo:
int_of_float(truncateTo) > 0 int_of_float(truncateTo) > 0
? Some(int_of_float(truncateTo)) : None, ? Some(int_of_float(truncateTo)) : None,
kernelWidth: kernelWidth |> int_of_float, kernelWidth: kernelWidth == 0.0 ? None : Some(kernelWidth),
}) })
| _ => None | _ => None
}; };

View File

@ -15,41 +15,34 @@ let applyTruncation = (truncateTo, distPlus) =>
| _ => None | _ => None
}; };
// ~samplingInputs=RenderTypes.Sampling.Inputs.empty,
// ~truncateTo: option(int),
// t: distPlusIngredients,
//Make truncation optional
let toDistPlus = let toDistPlus =
( (
~sampleCount=2000, inputs:RenderTypes.DistPlus.inputs
~outputXYPoints=1500,
~truncateTo=Some(300),
~kernelWidth=5,
t: distPlusIngredients,
) )
: option(distPlus) => { : option(distPlus) => {
let toDist = shape => let toDist = shape =>
Distributions.DistPlus.make( Distributions.DistPlus.make(
~shape, ~shape,
~domain=t.domain, ~domain=inputs.distPlusIngredients.domain,
~unit=t.unit, ~unit=inputs.distPlusIngredients.unit,
~guesstimatorString=Some(t.guesstimatorString), ~guesstimatorString=Some(inputs.distPlusIngredients.guesstimatorString),
(), (),
) )
|> Distributions.DistPlus.T.scaleToIntegralSum(~intendedSum=1.0); |> Distributions.DistPlus.T.scaleToIntegralSum(~intendedSum=1.0);
let parsed1 = MathJsParser.fromString(t.guesstimatorString);
let shape = let shape =
switch (parsed1) { GuesstimatorToShape.run(
| Ok(r) => ~renderingInputs={
let shape = SymbolicDist.toShape(truncateTo |> E.O.default(10000), r); guesstimatorString: inputs.distPlusIngredients.guesstimatorString,
Some(shape |> toDist); shapeLength: inputs.recommendedLength,
| _ => },
let fewSamples = Guesstimator.stringToSamples(t.guesstimatorString, 10); ~samplingInputs=inputs.samplingInputs,
if (fewSamples |> E.A.length > 0) { )
let samples = |> GuesstimatorToShape.getShape;
Guesstimator.stringToSamples(t.guesstimatorString, sampleCount); //TODO: Apply truncation
let shape = let foo = shape |> E.O.fmap(toDist);
Samples.T.toShape(~samples, ~outputXYPoints, ~kernelWidth, ()); foo;
shape |> E.O.fmap(toDist) |> applyTruncation(truncateTo);
} else {
None;
};
};
shape;
}; };

View File

@ -0,0 +1,32 @@
let runSymbolic =
(renderingInputs: RenderTypes.primaryInputs) =>{
let graph = MathJsParser.fromString(renderingInputs.guesstimatorString);
graph |> E.R.fmap(g => RenderTypes.Symbolic.make(g, SymbolicDist.toShape(renderingInputs.shapeLength,g)))
}
let getShape = (r: RenderTypes.Combined.outputs) =>
switch (r.symbolic, r.sampling) {
| (Some(Ok({shape})), _) => Some(shape)
| (_, Some({shape})) => shape
| _ => None
};
let run =
(
~renderingInputs: RenderTypes.primaryInputs,
~samplingInputs: RenderTypes.Sampling.inputs,
)
: RenderTypes.Combined.outputs => {
let symbolic = runSymbolic(renderingInputs);
let sampling =
switch (symbolic) {
| Ok(r) => None
| Error(r) =>
Samples.T.fromGuesstimatorString(
~guesstimatorString=renderingInputs.guesstimatorString,
~samplingInputs,
(),
)
};
{symbolic: Some(symbolic), sampling};
};

View File

@ -8,7 +8,8 @@ type assumptions = {
discreteProbabilityMass: option(float), discreteProbabilityMass: option(float),
}; };
let buildSimple = (~continuous, ~discrete): option(DistTypes.shape) => { let buildSimple = (~continuous: option(DistTypes.continuousShape), ~discrete): option(DistTypes.shape) => {
let continuous = continuous |> E.O.default(Distributions.Continuous.make(`Linear, {xs: [||], ys: [||]}))
let cLength = let cLength =
continuous continuous
|> Distributions.Continuous.getShape |> Distributions.Continuous.getShape

View File

@ -0,0 +1,86 @@
type primaryInputs = {
guesstimatorString: string,
shapeLength: int,
};
module Sampling = {
type inputs = {
sampleCount: option(int),
outputXYPoints: option(int),
kernelWidth: option(float),
};
type samplingStats = {
sampleCount: int,
outputXYPoints: int,
bandwidthXSuggested: float,
bandwidthUnitSuggested: float,
bandwidthXImplemented: float,
bandwidthUnitImplemented: float,
};
type outputs = {
continuousParseParams: option(samplingStats),
shape: option(ProbExample.DistTypes.shape),
};
module Inputs = {
let defaultSampleCount = 5000;
let defaultOutputXYPoints = 10000;
let empty = {sampleCount: None, outputXYPoints: None, kernelWidth: None};
type fInputs = {
sampleCount: int,
outputXYPoints: int,
kernelWidth: option(float),
};
let toF = (i: inputs): fInputs => {
sampleCount: i.sampleCount |> E.O.default(defaultSampleCount),
outputXYPoints: i.outputXYPoints |> E.O.default(defaultOutputXYPoints),
kernelWidth: i.kernelWidth,
};
};
};
module Symbolic = {
type inputs = {length: int};
type outputs = {
graph: ProbExample.SymbolicDist.bigDist,
shape: ProbExample.DistTypes.shape,
};
let make = (graph, shape) => {graph, shape};
};
module Combined = {
type inputs = {
samplingInputs: Sampling.inputs,
symbolicInputs: Symbolic.inputs,
guesstimatorString: string,
};
type outputs = {
symbolic: option(Belt.Result.t(Symbolic.outputs, string)),
sampling: option(Sampling.outputs),
};
};
module DistPlus = {
let defaultRecommendedLength = 10000;
let defaultShouldTruncate = true;
type inputs = {
distPlusIngredients: DistTypes.distPlusIngredients,
samplingInputs: Sampling.inputs,
recommendedLength: int,
shouldTruncate: bool,
};
let make =
(
~samplingInputs=Sampling.Inputs.empty,
~recommendedLength=defaultRecommendedLength,
~shouldTruncate=defaultShouldTruncate,
~distPlusIngredients,
()
)
: inputs => {
distPlusIngredients,
samplingInputs,
recommendedLength,
shouldTruncate,
};
};

View File

@ -19,12 +19,8 @@ let propValue = (t: Prop.Value.t) => {
| ConditionalArray(r) => "Array" |> ReasonReact.string | ConditionalArray(r) => "Array" |> ReasonReact.string
| DistPlusIngredients((r: DistTypes.distPlusIngredients)) => | DistPlusIngredients((r: DistTypes.distPlusIngredients)) =>
let newDistribution = let newDistribution =
DistPlusIngredients.toDistPlus( RenderTypes.DistPlus.make(~distPlusIngredients=r, ~recommendedLength=1000, ~shouldTruncate=true,())
~sampleCount=1000, |> DistPlusIngredients.toDistPlus
~outputXYPoints=2000,
~truncateTo=Some(500),
r,
);
switch (newDistribution) { switch (newDistribution) {
| Some(distribution) => | Some(distribution) =>
<div> <DistPlusPlot distPlus=distribution /> </div> <div> <DistPlusPlot distPlus=distribution /> </div>

View File

@ -110,6 +110,7 @@ module Model = {
// TODO: Fixe number that integral is calculated for // TODO: Fixe number that integral is calculated for
let getGlobalCatastropheChance = dateTime => { let getGlobalCatastropheChance = dateTime => {
GlobalCatastrophe.makeI(MomentRe.momentNow()) GlobalCatastrophe.makeI(MomentRe.momentNow())
|> RenderTypes.DistPlus.make(~distPlusIngredients=_, ())
|> DistPlusIngredients.toDistPlus |> DistPlusIngredients.toDistPlus
|> E.O.bind(_, Distributions.DistPlusTime.Integral.xToY(Time(dateTime))); |> E.O.bind(_, Distributions.DistPlusTime.Integral.xToY(Time(dateTime)));
}; };

View File

@ -245,6 +245,5 @@ let fromString = str => {
} }
); );
let value = E.R.bind(mathJsParse, MathAdtToDistDst.run); let value = E.R.bind(mathJsParse, MathAdtToDistDst.run);
Js.log4("fromString", mathJsToJson, mathJsParse, value);
value; value;
}; };

View File

@ -296,7 +296,7 @@ module PointwiseAddDistributionsWeighted = {
let normalized = normalizeWeights(dists); let normalized = normalizeWeights(dists);
let continuous = normalized |> E.A.filter(((r,_)) => GenericSimple.contType(r) == `Continuous) |> continuousShape(_, sampleCount); let continuous = normalized |> E.A.filter(((r,_)) => GenericSimple.contType(r) == `Continuous) |> continuousShape(_, sampleCount);
let discrete = normalized |> E.A.filter(((r,_)) => GenericSimple.contType(r) == `Discrete) |> discreteShape(_, sampleCount); let discrete = normalized |> E.A.filter(((r,_)) => GenericSimple.contType(r) == `Discrete) |> discreteShape(_, sampleCount);
let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete); let shape = MixedShapeBuilder.buildSimple(~continuous=Some(continuous), ~discrete);
shape |> E.O.toExt("") shape |> E.O.toExt("")
}; };