Algorithm Search⌘K

Theoremresearch

Polyhedral lemma

Also called Lee-Sun-Sun-Taylor lemma, exact post-selection inference for the lasso.

The lasso selection event is a polyhedron in y, so a linear contrast of y given the event has a truncated normal law. This gives exact post-selection intervals.

The polyhedral lemma, also called the Lee-Sun-Sun-Taylor lemma, gives the exact law of a linear function η⊤y\eta^\top y of a normal vector yy given that yy fell in a polyhedron {Ay≤d}\{Ay \le d\}. Given the event and the part of yy that is independent of η⊤y\eta^\top y, the law is a normal law truncated to an interval [V−,V+][V^-, V^+] whose ends are computed from the data. The truncated normal CDF at the observed value is then exactly uniform, which gives exact tests and intervals after selection. The lemma is the engine of post-selection inference for the lasso, forward stepwise regression and least angle regression, whose selection events are polyhedra. Its other name, exact post-selection inference for the lasso, comes from the title of the paper of Lee, Sun, Sun and Taylor.

Statement

Let y∈Rny \in \mathbb R^n, A∈Rq×nA \in \mathbb R^{q \times n}, d∈Rqd \in \mathbb R^q and η∈Rn\eta \in \mathbb R^n. (Lee et al. write bb for dd; here bb is kept for regression coefficients.) For θ∈R\theta \in \mathbb R, σ>0\sigma > 0 and a<ea < e in [−∞,∞][-\infty, \infty], let Fθ,σ2[a,e]F^{[a, e]}_{\theta, \sigma^2} be the CDF of the normal law N(θ,σ2)N(\theta, \sigma^2) truncated to [a,e][a, e]:

Fθ,σ2[a,e](x)=Φ(x−θσ)−Φ(a−θσ)Φ(e−θσ)−Φ(a−θσ),a≤x≤e.F^{[a, e]}_{\theta, \sigma^2}(x) = \frac{\Phi\bigl(\frac{x - \theta}{\sigma}\bigr) - \Phi\bigl(\frac{a - \theta}{\sigma}\bigr)}{\Phi\bigl(\frac{e - \theta}{\sigma}\bigr) - \Phi\bigl(\frac{a - \theta}{\sigma}\bigr)}, \qquad a \le x \le e.

Conditions.

  1. y∼N(μ,Σ)y \sim N(\mu, \Sigma) with Σ\Sigma known; Σ\Sigma may be singular.
  2. AA and dd are fixed, and P(Ay≤d)>0P(Ay \le d) > 0.
  3. η\eta is fixed and ση2=η⊤Ση>0\sigma_\eta^2 = \eta^\top\Sigma\eta > 0.

Write t=η⊤yt = \eta^\top y, c=Ση/ση2c = \Sigma\eta/\sigma_\eta^2, z=y−ctz = y - ct and α=Ac\alpha = Ac, and define

V−(z)=max⁡j:αj<0dj−(Az)jαj,V+(z)=min⁡j:αj>0dj−(Az)jαj,V0(z)=min⁡j:αj=0(dj−(Az)j),V^-(z) = \max_{j: \alpha_j < 0}\frac{d_j - (Az)_j}{\alpha_j}, \qquad V^+(z) = \min_{j: \alpha_j > 0}\frac{d_j - (Az)_j}{\alpha_j}, \qquad V^0(z) = \min_{j: \alpha_j = 0}\bigl(d_j - (Az)_j\bigr),

with max⁡∅=−∞\max \emptyset = -\infty and min⁡∅=+∞\min \emptyset = +\infty.

Theorem (Lee, Sun, Sun and Taylor 2016). Under conditions 1 to 3:

  1. {Ay≤d}={V−(z)≤t≤V+(z), V0(z)≥0}\{Ay \le d\} = \{V^-(z) \le t \le V^+(z),\ V^0(z) \ge 0\}.
  2. tt and zz are independent.
  3. For almost every z0z_0 with P(Ay≤d∣z=z0)>0P(Ay \le d \mid z = z_0) > 0, the law of tt given Ay≤dAy \le d and z=z0z = z_0 is N(η⊤μ,ση2)N(\eta^\top\mu, \sigma_\eta^2) truncated to [V−(z0),V+(z0)][V^-(z_0), V^+(z_0)].
  4. The pivot Fη⊤μ,ση2[V−(z),V+(z)](t)F^{[V^-(z), V^+(z)]}_{\eta^\top\mu, \sigma_\eta^2}(t) has the uniform law on (0,1)(0, 1) given Ay≤dAy \le d.

