Improved handling of mixed discreteProbabilityMassFraction

This commit is contained in:
Ozzie Gooen 2020-02-16 11:01:37 +00:00
parent 03cc7293a4
commit 5de75a402b
3 changed files with 27 additions and 8 deletions

View File

@ -61,10 +61,19 @@ module Value = {
let newDistribution = let newDistribution =
GenericDistribution.renderIfNeeded(~sampleCount=1000, r); GenericDistribution.renderIfNeeded(~sampleCount=1000, r);
switch (newDistribution) { switch (newDistribution) {
| Some({generationSource: Shape(Mixed({continuous: n, discrete: d}))}) => | Some({
generationSource:
Shape(
Mixed({
continuous: n,
discrete: d,
discreteProbabilityMassFraction: f,
}),
),
}) =>
<div> <div>
<Chart height=100 data={n |> Shape.Continuous.toJs} /> <Chart height=100 data={n |> Shape.Continuous.toJs} />
{Shape.Discrete.render(d)} {d |> Shape.Discrete.scaleYToTotal(f) |> Shape.Discrete.render}
</div> </div>
| None => "Something went wrong" |> ReasonReact.string | None => "Something went wrong" |> ReasonReact.string
| _ => <div /> | _ => <div />

View File

@ -38,7 +38,7 @@ module Discrete = {
Belt.Array.zip(p.xs, p.ys) Belt.Array.zip(p.xs, p.ys)
->Belt.Array.reduce([||], (items, (x, y)) => ->Belt.Array.reduce([||], (items, (x, y)) =>
switch (_lastElement(items)) { switch (_lastElement(items)) {
| Some((_, yLast)) => [|(x, y +. yLast)|] | Some((_, yLast)) => E.A.append(items, [|(x, y +. yLast)|])
| None => [|(x, y)|] | None => [|(x, y)|]
} }
) )
@ -46,6 +46,16 @@ module Discrete = {
fromArrays(xs, ys); fromArrays(xs, ys);
}; };
let ySum = (t: t) => {
E.A.fold_left((a, b) => a +. b, 0., t.ys);
};
let scaleYToTotal = (totalDesired, t: t): t => {
let currentSum = ySum(t);
let difference = totalDesired /. currentSum;
{xs: t.xs, ys: t.ys |> E.A.fmap(y => y *. difference)};
};
let render = (t: t) => let render = (t: t) =>
Belt.Array.zip(t.xs, t.ys) Belt.Array.zip(t.xs, t.ys)
|> E.A.fmap(((x, y)) => |> E.A.fmap(((x, y)) =>
@ -109,11 +119,11 @@ module Mixed = {
| { | {
continuous: ADDS_TO_1, continuous: ADDS_TO_1,
discrete: ADDS_TO_CORRECT_PROBABILITY, discrete: ADDS_TO_CORRECT_PROBABILITY,
discreteProbabilityMass: Some(r), discreteProbabilityMass: None,
} => } =>
Some( let discreteProbabilityMassFraction = Discrete.ySum(discrete);
make(~continuous, ~discrete, ~discreteProbabilityMassFraction=r), let discrete = Discrete.scaleYToTotal(1.0, discrete);
) Some(make(~continuous, ~discrete, ~discreteProbabilityMassFraction));
| _ => None | _ => None
}; };
}; };

View File

@ -28,7 +28,7 @@ module Internals = {
let assumptions: Shape.Mixed.Builder.assumptions = { let assumptions: Shape.Mixed.Builder.assumptions = {
continuous: ADDS_TO_1, continuous: ADDS_TO_1,
discrete: ADDS_TO_CORRECT_PROBABILITY, discrete: ADDS_TO_CORRECT_PROBABILITY,
discreteProbabilityMass: Some(0.3), discreteProbabilityMass: None,
}; };
Shape.Mixed.Builder.build( Shape.Mixed.Builder.build(
~continuous=toContinous(r), ~continuous=toContinous(r),