Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rng initialized with seedrandom (alea) #2992

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/squiggle-lang/__tests__/dist/Dotwise_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fc from "fast-check";
import seedrandom from "seedrandom";

import { binaryOperations } from "../../src/dist/distOperations/index.js";
import { DivisionByZeroError } from "../../src/operationError.js";
Expand All @@ -9,6 +10,8 @@ import {
unpackResult,
} from "../helpers/distHelpers.js";

const rng = seedrandom();

describe("dotSubtract", () => {
test("mean of normal minus exponential (unit)", () => {
const mean = 0;
Expand All @@ -17,7 +20,7 @@ describe("dotSubtract", () => {
binaryOperations.pointwiseSubtract(
mkNormal(mean, 1.0),
mkExponential(rate),
{ env }
{ env, rng }
)
);
const meanValue = dotDifference.mean();
Expand All @@ -38,7 +41,7 @@ describe("dotSubtract", () => {
const dotDifferenceR = binaryOperations.pointwiseSubtract(
mkNormal(mean, 1.0),
mkExponential(rate),
{ env }
{ env, rng }
);
if (!dotDifferenceR.ok) {
const err = dotDifferenceR.value;
Expand Down
5 changes: 4 additions & 1 deletion packages/squiggle-lang/__tests__/dist/GenericDist_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import seedrandom from "seedrandom";

import { BaseDist } from "../../src/dist/BaseDist.js";
import { DistError } from "../../src/dist/DistError.js";
import { SampleSetDist } from "../../src/dist/SampleSetDist/index.js";
Expand All @@ -22,6 +24,7 @@ const env: Env = {
sampleCount: 100,
xyPointLength: 100,
};
const rng = seedrandom();

describe("toPointSet", () => {
test("on symbolic normal distribution", () => {
Expand All @@ -35,7 +38,7 @@ describe("toPointSet", () => {
const pointSet = unpackResult(
Result.bind(
Result.bind(normalDist5.toPointSetDist(env), (pointSet) =>
SampleSetDist.fromDist(pointSet, env)
SampleSetDist.fromDist(pointSet, env, rng)
),
(sampleSet) => sampleSet.toPointSetDist(env)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ when things substantially change.
Also, there are some open comments in https://github.com/quantified-uncertainty/squiggle/pull/232 that haven't been addressed.
*/

import seedrandom from "seedrandom";

import { binaryOperations } from "../../../src/dist/distOperations/index.js";
import * as Result from "../../../src/utility/result.js";
import {
Expand All @@ -16,27 +18,30 @@ import {
} from "../../fixtures/distFixtures.js";
import { env, unpackResult } from "../../helpers/distHelpers.js";

const rng = seedrandom();
const { algebraicAdd } = binaryOperations;

describe("(Algebraic) addition of distributions", () => {
describe("mean", () => {
test("normal(mean=5) + normal(mean=20)", () => {
expect(
unpackResult(algebraicAdd(normalDist5, normalDist20, { env })).mean()
unpackResult(
algebraicAdd(normalDist5, normalDist20, { env, rng })
).mean()
).toBe(2.5e1);
});

test("uniform(low=9, high=10) + beta(alpha=2, beta=5)", () => {
const received = unpackResult(
algebraicAdd(uniformDist, betaDist, { env })
algebraicAdd(uniformDist, betaDist, { env, rng })
).mean();
// 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.
expect(received).toBeCloseTo(9.786831807237022, 1); // (uniformMean +. betaMean)
});
test("beta(alpha=2, beta=5) + uniform(low=9, high=10)", () => {
const received = unpackResult(
algebraicAdd(betaDist, uniformDist, { env })
algebraicAdd(betaDist, uniformDist, { env, rng })
).mean();

// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
Expand All @@ -51,8 +56,9 @@ describe("(Algebraic) addition of distributions", () => {
(x) => {
const received = unpackResult(normalDist10.pdf(x)); // this should be normal(10, sqrt(8))
const calculated: number = unpackResult(
Result.bind(algebraicAdd(normalDist5, normalDist5, { env }), (d) =>
d.pdf(x, { env })
Result.bind(
algebraicAdd(normalDist5, normalDist5, { env, rng }),
(d) => d.pdf(x, { env })
)
);

Expand All @@ -63,16 +69,17 @@ describe("(Algebraic) addition of distributions", () => {
test("(normal(mean=10) + normal(mean=10)).pdf(1.9e1)", () => {
const received = unpackResult(normalDist20.pdf(1.9e1));
const calculated: number = unpackResult(
Result.bind(algebraicAdd(normalDist10, normalDist10, { env }), (d) =>
d.pdf(1.9e1, { env })
Result.bind(
algebraicAdd(normalDist10, normalDist10, { env, rng }),
(d) => d.pdf(1.9e1, { env })
)
);

expect(received).toBeCloseTo(calculated, 1);
});
test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).pdf(10)", () => {
const received: number = unpackResult(
Result.bind(algebraicAdd(uniformDist, betaDist, { env }), (d) =>
Result.bind(algebraicAdd(uniformDist, betaDist, { env, rng }), (d) =>
d.pdf(1e1, { env })
)
);
Expand All @@ -84,7 +91,7 @@ describe("(Algebraic) addition of distributions", () => {
});
test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).pdf(10)", () => {
const received = unpackResult(
Result.bind(algebraicAdd(betaDist, uniformDist, { env }), (d) =>
Result.bind(algebraicAdd(betaDist, uniformDist, { env, rng }), (d) =>
d.pdf(1e1, { env })
)
);
Expand All @@ -99,7 +106,7 @@ describe("(Algebraic) addition of distributions", () => {
(x) => {
const received = normalDist10.cdf(x);
const calculated = unpackResult(
algebraicAdd(normalDist5, normalDist5, { env })
algebraicAdd(normalDist5, normalDist5, { env, rng })
).cdf(x);

expect(received).toBeCloseTo(calculated, 0);
Expand All @@ -110,14 +117,14 @@ describe("(Algebraic) addition of distributions", () => {
const received = normalDist20.cdf(1.25e1);

const calculated = unpackResult(
algebraicAdd(normalDist10, normalDist10, { env })
algebraicAdd(normalDist10, normalDist10, { env, rng })
).cdf(1.25e1);

expect(received).toBeCloseTo(calculated, 2);
});
test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).cdf(10)", () => {
const received = unpackResult(
algebraicAdd(uniformDist, betaDist, { env })
algebraicAdd(uniformDist, betaDist, { env, rng })
).cdf(1e1);

// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
Expand All @@ -126,7 +133,7 @@ describe("(Algebraic) addition of distributions", () => {
});
test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).cdf(10)", () => {
const received = unpackResult(
algebraicAdd(betaDist, uniformDist, { env })
algebraicAdd(betaDist, uniformDist, { env, rng })
).cdf(1e1);

// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
Expand All @@ -142,7 +149,7 @@ describe("(Algebraic) addition of distributions", () => {
const received = normalDist10.inv(x);

const calculated = unpackResult(
algebraicAdd(normalDist5, normalDist5, { env })
algebraicAdd(normalDist5, normalDist5, { env, rng })
).inv(x);

expect(received).toBeCloseTo(calculated, -1);
Expand All @@ -152,14 +159,14 @@ describe("(Algebraic) addition of distributions", () => {
const received = normalDist20.inv(1e-1);

const calculated = unpackResult(
algebraicAdd(normalDist10, normalDist10, { env })
algebraicAdd(normalDist10, normalDist10, { env, rng })
).inv(1e-1);

expect(received).toBeCloseTo(calculated, -1);
});
test("(uniform(low=9, high=10) + beta(alpha=2, beta=5)).inv(2e-2)", () => {
const received = unpackResult(
algebraicAdd(uniformDist, betaDist, { env })
algebraicAdd(uniformDist, betaDist, { env, rng })
).inv(2e-2);

// This is nondeterministic, we could be in a situation where ci fails but you click rerun and it passes, which is bad.
Expand All @@ -168,7 +175,7 @@ describe("(Algebraic) addition of distributions", () => {
});
test("(beta(alpha=2, beta=5) + uniform(low=9, high=10)).inv(2e-2)", () => {
const received = unpackResult(
algebraicAdd(betaDist, uniformDist, { env })
algebraicAdd(betaDist, uniformDist, { env, rng })
).inv(2e-2);

// 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 @@ -8,6 +8,8 @@ Details in https://squiggle-language.com/docs/internal/invariants/
Note: epsilon of 1e3 means the invariants are, in general, not being satisfied.
*/

import seedrandom from "seedrandom";

import { BaseDist } from "../../../src/dist/BaseDist.js";
import {
BinaryOperation,
Expand All @@ -28,6 +30,7 @@ import {
import { expectErrorToBeBounded } from "../../helpers/helpers.js";

const epsilon = 5e1;
const rng = seedrandom();

const distributions = [
mkNormal(4, 1),
Expand Down Expand Up @@ -58,7 +61,7 @@ const testOperationMean = (
dist2: BaseDist,
{ epsilon, env }: { epsilon: number; env: Env }
) => {
const received = unpackResult(distOp(dist1, dist2, { env })).mean();
const received = unpackResult(distOp(dist1, dist2, { env, rng })).mean();

const expected = floatOp(dist1.mean(), dist2.mean());

Expand Down
10 changes: 7 additions & 3 deletions packages/squiggle-lang/__tests__/dist/Mixture_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import seedrandom from "seedrandom";

import { mixture } from "../../src/dist/distOperations/index.js";
import {
env,
Expand All @@ -9,6 +11,8 @@ import {
unpackResult,
} from "../helpers/distHelpers.js";

const rng = seedrandom();

describe("mixture", () => {
test.each([
[0.0, 1e2],
Expand All @@ -23,7 +27,7 @@ describe("mixture", () => {
[mkNormal(mean1, 9e-1), 0.5],
[mkNormal(mean2, 9e-1), 0.5],
],
{ env }
{ env, rng }
)
).mean();
expect(meanValue).toBeCloseTo((mean1 + mean2) / 2, -1);
Expand All @@ -45,7 +49,7 @@ describe("mixture", () => {
[mkBeta(alpha, beta), betaWeight],
[mkExponential(rate), exponentialWeight],
],
{ env }
{ env, rng }
)
).mean();
const betaMean = 1 / (1 + beta / alpha);
Expand Down Expand Up @@ -81,7 +85,7 @@ describe("mixture", () => {
[mkUniform(low, high), uniformWeight],
[mkLognormal(mu, sigma), lognormalWeight],
],
{ env }
{ env, rng }
)
).mean();
const uniformMean = (low + high) / 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import seedrandom from "seedrandom";
import { BaseDist } from "../../../src/dist/BaseDist.js";
import { distErrorToString } from "../../../src/dist/DistError.js";
import {
Expand All @@ -20,6 +21,8 @@ import {
unpackResult,
} from "../../helpers/distHelpers.js";

const rng = seedrandom();

const klDivergence = (prediction: BaseDist, answer: BaseDist): number => {
const result = logScoreDistAnswer({
estimate: prediction,
Expand Down Expand Up @@ -106,7 +109,7 @@ describe("klDivergence: discrete -> discrete -> float", () => {
[point1, 1],
[point2, 1],
],
{ env }
{ env, rng }
)
);
const b = unpackResult(
Expand All @@ -116,7 +119,7 @@ describe("klDivergence: discrete -> discrete -> float", () => {
[point2, 1],
[point3, 1],
],
{ env }
{ env, rng }
)
);

Expand All @@ -143,7 +146,7 @@ describe("klDivergence: mixed -> mixed -> float", () => {
[point1, 1.0],
[uniformDist, 1.0],
],
{ env }
{ env, rng }
)
);
const b = unpackResult(
Expand All @@ -153,7 +156,7 @@ describe("klDivergence: mixed -> mixed -> float", () => {
[floatDist, 1.0],
[normalDist10, 1.0],
],
{ env }
{ env, rng }
)
);
const c = unpackResult(
Expand All @@ -164,7 +167,7 @@ describe("klDivergence: mixed -> mixed -> float", () => {
[point3, 1.0],
[uniformDist, 1.0],
],
{ env }
{ env, rng }
)
);
const d = unpackResult(
Expand All @@ -176,7 +179,7 @@ describe("klDivergence: mixed -> mixed -> float", () => {
[floatDist, 1.0],
[uniformDist2, 1.0],
],
{ env }
{ env, rng }
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import seedrandom from "seedrandom";

import {
logScoreScalarAnswer,
mixture,
} from "../../../src/dist/distOperations/index.js";
import { env, mkPointMass, unpackResult } from "../../helpers/distHelpers.js";

const rng = seedrandom();

describe("WithScalarAnswer: discrete -> scalar -> score", () => {
const pointA = mkPointMass(3.0);
const pointB = mkPointMass(2.0);
Expand All @@ -19,7 +23,7 @@ describe("WithScalarAnswer: discrete -> scalar -> score", () => {
[pointC, 0.25],
[pointD, 0.25],
],
{ env }
{ env, rng }
)
);

Expand All @@ -42,7 +46,7 @@ describe("WithScalarAnswer: discrete -> scalar -> score", () => {
[pointA, 0.75],
[pointB, 0.25],
],
{ env }
{ env, rng }
)
);

Expand All @@ -65,7 +69,7 @@ describe("WithScalarAnswer: discrete -> scalar -> score", () => {
[pointA, 0.5],
[pointB, 0.5],
],
{ env }
{ env, rng }
)
);
const prediction = unpackResult(
Expand All @@ -74,7 +78,7 @@ describe("WithScalarAnswer: discrete -> scalar -> score", () => {
[pointA, 0.75],
[pointB, 0.25],
],
{ env }
{ env, rng }
)
);

Expand Down