Added smarter truncator

This commit is contained in:
Ozzie Gooen 2020-03-18 20:50:01 +00:00
parent d0825581af
commit 49b78ae814
5 changed files with 28 additions and 19 deletions

View File

@ -151,7 +151,7 @@ let make = () => {
~schema, ~schema,
~onSubmit=({state}) => {None}, ~onSubmit=({state}) => {None},
~initialState={ ~initialState={
guesstimatorString: "mm(40 to 80, floor(50 to 80), [.5,.5])", guesstimatorString: "40 to 8000",
domainType: "Complete", domainType: "Complete",
xPoint: "50.0", xPoint: "50.0",
xPoint2: "60.0", xPoint2: "60.0",
@ -162,7 +162,7 @@ let make = () => {
unit: "days", unit: "days",
sampleCount: "10000", sampleCount: "10000",
outputXYPoints: "500", outputXYPoints: "500",
truncateTo: "0", truncateTo: "100",
kernelWidth: "5", kernelWidth: "5",
}, },
(), (),

View File

@ -23,7 +23,6 @@ let toDistPlus =
~sampleCount, ~sampleCount,
~outputXYPoints, ~outputXYPoints,
~kernelWidth, ~kernelWidth,
~truncateTo,
(), (),
); );
let distPlus = let distPlus =
@ -40,5 +39,9 @@ let toDistPlus =
|> E.O.fmap( |> E.O.fmap(
Distributions.DistPlus.T.scaleToIntegralSum(~intendedSum=1.0), Distributions.DistPlus.T.scaleToIntegralSum(~intendedSum=1.0),
); );
distPlus; switch (truncateTo, distPlus) {
| (Some(t), Some(d)) => Some(d |> Distributions.DistPlus.T.truncate(t))
| (None, Some(d)) => Some(d)
| _ => None
};
}; };

View File

