Very minor notes & cleanup

This commit is contained in:
Ozzie Gooen 2020-03-18 21:46:43 +00:00
parent 49b78ae814
commit 37c1814a60
4 changed files with 16 additions and 43 deletions

View File

@ -25,6 +25,9 @@ let toDistPlus =
~kernelWidth, ~kernelWidth,
(), (),
); );
// TODO:
// Truncate before resizing.
// Faster sort
let distPlus = let distPlus =
shape shape
|> E.O.fmap( |> E.O.fmap(

View File

@ -54,6 +54,8 @@ module Dist = (T: dist) => {
let toDiscrete = T.toDiscrete; let toDiscrete = T.toDiscrete;
let toScaledContinuous = T.toScaledContinuous; let toScaledContinuous = T.toScaledContinuous;
let toScaledDiscrete = T.toScaledDiscrete; 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) => let scaleBy = (~scale=1.0, t: t) =>
t |> pointwiseFmap((r: float) => r *. scale); 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. // This is suboptimal because it could get the cache but doesn't here.
let scaleToIntegralSum = (~intendedSum=1.0, t: t) => { let scaleToIntegralSum =
let scale = intendedSum /. Integral.sum(~cache=None, t); (~cache: option(integral)=None, ~intendedSum=1.0, t: t) => {
let scale = intendedSum /. Integral.sum(~cache, t);
scaleBy(~scale, t); scaleBy(~scale, t);
}; };
}; };
@ -100,9 +103,6 @@ module Continuous = {
}; };
let shapeFn = (fn, t: t) => t |> xyShape |> fn; let shapeFn = (fn, t: t) => t |> xyShape |> fn;
let convertToNewLength = i =>
shapeMap(CdfLibrary.Distribution.convertToNewLength(i));
module T = module T =
Dist({ Dist({
type t = DistTypes.continuousShape; type t = DistTypes.continuousShape;
@ -207,7 +207,6 @@ module Discrete = {
|> DistTypes.MixedPoint.makeDiscrete; |> 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) => let integralXtoY = (~cache, f, t) =>
t |> integral(~cache) |> Continuous.getShape |> XYShape.T.findY(f); t |> integral(~cache) |> Continuous.getShape |> XYShape.T.findY(f);
@ -231,10 +230,12 @@ module Mixed = {
({discreteProbabilityMassFraction}: DistTypes.mixedShape, f) => ({discreteProbabilityMassFraction}: DistTypes.mixedShape, f) =>
f *. discreteProbabilityMassFraction; f *. discreteProbabilityMassFraction;
//TODO: Warning: This currently computes the integral, which is expensive.
let scaleContinuousFn = let scaleContinuousFn =
({discreteProbabilityMassFraction}: DistTypes.mixedShape, f) => ({discreteProbabilityMassFraction}: DistTypes.mixedShape, f) =>
f *. (1.0 -. discreteProbabilityMassFraction); f *. (1.0 -. discreteProbabilityMassFraction);
//TODO: Warning: This currently computes the integral, which is expensive.
let scaleContinuous = ({discreteProbabilityMassFraction}: t, continuous) => let scaleContinuous = ({discreteProbabilityMassFraction}: t, continuous) =>
continuous continuous
|> Continuous.T.scaleToIntegralSum( |> Continuous.T.scaleToIntegralSum(
@ -607,7 +608,7 @@ module DistPlus = {
let toDiscreteProbabilityMass = let toDiscreteProbabilityMass =
shapeFn(Shape.T.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) => let integral = (~cache, t: t) =>
updateShape(Continuous(t.integralCache), t); updateShape(Continuous(t.integralCache), t);

View File

@ -146,10 +146,11 @@ module T = {
t |> E.A.stableSortBy(_, ((x1, _), (x2, _)) => x1 > x2 ? 1 : 0); t |> E.A.stableSortBy(_, ((x1, _), (x2, _)) => x1 > x2 ? 1 : 0);
}; };
// TODO: Use faster sort at least, geese.
module Combine = { module Combine = {
let combineLinear = (t1: t, t2: t, fn: (float, float) => float) => { let combineLinear = (t1: t, t2: t, fn: (float, float) => float) => {
let allXs = Belt.Array.concat(xs(t1), xs(t2)); let allXs = Belt.Array.concat(xs(t1), xs(t2));
allXs |> Array.sort(compare); allXs |> Array.fast_sort(compare);
let allYs = let allYs =
allXs allXs
|> E.A.fmap(x => { |> E.A.fmap(x => {
@ -163,7 +164,7 @@ module T = {
let combineStepwise = let combineStepwise =
(t1: t, t2: t, fn: (option(float), option(float)) => float) => { (t1: t, t2: t, fn: (option(float), option(float)) => float) => {
let allXs = Belt.Array.concat(xs(t1), xs(t2)); let allXs = Belt.Array.concat(xs(t1), xs(t2));
allXs |> Array.sort(compare); allXs |> Array.fast_sort(compare);
let allYs = let allYs =
allXs allXs
|> E.A.fmap(x => { |> E.A.fmap(x => {
@ -177,7 +178,7 @@ module T = {
let combineIfAtX = let combineIfAtX =
(t1: t, t2: t, fn: (option(float), option(float)) => float) => { (t1: t, t2: t, fn: (option(float), option(float)) => float) => {
let allXs = Belt.Array.concat(xs(t1), xs(t2)); let allXs = Belt.Array.concat(xs(t1), xs(t2));
allXs |> Array.sort(compare); allXs |> Array.fast_sort(compare);
let allYs = let allYs =
allXs allXs
|> E.A.fmap(x => { |> E.A.fmap(x => {
@ -202,7 +203,7 @@ module T = {
// todo: This is broken :( // todo: This is broken :(
let combine = (t1: t, t2: t) => { let combine = (t1: t, t2: t) => {
let array = Belt.Array.concat(zip(t1), zip(t2)); let array = Belt.Array.concat(zip(t1), zip(t2));
Array.sort(comparePoints, array); Array.fast_sort(comparePoints, array);
array |> Belt.Array.unzip |> fromArray; array |> Belt.Array.unzip |> fromArray;
}; };

View File

@ -35,40 +35,8 @@ module Internals = {
external samplesToContinuousPdf: external samplesToContinuousPdf:
(array(float), int, int) => CdfLibrary.JS.distJs = (array(float), int, int) => CdfLibrary.JS.distJs =
"samplesToContinuousPdf"; "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 = { module KDE = {
let normalSampling = (samples, outputXYPoints, kernelWidth) => { let normalSampling = (samples, outputXYPoints, kernelWidth) => {
samples samples