Fix functions not rendering

Fixes #59
This commit is contained in:
Sam Nolan 2022-03-25 15:35:32 +11:00
parent 8aea739fd0
commit c4b6a8d097
4 changed files with 39 additions and 18 deletions

View File

@ -163,7 +163,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = (props) => {
// We are looking at a function. In this case, we draw a Percentiles chart // We are looking at a function. In this case, we draw a Percentiles chart
let start = props.diagramStart ? props.diagramStart : 0; let start = props.diagramStart ? props.diagramStart : 0;
let stop = props.diagramStop ? props.diagramStop : 10; let stop = props.diagramStop ? props.diagramStop : 10;
let count = props.diagramCount ? props.diagramCount : 0.1; let count = props.diagramCount ? props.diagramCount : 100;
let step = (stop - start) / count; let step = (stop - start) / count;
let data = _.range(start, stop, step).map((x) => { let data = _.range(start, stop, step).map((x) => {
if (chartResult.NAME == "Function") { if (chartResult.NAME == "Function") {
@ -192,10 +192,10 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = (props) => {
p99: percentiles[12], p99: percentiles[12],
}; };
} }
return null;
} }
return 0;
}); });
return <SquigglePercentilesChart data={{ facet: data }} />; return <SquigglePercentilesChart data={{ facet: data.filter(x => x !== null) }} />;
} }
}); });
return <>{chartResults}</>; return <>{chartResults}</>;

View File

