Added further simple tests
This commit is contained in:
parent
24dc4e657e
commit
d7f95df430
|
@ -72,6 +72,15 @@ describe("Shape", () => {
|
||||||
T.Integral.get(~cache=None, continuous) |> getShape,
|
T.Integral.get(~cache=None, continuous) |> getShape,
|
||||||
{xs: [|1.0, 4.0, 8.0|], ys: [|0.0, 25.5, 47.5|]},
|
{xs: [|1.0, 4.0, 8.0|], ys: [|0.0, 25.5, 47.5|]},
|
||||||
);
|
);
|
||||||
|
makeTest(
|
||||||
|
"toLinear",
|
||||||
|
{
|
||||||
|
let continuous =
|
||||||
|
make({xs: [|1., 4., 8.|], ys: [|0.1, 5., 1.0|]}, `Stepwise);
|
||||||
|
continuous |> toLinear |> getShape;
|
||||||
|
},
|
||||||
|
{xs: [|1.0, 4.0, 4.0, 8.0, 8.0|], ys: [|0.1, 0.1, 5.0, 5.0, 1.0|]},
|
||||||
|
);
|
||||||
makeTest(
|
makeTest(
|
||||||
"integralXToY",
|
"integralXToY",
|
||||||
T.Integral.xToY(~cache=None, 0.0, continuous),
|
T.Integral.xToY(~cache=None, 0.0, continuous),
|
||||||
|
@ -156,4 +165,99 @@ describe("Shape", () => {
|
||||||
);
|
);
|
||||||
makeTest("integralSum", T.Integral.sum(~cache=None, discrete), 1.0);
|
makeTest("integralSum", T.Integral.sum(~cache=None, discrete), 1.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Mixed", () => {
|
||||||
|
open Distributions.Mixed;
|
||||||
|
let discrete: DistTypes.xyShape = {
|
||||||
|
xs: [|1., 4., 8.|],
|
||||||
|
ys: [|0.3, 0.5, 0.2|],
|
||||||
|
};
|
||||||
|
let continuous =
|
||||||
|
Distributions.Continuous.make(
|
||||||
|
{xs: [|3., 7., 14.|], ys: [|0.058, 0.082, 0.124|]},
|
||||||
|
`Linear,
|
||||||
|
)
|
||||||
|
|> Distributions.Continuous.T.scaleToIntegralSum(~intendedSum=1.0);
|
||||||
|
let mixed =
|
||||||
|
MixedShapeBuilder.build(
|
||||||
|
~continuous,
|
||||||
|
~discrete,
|
||||||
|
~assumptions={
|
||||||
|
continuous: ADDS_TO_CORRECT_PROBABILITY,
|
||||||
|
discrete: ADDS_TO_CORRECT_PROBABILITY,
|
||||||
|
discreteProbabilityMass: Some(0.5),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|> E.O.toExn("");
|
||||||
|
makeTest("minX", T.minX(mixed), Some(1.0));
|
||||||
|
makeTest("maxX", T.maxX(mixed), Some(14.0));
|
||||||
|
makeTest(
|
||||||
|
"pointwiseFmap",
|
||||||
|
T.pointwiseFmap(r => r *. 2.0, mixed),
|
||||||
|
Distributions.Mixed.make(
|
||||||
|
~continuous=
|
||||||
|
Distributions.Continuous.make(
|
||||||
|
{
|
||||||
|
xs: [|3., 7., 14.|],
|
||||||
|
ys: [|
|
||||||
|
0.11588411588411589,
|
||||||
|
0.16383616383616384,
|
||||||
|
0.24775224775224775,
|
||||||
|
|],
|
||||||
|
},
|
||||||
|
`Linear,
|
||||||
|
),
|
||||||
|
~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]},
|
||||||
|
~discreteProbabilityMassFraction=0.5,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
makeTest(
|
||||||
|
"xToY at 4.0",
|
||||||
|
T.xToY(4., mixed),
|
||||||
|
{discrete: 0.25, continuous: 0.03196803196803197},
|
||||||
|
);
|
||||||
|
makeTest(
|
||||||
|
"xToY at 0.0",
|
||||||
|
T.xToY(0., mixed),
|
||||||
|
{discrete: 0.0, continuous: 0.028971028971028972},
|
||||||
|
);
|
||||||
|
makeTest(
|
||||||
|
"xToY at 5.0",
|
||||||
|
T.xToY(7., mixed),
|
||||||
|
{discrete: 0.0, continuous: 0.04095904095904096},
|
||||||
|
);
|
||||||
|
makeTest(
|
||||||
|
"scaleBy",
|
||||||
|
T.scaleBy(~scale=2.0, mixed),
|
||||||
|
Distributions.Mixed.make(
|
||||||
|
~continuous=
|
||||||
|
Distributions.Continuous.make(
|
||||||
|
{
|
||||||
|
xs: [|3., 7., 14.|],
|
||||||
|
ys: [|
|
||||||
|
0.11588411588411589,
|
||||||
|
0.16383616383616384,
|
||||||
|
0.24775224775224775,
|
||||||
|
|],
|
||||||
|
},
|
||||||
|
`Linear,
|
||||||
|
),
|
||||||
|
~discrete={xs: [|1., 4., 8.|], ys: [|0.6, 1.0, 0.4|]},
|
||||||
|
~discreteProbabilityMassFraction=0.5,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
makeTest(
|
||||||
|
"integral",
|
||||||
|
T.Integral.get(~cache=None, mixed),
|
||||||
|
Distributions.Continuous.make(
|
||||||
|
{
|
||||||
|
xs: [|1., 3., 4., 4., 7., 8., 8., 14.|],
|
||||||
|
ys: [|0.15, 0.0, 0.15, 0.4, 0.13986013986013987, 0.4, 0.5, 0.5|],
|
||||||
|
},
|
||||||
|
`Linear,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// makeTest("integralXToY", T.Integral.xToY(~cache=None, 6.0, mixed), 0.9);
|
||||||
|
// makeTest("integralSum", T.Integral.sum(~cache=None, mixed), 1.0);
|
||||||
|
});
|
||||||
});
|
});
|
|
@ -60,12 +60,12 @@ let make = (~distPlus: DistTypes.distPlus) => {
|
||||||
() => {<IntegralChart distPlus onHover={r => {setX(_ => r)}} />},
|
() => {<IntegralChart distPlus onHover={r => {setX(_ => r)}} />},
|
||||||
[|distPlus|],
|
[|distPlus|],
|
||||||
);
|
);
|
||||||
// Js.log4(
|
Js.log4(
|
||||||
// "distPlus",
|
"distPlus",
|
||||||
// x,
|
x,
|
||||||
// distPlus,
|
distPlus,
|
||||||
// distPlus |> Distributions.DistPlus.T.xToY(x),
|
distPlus |> Distributions.DistPlus.T.xToY(x),
|
||||||
// );
|
);
|
||||||
<div>
|
<div>
|
||||||
chart
|
chart
|
||||||
chart2
|
chart2
|
||||||
|
|
|
@ -320,9 +320,9 @@ export class CdfChartD3 {
|
||||||
function mouseover() {
|
function mouseover() {
|
||||||
const mouse = d3.mouse(this);
|
const mouse = d3.mouse(this);
|
||||||
hoverLine.attr('opacity', 1).attr('x1', mouse[0]).attr('x2', mouse[0]);
|
hoverLine.attr('opacity', 1).attr('x1', mouse[0]).attr('x2', mouse[0]);
|
||||||
const xValue = mouse[0] > range[0] && mouse[0] < range[1]
|
const xValue = xScale.invert(mouse[0]).toFixed(2);
|
||||||
? xScale.invert(mouse[0]).toFixed(2)
|
// This used to be here, but doesn't seem important
|
||||||
: 0;
|
// const xValue = (mouse[0] > range[0] && mouse[0] < range[1]) ? : 0;
|
||||||
context.attrs.onHover(xValue);
|
context.attrs.onHover(xValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,6 +72,8 @@ module Continuous = {
|
||||||
xyShape: fn(xyShape),
|
xyShape: fn(xyShape),
|
||||||
interpolation,
|
interpolation,
|
||||||
};
|
};
|
||||||
|
let lastY = (t: t) =>
|
||||||
|
t |> xyShape |> XYShape.unsafeLast |> (((_, y)) => y);
|
||||||
let oShapeMap =
|
let oShapeMap =
|
||||||
(fn, {xyShape, interpolation}: t): option(DistTypes.continuousShape) =>
|
(fn, {xyShape, interpolation}: t): option(DistTypes.continuousShape) =>
|
||||||
fn(xyShape) |> E.O.fmap(make(_, interpolation));
|
fn(xyShape) |> E.O.fmap(make(_, interpolation));
|
||||||
|
@ -90,25 +92,6 @@ module Continuous = {
|
||||||
type t = DistTypes.continuousShape;
|
type t = DistTypes.continuousShape;
|
||||||
type integral = DistTypes.continuousShape;
|
type integral = DistTypes.continuousShape;
|
||||||
let shapeFn = (fn, t: t) => t |> xyShape |> fn;
|
let shapeFn = (fn, t: t) => t |> xyShape |> fn;
|
||||||
// TODO: Obviously fix this, it's terrible. Use interpolation param to do appropriate interpolation.
|
|
||||||
// TODO: Steps could be 1 value, interpolation needs at least 2.
|
|
||||||
// TODO: integrateWithTriangles should return (x0, 0.0) as the first item.
|
|
||||||
let integral = (~cache, t) =>
|
|
||||||
cache
|
|
||||||
|> E.O.default(
|
|
||||||
t
|
|
||||||
|> xyShape
|
|
||||||
|> XYShape.Range.integrateWithTriangles
|
|
||||||
|> E.O.toExt("Error1")
|
|
||||||
|> fromShape,
|
|
||||||
);
|
|
||||||
// This seems wrong, we really want the ending bit, I'd assume
|
|
||||||
let integralSum = (~cache, t) =>
|
|
||||||
t
|
|
||||||
|> integral(~cache)
|
|
||||||
|> xyShape
|
|
||||||
|> XYShape.unsafeLast
|
|
||||||
|> (((_, y)) => y);
|
|
||||||
let minX = shapeFn(XYShape.minX);
|
let minX = shapeFn(XYShape.minX);
|
||||||
let maxX = shapeFn(XYShape.maxX);
|
let maxX = shapeFn(XYShape.maxX);
|
||||||
let pointwiseFmap = (fn, t: t) =>
|
let pointwiseFmap = (fn, t: t) =>
|
||||||
|
@ -126,6 +109,16 @@ module Continuous = {
|
||||||
|> XYShape.XtoY.linear(f)
|
|> XYShape.XtoY.linear(f)
|
||||||
|> DistTypes.MixedPoint.makeContinuous
|
|> DistTypes.MixedPoint.makeContinuous
|
||||||
};
|
};
|
||||||
|
let integral = (~cache, t) =>
|
||||||
|
cache
|
||||||
|
|> E.O.default(
|
||||||
|
t
|
||||||
|
|> xyShape
|
||||||
|
|> XYShape.Range.integrateWithTriangles
|
||||||
|
|> E.O.toExt("This should not have happened")
|
||||||
|
|> fromShape,
|
||||||
|
);
|
||||||
|
let integralSum = (~cache, t) => t |> integral(~cache) |> lastY;
|
||||||
let integralXtoY = (~cache, f, t) =>
|
let integralXtoY = (~cache, f, t) =>
|
||||||
t |> integral(~cache) |> shapeFn(CdfLibrary.Distribution.findY(f));
|
t |> integral(~cache) |> shapeFn(CdfLibrary.Distribution.findY(f));
|
||||||
let toContinuous = t => Some(t);
|
let toContinuous = t => Some(t);
|
||||||
|
@ -135,23 +128,16 @@ module Continuous = {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// |> XYShape.Range.stepsToContinuous
|
|
||||||
// |> E.O.toExt("ERROR"),
|
|
||||||
module Discrete = {
|
module Discrete = {
|
||||||
module T =
|
module T =
|
||||||
Dist({
|
Dist({
|
||||||
type t = DistTypes.discreteShape;
|
type t = DistTypes.discreteShape;
|
||||||
type integral = DistTypes.continuousShape;
|
type integral = DistTypes.continuousShape;
|
||||||
// todo: test this. Remove "stepstoContinuos-move elsewhere"
|
|
||||||
// todo: Make sure this works fine with one value. This is important for step functionality.
|
|
||||||
let integral = (~cache, t) =>
|
let integral = (~cache, t) =>
|
||||||
cache
|
cache
|
||||||
|> E.O.default(
|
|> E.O.default(Continuous.make(XYShape.accumulateYs(t), `Stepwise));
|
||||||
{
|
let integralSum = (~cache, t) =>
|
||||||
Continuous.make(XYShape.accumulateYs(t), `Stepwise);
|
t |> integral(~cache) |> Continuous.lastY;
|
||||||
},
|
|
||||||
);
|
|
||||||
let integralSum = (~cache, t) => t |> XYShape.ySum;
|
|
||||||
let minX = XYShape.minX;
|
let minX = XYShape.minX;
|
||||||
let maxX = XYShape.maxX;
|
let maxX = XYShape.maxX;
|
||||||
let pointwiseFmap = XYShape.pointwiseMap;
|
let pointwiseFmap = XYShape.pointwiseMap;
|
||||||
|
|
|
@ -120,9 +120,6 @@ module Range = {
|
||||||
let toT = r => r |> Belt.Array.unzip |> fromArray;
|
let toT = r => r |> Belt.Array.unzip |> fromArray;
|
||||||
let nextX = ((_, (nextX, _)): zippedRange) => nextX;
|
let nextX = ((_, (nextX, _)): zippedRange) => nextX;
|
||||||
|
|
||||||
let rangeAreaAssumingSteps = (((lastX, lastY), (nextX, _)): zippedRange) =>
|
|
||||||
(nextX -. lastX) *. lastY;
|
|
||||||
|
|
||||||
let rangePointAssumingSteps =
|
let rangePointAssumingSteps =
|
||||||
(((lastX, lastY), (nextX, nextY)): zippedRange) => (
|
(((lastX, lastY), (nextX, nextY)): zippedRange) => (
|
||||||
nextX,
|
nextX,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user