2022-04-01 17:21:24 +00:00
open Jest
2022-04-02 20:25:41 +00:00
let testSkip: (bool, string, unit => assertion) => unit = (skip: bool) =>
if skip {
Skip.test
} else {
test
}
let testEval = (~skip=false, str, result) =>
testSkip(skip)(str, () => Reducer_TestHelpers.expectEvalToBe(str, result))
let testParse = (~skip=false, str, result) =>
testSkip(skip)(str, () => Reducer_TestHelpers.expectParseToBe(str, result))
2022-04-01 17:21:24 +00:00
2022-04-02 21:06:57 +00:00
describe("eval on distribution functions", () => {
describe("normal distribution", () => {
2022-04-02 15:01:53 +00:00
testEval("normal(5,2)", "Ok(Normal(5,2))")
2022-04-02 21:06:57 +00:00
})
describe("lognormal distribution", () => {
testEval("lognormal(5,2)", "Ok(Lognormal(5,2))")
})
describe("unaryMinus", () => {
2022-04-13 04:36:30 +00:00
testEval("mean(-normal(5,2))", "Ok(-5)")
2022-04-23 20:57:06 +00:00
testEval("-normal(5,2)", "Ok(Normal(-5,2))")
2022-04-02 21:06:57 +00:00
})
describe("to", () => {
2022-09-18 14:33:01 +00:00
testEval("5 to 2", "Error(Error: Low value must be less than high value.)")
2022-04-14 20:35:24 +00:00
testEval("to(2,5)", "Ok(Lognormal(1.1512925464970227,0.27853260523016377))")
testEval("to(-2,2)", "Ok(Normal(0,1.2159136638235384))")
2022-04-02 21:06:57 +00:00
})
describe("mean", () => {
2022-04-02 15:01:53 +00:00
testEval("mean(normal(5,2))", "Ok(5)")
testEval("mean(lognormal(1,2))", "Ok(20.085536923187668)")
2022-05-04 15:53:41 +00:00
testEval("mean(gamma(5,5))", "Ok(25)")
2022-05-15 23:42:10 +00:00
testEval("mean(bernoulli(0.2))", "Ok(0.2)")
testEval("mean(bernoulli(0.8))", "Ok(0.8)")
2022-05-16 01:10:13 +00:00
testEval("mean(logistic(5,1))", "Ok(5)")
2022-04-02 21:06:57 +00:00
})
2022-04-08 13:08:49 +00:00
describe("toString", () => {
testEval("toString(normal(5,2))", "Ok('Normal(5,2)')")
})
2022-04-02 21:06:57 +00:00
describe("normalize", () => {
2022-04-02 15:01:53 +00:00
testEval("normalize(normal(5,2))", "Ok(Normal(5,2))")
2022-04-02 21:06:57 +00:00
})
describe("add", () => {
2022-04-02 15:01:53 +00:00
testEval("add(normal(5,2), normal(10,2))", "Ok(Normal(15,2.8284271247461903))")
testEval("add(normal(5,2), lognormal(10,2))", "Ok(Sample Set Distribution)")
2022-04-13 04:36:30 +00:00
testEval("add(normal(5,2), 3)", "Ok(Normal(8,2))")
testEval("add(3, normal(5,2))", "Ok(Normal(8,2))")
testEval("3+normal(5,2)", "Ok(Normal(8,2))")
testEval("normal(5,2)+3", "Ok(Normal(8,2))")
2022-04-02 21:06:57 +00:00
})
2022-04-13 05:11:14 +00:00
describe("subtract", () => {
testEval("10 - normal(5, 1)", "Ok(Normal(5,1))")
testEval("normal(5, 1) - 10", "Ok(Normal(-5,1))")
2022-04-25 19:46:56 +00:00
testEval("mean(1 - toPointSet(normal(5, 2)))", "Ok(-4.002309896304692)")
2022-04-13 05:11:14 +00:00
})
describe("multiply", () => {
testEval("normal(10, 2) * 2", "Ok(Normal(20,4))")
testEval("2 * normal(10, 2)", "Ok(Normal(20,4))")
2022-04-14 20:52:36 +00:00
testEval("lognormal(5,2) * lognormal(10,2)", "Ok(Lognormal(15,2.8284271247461903))")
testEval("lognormal(10, 2) * lognormal(5, 2)", "Ok(Lognormal(15,2.8284271247461903))")
2022-04-13 05:11:14 +00:00
testEval("2 * lognormal(5, 2)", "Ok(Lognormal(5.693147180559945,2))")
testEval("lognormal(5, 2) * 2", "Ok(Lognormal(5.693147180559945,2))")
})
describe("division", () => {
2022-04-15 17:01:49 +00:00
testEval("lognormal(5,2) / lognormal(10,2)", "Ok(Lognormal(-5,2.8284271247461903))")
testEval("lognormal(10,2) / lognormal(5,2)", "Ok(Lognormal(5,2.8284271247461903))")
2022-04-13 05:11:14 +00:00
testEval("lognormal(5, 2) / 2", "Ok(Lognormal(4.306852819440055,2))")
testEval("2 / lognormal(5, 2)", "Ok(Lognormal(-4.306852819440055,2))")
2022-04-22 16:43:18 +00:00
testEval("2 / normal(10, 2)", "Ok(Sample Set Distribution)")
2022-04-13 05:11:14 +00:00
testEval("normal(10, 2) / 2", "Ok(Normal(5,1))")
2022-04-02 21:06:57 +00:00
})
describe("truncate", () => {
2022-04-02 15:01:53 +00:00
testEval("truncateLeft(normal(5,2), 3)", "Ok(Point Set Distribution)")
2022-04-02 20:25:41 +00:00
testEval("truncateRight(normal(5,2), 3)", "Ok(Point Set Distribution)")
testEval("truncate(normal(5,2), 3, 8)", "Ok(Point Set Distribution)")
2022-08-21 00:10:08 +00:00
testEval("truncate(normal(5,2) |> SampleSet.fromDist, 3, 8)", "Ok(Sample Set Distribution)")
2022-06-14 23:54:59 +00:00
testEval("isNormalized(truncate(normal(5,2), 3, 8))", "Ok(true)")
2022-04-02 20:25:41 +00:00
})
describe("exp", () => {
2022-04-22 16:43:18 +00:00
testEval("exp(normal(5,2))", "Ok(Sample Set Distribution)")
2022-04-02 20:25:41 +00:00
})
describe("pow", () => {
2022-04-22 16:43:18 +00:00
testEval("pow(3, uniform(5,8))", "Ok(Sample Set Distribution)")
testEval("pow(uniform(5,8), 3)", "Ok(Sample Set Distribution)")
2022-04-02 20:25:41 +00:00
testEval("pow(uniform(5,8), uniform(9, 10))", "Ok(Sample Set Distribution)")
})
describe("log", () => {
2022-04-22 16:43:18 +00:00
testEval("log(2, uniform(5,8))", "Ok(Sample Set Distribution)")
2022-04-23 22:07:26 +00:00
testEval(
"log(normal(5,2), 3)",
2022-04-28 12:14:07 +00:00
"Error(Distribution Math Error: Logarithm of input error: First input must be completely greater than 0)",
2022-04-23 22:07:26 +00:00
)
2022-04-22 20:27:17 +00:00
testEval(
"log(normal(5,2), normal(10,1))",
2022-04-28 12:14:07 +00:00
"Error(Distribution Math Error: Logarithm of input error: First input must be completely greater than 0)",
2022-04-22 20:27:17 +00:00
)
2022-09-24 15:08:41 +00:00
testEval("log(2, SampleSet.fromDist(0.0001 to 5))", "Ok(Sample Set Distribution)") // log with low values, see https://github.com/quantified-uncertainty/squiggle/issues/1098
2022-04-22 16:43:18 +00:00
testEval("log(uniform(5,8))", "Ok(Sample Set Distribution)")
testEval("log10(uniform(5,8))", "Ok(Sample Set Distribution)")
2022-04-02 20:25:41 +00:00
})
describe("dotAdd", () => {
testEval("dotAdd(normal(5,2), lognormal(10,2))", "Ok(Point Set Distribution)")
testEval("dotAdd(normal(5,2), 3)", "Ok(Point Set Distribution)")
})
describe("equality", () => {
testEval(~skip=true, "normal(5,2) == normal(5,2)", "Ok(true)")
})
describe("mixture", () => {
2022-04-13 02:03:04 +00:00
testEval("mx(normal(5,2), normal(10,1), normal(15, 1))", "Ok(Point Set Distribution)")
testEval("mixture(normal(5,2), normal(10,1), [0.2, 0.4])", "Ok(Point Set Distribution)")
2022-04-02 20:25:41 +00:00
})
})
2022-04-02 21:06:57 +00:00
describe("parse on distribution functions", () => {
describe("power", () => {
2022-09-19 11:00:38 +00:00
testParse("normal(5,2) ^ normal(5,1)", "Ok((pow)((normal)(5, 2), (normal)(5, 1)))")
2022-09-17 22:19:08 +00:00
testParse("3 ^ normal(5,1)", "Ok((pow)(3, (normal)(5, 1)))")
testParse("normal(5,2) ^ 3", "Ok((pow)((normal)(5, 2), 3))")
2022-04-02 21:06:57 +00:00
})
2022-04-13 04:36:30 +00:00
describe("subtraction", () => {
2022-09-17 22:19:08 +00:00
testParse("10 - normal(5,1)", "Ok((subtract)(10, (normal)(5, 1)))")
testParse("normal(5,1) - 10", "Ok((subtract)((normal)(5, 1), 10))")
2022-04-13 04:36:30 +00:00
})
2022-04-02 21:06:57 +00:00
describe("pointwise arithmetic expressions", () => {
testParse(~skip=true, "normal(5,2) .+ normal(5,1)", "Ok((:dotAdd (:normal 5 2) (:normal 5 1)))")
2022-04-12 23:59:40 +00:00
testParse(
~skip=true,
"normal(5,2) .- normal(5,1)",
2022-09-17 22:19:08 +00:00
"Ok((:$$_block_$$ (:dotSubtract (:normal 5 2) (:normal 5 1))))",
2022-05-29 15:00:16 +00:00
// TODO: !!! returns "Ok({(:dotPow (:normal 5 2) (:normal 5 1))})"
2022-04-12 23:59:40 +00:00
)
2022-09-19 11:00:38 +00:00
testParse("normal(5,2) .* normal(5,1)", "Ok((dotMultiply)((normal)(5, 2), (normal)(5, 1)))")
testParse("normal(5,2) ./ normal(5,1)", "Ok((dotDivide)((normal)(5, 2), (normal)(5, 1)))")
testParse("normal(5,2) .^ normal(5,1)", "Ok((dotPow)((normal)(5, 2), (normal)(5, 1)))")
2022-04-02 21:06:57 +00:00
})
describe("equality", () => {
2022-09-17 22:19:08 +00:00
testParse("5 == normal(5,2)", "Ok((equal)(5, (normal)(5, 2)))")
2022-04-02 21:06:57 +00:00
})
describe("pointwise adding two normals", () => {
testParse(~skip=true, "normal(5,2) .+ normal(5,1)", "Ok((:dotAdd (:normal 5 2) (:normal 5 1)))")
})
describe("exponential of one distribution", () => {
testParse(~skip=true, "exp(normal(5,2)", "Ok((:pow (:normal 5 2) 3))")
2022-04-01 17:21:24 +00:00
})
})