The GPK test is motivated by the observation that the MMD test performs poorly for detecting differences in variances. The unbiased MMD\(^2\) estimator for a given kernel function \(k\) can be written as
$$\text{MMD}_u^2 = \alpha + \beta - 2\gamma, \text{ where}$$
$$\alpha = \frac{1}{n_1^2 - n_1}\sum_{i=1}^{n_1}\sum_{j=1, j\ne i}^{n_1} k(X_{1i}, X_{1j}),$$
$$\beta = \frac{1}{n_2^2 - n_2}\sum_{i=1}^{n_2}\sum_{j=1, j\ne i}^{n_2} k(X_{2i}, X_{2j}),$$
$$\gamma = \frac{1}{n_1 n_2}\sum_{i=1}^{n_1}\sum_{j=1}^{n_2} k(X_{1i}, X_{2j}).$$
The GPK test statistic is defined as
$$\text{GPK} = (\alpha - \text{E}(\alpha), \beta - \text{E}(\beta))\Sigma^{-1} \binom{\alpha - \text{E}(\alpha)}{\beta - \text{E}(\beta)}$$
$$= Z_{W,1}^2 + Z_D^2\text{ with}$$
$$Z_{W,r} = \frac{W_r - \text{E}(W_r)}{\sqrt{\text{Var}(W_r)}}, W_r = r\frac{n_1 \alpha}{n_1 + n_2}, $$
$$Z_D = \frac{D - \text{E}(D)}{\sqrt{\text{Var}(D)}}, D = n_1(n_1 - 1)\alpha - n_2(n_2 - 1)\beta,$$
where the expectations are calculated under the null and \(\Sigma\) is the covariance matrix of \(\alpha\) and \(\beta\) under the null.
The asymptotic null distribution for GPK is unknown. Therefore, only a permutation test can be performed.
For \(r \ne 1\), the asymptotic null distribution of \(Z_{W,r}\) is normal, but for \(r\) further away from 1, the test performance decreases. Therefore, \(r_1 = 1.2\) and \(r_2 = 0.8\) are proposed as a compromise.
For the fast GPK test, three (asymptotic or permutation) tests based on \(Z_{W, r1}\), \(Z_{W, r2}\) and \(Z_{D}\) are concucted and the overall p value is calculated as 3 times the minimum of the three p values.
For the fast MMD test, only the two asymptotic tests based on \(Z_{W, r1}\), \(Z_{W, r2}\) are used and the p value is 2 times the minimum of the two p values. This is an approximation of the MMD-permutation test, see MMD
.
This implementation is a wrapper function around the function kertests
that modifies the in- and output of that function to match the other functions provided in this package. For more details see the kertests
.
findSigma
finds the optimal bandwidth parameter of the kernel function using the median heuristic and is a wrapper around med_sigma
.