Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chi-square test conditions #482

Merged
merged 8 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/scala/com/amazon/deequ/analyzers/Distance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ object Distance {
sample.map(e => (e._1, e._2.toDouble)), expectedNorm, absThresholdYates, percThresholdYates, absThresholdCochran)

// If less than 2 categories remain we cannot conduct the test
if (regroupedSample.keySet.size < chisquareMinDimension) {
if (regroupedExpected.keySet.size < chisquareMinDimension) {
Double.NaN
} else {
// run chi-square test and return statistics or p-value
Expand Down
28 changes: 28 additions & 0 deletions src/test/scala/com/amazon/deequ/KLL/KLLDistanceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class KLLDistanceTest extends WordSpec with SparkContextSpec
assert(distance == 0.06015037593984962)
}


"Categorial distance should compute correct linf_robust" in {
val sample1 = scala.collection.mutable.Map(
"a" -> 10L, "b" -> 20L, "c" -> 25L, "d" -> 10L, "e" -> 5L)
Expand Down Expand Up @@ -183,6 +184,33 @@ class KLLDistanceTest extends WordSpec with SparkContextSpec
assert(distance.isNaN)
}

"Categorial chi-square distance when number of expected categories below minimum" in {
val sample = scala.collection.mutable.Map(
"a" -> 10L, "b" -> 20L)
val expected = scala.collection.mutable.Map(
"b" -> 20L)
val distance = Distance.categoricalDistance(sample, expected, method = ChisquareMethod())
assert(distance.equals(Double.NaN))
}

"Categorial chi-square distance when categories do not match" in {
val sample = scala.collection.mutable.Map(
"a" -> 15L, "b" -> 20L)
val expected = scala.collection.mutable.Map(
"c" -> 20L, "d" -> 20L)
val distance = Distance.categoricalDistance(sample, expected, method = ChisquareMethod())
assert(distance.equals(Double.NaN))
}

"Categorial chi-square distance when number of sample categories is below minimum" in {
val sample = scala.collection.mutable.Map(
"a" -> 30L)
val expected = scala.collection.mutable.Map(
"a" -> 20L, "b" -> 20L)
val distance = Distance.categoricalDistance(sample, expected, method = ChisquareMethod())
assert(distance == 4.3204630539861455E-8)
}

"Population Stability Index (PSI) test with deciles " in {

val expected: List[BucketValue] = List(BucketValue(1.0, 1.05, 428), BucketValue(1.05, 1.1, 425),
Expand Down