In plain words

Suppose a rule looked at noisy data and chose what to report. Most rules of this kind can be written as a list of straight-line checks on the data, so the data sets that lead to the same choice fill a region with flat sides. Now take one number we want to study, say the effect of the chosen variable. Split the data into this number and the rest, which is unrelated to it. Hold the rest fixed. The flat-sided region then allows the number to move only between a lowest and a highest value. So, given the choice, the number is a bell-curve draw that was forced to land in that range. We know exactly how such a draw behaves, so we can compute an honest p-value and an honest interval that take the choice into account.

Proof

Part 1 (the event on a line). Since y=z+cty = z + ct, Ay≤dAy \le d holds if and only if αjt≤dj−(Az)j\alpha_j t \le d_j - (Az)_j for every row jj. A row with αj>0\alpha_j > 0 says t≤(dj−(Az)j)/αjt \le (d_j - (Az)_j)/\alpha_j; a row with αj<0\alpha_j < 0 says, after division by a negative number, t≥(dj−(Az)j)/αjt \ge (d_j - (Az)_j)/\alpha_j; and a row with αj=0\alpha_j = 0 says dj−(Az)j≥0d_j - (Az)_j \ge 0 and does not involve tt. The rows of the first kind together say t≤V+(z)t \le V^+(z), those of the second kind t≥V−(z)t \ge V^-(z), those of the third kind V0(z)≥0V^0(z) \ge 0. □\square

Part 2 (independence). The pair (z,t)(z, t) is a linear function of the normal vector yy, so it is jointly normal. Its cross covariance is

Cov⁡(z,t)=Cov⁡(y−c η⊤y, η⊤y)=Ση−c η⊤Ση=Ση−Ση=0.\operatorname{Cov}(z, t) = \operatorname{Cov}(y - c\,\eta^\top y,\ \eta^\top y) = \Sigma\eta - c\,\eta^\top\Sigma\eta = \Sigma\eta - \Sigma\eta = 0.

For a jointly normal vector, zero cross covariance between two blocks means independence of the blocks, also when the covariance is singular, because the joint characteristic function then factors. □\square

Part 3 (the truncated law). Write θ=η⊤μ\theta = \eta^\top\mu, E={Ay≤d}E = \{Ay \le d\}, I(z)=[V−(z),V+(z)]I(z) = [V^-(z), V^+(z)] and π(z)=Pθ(T∈I(z))\pi(z) = P_\theta(T \in I(z)) for T∼N(θ,ση2)T \sim N(\theta, \sigma_\eta^2). By Part 1, 1E=1{V0(z)≥0} 1{t∈I(z)}\mathbf 1_E = \mathbf 1\{V^0(z) \ge 0\}\,\mathbf 1\{t \in I(z)\}. The functions V−V^-, V+V^+ and V0V^0 are measurable, being maxima and minima of finitely many affine functions. By Part 2, tt is independent of zz and has the law N(θ,ση2)N(\theta, \sigma_\eta^2), so for every Borel set BB, by the substitution rule for conditional expectations of independent variables,

P(t∈B, E∣z)=1{V0(z)≥0} Pθ(T∈B∩I(z))almost surely.P(t \in B,\ E \mid z) = \mathbf 1\{V^0(z) \ge 0\}\,P_\theta\bigl(T \in B \cap I(z)\bigr) \quad \text{almost surely}.

With B=RB = \mathbb R this gives P(E∣z)=1{V0(z)≥0} π(z)P(E \mid z) = \mathbf 1\{V^0(z) \ge 0\}\,\pi(z). Where this is positive, the ratio of the two is Pθ(T∈B∣T∈I(z))P_\theta(T \in B \mid T \in I(z)), the truncated normal law. □\square

Part 4 (the uniform pivot). Let G=Fθ,ση2I(z)(t)G = F^{I(z)}_{\theta, \sigma_\eta^2}(t) and u∈(0,1)u \in (0, 1). If XX has a continuous CDF FF, then P(F(X)≤u)=uP(F(X) \le u) = u. Apply this to the truncated law of Part 3, which has a continuous CDF, for a fixed zz with V0(z)≥0V^0(z) \ge 0 and π(z)>0\pi(z) > 0:

P(G≤u, E∣z)=π(z) P(Fθ,ση2I(z)(T)≤u∣T∈I(z))=u π(z).P(G \le u,\ E \mid z) = \pi(z)\,P\bigl(F^{I(z)}_{\theta, \sigma_\eta^2}(T) \le u \bigm| T \in I(z)\bigr) = u\,\pi(z).

If V0(z)<0V^0(z) < 0 or π(z)=0\pi(z) = 0, both P(G≤u,E∣z)P(G \le u, E \mid z) and u P(E∣z)u\,P(E \mid z) are 0. So P(G≤u,E∣z)=u P(E∣z)P(G \le u, E \mid z) = u\,P(E \mid z) almost surely. Taking expectations gives P(G≤u,E)=u P(E)P(G \le u, E) = u\,P(E), and dividing by P(E)>0P(E) > 0 gives P(G≤u∣E)=uP(G \le u \mid E) = u. □\square

Consequences

Tests. To test η⊤μ=θ0\eta^\top\mu = \theta_0 against η⊤μ>θ0\eta^\top\mu > \theta_0, reject when 1−Fθ0,ση2[V−,V+](t)≤α1 - F^{[V^-, V^+]}_{\theta_0, \sigma_\eta^2}(t) \le \alpha. By Part 4 the level is exactly α\alpha given the selection event, and so also given the selected model.

Intervals. For a<x<ea < x < e, the map θ↦Fθ,σ2[a,e](x)\theta \mapsto F^{[a, e]}_{\theta, \sigma^2}(x) is strictly decreasing. Proof. The truncated law has density proportional to eθs/σ2e−s2/(2σ2)e^{\theta s/\sigma^2}e^{-s^2/(2\sigma^2)} on [a,e][a, e], a one-parameter exponential family. Differentiating under the integral, ∂θlog⁡Fθ(x)=(Eθ[S∣S≤x]−Eθ[S])/σ2\partial_\theta\log F_\theta(x) = (E_\theta[S \mid S \le x] - E_\theta[S])/\sigma^2 for SS with this law, and the mean of a law with a density, restricted to its part below x<ex < e, is smaller than the full mean. As θ→−∞\theta \to -\infty the law piles up at aa and Fθ(x)→1F_\theta(x) \to 1; as θ→∞\theta \to \infty it piles up at ee, or moves to +∞+\infty if e=∞e = \infty, and Fθ(x)→0F_\theta(x) \to 0. □\square So there are unique L<UL < U with FL(t)=1−α/2F_L(t) = 1 - \alpha/2 and FU(t)=α/2F_U(t) = \alpha/2, and {L≤η⊤μ≤U}={α/2≤Fη⊤μ(t)≤1−α/2}\{L \le \eta^\top\mu \le U\} = \{\alpha/2 \le F_{\eta^\top\mu}(t) \le 1 - \alpha/2\}, which has probability 1−α1 - \alpha given the event by Part 4. A bisection on θ\theta finds LL and UU.

Selection events that are polyhedra. The lemma needs AA, dd and η\eta fixed, so in use one conditions on the event {M^=M}\{\hat M = M\} for the model MM that was selected, and lets η=ηM\eta = \eta_M, for example the row of (XM⊤XM)−1XM⊤(X_M^\top X_M)^{-1}X_M^\top for one coefficient. Then the lemma applies whenever {M^=M}\{\hat M = M\}, or a finer event inside it, is a polyhedron.

  • Largest ∣yj∣|y_j|. Selecting jj with sign ss is the event {syj≥yk, syj≥−yk for k≠j, syj≥0}\{s y_j \ge y_k,\ s y_j \ge -y_k \text{ for } k \ne j,\ s y_j \ge 0\}.
  • The lasso. Let b^\hat b minimize 12∥y−Xb∥2+λ∥b∥1\frac12\|y - Xb\|^2 + \lambda\|b\|_1 with fixed λ>0\lambda > 0; here bb is the coefficient vector. Let EE be the active set, ss its signs, XEX_E of full column rank, XE+=(XE⊤XE)−1XE⊤X_E^+ = (X_E^\top X_E)^{-1}X_E^\top and PE=XEXE+P_E = X_EX_E^+. Up to a set of probability 0, {E^=E,s^=s}\{\hat E = E, \hat s = s\} is the polyhedron of the two conditions
−diag⁡(s)XE+y<−λdiag⁡(s)(XE⊤XE)−1s,∣1λX−E⊤(I−PE)y+X−E⊤(XE+)⊤s∣<1.-\operatorname{diag}(s)X_E^+y < -\lambda\operatorname{diag}(s)(X_E^\top X_E)^{-1}s, \qquad \Bigl|\tfrac1\lambda X_{-E}^\top(I - P_E)y + X_{-E}^\top(X_E^+)^\top s\Bigr| < \mathbf 1.

Proof. By the KKT conditions, b^\hat b solves the problem if and only if X⊤(y−Xb^)=λgX^\top(y - X\hat b) = \lambda g with gj=sign⁡(b^j)g_j = \operatorname{sign}(\hat b_j) where b^j≠0\hat b_j \ne 0 and ∣gj∣≤1|g_j| \le 1 elsewhere. If E^=E\hat E = E and s^=s\hat s = s, the rows in EE give b^E=XE+y−λ(XE⊤XE)−1s\hat b_E = X_E^+y - \lambda(X_E^\top X_E)^{-1}s, so the first condition is diag⁡(s)b^E>0\operatorname{diag}(s)\hat b_E > 0; the rows outside EE give g−Eg_{-E} equal to the vector inside the absolute value, using y−XEb^E=(I−PE)y+λ(XE+)⊤sy - X_E\hat b_E = (I - P_E)y + \lambda(X_E^+)^\top s; and ∣g−E∣=1|g_{-E}| = 1 in some coordinate has probability 0. Conversely, if yy satisfies both conditions, the vector with these b^E\hat b_E and b^−E=0\hat b_{-E} = 0 satisfies the KKT conditions, so it is a solution. All solutions have the same fit Xb^X\hat b, because the loss is strictly convex in XbXb and the objective is convex, so they have the same gg, and ∣gj∣<1|g_j| < 1 forces b^j=0\hat b_j = 0 off EE; since XEX_E has full rank, the solution is unique. □\square

  • Forward stepwise and least angle regression. Each step compares the candidates by inner products with the current residual, and the event "this variable entered with this sign at this step" is again a set of linear inequalities in yy (Tibshirani et al. 2016).

Conditioning only on E^=E\hat E = E, without the signs, gives a union of 2∣E∣2^{|E|} polyhedra; on a line through yy this is a union of intervals, and the same proof gives a normal law truncated to that union, with shorter intervals.

A hand example. Take y∼N(μ,I3)y \sim N(\mu, I_3), the largest-∣yj∣|y_j| rule and the observed y=(2.5,−1.0,0.8)y = (2.5, -1.0, 0.8). The rule picks j=1j = 1 with sign ++, so η=e1\eta = e_1, t=2.5t = 2.5, c=e1c = e_1 and z=(0,−1.0,0.8)z = (0, -1.0, 0.8). Every row of AA has coefficient −1-1 on y1y_1, so αj=−1\alpha_j = -1 for all rows, V+=∞V^+ = \infty, and V−=max⁡(∣y2∣,∣y3∣,0)=1V^- = \max(|y_2|, |y_3|, 0) = 1: given the event and zz, y1y_1 is a normal variable truncated to [1,∞)[1, \infty). The selective p-value for μ1=0\mu_1 = 0 against μ1>0\mu_1 > 0 is Q(2.5)/Q(1)=0.006210/0.158655=0.0391Q(2.5)/Q(1) = 0.006210/0.158655 = 0.0391, where Q=1−ΦQ = 1 - \Phi, against the naive one-sided 0.00620.0062. The 95% interval is [−0.341,4.455][-0.341, 4.455], against the naive [0.540,4.460][0.540, 4.460]. Moving y1y_1 and keeping y2y_2 and y3y_3:

y1y_1 selective 95% interval selective p naive one-sided p
1.05 [−72.74,1.531][-72.74, 1.531] 0.926 0.147
1.2 [−17.29,2.657][-17.29, 2.657] 0.725 0.115
1.5 [−5.994,3.309][-5.994, 3.309] 0.421 0.0668
2.0 [−1.933,3.933][-1.933, 3.933] 0.143 0.0228
2.5 [−0.341,4.455][-0.341, 4.455] 0.0391 0.0062
3.0 [0.630,4.959][0.630, 4.959] 0.0085 0.0013
4.0 [1.963,5.960][1.963, 5.960] 0.0002 0.00003

Far from the edge V−=1V^- = 1 the selective and naive answers agree; near it the data barely passed the selection, and the interval becomes long on the left.

