Change order of continuous distribution make command

This commit is contained in:
Ozzie Gooen 2020-03-28 21:46:17 +00:00
parent 786ee1fded
commit 54eab1f204
5 changed files with 42 additions and 42 deletions

View File

@ -15,7 +15,7 @@ let makeTest = (~only=false, str, item1, item2) =>
describe("Shape", () => { describe("Shape", () => {
describe("Continuous", () => { describe("Continuous", () => {
open Distributions.Continuous; open Distributions.Continuous;
let continuous = make(shape, `Linear); let continuous = make(`Linear, shape);
makeTest("minX", T.minX(continuous), 1.0); makeTest("minX", T.minX(continuous), 1.0);
makeTest("maxX", T.maxX(continuous), 8.0); makeTest("maxX", T.maxX(continuous), 8.0);
makeTest( makeTest(
@ -48,7 +48,7 @@ describe("Shape", () => {
); );
}); });
describe("when Stepwise", () => { describe("when Stepwise", () => {
let continuous = make(shape, `Stepwise); let continuous = make(`Stepwise, shape);
makeTest( makeTest(
"at 4.0", "at 4.0",
T.xToY(4., continuous), T.xToY(4., continuous),
@ -80,7 +80,7 @@ describe("Shape", () => {
"toLinear", "toLinear",
{ {
let continuous = let continuous =
make({xs: [|1., 4., 8.|], ys: [|0.1, 5., 1.0|]}, `Stepwise); make(`Stepwise, {xs: [|1., 4., 8.|], ys: [|0.1, 5., 1.0|]});
continuous |> toLinear |> E.O.fmap(getShape); continuous |> toLinear |> E.O.fmap(getShape);
}, },
Some({ Some({
@ -91,7 +91,7 @@ describe("Shape", () => {
makeTest( makeTest(
"toLinear", "toLinear",
{ {
let continuous = make({xs: [|0.0|], ys: [|0.3|]}, `Stepwise); let continuous = make(`Stepwise, {xs: [|0.0|], ys: [|0.3|]});
continuous |> toLinear |> E.O.fmap(getShape); continuous |> toLinear |> E.O.fmap(getShape);
}, },
Some({xs: [|0.0|], ys: [|0.3|]}), Some({xs: [|0.0|], ys: [|0.3|]}),
@ -170,14 +170,14 @@ describe("Shape", () => {
"integral", "integral",
T.Integral.get(~cache=None, discrete), T.Integral.get(~cache=None, discrete),
Distributions.Continuous.make( Distributions.Continuous.make(
{xs: [|1., 4., 8.|], ys: [|0.3, 0.8, 1.0|]},
`Stepwise, `Stepwise,
{xs: [|1., 4., 8.|], ys: [|0.3, 0.8, 1.0|]},
), ),
); );
makeTest( makeTest(
"integral with 1 element", "integral with 1 element",
T.Integral.get(~cache=None, {xs: [|0.0|], ys: [|1.0|]}), T.Integral.get(~cache=None, {xs: [|0.0|], ys: [|1.0|]}),
Distributions.Continuous.make({xs: [|0.0|], ys: [|1.0|]}, `Stepwise), Distributions.Continuous.make(`Stepwise, {xs: [|0.0|], ys: [|1.0|]}),
); );
makeTest( makeTest(
"integralXToY", "integralXToY",
@ -195,8 +195,8 @@ describe("Shape", () => {
}; };
let continuous = let continuous =
Distributions.Continuous.make( Distributions.Continuous.make(
{xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]},
`Linear, `Linear,
{xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]},
) )
|> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0); |> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0);
let mixed = let mixed =
@ -218,6 +218,7 @@ describe("Shape", () => {
Distributions.Mixed.make( Distributions.Mixed.make(
~continuous= ~continuous=
Distributions.Continuous.make( Distributions.Continuous.make(
`Linear,
{ {
xs: [|3., 7., 14.|], xs: [|3., 7., 14.|],
ys: [| ys: [|
@ -226,7 +227,6 @@ describe("Shape", () => {
0.24775224775224775, 0.24775224775224775,
|], |],
}, },
`Linear,
), ),
~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]}, ~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]},
~discreteProbabilityMassFraction=0.5, ~discreteProbabilityMassFraction=0.5,
@ -254,6 +254,7 @@ describe("Shape", () => {
Distributions.Mixed.make( Distributions.Mixed.make(
~continuous= ~continuous=
Distributions.Continuous.make( Distributions.Continuous.make(
`Linear,
{ {
xs: [|3., 7., 14.|], xs: [|3., 7., 14.|],
ys: [| ys: [|
@ -262,7 +263,6 @@ describe("Shape", () => {
0.24775224775224775, 0.24775224775224775,
|], |],
}, },
`Linear,
), ),
~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]}, ~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]},
~discreteProbabilityMassFraction=0.5, ~discreteProbabilityMassFraction=0.5,
@ -272,6 +272,7 @@ describe("Shape", () => {
"integral", "integral",
T.Integral.get(~cache=None, mixed), T.Integral.get(~cache=None, mixed),
Distributions.Continuous.make( Distributions.Continuous.make(
`Linear,
{ {
xs: [|1.00007, 1.00007, 3., 4., 4.00007, 7., 8., 8.00007, 14.|], xs: [|1.00007, 1.00007, 3., 4., 4.00007, 7., 8., 8.00007, 14.|],
ys: [| ys: [|
@ -286,7 +287,6 @@ describe("Shape", () => {
1.0, 1.0,
|], |],
}, },
`Linear,
), ),
); );
}); });
@ -299,8 +299,8 @@ describe("Shape", () => {
}; };
let continuous = let continuous =
Distributions.Continuous.make( Distributions.Continuous.make(
{xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]},
`Linear, `Linear,
{xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]},
) )
|> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0); |> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0);
let mixed = let mixed =
@ -343,6 +343,7 @@ describe("Shape", () => {
T.Integral.get(~cache=None, distPlus) |> T.toContinuous, T.Integral.get(~cache=None, distPlus) |> T.toContinuous,
Some( Some(
Distributions.Continuous.make( Distributions.Continuous.make(
`Linear,
{ {
xs: [|1.00007, 1.00007, 3., 4., 4.00007, 7., 8., 8.00007, 14.|], xs: [|1.00007, 1.00007, 3., 4., 4.00007, 7., 8., 8.00007, 14.|],
ys: [| ys: [|
@ -357,7 +358,6 @@ describe("Shape", () => {
1.0, 1.0,
|], |],
}, },
`Linear,
), ),
), ),
); );

View File

@ -105,7 +105,7 @@ module T = {
samples |> KDE.normalSampling(_, outputXYPoints, kernelWidth); samples |> KDE.normalSampling(_, outputXYPoints, kernelWidth);
} }
: {xs: [||], ys: [||]}; : {xs: [||], ys: [||]};
let continuous = pdf |> Distributions.Continuous.fromShape; let continuous = pdf |> Distributions.Continuous.make(`Linear);
let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete); let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete);
shape; shape;
}; };

View File

@ -43,7 +43,9 @@ module DemoDist = {
let distPlus = let distPlus =
Distributions.DistPlus.make( Distributions.DistPlus.make(
~shape= ~shape=
Continuous(Distributions.Continuous.fromShape({xs, ys})), Continuous(
Distributions.Continuous.make(`Linear, {xs, ys}),
),
~domain=Complete, ~domain=Complete,
~unit=UnspecifiedDistribution, ~unit=UnspecifiedDistribution,
~guesstimatorString=None, ~guesstimatorString=None,

View File

@ -52,7 +52,8 @@ module DemoDist = {
|> E.O.fmap(shape => { |> E.O.fmap(shape => {
let distPlus = let distPlus =
Distributions.DistPlus.make( Distributions.DistPlus.make(
~shape=Continuous(Distributions.Continuous.fromShape(shape)), ~shape=
Continuous(Distributions.Continuous.make(`Linear, shape)),
~domain=Complete, ~domain=Complete,
~unit=UnspecifiedDistribution, ~unit=UnspecifiedDistribution,
~guesstimatorString=None, ~guesstimatorString=None,

View File

@ -58,8 +58,7 @@ module Continuous = {
type t = DistTypes.continuousShape; type t = DistTypes.continuousShape;
let getShape = (t: t) => t.xyShape; let getShape = (t: t) => t.xyShape;
let interpolation = (t: t) => t.interpolation; let interpolation = (t: t) => t.interpolation;
let make = (xyShape, interpolation): t => {xyShape, interpolation}; let make = (interpolation, xyShape): t => {xyShape, interpolation};
let fromShape = xyShape => make(xyShape, `Linear);
let shapeMap = (fn, {xyShape, interpolation}: t): t => { let shapeMap = (fn, {xyShape, interpolation}: t): t => {
xyShape: fn(xyShape), xyShape: fn(xyShape),
interpolation, interpolation,
@ -67,14 +66,12 @@ module Continuous = {
let lastY = (t: t) => t |> getShape |> XYShape.T.lastY; let lastY = (t: t) => t |> getShape |> XYShape.T.lastY;
let oShapeMap = let oShapeMap =
(fn, {xyShape, interpolation}: t): option(DistTypes.continuousShape) => (fn, {xyShape, interpolation}: t): option(DistTypes.continuousShape) =>
fn(xyShape) |> E.O.fmap(make(_, interpolation)); fn(xyShape) |> E.O.fmap(make(interpolation));
let toLinear = (t: t): option(t) => { let toLinear = (t: t): option(t) => {
switch (t) { switch (t) {
| {interpolation: `Stepwise, xyShape} => | {interpolation: `Stepwise, xyShape} =>
xyShape xyShape |> XYShape.Range.stepsToContinuous |> E.O.fmap(make(`Linear))
|> XYShape.Range.stepsToContinuous
|> E.O.fmap(xyShape => make(xyShape, `Linear))
| {interpolation: `Linear, _} => Some(t) | {interpolation: `Linear, _} => Some(t)
}; };
}; };
@ -84,24 +81,23 @@ module Continuous = {
Dist({ Dist({
type t = DistTypes.continuousShape; type t = DistTypes.continuousShape;
type integral = DistTypes.continuousShape; type integral = DistTypes.continuousShape;
let minX = shapeFn(r => r |> XYShape.T.minX); let minX = shapeFn(XYShape.T.minX);
let maxX = shapeFn(r => r |> XYShape.T.maxX); let maxX = shapeFn(XYShape.T.maxX);
let toDiscreteProbabilityMass = _ => 0.0; let toDiscreteProbabilityMass = _ => 0.0;
let mapY = (fn, t: t) => let mapY = fn => shapeMap(XYShape.T.mapY(fn));
t |> getShape |> XYShape.T.mapY(fn) |> fromShape;
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) { (
| `Stepwise => switch (interpolation) {
xyShape | `Stepwise =>
|> XYShape.XtoY.stepwiseIncremental(f) xyShape
|> E.O.default(0.0) |> XYShape.XtoY.stepwiseIncremental(f)
|> DistTypes.MixedPoint.makeContinuous |> E.O.default(0.0)
| `Linear => | `Linear => xyShape |> XYShape.XtoY.linear(f)
xyShape }
|> XYShape.XtoY.linear(f) )
|> DistTypes.MixedPoint.makeContinuous |> DistTypes.MixedPoint.makeContinuous;
}; };
// let combineWithFn = (t1: t, t2: t, fn: (float, float) => float) => { // let combineWithFn = (t1: t, t2: t, fn: (float, float) => float) => {
// switch(t1, t2){ // switch(t1, t2){
@ -110,6 +106,7 @@ module Continuous = {
// } // }
// }; // };
// TODO: This should work with stepwise plots.
let integral = (~cache, t) => let integral = (~cache, t) =>
switch (cache) { switch (cache) {
| Some(cache) => cache | Some(cache) => cache
@ -118,13 +115,13 @@ module Continuous = {
|> getShape |> getShape
|> XYShape.Range.integrateWithTriangles |> XYShape.Range.integrateWithTriangles
|> E.O.toExt("This should not have happened") |> E.O.toExt("This should not have happened")
|> fromShape |> make(`Linear)
}; };
let truncate = (~cache=None, i, t) => let truncate = (~cache=None, length, t) =>
t t
|> shapeMap( |> shapeMap(
XYShape.XsConversion.proportionByProbabilityMass( XYShape.XsConversion.proportionByProbabilityMass(
i, length,
integral(~cache, t).xyShape, integral(~cache, t).xyShape,
), ),
); );
@ -156,7 +153,7 @@ module Discrete = {
let integral = (~cache, t) => let integral = (~cache, t) =>
switch (cache) { switch (cache) {
| Some(c) => c | Some(c) => c
| None => Continuous.make(XYShape.T.accumulateYs((+.), t), `Stepwise) | None => Continuous.make(`Stepwise, XYShape.T.accumulateYs((+.), t))
}; };
let integralEndY = (~cache, t) => let integralEndY = (~cache, t) =>
t |> integral(~cache) |> Continuous.lastY; t |> integral(~cache) |> Continuous.lastY;
@ -325,12 +322,12 @@ module Mixed = {
let result = let result =
Continuous.make( Continuous.make(
`Linear,
XYShape.Combine.combineLinear( XYShape.Combine.combineLinear(
~fn=(a, b) => a +. b, ~fn=(a, b) => a +. b,
Continuous.getShape(cont), Continuous.getShape(cont),
Continuous.getShape(dist), Continuous.getShape(dist),
), ),
`Linear,
); );
result; result;
}; };