-
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.
Extract a few more Distribution functions from Statistical (#1975)
* Extract a few more Distribution functions from Statistical; this time EXPONDIST() and HYPGEOMDIST() * Extract the F Distribution (although only F.DIST() is implemented so far * Updae docblocks * PHPCS
- Loading branch information
Mark Baker
authored
Mar 31, 2021
1 parent
029f345
commit 17af132
Showing
10 changed files
with
344 additions
and
118 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
49 changes: 49 additions & 0 deletions
49
src/PhpSpreadsheet/Calculation/Statistical/Distributions/Exponential.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,49 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; | ||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; | ||
|
||
class Exponential | ||
{ | ||
use BaseValidations; | ||
|
||
/** | ||
* EXPONDIST. | ||
* | ||
* Returns the exponential distribution. Use EXPONDIST to model the time between events, | ||
* such as how long an automated bank teller takes to deliver cash. For example, you can | ||
* use EXPONDIST to determine the probability that the process takes at most 1 minute. | ||
* | ||
* @param mixed (float) $value Value of the function | ||
* @param mixed (float) $lambda The parameter value | ||
* @param mixed (bool) $cumulative | ||
* | ||
* @return float|string | ||
*/ | ||
public static function distribution($value, $lambda, $cumulative) | ||
{ | ||
$value = Functions::flattenSingleValue($value); | ||
$lambda = Functions::flattenSingleValue($lambda); | ||
$cumulative = Functions::flattenSingleValue($cumulative); | ||
|
||
try { | ||
$value = self::validateFloat($value); | ||
$lambda = self::validateFloat($lambda); | ||
$cumulative = self::validateBool($cumulative); | ||
} catch (Exception $e) { | ||
return $e->getMessage(); | ||
} | ||
|
||
if (($value < 0) || ($lambda < 0)) { | ||
return Functions::NAN(); | ||
} | ||
|
||
if ($cumulative === true) { | ||
return 1 - exp(0 - $value * $lambda); | ||
} | ||
|
||
return $lambda * exp(0 - $value * $lambda); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/PhpSpreadsheet/Calculation/Statistical/Distributions/F.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,59 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Exception; | ||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; | ||
|
||
class F | ||
{ | ||
use BaseValidations; | ||
|
||
/** | ||
* F.DIST. | ||
* | ||
* Returns the F probability distribution. | ||
* You can use this function to determine whether two data sets have different degrees of diversity. | ||
* For example, you can examine the test scores of men and women entering high school, and determine | ||
* if the variability in the females is different from that found in the males. | ||
* | ||
* @param mixed(float) $value Value of the function | ||
* @param mixed(int) $u The numerator degrees of freedom | ||
* @param mixed(int) $v The denominator degrees of freedom | ||
* @param mixed(bool) $cumulative If cumulative is TRUE, F.DIST returns the cumulative distribution function; | ||
* if FALSE, it returns the probability density function. | ||
* | ||
* @return float|string | ||
*/ | ||
public static function distribution($value, $u, $v, $cumulative) | ||
{ | ||
$value = Functions::flattenSingleValue($value); | ||
$u = Functions::flattenSingleValue($u); | ||
$v = Functions::flattenSingleValue($v); | ||
$cumulative = Functions::flattenSingleValue($cumulative); | ||
|
||
try { | ||
$value = self::validateFloat($value); | ||
$u = self::validateInt($u); | ||
$v = self::validateInt($v); | ||
$cumulative = self::validateBool($cumulative); | ||
} catch (Exception $e) { | ||
return $e->getMessage(); | ||
} | ||
|
||
if ($value < 0 || $u < 1 || $v < 1) { | ||
return Functions::NAN(); | ||
} | ||
|
||
if ($cumulative) { | ||
$adjustedValue = ($u * $value) / ($u * $value + $v); | ||
|
||
return Beta::incompleteBeta($adjustedValue, $u / 2, $v / 2); | ||
} | ||
|
||
return (Gamma::gammaValue(($v + $u) / 2) / | ||
(Gamma::gammaValue($u / 2) * Gamma::gammaValue($v / 2))) * | ||
(($u / $v) ** ($u / 2)) * | ||
(($value ** (($u - 2) / 2)) / ((1 + ($u / $v) * $value) ** (($u + $v) / 2))); | ||
} | ||
} |
Oops, something went wrong.