
Cumulative Distribution Function of the Difference Between Two Independent Beta Variables
Source:R/pbetadiff.R
pbetadiff.RdCalculates the cumulative distribution function (CDF) of the difference between two independent Beta-distributed random variables. Specifically, computes \(P(\pi_t - \pi_c \le q)\) or \(P(\pi_t - \pi_c > q)\), where \(\pi_j \sim \mathrm{Beta}(\alpha_j, \beta_j)\) for \(j \in \{t, c\}\).
Arguments
- q
A numeric scalar in
[-1, 1]representing the quantile threshold for the difference in proportions.- alpha_t
A positive numeric scalar giving the first shape parameter of the Beta distribution for the treatment group.
- alpha_c
A positive numeric scalar giving the first shape parameter of the Beta distribution for the control group.
- beta_t
A positive numeric scalar giving the second shape parameter of the Beta distribution for the treatment group.
- beta_c
A positive numeric scalar giving the second shape parameter of the Beta distribution for the control group.
- lower.tail
A logical scalar; if
TRUE(default), the function returns \(P(\pi_t - \pi_c \le q)\), otherwise \(P(\pi_t - \pi_c > q)\).
Details
The upper-tail probability is obtained via the convolution formula:
$$P(\pi_t - \pi_c > q)
= \int_0^1 F_{\mathrm{Beta}(\alpha_c, \beta_c)}(x - q)\,
f_{\mathrm{Beta}(\alpha_t, \beta_t)}(x)\, dx$$
where \(f_{\mathrm{Beta}(\alpha_t, \beta_t)}\) is the density of
\(\pi_t\) and \(F_{\mathrm{Beta}(\alpha_c, \beta_c)}\) is the CDF of
\(\pi_c\). Boundary cases are handled automatically by pbeta
(\(0\) for \(x - q \le 0\), \(1\) for \(x - q \ge 1\)), so
integrating over [0, 1] is safe.
This single-integral convolution replaces an equivalent double-integral
formulation based on Appell's F1 hypergeometric function, yielding the
same result with lower computational cost because both pbeta and
dbeta are implemented in compiled C code.
Examples
# P(pi_t - pi_c > 0.2) with symmetric Beta(0.5, 0.5) priors
pbetadiff(0.2, 0.5, 0.5, 0.5, 0.5, lower.tail = FALSE)
#> [1] 0.3377403
# P(pi_t - pi_c > -0.1) with informative priors
pbetadiff(-0.1, 2, 1, 3, 4, lower.tail = FALSE)
#> [1] 0.8783
# P(pi_t - pi_c > 0) with equal priors -- should be approximately 0.5
pbetadiff(0, 1, 1, 1, 1, lower.tail = FALSE)
#> [1] 0.5
# Lower tail: P(pi_t - pi_c <= 0.1) with symmetric priors
pbetadiff(0.1, 2, 2, 2, 2, lower.tail = TRUE)
#> [1] 0.6181518