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