Compare commits

..

No commits in common. "1ea48d8dd0f7da03b6201665950251610ff8d886" and "8a0869d535ad8743b4170e2a756707186a3c95be" have entirely different histories.

8 changed files with 28 additions and 31 deletions

2
.gitignore vendored

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

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

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

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

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

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

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

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