Added cache where appropriate

This commit is contained in:
Ozzie Gooen 2020-02-22 10:10:10 +00:00
parent 745cf01317
commit 8421892d1d
2 changed files with 69 additions and 37 deletions

View File

@ -22,6 +22,13 @@ type yPoint =
| Continuous(float) | Continuous(float)
| Discrete(float); | Discrete(float);
let yPointCont = (y: yPoint) =>
switch (y) {
| Continuous(f) => f
| Mixed({continuous}) => continuous
| _ => 0.0
};
module type dist = { module type dist = {
type t; type t;
type integral; type integral;
@ -29,10 +36,10 @@ module type dist = {
let maxX: t => option(float); let maxX: t => option(float);
let pointwiseFmap: (float => float, t) => t; let pointwiseFmap: (float => float, t) => t;
let xToY: (float, t) => yPoint; let xToY: (float, t) => yPoint;
let xToIntegralY: (float, t) => float; let xToIntegralY: (~cache: option(integral), float, t) => float;
let shape: t => DistributionTypes.shape; let shape: t => DistributionTypes.shape;
let integral: t => integral; let integral: (~cache: option(integral), t) => integral;
let integralSum: t => float; let integralSum: (~cache: option(integral), t) => float;
}; };
module Dist = (T: dist) => { module Dist = (T: dist) => {
@ -52,32 +59,34 @@ module Continuous =
Dist({ Dist({
type t = DistributionTypes.continuousShape; type t = DistributionTypes.continuousShape;
type integral = DistributionTypes.continuousShape; type integral = DistributionTypes.continuousShape;
let integral = t => let integral = (~cache, t) =>
t |> Shape.XYShape.Range.integrateWithTriangles |> E.O.toExt(""); t |> Shape.XYShape.Range.integrateWithTriangles |> E.O.toExt("");
let integralSum = t => t |> integral |> Shape.XYShape.ySum; // This seems wrong, we really want the ending bit, I'd assume
let integralSum = (~cache, t) =>
t |> integral(~cache) |> Shape.XYShape.ySum;
let minX = Shape.XYShape.minX; let minX = Shape.XYShape.minX;
let maxX = Shape.XYShape.maxX; let maxX = Shape.XYShape.maxX;
let pointwiseFmap = Shape.XYShape.pointwiseMap; let pointwiseFmap = Shape.XYShape.pointwiseMap;
let shape = (t: t): DistributionTypes.shape => Continuous(t); let shape = (t: t): DistributionTypes.shape => Continuous(t);
let xToY = (f, t) => let xToY = (f, t) =>
CdfLibrary.Distribution.findY(f, t) |> (e => Continuous(e)); CdfLibrary.Distribution.findY(f, t) |> (e => Continuous(e));
let xToIntegralY = (f, t) => let xToIntegralY = (~cache, f, t) =>
t |> integral |> CdfLibrary.Distribution.findY(f); t |> integral(~cache) |> CdfLibrary.Distribution.findY(f);
}); });
module Discrete = module Discrete =
Dist({ Dist({
type t = DistributionTypes.discreteShape; type t = DistributionTypes.discreteShape;
type integral = DistributionTypes.continuousShape; type integral = DistributionTypes.continuousShape;
let integral = t => t |> Shape.Discrete.integrate; let integral = (~cache, t) => t |> Shape.Discrete.integrate;
let integralSum = t => t |> Shape.XYShape.ySum; let integralSum = (~cache, t) => t |> Shape.XYShape.ySum;
let minX = Shape.XYShape.minX; let minX = Shape.XYShape.minX;
let maxX = Shape.XYShape.maxX; let maxX = Shape.XYShape.maxX;
let pointwiseFmap = Shape.XYShape.pointwiseMap; let pointwiseFmap = Shape.XYShape.pointwiseMap;
let shape = (t: t): DistributionTypes.shape => Discrete(t); let shape = (t: t): DistributionTypes.shape => Discrete(t);
let xToY = (f, t) => let xToY = (f, t) =>
CdfLibrary.Distribution.findY(f, t) |> (e => Discrete(e)); CdfLibrary.Distribution.findY(f, t) |> (e => Discrete(e));
let xToIntegralY = (f, t) => let xToIntegralY = (~cache, f, t) =>
t |> Shape.XYShape.accumulateYs |> CdfLibrary.Distribution.findY(f); t |> Shape.XYShape.accumulateYs |> CdfLibrary.Distribution.findY(f);
}); });
@ -119,24 +128,35 @@ module Mixed =
); );
// TODO: Add these two directly, once interpolation is added. // TODO: Add these two directly, once interpolation is added.
let integral = t => { let integral = (~cache, t) => {
// let cont = scaledContinuousComponent(t); // let cont = scaledContinuousComponent(t);
// let discrete = scaledDiscreteComponent(t); // let discrete = scaledDiscreteComponent(t);
scaledContinuousComponent(t) |> E.O.toExt(""); switch (cache) {
| Some(cache) => cache
| None => scaledContinuousComponent(t) |> E.O.toExt("")
};
}; };
let integralSum = let integralSum =
({discrete, continuous, discreteProbabilityMassFraction}: t) => { (~cache, {discrete, continuous, discreteProbabilityMassFraction}: t) => {
Discrete.integralSum(discrete) switch (cache) {
| Some(cache) => 3.0
| None =>
Discrete.integralSum(~cache=None, discrete)
*. discreteProbabilityMassFraction *. discreteProbabilityMassFraction
+. Continuous.integralSum(continuous) +. Continuous.integralSum(~cache=None, continuous)
*. (1.0 -. discreteProbabilityMassFraction); *. (1.0 -. discreteProbabilityMassFraction)
};
}; };
let xToIntegralY = let xToIntegralY =
(f, {discrete, continuous, discreteProbabilityMassFraction}: t) => { (
let cont = Continuous.xToIntegralY(f, continuous); ~cache,
let discrete = Discrete.xToIntegralY(f, discrete); f,
{discrete, continuous, discreteProbabilityMassFraction}: t,
) => {
let cont = Continuous.xToIntegralY(~cache, f, continuous);
let discrete = Discrete.xToIntegralY(~cache, f, discrete);
discrete discrete
*. discreteProbabilityMassFraction *. discreteProbabilityMassFraction
+. cont +. cont
@ -165,23 +185,31 @@ module Shape =
let shape = (t: t) => t; let shape = (t: t) => t;
let minX = (t: t) => let minX = (t: t) =>
Shape.T.mapToAll(t, (Mixed.minX, Discrete.minX, Continuous.minX)); Shape.T.mapToAll(t, (Mixed.minX, Discrete.minX, Continuous.minX));
let integral = (t: t) => let integral = (~cache, t: t) =>
Shape.T.mapToAll(
t,
(Mixed.integral, Discrete.integral, Continuous.integral),
);
let integralSum = (t: t) =>
Shape.T.mapToAll(
t,
(Mixed.integralSum, Discrete.integralSum, Continuous.integralSum),
);
let xToIntegralY = (f, t) => {
Shape.T.mapToAll( Shape.T.mapToAll(
t, t,
( (
Mixed.xToIntegralY(f), Mixed.integral(~cache),
Discrete.xToIntegralY(f), Discrete.integral(~cache),
Continuous.xToIntegralY(f), Continuous.integral(~cache),
),
);
let integralSum = (~cache, t: t) =>
Shape.T.mapToAll(
t,
(
Mixed.integralSum(~cache),
Discrete.integralSum(~cache),
Continuous.integralSum(~cache),
),
);
let xToIntegralY = (~cache, f, t) => {
Shape.T.mapToAll(
t,
(
Mixed.xToIntegralY(~cache, f),
Discrete.xToIntegralY(~cache, f),
Continuous.xToIntegralY(~cache, f),
), ),
); );
}; };
@ -210,9 +238,11 @@ module WithMetadata =
let pointwiseFmap = (fn, {shape, _} as t: t): t => let pointwiseFmap = (fn, {shape, _} as t: t): t =>
fromShape(Shape.pointwiseFmap(fn, shape), t); fromShape(Shape.pointwiseFmap(fn, shape), t);
let integral = (t: t) => fromShape(Continuous(t.integralCache), t); let integral = (~cache, t: t) =>
let integralSum = (t: t) => t |> shape |> Shape.integralSum; fromShape(Continuous(t.integralCache), t);
let xToIntegralY = (f, t) => { let integralSum = (~cache, t: t) =>
t |> shape |> Shape.integralSum(~cache=Some(t.integralCache));
let xToIntegralY = (~cache, f, t) => {
3.0; 3.0;
}; };
}); });

View File

@ -14,6 +14,8 @@ type xyShape = {
ys: array(float), ys: array(float),
}; };
type contintinuousInterpolationStrategy = [ | `Stepwise | `Interpolated];
type continuousShape = xyShape; type continuousShape = xyShape;
type discreteShape = xyShape; type discreteShape = xyShape;