Compare commits

...

4 Commits

Author SHA1 Message Date
1ea48d8dd0 Code standards
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2024-02-15 10:25:28 +00:00
f372d9a028 Constants for some fixed square root math. 2024-02-15 10:12:45 +00:00
5bebd9310d Optimization efforts 2024-02-15 09:08:01 +00:00
b966a930a4 Store benchmark history 2024-02-14 16:02:27 +00:00
8 changed files with 31 additions and 28 deletions

2
.gitignore vendored

@ -1,4 +1,4 @@
.vscode
.*/
vendor
.*.cache/
*.phar

@ -1,6 +1,10 @@
{
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "benchmark/",
"runner.php_disable_ini": true,
"runner.retry_threshold": 5,
"runner.iterations": 10,
"storage.xml_storage_path": "output/benchmarking/",
"report.outputs": {
"build-artifact": {
"renderer": "html",
@ -8,4 +12,5 @@
"title": "Benchmarking"
}
}
}

@ -15,11 +15,11 @@ class BasicMath
/**
* Squares the input (x^2 = x * x)
*
* @param float $x Value to square (x)
* @param $x Value to square (x)
*
* @return float The squared value (x^2)
*/
public static function square($x): float
public static function square(float $x): float
{
return $x * $x;
}

@ -12,13 +12,11 @@ class DiagonalMatrix extends Matrix
public function __construct(array $diagonalValues)
{
$diagonalCount = count($diagonalValues);
$rowCount = $diagonalCount;
$colCount = $rowCount;
parent::__construct($rowCount, $colCount);
parent::__construct($diagonalCount, $diagonalCount);
for ($currentRow = 0; $currentRow < $rowCount; $currentRow++) {
for ($currentCol = 0; $currentCol < $colCount; $currentCol++) {
for ($currentRow = 0; $currentRow < $diagonalCount; $currentRow++) {
for ($currentCol = 0; $currentCol < $diagonalCount; $currentCol++) {
if ($currentRow === $currentCol) {
$this->setValue($currentRow, $currentCol, $diagonalValues[$currentRow]);
} else {

@ -12,6 +12,13 @@ namespace DNW\Skills\Numerics;
*/
class GaussianDistribution implements \Stringable
{
//sqrt(2*pi)
//from https://www.wolframalpha.com/input?i=sqrt%282*pi%29
private const M_SQRT_2_PI = 2.5066282746310005024157652848110452530069867406099383166299235763;
//log(sqrt(2*pi))
//From https://www.wolframalpha.com/input?i=log%28sqrt%282*pi%29%29
private const M_LOG_SQRT_2_PI = 0.9189385332046727417803297364056176398613974736377834128171515404;
// precision and precisionMean are used because they make multiplying and dividing simpler
// (the the accompanying math paper for more details)
private float $precision;
@ -62,7 +69,7 @@ class GaussianDistribution implements \Stringable
public function getNormalizationConstant(): float
{
// Great derivation of this is at http://www.astro.psu.edu/~mce/A451_2/A451/downloads/notes0.pdf
return 1.0 / (sqrt(2 * M_PI) * $this->standardDeviation);
return 1.0 / (self::M_SQRT_2_PI * $this->standardDeviation);
}
public static function fromPrecisionMean(float $precisionMean, float $precision): self
@ -115,9 +122,7 @@ class GaussianDistribution implements \Stringable
$varianceSum = $left->variance + $right->variance;
$meanDifference = $left->mean - $right->mean;
$logSqrt2Pi = log(sqrt(2 * M_PI));
return -$logSqrt2Pi - (log($varianceSum) / 2.0) - (BasicMath::square($meanDifference) / (2.0 * $varianceSum));
return -self::M_LOG_SQRT_2_PI - (log($varianceSum) / 2.0) - (BasicMath::square($meanDifference) / (2.0 * $varianceSum));
}
public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator): self
@ -137,9 +142,7 @@ class GaussianDistribution implements \Stringable
$varianceDifference = $denominator->variance - $numerator->variance;
$meanDifference = $numerator->mean - $denominator->mean;
$logSqrt2Pi = log(sqrt(2 * M_PI));
return log($denominator->variance) + $logSqrt2Pi - log($varianceDifference) / 2.0 +
return log($denominator->variance) + self::M_LOG_SQRT_2_PI - log($varianceDifference) / 2.0 +
BasicMath::square($meanDifference) / (2 * $varianceDifference);
}
@ -150,7 +153,7 @@ class GaussianDistribution implements \Stringable
// P(x) = ------------------- * e
// stdDev * sqrt(2*pi)
$multiplier = 1.0 / ($standardDeviation * sqrt(2 * M_PI));
$multiplier = 1.0 / ($standardDeviation * self::M_SQRT_2_PI);
$expPart = exp((-1.0 * BasicMath::square($x - $mean)) / (2 * BasicMath::square($standardDeviation)));
return $multiplier * $expPart;
@ -158,8 +161,7 @@ class GaussianDistribution implements \Stringable
public static function cumulativeTo(float $x, float $mean = 0.0, float $standardDeviation = 1.0): float
{
$invsqrt2 = -0.707106781186547524400844362104;
$result = GaussianDistribution::errorFunctionCumulativeTo($invsqrt2 * $x);
$result = GaussianDistribution::errorFunctionCumulativeTo(-M_SQRT1_2 * $x);
return 0.5 * $result;
}
@ -231,11 +233,11 @@ class GaussianDistribution implements \Stringable
$pp = ($p < 1.0) ? $p : 2 - $p;
$t = sqrt(-2 * log($pp / 2.0)); // Initial guess
$x = -0.70711 * ((2.30753 + $t * 0.27061) / (1.0 + $t * (0.99229 + $t * 0.04481)) - $t);
$x = -M_SQRT1_2 * ((2.30753 + $t * 0.27061) / (1.0 + $t * (0.99229 + $t * 0.04481)) - $t);
for ($j = 0; $j < 2; $j++) {
$err = GaussianDistribution::errorFunctionCumulativeTo($x) - $pp;
$x += $err / (1.12837916709551257 * exp(-BasicMath::square($x)) - $x * $err); // Halley
$x += $err / (M_2_SQRTPI * exp(-BasicMath::square($x)) - $x * $err); // Halley
}
return ($p < 1.0) ? $x : -$x;
@ -244,7 +246,7 @@ class GaussianDistribution implements \Stringable
public static function inverseCumulativeTo(float $x, float $mean = 0.0, float $standardDeviation = 1.0): float
{
// From numerical recipes, page 320
return $mean - sqrt(2) * $standardDeviation * GaussianDistribution::inverseErrorFunctionCumulativeTo(2 * $x);
return $mean - M_SQRT2 * $standardDeviation * GaussianDistribution::inverseErrorFunctionCumulativeTo(2 * $x);
}
public function __toString(): string

@ -8,18 +8,17 @@ class SquareMatrix extends Matrix
{
public function __construct(float|int ...$allValues)
{
$rows = (int)sqrt(count($allValues));
$cols = $rows;
$size = (int)sqrt(count($allValues));
$matrixData = [];
$allValuesIndex = 0;
for ($currentRow = 0; $currentRow < $rows; $currentRow++) {
for ($currentColumn = 0; $currentColumn < $cols; $currentColumn++) {
for ($currentRow = 0; $currentRow < $size; $currentRow++) {
for ($currentColumn = 0; $currentColumn < $size; $currentColumn++) {
$matrixData[$currentRow][$currentColumn] = $allValues[$allValuesIndex++];
}
}
parent::__construct($rows, $cols, $matrixData);
parent::__construct($size, $size, $matrixData);
}
}

@ -18,6 +18,6 @@ final class DrawMargin
//
// margin = inversecdf((draw probability + 1)/2) * sqrt(n1+n2) * beta
// n1 and n2 are the number of players on each team
return GaussianDistribution::inverseCumulativeTo(.5 * ($drawProbability + 1), 0, 1) * sqrt(1 + 1) * $beta;
return GaussianDistribution::inverseCumulativeTo(.5 * ($drawProbability + 1), 0, 1) * M_SQRT2 * $beta;
}
}

@ -181,7 +181,6 @@ class GaussianWeightedSumFactor extends GaussianFactor
}
$newPrecision = 1.0 / $inverseOfNewPrecisionSum;
$anotherNewPrecision = 1.0 / $anotherInverseOfNewPrecisionSum;
$newPrecisionMean = $newPrecision * $weightedMeanSum;