(rebase): (rebase): (rebase): **refactor**: rm duplicated genericDist and error declarations

This commit is contained in:
Quinn Dougherty 2022-04-11 14:00:56 -04:00
parent c6e78a1fd4
commit 6a4d615d3d
9 changed files with 89 additions and 23 deletions

View File

@ -0,0 +1,65 @@
open Jest
open Expect
open TestHelpers
//let env: DistributionOperation.env = {
// sampleCount: 100,
// xyPointLength: 100,
//}
let {
normalDist5,
normalDist10,
normalDist20,
normalDist, // mean=5; stdev=2
uniformDist, // low=9; high=10
betaDist, // alpha=2; beta=5
lognormalDist, // mu=0; sigma=1
cauchyDist, // local=1; scale=1
triangularDist, // low=1; medium=2; high=3;
exponentialDist, // rate=2
} = module(GenericDist_Fixtures)
// let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
// let {run} = module(DistributionOperation)
let {
algebraicAdd,
algebraicMultiply,
algebraicDivide,
algebraicSubtract,
algebraicLogarithm,
algebraicPower
} = module(DistributionOperation.Constructors)
// let toExt: option<'a> => 'a = E.O.toExt(
// "Should be impossible to reach (This error is in test file)",
// )
let algebraicAdd = algebraicAdd(~env)
let algebraicMultiply = algebraicMultiply(~env)
let algebraicDivide = algebraicDivide(~env)
let algebraicSubtract = algebraicSubtract(~env)
let algebraicLogarithm = algebraicLogarithm(~env)
let algebraicPower = algebraicPower(~env)
describe("Addition of distributions", () => {
describe("mean", () => {
test("normal(mean=5) + normal(mean=20)", () => {
normalDist5
-> algebraicAdd(normalDist20)
-> E.R2.fmap(GenericDist_Types.Constructors.UsingDists.mean)
-> E.R2.fmap(run)
-> E.R2.fmap(toFloat)
-> E.R.toExn
-> expect
-> toBe(Some(2.5e1))
// -> toBeSoCloseTo(2.5e1, ~digits=7)
})
})
// testAll("(normal(mean=5) + normal(mean=5)).pdf", list{1e0, 3e0, 5e0, 7e0}, x => {
// let additionValue = algebraicAdd(normalDist5, normalDist5)
// let targetValue = run(FromDist(ToFloat(#Pdf(x)), normalDist10)) -> unpackFloat
// E.R.fmap(run(FromDist(ToFloat(#Pdf(x)), additionValue)))
// -> unpackFloat
// -> expect
// -> toBeSoCloseTo(2.5e1, ~digits=7)
// })
})

View File

