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
| 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

View File

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

View File

@ -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<scoreDistOrScalar>)
type toScore = LogScore(genericDistOrScalar, option<genericDistOrScalar>)
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,
)
}

View File

@ -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<scoreDistOrScalar>,
~esti: genericDistOrScalar,
~answ: genericDistOrScalar,
~prior: option<genericDistOrScalar>,
): result<PointSetDist_Scoring.scoreArgs, error> => {
let toPointSetFn = toPointSet(
~xyPointLength=MagicNumbers.Environment.defaultXYPointLength,
~sampleCount=MagicNumbers.Environment.defaultSampleCount,
~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
| 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<scoreDistOrScalar>,
~estimate: genericDistOrScalar,
~answer: genericDistOrScalar,
~prior: option<genericDistOrScalar>,
): result<float, error> =>
argsMake(~esti=estimate, ~answ=answer, ~prior)->E.R.bind(x =>
x->PointSetDist.logScore->E.R2.errMap(y => DistributionTypes.OperationError(y))

View File

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

View File

@ -9,7 +9,7 @@ type scoreArgs =
| ScalarEstimateScalarAnswer(abstractScoreArgs<scalar, scalar>)
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
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,

View File

@ -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,
),
)