From 37c1814a6061ca7771f613dc7dcff47d37a93f8e Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 18 Mar 2020 21:46:43 +0000 Subject: [PATCH] Very minor notes & cleanup --- src/distributions/DistPlusIngredients.re | 3 +++ src/distributions/Distributions.re | 15 +++++------ src/distributions/XYShape.re | 9 ++++--- src/utility/Guesstimator.re | 32 ------------------------ 4 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/distributions/DistPlusIngredients.re b/src/distributions/DistPlusIngredients.re index 86fd10f3..1d58b298 100644 --- a/src/distributions/DistPlusIngredients.re +++ b/src/distributions/DistPlusIngredients.re @@ -25,6 +25,9 @@ let toDistPlus = ~kernelWidth, (), ); + // TODO: + // Truncate before resizing. + // Faster sort let distPlus = shape |> E.O.fmap( diff --git a/src/distributions/Distributions.re b/src/distributions/Distributions.re index be95b200..f78df201 100644 --- a/src/distributions/Distributions.re +++ b/src/distributions/Distributions.re @@ -54,6 +54,8 @@ module Dist = (T: dist) => { let toDiscrete = T.toDiscrete; let toScaledContinuous = T.toScaledContinuous; let toScaledDiscrete = T.toScaledDiscrete; + + // TODO: Move this to each class, have use integral to produce integral in DistPlus class. let scaleBy = (~scale=1.0, t: t) => t |> pointwiseFmap((r: float) => r *. scale); @@ -66,8 +68,9 @@ module Dist = (T: dist) => { }; // This is suboptimal because it could get the cache but doesn't here. - let scaleToIntegralSum = (~intendedSum=1.0, t: t) => { - let scale = intendedSum /. Integral.sum(~cache=None, t); + let scaleToIntegralSum = + (~cache: option(integral)=None, ~intendedSum=1.0, t: t) => { + let scale = intendedSum /. Integral.sum(~cache, t); scaleBy(~scale, t); }; }; @@ -100,9 +103,6 @@ module Continuous = { }; let shapeFn = (fn, t: t) => t |> xyShape |> fn; - let convertToNewLength = i => - shapeMap(CdfLibrary.Distribution.convertToNewLength(i)); - module T = Dist({ type t = DistTypes.continuousShape; @@ -207,7 +207,6 @@ module Discrete = { |> DistTypes.MixedPoint.makeDiscrete; }; - // todo: This should use cache and/or same code as above. FindingY is more complex, should use interpolationType. let integralXtoY = (~cache, f, t) => t |> integral(~cache) |> Continuous.getShape |> XYShape.T.findY(f); @@ -231,10 +230,12 @@ module Mixed = { ({discreteProbabilityMassFraction}: DistTypes.mixedShape, f) => f *. discreteProbabilityMassFraction; + //TODO: Warning: This currently computes the integral, which is expensive. let scaleContinuousFn = ({discreteProbabilityMassFraction}: DistTypes.mixedShape, f) => f *. (1.0 -. discreteProbabilityMassFraction); + //TODO: Warning: This currently computes the integral, which is expensive. let scaleContinuous = ({discreteProbabilityMassFraction}: t, continuous) => continuous |> Continuous.T.scaleToIntegralSum( @@ -607,7 +608,7 @@ module DistPlus = { let toDiscreteProbabilityMass = shapeFn(Shape.T.toDiscreteProbabilityMass); - // This bit is kind of akward, could probably use rethinking. + // This bit is kind of awkward, could probably use rethinking. let integral = (~cache, t: t) => updateShape(Continuous(t.integralCache), t); diff --git a/src/distributions/XYShape.re b/src/distributions/XYShape.re index 7cc80525..16074d87 100644 --- a/src/distributions/XYShape.re +++ b/src/distributions/XYShape.re @@ -146,10 +146,11 @@ module T = { t |> E.A.stableSortBy(_, ((x1, _), (x2, _)) => x1 > x2 ? 1 : 0); }; + // TODO: Use faster sort at least, geese. module Combine = { let combineLinear = (t1: t, t2: t, fn: (float, float) => float) => { let allXs = Belt.Array.concat(xs(t1), xs(t2)); - allXs |> Array.sort(compare); + allXs |> Array.fast_sort(compare); let allYs = allXs |> E.A.fmap(x => { @@ -163,7 +164,7 @@ module T = { let combineStepwise = (t1: t, t2: t, fn: (option(float), option(float)) => float) => { let allXs = Belt.Array.concat(xs(t1), xs(t2)); - allXs |> Array.sort(compare); + allXs |> Array.fast_sort(compare); let allYs = allXs |> E.A.fmap(x => { @@ -177,7 +178,7 @@ module T = { let combineIfAtX = (t1: t, t2: t, fn: (option(float), option(float)) => float) => { let allXs = Belt.Array.concat(xs(t1), xs(t2)); - allXs |> Array.sort(compare); + allXs |> Array.fast_sort(compare); let allYs = allXs |> E.A.fmap(x => { @@ -202,7 +203,7 @@ module T = { // todo: This is broken :( let combine = (t1: t, t2: t) => { let array = Belt.Array.concat(zip(t1), zip(t2)); - Array.sort(comparePoints, array); + Array.fast_sort(comparePoints, array); array |> Belt.Array.unzip |> fromArray; }; diff --git a/src/utility/Guesstimator.re b/src/utility/Guesstimator.re index 168810eb..4a7ba2bd 100644 --- a/src/utility/Guesstimator.re +++ b/src/utility/Guesstimator.re @@ -35,40 +35,8 @@ module Internals = { external samplesToContinuousPdf: (array(float), int, int) => CdfLibrary.JS.distJs = "samplesToContinuousPdf"; - - // todo: Format to correct mass, also normalize the pdf. - let toMixedShape = - (~truncateTo=Some(500), r: combined): option(DistTypes.shape) => { - let continuous = toContinous(r); - let continuous = - 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); - // let discreteProb = - // d |> Distributions.Discrete.T.Integral.sum(~cache=None); - - let shape = MixedShapeBuilder.buildSimple(~continuous, ~discrete); - shape; - }; }; -let stringToMixedShape = - ( - ~string, - ~sampleCount=3000, - ~outputXYPoints=3000, - ~width=10, - ~truncateTo=Some(500), - (), - ) => - Internals.toCombinedFormat(string, sampleCount, outputXYPoints, width) - |> Internals.toMixedShape(~truncateTo); - module KDE = { let normalSampling = (samples, outputXYPoints, kernelWidth) => { samples