some of the comments

This commit is contained in:
Quinn Dougherty 2022-06-20 08:28:30 -04:00
parent 6a9179d4b8
commit 584c639d01
7 changed files with 55 additions and 63 deletions

View File

@ -146,7 +146,7 @@ let rec run = (~env, functionCallInfo: functionCallInfo): outputType => {
} }
| ToDist(Normalize) => dist->GenericDist.normalize->Dist | ToDist(Normalize) => dist->GenericDist.normalize->Dist
| ToScore(LogScore(answer, prior)) => | 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)) ->E.R2.fmap(s => Float(s))
->OutputLocal.fromResult ->OutputLocal.fromResult
| ToBool(IsNormalized) => dist->GenericDist.isNormalized->Bool | ToBool(IsNormalized) => dist->GenericDist.isNormalized->Bool

View File

@ -68,7 +68,7 @@ module Constructors: {
~env: env, ~env: env,
genericDist, genericDist,
genericDist, genericDist,
DistributionTypes.DistributionOperation.scoreDistOrScalar, DistributionTypes.DistributionOperation.genericDistOrScalar,
) => result<float, error> ) => result<float, error>
@genType @genType
let distEstimateScalarAnswer: (~env: env, genericDist, float) => result<float, error> let distEstimateScalarAnswer: (~env: env, genericDist, float) => result<float, error>
@ -77,7 +77,7 @@ module Constructors: {
~env: env, ~env: env,
genericDist, genericDist,
float, float,
DistributionTypes.DistributionOperation.scoreDistOrScalar, DistributionTypes.DistributionOperation.genericDistOrScalar,
) => result<float, error> ) => result<float, error>
@genType @genType
let scalarEstimateDistAnswer: (~env: env, float, genericDist) => result<float, error> let scalarEstimateDistAnswer: (~env: env, float, genericDist) => result<float, error>
@ -86,7 +86,7 @@ module Constructors: {
~env: env, ~env: env,
float, float,
genericDist, genericDist,
DistributionTypes.DistributionOperation.scoreDistOrScalar, DistributionTypes.DistributionOperation.genericDistOrScalar,
) => result<float, error> ) => result<float, error>
@genType @genType
let scalarEstimateScalarAnswer: (~env: env, float, float) => result<float, error> let scalarEstimateScalarAnswer: (~env: env, float, float) => result<float, error>
@ -95,7 +95,7 @@ module Constructors: {
~env: env, ~env: env,
float, float,
float, float,
DistributionTypes.DistributionOperation.scoreDistOrScalar, DistributionTypes.DistributionOperation.genericDistOrScalar,
) => result<float, error> ) => result<float, error>
} }
@genType @genType

View File

