-
Notifications
You must be signed in to change notification settings - Fork 541
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
Adding Wilson Score Confidence Interval Strategy #567
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3b41e4c
Configurable RetainCompletenessRule
zeotuan ac337ea
Add doc string
zeotuan db9b764
Add default completeness const
zeotuan 91b1728
Add ConfidenceIntervalStrategy
zeotuan f096cd6
resolve master conflict
zeotuan 8cbffcd
Add Separate Wilson and Wald Interval Test
zeotuan 3a9916f
Add License information, Fix formatting
zeotuan d27cb9b
Add License information
zeotuan 3f849f8
formatting fix
zeotuan 71d6e3f
Update documentation
zeotuan 387ab81
Make WaldInterval the default strategy for now
zeotuan 913c795
Formatting import to per line
zeotuan bfe2c78
Separate group import to per line import
zeotuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/scala/com/amazon/deequ/suggestions/rules/interval/ConfidenceIntervalStrategy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License | ||
* is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.deequ.suggestions.rules.interval | ||
|
||
import breeze.stats.distributions.{Gaussian, Rand} | ||
import com.amazon.deequ.suggestions.rules.interval.ConfidenceIntervalStrategy._ | ||
|
||
/** | ||
* Strategy for calculate confidence interval | ||
* */ | ||
trait ConfidenceIntervalStrategy { | ||
|
||
/** | ||
* Generated confidence interval interval | ||
* @param pHat sample of the population that share a trait | ||
* @param numRecords overall number of records | ||
* @param confidence confidence level of method used to estimate the interval. | ||
* @return | ||
*/ | ||
def calculateTargetConfidenceInterval( | ||
pHat: Double, | ||
numRecords: Long, | ||
confidence: Double = defaultConfidence | ||
): ConfidenceInterval | ||
|
||
def validateInput(pHat: Double, confidence: Double): Unit = { | ||
require(0.0 <= pHat && pHat <= 1.0, "pHat must be between 0.0 and 1.0") | ||
require(0.0 <= confidence && confidence <= 1.0, "confidence must be between 0.0 and 1.0") | ||
} | ||
|
||
def calculateZScore(confidence: Double): Double = Gaussian(0, 1)(Rand).inverseCdf(1 - ((1.0 - confidence)/ 2.0)) | ||
} | ||
|
||
object ConfidenceIntervalStrategy { | ||
val defaultConfidence = 0.95 | ||
val defaultIntervalStrategy: ConfidenceIntervalStrategy = WaldIntervalStrategy() | ||
|
||
case class ConfidenceInterval(lowerBound: Double, upperBound: Double) | ||
} | ||
|
||
|
47 changes: 47 additions & 0 deletions
47
src/main/scala/com/amazon/deequ/suggestions/rules/interval/WaldIntervalStrategy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License | ||
* is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.deequ.suggestions.rules.interval | ||
|
||
import com.amazon.deequ.suggestions.rules.interval.ConfidenceIntervalStrategy.ConfidenceInterval | ||
import com.amazon.deequ.suggestions.rules.interval.ConfidenceIntervalStrategy.defaultConfidence | ||
|
||
import scala.math.BigDecimal.RoundingMode | ||
|
||
/** | ||
* Implements the Wald Interval method for creating a binomial proportion confidence interval. Provided for backwards | ||
* compatibility. using [[WaldIntervalStrategy]] for calculating confidence interval can be problematic when dealing | ||
* with small sample sizes or proportions close to 0 or 1. It also have poorer coverage and might produce confidence | ||
* limit outside the range of [0,1] | ||
* @see <a | ||
* href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Normal_approximation_interval"> | ||
* Normal approximation interval (Wikipedia)</a> | ||
*/ | ||
@deprecated("WilsonScoreIntervalStrategy is recommended for calculating confidence interval") | ||
case class WaldIntervalStrategy() extends ConfidenceIntervalStrategy { | ||
def calculateTargetConfidenceInterval( | ||
pHat: Double, | ||
numRecords: Long, | ||
confidence: Double = defaultConfidence | ||
): ConfidenceInterval = { | ||
validateInput(pHat, confidence) | ||
val successRatio = BigDecimal(pHat) | ||
val marginOfError = BigDecimal(calculateZScore(confidence) * math.sqrt(pHat * (1 - pHat) / numRecords)) | ||
val lowerBound = (successRatio - marginOfError).setScale(2, RoundingMode.DOWN).toDouble | ||
val upperBound = (successRatio + marginOfError).setScale(2, RoundingMode.UP).toDouble | ||
ConfidenceInterval(lowerBound, upperBound) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/scala/com/amazon/deequ/suggestions/rules/interval/WilsonScoreIntervalStrategy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License | ||
* is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.deequ.suggestions.rules.interval | ||
|
||
import com.amazon.deequ.suggestions.rules.interval.ConfidenceIntervalStrategy.ConfidenceInterval | ||
import com.amazon.deequ.suggestions.rules.interval.ConfidenceIntervalStrategy.defaultConfidence | ||
|
||
import scala.math.BigDecimal.RoundingMode | ||
|
||
/** | ||
* Using Wilson score method for creating a binomial proportion confidence interval. | ||
* | ||
* @see <a | ||
* href="http://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval"> | ||
* Wilson score interval (Wikipedia)</a> | ||
*/ | ||
case class WilsonScoreIntervalStrategy() extends ConfidenceIntervalStrategy { | ||
|
||
def calculateTargetConfidenceInterval( | ||
pHat: Double, numRecords: Long, | ||
confidence: Double = defaultConfidence | ||
): ConfidenceInterval = { | ||
validateInput(pHat, confidence) | ||
val zScore = calculateZScore(confidence) | ||
val zSquareOverN = math.pow(zScore, 2) / numRecords | ||
val factor = 1.0 / (1 + zSquareOverN) | ||
val adjustedSuccessRatio = pHat + zSquareOverN/2 | ||
val marginOfError = zScore * math.sqrt(pHat * (1 - pHat)/numRecords + zSquareOverN/(4 * numRecords)) | ||
val lowerBound = BigDecimal(factor * (adjustedSuccessRatio - marginOfError)).setScale(2, RoundingMode.DOWN).toDouble | ||
val upperBound = BigDecimal(factor * (adjustedSuccessRatio + marginOfError)).setScale(2, RoundingMode.UP).toDouble | ||
ConfidenceInterval(lowerBound, upperBound) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently also calculate upperBound for these ConfidenceInterval. At the moment we don't actually make use of the upperBound though