FindX tests

This commit is contained in:
Roman Galochkin 2020-02-25 12:04:26 +03:00
parent 94a9712788
commit 188266546d
2 changed files with 30 additions and 6 deletions

View File

@ -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.)
});
});
});

View File

@ -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(