From 188266546d26f0df1a0f58dda33f0ced3620c18a Mon Sep 17 00:00:00 2001 From: Roman Galochkin Date: Tue, 25 Feb 2020 12:04:26 +0300 Subject: [PATCH] FindX tests --- __tests__/CDF__Test.re | 34 +++++++++++++++++++---- src/utility/lib/continuousDistribution.js | 2 ++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/__tests__/CDF__Test.re b/__tests__/CDF__Test.re index cf271f94..66c35fdf 100644 --- a/__tests__/CDF__Test.re +++ b/__tests__/CDF__Test.re @@ -59,23 +59,45 @@ describe("CDF", () => { CDF.Make({ let shape = CDF.order({xs: [|1., 2., 3.|], ys: [|5., 6., 7.|]}); }); - test("findY#1", () => { + test("#1", () => { expect(Dist.findY(1.)) |> toEqual(5.) }); - test("findY#2", () => { + test("#2", () => { expect(Dist.findY(1.5)) |> toEqual(5.5) }); - test("findY#3", () => { + test("#3", () => { expect(Dist.findY(3.)) |> toEqual(7.) }); - test("findY#4", () => { + test("#4", () => { expect(Dist.findY(4.)) |> toEqual(7.) }); - test("findY#5", () => { + test("#5", () => { expect(Dist.findY(15.)) |> toEqual(7.) }); - test("findY#6", () => { + test("#6", () => { expect(Dist.findY(-1.)) |> toEqual(5.) }); }); + + describe("findX", () => { + module Dist = + CDF.Make({ + let shape = CDF.order({xs: [|1., 2., 3.|], ys: [|5., 6., 7.|]}); + }); + test("#1", () => { + expect(Dist.findX(5.)) |> toEqual(1.) + }); + test("#2", () => { + expect(Dist.findX(7.)) |> toEqual(3.) + }); + test("#3", () => { + expect(Dist.findX(5.5)) |> toEqual(1.5) + }); + test("#4", () => { + expect(Dist.findX(8.)) |> toEqual(3.) + }); + test("#5", () => { + expect(Dist.findX(4.)) |> toEqual(1.) + }); + }); }); diff --git a/src/utility/lib/continuousDistribution.js b/src/utility/lib/continuousDistribution.js index 75355a36..1e639398 100644 --- a/src/utility/lib/continuousDistribution.js +++ b/src/utility/lib/continuousDistribution.js @@ -117,8 +117,10 @@ class ContinuousDistribution { let firstHigherIndex = this.ys.findIndex(Y => Y >= y); if (firstHigherIndex < 0) return this.xs[this.xs.length - 1]; if (firstHigherIndex === 0) return this.xs[0]; + let lowerOrEqualIndex = firstHigherIndex - 1; if (lowerOrEqualIndex < 0) lowerOrEqualIndex = 0; + let needsInterpolation = this.ys[lowerOrEqualIndex] !== y; if (needsInterpolation) { return interpolate(