Responded to two simple CR comments

This commit is contained in:
Ozzie Gooen 2022-03-29 21:28:14 -04:00
parent 2fce3d67e9
commit ffc622fb6d
5 changed files with 23 additions and 20 deletions

View File

@ -59,7 +59,7 @@ describe("toPointSet", () => {
run(#fromDist(#toDist(#toPointSet), #SampleSet([0.0, 1.0, 2.0, 3.0])))->fmap( run(#fromDist(#toDist(#toPointSet), #SampleSet([0.0, 1.0, 2.0, 3.0])))->fmap(
#fromDist(#toFloat(#Mean)), #fromDist(#toFloat(#Mean)),
) )
expect(result)->toEqual(#Error(Other("Converting sampleSet to pointSet failed"))) expect(result)->toEqual(#GenDistError(Other("Converting sampleSet to pointSet failed")))
}) })
test("on sample set", () => { test("on sample set", () => {

View File

@ -207,7 +207,7 @@ let pointwiseCombinationFloat = (
operation: GenericDist_Types.Operation.arithmeticOperation, operation: GenericDist_Types.Operation.arithmeticOperation,
f: float, f: float,
): result<t, error> => { ): result<t, error> => {
switch operation { let m = switch operation {
| #Add | #Subtract => Error(GenericDist_Types.DistributionVerticalShiftIsInvalid) | #Add | #Subtract => Error(GenericDist_Types.DistributionVerticalShiftIsInvalid)
| (#Multiply | #Divide | #Exponentiate | #Log) as operation => | (#Multiply | #Divide | #Exponentiate | #Log) as operation =>
toPointSet(t)->E.R2.fmap(t => { toPointSet(t)->E.R2.fmap(t => {
@ -222,10 +222,12 @@ let pointwiseCombinationFloat = (
t, t,
) )
}) })
}->E.R2.fmap(r => #PointSet(r)) }
m->E.R2.fmap(r => #PointSet(r))
} }
//Note: The result should always cumulatively sum to 1. This would be good to test. //Note: The result should always cumulatively sum to 1. This would be good to test.
//Note: If the inputs are not normalized, this will return poor results. The weights probably refer to the post-normalized forms. It would be good to apply a catch to this.
let mixture = ( let mixture = (
values: array<(t, float)>, values: array<(t, float)>,
scaleMultiply: scaleMultiplyFn, scaleMultiply: scaleMultiplyFn,

View File

@ -1,4 +1,4 @@
type operation = GenericDist_Types.Operation.genericFunctionCall type operation = GenericDist_Types.Operation.genericFunctionCallInfo
type genericDist = GenericDist_Types.genericDist type genericDist = GenericDist_Types.genericDist
type error = GenericDist_Types.error type error = GenericDist_Types.error
@ -11,9 +11,9 @@ type params = {
type outputType = [ type outputType = [
| #Dist(genericDist) | #Dist(genericDist)
| #Error(error)
| #Float(float) | #Float(float)
| #String(string) | #String(string)
| #GenDistError(error)
] ]
module Output = { module Output = {
@ -37,7 +37,7 @@ module Output = {
let toError = (o: outputType) => let toError = (o: outputType) =>
switch o { switch o {
| #Error(d) => Some(d) | #GenDistError(d) => Some(d)
| _ => None | _ => None
} }
} }
@ -45,14 +45,14 @@ module Output = {
let fromResult = (r: result<outputType, error>): outputType => let fromResult = (r: result<outputType, error>): outputType =>
switch r { switch r {
| Ok(o) => o | Ok(o) => o
| Error(e) => #Error(e) | Error(e) => #GenDistError(e)
} }
let outputToDistResult = (b: outputType): result<genericDist, error> => let outputToDistResult = (b: outputType): result<genericDist, error> =>
switch b { switch b {
| #Dist(r) => Ok(r) | #Dist(r) => Ok(r)
| #Error(r) => Error(r) | #GenDistError(r) => Error(r)
| _ => Error(ImpossiblePath) | _ => Error(Unreachable)
} }
let rec run = (extra, fnName: operation): outputType => { let rec run = (extra, fnName: operation): outputType => {
@ -65,16 +65,16 @@ let rec run = (extra, fnName: operation): outputType => {
let toPointSet = r => { let toPointSet = r => {
switch reCall(~fnName=#fromDist(#toDist(#toPointSet), r), ()) { switch reCall(~fnName=#fromDist(#toDist(#toPointSet), r), ()) {
| #Dist(#PointSet(p)) => Ok(p) | #Dist(#PointSet(p)) => Ok(p)
| #Error(r) => Error(r) | #GenDistError(r) => Error(r)
| _ => Error(ImpossiblePath) | _ => Error(Unreachable)
} }
} }
let toSampleSet = r => { let toSampleSet = r => {
switch reCall(~fnName=#fromDist(#toDist(#toSampleSet(sampleCount)), r), ()) { switch reCall(~fnName=#fromDist(#toDist(#toSampleSet(sampleCount)), r), ()) {
| #Dist(#SampleSet(p)) => Ok(p) | #Dist(#SampleSet(p)) => Ok(p)
| #Error(r) => Error(r) | #GenDistError(r) => Error(r)
| _ => Error(ImpossiblePath) | _ => Error(Unreachable)
} }
} }
@ -106,7 +106,7 @@ let rec run = (extra, fnName: operation): outputType => {
dist->GenericDist.toPointSet(xyPointLength)->E.R2.fmap(r => #Dist(#PointSet(r)))->fromResult dist->GenericDist.toPointSet(xyPointLength)->E.R2.fmap(r => #Dist(#PointSet(r)))->fromResult
| #toDist(#toSampleSet(n)) => | #toDist(#toSampleSet(n)) =>
dist->GenericDist.sampleN(n)->E.R2.fmap(r => #Dist(#SampleSet(r)))->fromResult dist->GenericDist.sampleN(n)->E.R2.fmap(r => #Dist(#SampleSet(r)))->fromResult
| #toDistCombination(#Algebraic, _, #Float(_)) => #Error(NotYetImplemented) | #toDistCombination(#Algebraic, _, #Float(_)) => #GenDistError(NotYetImplemented)
| #toDistCombination(#Algebraic, operation, #Dist(dist2)) => | #toDistCombination(#Algebraic, operation, #Dist(dist2)) =>
dist dist
->GenericDist.algebraicCombination(toPointSet, toSampleSet, operation, dist2) ->GenericDist.algebraicCombination(toPointSet, toSampleSet, operation, dist2)
@ -143,7 +143,7 @@ let fmap = (
let newFnCall: result<operation, error> = switch (fn, input) { let newFnCall: result<operation, error> = switch (fn, input) {
| (#fromDist(fromDist), #Dist(o)) => Ok(#fromDist(fromDist, o)) | (#fromDist(fromDist), #Dist(o)) => Ok(#fromDist(fromDist, o))
| (#fromFloat(fromDist), #Float(o)) => Ok(#fromFloat(fromDist, o)) | (#fromFloat(fromDist), #Float(o)) => Ok(#fromFloat(fromDist, o))
| (_, #Error(r)) => Error(r) | (_, #GenDistError(r)) => Error(r)
| (#fromDist(_), _) => Error(Other("Expected dist, got something else")) | (#fromDist(_), _) => Error(Other("Expected dist, got something else"))
| (#fromFloat(_), _) => Error(Other("Expected float, got something else")) | (#fromFloat(_), _) => Error(Other("Expected float, got something else"))
} }

View File

@ -5,12 +5,12 @@ type params = {
type outputType = [ type outputType = [
| #Dist(GenericDist_Types.genericDist) | #Dist(GenericDist_Types.genericDist)
| #Error(GenericDist_Types.error) | #GenDistError(GenericDist_Types.error)
| #Float(float) | #Float(float)
| #String(string) | #String(string)
] ]
let run: (params, GenericDist_Types.Operation.genericFunctionCall) => outputType let run: (params, GenericDist_Types.Operation.genericFunctionCallInfo) => outputType
let runFromDist: ( let runFromDist: (
params, params,
GenericDist_Types.Operation.fromDist, GenericDist_Types.Operation.fromDist,

View File

@ -6,7 +6,7 @@ type genericDist = [
type error = type error =
| NotYetImplemented | NotYetImplemented
| ImpossiblePath | Unreachable
| DistributionVerticalShiftIsInvalid | DistributionVerticalShiftIsInvalid
| Other(string) | Other(string)
@ -67,12 +67,13 @@ module Operation = {
| #fromFloat(fromDist) | #fromFloat(fromDist)
] ]
type genericFunctionCall = [ type genericFunctionCallInfo = [
| #fromDist(fromDist, genericDist) | #fromDist(fromDist, genericDist)
| #fromFloat(fromDist, float) | #fromFloat(fromDist, float)
| #mixture(array<(genericDist, float)>) | #mixture(array<(genericDist, float)>)
] ]
//TODO: Should support all genericFunctionCallInfo types
let toString = (distFunction: fromDist): string => let toString = (distFunction: fromDist): string =>
switch distFunction { switch distFunction {
| #toFloat(#Cdf(r)) => `cdf(${E.Float.toFixed(r)})` | #toFloat(#Cdf(r)) => `cdf(${E.Float.toFixed(r)})`