Start of refactor for toPointSetDist

This commit is contained in:
Ozzie Gooen 2022-04-09 20:21:02 -04:00
parent 61aaca3e2f
commit fa3d874a4e
2 changed files with 19 additions and 20 deletions

View File

@ -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
})
}

View File

@ -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)
}