Skip to content

Instantly share code, notes, and snippets.

@vi

vi/wilson.pl

Last active Aug 29, 2015
Embed
What would you like to do?
Simple Perl script for playing with formula from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
#!/usr/bin/perl -wln
# Calculate lower and upper bounds of Wilson confidence interval based on upvotes and downvotes
# Based on SQL expression from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
# Confidence parameter is hardcoded at 95%
# Each input line is two numbers: number_of_upvotes <whitespace> number_of_downvotes
# Each output line is two numbers: minimal_estimated_ratio_of_upvoters <space> maximal_estimated_ratio_of_upvoters
use strict;
INIT { $|=1; }
my ($positive, $negative) = /\S+/g;
my $total = $positive + $negative;
$total=0.0001 if $total==0;
my $lower = (($positive + 1.9208) / $total - 1.96 * sqrt(($positive * $negative) / $total + 0.9604) / $total) / (1 + 3.8416 / $total);
my $upper = (($positive + 1.9208) / $total + 1.96 * sqrt(($positive * $negative) / $total + 0.9604) / $total) / (1 + 3.8416 / $total);
print "$lower $upper";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.