diff --git a/packages/squiggle-lang/__tests__/Distributions/Dotwise_test.res b/packages/squiggle-lang/__tests__/Distributions/Dotwise_test.res new file mode 100644 index 00000000..6a61e3e1 --- /dev/null +++ b/packages/squiggle-lang/__tests__/Distributions/Dotwise_test.res @@ -0,0 +1,50 @@ +open Jest +open Expect +open TestHelpers +open FastCheck +open Arbitrary +open Property.Sync + +describe("dotSubtract", () => { + test("mean of normal minus exponential (unit)", () => { + let mean = 0.0 + let rate = 10.0 + + let dotDifference = DistributionOperation.Constructors.pointwiseSubtract( + ~env, + mkNormal(mean, 1.0), + mkExponential(rate), + ) + let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), dotDifference) + let meanAnalytical = mean -. 1.0 /. rate + switch meanResult { + | Ok(meanValue) => meanValue->expect->toBeCloseTo(meanAnalytical) + | Error(err) => err->expect->toBe(DistributionTypes.OperationError(DivisionByZeroError)) + } + }) + Skip.test("mean of normal minus exponential (property)", () => { + assert_( + property2(float_(), floatRange(1e-5, 1e5), (mean, rate) => { + // We limit ourselves to stdev=1 so that the integral is trivial + let dotDifference = DistributionOperation.Constructors.pointwiseSubtract( + ~env, + mkNormal(mean, 1.0), + mkExponential(rate), + ) + let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), dotDifference) + // according to algebra or random variables, + let meanAnalytical = mean -. 1.0 /. rate + Js.Console.log3( + mean, + rate, + E.R.fmap(x => abs_float(x -. meanAnalytical) /. abs_float(meanAnalytical), meanResult), + ) + switch meanResult { + | Ok(meanValue) => abs_float(meanValue -. meanAnalytical) /. abs_float(meanValue) < 1e-2 // 1% relative error + | Error(err) => err === DistributionTypes.OperationError(DivisionByZeroError) + } + }), + ) + pass + }) +}) diff --git a/packages/squiggle-lang/__tests__/Distributions/Scale_test.res b/packages/squiggle-lang/__tests__/Distributions/Scale_test.res index 648013f6..45151e99 100644 --- a/packages/squiggle-lang/__tests__/Distributions/Scale_test.res +++ b/packages/squiggle-lang/__tests__/Distributions/Scale_test.res @@ -1,19 +1,30 @@ -/* -This test case comes via Nuño https://github.com/quantified-uncertainty/squiggle/issues/433 - - -*/ - open Jest open Expect open TestHelpers describe("Scale logarithm", () => { - test("mean of the base two scalar logarithm of an exponential(10)", () => { - let scalelog = DistributionOperation.Constructors.scaleLogarithm(~env, mkExponential(10.0), 2.0) + // test("mean of the base e scalar logarithm of an exponential(10)", () => { + // let rate = 10.0 + // let scalelog = DistributionOperation.Constructors.scaleLogarithm(~env, mkExponential(rate), MagicNumbers.Math.e) + // + // let meanResult = E.R2.bind(DistributionOperation.Constructors.mean(~env), scalelog) + // let meanAnalytical = Js.Math.log(rate /. MagicNumbers.Math.e) + // switch meanResult { + // | Ok(meanValue) => meanValue -> expect -> toBeCloseTo(meanAnalytical) + // | Error(err) => err -> expect -> toBe(DistributionTypes.OperationError(DivisionByZeroError)) + // } + // }) + let low = 10.0 + let high = 100.0 + let scalelog = DistributionOperation.Constructors.scaleLogarithm(~env, mkUniform(low, high), 2.0) - E.R2.bind(DistributionOperation.Constructors.mean(~env), scalelog) - ->expect - ->toEqual(Ok(-2.348336572091017)) + 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 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) + | Error(err) => err->expect->toBe(DistributionTypes.OperationError(NegativeInfinityError)) + } }) }) diff --git a/packages/squiggle-lang/__tests__/Distributions/Score_test.res b/packages/squiggle-lang/__tests__/Distributions/Score_test.res new file mode 100644 index 00000000..33cb0c82 --- /dev/null +++ b/packages/squiggle-lang/__tests__/Distributions/Score_test.res @@ -0,0 +1,11 @@ +/* +This test case comes via Nuño https://github.com/quantified-uncertainty/squiggle/issues/433 +*/ +open Jest +open Expect + +describe("KL divergence", () => { + test("our's agrees with analytical", () => { + true->expect->toBe(true) + }) +}) diff --git a/packages/squiggle-lang/bsconfig.json b/packages/squiggle-lang/bsconfig.json index e98b3822..81e4e133 100644 --- a/packages/squiggle-lang/bsconfig.json +++ b/packages/squiggle-lang/bsconfig.json @@ -20,7 +20,8 @@ ], "suffix": ".bs.js", "namespace": true, - "bs-dependencies": ["@glennsl/rescript-jest", "bisect_ppx"], + "bs-dependencies": ["bisect_ppx"], + "bs-dev-dependencies": ["@glennsl/rescript-jest", "rescript-fast-check"], "gentypeconfig": { "language": "typescript", "module": "commonjs",