Sensitivity to parallel trends¶
An event study’s pre-treatment coefficients show whether treated and control
units were trending alike before treatment. They cannot show that the trends
would have stayed alike afterwards, and a pre-trend test that does not reject
is weak evidence that they would. Rambachan and Roth (2023, A More Credible
Approach to Parallel Trends, Review of Economic Studies 90(5)) replace “the
trends are parallel” with “the trends differ after treatment by no more than
some multiple M of how much they differed before”. trend_sensitivity gives
confidence intervals for a post-treatment effect that are valid under that
weaker assumption, for each value of M you ask about. The largest M at which
the interval still excludes zero (the breakdown value) says how much
departure from parallel trends the conclusion can take.
This is the method of the authors’ R package HonestDiD, with the same restrictions, intervals and defaults, and it gives the same intervals. It is much faster: the R package solves hundreds of thousands of small linear programs for a relative-magnitudes analysis, building each one in R; here they are solved in compiled code, each starting from the previous solution. A relative-magnitudes interval takes a few hundred times less time than in R (0.03 second instead of 7.6 with 4 pre-treatment periods, 0.19 instead of 52 with 32, on one thread), and a smoothness FLCI hundredths of a second instead of about 3 (see the benchmarks). The smoothness intervals use a few megabytes of memory, where the R package uses about half a gigabyte; the relative-magnitudes intervals use more as the event study grows (a few hundred megabytes for 10 values of M with 64 pre-treatment periods).
Use it¶
On a regression with leads and lags, named by i():
from hdfe_stream import feols_stream
fit = feols_stream("y ~ i(rel_time, ref=-1) | unit + year", data,
vcov={"CRV1": "unit"})
s = fit.trend_sensitivity(M=[0.5, 1, 1.5, 2])
s.table # M, lower, upper: the robust interval at each M
s.original # the usual interval under exact parallel trends
s.breakdown # largest M in the grid at which the effect stays significant
s.plot() # needs matplotlib
fit.plot_event_study() # the event study itself, with its reference period
The event study is every coefficient named <variable>::<event time>, under
the fit’s current vcov (with_vcov chooses another). The reference event time
(default reference=-1) is omitted from the regression, and the event times
are consecutive on each side of it. keep= selects the coefficients when the
fit has more than one such variable. The effect bounded is the one at event
time 0 by default; target= takes another event time, "average" (the mean
over all post-reference event times) or weights {event time: weight}.
From any event study’s coefficients and covariance:
from hdfe_stream.core.sensitivity import trend_sensitivity
s = trend_sensitivity(beta, vcov, n_pre=4, restriction="smoothness", M=[0, 0.01, 0.02])
beta lists the pre-treatment coefficients from the earliest to the one just
before the reference period, then the post-treatment ones; target is then a
vector of weights on the post-treatment coefficients (default: the first).
diff-stream’s staggered-adoption estimators have the same method, which finds the event study, its reference period and anticipation in the fit.
Restrictions¶
restriction= says how the violation of parallel trends after treatment
(δ_post) may relate to the one before it (δ_pre, which the pre-treatment
coefficients estimate).
|
R |
The violation after treatment |
M = 0 means |
|---|---|---|---|
|
Δ^RM, |
no change from one period to the next larger than M times the largest such change before treatment |
exact parallel trends after treatment |
|
Δ^SD, |
its slope changes by at most M between consecutive periods (M is in the outcome’s units) |
a linear trend, extrapolated from before treatment |
|
Δ^SDRM, |
changes in slope no larger than M times the largest one before treatment |
a linear trend |
Either of two shape restrictions can be added:
bias="positive"or"negative": the violation after treatment has this sign (R’s Δ^…PB, Δ^…NB).monotonicity="increasing"or"decreasing": the violation is monotone over every period (R’s Δ^…I, Δ^…D).
The default values of M are R’s: for smoothness, ten values from 0 to an upper bound on the largest second difference of the pre-treatment coefficients; for the relative-magnitudes restrictions, ten values from 0 to 2. Choose M from what you know about the setting. The R package’s vignette discusses how.
Intervals¶
|
R |
Available for |
Default for |
|---|---|---|---|
|
FLCI |
smoothness |
smoothness |
|
Conditional |
all |
|
|
C-F |
smoothness |
smoothness with a sign or monotonicity restriction |
|
C-LF |
all |
the relative-magnitudes restrictions |
The fixed-length interval (FLCI) is an affine estimator plus and minus the
half-length that covers the worst-case bias. It is short when the restriction
is smoothness, but ignores sign and monotonicity restrictions (you get a
warning). It is not consistent for the relative-magnitudes restrictions,
which therefore allow only the other two.
The conditional test of Andrews, Roth and Pakes (2023) is inverted over a grid
of 1,000 values of the effect (grid_points=, grid_bounds=). The hybrids
add a first-stage test of size α/10: the FLCI’s, or the least-favorable
critical value’s, which is simulated (simulations=1000, seed=0).
Agreement with R¶
The tests compare every restriction, sign and monotonicity variant and interval with HonestDiD 0.2.8, on the package’s two example event studies and on simulated event studies with one to five pre-treatment and one to four post-treatment periods, at every point of R’s grid. They use R’s own least-favorable critical values, since the simulated draws differ between the languages. The differences that remain are deliberate:
The folded-normal quantile is exact. The FLCI’s half-length is a quantile of |N(t, 1)|, which R estimates from 10⁶ simulated draws. Computed exactly, the half-length differs from R’s by up to about 0.1%.
The least-favorable critical value uses NumPy’s random numbers. It takes the same number of draws as R (1,000) from a fixed seed, so it is reproducible, but differs from R’s by simulation error. That can move a hybrid interval’s end by a grid step.
The FLCI is optimized exactly. The optimal estimator’s worst-case bias and variance trade off along a path that is computed exactly (it is the solution path of a lasso), and the half-length is minimized along it. R bisects on a numerical derivative and, when that fails, takes the best of 100 grid points; the interval here is then never longer than R’s.
The grid reaches the interval. R centers its grid for the relative-magnitudes restrictions on zero, ±20 standard errors wide. When the accepted set reaches an end of the grid, R reports the grid’s end with a warning; here the grid is extended at the same spacing until it closes. When the grid misses the interval altogether (an effect more than about 20 standard errors from zero), R reports an empty interval; here the same grid is centered on the estimate. Neither case arises when R’s interval is closed.
Monotonicity with one pre-treatment period. R’s constraint matrix for this case requires δ₋₁ ≤ δ₁ where the restriction is δ₋₁ ≤ 0 (an indexing slip in
.create_A_M). This package uses the restriction as stated. With two or more pre-treatment periods the two agree.No errors where R stops. For a few event studies, R’s conditional test stops with “system is exactly singular” at some grid point (its closed form for the truncation bounds needs a matrix that is singular there). Here those points use the dual computation of the bounds that R uses for degenerate solutions.
An empty interval is NaN, with a warning, where R reports
Infand-Inf.
Speed¶
The analysis uses only the event study’s coefficients and their covariance,
so its time does not depend on the size of the panel, only on the number of
event times. Two benchmarks compare it with R’s HonestDiD and with diff-diff
(benchmarks/sensitivity_benchmark.py and event_study_benchmark.py; each
table says what it was measured on). diff-diff is timed for the smoothness
FLCI only: its relative-magnitudes interval is not the method’s (it widens the
estimated identified set by z standard errors, its conditional test being
disabled).
An event study, then its sensitivity analysis. On a simulated panel of
1,000,000 units observed for 13 years, with one treatment date, the event study
y ~ i(rel_time, ref=-1) | unit + year is fitted and its first post-treatment
effect analyzed at 10 values of M, the same in every library. With hdfe-stream,
fitting the event study takes nearly all the time; in R, the sensitivity
analysis does.
Measured on an EC2 c8id.4xlarge (16 logical CPUs, 30.8 GB), 2026-11-21; R 4.5.3, HonestDiD 0.2.8, fixest 0.14.2, diff-diff 4.0.0, hdfe-stream 0.12.0.
Event study (s) |
Relative magnitudes, C-LF (s) |
Smoothness, FLCI (s) |
Peak memory (GB) |
|
|---|---|---|---|---|
hdfe_stream |
8.24 |
0.132 |
0.0765 |
2.8 |
R: fixest, HonestDiD |
6.68 |
102 |
13.1 |
6.2 |
R: fixest, HonestDiD (parallel) |
6.97 |
17.1 |
5.49 |
10.4 |
diff-diff |
61.3 |
– |
2.86 |
15.8 |
More event times. Seconds for one value of M and for R’s default grid of
10, on simulated event studies with 4 post-treatment periods (1,000 grid points
for the conditional tests). R’s parallel = TRUE runs the values of M on forked
worker processes, one per logical CPU, so it is measured for the grid only; it
shortens the grid’s time, at several times the memory.
Measured on an EC2 c8id.4xlarge (16 logical CPUs, 30.8 GB), 2026-11-21; R 4.5.3, HonestDiD 0.2.8, fixest 0.14.2, diff-diff 4.0.0, hdfe-stream 0.12.0.
One value of M, seconds:
Pre-treatment periods |
Restriction, interval |
R HonestDiD |
hdfe-stream, 1 thread |
hdfe-stream, 16 threads |
diff-diff |
|---|---|---|---|---|---|
4 |
relative magnitudes, C-LF |
7.56 |
0.0257 |
0.0121 |
– |
4 |
smoothness and relative magnitudes, C-LF |
7.55 |
0.0235 |
0.0111 |
– |
4 |
smoothness, FLCI |
2.81 |
0.00323 |
0.0033 |
0.31 |
4 |
smoothness, C-F |
7.07 |
0.0116 |
0.00956 |
– |
8 |
relative magnitudes, C-LF |
11 |
0.0427 |
0.0168 |
– |
8 |
smoothness and relative magnitudes, C-LF |
11.1 |
0.0392 |
0.0162 |
– |
8 |
smoothness, FLCI |
2.85 |
0.00889 |
0.00879 |
0.323 |
8 |
smoothness, C-F |
6.95 |
0.0138 |
0.0158 |
– |
16 |
relative magnitudes, C-LF |
27.6 |
0.0906 |
0.0347 |
– |
16 |
smoothness and relative magnitudes, C-LF |
29.9 |
0.0994 |
0.0386 |
– |
16 |
smoothness, FLCI |
3 |
0.0351 |
0.035 |
0.376 |
16 |
smoothness, C-F |
4.66 |
0.0444 |
0.0439 |
– |
32 |
relative magnitudes, C-LF |
51.7 |
0.186 |
0.0613 |
– |
32 |
smoothness and relative magnitudes, C-LF |
60.9 |
0.195 |
0.063 |
– |
32 |
smoothness, FLCI |
3.34 |
0.0821 |
0.0805 |
0.802 |
32 |
smoothness, C-F |
7.42 |
0.0925 |
0.0893 |
– |
64 |
relative magnitudes, C-LF |
115 |
0.393 |
0.122 |
– |
64 |
smoothness and relative magnitudes, C-LF |
156 |
0.538 |
0.142 |
– |
64 |
smoothness, FLCI |
7.6 |
0.178 |
0.177 |
4.25 |
64 |
smoothness, C-F |
9.19 |
0.197 |
0.197 |
– |
R’s default grid of 10 values of M, seconds:
Pre-treatment periods |
Restriction, interval |
R HonestDiD |
R HonestDiD, parallel |
hdfe-stream, 1 thread |
hdfe-stream, 16 threads |
diff-diff |
|---|---|---|---|---|---|---|
4 |
relative magnitudes, C-LF |
68.6 |
12.9 |
0.226 |
0.0712 |
– |
4 |
smoothness and relative magnitudes, C-LF |
66.8 |
12.3 |
0.207 |
0.0615 |
– |
4 |
smoothness, FLCI |
11.1 |
5.74 |
0.0261 |
0.0262 |
2.74 |
4 |
smoothness, C-F |
33.5 |
8.9 |
0.0822 |
0.0444 |
– |
8 |
relative magnitudes, C-LF |
107 |
19.2 |
0.386 |
0.115 |
– |
8 |
smoothness and relative magnitudes, C-LF |
108 |
19.9 |
0.359 |
0.11 |
– |
8 |
smoothness, FLCI |
18.6 |
7.45 |
0.0744 |
0.0746 |
3.03 |
8 |
smoothness, C-F |
35.3 |
8.45 |
0.124 |
0.0932 |
– |
16 |
relative magnitudes, C-LF |
278 |
47.6 |
0.872 |
0.251 |
– |
16 |
smoothness and relative magnitudes, C-LF |
302 |
51.6 |
4.74 |
0.3 |
– |
16 |
smoothness, FLCI |
12.3 |
6.01 |
0.341 |
0.346 |
3.4 |
16 |
smoothness, C-F |
34.9 |
9.84 |
0.369 |
0.335 |
– |
32 |
relative magnitudes, C-LF |
501 |
89.3 |
1.74 |
0.492 |
– |
32 |
smoothness and relative magnitudes, C-LF |
587 |
102 |
1.99 |
0.534 |
– |
32 |
smoothness, FLCI |
15.4 |
6.87 |
0.744 |
0.746 |
8 |
32 |
smoothness, C-F |
36.4 |
9.48 |
0.823 |
0.779 |
– |
64 |
relative magnitudes, C-LF |
1,056 |
182 |
3.85 |
1.11 |
– |
64 |
smoothness and relative magnitudes, C-LF |
1,522 |
244 |
5.78 |
1.39 |
– |
64 |
smoothness, FLCI |
43.8 |
40.1 |
1.64 |
1.65 |
40.7 |
64 |
smoothness, C-F |
59.1 |
42.3 |
1.76 |
1.71 |
– |
Largest increase in peak memory over the process’s value after loading the packages:
R HonestDiD: 706 MB
R HonestDiD, parallel: 6,111 MB
hdfe-stream, 1 thread: 488 MB
hdfe-stream, 16 threads: 491 MB
diff-diff: 8 MB
The work is linear in the number of pre-treatment periods for the
relative-magnitudes restrictions (one polyhedron per pre-treatment period and
sign). The polyhedra and the values of M run in parallel (n_threads=).
Memory grows with the event study, not the panel. The smoothness intervals
stay under 5 MB. The relative-magnitudes intervals use about 40 MB per value
of M with 64 pre-treatment periods (R: about 170 MB in all).
Reference¶
- hdfe_stream.core.sensitivity.trend_sensitivity(beta, vcov, n_pre, *, restriction='relative_magnitudes', M=None, target=None, bias=None, monotonicity=None, ci_method=None, alpha=0.05, grid_points=1000, grid_bounds=None, simulations=1000, seed=0, n_threads=None, pre=None, post=None)[source]¶
Confidence intervals for a post-treatment effect that allow parallel trends to fail by up to M (Rambachan and Roth, 2023).
- Parameters:
beta (array-like) – Event-study coefficients: the
n_prepre-treatment ones from the earliest to the one just before the reference period, then the post-treatment ones from the first after it. The reference period is omitted (normalized to zero), and the periods are consecutive.vcov (array-like) – Their covariance.
n_pre (int) – Number of pre-treatment coefficients.
restriction ({"relative_magnitudes", "smoothness", "smoothness_relative_magnitudes"}) – How the post-treatment violation delta_post may differ from parallel trends.
"relative_magnitudes"(R’s Delta^RM): no period-to-period change after treatment larger than M times the largest one before it."smoothness"(Delta^SD): the slope of the violation changes by at most M between consecutive periods, so M = 0 allows exactly a linear trend."smoothness_relative_magnitudes"(Delta^SDRM, R’s “deviation from linear trend”): changes in slope after treatment no larger than M times the largest one before it.M (float or array-like, optional) – The values of M (M-bar) at which to compute intervals. By default R’s grids (see
default_M).target (array-like, optional) – Weights l on the post-treatment coefficients: the target is l’tau_post. Default: the first post-treatment period, as in R.
bias ({"positive", "negative"}, optional) – Also require the violation after treatment to have this sign.
monotonicity ({"increasing", "decreasing"}, optional) – Also require the violation to be monotone in this direction over all periods. At most one of
biasandmonotonicity.ci_method ({"fixed_length", "conditional", "hybrid_fixed_length", "hybrid_least_favorable"}, optional) – The interval (R’s FLCI, Conditional, C-F and C-LF). Default as in R:
"fixed_length"for smoothness,"hybrid_fixed_length"for smoothness with a sign or monotonicity restriction,"hybrid_least_favorable"for the relative-magnitudes restrictions, which allow only it and"conditional".alpha (float, default 0.05) – One minus the coverage.
grid_points (int, default 1000) – Grid of target values over which the conditional and hybrid tests are inverted, as in R; the interval runs from the smallest to the largest accepted point. When the accepted set reaches an end of R’s grid, the grid is extended at the same spacing until it is closed.
grid_bounds ((float, float), optional) – The grid’s ends; by default R’s.
simulations (int, default 1000) – Draws for the least-favorable hybrid’s first-stage critical value.
seed (int, default 0) – Seed for those draws.
n_threads (int, optional) – Threads for the tests (the pieces of the relative-magnitudes restrictions and the values of M run in parallel); by default all available CPUs.
pre (list, optional) – Labels of the pre- and post-treatment coefficients for the report.
post (list, optional) – Labels of the pre- and post-treatment coefficients for the report.
- Returns:
TrendSensitivityResult
- hdfe_stream.core.sensitivity.event_study_sensitivity(times, beta, vcov, *, reference=-1, target=None, **options)[source]¶
trend_sensitivityfor an event study labeled by integer event times (seeevent_study_inputsforreferenceandtarget;optionsaretrend_sensitivity’s).
- hdfe_stream.core.sensitivity.event_study_inputs(times, beta, vcov, *, reference=-1, target=None)[source]¶
The inputs of
trend_sensitivityfrom an event study labeled by integer event times: the coefficients beforereferenceand after it, in event-time order, and the target’s weights.targetis an event time after the reference (default: 0 if estimated, else the first after the reference),"average"(equal weights on every post-reference coefficient), or{event time: weight}.Returns (beta, vcov, n_pre, weights, pre_times, post_times).
- hdfe_stream.core.sensitivity.default_M(beta, vcov, n_pre, restriction)[source]¶
R’s default grids: for smoothness, 10 values from 0 to the 95% upper bound of the largest pre-period second difference (to the first pre-period coefficient’s standard error when there is one pre-period); for the relative-magnitudes restrictions, 10 values from 0 to 2.
- class hdfe_stream.core.sensitivity.TrendSensitivityResult[source]¶
Confidence intervals for a post-treatment effect under restrictions on how far parallel trends may fail, one per value of M.
- Attributes:
table (polars.DataFrame) –
M,lowerandupper: the robust confidence interval at each value of M (M-bar for the relative-magnitudes restrictions).original (dict) – The usual interval under exact parallel trends:
estimate,se,lower,upper.restriction, bias, monotonicity, ci_method (str or None) – The restriction on the violations and the interval, as passed.
alpha (float) – One minus the coverage.
target (numpy.ndarray) – The weights l on the post-treatment coefficients: the target is l’tau_post.
n_pre, n_post (int) – Pre- and post-treatment coefficients used.
pre, post (list) – Their labels (event times or coefficient names), when known.
r_names (dict) – The R package’s names for the restriction (
Delta) and the interval (method), for comparison with its output.diagnostics (dict) – Grid size, the grid’s bounds for each M and whether it was extended.
- property breakdown¶
The largest M in the grid up to which every robust interval excludes zero: the effect stays significant for violations up to this size. None when the interval at the smallest M already includes zero. It is read off the grid of M values; a finer grid refines it.
- property M¶
The values of M, in order.
- plot(ax=None, *, title=None)[source]¶
The robust intervals against M, with the original interval at the left, as in R’s createSensitivityPlot. Needs matplotlib (not a dependency of this package:
pip install hdfe-stream[plot]). Draws onaxif given; returns the figure.
- __init__(table, original, restriction, ci_method, alpha, target, n_pre, n_post, bias=None, monotonicity=None, pre=<factory>, post=<factory>, r_names=<factory>, diagnostics=<factory>, _grid_accept=<factory>)¶
- classmethod __new__(*args, **kwargs)¶
- class hdfe_stream.core.sensitivity.TrendSensitivityWarning[source]¶
A sensitivity interval is unreliable as computed: every value of the target was rejected, or the accepted set reached the edge of the search.
- __init__(*args, **kwargs)¶
- classmethod __new__(*args, **kwargs)¶
- HDFEResult.trend_sensitivity(keep=None, *, reference=-1, target=None, restriction='relative_magnitudes', M=None, bias=None, monotonicity=None, ci_method=None, alpha=0.05, drop=None, exact_match=False, **options)[source]
Sensitivity of an event study to violations of parallel trends (Rambachan and Roth, 2023; the method of R’s HonestDiD): confidence intervals for a post-treatment effect that allow the trend difference after treatment to depart from parallel by up to M, measured against the pre-treatment coefficients.
The event study is the coefficients named
<variable>::<event time>, asi(event_time, ref=-1)names them, under the current vcov (seewith_vcov): by default every such coefficient, which must come from one variable;keep,dropandexact_matchselect others, as inconfint. The reference event time is omitted from the regression, and the event times are consecutive on each side of it.- Parameters:
keep – Select the event-study coefficients by name, as in
confint.drop – Select the event-study coefficients by name, as in
confint.exact_match – Select the event-study coefficients by name, as in
confint.reference (int, default -1) – The omitted event time: the coefficients before it are the pre-treatment ones.
target (int, "average" or dict, optional) – The effect to bound: one post-reference event time (default 0, or the first after the reference if 0 is not estimated), the average over all post-reference event times, or weights
{event time: weight}.restriction – The restriction on the violations and the interval: see
hdfe_stream.core.sensitivity.trend_sensitivity.M – The restriction on the violations and the interval: see
hdfe_stream.core.sensitivity.trend_sensitivity.bias – The restriction on the violations and the interval: see
hdfe_stream.core.sensitivity.trend_sensitivity.monotonicity – The restriction on the violations and the interval: see
hdfe_stream.core.sensitivity.trend_sensitivity.ci_method – The restriction on the violations and the interval: see
hdfe_stream.core.sensitivity.trend_sensitivity.alpha – The restriction on the violations and the interval: see
hdfe_stream.core.sensitivity.trend_sensitivity.**options –
grid_points,grid_bounds,simulations,seed,n_threads: seetrend_sensitivity.
- Returns:
hdfe_stream.core.sensitivity.TrendSensitivityResult – The robust interval at each M (
.table), the original interval, the breakdown value and a plot.
- HDFEResult.plot_event_study(keep=None, *, reference=-1, alpha=0.05, joint=False, ax=None, title=None, drop=None, exact_match=False, **joint_options)[source]
Plot an event study: the coefficients named
<variable>::<event time>(asi(event_time, ref=-1)names them) against their event times, with their confidence intervals, the omitted reference period at zero, and a dashed line after it (pyfixest’siplot). Needs matplotlib (not a dependency:pip install hdfe-stream[plot]).- Parameters:
keep – Select the coefficients, as in
confint(by default every one named<variable>::<event time>, from one variable).drop – Select the coefficients, as in
confint(by default every one named<variable>::<event time>, from one variable).exact_match – Select the coefficients, as in
confint(by default every one named<variable>::<event time>, from one variable).reference (int or None, default -1) – The omitted reference event time; None draws none.
alpha (float, default 0.05) – One minus the coverage.
joint (bool, default False) – Simultaneous intervals over the plotted coefficients (
confint’sjoint=True, with itsrepsandseedinjoint_options) instead of pointwise ones.ax (matplotlib.axes.Axes, optional) – Draw here instead of on a new figure.
title (str, optional)
- Returns:
matplotlib.figure.Figure
- hdfe_stream.core.plots.event_study_plot(times, estimates, lower, upper, *, reference=None, ax=None, title=None, ylabel='Estimate and confidence interval', xlabel='Event time')[source]¶
An event study: each coefficient and its interval against its event time, the omitted reference period at zero (hollow), a line at zero and a dashed line between the reference period and the first period after it. Draws on
axif given; returns the figure.- Parameters:
times (sequence of int) – The coefficients’ event times.
estimates (array-like) – The coefficients and their intervals’ ends.
lower (array-like) – The coefficients and their intervals’ ends.
upper (array-like) – The coefficients and their intervals’ ends.
reference (int, optional) – The omitted reference event time, drawn at zero when it is not among
times.ax (matplotlib.axes.Axes, optional)
title (str, optional)
ylabel (str, optional)
xlabel (str, optional)