Files
trueskill/src/TrueSkill/Factors/GaussianGreaterThanFactor.php

87 lines
2.7 KiB
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
namespace DNW\Skills\TrueSkill\Factors;
2022-07-05 15:33:34 +02:00
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
2022-07-05 15:55:47 +02:00
use DNW\Skills\Numerics\GaussianDistribution;
use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
/**
* Factor representing a team difference that has exceeded the draw margin.
*
* See the accompanying math paper for more details.
*/
class GaussianGreaterThanFactor extends GaussianFactor
{
2023-08-02 10:10:57 +00:00
private float $epsilon;
2023-08-02 10:10:57 +00:00
public function __construct(float $epsilon, Variable $variable)
{
2023-08-08 07:00:51 +00:00
parent::__construct(\sprintf('%s > %.2f', (string)$variable, $epsilon));
2023-08-01 13:53:19 +00:00
$this->epsilon = $epsilon;
$this->createVariableToMessageBinding($variable);
}
2023-08-02 10:10:57 +00:00
public function getLogNormalization(): float
{
$vars = $this->getVariables();
$marginal = $vars[0]->getValue();
2023-08-03 14:26:00 +00:00
$messages = $this->getMessages();
$message = $messages[0]->getValue();
$messageFromVariable = GaussianDistribution::divide($marginal, $message);
2022-07-05 15:55:47 +02:00
return -GaussianDistribution::logProductNormalization($messageFromVariable, $message)
+
log(
GaussianDistribution::cumulativeTo(
2023-08-01 13:53:19 +00:00
($messageFromVariable->getMean() - $this->epsilon) /
$messageFromVariable->getStandardDeviation()
)
);
}
2023-08-02 10:10:57 +00:00
protected function updateMessageVariable(Message $message, Variable $variable): float
{
$oldMarginal = clone $variable->getValue();
$oldMessage = clone $message->getValue();
$messageFromVar = GaussianDistribution::divide($oldMarginal, $oldMessage);
$c = $messageFromVar->getPrecision();
$d = $messageFromVar->getPrecisionMean();
$sqrtC = sqrt($c);
$dOnSqrtC = $d / $sqrtC;
2023-08-01 13:53:19 +00:00
$epsilsonTimesSqrtC = $this->epsilon * $sqrtC;
$d = $messageFromVar->getPrecisionMean();
$denom = 1.0 - TruncatedGaussianCorrectionFunctions::wExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC);
$newPrecision = $c / $denom;
$newPrecisionMean = (
$d +
$sqrtC *
TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)
) / $denom;
$newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
$newMessage = GaussianDistribution::divide(
GaussianDistribution::multiply($oldMessage, $newMarginal),
$oldMarginal
);
// Update the message and marginal
$message->setValue($newMessage);
$variable->setValue($newMarginal);
// Return the difference in the new marginal
return GaussianDistribution::subtract($newMarginal, $oldMarginal);
}
2022-07-05 15:55:47 +02:00
}