@ -18,11 +18,9 @@ let {
triangularDist,
exponentialDist,
} = module(GenericDist_Fixtures)
let mkNormal = (mean, stdev) => GenericDist_Types.Symbolic(#Normal({mean: mean, stdev: stdev}))
let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output)
let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
let {run} = module(DistributionOperation)
let {fmap} = module(DistributionOperation.Output)
let run = run(~env)
let outputMap = fmap(~env)
let toExt: option<'a> => 'a = E.O.toExt(

View File

@ -11,3 +11,4 @@ let triangularDist: GenericDist_Types.genericDist = Symbolic(
)
let exponentialDist: GenericDist_Types.genericDist = Symbolic(#Exponential({rate: 2.0}))
let uniformDist: GenericDist_Types.genericDist = Symbolic(#Uniform({low: 9.0, high: 10.0}))
let floatDist: GenericDist_Types.genericDist = Symbolic(#Float(1e1))

View File

@ -3,7 +3,7 @@ open Expect
open TestHelpers
// TODO: use Normal.make (but preferably after teh new validation dispatch is in)
let mkNormal = (mean, stdev) => GenericDist_Types.Symbolic(#Normal({mean: mean, stdev: stdev}))
let mkNormal = (mean, stdev) => DistributionTypes.Symbolic(#Normal({mean: mean, stdev: stdev}))
describe("(Symbolic) normalize", () => {
testAll("has no impact on normal distributions", list{-1e8, -1e-2, 0.0, 1e-4, 1e16}, mean => {

View File

@ -1,6 +1,6 @@
type functionCallInfo = GenericDist_Types.Operation.genericFunctionCallInfo
type genericDist = GenericDist_Types.genericDist
type error = GenericDist_Types.error
type genericDist = DistributionTypes.genericDist
type error = DistributionTypes.error
// TODO: It could be great to use a cache for some calculations (basically, do memoization). Also, better analytics/tracking could go a long way.

View File

@ -1,8 +1,10 @@
@genType
type genericDist =
| PointSet(PointSetTypes.pointSetDist)
| SampleSet(array<float>)
| SampleSet(SampleSetDist.t)
| Symbolic(SymbolicDistTypes.symbolicDist)
@genType
type error =
| NotYetImplemented
| Unreachable

View File

@ -1,6 +1,6 @@
//TODO: multimodal, add interface, test somehow, track performance, refactor sampleSet, refactor ASTEvaluator.res.
type t = GenericDist_Types.genericDist
type error = GenericDist_Types.error
type t = DistributionTypes.genericDist
type error = DistributionTypes.error
type toPointSetFn = t => result<PointSetTypes.pointSetDist, error>
type toSampleSetFn = t => result<SampleSetDist.t, error>
type scaleMultiplyFn = (t, float) => result<t, error>
@ -115,7 +115,7 @@ module Truncate = {
| Some(r) => Ok(r)
| None =>
toPointSetFn(t)->E.R2.fmap(t => {
GenericDist_Types.PointSet(PointSetDist.T.truncate(leftCutoff, rightCutoff, t))
DistributionTypes.PointSet(PointSetDist.T.truncate(leftCutoff, rightCutoff, t))
})
}
}
@ -168,7 +168,7 @@ module AlgebraicCombination = {
->E.R.bind(((t1, t2)) => {
SampleSetDist.map2(~fn, ~t1, ~t2)->GenericDist_Types.Error.resultStringToResultError
})
->E.R2.fmap(r => GenericDist_Types.SampleSet(r))
->E.R2.fmap(r => DistributionTypes.SampleSet(r))
}
//I'm (Ozzie) really just guessing here, very little idea what's best
@ -206,7 +206,7 @@ module AlgebraicCombination = {
arithmeticOperation,
t1,
t2,
)->E.R2.fmap(r => GenericDist_Types.PointSet(r))
)->E.R2.fmap(r => DistributionTypes.PointSet(r))
}
}
}
@ -229,7 +229,7 @@ let pointwiseCombination = (
t2,
)
)
->E.R2.fmap(r => GenericDist_Types.PointSet(r))
->E.R2.fmap(r => DistributionTypes.PointSet(r))
}
let pointwiseCombinationFloat = (
@ -239,7 +239,7 @@ let pointwiseCombinationFloat = (
~float: float,
): result<t, error> => {
let m = switch arithmeticOperation {
| #Add | #Subtract => Error(GenericDist_Types.DistributionVerticalShiftIsInvalid)
| #Add | #Subtract => Error(DistributionTypes.DistributionVerticalShiftIsInvalid)
| (#Multiply | #Divide | #Power | #Logarithm) as arithmeticOperation =>
toPointSetFn(t)->E.R2.fmap(t => {
//TODO: Move to PointSet codebase
@ -254,7 +254,7 @@ let pointwiseCombinationFloat = (
)
})
}
m->E.R2.fmap(r => GenericDist_Types.PointSet(r))
m->E.R2.fmap(r => DistributionTypes.PointSet(r))
}
//Note: The result should always cumulatively sum to 1. This would be good to test.
@ -265,7 +265,7 @@ let mixture = (
~pointwiseAddFn: pointwiseAddFn,
) => {
if E.A.length(values) == 0 {
Error(GenericDist_Types.Other("Mixture error: mixture must have at least 1 element"))
Error(DistributionTypes.Other("Mixture error: mixture must have at least 1 element"))
} else {
let totalWeight = values->E.A2.fmap(E.Tuple2.second)->E.A.Floats.sum
let properlyWeightedValues =

View File

@ -1,15 +1,15 @@
type genericDist =
| PointSet(PointSetTypes.pointSetDist)
| SampleSet(SampleSetDist.t)
| Symbolic(SymbolicDistTypes.symbolicDist)
type genericDist = DistributionTypes.genericDist
@genType
<<<<<<< HEAD
type error =
| NotYetImplemented
| Unreachable
| DistributionVerticalShiftIsInvalid
| ArgumentError(string)
| Other(string)
=======
type error = DistributionTypes.error
>>>>>>> eff1810 ((rebase): (rebase): **refactor**: rm duplicated `genericDist` and `error` declarations)
@genType
module Error = {

View File

@ -11,10 +11,10 @@ The below few seem to work fine. In the future there's definitely more work to d
type samplingParams = DistributionOperation.env
@genType
type genericDist = GenericDist_Types.genericDist
type genericDist = DistributionTypes.genericDist
@genType
type distributionError = GenericDist_Types.error
type distributionError = DistributionTypes.error
@genType
type resultDist = result<genericDist, distributionError>