Formatter
This commit is contained in:
parent
93f4c1e0c2
commit
b87e952785
|
@ -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.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.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("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(
|
testEvalToBe(
|
||||||
"addOne(t)=t+1; SampleSet.toList(SampleSet.map(SampleSet.fromList([1,2,3,4,5,6]), addOne))",
|
"addOne(t)=t+1; SampleSet.toList(SampleSet.map(SampleSet.fromList([1,2,3,4,5,6]), addOne))",
|
||||||
"Ok([2,3,4,5,6,7])",
|
"Ok([2,3,4,5,6,7])",
|
||||||
|
|
|
@ -31,7 +31,7 @@ let isSymbolic = (t: t) =>
|
||||||
|
|
||||||
let sampleN = (t: t, n) =>
|
let sampleN = (t: t, n) =>
|
||||||
switch t {
|
switch t {
|
||||||
| PointSet(r) => PointSetDist.T.sampleN(r,n)
|
| PointSet(r) => PointSetDist.T.sampleN(r, n)
|
||||||
| SampleSet(r) => SampleSetDist.sampleN(r, n)
|
| SampleSet(r) => SampleSetDist.sampleN(r, n)
|
||||||
| Symbolic(r) => SymbolicDist.T.sampleN(n, r)
|
| Symbolic(r) => SymbolicDist.T.sampleN(n, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -271,24 +271,24 @@ module T = Dist({
|
||||||
let variance = (t: t): float =>
|
let variance = (t: t): float =>
|
||||||
XYShape.Analysis.getVarianceDangerously(t, mean, Analysis.getMeanOfSquares)
|
XYShape.Analysis.getVarianceDangerously(t, mean, Analysis.getMeanOfSquares)
|
||||||
|
|
||||||
let doN = (n, fn) => {
|
let doN = (n, fn) => {
|
||||||
let items = Belt.Array.make(n, 0.0)
|
let items = Belt.Array.make(n, 0.0)
|
||||||
for x in 0 to n - 1 {
|
for x in 0 to n - 1 {
|
||||||
let _ = Belt.Array.set(items, x, fn())
|
let _ = Belt.Array.set(items, x, fn())
|
||||||
|
}
|
||||||
|
items
|
||||||
}
|
}
|
||||||
items
|
|
||||||
}
|
|
||||||
|
|
||||||
let sample = (t: t): float => {
|
let sample = (t: t): float => {
|
||||||
let randomItem = Random.float(1.0)
|
let randomItem = Random.float(1.0)
|
||||||
t |> integralYtoX(randomItem)
|
t |> integralYtoX(randomItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
let sampleN = (dist, n) => {
|
let sampleN = (dist, n) => {
|
||||||
let integralCache = integral(dist)
|
let integralCache = integral(dist)
|
||||||
let distWithUpdatedIntegralCache = updateIntegralCache(Some(integralCache), dist)
|
let distWithUpdatedIntegralCache = updateIntegralCache(Some(integralCache), dist)
|
||||||
doN(n, () => sample(distWithUpdatedIntegralCache))
|
doN(n, () => sample(distWithUpdatedIntegralCache))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
let isNormalized = (t: t): bool => {
|
let isNormalized = (t: t): bool => {
|
||||||
|
|
|
@ -224,8 +224,8 @@ module T = Dist({
|
||||||
XYShape.Analysis.getVarianceDangerously(t, mean, getMeanOfSquares)
|
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
|
let normalized = t->normalize->getShape
|
||||||
Stdlib.Random.sample(normalized.xs, {probs: normalized.ys, size: n})
|
Stdlib.Random.sample(normalized.xs, {probs: normalized.ys, size: n})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -270,9 +270,9 @@ module T = Dist({
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let discreteIntegralSum =({discrete}: t): float => Discrete.T.Integral.sum(discrete)
|
let discreteIntegralSum = ({discrete}: t): float => Discrete.T.Integral.sum(discrete)
|
||||||
let continuousIntegralSum =({continuous}: t): float => Continuous.T.Integral.sum(continuous)
|
let continuousIntegralSum = ({continuous}: t): float => Continuous.T.Integral.sum(continuous)
|
||||||
let integralSum =(t:t): float => discreteIntegralSum(t) +. continuousIntegralSum(t)
|
let integralSum = (t: t): float => discreteIntegralSum(t) +. continuousIntegralSum(t)
|
||||||
|
|
||||||
let mean = ({discrete, continuous} as t: t): float => {
|
let mean = ({discrete, continuous} as t: t): float => {
|
||||||
let discreteMean = Discrete.T.mean(discrete)
|
let discreteMean = Discrete.T.mean(discrete)
|
||||||
|
@ -289,7 +289,7 @@ module T = Dist({
|
||||||
let _integralSum = integralSum(t)
|
let _integralSum = integralSum(t)
|
||||||
let getMeanOfSquares = ({discrete, continuous}: t) => {
|
let getMeanOfSquares = ({discrete, continuous}: t) => {
|
||||||
let discreteMean = discrete |> Discrete.shapeMap(XYShape.T.square) |> Discrete.T.mean
|
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)) /.
|
(discreteMean *. discreteIntegralSum(t) +. continuousMean *. continuousIntegralSum(t)) /.
|
||||||
integralSum(t)
|
integralSum(t)
|
||||||
}
|
}
|
||||||
|
@ -300,16 +300,18 @@ module T = Dist({
|
||||||
| _ => XYShape.Analysis.getVarianceDangerously(t, mean, getMeanOfSquares)
|
| _ => XYShape.Analysis.getVarianceDangerously(t, mean, getMeanOfSquares)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let sampleN = (t: t, n:int): array<float> => {
|
let sampleN = (t: t, n: int): array<float> => {
|
||||||
let discreteIntegralSum = discreteIntegralSum(t);
|
let discreteIntegralSum = discreteIntegralSum(t)
|
||||||
let integralSum = integralSum(t);
|
let integralSum = integralSum(t)
|
||||||
let discreteSampleLength:int = (Js.Int.toFloat(n) *. discreteIntegralSum /. integralSum) -> E.Float.toInt
|
let discreteSampleLength: int =
|
||||||
let continuousSampleLength = n - discreteSampleLength;
|
(Js.Int.toFloat(n) *. discreteIntegralSum /. integralSum)->E.Float.toInt
|
||||||
let continuousSamples = t.continuous ->Continuous.T.normalize-> Continuous.T.sampleN( continuousSampleLength)
|
let continuousSampleLength = n - discreteSampleLength
|
||||||
let discreteSamples = t.discrete ->Discrete.T.normalize->Discrete.T.sampleN(discreteSampleLength)
|
let continuousSamples =
|
||||||
Js.log3("Samples", continuousSamples, discreteSamples);
|
t.continuous->Continuous.T.normalize->Continuous.T.sampleN(continuousSampleLength)
|
||||||
E.A.concat(discreteSamples, continuousSamples) -> E.A.shuffle
|
let discreteSamples = t.discrete->Discrete.T.normalize->Discrete.T.sampleN(discreteSampleLength)
|
||||||
|
Js.log3("Samples", continuousSamples, discreteSamples)
|
||||||
|
E.A.concat(discreteSamples, continuousSamples)->E.A.shuffle
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -201,9 +201,9 @@ module T = Dist({
|
||||||
|
|
||||||
let sampleN = (t: t, int): array<float> =>
|
let sampleN = (t: t, int): array<float> =>
|
||||||
switch t {
|
switch t {
|
||||||
| Mixed(m) => Mixed.T.sampleN(m,int)
|
| Mixed(m) => Mixed.T.sampleN(m, int)
|
||||||
| Discrete(m) => Discrete.T.sampleN(m,int)
|
| Discrete(m) => Discrete.T.sampleN(m, int)
|
||||||
| Continuous(m) => Continuous.T.sampleN(m,int)
|
| Continuous(m) => Continuous.T.sampleN(m, int)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user