*! version 1.0.0 19aug2026

Title

xttvpivmg methods -- Methods and formulas for the TVP-IV-MG estimator

This page documents the econometrics behind xttvpivmg and maps every block
of the implementation onto a numbered result in

Bai, Y., M. Marcellino and G. Kapetanios (2026), "Mean group instrumental variable
estimation of time-varying large heterogeneous panels with endogenous regressors",
Econometrics and Statistics 37, 26-41 -- referred to below as BMK.

Notation follows the paper. Equation numbers in parentheses are BMK's own.

Model

For i = 1,...,N and t = 1,...,T,

y_it = x_it' b_it + u_it (1)

x_it = P_it' z_it + v_it (2)

with x_it a k x 1 vector of regressors (one element being a constant), z_it a
p x 1 vector of instruments with p >= k, P_it the p x k first-stage coefficient
matrix, and v_it a k x 1 error vector. The regressors are correlated with u_it;
the instruments are not, but are correlated with x_it.

The coefficients follow the random-coefficient decomposition (13):

b_it = b0(t) + e_it (13)

where b0(t) = E(b_it) is a deterministic sequence of cross-sectional mean
coefficients -- the estimand -- and e_it is a mean-zero random
deviation. Both components are restricted only by smoothness, not by any
parametric form.

This nests the standard random-coefficient panel model of Hsiao and Pesaran (2008)
as the special case b0(t) = b0, e_it = e_i (Remark 2), and extends Chen and Huang
(2018) by allowing both cross-sectional heterogeneity and endogenous regressors.

Assumptions

