Skip to content

Instantly share code, notes, and snippets.

@erose
Created April 4, 2021 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erose/a1ea9c4d62895c796e28525991fe2c1b to your computer and use it in GitHub Desktop.
Save erose/a1ea9c4d62895c796e28525991fe2c1b to your computer and use it in GitHub Desktop.
# A solution to "how do I sort by average rating, while taking into account the fact that some things have more ratings than others and this provides more Bayesian evidence about their quality? See https://www.evanmiller.org/how-not-to-sort-by-average-rating.html
def ci_lower_bound(pos, n)
if n == 0
return 0
end
z = 1.96 # hardcoded confidence level of 0.95
phat = 1.0*pos/n
midpoint = phat + z*z/(2*n)
half_width = z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n)
[(midpoint - half_width)/(1+z*z/n), (midpoint + half_width)/(1+z*z/n)]
end
p ci_lower_bound(81.83, 92.67)
p ci_lower_bound(48, 59)
p ci_lower_bound(152, 157)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment