Pint applied for formatting

This commit is contained in:
Alex Wulf
2022-07-05 15:55:47 +02:00
parent bfc558d1f2
commit 7d4547df6a
68 changed files with 670 additions and 402 deletions

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\TrueSkill\Factors;
<?php
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Factor;
use DNW\Skills\FactorGraphs\Message;
@ -14,8 +16,9 @@ abstract class GaussianFactor extends Factor
/**
* Sends the factor-graph message with and returns the log-normalization constant.
* @param Message $message
* @param Variable $variable
*
* @param Message $message
* @param Variable $variable
* @return float|int
*/
protected function sendMessageVariable(Message $message, Variable $variable)
@ -24,6 +27,7 @@ abstract class GaussianFactor extends Factor
$messageValue = $message->getValue();
$logZ = GaussianDistribution::logProductNormalization($marginal, $messageValue);
$variable->setValue(GaussianDistribution::multiply($marginal, $messageValue));
return $logZ;
}
@ -33,7 +37,8 @@ abstract class GaussianFactor extends Factor
$binding = parent::createVariableToMessageBindingWithMessage($variable,
new Message(
$newDistribution,
sprintf("message from %s to %s", $this, $variable)));
sprintf('message from %s to %s', $this, $variable)));
return $binding;
}
}
}

View File

@ -1,9 +1,11 @@
<?php namespace DNW\Skills\TrueSkill\Factors;
<?php
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\Numerics\GaussianDistribution;
use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\Numerics\GaussianDistribution;
use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
/**
* Factor representing a team difference that has exceeded the draw margin.
@ -16,7 +18,7 @@ class GaussianGreaterThanFactor extends GaussianFactor
public function __construct($epsilon, Variable $variable)
{
parent::__construct(\sprintf("%s > %.2f", $variable, $epsilon));
parent::__construct(\sprintf('%s > %.2f', $variable, $epsilon));
$this->_epsilon = $epsilon;
$this->createVariableToMessageBinding($variable);
}
@ -31,6 +33,7 @@ class GaussianGreaterThanFactor extends GaussianFactor
$messages = $this->getMessages();
$message = $messages[0]->getValue();
$messageFromVariable = GaussianDistribution::divide($marginal, $message);
return -GaussianDistribution::logProductNormalization($messageFromVariable, $message)
+
log(
@ -39,7 +42,6 @@ class GaussianGreaterThanFactor extends GaussianFactor
$messageFromVariable->getStandardDeviation()
)
);
}
protected function updateMessageVariable(Message $message, Variable $variable)
@ -82,4 +84,4 @@ class GaussianGreaterThanFactor extends GaussianFactor
// Return the difference in the new marginal
return GaussianDistribution::subtract($newMarginal, $oldMarginal);
}
}
}

View File

@ -1,10 +1,12 @@
<?php namespace DNW\Skills\TrueSkill\Factors;
<?php
namespace DNW\Skills\TrueSkill\Factors;
use Exception;
use DNW\Skills\FactorGraphs\KeyedVariable;
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\Numerics\GaussianDistribution;
use Exception;
/**
* Connects two variables and adds uncertainty.
@ -17,7 +19,7 @@ class GaussianLikelihoodFactor extends GaussianFactor
public function __construct($betaSquared, Variable $variable1, Variable $variable2)
{
parent::__construct(sprintf("Likelihood of %s going to %s", $variable2, $variable1));
parent::__construct(sprintf('Likelihood of %s going to %s', $variable2, $variable1));
$this->_precision = 1.0 / $betaSquared;
$this->createVariableToMessageBinding($variable1);
$this->createVariableToMessageBinding($variable2);
@ -80,4 +82,4 @@ class GaussianLikelihoodFactor extends GaussianFactor
throw new Exception();
}
}
}
}

View File

@ -1,4 +1,6 @@
<?php namespace DNW\Skills\TrueSkill\Factors;
<?php
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
@ -15,10 +17,10 @@ class GaussianPriorFactor extends GaussianFactor
public function __construct($mean, $variance, Variable $variable)
{
parent::__construct(sprintf("Prior value going to %s", $variable));
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);
}
@ -34,6 +36,7 @@ class GaussianPriorFactor extends GaussianFactor
$variable->setValue($newMarginal);
$message->setValue($this->_newMessage);
return GaussianDistribution::subtract($oldMarginal, $newMarginal);
}
}
}

View File

@ -1,8 +1,10 @@
<?php namespace DNW\Skills\TrueSkill\Factors;
<?php
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\Guard;
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\Guard;
use DNW\Skills\Numerics\BasicMath;
use DNW\Skills\Numerics\GaussianDistribution;
@ -13,18 +15,19 @@ use DNW\Skills\Numerics\GaussianDistribution;
*/
class GaussianWeightedSumFactor extends GaussianFactor
{
private $_variableIndexOrdersForWeights = array();
private $_variableIndexOrdersForWeights = [];
// This following is used for convenience, for example, the first entry is [0, 1, 2]
// corresponding to v[0] = a1*v[1] + a2*v[2]
private $_weights;
private $_weightsSquared;
public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights = null)
{
parent::__construct(self::createName($sumVariable, $variablesToSum, $variableWeights));
$this->_weights = array();
$this->_weightsSquared = array();
$this->_weights = [];
$this->_weightsSquared = [];
// The first weights are a straightforward copy
// v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n
@ -40,7 +43,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
$variablesToSumLength = count($variablesToSum);
// 0..n-1
$this->_variableIndexOrdersForWeights[0] = array();
$this->_variableIndexOrdersForWeights[0] = [];
for ($i = 0; $i < ($variablesToSumLength + 1); $i++) {
$this->_variableIndexOrdersForWeights[0][] = $i;
}
@ -178,6 +181,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
// Return the difference in the new marginal
$finalDiff = GaussianDistribution::subtract($newMarginal, $marginal0);
return $finalDiff;
}
@ -186,10 +190,10 @@ class GaussianWeightedSumFactor extends GaussianFactor
$allMessages = $this->getMessages();
$allVariables = $this->getVariables();
Guard::argumentIsValidIndex($messageIndex, count($allMessages), "messageIndex");
Guard::argumentIsValidIndex($messageIndex, count($allMessages), 'messageIndex');
$updatedMessages = array();
$updatedVariables = array();
$updatedMessages = [];
$updatedVariables = [];
$indicesToUse = $this->_variableIndexOrdersForWeights[$messageIndex];
@ -211,7 +215,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
private static function createName($sumVariable, $variablesToSum, $weights)
{
// TODO: Perf? Use PHP equivalent of StringBuilder? implode on arrays?
$result = (string)$sumVariable;
$result = (string) $sumVariable;
$result .= ' = ';
$totalVars = count($variablesToSum);
@ -222,15 +226,15 @@ class GaussianWeightedSumFactor extends GaussianFactor
$result .= '-';
}
$absValue = sprintf("%.2f", \abs($weights[$i])); // 0.00?
$absValue = sprintf('%.2f', \abs($weights[$i])); // 0.00?
$result .= $absValue;
$result .= "*[";
$result .= (string)$variablesToSum[$i];
$result .= '*[';
$result .= (string) $variablesToSum[$i];
$result .= ']';
$isLast = ($i == ($totalVars - 1));
if (!$isLast) {
if (! $isLast) {
if ($weights[$i + 1] >= 0) {
$result .= ' + ';
} else {
@ -241,4 +245,4 @@ class GaussianWeightedSumFactor extends GaussianFactor
return $result;
}
}
}

