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

48 lines
1.5 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
{
private readonly GaussianDistribution $newMessage;
2023-08-01 12:43:58 +00:00
public function __construct(float $mean, float $variance, Variable $variable)
{
2024-03-19 15:56:57 +00:00
//Prior value going to $variable
parent::__construct();
$this->newMessage = new GaussianDistribution($mean, sqrt($variance));
2023-08-01 13:53:19 +00:00
$newMessage = new Message(
2024-03-19 15:56:57 +00:00
GaussianDistribution::fromPrecisionMean(0, 0)
2023-08-01 13:53:19 +00:00
);
$this->createVariableToMessageBindingWithMessage($variable, $newMessage);
}
2025-01-28 09:20:03 +00:00
#[\Override]
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
}