From a60187a3fd12dba5f1d993b299d673b5f45d46dd Mon Sep 17 00:00:00 2001 From: Jens True Date: Wed, 2 Aug 2023 09:36:44 +0000 Subject: [PATCH] More type work --- src/FactorGraphs/FactorList.php | 9 ++++++--- src/FactorGraphs/Schedule.php | 4 ++-- src/FactorGraphs/ScheduleLoop.php | 2 +- src/FactorGraphs/ScheduleSequence.php | 4 ++-- src/FactorGraphs/ScheduleStep.php | 4 ++-- src/Numerics/Matrix.php | 4 ++-- src/Numerics/Range.php | 6 +++--- src/Rating.php | 4 ++-- src/TrueSkill/Factors/GaussianLikelihoodFactor.php | 8 ++++---- src/TrueSkill/Factors/GaussianWithinFactor.php | 8 ++++---- src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php | 3 ++- src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php | 2 +- src/TrueSkill/TrueSkillFactorGraph.php | 9 ++++++--- src/TrueSkill/TwoPlayerTrueSkillCalculator.php | 2 +- 14 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/FactorGraphs/FactorList.php b/src/FactorGraphs/FactorList.php index 394584a..8fec3d2 100644 --- a/src/FactorGraphs/FactorList.php +++ b/src/FactorGraphs/FactorList.php @@ -7,9 +7,12 @@ namespace DNW\Skills\FactorGraphs; */ class FactorList { + /** + * @var Factor[] $list + */ private array $list = []; - public function getLogNormalization() + public function getLogNormalization(): float { $list = $this->list; foreach ($list as &$currentFactor) { @@ -39,12 +42,12 @@ class FactorList return $sumLogZ + $sumLogS; } - public function count() + public function count(): int { return count($this->list); } - public function addFactor(Factor $factor) + public function addFactor(Factor $factor): Factor { $this->list[] = $factor; diff --git a/src/FactorGraphs/Schedule.php b/src/FactorGraphs/Schedule.php index 865690f..70dbd3d 100644 --- a/src/FactorGraphs/Schedule.php +++ b/src/FactorGraphs/Schedule.php @@ -8,10 +8,10 @@ abstract class Schedule implements \Stringable { } - abstract public function visit(int $depth = -1, int $maxDepth = 0); + abstract public function visit(int $depth = -1, int $maxDepth = 0): float; public function __toString(): string { - return (string) $this->name; + return $this->name; } } diff --git a/src/FactorGraphs/ScheduleLoop.php b/src/FactorGraphs/ScheduleLoop.php index e206b63..5bc34c0 100644 --- a/src/FactorGraphs/ScheduleLoop.php +++ b/src/FactorGraphs/ScheduleLoop.php @@ -9,7 +9,7 @@ class ScheduleLoop extends Schedule parent::__construct($name); } - public function visit(int $depth = -1, int $maxDepth = 0) + public function visit(int $depth = -1, int $maxDepth = 0): float { $totalIterations = 1; $delta = $this->scheduleToLoop->visit($depth + 1, $maxDepth); diff --git a/src/FactorGraphs/ScheduleSequence.php b/src/FactorGraphs/ScheduleSequence.php index 1989868..e947acf 100644 --- a/src/FactorGraphs/ScheduleSequence.php +++ b/src/FactorGraphs/ScheduleSequence.php @@ -4,12 +4,12 @@ namespace DNW\Skills\FactorGraphs; class ScheduleSequence extends Schedule { - public function __construct($name, private readonly array $schedules) + public function __construct(string $name, private readonly array $schedules) { parent::__construct($name); } - public function visit($depth = -1, $maxDepth = 0) + public function visit(int $depth = -1, int $maxDepth = 0): float { $maxDelta = 0; diff --git a/src/FactorGraphs/ScheduleStep.php b/src/FactorGraphs/ScheduleStep.php index fed63f5..306adcd 100644 --- a/src/FactorGraphs/ScheduleStep.php +++ b/src/FactorGraphs/ScheduleStep.php @@ -4,12 +4,12 @@ namespace DNW\Skills\FactorGraphs; class ScheduleStep extends Schedule { - public function __construct($name, private readonly Factor $factor, private $index) + public function __construct(string $name, private readonly Factor $factor, private $index) { parent::__construct($name); } - public function visit(int $depth = -1, int $maxDepth = 0) + public function visit(int $depth = -1, int $maxDepth = 0): float { $currentFactor = $this->factor; diff --git a/src/Numerics/Matrix.php b/src/Numerics/Matrix.php index 7c59fcf..1d85b22 100644 --- a/src/Numerics/Matrix.php +++ b/src/Numerics/Matrix.php @@ -298,7 +298,7 @@ class Matrix return new Matrix($this->rowCount - 1, $this->columnCount - 1, $result); } - public function getCofactor($rowToRemove, $columnToRemove) + public function getCofactor(int $rowToRemove, int $columnToRemove): float { // See http://en.wikipedia.org/wiki/Cofactor_(linear_algebra) for details // REVIEW: should things be reversed since I'm 0 indexed? @@ -312,7 +312,7 @@ class Matrix } } - public function equals($otherMatrix) + public function equals(Matrix $otherMatrix): bool { // If one is null, but not both, return false. if ($otherMatrix == null) { diff --git a/src/Numerics/Range.php b/src/Numerics/Range.php index 7ad5ef4..f49add5 100644 --- a/src/Numerics/Range.php +++ b/src/Numerics/Range.php @@ -33,17 +33,17 @@ class Range // REVIEW: It's probably bad form to have access statics via a derived class, but the syntax looks better :-) - public static function inclusive(int $min, int $max): self + public static function inclusive(int $min, int $max): static { return static::create($min, $max); } - public static function exactly(int $value): self + public static function exactly(int $value): static { return static::create($value, $value); } - public static function atLeast(int $minimumValue): self + public static function atLeast(int $minimumValue): static { return static::create($minimumValue, PHP_INT_MAX); } diff --git a/src/Rating.php b/src/Rating.php index 512d8c8..cbfd146 100644 --- a/src/Rating.php +++ b/src/Rating.php @@ -44,10 +44,10 @@ class Rating implements \Stringable return $this->mean - $this->conservativeStandardDeviationMultiplier * $this->standardDeviation; } - public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage): Rating + public function getPartialUpdate(Rating $prior, Rating $fullPosterior, float $updatePercentage): Rating { $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 diff --git a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php index 1e6b490..ab88f49 100644 --- a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php +++ b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php @@ -15,9 +15,9 @@ use Exception; */ class GaussianLikelihoodFactor extends GaussianFactor { - private $precision; + private float $precision; - public function __construct($betaSquared, Variable $variable1, Variable $variable2) + public function __construct(float $betaSquared, Variable $variable1, Variable $variable2) { parent::__construct(sprintf('Likelihood of %s going to %s', $variable2, $variable1)); $this->precision = 1.0 / $betaSquared; @@ -42,7 +42,7 @@ class GaussianLikelihoodFactor extends GaussianFactor ); } - private function updateHelper(Message $message1, Message $message2, Variable $variable1, Variable $variable2) + private function updateHelper(Message $message1, Message $message2, Variable $variable1, Variable $variable2): float { $message1Value = clone $message1->getValue(); $message2Value = clone $message2->getValue(); @@ -70,7 +70,7 @@ class GaussianLikelihoodFactor extends GaussianFactor return GaussianDistribution::subtract($newMarginal, $marginal1); } - public function updateMessageIndex($messageIndex) + public function updateMessageIndex($messageIndex): float { $messages = $this->getMessages(); $vars = $this->getVariables(); diff --git a/src/TrueSkill/Factors/GaussianWithinFactor.php b/src/TrueSkill/Factors/GaussianWithinFactor.php index 46ce8f7..c8c786c 100644 --- a/src/TrueSkill/Factors/GaussianWithinFactor.php +++ b/src/TrueSkill/Factors/GaussianWithinFactor.php @@ -14,16 +14,16 @@ use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions; */ class GaussianWithinFactor extends GaussianFactor { - private $epsilon; + private float $epsilon; - public function __construct($epsilon, Variable $variable) + public function __construct(float $epsilon, Variable $variable) { parent::__construct(sprintf('%s <= %.2f', $variable, $epsilon)); $this->epsilon = $epsilon; $this->createVariableToMessageBinding($variable); } - public function getLogNormalization() + public function getLogNormalization(): float { /** * @var Variable[] $variables @@ -46,7 +46,7 @@ class GaussianWithinFactor extends GaussianFactor return -GaussianDistribution::logProductNormalization($messageFromVariable, $message) + log($z); } - protected function updateMessageVariable(Message $message, Variable $variable) + protected function updateMessageVariable(Message $message, Variable $variable): float { $oldMarginal = clone $variable->getValue(); $oldMessage = clone $message->getValue(); diff --git a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index 37c78ec..225e44d 100644 --- a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -4,6 +4,7 @@ namespace DNW\Skills\TrueSkill\Layers; use DNW\Skills\FactorGraphs\ScheduleStep; use DNW\Skills\FactorGraphs\Variable; +use DNW\Skills\FactorGraphs\KeyedVariable; use DNW\Skills\Numerics\BasicMath; use DNW\Skills\Rating; use DNW\Skills\TrueSkill\Factors\GaussianPriorFactor; @@ -63,7 +64,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer ); } - private function createSkillOutputVariable($key) + private function createSkillOutputVariable(mixed $key) : KeyedVariable { $parentFactorGraph = $this->getParentFactorGraph(); $variableFactory = $parentFactorGraph->getVariableFactory(); diff --git a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index 4819075..d6e7717 100644 --- a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -40,7 +40,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer ); } - private function createOutputVariable($key): KeyedVariable + private function createOutputVariable(mixed $key): KeyedVariable { return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key . "'s performance"); } diff --git a/src/TrueSkill/TrueSkillFactorGraph.php b/src/TrueSkill/TrueSkillFactorGraph.php index 4d70a9e..98c7da5 100644 --- a/src/TrueSkill/TrueSkillFactorGraph.php +++ b/src/TrueSkill/TrueSkillFactorGraph.php @@ -19,9 +19,12 @@ use DNW\Skills\TrueSkill\Layers\TeamPerformancesToTeamPerformanceDifferencesLaye class TrueSkillFactorGraph extends FactorGraph { - private $layers; + /** + * @var FactorGraphLayer[] $layers + */ + private array $layers; - private $priorLayer; + private PlayerPriorValuesToSkillsLayer $priorLayer; public function __construct(private readonly GameInfo $gameInfo, array $teams, array $teamRanks) { @@ -43,7 +46,7 @@ class TrueSkillFactorGraph extends FactorGraph ]; } - public function getGameInfo() + public function getGameInfo(): GameInfo { return $this->gameInfo; } diff --git a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php index e7ab5ca..40d223d 100644 --- a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php +++ b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php @@ -78,7 +78,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator return $results; } - private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison): Rating + private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, PairwiseComparison $comparison): Rating { $drawMargin = DrawMargin::getDrawMarginFromDrawProbability( $gameInfo->getDrawProbability(),