Skip to content

Instantly share code, notes, and snippets.

@Gattermeier
Last active August 17, 2020 18:14
Show Gist options
  • Save Gattermeier/8145b87d6b18b693a8dd6ff865feaee9 to your computer and use it in GitHub Desktop.
Save Gattermeier/8145b87d6b18b693a8dd6ff865feaee9 to your computer and use it in GitHub Desktop.
Lower bound of Wilson score confidence interval for a Bernoulli parameter
// 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