A measured check. With 5 coordinates, 4000 draws (seed 20261114) and the same rule, the Kolmogorov-Smirnov distance of the 4000 pivots from the uniform law was 0.0114 for μ=0\mu = 0 and 0.0161 for μ=(1,0.5,0,0,−2)\mu = (1, 0.5, 0, 0, -2). The naive pivot Φ(t−η⊤μ)\Phi(t - \eta^\top\mu) gave 0.7249 and 0.3593. These distances are measurements of one simulation; the uniformity itself is what Part 4 proves.

Tightness

Normality. Part 2 uses that uncorrelated parts of a normal vector are independent. For other error laws, zz and tt are uncorrelated but not independent, the law of tt given zz is not normal, and the pivot is only approximately uniform. Asymptotic versions need extra conditions, and they are less well understood near the edges of the selection region.

Known covariance. The pivot uses ση2\sigma_\eta^2. With σ2\sigma^2 estimated from the same data, the plug-in pivot is not exactly uniform, and the estimate is not independent of the selection when it uses the residuals of the selected model.

Fixed AA, dd and η\eta. If the analyst chooses η\eta after looking at more than the selection event, for example at tt itself, Part 4 fails.

Length of the intervals. In the table, as y1y_1 moves down to V−=1V^- = 1, the lower end of the interval goes to −∞-\infty. Kivaranovic and Leeb (2021) proved that, in common settings, the expected length of these intervals is infinite. The lemma is exact, but it can be very uninformative.

Power. Conditioning on zz and on the signs uses less of the data than is available. Conditioning on the selected model alone (the union of polyhedra above), or randomizing the selection step (Tian and Taylor 2018), gives shorter intervals with the same exact guarantee.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

polyhedral_lemma.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/polyhedral-lemma/. The authors are listed in prov/TA2HX1.jsonl.
"""The polyhedral lemma of Lee, Sun, Sun and Taylor (2016), and the selective test and interval it gives.

Let y ~ N(mu, Sigma) and let the selection event be a polyhedron {A y <= b}.
For a direction eta set c = Sigma eta / (eta^T Sigma eta) and z = y - c eta^T y.
Then {A y <= b} = {V-(z) <= eta^T y <= V+(z), V0(z) >= 0} with

    alpha = A c,
    V-(z) = max over alpha_j < 0 of (b_j - (A z)_j) / alpha_j,
    V+(z) = min over alpha_j > 0 of (b_j - (A z)_j) / alpha_j,
    V0(z) = min over alpha_j = 0 of  b_j - (A z)_j,

and z is independent of eta^T y. So, given the event and z, eta^T y is normal
with mean eta^T mu and variance eta^T Sigma eta, truncated to [V-, V+], and
its truncated CDF at the observed value is uniform on (0, 1).

The truncated CDF uses log tails, so it stays accurate when both limits are
far out in the same tail (the case that a plain difference of Phi values gets
wrong).
"""

import math

_SQRT2 = math.sqrt(2.0)
_LOG_SQRT_2PI = 0.5 * math.log(2.0 * math.pi)


def log_norm_sf(x):
    """log P(Z > x) for a standard normal Z, accurate in the far upper tail."""
    if x == math.inf:
        return -math.inf
    if x == -math.inf:
        return 0.0
    if x < 30.0:
        return math.log(0.5 * math.erfc(x / _SQRT2))
    # Asymptotic series of the Mills ratio; for x >= 30 the first omitted term is below 3e-14.
    u = 1.0 / (x * x)
    series = -u + 3.0 * u ** 2 - 15.0 * u ** 3 + 105.0 * u ** 4 - 945.0 * u ** 5
    return -0.5 * x * x - math.log(x) - _LOG_SQRT_2PI + math.log1p(series)


def truncated_normal_cdf(x, mu, sd, a, b):
    """P(T <= x) for T normal with mean mu and standard deviation sd, conditioned on a <= T <= b."""
    if not a < b:
        raise ValueError("need a < b")
    if x <= a:
        return 0.0
    if x >= b:
        return 1.0
    za, zb, zx = (a - mu) / sd, (b - mu) / sd, (x - mu) / sd
    if za >= 0.0:
        # Whole interval in the upper tail: (Q(za) - Q(zx)) / (Q(za) - Q(zb)), with ratios of tails.
        la = log_norm_sf(za)
        rx = math.exp(log_norm_sf(zx) - la)
        rb = math.exp(log_norm_sf(zb) - la)
        return (1.0 - rx) / (1.0 - rb)
    if zb <= 0.0:
        # Whole interval in the lower tail: mirror, Phi(t) = Q(-t).
        lb = log_norm_sf(-zb)
        rx = math.exp(log_norm_sf(-zx) - lb)
        ra = math.exp(log_norm_sf(-za) - lb)
        return (rx - ra) / (1.0 - ra)
    lo = 0.5 * math.erfc(-za / _SQRT2)
    hi = 0.5 * math.erfc(-zb / _SQRT2)
    mid = 0.5 * math.erfc(-zx / _SQRT2)
    return (mid - lo) / (hi - lo)


def truncation_limits(a_mat, b_vec, eta, sigma, y):
    """(V-, V+, V0, eta^T y, eta^T Sigma eta) for the event {A y <= b} and the direction eta."""
    n = len(y)
    s_eta = [math.fsum(sigma[i][k] * eta[k] for k in range(n)) for i in range(n)]
    var = math.fsum(e * s for e, s in zip(eta, s_eta))
    if not var > 0.0:
        raise ValueError("eta^T Sigma eta must be positive")
    c = [s / var for s in s_eta]
    t = math.fsum(e * v for e, v in zip(eta, y))
    z = [v - ci * t for v, ci in zip(y, c)]
    vminus, vplus, v0 = -math.inf, math.inf, math.inf
    for row, bj in zip(a_mat, b_vec):
        alpha = math.fsum(r * ci for r, ci in zip(row, c))
        rest = bj - math.fsum(r * zi for r, zi in zip(row, z))
        if alpha > 0.0:
            vplus = min(vplus, rest / alpha)
        elif alpha < 0.0:
            vminus = max(vminus, rest / alpha)
        else:
            v0 = min(v0, rest)
    return vminus, vplus, v0, t, var


def max_abs_selection(y):
    """The index j and sign s of the largest |y_j|, and the polyhedron {A y <= 0} of that event."""
    p = len(y)
    j = max(range(p), key=lambda k: abs(y[k]))
    s = 1.0 if y[j] >= 0.0 else -1.0
    rows = []
    for k in range(p):
        if k == j:
            continue
        for sign in (1.0, -1.0):
            row = [0.0] * p
            row[j] = -s          # -s y_j + sign * y_k <= 0, that is s y_j >= sign * y_k
            row[k] = sign
            rows.append(row)
    row = [0.0] * p
    row[j] = -s
    rows.append(row)            # s y_j >= 0
    return j, s, rows, [0.0] * len(rows)


def selective_pivot(a_mat, b_vec, eta, sigma, y, mu_eta=0.0):
    """The truncated normal CDF of eta^T y at mean mu_eta; uniform on (0, 1) given the event when mu_eta is true."""
    vm, vp, v0, t, var = truncation_limits(a_mat, b_vec, eta, sigma, y)
    if v0 < 0.0 or not vm <= t <= vp:
        raise ValueError("y is not in the selection event")
    return truncated_normal_cdf(t, mu_eta, math.sqrt(var), vm, vp)


def selective_p_value(a_mat, b_vec, eta, sigma, y):
    """One-sided p-value for H0: eta^T mu = 0 against eta^T mu > 0, valid given the selection event."""
    return 1.0 - selective_pivot(a_mat, b_vec, eta, sigma, y, 0.0)


def selective_interval(a_mat, b_vec, eta, sigma, y, level=0.95):
    """The equal-tailed interval for eta^T mu by inverting the pivot, which decreases in the mean."""
    vm, vp, v0, t, var = truncation_limits(a_mat, b_vec, eta, sigma, y)
    sd = math.sqrt(var)
    alpha = 1.0 - level

    def root(target):
        lo, hi = t - sd, t + sd
        while truncated_normal_cdf(t, lo, sd, vm, vp) < target:
            lo -= 2.0 * (hi - lo)
        while truncated_normal_cdf(t, hi, sd, vm, vp) > target:
            hi += 2.0 * (hi - lo)
        for _ in range(200):
            mid = 0.5 * (lo + hi)
            if truncated_normal_cdf(t, mid, sd, vm, vp) > target:
                lo = mid
            else:
                hi = mid
        return 0.5 * (lo + hi)

    return root(1.0 - alpha / 2.0), root(alpha / 2.0)
