diff --git a/.gitignore b/.gitignore index 9b548cc4..72fa0d42 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ lib/* *.cache build yarn-error.log -*.bs.js \ No newline at end of file +*.bs.js +# Local Netlify folder +.netlify \ No newline at end of file diff --git a/src/LimitedDomainCdf.re b/src/LimitedDomainCdf.re index 7a33db45..fad1d421 100644 --- a/src/LimitedDomainCdf.re +++ b/src/LimitedDomainCdf.re @@ -19,8 +19,6 @@ let _lastElement = (a: array('a)) => let probabilityBeforeDomainMax = (t: t) => _lastElement(t.distribution.ys); -let chanceByX = (t: t) => t.distribution; - let domainMaxX = (t: t) => t.domainMaxX; let probabilityDistribution = (t: t) => @@ -29,5 +27,11 @@ let probabilityDistribution = (t: t) => let probability = (t: t, xPoint: float) => CdfLibrary.Distribution.findY(xPoint, probabilityDistribution(t)); +let probabilityInverse = (t: t, yPoint: float) => + CdfLibrary.Distribution.findX(yPoint, probabilityDistribution(t)); + let cumulativeProbability = (t: t, xPoint: float) => - CdfLibrary.Distribution.findY(xPoint, t.distribution); \ No newline at end of file + CdfLibrary.Distribution.findY(xPoint, t.distribution); + +let cumulativeProbabilityInverse = (t: t, yPoint: float) => + CdfLibrary.Distribution.findX(yPoint, t.distribution); \ No newline at end of file diff --git a/src/TimeLimitedDomainCdf.re b/src/TimeLimitedDomainCdf.re new file mode 100644 index 00000000..152497a9 --- /dev/null +++ b/src/TimeLimitedDomainCdf.re @@ -0,0 +1,74 @@ +type timeUnit = [ + | `days + | `hours + | `milliseconds + | `minutes + | `months + | `quarters + | `seconds + | `weeks + | `years +]; + +type t = { + timeUnit, + timeStart: MomentRe.Moment.t, + limitedDomainCdf: LimitedDomainCdf.t, +}; + +module XSpecification = { + type xSpecification = + | Time(MomentRe.Moment.t) + | DifferenceFromStart(float, timeUnit) + | CdfXCoordinate(float); + + let toTime = (t: t, xSpecification: xSpecification) => + switch (xSpecification) { + | Time(r) => r + | DifferenceFromStart(r, unit) => + t.timeStart + |> MomentRe.Moment.add(~duration=MomentRe.duration(r, unit)) + | CdfXCoordinate(r) => + t.timeStart + |> MomentRe.Moment.add(~duration=MomentRe.duration(r, t.timeUnit)) + }; + + let rec toCdfXCoordinate = (t: t, xSpecification: xSpecification) => + switch (xSpecification) { + | Time(r) => MomentRe.diff(t.timeStart, r, t.timeUnit) + | DifferenceFromStart(r, unit) => + let newTime = toTime(t, DifferenceFromStart(r, unit)); + toCdfXCoordinate(t, Time(newTime)); + | CdfXCoordinate(r) => r + }; + + let fromDifference = (~t: t, ~duration: float, ~unit=t.timeUnit, ()) => + Time( + MomentRe.Moment.add( + ~duration=MomentRe.duration(duration, unit), + t.timeStart, + ), + ); +}; + +let probabilityBeforeDomainMax = (t: t) => + LimitedDomainCdf.probabilityBeforeDomainMax(t.limitedDomainCdf); + +let domainMaxX = (t: t) => + LimitedDomainCdf.probabilityBeforeDomainMax(t.limitedDomainCdf); + +let probability = (t: t, x: XSpecification.xSpecification) => + LimitedDomainCdf.probability( + t.limitedDomainCdf, + XSpecification.toCdfXCoordinate(t, x), + ); + +let probabilityInverse = (t: t, y: float) => + XSpecification.CdfXCoordinate( + LimitedDomainCdf.probabilityInverse(t.limitedDomainCdf, y), + ); + +let cumulativeProbabilityInverse = (t: t, y: float) => + XSpecification.CdfXCoordinate( + LimitedDomainCdf.cumulativeProbabilityInverse(t.limitedDomainCdf, y), + ); \ No newline at end of file diff --git a/src/models/GlobalCatastrophe.re b/src/models/GlobalCatastrophe.re index 7487959d..6c843644 100644 --- a/src/models/GlobalCatastrophe.re +++ b/src/models/GlobalCatastrophe.re @@ -1,9 +1,4 @@ module Model = { - let foo = { - let bar = 2.0; - (); - }; - let make = (dateTime: MomentRe.Moment.t, currentDateTime: MomentRe.Moment.t) => { let yearDiff = MomentRe.diff(dateTime, currentDateTime, `days) /. 365.; Prop.Value.Probability(0.001 *. yearDiff);