Merge pull request #369 from quantified-uncertainty/ordered-xyshape

Ensure xyShape is ordered by x coordinates when multiplying by negatives
This commit is contained in:
Ozzie Gooen 2022-04-25 15:05:08 -04:00 committed by GitHub
commit dc3faad897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 3 deletions

View File

@ -0,0 +1,16 @@
open Jest
open TestHelpers
describe("Combining Continuous and Discrete Distributions", () => {
makeTest(
"keep order of xs when multiplying by negative number",
AlgebraicShapeCombination.isOrdered(
AlgebraicShapeCombination.combineShapesContinuousDiscrete(
#Multiply,
{xs: [0., 1.], ys: [1., 1.]},
{xs: [-1.], ys: [1.]},
),
), // Multiply distribution by -1
true,
)
})

View File

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

View File

@ -215,7 +215,6 @@ let combineShapesContinuousDiscrete = (
continuousShape.ys[i] *. discreteShape.ys[j],
),
) |> ignore
()
}
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore
()
@ -225,9 +224,13 @@ let combineShapesContinuousDiscrete = (
// creates a new continuous shape for each one of the discrete points, and collects them in outXYShapes.
let dxyShape: array<(float, float)> = Belt.Array.makeUninitializedUnsafe(t1n)
for i in 0 to t1n - 1 {
// If this operation would flip the x axis (such as -1 * normal(5, 2)),
// then we want to fill the shape in backwards to ensure all the points
// are still in the right order
let index = discreteShape.xs[j] > 0.0 ? i : t1n - 1 - i
Belt.Array.set(
dxyShape,
i,
index,
(
fn(continuousShape.xs[i], discreteShape.xs[j]),
continuousShape.ys[i] *. discreteShape.ys[j] /. Js.Math.abs_float(discreteShape.xs[j]),
@ -251,3 +254,5 @@ let combineShapesContinuousDiscrete = (
XYShape.T.empty,
)
}
let isOrdered = (a: XYShape.T.t): bool => E.A.Sorted.Floats.isSorted(a.xs)

View File

@ -363,6 +363,9 @@ module A = {
|> Rationale.Result.return
}
let tail = Belt.Array.sliceToEnd(_, 1)
let zip = Belt.Array.zip
// This zips while taking the longest elements of each array.
let zipMaxLength = (array1, array2) => {
let maxLength = Int.max(length(array1), length(array2))
@ -506,6 +509,9 @@ module A = {
}
module Floats = {
let isSorted = (ar: array<float>): bool =>
reduce(zip(ar, tail(ar)), true, (acc, (first, second)) => acc && first < second)
let makeIncrementalUp = (a, b) =>
Array.make(b - a + 1, a) |> Array.mapi((i, c) => c + i) |> Belt.Array.map(_, float_of_int)