Actually use cache when needed

This commit is contained in:
Ozzie Gooen 2020-02-25 12:28:26 +00:00
parent 6c536183d4
commit 67ee34d821
7 changed files with 66 additions and 47 deletions

View File

@ -3,7 +3,7 @@
let timeDist =
DistPlusIngredients.make(
~guesstimatorString="mm(floor(10 to 15), 10 to 11, [.9,.1])",
~guesstimatorString="(floor(10 to 15))",
~domain=Complete,
~unit=
DistTypes.TimeDistribution({zero: MomentRe.momentNow(), unit: `years}),
@ -12,7 +12,11 @@ let timeDist =
let setup = dist =>
dist
|> DistPlusIngredients.toDistPlus(~sampleCount=5000, ~outputXYPoints=1000);
|> DistPlusIngredients.toDistPlus(
~sampleCount=10000,
~outputXYPoints=2000,
~truncateTo=Some(1000),
);
let distributions = () =>
<div>
@ -20,7 +24,7 @@ let distributions = () =>
<h2> {"Single-Discrete" |> ReasonReact.string} </h2>
{setup(
DistPlusIngredients.make(
~guesstimatorString="8 to 12, [.5,.5])",
~guesstimatorString="mm(5 to 20, floor(normal(20,2)), [.5, .05])",
~domain=Complete,
(),
),

View File

@ -37,7 +37,6 @@ module IntegralChart = {
let minX = integral |> Distributions.Continuous.T.minX;
let maxX = integral |> Distributions.Continuous.T.maxX;
let timeScale = distPlus.unit |> DistTypes.DistributionUnit.toJson;
Js.log3("HIHI", continuous, distPlus);
<DistributionPlot
minX
maxX

View File

@ -9,13 +9,19 @@ let make =
};
let toDistPlus =
(~sampleCount=1000, ~outputXYPoints=1000, t: distPlusIngredients)
(
~sampleCount=2000,
~outputXYPoints=2000,
~truncateTo=Some(100),
t: distPlusIngredients,
)
: option(distPlus) => {
let shape =
Guesstimator.stringToMixedShape(
~string=t.guesstimatorString,
~sampleCount,
~outputXYPoints,
~truncateTo,
(),
);
let distPlus =

View File

@ -127,14 +127,15 @@ module Continuous = {
// };
let integral = (~cache, t) =>
cache
|> E.O.default(
switch (cache) {
| Some(cache) => cache
| None =>
t
|> xyShape
|> XYShape.Range.integrateWithTriangles
|> E.O.toExt("This should not have happened")
|> fromShape,
);
|> fromShape
};
let integralEndY = (~cache, t) => t |> integral(~cache) |> lastY;
let integralXtoY = (~cache, f, t) =>
t |> integral(~cache) |> shapeFn(CdfLibrary.Distribution.findY(f));
@ -151,8 +152,10 @@ module Discrete = {
type t = DistTypes.discreteShape;
type integral = DistTypes.continuousShape;
let integral = (~cache, t) =>
cache
|> E.O.default(Continuous.make(XYShape.accumulateYs(t), `Stepwise));
switch (cache) {
| Some(c) => c
| None => Continuous.make(XYShape.accumulateYs(t), `Stepwise)
};
let integralEndY = (~cache, t) =>
t |> integral(~cache) |> Continuous.lastY;
let minX = XYShape.minX;
@ -253,9 +256,9 @@ module Mixed = {
~cache,
{continuous, discrete, discreteProbabilityMassFraction} as t: t,
) => {
cache
|> E.O.default(
{
switch (cache) {
| Some(cache) => cache
| None =>
let cont =
continuous
|> Continuous.T.Integral.get(~cache=None)
@ -265,20 +268,15 @@ module Mixed = {
|> Discrete.T.Integral.get(~cache=None)
|> Continuous.toLinear
|> E.O.toExn("")
|> Continuous.T.scaleBy(
~scale=discreteProbabilityMassFraction,
);
|> Continuous.T.scaleBy(~scale=discreteProbabilityMassFraction);
Continuous.make(
XYShape.Combine.combineLinear(
Continuous.getShape(cont),
Continuous.getShape(dist),
(a, b) =>
Continuous.getShape(cont), Continuous.getShape(dist), (a, b) =>
a +. b
),
`Linear,
);
},
);
};
};
let integralEndY = (~cache, t: t) => {

View File

@ -1,4 +1,4 @@
let guesstimatorString = "floor(10 to 20)";
let guesstimatorString = "normal(40,10)";
let makeI = (currentDateTime: MomentRe.Moment.t) => {
DistPlusIngredients.make(

View File

@ -29,9 +29,15 @@ module Internals = {
external toCombinedFormat: (string, int, int) => combined = "run";
// todo: Format to correct mass, also normalize the pdf.
let toMixedShape = (r: combined): option(DistTypes.shape) => {
let toMixedShape =
(~truncateTo=Some(500), r: combined): option(DistTypes.shape) => {
let continuous = toContinous(r);
let continuous =
toContinous(r) |> Distributions.Continuous.convertToNewLength(100);
switch (truncateTo) {
| Some(t) =>
continuous |> Distributions.Continuous.convertToNewLength(t)
| None => continuous
};
let discrete = toDiscrete(r);
// let continuousProb =
// cont |> Distributions.Continuous.T.Integral.sum(~cache=None);
@ -44,6 +50,12 @@ module Internals = {
};
let stringToMixedShape =
(~string, ~sampleCount=1000, ~outputXYPoints=1000, ()) =>
(
~string,
~sampleCount=1000,
~outputXYPoints=1000,
~truncateTo=Some(500),
(),
) =>
Internals.toCombinedFormat(string, sampleCount, outputXYPoints)
|> Internals.toMixedShape;
|> Internals.toMixedShape(~truncateTo);

View File

@ -42,7 +42,7 @@ const toPdf = (values, outputResolutionCount, min, max) => {
let discrete = {xs: frequencies.map(f => f.value), ys: frequencies.map(f => f.percentage)};
let continuous = {ys: [], xs: []};
if (continuousSamples.length > 1){
if (continuousSamples.length > 20){
const samples = new Samples(continuousSamples);
const ratioSize$ = ratioSize(samples);