Skip to content

Instantly share code, notes, and snippets.

@mlshvdv
Created May 2, 2014 16:15
Show Gist options
  • Save mlshvdv/083571cbef018f848fe9 to your computer and use it in GitHub Desktop.
Save mlshvdv/083571cbef018f848fe9 to your computer and use it in GitHub Desktop.
Wilson score formula with range
/**
* Wilson score formula with range
*
* @param {number} sum Objects sum
* @param {number} total Total objects
* @param {object} range Objects (votes) range: [0, 1] or [0, n]
* @param {number} z Confidence level
*/
function wilsonScoreRange(sum, total, range, z) {
// http://amix.dk/blog/post/19588
if (total <= 0 || total < sum) return 0;
// z:
// 95% = 1.644853
// 99% = 2.326348
z = (typeof z == 'number' ? z : 1.644853);
range = (typeof range == 'object' ? range : [0, 1]);
var min = range[0],
width = ( range[1] - min ).toFixed(10),
phat = (sum - total * min) / width / total.toFixed(10),
rating = (phat + z*z / (2*total) - z*Math.sqrt( (phat * (1-phat) + z*z / (4*total) ) / total) ) / (1 + z*z / total);
return rating * width + min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment