Minor touch-ups

This commit is contained in:
Ozzie Gooen 2020-03-28 22:51:53 +00:00
parent 54eab1f204
commit 53675232e7

View File

@ -139,13 +139,9 @@ module Continuous = {
module Discrete = { module Discrete = {
let sortedByY = (t: DistTypes.discreteShape) => let sortedByY = (t: DistTypes.discreteShape) =>
t t |> XYShape.T.zip |> XYShape.Zipped.sortByY;
|> XYShape.T.zip
|> E.A.stableSortBy(_, ((_, y1), (_, y2)) => y1 > y2 ? 1 : 0);
let sortedByX = (t: DistTypes.discreteShape) => let sortedByX = (t: DistTypes.discreteShape) =>
t t |> XYShape.T.zip |> XYShape.Zipped.sortByX;
|> XYShape.T.zip
|> E.A.stableSortBy(_, ((x1, _), (x2, _)) => x1 > x2 ? 1 : 0);
module T = module T =
Dist({ Dist({
type t = DistTypes.discreteShape; type t = DistTypes.discreteShape;
@ -159,7 +155,7 @@ module Discrete = {
t |> integral(~cache) |> Continuous.lastY; t |> integral(~cache) |> Continuous.lastY;
let minX = XYShape.T.minX; let minX = XYShape.T.minX;
let maxX = XYShape.T.maxX; let maxX = XYShape.T.maxX;
let toDiscreteProbabilityMass = t => 1.0; let toDiscreteProbabilityMass = _ => 1.0;
let mapY = XYShape.T.mapY; let mapY = XYShape.T.mapY;
let toShape = (t: t): DistTypes.shape => Discrete(t); let toShape = (t: t): DistTypes.shape => Discrete(t);
let toContinuous = _ => None; let toContinuous = _ => None;
@ -195,6 +191,7 @@ module Discrete = {
}); });
}; };
// TODO: I think this shouldn't assume continuous/discrete are normalized to 1.0, and thus should not need the discreteProbabilityMassFraction being separate.
module Mixed = { module Mixed = {
type t = DistTypes.mixedShape; type t = DistTypes.mixedShape;
let make = let make =
@ -253,11 +250,12 @@ module Mixed = {
DistTypes.MixedPoint.add(c, d); DistTypes.MixedPoint.add(c, d);
}; };
// Warning: It's not clear how to update the discreteProbabilityMassFraction, so this may create small errors.
let truncate = let truncate =
( (
~cache=None, ~cache=None,
count, count,
{discrete, continuous, discreteProbabilityMassFraction} as t: t, {discrete, continuous, discreteProbabilityMassFraction}: t,
) )
: t => { : t => {
{ {
@ -324,7 +322,7 @@ module Mixed = {
Continuous.make( Continuous.make(
`Linear, `Linear,
XYShape.Combine.combineLinear( XYShape.Combine.combineLinear(
~fn=(a, b) => a +. b, ~fn=(+.),
Continuous.getShape(cont), Continuous.getShape(cont),
Continuous.getShape(dist), Continuous.getShape(dist),
), ),
@ -351,7 +349,7 @@ module Mixed = {
|> XYShape.YtoX.linear(f); |> XYShape.YtoX.linear(f);
}; };
// TODO: This functionality is kinda weird, because it seems to assume the cdf adds to 1.0 elsewhere, which wouldn't happen here. // TODO: This part really needs to be rethought, I'm quite sure this is just broken. Mapping Ys would change the desired discreteProbabilityMassFraction.
let mapY = let mapY =
(fn, {discrete, continuous, discreteProbabilityMassFraction}: t): t => { (fn, {discrete, continuous, discreteProbabilityMassFraction}: t): t => {
{ {
@ -369,133 +367,101 @@ module Shape = {
type t = DistTypes.shape; type t = DistTypes.shape;
type integral = DistTypes.continuousShape; type integral = DistTypes.continuousShape;
// todo: change order of arguments so t goes last. let mapToAll = ((fn1, fn2, fn3), t: t) =>
// todo: Think of other name here?
let mapToAll = (t: t, (fn1, fn2, fn3)) =>
switch (t) { switch (t) {
| Mixed(m) => fn1(m) | Mixed(m) => fn1(m)
| Discrete(m) => fn2(m) | Discrete(m) => fn2(m)
| Continuous(m) => fn3(m) | Continuous(m) => fn3(m)
}; };
let fmap = (t: t, (fn1, fn2, fn3)): t => let fmap = ((fn1, fn2, fn3), t: t): t =>
switch (t) { switch (t) {
| Mixed(m) => Mixed(fn1(m)) | Mixed(m) => Mixed(fn1(m))
| Discrete(m) => Discrete(fn2(m)) | Discrete(m) => Discrete(fn2(m))
| Continuous(m) => Continuous(fn3(m)) | Continuous(m) => Continuous(fn3(m))
}; };
let xToY = (f, t) => let xToY = f =>
mapToAll( mapToAll((
t, Mixed.T.xToY(f),
(Mixed.T.xToY(f), Discrete.T.xToY(f), Continuous.T.xToY(f)), Discrete.T.xToY(f),
); Continuous.T.xToY(f),
));
let toShape = (t: t) => t; let toShape = (t: t) => t;
let toContinuous = (t: t) => let toContinuous =
mapToAll( mapToAll((
t,
(
Mixed.T.toContinuous, Mixed.T.toContinuous,
Discrete.T.toContinuous, Discrete.T.toContinuous,
Continuous.T.toContinuous, Continuous.T.toContinuous,
), ));
); let toDiscrete =
let toDiscrete = (t: t) => mapToAll((
mapToAll(
t,
(
Mixed.T.toDiscrete, Mixed.T.toDiscrete,
Discrete.T.toDiscrete, Discrete.T.toDiscrete,
Continuous.T.toDiscrete, Continuous.T.toDiscrete,
), ));
);
let truncate = (~cache=None, i, t: t) => let truncate = (~cache=None, i) =>
fmap( fmap((
t,
(
Mixed.T.truncate(i), Mixed.T.truncate(i),
Discrete.T.truncate(i), Discrete.T.truncate(i),
Continuous.T.truncate(i), Continuous.T.truncate(i),
), ));
);
let toDiscreteProbabilityMass = (t: t) => let toDiscreteProbabilityMass =
mapToAll( mapToAll((
t,
(
Mixed.T.toDiscreteProbabilityMass, Mixed.T.toDiscreteProbabilityMass,
Discrete.T.toDiscreteProbabilityMass, Discrete.T.toDiscreteProbabilityMass,
Continuous.T.toDiscreteProbabilityMass, Continuous.T.toDiscreteProbabilityMass,
), ));
);
let toScaledDiscrete = (t: t) => let toScaledDiscrete =
mapToAll( mapToAll((
t,
(
Mixed.T.toScaledDiscrete, Mixed.T.toScaledDiscrete,
Discrete.T.toScaledDiscrete, Discrete.T.toScaledDiscrete,
Continuous.T.toScaledDiscrete, Continuous.T.toScaledDiscrete,
), ));
); let toScaledContinuous =
let toScaledContinuous = (t: t) => mapToAll((
mapToAll(
t,
(
Mixed.T.toScaledContinuous, Mixed.T.toScaledContinuous,
Discrete.T.toScaledContinuous, Discrete.T.toScaledContinuous,
Continuous.T.toScaledContinuous, Continuous.T.toScaledContinuous,
), ));
); let minX = mapToAll((Mixed.T.minX, Discrete.T.minX, Continuous.T.minX));
let minX = (t: t) => let integral = (~cache) => {
mapToAll(t, (Mixed.T.minX, Discrete.T.minX, Continuous.T.minX)); mapToAll((
let integral = (~cache, t: t) => {
mapToAll(
t,
(
Mixed.T.Integral.get(~cache), Mixed.T.Integral.get(~cache),
Discrete.T.Integral.get(~cache), Discrete.T.Integral.get(~cache),
Continuous.T.Integral.get(~cache), Continuous.T.Integral.get(~cache),
), ));
);
}; };
let integralEndY = (~cache, t: t) => let integralEndY = (~cache) =>
mapToAll( mapToAll((
t,
(
Mixed.T.Integral.sum(~cache), Mixed.T.Integral.sum(~cache),
Discrete.T.Integral.sum(~cache), Discrete.T.Integral.sum(~cache),
Continuous.T.Integral.sum(~cache), Continuous.T.Integral.sum(~cache),
), ));
); let integralXtoY = (~cache, f) => {
let integralXtoY = (~cache, f, t) => { mapToAll((
mapToAll(
t,
(
Mixed.T.Integral.xToY(~cache, f), Mixed.T.Integral.xToY(~cache, f),
Discrete.T.Integral.xToY(~cache, f), Discrete.T.Integral.xToY(~cache, f),
Continuous.T.Integral.xToY(~cache, f), Continuous.T.Integral.xToY(~cache, f),
), ));
);
}; };
let integralYtoX = (~cache, f, t) => { let integralYtoX = (~cache, f) => {
mapToAll( mapToAll((
t,
(
Mixed.T.Integral.yToX(~cache, f), Mixed.T.Integral.yToX(~cache, f),
Discrete.T.Integral.yToX(~cache, f), Discrete.T.Integral.yToX(~cache, f),
Continuous.T.Integral.yToX(~cache, f), Continuous.T.Integral.yToX(~cache, f),
), ));
);
}; };
let maxX = (t: t) => let maxX = mapToAll((Mixed.T.maxX, Discrete.T.maxX, Continuous.T.maxX));
mapToAll(t, (Mixed.T.maxX, Discrete.T.maxX, Continuous.T.maxX)); let mapY = fn =>
let mapY = (fn, t: t) => fmap((
fmap( Mixed.T.mapY(fn),
t, Discrete.T.mapY(fn),
(Mixed.T.mapY(fn), Discrete.T.mapY(fn), Continuous.T.mapY(fn)), Continuous.T.mapY(fn),
); ));
}); });
}; };