test_polyhedral_lemma.py 111 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/polyhedral-lemma/. The authors are listed in prov/TA2HX1.jsonl.

import math
import random

import pytest

from polyhedral_lemma import (
    log_norm_sf, max_abs_selection, selective_interval, selective_p_value, selective_pivot,
    truncated_normal_cdf, truncation_limits,
)


def eye(p):
    return [[1.0 if i == j else 0.0 for j in range(p)] for i in range(p)]


def ks_uniform(values):
    v = sorted(values)
    n = len(v)
    return max(max((i + 1) / n - u, u - i / n) for i, u in enumerate(v))


def test_log_tail_is_continuous_and_accurate():
    for x in (0.0, 1.0, 5.0, 20.0, 29.0):
        assert math.isclose(log_norm_sf(x), math.log(0.5 * math.erfc(x / math.sqrt(2.0))), rel_tol=1e-13)
    left = math.log(0.5 * math.erfc(30.0 / math.sqrt(2.0)))
    assert math.isclose(log_norm_sf(30.0), left, rel_tol=1e-13)
    assert math.isclose(log_norm_sf(30.0 + 1e-9), left, rel_tol=1e-9)
    assert log_norm_sf(1e4) < -4.9e7


def test_truncated_cdf_far_in_the_tail():
    # Mean 0, truncated to [40, inf): T - 40 is close to exponential with rate 40.
    for d in (0.01, 0.05, 0.1):
        f = truncated_normal_cdf(40.0 + d, 0.0, 1.0, 40.0, math.inf)
        assert abs(f - (1.0 - math.exp(-40.0 * d - d * d / 2.0) * 40.0 / (40.0 + d))) < 2e-3
    assert truncated_normal_cdf(-40.05, 0.0, 1.0, -math.inf, -40.0) == pytest.approx(
        1.0 - truncated_normal_cdf(40.05, 0.0, 1.0, 40.0, math.inf), abs=1e-12)
    # Inside the bulk it is the plain formula.
    phi = lambda t: 0.5 * math.erfc(-t / math.sqrt(2.0))
    assert truncated_normal_cdf(0.3, 0.0, 1.0, -1.0, 2.0) == pytest.approx((phi(0.3) - phi(-1.0)) / (phi(2.0) - phi(-1.0)), rel=1e-12)


def test_limits_describe_the_event_on_the_line():
    rng = random.Random(20261114)
    for _ in range(200):
        p = 4
        a = [[rng.uniform(-1, 1) for _ in range(p)] for _ in range(6)]
        b = [rng.uniform(0.5, 2.0) for _ in range(6)]
        m = [[rng.uniform(-1, 1) for _ in range(p)] for _ in range(p)]
        sigma = [[math.fsum(m[i][k] * m[j][k] for k in range(p)) + (1.0 if i == j else 0.0) for j in range(p)] for i in range(p)]
        eta = [rng.uniform(-1, 1) for _ in range(p)]
        y = [0.0] * p  # A 0 = 0 <= b, so y is inside the polyhedron
        vm, vp, v0, t, var = truncation_limits(a, b, eta, sigma, y)
        assert v0 >= 0.0 and vm <= t <= vp
        s_eta = [math.fsum(sigma[i][k] * eta[k] for k in range(p)) for i in range(p)]
        c = [s / var for s in s_eta]
        z = [yi - ci * t for yi, ci in zip(y, c)]
        for s in [vm - 0.01, vm + 0.01, vp - 0.01, vp + 0.01, t]:
            if not math.isfinite(s):
                continue
            point = [zi + ci * s for zi, ci in zip(z, c)]
            inside = all(math.fsum(r * q for r, q in zip(row, point)) <= bj + 1e-12 for row, bj in zip(a, b))
            assert inside == (vm <= s <= vp)


def test_one_step_selection_hand_example():
    y = [2.5, -1.0, 0.8]
    j, s, a, b = max_abs_selection(y)
    assert (j, s) == (0, 1.0)
    eta = [1.0, 0.0, 0.0]
    vm, vp, v0, t, var = truncation_limits(a, b, eta, eye(3), y)
    assert (vm, vp, t) == (1.0, math.inf, 2.5)
    q = lambda x: 0.5 * math.erfc(x / math.sqrt(2.0))
    assert selective_p_value(a, b, eta, eye(3), y) == pytest.approx(q(2.5) / q(1.0), rel=1e-12)
    lo, hi = selective_interval(a, b, eta, eye(3), y)
    assert lo == pytest.approx(-0.34087, abs=1e-4)
    assert hi == pytest.approx(4.45540, abs=1e-4)
    assert lo < 2.5 - 1.96  # the selective interval is wider than the naive one on the left
    assert selective_pivot(a, b, eta, eye(3), y, lo) == pytest.approx(0.975, abs=1e-9)
    assert selective_pivot(a, b, eta, eye(3), y, hi) == pytest.approx(0.025, abs=1e-9)


