Last active
September 25, 2023 02:45
-
-
Save dfabulich/fc6b13a8bffc5518c4731347de642749 to your computer and use it in GitHub Desktop.
Evan Miller Ranking Items with Star Ratings, in JavaScript
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
"use strict"; | |
// based on unutbu's stackoverflow answer | |
// https://stackoverflow.com/a/40958702/54829 | |
// which is based on Evan Miller's blog post | |
// http://www.evanmiller.org/ranking-items-with-star-ratings.html | |
function starsort(ratings) { | |
function sum(array) { return array.reduce((x, y) => x + y, 0) }; | |
function square(x) { return x * x; }; | |
const confidenceZ = 1.65; | |
// include five fake reviews | |
// 1 1-star, 1 2-star, 1 3-star, 1 4-star, 1 5-star | |
const fakeRatings = ratings.map(count => count + 1); | |
const N = sum(fakeRatings); | |
const average = sum(fakeRatings.map((count, i) => (i + 1) * count)) / N; | |
// Dirichlet standard deviation of average | |
const x = sum(fakeRatings.map((count, i) => square(i + 1) * count)) / N; | |
const standardDeviation = Math.sqrt((x - square(average)) / (N + 1)); | |
return average - confidenceZ * standardDeviation; | |
} | |
console.log(starsort([60, 80, 75, 20, 25])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a Go version, for anyone searching like I was: