mirror of
				https://github.com/furyfire/trueskill.git
				synced 2025-11-04 10:12:28 +01:00 
			
		
		
		
	Cleanup in src/, adding namespaces, removing php closing tag and general code cleanup
This commit is contained in:
		@@ -1,7 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . '/../Rating.php');
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\Rating;
 | 
			
		||||
 | 
			
		||||
@@ -10,10 +7,8 @@ use Moserware\Skills\Rating;
 | 
			
		||||
 */
 | 
			
		||||
class EloRating extends Rating
 | 
			
		||||
{
 | 
			
		||||
    public function __construct($rating)        
 | 
			
		||||
    public function __construct($rating)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($rating, 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,17 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/FideKFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TwoPlayerEloCalculator.php");
 | 
			
		||||
 | 
			
		||||
/** Including Elo's scheme as a simple comparison.
 | 
			
		||||
 *  See http://en.wikipedia.org/wiki/Elo_rating_system#Theory
 | 
			
		||||
 *  for more details
 | 
			
		||||
/**
 | 
			
		||||
 * Including Elo's scheme as a simple comparison.
 | 
			
		||||
 * See http://en.wikipedia.org/wiki/Elo_rating_system#Theory
 | 
			
		||||
 * for more details
 | 
			
		||||
 */
 | 
			
		||||
class FideEloCalculator extends TwoPlayerEloCalculator
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(FideKFactor $kFactor)        
 | 
			
		||||
    public function __construct(FideKFactor $kFactor)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($kFactor);
 | 
			
		||||
    }
 | 
			
		||||
@@ -31,11 +27,9 @@ class FideEloCalculator extends TwoPlayerEloCalculator
 | 
			
		||||
        $ratingDifference = $opponentRating - $playerRating;
 | 
			
		||||
 | 
			
		||||
        return 1.0
 | 
			
		||||
               /
 | 
			
		||||
               (
 | 
			
		||||
                   1.0 + pow(10.0, $ratingDifference / (2 * $gameInfo->getBeta()))
 | 
			
		||||
               );
 | 
			
		||||
    }        
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
        /
 | 
			
		||||
        (
 | 
			
		||||
            1.0 + pow(10.0, $ratingDifference / (2 * $gameInfo->getBeta()))
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,30 +1,14 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/KFactor.php");
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
// see http://ratings.fide.com/calculator_rtd.phtml for details
 | 
			
		||||
class FideKFactor extends KFactor
 | 
			
		||||
{
 | 
			
		||||
    public function getValueForRating($rating)
 | 
			
		||||
    {
 | 
			
		||||
        if ($rating < 2400)
 | 
			
		||||
        {
 | 
			
		||||
        if ($rating < 2400) {
 | 
			
		||||
            return 15;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return 10;
 | 
			
		||||
    }    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Indicates someone who has played less than 30 games.
 | 
			
		||||
 */
 | 
			
		||||
class ProvisionalFideKFactor extends FideKFactor
 | 
			
		||||
{
 | 
			
		||||
    public function getValueForRating($rating)
 | 
			
		||||
    {
 | 
			
		||||
        return 25;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,10 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../GameInfo.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Numerics/GaussianDistribution.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/KFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TwoPlayerEloCalculator.php");
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\GameInfo;
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
@@ -29,6 +23,4 @@ class GaussianEloCalculator extends TwoPlayerEloCalculator
 | 
			
		||||
            /
 | 
			
		||||
            (sqrt(2) * $gameInfo->getBeta()));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,9 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\Elo;
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
class KFactor
 | 
			
		||||
{
 | 
			
		||||
    const DEFAULT_KFACTOR = 24;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    private $_value;
 | 
			
		||||
 | 
			
		||||
    public function __construct($exactKFactor = self::DEFAULT_KFACTOR)
 | 
			
		||||
@@ -17,6 +15,4 @@ class KFactor
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_value;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								src/Elo/ProvisionalFideKFactor.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/Elo/ProvisionalFideKFactor.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Indicates someone who has played less than 30 games.
 | 
			
		||||
 */
 | 
			
		||||
class ProvisionalFideKFactor extends FideKFactor
 | 
			
		||||
{
 | 
			
		||||
    public function getValueForRating($rating)
 | 
			
		||||
    {
 | 
			
		||||
        return 25;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,13 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PairwiseComparison.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../RankSorter.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../SkillCalculator.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PlayersRange.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TeamsRange.php");
 | 
			
		||||
<?php namespace Moserware\Skills\Elo;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\PairwiseComparison;
 | 
			
		||||
use Moserware\Skills\RankSorter;
 | 
			
		||||
@@ -100,6 +91,4 @@ abstract class TwoPlayerEloCalculator extends SkillCalculator
 | 
			
		||||
        $deltaFrom50Percent = abs(getPlayerWinProbability($gameInfo, $player1Rating, $player2Rating) - 0.5);
 | 
			
		||||
        return (0.5 - $deltaFrom50Percent) / 0.5;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								src/FactorGraphs/DefaultVariable.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/FactorGraphs/DefaultVariable.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
 | 
			
		||||
class DefaultVariable extends Variable
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct("Default", null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function &getValue()
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setValue(&$value)
 | 
			
		||||
    {
 | 
			
		||||
        throw new Exception();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Guard.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../HashMap.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/Variable.php");
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
use Moserware\Skills\Guard;
 | 
			
		||||
use Moserware\Skills\HashMap;
 | 
			
		||||
 | 
			
		||||
@@ -38,12 +33,12 @@ abstract class Factor
 | 
			
		||||
    {
 | 
			
		||||
        return count($this->_messages);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    protected function &getVariables()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_variables;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    protected function &getMessages()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_messages;
 | 
			
		||||
@@ -56,7 +51,7 @@ abstract class Factor
 | 
			
		||||
    {
 | 
			
		||||
        Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex");
 | 
			
		||||
        $message = &$this->_messages[$messageIndex];
 | 
			
		||||
        $variable = &$this->_messageToVariableBinding->getValue($message);        
 | 
			
		||||
        $variable = &$this->_messageToVariableBinding->getValue($message);
 | 
			
		||||
        return $this->updateMessageVariable($message, $variable);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -71,8 +66,7 @@ abstract class Factor
 | 
			
		||||
    public function resetMarginals()
 | 
			
		||||
    {
 | 
			
		||||
        $allValues = &$this->_messageToVariableBinding->getAllValues();
 | 
			
		||||
        foreach ($allValues as &$currentVariable)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($allValues as &$currentVariable) {
 | 
			
		||||
            $currentVariable->resetToPrior();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -108,6 +102,4 @@ abstract class Factor
 | 
			
		||||
    {
 | 
			
		||||
        return ($this->_name != null) ? $this->_name : base::__toString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/VariableFactory.php");
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class FactorGraph
 | 
			
		||||
{
 | 
			
		||||
@@ -17,5 +14,4 @@ class FactorGraph
 | 
			
		||||
    {
 | 
			
		||||
        $this->_variableFactory = &$factory;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Factor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/FactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/Schedule.php");
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
abstract class FactorGraphLayer
 | 
			
		||||
{
 | 
			
		||||
@@ -69,6 +64,4 @@ abstract class FactorGraphLayer
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Factor.php");
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Helper class for computing the factor graph's normalization constant.
 | 
			
		||||
@@ -14,8 +10,7 @@ class FactorList
 | 
			
		||||
    public function getLogNormalization()
 | 
			
		||||
    {
 | 
			
		||||
        $list = &$this->_list;
 | 
			
		||||
        foreach($list as &$currentFactor)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($list as &$currentFactor) {
 | 
			
		||||
            $currentFactor->resetMarginals();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -23,22 +18,19 @@ class FactorList
 | 
			
		||||
 | 
			
		||||
        $listCount = count($this->_list);
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $listCount; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $listCount; $i++) {
 | 
			
		||||
            $f = $this->_list[$i];
 | 
			
		||||
 | 
			
		||||
            $numberOfMessages = $f->getNumberOfMessages();
 | 
			
		||||
 | 
			
		||||
            for ($j = 0; $j < $numberOfMessages; $j++)
 | 
			
		||||
            {
 | 
			
		||||
            for ($j = 0; $j < $numberOfMessages; $j++) {
 | 
			
		||||
                $sumLogZ += $f->sendMessageIndex($j);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $sumLogS = 0;
 | 
			
		||||
 | 
			
		||||
        foreach($list as &$currentFactor)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($list as &$currentFactor) {
 | 
			
		||||
            $sumLogS = $sumLogS + $currentFactor->getLogNormalization();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -55,6 +47,4 @@ class FactorList
 | 
			
		||||
        $this->_list[] = $factor;
 | 
			
		||||
        return $factor;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								src/FactorGraphs/KeyedVariable.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/FactorGraphs/KeyedVariable.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class KeyedVariable extends Variable
 | 
			
		||||
{
 | 
			
		||||
    private $_key;
 | 
			
		||||
 | 
			
		||||
    public function __construct(&$key, $name, &$prior)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name, $prior);
 | 
			
		||||
        $this->_key = &$key;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function &getKey()
 | 
			
		||||
    {
 | 
			
		||||
        $key = &$this->_key;
 | 
			
		||||
        return $key;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class Message
 | 
			
		||||
{
 | 
			
		||||
    private $_name;    
 | 
			
		||||
    private $_name;
 | 
			
		||||
    private $_value;
 | 
			
		||||
 | 
			
		||||
    public function __construct(&$value = null, $name = null)
 | 
			
		||||
    {
 | 
			
		||||
        $this->_name = $name;        
 | 
			
		||||
        $this->_name = $name;
 | 
			
		||||
        $this->_value = $value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -19,14 +18,12 @@ class Message
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setValue(&$value)
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        $this->_value = &$value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function __toString()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_name; 
 | 
			
		||||
        return (string)$this->_name;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Factor.php");
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
abstract class Schedule
 | 
			
		||||
{
 | 
			
		||||
@@ -18,77 +15,4 @@ abstract class Schedule
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_name;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ScheduleStep extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    private $_factor;
 | 
			
		||||
    private $_index;
 | 
			
		||||
 | 
			
		||||
    public function __construct($name, Factor &$factor, $index)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
        $this->_factor = $factor;
 | 
			
		||||
        $this->_index = $index;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    {
 | 
			
		||||
        $currentFactor = &$this->_factor;        
 | 
			
		||||
        $delta = $currentFactor->updateMessageIndex($this->_index);
 | 
			
		||||
        return $delta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ScheduleSequence extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    private $_schedules;
 | 
			
		||||
 | 
			
		||||
    public function __construct($name, array $schedules)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
        $this->_schedules = $schedules;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    {
 | 
			
		||||
        $maxDelta = 0;
 | 
			
		||||
 | 
			
		||||
        $schedules = &$this->_schedules;
 | 
			
		||||
        foreach ($schedules as &$currentSchedule)
 | 
			
		||||
        {
 | 
			
		||||
            $currentVisit = $currentSchedule->visit($depth + 1, $maxDepth);
 | 
			
		||||
            $maxDelta = max($currentVisit, $maxDelta);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $maxDelta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ScheduleLoop extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    private $_maxDelta;
 | 
			
		||||
    private $_scheduleToLoop;
 | 
			
		||||
 | 
			
		||||
    public function __construct($name, Schedule &$scheduleToLoop, $maxDelta)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
        $this->_scheduleToLoop = $scheduleToLoop;
 | 
			
		||||
        $this->_maxDelta = $maxDelta;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    {
 | 
			
		||||
        $totalIterations = 1;
 | 
			
		||||
        $delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
 | 
			
		||||
        while ($delta > $this->_maxDelta)
 | 
			
		||||
        {
 | 
			
		||||
            $delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
 | 
			
		||||
            $totalIterations++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $delta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								src/FactorGraphs/ScheduleLoop.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/FactorGraphs/ScheduleLoop.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class ScheduleLoop extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    private $_maxDelta;
 | 
			
		||||
    private $_scheduleToLoop;
 | 
			
		||||
 | 
			
		||||
    public function __construct($name, Schedule &$scheduleToLoop, $maxDelta)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
        $this->_scheduleToLoop = $scheduleToLoop;
 | 
			
		||||
        $this->_maxDelta = $maxDelta;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    {
 | 
			
		||||
        $totalIterations = 1;
 | 
			
		||||
        $delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
 | 
			
		||||
        while ($delta > $this->_maxDelta) {
 | 
			
		||||
            $delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
 | 
			
		||||
            $totalIterations++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $delta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								src/FactorGraphs/ScheduleSequence.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/FactorGraphs/ScheduleSequence.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class ScheduleSequence extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    private $_schedules;
 | 
			
		||||
 | 
			
		||||
    public function __construct($name, array $schedules)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
        $this->_schedules = $schedules;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    {
 | 
			
		||||
        $maxDelta = 0;
 | 
			
		||||
 | 
			
		||||
        $schedules = &$this->_schedules;
 | 
			
		||||
        foreach ($schedules as &$currentSchedule) {
 | 
			
		||||
            $currentVisit = $currentSchedule->visit($depth + 1, $maxDepth);
 | 
			
		||||
            $maxDelta = max($currentVisit, $maxDelta);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $maxDelta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								src/FactorGraphs/ScheduleStep.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/FactorGraphs/ScheduleStep.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class ScheduleStep extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    private $_factor;
 | 
			
		||||
    private $_index;
 | 
			
		||||
 | 
			
		||||
    public function __construct($name, Factor &$factor, $index)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
        $this->_factor = $factor;
 | 
			
		||||
        $this->_index = $index;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    {
 | 
			
		||||
        $currentFactor = &$this->_factor;
 | 
			
		||||
        $delta = $currentFactor->updateMessageIndex($this->_index);
 | 
			
		||||
        return $delta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -16,12 +16,12 @@ class Variable
 | 
			
		||||
 | 
			
		||||
    public function &getValue()
 | 
			
		||||
    {
 | 
			
		||||
        $value = &$this->_value;        
 | 
			
		||||
        $value = &$this->_value;
 | 
			
		||||
        return $value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setValue(&$value)
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        $this->_value = &$value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -34,40 +34,4 @@ class Variable
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_name;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class DefaultVariable extends Variable
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct("Default", null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function &getValue()
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setValue(&$value)
 | 
			
		||||
    {
 | 
			
		||||
        throw new Exception();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class KeyedVariable extends Variable
 | 
			
		||||
{
 | 
			
		||||
    private $_key;
 | 
			
		||||
    public function __construct(&$key, $name, &$prior)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name, $prior);
 | 
			
		||||
        $this->_key = &$key;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function &getKey()
 | 
			
		||||
    {
 | 
			
		||||
        $key = &$this->_key;
 | 
			
		||||
        return $key;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Variable.php");
 | 
			
		||||
<?php namespace Moserware\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class VariableFactory
 | 
			
		||||
{
 | 
			
		||||
@@ -28,5 +24,3 @@ class VariableFactory
 | 
			
		||||
        return $newVar;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Rating.php");
 | 
			
		||||
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Parameters about the game for calculating the TrueSkill.
 | 
			
		||||
@@ -20,12 +15,12 @@ class GameInfo
 | 
			
		||||
    private $_initialStandardDeviation;
 | 
			
		||||
    private $_beta;
 | 
			
		||||
    private $_dynamicsFactor;
 | 
			
		||||
    private $_drawProbability;    
 | 
			
		||||
    
 | 
			
		||||
    public function __construct($initialMean = self::DEFAULT_INITIAL_MEAN, 
 | 
			
		||||
                                $initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION, 
 | 
			
		||||
                                $beta = self::DEFAULT_BETA, 
 | 
			
		||||
                                $dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR, 
 | 
			
		||||
    private $_drawProbability;
 | 
			
		||||
 | 
			
		||||
    public function __construct($initialMean = self::DEFAULT_INITIAL_MEAN,
 | 
			
		||||
                                $initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION,
 | 
			
		||||
                                $beta = self::DEFAULT_BETA,
 | 
			
		||||
                                $dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR,
 | 
			
		||||
                                $drawProbability = self::DEFAULT_DRAW_PROBABILITY)
 | 
			
		||||
    {
 | 
			
		||||
        $this->_initialMean = $initialMean;
 | 
			
		||||
@@ -33,19 +28,19 @@ class GameInfo
 | 
			
		||||
        $this->_beta = $beta;
 | 
			
		||||
        $this->_dynamicsFactor = $dynamicsFactor;
 | 
			
		||||
        $this->_drawProbability = $drawProbability;
 | 
			
		||||
    }   
 | 
			
		||||
    
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function getInitialMean()
 | 
			
		||||
    { 
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_initialMean;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function getInitialStandardDeviation()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_initialStandardDeviation;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function getBeta()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_beta;
 | 
			
		||||
@@ -55,7 +50,7 @@ class GameInfo
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_dynamicsFactor;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function getDrawProbability()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_drawProbability;
 | 
			
		||||
@@ -65,6 +60,4 @@ class GameInfo
 | 
			
		||||
    {
 | 
			
		||||
        return new Rating($this->_initialMean, $this->_initialStandardDeviation);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,35 +1,32 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Verifies argument contracts.
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * @see http://www.moserware.com/2008/01/borrowing-ideas-from-3-interesting.html
 | 
			
		||||
 */
 | 
			
		||||
class Guard
 | 
			
		||||
{
 | 
			
		||||
    public static function argumentNotNull($value, $parameterName)
 | 
			
		||||
    {
 | 
			
		||||
        if ($value == null)
 | 
			
		||||
        {
 | 
			
		||||
        if ($value == null) {
 | 
			
		||||
            throw new Exception($parameterName . " can not be null");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function argumentIsValidIndex($index, $count, $parameterName)
 | 
			
		||||
    {
 | 
			
		||||
        if (($index < 0) || ($index >= $count))
 | 
			
		||||
        {
 | 
			
		||||
        if (($index < 0) || ($index >= $count)) {
 | 
			
		||||
            throw new Exception($parameterName . " is an invalid index");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function argumentInRangeInclusive($value, $min, $max, $parameterName)
 | 
			
		||||
    {
 | 
			
		||||
        if (($value < $min) || ($value > $max))
 | 
			
		||||
        {
 | 
			
		||||
        if (($value < $min) || ($value > $max)) {
 | 
			
		||||
            throw new Exception($parameterName . " is not in the valid range [" . $min . ", " . $max . "]");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Basic hashmap that supports object keys.
 | 
			
		||||
@@ -27,29 +25,27 @@ class HashMap
 | 
			
		||||
 | 
			
		||||
    public function &getAllKeys()
 | 
			
		||||
    {
 | 
			
		||||
        $keys = &\array_values($this->_hashToKey);
 | 
			
		||||
        $keys = &array_values($this->_hashToKey);
 | 
			
		||||
        return $keys;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getAllValues()
 | 
			
		||||
    {
 | 
			
		||||
        $values = &\array_values($this->_hashToValue);
 | 
			
		||||
        $values = &array_values($this->_hashToValue);
 | 
			
		||||
        return $values;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function count()
 | 
			
		||||
    {        
 | 
			
		||||
        return \count($this->_hashToKey);
 | 
			
		||||
    {
 | 
			
		||||
        return count($this->_hashToKey);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static function getHash(&$key)
 | 
			
		||||
    {
 | 
			
		||||
        if(\is_object($key))
 | 
			
		||||
        {
 | 
			
		||||
            return \spl_object_hash($key);
 | 
			
		||||
        if (is_object($key)) {
 | 
			
		||||
            return spl_object_hash($key);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $key;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,16 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Indicates support for allowing partial play (where a player only plays a part of the time).
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
interface ISupportPartialPlay
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Indicates the percent of the time the player should be weighted where 0.0 indicates the player didn't play and 1.0 indicates the player played 100% of the time.
 | 
			
		||||
     */
 | 
			
		||||
    public function getPartialPlayPercentage();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
interface ISupportPartialUpdate
 | 
			
		||||
{
 | 
			
		||||
@@ -7,5 +6,4 @@ interface ISupportPartialUpdate
 | 
			
		||||
     * Indicated how much of a skill update a player should receive where 0.0 represents no update and 1.0 represents 100% of the update.
 | 
			
		||||
     */
 | 
			
		||||
    public function getPartialUpdatePercentage();
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,31 +1,30 @@
 | 
			
		||||
<?php
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Basic math functions.
 | 
			
		||||
 * 
 | 
			
		||||
 * @author     Jeff Moser <jeff@moserware.com>
 | 
			
		||||
 * @copyright  2010 Jeff Moser 
 | 
			
		||||
 */
 | 
			
		||||
class BasicMatch {
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Squares the input (x^2 = x * x)
 | 
			
		||||
 * @param number $x Value to square (x)
 | 
			
		||||
 * @return number The squared value (x^2)
 | 
			
		||||
 */
 | 
			
		||||
function square($x)
 | 
			
		||||
{
 | 
			
		||||
    return $x * $x;
 | 
			
		||||
}
 | 
			
		||||
    /**
 | 
			
		||||
     * Squares the input (x^2 = x * x)
 | 
			
		||||
     * @param number $x Value to square (x)
 | 
			
		||||
     * @return number The squared value (x^2)
 | 
			
		||||
     */
 | 
			
		||||
    public static function square($x) {
 | 
			
		||||
        return $x * $x;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Sums the items in $itemsToSum
 | 
			
		||||
 * @param array $itemsToSum The items to sum,
 | 
			
		||||
 * @param callback $callback The function to apply to each array element before summing.
 | 
			
		||||
 * @return number The sum.
 | 
			
		||||
 */
 | 
			
		||||
function sum(array $itemsToSum, $callback )
 | 
			
		||||
{
 | 
			
		||||
    $mappedItems = array_map($callback, $itemsToSum);
 | 
			
		||||
    return array_sum($mappedItems);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
    /**
 | 
			
		||||
     * Sums the items in $itemsToSum
 | 
			
		||||
     * @param array $itemsToSum The items to sum,
 | 
			
		||||
     * @param callback $callback The function to apply to each array element before summing.
 | 
			
		||||
     * @return number The sum.
 | 
			
		||||
     */
 | 
			
		||||
    public static function sum(array $itemsToSum, $callback) {
 | 
			
		||||
        $mappedItems = array_map($callback, $itemsToSum);
 | 
			
		||||
        return array_sum($mappedItems);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								src/Numerics/DiagonalMatrix.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/Numerics/DiagonalMatrix.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class DiagonalMatrix extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(array $diagonalValues)
 | 
			
		||||
    {
 | 
			
		||||
        $diagonalCount = count($diagonalValues);
 | 
			
		||||
        $rowCount = $diagonalCount;
 | 
			
		||||
        $colCount = $rowCount;
 | 
			
		||||
 | 
			
		||||
        parent::__construct($rowCount, $colCount);
 | 
			
		||||
 | 
			
		||||
        for ($currentRow = 0; $currentRow < $rowCount; $currentRow++) {
 | 
			
		||||
            for ($currentCol = 0; $currentCol < $colCount; $currentCol++) {
 | 
			
		||||
                if ($currentRow == $currentCol) {
 | 
			
		||||
                    $this->setValue($currentRow, $currentCol, $diagonalValues[$currentRow]);
 | 
			
		||||
                } else {
 | 
			
		||||
                    $this->setValue($currentRow, $currentCol, 0);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Numerics;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/basicmath.php");
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Computes Gaussian (bell curve) values.
 | 
			
		||||
@@ -14,49 +10,43 @@ class GaussianDistribution
 | 
			
		||||
{
 | 
			
		||||
    private $_mean;
 | 
			
		||||
    private $_standardDeviation;
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    // precision and precisionMean are used because they make multiplying and dividing simpler
 | 
			
		||||
    // (the the accompanying math paper for more details)
 | 
			
		||||
    private $_precision;
 | 
			
		||||
    private $_precisionMean;
 | 
			
		||||
    private $_variance;
 | 
			
		||||
 | 
			
		||||
    function __construct($mean = 0.0, $standardDeviation = 1.0)
 | 
			
		||||
    {   
 | 
			
		||||
        $this->_mean = $mean;        
 | 
			
		||||
    public function __construct($mean = 0.0, $standardDeviation = 1.0)
 | 
			
		||||
    {
 | 
			
		||||
        $this->_mean = $mean;
 | 
			
		||||
        $this->_standardDeviation = $standardDeviation;
 | 
			
		||||
        $this->_variance = square($standardDeviation);
 | 
			
		||||
        $this->_variance = BasicMatch::square($standardDeviation);
 | 
			
		||||
 | 
			
		||||
        if($this->_variance != 0)
 | 
			
		||||
        {
 | 
			
		||||
            $this->_precision = 1.0/$this->_variance;
 | 
			
		||||
            $this->_precisionMean = $this->_precision*$this->_mean;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
        if ($this->_variance != 0) {
 | 
			
		||||
            $this->_precision = 1.0 / $this->_variance;
 | 
			
		||||
            $this->_precisionMean = $this->_precision * $this->_mean;
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->_precision = \INF;
 | 
			
		||||
 | 
			
		||||
            if($this->_mean == 0)
 | 
			
		||||
            {
 | 
			
		||||
            if ($this->_mean == 0) {
 | 
			
		||||
                $this->_precisionMean = 0;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
            } else {
 | 
			
		||||
                $this->_precisionMean = \INF;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }    
 | 
			
		||||
    
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getMean()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_mean;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function getVariance()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_variance;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function getStandardDeviation()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_standardDeviation;
 | 
			
		||||
@@ -71,13 +61,13 @@ class GaussianDistribution
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_precisionMean;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function getNormalizationConstant()
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        // Great derivation of this is at http://www.astro.psu.edu/~mce/A451_2/A451/downloads/notes0.pdf
 | 
			
		||||
        return 1.0/(sqrt(2*M_PI)*$this->_standardDeviation);    
 | 
			
		||||
        return 1.0 / (sqrt(2 * M_PI) * $this->_standardDeviation);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function __clone()
 | 
			
		||||
    {
 | 
			
		||||
        $result = new GaussianDistribution();
 | 
			
		||||
@@ -88,35 +78,32 @@ class GaussianDistribution
 | 
			
		||||
        $result->_precisionMean = $this->_precisionMean;
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public static function fromPrecisionMean($precisionMean, $precision)
 | 
			
		||||
    {
 | 
			
		||||
        $result = new GaussianDistribution();
 | 
			
		||||
        $result->_precision = $precision;
 | 
			
		||||
        $result->_precisionMean = $precisionMean;
 | 
			
		||||
 | 
			
		||||
        if($precision != 0)
 | 
			
		||||
        {
 | 
			
		||||
            $result->_variance = 1.0/$precision;
 | 
			
		||||
        if ($precision != 0) {
 | 
			
		||||
            $result->_variance = 1.0 / $precision;
 | 
			
		||||
            $result->_standardDeviation = sqrt($result->_variance);
 | 
			
		||||
            $result->_mean = $result->_precisionMean/$result->_precision;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            $result->_mean = $result->_precisionMean / $result->_precision;
 | 
			
		||||
        } else {
 | 
			
		||||
            $result->_variance = \INF;
 | 
			
		||||
            $result->_standardDeviation = \INF;
 | 
			
		||||
            $result->_mean = \NAN;
 | 
			
		||||
        }
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // For details, see http://www.tina-vision.net/tina-knoppix/tina-memo/2003-003.pdf
 | 
			
		||||
    // for multiplication, the precision mean ones are easier to write :)
 | 
			
		||||
    public static function multiply(GaussianDistribution $left, GaussianDistribution $right)
 | 
			
		||||
    {
 | 
			
		||||
        return GaussianDistribution::fromPrecisionMean($left->_precisionMean + $right->_precisionMean, $left->_precision + $right->_precision);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // Computes the absolute difference between two Gaussians
 | 
			
		||||
    public static function absoluteDifference(GaussianDistribution $left, GaussianDistribution $right)
 | 
			
		||||
    {
 | 
			
		||||
@@ -130,43 +117,41 @@ class GaussianDistribution
 | 
			
		||||
    {
 | 
			
		||||
        return GaussianDistribution::absoluteDifference($left, $right);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public static function logProductNormalization(GaussianDistribution $left, GaussianDistribution $right)
 | 
			
		||||
    {
 | 
			
		||||
        if (($left->_precision == 0) || ($right->_precision == 0))
 | 
			
		||||
        {
 | 
			
		||||
        if (($left->_precision == 0) || ($right->_precision == 0)) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $varianceSum = $left->_variance + $right->_variance;
 | 
			
		||||
        $meanDifference = $left->_mean - $right->_mean;
 | 
			
		||||
 | 
			
		||||
        $logSqrt2Pi = log(sqrt(2*M_PI));
 | 
			
		||||
        return -$logSqrt2Pi - (log($varianceSum)/2.0) - (square($meanDifference)/(2.0*$varianceSum));
 | 
			
		||||
        $logSqrt2Pi = log(sqrt(2 * M_PI));
 | 
			
		||||
        return -$logSqrt2Pi - (log($varianceSum) / 2.0) - (BasicMatch::square($meanDifference) / (2.0 * $varianceSum));
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator)
 | 
			
		||||
    {
 | 
			
		||||
        return GaussianDistribution::fromPrecisionMean($numerator->_precisionMean - $denominator->_precisionMean,
 | 
			
		||||
                                 $numerator->_precision - $denominator->_precision);
 | 
			
		||||
            $numerator->_precision - $denominator->_precision);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function logRatioNormalization(GaussianDistribution $numerator, GaussianDistribution $denominator)
 | 
			
		||||
    {
 | 
			
		||||
        if (($numerator->_precision == 0) || ($denominator->_precision == 0))
 | 
			
		||||
        {
 | 
			
		||||
        if (($numerator->_precision == 0) || ($denominator->_precision == 0)) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $varianceDifference = $denominator->_variance - $numerator->_variance;
 | 
			
		||||
        $meanDifference = $numerator->_mean - $denominator->_mean;
 | 
			
		||||
 | 
			
		||||
        $logSqrt2Pi = log(sqrt(2*M_PI));
 | 
			
		||||
        $logSqrt2Pi = log(sqrt(2 * M_PI));
 | 
			
		||||
 | 
			
		||||
        return log($denominator->_variance) + $logSqrt2Pi - log($varianceDifference)/2.0 +
 | 
			
		||||
               square($meanDifference)/(2*$varianceDifference);
 | 
			
		||||
        return log($denominator->_variance) + $logSqrt2Pi - log($varianceDifference) / 2.0 +
 | 
			
		||||
        BasicMatch::square($meanDifference) / (2 * $varianceDifference);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public static function at($x, $mean = 0.0, $standardDeviation = 1.0)
 | 
			
		||||
    {
 | 
			
		||||
        // See http://mathworld.wolfram.com/NormalDistribution.html
 | 
			
		||||
@@ -174,93 +159,89 @@ class GaussianDistribution
 | 
			
		||||
        // P(x) = ------------------- * e
 | 
			
		||||
        //        stdDev * sqrt(2*pi)
 | 
			
		||||
 | 
			
		||||
        $multiplier = 1.0/($standardDeviation*sqrt(2*M_PI));
 | 
			
		||||
        $expPart = exp((-1.0*square($x - $mean))/(2*square($standardDeviation)));
 | 
			
		||||
        $result = $multiplier*$expPart;
 | 
			
		||||
        $multiplier = 1.0 / ($standardDeviation * sqrt(2 * M_PI));
 | 
			
		||||
        $expPart = exp((-1.0 * BasicMatch::square($x - $mean)) / (2 * BasicMatch::square($standardDeviation)));
 | 
			
		||||
        $result = $multiplier * $expPart;
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function cumulativeTo($x, $mean = 0.0, $standardDeviation = 1.0)
 | 
			
		||||
    {
 | 
			
		||||
        $invsqrt2 = -0.707106781186547524400844362104;
 | 
			
		||||
        $result = GaussianDistribution::errorFunctionCumulativeTo($invsqrt2*$x);
 | 
			
		||||
        return 0.5*$result;
 | 
			
		||||
        $result = GaussianDistribution::errorFunctionCumulativeTo($invsqrt2 * $x);
 | 
			
		||||
        return 0.5 * $result;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    private static function errorFunctionCumulativeTo($x)
 | 
			
		||||
    {
 | 
			
		||||
        // Derived from page 265 of Numerical Recipes 3rd Edition            
 | 
			
		||||
        $z = abs($x);
 | 
			
		||||
 | 
			
		||||
        $t = 2.0/(2.0 + $z);
 | 
			
		||||
        $ty = 4*$t - 2;
 | 
			
		||||
        $t = 2.0 / (2.0 + $z);
 | 
			
		||||
        $ty = 4 * $t - 2;
 | 
			
		||||
 | 
			
		||||
        $coefficients = array(
 | 
			
		||||
                                -1.3026537197817094, 
 | 
			
		||||
                                6.4196979235649026e-1,
 | 
			
		||||
                                1.9476473204185836e-2, 
 | 
			
		||||
                                -9.561514786808631e-3, 
 | 
			
		||||
                                -9.46595344482036e-4,
 | 
			
		||||
                                3.66839497852761e-4, 
 | 
			
		||||
                                4.2523324806907e-5, 
 | 
			
		||||
                                -2.0278578112534e-5,
 | 
			
		||||
                                -1.624290004647e-6, 
 | 
			
		||||
                                1.303655835580e-6, 
 | 
			
		||||
                                1.5626441722e-8, 
 | 
			
		||||
                                -8.5238095915e-8,
 | 
			
		||||
                                6.529054439e-9, 
 | 
			
		||||
                                5.059343495e-9, 
 | 
			
		||||
                                -9.91364156e-10, 
 | 
			
		||||
                                -2.27365122e-10,
 | 
			
		||||
                                9.6467911e-11, 
 | 
			
		||||
                                2.394038e-12, 
 | 
			
		||||
                                -6.886027e-12, 
 | 
			
		||||
                                8.94487e-13, 
 | 
			
		||||
                                3.13092e-13,
 | 
			
		||||
                                -1.12708e-13, 
 | 
			
		||||
                                3.81e-16, 
 | 
			
		||||
                                7.106e-15, 
 | 
			
		||||
                                -1.523e-15, 
 | 
			
		||||
                                -9.4e-17, 
 | 
			
		||||
                                1.21e-16, 
 | 
			
		||||
                                -2.8e-17 );
 | 
			
		||||
            -1.3026537197817094,
 | 
			
		||||
            6.4196979235649026e-1,
 | 
			
		||||
            1.9476473204185836e-2,
 | 
			
		||||
            -9.561514786808631e-3,
 | 
			
		||||
            -9.46595344482036e-4,
 | 
			
		||||
            3.66839497852761e-4,
 | 
			
		||||
            4.2523324806907e-5,
 | 
			
		||||
            -2.0278578112534e-5,
 | 
			
		||||
            -1.624290004647e-6,
 | 
			
		||||
            1.303655835580e-6,
 | 
			
		||||
            1.5626441722e-8,
 | 
			
		||||
            -8.5238095915e-8,
 | 
			
		||||
            6.529054439e-9,
 | 
			
		||||
            5.059343495e-9,
 | 
			
		||||
            -9.91364156e-10,
 | 
			
		||||
            -2.27365122e-10,
 | 
			
		||||
            9.6467911e-11,
 | 
			
		||||
            2.394038e-12,
 | 
			
		||||
            -6.886027e-12,
 | 
			
		||||
            8.94487e-13,
 | 
			
		||||
            3.13092e-13,
 | 
			
		||||
            -1.12708e-13,
 | 
			
		||||
            3.81e-16,
 | 
			
		||||
            7.106e-15,
 | 
			
		||||
            -1.523e-15,
 | 
			
		||||
            -9.4e-17,
 | 
			
		||||
            1.21e-16,
 | 
			
		||||
            -2.8e-17);
 | 
			
		||||
 | 
			
		||||
        $ncof = count($coefficients);
 | 
			
		||||
        $d = 0.0;
 | 
			
		||||
        $dd = 0.0;
 | 
			
		||||
 | 
			
		||||
        for ($j = $ncof - 1; $j > 0; $j--)
 | 
			
		||||
        {
 | 
			
		||||
        for ($j = $ncof - 1; $j > 0; $j--) {
 | 
			
		||||
            $tmp = $d;
 | 
			
		||||
            $d = $ty*$d - $dd + $coefficients[$j];
 | 
			
		||||
            $d = $ty * $d - $dd + $coefficients[$j];
 | 
			
		||||
            $dd = $tmp;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $ans = $t*exp(-$z*$z + 0.5*($coefficients[0] + $ty*$d) - $dd);
 | 
			
		||||
        $ans = $t * exp(-$z * $z + 0.5 * ($coefficients[0] + $ty * $d) - $dd);
 | 
			
		||||
        return ($x >= 0.0) ? $ans : (2.0 - $ans);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    private static function inverseErrorFunctionCumulativeTo($p)
 | 
			
		||||
    {
 | 
			
		||||
        // From page 265 of numerical recipes                       
 | 
			
		||||
 | 
			
		||||
        if ($p >= 2.0)
 | 
			
		||||
        {
 | 
			
		||||
        if ($p >= 2.0) {
 | 
			
		||||
            return -100;
 | 
			
		||||
        }
 | 
			
		||||
        if ($p <= 0.0)
 | 
			
		||||
        {
 | 
			
		||||
        if ($p <= 0.0) {
 | 
			
		||||
            return 100;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $pp = ($p < 1.0) ? $p : 2 - $p;
 | 
			
		||||
        $t = sqrt(-2*log($pp/2.0)); // Initial guess
 | 
			
		||||
        $x = -0.70711*((2.30753 + $t*0.27061)/(1.0 + $t*(0.99229 + $t*0.04481)) - $t);
 | 
			
		||||
        $t = sqrt(-2 * log($pp / 2.0)); // Initial guess
 | 
			
		||||
        $x = -0.70711 * ((2.30753 + $t * 0.27061) / (1.0 + $t * (0.99229 + $t * 0.04481)) - $t);
 | 
			
		||||
 | 
			
		||||
        for ($j = 0; $j < 2; $j++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($j = 0; $j < 2; $j++) {
 | 
			
		||||
            $err = GaussianDistribution::errorFunctionCumulativeTo($x) - $pp;
 | 
			
		||||
            $x += $err/(1.12837916709551257*exp(-square($x)) - $x*$err); // Halley                
 | 
			
		||||
            $x += $err / (1.12837916709551257 * exp(-BasicMatch::square($x)) - $x * $err); // Halley
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return ($p < 1.0) ? $x : -$x;
 | 
			
		||||
@@ -269,12 +250,11 @@ class GaussianDistribution
 | 
			
		||||
    public static function inverseCumulativeTo($x, $mean = 0.0, $standardDeviation = 1.0)
 | 
			
		||||
    {
 | 
			
		||||
        // From numerical recipes, page 320
 | 
			
		||||
        return $mean - sqrt(2)*$standardDeviation*GaussianDistribution::inverseErrorFunctionCumulativeTo(2*$x);
 | 
			
		||||
        return $mean - sqrt(2) * $standardDeviation * GaussianDistribution::inverseErrorFunctionCumulativeTo(2 * $x);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function __toString()
 | 
			
		||||
    {
 | 
			
		||||
        return sprintf("mean=%.4f standardDeviation=%.4f", $this->_mean, $this->_standardDeviation);
 | 
			
		||||
    }  
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								src/Numerics/IdentityMatrix.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Numerics/IdentityMatrix.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class IdentityMatrix extends DiagonalMatrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct($rows)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct(array_fill(0, $rows, 1));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Numerics;
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class Matrix
 | 
			
		||||
{
 | 
			
		||||
@@ -369,76 +368,4 @@ class Matrix
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class Vector extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(array $vectorValues)
 | 
			
		||||
    {
 | 
			
		||||
        $columnValues = array();
 | 
			
		||||
        foreach($vectorValues as $currentVectorValue)
 | 
			
		||||
        {
 | 
			
		||||
            $columnValues[] = array($currentVectorValue);
 | 
			
		||||
        }
 | 
			
		||||
        parent::__construct(count($vectorValues), 1, $columnValues);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class SquareMatrix extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $allValues = \func_get_args();
 | 
			
		||||
        $rows = (int) sqrt(count($allValues));
 | 
			
		||||
        $cols = $rows;
 | 
			
		||||
        
 | 
			
		||||
        $matrixData = array();
 | 
			
		||||
        $allValuesIndex = 0;
 | 
			
		||||
 | 
			
		||||
        for ($currentRow = 0; $currentRow < $rows; $currentRow++)
 | 
			
		||||
        {        
 | 
			
		||||
            for ($currentColumn = 0; $currentColumn < $cols; $currentColumn++)
 | 
			
		||||
            {
 | 
			
		||||
                $matrixData[$currentRow][$currentColumn] = $allValues[$allValuesIndex++];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
                
 | 
			
		||||
        parent::__construct($rows, $cols, $matrixData);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class DiagonalMatrix extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(array $diagonalValues)
 | 
			
		||||
    {
 | 
			
		||||
        $diagonalCount = count($diagonalValues);
 | 
			
		||||
        $rowCount = $diagonalCount;
 | 
			
		||||
        $colCount = $rowCount;
 | 
			
		||||
        
 | 
			
		||||
        parent::__construct($rowCount, $colCount);
 | 
			
		||||
        
 | 
			
		||||
        for($currentRow = 0; $currentRow < $rowCount; $currentRow++)
 | 
			
		||||
        {
 | 
			
		||||
            for($currentCol = 0; $currentCol < $colCount; $currentCol++)
 | 
			
		||||
            {
 | 
			
		||||
                if($currentRow == $currentCol)
 | 
			
		||||
                {
 | 
			
		||||
                    $this->setValue($currentRow, $currentCol, $diagonalValues[$currentRow]);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    $this->setValue($currentRow, $currentCol, 0);
 | 
			
		||||
                }                
 | 
			
		||||
            }
 | 
			
		||||
        }             
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class IdentityMatrix extends DiagonalMatrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct($rows)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct(\array_fill(0, $rows, 1));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,10 +1,10 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Numerics;
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
// The whole purpose of this class is to make the code for the SkillCalculator(s)
 | 
			
		||||
// look a little cleaner
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
 | 
			
		||||
class Range
 | 
			
		||||
{
 | 
			
		||||
    private $_min;
 | 
			
		||||
@@ -57,6 +57,4 @@ class Range
 | 
			
		||||
    {
 | 
			
		||||
        return ($this->_min <= $value) && ($value <= $this->_max);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								src/Numerics/SquareMatrix.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/Numerics/SquareMatrix.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class SquareMatrix extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $allValues = func_get_args();
 | 
			
		||||
        $rows = (int)sqrt(count($allValues));
 | 
			
		||||
        $cols = $rows;
 | 
			
		||||
 | 
			
		||||
        $matrixData = array();
 | 
			
		||||
        $allValuesIndex = 0;
 | 
			
		||||
 | 
			
		||||
        for ($currentRow = 0; $currentRow < $rows; $currentRow++) {
 | 
			
		||||
            for ($currentColumn = 0; $currentColumn < $cols; $currentColumn++) {
 | 
			
		||||
                $matrixData[$currentRow][$currentColumn] = $allValues[$allValuesIndex++];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        parent::__construct($rows, $cols, $matrixData);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								src/Numerics/Vector.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/Numerics/Vector.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
<?php namespace Moserware\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class Vector extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(array $vectorValues)
 | 
			
		||||
    {
 | 
			
		||||
        $columnValues = array();
 | 
			
		||||
        foreach($vectorValues as $currentVectorValue)
 | 
			
		||||
        {
 | 
			
		||||
            $columnValues[] = array($currentVectorValue);
 | 
			
		||||
        }
 | 
			
		||||
        parent::__construct(count($vectorValues), 1, $columnValues);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents a comparison between two players.
 | 
			
		||||
@@ -15,13 +14,11 @@ class PairwiseComparison
 | 
			
		||||
    {
 | 
			
		||||
        switch ($comparison) {
 | 
			
		||||
            case PairwiseComparison::WIN:
 | 
			
		||||
                return array(1,2);
 | 
			
		||||
                return array(1, 2);
 | 
			
		||||
            case PairwiseComparison::LOSE:
 | 
			
		||||
                return array(2,1);
 | 
			
		||||
                return array(2, 1);
 | 
			
		||||
            default:
 | 
			
		||||
                return array(1,1);
 | 
			
		||||
                return array(1, 1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/ISupportPartialPlay.php");
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
class PartialPlay
 | 
			
		||||
{
 | 
			
		||||
@@ -9,8 +6,7 @@ class PartialPlay
 | 
			
		||||
    {
 | 
			
		||||
        // If the player doesn't support the interface, assume 1.0 == 100%
 | 
			
		||||
        $supportsPartialPlay = $player instanceof ISupportPartialPlay;
 | 
			
		||||
        if (!$supportsPartialPlay)
 | 
			
		||||
        {
 | 
			
		||||
        if (!$supportsPartialPlay) {
 | 
			
		||||
            return 1.0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -18,13 +14,10 @@ class PartialPlay
 | 
			
		||||
 | 
			
		||||
        // HACK to get around bug near 0
 | 
			
		||||
        $smallestPercentage = 0.0001;
 | 
			
		||||
        if ($partialPlayPercentage < $smallestPercentage)
 | 
			
		||||
        {
 | 
			
		||||
        if ($partialPlayPercentage < $smallestPercentage) {
 | 
			
		||||
            $partialPlayPercentage = $smallestPercentage;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $partialPlayPercentage;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Guard.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/ISupportPartialPlay.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/ISupportPartialUpdate.php");
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents a player who has a Rating.
 | 
			
		||||
@@ -19,7 +14,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructs a player.
 | 
			
		||||
     * 
 | 
			
		||||
     *
 | 
			
		||||
     * @param mixed $id The identifier for the player, such as a name.
 | 
			
		||||
     * @param number $partialPlayPercentage The weight percentage to give this player when calculating a new rank.
 | 
			
		||||
     * @param number $partialUpdatePercentage Indicated how much of a skill update a player should receive where 0 represents no update and 1.0 represents 100% of the update.
 | 
			
		||||
@@ -44,7 +39,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate
 | 
			
		||||
        $id = &$this->_Id;
 | 
			
		||||
        return $this->_Id;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Indicates the percent of the time the player should be weighted where 0.0 indicates the player didn't play and 1.0 indicates the player played 100% of the time.
 | 
			
		||||
     */
 | 
			
		||||
@@ -52,7 +47,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_PartialPlayPercentage;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Indicated how much of a skill update a player should receive where 0.0 represents no update and 1.0 represents 100% of the update.
 | 
			
		||||
     */
 | 
			
		||||
@@ -60,15 +55,13 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_PartialUpdatePercentage;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function __toString()
 | 
			
		||||
    {
 | 
			
		||||
        if ($this->_Id != null)
 | 
			
		||||
        {
 | 
			
		||||
        if ($this->_Id != null) {
 | 
			
		||||
            return (string)$this->_Id;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return parent::__toString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Numerics/Range.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\Range;
 | 
			
		||||
use Moserware\Skills\Numerics\Range;
 | 
			
		||||
 | 
			
		||||
class PlayersRange extends Range
 | 
			
		||||
{
 | 
			
		||||
@@ -11,11 +8,9 @@ class PlayersRange extends Range
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($min, $max);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    protected static function create($min, $max)
 | 
			
		||||
    {
 | 
			
		||||
        return new PlayersRange($min, $max);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Helper class to sort ranks in non-decreasing order.
 | 
			
		||||
@@ -9,15 +7,14 @@ class RankSorter
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Performs an in-place sort of the items in according to the ranks in non-decreasing order.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param $items The items to sort according to the order specified by ranks.
 | 
			
		||||
     * @param $ranks The ranks for each item where 1 is first place.
 | 
			
		||||
     *
 | 
			
		||||
     * @param array $teams The items to sort according to the order specified by ranks.
 | 
			
		||||
     * @param array $teamRanks The ranks for each item where 1 is first place.
 | 
			
		||||
     * @return array
 | 
			
		||||
     */
 | 
			
		||||
    public static function sort(array &$teams, array &$teamRanks)
 | 
			
		||||
    {        
 | 
			
		||||
        array_multisort($teamRanks, $teams);
 | 
			
		||||
        return $teams;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
// Container for a player's rating.
 | 
			
		||||
class Rating 
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
class Rating
 | 
			
		||||
{
 | 
			
		||||
    const CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    private $_conservativeStandardDeviationMultiplier;
 | 
			
		||||
    private $_mean;
 | 
			
		||||
    private $_standardDeviation;
 | 
			
		||||
@@ -15,7 +15,7 @@ class Rating
 | 
			
		||||
     * Constructs a rating.
 | 
			
		||||
     * @param double $mean The statistical mean value of the rating (also known as mu).
 | 
			
		||||
     * @param double $standardDeviation The standard deviation of the rating (also known as s).
 | 
			
		||||
     * @param double $conservativeStandardDeviationMultiplier optional The number of standardDeviations to subtract from the mean to achieve a conservative rating.
 | 
			
		||||
     * @param float|int $conservativeStandardDeviationMultiplier optional The number of standardDeviations to subtract from the mean to achieve a conservative rating.
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct($mean, $standardDeviation, $conservativeStandardDeviationMultiplier = self::CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER)
 | 
			
		||||
    {
 | 
			
		||||
@@ -29,7 +29,7 @@ class Rating
 | 
			
		||||
     */
 | 
			
		||||
    public function getMean()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_mean;        
 | 
			
		||||
        return $this->_mean;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -45,27 +45,28 @@ class Rating
 | 
			
		||||
     */
 | 
			
		||||
    public function getConservativeRating()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_mean - $this->_conservativeStandardDeviationMultiplier*$this->_standardDeviation;
 | 
			
		||||
        return $this->_mean - $this->_conservativeStandardDeviationMultiplier * $this->_standardDeviation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage)
 | 
			
		||||
    {
 | 
			
		||||
        $priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation());
 | 
			
		||||
        $posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior.getStandardDeviation());
 | 
			
		||||
        $posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior . getStandardDeviation());
 | 
			
		||||
 | 
			
		||||
        // From a clarification email from Ralf Herbrich:
 | 
			
		||||
        // "the idea is to compute a linear interpolation between the prior and posterior skills of each player 
 | 
			
		||||
        //  ... in the canonical space of parameters"
 | 
			
		||||
 | 
			
		||||
        $precisionDifference = $posteriorGaussian->getPrecision() - $priorGaussian->getPrecision();
 | 
			
		||||
        $partialPrecisionDifference = $updatePercentage*$precisionDifference;
 | 
			
		||||
        $partialPrecisionDifference = $updatePercentage * $precisionDifference;
 | 
			
		||||
 | 
			
		||||
        $precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian.getPrecisionMean();
 | 
			
		||||
        $partialPrecisionMeanDifference = $updatePercentage*$precisionMeanDifference;
 | 
			
		||||
        $precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian . getPrecisionMean();
 | 
			
		||||
        $partialPrecisionMeanDifference = $updatePercentage * $precisionMeanDifference;
 | 
			
		||||
 | 
			
		||||
        $partialPosteriorGaussion = GaussianDistribution::fromPrecisionMean(
 | 
			
		||||
            $priorGaussian->getPrecisionMean() + $partialPrecisionMeanDifference,
 | 
			
		||||
            $priorGaussian->getPrecision() + $partialPrecisionDifference);
 | 
			
		||||
            $priorGaussian->getPrecision() + $partialPrecisionDifference
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return new Rating($partialPosteriorGaussion->getMean(), $partialPosteriorGaussion->getStandardDeviation(), $prior->_conservativeStandardDeviationMultiplier);
 | 
			
		||||
    }
 | 
			
		||||
@@ -73,7 +74,5 @@ class Rating
 | 
			
		||||
    public function __toString()
 | 
			
		||||
    {
 | 
			
		||||
        return sprintf("mean=%.4f, standardDeviation=%.4f", $this->_mean, $this->_standardDeviation);
 | 
			
		||||
    }  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/HashMap.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/Player.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/Rating.php");
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
class RatingContainer
 | 
			
		||||
{
 | 
			
		||||
@@ -24,13 +19,13 @@ class RatingContainer
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_playerToRating->setValue($player, $rating);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function &getAllPlayers()
 | 
			
		||||
    {
 | 
			
		||||
        $allPlayers = &$this->_playerToRating->getAllKeys();
 | 
			
		||||
        return $allPlayers;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public function &getAllRatings()
 | 
			
		||||
    {
 | 
			
		||||
        $allRatings = &$this->_playerToRating->getAllValues();
 | 
			
		||||
@@ -40,6 +35,5 @@ class RatingContainer
 | 
			
		||||
    public function count()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->_playerToRating->count();
 | 
			
		||||
    }    
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,8 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/GameInfo.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/PlayersRange.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TeamsRange.php");
 | 
			
		||||
use Exception;
 | 
			
		||||
 | 
			
		||||
/** 
 | 
			
		||||
/**
 | 
			
		||||
 * Base class for all skill calculator implementations.
 | 
			
		||||
 */
 | 
			
		||||
abstract class SkillCalculator
 | 
			
		||||
@@ -13,7 +10,7 @@ abstract class SkillCalculator
 | 
			
		||||
    private $_supportedOptions;
 | 
			
		||||
    private $_playersPerTeamAllowed;
 | 
			
		||||
    private $_totalTeamsAllowed;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    protected function __construct($supportedOptions, TeamsRange $totalTeamsAllowed, PlayersRange $playerPerTeamAllowed)
 | 
			
		||||
    {
 | 
			
		||||
        $this->_supportedOptions = $supportedOptions;
 | 
			
		||||
@@ -23,9 +20,10 @@ abstract class SkillCalculator
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Calculates new ratings based on the prior ratings and team ranks.
 | 
			
		||||
     * @param $gameInfo Parameters for the game.
 | 
			
		||||
     * @param $teams A mapping of team players and their ratings.
 | 
			
		||||
     * @param $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2).
 | 
			
		||||
     * @param GameInfo $gameInfo Parameters for the game.
 | 
			
		||||
     * @param array $teamsOfPlayerToRatings A mapping of team players and their ratings.
 | 
			
		||||
     * @param array $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2).
 | 
			
		||||
     *
 | 
			
		||||
     * @return All the players and their new ratings.
 | 
			
		||||
     */
 | 
			
		||||
    public abstract function calculateNewRatings(GameInfo &$gameInfo,
 | 
			
		||||
@@ -34,18 +32,18 @@ abstract class SkillCalculator
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Calculates the match quality as the likelihood of all teams drawing.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param $gameInfo Parameters for the game.
 | 
			
		||||
     * @param $teams A mapping of team players and their ratings.
 | 
			
		||||
     *
 | 
			
		||||
     * @param GameInfo $gameInfo Parameters for the game.
 | 
			
		||||
     * @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,
 | 
			
		||||
                                                   array &$teamsOfPlayerToRatings);
 | 
			
		||||
 | 
			
		||||
    public function isSupported($option)
 | 
			
		||||
    {           
 | 
			
		||||
        return ($this->_supportedOptions & $option) == $option;             
 | 
			
		||||
    }    
 | 
			
		||||
    {
 | 
			
		||||
        return ($this->_supportedOptions & $option) == $option;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function validateTeamCountAndPlayersCountPerTeam(array &$teamsOfPlayerToRatings)
 | 
			
		||||
    {
 | 
			
		||||
@@ -56,20 +54,17 @@ abstract class SkillCalculator
 | 
			
		||||
        array &$teams,
 | 
			
		||||
        TeamsRange &$totalTeams,
 | 
			
		||||
        PlayersRange &$playersPerTeam)
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        $countOfTeams = 0;
 | 
			
		||||
        
 | 
			
		||||
        foreach ($teams as $currentTeam)
 | 
			
		||||
        {
 | 
			
		||||
            if (!$playersPerTeam->isInRange($currentTeam->count()))
 | 
			
		||||
            {
 | 
			
		||||
                throw new \Exception("Player count is not in range");
 | 
			
		||||
 | 
			
		||||
        foreach ($teams as $currentTeam) {
 | 
			
		||||
            if (!$playersPerTeam->isInRange($currentTeam->count())) {
 | 
			
		||||
                throw new Exception("Player count is not in range");
 | 
			
		||||
            }
 | 
			
		||||
            $countOfTeams++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!$totalTeams->isInRange($countOfTeams))
 | 
			
		||||
        {
 | 
			
		||||
        if (!$totalTeams->isInRange($countOfTeams)) {
 | 
			
		||||
            throw new Exception("Team range is not in range");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -80,5 +75,4 @@ class SkillCalculatorSupportedOptions
 | 
			
		||||
    const NONE = 0x00;
 | 
			
		||||
    const PARTIAL_PLAY = 0x01;
 | 
			
		||||
    const PARTIAL_UPDATE = 0x02;
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								src/Team.php
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								src/Team.php
									
									
									
									
									
								
							@@ -1,9 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . '/Player.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Rating.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/RatingContainer.php');
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
class Team extends RatingContainer
 | 
			
		||||
{
 | 
			
		||||
@@ -11,7 +6,7 @@ class Team extends RatingContainer
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct();
 | 
			
		||||
        
 | 
			
		||||
        if(!\is_null($player))
 | 
			
		||||
        if(!is_null($player))
 | 
			
		||||
        {
 | 
			
		||||
            $this->addPlayer($player, $rating);
 | 
			
		||||
        }
 | 
			
		||||
@@ -22,6 +17,4 @@ class Team extends RatingContainer
 | 
			
		||||
        $this->setRating($player, $rating);
 | 
			
		||||
        return $this;
 | 
			
		||||
    }    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,10 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
class Teams
 | 
			
		||||
{
 | 
			
		||||
    public static function concat(/*variable arguments*/)
 | 
			
		||||
    {
 | 
			
		||||
        $args = \func_get_args();
 | 
			
		||||
        $args = func_get_args();
 | 
			
		||||
        $result = array();
 | 
			
		||||
 | 
			
		||||
        foreach ($args as &$currentTeam) {
 | 
			
		||||
@@ -15,5 +14,4 @@ class Teams
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills;
 | 
			
		||||
<?php namespace Moserware\Skills;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/Numerics/Range.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\Range;
 | 
			
		||||
use Moserware\Skills\Numerics\Range;
 | 
			
		||||
 | 
			
		||||
class TeamsRange extends Range
 | 
			
		||||
{
 | 
			
		||||
@@ -16,6 +13,4 @@ class TeamsRange extends Range
 | 
			
		||||
    {
 | 
			
		||||
        return new TeamsRange($min, $max);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Numerics/GaussianDistribution.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
final class DrawMargin
 | 
			
		||||
{
 | 
			
		||||
@@ -17,10 +14,6 @@ final class DrawMargin
 | 
			
		||||
        //
 | 
			
		||||
        // margin = inversecdf((draw probability + 1)/2) * sqrt(n1+n2) * beta
 | 
			
		||||
        // n1 and n2 are the number of players on each team
 | 
			
		||||
        $margin = GaussianDistribution::inverseCumulativeTo(.5*($drawProbability + 1), 0, 1)*sqrt(1 + 1)*
 | 
			
		||||
                        $beta;
 | 
			
		||||
        return $margin;
 | 
			
		||||
        return GaussianDistribution::inverseCumulativeTo(.5 * ($drawProbability + 1), 0, 1) * sqrt(1 + 1) * $beta;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,28 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../GameInfo.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Guard.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../ISupportPartialPlay.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../ISupportPartialUpdate.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PartialPlay.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PlayersRange.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../RankSorter.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../SkillCalculator.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TeamsRange.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Numerics/BasicMath.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Numerics/Matrix.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraph.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\DiagonalMatrix;
 | 
			
		||||
use Moserware\Numerics\Matrix;
 | 
			
		||||
use Moserware\Numerics\Vector;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
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\DiagonalMatrix;
 | 
			
		||||
use Moserware\Skills\Numerics\Matrix;
 | 
			
		||||
use Moserware\Skills\Numerics\Vector;
 | 
			
		||||
use Moserware\Skills\PartialPlay;
 | 
			
		||||
use Moserware\Skills\PlayersRange;
 | 
			
		||||
use Moserware\Skills\RankSorter;
 | 
			
		||||
@@ -52,7 +37,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        $factorGraph = new TrueSkillFactorGraph($gameInfo, $teams, $teamRanks);
 | 
			
		||||
        $factorGraph->buildGraph();
 | 
			
		||||
        $factorGraph->runSchedule();
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        $probabilityOfOutcome = $factorGraph->getProbabilityOfRanking();
 | 
			
		||||
 | 
			
		||||
        return $factorGraph->getUpdatedRatings();
 | 
			
		||||
@@ -70,17 +55,19 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        $playerTeamAssignmentsMatrix = $this->createPlayerTeamAssignmentMatrix($teamAssignmentsList, $meanVector->getRowCount());
 | 
			
		||||
        $playerTeamAssignmentsMatrixTranspose = $playerTeamAssignmentsMatrix->getTranspose();
 | 
			
		||||
 | 
			
		||||
        $betaSquared = square($gameInfo->getBeta());
 | 
			
		||||
        $betaSquared = BasicMatch::square($gameInfo->getBeta());
 | 
			
		||||
 | 
			
		||||
        $start = Matrix::multiply($meanVectorTranspose, $playerTeamAssignmentsMatrix);
 | 
			
		||||
 | 
			
		||||
        $aTa = Matrix::multiply(
 | 
			
		||||
                        Matrix::scalarMultiply($betaSquared,
 | 
			
		||||
                                               $playerTeamAssignmentsMatrixTranspose),
 | 
			
		||||
                        $playerTeamAssignmentsMatrix);
 | 
			
		||||
            Matrix::scalarMultiply($betaSquared, $playerTeamAssignmentsMatrixTranspose),
 | 
			
		||||
            $playerTeamAssignmentsMatrix
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $aTSA = Matrix::multiply(
 | 
			
		||||
                    Matrix::multiply($playerTeamAssignmentsMatrixTranspose, $skillsMatrix),
 | 
			
		||||
                    $playerTeamAssignmentsMatrix);
 | 
			
		||||
            Matrix::multiply($playerTeamAssignmentsMatrixTranspose, $skillsMatrix),
 | 
			
		||||
            $playerTeamAssignmentsMatrix
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $middle = Matrix::add($aTa, $aTSA);
 | 
			
		||||
 | 
			
		||||
@@ -104,10 +91,9 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
    {
 | 
			
		||||
        // A simple vector of all the player means.
 | 
			
		||||
        return new Vector(self::getPlayerRatingValues($teamAssignmentsList,
 | 
			
		||||
                                                      function($rating) 
 | 
			
		||||
                                                      { 
 | 
			
		||||
                                                           return $rating->getMean();
 | 
			
		||||
                                                      }));
 | 
			
		||||
            function ($rating) {
 | 
			
		||||
                return $rating->getMean();
 | 
			
		||||
            }));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static function getPlayerCovarianceMatrix(array &$teamAssignmentsList)
 | 
			
		||||
@@ -115,11 +101,10 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        // This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all
 | 
			
		||||
        // players.
 | 
			
		||||
        return new DiagonalMatrix(
 | 
			
		||||
                        self::getPlayerRatingValues($teamAssignmentsList,
 | 
			
		||||
                                                    function($rating)
 | 
			
		||||
                                                    {
 | 
			
		||||
                                                       return square($rating->getStandardDeviation());
 | 
			
		||||
                                                    }));
 | 
			
		||||
            self::getPlayerRatingValues($teamAssignmentsList,
 | 
			
		||||
                function ($rating) {
 | 
			
		||||
                    return BasicMatch::square($rating->getStandardDeviation());
 | 
			
		||||
                }));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Helper function that gets a list of values for all player ratings
 | 
			
		||||
@@ -128,10 +113,8 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
    {
 | 
			
		||||
        $playerRatingValues = array();
 | 
			
		||||
 | 
			
		||||
        foreach ($teamAssignmentsList as $currentTeam)
 | 
			
		||||
        {
 | 
			
		||||
            foreach ($currentTeam->getAllRatings() as $currentRating)
 | 
			
		||||
            {
 | 
			
		||||
        foreach ($teamAssignmentsList as $currentTeam) {
 | 
			
		||||
            foreach ($currentTeam->getAllRatings() as $currentRating) {
 | 
			
		||||
                $playerRatingValues[] = $playerRatingFunction($currentRating);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -164,16 +147,14 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
 | 
			
		||||
        $currentColumn = 0;
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $teamAssignmentsListCount - 1; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $teamAssignmentsListCount - 1; $i++) {
 | 
			
		||||
            $currentTeam = $teamAssignmentsList[$i];
 | 
			
		||||
 | 
			
		||||
            // Need to add in 0's for all the previous players, since they're not
 | 
			
		||||
            // on this team
 | 
			
		||||
            $playerAssignments[$currentColumn] = ($totalPreviousPlayers > 0) ? \array_fill(0, $totalPreviousPlayers, 0) : array();
 | 
			
		||||
 | 
			
		||||
            foreach ($currentTeam->getAllPlayers() as $currentPlayer)
 | 
			
		||||
            {
 | 
			
		||||
            foreach ($currentTeam->getAllPlayers() as $currentPlayer) {
 | 
			
		||||
                $playerAssignments[$currentColumn][] = PartialPlay::getPartialPlayPercentage($currentPlayer);
 | 
			
		||||
                // indicates the player is on the team
 | 
			
		||||
                $totalPreviousPlayers++;
 | 
			
		||||
@@ -182,15 +163,13 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
            $rowsRemaining = $totalPlayers - $totalPreviousPlayers;
 | 
			
		||||
 | 
			
		||||
            $nextTeam = $teamAssignmentsList[$i + 1];
 | 
			
		||||
            foreach ($nextTeam->getAllPlayers() as $nextTeamPlayer)
 | 
			
		||||
            {
 | 
			
		||||
            foreach ($nextTeam->getAllPlayers() as $nextTeamPlayer) {
 | 
			
		||||
                // Add a -1 * playing time to represent the difference
 | 
			
		||||
                $playerAssignments[$currentColumn][] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayer);
 | 
			
		||||
                $rowsRemaining--;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            for ($ixAdditionalRow = 0; $ixAdditionalRow < $rowsRemaining; $ixAdditionalRow++)
 | 
			
		||||
            {
 | 
			
		||||
            for ($ixAdditionalRow = 0; $ixAdditionalRow < $rowsRemaining; $ixAdditionalRow++) {
 | 
			
		||||
                // Pad with zeros
 | 
			
		||||
                $playerAssignments[$currentColumn][] = 0;
 | 
			
		||||
            }
 | 
			
		||||
@@ -202,6 +181,4 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
 | 
			
		||||
        return $playerTeamAssignmentsMatrix;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,16 +1,9 @@
 | 
			
		||||
<?php
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Factor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Factor;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Message;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
abstract class GaussianFactor extends Factor
 | 
			
		||||
{
 | 
			
		||||
@@ -21,6 +14,9 @@ abstract class GaussianFactor extends Factor
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sends the factor-graph message with and returns the log-normalization constant.
 | 
			
		||||
     * @param Message $message
 | 
			
		||||
     * @param Variable $variable
 | 
			
		||||
     * @return float|int
 | 
			
		||||
     */
 | 
			
		||||
    protected function sendMessageVariable(Message &$message, Variable &$variable)
 | 
			
		||||
    {
 | 
			
		||||
@@ -35,11 +31,9 @@ abstract class GaussianFactor extends Factor
 | 
			
		||||
    {
 | 
			
		||||
        $newDistribution = GaussianDistribution::fromPrecisionMean(0, 0);
 | 
			
		||||
        $binding = &parent::createVariableToMessageBindingWithMessage($variable,
 | 
			
		||||
                                                      new Message(
 | 
			
		||||
                                                          $newDistribution,
 | 
			
		||||
                                                          sprintf("message from %s to %s", $this, $variable)));
 | 
			
		||||
            new Message(
 | 
			
		||||
                $newDistribution,
 | 
			
		||||
                sprintf("message from %s to %s", $this, $variable)));
 | 
			
		||||
        return $binding;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,13 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TruncatedGaussianCorrectionFunctions.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/GaussianFactor.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Message;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
@@ -36,10 +29,10 @@ class GaussianGreaterThanFactor extends GaussianFactor
 | 
			
		||||
        $message = &$messages[0]->getValue();
 | 
			
		||||
        $messageFromVariable = GaussianDistribution::divide($marginal, $message);
 | 
			
		||||
        return -GaussianDistribution::logProductNormalization($messageFromVariable, $message)
 | 
			
		||||
               +
 | 
			
		||||
               log(
 | 
			
		||||
                   GaussianDistribution::cumulativeTo(($messageFromVariable->getMean() - $this->_epsilon)/
 | 
			
		||||
                                                     $messageFromVariable->getStandardDeviation()));
 | 
			
		||||
        +
 | 
			
		||||
        log(
 | 
			
		||||
            GaussianDistribution::cumulativeTo(($messageFromVariable->getMean() - $this->_epsilon) /
 | 
			
		||||
                $messageFromVariable->getStandardDeviation()));
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -54,24 +47,24 @@ class GaussianGreaterThanFactor extends GaussianFactor
 | 
			
		||||
 | 
			
		||||
        $sqrtC = sqrt($c);
 | 
			
		||||
 | 
			
		||||
        $dOnSqrtC = $d/$sqrtC;
 | 
			
		||||
        $dOnSqrtC = $d / $sqrtC;
 | 
			
		||||
 | 
			
		||||
        $epsilsonTimesSqrtC = $this->_epsilon*$sqrtC;
 | 
			
		||||
        $epsilsonTimesSqrtC = $this->_epsilon * $sqrtC;
 | 
			
		||||
        $d = $messageFromVar->getPrecisionMean();
 | 
			
		||||
 | 
			
		||||
        $denom = 1.0 - TruncatedGaussianCorrectionFunctions::wExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC);
 | 
			
		||||
 | 
			
		||||
        $newPrecision = $c/$denom;
 | 
			
		||||
        $newPrecision = $c / $denom;
 | 
			
		||||
        $newPrecisionMean = ($d +
 | 
			
		||||
                             $sqrtC*
 | 
			
		||||
                             TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC))/
 | 
			
		||||
                             $denom;
 | 
			
		||||
                $sqrtC *
 | 
			
		||||
                TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)) /
 | 
			
		||||
            $denom;
 | 
			
		||||
 | 
			
		||||
        $newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
 | 
			
		||||
 | 
			
		||||
        $newMessage = GaussianDistribution::divide(
 | 
			
		||||
                              GaussianDistribution::multiply($oldMessage, $newMarginal),
 | 
			
		||||
                              $oldMarginal);
 | 
			
		||||
            GaussianDistribution::multiply($oldMessage, $newMarginal),
 | 
			
		||||
            $oldMarginal);
 | 
			
		||||
 | 
			
		||||
        // Update the message and marginal
 | 
			
		||||
        $message->setValue($newMessage);
 | 
			
		||||
@@ -81,5 +74,4 @@ class GaussianGreaterThanFactor extends GaussianFactor
 | 
			
		||||
        // Return the difference in the new marginal
 | 
			
		||||
        return GaussianDistribution::subtract($newMarginal, $oldMarginal);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,18 +1,13 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/GaussianFactor.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Exception;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Message;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Connects two variables and adds uncertainty.
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * See the accompanying math paper for more details.
 | 
			
		||||
 */
 | 
			
		||||
class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
@@ -22,7 +17,7 @@ class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
    public function __construct($betaSquared, Variable &$variable1, Variable &$variable2)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct(sprintf("Likelihood of %s going to %s", $variable2, $variable1));
 | 
			
		||||
        $this->_precision = 1.0/$betaSquared;
 | 
			
		||||
        $this->_precision = 1.0 / $betaSquared;
 | 
			
		||||
        $this->createVariableToMessageBinding($variable1);
 | 
			
		||||
        $this->createVariableToMessageBinding($variable2);
 | 
			
		||||
    }
 | 
			
		||||
@@ -33,24 +28,24 @@ class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
        $messages = &$this->getMessages();
 | 
			
		||||
 | 
			
		||||
        return GaussianDistribution::logRatioNormalization(
 | 
			
		||||
                $vars[0]->getValue(),
 | 
			
		||||
                $messages[0]->getValue());
 | 
			
		||||
            $vars[0]->getValue(),
 | 
			
		||||
            $messages[0]->getValue());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function updateHelper(Message &$message1, Message &$message2,
 | 
			
		||||
                                  Variable &$variable1, Variable &$variable2)
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        $message1Value = clone $message1->getValue();
 | 
			
		||||
        $message2Value = clone $message2->getValue();        
 | 
			
		||||
        
 | 
			
		||||
        $message2Value = clone $message2->getValue();
 | 
			
		||||
 | 
			
		||||
        $marginal1 = clone $variable1->getValue();
 | 
			
		||||
        $marginal2 = clone $variable2->getValue();
 | 
			
		||||
 | 
			
		||||
        $a = $this->_precision/($this->_precision + $marginal2->getPrecision() - $message2Value->getPrecision());
 | 
			
		||||
        $a = $this->_precision / ($this->_precision + $marginal2->getPrecision() - $message2Value->getPrecision());
 | 
			
		||||
 | 
			
		||||
        $newMessage = GaussianDistribution::fromPrecisionMean(
 | 
			
		||||
            $a*($marginal2->getPrecisionMean() - $message2Value->getPrecisionMean()),
 | 
			
		||||
            $a*($marginal2->getPrecision() - $message2Value->getPrecision()));
 | 
			
		||||
            $a * ($marginal2->getPrecisionMean() - $message2Value->getPrecisionMean()),
 | 
			
		||||
            $a * ($marginal2->getPrecision() - $message2Value->getPrecision()));
 | 
			
		||||
 | 
			
		||||
        $oldMarginalWithoutMessage = GaussianDistribution::divide($marginal1, $message1Value);
 | 
			
		||||
 | 
			
		||||
@@ -68,20 +63,17 @@ class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
    public function updateMessageIndex($messageIndex)
 | 
			
		||||
    {
 | 
			
		||||
        $messages = &$this->getMessages();
 | 
			
		||||
        $vars = &$this->getVariables();       
 | 
			
		||||
        $vars = &$this->getVariables();
 | 
			
		||||
 | 
			
		||||
        switch ($messageIndex)
 | 
			
		||||
        {
 | 
			
		||||
        switch ($messageIndex) {
 | 
			
		||||
            case 0:
 | 
			
		||||
                return $this->updateHelper($messages[0], $messages[1],
 | 
			
		||||
                                           $vars[0], $vars[1]);
 | 
			
		||||
                    $vars[0], $vars[1]);
 | 
			
		||||
            case 1:
 | 
			
		||||
                return $this->updateHelper($messages[1], $messages[0],
 | 
			
		||||
                                           $vars[1], $vars[0]);
 | 
			
		||||
                    $vars[1], $vars[0]);
 | 
			
		||||
            default:
 | 
			
		||||
                throw new Exception();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +1,8 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/GaussianFactor.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Message;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Supplies the factor graph with prior information.
 | 
			
		||||
@@ -24,7 +18,7 @@ class GaussianPriorFactor extends GaussianFactor
 | 
			
		||||
        parent::__construct(sprintf("Prior value going to %s", $variable));
 | 
			
		||||
        $this->_newMessage = new GaussianDistribution($mean, sqrt($variance));
 | 
			
		||||
        $newMessage = new Message(GaussianDistribution::fromPrecisionMean(0, 0),
 | 
			
		||||
                                  sprintf("message from %s to %s", $this, $variable));
 | 
			
		||||
            sprintf("message from %s to %s", $this, $variable));
 | 
			
		||||
 | 
			
		||||
        $this->createVariableToMessageBindingWithMessage($variable, $newMessage);
 | 
			
		||||
    }
 | 
			
		||||
@@ -43,6 +37,4 @@ class GaussianPriorFactor extends GaussianFactor
 | 
			
		||||
        $message->setValue($newMessage);
 | 
			
		||||
        return GaussianDistribution::subtract($oldMarginal, $newMarginal);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,21 +1,14 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Guard.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/BasicMath.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/GaussianFactor.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\Guard;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Message;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
use Moserware\Skills\Numerics\BasicMatch;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Factor that sums together multiple Gaussians.
 | 
			
		||||
 *  
 | 
			
		||||
 *
 | 
			
		||||
 * See the accompanying math paper for more details.s
 | 
			
		||||
 */
 | 
			
		||||
class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
@@ -37,20 +30,18 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
        // v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n
 | 
			
		||||
        $variableWeightsLength = count($variableWeights);
 | 
			
		||||
        $this->_weights[0] = \array_fill(0, count($variableWeights), 0);
 | 
			
		||||
        
 | 
			
		||||
        for($i = 0; $i < $variableWeightsLength; $i++)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $variableWeightsLength; $i++) {
 | 
			
		||||
            $weight = &$variableWeights[$i];
 | 
			
		||||
            $this->_weights[0][$i] = $weight;
 | 
			
		||||
            $this->_weightsSquared[0][$i] = square($weight);
 | 
			
		||||
            $this->_weightsSquared[0][$i] = BasicMatch::square($weight);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $variablesToSumLength = count($variablesToSum);
 | 
			
		||||
 | 
			
		||||
        // 0..n-1
 | 
			
		||||
        $this->_variableIndexOrdersForWeights[0] = array();
 | 
			
		||||
        for($i = 0; $i < ($variablesToSumLength + 1); $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < ($variablesToSumLength + 1); $i++) {
 | 
			
		||||
            $this->_variableIndexOrdersForWeights[0][] = $i;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -62,10 +53,9 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
        // By convention, we'll put the v_0 term at the end
 | 
			
		||||
 | 
			
		||||
        $weightsLength = $variableWeightsLength + 1;
 | 
			
		||||
        for ($weightsIndex = 1; $weightsIndex < $weightsLength; $weightsIndex++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($weightsIndex = 1; $weightsIndex < $weightsLength; $weightsIndex++) {
 | 
			
		||||
            $currentWeights = \array_fill(0, $variableWeightsLength, 0);
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            $variableIndices = \array_fill(0, $variableWeightsLength + 1, 0);
 | 
			
		||||
            $variableIndices[0] = $weightsIndex;
 | 
			
		||||
 | 
			
		||||
@@ -73,42 +63,38 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
 | 
			
		||||
            // keep a single variable to keep track of where we are in the array.
 | 
			
		||||
            // This is helpful since we skip over one of the spots
 | 
			
		||||
            $currentDestinationWeightIndex = 0;            
 | 
			
		||||
            $currentDestinationWeightIndex = 0;
 | 
			
		||||
 | 
			
		||||
            for ($currentWeightSourceIndex = 0;
 | 
			
		||||
                 $currentWeightSourceIndex < $variableWeightsLength;
 | 
			
		||||
                 $currentWeightSourceIndex++)
 | 
			
		||||
            {
 | 
			
		||||
                if ($currentWeightSourceIndex == ($weightsIndex - 1))
 | 
			
		||||
                {
 | 
			
		||||
                 $currentWeightSourceIndex++) {
 | 
			
		||||
                if ($currentWeightSourceIndex == ($weightsIndex - 1)) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                $currentWeight = (-$variableWeights[$currentWeightSourceIndex]/$variableWeights[$weightsIndex - 1]);
 | 
			
		||||
                $currentWeight = (-$variableWeights[$currentWeightSourceIndex] / $variableWeights[$weightsIndex - 1]);
 | 
			
		||||
 | 
			
		||||
                if ($variableWeights[$weightsIndex - 1] == 0)
 | 
			
		||||
                {
 | 
			
		||||
                if ($variableWeights[$weightsIndex - 1] == 0) {
 | 
			
		||||
                    // HACK: Getting around division by zero
 | 
			
		||||
                    $currentWeight = 0;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                $currentWeights[$currentDestinationWeightIndex] = $currentWeight;
 | 
			
		||||
                $currentWeightsSquared[$currentDestinationWeightIndex] = $currentWeight*$currentWeight;
 | 
			
		||||
                $currentWeightsSquared[$currentDestinationWeightIndex] = $currentWeight * $currentWeight;
 | 
			
		||||
 | 
			
		||||
                $variableIndices[$currentDestinationWeightIndex + 1] = $currentWeightSourceIndex + 1;
 | 
			
		||||
                $currentDestinationWeightIndex++;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // And the final one
 | 
			
		||||
            $finalWeight = 1.0/$variableWeights[$weightsIndex - 1];
 | 
			
		||||
            $finalWeight = 1.0 / $variableWeights[$weightsIndex - 1];
 | 
			
		||||
 | 
			
		||||
            if ($variableWeights[$weightsIndex - 1] == 0)
 | 
			
		||||
            {
 | 
			
		||||
            if ($variableWeights[$weightsIndex - 1] == 0) {
 | 
			
		||||
                // HACK: Getting around division by zero
 | 
			
		||||
                $finalWeight = 0;
 | 
			
		||||
            }
 | 
			
		||||
            $currentWeights[$currentDestinationWeightIndex] = $finalWeight;
 | 
			
		||||
            $currentWeightsSquared[$currentDestinationWeightIndex] = square($finalWeight);
 | 
			
		||||
            $currentWeightsSquared[$currentDestinationWeightIndex] = BasicMatch::square($finalWeight);
 | 
			
		||||
            $variableIndices[count($variableWeights)] = 0;
 | 
			
		||||
            $this->_variableIndexOrdersForWeights[] = $variableIndices;
 | 
			
		||||
 | 
			
		||||
@@ -118,11 +104,10 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
 | 
			
		||||
        $this->createVariableToMessageBinding($sumVariable);
 | 
			
		||||
 | 
			
		||||
        foreach ($variablesToSum as &$currentVariable)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($variablesToSum as &$currentVariable) {
 | 
			
		||||
            $localCurrentVariable = &$currentVariable;
 | 
			
		||||
            $this->createVariableToMessageBinding($localCurrentVariable);
 | 
			
		||||
        }        
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getLogNormalization()
 | 
			
		||||
@@ -134,8 +119,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
 | 
			
		||||
        // We start at 1 since offset 0 has the sum
 | 
			
		||||
        $varCount = count($vars);
 | 
			
		||||
        for ($i = 1; $i < $varCount; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 1; $i < $varCount; $i++) {
 | 
			
		||||
            $result += GaussianDistribution::logRatioNormalization($vars[$i]->getValue(), $messages[$i]->getValue());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -160,30 +144,29 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
 | 
			
		||||
        $weightsSquaredLength = count($weightsSquared);
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $weightsSquaredLength; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $weightsSquaredLength; $i++) {
 | 
			
		||||
            // These flow directly from the paper
 | 
			
		||||
 | 
			
		||||
            $inverseOfNewPrecisionSum += $weightsSquared[$i]/
 | 
			
		||||
                                         ($variables[$i + 1]->getValue()->getPrecision() - $messages[$i + 1]->getValue()->getPrecision());
 | 
			
		||||
            $inverseOfNewPrecisionSum += $weightsSquared[$i] /
 | 
			
		||||
                ($variables[$i + 1]->getValue()->getPrecision() - $messages[$i + 1]->getValue()->getPrecision());
 | 
			
		||||
 | 
			
		||||
            $diff = GaussianDistribution::divide($variables[$i + 1]->getValue(), $messages[$i + 1]->getValue());
 | 
			
		||||
            $anotherInverseOfNewPrecisionSum += $weightsSquared[$i]/$diff->getPrecision();
 | 
			
		||||
            $anotherInverseOfNewPrecisionSum += $weightsSquared[$i] / $diff->getPrecision();
 | 
			
		||||
 | 
			
		||||
            $weightedMeanSum += $weights[$i]
 | 
			
		||||
                                *
 | 
			
		||||
                                ($variables[$i + 1]->getValue()->getPrecisionMean() - $messages[$i + 1]->getValue()->getPrecisionMean())
 | 
			
		||||
                                /
 | 
			
		||||
                                ($variables[$i + 1]->getValue()->getPrecision() - $messages[$i + 1]->getValue()->getPrecision());
 | 
			
		||||
                *
 | 
			
		||||
                ($variables[$i + 1]->getValue()->getPrecisionMean() - $messages[$i + 1]->getValue()->getPrecisionMean())
 | 
			
		||||
                /
 | 
			
		||||
                ($variables[$i + 1]->getValue()->getPrecision() - $messages[$i + 1]->getValue()->getPrecision());
 | 
			
		||||
 | 
			
		||||
            $anotherWeightedMeanSum += $weights[$i]*$diff->getPrecisionMean()/$diff->getPrecision();
 | 
			
		||||
            $anotherWeightedMeanSum += $weights[$i] * $diff->getPrecisionMean() / $diff->getPrecision();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $newPrecision = 1.0/$inverseOfNewPrecisionSum;
 | 
			
		||||
        $anotherNewPrecision = 1.0/$anotherInverseOfNewPrecisionSum;
 | 
			
		||||
        $newPrecision = 1.0 / $inverseOfNewPrecisionSum;
 | 
			
		||||
        $anotherNewPrecision = 1.0 / $anotherInverseOfNewPrecisionSum;
 | 
			
		||||
 | 
			
		||||
        $newPrecisionMean = $newPrecision*$weightedMeanSum;
 | 
			
		||||
        $anotherNewPrecisionMean = $anotherNewPrecision*$anotherWeightedMeanSum;
 | 
			
		||||
        $newPrecisionMean = $newPrecision * $weightedMeanSum;
 | 
			
		||||
        $anotherNewPrecisionMean = $anotherNewPrecision * $anotherWeightedMeanSum;
 | 
			
		||||
 | 
			
		||||
        $newMessage = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
 | 
			
		||||
        $oldMarginalWithoutMessage = GaussianDistribution::divide($marginal0, $message0);
 | 
			
		||||
@@ -201,7 +184,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function updateMessageIndex($messageIndex)
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        $allMessages = &$this->getMessages();
 | 
			
		||||
        $allVariables = &$this->getVariables();
 | 
			
		||||
 | 
			
		||||
@@ -216,16 +199,15 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
        // order as the weights. Thankfully, the weights and messages share the same index numbers,
 | 
			
		||||
        // so we just need to make sure they're consistent
 | 
			
		||||
        $allMessagesCount = count($allMessages);
 | 
			
		||||
        for ($i = 0; $i < $allMessagesCount; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $allMessagesCount; $i++) {
 | 
			
		||||
            $updatedMessages[] = &$allMessages[$indicesToUse[$i]];
 | 
			
		||||
            $updatedVariables[] = &$allVariables[$indicesToUse[$i]];
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        return $this->updateHelper($this->_weights[$messageIndex],
 | 
			
		||||
                                   $this->_weightsSquared[$messageIndex],
 | 
			
		||||
                                   $updatedMessages,
 | 
			
		||||
                                   $updatedVariables);
 | 
			
		||||
            $this->_weightsSquared[$messageIndex],
 | 
			
		||||
            $updatedMessages,
 | 
			
		||||
            $updatedVariables);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static function createName($sumVariable, $variablesToSum, $weights)
 | 
			
		||||
@@ -233,14 +215,12 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
        // TODO: Perf? Use PHP equivalent of StringBuilder? implode on arrays?
 | 
			
		||||
        $result = (string)$sumVariable;
 | 
			
		||||
        $result .= ' = ';
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        $totalVars = count($variablesToSum);
 | 
			
		||||
        for($i = 0; $i < $totalVars; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $totalVars; $i++) {
 | 
			
		||||
            $isFirst = ($i == 0);
 | 
			
		||||
            
 | 
			
		||||
            if($isFirst && ($weights[$i] < 0))
 | 
			
		||||
            {
 | 
			
		||||
 | 
			
		||||
            if ($isFirst && ($weights[$i] < 0)) {
 | 
			
		||||
                $result .= '-';
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -249,24 +229,18 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
            $result .= "*[";
 | 
			
		||||
            $result .= (string)$variablesToSum[$i];
 | 
			
		||||
            $result .= ']';
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            $isLast = ($i == ($totalVars - 1));
 | 
			
		||||
            
 | 
			
		||||
            if(!$isLast)
 | 
			
		||||
            {
 | 
			
		||||
                if($weights[$i + 1] >= 0)
 | 
			
		||||
                {
 | 
			
		||||
 | 
			
		||||
            if (!$isLast) {
 | 
			
		||||
                if ($weights[$i + 1] >= 0) {
 | 
			
		||||
                    $result .= ' + ';
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                } else {
 | 
			
		||||
                    $result .= ' - ';
 | 
			
		||||
                }
 | 
			
		||||
            }                
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,13 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TruncatedGaussianCorrectionFunctions.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/GaussianFactor.php");
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Message;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
@@ -38,9 +31,9 @@ class GaussianWithinFactor extends GaussianFactor
 | 
			
		||||
        $messageFromVariable = GaussianDistribution::divide($marginal, $message);
 | 
			
		||||
        $mean = $messageFromVariable->getMean();
 | 
			
		||||
        $std = $messageFromVariable->getStandardDeviation();
 | 
			
		||||
        $z = GaussianDistribution::cumulativeTo(($this->_epsilon - $mean)/$std)
 | 
			
		||||
             -
 | 
			
		||||
             GaussianDistribution::cumulativeTo((-$this->_epsilon - $mean)/$std);
 | 
			
		||||
        $z = GaussianDistribution::cumulativeTo(($this->_epsilon - $mean) / $std)
 | 
			
		||||
            -
 | 
			
		||||
            GaussianDistribution::cumulativeTo((-$this->_epsilon - $mean) / $std);
 | 
			
		||||
 | 
			
		||||
        return -GaussianDistribution::logProductNormalization($messageFromVariable, $message) + log($z);
 | 
			
		||||
    }
 | 
			
		||||
@@ -55,22 +48,22 @@ class GaussianWithinFactor extends GaussianFactor
 | 
			
		||||
        $d = $messageFromVariable->getPrecisionMean();
 | 
			
		||||
 | 
			
		||||
        $sqrtC = sqrt($c);
 | 
			
		||||
        $dOnSqrtC = $d/$sqrtC;
 | 
			
		||||
        $dOnSqrtC = $d / $sqrtC;
 | 
			
		||||
 | 
			
		||||
        $epsilonTimesSqrtC = $this->_epsilon*$sqrtC;
 | 
			
		||||
        $epsilonTimesSqrtC = $this->_epsilon * $sqrtC;
 | 
			
		||||
        $d = $messageFromVariable->getPrecisionMean();
 | 
			
		||||
 | 
			
		||||
        $denominator = 1.0 - TruncatedGaussianCorrectionFunctions::wWithinMargin($dOnSqrtC, $epsilonTimesSqrtC);
 | 
			
		||||
        $newPrecision = $c/$denominator;
 | 
			
		||||
        $newPrecision = $c / $denominator;
 | 
			
		||||
        $newPrecisionMean = ($d +
 | 
			
		||||
                                   $sqrtC*
 | 
			
		||||
                                   TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC))/
 | 
			
		||||
                                  $denominator;
 | 
			
		||||
                $sqrtC *
 | 
			
		||||
                TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)) /
 | 
			
		||||
            $denominator;
 | 
			
		||||
 | 
			
		||||
        $newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
 | 
			
		||||
        $newMessage = GaussianDistribution::divide(
 | 
			
		||||
                        GaussianDistribution::multiply($oldMessage, $newMarginal),
 | 
			
		||||
                        $oldMarginal);
 | 
			
		||||
            GaussianDistribution::multiply($oldMessage, $newMarginal),
 | 
			
		||||
            $oldMarginal);
 | 
			
		||||
 | 
			
		||||
        // Update the message and marginal
 | 
			
		||||
        $message->setValue($newMessage);
 | 
			
		||||
@@ -79,6 +72,4 @@ class GaussianWithinFactor extends GaussianFactor
 | 
			
		||||
        // Return the difference in the new marginal
 | 
			
		||||
        return GaussianDistribution::subtract($newMarginal, $oldMarginal);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TeamPerformancesToTeamPerformanceDifferencesLayer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TeamDifferencesComparisonLayer.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleLoop;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleSequence;
 | 
			
		||||
@@ -30,8 +23,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
    public function &getLocalFactors()
 | 
			
		||||
    {
 | 
			
		||||
        $localFactors =
 | 
			
		||||
            \array_merge($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
 | 
			
		||||
                         $this->_TeamDifferencesComparisonLayer->getLocalFactors());
 | 
			
		||||
            array_merge($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
 | 
			
		||||
                $this->_TeamDifferencesComparisonLayer->getLocalFactors());
 | 
			
		||||
        return $localFactors;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -47,9 +40,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createPriorSchedule()
 | 
			
		||||
    {        
 | 
			
		||||
        switch (count($this->getInputVariablesGroups()))
 | 
			
		||||
        {
 | 
			
		||||
    {
 | 
			
		||||
        switch (count($this->getInputVariablesGroups())) {
 | 
			
		||||
            case 0:
 | 
			
		||||
            case 1:
 | 
			
		||||
                throw new InvalidOperationException();
 | 
			
		||||
@@ -72,15 +64,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        $innerSchedule = new ScheduleSequence(
 | 
			
		||||
            "inner schedule",
 | 
			
		||||
            array(
 | 
			
		||||
                    $loop,
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        "teamPerformanceToPerformanceDifferenceFactors[0] @ 1",
 | 
			
		||||
                        $firstDifferencesFactor, 1),
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        sprintf("teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2", $totalTeamDifferences),
 | 
			
		||||
                        $lastDifferencesFactor, 2)
 | 
			
		||||
                )
 | 
			
		||||
            );
 | 
			
		||||
                $loop,
 | 
			
		||||
                new ScheduleStep(
 | 
			
		||||
                    "teamPerformanceToPerformanceDifferenceFactors[0] @ 1",
 | 
			
		||||
                    $firstDifferencesFactor, 1),
 | 
			
		||||
                new ScheduleStep(
 | 
			
		||||
                    sprintf("teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2", $totalTeamDifferences),
 | 
			
		||||
                    $lastDifferencesFactor, 2)
 | 
			
		||||
            )
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return $innerSchedule;
 | 
			
		||||
    }
 | 
			
		||||
@@ -93,15 +85,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        $firstPerfToTeamDiff = &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0];
 | 
			
		||||
        $firstTeamDiffComparison = &$teamDifferencesComparisonLayerLocalFactors[0];
 | 
			
		||||
        $itemsToSequence = array(
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        "send team perf to perf differences",
 | 
			
		||||
                        $firstPerfToTeamDiff,
 | 
			
		||||
                        0),
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        "send to greater than or within factor",
 | 
			
		||||
                        $firstTeamDiffComparison,
 | 
			
		||||
                        0)
 | 
			
		||||
                );
 | 
			
		||||
            new ScheduleStep(
 | 
			
		||||
                "send team perf to perf differences",
 | 
			
		||||
                $firstPerfToTeamDiff,
 | 
			
		||||
                0),
 | 
			
		||||
            new ScheduleStep(
 | 
			
		||||
                "send to greater than or within factor",
 | 
			
		||||
                $firstTeamDiffComparison,
 | 
			
		||||
                0)
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return $this->scheduleSequence(
 | 
			
		||||
            $itemsToSequence,
 | 
			
		||||
@@ -114,8 +106,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
 | 
			
		||||
        $forwardScheduleList = array();
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $totalTeamDifferences - 1; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $totalTeamDifferences - 1; $i++) {
 | 
			
		||||
            $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
 | 
			
		||||
            $teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors();
 | 
			
		||||
 | 
			
		||||
@@ -125,16 +116,16 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
            $currentForwardSchedulePiece =
 | 
			
		||||
                $this->scheduleSequence(
 | 
			
		||||
                    array(
 | 
			
		||||
                            new ScheduleStep(
 | 
			
		||||
                                sprintf("team perf to perf diff %d", $i),
 | 
			
		||||
                                $currentTeamPerfToTeamPerfDiff, 0),
 | 
			
		||||
                            new ScheduleStep(
 | 
			
		||||
                                sprintf("greater than or within result factor %d", $i),
 | 
			
		||||
                                $currentTeamDiffComparison, 0),
 | 
			
		||||
                            new ScheduleStep(
 | 
			
		||||
                                sprintf("team perf to perf diff factors [%d], 2", $i),
 | 
			
		||||
                                $currentTeamPerfToTeamPerfDiff, 2)
 | 
			
		||||
                        ), sprintf("current forward schedule piece %d", $i));
 | 
			
		||||
                        new ScheduleStep(
 | 
			
		||||
                            sprintf("team perf to perf diff %d", $i),
 | 
			
		||||
                            $currentTeamPerfToTeamPerfDiff, 0),
 | 
			
		||||
                        new ScheduleStep(
 | 
			
		||||
                            sprintf("greater than or within result factor %d", $i),
 | 
			
		||||
                            $currentTeamDiffComparison, 0),
 | 
			
		||||
                        new ScheduleStep(
 | 
			
		||||
                            sprintf("team perf to perf diff factors [%d], 2", $i),
 | 
			
		||||
                            $currentTeamPerfToTeamPerfDiff, 2)
 | 
			
		||||
                    ), sprintf("current forward schedule piece %d", $i));
 | 
			
		||||
 | 
			
		||||
            $forwardScheduleList[] = $currentForwardSchedulePiece;
 | 
			
		||||
        }
 | 
			
		||||
@@ -143,8 +134,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
 | 
			
		||||
        $backwardScheduleList = array();
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $totalTeamDifferences - 1; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $totalTeamDifferences - 1; $i++) {
 | 
			
		||||
            $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
 | 
			
		||||
            $teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors();
 | 
			
		||||
 | 
			
		||||
@@ -155,15 +145,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
            $currentBackwardSchedulePiece = new ScheduleSequence(
 | 
			
		||||
                "current backward schedule piece",
 | 
			
		||||
                array(
 | 
			
		||||
                        new ScheduleStep(
 | 
			
		||||
                            sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
 | 
			
		||||
                            $differencesFactor, 0),
 | 
			
		||||
                        new ScheduleStep(
 | 
			
		||||
                            sprintf("greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
 | 
			
		||||
                            $comparisonFactor, 0),
 | 
			
		||||
                        new ScheduleStep(
 | 
			
		||||
                            sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1", $i),
 | 
			
		||||
                            $performancesToDifferencesFactor, 1)
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
 | 
			
		||||
                        $differencesFactor, 0),
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        sprintf("greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
 | 
			
		||||
                        $comparisonFactor, 0),
 | 
			
		||||
                    new ScheduleStep(
 | 
			
		||||
                        sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1", $i),
 | 
			
		||||
                        $performancesToDifferencesFactor, 1)
 | 
			
		||||
                ));
 | 
			
		||||
            $backwardScheduleList[] = $currentBackwardSchedulePiece;
 | 
			
		||||
        }
 | 
			
		||||
@@ -184,6 +174,4 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
 | 
			
		||||
        return $loop;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,13 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../PartialPlay.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Factors/GaussianWeightedSumFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TeamPerformancesToTeamPerformanceDifferencesLayer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TeamDifferencesComparisonLayer.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\PartialPlay;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleLoop;
 | 
			
		||||
@@ -26,18 +17,17 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
 | 
			
		||||
    public function buildLayer()
 | 
			
		||||
    {
 | 
			
		||||
        $inputVariablesGroups = &$this->getInputVariablesGroups();
 | 
			
		||||
        foreach ($inputVariablesGroups as &$currentTeam)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($inputVariablesGroups as &$currentTeam) {
 | 
			
		||||
            $localCurrentTeam = &$currentTeam;
 | 
			
		||||
            $teamPerformance = &$this->createOutputVariable($localCurrentTeam);
 | 
			
		||||
            $newSumFactor = $this->createPlayerToTeamSumFactor($localCurrentTeam, $teamPerformance);
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            $this->addLayerFactor($newSumFactor);
 | 
			
		||||
 | 
			
		||||
            // REVIEW: Does it make sense to have groups of one?
 | 
			
		||||
            $outputVariablesGroups = &$this->getOutputVariablesGroups();
 | 
			
		||||
            $outputVariablesGroups[] = array($teamPerformance);
 | 
			
		||||
        }        
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createPriorSchedule()
 | 
			
		||||
@@ -45,45 +35,41 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
 | 
			
		||||
        $localFactors = &$this->getLocalFactors();
 | 
			
		||||
 | 
			
		||||
        $sequence = &$this->scheduleSequence(
 | 
			
		||||
                                            array_map(
 | 
			
		||||
                                                    function($weightedSumFactor)
 | 
			
		||||
                                                    {
 | 
			
		||||
                                                        return new ScheduleStep("Perf to Team Perf Step", $weightedSumFactor, 0);
 | 
			
		||||
                                                    },
 | 
			
		||||
                                                    $localFactors),
 | 
			
		||||
                                            "all player perf to team perf schedule");
 | 
			
		||||
            array_map(
 | 
			
		||||
                function ($weightedSumFactor) {
 | 
			
		||||
                    return new ScheduleStep("Perf to Team Perf Step", $weightedSumFactor, 0);
 | 
			
		||||
                },
 | 
			
		||||
                $localFactors),
 | 
			
		||||
            "all player perf to team perf schedule");
 | 
			
		||||
        return $sequence;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function createPlayerToTeamSumFactor(&$teamMembers, &$sumVariable)
 | 
			
		||||
    {
 | 
			
		||||
        $weights = array_map(
 | 
			
		||||
                        function($v)
 | 
			
		||||
                        {
 | 
			
		||||
                            $player = &$v->getKey();
 | 
			
		||||
                            return PartialPlay::getPartialPlayPercentage($player);
 | 
			
		||||
                        },
 | 
			
		||||
                        $teamMembers);
 | 
			
		||||
            function ($v) {
 | 
			
		||||
                $player = &$v->getKey();
 | 
			
		||||
                return PartialPlay::getPartialPlayPercentage($player);
 | 
			
		||||
            },
 | 
			
		||||
            $teamMembers);
 | 
			
		||||
 | 
			
		||||
        return new GaussianWeightedSumFactor(
 | 
			
		||||
                $sumVariable,
 | 
			
		||||
                $teamMembers,
 | 
			
		||||
                $weights);
 | 
			
		||||
                                                 
 | 
			
		||||
            $sumVariable,
 | 
			
		||||
            $teamMembers,
 | 
			
		||||
            $weights);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createPosteriorSchedule()
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        $allFactors = array();
 | 
			
		||||
        $localFactors = &$this->getLocalFactors();
 | 
			
		||||
        foreach($localFactors as &$currentFactor)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($localFactors as &$currentFactor) {
 | 
			
		||||
            $localCurrentFactor = &$currentFactor;
 | 
			
		||||
            $numberOfMessages = $localCurrentFactor->getNumberOfMessages();
 | 
			
		||||
            for($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++)
 | 
			
		||||
            {
 | 
			
		||||
            for ($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++) {
 | 
			
		||||
                $allFactors[] = new ScheduleStep("team sum perf @" . $currentIteration,
 | 
			
		||||
                                                 $localCurrentFactor, $currentIteration);
 | 
			
		||||
                    $localCurrentFactor, $currentIteration);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return $this->scheduleSequence($allFactors, "all of the team's sum iterations");
 | 
			
		||||
@@ -91,16 +77,13 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
 | 
			
		||||
 | 
			
		||||
    private function &createOutputVariable(&$team)
 | 
			
		||||
    {
 | 
			
		||||
        $memberNames = \array_map(function ($currentPlayer)
 | 
			
		||||
                                  {
 | 
			
		||||
                                        return (string)($currentPlayer->getKey());
 | 
			
		||||
                                  },
 | 
			
		||||
                                  $team);
 | 
			
		||||
        $memberNames = \array_map(function ($currentPlayer) {
 | 
			
		||||
            return (string)($currentPlayer->getKey());
 | 
			
		||||
        },
 | 
			
		||||
            $team);
 | 
			
		||||
 | 
			
		||||
        $teamMemberNames = \join(", ", $memberNames);
 | 
			
		||||
        $outputVariable = &$this->getParentFactorGraph()->getVariableFactory()->createBasicVariable("Team[" . $teamMemberNames . "]'s performance");
 | 
			
		||||
        return $outputVariable;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,20 +1,9 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Rating.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/BasicMath.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Factors/GaussianPriorFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\Numerics\BasicMatch;
 | 
			
		||||
use Moserware\Skills\Rating;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleLoop;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleSequence;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleStep;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
 | 
			
		||||
use Moserware\Skills\TrueSkill\Factors\GaussianPriorFactor;
 | 
			
		||||
 | 
			
		||||
@@ -33,14 +22,12 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
    public function buildLayer()
 | 
			
		||||
    {
 | 
			
		||||
        $teams = &$this->_teams;
 | 
			
		||||
        foreach ($teams as &$currentTeam)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($teams as &$currentTeam) {
 | 
			
		||||
            $localCurrentTeam = &$currentTeam;
 | 
			
		||||
            $currentTeamSkills = array();
 | 
			
		||||
 | 
			
		||||
            $currentTeamAllPlayers = $localCurrentTeam->getAllPlayers();
 | 
			
		||||
            foreach ($currentTeamAllPlayers as &$currentTeamPlayer)
 | 
			
		||||
            {
 | 
			
		||||
            foreach ($currentTeamAllPlayers as &$currentTeamPlayer) {
 | 
			
		||||
                $localCurrentTeamPlayer = &$currentTeamPlayer;
 | 
			
		||||
                $currentTeamPlayerRating = $currentTeam->getRating($localCurrentTeamPlayer);
 | 
			
		||||
                $playerSkill = &$this->createSkillOutputVariable($localCurrentTeamPlayer);
 | 
			
		||||
@@ -58,21 +45,22 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
    {
 | 
			
		||||
        $localFactors = &$this->getLocalFactors();
 | 
			
		||||
        return $this->scheduleSequence(
 | 
			
		||||
                array_map(
 | 
			
		||||
                        function(&$prior)
 | 
			
		||||
                        {
 | 
			
		||||
                            return new ScheduleStep("Prior to Skill Step", $prior, 0);
 | 
			
		||||
                        },
 | 
			
		||||
                        $localFactors),
 | 
			
		||||
                 "All priors");
 | 
			
		||||
            array_map(
 | 
			
		||||
                function (&$prior) {
 | 
			
		||||
                    return new ScheduleStep("Prior to Skill Step", $prior, 0);
 | 
			
		||||
                },
 | 
			
		||||
                $localFactors),
 | 
			
		||||
            "All priors");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function createPriorFactor(&$player, Rating &$priorRating, Variable &$skillsVariable)
 | 
			
		||||
    {
 | 
			
		||||
        return new GaussianPriorFactor($priorRating->getMean(),
 | 
			
		||||
                                       square($priorRating->getStandardDeviation()) +
 | 
			
		||||
                                       square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
 | 
			
		||||
                                       $skillsVariable);
 | 
			
		||||
        return new GaussianPriorFactor(
 | 
			
		||||
            $priorRating->getMean(),
 | 
			
		||||
            BasicMatch::square($priorRating->getStandardDeviation()) +
 | 
			
		||||
            BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
 | 
			
		||||
            $skillsVariable
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function &createSkillOutputVariable(&$key)
 | 
			
		||||
@@ -82,6 +70,4 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        $skillOutputVariable = &$variableFactory->createKeyedVariable($key, $key . "'s skill");
 | 
			
		||||
        return $skillOutputVariable;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,15 +1,8 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../Numerics/BasicMath.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Factors/GaussianLikelihoodFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\FactorGraphs\ScheduleStep;
 | 
			
		||||
use Moserware\Skills\FactorGraphs\KeyedVariable;
 | 
			
		||||
use Moserware\Skills\Numerics\BasicMatch;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
 | 
			
		||||
use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
 | 
			
		||||
 | 
			
		||||
@@ -25,27 +18,29 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        $inputVariablesGroups = &$this->getInputVariablesGroups();
 | 
			
		||||
        $outputVariablesGroups = &$this->getOutputVariablesGroups();
 | 
			
		||||
 | 
			
		||||
        foreach ($inputVariablesGroups as &$currentTeam)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($inputVariablesGroups as &$currentTeam) {
 | 
			
		||||
            $currentTeamPlayerPerformances = array();
 | 
			
		||||
 | 
			
		||||
            foreach ($currentTeam as &$playerSkillVariable)
 | 
			
		||||
            {
 | 
			
		||||
            foreach ($currentTeam as &$playerSkillVariable) {
 | 
			
		||||
                $localPlayerSkillVariable = &$playerSkillVariable;
 | 
			
		||||
                $currentPlayer = &$localPlayerSkillVariable->getKey();
 | 
			
		||||
                $playerPerformance = &$this->createOutputVariable($currentPlayer);
 | 
			
		||||
                $newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance);
 | 
			
		||||
                $this->addLayerFactor($newLikelihoodFactor);
 | 
			
		||||
                $currentTeamPlayerPerformances[] = $playerPerformance;
 | 
			
		||||
            }            
 | 
			
		||||
            
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $outputVariablesGroups[] = $currentTeamPlayerPerformances;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance)
 | 
			
		||||
    {
 | 
			
		||||
        return new GaussianLikelihoodFactor(square($this->getParentFactorGraph()->getGameInfo()->getBeta()), $playerPerformance, $playerSkill);
 | 
			
		||||
        return new GaussianLikelihoodFactor(
 | 
			
		||||
            BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
 | 
			
		||||
            $playerPerformance,
 | 
			
		||||
            $playerSkill
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function &createOutputVariable(&$key)
 | 
			
		||||
@@ -58,27 +53,23 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
    {
 | 
			
		||||
        $localFactors = &$this->getLocalFactors();
 | 
			
		||||
        return $this->scheduleSequence(
 | 
			
		||||
                array_map(
 | 
			
		||||
                        function($likelihood)
 | 
			
		||||
                        {
 | 
			
		||||
                            return new ScheduleStep("Skill to Perf step", $likelihood, 0);
 | 
			
		||||
                        },
 | 
			
		||||
                        $localFactors),
 | 
			
		||||
                "All skill to performance sending");
 | 
			
		||||
            array_map(
 | 
			
		||||
                function ($likelihood) {
 | 
			
		||||
                    return new ScheduleStep("Skill to Perf step", $likelihood, 0);
 | 
			
		||||
                },
 | 
			
		||||
                $localFactors),
 | 
			
		||||
            "All skill to performance sending");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createPosteriorSchedule()
 | 
			
		||||
    {
 | 
			
		||||
        $localFactors = &$this->getLocalFactors();
 | 
			
		||||
        return $this->scheduleSequence(
 | 
			
		||||
                array_map(
 | 
			
		||||
                        function($likelihood)
 | 
			
		||||
                        {
 | 
			
		||||
                            return new ScheduleStep("name", $likelihood, 1);                    
 | 
			
		||||
                        },
 | 
			
		||||
                        $localFactors),
 | 
			
		||||
                "All skill to performance sending");
 | 
			
		||||
            array_map(
 | 
			
		||||
                function ($likelihood) {
 | 
			
		||||
                    return new ScheduleStep("name", $likelihood, 1);
 | 
			
		||||
                },
 | 
			
		||||
                $localFactors),
 | 
			
		||||
            "All skill to performance sending");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../DrawMargin.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Factors/GaussianGreaterThanFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Factors/GaussianWithinFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\TrueSkill\DrawMargin;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
 | 
			
		||||
@@ -30,8 +23,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        $inputVarGroups = &$this->getInputVariablesGroups();
 | 
			
		||||
        $inputVarGroupsCount = count($inputVarGroups);
 | 
			
		||||
 | 
			
		||||
        for ($i = 0; $i < $inputVarGroupsCount; $i++)
 | 
			
		||||
        {
 | 
			
		||||
        for ($i = 0; $i < $inputVarGroupsCount; $i++) {
 | 
			
		||||
            $isDraw = ($this->_teamRanks[$i] == $this->_teamRanks[$i + 1]);
 | 
			
		||||
            $teamDifference = &$inputVarGroups[$i][0];
 | 
			
		||||
 | 
			
		||||
@@ -43,6 +35,4 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
            $this->addLayerFactor($factor);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,13 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Factors/GaussianWeightedSumFactor.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\FactorGraphs\Variable;
 | 
			
		||||
use Moserware\Skills\TrueSkill\DrawMargin;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
 | 
			
		||||
use Moserware\Skills\TrueSkill\Factors\GaussianWeightedSumFactor;
 | 
			
		||||
 | 
			
		||||
@@ -51,6 +44,4 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG
 | 
			
		||||
        $outputVariable = &$this->getParentFactorGraph()->getVariableFactory()->createBasicVariable("Team performance difference");
 | 
			
		||||
        return $outputVariable;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../../FactorGraphs/FactorGraphLayer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\FactorGraphs\FactorGraphLayer;
 | 
			
		||||
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
 | 
			
		||||
@@ -13,6 +9,4 @@ abstract class TrueSkillFactorGraphLayer extends FactorGraphLayer
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($parentGraph);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,22 +1,5 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . '/../GameInfo.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../Rating.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../RatingContainer.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../FactorGraphs/FactorGraph.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../FactorGraphs/FactorList.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../FactorGraphs/Schedule.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../FactorGraphs/VariableFactory.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/../Numerics/GaussianDistribution.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Layers/IteratedTeamDifferencesInnerLayer.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Layers/PlayerPerformancesToTeamPerformancesLayer.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Layers/PlayerPriorValuesToSkillsLayer.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Layers/PlayerSkillsToPerformancesLayer.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Layers/TeamDifferencesComparisonLayer.php');
 | 
			
		||||
require_once(dirname(__FILE__) . '/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php');
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\GameInfo;
 | 
			
		||||
use Moserware\Skills\Rating;
 | 
			
		||||
use Moserware\Skills\RatingContainer;
 | 
			
		||||
@@ -35,28 +18,27 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
{
 | 
			
		||||
    private $_gameInfo;
 | 
			
		||||
    private $_layers;
 | 
			
		||||
    private $_priorLayer;    
 | 
			
		||||
    private $_priorLayer;
 | 
			
		||||
 | 
			
		||||
    public function __construct(GameInfo &$gameInfo, array &$teams, array $teamRanks)
 | 
			
		||||
    {
 | 
			
		||||
        $this->_priorLayer = new PlayerPriorValuesToSkillsLayer($this, $teams);
 | 
			
		||||
        $this->_gameInfo = $gameInfo;
 | 
			
		||||
        $newFactory = new VariableFactory(
 | 
			
		||||
                                        function()
 | 
			
		||||
                                        {
 | 
			
		||||
                                            return GaussianDistribution::fromPrecisionMean(0, 0);
 | 
			
		||||
                                        });
 | 
			
		||||
                                        
 | 
			
		||||
            function () {
 | 
			
		||||
                return GaussianDistribution::fromPrecisionMean(0, 0);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        $this->setVariableFactory($newFactory);
 | 
			
		||||
        $this->_layers = array(
 | 
			
		||||
                              $this->_priorLayer,
 | 
			
		||||
                              new PlayerSkillsToPerformancesLayer($this),
 | 
			
		||||
                              new PlayerPerformancesToTeamPerformancesLayer($this),
 | 
			
		||||
                              new IteratedTeamDifferencesInnerLayer(
 | 
			
		||||
                                  $this,
 | 
			
		||||
                                  new TeamPerformancesToTeamPerformanceDifferencesLayer($this),
 | 
			
		||||
                                  new TeamDifferencesComparisonLayer($this, $teamRanks))
 | 
			
		||||
                              );
 | 
			
		||||
            $this->_priorLayer,
 | 
			
		||||
            new PlayerSkillsToPerformancesLayer($this),
 | 
			
		||||
            new PlayerPerformancesToTeamPerformancesLayer($this),
 | 
			
		||||
            new IteratedTeamDifferencesInnerLayer(
 | 
			
		||||
                $this,
 | 
			
		||||
                new TeamPerformancesToTeamPerformanceDifferencesLayer($this),
 | 
			
		||||
                new TeamDifferencesComparisonLayer($this, $teamRanks))
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getGameInfo()
 | 
			
		||||
@@ -69,16 +51,14 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
        $lastOutput = null;
 | 
			
		||||
 | 
			
		||||
        $layers = &$this->_layers;
 | 
			
		||||
        foreach ($layers as &$currentLayer)
 | 
			
		||||
        {
 | 
			
		||||
            if ($lastOutput != null)
 | 
			
		||||
            {
 | 
			
		||||
        foreach ($layers as &$currentLayer) {
 | 
			
		||||
            if ($lastOutput != null) {
 | 
			
		||||
                $currentLayer->setInputVariablesGroups($lastOutput);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $currentLayer->buildLayer();
 | 
			
		||||
 | 
			
		||||
            $lastOutput = &$currentLayer->getOutputVariablesGroups();       
 | 
			
		||||
            $lastOutput = &$currentLayer->getOutputVariablesGroups();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -93,11 +73,9 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
        $factorList = new FactorList();
 | 
			
		||||
 | 
			
		||||
        $layers = &$this->_layers;
 | 
			
		||||
        foreach ($layers as &$currentLayer)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($layers as &$currentLayer) {
 | 
			
		||||
            $localFactors = &$currentLayer->getLocalFactors();
 | 
			
		||||
            foreach ($localFactors as &$currentFactor)
 | 
			
		||||
            {
 | 
			
		||||
            foreach ($localFactors as &$currentFactor) {
 | 
			
		||||
                $localCurrentFactor = &$currentFactor;
 | 
			
		||||
                $factorList->addFactor($localCurrentFactor);
 | 
			
		||||
            }
 | 
			
		||||
@@ -112,22 +90,18 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
        $fullSchedule = array();
 | 
			
		||||
 | 
			
		||||
        $layers = &$this->_layers;
 | 
			
		||||
        foreach ($layers as &$currentLayer)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($layers as &$currentLayer) {
 | 
			
		||||
            $currentPriorSchedule = $currentLayer->createPriorSchedule();
 | 
			
		||||
            if ($currentPriorSchedule != null)
 | 
			
		||||
            {
 | 
			
		||||
            if ($currentPriorSchedule != null) {
 | 
			
		||||
                $fullSchedule[] = $currentPriorSchedule;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        $allLayersReverse = \array_reverse($this->_layers);
 | 
			
		||||
 | 
			
		||||
        foreach ($allLayersReverse as &$currentLayer)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($allLayersReverse as &$currentLayer) {
 | 
			
		||||
            $currentPosteriorSchedule = $currentLayer->createPosteriorSchedule();
 | 
			
		||||
            if ($currentPosteriorSchedule != null)
 | 
			
		||||
            {
 | 
			
		||||
            if ($currentPosteriorSchedule != null) {
 | 
			
		||||
                $fullSchedule[] = $currentPosteriorSchedule;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@@ -140,13 +114,11 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
        $result = new RatingContainer();
 | 
			
		||||
 | 
			
		||||
        $priorLayerOutputVariablesGroups = &$this->_priorLayer->getOutputVariablesGroups();
 | 
			
		||||
        foreach ($priorLayerOutputVariablesGroups as &$currentTeam)
 | 
			
		||||
        {
 | 
			
		||||
            foreach ($currentTeam as &$currentPlayer)
 | 
			
		||||
            {
 | 
			
		||||
                $localCurrentPlayer = &$currentPlayer->getKey();                
 | 
			
		||||
        foreach ($priorLayerOutputVariablesGroups as &$currentTeam) {
 | 
			
		||||
            foreach ($currentTeam as &$currentPlayer) {
 | 
			
		||||
                $localCurrentPlayer = &$currentPlayer->getKey();
 | 
			
		||||
                $newRating = new Rating($currentPlayer->getValue()->getMean(),
 | 
			
		||||
                                        $currentPlayer->getValue()->getStandardDeviation());
 | 
			
		||||
                    $currentPlayer->getValue()->getStandardDeviation());
 | 
			
		||||
 | 
			
		||||
                $result->setRating($localCurrentPlayer, $newRating);
 | 
			
		||||
            }
 | 
			
		||||
@@ -154,6 +126,4 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . '/../Numerics/GaussianDistribution.php');
 | 
			
		||||
 | 
			
		||||
use Moserware\Numerics\GaussianDistribution;
 | 
			
		||||
use Moserware\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 | 
			
		||||
class TruncatedGaussianCorrectionFunctions
 | 
			
		||||
{
 | 
			
		||||
@@ -11,64 +8,64 @@ class TruncatedGaussianCorrectionFunctions
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The "V" function where the team performance difference is greater than the draw margin.
 | 
			
		||||
     * 
 | 
			
		||||
     *
 | 
			
		||||
     * In the reference F# implementation, this is referred to as "the additive
 | 
			
		||||
     * correction of a single-sided truncated Gaussian with unit variance."
 | 
			
		||||
     * 
 | 
			
		||||
     *
 | 
			
		||||
     * @param $teamPerformanceDifference
 | 
			
		||||
     * @param number $drawMargin In the paper, it's referred to as just "ε".
 | 
			
		||||
     * @param $c
 | 
			
		||||
     * @return float
 | 
			
		||||
     */
 | 
			
		||||
    public static function vExceedsMarginScaled($teamPerformanceDifference, $drawMargin, $c)
 | 
			
		||||
    {
 | 
			
		||||
        return self::vExceedsMargin($teamPerformanceDifference/$c, $drawMargin/$c);
 | 
			
		||||
        return self::vExceedsMargin($teamPerformanceDifference / $c, $drawMargin / $c);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function vExceedsMargin($teamPerformanceDifference, $drawMargin)
 | 
			
		||||
    {
 | 
			
		||||
        $denominator = GaussianDistribution::cumulativeTo($teamPerformanceDifference - $drawMargin);
 | 
			
		||||
 | 
			
		||||
        if ($denominator < 2.222758749e-162)
 | 
			
		||||
        {
 | 
			
		||||
        if ($denominator < 2.222758749e-162) {
 | 
			
		||||
            return -$teamPerformanceDifference + $drawMargin;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return GaussianDistribution::at($teamPerformanceDifference - $drawMargin)/$denominator;
 | 
			
		||||
        return GaussianDistribution::at($teamPerformanceDifference - $drawMargin) / $denominator;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The "W" function where the team performance difference is greater than the draw margin.
 | 
			
		||||
     * 
 | 
			
		||||
     *
 | 
			
		||||
     * In the reference F# implementation, this is referred to as "the multiplicative
 | 
			
		||||
     * correction of a single-sided truncated Gaussian with unit variance."
 | 
			
		||||
     */
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public static function wExceedsMarginScaled($teamPerformanceDifference, $drawMargin, $c)
 | 
			
		||||
    {
 | 
			
		||||
        return self::wExceedsMargin($teamPerformanceDifference/$c, $drawMargin/$c);
 | 
			
		||||
        return self::wExceedsMargin($teamPerformanceDifference / $c, $drawMargin / $c);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function wExceedsMargin($teamPerformanceDifference, $drawMargin)
 | 
			
		||||
    {
 | 
			
		||||
        $denominator = GaussianDistribution::cumulativeTo($teamPerformanceDifference - $drawMargin);
 | 
			
		||||
 | 
			
		||||
        if ($denominator < 2.222758749e-162)
 | 
			
		||||
        {
 | 
			
		||||
            if ($teamPerformanceDifference < 0.0)
 | 
			
		||||
            {
 | 
			
		||||
        if ($denominator < 2.222758749e-162) {
 | 
			
		||||
            if ($teamPerformanceDifference < 0.0) {
 | 
			
		||||
                return 1.0;
 | 
			
		||||
            }
 | 
			
		||||
            return 0.0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $vWin = self::vExceedsMargin($teamPerformanceDifference, $drawMargin);
 | 
			
		||||
        return $vWin*($vWin + $teamPerformanceDifference - $drawMargin);
 | 
			
		||||
        return $vWin * ($vWin + $teamPerformanceDifference - $drawMargin);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // the additive correction of a double-sided truncated Gaussian with unit variance
 | 
			
		||||
    public static function vWithinMarginScaled($teamPerformanceDifference, $drawMargin, $c)
 | 
			
		||||
    {
 | 
			
		||||
        return self::vWithinMargin($teamPerformanceDifference/$c, $drawMargin/$c);
 | 
			
		||||
        return self::vWithinMargin($teamPerformanceDifference / $c, $drawMargin / $c);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // from F#:
 | 
			
		||||
    public static function vWithinMargin($teamPerformanceDifference, $drawMargin)
 | 
			
		||||
    {
 | 
			
		||||
@@ -77,10 +74,8 @@ class TruncatedGaussianCorrectionFunctions
 | 
			
		||||
            GaussianDistribution::cumulativeTo($drawMargin - $teamPerformanceDifferenceAbsoluteValue) -
 | 
			
		||||
            GaussianDistribution::cumulativeTo(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue);
 | 
			
		||||
 | 
			
		||||
        if ($denominator < 2.222758749e-162)
 | 
			
		||||
        {
 | 
			
		||||
            if ($teamPerformanceDifference < 0.0)
 | 
			
		||||
            {
 | 
			
		||||
        if ($denominator < 2.222758749e-162) {
 | 
			
		||||
            if ($teamPerformanceDifference < 0.0) {
 | 
			
		||||
                return -$teamPerformanceDifference - $drawMargin;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@@ -88,20 +83,19 @@ class TruncatedGaussianCorrectionFunctions
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $numerator = GaussianDistribution::at(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue) -
 | 
			
		||||
                     GaussianDistribution::at($drawMargin - $teamPerformanceDifferenceAbsoluteValue);
 | 
			
		||||
            GaussianDistribution::at($drawMargin - $teamPerformanceDifferenceAbsoluteValue);
 | 
			
		||||
 | 
			
		||||
        if ($teamPerformanceDifference < 0.0)
 | 
			
		||||
        {
 | 
			
		||||
            return -$numerator/$denominator;
 | 
			
		||||
        if ($teamPerformanceDifference < 0.0) {
 | 
			
		||||
            return -$numerator / $denominator;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $numerator/$denominator;
 | 
			
		||||
        return $numerator / $denominator;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // the multiplicative correction of a double-sided truncated Gaussian with unit variance
 | 
			
		||||
    public static function wWithinMarginScaled($teamPerformanceDifference, $drawMargin, $c)
 | 
			
		||||
    {
 | 
			
		||||
        return self::wWithinMargin($teamPerformanceDifference/$c, $drawMargin/$c);
 | 
			
		||||
        return self::wWithinMargin($teamPerformanceDifference / $c, $drawMargin / $c);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // From F#:
 | 
			
		||||
@@ -109,25 +103,23 @@ class TruncatedGaussianCorrectionFunctions
 | 
			
		||||
    {
 | 
			
		||||
        $teamPerformanceDifferenceAbsoluteValue = abs($teamPerformanceDifference);
 | 
			
		||||
        $denominator = GaussianDistribution::cumulativeTo($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
                       -
 | 
			
		||||
                       GaussianDistribution::cumulativeTo(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue);
 | 
			
		||||
            -
 | 
			
		||||
            GaussianDistribution::cumulativeTo(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue);
 | 
			
		||||
 | 
			
		||||
        if ($denominator < 2.222758749e-162)
 | 
			
		||||
        {
 | 
			
		||||
        if ($denominator < 2.222758749e-162) {
 | 
			
		||||
            return 1.0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $vt = self::vWithinMargin($teamPerformanceDifferenceAbsoluteValue, $drawMargin);
 | 
			
		||||
 | 
			
		||||
        return $vt*$vt +
 | 
			
		||||
               (
 | 
			
		||||
                   ($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
                   *
 | 
			
		||||
                   GaussianDistribution::at(
 | 
			
		||||
                       $drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
                   - (-$drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
                     *
 | 
			
		||||
                     GaussianDistribution::at(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue))/$denominator;
 | 
			
		||||
        return $vt * $vt +
 | 
			
		||||
        (
 | 
			
		||||
            ($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
            *
 | 
			
		||||
            GaussianDistribution::at(
 | 
			
		||||
                $drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
            - (-$drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
            *
 | 
			
		||||
            GaussianDistribution::at(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue)) / $denominator;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,25 +1,8 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../GameInfo.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Guard.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PairwiseComparison.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../RankSorter.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Rating.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../RatingContainer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../SkillCalculator.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PlayersRange.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TeamsRange.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Numerics/BasicMath.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/DrawMargin.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TruncatedGaussianCorrectionFunctions.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\GameInfo;
 | 
			
		||||
use Moserware\Skills\Guard;
 | 
			
		||||
use Moserware\Skills\Numerics\BasicMatch;
 | 
			
		||||
use Moserware\Skills\PairwiseComparison;
 | 
			
		||||
use Moserware\Skills\RankSorter;
 | 
			
		||||
use Moserware\Skills\Rating;
 | 
			
		||||
@@ -36,7 +19,6 @@ use Moserware\Skills\TeamsRange;
 | 
			
		||||
 * When you only have two players, a lot of the math simplifies. The main purpose of this class
 | 
			
		||||
 * is to show the bare minimum of what a TrueSkill implementation should have.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
class TwoPlayerTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
{
 | 
			
		||||
    public function __construct()
 | 
			
		||||
@@ -86,16 +68,18 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
 | 
			
		||||
    private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison)
 | 
			
		||||
    {
 | 
			
		||||
        $drawMargin = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(),
 | 
			
		||||
                                                                   $gameInfo->getBeta());
 | 
			
		||||
        $drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
 | 
			
		||||
            $gameInfo->getDrawProbability(),
 | 
			
		||||
            $gameInfo->getBeta()
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $c =
 | 
			
		||||
            sqrt(
 | 
			
		||||
                square($selfRating->getStandardDeviation())
 | 
			
		||||
                +
 | 
			
		||||
                square($opponentRating->getStandardDeviation())
 | 
			
		||||
                +
 | 
			
		||||
                2*square($gameInfo->getBeta()));
 | 
			
		||||
        $c = sqrt(
 | 
			
		||||
            BasicMatch::square($selfRating->getStandardDeviation())
 | 
			
		||||
            +
 | 
			
		||||
            BasicMatch::square($opponentRating->getStandardDeviation())
 | 
			
		||||
            +
 | 
			
		||||
            2 * BasicMatch::square($gameInfo->getBeta())
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $winningMean = $selfRating->getMean();
 | 
			
		||||
        $losingMean = $opponentRating->getMean();
 | 
			
		||||
@@ -128,10 +112,10 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
            $rankMultiplier = 1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $meanMultiplier = (square($selfRating->getStandardDeviation()) + square($gameInfo->getDynamicsFactor()))/$c;
 | 
			
		||||
        $meanMultiplier = (BasicMatch::square($selfRating->getStandardDeviation()) + BasicMatch::square($gameInfo->getDynamicsFactor()))/$c;
 | 
			
		||||
 | 
			
		||||
        $varianceWithDynamics = square($selfRating->getStandardDeviation()) + square($gameInfo->getDynamicsFactor());
 | 
			
		||||
        $stdDevMultiplier = $varianceWithDynamics/square($c);
 | 
			
		||||
        $varianceWithDynamics = BasicMatch::square($selfRating->getStandardDeviation()) + BasicMatch::square($gameInfo->getDynamicsFactor());
 | 
			
		||||
        $stdDevMultiplier = $varianceWithDynamics/BasicMatch::square($c);
 | 
			
		||||
 | 
			
		||||
        $newMean = $selfRating->getMean() + ($rankMultiplier*$meanMultiplier*$v);
 | 
			
		||||
        $newStdDev = sqrt($varianceWithDynamics*(1 - $w*$stdDevMultiplier));
 | 
			
		||||
@@ -157,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 = square($gameInfo->getBeta());
 | 
			
		||||
        $player1SigmaSquared = square($player1Rating->getStandardDeviation());
 | 
			
		||||
        $player2SigmaSquared = square($player2Rating->getStandardDeviation());
 | 
			
		||||
        $betaSquared = BasicMatch::square($gameInfo->getBeta());
 | 
			
		||||
        $player1SigmaSquared = BasicMatch::square($player1Rating->getStandardDeviation());
 | 
			
		||||
        $player2SigmaSquared = BasicMatch::square($player2Rating->getStandardDeviation());
 | 
			
		||||
 | 
			
		||||
        // This is the square root part of the equation:
 | 
			
		||||
        $sqrtPart =
 | 
			
		||||
@@ -171,11 +155,10 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        // This is the exponent part of the equation:
 | 
			
		||||
        $expPart =
 | 
			
		||||
            exp(
 | 
			
		||||
                (-1*square($player1Rating->getMean() - $player2Rating->getMean()))
 | 
			
		||||
                (-1*BasicMatch::square($player1Rating->getMean() - $player2Rating->getMean()))
 | 
			
		||||
                /
 | 
			
		||||
                (2*(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared)));
 | 
			
		||||
 | 
			
		||||
        return $sqrtPart*$expPart;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
@@ -1,27 +1,8 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../GameInfo.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Guard.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PairwiseComparison.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../RankSorter.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Rating.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../RatingContainer.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../SkillCalculator.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Team.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../PlayersRange.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/../TeamsRange.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/../Numerics/BasicMath.php");
 | 
			
		||||
 | 
			
		||||
require_once(dirname(__FILE__) . "/DrawMargin.php");
 | 
			
		||||
require_once(dirname(__FILE__) . "/TruncatedGaussianCorrectionFunctions.php");
 | 
			
		||||
<?php namespace Moserware\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
use Moserware\Skills\GameInfo;
 | 
			
		||||
use Moserware\Skills\Guard;
 | 
			
		||||
use Moserware\Skills\Numerics\BasicMatch;
 | 
			
		||||
use Moserware\Skills\PairwiseComparison;
 | 
			
		||||
use Moserware\Skills\RankSorter;
 | 
			
		||||
use Moserware\Skills\Rating;
 | 
			
		||||
@@ -36,7 +17,7 @@ use Moserware\Skills\Team;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Calculates new ratings for only two teams where each team has 1 or more players.
 | 
			
		||||
 * 
 | 
			
		||||
 *
 | 
			
		||||
 * When you only have two teams, the math is still simple: no factor graphs are used yet.
 | 
			
		||||
 */
 | 
			
		||||
class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
@@ -63,16 +44,16 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        $results = new RatingContainer();
 | 
			
		||||
 | 
			
		||||
        self::updatePlayerRatings($gameInfo,
 | 
			
		||||
                                  $results,
 | 
			
		||||
                                  $team1,
 | 
			
		||||
                                  $team2,
 | 
			
		||||
                                  $wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::WIN);
 | 
			
		||||
            $results,
 | 
			
		||||
            $team1,
 | 
			
		||||
            $team2,
 | 
			
		||||
            $wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::WIN);
 | 
			
		||||
 | 
			
		||||
        self::updatePlayerRatings($gameInfo,
 | 
			
		||||
                                  $results,
 | 
			
		||||
                                  $team2,
 | 
			
		||||
                                  $team1,
 | 
			
		||||
                                  $wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::LOSE);
 | 
			
		||||
            $results,
 | 
			
		||||
            $team2,
 | 
			
		||||
            $team1,
 | 
			
		||||
            $wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::LOSE);
 | 
			
		||||
 | 
			
		||||
        return $results;
 | 
			
		||||
    }
 | 
			
		||||
@@ -83,41 +64,39 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
                                                Team $otherTeam,
 | 
			
		||||
                                                $selfToOtherTeamComparison)
 | 
			
		||||
    {
 | 
			
		||||
        $drawMargin = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(),
 | 
			
		||||
                                                                   $gameInfo->getBeta());
 | 
			
		||||
        $drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
 | 
			
		||||
            $gameInfo->getDrawProbability(),
 | 
			
		||||
            $gameInfo->getBeta()
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $betaSquared = square($gameInfo->getBeta());
 | 
			
		||||
        $tauSquared = square($gameInfo->getDynamicsFactor());
 | 
			
		||||
        $betaSquared = BasicMatch::square($gameInfo->getBeta());
 | 
			
		||||
        $tauSquared = BasicMatch::square($gameInfo->getDynamicsFactor());
 | 
			
		||||
 | 
			
		||||
        $totalPlayers = $selfTeam->count() + $otherTeam->count();
 | 
			
		||||
 | 
			
		||||
        $meanGetter =
 | 
			
		||||
            function($currentRating)
 | 
			
		||||
            {
 | 
			
		||||
                return $currentRating->getMean();
 | 
			
		||||
            };
 | 
			
		||||
        $meanGetter = function ($currentRating) {
 | 
			
		||||
            return $currentRating->getMean();
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        $selfMeanSum = sum($selfTeam->getAllRatings(), $meanGetter);
 | 
			
		||||
        $otherTeamMeanSum = sum($otherTeam->getAllRatings(), $meanGetter);
 | 
			
		||||
        $selfMeanSum = BasicMatch::sum($selfTeam->getAllRatings(), $meanGetter);
 | 
			
		||||
        $otherTeamMeanSum = BasicMatch::sum($otherTeam->getAllRatings(), $meanGetter);
 | 
			
		||||
 | 
			
		||||
        $varianceGetter =
 | 
			
		||||
            function($currentRating)
 | 
			
		||||
            {
 | 
			
		||||
                return square($currentRating->getStandardDeviation());
 | 
			
		||||
            };
 | 
			
		||||
        $varianceGetter = function ($currentRating) {
 | 
			
		||||
            return BasicMatch::square($currentRating->getStandardDeviation());
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        $c = sqrt(
 | 
			
		||||
                  sum($selfTeam->getAllRatings(), $varianceGetter)
 | 
			
		||||
                  +
 | 
			
		||||
                  sum($otherTeam->getAllRatings(), $varianceGetter)
 | 
			
		||||
                  +
 | 
			
		||||
                  $totalPlayers*$betaSquared);
 | 
			
		||||
            BasicMatch::sum($selfTeam->getAllRatings(), $varianceGetter)
 | 
			
		||||
            +
 | 
			
		||||
            BasicMatch::sum($otherTeam->getAllRatings(), $varianceGetter)
 | 
			
		||||
            +
 | 
			
		||||
            $totalPlayers * $betaSquared
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $winningMean = $selfMeanSum;
 | 
			
		||||
        $losingMean = $otherTeamMeanSum;
 | 
			
		||||
 | 
			
		||||
        switch ($selfToOtherTeamComparison)
 | 
			
		||||
        {
 | 
			
		||||
        switch ($selfToOtherTeamComparison) {
 | 
			
		||||
            case PairwiseComparison::WIN:
 | 
			
		||||
            case PairwiseComparison::DRAW:
 | 
			
		||||
                // NOP
 | 
			
		||||
@@ -130,15 +109,12 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
 | 
			
		||||
        $meanDelta = $winningMean - $losingMean;
 | 
			
		||||
 | 
			
		||||
        if ($selfToOtherTeamComparison != PairwiseComparison::DRAW)
 | 
			
		||||
        {
 | 
			
		||||
        if ($selfToOtherTeamComparison != PairwiseComparison::DRAW) {
 | 
			
		||||
            // non-draw case
 | 
			
		||||
            $v = TruncatedGaussianCorrectionFunctions::vExceedsMarginScaled($meanDelta, $drawMargin, $c);
 | 
			
		||||
            $w = TruncatedGaussianCorrectionFunctions::wExceedsMarginScaled($meanDelta, $drawMargin, $c);
 | 
			
		||||
            $rankMultiplier = (int) $selfToOtherTeamComparison;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            $rankMultiplier = (int)$selfToOtherTeamComparison;
 | 
			
		||||
        } else {
 | 
			
		||||
            // assume draw
 | 
			
		||||
            $v = TruncatedGaussianCorrectionFunctions::vWithinMarginScaled($meanDelta, $drawMargin, $c);
 | 
			
		||||
            $w = TruncatedGaussianCorrectionFunctions::wWithinMarginScaled($meanDelta, $drawMargin, $c);
 | 
			
		||||
@@ -146,19 +122,18 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $selfTeamAllPlayers = &$selfTeam->getAllPlayers();
 | 
			
		||||
        foreach ($selfTeamAllPlayers as &$selfTeamCurrentPlayer)
 | 
			
		||||
        {
 | 
			
		||||
        foreach ($selfTeamAllPlayers as &$selfTeamCurrentPlayer) {
 | 
			
		||||
            $localSelfTeamCurrentPlayer = &$selfTeamCurrentPlayer;
 | 
			
		||||
            $previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer);
 | 
			
		||||
 | 
			
		||||
            $meanMultiplier = (square($previousPlayerRating->getStandardDeviation()) + $tauSquared)/$c;
 | 
			
		||||
            $stdDevMultiplier = (square($previousPlayerRating->getStandardDeviation()) + $tauSquared)/square($c);
 | 
			
		||||
            $meanMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
 | 
			
		||||
            $stdDevMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMatch::square($c);
 | 
			
		||||
 | 
			
		||||
            $playerMeanDelta = ($rankMultiplier*$meanMultiplier*$v);
 | 
			
		||||
            $playerMeanDelta = ($rankMultiplier * $meanMultiplier * $v);
 | 
			
		||||
            $newMean = $previousPlayerRating->getMean() + $playerMeanDelta;
 | 
			
		||||
 | 
			
		||||
            $newStdDev =
 | 
			
		||||
                sqrt((square($previousPlayerRating->getStandardDeviation()) + $tauSquared)*(1 - $w*$stdDevMultiplier));
 | 
			
		||||
                sqrt((BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier));
 | 
			
		||||
 | 
			
		||||
            $newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev));
 | 
			
		||||
        }
 | 
			
		||||
@@ -182,45 +157,38 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
 | 
			
		||||
        $totalPlayers = $team1Count + $team2Count;
 | 
			
		||||
 | 
			
		||||
        $betaSquared = square($gameInfo->getBeta());
 | 
			
		||||
        $betaSquared = BasicMatch::square($gameInfo->getBeta());
 | 
			
		||||
 | 
			
		||||
        $meanGetter =
 | 
			
		||||
            function($currentRating)
 | 
			
		||||
            {
 | 
			
		||||
                return $currentRating->getMean();
 | 
			
		||||
            };
 | 
			
		||||
        $meanGetter = function ($currentRating) {
 | 
			
		||||
            return $currentRating->getMean();
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        $varianceGetter =
 | 
			
		||||
            function($currentRating)
 | 
			
		||||
            {
 | 
			
		||||
                return square($currentRating->getStandardDeviation());
 | 
			
		||||
            };
 | 
			
		||||
        $varianceGetter = function ($currentRating) {
 | 
			
		||||
            return BasicMatch::square($currentRating->getStandardDeviation());
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        $team1MeanSum = sum($team1Ratings, $meanGetter);
 | 
			
		||||
        $team1StdDevSquared = sum($team1Ratings, $varianceGetter);
 | 
			
		||||
        $team1MeanSum = BasicMatch::sum($team1Ratings, $meanGetter);
 | 
			
		||||
        $team1StdDevSquared = BasicMatch::sum($team1Ratings, $varianceGetter);
 | 
			
		||||
 | 
			
		||||
        $team2MeanSum = sum($team2Ratings, $meanGetter);
 | 
			
		||||
        $team2SigmaSquared = sum($team2Ratings, $varianceGetter);
 | 
			
		||||
        $team2MeanSum = BasicMatch::sum($team2Ratings, $meanGetter);
 | 
			
		||||
        $team2SigmaSquared = BasicMatch::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
 | 
			
		||||
        // the exponential part to make the code easier to read.
 | 
			
		||||
 | 
			
		||||
        $sqrtPart
 | 
			
		||||
            = sqrt(
 | 
			
		||||
                ($totalPlayers*$betaSquared)
 | 
			
		||||
                /
 | 
			
		||||
                ($totalPlayers*$betaSquared + $team1StdDevSquared + $team2SigmaSquared)
 | 
			
		||||
                );
 | 
			
		||||
        $sqrtPart = sqrt(
 | 
			
		||||
            ($totalPlayers * $betaSquared)
 | 
			
		||||
            /
 | 
			
		||||
            ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared)
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        $expPart
 | 
			
		||||
            = exp(
 | 
			
		||||
                (-1*square($team1MeanSum - $team2MeanSum))
 | 
			
		||||
                /
 | 
			
		||||
                (2*($totalPlayers*$betaSquared + $team1StdDevSquared + $team2SigmaSquared))
 | 
			
		||||
                );
 | 
			
		||||
        $expPart = exp(
 | 
			
		||||
            (-1 * BasicMatch::square($team1MeanSum - $team2MeanSum))
 | 
			
		||||
            /
 | 
			
		||||
            (2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared))
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        return $expPart*$sqrtPart;
 | 
			
		||||
        return $expPart * $sqrtPart;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
?>
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user