Last active
August 17, 2020 18:14
-
-
Save Gattermeier/8145b87d6b18b693a8dd6ff865feaee9 to your computer and use it in GitHub Desktop.
Lower bound of Wilson score confidence interval for a Bernoulli parameter
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
// Golang implementation of Evan Miller's algorithm for ranking stuff based on upvotes: | |
// http://www.evanmiller.org/how-not-to-sort-by-average-rating.html | |
// playground link: https://play.golang.org/p/dV6yk6pBY5y | |
func lowerBound(upvotes, n int, confidence float64) (float64, error) { | |
if n == 0 { | |
return 0, errors.New("n can't be 0") | |
} | |
floatN := float64(n) | |
phat := float64(1.0 * upvotes / n) | |
z := math.Erfinv(1 - (1-confidence)/2) | |
result := (phat + z*z/(2*floatN) - z*math.Sqrt((phat*(1-phat)+z*z/(4*floatN))/floatN)) / (1 + z*z/floatN) | |
return result, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment