trueskill/src/Rating.php

75 lines
2.9 KiB
PHP
Raw Normal View History

2022-07-05 13:55:47 +00:00
<?php
namespace DNW\Skills;
// Container for a player's rating.
2022-07-05 13:33:34 +00:00
use DNW\Skills\Numerics\GaussianDistribution;
2022-07-05 14:21:06 +00:00
class Rating implements \Stringable
{
2022-07-05 14:21:06 +00:00
final const CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3;
/**
* Constructs a rating.
2022-07-05 13:55:47 +00:00
*
2022-07-05 14:21:06 +00:00
* @param float $_mean The statistical mean value of the rating (also known as mu).
* @param float $_standardDeviation The standard deviation of the rating (also known as s).
* @param float|int $_conservativeStandardDeviationMultiplier optional The number of standardDeviations to subtract from the mean to achieve a conservative rating.
*/
2022-07-05 14:21:06 +00:00
public function __construct(private $_mean, private $_standardDeviation, private $_conservativeStandardDeviationMultiplier = self::CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER)
{
}
/**
* The statistical mean value of the rating (also known as <EFBFBD>).
*/
public function getMean()
{
return $this->_mean;
}
/**
* The standard deviation (the spread) of the rating. This is also known as s.
*/
public function getStandardDeviation()
{
return $this->_standardDeviation;
}
/**
* A conservative estimate of skill based on the mean and standard deviation.
*/
public function getConservativeRating()
{
return $this->_mean - $this->_conservativeStandardDeviationMultiplier * $this->_standardDeviation;
}
public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage)
{
$priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation());
2022-07-05 13:55:47 +00:00
$posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior.getStandardDeviation());
// From a clarification email from Ralf Herbrich:
2022-07-05 13:33:34 +00:00
// "the idea is to compute a linear interpolation between the prior and posterior skills of each player
// ... in the canonical space of parameters"
$precisionDifference = $posteriorGaussian->getPrecision() - $priorGaussian->getPrecision();
$partialPrecisionDifference = $updatePercentage * $precisionDifference;
2022-07-05 13:55:47 +00:00
$precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian.getPrecisionMean();
$partialPrecisionMeanDifference = $updatePercentage * $precisionMeanDifference;
$partialPosteriorGaussion = GaussianDistribution::fromPrecisionMean(
$priorGaussian->getPrecisionMean() + $partialPrecisionMeanDifference,
$priorGaussian->getPrecision() + $partialPrecisionDifference
);
return new Rating($partialPosteriorGaussion->getMean(), $partialPosteriorGaussion->getStandardDeviation(), $prior->_conservativeStandardDeviationMultiplier);
}
2022-07-05 14:21:06 +00:00
public function __toString(): string
{
2022-07-05 13:55:47 +00:00
return sprintf('mean=%.4f, standardDeviation=%.4f', $this->_mean, $this->_standardDeviation);
}
2022-07-05 13:55:47 +00:00
}