29 public static function distribution($value, $alpha, $beta, $rMin = 0.0, $rMax = 1.0)
44 return $e->getMessage();
52 if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
57 $value /= ($rMax - $rMin);
59 return self::incompleteBeta($value, $alpha, $beta);
75 public static function inverse($probability, $alpha, $beta, $rMin = 0.0, $rMax = 1.0)
90 return $e->getMessage();
98 if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0.0)) {
102 return self::calculateInverse($probability, $alpha, $beta, $rMin, $rMax);
108 private static function calculateInverse(
float $probability,
float $alpha,
float $beta,
float $rMin,
float $rMax)
115 $guess = ($a + $b) / 2;
116 $result = self::distribution($guess, $alpha, $beta);
119 } elseif (
$result > $probability) {
126 if (
$i === self::MAX_ITERATIONS) {
130 return round($rMin + $guess * ($rMax - $rMin), 12);
151 } elseif ($x >= 1.0) {
153 } elseif (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > self::LOG_GAMMA_X_MAX_VALUE)) {
157 $beta_gam = exp((0 - self::logBeta($p, $q)) + $p * log($x) + $q * log(1.0 - $x));
158 if ($x < ($p + 1.0) / ($p + $q + 2.0)) {
159 return $beta_gam * self::betaFraction($x, $p, $q) / $p;
162 return 1.0 - ($beta_gam * self::betaFraction(1 - $x, $q, $p) / $q);
182 private static function logBeta(
float $p,
float $q): float
184 if ($p != self::$logBetaCacheP || $q != self::$logBetaCacheQ) {
185 self::$logBetaCacheP = $p;
186 self::$logBetaCacheQ = $q;
187 if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > self::LOG_GAMMA_X_MAX_VALUE)) {
188 self::$logBetaCacheResult = 0.0;
194 return self::$logBetaCacheResult;
209 $h = 1.0 - $sum_pq * $x / $p_plus;
210 if (abs(
$h) < self::XMININ) {
220 $d =
$m * ($q -
$m) * $x / (($p_minus + $m2) * ($p + $m2));
222 if (abs($h) < self::XMININ) {
227 if (abs(
$c) < self::XMININ) {
232 $d = -($p +
$m) * ($sum_pq +
$m) * $x / (($p + $m2) * ($p_plus + $m2));
234 if (abs($h) < self::XMININ) {
239 if (abs($c) < self::XMININ) {
250 private static function betaValue(
float $a,
float $b): float
258 return self::incompleteBeta($value, $a, $b) / self::betaValue($a, $b);
static distribution($value, $alpha, $beta, $rMin=0.0, $rMax=1.0)
BETADIST.
const LOG_GAMMA_X_MAX_VALUE
static regularizedIncompleteBeta(float $value, float $a, float $b)
static betaFraction(float $x, float $p, float $q)
Evaluates of continued fraction part of incomplete beta function.
static incompleteBeta(float $x, float $p, float $q)
Incomplete beta function.
static $logBetaCacheResult
static inverse($probability, $alpha, $beta, $rMin=0.0, $rMax=1.0)
BETAINV.
static betaValue(float $a, float $b)
static validateFloat($value)
static gammaValue(float $value)
static logBeta(float $p, float $q)
The natural logarithm of the beta function.
static calculateInverse(float $probability, float $alpha, float $beta, float $rMin, float $rMax)
static validateProbability($probability)
static logGamma(float $x)
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d