wilson-interval-func
Version:
Calculate pearson correlation rank
65 lines (57 loc) • 1.45 kB
JavaScript
import pnormaldist from 'pnormaldist';
export default class WilsonInterval {
/**
*
* @param positiveVotes
* @param totalVotes
* @returns {number}
*/
static calc(positiveVotes, totalVotes) {
const z = pnormaldist(1 - (1 - WilsonInterval.CONFIDENCE) / 2);
return totalVotes ? WilsonInterval.lowerBound(positiveVotes, totalVotes, z) : 0;
}
/**
*
* @param positiveVotes
* @param totalVotes
* @param confidence
* @returns {number}
*/
static lowerBound(positiveVotes, totalVotes, confidence)
{
const phat = 1.0 * positiveVotes / totalVotes,
numerator = WilsonInterval.calcNumerator(totalVotes, confidence, phat),
denominator = WilsonInterval.calcDenominator(totalVotes, confidence);
return numerator / denominator;
}
/**
*
* @param total
* @param z
* @returns {number}
*/
static calcDenominator(total, z)
{
return 1 + z * z / total;
}
/**
*
* @param total
* @param z
* @param phat
* @returns {number}
*/
static calcNumerator(total, z, phat)
{
return phat + z * z / (2 * total) - z * Math.sqrt((phat * (1 - phat) + z * z / (4 * total)) / total);
}
/**
*
* @returns {number}
* @constructor
*/
static get CONFIDENCE() {
return .975;
}
}
module.exports = WilsonInterval;