diff --git a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res index 90381fba..f87b3a22 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res @@ -64,22 +64,17 @@ let toPointSet = ( switch (t: t) { | PointSet(pointSet) => Ok(pointSet) | Symbolic(r) => Ok(SymbolicDist.T.toPointSetDist(~xSelection, xyPointLength, r)) - | SampleSet(r) => { - let response = SampleSetDist.toPointSetDist( - ~samples=r, - ~samplingInputs={ - sampleCount: sampleCount, - outputXYPoints: xyPointLength, - pointSetDistLength: xyPointLength, - kernelWidth: None, - }, - (), - ).pointSetDist - switch response { - | Some(r) => Ok(r) - | None => Error(Other("Converting sampleSet to pointSet failed")) - } - } + | SampleSet(r) => + SampleSetDist.toPointSetDist2( + ~samples=r, + ~samplingInputs={ + sampleCount: sampleCount, + outputXYPoints: xyPointLength, + pointSetDistLength: xyPointLength, + kernelWidth: None, + }, + (), + )->mapStringErrors } } @@ -170,8 +165,7 @@ module AlgebraicCombination = { ) => { let arithmeticOperation = Operation.Algebraic.toFn(arithmeticOperation) E.R.merge(toSampleSet(t1), toSampleSet(t2))->E.R.bind(((a, b)) => { - SampleSetDist.runMonteCarlo(arithmeticOperation, a, b) - ->mapStringErrors + SampleSetDist.map2(~fn=arithmeticOperation, ~t1=a, ~t2=b)->mapStringErrors }) } diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res index cea650f8..264b32ea 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res @@ -15,11 +15,15 @@ module T: { include T -// TODO: Refactor to raise correct error when not enough samples +let length = (t:t) => get(t) |> E.A.length; +// TODO: Refactor to raise correct error when not enough samples let toPointSetDist = (~samples: t, ~samplingInputs: SamplingInputs.samplingInputs, ()) => SampleSetDist_ToPointSet.toPointSetDist(~samples=get(samples), ~samplingInputs, ()) +let toPointSetDist2 = (~samples: t, ~samplingInputs: SamplingInputs.samplingInputs, ()) => + SampleSetDist_ToPointSet.toPointSetDist(~samples=get(samples), ~samplingInputs, ()).pointSetDist |> E.O.toResult("Failed to convert to PointSetDist") + //Randomly get one sample from the distribution let sample = (t: t): float => { let i = E.Int.random(~min=0, ~max=E.A.length(get(t)) - 1) @@ -42,7 +46,8 @@ let sampleN = (t: t, n) => { } } -let runMonteCarlo = (fn: (float, float) => float, t1: t, t2: t) => { +//TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this. +let map2 = (~fn: (float, float) => float, ~t1: t, ~t2: t) => { let samples = Belt.Array.zip(get(t1), get(t2))->E.A2.fmap(((a, b)) => fn(a, b)) make(samples) }