Skip to content

Commit 5ed9ce7

Browse files
authored
10892 Implement std.mathspecial.betaIncompleteCompl (#10893)
This implements std.mathspecial.betaIncompleteCompl and updates the documentation for std.mathspecial.betaIncomplete accordingly.
1 parent 748ea8a commit 5ed9ce7

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

std/mathspecial.d

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,32 +264,75 @@ real logmdigammaInverse(real x)
264264
return std.internal.math.gammafunction.logmdigammaInverse(x);
265265
}
266266

267-
/** Incomplete beta integral
267+
/** Regularized incomplete beta function $(SUB I, x)(a,b)
268268
*
269-
* Returns regularized incomplete beta integral of the arguments, evaluated
270-
* from zero to x. The regularized incomplete beta function is defined as
269+
* Mathematically, if a and b are positive real numbers, and 0 $(LE) x $(LE) 1, then
270+
* $(SUB I, x)(a,b) = $(INTEGRATE 0, x)$(POWER t, a-1)$(POWER (1-t), b-1)dt/B(a,b) where B is the
271+
* beta function. It is also the cumulative distribution function of the beta distribution.
271272
*
272-
* betaIncomplete(a, b, x) = $(GAMMA)(a + b) / ( $(GAMMA)(a) $(GAMMA)(b) ) *
273-
* $(INTEGRATE 0, x) $(POWER t, a-1)$(POWER (1-t), b-1) dt
273+
* `betaIncomplete(a, b, x)` evaluates $(SUB I, `x`)(`a`,`b`).
274274
*
275-
* and is the same as the cumulative distribution function of the Beta
276-
* distribution.
275+
* Params:
276+
* a = the first argument of B, must be positive
277+
* b = the second argument of B, must be positive
278+
* x = the fraction of integration completion from below, 0 $(LE) x $(LE) 1
277279
*
278-
* The domain of definition is 0 <= x <= 1. In this
279-
* implementation a and b are restricted to positive values.
280-
* The integral from x to 1 may be obtained by the symmetry
281-
* relation
280+
* Returns:
281+
* It returns $(SUB I, x)(a,b), an element of [0,1].
282282
*
283-
* betaIncompleteCompl(a, b, x ) = betaIncomplete( b, a, 1-x )
283+
* Note:
284+
* The integral is evaluated by a continued fraction expansion or, when `b * x` is small, by a
285+
* power series.
284286
*
285-
* The integral is evaluated by a continued fraction expansion
286-
* or, when b * x is small, by a power series.
287+
* See_Also: $(LREF beta) $(LREF betaIncompleteCompl)
287288
*/
288289
real betaIncomplete(real a, real b, real x )
289290
{
290291
return std.internal.math.gammafunction.betaIncomplete(a, b, x);
291292
}
292293

294+
/** Regularized incomplete beta function complement $(SUB I$(SUP C), x)(a,b)
295+
*
296+
* Mathematically, if a $(GT) 0, b $(GT) 0, and 0 $(LE) x $(LE) 1, then
297+
* $(SUB I$(SUP C), x)(a,b) = $(INTEGRATE x, 1)$(POWER t, a-1)$(POWER (1-t), b-1)dt/B(a,b) where B
298+
* is the beta function. It is also the complement of the cumulative distribution function of the
299+
* beta distribution. It can be shown that $(SUB I$(SUP C), x)(a,b) = $(SUB I, 1-x)(b,a).
300+
*
301+
* `betaIncompleteCompl(a, b, x)` evaluates $(SUB I$(SUP C), `x`)(`a`,`b`).
302+
*
303+
* Params:
304+
* a = the first argument of B, must be positive
305+
* b = the second argument of B, must be positive
306+
* x = the fraction of integration completion from above, 0 $(LE) x $(LE) 1
307+
*
308+
* Returns:
309+
* It returns $(SUB I$(SUP C), x)(a,b), an element of [0,1].
310+
*
311+
* See_Also: $(LREF beta) $(LREF betaIncomplete)
312+
*/
313+
real betaIncompleteCompl(real a, real b, real x)
314+
in
315+
{
316+
// allow NaN input to pass through so that it can be addressed by the
317+
// internal NaN payload propagation logic
318+
if (!isNaN(a) && !isNaN(b) && !isNaN(x))
319+
{
320+
assert(signbit(a) == 0, "a must be positive");
321+
assert(signbit(b) == 0, "b must be positive");
322+
assert(x >= 0 && x <= 1, "x must be in [0, 1]");
323+
}
324+
}
325+
do
326+
{
327+
return std.internal.math.gammafunction.betaIncomplete(b, a, 1-x);
328+
}
329+
330+
///
331+
@safe unittest
332+
{
333+
assert(betaIncompleteCompl(.1, .2, 0) == betaIncomplete(.2, .1, 1));
334+
}
335+
293336
/** Inverse of incomplete beta integral
294337
*
295338
* Given y, the function finds x such that

0 commit comments

Comments
 (0)