@ -92,9 +92,9 @@ module DistributionOperation = {
| ToString | ToString
| ToSparkline(int) | ToSparkline(int)
type scoreDistOrScalar = Score_Dist(genericDist) | Score_Scalar(float) type genericDistOrScalar = GDist(genericDist) | GScalar(float)
type toScore = LogScore(scoreDistOrScalar, option<scoreDistOrScalar>) type toScore = LogScore(genericDistOrScalar, option<genericDistOrScalar>)
type fromDist = type fromDist =
| ToFloat(toFloat) | ToFloat(toFloat)
@ -165,35 +165,35 @@ module Constructors = {
let inspect = (dist): t => FromDist(ToDist(Inspect), dist) let inspect = (dist): t => FromDist(ToDist(Inspect), dist)
module LogScore = { module LogScore = {
let distEstimateDistAnswer = (estimate, answer): t => FromDist( let distEstimateDistAnswer = (estimate, answer): t => FromDist(
ToScore(LogScore(Score_Dist(answer), None)), ToScore(LogScore(GDist(answer), None)),
estimate, estimate,
) )
let distEstimateDistAnswerWithPrior = (estimate, answer, prior): t => FromDist( let distEstimateDistAnswerWithPrior = (estimate, answer, prior): t => FromDist(
ToScore(LogScore(Score_Dist(answer), Some(prior))), ToScore(LogScore(GDist(answer), Some(prior))),
estimate, estimate,
) )
let distEstimateScalarAnswer = (estimate, answer): t => FromDist( let distEstimateScalarAnswer = (estimate, answer): t => FromDist(
ToScore(LogScore(Score_Scalar(answer), None)), ToScore(LogScore(GScalar(answer), None)),
estimate, estimate,
) )
let distEstimateScalarAnswerWithPrior = (estimate, answer, prior): t => FromDist( let distEstimateScalarAnswerWithPrior = (estimate, answer, prior): t => FromDist(
ToScore(LogScore(Score_Scalar(answer), Some(prior))), ToScore(LogScore(GScalar(answer), Some(prior))),
estimate, estimate,
) )
let scalarEstimateDistAnswer = (estimate, answer): t => FromFloat( let scalarEstimateDistAnswer = (estimate, answer): t => FromFloat(
ToScore(LogScore(Score_Dist(answer), None)), ToScore(LogScore(GDist(answer), None)),
estimate, estimate,
) )
let scalarEstimateDistAnswerWithPrior = (estimate, answer, prior): t => FromFloat( let scalarEstimateDistAnswerWithPrior = (estimate, answer, prior): t => FromFloat(
ToScore(LogScore(Score_Dist(answer), Some(prior))), ToScore(LogScore(GDist(answer), Some(prior))),
estimate, estimate,
) )
let scalarEstimateScalarAnswer = (estimate, answer): t => FromFloat( let scalarEstimateScalarAnswer = (estimate, answer): t => FromFloat(
ToScore(LogScore(Score_Scalar(answer), None)), ToScore(LogScore(GScalar(answer), None)),
estimate, estimate,
) )
let scalarEstimateScalarAnswerWithPrior = (estimate, answer, prior): t => FromFloat( let scalarEstimateScalarAnswerWithPrior = (estimate, answer, prior): t => FromFloat(
ToScore(LogScore(Score_Scalar(answer), Some(prior))), ToScore(LogScore(GScalar(answer), Some(prior))),
estimate, estimate,
) )
} }

View File

@ -120,72 +120,64 @@ let toPointSet = (
} }
module Score = { 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 = ( let argsMake = (
~esti: scoreDistOrScalar, ~esti: genericDistOrScalar,
~answ: scoreDistOrScalar, ~answ: genericDistOrScalar,
~prior: option<scoreDistOrScalar>, ~prior: option<genericDistOrScalar>,
): result<PointSetDist_Scoring.scoreArgs, error> => { ): result<PointSetDist_Scoring.scoreArgs, error> => {
let toPointSetFn = toPointSet( let toPointSetFn = toPointSet(
~xyPointLength=MagicNumbers.Environment.defaultXYPointLength, ~xyPointLength=MagicNumbers.Environment.defaultXYPointLength,
~sampleCount=MagicNumbers.Environment.defaultSampleCount, ~sampleCount=MagicNumbers.Environment.defaultSampleCount,
~xSelection=#ByWeight, ~xSelection=#ByWeight,
) )
let prior': option<result<pointSet_ScoreDistOrScalar, error>> = switch prior { let twoDists = PointSetDist_Scoring.twoGenericDistsToTwoPointSetDists
let prior': option<result<psDistOrScalar, error>> = switch prior {
| None => None | None => None
| Some(Score_Dist(d)) => toPointSetFn(d, ())->E.R.bind(x => x->D->Ok)->Some | Some(GDist(d)) => toPointSetFn(d, ())->E.R2.fmap(x => x->PSDist)->Some
| Some(Score_Scalar(s)) => s->S->Ok->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') { switch (esti, answ, prior') {
| (Score_Dist(esti'), Score_Dist(answ'), None) => | (GDist(esti'), GDist(answ'), None) =>
twoDists(esti', answ')->E.R.bind(((esti'', answ'')) => twoDists(~toPointSetFn, esti', answ')->E.R2.fmap(((esti'', answ'')) =>
{estimate: esti'', answer: answ'', prior: None} {estimate: esti'', answer: answ'', prior: None}
->PointSetDist_Scoring.DistEstimateDistAnswer ->PointSetDist_Scoring.DistEstimateDistAnswer
->Ok
) )
| (Score_Dist(esti'), Score_Dist(answ'), Some(Ok(D(prior'')))) => | (GDist(esti'), GDist(answ'), Some(Ok(PSDist(prior'')))) =>
twoDists(esti', answ')->E.R.bind(((esti'', answ'')) => twoDists(~toPointSetFn, esti', answ')->E.R2.fmap(((esti'', answ'')) =>
{estimate: esti'', answer: answ'', prior: Some(prior'')} {estimate: esti'', answer: answ'', prior: Some(prior'')}
->PointSetDist_Scoring.DistEstimateDistAnswer ->PointSetDist_Scoring.DistEstimateDistAnswer
->Ok
) )
| (Score_Dist(_), _, Some(Ok(S(_)))) => DistributionTypes.Unreachable->Error | (GDist(_), _, Some(Ok(PSScalar(_)))) => DistributionTypes.Unreachable->Error
| (Score_Dist(esti'), Score_Scalar(answ'), None) => | (GDist(esti'), GScalar(answ'), None) =>
toPointSetFn(esti', ())->E.R.bind(esti'' => toPointSetFn(esti', ())->E.R.bind(esti'' =>
{estimate: esti'', answer: answ', prior: None} {estimate: esti'', answer: answ', prior: None}
->PointSetDist_Scoring.DistEstimateScalarAnswer ->PointSetDist_Scoring.DistEstimateScalarAnswer
->Ok ->Ok
) )
| (Score_Dist(esti'), Score_Scalar(answ'), Some(Ok(D(prior'')))) => | (GDist(esti'), GScalar(answ'), Some(Ok(PSDist(prior'')))) =>
toPointSetFn(esti', ())->E.R.bind(esti'' => toPointSetFn(esti', ())->E.R2.fmap(esti'' =>
{estimate: esti'', answer: answ', prior: Some(prior'')} {estimate: esti'', answer: answ', prior: Some(prior'')}
->PointSetDist_Scoring.DistEstimateScalarAnswer ->PointSetDist_Scoring.DistEstimateScalarAnswer
->Ok
) )
| (Score_Scalar(esti'), Score_Dist(answ'), None) => | (GScalar(esti'), GDist(answ'), None) =>
toPointSetFn(answ', ())->E.R.bind(answ'' => toPointSetFn(answ', ())->E.R2.fmap(answ'' =>
{estimate: esti', answer: answ'', prior: None} {estimate: esti', answer: answ'', prior: None}
->PointSetDist_Scoring.ScalarEstimateDistAnswer ->PointSetDist_Scoring.ScalarEstimateDistAnswer
->Ok
) )
| (Score_Scalar(esti'), Score_Dist(answ'), Some(Ok(S(prior'')))) => | (GScalar(esti'), GDist(answ'), Some(Ok(PSScalar(prior'')))) =>
toPointSetFn(answ', ())->E.R.bind(answ'' => toPointSetFn(answ', ())->E.R2.fmap(answ'' =>
{estimate: esti', answer: answ'', prior: Some(prior'')} {estimate: esti', answer: answ'', prior: Some(prior'')}
->PointSetDist_Scoring.ScalarEstimateDistAnswer ->PointSetDist_Scoring.ScalarEstimateDistAnswer
->Ok
) )
| (Score_Scalar(_), _, Some(Ok(D(_)))) => DistributionTypes.Unreachable->Error | (GScalar(_), _, Some(Ok(PSDist(_)))) => DistributionTypes.Unreachable->Error
| (Score_Scalar(esti'), Score_Scalar(answ'), None) => | (GScalar(esti'), GScalar(answ'), None) =>
{estimate: esti', answer: answ', prior: None} {estimate: esti', answer: answ', prior: None}
->PointSetDist_Scoring.ScalarEstimateScalarAnswer ->PointSetDist_Scoring.ScalarEstimateScalarAnswer
->Ok ->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} {estimate: esti', answer: answ', prior: prior''->Some}
->PointSetDist_Scoring.ScalarEstimateScalarAnswer ->PointSetDist_Scoring.ScalarEstimateScalarAnswer
->Ok ->Ok
@ -194,9 +186,9 @@ module Score = {
} }
let logScore = ( let logScore = (
~estimate: scoreDistOrScalar, ~estimate: genericDistOrScalar,
~answer: scoreDistOrScalar, ~answer: genericDistOrScalar,
~prior: option<scoreDistOrScalar>, ~prior: option<genericDistOrScalar>,
): result<float, error> => ): result<float, error> =>
argsMake(~esti=estimate, ~answ=answer, ~prior)->E.R.bind(x => argsMake(~esti=estimate, ~answ=answer, ~prior)->E.R.bind(x =>
x->PointSetDist.logScore->E.R2.errMap(y => DistributionTypes.OperationError(y)) x->PointSetDist.logScore->E.R2.errMap(y => DistributionTypes.OperationError(y))

View File

@ -26,9 +26,9 @@ let toFloatOperation: (
module Score: { module Score: {
let logScore: ( let logScore: (
~estimate: DistributionTypes.DistributionOperation.scoreDistOrScalar, ~estimate: DistributionTypes.DistributionOperation.genericDistOrScalar,
~answer: DistributionTypes.DistributionOperation.scoreDistOrScalar, ~answer: DistributionTypes.DistributionOperation.genericDistOrScalar,
~prior: option<DistributionTypes.DistributionOperation.scoreDistOrScalar>, ~prior: option<DistributionTypes.DistributionOperation.genericDistOrScalar>,
) => result<float, error> ) => result<float, error>
} }

View File

@ -9,7 +9,7 @@ type scoreArgs =
| ScalarEstimateScalarAnswer(abstractScoreArgs<scalar, scalar>) | ScalarEstimateScalarAnswer(abstractScoreArgs<scalar, scalar>)
let logFn = Js.Math.log // base e let logFn = Js.Math.log // base e
let minusScaledLogOfQuot = (~esti, ~answ): result<float, Operation.Error.t> => { let minusScaledLogOfQuotient = (~esti, ~answ): result<float, Operation.Error.t> => {
let quot = esti /. answ let quot = esti /. answ
quot < 0.0 ? Error(Operation.ComplexNumberError) : Ok(-.answ *. logFn(quot)) quot < 0.0 ? Error(Operation.ComplexNumberError) : Ok(-.answ *. logFn(quot))
} }
@ -26,7 +26,7 @@ module WithDistAnswer = {
} else if estimateElement == 0.0 { } else if estimateElement == 0.0 {
Ok(infinity) Ok(infinity)
} else { } else {
minusScaledLogOfQuot(~esti=estimateElement, ~answ=answerElement) minusScaledLogOfQuotient(~esti=estimateElement, ~answ=answerElement)
} }
let rec sum = (~estimate: t, ~answer: t, ~combineFn, ~integrateFn, ~toMixedFn): result< 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 { } else if numerator == 0.0 || priorDensityOfAnswer == 0.0 {
infinity->Ok infinity->Ok
} else { } else {
minusScaledLogOfQuot(~esti=numerator, ~answ=priorDensityOfAnswer) minusScaledLogOfQuotient(~esti=numerator, ~answ=priorDensityOfAnswer)
} }
} }
@ -151,7 +151,7 @@ module TwoScalars = {
} else if estimate == 0.0 { } else if estimate == 0.0 {
infinity->Ok infinity->Ok
} else { } else {
minusScaledLogOfQuot(~esti=estimate, ~answ=answer) minusScaledLogOfQuotient(~esti=estimate, ~answ=answer)
} }
let scoreWithPrior = (~estimate: float, ~answer: float, ~prior: float) => let scoreWithPrior = (~estimate: float, ~answer: float, ~prior: float) =>
@ -160,10 +160,13 @@ module TwoScalars = {
} else if estimate == 0.0 || prior == 0.0 { } else if estimate == 0.0 || prior == 0.0 {
infinity->Ok infinity->Ok
} else { } 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< let logScore = (args: scoreArgs, ~combineFn, ~integrateFn, ~toMixedFn): result<
float, float,
Operation.Error.t, Operation.Error.t,

View File

@ -214,15 +214,12 @@ let dispatchToGenericOutput = (
| ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist, ~env) | ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist, ~env)
| ("klDivergence", [EvDistribution(estimate), EvDistribution(answer)]) => | ("klDivergence", [EvDistribution(estimate), EvDistribution(answer)]) =>
Some( Some(
DistributionOperation.run( DistributionOperation.run(FromDist(ToScore(LogScore(GDist(answer), None)), estimate), ~env),
FromDist(ToScore(LogScore(Score_Dist(answer), None)), estimate),
~env,
),
) )
| ("klDivergence", [EvDistribution(estimate), EvDistribution(answer), EvDistribution(prior)]) => | ("klDivergence", [EvDistribution(estimate), EvDistribution(answer), EvDistribution(prior)]) =>
Some( Some(
DistributionOperation.run( DistributionOperation.run(
FromDist(ToScore(LogScore(Score_Dist(answer), Some(Score_Dist(prior)))), estimate), FromDist(ToScore(LogScore(GDist(answer), Some(GDist(prior)))), estimate),
~env, ~env,
), ),
) )