Moved out distToFloatOps to better places

This commit is contained in:
Ozzie Gooen 2020-07-02 12:25:04 +01:00
parent 101824e500
commit 4663700f67
4 changed files with 23 additions and 28 deletions

View File

@ -333,12 +333,7 @@ module Continuous = {
}; };
let combineAlgebraically = let combineAlgebraically =
( (~downsample=false, op: SymbolicTypes.algebraicOperation, t1: t, t2: t) => {
~downsample=false,
op: SymbolicTypes.algebraicOperation,
t1: t,
t2: t,
) => {
let s1 = t1 |> getShape; let s1 = t1 |> getShape;
let s2 = t2 |> getShape; let s2 = t2 |> getShape;
let t1n = s1 |> XYShape.T.length; let t1n = s1 |> XYShape.T.length;
@ -845,12 +840,7 @@ module Mixed = {
}); });
let combineAlgebraically = let combineAlgebraically =
( (~downsample=false, op: SymbolicTypes.algebraicOperation, t1: t, t2: t)
~downsample=false,
op: SymbolicTypes.algebraicOperation,
t1: t,
t2: t,
)
: t => { : t => {
// Discrete convolution can cause a huge increase in the number of samples, // Discrete convolution can cause a huge increase in the number of samples,
// so we'll first downsample. // so we'll first downsample.
@ -1105,6 +1095,14 @@ module Shape = {
| Continuous(m) => Continuous.T.variance(m) | Continuous(m) => Continuous.T.variance(m)
}; };
}); });
let operate = (distToFloatOp: SymbolicTypes.distToFloatOperation, s) =>
switch (distToFloatOp) {
| `Pdf(f) => pdf(f, s)
| `Inv(f) => inv(f, s)
| `Sample => sample(s)
| `Mean => T.mean(s)
};
}; };
module DistPlus = { module DistPlus = {

View File

@ -246,6 +246,14 @@ module T = {
| `Uniform(n) => Uniform.mean(n) | `Uniform(n) => Uniform.mean(n)
| `Float(n) => Float.mean(n); | `Float(n) => Float.mean(n);
let operate = (distToFloatOp: distToFloatOperation, s) =>
switch (distToFloatOp) {
| `Pdf(f) => Ok(pdf(f, s))
| `Inv(f) => Ok(inv(f, s))
| `Sample => Ok(sample(s))
| `Mean => mean(s)
};
let interpolateXs = let interpolateXs =
(~xSelection: [ | `Linear | `ByWeight]=`Linear, dist: symbolicDist, n) => { (~xSelection: [ | `Linear | `ByWeight]=`Linear, dist: symbolicDist, n) => {
switch (xSelection, dist) { switch (xSelection, dist) {

View File

@ -48,6 +48,7 @@ type symbolicDist = [
| `Float(float) // Dirac delta at x. Practically useful only in the context of multimodals. | `Float(float) // Dirac delta at x. Practically useful only in the context of multimodals.
]; ];
// todo: These operations are really applicable for all dists
type algebraicOperation = [ | `Add | `Multiply | `Subtract | `Divide]; type algebraicOperation = [ | `Add | `Multiply | `Subtract | `Divide];
type pointwiseOperation = [ | `Add | `Multiply]; type pointwiseOperation = [ | `Add | `Multiply];
type scaleOperation = [ | `Multiply | `Exponentiate | `Log]; type scaleOperation = [ | `Multiply | `Exponentiate | `Log];

View File

@ -266,26 +266,14 @@ module TreeNode = {
module FloatFromDist = { module FloatFromDist = {
let evaluateFromSymbolic = (distToFloatOp: distToFloatOperation, s) => { let evaluateFromSymbolic = (distToFloatOp: distToFloatOperation, s) => {
let value = SymbolicDist.T.operate(distToFloatOp, s)
switch (distToFloatOp) { |> E.R.bind(_, v => Ok(`Leaf(`SymbolicDist(`Float(v)))));
| `Pdf(f) => Ok(SymbolicDist.T.pdf(f, s))
| `Inv(f) => Ok(SymbolicDist.T.inv(f, s))
| `Sample => Ok(SymbolicDist.T.sample(s))
| `Mean => SymbolicDist.T.mean(s)
};
E.R.bind(value, v => Ok(`Leaf(`SymbolicDist(`Float(v)))));
}; };
let evaluateFromRenderedDist = let evaluateFromRenderedDist =
(distToFloatOp: distToFloatOperation, rs: DistTypes.shape) (distToFloatOp: distToFloatOperation, rs: DistTypes.shape)
: result(treeNode, string) => { : result(treeNode, string) => {
let value = Distributions.Shape.operate(distToFloatOp, rs)
switch (distToFloatOp) { |> (v => Ok(`Leaf(`SymbolicDist(`Float(v)))));
| `Pdf(f) => Ok(Distributions.Shape.pdf(f, rs))
| `Inv(f) => Ok(Distributions.Shape.inv(f, rs)) // TODO: this is tricky for discrete distributions, because they have a stepwise CDF
| `Sample => Ok(Distributions.Shape.sample(rs))
| `Mean => Ok(Distributions.Shape.T.mean(rs))
};
E.R.bind(value, v => Ok(`Leaf(`SymbolicDist(`Float(v)))));
}; };
let rec evaluateToLeaf = let rec evaluateToLeaf =
( (