def test_pivot_is_uniform_after_selection_monte_carlo():
    # One-step selection of the largest |y_j| among p = 5, then inference on the selected mean.
    # Tolerance: with 4000 draws the Kolmogorov-Smirnov distance of a true uniform sample exceeds
    # 0.0258 with probability about 0.01, so 0.03 is a safe bound for the fixed seed.
    rng = random.Random(20261114)
    p, reps = 5, 4000
    for mu in ([0.0] * p, [1.0, 0.5, 0.0, 0.0, -2.0]):
        pivots, naive = [], []
        for _ in range(reps):
            y = [m + rng.gauss(0.0, 1.0) for m in mu]
            j, s, a, b = max_abs_selection(y)
            eta = [0.0] * p
            eta[j] = s
            pivots.append(selective_pivot(a, b, eta, eye(p), y, s * mu[j]))
            naive.append(0.5 * math.erfc(-(s * y[j] - s * mu[j]) / math.sqrt(2.0)))
        assert ks_uniform(pivots) < 0.03
        assert ks_uniform(naive) > 0.15  # the pivot that ignores selection is far from uniform


def test_event_check():
    y = [2.5, -1.0, 0.8]
    _, _, a, b = max_abs_selection(y)
    with pytest.raises(ValueError):
        selective_pivot(a, b, [1.0, 0.0, 0.0], eye(3), [0.1, 3.0, 0.0])
    with pytest.raises(ValueError):
        truncated_normal_cdf(0.0, 0.0, 1.0, 1.0, 1.0)

History

Lockhart, Taylor, Tibshirani and Tibshirani (2014) gave the covariance test, an asymptotic test for variables entering along the lasso path. Lee, Sun, Sun and Taylor posted the exact result in 2013 and published it in the Annals of Statistics in 2016, with the lasso as the main application. Tibshirani, Taylor, Lockhart and Tibshirani (2016) used the same lemma for forward stepwise regression and least angle regression, and Taylor and Tibshirani (2015) placed it in a general framework of selective inference. Later work by Tian and Taylor (2018) and others added randomization for power, and Kivaranovic and Leeb (2021) studied the length of the intervals.

References

  1. Jason D. Lee, Dennis L. Sun, Yuekai Sun and Jonathan E. Taylor, Exact post-selection inference, with application to the lasso, Annals of Statistics 44(3), 2016, pp. 907 to 927, Sections 4 and 5 (the lasso selection event as a polyhedron, the polyhedral lemma and the truncated Gaussian pivot)
  2. Ryan J. Tibshirani, Jonathan Taylor, Richard Lockhart and Robert Tibshirani, Exact post-selection inference for sequential regression procedures, Journal of the American Statistical Association 111(514), 2016, pp. 600 to 620, Sections 2 to 4 (the polyhedral form of forward stepwise and least angle regression events, the pivot for general Sigma)
  3. Jonathan Taylor and Robert J. Tibshirani, Statistical learning and selective inference, Proceedings of the National Academy of Sciences 112(25), 2015, pp. 7629 to 7634, The whole paper (the lemma in the setting of selective inference)
  4. Danijel Kivaranovic and Hannes Leeb, On the length of post-model-selection confidence intervals conditional on polyhedral constraints, Journal of the American Statistical Association 116(534), 2021, pp. 845 to 857, The main theorems (infinite expected length of the intervals from the lemma)
  5. Xiaoying Tian and Jonathan Taylor, Selective inference with a randomized response, Annals of Statistics 46(2), 2018, pp. 679 to 710, The whole paper (randomized selection, which leaves more information for the inference than the lemma does)
  6. Richard Lockhart, Jonathan Taylor, Ryan J. Tibshirani and Robert Tibshirani, A significance test for the lasso, Annals of Statistics 42(2), 2014, pp. 413 to 468, The whole paper (the covariance test, an asymptotic test along the lasso path that came before the exact lemma)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/TA2HX1/text.yaml