mirror of
https://github.com/furyfire/trueskill.git
synced 2025-01-16 01:47:39 +00:00
Fixing failing tests and misc cleanup
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
<?php namespace Moserware\Skills\Elo;
|
<?php namespace Moserware\Skills\Elo;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Moserware\Skills\GameInfo;
|
||||||
use Moserware\Skills\PairwiseComparison;
|
use Moserware\Skills\PairwiseComparison;
|
||||||
use Moserware\Skills\RankSorter;
|
use Moserware\Skills\RankSorter;
|
||||||
use Moserware\Skills\SkillCalculator;
|
use Moserware\Skills\SkillCalculator;
|
||||||
@ -18,7 +20,7 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator
|
|||||||
$this->_kFactor = $kFactor;
|
$this->_kFactor = $kFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculateNewRatings($gameInfo,
|
public function calculateNewRatings(GameInfo $gameInfo,
|
||||||
array $teamsOfPlayerToRatings,
|
array $teamsOfPlayerToRatings,
|
||||||
array $teamRanks)
|
array $teamRanks)
|
||||||
{
|
{
|
||||||
@ -71,9 +73,9 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator
|
|||||||
|
|
||||||
public abstract function getPlayerWinProbability($gameInfo, $playerRating, $opponentRating);
|
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];
|
$team1 = $teamsOfPlayerToRatings[0];
|
||||||
$team2 = $teamsOfPlayerToRatings[1];
|
$team2 = $teamsOfPlayerToRatings[1];
|
||||||
|
|
||||||
@ -88,7 +90,7 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator
|
|||||||
// The TrueSkill paper mentions that they used s1 - s2 (rating difference) to
|
// 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%
|
// 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
|
// 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;
|
return (0.5 - $deltaFrom50Percent) / 0.5;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,7 +6,7 @@
|
|||||||
* @author Jeff Moser <jeff@moserware.com>
|
* @author Jeff Moser <jeff@moserware.com>
|
||||||
* @copyright 2010 Jeff Moser
|
* @copyright 2010 Jeff Moser
|
||||||
*/
|
*/
|
||||||
class BasicMatch {
|
class BasicMath {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Squares the input (x^2 = x * x)
|
* Squares the input (x^2 = x * x)
|
||||||
|
@ -21,7 +21,7 @@ class GaussianDistribution
|
|||||||
{
|
{
|
||||||
$this->_mean = $mean;
|
$this->_mean = $mean;
|
||||||
$this->_standardDeviation = $standardDeviation;
|
$this->_standardDeviation = $standardDeviation;
|
||||||
$this->_variance = BasicMatch::square($standardDeviation);
|
$this->_variance = BasicMath::square($standardDeviation);
|
||||||
|
|
||||||
if ($this->_variance != 0) {
|
if ($this->_variance != 0) {
|
||||||
$this->_precision = 1.0 / $this->_variance;
|
$this->_precision = 1.0 / $this->_variance;
|
||||||
@ -128,7 +128,7 @@ class GaussianDistribution
|
|||||||
$meanDifference = $left->_mean - $right->_mean;
|
$meanDifference = $left->_mean - $right->_mean;
|
||||||
|
|
||||||
$logSqrt2Pi = log(sqrt(2 * M_PI));
|
$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)
|
public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator)
|
||||||
@ -149,7 +149,7 @@ class GaussianDistribution
|
|||||||
$logSqrt2Pi = log(sqrt(2 * M_PI));
|
$logSqrt2Pi = log(sqrt(2 * M_PI));
|
||||||
|
|
||||||
return log($denominator->_variance) + $logSqrt2Pi - log($varianceDifference) / 2.0 +
|
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)
|
public static function at($x, $mean = 0.0, $standardDeviation = 1.0)
|
||||||
@ -160,7 +160,7 @@ class GaussianDistribution
|
|||||||
// stdDev * sqrt(2*pi)
|
// stdDev * sqrt(2*pi)
|
||||||
|
|
||||||
$multiplier = 1.0 / ($standardDeviation * sqrt(2 * M_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;
|
$result = $multiplier * $expPart;
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@ -241,7 +241,7 @@ class GaussianDistribution
|
|||||||
|
|
||||||
for ($j = 0; $j < 2; $j++) {
|
for ($j = 0; $j < 2; $j++) {
|
||||||
$err = GaussianDistribution::errorFunctionCumulativeTo($x) - $pp;
|
$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;
|
return ($p < 1.0) ? $x : -$x;
|
||||||
|
@ -26,7 +26,7 @@ abstract class SkillCalculator
|
|||||||
*
|
*
|
||||||
* @return All the players and their new ratings.
|
* @return All the players and their new ratings.
|
||||||
*/
|
*/
|
||||||
public abstract function calculateNewRatings(GameInfo &$gameInfo,
|
public abstract function calculateNewRatings(GameInfo $gameInfo,
|
||||||
array $teamsOfPlayerToRatings,
|
array $teamsOfPlayerToRatings,
|
||||||
array $teamRanks);
|
array $teamRanks);
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ abstract class SkillCalculator
|
|||||||
* @param array $teamsOfPlayerToRatings A mapping of team players and their ratings.
|
* @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).
|
* @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);
|
array &$teamsOfPlayerToRatings);
|
||||||
|
|
||||||
public function isSupported($option)
|
public function isSupported($option)
|
||||||
@ -58,7 +58,7 @@ abstract class SkillCalculator
|
|||||||
$countOfTeams = 0;
|
$countOfTeams = 0;
|
||||||
|
|
||||||
foreach ($teams as $currentTeam) {
|
foreach ($teams as $currentTeam) {
|
||||||
if (!$playersPerTeam->isInRange($currentTeam->count())) {
|
if (!$playersPerTeam->isInRange(count($currentTeam))) {
|
||||||
throw new Exception("Player count is not in range");
|
throw new Exception("Player count is not in range");
|
||||||
}
|
}
|
||||||
$countOfTeams++;
|
$countOfTeams++;
|
||||||
|
@ -4,7 +4,7 @@ use Moserware\Skills\GameInfo;
|
|||||||
use Moserware\Skills\Guard;
|
use Moserware\Skills\Guard;
|
||||||
use Moserware\Skills\ISupportPartialPlay;
|
use Moserware\Skills\ISupportPartialPlay;
|
||||||
use Moserware\Skills\ISupportPartialUpdate;
|
use Moserware\Skills\ISupportPartialUpdate;
|
||||||
use Moserware\Skills\Numerics\BasicMatch;
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\Numerics\DiagonalMatrix;
|
use Moserware\Skills\Numerics\DiagonalMatrix;
|
||||||
use Moserware\Skills\Numerics\Matrix;
|
use Moserware\Skills\Numerics\Matrix;
|
||||||
use Moserware\Skills\Numerics\Vector;
|
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));
|
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 $teams,
|
||||||
array $teamRanks)
|
array $teamRanks)
|
||||||
{
|
{
|
||||||
@ -43,7 +43,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
|
|||||||
return $factorGraph->getUpdatedRatings();
|
return $factorGraph->getUpdatedRatings();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculateMatchQuality(GameInfo &$gameInfo,
|
public function calculateMatchQuality(GameInfo $gameInfo,
|
||||||
array &$teams)
|
array &$teams)
|
||||||
{
|
{
|
||||||
// We need to create the A matrix which is the player team assigments.
|
// 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());
|
$playerTeamAssignmentsMatrix = $this->createPlayerTeamAssignmentMatrix($teamAssignmentsList, $meanVector->getRowCount());
|
||||||
$playerTeamAssignmentsMatrixTranspose = $playerTeamAssignmentsMatrix->getTranspose();
|
$playerTeamAssignmentsMatrixTranspose = $playerTeamAssignmentsMatrix->getTranspose();
|
||||||
|
|
||||||
$betaSquared = BasicMatch::square($gameInfo->getBeta());
|
$betaSquared = BasicMath::square($gameInfo->getBeta());
|
||||||
|
|
||||||
$start = Matrix::multiply($meanVectorTranspose, $playerTeamAssignmentsMatrix);
|
$start = Matrix::multiply($meanVectorTranspose, $playerTeamAssignmentsMatrix);
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
|
|||||||
return new DiagonalMatrix(
|
return new DiagonalMatrix(
|
||||||
self::getPlayerRatingValues($teamAssignmentsList,
|
self::getPlayerRatingValues($teamAssignmentsList,
|
||||||
function ($rating) {
|
function ($rating) {
|
||||||
return BasicMatch::square($rating->getStandardDeviation());
|
return BasicMath::square($rating->getStandardDeviation());
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use Moserware\Skills\Guard;
|
use Moserware\Skills\Guard;
|
||||||
use Moserware\Skills\FactorGraphs\Message;
|
use Moserware\Skills\FactorGraphs\Message;
|
||||||
use Moserware\Skills\FactorGraphs\Variable;
|
use Moserware\Skills\FactorGraphs\Variable;
|
||||||
use Moserware\Skills\Numerics\BasicMatch;
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\Numerics\GaussianDistribution;
|
use Moserware\Skills\Numerics\GaussianDistribution;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,7 +34,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
|
|||||||
for ($i = 0; $i < $variableWeightsLength; $i++) {
|
for ($i = 0; $i < $variableWeightsLength; $i++) {
|
||||||
$weight = &$variableWeights[$i];
|
$weight = &$variableWeights[$i];
|
||||||
$this->_weights[0][$i] = $weight;
|
$this->_weights[0][$i] = $weight;
|
||||||
$this->_weightsSquared[0][$i] = BasicMatch::square($weight);
|
$this->_weightsSquared[0][$i] = BasicMath::square($weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
$variablesToSumLength = count($variablesToSum);
|
$variablesToSumLength = count($variablesToSum);
|
||||||
@ -94,7 +94,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
|
|||||||
$finalWeight = 0;
|
$finalWeight = 0;
|
||||||
}
|
}
|
||||||
$currentWeights[$currentDestinationWeightIndex] = $finalWeight;
|
$currentWeights[$currentDestinationWeightIndex] = $finalWeight;
|
||||||
$currentWeightsSquared[$currentDestinationWeightIndex] = BasicMatch::square($finalWeight);
|
$currentWeightsSquared[$currentDestinationWeightIndex] = BasicMath::square($finalWeight);
|
||||||
$variableIndices[count($variableWeights)] = 0;
|
$variableIndices[count($variableWeights)] = 0;
|
||||||
$this->_variableIndexOrdersForWeights[] = $variableIndices;
|
$this->_variableIndexOrdersForWeights[] = $variableIndices;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
|
<?php namespace Moserware\Skills\TrueSkill\Layers;
|
||||||
|
|
||||||
use Moserware\Skills\Numerics\BasicMatch;
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\Rating;
|
use Moserware\Skills\Rating;
|
||||||
use Moserware\Skills\FactorGraphs\ScheduleStep;
|
use Moserware\Skills\FactorGraphs\ScheduleStep;
|
||||||
use Moserware\Skills\FactorGraphs\Variable;
|
use Moserware\Skills\FactorGraphs\Variable;
|
||||||
@ -57,8 +57,8 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
|
|||||||
{
|
{
|
||||||
return new GaussianPriorFactor(
|
return new GaussianPriorFactor(
|
||||||
$priorRating->getMean(),
|
$priorRating->getMean(),
|
||||||
BasicMatch::square($priorRating->getStandardDeviation()) +
|
BasicMath::square($priorRating->getStandardDeviation()) +
|
||||||
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
|
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
|
||||||
$skillsVariable
|
$skillsVariable
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use Moserware\Skills\FactorGraphs\ScheduleStep;
|
use Moserware\Skills\FactorGraphs\ScheduleStep;
|
||||||
use Moserware\Skills\FactorGraphs\KeyedVariable;
|
use Moserware\Skills\FactorGraphs\KeyedVariable;
|
||||||
use Moserware\Skills\Numerics\BasicMatch;
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
|
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
|
||||||
use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
|
use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
|
|||||||
private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance)
|
private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance)
|
||||||
{
|
{
|
||||||
return new GaussianLikelihoodFactor(
|
return new GaussianLikelihoodFactor(
|
||||||
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
|
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
|
||||||
$playerPerformance,
|
$playerPerformance,
|
||||||
$playerSkill
|
$playerSkill
|
||||||
);
|
);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use Moserware\Skills\GameInfo;
|
use Moserware\Skills\GameInfo;
|
||||||
use Moserware\Skills\Guard;
|
use Moserware\Skills\Guard;
|
||||||
use Moserware\Skills\Numerics\BasicMatch;
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\PairwiseComparison;
|
use Moserware\Skills\PairwiseComparison;
|
||||||
use Moserware\Skills\RankSorter;
|
use Moserware\Skills\RankSorter;
|
||||||
use Moserware\Skills\Rating;
|
use Moserware\Skills\Rating;
|
||||||
@ -26,7 +26,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::exactly(1));
|
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::exactly(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculateNewRatings(GameInfo &$gameInfo,
|
public function calculateNewRatings(GameInfo $gameInfo,
|
||||||
array $teams,
|
array $teams,
|
||||||
array $teamRanks)
|
array $teamRanks)
|
||||||
{
|
{
|
||||||
@ -74,11 +74,11 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
);
|
);
|
||||||
|
|
||||||
$c = sqrt(
|
$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();
|
$winningMean = $selfRating->getMean();
|
||||||
@ -112,10 +112,10 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
$rankMultiplier = 1;
|
$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());
|
$varianceWithDynamics = BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor());
|
||||||
$stdDevMultiplier = $varianceWithDynamics/BasicMatch::square($c);
|
$stdDevMultiplier = $varianceWithDynamics/BasicMath::square($c);
|
||||||
|
|
||||||
$newMean = $selfRating->getMean() + ($rankMultiplier*$meanMultiplier*$v);
|
$newMean = $selfRating->getMean() + ($rankMultiplier*$meanMultiplier*$v);
|
||||||
$newStdDev = sqrt($varianceWithDynamics*(1 - $w*$stdDevMultiplier));
|
$newStdDev = sqrt($varianceWithDynamics*(1 - $w*$stdDevMultiplier));
|
||||||
@ -126,7 +126,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc }
|
* {@inheritdoc }
|
||||||
*/
|
*/
|
||||||
public function calculateMatchQuality(GameInfo &$gameInfo, array &$teams)
|
public function calculateMatchQuality(GameInfo $gameInfo, array &$teams)
|
||||||
{
|
{
|
||||||
Guard::argumentNotNull($gameInfo, "gameInfo");
|
Guard::argumentNotNull($gameInfo, "gameInfo");
|
||||||
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
||||||
@ -141,9 +141,9 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
$player2Rating = $team2Ratings[0];
|
$player2Rating = $team2Ratings[0];
|
||||||
|
|
||||||
// We just use equation 4.1 found on page 8 of the TrueSkill 2006 paper:
|
// We just use equation 4.1 found on page 8 of the TrueSkill 2006 paper:
|
||||||
$betaSquared = BasicMatch::square($gameInfo->getBeta());
|
$betaSquared = BasicMath::square($gameInfo->getBeta());
|
||||||
$player1SigmaSquared = BasicMatch::square($player1Rating->getStandardDeviation());
|
$player1SigmaSquared = BasicMath::square($player1Rating->getStandardDeviation());
|
||||||
$player2SigmaSquared = BasicMatch::square($player2Rating->getStandardDeviation());
|
$player2SigmaSquared = BasicMath::square($player2Rating->getStandardDeviation());
|
||||||
|
|
||||||
// This is the square root part of the equation:
|
// This is the square root part of the equation:
|
||||||
$sqrtPart =
|
$sqrtPart =
|
||||||
@ -155,7 +155,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
// This is the exponent part of the equation:
|
// This is the exponent part of the equation:
|
||||||
$expPart =
|
$expPart =
|
||||||
exp(
|
exp(
|
||||||
(-1*BasicMatch::square($player1Rating->getMean() - $player2Rating->getMean()))
|
(-1*BasicMath::square($player1Rating->getMean() - $player2Rating->getMean()))
|
||||||
/
|
/
|
||||||
(2*(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared)));
|
(2*(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared)));
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use Moserware\Skills\GameInfo;
|
use Moserware\Skills\GameInfo;
|
||||||
use Moserware\Skills\Guard;
|
use Moserware\Skills\Guard;
|
||||||
use Moserware\Skills\Numerics\BasicMatch;
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\PairwiseComparison;
|
use Moserware\Skills\PairwiseComparison;
|
||||||
use Moserware\Skills\RankSorter;
|
use Moserware\Skills\RankSorter;
|
||||||
use Moserware\Skills\Rating;
|
use Moserware\Skills\Rating;
|
||||||
@ -27,9 +27,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::atLeast(1));
|
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::atLeast(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function calculateNewRatings(GameInfo &$gameInfo,
|
public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks)
|
||||||
array $teams,
|
|
||||||
array $teamRanks)
|
|
||||||
{
|
{
|
||||||
Guard::argumentNotNull($gameInfo, "gameInfo");
|
Guard::argumentNotNull($gameInfo, "gameInfo");
|
||||||
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
||||||
@ -69,8 +67,8 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
$gameInfo->getBeta()
|
$gameInfo->getBeta()
|
||||||
);
|
);
|
||||||
|
|
||||||
$betaSquared = BasicMatch::square($gameInfo->getBeta());
|
$betaSquared = BasicMath::square($gameInfo->getBeta());
|
||||||
$tauSquared = BasicMatch::square($gameInfo->getDynamicsFactor());
|
$tauSquared = BasicMath::square($gameInfo->getDynamicsFactor());
|
||||||
|
|
||||||
$totalPlayers = $selfTeam->count() + $otherTeam->count();
|
$totalPlayers = $selfTeam->count() + $otherTeam->count();
|
||||||
|
|
||||||
@ -78,17 +76,17 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
return $currentRating->getMean();
|
return $currentRating->getMean();
|
||||||
};
|
};
|
||||||
|
|
||||||
$selfMeanSum = BasicMatch::sum($selfTeam->getAllRatings(), $meanGetter);
|
$selfMeanSum = BasicMath::sum($selfTeam->getAllRatings(), $meanGetter);
|
||||||
$otherTeamMeanSum = BasicMatch::sum($otherTeam->getAllRatings(), $meanGetter);
|
$otherTeamMeanSum = BasicMath::sum($otherTeam->getAllRatings(), $meanGetter);
|
||||||
|
|
||||||
$varianceGetter = function ($currentRating) {
|
$varianceGetter = function ($currentRating) {
|
||||||
return BasicMatch::square($currentRating->getStandardDeviation());
|
return BasicMath::square($currentRating->getStandardDeviation());
|
||||||
};
|
};
|
||||||
|
|
||||||
$c = sqrt(
|
$c = sqrt(
|
||||||
BasicMatch::sum($selfTeam->getAllRatings(), $varianceGetter)
|
BasicMath::sum($selfTeam->getAllRatings(), $varianceGetter)
|
||||||
+
|
+
|
||||||
BasicMatch::sum($otherTeam->getAllRatings(), $varianceGetter)
|
BasicMath::sum($otherTeam->getAllRatings(), $varianceGetter)
|
||||||
+
|
+
|
||||||
$totalPlayers * $betaSquared
|
$totalPlayers * $betaSquared
|
||||||
);
|
);
|
||||||
@ -126,14 +124,15 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
$localSelfTeamCurrentPlayer = &$selfTeamCurrentPlayer;
|
$localSelfTeamCurrentPlayer = &$selfTeamCurrentPlayer;
|
||||||
$previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer);
|
$previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer);
|
||||||
|
|
||||||
$meanMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
|
$meanMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
|
||||||
$stdDevMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMatch::square($c);
|
$stdDevMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMath::square($c);
|
||||||
|
|
||||||
$playerMeanDelta = ($rankMultiplier * $meanMultiplier * $v);
|
$playerMeanDelta = ($rankMultiplier * $meanMultiplier * $v);
|
||||||
$newMean = $previousPlayerRating->getMean() + $playerMeanDelta;
|
$newMean = $previousPlayerRating->getMean() + $playerMeanDelta;
|
||||||
|
|
||||||
$newStdDev =
|
$newStdDev = sqrt(
|
||||||
sqrt((BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier));
|
(BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier)
|
||||||
|
);
|
||||||
|
|
||||||
$newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev));
|
$newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev));
|
||||||
}
|
}
|
||||||
@ -142,8 +141,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function calculateMatchQuality(GameInfo &$gameInfo,
|
public function calculateMatchQuality(GameInfo $gameInfo, array &$teams)
|
||||||
array &$teams)
|
|
||||||
{
|
{
|
||||||
Guard::argumentNotNull($gameInfo, "gameInfo");
|
Guard::argumentNotNull($gameInfo, "gameInfo");
|
||||||
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
||||||
@ -157,21 +155,21 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
|
|
||||||
$totalPlayers = $team1Count + $team2Count;
|
$totalPlayers = $team1Count + $team2Count;
|
||||||
|
|
||||||
$betaSquared = BasicMatch::square($gameInfo->getBeta());
|
$betaSquared = BasicMath::square($gameInfo->getBeta());
|
||||||
|
|
||||||
$meanGetter = function ($currentRating) {
|
$meanGetter = function ($currentRating) {
|
||||||
return $currentRating->getMean();
|
return $currentRating->getMean();
|
||||||
};
|
};
|
||||||
|
|
||||||
$varianceGetter = function ($currentRating) {
|
$varianceGetter = function ($currentRating) {
|
||||||
return BasicMatch::square($currentRating->getStandardDeviation());
|
return BasicMath::square($currentRating->getStandardDeviation());
|
||||||
};
|
};
|
||||||
|
|
||||||
$team1MeanSum = BasicMatch::sum($team1Ratings, $meanGetter);
|
$team1MeanSum = BasicMath::sum($team1Ratings, $meanGetter);
|
||||||
$team1StdDevSquared = BasicMatch::sum($team1Ratings, $varianceGetter);
|
$team1StdDevSquared = BasicMath::sum($team1Ratings, $varianceGetter);
|
||||||
|
|
||||||
$team2MeanSum = BasicMatch::sum($team2Ratings, $meanGetter);
|
$team2MeanSum = BasicMath::sum($team2Ratings, $meanGetter);
|
||||||
$team2SigmaSquared = BasicMatch::sum($team2Ratings, $varianceGetter);
|
$team2SigmaSquared = BasicMath::sum($team2Ratings, $varianceGetter);
|
||||||
|
|
||||||
// This comes from equation 4.1 in the TrueSkill paper on page 8
|
// 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
|
// The equation was broken up into the part under the square root sign and
|
||||||
@ -184,7 +182,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
|||||||
);
|
);
|
||||||
|
|
||||||
$expPart = exp(
|
$expPart = exp(
|
||||||
(-1 * BasicMatch::square($team1MeanSum - $team2MeanSum))
|
(-1 * BasicMath::square($team1MeanSum - $team2MeanSum))
|
||||||
/
|
/
|
||||||
(2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared))
|
(2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared))
|
||||||
);
|
);
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
<?php namespace Moserware\Skills\Tests\Elo;
|
<?php namespace Moserware\Skills\Tests\Elo;
|
||||||
|
|
||||||
|
use Moserware\Skills\Elo\EloRating;
|
||||||
|
use Moserware\Skills\Elo\FideEloCalculator;
|
||||||
use Moserware\Skills\GameInfo;
|
use Moserware\Skills\GameInfo;
|
||||||
use Moserware\Skills\PairwiseComparison;
|
use Moserware\Skills\PairwiseComparison;
|
||||||
|
use Moserware\Skills\Tests\TestCase;
|
||||||
|
|
||||||
class EloAssert
|
class EloAssert
|
||||||
{
|
{
|
||||||
const ERROR_TOLERANCE = 0.1;
|
const ERROR_TOLERANCE = 0.1;
|
||||||
|
|
||||||
public static function assertChessRating(
|
public static function assertChessRating(
|
||||||
$testClass,
|
TestCase $testClass,
|
||||||
$twoPlayerEloCalculator,
|
FideEloCalculator $twoPlayerEloCalculator,
|
||||||
$player1BeforeRating,
|
$player1BeforeRating,
|
||||||
$player2BeforeRating,
|
$player2BeforeRating,
|
||||||
$player1Result,
|
$player1Result,
|
||||||
@ -31,7 +34,8 @@ class EloAssert
|
|||||||
$result = $twoPlayerEloCalculator->calculateNewRatings(
|
$result = $twoPlayerEloCalculator->calculateNewRatings(
|
||||||
$chessGameInfo,
|
$chessGameInfo,
|
||||||
$teams,
|
$teams,
|
||||||
$ranks);
|
$ranks
|
||||||
|
);
|
||||||
|
|
||||||
$testClass->assertEquals($player1AfterRating, $result[$player1]->getMean(), '', self::ERROR_TOLERANCE);
|
$testClass->assertEquals($player1AfterRating, $result[$player1]->getMean(), '', self::ERROR_TOLERANCE);
|
||||||
$testClass->assertEquals($player2AfterRating, $result[$player2]->getMean(), '', self::ERROR_TOLERANCE);
|
$testClass->assertEquals($player2AfterRating, $result[$player2]->getMean(), '', self::ERROR_TOLERANCE);
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
<?php namespace Moserware\Skills\Tests\Numerics;
|
<?php namespace Moserware\Skills\Tests\Numerics;
|
||||||
|
|
||||||
|
use Moserware\Skills\Numerics\BasicMath;
|
||||||
use Moserware\Skills\Tests\TestCase;
|
use Moserware\Skills\Tests\TestCase;
|
||||||
|
|
||||||
class BasicMathTest extends TestCase
|
class BasicMathTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testSquare()
|
public function testSquare()
|
||||||
{
|
{
|
||||||
$this->assertEquals(1, Moserware\Numerics\square(1));
|
$this->assertEquals(1, BasicMath::square(1));
|
||||||
$this->assertEquals(1.44, Moserware\Numerics\square(1.2));
|
$this->assertEquals(1.44, BasicMath::square(1.2));
|
||||||
$this->assertEquals(4, Moserware\Numerics\square(2));
|
$this->assertEquals(4, BasicMath::square(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
<?php namespace Moserware\Skills\Tests\Numerics;
|
<?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;
|
use Moserware\Skills\Tests\TestCase;
|
||||||
|
|
||||||
class GaussianDistributionTest extends TestCase
|
class GaussianDistributionTest extends TestCase
|
||||||
@ -36,10 +37,10 @@ class GaussianDistributionTest extends TestCase
|
|||||||
|
|
||||||
$product2 = GaussianDistribution::multiply($m4s5, $m6s7);
|
$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);
|
$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);
|
$this->assertEquals($expectedSigma, $product2->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +54,7 @@ class GaussianDistributionTest extends TestCase
|
|||||||
$this->assertEquals(2.0, $productDividedByStandardNormal->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
|
$this->assertEquals(2.0, $productDividedByStandardNormal->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||||
$this->assertEquals(3.0, $productDividedByStandardNormal->getStandardDeviation(), '', 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)))));
|
$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);
|
$m4s5 = new GaussianDistribution(4, 5);
|
||||||
$product2DividedByM4S5 = GaussianDistribution::divide($product2, $m4s5);
|
$product2DividedByM4S5 = GaussianDistribution::divide($product2, $m4s5);
|
||||||
$this->assertEquals(6.0, $product2DividedByM4S5->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
|
$this->assertEquals(6.0, $product2DividedByM4S5->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php namespace Moserware\Skills\Tests\Numerics;
|
<?php namespace Moserware\Skills\Tests\Numerics;
|
||||||
|
|
||||||
use Moserware\Numerics\Matrix;
|
use Moserware\Skills\Numerics\IdentityMatrix;
|
||||||
use Moserware\Numerics\IdentityMatrix;
|
use Moserware\Skills\Numerics\Matrix;
|
||||||
use Moserware\Numerics\SquareMatrix;
|
use Moserware\Skills\Numerics\SquareMatrix;
|
||||||
use Moserware\Skills\Tests\TestCase;
|
use Moserware\Skills\Tests\TestCase;
|
||||||
|
|
||||||
class MatrixTest extends TestCase
|
class MatrixTest extends TestCase
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php namespace Moserware\Skills\Tests;
|
<?php namespace Moserware\Skills\Tests;
|
||||||
|
|
||||||
class TestCase extends \PHPUnit_Framework_TestCase {
|
class TestCase extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
}
|
}
|
Reference in New Issue
Block a user