Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save anwarmuhamat/19f07b8aea4c5b966f84 to your computer and use it in GitHub Desktop.

Select an option

Save anwarmuhamat/19f07b8aea4c5b966f84 to your computer and use it in GitHub Desktop.

Revisions

  1. @mbadolato mbadolato revised this gist Jan 6, 2014. 2 changed files with 12 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions WilsonConfidenceIntervalCalculator.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    <?php

    /*
    * (c) Mark Badolato <mbadolato@gmail.com>
    *
    * This content is released under the {@link http://www.opensource.org/licenses/MIT MIT License.}
    */

    namespace Bado\ScoreCalculator;

    /**
    6 changes: 6 additions & 0 deletions WilsonConfidenceIntervalCalculatorTest.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    <?php

    /*
    * (c) Mark Badolato <mbadolato@gmail.com>
    *
    * This content is released under the {@link http://www.opensource.org/licenses/MIT MIT License.}
    */

    namespace Bado\Tests\ScoreCalculator;

    use Bado\ScoreCalculator\WilsonConfidenceIntervalCalculator;
  2. @mbadolato mbadolato revised this gist Jan 4, 2014. 2 changed files with 70 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions WilsonConfidenceIntervalCalculator.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?php

    namespace ScoreCalculator;
    namespace Bado\ScoreCalculator;

    /**
    * Calculate a score based on a Wilson Confidence Interval
    @@ -18,15 +18,15 @@ class WilsonConfidenceIntervalCalculator
    */
    const CONFIDENCE = 2.241403;

    public function calculate($positiveVotes, $totalVotes)
    public function getScore($positiveVotes, $totalVotes)
    {
    return $totalVotes ? $this->average($positiveVotes, $totalVotes) : 0;
    return $totalVotes ? $this->lowerBound($positiveVotes, $totalVotes) : 0;
    }

    private function average($positiveVotes, $totalVotes)
    private function lowerBound($positiveVotes, $totalVotes)
    {
    $avg = 1.0 * $positiveVotes / $totalVotes;
    $numerator = $this->calculationNumerator($totalVotes, self::CONFIDENCE, $avg);
    $phat = 1.0 * $positiveVotes / $totalVotes;
    $numerator = $this->calculationNumerator($totalVotes, self::CONFIDENCE, $phat);
    $denominator = $this->calculationDenominator($totalVotes, self::CONFIDENCE);

    return $numerator / $denominator;
    @@ -37,8 +37,8 @@ private function calculationDenominator($total, $z)
    return 1 + $z * $z / $total;
    }

    private function calculationNumerator($total, $z, $avg)
    private function calculationNumerator($total, $z, $phat)
    {
    return $avg + $z * $z / (2 * $total) - $z * sqrt(($avg * (1 - $avg) + $z * $z / (4 * $total)) / $total);
    return $phat + $z * $z / (2 * $total) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $total)) / $total);
    }
    }
    62 changes: 62 additions & 0 deletions WilsonConfidenceIntervalCalculatorTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php

    namespace Bado\Tests\ScoreCalculator;

    use Bado\ScoreCalculator\WilsonConfidenceIntervalCalculator;

    class WilsonConfidenceIntervalCalculatorTest extends \PHPUnit_Framework_TestCase
    {
    /** @var WilsonConfidenceIntervalCalculator */
    private $calculator;

    /** @test */
    public function calculateFiftyTwoPositiveOutOfSeventySixTotal()
    {
    $this->floatAssertion(0.556480, $this->getScore(52, 76));
    }

    /** @test */
    public function calculateNoPositiveOutOfTenTotal()
    {
    $this->floatAssertion(0, $this->getScore(0, 10));
    }

    /** @test */
    public function calculateNoVotes()
    {
    $this->floatAssertion(0, $this->getScore(0, 0));
    }

    /** @test */
    public function calculateOneOutOfTwo()
    {
    $this->floatAssertion(0.077136, $this->getScore(1, 2));
    }

    /** @test */
    public function calculateTenPositiveOutOfTenTotal()
    {
    $this->floatAssertion(0.665607, $this->getScore(10, 10));
    }

    /** @test */
    public function calculateTenPositiveOutOfTwentyTotal()
    {
    $this->floatAssertion(0.275967, $this->getScore(10, 20));
    }

    protected function setUp()
    {
    $this->calculator = new WilsonConfidenceIntervalCalculator();
    }

    private function floatAssertion($expected, $result)
    {
    $this->assertEquals($expected, $result, '', 0.000001);
    }

    private function getScore($positiveVotes, $totalVotes)
    {
    return $this->calculator->getScore($positiveVotes, $totalVotes);
    }
    }
  3. @mbadolato mbadolato revised this gist Jan 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WilsonConfidenceIntervalCalculator.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    *
    * Based on concepts discussed at @link http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
    */
    class WilsonConfidenceIntervalCalculator implements ScoringAlgorithmInterface
    class WilsonConfidenceIntervalCalculator
    {
    /**
    * Computed value for confidence (z)
  4. @mbadolato mbadolato revised this gist Jan 4, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion WilsonConfidenceIntervalCalculator.php
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,6 @@ class WilsonConfidenceIntervalCalculator implements ScoringAlgorithmInterface
    */
    const CONFIDENCE = 2.241403;

    /** {@inheritDoc} */
    public function calculate($positiveVotes, $totalVotes)
    {
    return $totalVotes ? $this->average($positiveVotes, $totalVotes) : 0;
  5. @mbadolato mbadolato created this gist Jan 4, 2014.
    45 changes: 45 additions & 0 deletions WilsonConfidenceIntervalCalculator.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php

    namespace ScoreCalculator;

    /**
    * Calculate a score based on a Wilson Confidence Interval
    *
    * Based on concepts discussed at @link http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
    */
    class WilsonConfidenceIntervalCalculator implements ScoringAlgorithmInterface
    {
    /**
    * Computed value for confidence (z)
    *
    * These values were computed using Ruby's Statistics2.pnormaldist function
    * 1.959964 = 95.0% confidence
    * 2.241403 = 97.5% confidence
    */
    const CONFIDENCE = 2.241403;

    /** {@inheritDoc} */
    public function calculate($positiveVotes, $totalVotes)
    {
    return $totalVotes ? $this->average($positiveVotes, $totalVotes) : 0;
    }

    private function average($positiveVotes, $totalVotes)
    {
    $avg = 1.0 * $positiveVotes / $totalVotes;
    $numerator = $this->calculationNumerator($totalVotes, self::CONFIDENCE, $avg);
    $denominator = $this->calculationDenominator($totalVotes, self::CONFIDENCE);

    return $numerator / $denominator;
    }

    private function calculationDenominator($total, $z)
    {
    return 1 + $z * $z / $total;
    }

    private function calculationNumerator($total, $z, $avg)
    {
    return $avg + $z * $z / (2 * $total) - $z * sqrt(($avg * (1 - $avg) + $z * $z / (4 * $total)) / $total);
    }
    }