Formatter

This commit is contained in:
Ozzie Gooen 2022-09-02 21:53:20 -07:00
parent 93f4c1e0c2
commit b87e952785
6 changed files with 47 additions and 39 deletions

View File

@ -63,9 +63,15 @@ 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(
"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(
"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

@ -31,7 +31,7 @@ let isSymbolic = (t: t) =>
let sampleN = (t: t, n) =>
switch t {
| PointSet(r) => PointSetDist.T.sampleN(r,n)
| PointSet(r) => PointSetDist.T.sampleN(r, n)
| SampleSet(r) => SampleSetDist.sampleN(r, n)
| Symbolic(r) => SymbolicDist.T.sampleN(n, r)
}

View File

@ -271,24 +271,24 @@ module T = Dist({
let variance = (t: t): float =>
XYShape.Analysis.getVarianceDangerously(t, mean, Analysis.getMeanOfSquares)
let doN = (n, fn) => {
let doN = (n, fn) => {
let items = Belt.Array.make(n, 0.0)
for x in 0 to n - 1 {
let _ = Belt.Array.set(items, x, fn())
}
items
}
}
let sample = (t: t): float => {
let sample = (t: t): float => {
let randomItem = Random.float(1.0)
t |> integralYtoX(randomItem)
}
}
let sampleN = (dist, n) => {
let sampleN = (dist, n) => {
let integralCache = integral(dist)
let distWithUpdatedIntegralCache = updateIntegralCache(Some(integralCache), dist)
doN(n, () => sample(distWithUpdatedIntegralCache))
}
}
})
let isNormalized = (t: t): bool => {

View File

@ -224,8 +224,8 @@ module T = Dist({
XYShape.Analysis.getVarianceDangerously(t, mean, getMeanOfSquares)
}
let sampleN = (t: t, n): array<float> => {
let sampleN = (t: t, n): array<float> => {
let normalized = t->normalize->getShape
Stdlib.Random.sample(normalized.xs, {probs: normalized.ys, size: n})
}
}
})

View File

@ -270,9 +270,9 @@ module T = Dist({
})
}
let discreteIntegralSum =({discrete}: t): float => Discrete.T.Integral.sum(discrete)
let continuousIntegralSum =({continuous}: t): float => Continuous.T.Integral.sum(continuous)
let integralSum =(t:t): float => discreteIntegralSum(t) +. continuousIntegralSum(t)
let discreteIntegralSum = ({discrete}: t): float => Discrete.T.Integral.sum(discrete)
let continuousIntegralSum = ({continuous}: t): float => Continuous.T.Integral.sum(continuous)
let integralSum = (t: t): float => discreteIntegralSum(t) +. continuousIntegralSum(t)
let mean = ({discrete, continuous} as t: t): float => {
let discreteMean = Discrete.T.mean(discrete)
@ -289,7 +289,7 @@ module T = Dist({
let _integralSum = integralSum(t)
let getMeanOfSquares = ({discrete, continuous}: t) => {
let discreteMean = discrete |> Discrete.shapeMap(XYShape.T.square) |> Discrete.T.mean
let continuousMean = continuous -> Continuous.Analysis.getMeanOfSquares
let continuousMean = continuous->Continuous.Analysis.getMeanOfSquares
(discreteMean *. discreteIntegralSum(t) +. continuousMean *. continuousIntegralSum(t)) /.
integralSum(t)
}
@ -301,15 +301,17 @@ module T = Dist({
}
}
let sampleN = (t: t, n:int): array<float> => {
let discreteIntegralSum = discreteIntegralSum(t);
let integralSum = integralSum(t);
let discreteSampleLength:int = (Js.Int.toFloat(n) *. discreteIntegralSum /. integralSum) -> E.Float.toInt
let continuousSampleLength = n - discreteSampleLength;
let continuousSamples = t.continuous ->Continuous.T.normalize-> Continuous.T.sampleN( continuousSampleLength)
let discreteSamples = t.discrete ->Discrete.T.normalize->Discrete.T.sampleN(discreteSampleLength)
Js.log3("Samples", continuousSamples, discreteSamples);
E.A.concat(discreteSamples, continuousSamples) -> E.A.shuffle
let sampleN = (t: t, n: int): array<float> => {
let discreteIntegralSum = discreteIntegralSum(t)
let integralSum = integralSum(t)
let discreteSampleLength: int =
(Js.Int.toFloat(n) *. discreteIntegralSum /. integralSum)->E.Float.toInt
let continuousSampleLength = n - discreteSampleLength
let continuousSamples =
t.continuous->Continuous.T.normalize->Continuous.T.sampleN(continuousSampleLength)
let discreteSamples = t.discrete->Discrete.T.normalize->Discrete.T.sampleN(discreteSampleLength)
Js.log3("Samples", continuousSamples, discreteSamples)
E.A.concat(discreteSamples, continuousSamples)->E.A.shuffle
}
})

View File

@ -201,9 +201,9 @@ module T = Dist({
let sampleN = (t: t, int): array<float> =>
switch t {
| Mixed(m) => Mixed.T.sampleN(m,int)
| Discrete(m) => Discrete.T.sampleN(m,int)
| Continuous(m) => Continuous.T.sampleN(m,int)
| Mixed(m) => Mixed.T.sampleN(m, int)
| Discrete(m) => Discrete.T.sampleN(m, int)
| Continuous(m) => Continuous.T.sampleN(m, int)
}
})