-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementing Newton-Raphson for the inverse of Statistical Dist…
…ributions (#1958) * Start implementing Newton-Raphson for the inverse of Statistical Distributions, starting with the two-tailed Student-T * Additional unit tests and validations * Use the new Newton Raphson class for calculating the Inverse of ChiSquared * Extract Weibull distribution, and provide unit tests
- Loading branch information
Mark Baker
authored
Mar 27, 2021
1 parent
c699d14
commit ec25314
Showing
13 changed files
with
493 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/PhpSpreadsheet/Calculation/Statistical/Distributions/NewtonRaphson.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; | ||
|
||
class NewtonRaphson | ||
{ | ||
private const MAX_ITERATIONS = 256; | ||
|
||
protected $callback; | ||
|
||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
public function execute($probability) | ||
{ | ||
$xLo = 100; | ||
$xHi = 0; | ||
|
||
$x = $xNew = 1; | ||
$dx = 1; | ||
$i = 0; | ||
|
||
while ((abs($dx) > Functions::PRECISION) && ($i++ < self::MAX_ITERATIONS)) { | ||
// Apply Newton-Raphson step | ||
$result = call_user_func($this->callback, $x); | ||
$error = $result - $probability; | ||
|
||
if ($error == 0.0) { | ||
$dx = 0; | ||
} elseif ($error < 0.0) { | ||
$xLo = $x; | ||
} else { | ||
$xHi = $x; | ||
} | ||
|
||
// Avoid division by zero | ||
if ($result != 0.0) { | ||
$dx = $error / $result; | ||
$xNew = $x - $dx; | ||
} | ||
|
||
// If the NR fails to converge (which for example may be the | ||
// case if the initial guess is too rough) we apply a bisection | ||
// step to determine a more narrow interval around the root. | ||
if (($xNew < $xLo) || ($xNew > $xHi) || ($result == 0.0)) { | ||
$xNew = ($xLo + $xHi) / 2; | ||
$dx = $xNew - $x; | ||
} | ||
$x = $xNew; | ||
} | ||
|
||
if ($i == self::MAX_ITERATIONS) { | ||
return Functions::NA(); | ||
} | ||
|
||
return $x; | ||
} | ||
} |
Oops, something went wrong.