2.1 Moment bounds of order theta > 8 on x, u, z, v; strong mixing with
geometrically decaying coefficients; the matrix Szz(t) = E(z_it z_it') has
uniformly bounded inverse. Serial correlation and conditional heteroskedasticity of
unknown form are permitted.

2.2 Cross-sectional independence of (x_i, z_i, u_i, v_i, e_i, P_i)
across i. This is the binding restriction: there is no common-factor or CCE variant
of this estimator.

2.3 Elements of P_it satisfy a Holder smoothness condition with
exponent g1 and have thin-tailed distributions.

2.4 (i) b0(t) is deterministic; (ii) b0(t) is Holder continuous with
exponent g2, i.e. b0(t) = g(t/T) for a Holder-g2 function g; (iii) the random part
e_it is smooth with exponent g3 and thin-tailed. Condition (ii) is strictly weaker
than the twice-differentiability required by Chen and Huang (2018), which is the
case g2 = 1.

2.5 E(u_it|z_it) = 0 and E(v_it|z_it) = 0; the matrix
P_it' Szz(t) P_it has uniformly bounded inverse (identification / rank).

2.6 H = o(L / (log T)^max(1,2/a)) and L = o(T^g1). Note the ordering:
H must be of smaller order than L. See open
choice 4.

The scaled random walk X(t) = xi(t)/sqrt(t), with xi a random walk with N(0,1)
innovations, satisfies 2.3 and 2.4 with g = 1/2. This is the class used in BMK's
Monte Carlo and it is what xttvpivmg_example.do simulates.

The estimator

Kernel weights (5). With bandwidths H (second stage) and L (first stage),

b_j,t(H) = K(|j-t|/H) and b_j,t(L) = K(|j-t|/L) (5)

K(.) is non-negative and continuous, with bounded or unbounded support, satisfying
the tail condition (7): |K(x)| <= C(1+x^v)^(-1) with v >= 2. Implemented kernels:

Gaussian K(x) = exp(-x^2/2)

Epanechnikov K(x) = 0.75(1-x^2) I{|x|<=1}

rectangle / uniform K(x) = 0.5 I{|x|<=1}

exponential K(x) = exp(-c x^a), c > 0, a > 0

The estimator is a ratio of two kernel-weighted sums, so any multiplicative
constant in K cancels exactly. The 0.5 and 0.75 above are therefore numerically
irrelevant and are retained only for fidelity to the paper's definitions.

Both bandwidths must satisfy (6): c1*T^(1/(theta/4-1)+d) <= H, L <= c2*T^(1-d).

First stage (4). For each unit i and each evaluation point t, a
kernel-weighted local-constant projection of x on z:

Phat_it = [ sum_j b_j,t(L) z_ij z_ij' ]^(-1) [ sum_j b_j,t(L) z_ij x_ij' ] (4)

This is a p x p solve followed by a p x k multiply, repeated for every (i,t).

Second stage (3). The time-varying IV estimator for unit i at date t:

bhat^IV_it = [ sum_j b_j,t(H) Phat_ij' z_ij x_ij' ]^(-1) [ sum_j b_j,t(H) Phat_ij' z_ij y_ij ] (3)

Mean group (8). The estimator of the cross-sectional mean path:

bhat_MG(t) = (1/N) sum_i bhat^IV_it (8)

which generalises the OLS-type mean-group estimator of Pesaran and Smith (1995)
from a constant mean coefficient to a time-varying mean path.

Exogenous regressors and dynamics (Remark 4). Weakly exogenous regressors g_it
satisfying E(u_it|g_it) = 0 are accommodated by augmenting the instrument block
rather than the instrument list:

bhat^IV_it = [ sum_j b_j,t(H) [Phat_ij'z_ij : g_ij] x_ij' ]^(-1) [ sum_j b_j,t(H) [Phat_ij'z_ij : g_ij] y_ij ]

This is what admits dynamic panel models with y_i,t-1 as a regressor, following
Hsiao, Pesaran and Tahmiscioglu (1999).

Fixed effects (Remark 1). Two equivalent treatments. (a) Set x_1,it = 1; then
b_1,it is itself a time-varying fixed effect. (b) Write y_it = a_i + x_it'b_it +
u_it (or a_it), regress y and x separately on a constant, and run the model on the
residuals. The pre-step may be time-invariant or itself kernel-smoothed, and BMK
show it does not affect the asymptotic analysis.

Asymptotic theory (Theorem 1)

Define r(T,H,g) = (H/T)^g + sqrt(log T / H) and
r(T,H,g,a) = sqrt(log T / H) + (H/T)^g (log T)^(1/a) (16).

(i) Uniform consistency. As (N,T) -> infinity,

max_t ||bhat_MG(t) - b0(t)|| = Op( r(T,L,g1,a) + (log T)^(1/a) r(T,H,g2) / sqrt(N)
+ (log T)^(1/a) (H/T)^g3 / sqrt(N) + (log T)^(2/a) / sqrt(N) ).

(ii) Pointwise asymptotic normality. If (H/T)^g2 = o(N^(-1/2)), then for
t = floor(Tr) with 0 < r < 1,

sqrt(N) Sigma_e(t)^(-1/2) ( bhat_MG(t) - b0(t) ) -> N(0, I_k)

where Sigma_e(t) = lim Var( (1/sqrt(N)) sum_i e_it ), consistently estimated by

Sigmahat_e(t) = (1/N) sum_i ( bhat^IV_it - bhat_MG(t) )( bhat^IV_it - bhat_MG(t) )' (17)

Why the variance is so simple. Appendix B shows the expansion collapses to

sqrt(N) ( bhat_MG(t) - b0(t) ) = (1/sqrt(N)) sum_i e_it + op(1)

The kernel-IV sampling error is Op(1/sqrt(NH)) and the smoothing bias is
Op((H/T)^g2); both are dominated by the Op(1/sqrt(N)) heterogeneity term. Hence
no HAC estimator, no long-run variance and no bootstrap are required --
the standard error is purely the cross-sectional dispersion of the N unit paths,
exactly as in a conventional mean-group estimator.

Correlated random coefficients. No dependence assumption between e_it and
x_it is used. The key ingredient is the smoothness condition (15) on e_it, not an
orthogonality condition. So unlike the standard random-coefficient MG estimator,
which is inconsistent when E(e_i|x_it) is non-zero, TVP-IV-MG remains valid. BMK
build this case into their Monte Carlo deliberately.

The (N,T) regime. The normality condition (H/T)^g2 = o(N^(-1/2)) means
sqrt(N)/T^(g2(1-h)) -> 0 with H = T^h. With g2 = 1/2 and h = 1/2 this is
sqrt(N)/T^0.25 -> 0: T must diverge faster than N. BMK's Tables 1-3 show
coverage rising in T but falling in N for fixed T, which is this condition
biting.

Bandwidth selection (section 2.3)

Rule of thumb. H = T^h for 0 < h < 1. Giraitis, Kapetanios and Yates (2018)
find h = 1/2 best in finite samples; BMK use H = L = T^0.5 as their benchmark.

Leave-one-unit-out cross-validation. Adapted from Sun, Carroll and Li (2009).
Remove unit i and form

bhat^(-i)_MG(t) = (1/N) sum_{j != i} bhat^IV_j(t)

then choose (H,L) to minimise

sum_i sum_t ( y_it - x_it' bhat^(-i)_MG(t) )^2

The search is over a two-dimensional grid of exponents T^b. BMK's Monte Carlo uses
b in {0.3,0.4,0.5,0.6,0.7}; their empirical application uses
b in {0.30,0.35,...,0.85} and selects H = T^0.65, L = T^0.30.

Implementation note. The leave-one-out average requires no re-estimation:
bhat^(-i)_MG(t) = ( sum_j bhat_j(t) - bhat_i(t) ) / N. One estimation pass per
(H,L) pair therefore suffices, followed by N cheap subtractions.

Step-to-equation map

Each row names a block of xttvpivmg.ado and the result it implements.

Code Implements

_tvpiv_kgen() kernel K(.) and the tail condition (5), (7)

_tvpiv_wt() the weights b_j,t = K(|j-t|/bw) for fixed t (5)

_tvpiv_unit() first loop first stage Phat_it (4)

... assignment to Xh the projection xhat_ij = Phat_ij' z_ij (3), Remark 4

_tvpiv_unit(), second loop second stage bhat^IV_it (3)

_tvpiv_all() loop over units; the dmode branches are Remark 1

_tvpiv_tvdemean() Remark 1(b) with a time-varying fixed effect

_tvpiv_mg() mean-group average (8)

_tvpiv_cvobj() leave-one-unit-out CV objective (section 2.3)

variance loop in xttvpivmg_work() Sigmahat_e(t) and Var(bhat_MG(t)) (17), Thm 1(ii)

trimming block the interior-point restriction of Thm 1(ii); the window
t = H+1,...,T-H of section 3

xttvpivmg_graph Figure 1: path, pointwise band, zero line

Faithfulness notes

Points at which a plausible-looking shortcut would not reproduce the paper.
Each was checked explicitly against the text and against the independent gretl
implementation ketvals.

1. Phat is evaluated at the summation index j, not at t. Equation (3) reads
Phat_ij' z_ij inside a sum over j. Using Phat_it' z_ij instead -- the
natural mistake, since t is the evaluation point -- gives an estimator that
is only asymptotically equivalent and carries finite-sample contamination. The
implementation computes Phat for every j first, then forms the row-wise fitted
values. This matches the xfithat construction in ketvals.

2. The second-stage matrix is not symmetric. A(t) = sum_j b_j,t xhat_ij x_ij'
has xhat on the left and x on the right. It must be inverted with an LU solve;
using a symmetric-matrix routine such as invsym() would silently symmetrise
it and change the estimates. The first-stage Szz is symmetric and does use
invsym(), which additionally supplies a generalized inverse under benign
collinearity.

3. The kernel constant cancels, the bandwidth does not. K enters only through
ratios, so scale factors are irrelevant; but H appears inside K(|j-t|/H) and is
therefore fully consequential. The Gaussian kernel is not truncated at |j-t|
<= H: it has unbounded support and every observation enters at every date.

4. The estimator is two-sided. b_j,t(H) uses j both below and above t. These
are smoothed, not filtered, estimates and are not available in real time.

5. The variance divisor is N^2, not N(N-1). Equation (17) defines Sigmahat_e
with divisor N, and Theorem 1(ii) then divides by N again. The conventional
Pesaran-Smith mean-group variance uses N(N-1). The default here is the paper's;
vce(mg) gives the convention.

6. Trimming is not cosmetic. Theorem 1(ii) holds at interior points
t = floor(Tr), 0 < r < 1. At the boundary the kernel becomes one-sided and both
bias and variance change. BMK compute all reported statistics on
t = H+1,...,T-H, and so does this command by default.

7. The constant belongs in both x and z. Equation (1) states that one element
of x_it is a constant, and BMK's empirical instrument set begins with an intercept.
Both are added automatically unless noconstant is given.

8. Exogenous regressors instrument themselves. Projecting an exogenous
regressor on an instrument set that contains it returns it exactly (the local
weighted regression fits perfectly). Skipping that projection is therefore an exact
algebraic simplification, not an approximation, and is precisely Remark 4.

Departures and open choices

Places where BMK are silent or internally inconsistent, and what this command does.
None of these is hidden; each is a documented decision.

1. CV summation range. Section 2.3 writes the objective as a sum over
t = 1,...,T, but every reported statistic in section 3 is computed on the trimmed
interior. Untrimmed cross-validation lets boundary bias drive the bandwidth choice.
Default: trim the CV objective too. Override: nocvtrim.

2. Leave-one-out divisor. BMK print (1/N) sum_{j!=i}, which sums
N-1 terms but divides by N. Sun, Carroll and Li (2009), from which the procedure is
adapted, use 1/(N-1). Default: the paper's 1/N. Override:
cvdivisor(nminus1). In practice this shifts the objective level but seldom
the argmin.

3. Variance divisor. As in faithfulness note 5. Default: vce(paper).

4. Assumption 2.6 versus the empirical result. Assumption 2.6 requires
H = o(L/(log T)^max(1,2/a)) -- H of smaller order than L. The
cross-validation in BMK's own application selects H = T^0.65 against L = T^0.30,
the opposite ordering, and the paper does not remark on it. This command does not
constrain the CV grid to conceal the tension: if CV returns h > l, that is what is
reported. Users writing up CV-selected bandwidths should note the issue.

5. Unbalanced panels and gaps. BMK are silent; the kernel sums assume a
complete regular rectangle and |j-t| is measured in index units. This command
requires a balanced, gap-free panel on a regular time grid and errors out otherwise
rather than guessing.

6. Singular unit-date systems. Not discussed by BMK. This command drops the
offending unit from the mean-group average at that date only, and reports the
count in e(nsing).

7. Not provided, not invented. BMK give no test of H0: b0(t) = b0, no uniform
confidence band, no weak-instrument diagnostic, and no CSD-robust variant. None is
supplied here. Per-period Hausman and Sargan statistics exist in Giraitis,
Kapetanios and Marcellino (2021) for the time-series case and are implemented
in the gretl package ketvals, but they are not part of BMK.

Validation

Against the paper's own Monte Carlo. xttvpivmg_example.do reproduces
BMK's DGP of section 3: a dynamic panel with a time-varying intercept, a
time-varying autoregressive coefficient, one exogenous and one endogenous
regressor, time-varying endogeneity, and correlated random coefficients. All
time-varying parameters are scaled random walks X(t) = xi(t)/sqrt(t), giving
g1 = g2 = g3 = 1/2. The script checks that the estimated paths track the true
b0(t), that accuracy improves with both N and T, and that CV outperforms the rule
of thumb -- the paper's central Monte Carlo findings.

Against an independent implementation. The N per-unit paths are exactly the
time-series estimator of Giraitis, Kapetanios and Marcellino (2021), implemented
independently in gretl by Valentini and Lucchetti as ketvals. Running
tv_IV unit by unit and averaging reproduces e(bmg). Note that
ketvals reports the time-series sandwich variance
(K2/K^2)(P'Szz P)^(-1)(P'Szu P)(P'Szz P)^(-1), which is a different object from
eq. (17) and should not be expected to match e(semg).

A caution from the replication literature. Lucchetti and Valentini (2023)
report that a coding oversight may have affected some results in the original
Giraitis, Kapetanios and Marcellino (2021) paper, while confirming that the
estimator is remarkably robust across kernels and bandwidths. Their paper is worth
reading before relying on any implementation of this family, including this one.

References

Bai, Y., M. Marcellino and G. Kapetanios. 2026. Econometrics and Statistics
37: 26-41. doi:10.1016/j.ecosta.2023.06.004

Chen, B. and L. Huang. 2018. Nonparametric testing for smooth structural changes in
panel data models. Journal of Econometrics 202(2): 245-267.

Dendramis, Y., L. Giraitis and G. Kapetanios. 2021. Econometric Theory 37(6):
1100-1134.

Giraitis, L., G. Kapetanios and M. Marcellino. 2021. Journal of Econometrics
224(2): 394-415. doi:10.1016/j.jeconom.2020.08.013

Giraitis, L., G. Kapetanios and T. Yates. 2014. Journal of Econometrics
179(1): 46-65.

Giraitis, L., G. Kapetanios and T. Yates. 2018. Journal of Time Series
Analysis 39(2): 129-149.

Hsiao, C. and M. H. Pesaran. 2008. Random coefficient models. In The
Econometrics of Panel Data, 185-213. Springer.

Hsiao, C., M. H. Pesaran and A. K. Tahmiscioglu. 1999. Bayes estimation of
short-run coefficients in dynamic panel data models. In Analysis of Panel Data
and Limited Dependent Variable Models, 268-296. Cambridge University Press.

Lucchetti, R. and F. Valentini. 2023. Empirical Economics 65(6): 3001-3026.
doi:10.1007/s00181-023-02450-6

Pesaran, M. H. and R. Smith. 1995. Journal of Econometrics 68(1): 79-113.

Sun, Y., R. J. Carroll and D. Li. 2009. Advances in Econometrics 25: 101-129.
doi:10.1108/S0731-9053(2009)0000025006

Author

Dr Merwan Roudane

merwanroudane920@gmail.com

https://github.com/merwanroudane

Also see

xttvpivmg, xttvpivmg postestimation