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, ~distPlusIngredients,
~environment= ~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, ->Belt.Map.String.fromArray,
(), (),
); );
@ -164,12 +169,18 @@ module DemoDist = {
switch (response1) { switch (response1) {
| Ok(`DistPlus(distPlus1)) => | Ok(`DistPlus(distPlus1)) =>
<DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} /> <DistPlusPlot distPlus={DistPlus.T.normalize(distPlus1)} />
| Ok(`Function(f, a)) => | Ok(`Function((f, a), env)) =>
// Problem: When it gets the function, it doesn't save state about previous commands // 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) 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 => |> E.A.fmap(r =>
DistPlusRenderer.runFunction( DistPlusRenderer.runFunction(
inputs1, foo,
(f, a), (f, a),
[|`SymbolicDist(`Float(r))|], [|`SymbolicDist(`Float(r))|],
) )

View File

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

View File

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

View File

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