mirror of
https://github.com/furyfire/trueskill.git
synced 2025-01-15 17:37:39 +00:00
Fixing failing tests and misc cleanup
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
<?php namespace Moserware\Skills\Elo;
|
||||
|
||||
use Exception;
|
||||
use Moserware\Skills\GameInfo;
|
||||
use Moserware\Skills\PairwiseComparison;
|
||||
use Moserware\Skills\RankSorter;
|
||||
use Moserware\Skills\SkillCalculator;
|
||||
@ -18,7 +20,7 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator
|
||||
$this->_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;
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
* @author Jeff Moser <jeff@moserware.com>
|
||||
* @copyright 2010 Jeff Moser
|
||||
*/
|
||||
class BasicMatch {
|
||||
class BasicMath {
|
||||
|
||||
/**
|
||||
* Squares the input (x^2 = x * x)
|
||||
|
@ -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;
|
||||
|
@ -36,4 +36,4 @@ class RatingContainer
|
||||
{
|
||||
return $this->_playerToRating->count();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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++;
|
||||
|
@ -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());
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
|
||||
|
||||
use Moserware\Skills\Numerics\BasicMatch;
|
||||
use Moserware\Skills\Numerics\BasicMath;
|
||||
use Moserware\Skills\Rating;
|
||||
use Moserware\Skills\FactorGraphs\ScheduleStep;
|
||||
use Moserware\Skills\FactorGraphs\Variable;
|
||||
@ -57,8 +57,8 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
|
||||
{
|
||||
return new GaussianPriorFactor(
|
||||
$priorRating->getMean(),
|
||||
BasicMatch::square($priorRating->getStandardDeviation()) +
|
||||
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
|
||||
BasicMath::square($priorRating->getStandardDeviation()) +
|
||||
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
|
||||
$skillsVariable
|
||||
);
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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)));
|
||||
|
||||
|
@ -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))
|
||||
);
|
||||
|
@ -1,15 +1,18 @@
|
||||
<?php namespace Moserware\Skills\Tests\Elo;
|
||||
|
||||
use Moserware\Skills\Elo\EloRating;
|
||||
use Moserware\Skills\Elo\FideEloCalculator;
|
||||
use Moserware\Skills\GameInfo;
|
||||
use Moserware\Skills\PairwiseComparison;
|
||||
use Moserware\Skills\Tests\TestCase;
|
||||
|
||||
class EloAssert
|
||||
{
|
||||
const ERROR_TOLERANCE = 0.1;
|
||||
|
||||
public static function assertChessRating(
|
||||
$testClass,
|
||||
$twoPlayerEloCalculator,
|
||||
TestCase $testClass,
|
||||
FideEloCalculator $twoPlayerEloCalculator,
|
||||
$player1BeforeRating,
|
||||
$player2BeforeRating,
|
||||
$player1Result,
|
||||
@ -31,7 +34,8 @@ class EloAssert
|
||||
$result = $twoPlayerEloCalculator->calculateNewRatings(
|
||||
$chessGameInfo,
|
||||
$teams,
|
||||
$ranks);
|
||||
$ranks
|
||||
);
|
||||
|
||||
$testClass->assertEquals($player1AfterRating, $result[$player1]->getMean(), '', self::ERROR_TOLERANCE);
|
||||
$testClass->assertEquals($player2AfterRating, $result[$player2]->getMean(), '', self::ERROR_TOLERANCE);
|
||||
|
@ -1,13 +1,14 @@
|
||||
<?php namespace Moserware\Skills\Tests\Numerics;
|
||||
|
||||
use Moserware\Skills\Numerics\BasicMath;
|
||||
use Moserware\Skills\Tests\TestCase;
|
||||
|
||||
class BasicMathTest extends TestCase
|
||||
{
|
||||
public function testSquare()
|
||||
{
|
||||
$this->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));
|
||||
}
|
||||
}
|
@ -1,33 +1,34 @@
|
||||
<?php namespace Moserware\Skills\Tests\Numerics;
|
||||
|
||||
use Moserware\Numerics\GaussianDistribution;
|
||||
use Moserware\Skills\Numerics\BasicMath;
|
||||
use Moserware\Skills\Numerics\GaussianDistribution;
|
||||
use Moserware\Skills\Tests\TestCase;
|
||||
|
||||
class GaussianDistributionTest extends TestCase
|
||||
{
|
||||
{
|
||||
const ERROR_TOLERANCE = 0.000001;
|
||||
|
||||
|
||||
public function testCumulativeTo()
|
||||
{
|
||||
{
|
||||
// Verified with WolframAlpha
|
||||
// (e.g. http://www.wolframalpha.com/input/?i=CDF%5BNormalDistribution%5B0%2C1%5D%2C+0.5%5D )
|
||||
$this->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
|
||||
|
@ -1,8 +1,8 @@
|
||||
<?php namespace Moserware\Skills\Tests\Numerics;
|
||||
|
||||
use Moserware\Numerics\Matrix;
|
||||
use Moserware\Numerics\IdentityMatrix;
|
||||
use Moserware\Numerics\SquareMatrix;
|
||||
use Moserware\Skills\Numerics\IdentityMatrix;
|
||||
use Moserware\Skills\Numerics\Matrix;
|
||||
use Moserware\Skills\Numerics\SquareMatrix;
|
||||
use Moserware\Skills\Tests\TestCase;
|
||||
|
||||
class MatrixTest extends TestCase
|
||||
|
@ -6,17 +6,17 @@ class RankSorterTest extends TestCase
|
||||
{
|
||||
public function testSort()
|
||||
{
|
||||
$team1 = array( "a" => 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]);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php namespace Moserware\Skills\Tests;
|
||||
|
||||
class TestCase extends \PHPUnit_Framework_TestCase {
|
||||
class TestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
}
|
@ -4,9 +4,9 @@ use Moserware\Skills\Tests\TestCase;
|
||||
use Moserware\Skills\TrueSkill\DrawMargin;
|
||||
|
||||
class DrawMarginTest extends TestCase
|
||||
{
|
||||
{
|
||||
const ERROR_TOLERANCE = 0.000001;
|
||||
|
||||
|
||||
public function testGetDrawMarginFromDrawProbability()
|
||||
{
|
||||
$beta = 25.0 / 6.0;
|
||||
@ -20,5 +20,5 @@ class DrawMarginTest extends TestCase
|
||||
{
|
||||
$actual = DrawMargin::getDrawMarginFromDrawProbability($drawProbability, $beta);
|
||||
$this->assertEquals($expected, $actual, '', DrawMarginTest::ERROR_TOLERANCE);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user