Merge pull request #1050 from quantified-uncertainty/SampleSet.max-min

Added SampleSet.min and Sampleset.max
This commit is contained in:
Ozzie Gooen 2022-09-01 09:07:02 -07:00 committed by GitHub
commit a97d1bae6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 1 deletions

View File

@ -63,6 +63,9 @@ describe("FunctionRegistry Library", () => {
testEvalToBe("SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)")
testEvalToBe("SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)")
testEvalToBe("SampleSet.fromFn({|| sample(normal(5,2))})", "Ok(Sample Set Distribution)")
testEvalToBe("SampleSet.min(SampleSet.fromDist(normal(50,2)), 2)", "Ok(Sample Set Distribution)")
testEvalToBe("mean(SampleSet.min(SampleSet.fromDist(normal(50,2)), 2))", "Ok(2)")
testEvalToBe("SampleSet.max(SampleSet.fromDist(normal(50,2)), 10)", "Ok(Sample Set Distribution)")
testEvalToBe(
"addOne(t)=t+1; SampleSet.toList(SampleSet.map(SampleSet.fromList([1,2,3,4,5,6]), addOne))",
"Ok([2,3,4,5,6,7])",

View File

@ -159,3 +159,9 @@ let truncate = (t, ~leftCutoff: option<float>, ~rightCutoff: option<float>) => {
let withTruncatedRight = t => rightCutoff |> E.O.dimap(left => truncateRight(t, left), _ => Ok(t))
t->withTruncatedLeft |> E.R2.bind(withTruncatedRight)
}
let minOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.min_float(a, b)), ~t1, ~t2)
let maxOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.max_float(a, b)), ~t1, ~t2)
let minOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.min_float(a, f)), t)
let maxOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.max_float(a, f)), t)

View File

@ -75,7 +75,7 @@ module Internal = {
}
}
let library = [
let libaryBase = [
Function.make(
~name="fromDist",
~nameSpace,
@ -274,3 +274,63 @@ let library = [
(),
),
]
module Comparison = {
let template = (name, inputs, run) => {
FnDefinition.make(
~name,
~inputs,
~run=(inputs, _, _, _) => {
run(inputs)
},
(),
)
}
let wrapper = r =>
r
->E.R2.fmap(r => r->Wrappers.sampleSet->Wrappers.evDistribution)
->E.R2.errMap(SampleSetDist.Error.toString)
let mkBig = (name, withDist, withFloat) =>
Function.make(
~name,
~nameSpace,
~requiresNamespace=false,
~examples=[
`SampleSet.${name}(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(6,2)))`,
`SampleSet.${name}(SampleSet.fromDist(normal(5,2)), 3.0)`,
`SampleSet.${name}(4.0, SampleSet.fromDist(normal(6,2)))`,
],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[
template(name, [FRTypeDist, FRTypeDist], inputs => {
switch inputs {
| [IEvDistribution(SampleSet(dist1)), IEvDistribution(SampleSet(dist2))] =>
withDist(dist1, dist2)->wrapper
| _ => Error(impossibleError)
}
}),
template(name, [FRTypeDist, FRTypeNumber], inputs => {
switch inputs {
| [IEvDistribution(SampleSet(dist)), IEvNumber(f)] => withFloat(dist, f)->wrapper
| _ => Error(impossibleError)
}
}),
template(name, [FRTypeNumber, FRTypeDist], inputs => {
switch inputs {
| [IEvNumber(f), IEvDistribution(SampleSet(dist))] => withFloat(dist, f)->wrapper
| _ => Error(impossibleError)
}
}),
],
(),
)
let library = [
mkBig("min", SampleSetDist.minOfTwo, SampleSetDist.minOfFloat),
mkBig("max", SampleSetDist.maxOfTwo, SampleSetDist.maxOfFloat),
]
}
let library = E.A.append(libaryBase, Comparison.library)