From 3a56d6fca4e83943c0dcd24fc73d1a16bc760513 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Mon, 23 May 2022 12:07:03 -0400 Subject: [PATCH] intermediate commit Value: [1e-10 to 1e-5] --- .../Distributions/PointSetDist/Continuous.res | 26 +++++++------- .../Distributions/PointSetDist/Discrete.res | 20 +++++------ .../PointSetDist/Distributions.res | 16 ++++----- .../Distributions/PointSetDist/Mixed.res | 16 ++++----- .../PointSetDist/PointSetDist.res | 35 ++++++++++--------- .../PointSetDist/PointSetDist_Scoring.res | 26 ++++++++------ 6 files changed, 73 insertions(+), 66 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res index 3661a531..658c6b8a 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res @@ -271,19 +271,19 @@ module T = Dist({ let variance = (t: t): float => XYShape.Analysis.getVarianceDangerously(t, mean, Analysis.getMeanOfSquares) - let klDivergence = (prediction: t, answer: t) => { - let newShape = XYShape.PointwiseCombination.combineAlongSupportOfSecondArgument( - PointSetDist_Scoring.KLDivergence.integrand, - prediction.xyShape, - answer.xyShape, - ) - newShape->E.R2.fmap(x => x->make->integralEndY) - } - let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { - let priorPdf = prior->E.O2.fmap((shape, x) => XYShape.XtoY.linear(x, shape.xyShape)) - let predictionPdf = x => XYShape.XtoY.linear(x, prediction.xyShape) - PointSetDist_Scoring.LogScoreWithPointResolution.score(~priorPdf, ~predictionPdf, ~answer) - } + // let klDivergence = (prediction: t, answer: t) => { + // let newShape = XYShape.PointwiseCombination.combineAlongSupportOfSecondArgument( + // PointSetDist_Scoring.KLDivergence.integrand, + // prediction.xyShape, + // answer.xyShape, + // ) + // newShape->E.R2.fmap(x => x->make->integralEndY) + // } + // let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { + // let priorPdf = prior->E.O2.fmap((shape, x) => XYShape.XtoY.linear(x, shape.xyShape)) + // let predictionPdf = x => XYShape.XtoY.linear(x, prediction.xyShape) + // PointSetDist_Scoring.LogScoreWithPointResolution.score(~priorPdf, ~predictionPdf, ~answer) + // } }) let isNormalized = (t: t): bool => { diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res index fea5db6f..b2347dc2 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res @@ -222,14 +222,14 @@ module T = Dist({ XYShape.Analysis.getVarianceDangerously(t, mean, getMeanOfSquares) } - let klDivergence = (prediction: t, answer: t) => { - combinePointwise( - ~fn=PointSetDist_Scoring.KLDivergence.integrand, - prediction, - answer, - )->E.R2.fmap(integralEndY) - } - let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { - Error(Operation.NotYetImplemented) - } + // let klDivergence = (prediction: t, answer: t) => { + // combinePointwise( + // ~fn=PointSetDist_Scoring.KLDivergence.integrand, + // prediction, + // answer, + // )->E.R2.fmap(integralEndY) + // } + // let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { + // Error(Operation.NotYetImplemented) + // } }) diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Distributions.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Distributions.res index 2d0358ec..fe848107 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Distributions.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Distributions.res @@ -33,12 +33,12 @@ module type dist = { let mean: t => float let variance: t => float - let klDivergence: (t, t) => result - let logScoreWithPointResolution: ( - ~prediction: t, - ~answer: float, - ~prior: option, - ) => result + // let klDivergence: (t, t) => result + // let logScoreWithPointResolution: ( + // ~prediction: t, + // ~answer: float, + // ~prior: option, + // ) => result } module Dist = (T: dist) => { @@ -61,8 +61,8 @@ module Dist = (T: dist) => { let mean = T.mean let variance = T.variance let integralEndY = T.integralEndY - let klDivergence = T.klDivergence - let logScoreWithPointResolution = T.logScoreWithPointResolution + // let klDivergence = T.klDivergence + // let logScoreWithPointResolution = T.logScoreWithPointResolution let updateIntegralCache = T.updateIntegralCache diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Mixed.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Mixed.res index 42a88909..c9d46efb 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Mixed.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Mixed.res @@ -301,14 +301,14 @@ module T = Dist({ } } - let klDivergence = (prediction: t, answer: t) => { - let klDiscretePart = Discrete.T.klDivergence(prediction.discrete, answer.discrete) - let klContinuousPart = Continuous.T.klDivergence(prediction.continuous, answer.continuous) - E.R.merge(klDiscretePart, klContinuousPart)->E.R2.fmap(t => fst(t) +. snd(t)) - } - let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { - Error(Operation.NotYetImplemented) - } + // let klDivergence = (prediction: t, answer: t) => { + // let klDiscretePart = Discrete.T.klDivergence(prediction.discrete, answer.discrete) + // let klContinuousPart = Continuous.T.klDivergence(prediction.continuous, answer.continuous) + // E.R.merge(klDiscretePart, klContinuousPart)->E.R2.fmap(t => fst(t) +. snd(t)) + // } + // let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { + // Error(Operation.NotYetImplemented) + // } }) let combineAlgebraically = (op: Operation.convolutionOperation, t1: t, t2: t): t => { diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res index d21a7383..7be054c2 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res @@ -196,24 +196,27 @@ module T = Dist({ | Continuous(m) => Continuous.T.variance(m) } - let klDivergence = (prediction: t, answer: t) => - switch (prediction, answer) { - | (Continuous(t1), Continuous(t2)) => Continuous.T.klDivergence(t1, t2) - | (Discrete(t1), Discrete(t2)) => Discrete.T.klDivergence(t1, t2) - | (m1, m2) => Mixed.T.klDivergence(m1->toMixed, m2->toMixed) - } - - let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { - switch (prior, prediction) { - | (Some(Continuous(t1)), Continuous(t2)) => - Continuous.T.logScoreWithPointResolution(~prediction=t2, ~answer, ~prior=t1->Some) - | (None, Continuous(t2)) => - Continuous.T.logScoreWithPointResolution(~prediction=t2, ~answer, ~prior=None) - | _ => Error(Operation.NotYetImplemented) - } - } + // let klDivergence = (prediction: t, answer: t) => + // switch (prediction, answer) { + // | (Continuous(t1), Continuous(t2)) => Continuous.T.klDivergence(t1, t2) + // | (Discrete(t1), Discrete(t2)) => Discrete.T.klDivergence(t1, t2) + // | (m1, m2) => Mixed.T.klDivergence(m1->toMixed, m2->toMixed) + // } + // + // let logScoreWithPointResolution = (~prediction: t, ~answer: float, ~prior: option) => { + // switch (prior, prediction) { + // | (Some(Continuous(t1)), Continuous(t2)) => + // Continuous.T.logScoreWithPointResolution(~prediction=t2, ~answer, ~prior=t1->Some) + // | (None, Continuous(t2)) => + // Continuous.T.logScoreWithPointResolution(~prediction=t2, ~answer, ~prior=None) + // | _ => Error(Operation.NotYetImplemented) + // } + // } }) +let logScore = (args: PointSetDist_Scoring.scoreArgs): result => + PointSetDist_Scoring.logScore(args) + let pdf = (f: float, t: t) => { let mixedPoint: PointSetTypes.mixedPoint = T.xToY(f, t) mixedPoint.continuous +. mixedPoint.discrete diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist_Scoring.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist_Scoring.res index 6d843a9d..7ff86c91 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist_Scoring.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist_Scoring.res @@ -1,4 +1,4 @@ -type t = PointSetDist.pointSetDist +type t = PointSetTypes.pointSetDist type continuousShape = PointSetTypes.continuousShape type discreteShape = PointSetTypes.discreteShape type mixedShape = PointSetTypes.mixedShape @@ -30,16 +30,16 @@ module WithDistAnswer = { minusScaledLogOfQuot(~esti=estimateElement, ~answ=answerElement) } - let sum = (~estimate: t, ~answer: t, ~integrateFn) => - PointSetDist.combinePointwise(integrand, estimate, answer)->E.R2.fmap(integrateFn) + let sum = (~estimate: t, ~answer: t, ~combineFn, ~integrateFn) => + combineFn(integrand, estimate, answer)->E.R2.fmap(integrateFn) - let sumWithPrior = (~estimate: t, ~answer: t, ~prior: t, ~integrateFn): result< + let sumWithPrior = (~estimate: t, ~answer: t, ~prior: t, ~combineFn, ~integrateFn): result< float, Operation.Error.t, > => { - let kl1 = sum(~estimate, ~answer, ~integrateFn) - let kl2 = sum(~estimate=prior, ~answer, ~integrateFn) - E.R.merge(kl1, kl2)->E.R2.fmap(((k1', k2')) => kl1' -. kl2') + let kl1 = sum(~estimate, ~answer, ~combineFn, ~integrateFn) + let kl2 = sum(~estimate=prior, ~answer, ~combineFn, ~integrateFn) + E.R.merge(kl1, kl2)->E.R2.fmap(((kl1', kl2')) => kl1' -. kl2') } } @@ -69,11 +69,15 @@ module WithScalarAnswer = { minusScaledLogOfQuot(~esti=numerator, ~answ=priorDensityOfAnswer) } } - let score = (~estimate: t, ~answer: t): result => { - let estimatePdf = x => XYShape.XtoY.linear(x, estimate.xyShape) + let score = (~estimate: t, ~answer: t, ~mapper): result => { + let pdf = (shape, ~x) => XYShape.XtoY.linear(x, shape.xyShape) + let estimatePdf = mapper((x => pdf(~x), x => pdf(~x), x => pdf(~x))) score'(~estimatePdf, ~answer) } - let scoreWithPrior = (~estimate: t, ~answer: t, ~prior: t): result => { + let scoreWithPrior = (~estimate: t, ~answer: t, ~prior: t, ~mapper): result< + float, + Operation.Error.t, + > => { let estimatePdf = x => XYShape.XtoY.linear(x, estimate.xyShape) let priorPdf = x => XYShape.XtoY.linear(x, prior.xyShape) scoreWithPrior'(~estimatePdf, ~answer, ~priorPdf) @@ -100,7 +104,7 @@ module TwoScalars = { } } -let logScore = (args: scoreArgs, ~integrateFn): result => +let logScore = (args: scoreArgs, ~combineFn, ~integrateFn): result => switch args { | DistEstimateDistAnswer({estimate, answer, prior: None}) => WithDistAnswer.sum(~estimate, ~answer, ~integrateFn)