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

47 lines
1.6 KiB
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
declare(strict_types=1);
2022-07-05 15:55:47 +02:00
namespace DNW\Skills\TrueSkill\Factors;
2022-07-05 15:33:34 +02:00
use DNW\Skills\FactorGraphs\Message;
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\Numerics\GaussianDistribution;
/**
* Supplies the factor graph with prior information.
*
* See the accompanying math paper for more details.
*/
class GaussianPriorFactor extends GaussianFactor
{
2023-08-02 09:04:56 +00:00
private GaussianDistribution $newMessage;
2023-08-01 12:43:58 +00:00
public function __construct(float $mean, float $variance, Variable $variable)
{
2023-08-08 07:00:51 +00:00
parent::__construct(sprintf('Prior value going to %s', (string)$variable));
$this->newMessage = new GaussianDistribution($mean, sqrt($variance));
2023-08-01 13:53:19 +00:00
$newMessage = new Message(
GaussianDistribution::fromPrecisionMean(0, 0),
2023-08-08 07:00:51 +00:00
sprintf('message from %s to %s', (string)$this, (string)$variable)
2023-08-01 13:53:19 +00:00
);
$this->createVariableToMessageBindingWithMessage($variable, $newMessage);
}
2023-08-01 12:43:58 +00:00
protected function updateMessageVariable(Message $message, Variable $variable): float
{
$oldMarginal = clone $variable->getValue();
$oldMessage = $message;
$newMarginal = GaussianDistribution::fromPrecisionMean(
$oldMarginal->getPrecisionMean() + $this->newMessage->getPrecisionMean() - $oldMessage->getValue()->getPrecisionMean(),
$oldMarginal->getPrecision() + $this->newMessage->getPrecision() - $oldMessage->getValue()->getPrecision()
);
$variable->setValue($newMarginal);
$message->setValue($this->newMessage);
2022-07-05 15:55:47 +02:00
return GaussianDistribution::subtract($oldMarginal, $newMarginal);
}
2022-07-05 15:55:47 +02:00
}