Skip to contents

This function calculates the cumulative distribution function (CDF) of the difference between two independent beta-distributed random variables using numerical integration of the probability density function. The density function uses Appell's first hypergeometric function for computation. Specifically, it computes P(X1 - X2 ≤ q) or P(X1 - X2 > q) where X1 ~ Beta(alpha1, beta1) and X2 ~ Beta(alpha2, beta2).

Usage

p2betadiff(q, alpha1, alpha2, beta1, beta2, lower.tail = TRUE)

Arguments

q

A numeric value in the range [-1, 1] representing the quantile threshold.

alpha1

A positive numeric value representing the first shape parameter of the first beta distribution.

alpha2

A positive numeric value representing the first shape parameter of the second beta distribution.

beta1

A positive numeric value representing the second shape parameter of the first beta distribution.

beta2

A positive numeric value representing the second shape parameter of the second beta distribution.

lower.tail

A logical value; if TRUE (default), probabilities are P(X1 - X2 ≤ q), otherwise P(X1 - X2 > q).

Value

A numeric value in [0, 1] representing the cumulative probability that the difference between the two beta variables is below (if lower.tail = TRUE) or exceeds (if lower.tail = FALSE) the quantile q.

Details

The function computes the CDF by numerically integrating the probability density function of the difference using adaptive quadrature. The density is evaluated using piecewise formulations based on Appell's F1 hypergeometric function:

  • For π ∈ [-1, 0): Uses Appell's F1 with parameters adjusted for negative differences

  • For π ∈ [0, 1): Uses Appell's F1 with parameters adjusted for positive differences

  • Special case handling when π = 0 and certain parameter conditions are met

The upper tail probability P(X1 - X2 > q) is computed as: $$P(X1 - X2 > q) = \int_q^1 f(π) dπ$$

where f(π) is the probability density function of the difference.

Examples

# Calculate P(Beta(0.5, 0.5) - Beta(0.5, 0.5) > 0.2)
p2betadiff(0.2, 0.5, 0.5, 0.5, 0.5, lower.tail = FALSE)
#> [1] 0.3377407

# Calculate P(Beta(2, 3) - Beta(1, 4) > -0.1)
p2betadiff(-0.1, 2, 3, 1, 4, lower.tail = FALSE)
#> [1] 0.8614457

# Calculate P(Beta(1, 1) - Beta(1, 1) > 0) - should be approximately 0.5
p2betadiff(0, 1, 1, 1, 1, lower.tail = FALSE)
#> [1] 0.5

# Calculate lower tail probability P(Beta(2, 2) - Beta(2, 2) ≤ 0.1)
p2betadiff(0.1, 2, 2, 2, 2, lower.tail = TRUE)
#> [1] 0.6181498