@ -110,6 +110,7 @@ module Internals = {
inputs : Inputs.inputs, inputs : Inputs.inputs,
env : ASTTypes.environment) => { env : ASTTypes.environment) => {
(input : float) => { (input : float) => {
Js.log2("Environment", inputs);
let foo: Inputs.inputs = {...inputs, environment: env}; let foo: Inputs.inputs = {...inputs, environment: env};
evaluateFunction( evaluateFunction(
foo, foo,

View File

@ -22,7 +22,7 @@ let makeSymbolicFromTwoFloats = (name, fn) =>
~inputTypes=[#Float, #Float], ~inputTypes=[#Float, #Float],
~run=x => ~run=x =>
switch x { switch x {
| [#Float(a), #Float(b)] => Ok(#SymbolicDist(fn(a, b))) | [#Float(a), #Float(b)] => fn(a, b) |> E.R.fmap(r => (#SymbolicDist(r)))
| e => wrongInputsError(e) | e => wrongInputsError(e)
}, },
(), (),
@ -35,7 +35,7 @@ let makeSymbolicFromOneFloat = (name, fn) =>
~inputTypes=[#Float], ~inputTypes=[#Float],
~run=x => ~run=x =>
switch x { switch x {
| [#Float(a)] => Ok(#SymbolicDist(fn(a))) | [#Float(a)] => fn(a) |> E.R.fmap(r => #SymbolicDist(r))
| e => wrongInputsError(e) | e => wrongInputsError(e)
}, },
(), (),

View File

@ -2,7 +2,10 @@ open SymbolicDistTypes
module Normal = { module Normal = {
type t = normal type t = normal
let make = (mean, stdev): symbolicDist => #Normal({mean: mean, stdev: stdev}) let make = (mean: float, stdev: float): result<symbolicDist,string> =>
stdev > 0.0
? Ok(#Normal({mean: mean, stdev: stdev}))
: Error("Standard deviation of normal distribution must be larger than 0")
let pdf = (x, t: t) => Jstat.Normal.pdf(x, t.mean, t.stdev) let pdf = (x, t: t) => Jstat.Normal.pdf(x, t.mean, t.stdev)
let cdf = (x, t: t) => Jstat.Normal.cdf(x, t.mean, t.stdev) let cdf = (x, t: t) => Jstat.Normal.cdf(x, t.mean, t.stdev)
@ -45,10 +48,12 @@ module Normal = {
module Exponential = { module Exponential = {
type t = exponential type t = exponential
let make = (rate: float): symbolicDist => let make = (rate: float): result<symbolicDist,string> =>
#Exponential({ rate > 0.0
rate: rate, ? Ok(#Exponential({
}) rate: rate,
}))
: Error("Exponential distributions mean must be larger than 0")
let pdf = (x, t: t) => Jstat.Exponential.pdf(x, t.rate) let pdf = (x, t: t) => Jstat.Exponential.pdf(x, t.rate)
let cdf = (x, t: t) => Jstat.Exponential.cdf(x, t.rate) let cdf = (x, t: t) => Jstat.Exponential.cdf(x, t.rate)
let inv = (p, t: t) => Jstat.Exponential.inv(p, t.rate) let inv = (p, t: t) => Jstat.Exponential.inv(p, t.rate)
@ -84,7 +89,10 @@ module Triangular = {
module Beta = { module Beta = {
type t = beta type t = beta
let make = (alpha, beta) => #Beta({alpha: alpha, beta: beta}) let make = (alpha, beta) =>
alpha > 0.0 && beta > 0.0
? Ok(#Beta({alpha: alpha, beta: beta}))
: Error("Beta distribution parameters must be positive")
let pdf = (x, t: t) => Jstat.Beta.pdf(x, t.alpha, t.beta) let pdf = (x, t: t) => Jstat.Beta.pdf(x, t.alpha, t.beta)
let cdf = (x, t: t) => Jstat.Beta.cdf(x, t.alpha, t.beta) let cdf = (x, t: t) => Jstat.Beta.cdf(x, t.alpha, t.beta)
let inv = (p, t: t) => Jstat.Beta.inv(p, t.alpha, t.beta) let inv = (p, t: t) => Jstat.Beta.inv(p, t.alpha, t.beta)
@ -95,7 +103,10 @@ module Beta = {
module Lognormal = { module Lognormal = {
type t = lognormal type t = lognormal
let make = (mu, sigma) => #Lognormal({mu: mu, sigma: sigma}) let make = (mu, sigma) =>
sigma > 0.0
? Ok(#Lognormal({mu: mu, sigma: sigma}))
: Error("Lognormal standard deviation must be larger than 0")
let pdf = (x, t: t) => Jstat.Lognormal.pdf(x, t.mu, t.sigma) let pdf = (x, t: t) => Jstat.Lognormal.pdf(x, t.mu, t.sigma)
let cdf = (x, t: t) => Jstat.Lognormal.cdf(x, t.mu, t.sigma) let cdf = (x, t: t) => Jstat.Lognormal.cdf(x, t.mu, t.sigma)
let inv = (p, t: t) => Jstat.Lognormal.inv(p, t.mu, t.sigma) let inv = (p, t: t) => Jstat.Lognormal.inv(p, t.mu, t.sigma)
@ -110,11 +121,16 @@ module Lognormal = {
#Lognormal({mu: mu, sigma: sigma}) #Lognormal({mu: mu, sigma: sigma})
} }
let fromMeanAndStdev = (mean, stdev) => { let fromMeanAndStdev = (mean, stdev) => {
let variance = Js.Math.pow_float(~base=stdev, ~exp=2.0) if stdev > 0.0 {
let meanSquared = Js.Math.pow_float(~base=mean, ~exp=2.0) let variance = Js.Math.pow_float(~base=stdev, ~exp=2.0)
let mu = Js.Math.log(mean) -. 0.5 *. Js.Math.log(variance /. meanSquared +. 1.0) let meanSquared = Js.Math.pow_float(~base=mean, ~exp=2.0)
let sigma = Js.Math.pow_float(~base=Js.Math.log(variance /. meanSquared +. 1.0), ~exp=0.5) let mu = Js.Math.log(mean) -. 0.5 *. Js.Math.log(variance /. meanSquared +. 1.0)
#Lognormal({mu: mu, sigma: sigma}) let sigma = Js.Math.pow_float(~base=Js.Math.log(variance /. meanSquared +. 1.0), ~exp=0.5)
Ok(#Lognormal({mu: mu, sigma: sigma}))
}
else {
Error("Lognormal standard deviation must be larger than 0")
}
} }
let multiply = (l1, l2) => { let multiply = (l1, l2) => {
@ -137,7 +153,11 @@ module Lognormal = {
module Uniform = { module Uniform = {
type t = uniform type t = uniform
let make = (low, high) => #Uniform({low: low, high: high}) let make = (low, high) =>
high > low
? Ok(#Uniform({low: low, high: high}))
: Error("High must be larger than low")
let pdf = (x, t: t) => Jstat.Uniform.pdf(x, t.low, t.high) let pdf = (x, t: t) => Jstat.Uniform.pdf(x, t.low, t.high)
let cdf = (x, t: t) => Jstat.Uniform.cdf(x, t.low, t.high) let cdf = (x, t: t) => Jstat.Uniform.cdf(x, t.low, t.high)
let inv = (p, t: t) => Jstat.Uniform.inv(p, t.low, t.high) let inv = (p, t: t) => Jstat.Uniform.inv(p, t.low, t.high)