@ -16,10 +16,10 @@ let max = (f1: option(float), f2: option(float)) =>
module type dist = { module type dist = {
type t; type t;
type integral;
let minX: t => option(float); let minX: t => option(float);
let maxX: t => option(float); let maxX: t => option(float);
let pointwiseFmap: (float => float, t) => t; let pointwiseFmap: (float => float, t) => t;
let truncate: (int, t) => t;
let xToY: (float, t) => DistTypes.mixedPoint; let xToY: (float, t) => DistTypes.mixedPoint;
let toShape: t => DistTypes.shape; let toShape: t => DistTypes.shape;
let toContinuous: t => option(DistTypes.continuousShape); let toContinuous: t => option(DistTypes.continuousShape);
@ -27,8 +27,8 @@ module type dist = {
let toScaledContinuous: t => option(DistTypes.continuousShape); let toScaledContinuous: t => option(DistTypes.continuousShape);
let toScaledDiscrete: t => option(DistTypes.discreteShape); let toScaledDiscrete: t => option(DistTypes.discreteShape);
let toDiscreteProbabilityMass: t => float; let toDiscreteProbabilityMass: t => float;
let truncate: (~cache: option(integral)=?, int, t) => t;
type integral;
let integral: (~cache: option(integral), t) => integral; let integral: (~cache: option(integral), t) => integral;
let integralEndY: (~cache: option(integral), t) => float; let integralEndY: (~cache: option(integral), t) => float;
let integralXtoY: (~cache: option(integral), float, t) => float; let integralXtoY: (~cache: option(integral), float, t) => float;
@ -112,7 +112,6 @@ module Continuous = {
let toDiscreteProbabilityMass = _ => 0.0; let toDiscreteProbabilityMass = _ => 0.0;
let pointwiseFmap = (fn, t: t) => let pointwiseFmap = (fn, t: t) =>
t |> xyShape |> XYShape.T.pointwiseMap(fn) |> fromShape; t |> xyShape |> XYShape.T.pointwiseMap(fn) |> fromShape;
let truncate = i => shapeMap(XYShape.T.convertToNewLength(i));
let toShape = (t: t): DistTypes.shape => Continuous(t); let toShape = (t: t): DistTypes.shape => Continuous(t);
let xToY = (f, {interpolation, xyShape}: t) => let xToY = (f, {interpolation, xyShape}: t) =>
switch (interpolation) { switch (interpolation) {
@ -144,6 +143,14 @@ module Continuous = {
|> E.O.toExt("This should not have happened") |> E.O.toExt("This should not have happened")
|> fromShape |> fromShape
}; };
let truncate = (~cache=None, i, t) =>
t
|> shapeMap(
XYShape.T.convertToNewLengthByProbabilityMass(
i,
integral(~cache, t).xyShape,
),
);
let integralEndY = (~cache, t) => t |> integral(~cache) |> lastY; let integralEndY = (~cache, t) => t |> integral(~cache) |> lastY;
let integralXtoY = (~cache, f, t) => let integralXtoY = (~cache, f, t) =>
t |> integral(~cache) |> shapeFn(XYShape.T.findY(f)); t |> integral(~cache) |> shapeFn(XYShape.T.findY(f));
@ -185,7 +192,7 @@ module Discrete = {
let toDiscrete = t => Some(t); let toDiscrete = t => Some(t);
let toScaledContinuous = _ => None; let toScaledContinuous = _ => None;
let toScaledDiscrete = t => Some(t); let toScaledDiscrete = t => Some(t);
let truncate = (i, t: t): DistTypes.discreteShape => let truncate = (~cache=None, i, t: t): DistTypes.discreteShape =>
t t
|> XYShape.T.zip |> XYShape.T.zip
|> XYShape.T.Zipped.sortByY |> XYShape.T.Zipped.sortByY
@ -267,6 +274,7 @@ module Mixed = {
let truncate = let truncate =
( (
~cache=None,
count, count,
{discrete, continuous, discreteProbabilityMassFraction} as t: t, {discrete, continuous, discreteProbabilityMassFraction} as t: t,
) )
@ -414,7 +422,7 @@ module Shape = {
), ),
); );
let truncate = (i, t: t) => let truncate = (~cache=None, i, t: t) =>
fmap( fmap(
t, t,
( (
@ -603,7 +611,7 @@ module DistPlus = {
let integral = (~cache, t: t) => let integral = (~cache, t: t) =>
updateShape(Continuous(t.integralCache), t); updateShape(Continuous(t.integralCache), t);
let truncate = (i, t) => let truncate = (~cache=None, i, t) =>
updateShape(t |> toShape |> Shape.T.truncate(i), t); updateShape(t |> toShape |> Shape.T.truncate(i), t);
// todo: adjust for limit, maybe? // todo: adjust for limit, maybe?
let pointwiseFmap = (fn, {shape, _} as t: t): t => let pointwiseFmap = (fn, {shape, _} as t: t): t =>

View File

@ -112,6 +112,13 @@ module T = {
); );
}; };
let convertToNewLengthByProbabilityMass =
(newLength: int, integral: t, t: t): DistTypes.xyShape => {
Functions.range(0.0, 1.0, newLength)
|> E.A.fmap(findX(_, integral))
|> convertWithAlternativeXs(_, t);
};
module XtoY = { module XtoY = {
let stepwiseIncremental = (f, t: t) => let stepwiseIncremental = (f, t: t) =>
firstPairAtOrBeforeValue(f, t) |> E.O.fmap(((_, y)) => y); firstPairAtOrBeforeValue(f, t) |> E.O.fmap(((_, y)) => y);

View File

@ -184,11 +184,9 @@ let toMixed =
~sampleCount=3000, ~sampleCount=3000,
~outputXYPoints=3000, ~outputXYPoints=3000,
~kernelWidth=10, ~kernelWidth=10,
~truncateTo=Some(500),
~cuttoff=0.995, ~cuttoff=0.995,
(), (),
) => { ) => {
// let truncateTo = None;
let start = Js.Date.now(); let start = Js.Date.now();
let timeMessage = message => Js.log2(message, Js.Date.now() -. start); let timeMessage = message => Js.log2(message, Js.Date.now() -. start);
timeMessage("Starting"); timeMessage("Starting");
@ -215,12 +213,5 @@ let toMixed =
let continuous = pdf |> Distributions.Continuous.fromShape; let continuous = pdf |> Distributions.Continuous.fromShape;
let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete); let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete);
timeMessage("Finished shape"); timeMessage("Finished shape");
let shape =
switch (truncateTo, shape) {
| (Some(trunctate), Some(shape)) =>
Some(shape |> Distributions.Shape.T.truncate(trunctate))
| (None, Some(shape)) => Some(shape)
| _ => None
};
shape; shape;
}; };