From 584c639d01fe843a5d5ae912e56f7f645bdb8c39 Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Mon, 20 Jun 2022 08:28:30 -0400 Subject: [PATCH] some of the comments --- .../Distributions/DistributionOperation.res | 2 +- .../Distributions/DistributionOperation.resi | 8 +-- .../Distributions/DistributionTypes.res | 20 +++--- .../rescript/Distributions/GenericDist.res | 62 ++++++++----------- .../rescript/Distributions/GenericDist.resi | 6 +- .../PointSetDist/PointSetDist_Scoring.res | 13 ++-- .../ReducerInterface_GenericDistribution.res | 7 +-- 7 files changed, 55 insertions(+), 63 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.res b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.res index b1d52319..8190a057 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.res +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.res @@ -146,7 +146,7 @@ let rec run = (~env, functionCallInfo: functionCallInfo): outputType => { } | ToDist(Normalize) => dist->GenericDist.normalize->Dist | ToScore(LogScore(answer, prior)) => - GenericDist.Score.logScore(~estimate=Score_Dist(dist), ~answer, ~prior) + GenericDist.Score.logScore(~estimate=GDist(dist), ~answer, ~prior) ->E.R2.fmap(s => Float(s)) ->OutputLocal.fromResult | ToBool(IsNormalized) => dist->GenericDist.isNormalized->Bool diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.resi b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.resi index eaad6bb8..e2d0290b 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.resi +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation.resi @@ -68,7 +68,7 @@ module Constructors: { ~env: env, genericDist, genericDist, - DistributionTypes.DistributionOperation.scoreDistOrScalar, + DistributionTypes.DistributionOperation.genericDistOrScalar, ) => result @genType let distEstimateScalarAnswer: (~env: env, genericDist, float) => result @@ -77,7 +77,7 @@ module Constructors: { ~env: env, genericDist, float, - DistributionTypes.DistributionOperation.scoreDistOrScalar, + DistributionTypes.DistributionOperation.genericDistOrScalar, ) => result @genType let scalarEstimateDistAnswer: (~env: env, float, genericDist) => result @@ -86,7 +86,7 @@ module Constructors: { ~env: env, float, genericDist, - DistributionTypes.DistributionOperation.scoreDistOrScalar, + DistributionTypes.DistributionOperation.genericDistOrScalar, ) => result @genType let scalarEstimateScalarAnswer: (~env: env, float, float) => result @@ -95,7 +95,7 @@ module Constructors: { ~env: env, float, float, - DistributionTypes.DistributionOperation.scoreDistOrScalar, + DistributionTypes.DistributionOperation.genericDistOrScalar, ) => result } @genType diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res b/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res index 71c3ed02..20b29b38 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res @@ -92,9 +92,9 @@ module DistributionOperation = { | ToString | ToSparkline(int) - type scoreDistOrScalar = Score_Dist(genericDist) | Score_Scalar(float) + type genericDistOrScalar = GDist(genericDist) | GScalar(float) - type toScore = LogScore(scoreDistOrScalar, option) + type toScore = LogScore(genericDistOrScalar, option) type fromDist = | ToFloat(toFloat) @@ -165,35 +165,35 @@ module Constructors = { let inspect = (dist): t => FromDist(ToDist(Inspect), dist) module LogScore = { let distEstimateDistAnswer = (estimate, answer): t => FromDist( - ToScore(LogScore(Score_Dist(answer), None)), + ToScore(LogScore(GDist(answer), None)), estimate, ) let distEstimateDistAnswerWithPrior = (estimate, answer, prior): t => FromDist( - ToScore(LogScore(Score_Dist(answer), Some(prior))), + ToScore(LogScore(GDist(answer), Some(prior))), estimate, ) let distEstimateScalarAnswer = (estimate, answer): t => FromDist( - ToScore(LogScore(Score_Scalar(answer), None)), + ToScore(LogScore(GScalar(answer), None)), estimate, ) let distEstimateScalarAnswerWithPrior = (estimate, answer, prior): t => FromDist( - ToScore(LogScore(Score_Scalar(answer), Some(prior))), + ToScore(LogScore(GScalar(answer), Some(prior))), estimate, ) let scalarEstimateDistAnswer = (estimate, answer): t => FromFloat( - ToScore(LogScore(Score_Dist(answer), None)), + ToScore(LogScore(GDist(answer), None)), estimate, ) let scalarEstimateDistAnswerWithPrior = (estimate, answer, prior): t => FromFloat( - ToScore(LogScore(Score_Dist(answer), Some(prior))), + ToScore(LogScore(GDist(answer), Some(prior))), estimate, ) let scalarEstimateScalarAnswer = (estimate, answer): t => FromFloat( - ToScore(LogScore(Score_Scalar(answer), None)), + ToScore(LogScore(GScalar(answer), None)), estimate, ) let scalarEstimateScalarAnswerWithPrior = (estimate, answer, prior): t => FromFloat( - ToScore(LogScore(Score_Scalar(answer), Some(prior))), + ToScore(LogScore(GScalar(answer), Some(prior))), estimate, ) } diff --git a/packages/squiggle-lang/src/rescript/Distributions/GenericDist.res b/packages/squiggle-lang/src/rescript/Distributions/GenericDist.res index 3dd9981a..4d54c62f 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist.res @@ -120,72 +120,64 @@ let toPointSet = ( } module Score = { - type scoreDistOrScalar = DistributionTypes.DistributionOperation.scoreDistOrScalar + type genericDistOrScalar = DistributionTypes.DistributionOperation.genericDistOrScalar + type psDistOrScalar = PSDist(PointSetTypes.pointSetDist) | PSScalar(float) - type pointSet_ScoreDistOrScalar = D(PointSetTypes.pointSetDist) | S(float) let argsMake = ( - ~esti: scoreDistOrScalar, - ~answ: scoreDistOrScalar, - ~prior: option, + ~esti: genericDistOrScalar, + ~answ: genericDistOrScalar, + ~prior: option, ): result => { let toPointSetFn = toPointSet( ~xyPointLength=MagicNumbers.Environment.defaultXYPointLength, ~sampleCount=MagicNumbers.Environment.defaultSampleCount, ~xSelection=#ByWeight, ) - let prior': option> = switch prior { + let twoDists = PointSetDist_Scoring.twoGenericDistsToTwoPointSetDists + let prior': option> = switch prior { | None => None - | Some(Score_Dist(d)) => toPointSetFn(d, ())->E.R.bind(x => x->D->Ok)->Some - | Some(Score_Scalar(s)) => s->S->Ok->Some + | Some(GDist(d)) => toPointSetFn(d, ())->E.R2.fmap(x => x->PSDist)->Some + | Some(GScalar(s)) => s->PSScalar->Ok->Some } - let twoDists = (esti': t, answ': t): result< - (PointSetTypes.pointSetDist, PointSetTypes.pointSetDist), - error, - > => E.R.merge(toPointSetFn(esti', ()), toPointSetFn(answ', ())) switch (esti, answ, prior') { - | (Score_Dist(esti'), Score_Dist(answ'), None) => - twoDists(esti', answ')->E.R.bind(((esti'', answ'')) => + | (GDist(esti'), GDist(answ'), None) => + twoDists(~toPointSetFn, esti', answ')->E.R2.fmap(((esti'', answ'')) => {estimate: esti'', answer: answ'', prior: None} ->PointSetDist_Scoring.DistEstimateDistAnswer - ->Ok ) - | (Score_Dist(esti'), Score_Dist(answ'), Some(Ok(D(prior'')))) => - twoDists(esti', answ')->E.R.bind(((esti'', answ'')) => + | (GDist(esti'), GDist(answ'), Some(Ok(PSDist(prior'')))) => + twoDists(~toPointSetFn, esti', answ')->E.R2.fmap(((esti'', answ'')) => {estimate: esti'', answer: answ'', prior: Some(prior'')} ->PointSetDist_Scoring.DistEstimateDistAnswer - ->Ok ) - | (Score_Dist(_), _, Some(Ok(S(_)))) => DistributionTypes.Unreachable->Error - | (Score_Dist(esti'), Score_Scalar(answ'), None) => + | (GDist(_), _, Some(Ok(PSScalar(_)))) => DistributionTypes.Unreachable->Error + | (GDist(esti'), GScalar(answ'), None) => toPointSetFn(esti', ())->E.R.bind(esti'' => {estimate: esti'', answer: answ', prior: None} ->PointSetDist_Scoring.DistEstimateScalarAnswer ->Ok ) - | (Score_Dist(esti'), Score_Scalar(answ'), Some(Ok(D(prior'')))) => - toPointSetFn(esti', ())->E.R.bind(esti'' => + | (GDist(esti'), GScalar(answ'), Some(Ok(PSDist(prior'')))) => + toPointSetFn(esti', ())->E.R2.fmap(esti'' => {estimate: esti'', answer: answ', prior: Some(prior'')} ->PointSetDist_Scoring.DistEstimateScalarAnswer - ->Ok ) - | (Score_Scalar(esti'), Score_Dist(answ'), None) => - toPointSetFn(answ', ())->E.R.bind(answ'' => + | (GScalar(esti'), GDist(answ'), None) => + toPointSetFn(answ', ())->E.R2.fmap(answ'' => {estimate: esti', answer: answ'', prior: None} ->PointSetDist_Scoring.ScalarEstimateDistAnswer - ->Ok ) - | (Score_Scalar(esti'), Score_Dist(answ'), Some(Ok(S(prior'')))) => - toPointSetFn(answ', ())->E.R.bind(answ'' => + | (GScalar(esti'), GDist(answ'), Some(Ok(PSScalar(prior'')))) => + toPointSetFn(answ', ())->E.R2.fmap(answ'' => {estimate: esti', answer: answ'', prior: Some(prior'')} ->PointSetDist_Scoring.ScalarEstimateDistAnswer - ->Ok ) - | (Score_Scalar(_), _, Some(Ok(D(_)))) => DistributionTypes.Unreachable->Error - | (Score_Scalar(esti'), Score_Scalar(answ'), None) => + | (GScalar(_), _, Some(Ok(PSDist(_)))) => DistributionTypes.Unreachable->Error + | (GScalar(esti'), GScalar(answ'), None) => {estimate: esti', answer: answ', prior: None} ->PointSetDist_Scoring.ScalarEstimateScalarAnswer ->Ok - | (Score_Scalar(esti'), Score_Scalar(answ'), Some(Ok(S(prior'')))) => + | (GScalar(esti'), GScalar(answ'), Some(Ok(PSScalar(prior'')))) => {estimate: esti', answer: answ', prior: prior''->Some} ->PointSetDist_Scoring.ScalarEstimateScalarAnswer ->Ok @@ -194,9 +186,9 @@ module Score = { } let logScore = ( - ~estimate: scoreDistOrScalar, - ~answer: scoreDistOrScalar, - ~prior: option, + ~estimate: genericDistOrScalar, + ~answer: genericDistOrScalar, + ~prior: option, ): result => argsMake(~esti=estimate, ~answ=answer, ~prior)->E.R.bind(x => x->PointSetDist.logScore->E.R2.errMap(y => DistributionTypes.OperationError(y)) diff --git a/packages/squiggle-lang/src/rescript/Distributions/GenericDist.resi b/packages/squiggle-lang/src/rescript/Distributions/GenericDist.resi index 9e00da14..432ae847 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist.resi +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist.resi @@ -26,9 +26,9 @@ let toFloatOperation: ( module Score: { let logScore: ( - ~estimate: DistributionTypes.DistributionOperation.scoreDistOrScalar, - ~answer: DistributionTypes.DistributionOperation.scoreDistOrScalar, - ~prior: option, + ~estimate: DistributionTypes.DistributionOperation.genericDistOrScalar, + ~answer: DistributionTypes.DistributionOperation.genericDistOrScalar, + ~prior: option, ) => result } 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 a228fcdc..20bce7a7 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist_Scoring.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist_Scoring.res @@ -9,7 +9,7 @@ type scoreArgs = | ScalarEstimateScalarAnswer(abstractScoreArgs) let logFn = Js.Math.log // base e -let minusScaledLogOfQuot = (~esti, ~answ): result => { +let minusScaledLogOfQuotient = (~esti, ~answ): result => { let quot = esti /. answ quot < 0.0 ? Error(Operation.ComplexNumberError) : Ok(-.answ *. logFn(quot)) } @@ -26,7 +26,7 @@ module WithDistAnswer = { } else if estimateElement == 0.0 { Ok(infinity) } else { - minusScaledLogOfQuot(~esti=estimateElement, ~answ=answerElement) + minusScaledLogOfQuotient(~esti=estimateElement, ~answ=answerElement) } let rec sum = (~estimate: t, ~answer: t, ~combineFn, ~integrateFn, ~toMixedFn): result< @@ -109,7 +109,7 @@ module WithScalarAnswer = { } else if numerator == 0.0 || priorDensityOfAnswer == 0.0 { infinity->Ok } else { - minusScaledLogOfQuot(~esti=numerator, ~answ=priorDensityOfAnswer) + minusScaledLogOfQuotient(~esti=numerator, ~answ=priorDensityOfAnswer) } } @@ -151,7 +151,7 @@ module TwoScalars = { } else if estimate == 0.0 { infinity->Ok } else { - minusScaledLogOfQuot(~esti=estimate, ~answ=answer) + minusScaledLogOfQuotient(~esti=estimate, ~answ=answer) } let scoreWithPrior = (~estimate: float, ~answer: float, ~prior: float) => @@ -160,10 +160,13 @@ module TwoScalars = { } else if estimate == 0.0 || prior == 0.0 { infinity->Ok } else { - minusScaledLogOfQuot(~esti=estimate /. prior, ~answ=answer) + minusScaledLogOfQuotient(~esti=estimate /. prior, ~answ=answer) } } +let twoGenericDistsToTwoPointSetDists = (~toPointSetFn, estimate, answer): result<(t, t), 'e> => + E.R.merge(toPointSetFn(estimate, ()), toPointSetFn(answer, ())) + let logScore = (args: scoreArgs, ~combineFn, ~integrateFn, ~toMixedFn): result< float, Operation.Error.t, diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index 98e4842d..2959f924 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -214,15 +214,12 @@ let dispatchToGenericOutput = ( | ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist, ~env) | ("klDivergence", [EvDistribution(estimate), EvDistribution(answer)]) => Some( - DistributionOperation.run( - FromDist(ToScore(LogScore(Score_Dist(answer), None)), estimate), - ~env, - ), + DistributionOperation.run(FromDist(ToScore(LogScore(GDist(answer), None)), estimate), ~env), ) | ("klDivergence", [EvDistribution(estimate), EvDistribution(answer), EvDistribution(prior)]) => Some( DistributionOperation.run( - FromDist(ToScore(LogScore(Score_Dist(answer), Some(Score_Dist(prior)))), estimate), + FromDist(ToScore(LogScore(GDist(answer), Some(GDist(prior)))), estimate), ~env, ), )