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

Scoring refactoring #3116

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
5 changes: 5 additions & 0 deletions .changeset/bright-yaks-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Breaking: Replaces Dist.logScore with Dist.logScoreDistAnswer and Dist.logScoreScalarAnswer
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { BaseDist } from "../../../src/dist/BaseDist.js";
import { distErrorToString } from "../../../src/dist/DistError.js";
import {
logScoreDistAnswer,
mixture,
} from "../../../src/dist/distOperations/index.js";
import { mixture } from "../../../src/dist/distOperations/index.js";
import { logScoreDistAnswer } from "../../../src/fr/scoring.js";
import { getDefaultRng } from "../../../src/rng/index.js";
import {
floatDist,
Expand All @@ -23,19 +20,12 @@ import {

const rng = getDefaultRng();

const klDivergence = (prediction: BaseDist, answer: BaseDist): number => {
const result = logScoreDistAnswer({
const klDivergence = (prediction: BaseDist, answer: BaseDist): number =>
logScoreDistAnswer({
estimate: prediction,
answer,
prior: undefined,
env,
});
if (!result.ok) {
console.log(distErrorToString(result.value));
throw new Error("logScore failed");
}
return result.value;
};
}).integralSum();

// integral from low to high of 1 / (high - low) log(normal(mean, stdev)(x) / (1 / (high - low))) dx
const klNormalUniform = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
logScoreScalarAnswer,
mixture,
} from "../../../src/dist/distOperations/index.js";
import { mixture } from "../../../src/dist/distOperations/index.js";
import { logScoreScalarAnswer } from "../../../src/dist/scoring/ScalarAnswer.js";
import { getDefaultRng } from "../../../src/rng/index.js";
import { env, mkPointMass, unpackResult } from "../../helpers/distHelpers.js";

Expand All @@ -27,14 +25,11 @@ describe("WithScalarAnswer: discrete -> scalar -> score", () => {
);

const answer = 2.0; // So this is: assigning 100% probability to 2.0
const x = unpackResult(
logScoreScalarAnswer({
estimate: prediction,
answer,
prior: undefined,
env,
})
);
const x = logScoreScalarAnswer({
estimate: prediction,
answer,
env,
}).getOrThrow().discrete;
expect(x).toEqual(-Math.log(0.25 / 1.0));
});

Expand All @@ -54,42 +49,12 @@ describe("WithScalarAnswer: discrete -> scalar -> score", () => {
logScoreScalarAnswer({
estimate: prediction,
answer,
prior: undefined,
env,
})
);
expect(x).toEqual(-Math.log(0.75 / 1.0));
});

test("scoreWithPrior: agrees with analytical answer when finite", () => {
const prior = unpackResult(
mixture(
[
[pointA, 0.5],
[pointB, 0.5],
],
{ env, rng }
)
);
const prediction = unpackResult(
mixture(
[
[pointA, 0.75],
[pointB, 0.25],
],
{ env, rng }
)
);

const answer = 3.0; // So this is: assigning 100% probability to 2.0
const x = unpackResult(
logScoreScalarAnswer({
estimate: prediction,
answer,
prior,
env,
})
);
expect(x).toEqual(-Math.log(0.75 / 1.0) - -Math.log(0.5 / 1.0));
expect(x).toEqual({
continuous: Infinity,
discrete: -Math.log(0.75 / 1.0),
});
});
});
3 changes: 2 additions & 1 deletion packages/squiggle-lang/__tests__/helpers/distHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const env: Env = defaultEnv;

export const unpackResult = <T>(x: Result.result<T, unknown>): T => {
if (!x.ok) {
throw new Error("failed");
console.error(x.value);
throw new Error("unpacking failed");
}
return x.value;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/__tests__/library/dist_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Dist", () => {
"[PointMass(4),PointMass(-2)]"
);
testEvalToBe(
"Dist.logScore({estimate: mx(Sym.normal(5,2), Sym.uniform(-1000, 1000), [.5, .5]), answer: Sym.normal(5.2,2.2)})",
"0.5787500741731704"
"Dist.logScoreDistAnswer(mx(Sym.normal(5,2), Sym.uniform(-1000, 1000), [.5, .5]), Sym.normal(5.2,2.2))",
"{score: 0.5787500741731704, shape: Point Set Distribution}"
);
});
8 changes: 2 additions & 6 deletions packages/squiggle-lang/__tests__/library/scoring_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { testEvalToBe } from "../helpers/reducerHelpers.js";

describe("Scoring", () => {
testEvalToBe(
"Dist.logScore({estimate: Sym.normal(5,2), answer: Sym.normal(5.2,1), prior: Sym.normal(5.5,3)})",
"-0.33591375663884876"
);
testEvalToBe(
"Dist.logScore({estimate: Sym.normal(5,2), answer: Sym.normal(5.2,1)})",
"Dist.logScoreDistAnswer(Sym.normal(5,2), Sym.normal(5.2,1)).score",
"0.32244107041564646"
);
testEvalToBe(
"Dist.logScore({estimate: Sym.normal(5,2), answer: 4.5})",
"Dist.logScoreNumericAnswer(Sym.normal(5,2), 4.5).continuous",
"1.6433360626394853"
);
testEvalToBe(
Expand Down
20 changes: 13 additions & 7 deletions packages/squiggle-lang/src/PointSet/Continuous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ export class ContinuousShape implements PointSet<ContinuousShape> {
}

xToY(f: number) {
switch (this.interpolation) {
case "Stepwise":
return MixedPoint.makeContinuous(
XYShape.XtoY.stepwiseIncremental(this.xyShape, f) ?? 0
);
case "Linear":
return MixedPoint.makeContinuous(XYShape.XtoY.linear(this.xyShape, f));
if (this.xyShape.xs.length === 0) {
return MixedPoint.makeContinuous(0);
} else {
switch (this.interpolation) {
case "Stepwise":
return MixedPoint.makeContinuous(
XYShape.XtoY.stepwiseIncremental(this.xyShape, f) ?? 0
);
case "Linear":
return MixedPoint.makeContinuous(
XYShape.XtoY.linear(this.xyShape, f)
);
}
}
}

Expand Down
130 changes: 0 additions & 130 deletions packages/squiggle-lang/src/PointSet/PointSetDist_Scoring.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/squiggle-lang/src/dist/distOperations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { type BinaryOperation, binaryOperations } from "./binaryOperations.js";
export { logScoreDistAnswer, logScoreScalarAnswer } from "./logScore.js";
export { mixture } from "./mixture.js";
export { pointwiseCombinationFloat } from "./pointwiseCombination.js";
export { scaleLog, scaleLogWithThreshold } from "./scaleOperations.js";
Loading
Loading