diff --git a/__tests__/Distributions__Test.re b/__tests__/Distributions__Test.re index 8d7dd906..a6757282 100644 --- a/__tests__/Distributions__Test.re +++ b/__tests__/Distributions__Test.re @@ -15,7 +15,7 @@ let makeTest = (~only=false, str, item1, item2) => describe("Shape", () => { describe("Continuous", () => { open Distributions.Continuous; - let continuous = make(shape, `Linear); + let continuous = make(`Linear, shape); makeTest("minX", T.minX(continuous), 1.0); makeTest("maxX", T.maxX(continuous), 8.0); makeTest( @@ -48,7 +48,7 @@ describe("Shape", () => { ); }); describe("when Stepwise", () => { - let continuous = make(shape, `Stepwise); + let continuous = make(`Stepwise, shape); makeTest( "at 4.0", T.xToY(4., continuous), @@ -80,7 +80,7 @@ describe("Shape", () => { "toLinear", { 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); }, Some({ @@ -91,7 +91,7 @@ describe("Shape", () => { makeTest( "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); }, Some({xs: [|0.0|], ys: [|0.3|]}), @@ -170,14 +170,14 @@ describe("Shape", () => { "integral", T.Integral.get(~cache=None, discrete), Distributions.Continuous.make( - {xs: [|1., 4., 8.|], ys: [|0.3, 0.8, 1.0|]}, `Stepwise, + {xs: [|1., 4., 8.|], ys: [|0.3, 0.8, 1.0|]}, ), ); makeTest( "integral with 1 element", 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( "integralXToY", @@ -195,8 +195,8 @@ describe("Shape", () => { }; let continuous = Distributions.Continuous.make( - {xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]}, `Linear, + {xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]}, ) |> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0); let mixed = @@ -218,6 +218,7 @@ describe("Shape", () => { Distributions.Mixed.make( ~continuous= Distributions.Continuous.make( + `Linear, { xs: [|3., 7., 14.|], ys: [| @@ -226,7 +227,6 @@ describe("Shape", () => { 0.24775224775224775, |], }, - `Linear, ), ~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]}, ~discreteProbabilityMassFraction=0.5, @@ -254,6 +254,7 @@ describe("Shape", () => { Distributions.Mixed.make( ~continuous= Distributions.Continuous.make( + `Linear, { xs: [|3., 7., 14.|], ys: [| @@ -262,7 +263,6 @@ describe("Shape", () => { 0.24775224775224775, |], }, - `Linear, ), ~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]}, ~discreteProbabilityMassFraction=0.5, @@ -272,6 +272,7 @@ describe("Shape", () => { "integral", T.Integral.get(~cache=None, mixed), Distributions.Continuous.make( + `Linear, { xs: [|1.00007, 1.00007, 3., 4., 4.00007, 7., 8., 8.00007, 14.|], ys: [| @@ -286,7 +287,6 @@ describe("Shape", () => { 1.0, |], }, - `Linear, ), ); }); @@ -299,8 +299,8 @@ describe("Shape", () => { }; let continuous = Distributions.Continuous.make( - {xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]}, `Linear, + {xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]}, ) |> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0); let mixed = @@ -343,6 +343,7 @@ describe("Shape", () => { T.Integral.get(~cache=None, distPlus) |> T.toContinuous, Some( Distributions.Continuous.make( + `Linear, { xs: [|1.00007, 1.00007, 3., 4., 4.00007, 7., 8., 8.00007, 14.|], ys: [| @@ -357,7 +358,6 @@ describe("Shape", () => { 1.0, |], }, - `Linear, ), ), ); diff --git a/src/Samples.re b/src/Samples.re index 354e0029..7f7fd61d 100644 --- a/src/Samples.re +++ b/src/Samples.re @@ -105,7 +105,7 @@ module T = { samples |> KDE.normalSampling(_, outputXYPoints, kernelWidth); } : {xs: [||], ys: [||]}; - let continuous = pdf |> Distributions.Continuous.fromShape; + let continuous = pdf |> Distributions.Continuous.make(`Linear); let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete); shape; }; diff --git a/src/components/DistBuilder2.re b/src/components/DistBuilder2.re index 76fe6c0b..01fb4746 100644 --- a/src/components/DistBuilder2.re +++ b/src/components/DistBuilder2.re @@ -43,7 +43,9 @@ module DemoDist = { let distPlus = Distributions.DistPlus.make( ~shape= - Continuous(Distributions.Continuous.fromShape({xs, ys})), + Continuous( + Distributions.Continuous.make(`Linear, {xs, ys}), + ), ~domain=Complete, ~unit=UnspecifiedDistribution, ~guesstimatorString=None, diff --git a/src/components/DistBuilder3.re b/src/components/DistBuilder3.re index ba13ac64..96fba988 100644 --- a/src/components/DistBuilder3.re +++ b/src/components/DistBuilder3.re @@ -52,7 +52,8 @@ module DemoDist = { |> E.O.fmap(shape => { let distPlus = Distributions.DistPlus.make( - ~shape=Continuous(Distributions.Continuous.fromShape(shape)), + ~shape= + Continuous(Distributions.Continuous.make(`Linear, shape)), ~domain=Complete, ~unit=UnspecifiedDistribution, ~guesstimatorString=None, diff --git a/src/distribution/Distributions.re b/src/distribution/Distributions.re index 66cd40b0..15bb47c2 100644 --- a/src/distribution/Distributions.re +++ b/src/distribution/Distributions.re @@ -58,8 +58,7 @@ module Continuous = { type t = DistTypes.continuousShape; let getShape = (t: t) => t.xyShape; let interpolation = (t: t) => t.interpolation; - let make = (xyShape, interpolation): t => {xyShape, interpolation}; - let fromShape = xyShape => make(xyShape, `Linear); + let make = (interpolation, xyShape): t => {xyShape, interpolation}; let shapeMap = (fn, {xyShape, interpolation}: t): t => { xyShape: fn(xyShape), interpolation, @@ -67,14 +66,12 @@ module Continuous = { let lastY = (t: t) => t |> getShape |> XYShape.T.lastY; let oShapeMap = (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) => { switch (t) { | {interpolation: `Stepwise, xyShape} => - xyShape - |> XYShape.Range.stepsToContinuous - |> E.O.fmap(xyShape => make(xyShape, `Linear)) + xyShape |> XYShape.Range.stepsToContinuous |> E.O.fmap(make(`Linear)) | {interpolation: `Linear, _} => Some(t) }; }; @@ -84,24 +81,23 @@ module Continuous = { Dist({ type t = DistTypes.continuousShape; type integral = DistTypes.continuousShape; - let minX = shapeFn(r => r |> XYShape.T.minX); - let maxX = shapeFn(r => r |> XYShape.T.maxX); + let minX = shapeFn(XYShape.T.minX); + let maxX = shapeFn(XYShape.T.maxX); let toDiscreteProbabilityMass = _ => 0.0; - let mapY = (fn, t: t) => - t |> getShape |> XYShape.T.mapY(fn) |> fromShape; + let mapY = fn => shapeMap(XYShape.T.mapY(fn)); let toShape = (t: t): DistTypes.shape => Continuous(t); - let xToY = (f, {interpolation, xyShape}: t) => - switch (interpolation) { - | `Stepwise => - xyShape - |> XYShape.XtoY.stepwiseIncremental(f) - |> E.O.default(0.0) - |> DistTypes.MixedPoint.makeContinuous - | `Linear => - xyShape - |> XYShape.XtoY.linear(f) - |> DistTypes.MixedPoint.makeContinuous - }; + let xToY = (f, {interpolation, xyShape}: t) => { + ( + switch (interpolation) { + | `Stepwise => + xyShape + |> XYShape.XtoY.stepwiseIncremental(f) + |> E.O.default(0.0) + | `Linear => xyShape |> XYShape.XtoY.linear(f) + } + ) + |> DistTypes.MixedPoint.makeContinuous; + }; // let combineWithFn = (t1: t, t2: t, fn: (float, float) => float) => { // switch(t1, t2){ @@ -110,6 +106,7 @@ module Continuous = { // } // }; + // TODO: This should work with stepwise plots. let integral = (~cache, t) => switch (cache) { | Some(cache) => cache @@ -118,13 +115,13 @@ module Continuous = { |> getShape |> XYShape.Range.integrateWithTriangles |> E.O.toExt("This should not have happened") - |> fromShape + |> make(`Linear) }; - let truncate = (~cache=None, i, t) => + let truncate = (~cache=None, length, t) => t |> shapeMap( XYShape.XsConversion.proportionByProbabilityMass( - i, + length, integral(~cache, t).xyShape, ), ); @@ -156,7 +153,7 @@ module Discrete = { let integral = (~cache, t) => switch (cache) { | Some(c) => c - | None => Continuous.make(XYShape.T.accumulateYs((+.), t), `Stepwise) + | None => Continuous.make(`Stepwise, XYShape.T.accumulateYs((+.), t)) }; let integralEndY = (~cache, t) => t |> integral(~cache) |> Continuous.lastY; @@ -325,12 +322,12 @@ module Mixed = { let result = Continuous.make( + `Linear, XYShape.Combine.combineLinear( ~fn=(a, b) => a +. b, Continuous.getShape(cont), Continuous.getShape(dist), ), - `Linear, ); result; };