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 = { module Shapee = {
[@react.component] [@react.component]
let make = (~shape: DistributionTypes.pointsType, ~timeScale, ~onHover) => { let make = (~shape: DistributionTypes.pointsType, ~timeScale, ~onHover) => {
let discrete = Shape.Any.scaledDiscreteComponent(shape); let discrete = Shape.PointsType.scaledDiscreteComponent(shape);
let continuous = Shape.Any.scaledContinuousComponent(shape); let continuous = Shape.PointsType.scaledContinuousComponent(shape);
<div> <div>
<CdfChart__Plain <CdfChart__Plain
minX={Shape.Any.minX(shape)} minX={Shape.PointsType.minX(shape)}
maxX={Shape.Any.maxX(shape)} maxX={Shape.PointsType.maxX(shape)}
?discrete ?discrete
?continuous ?continuous
color={`hex("333")} 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) => { let normalize = (t: genericDistribution): option(genericDistribution) => {
switch (t.generationSource) { switch (t.generationSource) {
| Shape(shape) => | Shape(shape) =>
normalizePdf(shape) Shape.PointsType.normalizePdf(shape)
|> E.O.fmap(shape => {...t, generationSource: Shape(shape)}) |> E.O.fmap(shape => {...t, generationSource: Shape(shape)})
| GuesstimatorString(_) => Some(t) | GuesstimatorString(_) => Some(t)
}; };
@ -125,7 +91,7 @@ let yIntegral = (t: DistributionTypes.genericDistribution, x) => {
let normalize = n => n *. Domain.normalizeProbabilityMass(t.domain); let normalize = n => n *. Domain.normalizeProbabilityMass(t.domain);
switch (t) { switch (t) {
| {generationSource: Shape(shape)} => | {generationSource: Shape(shape)} =>
Shape.Any.yIntegral(shape, x) Shape.PointsType.yIntegral(shape, x)
|> E.O.fmap(addInitialMass) |> E.O.fmap(addInitialMass)
|> E.O.fmap(normalize) |> E.O.fmap(normalize)
| _ => None | _ => None

View File

@ -234,7 +234,7 @@ module Mixed = {
}; };
}; };
module Any = { module PointsType = {
type t = DistributionTypes.pointsType; type t = DistributionTypes.pointsType;
let y = (t: t, x: float) => let y = (t: t, x: float) =>
@ -318,4 +318,38 @@ module Any = {
| Continuous(_) => None | 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 => let xRisk = conditionals =>
Prop.Value.ConditionalArray.get(conditionals, "Global Existential Event"); 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 = let make =
( (
group: group, group: group,
@ -131,7 +146,14 @@ module Model = {
switch (xRisk) { switch (xRisk) {
| Some({truthValue: true}) => "0" | Some({truthValue: true}) => "0"
| Some({truthValue: false}) => difference | 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 = 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 = { module Model = {
let make = (currentDateTime: MomentRe.Moment.t) => { let make = (currentDateTime: MomentRe.Moment.t) => {
@ -6,7 +6,6 @@ module Model = {
GenericDistribution.make( GenericDistribution.make(
~generationSource=GuesstimatorString(guesstimatorString), ~generationSource=GuesstimatorString(guesstimatorString),
~probabilityType=Cdf, ~probabilityType=Cdf,
~domain=RightLimited({xPoint: 200., excludingProbabilityMass: 0.3}),
~unit=TimeDistribution({zero: currentDateTime, unit: `years}), ~unit=TimeDistribution({zero: currentDateTime, unit: `years}),
(), (),
); );