View File

@ -1,9 +1,11 @@
<?php namespace DNW\Skills\TrueSkill\Factors;
<?php
namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\Numerics\GaussianDistribution;
use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\Numerics\GaussianDistribution;
use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
/**
* Factor representing a team difference that has not exceeded the draw margin.
@ -16,7 +18,7 @@ class GaussianWithinFactor extends GaussianFactor
public function __construct($epsilon, Variable $variable)
{
parent::__construct(sprintf("%s <= %.2f", $variable, $epsilon));
parent::__construct(sprintf('%s <= %.2f', $variable, $epsilon));
$this->_epsilon = $epsilon;
$this->createVariableToMessageBinding($variable);
}
@ -57,7 +59,7 @@ class GaussianWithinFactor extends GaussianFactor
$denominator = 1.0 - TruncatedGaussianCorrectionFunctions::wWithinMargin($dOnSqrtC, $epsilonTimesSqrtC);
$newPrecision = $c / $denominator;
$newPrecisionMean = ( $d +
$newPrecisionMean = ($d +
$sqrtC *
TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)
) / $denominator;
@ -75,4 +77,4 @@ class GaussianWithinFactor extends GaussianFactor
// Return the difference in the new marginal
return GaussianDistribution::subtract($newMarginal, $oldMarginal);
}
}
}