Simple mixed distribution

This commit is contained in:
Ozzie Gooen 2020-02-15 16:29:23 +00:00
parent 5bd66be29e
commit ab9c8726d6
5 changed files with 84 additions and 54 deletions

View File

@ -1,12 +1,17 @@
type t = { type t = {
distribution: Types.distribution, distribution: Types.ContinuousDistribution.t,
domainMaxX: float, domainMaxX: float,
}; };
let make = (~distribution, ~domainMaxX): t => {distribution, domainMaxX}; let make = (~distribution, ~domainMaxX): t => {distribution, domainMaxX};
let fromCdf = (cdf: Types.cdf, domainMaxX: float, probabilityAtMaxX: float) => { let fromCdf =
let distribution: Types.distribution = { (
cdf: Types.ContinuousDistribution.t,
domainMaxX: float,
probabilityAtMaxX: float,
) => {
let distribution: Types.ContinuousDistribution.t = {
xs: cdf.xs, xs: cdf.xs,
ys: cdf.ys |> E.A.fmap(r => r *. probabilityAtMaxX), ys: cdf.ys |> E.A.fmap(r => r *. probabilityAtMaxX),
}; };

View File

@ -66,7 +66,7 @@ type t = {
let make = let make =
( (
~timeVector: timeVector, ~timeVector: timeVector,
~distribution: Types.distribution, ~distribution: Types.ContinuousDistribution.t,
~probabilityAtMaxX: float, ~probabilityAtMaxX: float,
~maxX: [ | `time(MomentRe.Moment.t) | `x(float)], ~maxX: [ | `time(MomentRe.Moment.t) | `x(float)],
) )

View File

@ -17,7 +17,7 @@ module Value = {
| Conditional(conditional) | Conditional(conditional)
| TimeLimitedDomainCdf(TimeLimitedDomainCdf.t) | TimeLimitedDomainCdf(TimeLimitedDomainCdf.t)
| TimeLimitedDomainCdfLazy( | TimeLimitedDomainCdfLazy(
(string => Types.distribution) => TimeLimitedDomainCdf.t, (string => Types.ContinuousDistribution.t) => TimeLimitedDomainCdf.t,
) )
| ConditionalArray(array(conditional)) | ConditionalArray(array(conditional))
| FloatCdf(string); | FloatCdf(string);
@ -59,23 +59,34 @@ module Value = {
let timeLimited = r(CdfLibrary.Distribution.fromString(_, 1000)); let timeLimited = r(CdfLibrary.Distribution.fromString(_, 1000));
let cdf = timeLimited.limitedDomainCdf.distribution; let cdf = timeLimited.limitedDomainCdf.distribution;
<> <>
<Chart height=100 data={cdf |> Types.toJs} /> <Chart height=100 data={cdf |> Types.ContinuousDistribution.toJs} />
<Chart <Chart
height=100 height=100
data={cdf |> CdfLibrary.Distribution.toPdf |> Types.toJs} data={
cdf
|> CdfLibrary.Distribution.toPdf
|> Types.ContinuousDistribution.toJs
}
/> />
{FloatCdf.logNormal(50., 20.) |> ReasonReact.string} {FloatCdf.logNormal(50., 20.) |> ReasonReact.string}
</>; </>;
| TimeLimitedDomainCdf(r) => | TimeLimitedDomainCdf(r) =>
let cdf: Types.distribution = r.limitedDomainCdf.distribution; let cdf: Types.ContinuousDistribution.t =
<> <Chart height=100 data={cdf |> Types.toJs} /> </>; r.limitedDomainCdf.distribution;
<>
<Chart height=100 data={cdf |> Types.ContinuousDistribution.toJs} />
</>;
| FloatCdf(r) => | FloatCdf(r) =>
let cdf: Types.distribution = let cdf: Types.ContinuousDistribution.t =
CdfLibrary.Distribution.fromString(r, 2000); CdfLibrary.Distribution.fromString(r, 2000);
<> <>
<Chart <Chart
height=100 height=100
data={cdf |> CdfLibrary.Distribution.toPdf |> Types.toJs} data={
cdf
|> CdfLibrary.Distribution.toPdf
|> Types.ContinuousDistribution.toJs
}
/> />
{r |> ReasonReact.string} {r |> ReasonReact.string}
</>; </>;

View File

@ -1,54 +1,66 @@
type distribution = { module ContinuousDistribution = {
xs: array(float),
ys: array(float),
};
let toJs = (t: distribution) => {
{"xs": t.xs, "ys": t.ys};
};
let toComponentsDist = (d: distribution): ForetoldComponents.Types.Dist.t => {
xs: d.xs,
ys: d.ys,
};
type pdf = distribution;
type cdf = distribution;
let foo = (b: pdf) => 3.9;
let bar: cdf = {xs: [||], ys: [||]};
let cc = foo(bar);
module LimitedDomainCdf = {
type t = { type t = {
distribution, xs: array(float),
domainMaxX: float, ys: array(float),
}; };
let fromCdf = (cdf: cdf, domainMaxX: float, probabilityAtMaxX: float) => { let toJs = (t: t) => {
let distribution: distribution = { {"xs": t.xs, "ys": t.ys};
xs: cdf.xs,
ys: cdf.ys |> E.A.fmap(r => r *. probabilityAtMaxX),
};
{distribution, domainMaxX};
}; };
let toComponentsDist = (d: t): ForetoldComponents.Types.Dist.t => {
xs: d.xs,
ys: d.ys,
};
type pdf = t;
type cdf = t;
};
module DiscreteDistribution = {
type t = {
xs: array(float),
ys: array(float),
};
let fromArray = (xs, ys) => {xs, ys};
let _lastElement = (a: array('a)) => let _lastElement = (a: array('a)) =>
switch (Belt.Array.size(a)) { switch (Belt.Array.size(a)) {
| 0 => None | 0 => None
| n => Belt.Array.get(a, n) | n => Belt.Array.get(a, n)
}; };
let probabilityBeforeDomainMax = (t: t) => _lastElement(t.distribution.ys); let derivative = (p: t) => {
let (xs, ys) =
Belt.Array.zip(p.xs, p.ys)
->Belt.Array.reduce([||], (items, (x, y)) =>
switch (_lastElement(items)) {
| Some((_, yLast)) => [|(x, y -. yLast)|]
| None => [|(x, y)|]
}
)
|> Belt.Array.unzip;
fromArray(xs, ys);
};
let chanceByX = (t: t) => t.distribution; let integral = (p: t) => {
let (xs, ys) =
Belt.Array.zip(p.xs, p.ys)
->Belt.Array.reduce([||], (items, (x, y)) =>
switch (_lastElement(items)) {
| Some((_, yLast)) => [|(x, y +. yLast)|]
| None => [|(x, y)|]
}
)
|> Belt.Array.unzip;
fromArray(xs, ys);
};
};
let domainMaxX = (t: t) => t.domainMaxX; module MixedDistribution = {
// let probabilityDistribution = (t: t) => type distribution = {
// t.distribution |> CdfLibrary.Distribution.toPdf; discrete: DiscreteDistribution.t,
// let probability = (t: t, xPoint: float) => continuous: ContinuousDistribution.t,
// CdfLibrary.Distribution.findY(xPoint, probabilityDistribution(t)); };
// let cumulativeProbability = (t: t, xPoint: float) =>
// CdfLibrary.Distribution.findY(xPoint, t.distribution);
}; };

View File

@ -5,14 +5,16 @@ module JS = {
ys: array(float), ys: array(float),
}; };
let distToJs = (d: Types.distribution) => distJs(~xs=d.xs, ~ys=d.ys); let distToJs = (d: Types.ContinuousDistribution.t) =>
distJs(~xs=d.xs, ~ys=d.ys);
let jsToDist = (d: distJs): Types.distribution => { let jsToDist = (d: distJs): Types.ContinuousDistribution.t => {
xs: xsGet(d), xs: xsGet(d),
ys: ysGet(d), ys: ysGet(d),
}; };
let doAsDist = (f, d: Types.distribution) => d |> distToJs |> f |> jsToDist; let doAsDist = (f, d: Types.ContinuousDistribution.t) =>
d |> distToJs |> f |> jsToDist;
[@bs.module "./CdfLibrary.js"] [@bs.module "./CdfLibrary.js"]
external cdfToPdf: distJs => distJs = "cdfToPdf"; external cdfToPdf: distJs => distJs = "cdfToPdf";