Probability Density Function of the Difference Between Two Beta Variables
Source:R/d2betadiff.R
d2betadiff.RdThis function calculates the probability density function (PDF) of the difference between two independent beta-distributed random variables. The computation uses Appell's first hypergeometric function for numerical evaluation. Specifically, it computes the density f(π) where π = X1 - X2, with X1 ~ Beta(alpha1, beta1) and X2 ~ Beta(alpha2, beta2).
Arguments
- pi
A numeric value in the range
[-1, 1]representing the difference between the two beta variables.- 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.
Value
A numeric value representing the probability density at the specified
difference value. Returns 0 if pi is outside the range [-1, 1].
Details
The function evaluates the density using different formulations depending on the range of π:
For π ∈
[-1, 0): Uses Appell's F1 function with parameters adjusted for negative differencesFor π ∈
[0, 1): Uses Appell's F1 function with parameters adjusted for positive differencesSpecial case at π = 0: When alpha1 + alpha2 > 1 and beta1 + beta2 > 1, uses a simplified beta function form
For π ∉
[-1, 1]: Returns 0 (outside support of the distribution)
The normalization constant k is computed as: $$k = \frac{1}{B(\alpha_1, \beta_1) B(\alpha_2, \beta_2)}$$
where B(·,·) is the beta function.
Examples
# Calculate density at π = 0.2 for symmetric beta distributions
d2betadiff(0.2, 0.5, 0.5, 0.5, 0.5)
#> [1] 0.6111922
# Calculate density at π = 0 for Beta(2,3) - Beta(1,4)
d2betadiff(0, 2, 3, 1, 4)
#> [1] 0.8571429
# Calculate density at π = -0.3 for informative priors
d2betadiff(-0.3, 3, 2, 2, 3)
#> [1] 0.3267281
# Verify that density is 0 outside [-1, 1]
d2betadiff(1.5, 1, 1, 1, 1)
#> [1] 0