Ran yarn format

This commit is contained in:
Quinn Dougherty 2022-04-13 01:02:53 -04:00
parent 8287f51aae
commit de81928ea9
8 changed files with 512 additions and 457 deletions

View File

@ -25,7 +25,7 @@ let {
algebraicDivide, algebraicDivide,
algebraicSubtract, algebraicSubtract,
algebraicLogarithm, algebraicLogarithm,
algebraicPower algebraicPower,
} = module(DistributionOperation.Constructors) } = module(DistributionOperation.Constructors)
let algebraicAdd = algebraicAdd(~env) let algebraicAdd = algebraicAdd(~env)
@ -36,282 +36,329 @@ let algebraicLogarithm = algebraicLogarithm(~env)
let algebraicPower = algebraicPower(~env) let algebraicPower = algebraicPower(~env)
describe("(Algebraic) addition of distributions", () => { describe("(Algebraic) addition of distributions", () => {
describe("mean", () => { describe("mean", () => {
test("normal(mean=5) + normal(mean=20)", () => { test("normal(mean=5) + normal(mean=20)", () => {
normalDist5 normalDist5
-> algebraicAdd(normalDist20) ->algebraicAdd(normalDist20)
-> E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean) ->E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean)
-> E.R2.fmap(run) ->E.R2.fmap(run)
-> E.R2.fmap(toFloat) ->E.R2.fmap(toFloat)
-> E.R.toExn ->E.R.toExn
-> expect ->expect
-> toBe(Some(2.5e1)) ->toBe(Some(2.5e1))
}) })
test("uniform(low=9, high=10) + beta(alpha=2, beta=5)", () => { test("uniform(low=9, high=10) + beta(alpha=2, beta=5)", () => {
// let uniformMean = (9.0 +. 10.0) /. 2.0 // let uniformMean = (9.0 +. 10.0) /. 2.0
// let betaMean = 1.0 /. (1.0 +. 5.0 /. 2.0) // let betaMean = 1.0 /. (1.0 +. 5.0 /. 2.0)
let received = uniformDist let received =
-> algebraicAdd(betaDist) uniformDist
-> E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean) ->algebraicAdd(betaDist)
-> E.R2.fmap(run) ->E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean)
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=2. // sometimes it works with ~digits=2.
| Some(x) => x -> expect -> toBeSoCloseTo(0.01927225696028752, ~digits=1) // (uniformMean +. betaMean) | Some(x) => x->expect->toBeSoCloseTo(0.01927225696028752, ~digits=1) // (uniformMean +. betaMean)
} }
}) })
test("beta(alpha=2, beta=5) + uniform(low=9, high=10)", () => { test("beta(alpha=2, beta=5) + uniform(low=9, high=10)", () => {
// let uniformMean = (9.0 +. 10.0) /. 2.0 // let uniformMean = (9.0 +. 10.0) /. 2.0
// let betaMean = 1.0 /. (1.0 +. 5.0 /. 2.0) // let betaMean = 1.0 /. (1.0 +. 5.0 /. 2.0)
let received = betaDist let received =
-> algebraicAdd(uniformDist) betaDist
-> E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean) ->algebraicAdd(uniformDist)
-> E.R2.fmap(run) ->E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean)
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=2. // sometimes it works with ~digits=2.
| Some(x) => x -> expect -> toBeSoCloseTo(0.019275414920485248, ~digits=1) // (uniformMean +. betaMean) | Some(x) => x->expect->toBeSoCloseTo(0.019275414920485248, ~digits=1) // (uniformMean +. betaMean)
} }
}) })
}) })
describe("pdf", () => { describe("pdf", () => {
// TEST IS WRONG. SEE STDEV ADDITION EXPRESSION. // TEST IS WRONG. SEE STDEV ADDITION EXPRESSION.
testAll("(normal(mean=5) + normal(mean=5)).pdf (imprecise)", list{8e0, 1e1, 1.2e1, 1.4e1}, x => { testAll(
let received = normalDist10 // this should be normal(10, sqrt(8)) "(normal(mean=5) + normal(mean=5)).pdf (imprecise)",
-> Ok list{8e0, 1e1, 1.2e1, 1.4e1},
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, x)) x => {
-> E.R2.fmap(run) let received =
-> E.R2.fmap(toFloat) normalDist10 // this should be normal(10, sqrt(8))
-> E.R.toOption ->Ok
-> E.O.flatten ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, x))
let calculated = normalDist5 ->E.R2.fmap(run)
-> algebraicAdd(normalDist5) ->E.R2.fmap(toFloat)
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, x)) ->E.R.toOption
-> E.R2.fmap(run) ->E.O.flatten
-> E.R2.fmap(toFloat) let calculated =
-> E.R.toOption normalDist5
-> E.O.flatten ->algebraicAdd(normalDist5)
->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received { switch received {
| None => "this branch occurs when the dispatch to Jstat on trusted input fails." -> expect -> toBe("never") | None =>
| Some(x) => switch calculated { "this branch occurs when the dispatch to Jstat on trusted input fails."
| None => "algebraicAdd has" -> expect -> toBe("failed") ->expect
| Some(y) => x -> expect -> toBeSoCloseTo(y, ~digits=0) ->toBe("never")
| Some(x) =>
switch calculated {
| None => "algebraicAdd has"->expect->toBe("failed")
| Some(y) => x->expect->toBeSoCloseTo(y, ~digits=0)
} }
} }
}) },
)
test("(normal(mean=10) + normal(mean=10)).pdf(1.9e1)", () => { test("(normal(mean=10) + normal(mean=10)).pdf(1.9e1)", () => {
let received = normalDist20 let received =
-> Ok normalDist20
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1.9e1)) ->Ok
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1.9e1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toOption ->E.R2.fmap(toFloat)
-> E.O.flatten ->E.R.toOption
let calculated = normalDist10 ->E.O.flatten
-> algebraicAdd(normalDist10) let calculated =
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1.9e1)) normalDist10
-> E.R2.fmap(run) ->algebraicAdd(normalDist10)
-> E.R2.fmap(toFloat) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1.9e1))
-> E.R.toOption ->E.R2.fmap(run)
-> E.O.flatten ->E.R2.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received { switch received {
| None => "this branch occurs when the dispatch to Jstat on trusted input fails." -> expect -> toBe("never") | None =>
| Some(x) => switch calculated { "this branch occurs when the dispatch to Jstat on trusted input fails."
| None => "algebraicAdd has" -> expect -> toBe("failed") ->expect
| Some(y) => x -> expect -> toBeSoCloseTo(y, ~digits=1) ->toBe("never")
| Some(x) =>
switch calculated {
| None => "algebraicAdd has"->expect->toBe("failed")
| Some(y) => x->expect->toBeSoCloseTo(y, ~digits=1)
} }
} }
}) })
test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).pdf(10)", () => { test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).pdf(10)", () => {
let received = uniformDist let received =
-> algebraicAdd(betaDist) uniformDist
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1e1)) ->algebraicAdd(betaDist)
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1e1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=4. // sometimes it works with ~digits=4.
| Some(x) => x -> expect -> toBeSoCloseTo(0.001978994877226945, ~digits=3) | Some(x) => x->expect->toBeSoCloseTo(0.001978994877226945, ~digits=3)
} }
}) })
test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).pdf(10)", () => { test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).pdf(10)", () => {
let received = betaDist let received =
-> algebraicAdd(uniformDist) betaDist
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1e1)) ->algebraicAdd(uniformDist)
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.pdf(d, 1e1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=4. // sometimes it works with ~digits=4.
| Some(x) => x -> expect -> toBeSoCloseTo(0.001978994877226945, ~digits=3) | Some(x) => x->expect->toBeSoCloseTo(0.001978994877226945, ~digits=3)
} }
}) })
}) })
describe("cdf", () => { describe("cdf", () => {
testAll("(normal(mean=5) + normal(mean=5)).cdf (imprecise)", list{6e0, 8e0, 1e1, 1.2e1}, x => { testAll("(normal(mean=5) + normal(mean=5)).cdf (imprecise)", list{6e0, 8e0, 1e1, 1.2e1}, x => {
let received = normalDist10 let received =
-> Ok normalDist10
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, x)) ->Ok
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, x))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toOption ->E.R2.fmap(toFloat)
-> E.O.flatten ->E.R.toOption
let calculated = normalDist5 ->E.O.flatten
-> algebraicAdd(normalDist5) let calculated =
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, x)) normalDist5
-> E.R2.fmap(run) ->algebraicAdd(normalDist5)
-> E.R2.fmap(toFloat) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, x))
-> E.R.toOption ->E.R2.fmap(run)
-> E.O.flatten ->E.R2.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received { switch received {
| None => "this branch occurs when the dispatch to Jstat on trusted input fails." -> expect -> toBe("never") | None =>
| Some(x) => switch calculated { "this branch occurs when the dispatch to Jstat on trusted input fails."
| None => "algebraicAdd has" -> expect -> toBe("failed") ->expect
| Some(y) => x -> expect -> toBeSoCloseTo(y, ~digits=0) ->toBe("never")
| Some(x) =>
switch calculated {
| None => "algebraicAdd has"->expect->toBe("failed")
| Some(y) => x->expect->toBeSoCloseTo(y, ~digits=0)
} }
} }
}) })
test("(normal(mean=10) + normal(mean=10)).cdf(1.25e1)", () => { test("(normal(mean=10) + normal(mean=10)).cdf(1.25e1)", () => {
let received = normalDist20 let received =
-> Ok normalDist20
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1.25e1)) ->Ok
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1.25e1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toOption ->E.R2.fmap(toFloat)
-> E.O.flatten ->E.R.toOption
let calculated = normalDist10 ->E.O.flatten
-> algebraicAdd(normalDist10) let calculated =
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1.25e1)) normalDist10
-> E.R2.fmap(run) ->algebraicAdd(normalDist10)
-> E.R2.fmap(toFloat) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1.25e1))
-> E.R.toOption ->E.R2.fmap(run)
-> E.O.flatten ->E.R2.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received { switch received {
| None => "this branch occurs when the dispatch to Jstat on trusted input fails." -> expect -> toBe("never") | None =>
| Some(x) => switch calculated { "this branch occurs when the dispatch to Jstat on trusted input fails."
| None => "algebraicAdd has" -> expect -> toBe("failed") ->expect
| Some(y) => x -> expect -> toBeSoCloseTo(y, ~digits=2) ->toBe("never")
| Some(x) =>
switch calculated {
| None => "algebraicAdd has"->expect->toBe("failed")
| Some(y) => x->expect->toBeSoCloseTo(y, ~digits=2)
} }
} }
}) })
test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).cdf(10)", () => { test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).cdf(10)", () => {
let received = uniformDist let received =
-> algebraicAdd(betaDist) uniformDist
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1e1)) ->algebraicAdd(betaDist)
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1e1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=4. // sometimes it works with ~digits=4.
| Some(x) => x -> expect -> toBeSoCloseTo(0.0013961779932477507, ~digits=4) | Some(x) => x->expect->toBeSoCloseTo(0.0013961779932477507, ~digits=4)
} }
}) })
test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).cdf(10)", () => { test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).cdf(10)", () => {
let received = betaDist let received =
-> algebraicAdd(uniformDist) betaDist
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1e1)) ->algebraicAdd(uniformDist)
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.cdf(d, 1e1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=4. // sometimes it works with ~digits=4.
| Some(x) => x -> expect -> toBeSoCloseTo(0.001388898111625753, ~digits=3) | Some(x) => x->expect->toBeSoCloseTo(0.001388898111625753, ~digits=3)
} }
}) })
}) })
describe("inv", () => { describe("inv", () => {
testAll("(normal(mean=5) + normal(mean=5)).inv (imprecise)", list{5e-2, 4.2e-3, 9e-3}, x => { testAll("(normal(mean=5) + normal(mean=5)).inv (imprecise)", list{5e-2, 4.2e-3, 9e-3}, x => {
let received = normalDist10 let received =
-> Ok normalDist10
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, x)) ->Ok
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, x))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toOption ->E.R2.fmap(toFloat)
-> E.O.flatten ->E.R.toOption
let calculated = normalDist5 ->E.O.flatten
-> algebraicAdd(normalDist5) let calculated =
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, x)) normalDist5
-> E.R2.fmap(run) ->algebraicAdd(normalDist5)
-> E.R2.fmap(toFloat) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, x))
-> E.R.toOption ->E.R2.fmap(run)
-> E.O.flatten ->E.R2.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received { switch received {
| None => "this branch occurs when the dispatch to Jstat on trusted input fails." -> expect -> toBe("never") | None =>
| Some(x) => switch calculated { "this branch occurs when the dispatch to Jstat on trusted input fails."
| None => "algebraicAdd has" -> expect -> toBe("failed") ->expect
| Some(y) => x -> expect -> toBeSoCloseTo(y, ~digits=-1) ->toBe("never")
| Some(x) =>
switch calculated {
| None => "algebraicAdd has"->expect->toBe("failed")
| Some(y) => x->expect->toBeSoCloseTo(y, ~digits=-1)
} }
} }
}) })
test("(normal(mean=10) + normal(mean=10)).inv(1e-1)", () => { test("(normal(mean=10) + normal(mean=10)).inv(1e-1)", () => {
let received = normalDist20 let received =
-> Ok normalDist20
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 1e-1)) ->Ok
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 1e-1))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toOption ->E.R2.fmap(toFloat)
-> E.O.flatten ->E.R.toOption
let calculated = normalDist10 ->E.O.flatten
-> algebraicAdd(normalDist10) let calculated =
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 1e-1)) normalDist10
-> E.R2.fmap(run) ->algebraicAdd(normalDist10)
-> E.R2.fmap(toFloat) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 1e-1))
-> E.R.toOption ->E.R2.fmap(run)
-> E.O.flatten ->E.R2.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received { switch received {
| None => "this branch occurs when the dispatch to Jstat on trusted input fails." -> expect -> toBe("never") | None =>
| Some(x) => switch calculated { "this branch occurs when the dispatch to Jstat on trusted input fails."
| None => "algebraicAdd has" -> expect -> toBe("failed") ->expect
| Some(y) => x -> expect -> toBeSoCloseTo(y, ~digits=-1) ->toBe("never")
| Some(x) =>
switch calculated {
| None => "algebraicAdd has"->expect->toBe("failed")
| Some(y) => x->expect->toBeSoCloseTo(y, ~digits=-1)
} }
} }
}) })
test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).inv(2e-2)", () => { test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).inv(2e-2)", () => {
let received = uniformDist let received =
-> algebraicAdd(betaDist) uniformDist
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 2e-2)) ->algebraicAdd(betaDist)
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 2e-2))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=2. // sometimes it works with ~digits=2.
| Some(x) => x -> expect -> toBeSoCloseTo(10.927078217530806, ~digits=1) | Some(x) => x->expect->toBeSoCloseTo(10.927078217530806, ~digits=1)
} }
}) })
test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).inv(2e-2)", () => { test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).inv(2e-2)", () => {
let received = betaDist let received =
-> algebraicAdd(uniformDist) betaDist
-> E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 2e-2)) ->algebraicAdd(uniformDist)
-> E.R2.fmap(run) ->E.R2.fmap(d => GenericDist_Types.Constructors.UsingDists.inv(d, 2e-2))
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
-> E.R.toExn ->E.R2.fmap(toFloat)
->E.R.toExn
switch received { switch received {
| None => "algebraicAdd has" -> expect -> toBe("failed") | None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad. // This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
// sometimes it works with ~digits=2. // sometimes it works with ~digits=2.
| Some(x) => x -> expect -> toBeSoCloseTo(10.915396627014363, ~digits=0) | Some(x) => x->expect->toBeSoCloseTo(10.915396627014363, ~digits=0)
} }
}) })
}) })

View File

@ -8,7 +8,7 @@ let {
algebraicDivide, algebraicDivide,
algebraicSubtract, algebraicSubtract,
algebraicLogarithm, algebraicLogarithm,
algebraicPower algebraicPower,
} = module(DistributionOperation.Constructors) } = module(DistributionOperation.Constructors)
let algebraicAdd = algebraicAdd(~env) let algebraicAdd = algebraicAdd(~env)
@ -18,9 +18,7 @@ let algebraicSubtract = algebraicSubtract(~env)
let algebraicLogarithm = algebraicLogarithm(~env) let algebraicLogarithm = algebraicLogarithm(~env)
let algebraicPower = algebraicPower(~env) let algebraicPower = algebraicPower(~env)
describe("Mean", () => { describe("Mean", () => {
let mean = GenericDist_Types.Constructors.UsingDists.mean let mean = GenericDist_Types.Constructors.UsingDists.mean
let runMean: result<DistributionTypes.genericDist, DistributionTypes.error> => float = distR => { let runMean: result<DistributionTypes.genericDist, DistributionTypes.error> => float = distR => {
@ -30,7 +28,8 @@ describe("Mean", () => {
} }
} }
let impossiblePath: string => assertion = algebraicOp => `${algebraicOp} has`->expect->toEqual("failed") let impossiblePath: string => assertion = algebraicOp =>
`${algebraicOp} has`->expect->toEqual("failed")
let distributions = list{ let distributions = list{
normalMake(0.0, 1e0), normalMake(0.0, 1e0),
@ -40,7 +39,7 @@ describe("Mean", () => {
// cauchyMake(1e0, 1e0), // cauchyMake(1e0, 1e0),
lognormalMake(1e0, 1e0), lognormalMake(1e0, 1e0),
triangularMake(1e0, 1e1, 5e1), triangularMake(1e0, 1e1, 5e1),
Ok(floatMake(1e1)) Ok(floatMake(1e1)),
} }
let combinations = E.L.combinations2(distributions) let combinations = E.L.combinations2(distributions)
let zipDistsDists = E.L.zip(distributions, distributions) let zipDistsDists = E.L.zip(distributions, distributions)
@ -53,17 +52,18 @@ describe("Mean", () => {
let dist1 = E.R.fmap2(s => DistributionTypes.Other(s), dist1') let dist1 = E.R.fmap2(s => DistributionTypes.Other(s), dist1')
let dist2 = E.R.fmap2(s => DistributionTypes.Other(s), dist2') let dist2 = E.R.fmap2(s => DistributionTypes.Other(s), dist2')
let received = E.R.liftJoin2(algebraicAdd, dist1, dist2) let received =
-> E.R2.fmap(mean) E.R.liftJoin2(algebraicAdd, dist1, dist2)
-> E.R2.fmap(run) ->E.R2.fmap(mean)
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
->E.R2.fmap(toFloat)
let expected = runMean(dist1) +. runMean(dist2) let expected = runMean(dist1) +. runMean(dist2)
switch received { switch received {
| Error(err) => impossiblePath("algebraicAdd") | Error(err) => impossiblePath("algebraicAdd")
| Ok(x) => | Ok(x) =>
switch x { switch x {
| None => impossiblePath("algebraicAdd") | None => impossiblePath("algebraicAdd")
| Some(x) => x->expect->toBeSoCloseTo(expected, ~digits=digits) | Some(x) => x->expect->toBeSoCloseTo(expected, ~digits)
} }
} }
} }
@ -91,17 +91,18 @@ describe("Mean", () => {
let dist1 = E.R.fmap2(s => DistributionTypes.Other(s), dist1') let dist1 = E.R.fmap2(s => DistributionTypes.Other(s), dist1')
let dist2 = E.R.fmap2(s => DistributionTypes.Other(s), dist2') let dist2 = E.R.fmap2(s => DistributionTypes.Other(s), dist2')
let received = E.R.liftJoin2(algebraicSubtract, dist1, dist2) let received =
-> E.R2.fmap(mean) E.R.liftJoin2(algebraicSubtract, dist1, dist2)
-> E.R2.fmap(run) ->E.R2.fmap(mean)
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
->E.R2.fmap(toFloat)
let expected = runMean(dist1) -. runMean(dist2) let expected = runMean(dist1) -. runMean(dist2)
switch received { switch received {
| Error(err) => impossiblePath("algebraicSubtract") | Error(err) => impossiblePath("algebraicSubtract")
| Ok(x) => | Ok(x) =>
switch x { switch x {
| None => impossiblePath("algebraicSubtract") | None => impossiblePath("algebraicSubtract")
| Some(x) => x->expect->toBeSoCloseTo(expected, ~digits=digits) | Some(x) => x->expect->toBeSoCloseTo(expected, ~digits)
} }
} }
} }
@ -120,7 +121,6 @@ describe("Mean", () => {
let (dist1, dist2) = dists let (dist1, dist2) = dists
testSubtractionMean(dist2, dist1) testSubtractionMean(dist2, dist1)
}) })
}) })
describe("multiplication", () => { describe("multiplication", () => {
@ -130,17 +130,18 @@ describe("Mean", () => {
let dist1 = E.R.fmap2(s => DistributionTypes.Other(s), dist1') let dist1 = E.R.fmap2(s => DistributionTypes.Other(s), dist1')
let dist2 = E.R.fmap2(s => DistributionTypes.Other(s), dist2') let dist2 = E.R.fmap2(s => DistributionTypes.Other(s), dist2')
let received = E.R.liftJoin2(algebraicMultiply, dist1, dist2) let received =
-> E.R2.fmap(mean) E.R.liftJoin2(algebraicMultiply, dist1, dist2)
-> E.R2.fmap(run) ->E.R2.fmap(mean)
-> E.R2.fmap(toFloat) ->E.R2.fmap(run)
->E.R2.fmap(toFloat)
let expected = runMean(dist1) *. runMean(dist2) let expected = runMean(dist1) *. runMean(dist2)
switch received { switch received {
| Error(err) => impossiblePath("algebraicMultiply") | Error(err) => impossiblePath("algebraicMultiply")
| Ok(x) => | Ok(x) =>
switch x { switch x {
| None => impossiblePath("algebraicMultiply") | None => impossiblePath("algebraicMultiply")
| Some(x) => x->expect->toBeSoCloseTo(expected, ~digits=digits) | Some(x) => x->expect->toBeSoCloseTo(expected, ~digits)
} }
} }
} }
@ -159,6 +160,5 @@ describe("Mean", () => {
let (dist1, dist2) = dists let (dist1, dist2) = dists
testMultiplicationMean(dist2, dist1) testMultiplicationMean(dist2, dist1)
}) })
}) })
}) })

View File

@ -19,10 +19,12 @@ let run = DistributionOperation.run(~env)
let outputMap = fmap(~env) let outputMap = fmap(~env)
let unreachableInTestFileMessage = "Should be impossible to reach (This error is in test file)" let unreachableInTestFileMessage = "Should be impossible to reach (This error is in test file)"
let toExtFloat: option<float> => float = E.O.toExt(unreachableInTestFileMessage) let toExtFloat: option<float> => float = E.O.toExt(unreachableInTestFileMessage)
let toExtDist: option<DistributionTypes.genericDist> => DistributionTypes.genericDist = E.O.toExt(unreachableInTestFileMessage) let toExtDist: option<DistributionTypes.genericDist> => DistributionTypes.genericDist = E.O.toExt(
unreachableInTestFileMessage,
)
// let toExt: option<'a> => 'a = E.O.toExt(unreachableInTestFileMessage) // let toExt: option<'a> => 'a = E.O.toExt(unreachableInTestFileMessage)
let unpackFloat = x => x -> toFloat -> toExtFloat let unpackFloat = x => x->toFloat->toExtFloat
let unpackDist = y => y -> toDist -> toExtDist let unpackDist = y => y->toDist->toExtDist
let mkNormal = (mean, stdev) => DistributionTypes.Symbolic(#Normal({mean: mean, stdev: stdev})) let mkNormal = (mean, stdev) => DistributionTypes.Symbolic(#Normal({mean: mean, stdev: stdev}))
let mkBeta = (alpha, beta) => DistributionTypes.Symbolic(#Beta({alpha: alpha, beta: beta})) let mkBeta = (alpha, beta) => DistributionTypes.Symbolic(#Beta({alpha: alpha, beta: beta}))

View File

@ -3,8 +3,8 @@ open Expect
describe("E.L.combinations2", () => { describe("E.L.combinations2", () => {
test("size three", () => { test("size three", () => {
E.L.combinations2(list{"alice", "bob", "eve"}) -> expect -> toEqual( E.L.combinations2(list{"alice", "bob", "eve"})
list{("alice", "bob"), ("alice", "eve"), ("bob", "eve")} ->expect
) ->toEqual(list{("alice", "bob"), ("alice", "eve"), ("bob", "eve")})
}) })
}) })

View File

@ -180,7 +180,8 @@ module R = {
errorCondition(r) ? Error(errorMessage) : Ok(r) errorCondition(r) ? Error(errorMessage) : Ok(r)
let ap = Rationale.Result.ap let ap = Rationale.Result.ap
let ap' = (r, a) => switch r { let ap' = (r, a) =>
switch r {
| Ok(f) => fmap(f, a) | Ok(f) => fmap(f, a)
| Error(err) => Error(err) | Error(err) => Error(err)
} }
@ -189,11 +190,16 @@ module R = {
ap'(fmap(op, xR), yR) ap'(fmap(op, xR), yR)
} }
let liftJoin2: (('a, 'b) => result<'c, 'd>, result<'a, 'd>, result<'b, 'd>) => result<'c, 'd> = (op, xR, yR) => { let liftJoin2: (('a, 'b) => result<'c, 'd>, result<'a, 'd>, result<'b, 'd>) => result<'c, 'd> = (
op,
xR,
yR,
) => {
bind(liftM2(op, xR, yR), x => x) bind(liftM2(op, xR, yR), x => x)
} }
let fmap2 = (f, r) => switch r { let fmap2 = (f, r) =>
switch r {
| Ok(r) => r->Ok | Ok(r) => r->Ok
| Error(x) => x->f->Error | Error(x) => x->f->Error
} }