From 255541a679857414124cac85be8764615954d482 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 31 Aug 2022 21:43:36 -0700 Subject: [PATCH 1/3] Added SampleSet.min and Sampleset.max --- .../SampleSetDist/SampleSetDist.res | 6 ++ .../FunctionRegistry/Library/FR_Sampleset.res | 62 ++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res index f0fbff99..2b76b1fb 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res @@ -140,3 +140,9 @@ let truncate = (t, ~leftCutoff: option, ~rightCutoff: option) => { 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) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index c40fe349..1d5487ad 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -75,7 +75,7 @@ module Internal = { } } -let library = [ +let library1 = [ 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(library1, Comparison.library) From 96815b6ee519ff05784b1fa8cd4edccb8b3b088e Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 31 Aug 2022 22:14:41 -0700 Subject: [PATCH 2/3] Really simple tests for SampleSet.min and max --- .../SquiggleLibrary_FunctionRegistryLibrary_test.res | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res index 2418b4d8..7ddb57d4 100644 --- a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res +++ b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res @@ -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])", From e582fc5be332e4264b8a55bc111338fc55ad6f88 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 31 Aug 2022 22:15:13 -0700 Subject: [PATCH 3/3] library1 -> libraryBase --- .../src/rescript/FunctionRegistry/Library/FR_Sampleset.res | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index 1d5487ad..a11742db 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -75,7 +75,7 @@ module Internal = { } } -let library1 = [ +let libaryBase = [ Function.make( ~name="fromDist", ~nameSpace, @@ -333,4 +333,4 @@ module Comparison = { ] } -let library = E.A.append(library1, Comparison.library) +let library = E.A.append(libaryBase, Comparison.library)