Added further simple tests

This commit is contained in:
Ozzie Gooen 2020-02-24 11:11:03 +00:00
parent 24dc4e657e
commit d7f95df430
5 changed files with 128 additions and 41 deletions

View File

@ -72,6 +72,15 @@ describe("Shape", () => {
T.Integral.get(~cache=None, continuous) |> getShape,
{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(
"integralXToY",
T.Integral.xToY(~cache=None, 0.0, continuous),
@ -156,4 +165,99 @@ describe("Shape", () => {
);
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);
});
});

View File

@ -60,12 +60,12 @@ let make = (~distPlus: DistTypes.distPlus) => {
() => {<IntegralChart distPlus onHover={r => {setX(_ => r)}} />},
[|distPlus|],
);
// Js.log4(
// "distPlus",
// x,
// distPlus,
// distPlus |> Distributions.DistPlus.T.xToY(x),
// );
Js.log4(
"distPlus",
x,
distPlus,
distPlus |> Distributions.DistPlus.T.xToY(x),
);
<div>
chart
chart2

View File

@ -320,9 +320,9 @@ export class CdfChartD3 {
function mouseover() {
const mouse = d3.mouse(this);
hoverLine.attr('opacity', 1).attr('x1', mouse[0]).attr('x2', mouse[0]);
const xValue = mouse[0] > range[0] && mouse[0] < range[1]
? xScale.invert(mouse[0]).toFixed(2)
: 0;
const xValue = xScale.invert(mouse[0]).toFixed(2);
// This used to be here, but doesn't seem important
// const xValue = (mouse[0] > range[0] && mouse[0] < range[1]) ? : 0;
context.attrs.onHover(xValue);
}

View File

@ -72,6 +72,8 @@ module Continuous = {
xyShape: fn(xyShape),
interpolation,
};
let lastY = (t: t) =>
t |> xyShape |> XYShape.unsafeLast |> (((_, y)) => y);
let oShapeMap =
(fn, {xyShape, interpolation}: t): option(DistTypes.continuousShape) =>
fn(xyShape) |> E.O.fmap(make(_, interpolation));
@ -90,25 +92,6 @@ module Continuous = {
type t = DistTypes.continuousShape;
type integral = DistTypes.continuousShape;
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 maxX = shapeFn(XYShape.maxX);
let pointwiseFmap = (fn, t: t) =>
@ -126,6 +109,16 @@ module Continuous = {
|> XYShape.XtoY.linear(f)
|> 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) =>
t |> integral(~cache) |> shapeFn(CdfLibrary.Distribution.findY(f));
let toContinuous = t => Some(t);
@ -135,23 +128,16 @@ module Continuous = {
});
};
// |> XYShape.Range.stepsToContinuous
// |> E.O.toExt("ERROR"),
module Discrete = {
module T =
Dist({
type t = DistTypes.discreteShape;
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) =>
cache
|> E.O.default(
{
Continuous.make(XYShape.accumulateYs(t), `Stepwise);
},
);
let integralSum = (~cache, t) => t |> XYShape.ySum;
|> E.O.default(Continuous.make(XYShape.accumulateYs(t), `Stepwise));
let integralSum = (~cache, t) =>
t |> integral(~cache) |> Continuous.lastY;
let minX = XYShape.minX;
let maxX = XYShape.maxX;
let pointwiseFmap = XYShape.pointwiseMap;

View File

@ -120,9 +120,6 @@ module Range = {
let toT = r => r |> Belt.Array.unzip |> fromArray;
let nextX = ((_, (nextX, _)): zippedRange) => nextX;
let rangeAreaAssumingSteps = (((lastX, lastY), (nextX, _)): zippedRange) =>
(nextX -. lastX) *. lastY;
let rangePointAssumingSteps =
(((lastX, lastY), (nextX, nextY)): zippedRange) => (
nextX,