From 25b64d53f0deac61dc028eabb061dc5bbb9d3dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Liljenga=CC=8Ard?= Date: Tue, 24 May 2016 15:12:29 +0200 Subject: [PATCH] Fixing failing tests and misc cleanup --- src/Elo/TwoPlayerEloCalculator.php | 10 ++-- src/Numerics/BasicMath.php | 2 +- src/Numerics/GaussianDistribution.php | 10 ++-- src/RatingContainer.php | 2 +- src/SkillCalculator.php | 6 +-- .../FactorGraphTrueSkillCalculator.php | 10 ++-- .../Factors/GaussianWeightedSumFactor.php | 6 +-- .../Layers/PlayerPriorValuesToSkillsLayer.php | 6 +-- .../PlayerSkillsToPerformancesLayer.php | 4 +- .../TwoPlayerTrueSkillCalculator.php | 26 +++++----- src/TrueSkill/TwoTeamTrueSkillCalculator.php | 48 +++++++++---------- tests/Elo/EloAssert.php | 10 ++-- tests/Numerics/BasicMathTest.php | 7 +-- tests/Numerics/GaussianDistributionTest.php | 43 +++++++++-------- tests/Numerics/MatrixTest.php | 6 +-- tests/RankSorterTest.php | 16 +++---- tests/TestCase.php | 3 +- tests/TrueSkill/DrawMarginTest.php | 6 +-- ...FactorGraphTeamTrueSkillCalculatorTest.php | 4 +- tests/TrueSkill/TrueSkillCalculatorTests.php | 32 ++++++------- 20 files changed, 132 insertions(+), 125 deletions(-) diff --git a/src/Elo/TwoPlayerEloCalculator.php b/src/Elo/TwoPlayerEloCalculator.php index 94059d8..1b9061d 100644 --- a/src/Elo/TwoPlayerEloCalculator.php +++ b/src/Elo/TwoPlayerEloCalculator.php @@ -1,5 +1,7 @@ _kFactor = $kFactor; } - public function calculateNewRatings($gameInfo, + public function calculateNewRatings(GameInfo $gameInfo, array $teamsOfPlayerToRatings, array $teamRanks) { @@ -71,9 +73,9 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator public abstract function getPlayerWinProbability($gameInfo, $playerRating, $opponentRating); - public function calculateMatchQuality($gameInfo, array $teamsOfPlayerToRatings) + public function calculateMatchQuality(GameInfo $gameInfo, array &$teamsOfPlayerToRatings) { - validateTeamCountAndPlayersCountPerTeam($teamsOfPlayerToRatings); + $this->validateTeamCountAndPlayersCountPerTeam($teamsOfPlayerToRatings); $team1 = $teamsOfPlayerToRatings[0]; $team2 = $teamsOfPlayerToRatings[1]; @@ -88,7 +90,7 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator // The TrueSkill paper mentions that they used s1 - s2 (rating difference) to // determine match quality. I convert that to a percentage as a delta from 50% // using the cumulative density function of the specific curve being used - $deltaFrom50Percent = abs(getPlayerWinProbability($gameInfo, $player1Rating, $player2Rating) - 0.5); + $deltaFrom50Percent = abs($this->getPlayerWinProbability($gameInfo, $player1Rating, $player2Rating) - 0.5); return (0.5 - $deltaFrom50Percent) / 0.5; } } \ No newline at end of file diff --git a/src/Numerics/BasicMath.php b/src/Numerics/BasicMath.php index 2929cdf..4422606 100644 --- a/src/Numerics/BasicMath.php +++ b/src/Numerics/BasicMath.php @@ -6,7 +6,7 @@ * @author Jeff Moser * @copyright 2010 Jeff Moser */ -class BasicMatch { +class BasicMath { /** * Squares the input (x^2 = x * x) diff --git a/src/Numerics/GaussianDistribution.php b/src/Numerics/GaussianDistribution.php index 2b176ba..7f4591c 100644 --- a/src/Numerics/GaussianDistribution.php +++ b/src/Numerics/GaussianDistribution.php @@ -21,7 +21,7 @@ class GaussianDistribution { $this->_mean = $mean; $this->_standardDeviation = $standardDeviation; - $this->_variance = BasicMatch::square($standardDeviation); + $this->_variance = BasicMath::square($standardDeviation); if ($this->_variance != 0) { $this->_precision = 1.0 / $this->_variance; @@ -128,7 +128,7 @@ class GaussianDistribution $meanDifference = $left->_mean - $right->_mean; $logSqrt2Pi = log(sqrt(2 * M_PI)); - return -$logSqrt2Pi - (log($varianceSum) / 2.0) - (BasicMatch::square($meanDifference) / (2.0 * $varianceSum)); + return -$logSqrt2Pi - (log($varianceSum) / 2.0) - (BasicMath::square($meanDifference) / (2.0 * $varianceSum)); } public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator) @@ -149,7 +149,7 @@ class GaussianDistribution $logSqrt2Pi = log(sqrt(2 * M_PI)); return log($denominator->_variance) + $logSqrt2Pi - log($varianceDifference) / 2.0 + - BasicMatch::square($meanDifference) / (2 * $varianceDifference); + BasicMath::square($meanDifference) / (2 * $varianceDifference); } public static function at($x, $mean = 0.0, $standardDeviation = 1.0) @@ -160,7 +160,7 @@ class GaussianDistribution // stdDev * sqrt(2*pi) $multiplier = 1.0 / ($standardDeviation * sqrt(2 * M_PI)); - $expPart = exp((-1.0 * BasicMatch::square($x - $mean)) / (2 * BasicMatch::square($standardDeviation))); + $expPart = exp((-1.0 * BasicMath::square($x - $mean)) / (2 * BasicMath::square($standardDeviation))); $result = $multiplier * $expPart; return $result; } @@ -241,7 +241,7 @@ class GaussianDistribution for ($j = 0; $j < 2; $j++) { $err = GaussianDistribution::errorFunctionCumulativeTo($x) - $pp; - $x += $err / (1.12837916709551257 * exp(-BasicMatch::square($x)) - $x * $err); // Halley + $x += $err / (1.12837916709551257 * exp(-BasicMath::square($x)) - $x * $err); // Halley } return ($p < 1.0) ? $x : -$x; diff --git a/src/RatingContainer.php b/src/RatingContainer.php index 1977309..fcf06e3 100644 --- a/src/RatingContainer.php +++ b/src/RatingContainer.php @@ -36,4 +36,4 @@ class RatingContainer { return $this->_playerToRating->count(); } -} \ No newline at end of file +} diff --git a/src/SkillCalculator.php b/src/SkillCalculator.php index a526cdf..dbc7f72 100644 --- a/src/SkillCalculator.php +++ b/src/SkillCalculator.php @@ -26,7 +26,7 @@ abstract class SkillCalculator * * @return All the players and their new ratings. */ - public abstract function calculateNewRatings(GameInfo &$gameInfo, + public abstract function calculateNewRatings(GameInfo $gameInfo, array $teamsOfPlayerToRatings, array $teamRanks); @@ -37,7 +37,7 @@ abstract class SkillCalculator * @param array $teamsOfPlayerToRatings A mapping of team players and their ratings. * @return The quality of the match between the teams as a percentage (0% = bad, 100% = well matched). */ - public abstract function calculateMatchQuality(GameInfo &$gameInfo, + public abstract function calculateMatchQuality(GameInfo $gameInfo, array &$teamsOfPlayerToRatings); public function isSupported($option) @@ -58,7 +58,7 @@ abstract class SkillCalculator $countOfTeams = 0; foreach ($teams as $currentTeam) { - if (!$playersPerTeam->isInRange($currentTeam->count())) { + if (!$playersPerTeam->isInRange(count($currentTeam))) { throw new Exception("Player count is not in range"); } $countOfTeams++; diff --git a/src/TrueSkill/FactorGraphTrueSkillCalculator.php b/src/TrueSkill/FactorGraphTrueSkillCalculator.php index 6a23fa5..613f9bb 100644 --- a/src/TrueSkill/FactorGraphTrueSkillCalculator.php +++ b/src/TrueSkill/FactorGraphTrueSkillCalculator.php @@ -4,7 +4,7 @@ use Moserware\Skills\GameInfo; use Moserware\Skills\Guard; use Moserware\Skills\ISupportPartialPlay; use Moserware\Skills\ISupportPartialUpdate; -use Moserware\Skills\Numerics\BasicMatch; +use Moserware\Skills\Numerics\BasicMath; use Moserware\Skills\Numerics\DiagonalMatrix; use Moserware\Skills\Numerics\Matrix; use Moserware\Skills\Numerics\Vector; @@ -25,7 +25,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator parent::__construct(SkillCalculatorSupportedOptions::PARTIAL_PLAY | SkillCalculatorSupportedOptions::PARTIAL_UPDATE, TeamsRange::atLeast(2), PlayersRange::atLeast(1)); } - public function calculateNewRatings(GameInfo &$gameInfo, + public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks) { @@ -43,7 +43,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator return $factorGraph->getUpdatedRatings(); } - public function calculateMatchQuality(GameInfo &$gameInfo, + public function calculateMatchQuality(GameInfo $gameInfo, array &$teams) { // We need to create the A matrix which is the player team assigments. @@ -55,7 +55,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator $playerTeamAssignmentsMatrix = $this->createPlayerTeamAssignmentMatrix($teamAssignmentsList, $meanVector->getRowCount()); $playerTeamAssignmentsMatrixTranspose = $playerTeamAssignmentsMatrix->getTranspose(); - $betaSquared = BasicMatch::square($gameInfo->getBeta()); + $betaSquared = BasicMath::square($gameInfo->getBeta()); $start = Matrix::multiply($meanVectorTranspose, $playerTeamAssignmentsMatrix); @@ -103,7 +103,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator return new DiagonalMatrix( self::getPlayerRatingValues($teamAssignmentsList, function ($rating) { - return BasicMatch::square($rating->getStandardDeviation()); + return BasicMath::square($rating->getStandardDeviation()); })); } diff --git a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php index 4173019..5d4535f 100644 --- a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -3,7 +3,7 @@ use Moserware\Skills\Guard; use Moserware\Skills\FactorGraphs\Message; use Moserware\Skills\FactorGraphs\Variable; -use Moserware\Skills\Numerics\BasicMatch; +use Moserware\Skills\Numerics\BasicMath; use Moserware\Skills\Numerics\GaussianDistribution; /** @@ -34,7 +34,7 @@ class GaussianWeightedSumFactor extends GaussianFactor for ($i = 0; $i < $variableWeightsLength; $i++) { $weight = &$variableWeights[$i]; $this->_weights[0][$i] = $weight; - $this->_weightsSquared[0][$i] = BasicMatch::square($weight); + $this->_weightsSquared[0][$i] = BasicMath::square($weight); } $variablesToSumLength = count($variablesToSum); @@ -94,7 +94,7 @@ class GaussianWeightedSumFactor extends GaussianFactor $finalWeight = 0; } $currentWeights[$currentDestinationWeightIndex] = $finalWeight; - $currentWeightsSquared[$currentDestinationWeightIndex] = BasicMatch::square($finalWeight); + $currentWeightsSquared[$currentDestinationWeightIndex] = BasicMath::square($finalWeight); $variableIndices[count($variableWeights)] = 0; $this->_variableIndexOrdersForWeights[] = $variableIndices; diff --git a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index d87f9b2..60c160c 100644 --- a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -1,6 +1,6 @@ getMean(), - BasicMatch::square($priorRating->getStandardDeviation()) + - BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()), + BasicMath::square($priorRating->getStandardDeviation()) + + BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()), $skillsVariable ); } diff --git a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index 38602a8..6f2f639 100644 --- a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -2,7 +2,7 @@ use Moserware\Skills\FactorGraphs\ScheduleStep; use Moserware\Skills\FactorGraphs\KeyedVariable; -use Moserware\Skills\Numerics\BasicMatch; +use Moserware\Skills\Numerics\BasicMath; use Moserware\Skills\TrueSkill\TrueSkillFactorGraph; use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor; @@ -37,7 +37,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance) { return new GaussianLikelihoodFactor( - BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getBeta()), + BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()), $playerPerformance, $playerSkill ); diff --git a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php index 93a315c..bb16ff5 100644 --- a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php +++ b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php @@ -2,7 +2,7 @@ use Moserware\Skills\GameInfo; use Moserware\Skills\Guard; -use Moserware\Skills\Numerics\BasicMatch; +use Moserware\Skills\Numerics\BasicMath; use Moserware\Skills\PairwiseComparison; use Moserware\Skills\RankSorter; use Moserware\Skills\Rating; @@ -26,7 +26,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::exactly(1)); } - public function calculateNewRatings(GameInfo &$gameInfo, + public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks) { @@ -74,11 +74,11 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator ); $c = sqrt( - BasicMatch::square($selfRating->getStandardDeviation()) + BasicMath::square($selfRating->getStandardDeviation()) + - BasicMatch::square($opponentRating->getStandardDeviation()) + BasicMath::square($opponentRating->getStandardDeviation()) + - 2 * BasicMatch::square($gameInfo->getBeta()) + 2 * BasicMath::square($gameInfo->getBeta()) ); $winningMean = $selfRating->getMean(); @@ -112,10 +112,10 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator $rankMultiplier = 1; } - $meanMultiplier = (BasicMatch::square($selfRating->getStandardDeviation()) + BasicMatch::square($gameInfo->getDynamicsFactor()))/$c; + $meanMultiplier = (BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor()))/$c; - $varianceWithDynamics = BasicMatch::square($selfRating->getStandardDeviation()) + BasicMatch::square($gameInfo->getDynamicsFactor()); - $stdDevMultiplier = $varianceWithDynamics/BasicMatch::square($c); + $varianceWithDynamics = BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor()); + $stdDevMultiplier = $varianceWithDynamics/BasicMath::square($c); $newMean = $selfRating->getMean() + ($rankMultiplier*$meanMultiplier*$v); $newStdDev = sqrt($varianceWithDynamics*(1 - $w*$stdDevMultiplier)); @@ -126,7 +126,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator /** * {@inheritdoc } */ - public function calculateMatchQuality(GameInfo &$gameInfo, array &$teams) + public function calculateMatchQuality(GameInfo $gameInfo, array &$teams) { Guard::argumentNotNull($gameInfo, "gameInfo"); $this->validateTeamCountAndPlayersCountPerTeam($teams); @@ -141,9 +141,9 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator $player2Rating = $team2Ratings[0]; // We just use equation 4.1 found on page 8 of the TrueSkill 2006 paper: - $betaSquared = BasicMatch::square($gameInfo->getBeta()); - $player1SigmaSquared = BasicMatch::square($player1Rating->getStandardDeviation()); - $player2SigmaSquared = BasicMatch::square($player2Rating->getStandardDeviation()); + $betaSquared = BasicMath::square($gameInfo->getBeta()); + $player1SigmaSquared = BasicMath::square($player1Rating->getStandardDeviation()); + $player2SigmaSquared = BasicMath::square($player2Rating->getStandardDeviation()); // This is the square root part of the equation: $sqrtPart = @@ -155,7 +155,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator // This is the exponent part of the equation: $expPart = exp( - (-1*BasicMatch::square($player1Rating->getMean() - $player2Rating->getMean())) + (-1*BasicMath::square($player1Rating->getMean() - $player2Rating->getMean())) / (2*(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared))); diff --git a/src/TrueSkill/TwoTeamTrueSkillCalculator.php b/src/TrueSkill/TwoTeamTrueSkillCalculator.php index 826b344..9d77ce1 100644 --- a/src/TrueSkill/TwoTeamTrueSkillCalculator.php +++ b/src/TrueSkill/TwoTeamTrueSkillCalculator.php @@ -2,7 +2,7 @@ use Moserware\Skills\GameInfo; use Moserware\Skills\Guard; -use Moserware\Skills\Numerics\BasicMatch; +use Moserware\Skills\Numerics\BasicMath; use Moserware\Skills\PairwiseComparison; use Moserware\Skills\RankSorter; use Moserware\Skills\Rating; @@ -27,9 +27,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::atLeast(1)); } - public function calculateNewRatings(GameInfo &$gameInfo, - array $teams, - array $teamRanks) + public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks) { Guard::argumentNotNull($gameInfo, "gameInfo"); $this->validateTeamCountAndPlayersCountPerTeam($teams); @@ -69,8 +67,8 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator $gameInfo->getBeta() ); - $betaSquared = BasicMatch::square($gameInfo->getBeta()); - $tauSquared = BasicMatch::square($gameInfo->getDynamicsFactor()); + $betaSquared = BasicMath::square($gameInfo->getBeta()); + $tauSquared = BasicMath::square($gameInfo->getDynamicsFactor()); $totalPlayers = $selfTeam->count() + $otherTeam->count(); @@ -78,17 +76,17 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator return $currentRating->getMean(); }; - $selfMeanSum = BasicMatch::sum($selfTeam->getAllRatings(), $meanGetter); - $otherTeamMeanSum = BasicMatch::sum($otherTeam->getAllRatings(), $meanGetter); + $selfMeanSum = BasicMath::sum($selfTeam->getAllRatings(), $meanGetter); + $otherTeamMeanSum = BasicMath::sum($otherTeam->getAllRatings(), $meanGetter); $varianceGetter = function ($currentRating) { - return BasicMatch::square($currentRating->getStandardDeviation()); + return BasicMath::square($currentRating->getStandardDeviation()); }; $c = sqrt( - BasicMatch::sum($selfTeam->getAllRatings(), $varianceGetter) + BasicMath::sum($selfTeam->getAllRatings(), $varianceGetter) + - BasicMatch::sum($otherTeam->getAllRatings(), $varianceGetter) + BasicMath::sum($otherTeam->getAllRatings(), $varianceGetter) + $totalPlayers * $betaSquared ); @@ -126,24 +124,24 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator $localSelfTeamCurrentPlayer = &$selfTeamCurrentPlayer; $previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer); - $meanMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c; - $stdDevMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMatch::square($c); + $meanMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c; + $stdDevMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMath::square($c); $playerMeanDelta = ($rankMultiplier * $meanMultiplier * $v); $newMean = $previousPlayerRating->getMean() + $playerMeanDelta; - $newStdDev = - sqrt((BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier)); + $newStdDev = sqrt( + (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier) + ); $newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev)); } } /** - * {@inheritdoc } + * {@inheritdoc} */ - public function calculateMatchQuality(GameInfo &$gameInfo, - array &$teams) + public function calculateMatchQuality(GameInfo $gameInfo, array &$teams) { Guard::argumentNotNull($gameInfo, "gameInfo"); $this->validateTeamCountAndPlayersCountPerTeam($teams); @@ -157,21 +155,21 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator $totalPlayers = $team1Count + $team2Count; - $betaSquared = BasicMatch::square($gameInfo->getBeta()); + $betaSquared = BasicMath::square($gameInfo->getBeta()); $meanGetter = function ($currentRating) { return $currentRating->getMean(); }; $varianceGetter = function ($currentRating) { - return BasicMatch::square($currentRating->getStandardDeviation()); + return BasicMath::square($currentRating->getStandardDeviation()); }; - $team1MeanSum = BasicMatch::sum($team1Ratings, $meanGetter); - $team1StdDevSquared = BasicMatch::sum($team1Ratings, $varianceGetter); + $team1MeanSum = BasicMath::sum($team1Ratings, $meanGetter); + $team1StdDevSquared = BasicMath::sum($team1Ratings, $varianceGetter); - $team2MeanSum = BasicMatch::sum($team2Ratings, $meanGetter); - $team2SigmaSquared = BasicMatch::sum($team2Ratings, $varianceGetter); + $team2MeanSum = BasicMath::sum($team2Ratings, $meanGetter); + $team2SigmaSquared = BasicMath::sum($team2Ratings, $varianceGetter); // This comes from equation 4.1 in the TrueSkill paper on page 8 // The equation was broken up into the part under the square root sign and @@ -184,7 +182,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator ); $expPart = exp( - (-1 * BasicMatch::square($team1MeanSum - $team2MeanSum)) + (-1 * BasicMath::square($team1MeanSum - $team2MeanSum)) / (2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared)) ); diff --git a/tests/Elo/EloAssert.php b/tests/Elo/EloAssert.php index 4e99b6a..64eeb0c 100644 --- a/tests/Elo/EloAssert.php +++ b/tests/Elo/EloAssert.php @@ -1,15 +1,18 @@ calculateNewRatings( $chessGameInfo, $teams, - $ranks); + $ranks + ); $testClass->assertEquals($player1AfterRating, $result[$player1]->getMean(), '', self::ERROR_TOLERANCE); $testClass->assertEquals($player2AfterRating, $result[$player2]->getMean(), '', self::ERROR_TOLERANCE); diff --git a/tests/Numerics/BasicMathTest.php b/tests/Numerics/BasicMathTest.php index 693a6ee..add3fe6 100644 --- a/tests/Numerics/BasicMathTest.php +++ b/tests/Numerics/BasicMathTest.php @@ -1,13 +1,14 @@ assertEquals(1, Moserware\Numerics\square(1)); - $this->assertEquals(1.44, Moserware\Numerics\square(1.2)); - $this->assertEquals(4, Moserware\Numerics\square(2)); + $this->assertEquals(1, BasicMath::square(1)); + $this->assertEquals(1.44, BasicMath::square(1.2)); + $this->assertEquals(4, BasicMath::square(2)); } } \ No newline at end of file diff --git a/tests/Numerics/GaussianDistributionTest.php b/tests/Numerics/GaussianDistributionTest.php index 44601e5..bb8bf87 100644 --- a/tests/Numerics/GaussianDistributionTest.php +++ b/tests/Numerics/GaussianDistributionTest.php @@ -1,33 +1,34 @@ assertEquals( 0.691462, GaussianDistribution::cumulativeTo(0.5),'', GaussianDistributionTest::ERROR_TOLERANCE); + $this->assertEquals(0.691462, GaussianDistribution::cumulativeTo(0.5), '', GaussianDistributionTest::ERROR_TOLERANCE); } - + public function testAt() { // Verified with WolframAlpha // (e.g. http://www.wolframalpha.com/input/?i=PDF%5BNormalDistribution%5B0%2C1%5D%2C+0.5%5D ) $this->assertEquals(0.352065, GaussianDistribution::at(0.5), '', GaussianDistributionTest::ERROR_TOLERANCE); } - + public function testMultiplication() { // I verified this against the formula at http://www.tina-vision.net/tina-knoppix/tina-memo/2003-003.pdf - $standardNormal = new GaussianDistribution(0, 1); + $standardNormal = new GaussianDistribution(0, 1); $shiftedGaussian = new GaussianDistribution(2, 3); $product = GaussianDistribution::multiply($standardNormal, $shiftedGaussian); - + $this->assertEquals(0.2, $product->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE); $this->assertEquals(3.0 / sqrt(10), $product->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE); @@ -35,14 +36,14 @@ class GaussianDistributionTest extends TestCase $m6s7 = new GaussianDistribution(6, 7); $product2 = GaussianDistribution::multiply($m4s5, $m6s7); - - $expectedMean = (4 * BasicMatch::square(7) + 6 * BasicMatch::square(5)) / (BasicMatch::square(5) + BasicMatch::square(7)); + + $expectedMean = (4 * BasicMath::square(7) + 6 * BasicMath::square(5)) / (BasicMath::square(5) + BasicMath::square(7)); $this->assertEquals($expectedMean, $product2->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE); - $expectedSigma = sqrt(((BasicMatch::square(5) * BasicMatch::square(7)) / (BasicMatch::square(5) + BasicMatch::square(7)))); + $expectedSigma = sqrt(((BasicMath::square(5) * BasicMath::square(7)) / (BasicMath::square(5) + BasicMath::square(7)))); $this->assertEquals($expectedSigma, $product2->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE); } - + public function testDivision() { // Since the multiplication was worked out by hand, we use the same numbers but work backwards @@ -51,15 +52,15 @@ class GaussianDistributionTest extends TestCase $productDividedByStandardNormal = GaussianDistribution::divide($product, $standardNormal); $this->assertEquals(2.0, $productDividedByStandardNormal->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE); - $this->assertEquals(3.0, $productDividedByStandardNormal->getStandardDeviation(),'', GaussianDistributionTest::ERROR_TOLERANCE); - - $product2 = new GaussianDistribution((4 * BasicMatch::square(7) + 6 * BasicMatch::square(5)) / (BasicMatch::square(5) + BasicMatch::square(7)), sqrt(((BasicMatch::square(5) * BasicMatch::square(7)) / (BasicMatch::square(5) + BasicMatch::square(7))))); - $m4s5 = new GaussianDistribution(4,5); + $this->assertEquals(3.0, $productDividedByStandardNormal->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE); + + $product2 = new GaussianDistribution((4 * BasicMath::square(7) + 6 * BasicMath::square(5)) / (BasicMath::square(5) + BasicMath::square(7)), sqrt(((BasicMath::square(5) * BasicMath::square(7)) / (BasicMath::square(5) + BasicMath::square(7))))); + $m4s5 = new GaussianDistribution(4, 5); $product2DividedByM4S5 = GaussianDistribution::divide($product2, $m4s5); $this->assertEquals(6.0, $product2DividedByM4S5->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE); $this->assertEquals(7.0, $product2DividedByM4S5->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE); } - + public function testLogProductNormalization() { // Verified with Ralf Herbrich's F# implementation @@ -72,16 +73,16 @@ class GaussianDistributionTest extends TestCase $lpn2 = GaussianDistribution::logProductNormalization($m1s2, $m3s4); $this->assertEquals(-2.5168046699816684, $lpn2, '', GaussianDistributionTest::ERROR_TOLERANCE); } - + public function testLogRatioNormalization() { // Verified with Ralf Herbrich's F# implementation $m1s2 = new GaussianDistribution(1, 2); $m3s4 = new GaussianDistribution(3, 4); $lrn = GaussianDistribution::logRatioNormalization($m1s2, $m3s4); - $this->assertEquals(2.6157405972171204, $lrn, '', GaussianDistributionTest::ERROR_TOLERANCE); + $this->assertEquals(2.6157405972171204, $lrn, '', GaussianDistributionTest::ERROR_TOLERANCE); } - + public function testAbsoluteDifference() { // Verified with Ralf Herbrich's F# implementation diff --git a/tests/Numerics/MatrixTest.php b/tests/Numerics/MatrixTest.php index 413c048..0cf43ba 100644 --- a/tests/Numerics/MatrixTest.php +++ b/tests/Numerics/MatrixTest.php @@ -1,8 +1,8 @@ 1, "b" => 2 ); - $team2 = array( "c" => 3, "d" => 4 ); - $team3 = array( "e" => 5, "f" => 6 ); - + $team1 = array("a" => 1, "b" => 2); + $team2 = array("c" => 3, "d" => 4); + $team3 = array("e" => 5, "f" => 6); + $teams = array($team1, $team2, $team3); - + $teamRanks = array(3, 1, 2); - + $sortedRanks = RankSorter::sort($teams, $teamRanks); - - $this->assertEquals($team2, $sortedRanks[0]); + + $this->assertEquals($team2, $sortedRanks[0]); $this->assertEquals($team3, $sortedRanks[1]); $this->assertEquals($team1, $sortedRanks[2]); } diff --git a/tests/TestCase.php b/tests/TestCase.php index ff93132..1faf66a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,4 +1,5 @@ assertEquals($expected, $actual, '', DrawMarginTest::ERROR_TOLERANCE); - } + } } \ No newline at end of file diff --git a/tests/TrueSkill/FactorGraphTeamTrueSkillCalculatorTest.php b/tests/TrueSkill/FactorGraphTeamTrueSkillCalculatorTest.php index 6c3bbf9..dc769d3 100644 --- a/tests/TrueSkill/FactorGraphTeamTrueSkillCalculatorTest.php +++ b/tests/TrueSkill/FactorGraphTeamTrueSkillCalculatorTest.php @@ -6,9 +6,9 @@ use Moserware\Skills\TrueSkill\FactorGraphTrueSkillCalculator; class FactorGraphTrueSkillCalculatorTest extends TestCase { public function testFactorGraphTrueSkillCalculator() - { + { $calculator = new FactorGraphTrueSkillCalculator(); - + TrueSkillCalculatorTests::testAllTwoPlayerScenarios($this, $calculator); TrueSkillCalculatorTests::testAllTwoTeamScenarios($this, $calculator); TrueSkillCalculatorTests::testAllMultipleTeamScenarios($this, $calculator); diff --git a/tests/TrueSkill/TrueSkillCalculatorTests.php b/tests/TrueSkill/TrueSkillCalculatorTests.php index 802505e..7fb2937 100644 --- a/tests/TrueSkill/TrueSkillCalculatorTests.php +++ b/tests/TrueSkill/TrueSkillCalculatorTests.php @@ -17,14 +17,14 @@ class TrueSkillCalculatorTests public static function testAllTwoPlayerScenarios($testClass, SkillCalculator $calculator) { self::twoPlayerTestNotDrawn($testClass, $calculator); - self::twoPlayerTestDrawn($testClass, $calculator); + self::twoPlayerTestDrawn($testClass, $calculator); self::twoPlayerChessTestNotDrawn($testClass, $calculator); self::oneOnOneMassiveUpsetDrawTest($testClass, $calculator); } - + public static function testAllTwoTeamScenarios($testClass, SkillCalculator $calculator) { - self::oneOnTwoSimpleTest($testClass, $calculator); + self::oneOnTwoSimpleTest($testClass, $calculator); self::oneOnTwoSomewhatBalanced($testClass, $calculator); self::oneOnTwoDrawTest($testClass, $calculator); self::oneOnThreeDrawTest($testClass, $calculator); @@ -75,7 +75,7 @@ class TrueSkillCalculatorTests private static function twoPlayerTestNotDrawn($testClass, SkillCalculator $calculator) { $player1 = new Player(1); - $player2 = new Player(2); + $player2 = new Player(2); $gameInfo = new GameInfo(); $team1 = new Team($player1, $gameInfo->getDefaultRating()); @@ -621,7 +621,7 @@ class TrueSkillCalculatorTests $team1 = new Team($player1, $gameInfo->getDefaultRating()); $team2 = new Team($player2, $gameInfo->getDefaultRating()); $team3 = new Team($player3, $gameInfo->getDefaultRating()); - + $teams = Teams::concat($team1, $team2, $team3); $newRatings = $calculator->calculateNewRatings($gameInfo, $teams, array(1, 1, 1)); @@ -847,14 +847,14 @@ class TrueSkillCalculatorTests $team16 = new Team($player16, $gameInfo->getDefaultRating()); $teams = Teams::concat( - $team1, $team2, $team3, $team4, $team5, - $team6, $team7, $team8, $team9, $team10, - $team11, $team12, $team13, $team14, $team15, - $team16); + $team1, $team2, $team3, $team4, $team5, + $team6, $team7, $team8, $team9, $team10, + $team11, $team12, $team13, $team14, $team15, + $team16); $newRatings = $calculator->calculateNewRatings( - $gameInfo, $teams, - array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)); + $gameInfo, $teams, + array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)); $player1NewRating = $newRatings->getRating($player1); self::assertRating($testClass, 40.53945776946920, 5.27581643889050, $player1NewRating); @@ -913,8 +913,8 @@ class TrueSkillCalculatorTests $gameInfo = new GameInfo(); $team1 = new Team(); - $team1->addPlayer($player1, new Rating(40,4)); - $team1->addPlayer($player2, new Rating(45,3)); + $team1->addPlayer($player1, new Rating(40, 4)); + $team1->addPlayer($player2, new Rating(45, 3)); $player3 = new Player(3); $player4 = new Player(4); @@ -931,8 +931,8 @@ class TrueSkillCalculatorTests $player8 = new Player(8); $team3 = new Team(); - $team3->addPlayer($player7, new Rating(50,5)); - $team3->addPlayer($player8, new Rating(30,2)); + $team3->addPlayer($player7, new Rating(50, 5)); + $team3->addPlayer($player8, new Rating(30, 2)); $teams = Teams::concat($team1, $team2, $team3); $newRatingsWinLose = $calculator->calculateNewRatings($gameInfo, $teams, array(1, 2, 2)); @@ -986,7 +986,7 @@ class TrueSkillCalculatorTests private static function assertRating($testClass, $expectedMean, $expectedStandardDeviation, $actual) { - $testClass->assertEquals($expectedMean, $actual->getMean(), '', self::ERROR_TOLERANCE_TRUESKILL); + $testClass->assertEquals($expectedMean, $actual->getMean(), '', self::ERROR_TOLERANCE_TRUESKILL); $testClass->assertEquals($expectedStandardDeviation, $actual->getStandardDeviation(), '', self::ERROR_TOLERANCE_TRUESKILL); }