Moved some functionality to Shape.re

This commit is contained in:
Ozzie Gooen 2020-02-21 11:45:32 +00:00
parent 710edc2d21
commit 7bc62d3934
5 changed files with 65 additions and 44 deletions

View File

@ -1,12 +1,12 @@
module Shapee = {
[@react.component]
let make = (~shape: DistributionTypes.pointsType, ~timeScale, ~onHover) => {
let discrete = Shape.Any.scaledDiscreteComponent(shape);
let continuous = Shape.Any.scaledContinuousComponent(shape);
let discrete = Shape.PointsType.scaledDiscreteComponent(shape);
let continuous = Shape.PointsType.scaledContinuousComponent(shape);
<div>
<CdfChart__Plain
minX={Shape.Any.minX(shape)}
maxX={Shape.Any.maxX(shape)}
minX={Shape.PointsType.minX(shape)}
maxX={Shape.PointsType.maxX(shape)}
?discrete
?continuous
color={`hex("333")}

View File

@ -77,44 +77,10 @@ let renderIfNeeded =
};
};
let normalizeCdf = (t: DistributionTypes.pointsType) => {
switch (t) {
| Mixed({continuous, discrete, discreteProbabilityMassFraction}) =>
Mixed({
continuous: continuous |> Shape.Continuous.normalizeCdf,
discrete: discrete |> Shape.Discrete.scaleYToTotal(1.0),
discreteProbabilityMassFraction,
})
| Discrete(d) => Discrete(d |> Shape.Discrete.scaleYToTotal(1.0))
| Continuous(continuousShape) =>
Continuous(Shape.Continuous.normalizeCdf(continuousShape))
};
};
let normalizePdf = (t: DistributionTypes.pointsType) => {
switch (t) {
| Mixed({continuous, discrete, discreteProbabilityMassFraction}) =>
continuous
|> Shape.Continuous.scalePdf(~scaleTo=1.0)
|> E.O.fmap(r =>
Mixed({
continuous: r,
discrete: discrete |> Shape.Discrete.scaleYToTotal(1.0),
discreteProbabilityMassFraction,
})
)
| Discrete(d) => Some(Discrete(d |> Shape.Discrete.scaleYToTotal(1.0)))
| Continuous(continuousShape) =>
continuousShape
|> Shape.Continuous.scalePdf(~scaleTo=1.0)
|> E.O.fmap(r => Continuous(r))
};
};
let normalize = (t: genericDistribution): option(genericDistribution) => {
switch (t.generationSource) {
| Shape(shape) =>
normalizePdf(shape)
Shape.PointsType.normalizePdf(shape)
|> E.O.fmap(shape => {...t, generationSource: Shape(shape)})
| GuesstimatorString(_) => Some(t)
};
@ -125,7 +91,7 @@ let yIntegral = (t: DistributionTypes.genericDistribution, x) => {
let normalize = n => n *. Domain.normalizeProbabilityMass(t.domain);
switch (t) {
| {generationSource: Shape(shape)} =>
Shape.Any.yIntegral(shape, x)
Shape.PointsType.yIntegral(shape, x)
|> E.O.fmap(addInitialMass)
|> E.O.fmap(normalize)
| _ => None

View File

@ -234,7 +234,7 @@ module Mixed = {
};
};
module Any = {
module PointsType = {
type t = DistributionTypes.pointsType;
let y = (t: t, x: float) =>
@ -318,4 +318,38 @@ module Any = {
| Continuous(_) => None
};
};
let normalizeCdf = (t: DistributionTypes.pointsType) => {
switch (t) {
| Mixed({continuous, discrete, discreteProbabilityMassFraction}) =>
Mixed({
continuous: continuous |> Continuous.normalizeCdf,
discrete: discrete |> Discrete.scaleYToTotal(1.0),
discreteProbabilityMassFraction,
})
| Discrete(d) => Discrete(d |> Discrete.scaleYToTotal(1.0))
| Continuous(continuousShape) =>
Continuous(Continuous.normalizeCdf(continuousShape))
};
};
let normalizePdf = (t: DistributionTypes.pointsType) => {
switch (t) {
| Mixed({continuous, discrete, discreteProbabilityMassFraction}) =>
continuous
|> Continuous.scalePdf(~scaleTo=1.0)
|> E.O.fmap(r =>
Mixed({
continuous: r,
discrete: discrete |> Discrete.scaleYToTotal(1.0),
discreteProbabilityMassFraction,
})
)
| Discrete(d) => Some(Discrete(d |> Discrete.scaleYToTotal(1.0)))
| Continuous(continuousShape) =>
continuousShape
|> Continuous.scalePdf(~scaleTo=1.0)
|> E.O.fmap(r => Continuous(r))
};
};
};

View File

@ -107,6 +107,21 @@ module Model = {
let xRisk = conditionals =>
Prop.Value.ConditionalArray.get(conditionals, "Global Existential Event");
// TODO: Fixe number that integral is calculated for
let getGlobalCatastropheChance = dateTime => {
let model = GlobalCatastrophe.Model.make(dateTime);
switch (model) {
| Prop.Value.GenericDistribution(genericDistribution) =>
GenericDistribution.renderIfNeeded(
~sampleCount=1000,
genericDistribution,
)
|> E.O.bind(_, GenericDistribution.normalize)
|> E.O.bind(_, GenericDistribution.yIntegral(_, 18.0))
| _ => None
};
};
let make =
(
group: group,
@ -131,7 +146,14 @@ module Model = {
switch (xRisk) {
| Some({truthValue: true}) => "0"
| Some({truthValue: false}) => difference
| None => "uniform(0,1) > .3 ? " ++ difference ++ ": 0"
| None =>
let foo =
getGlobalCatastropheChance(dateTime)
|> E.O.fmap(E.Float.with2DigitsPrecision)
|> E.O.fmap((r: string) =>
"uniform(0,1) > " ++ r ++ " ? " ++ difference ++ ": 0"
);
foo |> E.O.default("");
};
let genericDistribution =

View File

@ -1,4 +1,4 @@
let guesstimatorString = "mm(10 to 30, floor(20 to 25), [.5,.5])";
let guesstimatorString = "20 to 80";
module Model = {
let make = (currentDateTime: MomentRe.Moment.t) => {
@ -6,7 +6,6 @@ module Model = {
GenericDistribution.make(
~generationSource=GuesstimatorString(guesstimatorString),
~probabilityType=Cdf,
~domain=RightLimited({xPoint: 200., excludingProbabilityMass: 0.3}),
~unit=TimeDistribution({zero: currentDateTime, unit: `years}),
(),
);