<?php

namespace Moserware\Skills\TrueSkill\Factors;

/// <summary>
/// Supplies the factor graph with prior information.
/// </summary>
/// <remarks>See the accompanying math paper for more details.</remarks>
class GaussianPriorFactor extends GaussianFactor
{
    private $_newMessage;

    public function __construct($mean, $variance, Variable $variable)
    {
        parent::__construct("Prior value going to {0}");
        $this->_newMessage = new GaussianDistribution($mean, sqrt($variance));
        $this->createVariableToMessageBinding($variable,
                                               new Message(
                                                   GaussianDistribution::fromPrecisionMean(0, 0),
                                                       "message from {0} to {1}",
                                                   this, variable));
    }

    protected function updateMessageVariable(Message $message, Variable $variable)
    {
        $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);
        return GaussianDistribution::subtract($oldMarginal, $newMarginal);
    }
}


?>