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