Simulation for hypothesis testing, but trying to be a little bit clever.
Another example of writing down generative models + conditioning.
In particular:
A null hypothesis \(H_0\) specifies a collection of possible distributions for the data.
A hypothesis test with level \(\alpha\) is a rule for rejecting or not rejecting \(H_0\), such that
\[ \Pr(\text{reject }H_0)\leq\alpha \]
under every distribution allowed by \(H_0\).
To construct a hypothesis test:
To get a level-\(\alpha\) test, we need
\[ \Pr(T(D)\in R)\leq\alpha \]
under every distribution allowed by \(H_0\).
To make the test useful, we want \(T(D)\in R\) to be more likely under alternatives we care about.
For example, to compare two groups, we might use
\[ T(X,Y)=|\overline X-\overline Y| \]
and reject when this difference is sufficiently large.
Note that the definition on its own doesn’t give us a useful procedure; it just specifies one property that we want.
Picking a uniform \([0,1]\) random variable independently of the data and rejecting when it comes up \(\leq\alpha\) is also a valid test, but it’s not useful.
Usefulness depends on the alternative and the test statistic.
Suppose we have \(n\) participants. Let \(Z_i\) be participant \(i\)’s outcome without treatment, \(W_i\) indicate whether they receive treatment, and \(Y_i\) be their observed outcome.
Write \(Z=(Z_1,\ldots,Z_n)\), and similarly for \(W\) and \(Y\).
Our model is
\[ Z_1,\ldots,Z_n\overset{\mathrm{iid}}{\sim}P, \]
\[ W\sim Q,\qquad W\perp Z, \]
\[ Y\mid Z,W\sim\text{something else}. \]
Here \(Q\) is the treatment-assignment distribution, controlled by the experimenter.
Our null hypothesis \(H_0\) is \(Y=Z\).
This is called the “sharp null”: treatment doesn’t do anything to anyone.
If we could simulate from \(H_0\):
\[ Z_1^*,\ldots,Z_n^*\overset{\mathrm{iid}}{\sim}P, \]
\[ W^*\sim Q,\qquad W^*\perp Z^*, \]
\[ Y^*=Z^*, \]
our simulations would give us an approximation of the distribution of our statistic \(T(Y^*,W^*)\) under the null.
Once we have an approximation of the null distribution, we can:
How do we choose \(R\)?
Suppose
\[ Z_1,\ldots,Z_n\overset{\mathrm{iid}}{\sim}N(\mu,\sigma^2). \]
The experimenter chooses exactly \(m\) of the \(n\) participants to receive treatment, with every group of \(m\) equally likely, where \(1\leq m<n\):
\[ W\sim\operatorname{Uniform} \left\{w\in\{0,1\}^n:\sum_{i=1}^n w_i=m\right\}. \]
Our test statistic is the absolute difference in group means:
\[ T(Y,W)= \left| \frac{1}{m}\sum_{i:W_i=1}Y_i - \frac{1}{n-m}\sum_{i:W_i=0}Y_i \right|. \]
Large values are evidence against \(H_0\).
alpha <- 0.05
T_null <- replicate(10000, {
Z <- rnorm(n, mu, sigma)
W <- sample(rep(c(1, 0), c(m, n - m)))
Y <- Z
T_stat(Y, W)
})
quantile(T_null, 1 - alpha) # reject when T >= thisThis requires knowing \(\mu\) and \(\sigma\).
Solution: Instead of simulating
\[ Z_1^*,\ldots,Z_n^*\overset{\mathrm{iid}}{\sim}P, \qquad W^*\sim Q, \qquad Y^*=Z^*, \]
we simulate from the distribution of \((Y, W) \mid Y = y_{\text{obs}}\) under the null to get the distribution of \(T(Y^*, W^*) \mid Y^* = y_{\text{obs}}\) under the null.
One way to think about this distribution:
Choose a test statistic \(T\) for which large values are evidence against \(H_0\).
Compute the observed statistic: \[ t_{\mathrm{obs}}=T(y_{\mathrm{obs}},w_{\mathrm{obs}}). \]
For \(b=1,\ldots,B\):
Use the simulated statistics to estimate the \((1-\alpha)\)-quantile \(c\) of the conditional null distribution.
Reject \(H_0\) if \(t_{\mathrm{obs}}>c\).
Discrete case: The overall probability of rejection is a convex combination of the conditional rejection probabilities.
\[ \Pr(\text{reject }H_0) = \sum_y \Pr(\text{reject }H_0\mid Y=y)\Pr(Y=y). \]
Every conditional rejection probability is \(\leq\alpha\), so the whole thing is also \(\leq\alpha\).
The same idea works in the continuous case, with an integral instead of a sum.
An experiment with \(n=20\), \(m=10\), and a true treatment effect of 1 (\(Y=Z+W\)):
T_stat <- function(y, w) abs(mean(y[w == 1]) - mean(y[w == 0]))
n <- 20; m <- 10
z <- rnorm(n)
w_obs <- sample(rep(c(1, 0), c(m, n - m))) # the experiment's assignment
y_obs <- z + 1 * w_obs # observed outcomes
t_obs <- T_stat(y_obs, w_obs)
alpha <- 0.05
## Randomization test: hold y_obs fixed, re-draw W from Q
T_star <- replicate(10000,
T_stat(y_obs, sample(rep(c(1, 0), c(m, n - m)))))
c_hat <- quantile(T_star, 1 - alpha)
t_obs > c_hat # reject?
mean(T_star >= t_obs) # p-valueWith \(\alpha=0.05\): \(t_{\mathrm{obs}}=1.30\), \(\hat c=1.00\): reject \(H_0\). p-value \(0.008\).
Now each participant is treated independently with probability \(1/2\), so \(Q\) is different. Only the lines that draw \(W\) change:
w_obs <- rbinom(n, 1, 0.5)
y_obs <- z + 1 * w_obs
t_obs <- T_stat(y_obs, w_obs)
T_star <- replicate(10000, T_stat(y_obs, rbinom(n, 1, 0.5)))The number treated now varies from draw to draw. If every participant lands in the same group, \(T\) is undefined; set \(T=0\) in that case:
The observed assignment treated 8 participants; the re-drawn assignments treated between 2 and 18.
With \(\alpha=0.05\): \(t_{\mathrm{obs}}=1.21\), \(\hat c=1.00\): reject \(H_0\). p-value \(0.015\).
Suppose
\[ X_1,\ldots,X_n\overset{\mathrm{iid}}{\sim}P, \qquad Y_1,\ldots,Y_m\overset{\mathrm{iid}}{\sim}Q, \]
with the two samples independent.
Write \(X=(X_1,\ldots,X_n)\) and \(Y=(Y_1,\ldots,Y_m)\).
Our null hypothesis is
\[ H_0:\quad P=Q. \]
If we knew the common distribution \(P=Q\), we could simulate under \(H_0\):
\[ X_1^*,\ldots,X_n^*,Y_1^*,\ldots,Y_m^* \overset{\mathrm{iid}}{\sim}P. \]
Compute \(T(X^*,Y^*)\) on simulated replicates, and reject for sufficiently extreme values of the observed statistic.
For example,
\[ T(X,Y)=|\overline X-\overline Y|. \]
We don’t know \(P\).
Let \(S(X,Y)\) be the sorted vector of all \(n+m\) pooled observations, including any repetitions.
We will try to get the sampling distribution of \(T(X^*, Y^*) \mid S(X^*, Y^*) = S(x_{\text{obs}}, y_{\text{obs}})\) under \(H_0\).
Note that under \(H_0\), our model is just \(X_i, Y_j \overset{\mathrm{iid}}{\sim} P\).
As before:
Suppose
\[ Y_1,\ldots,Y_n\overset{\mathrm{iid}}{\sim}P, \]
and let \(S(Y)\) be the sorted vector of values.
We have \(y_{\mathrm{obs}}\).
If we had a way of simulating from \(P\), we could use our rejection sampling algorithm and repeatedly draw \(Y\) until we got
\[ S(Y)=S(y_{\mathrm{obs}}). \]
Two problems: we don’t know \(P\), and even if we did, this could take forever.
Suppose the \(Y_i\) are discrete, with common probability mass function
\[ p(a)=\Pr(Y_i=a). \]
Suppose that \(\tilde y\) is any other element of the set we are conditioning on:
\[ S(\tilde y)=S(y_{\mathrm{obs}}). \]
What does this mean? \(\tilde y\) has the same values as \(y_{\mathrm{obs}}\), the same number of times.
We can write down \(\Pr(Y=\tilde y)\) in terms of the unknown \(p\):
\[ \Pr(Y=\tilde y) = p(\tilde y_1)\cdots p(\tilde y_n). \]
This is the same for every \(\tilde y\) with
\[ S(\tilde y)=S(y_{\mathrm{obs}}), \]
because the product contains the same factors, just in a different order.
Therefore,
\[ \Pr\!\left(Y=\tilde y\mid S(Y)=S(y_{\mathrm{obs}})\right) \propto \begin{cases} 0, & S(\tilde y)\neq S(y_{\mathrm{obs}}),\\ 1, & S(\tilde y)=S(y_{\mathrm{obs}}). \end{cases} \]
So every distinct reordering of \(y_{\mathrm{obs}}\) has the same conditional probability.
This we can simulate from directly: choose a reordering of \(y_{\mathrm{obs}}\), with all distinct reorderings equally likely.
Equivalently, uniformly permute the indices of \(y_{\mathrm{obs}}\).
Choose a test statistic \(T\) for which large values are evidence against \(H_0\).
Compute the observed statistic: \(t_{\mathrm{obs}}=T(x_{\mathrm{obs}},y_{\mathrm{obs}}).\)
Pool the observed values into one vector: \[ v=(x_{\mathrm{obs},1},\ldots,x_{\mathrm{obs},n}, y_{\mathrm{obs},1},\ldots,y_{\mathrm{obs},m}). \]
For \(b=1,\ldots,B\):
Use the simulated statistics to estimate the \((1-\alpha)\)-quantile \(c\) of the conditional null distribution.
Reject \(H_0\) if \(t_{\mathrm{obs}}>c\).
T_stat <- function(x, y) abs(mean(x) - mean(y))
n <- 15; m <- 10
x_obs <- rnorm(n, 0, 1) # observed data (here: simulated,
y_obs <- rnorm(m, 1, 1) # with Q shifted by 1)
t_obs <- T_stat(x_obs, y_obs)
alpha <- 0.05
v <- c(x_obs, y_obs) # pool
T_star <- replicate(10000, {
v_star <- sample(v) # uniformly random reordering
T_stat(v_star[1:n], v_star[(n + 1):(n + m)]) # split
})
c_hat <- quantile(T_star, 1 - alpha)
t_obs > c_hat # reject?
mean(T_star >= t_obs) # p-value
t.test(x_obs, y_obs, var.equal = TRUE)$p.value # for comparison
hist(T_star, breaks = 50)
abline(v = c(c_hat, t_obs), col = c("black", "red"), lty = c(2, 1), lwd = 2)Suppose, just for illustration, that we know \(P=Q=N(0,1)\), with \(n=m=10\).
Then the unconditional null distribution of the signed difference \(D=\overline X-\overline Y\) is \(N(0,\,1/10+1/10)\), standard deviation \(0.447\).
Draw many datasets from \(H_0\) and pick three: pooled standard deviation at the 5th percentile (\(0.72\)), median (\(0.97\)), and 95th percentile (\(1.24\)).
Conditional standard deviation is \(s_{\text{pool}}\sqrt{1/n+1/m}\): \(0.32\), \(0.44\), \(0.56\).
Conditioning fixes the observed values, so the conditional distribution can be narrower or wider than the unconditional one. Averaged over datasets, the rejection rate is still \(\leq\alpha\).
Suppose
\[ (X_1,Y_1),\ldots,(X_n,Y_n) \overset{\mathrm{iid}}{\sim}R, \]
where \(R\) is a joint distribution with marginal distributions \(P\) and \(Q\).
Our null hypothesis is
\[ H_0:\quad X_i\perp Y_i. \]
Under \(H_0\),
\[ X_1,\ldots,X_n\overset{\mathrm{iid}}{\sim}P, \qquad Y_1,\ldots,Y_n\overset{\mathrm{iid}}{\sim}Q, \]
with the entire \(X\) vector independent of the entire \(Y\) vector.
If we knew \(P\) and \(Q\), we could simulate under \(H_0\):
\[ X_1^*,\ldots,X_n^*\overset{\mathrm{iid}}{\sim}P, \qquad Y_1^*,\ldots,Y_n^*\overset{\mathrm{iid}}{\sim}Q, \]
independently.
Compute \(T(X^*,Y^*)\) on simulated replicates, and reject for sufficiently extreme values of the observed statistic.
For example, to look for linear association,
\[ T(X,Y)=|\operatorname{cor}(X,Y)|. \]
We don’t know \(P\) or \(Q\).
Simulate from a conditional distribution.
Let \(S(X), S(Y)\) be the sorted vectors of \(X\) and \(Y\) values, including any repetitions.
We condition on \(S(X)\) and \(S(Y)\).
Under \(H_0\), we simulate from the conditional distribution
\[ \boxed{ (X, Y)\mid S(X)=S(x_{\mathrm{obs}}),\ S(Y)=S(y_{\mathrm{obs}}). } \]
Because \(X\) and \(Y\) are independent under \(H_0\), this has the same distribution as simulating \(X\) and \(Y\) independently from
\[ Y\mid S(Y)=S(y_{\mathrm{obs}}), \quad X\mid S(X)=S(x_{\mathrm{obs}}), \]
These conditional distributions do not depend on \(P\) or \(Q\), for the same reason as before.
Same reasoning as before.
For each \(S(X)\), \(S(Y)\) values, we choose a rejection region with conditional probability at most \(\alpha\).
Every conditional rejection probability is at most \(\alpha\), so the overall rejection probability is also at most \(\alpha\).
Choose a test statistic \(T\) for which large values are evidence against \(H_0\).
Compute the observed statistic: \[ t_{\mathrm{obs}}=T(x_{\mathrm{obs}},y_{\mathrm{obs}}). \]
For \(b=1,\ldots,B\):
Use the simulated statistics to estimate the \((1-\alpha)\)-quantile \(c\) of the conditional null distribution.
Reject \(H_0\) if \(t_{\mathrm{obs}}>c\).
Suppose \(X_i\) and \(Y_i\) each have the mixture distribution
\[ 0.9\,N(0,0.05^2)+0.1\,N(1,0.05^2): \]
usually small, occasionally large. Under \(H_0\), \(X\) and \(Y\) are independent.
Observed data, \(n=10\): one large \(x\) and one large \(y\), and they are in the same pair. \(r=0.99\).
Compare two null distributions of \(r\):
Permutation p-value \(0.012\); unconditional p-value \(0.0008\).
Unconditionally, it is unusual to see exactly one large \(x\) and one large \(y\) in the same pair.
Conditionally on seeing exactly one large \(x\) and one large \(y\), it’s not so unusual for them to be paired.
This is a very old example from David Cox. “Some problem connected with statistical inference,” (1958) Annals of Mathematical Statistics. open pdf here
Suppose our model is:
\[ \begin{aligned} Z &\sim \text{Bern}(.5) \\ X \mid Z &\sim N(\theta, \sigma^2_Z) \end{aligned} \]
Both \(Z\) and \(X\) are observed.
Interpretation is: we flip a coin. If it comes up heads, we get one measurement from a precise instrument, and if it comes up tails, we get one measurement from an imprecise instrument.
We know whether we are using the precise or the imprecise instrument.
Test \(H_0:\theta=0\) with some test statistic \(T\). Given cutoffs \((c_0,c_1)\), apply the rule
\[ \begin{aligned} \text{if } z_{\mathrm{obs}}=0, \text{reject when } T(x_{\mathrm{obs}})>c_0, \\ \text{if } z_{\mathrm{obs}}=1, \text{reject when } T(x_{\mathrm{obs}})>c_1. \end{aligned} \]
Three possibilities for choosing \(c_0, c_1\):
An “unconditional” test, doesn’t take into account our knowledge of \(Z\). (This is not even discussed by Cox because it is such a bad idea.)
A “conditional” test, where we build null distributions conditional on the observed value of \(Z\).
We’re trying to get a valid test that maximizes the probability of rejecting under a certain non-null setup.
There will be many different pairs that satisfy our criteria.
Pick a particular alternative, say \(\theta = 1\).
The first pass ensures that the test is valid (probability at most \(\alpha\) of rejecting under the null). The second pass looks for cutoffs that will allow us to reject the most often under the specified alternative.
Suppose \(\sigma_0 = 1\), \(\sigma_1 = 0.1\), and \(\alpha = 0.05\). A shift of 1 is hard to detect with instrument 0 and easy to detect with instrument 1.
| Cutoffs | False-positive rate, noisy instrument | False-positive rate, precise instrument | Overall power at \(\theta=1\) |
|---|---|---|---|
| Conditional test | \(\alpha\) (5%) | \(\alpha\) (5%) | about 63% |
| Cutoffs chosen for \(\theta=1\) | nearly \(2\alpha\) (10%) | nearly 0% | about 69.5% |
The precise instrument can use a much stricter cutoff and still detect the shift almost every time. A looser cutoff helps the noisy instrument detect the shift more often.