Quick hacks to get function rendering to work for now

This commit is contained in:
Ozzie Gooen 2020-08-10 08:05:03 +01:00
parent 86a9c32702
commit 14302695ab
4 changed files with 42 additions and 32 deletions

View File

@ -155,7 +155,12 @@ module DemoDist = {
},
~distPlusIngredients,
~environment=
[||]
[|
("K", `SymbolicDist(`Float(1000.0))),
("M", `SymbolicDist(`Float(1000000.0))),
("B", `SymbolicDist(`Float(1000000000.0))),
("T", `SymbolicDist(`Float(1000000000000.0))),
|]
->Belt.Map.String.fromArray,
(),
);
@ -164,12 +169,18 @@ module DemoDist = {
switch (response1) {
| Ok(`DistPlus(distPlus1)) =>
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} />
| Ok(`Function(f, a)) =>
// Problem: When it gets the function, it doesn't save state about previous commands
let results = E.A.Floats.range(0.0, 10.0, 2)
| Ok(`Function((f, a), env)) =>
// Problem: When it gets the function, it doesn't save state about previous commands
let foo: DistPlusRenderer.Inputs.inputs = {
distPlusIngredients: inputs1.distPlusIngredients,
samplingInputs: inputs1.samplingInputs,
environment: env,
};
let results =
E.A.Floats.range(0.0, 10.0, 10)
|> E.A.fmap(r =>
DistPlusRenderer.runFunction(
inputs1,
foo,
(f, a),
[|`SymbolicDist(`Float(r))|],
)

View File

@ -84,14 +84,10 @@ module MathAdtToDistDst = {
module MathAdtCleaner = {
let transformWithSymbol = (f: float, s: string) =>
switch (s) {
| "K"
| "k" => Some(f *. 1000.)
| "M"
| "m" => Some(f *. 1000000.)
| "B"
| "b" => Some(f *. 1000000000.)
| "T"
| "t" => Some(f *. 1000000000000.)
| "K" => Some(f *. 1000.)
| "M" => Some(f *. 1000000.)
| "B" => Some(f *. 1000000000.)
| "T" => Some(f *. 1000000000000.)
| _ => None
};
let rec run =
@ -126,9 +122,7 @@ module MathAdtToDistDst = {
|> E.R.bind(_, nodeParser);
switch (g("mean"), g("stdev"), g("mu"), g("sigma")) {
| (Ok(mean), Ok(stdev), _, _) =>
Ok(
`FunctionCall(("lognormalFromMeanAndStdDev", [|mean, stdev|])),
)
Ok(`FunctionCall(("lognormalFromMeanAndStdDev", [|mean, stdev|])))
| (_, _, Ok(mu), Ok(sigma)) =>
Ok(`FunctionCall(("lognormal", [|mu, sigma|])))
| _ =>
@ -177,12 +171,13 @@ module MathAdtToDistDst = {
};
};
// Error("Dotwise exponentiation needs two operands")
// Error("Dotwise exponentiation needs two operands")
let operationParser =
(
name: string,
args: result(array(ExpressionTypes.ExpressionTree.node), string),
):result(ExpressionTypes.ExpressionTree.node,string) => {
)
: result(ExpressionTypes.ExpressionTree.node, string) => {
let toOkAlgebraic = r => Ok(`AlgebraicCombination(r));
let toOkPointwise = r => Ok(`PointwiseCombination(r));
let toOkTruncate = r => Ok(`Truncate(r));
@ -192,12 +187,13 @@ module MathAdtToDistDst = {
switch (name, args) {
| ("add", [|l, r|]) => toOkAlgebraic((`Add, l, r))
| ("add", _) => Error("Addition needs two operands")
| ("unaryMinus", [|l|]) => toOkAlgebraic((`Multiply, `SymbolicDist(`Float(-1.0)), l))
| ("unaryMinus", [|l|]) =>
toOkAlgebraic((`Multiply, `SymbolicDist(`Float(-1.0)), l))
| ("subtract", [|l, r|]) => toOkAlgebraic((`Subtract, l, r))
| ("subtract", _) => Error("Subtraction needs two operands")
| ("multiply", [|l, r|]) => toOkAlgebraic((`Multiply, l, r))
| ("multiply", _) => Error("Multiplication needs two operands")
| ("pow", [|l,r|]) => toOkAlgebraic((`Exponentiate, l, r))
| ("pow", [|l, r|]) => toOkAlgebraic((`Exponentiate, l, r))
| ("pow", _) => Error("Exponentiation needs two operands")
| ("dotMultiply", [|l, r|]) => toOkPointwise((`Multiply, l, r))
| ("dotMultiply", _) =>
@ -232,11 +228,13 @@ module MathAdtToDistDst = {
"truncate needs three arguments: the expression and both cutoffs",
)
| ("scaleMultiply", [|d, `SymbolicDist(`Float(v))|]) =>
Ok(`VerticalScaling(`Multiply, d, `SymbolicDist(`Float(v))))
Ok(`VerticalScaling((`Multiply, d, `SymbolicDist(`Float(v)))))
| ("scaleExp", [|d, `SymbolicDist(`Float(v))|]) =>
Ok(`VerticalScaling(`Exponentiate, d, `SymbolicDist(`Float(v))))
Ok(
`VerticalScaling((`Exponentiate, d, `SymbolicDist(`Float(v)))),
)
| ("scaleLog", [|d, `SymbolicDist(`Float(v))|]) =>
Ok(`VerticalScaling(`Log, d, `SymbolicDist(`Float(v))))
Ok(`VerticalScaling((`Log, d, `SymbolicDist(`Float(v)))))
| ("pdf", [|d, `SymbolicDist(`Float(v))|]) =>
toOkFloatFromDist((`Pdf(v), d))
| ("cdf", [|d, `SymbolicDist(`Float(v))|]) =>
@ -317,7 +315,7 @@ module MathAdtToDistDst = {
| Symbol(sym) => Ok(`Symbol(sym))
| Fn({name, args}) => functionParser(nodeParser, name, args)
| _ => {
Error("This type not currently supported")
Error("This type not currently supported");
};
// | FunctionAssignment({name, args, expression}) => {

View File

@ -112,7 +112,7 @@ module Internals = {
ins := addVariable(ins^, name, node);
None;
}
| `Expression(node) => Some(runNode(ins^, node) |> E.R.fmap(r => (ins, r))),
| `Expression(node) => Some(runNode(ins^, node) |> E.R.fmap(r => (ins^.environment, r))),
)
|> E.A.O.concatSomes
|> E.A.R.firstErrorOrOpen;
@ -175,7 +175,7 @@ let run = (inputs: Inputs.inputs) => {
|> E.R.fmap(Internals.outputToDistPlus(inputs));
};
let exportDistPlus = (inputs, node: ExpressionTypes.ExpressionTree.node) =>
let exportDistPlus = (inputs, env:ProbExample.ExpressionTypes.ExpressionTree.environment, node: ExpressionTypes.ExpressionTree.node) =>
node
|> renderIfNeeded(inputs)
|> E.R.bind(
@ -183,7 +183,7 @@ let exportDistPlus = (inputs, node: ExpressionTypes.ExpressionTree.node) =>
fun
| `RenderedDist(n) =>
Ok(`DistPlus(Internals.outputToDistPlus(inputs, n)))
| `Function(n) => Ok(`Function(n))
| `Function(n) => Ok(`Function(n, env))
| n =>
Error(
"Didn't output a rendered distribution. Format:"
@ -195,7 +195,7 @@ let run2 = (inputs: Inputs.inputs) => {
inputs
|> Internals.distPlusRenderInputsToInputs
|> Internals.inputsToLeaf
|> E.R.bind(_,((a,b)) => exportDistPlus(inputs,b))
|> E.R.bind(_,((a,b)) => exportDistPlus(inputs,a,b))
};
let runFunction =
@ -212,5 +212,5 @@ let runFunction =
fnInputs,
fn,
);
output |> E.R.bind(_, exportDistPlus(ins));
output |> E.R.bind(_, exportDistPlus(ins, inputs.environment));
};

View File

@ -1,5 +1,4 @@
// Not yet used
type inputs = {
samplingInputs: ExpressionTypes.ExpressionTree.SamplingInputs.t,
program: string,
@ -16,9 +15,11 @@ let addVariable =
),
};
let runNode = (inputs, node) => {
let runNode = (inputs: inputs, node) => {
ExpressionTree.toLeaf(
ExpressionTypes.ExpressionTree.SamplingInputs.withDefaults(inputs.samplingInputs),
ExpressionTypes.ExpressionTree.SamplingInputs.withDefaults(
inputs.samplingInputs,
),
inputs.environment,
node,
);