Adds integral function

This commit is contained in:
Roman Galochkin 2020-02-25 13:08:16 +03:00
parent c97c675642
commit 4a88acae66
3 changed files with 33 additions and 9 deletions

View File

@ -165,4 +165,15 @@ describe("CDF", () => {
expect(xs2[2]) |> toBe(70.) expect(xs2[2]) |> toBe(70.)
}); });
}); });
describe("integral", () => {
module Dist =
CDF.Make({
let shape =
CDF.order({xs: [|0., 1., 2., 4.|], ys: [|0.0, 1.0, 2.0, 2.0|]});
});
test("with regular inputs", () => {
expect(Dist.integral()) |> toBe(6.)
});
});
}); });

View File

@ -90,5 +90,18 @@ module Make = (Config: Config) => {
let sampleSingle = (): float => Js.Math.random() |> findY; let sampleSingle = (): float => Js.Math.random() |> findY;
let sample = (size: int): array(float) => let sample = (size: int): array(float) =>
Belt.Array.makeBy(size, i => sampleSingle()); Belt.Array.makeBy(size, i => sampleSingle());
1; let integral = () => {
Belt.Array.reduceWithIndex(ys, 0., (integral, y, i) => {
switch (i) {
| 0 => integral
| _ =>
let thisY = y;
let lastY = get(ys, i - 1);
let thisX = get(xs, i);
let lastX = get(xs, i - 1);
let sectionInterval = (thisY +. lastY) /. 2. *. (thisX -. lastX);
integral +. sectionInterval;
}
});
};
}; };

View File

@ -66,45 +66,45 @@ describe('ContinuousDistribution Class', () => {
describe('integral()', () => { describe('integral()', () => {
it('with regular inputs', () => { it('with regular inputs', () => {
const xs = [0,1,2,4]; const xs = [0, 1, 2, 4];
const ys = [0.0, 1.0, 2.0, 2.0]; const ys = [0.0, 1.0, 2.0, 2.0];
const cdf = new ContinuousDistribution(xs, ys); const cdf = new ContinuousDistribution(xs, ys);
const integral = cdf.integral(); const integral = cdf.integral();
expect(integral).toEqual(6); expect(integral).toEqual(6);
}); });
it('with an infinity', () => { it('with an infinity', () => {
const xs = [0,1,2,4]; const xs = [0, 1, 2, 4];
const ys = [0.0, 1.0, Infinity, 2.0]; const ys = [0.0, 1.0, Infinity, 2.0];
const cdf = new ContinuousDistribution(xs, ys); const cdf = new ContinuousDistribution(xs, ys);
const integral = cdf.integral(); const integral = cdf.integral();
expect(integral).toEqual(Infinity); expect(integral).toEqual(Infinity);
}); });
it('with negative infinity', () => { it('with negative infinity', () => {
const xs = [0,1,2,4]; const xs = [0, 1, 2, 4];
const ys = [0.0, 1.0, -Infinity, 2.0]; const ys = [0.0, 1.0, -Infinity, 2.0];
const cdf = new ContinuousDistribution(xs, ys); const cdf = new ContinuousDistribution(xs, ys);
const integral = cdf.integral(); const integral = cdf.integral();
expect(integral).toEqual(-Infinity); expect(integral).toEqual(-Infinity);
}); });
it('with both positive and negative infinities', () => { it('with both positive and negative infinities', () => {
const xs = [0,1,2,4]; const xs = [0, 1, 2, 4];
const ys = [0.0, 1.0, -Infinity, Infinity]; const ys = [0.0, 1.0, -Infinity, Infinity];
const cdf = new ContinuousDistribution(xs, ys); const cdf = new ContinuousDistribution(xs, ys);
const integral = cdf.integral(); const integral = cdf.integral();
expect(integral).toEqual(NaN); expect(integral).toEqual(NaN);
}); });
it('with a NaN and filterOutNaNs set to false', () => { it('with a NaN and filterOutNaNs set to false', () => {
const xs = [0,1,2,4]; const xs = [0, 1, 2, 4];
const ys = [0.0, 1.0, 2.0, NaN]; const ys = [0.0, 1.0, 2.0, NaN];
const cdf = new ContinuousDistribution(xs, ys); const cdf = new ContinuousDistribution(xs, ys);
const integral = cdf.integral({filterOutNaNs: false}); const integral = cdf.integral({ filterOutNaNs: false });
expect(integral).toEqual(NaN); expect(integral).toEqual(NaN);
}); });
it('with a NaN and filterOutNaNs set to true', () => { it('with a NaN and filterOutNaNs set to true', () => {
const xs = [0,1,2,4]; const xs = [0, 1, 2, 4];
const ys = [0.0, 1.0, 2.0, NaN]; const ys = [0.0, 1.0, 2.0, NaN];
const cdf = new ContinuousDistribution(xs, ys); const cdf = new ContinuousDistribution(xs, ys);
const integral = cdf.integral({filterOutNaNs: true}); const integral = cdf.integral({ filterOutNaNs: true });
expect(integral).toEqual(2); expect(integral).toEqual(2);
}); });
}) })