diff --git a/README.md b/README.md index d1e33aa..aefa58e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ It may be useful for testing correctness of limited features of the full Squiggle, or for sanity-checking the validity of some Squiggle models. -![](imgs/simple-squiggle.png) +![](imgs/simple-squiggle2.png) ## Built with @@ -94,7 +94,10 @@ Model: ( 2000000000 to 20000000000 ) / ( (1800000 to 2500000) * (0.25 to 0.75) * -> lognormal(9.53, 1.60) => lognormal(9.530291704996749, 1.596443005980748) + ( => ~996.6270585961881 to ~190271.4039258926 ) + ---------------------------------------------------- + ``` For ease of representation, the intermediary outputs are printed only to two decimal points. But this is just a display decision; the innards of the program work with the full set of decimals. diff --git a/imgs/simple-squiggle2.png b/imgs/simple-squiggle2.png new file mode 100644 index 0000000..6abd030 Binary files /dev/null and b/imgs/simple-squiggle2.png differ diff --git a/src/cli.js b/src/cli.js index b8d4a0e..d72a83a 100644 --- a/src/cli.js +++ b/src/cli.js @@ -15,7 +15,10 @@ let runTransformer = (string) => { let result = transformer(string, print); print(""); console.groupEnd(); - console.log(`=> ${result}`); + console.log(`=> ${result[0]}`); + console.log(` ( => ${result[1]} )`); + console.log(""); + print("-".repeat(52)); console.log(""); }; diff --git a/src/index.js b/src/index.js index fe093d6..afeeb52 100644 --- a/src/index.js +++ b/src/index.js @@ -137,7 +137,6 @@ let transformerInner = (string) => { let newMean = mean1 - mean2; let newStd = Math.sqrt(std1 ** 2 + std2 ** 2); return createLogarithmNode(newMean, newStd); - return new math.SymbolNode("xx"); } else if (isLognormalDividedByNumber) { let lognormalFactors = getFactors(node.args[0]); let mean = lognormalFactors[0]; @@ -172,8 +171,9 @@ let transformerInner = (string) => { return transformed; }; +const normal95confidencePoint = 1.6448536269514722; + let from90PercentCI = (low, high) => { - let normal95confidencePoint = 1.6448536269514722; let logLow = Math.log(low); let logHigh = Math.log(high); let mu = (logLow + logHigh) / 2; @@ -181,6 +181,14 @@ let from90PercentCI = (low, high) => { return [mu, sigma]; }; +let to90PercentCI = (mu, sigma) => { + let logHigh = mu + normal95confidencePoint * sigma; + let logLow = mu - normal95confidencePoint * sigma; + let high = Math.exp(logHigh); + let low = Math.exp(logLow); + return [low, high]; +}; + let simplePreprocessor = (string) => { // left for documentation purposes only function replacer(match, p1, p2) { @@ -197,7 +205,7 @@ let simplePreprocessor = (string) => { // simplePreprocessor("1 to 10 + 1 to 20"); -let customToStringHandler = (node, options) => { +let customToStringHandlerTwoDecimals = (node, options) => { if (node.type == "ConstantNode") { return node.value.toFixed(2); } @@ -215,23 +223,38 @@ let preprocessor = (string, print = console.log) => { print( `\t= ${math .parse(newString) - .toString({ handler: customToStringHandler })}` + .toString({ handler: customToStringHandlerTwoDecimals })}` ); return newString; // abc - 12345 - #$*% }; // preprocessor("1.2 to 10.5 * 1.1 to 20 * 1 to 2.5 * 1 to 5"); +let customToStringHandlerLognormals = (node, options) => { + if (isArgLognormal(node)) { + let factors = getFactors(node); + // print(node); + // print(factors); + let ninetyPercentCI = to90PercentCI(factors[0], factors[1]); + return `~${ninetyPercentCI[0]} to ~${ninetyPercentCI[1]}`; + } +}; + export function transformer(string, print = console.log) { string = preprocessor(string, print); let transformerOutput = transformerInner(string); let stringNew = transformerOutput.toString(); while (stringNew != string) { print( - `\t-> ${transformerOutput.toString({ handler: customToStringHandler })}` + `\t-> ${transformerOutput.toString({ + handler: customToStringHandlerTwoDecimals, + })}` ); string = stringNew; transformerOutput = transformerInner(string); stringNew = transformerOutput.toString(); } - return stringNew; + let stringNewAs90PercentCI = transformerOutput.toString({ + handler: customToStringHandlerLognormals, + }); + return [stringNew, stringNewAs90PercentCI]; }