
Cumulative Distribution Function of the Difference of Two Independent t-Distributed Variables via Moment-Matching Approximation
Source:R/ptdiff_MM.R
ptdiff_MM.RdCalculates the cumulative distribution function (CDF) of the difference between two independent non-standardised t-distributed random variables using a Moment-Matching approximation. Specifically, computes \(P(T_t - T_c \le q)\) or \(P(T_t - T_c > q)\), where \(T_k \sim t(\mu_k, \sigma_k^2, \nu_k)\) for \(k \in \{t, c\}\).
Arguments
- q
A numeric scalar representing the quantile threshold.
- mu_t
A numeric scalar or vector giving the location parameter of the t-distribution for the treatment group.
- mu_c
A numeric scalar or vector giving the location parameter of the t-distribution for the control group.
- sd_t
A positive numeric scalar or vector giving the scale parameter of the t-distribution for the treatment group.
- sd_c
A positive numeric scalar or vector giving the scale parameter of the t-distribution for the control group.
- nu_t
A numeric scalar giving the degrees of freedom of the t-distribution for the treatment group. Must be greater than 4 for finite fourth moment.
- nu_c
A numeric scalar giving the degrees of freedom of the t-distribution for the control group. Must be greater than 4 for finite fourth moment.
- lower.tail
A logical scalar; if
TRUE(default), the function returns \(P(T_t - T_c \le q)\), otherwise \(P(T_t - T_c > q)\).
Value
A numeric scalar or vector in [0, 1]. When mu_t,
mu_c, sd_t, or sd_c are vectors of length
\(n\), a vector of length \(n\) is returned.
Details
The difference \(D = T_t - T_c\) is approximated by a single non-standardised t-distribution \(t(\mu^*, {\sigma^*}^2, \nu^*)\) whose parameters are determined by matching the first two even moments of \(D\):
\(\mu^* = \mu_t - \mu_c\).
\({\sigma^*}^2\) is obtained from the second-moment equation.
\(\nu^*\) is obtained from the fourth-moment equation.
The approximation requires \(\nu_t > 4\) and \(\nu_c > 4\) for
finite fourth moments. It is exact in the normal limit
(\(\nu \to \infty\)) and works well in practice when \(\nu > 10\).
Because it reduces to a single call to pt(), it is orders of
magnitude faster than the numerical integration method
(ptdiff_NI) and is fully vectorised.
Examples
# P(T_t - T_c > 3) with equal parameters
ptdiff_MM(q = 3, mu_t = 2, mu_c = 0, sd_t = 1, sd_c = 1,
nu_t = 17, nu_c = 17, lower.tail = FALSE)
#> [1] 0.248517
# P(T_t - T_c > 1) with unequal scales
ptdiff_MM(q = 1, mu_t = 5, mu_c = 3, sd_t = 2, sd_c = 1.5,
nu_t = 10, nu_c = 15, lower.tail = FALSE)
#> [1] 0.6483581
# P(T_t - T_c > 0) with different degrees of freedom
ptdiff_MM(q = 0, mu_t = 1, mu_c = 1, sd_t = 1, sd_c = 1,
nu_t = 5, nu_c = 20, lower.tail = FALSE)
#> [1] 0.5
# Lower tail: P(T_t - T_c <= 2)
ptdiff_MM(q = 2, mu_t = 3, mu_c = 0, sd_t = 1.5, sd_c = 1.2,
nu_t = 12, nu_c = 15, lower.tail = TRUE)
#> [1] 0.3097574
# Vectorised usage
ptdiff_MM(q = 1, mu_t = c(2, 3, 4), mu_c = c(0, 1, 2),
sd_t = c(1, 1.2, 1.5), sd_c = c(1, 1.1, 1.3),
nu_t = 10, nu_c = 10, lower.tail = FALSE)
#> [1] 0.7456908 0.7174810 0.6818673