Files
trueskill/src/Rating.php

77 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace DNW\Skills;
use DNW\Skills\Numerics\GaussianDistribution;
/**
* Container for a player's rating.
*/
final readonly class Rating
{
private const float CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3;
/**
* Constructs a rating.
*
* @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 $conservativeStandardDeviationMultiplier optional The number of standardDeviations to subtract from the mean to achieve a conservative rating.
*/
public function __construct(private float $mean, private float $standardDeviation, private float $conservativeStandardDeviationMultiplier = self::CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER)
{
}
/**
* The statistical mean value of the rating (also known as mu).
*/
public function getMean(): float
{
return $this->mean;
}
/**
* The standard deviation (the spread) of the rating. This is also known as s.
*/
public function getStandardDeviation(): float
{
return $this->standardDeviation;
}
/**
* A conservative estimate of skill based on the mean and standard deviation.
*/
public function getConservativeRating(): float
{
return $this->mean - $this->conservativeStandardDeviationMultiplier * $this->standardDeviation;
}
/**
* Get a partial rating update.
*/
public function getPartialUpdate(Rating $prior, Rating $fullPosterior, float $updatePercentage): Rating
{
$priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation());
$posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior->getStandardDeviation());
// From a clarification email from Ralf Herbrich:
// "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;
$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);
}
}