From 7bc62d393486c40a1e2e0840130d17e1484d9988 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Fri, 21 Feb 2020 11:45:32 +0000 Subject: [PATCH] Moved some functionality to Shape.re --- .../charts/GenericDistributionChart.re | 8 ++-- src/core/GenericDistribution.re | 38 +------------------ src/core/Shape.re | 36 +++++++++++++++++- src/models/EAFunds.re | 24 +++++++++++- src/models/GlobalCatastrophe.re | 3 +- 5 files changed, 65 insertions(+), 44 deletions(-) diff --git a/src/components/charts/GenericDistributionChart.re b/src/components/charts/GenericDistributionChart.re index 675c9cb7..42bf3200 100644 --- a/src/components/charts/GenericDistributionChart.re +++ b/src/components/charts/GenericDistributionChart.re @@ -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);
{ - 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 diff --git a/src/core/Shape.re b/src/core/Shape.re index 58689e91..f54ade9e 100644 --- a/src/core/Shape.re +++ b/src/core/Shape.re @@ -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)) + }; + }; }; \ No newline at end of file diff --git a/src/models/EAFunds.re b/src/models/EAFunds.re index 818081ba..e34c1899 100644 --- a/src/models/EAFunds.re +++ b/src/models/EAFunds.re @@ -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 = diff --git a/src/models/GlobalCatastrophe.re b/src/models/GlobalCatastrophe.re index a0c7ef21..5be38fd7 100644 --- a/src/models/GlobalCatastrophe.re +++ b/src/models/GlobalCatastrophe.re @@ -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}), (), );