Beginning to add domain functionality

This commit is contained in:
Ozzie Gooen 2020-02-23 12:40:18 +00:00
parent 0328b86833
commit d947d43b56
7 changed files with 64 additions and 66 deletions

View File

@ -3,7 +3,6 @@ let timeDist =
GenericDistribution.make( GenericDistribution.make(
~generationSource= ~generationSource=
GuesstimatorString("mm(floor(normal(30,2)), normal(39,1), [.5,.5])"), GuesstimatorString("mm(floor(normal(30,2)), normal(39,1), [.5,.5])"),
~probabilityType=Pdf,
~domain=Complete, ~domain=Complete,
~unit=TimeDistribution({zero: MomentRe.momentNow(), unit: `days}), ~unit=TimeDistribution({zero: MomentRe.momentNow(), unit: `days}),
(), (),

View File

@ -23,11 +23,6 @@ module ComplexPowerChart = {
}; };
}; };
let bar: DistributionTypes.xyShape = {
ys: [|0.5, 0.8, 0.4, 1.0, 2.0|],
xs: [|0.0, 1., 2., 5., 8.|],
};
module IntegralChart = { module IntegralChart = {
[@react.component] [@react.component]
let make = (~complexPower: DistributionTypes.complexPower, ~onHover) => { let make = (~complexPower: DistributionTypes.complexPower, ~onHover) => {

View File

@ -109,6 +109,7 @@ module Continuous = {
let pointwiseFmap = (fn, t: t) => let pointwiseFmap = (fn, t: t) =>
t |> xyShape |> XYShape.pointwiseMap(fn) |> fromShape; t |> xyShape |> XYShape.pointwiseMap(fn) |> fromShape;
let toShape = (t: t): DistributionTypes.shape => Continuous(t); let toShape = (t: t): DistributionTypes.shape => Continuous(t);
// TODO: When Roman's PR comes in, fix this bit.
let xToY = (f, t) => let xToY = (f, t) =>
shapeFn(CdfLibrary.Distribution.findY(f), t) shapeFn(CdfLibrary.Distribution.findY(f), t)
|> DistributionTypes.MixedPoint.makeContinuous; |> DistributionTypes.MixedPoint.makeContinuous;
@ -181,11 +182,11 @@ module Mixed = {
}; };
}; };
let scaleDiscrete = let scaleDiscreteFn =
({discreteProbabilityMassFraction}: DistributionTypes.mixedShape, f) => ({discreteProbabilityMassFraction}: DistributionTypes.mixedShape, f) =>
f *. discreteProbabilityMassFraction; f *. discreteProbabilityMassFraction;
let scaleContinuous = let scaleContinuousFn =
({discreteProbabilityMassFraction}: DistributionTypes.mixedShape, f) => ({discreteProbabilityMassFraction}: DistributionTypes.mixedShape, f) =>
f *. (1.0 -. discreteProbabilityMassFraction); f *. (1.0 -. discreteProbabilityMassFraction);
@ -200,19 +201,15 @@ module Mixed = {
let toShape = (t: t): DistributionTypes.shape => Mixed(t); let toShape = (t: t): DistributionTypes.shape => Mixed(t);
let toContinuous = ({continuous}: t) => Some(continuous); let toContinuous = ({continuous}: t) => Some(continuous);
let toDiscrete = ({discrete}: t) => Some(discrete); let toDiscrete = ({discrete}: t) => Some(discrete);
let xToY = let xToY = (f, {discrete, continuous} as t: t) => {
(
f,
{discrete, continuous, discreteProbabilityMassFraction} as t: t,
) => {
let c = let c =
continuous continuous
|> Continuous.T.xToY(f) |> Continuous.T.xToY(f)
|> DistributionTypes.MixedPoint.fmap(scaleContinuous(t)); |> DistributionTypes.MixedPoint.fmap(scaleContinuousFn(t));
let d = let d =
discrete discrete
|> Discrete.T.xToY(f) |> Discrete.T.xToY(f)
|> DistributionTypes.MixedPoint.fmap(scaleDiscrete(t)); |> DistributionTypes.MixedPoint.fmap(scaleDiscreteFn(t));
DistributionTypes.MixedPoint.add(c, d); DistributionTypes.MixedPoint.add(c, d);
}; };
@ -254,33 +251,23 @@ module Mixed = {
); );
}; };
let integralSum = // todo: Get last element of actual sum.
( let integralSum = (~cache, {discrete, continuous} as t: t) => {
~cache,
{discrete, continuous, discreteProbabilityMassFraction}: t,
) => {
switch (cache) { switch (cache) {
| Some(cache) => 3.0 | Some(cache) => 3.0
| None => | None =>
Discrete.T.Integral.sum(~cache=None, discrete) scaleDiscreteFn(t, Discrete.T.Integral.sum(~cache=None, discrete))
*. discreteProbabilityMassFraction +. scaleContinuousFn(
+. Continuous.T.Integral.sum(~cache=None, continuous) t,
*. (1.0 -. discreteProbabilityMassFraction) Continuous.T.Integral.sum(~cache=None, continuous),
)
}; };
}; };
let integralXtoY = let integralXtoY = (~cache, f, {discrete, continuous} as t: t) => {
(
~cache,
f,
{discrete, continuous, discreteProbabilityMassFraction}: t,
) => {
let cont = Continuous.T.Integral.xToY(~cache, f, continuous); let cont = Continuous.T.Integral.xToY(~cache, f, continuous);
let discrete = Discrete.T.Integral.xToY(~cache, f, discrete); let discrete = Discrete.T.Integral.xToY(~cache, f, discrete);
discrete scaleDiscreteFn(t, discrete) +. scaleContinuousFn(t, cont);
*. discreteProbabilityMassFraction
+. cont
*. (1.0 -. discreteProbabilityMassFraction);
}; };
let pointwiseFmap = let pointwiseFmap =
@ -440,20 +427,57 @@ module ComplexPower = {
let toContinuous = shapeFn(Shape.T.toContinuous); let toContinuous = shapeFn(Shape.T.toContinuous);
let toDiscrete = shapeFn(Shape.T.toDiscrete); let toDiscrete = shapeFn(Shape.T.toDiscrete);
// todo: Adjust for total mass. // todo: Adjust for total mass.
let toScaledContinuous = shapeFn(Shape.T.toContinuous);
let toScaledDiscrete = shapeFn(Shape.T.toScaledDiscrete); let domainIncludedProbabilityMass = (t: t) =>
Domain.includedProbabilityMass(t.domain);
let domainIncludedProbabilityMassAdjustment = (t: t, f) =>
f *. Domain.includedProbabilityMass(t.domain);
let toScaledContinuous = (t: t) => {
t
|> toShape
|> Shape.T.toScaledContinuous
|> E.O.fmap(
Continuous.T.pointwiseFmap(r =>
r *. domainIncludedProbabilityMass(t)
),
);
};
let toScaledDiscrete = (t: t) => {
t
|> toShape
|> Shape.T.toScaledDiscrete
|> E.O.fmap(
Discrete.T.pointwiseFmap(
domainIncludedProbabilityMassAdjustment(t),
),
);
};
// todo: adjust for limit, and the fact that total mass is lower. // todo: adjust for limit, and the fact that total mass is lower.
let xToY = f => shapeFn(Shape.T.xToY(f)); let xToY = (f, t: t) =>
t
|> toShape
|> Shape.T.xToY(f)
|> MixedPoint.fmap(domainIncludedProbabilityMassAdjustment(t));
let minX = shapeFn(Shape.T.minX); let minX = shapeFn(Shape.T.minX);
let maxX = shapeFn(Shape.T.maxX); let maxX = shapeFn(Shape.T.maxX);
let fromShape = (shape, t): t => update(~shape, t); let fromShape = (t, shape): t => update(~shape, t);
// todo: adjust for limit
// todo: adjust for limit, maybe?
let pointwiseFmap = (fn, {shape, _} as t: t): t => let pointwiseFmap = (fn, {shape, _} as t: t): t =>
fromShape(Shape.T.pointwiseFmap(fn, shape), t); Shape.T.pointwiseFmap(fn, shape) |> fromShape(t);
// This bit is kind of akward, could probably use rethinking.
let integral = (~cache as _, t: t) => let integral = (~cache as _, t: t) =>
fromShape(Continuous(t.integralCache), t); fromShape(t, Continuous(t.integralCache));
let integralSum = (~cache as _, t: t) => let integralSum = (~cache as _, t: t) =>
Shape.T.Integral.sum(~cache=Some(t.integralCache), toShape(t)); Shape.T.Integral.sum(~cache=Some(t.integralCache), toShape(t));
// TODO: Fix this below, obviously. Adjust for limit. // TODO: Fix this below, obviously. Adjust for limit.
let integralXtoY = (~cache as _, f, t: t) => { let integralXtoY = (~cache as _, f, t: t) => {
Shape.T.Integral.xToY(~cache=Some(t.integralCache), f, toShape(t)); Shape.T.Integral.xToY(~cache=Some(t.integralCache), f, toShape(t));

View File

@ -14,16 +14,9 @@ type xyShape = {
ys: array(float), ys: array(float),
}; };
let foo = {xs: [|1., 2., 5.|], ys: [|1., 2., 3.|]};
let answer = {xs: [|1., 2., 2., 5., 5.|], ys: [|1., 1., 2., 2., 3.|]};
let toStepwise = (xyShape: xyShape) => {};
type interpolationMethod = [ | `Stepwise | `Linear];
type continuousShape = { type continuousShape = {
xyShape, xyShape,
interpolation: interpolationMethod, interpolation: [ | `Stepwise | `Linear],
}; };
type discreteShape = xyShape; type discreteShape = xyShape;
@ -59,14 +52,8 @@ type distributionUnit =
| UnspecifiedDistribution | UnspecifiedDistribution
| TimeDistribution(TimeTypes.timeVector); | TimeDistribution(TimeTypes.timeVector);
type probabilityType =
| Cdf
| Pdf
| Arbitrary;
type genericDistribution = { type genericDistribution = {
generationSource, generationSource,
probabilityType,
domain, domain,
unit: distributionUnit, unit: distributionUnit,
}; };
@ -104,6 +91,9 @@ module Domain = {
}; };
}; };
let includedProbabilityMass = (t: domain) =>
1.0 -. excludedProbabilityMass(t);
let initialProbabilityMass = (t: domain) => { let initialProbabilityMass = (t: domain) => {
switch (t) { switch (t) {
| Complete | Complete

View File

@ -1,16 +1,9 @@
open DistributionTypes; open DistributionTypes;
let make = let make =
( (~generationSource, ~domain=Complete, ~unit=UnspecifiedDistribution, ())
~generationSource,
~probabilityType=Pdf,
~domain=Complete,
~unit=UnspecifiedDistribution,
(),
)
: genericDistribution => { : genericDistribution => {
generationSource, generationSource,
probabilityType,
domain, domain,
unit, unit,
}; };

View File

@ -158,7 +158,6 @@ module Model = {
let genericDistribution = let genericDistribution =
GenericDistribution.make( GenericDistribution.make(
~generationSource=GuesstimatorString(str), ~generationSource=GuesstimatorString(str),
~probabilityType=Cdf,
~domain=Complete, ~domain=Complete,
~unit=UnspecifiedDistribution, ~unit=UnspecifiedDistribution,
(), (),
@ -175,7 +174,6 @@ module Model = {
GuesstimatorDist.logNormal(40., 4.), GuesstimatorDist.logNormal(40., 4.),
), ),
), ),
~probabilityType=Cdf,
~domain=RightLimited({xPoint: 100., excludingProbabilityMass: 0.3}), ~domain=RightLimited({xPoint: 100., excludingProbabilityMass: 0.3}),
~unit=TimeDistribution({zero: currentDateTime, unit: `years}), ~unit=TimeDistribution({zero: currentDateTime, unit: `years}),
(), (),

View File

@ -5,7 +5,6 @@ module Model = {
let genericDistribution = let genericDistribution =
GenericDistribution.make( GenericDistribution.make(
~generationSource=GuesstimatorString(guesstimatorString), ~generationSource=GuesstimatorString(guesstimatorString),
~probabilityType=Cdf,
~unit=TimeDistribution({zero: currentDateTime, unit: `years}), ~unit=TimeDistribution({zero: currentDateTime, unit: `years}),
(), (),
); );