2016-05-24 14:10:39 +02:00
|
|
|
<?php namespace Moserware\Skills\TrueSkill\Factors;
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2010-09-18 17:56:57 -04:00
|
|
|
use Moserware\Skills\FactorGraphs\Message;
|
|
|
|
use Moserware\Skills\FactorGraphs\Variable;
|
2016-05-24 14:10:39 +02:00
|
|
|
use Moserware\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
|
|
|
|
{
|
|
|
|
private $_newMessage;
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public function __construct($mean, $variance, Variable $variable)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2010-09-27 08:00:22 -04:00
|
|
|
parent::__construct(sprintf("Prior value going to %s", $variable));
|
2010-09-18 11:11:44 -04:00
|
|
|
$this->_newMessage = new GaussianDistribution($mean, sqrt($variance));
|
2010-09-25 18:25:56 -04:00
|
|
|
$newMessage = new Message(GaussianDistribution::fromPrecisionMean(0, 0),
|
2016-05-24 14:10:39 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
protected function updateMessageVariable(Message $message, Variable $variable)
|
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(
|
|
|
|
$oldMarginal->getPrecisionMean() + $this->_newMessage->getPrecisionMean() - $oldMessage->getValue()->getPrecisionMean(),
|
|
|
|
$oldMarginal->getPrecision() + $this->_newMessage->getPrecision() - $oldMessage->getValue()->getPrecision()
|
|
|
|
);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
|
|
|
$variable->setValue($newMarginal);
|
2016-05-24 16:31:21 +02:00
|
|
|
$message->setValue($this->_newMessage);
|
2010-09-18 11:11:44 -04:00
|
|
|
return GaussianDistribution::subtract($oldMarginal, $newMarginal);
|
|
|
|
}
|
2016-05-24 14:10:39 +02:00
|
|
|
}
|