Skip to content

Fix ranktest: build b_qq from the k-th right singular vector - #28

Merged
eloualiche merged 1 commit into
FixedEffects:mainfrom
paul-huebner:fix-ranktest-right-singular-vector
Oct 30, 2026
Merged

eloualiche merged 1 commit into
FixedEffects:mainfrom
paul-huebner:fix-ranktest-right-singular-vector

Conversation

@paul-huebner

Copy link
Copy Markdown
Contributor

The problem

In ranktest!, b_qq is built from vt[1:k, k]:

b_qq = iszero(vt_sub) ? vt[1:k, k]' : sqrt(vt_sub * vt_sub') * (vt_sub' \ vt[1:k, k]')

vt is V', so vt[1:k, k] is the k-th column of V', which is the k-th row of V. Kleibergen and Paap (2006, p. 102) build B_q⊥ from [V12; V22], the last k − q columns of V. For q = k − 1 that is the k-th column of V, the right singular vector of the smallest singular value, which is vt[k, 1:k].

Stata's ranktest does the same thing. The header of ranktest.jl says this file corresponds to it. In ranktest.ado (v2.0.04), m_svd is called with kpvt', which is V, and computes

bq = v22h*luqrinv(v22')*(v12 \ v22)'

In Mata, (v12 \ v22) stacks the two blocks vertically, so it is the k-th column of V. The a_qq side already matches Stata; only b_qq is transposed.

Effect: with one endogenous regressor, vt is 1×1 and nothing changes. With two or more, the KP rk statistic is wrong whenever V is not symmetric. This includes FixedEffectModels' F_kp.

  • The statistic depends on the order in which the endogenous variables are listed, which a rank test cannot do.
  • With simple() it understates the statistic. With robust or clustered covariance it can go either way.

Example (FixedEffectModels v2.1.0)

using FixedEffectModels, DataFrames, Random
rng = MersenneTwister(1)
n = 500
Z = randn(rng, n, 3)
V = randn(rng, n, 3) * [1.0 0.5 0.0; 0.0 1.0 0.5; 0.0 0.0 1.0]
X = Z * [0.3 0.1 0.0; 0.0 0.3 0.1; 0.1 0.0 0.3] .+ V
y = X * ones(3) .+ V * fill(0.5, 3) .+ randn(rng, n)
df = DataFrame(y = y, x1 = X[:, 1], x2 = X[:, 2], x3 = X[:, 3], z1 = Z[:, 1], z2 = Z[:, 2], z3 = Z[:, 3])
for f in (@formula(y ~ (x1 + x2 + x3 ~ z1 + z2 + z3)),
          @formula(y ~ (x2 + x3 + x1 ~ z1 + z2 + z3)),
          @formula(y ~ (x3 + x1 + x2 ~ z1 + z2 + z3)))
    println(f, "  simple: ", reg(df, f).F_kp, "  robust: ", reg(df, f, Vcov.robust()).F_kp)
end
Endogenous order F_kp simple, Vcov 0.8.3 F_kp robust, Vcov 0.8.3 F_kp simple, this PR F_kp robust, this PR
x1, x2, x3 3.070 3.296 4.421 4.829
x2, x3, x1 2.436 2.523 4.421 4.829
x3, x1, x2 1.640 1.627 4.421 4.829

With the fix, the homoskedastic statistic equals n * s_k^2, where s_k is the smallest singular value of theta. This is the Cragg–Donald form that KP reduces to under i.i.d. errors.

Changes

  • src/ranktest.jl: vt[1:k, k]' becomes vt[k, 1:k]', in both branches of the b_qq line, plus a comment.
  • test/estimators.jl: new tests for k = l = 3 and for k = 2, l = 4. All six fail on main and pass with the fix. They check that:
    • with simple(), the statistic equals n * s_k^2;
    • the statistic is invariant to permuting the endogenous variables, under simple() and under robust().

🤖 Generated with Claude Code

b_qq used vt[1:k, k], the k-th column of Vt, i.e. the k-th row of V. Kleibergen
and Paap (2006, p. 102) and Stata's ranktest (m_svd: bq = v22h*luqrinv(v22')*(v12 \ v22)')
use the k-th right singular vector, the k-th column of V = vt[k, 1:k]. With more than one
endogenous regressor, the KP rk statistic (and FixedEffectModels' F_kp) was therefore
wrong and depended on the order of the endogenous variables. With one endogenous
regressor vt is 1x1 and nothing changes.

Tests: with simple covariance the statistic equals n * s_k^2 (smallest singular value of
theta), and it is invariant to permuting the endogenous variables (simple and robust).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eloualiche

Copy link
Copy Markdown
Member

@matthieugomez?
Seems fine to me.
I also think it would be nice to have a list of tests that check parity with the stata package.

@matthieugomez

Copy link
Copy Markdown
Member

LGTM!

@eloualiche

Copy link
Copy Markdown
Member

Thanks @paul-huebner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants