Skip to content

Instantly share code, notes, and snippets.

@timelf123
Forked from Gattermeier/lower_bound.js
Created September 22, 2017 14:08
Show Gist options
  • Save timelf123/dadca20e7faa17969d3eb6ee375e2c98 to your computer and use it in GitHub Desktop.
Save timelf123/dadca20e7faa17969d3eb6ee375e2c98 to your computer and use it in GitHub Desktop.

Revisions

  1. @Gattermeier Gattermeier created this gist Apr 13, 2017.
    16 changes: 16 additions & 0 deletions lower_bound.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    // Node.js implementation of Evan Miller's algorithm for ranking stuff based on upvotes:
    // http://www.evanmiller.org/how-not-to-sort-by-average-rating.html

    const stats = require('simple-statistics')

    const lower_bound = (upvotes, n = 0, confidence = 0.95) => {
    if (n === 0) return 0

    // for performance purposes you might consider memoize the calcuation for z
    const z = stats.probit(1-(1-confidence)/2)

    // p̂, the fraction of upvotes
    const phat = 1.0 * upvotes / n

    return (phat + z*z / (2*n) - z * Math.sqrt((phat * (1 - phat) + z*z / (4*n)) / n)) / (1 + z*z/n)
    }