Skip to content

Commit

Permalink
Merge pull request #1318 from quantified-uncertainty/swap-args
Browse files Browse the repository at this point in the history
Switch to leading argument order
  • Loading branch information
berekuk authored Oct 26, 2022
2 parents aecc37f + 25e7837 commit bfc4839
Show file tree
Hide file tree
Showing 56 changed files with 552 additions and 588 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let {run} = module(DistributionOperation)
let run = run(~env)
let outputMap = fmap(~env)
let toExt: option<'a> => 'a = E.O.toExt(
_,
"Should be impossible to reach (This error is in test file)",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ describe("dotSubtract", () => {
mkNormal(mean, 1.0),
mkExponential(rate),
)
let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), dotDifference)
let meanResult = E.R.bind(dotDifference, DistributionOperation.Constructors.mean(~env))
let meanAnalytical =
mean -.
SymbolicDist.Exponential.mean({rate: rate})->E.R2.toExn(
SymbolicDist.Exponential.mean({rate: rate})->E.R.toExn(
"On trusted input this should never happen",
)
switch meanResult {
Expand All @@ -42,11 +42,11 @@ describe("dotSubtract", () => {
mkNormal(mean, 1.0),
mkExponential(rate),
)
let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), dotDifference)
let meanResult = E.R.bind(dotDifference, DistributionOperation.Constructors.mean(~env))
// according to algebra or random variables,
let meanAnalytical =
mean -.
SymbolicDist.Exponential.mean({rate: rate})->E.R2.toExn(
SymbolicDist.Exponential.mean({rate: rate})->E.R.toExn(
"On trusted input this should never happen",
)
switch meanResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ describe("(Algebraic) addition of distributions", () => {
() => {
normalDist5
->algebraicAdd(normalDist20)
->E.R2.fmap(DistributionTypes.Constructors.UsingDists.mean)
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(DistributionTypes.Constructors.UsingDists.mean)
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
->expect
->toBe(Some(2.5e1))
},
Expand All @@ -62,10 +62,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
uniformDist
->algebraicAdd(betaDist)
->E.R2.fmap(DistributionTypes.Constructors.UsingDists.mean)
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(DistributionTypes.Constructors.UsingDists.mean)
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand All @@ -82,10 +82,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
betaDist
->algebraicAdd(uniformDist)
->E.R2.fmap(DistributionTypes.Constructors.UsingDists.mean)
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(DistributionTypes.Constructors.UsingDists.mean)
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand All @@ -104,17 +104,17 @@ describe("(Algebraic) addition of distributions", () => {
let received =
normalDist10 // this should be normal(10, sqrt(8))
->Ok
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, x))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
let calculated =
normalDist5
->algebraicAdd(normalDist5)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, x))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten

Expand All @@ -137,17 +137,17 @@ describe("(Algebraic) addition of distributions", () => {
let received =
normalDist20
->Ok
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1.9e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1.9e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
let calculated =
normalDist10
->algebraicAdd(normalDist10)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1.9e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1.9e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received {
Expand All @@ -169,10 +169,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
uniformDist
->algebraicAdd(betaDist)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand All @@ -188,10 +188,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
betaDist
->algebraicAdd(uniformDist)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.pdf(d, 1e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| None => "algebraicAdd has"->expect->toBe("failed")
// This is nondeterministic.
Expand All @@ -208,17 +208,17 @@ describe("(Algebraic) addition of distributions", () => {
let received =
normalDist10
->Ok
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, x))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
let calculated =
normalDist5
->algebraicAdd(normalDist5)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, x))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten

Expand All @@ -241,17 +241,17 @@ describe("(Algebraic) addition of distributions", () => {
let received =
normalDist20
->Ok
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1.25e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1.25e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
let calculated =
normalDist10
->algebraicAdd(normalDist10)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1.25e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1.25e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received {
Expand All @@ -273,10 +273,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
uniformDist
->algebraicAdd(betaDist)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand All @@ -291,10 +291,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
betaDist
->algebraicAdd(uniformDist)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1e1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.cdf(d, 1e1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand All @@ -313,17 +313,17 @@ describe("(Algebraic) addition of distributions", () => {
let received =
normalDist10
->Ok
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, x))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
let calculated =
normalDist5
->algebraicAdd(normalDist5)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, x))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, x))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten

Expand All @@ -346,17 +346,17 @@ describe("(Algebraic) addition of distributions", () => {
let received =
normalDist20
->Ok
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 1e-1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 1e-1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
let calculated =
normalDist10
->algebraicAdd(normalDist10)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 1e-1))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 1e-1))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toOption
->E.O.flatten
switch received {
Expand All @@ -378,10 +378,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
uniformDist
->algebraicAdd(betaDist)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 2e-2))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 2e-2))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand All @@ -396,10 +396,10 @@ describe("(Algebraic) addition of distributions", () => {
let received =
betaDist
->algebraicAdd(uniformDist)
->E.R2.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 2e-2))
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(d => DistributionTypes.Constructors.UsingDists.inv(d, 2e-2))
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
switch received {
| 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Internals = {
let pairsOfDifferentDistributions = combinations2(distributions)

let runMean: DistributionTypes.genericDist => float = dist => {
dist->mean->run->toFloat->E.O2.toExn("Shouldn't see this because we trust testcase input")
dist->mean->run->toFloat->E.O.toExn("Shouldn't see this because we trust testcase input")
}

let testOperationMean = (
Expand All @@ -57,10 +57,10 @@ module Internals = {
let dist2 = dist2'->DistributionTypes.Symbolic
let received =
distOp(dist1, dist2)
->E.R2.fmap(mean)
->E.R2.fmap(run)
->E.R2.fmap(toFloat)
->E.R.toExn("Expected float", _)
->E.R.fmap(mean)
->E.R.fmap(run)
->E.R.fmap(toFloat)
->E.R.toExn("Expected float")
let expected = floatOp(runMean(dist1), runMean(dist2))
switch received {
| None => expectImpossiblePath(description)
Expand Down Expand Up @@ -91,7 +91,7 @@ describe("Means are invariant", () => {
describe("for addition", () => {
let testAdditionMean = testOperationMean(algebraicAdd, "algebraicAdd", \"+.", ~epsilon)
let testAddInvariant = (t1, t2) =>
E.R.liftM2(testAdditionMean, t1, t2)->E.R.toExn("Means were not invariant", _)
E.R.liftM2(testAdditionMean, t1, t2)->E.R.toExn("Means were not invariant")

testAll(
"with two of the same distribution",
Expand Down Expand Up @@ -128,7 +128,7 @@ describe("Means are invariant", () => {
~epsilon,
)
let testSubtractInvariant = (t1, t2) =>
E.R.liftM2(testSubtractionMean, t1, t2)->E.R.toExn("Means were not invariant", _)
E.R.liftM2(testSubtractionMean, t1, t2)->E.R.toExn("Means were not invariant")

testAll(
"with two of the same distribution",
Expand Down Expand Up @@ -165,7 +165,7 @@ describe("Means are invariant", () => {
~epsilon,
)
let testMultiplicationInvariant = (t1, t2) =>
E.R.liftM2(testMultiplicationMean, t1, t2)->E.R.toExn("Means were not invariant", _)
E.R.liftM2(testMultiplicationMean, t1, t2)->E.R.toExn("Means were not invariant")

testAll(
"with two of the same distribution",
Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/__tests__/Distributions/Scale_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Scale logarithm", () => {
MagicNumbers.Math.e,
)

let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), scalelog)
let meanResult = E.R.bind(scalelog, DistributionOperation.Constructors.mean(~env))
// expected value of log of exponential distribution.
let meanAnalytical = Js.Math.log(rate) +. 1.0
switch meanResult {
Expand All @@ -28,7 +28,7 @@ describe("Scale logarithm", () => {

test("mean of the base 2 scalar logarithm of a uniform(10, 100)", () => {
//For uniform pdf `_ => 1 / (b - a)`, the expected value of log of uniform is `integral from a to b of x * log(1 / (b -a)) dx`
let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), scalelog)
let meanResult = E.R.bind(scalelog, DistributionOperation.Constructors.mean(~env))
let meanAnalytical = -.Js.Math.log2(high -. low) /. 2.0 *. (high ** 2.0 -. low ** 2.0) // -. Js.Math.log2(high -. low)
switch meanResult {
| Ok(meanValue) => meanValue->expect->toBeCloseTo(meanAnalytical)
Expand Down
Loading

0 comments on commit bfc4839

Please sign in to comment.