diff --git a/__tests__/CDF__Test.re b/__tests__/CDF__Test.re index 66c35fdf..33ad8700 100644 --- a/__tests__/CDF__Test.re +++ b/__tests__/CDF__Test.re @@ -100,4 +100,47 @@ describe("CDF", () => { expect(Dist.findX(4.)) |> toEqual(1.) }); }); + + describe("convertWithAlternativeXs", () => { + open Functions; + let xs = up(1, 9); + let ys = up(20, 28); + module Dist = + CDF.Make({ + let shape = CDF.order({xs, ys}); + }); + + let xs2 = up(3, 7); + module Dist2 = + CDF.Make({ + let shape = Dist.convertWithAlternativeXs(xs2); + }); + + test("#1", () => { + expect(Dist2.xs) |> toEqual([|3., 4., 5., 6., 7.|]) + }); + test("#2", () => { + expect(Dist2.ys) |> toEqual([|22., 23., 24., 25., 26.|]) + }); + }); + + describe("convertToNewLength", () => { + open Functions; + let xs = up(1, 9); + let ys = up(50, 58); + module Dist = + CDF.Make({ + let shape = CDF.order({xs, ys}); + }); + module Dist2 = + CDF.Make({ + let shape = Dist.convertToNewLength(3); + }); + test("#1", () => { + expect(Dist2.xs) |> toEqual([|1., 5., 9.|]) + }); + test("#2", () => { + expect(Dist2.ys) |> toEqual([|50., 54., 58.|]) + }); + }); }); diff --git a/__tests__/Functions__Test.re b/__tests__/Functions__Test.re index d32f44e8..5dda5e3d 100644 --- a/__tests__/Functions__Test.re +++ b/__tests__/Functions__Test.re @@ -76,15 +76,17 @@ describe("Functions", () => { expect(Functions.random(1, 5)) |> toBeGreaterThanOrEqual(1) }); test("up#1", () => { - expect(Functions.up(1, 5)) |> toEqual([|1, 2, 3, 4, 5|]) + expect(Functions.up(1, 5)) |> toEqual([|1., 2., 3., 4., 5.|]) }); test("up#2", () => { - expect(Functions.up(-1, 5)) |> toEqual([|(-1), 0, 1, 2, 3, 4, 5|]) + expect(Functions.up(-1, 5)) + |> toEqual([|(-1.), 0., 1., 2., 3., 4., 5.|]) }); test("down#1", () => { - expect(Functions.down(5, 1)) |> toEqual([|5, 4, 3, 2, 1|]) + expect(Functions.down(5, 1)) |> toEqual([|5., 4., 3., 2., 1.|]) }); test("down#2", () => { - expect(Functions.down(5, -1)) |> toEqual([|5, 4, 3, 2, 1, 0, (-1)|]) + expect(Functions.down(5, -1)) + |> toEqual([|5., 4., 3., 2., 1., 0., (-1.)|]) }); }); diff --git a/src/utility/lib/CDF.re b/src/utility/lib/CDF.re index 09095ae2..b5f9221e 100644 --- a/src/utility/lib/CDF.re +++ b/src/utility/lib/CDF.re @@ -78,5 +78,17 @@ module Make = (Config: Config) => { }; }; }; + let convertWithAlternativeXs = (newXs: array(float)): DistTypes.xyShape => { + let newYs = Belt.Array.map(newXs, findY); + {xs: newXs, ys: newYs}; + }; + let convertToNewLength = (newLength: int): DistTypes.xyShape => { + Functions.( + range(min(xs), max(xs), newLength) |> convertWithAlternativeXs + ); + }; + let sampleSingle = (): float => Js.Math.random() |> findY; + let sample = (size: int): array(float) => + Belt.Array.makeBy(size, i => sampleSingle()); 1; }; diff --git a/src/utility/lib/Functions.re b/src/utility/lib/Functions.re index 26a8bf20..998084cc 100644 --- a/src/utility/lib/Functions.re +++ b/src/utility/lib/Functions.re @@ -13,9 +13,14 @@ let sum = Belt.Array.reduce(_, 0., (i, j) => i +. j); let mean = a => sum(a) /. (Array.length(a) |> float_of_int); let min = a => Belt.Array.reduce(a, a[0], (i, j) => i < j ? i : j); let max = a => Belt.Array.reduce(a, a[0], (i, j) => i > j ? i : j); -let up = (a, b) => Array.make(b - a + 1, a) |> Array.mapi((i, c) => c + i); +let up = (a, b) => + Array.make(b - a + 1, a) + |> Array.mapi((i, c) => c + i) + |> Belt.Array.map(_, float_of_int); let down = (a, b) => - Array.make(a - b + 1, a) |> Array.mapi((i, c) => c - i); + Array.make(a - b + 1, a) + |> Array.mapi((i, c) => c - i) + |> Belt.Array.map(_, float_of_int); let range = (min: float, max: float, n: int): array(float) => { switch (n) { | 0 => [||] diff --git a/src/utility/lib/continuousDistribution.js b/src/utility/lib/continuousDistribution.js index 1e639398..bd777908 100644 --- a/src/utility/lib/continuousDistribution.js +++ b/src/utility/lib/continuousDistribution.js @@ -136,6 +136,7 @@ class ContinuousDistribution { } /** + * @Done * @param {number[]} xs * @return {ContinuousDistribution} */ @@ -145,6 +146,7 @@ class ContinuousDistribution { } /** + * Done * @param {number} newLength * @return {ContinuousDistribution} */ @@ -154,6 +156,7 @@ class ContinuousDistribution { } /** + * @Done * @return {number} */ sampleSingle() { @@ -162,6 +165,7 @@ class ContinuousDistribution { } /** + * @Done * Poduce n samples, using ``sampleSingle`` for each. * @param size * @return {number[]}