Respond to Ozzie's PR comments

This commit is contained in:
Sam Nolan 2022-04-08 16:51:38 +10:00
parent 5a2c4c8aec
commit d6e18b1c4f
10 changed files with 35 additions and 45 deletions

View File

@ -1,17 +1,11 @@
open Jest open Jest
open Expect open Expect
let normalDist: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 5.0, stdev: 2.0})) let {normalDist, uniformDist, betaDist, lognormalDist, cauchyDist, triangularDist, exponentialDist} = module(GenericDist_Fixtures)
let uniformDist: GenericDist_Types.genericDist = Symbolic(#Uniform({low: 9.0, high: 10.0}))
let betaDist: GenericDist_Types.genericDist = Symbolic(#Beta({alpha: 2.0, beta: 5.0}))
let lognormalDist: GenericDist_Types.genericDist = Symbolic(#Lognormal({mu: 0.0, sigma: 1.0}))
let cauchyDist: GenericDist_Types.genericDist = Symbolic(#Cauchy({local: 1.0, scale: 1.0}))
let triangularDist: GenericDist_Types.genericDist = Symbolic(#Triangular({low: 1.0, medium: 2.0, high: 3.0}))
let exponentialDist: GenericDist_Types.genericDist = Symbolic(#Exponential({rate: 2.0}))
let runTest = (name: string, dist : GenericDist_Types.genericDist, expected: string) => { let runTest = (name: string, dist : GenericDist_Types.genericDist, expected: string) => {
test(name, () => { test(name, () => {
let result = GenericDist.toSparkline(~xyPointLength=100, ~sampleCount=100, ~buckets=20, dist) let result = GenericDist.toSparkline(~sampleCount=100, ~buckets=20, dist)
switch result { switch result {
| Ok(sparkline) => expect(sparkline)->toEqual(expected) | Ok(sparkline) => expect(sparkline)->toEqual(expected)
| Error(err) => expect("Error")->toEqual(expected) | Error(err) => expect("Error")->toEqual(expected)
@ -20,11 +14,11 @@ let runTest = (name: string, dist : GenericDist_Types.genericDist, expected: str
} }
describe("sparkline of generic distribution", () => { describe("sparkline of generic distribution", () => {
runTest("normal", normalDist, `▁▁▁▁▂▃▄▆▇██▇▆▄▃▂▁▁▁`) runTest("normal", normalDist, `▁▃▄▆▇████████▇▆▄▃▁`)
runTest("uniform", uniformDist, `████████████████████`) runTest("uniform", uniformDist, `▁██▁`)
runTest("beta", uniformDist, `████████████████████`) runTest("beta", betaDist, `▁▅▇█████████▇▇▆▅▄▃▂▁`)
runTest("lognormal", lognormalDist, `█▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁`) runTest("lognormal", lognormalDist, `▁▇████▇▇▆▆▅▄▄▃▃▂▂▁▁▁`)
runTest("cauchy", cauchyDist, `▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁`) runTest("cauchy", cauchyDist, `▁▁▁▂▄▅▆▇████▇▆▅▄▂▁▁▁`)
runTest("triangular", triangularDist, `▁▄▅▆▇████▇▆▅▄▁`) runTest("triangular", triangularDist, `▁▃▄▅▆████▆▅▄▃▁`)
runTest("exponential", exponentialDist, `█▆▄▃▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁`) runTest("exponential", exponentialDist, `███▇▇▆▆▆▅▅▄▄▃▃▃▂▂▁▁▁`)
}) })

View File

@ -0,0 +1,11 @@
let normalDist5: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 5.0, stdev: 2.0}))
let normalDist10: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 10.0, stdev: 2.0}))
let normalDist20: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 20.0, stdev: 2.0}))
let normalDist: GenericDist_Types.genericDist = normalDist5
let betaDist: GenericDist_Types.genericDist = Symbolic(#Beta({alpha: 2.0, beta: 5.0}))
let lognormalDist: GenericDist_Types.genericDist = Symbolic(#Lognormal({mu: 0.0, sigma: 1.0}))
let cauchyDist: GenericDist_Types.genericDist = Symbolic(#Cauchy({local: 1.0, scale: 1.0}))
let triangularDist: GenericDist_Types.genericDist = Symbolic(#Triangular({low: 1.0, medium: 2.0, high: 3.0}))
let exponentialDist: GenericDist_Types.genericDist = Symbolic(#Exponential({rate: 2.0}))
let uniformDist: GenericDist_Types.genericDist = Symbolic(#Uniform({low: 9.0, high: 10.0}))

View File

@ -6,11 +6,8 @@ let env: DistributionOperation.env = {
xyPointLength: 100, xyPointLength: 100,
} }
let normalDist5: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 5.0, stdev: 2.0}))
let normalDist10: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 10.0, stdev: 2.0}))
let normalDist20: GenericDist_Types.genericDist = Symbolic(#Normal({mean: 20.0, stdev: 2.0}))
let uniformDist: GenericDist_Types.genericDist = Symbolic(#Uniform({low: 9.0, high: 10.0}))
let {normalDist5, normalDist10, normalDist20, uniformDist} = module(GenericDist_Fixtures)
let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output) let {toFloat, toDist, toString, toError} = module(DistributionOperation.Output)
let {run} = module(DistributionOperation) let {run} = module(DistributionOperation)
let {fmap} = module(DistributionOperation.Output) let {fmap} = module(DistributionOperation.Output)

View File

@ -9,6 +9,3 @@ let expectParseToBe = (expr: string, answer: string) =>
let expectEvalToBe = (expr: string, answer: string) => let expectEvalToBe = (expr: string, answer: string) =>
Reducer.eval(expr)->ExpressionValue.toStringResult->expect->toBe(answer) Reducer.eval(expr)->ExpressionValue.toStringResult->expect->toBe(answer)
// Current configuration does not ignore this file so we have to have a test
test("test helpers", () => expect(1)->toBe(1))

View File

@ -22,12 +22,12 @@ describe("Normal distribution with sparklines", () => {
let normalDistAtMean5: SymbolicDistTypes.normal = {mean: 5.0, stdev: 2.0} let normalDistAtMean5: SymbolicDistTypes.normal = {mean: 5.0, stdev: 2.0}
let normalDistAtMean10: SymbolicDistTypes.normal = {mean: 10.0, stdev: 2.0} let normalDistAtMean10: SymbolicDistTypes.normal = {mean: 10.0, stdev: 2.0}
let range20Float = E.A.rangeFloat(0.0, 20.0) // [0.0,1.0,2.0,3.0,4.0,...19.0,] let range20Float = E.A.Floats.range(0.0, 20.0, 20) // [0.0,1.0,2.0,3.0,4.0,...19.0,]
let pdfNormalDistAtMean5 = x => Normal.pdf(x, normalDistAtMean5) let pdfNormalDistAtMean5 = x => Normal.pdf(x, normalDistAtMean5)
let sparklineMean5 = pdfImage(pdfNormalDistAtMean5, range20Float) let sparklineMean5 = pdfImage(pdfNormalDistAtMean5, range20Float)
makeTest("mean=5", Sparklines.create(sparklineMean5, ()), `▁▂▃▅███▅▃▂▁▁▁▁▁▁▁▁▁▁▁`) makeTest("mean=5", Sparklines.create(sparklineMean5, ()), `▁▂▃▆██▇▅▂▁▁▁▁▁▁▁▁▁▁▁`)
let sparklineMean15 = normalDistAtMean5 -> parameterWiseAdditionHelper(normalDistAtMean10) -> pdfImage(range20Float) let sparklineMean15 = normalDistAtMean5 -> parameterWiseAdditionHelper(normalDistAtMean10) -> pdfImage(range20Float)
makeTest("parameter-wise addition of two normal distributions", Sparklines.create(sparklineMean15, ()), `▁▁▁▁▁▁▁▁▁▁▂▃▅▇███▇▅▃▂`) makeTest("parameter-wise addition of two normal distributions", Sparklines.create(sparklineMean15, ()), `▁▁▁▁▁▁▁▁▁▂▃▄▆███▇▅▄▂`)
}) })

View File

@ -2,6 +2,7 @@
module.exports = { module.exports = {
preset: 'ts-jest', preset: 'ts-jest',
testEnvironment: 'node', testEnvironment: 'node',
testPathIgnorePatterns: [".*Fixtures.bs.js", "/node_modules/", ".*Helpers.bs.js"],
setupFilesAfterEnv: [ setupFilesAfterEnv: [
"<rootdir>/../../node_modules/bisect_ppx/src/runtime/js/jest.bs.js" "<rootdir>/../../node_modules/bisect_ppx/src/runtime/js/jest.bs.js"
], ],

View File

@ -75,8 +75,10 @@ let toPointSet = (~xyPointLength, ~sampleCount, t): result<PointSetTypes.pointSe
} }
} }
let toSparkline = (~xyPointLength: int, ~sampleCount: int, ~buckets: int = 20, t: t) : result<string, error> => let toSparkline = (~sampleCount: int, ~buckets: int = 20, t: t) : result<string, error> =>
toPointSet(~xyPointLength, ~sampleCount, t) -> E.R2.fmap(PointSetDist.toSparkline(buckets)) toPointSet(~xyPointLength=buckets, ~sampleCount, t)
-> E.R.bind(x => x -> PointSetDist.T.toContinuous -> E.O2.toResult(GenericDist_Types.Other("Could not convert to continuous")))
-> E.R2.fmap(c => Sparklines.create(Continuous.getShape(c).ys, ()))
module Truncate = { module Truncate = {
let trySymbolicSimplification = (leftCutoff, rightCutoff, t: t): option<t> => let trySymbolicSimplification = (leftCutoff, rightCutoff, t: t): option<t> =>

View File

@ -25,7 +25,6 @@ let toPointSet: (
t, t,
) => result<PointSetTypes.pointSetDist, error> ) => result<PointSetTypes.pointSetDist, error>
let toSparkline: ( let toSparkline: (
~xyPointLength: int,
~sampleCount: int, ~sampleCount: int,
~buckets: int=?, ~buckets: int=?,
t, t,

View File

@ -168,15 +168,6 @@ let pdf = (f: float, t: t) => {
let inv = T.Integral.yToX let inv = T.Integral.yToX
let cdf = T.Integral.xToY let cdf = T.Integral.xToY
@genType
let toSparkline = (buckets: int, t: t ): string => {
let size : float = T.maxX(t) -. T.minX(t)
let stepSize = size /. Belt.Int.toFloat(buckets)
let cdfImage = E.A.rangeFloat(~step=stepSize, T.minX(t), T.maxX(t)) -> Belt.Array.map(val => cdf(val,t))
Sparklines.create(E.A.diff(cdfImage), ())
}
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 {
@ -200,7 +191,6 @@ let isFloat = (t: t) =>
let sampleNRendered = (n, dist) => { let sampleNRendered = (n, dist) => {
let integralCache = T.Integral.get(dist) let integralCache = T.Integral.get(dist)
let distWithUpdatedIntegralCache = T.updateIntegralCache(Some(integralCache), dist) let distWithUpdatedIntegralCache = T.updateIntegralCache(Some(integralCache), dist)
doN(n, () => sample(distWithUpdatedIntegralCache)) doN(n, () => sample(distWithUpdatedIntegralCache))
} }

View File

@ -101,6 +101,7 @@ module O2 = {
let default = (a, b) => O.default(b, a) let default = (a, b) => O.default(b, a)
let toExn = (a, b) => O.toExn(b, a) let toExn = (a, b) => O.toExn(b, a)
let fmap = (a, b) => O.fmap(b, a) let fmap = (a, b) => O.fmap(b, a)
let toResult = (a, b) => O.toResult(b, a)
} }
/* Functions */ /* Functions */
@ -178,6 +179,7 @@ module R = {
module R2 = { module R2 = {
let fmap = (a,b) => R.fmap(b,a) let fmap = (a,b) => R.fmap(b,a)
let bind = (a, b) => R.bind(b, a)
} }
let safe_fn_of_string = (fn, s: string): option<'a> => let safe_fn_of_string = (fn, s: string): option<'a> =>
@ -290,13 +292,6 @@ module A = {
|> Rationale.Result.return |> Rationale.Result.return
} }
let diff = (arr: array<float>): array<float> =>
Belt.Array.zipBy(arr, Belt.Array.sliceToEnd(arr, 1), (left, right) => right -. left)
let rec rangeFloat = (~step: float=1.0, start : float, end: float) : array<float> =>
start > end ?
[]
: Belt.Array.concat([start], rangeFloat(~step, start +. step, end))
// This zips while taking the longest elements of each array. // This zips while taking the longest elements of each array.
let zipMaxLength = (array1, array2) => { let zipMaxLength = (array1, array2) => {
@ -448,6 +443,10 @@ module A = {
let mean = a => sum(a) /. (Array.length(a) |> float_of_int) let mean = a => sum(a) /. (Array.length(a) |> float_of_int)
let random = Js.Math.random_int let random = Js.Math.random_int
let diff = (arr: array<float>): array<float> =>
Belt.Array.zipBy(arr, Belt.Array.sliceToEnd(arr, 1), (left, right) => right -. left)
exception RangeError(string) exception RangeError(string)
let range = (min: float, max: float, n: int): array<float> => let range = (min: float, max: float, n: int): array<float> =>
switch n { switch n {