Lexicographic Synthetic Control (LEXSCM)#
Overview#
LEXSCM is a tool for synthetic experimental design: given a panel of units, a subset of which are eligible for treatment, it chooses which \(m\) of them to actually treat so that the rest of the panel forms a credible synthetic control for the treated group, and so that the resulting experiment has enough statistical power to detect the effects the analyst cares about. It is the design-stage analogue of the estimation-stage synthetic control methods elsewhere in mlsynth: instead of taking the treated unit as given and building a donor combination, it takes the donor pool as given and chooses the treated combination.
The method follows Abadie & Zhao, Synthetic Controls for Experimental Design [ABADIE2024], with the power / minimum-detectable-effect (MDE) machinery drawn from the synthetic-experimental-design work of Vives-i-Bastida (2022). LEXSCM optimizes two objectives in a strict priority order (“lexicographically”):
Validity – the treated combination reproduces the population trajectory on the pre-treatment predictors (small imbalance);
Power – subject to validity, the design detects the smallest possible effect (small minimum detectable effect).
The pipeline has four stages, each in its own helper module:
Treated-tuple selection (
lexsearch) – search the \(\binom{M}{m}\) candidate treated combinations for the most balanced ones, under an optional budget.Control fit (
fast_scm_control) – build a synthetic control for each candidate treated group and score its pre-treatment fit.Power analysis (
lexpower) – a moving-block placebo MDE curve over a grid of post-treatment horizons.Recommendation (
lexselect) – a lexicographic rule (validity gate \(\to\) power \(\to\) stability \(\to\) cost) returning a single recommended design plus a Pareto frontier.
All four stages run before the intervention. Once it has run, the gap the
chosen design produces also supports a cumulative total – the incremental
outcome over the whole post window, which is usually the number a design team
is after – with an interval built by resampling blocks of held-out residuals
(post_inference). See
Cumulative effects after the intervention.
When to use this estimator#
Reach for LEXSCM before an experiment, not after one. The typical setting is a geo-experiment or marketing test: you have a panel of markets, only some of which can be treated (a budget, an operational constraint, or a policy makes the rest off-limits), and you must decide which of the eligible markets to treat. Treat too few or the wrong ones and the remaining markets cannot reconstruct the treated trajectory, so the effect is biased; treat markets whose gap is too noisy and the experiment cannot detect the lift you care about. LEXSCM chooses the treated combination so that the untreated markets form a credible synthetic control (validity) and the resulting design has the power to detect a meaningfully small effect (power), in that strict priority order.
A concrete example: a retailer plans a price promotion and can run it in at most three of its forty largest metros. LEXSCM searches the eligible metros, returns the three whose combined sales path the rest of the chain can reproduce most closely on the pre-period, and reports the minimum lift that design could detect over a planned eight-week window – together with a budget gate, geography (spillover) exclusions, and coverage quotas so the chosen markets are non-adjacent and spread across regions. Once the promotion runs, the same object realizes into a standard effect report, and the gap it produces yields the cumulative lift over the eight weeks with an interval around it.
Notation#
There are \(N\) units \(\mathcal{N} \coloneqq \{1, \dots, N\}\),
observed over the pre-treatment window
\(\mathcal{T}_1 \coloneqq \{t \in \mathcal{T} : t \le T_0\}\) of length
\(T_0\), with \(\mathcal{T} \coloneqq \{1, \dots, T\}\) 1-indexed and the
intervention taking effect after \(T_0\) (the post-period
\(\mathcal{T}_2 \coloneqq \{t \in \mathcal{T} : t > T_0\}\); the design phase
uses only \(\mathcal{T}_1\)). Unit \(j\)’s outcome at time \(t\) is
\(y_{jt}\). A subset \(\mathcal{E} \subseteq \mathcal{N}\) of size
\(M = |\mathcal{E}|\) is flagged as treatment-eligible (the
candidate_col); the design must pick \(m\) of these as the treated set
\(\mathcal{S} \subseteq \mathcal{E}\), \(|\mathcal{S}| = m\). Treatment
weights on the chosen treated set are \(\mathbf{w}\) and control weights on
the remainder are \(\mathbf{v}\), both on a simplex; an optimiser carries the
star superscript \(\mathbf{w}^\ast\). The per-period gap between the
synthetic treated and synthetic control is \(e_t\); a sustained treatment
effect is \(\tau\) (the quantity the power analysis sizes), and \(\alpha\)
is the significance level. The interference graph is
\(\mathbf{A}\) with conflict-neighbours \(\mathcal{A}(\mathcal{S})\)
(below).
Mathematical Formulation#
Setup and notation#
We observe \(N\) units \(\mathcal{N} \coloneqq \{1, \dots, N\}\) over a pre-treatment period \(\mathcal{T}_1\) of length \(T_0\). Stack the pre-period outcomes as \(\mathbf{Y} \in \mathbb{R}^{T_0 \times N}\) (rows = time, columns = units) and, optionally, \(K\) time-invariant or pre-period covariates as \(\mathbf{Z} \in \mathbb{R}^{K \times N}\). The two are stacked vertically into the predictor matrix
so each column \(\mathbf{x}_j\) is unit \(j\)’s predictor profile.
A subset \(\mathcal{E} \subseteq \mathcal{N}\) of size
\(M = |\mathcal{E}|\) is flagged as treatment-eligible (the
candidate_col); the design must pick \(m\) of these.
A population weighting vector \(\mathbf{f} \in \mathbb{R}^N\),
\(\mathbf{f} \ge 0\), \(\mathbf{1}^\top \mathbf{f} = 1\) (uniform
\(1/N\) by default, or a weight_col such as population or revenue)
defines the estimand’s target trajectory – the \(\mathbf{f}\)-weighted
average unit
The pre-period predictor rows are split into an estimation window \(E\) (the first \(\lfloor \texttt{frac\_E} \cdot T_0 \rfloor\) time rows, plus all covariate rows) and a held-out blank window \(B\) (the remaining pre-period time rows). \(E\) is where the design is fit; \(B\) is a pre-treatment “dress rehearsal” with no treatment, used to validate fit and to calibrate power.
Over \(E\) the predictors are row-standardized against the population target:
with \(\sigma_t\) floored away from zero. Centring on \(\bar{x}_t\) puts the population target at the origin; scaling by the cross-sectional spread \(\sigma_t\) makes mixed-scale predictors (outcomes in dollars, covariates in percent) commensurable so no single predictor dominates the fit. The \(N \times N\) Gram matrix
summarizes all pairwise predictor inner products over \(E\).
Identifying assumptions#
LEXSCM inherits the design-based identification of Abadie & Zhao [ABADIE2024]. The untreated potential outcomes are assumed to follow an (approximate) linear factor model
with common time effects \(\delta_t\), latent time factors \(\theta_t\), unit loadings \(\mu_j\), and mean-zero transitory shocks \(\varepsilon_{j, t}\).
Approximate balance. There exist treatment weights \(\mathbf{w}\) on the chosen treated set \(\mathcal{S}\) and control weights \(\mathbf{v}\) on the remainder such that the synthetic treated and synthetic control reproduce the population target on the pre-period predictors, \(\bar{\mathbf{x}} - \sum_{j \in \mathcal{S}} w_j \mathbf{x}_j \approx 0\).
Remark. This is the design analogue of the SCM convex-hull condition, and it is the only substantive requirement. It is not imposed as an axiom: the achieved imbalance \(\lVert \bar{\mathbf{x}} - \sum_j w_j \mathbf{x}_j\rVert\) is a measurable goodness-of-fit quantity, reported for every design, on which the validity of the bias bound and the inference is conditional (Abadie & Zhao, p.13). The analyst checks the \(\approx 0\) condition instead of assuming it.
Factor structure controls bias. Under the linear factor model, matching the treated and synthetic-control trajectories on a long enough pre-period drives the latent loadings \(\mu_j\) into alignment, so the design-based bias of the average treatment effect on the treated is bounded by the achieved imbalance and shrinks as \(T_0\) grows and the fit tightens.
Remark. This is why Stage 1 minimizes imbalance, not any in-sample treatment contrast: imbalance is the quantity the bias bound is written in.
Placebo exchangeability / weak stationarity. On the blank window \(B\) no treatment has occurred, so the treated-minus-control gap is pure noise; that gap process is weakly stationary and free of anticipation, so its serial-dependence structure carries over to the post-treatment window.
Remark. This is what licenses the Stage 3 power analysis: the post-treatment null distribution of the test statistic is reconstructed by moving-block resampling the blank-window gaps, which preserves autocorrelation – the time-series-robust inference of Abadie & Zhao, not an i.i.d. permutation.
Stage 1 – Treated-tuple selection#
For a treated set \(\mathcal{S}\) with \(|\mathcal{S}| = m\) and simplex weights \(\mathbf{w} \in \Delta(\mathcal{S}) \coloneqq \{\mathbf{w} \ge 0 : \mathbf{1}^\top \mathbf{w} = 1\}\), the synthetic treated unit is \(\sum_{j \in \mathcal{S}} w_j \mathbf{x}_j\). Its imbalance against the population target is, over the estimation window,
where \(\mathbf{G}_{\mathcal{S}\mathcal{S}}\) is the \(m \times m\)
sub-Gram matrix on the rows and columns of \(\mathcal{S}\). Because the
design is \(\mathbf{f}\)-centred the population target sits at the origin,
so \(L(\mathcal{S})\) is the squared distance from the population centroid
to the convex hull of the selected donors, and \(\sqrt{L(\mathcal{S})}\)
is the achieved imbalance of Assumption 1. Stage 1 returns the top_K sets of
smallest \(L(\mathcal{S})\), subject to the budget below.
Weakly targeted designs (targeting_penalty)#
By default Stage 1 is fully targeted: the weights \(\mathbf{w}\) are free to contort onto the population mean (Abadie & Zhao’s representative-experiment goal, so the estimand is the population ATE \(\tau_t = \sum_j f_j(Y^I_{jt} - Y^N_{jt})\)). In practice you often treat the chosen markets as a group –
equal- or population-weighted – not with bespoke fractional weights, and you may prefer the treated group to look like itself, not the population.
targeting_penalty\(= \gamma \ge 0\) adds an anchor toward the group’s own equal-weight aggregate,
On the simplex \(\mathbf{1}^\top\mathbf{w}=1\), so \(\lVert \mathbf{w} - \tfrac1m\mathbf{1}\rVert_2^2 = \mathbf{w}^\top\mathbf{w} - \tfrac1m\) and the
penalty is exactly a diagonal ridge, \(\min_{\mathbf{w}\in\Delta} \mathbf{w}^\top(\mathbf{G}_{\mathcal{S}\mathcal{S}} + \gamma\mathbf{I})\mathbf{w}\). The reported imbalance stays the true targeting distance \(\sqrt{\mathbf{w}^\top\mathbf{G}_{\mathcal{S}\mathcal{S}}\mathbf{w}}\) at the penalized weights. \(\gamma = 0\) (default) is the fully targeted design; \(\gamma \to \infty\) selects the best equal-weight \(m\)-tuple; in between is weakly targeted, sliding the estimand from the population ATE toward the treated group’s own ATT. This is also the mechanism that discourages idiosyncratic treated sets: free weights can hit the population mean even for an odd set, whereas the anchor favours sets that sit near the population naturally (with near-equal weights), which the donor pool can also reconstruct.
Unit-level designs (unit_level_penalty)#
Stage 1 asks one question: does the treated combination reproduce the population target. It does not ask whether each chosen market is itself reproducible by the donors left over, and the two come apart where it matters. On a 62-market panel at \(m = 8\) the unpenalised design selected the two largest markets, which no convex combination of the remainder can reach, gave them 58 per cent of the weight, and its aggregate interval missed the truth – 31.9 per cent estimated against 8.5 true. Capping eligible market size, a crude proxy for the same constraint, cut the aggregate half-width from 16.9 to 5.7 per cent and restored coverage.
unit_level_penalty \(= \xi \ge 0\) adds the second term of Abadie and
Zhao’s equation (10), each treated market’s own reproducibility weighted by its
share of the aggregate:
Two properties make this cost nothing in the search. The inner weights
\(v_{\cdot j}\) appear in one term only, multiplied by the non-negative
scalar \(w_j\), and a positive scalar does not move an argmin – so
\(v^*_{\cdot j}\) is independent of \(\mathbf{w}\) and is just the
ordinary synthetic control for market \(j\). And on the simplex a linear
term folds into the quadratic exactly, \(c'\mathbf{w} = \mathbf{w}'
\tfrac{1}{2}(c\mathbf{1}' + \mathbf{1}c')\mathbf{w}\), so the penalty enters the
Gram the tuple search already runs on, the way targeting_penalty does, and
the same solver handles it.
The reproducibility of each candidate is measured against every other market in the panel, not against the complement of the tuple under test. Equation (10) asks for the second, which must be recomputed per tuple. Measured over 400 to 600 tuples per cell across \(J \in \{12, 62\}\), \(m \in \{2, 4, 8\}\) and \(\xi \in \{0.1, 1, 10\}\), the two pick the same design in 11 of 12 cells, with rank correlation at or above 0.986 and median error in the penalty of 0.0 to 0.7 per cent. They part at \(m = 8\) with \(\xi = 10\), where a tuple’s own exclusions matter most. The per-tuple form costs 46 ms against 0.4 seconds in total, so a 62-market design at \(m = 8\) would run for hours.
What \(\xi\) acts on is the weights, not the membership#
The term enters as \(\xi \sum_j w_j d_j\), so the design answers an unreachable market by giving it less weight and not by dropping it from the tuple. Because \(\sum_j w_j = 1\) the penalty is a convex combination of the \(d_j\), bounded by \(\max_j d_j\) whatever the tuple size, so it never grows with \(m\) and a larger tuple remains weakly better on the objective. A convex combination is minimised at a vertex, which is the sparsity Abadie and Zhao describe: large \(\xi\) puts all the weight on the most reproducible market. On a ten-market panel with a fixed six-market tuple the support holds at six through \(\xi = 100\) and collapses to three by \(\xi = 1000\), while the imbalance degrades from 3.15 to 21.0 well before it does.
That sparsity is admissible in the paper, whose cardinality constraint is a
range \(\underline{m} \le \lVert \mathbf{w} \rVert_0 \le \overline{m}\), so
a design may use fewer treated units than it selected. LEXSCM’s m is not a
range. It is a budget: m markets get treated and paid for, so a solution
that strands one at zero has spent that budget on a market the estimator then
ignores. min_treated_weight is the floor that prevents it, restoring
\(\lVert \mathbf{w} \rVert_0 = m\). Writing \(\mathbf{w} =
\varepsilon\mathbf{1} + (1 - m\varepsilon)\mathbf{u}\) leaves a problem of the
same shape, so the floor needs no new machinery, and it binds only where the
penalty would have pushed past it – the least reproducible market sits on the
floor, not at zero, and the ordering the penalty expresses survives.
Choosing \(\xi\) is not transferable. The penalty is a squared norm, so
rescaling the panel rescales it by the square, and a value tuned on one outcome
does not carry to another. Start at zero, which is Stage 1 exactly as it stands
and leaves every pinned value unchanged, and raise it only if the chosen markets
fail
approximability() after the
fact – which is the symptom this parameter exists to prevent.
How Stage 1 searches the treated sets: overview#
This subsection is for the analyst who will run LEXSCM and explain its choice. The next one, “How Stage 1 searches the treated sets: theory”, gives the optimization behind it.
In plain terms, Stage 1 looks for the \(m\) markets whose weighted average looks most like the whole population of candidate markets before the experiment. \(L(\mathcal{S})\) above is the squared distance between the population’s profile and the best weighted average of the chosen markets. A treated group that reproduces the population is one whose response to treatment speaks for the population.
Finding the best group is two problems. For one candidate group, the best weights solve a small convex problem (the next subsection) in microseconds; computing \(L(\mathcal{S})\) is called pricing the group. Choosing the group is the hard part, because there are \(\binom{M}{m}\) groups: 8.2 million for 4 of 120 markets, and 3.8 billion for 6.
When there are at most
enumerate_maxgroups (three million by default), Stage 1 prices every one, with a shortcut: a formula gives a floor under each group’s imbalance (“Screened pricing” below), and a group whose floor is already above thetop_K-th best found so far is skipped. Nothing that could win is skipped, so the groups returned are the best ones, and the status isOPTIMAL.Above
enumerate_maxthe search is local. It builds groups from several starting points, swaps one market for another while that lowers the imbalance, and perturbs the result to try other neighbourhoods. That finds good groups quickly, but it cannot prove that a group it never visited is not better, so the status isFEASIBLE.
What the result lets you say. Under OPTIMAL, the treated group is the
best-balanced group of \(m\) markets that meets the budget, and the search
proves it. Under FEASIBLE, it is the best group the local search found, and
its achieved imbalance \(\sqrt{L(\mathcal{S})}\), which the result reports,
is what the design’s validity rests on (“Why a search and not a full enumeration
or an exact MIP” below). SYNDES and MAREX run a branch and bound above their
limits, which proves the best group by ruling out whole families of groups at
once, each family with some markets forced in and some forced out. Stage 1 does
not, for the reason that section gives.
How Stage 1 searches the treated sets: theory#
This subsection is for readers who want the optimization behind the search: the
inner quadratic program and its solver, the screen, why Stage 1 does not run a
branch and bound, what the consensus block measures, and how OPTIMAL is
checked. None of it is needed to run LEXSCM; the overview above is enough for
that.
How a single tuple is built: the inner simplex QP#
“Building” a tuple \(\mathcal{S}\) means solving its inner problem
\(\min_{\mathbf{w} \in \Delta(\mathcal{S})} \mathbf{w}^\top
\mathbf{G}_{\mathcal{S}\mathcal{S}}\, \mathbf{w}\) for the optimal treatment
weights \(\mathbf{w}(\mathcal{S})\); the synthetic treated unit is then
\(\sum_{j \in \mathcal{S}} w_j(\mathcal{S})\, \mathbf{x}_j\) and the design’s
achieved imbalance is \(\sqrt{\mathbf{w}(\mathcal{S})^\top
\mathbf{G}_{\mathcal{S}\mathcal{S}}\, \mathbf{w}(\mathcal{S})}\). This convex
quadratic program over the probability simplex is solved by an Away-step
Frank-Wolfe (AFW)
routine in pure NumPy (_afw_single), chosen because every iterate stays
exactly on the simplex (no projection step) and the away move removes the
zig-zagging that plain Frank-Wolfe suffers near a face-constrained optimum
– so the support of \(\mathbf{w}\) (which donors carry positive weight)
sharpens in a handful of iterations.
Write \(\mathbf{Q} = \mathbf{G}_{\mathcal{S}\mathcal{S}}\), \(f(\mathbf{w}) = \mathbf{w}^\top \mathbf{Q} \mathbf{w}\), \(\nabla f(\mathbf{w}) = 2 \mathbf{Q} \mathbf{w}\). From a vertex start \(\mathbf{w}^{(0)} = \mathbf{e}_{\operatorname*{argmin}_i Q_{ii}}\) (the single donor closest to the target), each iteration (all vectors below are the iterate \(\mathbf{w}\) and its simplex vertices \(\mathbf{e}_i\)):
Pick two vertices. The Frank-Wolfe vertex \(s = \operatorname*{argmin}_i [\nabla f(\mathbf{w})]_i\) and the away vertex \(a = \operatorname*{argmax}_{i \in \operatorname{supp}(\mathbf{w})} [\nabla f(\mathbf{w})]_i\) – the currently active donor the gradient most wants to shed.
Choose the direction. Compare the FW direction \(\mathbf{d}_{\mathrm{FW}} = \mathbf{e}_s - \mathbf{w}\) against the away direction \(\mathbf{d}_{\mathrm{AW}} = \mathbf{w} - \mathbf{e}_a\) by \(\langle \nabla f, \mathbf{d}\rangle\) and take whichever descends more. The away step’s maximal feasible length is \(\gamma_{\max} = w_a / (1 - w_a)\); the FW step caps at \(1\).
Exact line search. Because \(f\) is quadratic the optimal step along \(\mathbf{d}\) is closed-form,
\[\gamma^\ast = \operatorname{clip}\!\Bigl( -\frac{\mathbf{w}^\top \mathbf{Q} \mathbf{d}}{\mathbf{d}^\top \mathbf{Q} \mathbf{d}},\; 0,\; \gamma_{\max}\Bigr),\]then \(\mathbf{w} \leftarrow \mathbf{w} + \gamma^\ast \mathbf{d}\), dropping \(a\) from the active set when a full away step empties it.
Certified lower bound. The Frank-Wolfe gap gives a running lower bound \(f(\mathbf{w}) + \min_i[\nabla f(\mathbf{w})]_i - \nabla f(\mathbf{w})^\top \mathbf{w} \le f(\mathbf{w}^\ast)\) on the tuple’s true minimal loss; iteration stops once the duality gap \(\nabla f(\mathbf{w})^\top \mathbf{w} - \min_i[\nabla f(\mathbf{w})]_i\) drops below
tol.
Screened pricing. Enumeration does not solve every tuple’s QP. For a tuple with \(\mathbf{Q} = \mathbf{G}_{\mathcal{S}\mathcal{S}}\), the minimiser over the affine hull, \(\mathbf{v} = \mathbf{Q}^{-1}\mathbf{1} / \mathbf{1}^\top\mathbf{Q}^{-1}\mathbf{1}\), gives the lower bound
which holds for any \(\mathbf{v}\), however inexact the solve, because
\((\mathbf{w} - \mathbf{v})^\top \mathbf{Q} (\mathbf{w} - \mathbf{v}) \ge 0\)
(_screen_bounds). A tuple whose bound exceeds the top_K-th best loss
found so far cannot enter the shortlist and is skipped. When
\(\mathbf{v}\) lies on the simplex and the bound meets
\(\mathbf{v}^\top \mathbf{Q} \mathbf{v}\), that value is
\(L(\mathcal{S})\) and no QP runs. Only the remaining tuples go to the
batched simplex QP (_afw_batched, Wolfe’s active set solved to
optimality), and stats["search"]["subsets_to_qp"] counts them. On the
45-store Walmart panel at six treated, no tuple of the 8,145,060 reached the
QP, for top_K of 1 and of 20, and the enumeration took 16 s against 93 s
for solving every tuple, with the same designs returned.
The returned top_K are re-solved one at a time by _afw_single to
pin \(\mathbf{w}(\mathcal{S})\) and the loss to full precision. Each returned
TreatedDesign then
carries its indices, the high-precision weights
\(\mathbf{w}(\mathcal{S})\), loss \(= L(\mathcal{S})\),
imbalance \(= \sqrt{L(\mathcal{S})}\), total_cost, and a
label-keyed weight_dict.
Why a search and not a full enumeration or an exact MIP#
The obvious approach – enumerate every treated combination and keep the best – is defeated by combinatorics. With \(M\) eligible markets and \(m\) to treat there are \(\binom{M}{m}\) combinations. For a modest design with \(M = 120\) eligible markets choosing \(m = 4\) that is already
candidate tuples, and the count explodes super-polynomially in \(m\): \(\binom{120}{6} \approx 3.8 \times 10^{9}\) and \(\binom{200}{6} \approx 8.2 \times 10^{10}\). Scoring a simplex QP for each is hopeless past small cases.
The natural fallback – a branch-and-bound integer program with a convex relaxation lower bound to prune – also fails here, for a structural reason specific to this objective. The population target is the \(f\)-weighted centroid of the candidate predictors, so it lies inside the convex hull of the full candidate set. Any convex (cardinality-free) relaxation of “distance from the origin to the hull of a chosen subset” is therefore \(\approx 0\) over the entire upper half of the branch-and-bound tree: the relaxation cannot certify that a partial selection is bad, so it cannot prune. An exact MIP would degenerate into near-exhaustive enumeration with extra overhead.
Both failures are also unnecessary. Abadie & Zhao [ABADIE2024] require only that the chosen design be feasible and approximately balanced; validity is conditional on the achieved imbalance, a reported quantity, not on a certificate of global optimality. LEXSCM therefore:
Enumerates exactly when \(\binom{M}{m} \le \texttt{enumerate\_max}\) (default \(3{,}000{,}000\)). This is the gold standard – it returns the certified global
top_Kand reports termination statusOPTIMAL.Runs a strengthened multi-start local search otherwise: greedy construction from diverse seeds, best-improvement swap descent to a local optimum, and basin-hopping kicks to escape it. In Monte Carlo this lands on the exact optimum 83-100% of the time and within roughly \(1\%\) (mean) / \(7\%\) (worst case) of the minimal imbalance – immaterial under the approximate-balance criterion. Termination status is
FEASIBLE.
Because the local search has no MIP optimality gap, LEXSCM reports a
consensus block in its place: the number of independent starts run, how
many of them ended on the incumbent (starts_reaching_incumbent, and
its share consensus_rate), the number of distinct local optima seen,
and the incumbent-improvement trail. (A convex hull lower bound is also
reported, but only as advisory information – for the reason above it is
\(\approx 0\) and is deliberately not turned into an optimality gap.)
What the consensus block does and does not tell you#
The block records what the search did. It does not measure whether the
incumbent is the global optimum, and consensus_rate in particular
should not be read that way.
The measurement: 18 instances built from a wide geo panel
(three population bands,
\(m \in \{3, 4\}\), three constraint regimes), each solved exactly by
enumeration, against 1,350 multi-start runs over
n_starts \(\in \{1, 2, 4, 8, 16\}\) and fifteen seeds. Within a
fixed n_starts – the only situation an analyst is ever in, since
n_starts is set before the number is read – consensus_rate
separates a suboptimal run from an exact one with an AUC of 0.44 to 0.54.
Half of that range is on the wrong side of a coin flip.
Pooled across n_starts the AUC is 0.38, which is worse than
uninformative: it runs backwards. The rate divides by the quantity that
determines accuracy. More starts explore more basins, so the share
reaching the incumbent falls while the answer improves:
|
share exact |
mean |
mean starts on incumbent |
|---|---|---|---|
1 |
0.51 |
0.64 |
1.2 |
4 |
0.90 |
0.44 |
3.1 |
16 |
1.00 |
0.40 |
11.0 |
A low consensus_rate is therefore not evidence of a miss, and two runs
at different n_starts cannot be compared on it. No threshold is usable
either: consensus_rate <= 0.5 catches three quarters of the suboptimal
runs but fires on 70% of the exact ones, and tightening it to
\(0.2\) drops the false-alarm rate to 16% while catching 3% of the
misses.
That corpus needs an exact solve per instance, so the suite pins the
structure behind it instead, on three small enumerable designs:
mlsynth/tests/test_lexscm_heuristic_consensus.py holds accuracy rising
with n_starts while the rate falls, the underlying count rising, and the
rate on wrong answers overlapping the rate on right ones – including runs
where every start agreed and the answer was still not the optimum.
What does predict accuracy is n_starts, which the analyst sets. On
that corpus the share of runs landing on the certified optimum was 0.51,
0.76, 0.90, 0.98 and 1.00 at n_starts of 1, 2, 4, 8 and 16, and the
suboptimal runs missed by 3.4% of imbalance on average. The default of 16
was exact on every returned run. Constraints are what makes the landscape
hard: the spillover-plus-budget regime accounts for most of the misses at
low n_starts, so raise n_starts when a budget or an adjacency graph
is active, and enumerate when \(\binom{M}{m}\) allows it.
What OPTIMAL certifies, and how it is checked#
The status is a statement about the algorithm and not a quantity measured on
the instance: OPTIMAL is reported because the enumeration branch was taken.
The claim it stands for is that the returned top_designs are the top_K
feasible treated \(m\)-tuples of smallest \(L(\mathcal{S})\) over the
entire feasible region – the candidate pool intersected with the budget,
spillover, stratum-quota and forced-unit constraints – and that each design’s
weights are the exact minimiser of its own inner QP.
mlsynth/tests/test_lexscm_optimality_certificate.py checks that claim
against a reference sharing no code with the search: an inner solver that
enumerates the \(2^{m} - 1\) faces of the simplex instead of running
Wolfe’s active set, and an outer brute force that scores every feasible tuple
behind plain-Python constraint predicates. The two agree tuple for tuple and
loss for loss across ten constraint regimes, the weak-targeting ridge, and
Grams that are rank deficient, dominated by one factor, or carrying duplicate
donors.
Three further things are pinned there. The weights carry a per-instance
certificate: with \(g = G_{\mathcal{S}\mathcal{S}} \mathbf{w}\) and
\(\nu = \mathbf{w}' G_{\mathcal{S}\mathcal{S}} \mathbf{w}\), the
conditions \(g_j \ge \nu\) for every \(j\), with equality on the
support, are the KKT conditions of the inner program and are computed from the
Gram alone, so they hold whatever solver produced \(\mathbf{w}\). The
number of tuples scored equals the size of the feasible region counted
independently. And the batched solver that ranks the candidates agrees with the
high-precision re-solve to \(5 \times 10^{-16}\) relative on the measured
instances, which is the step that would otherwise seat the wrong K in a
pool whose reported losses are all correct.
The checks are themselves checked. With the enumeration made to drop the optimum, to rank on perturbed losses, or to skip the budget filter, the comparison has to fail, and a test asserts that it does: a verification that passes on a broken search verifies nothing.
Ties are the one place the word is weaker than it sounds. Duplicate donors put
several tuples at the same minimum, and the search returns K of that tied
class. Any K of them are global minimisers, so the certificate holds on the
losses and not on the labels.
Building tuples in the heuristic regime#
When \(\binom{M}{m}\) exceeds enumerate_max the same per-tuple QP is
reused, but the set of tuples scored is grown adaptively by a multi-start
local search (_local_search):
Seeding. \(2 \times\)
n_startsseeds: then_startsunits with the smallest \(G_{jj}\) (single donors already nearest the population centroid – the cheapest places to start near balance) plusn_startsuniformly random units for diversity.Greedy construction. From a seed, repeatedly add the candidate that most lowers the batched loss of the partial tuple until \(|\mathcal{S}| = m\), skipping any addition that would breach the budget.
Best-improvement descent. Score the full 1-swap neighbourhood (replace one member with one non-member), move to the steepest improving swap, and repeat to a local optimum.
Basin-hopping kicks. Perturb the incumbent with a random 2-swap
kickand re-descend, keeping the better basin; repeatedn_kicks(4) times per start.
Every distinct tuple ever scored is cached, and the global top_K of
that cache is returned and re-solved to full precision exactly as in the
exact path. The consensus block – how many independent starts’ final
optima coincide with the incumbent – is the confidence diagnostic that
stands in for the absent MIP gap.
Budget constraints#
When a per-unit treatment cost \(c_j\) is supplied (unit_cost_col,
constant within unit) and a total budget \(B_{\max}\), every
returned design must satisfy the hard knapsack constraint
Two mechanisms enforce it. First, a sound feasibility presolve removes any eligible unit \(i\) that cannot belong to any budget-feasible \(m\)-tuple, i.e. whenever
This only ever drops provably impossible units; it never touches the
objective, so it cannot change which feasible design is optimal. Second,
the search itself respects the budget at every step: exact enumeration
filters combinations by their cost sum, and the local search rejects any
greedy addition, swap, or kick that would breach \(B_{\max}\). If no
\(m\)-tuple fits the budget the stage reports INFEASIBLE.
Stage 1 returns a solver-style diagnostics block (stats) with the
problem definition, presolve removals, search method and number of subsets
evaluated, the incumbent and worst-in-pool imbalance, the advisory
relaxation bound, the solution pool of top_K tuples, and termination
status / runtime.
Stage 2 – Control fit#
For each candidate treated set \(\mathcal{S}\) with weights
\(\mathbf{w}\), the synthetic treated trajectory is
\(\mathbf{X}_{\cdot, \mathcal{S}}\, \mathbf{w}\). A synthetic control is then
built from the non-treated units: control weights \(\mathbf{v}\) solve a
ridge-penalized quadratic program (penalty lambda_penalty) matching the
synthetic treated over \(E\), subject to an exclusion constraint
\(v_j = 0\) for all \(j \in \mathcal{S}\) (a treated unit cannot also be
its own control; the spillover adjacency widens this to
\(\mathcal{S} \cup \mathcal{A}(\mathcal{S})\)). The per-period gap is
Pre-treatment fit is summarized by the normalized mean-squared error
(NMSE) of the synthetic treated against the target on both windows
(nmse_E, nmse_B). The blank-window gaps
\(e_B = \{e_t : t \in B\}\) (residuals_B) are, under Assumption 3,
pure placebo noise; they are the raw material for the power analysis. This
stage is identical to the pre-rebuild control path – only the search and
power stages around it changed.
Spillover / interference exclusions#
When treating a set of units, interference is a design concern, and
Vives-i-Bastida (2022) handles it with two exclusion criteria that LEXSCM
implements directly. Both read off one conflict graph, encoded by a
symmetric matrix \(\mathbf{A} \in \{0, 1\}^{N \times N}\) with
\(A_{ij} = 1\) iff units \(i\) and \(j\) interfere (zero diagonal).
\(\mathbf{A}\) is supplied either as a cluster_col (\(A_{ij} = 1\)
iff \(i, j\) share a cluster, e.g. a state or province) or as an
adjacency / spillover matrix (\(A_{ij} = 1\) iff the entry exceeds
spillover_threshold); the two combine entrywise by logical OR. The matrix is
aligned to the IndexSet, the single source of truth for unit identity. Write the
conflict-neighbours of a treated set \(\mathcal{S}\) as
“No interference” (Stage 1, a treatment criterion). The treated set \(\mathcal{S} = \operatorname{supp}(\mathbf{w})\) must be conflict-free – an independent set of \(\mathbf{A}\), \(A_{ij} = 0\) for all \(i, j \in \mathcal{S}\). This restricts only the admissible supports, so it enters exactly where the cardinality constraint \(\lVert \mathbf{w} \rVert_0 = m\) already lives – a filter on the candidate tuples, not a term in the inner weight program – and only shrinks the search.
“Exclusion restriction” (Stage 2, a control criterion). The donor pool drops \(\mathcal{S} \cup \mathcal{A}(\mathcal{S})\): the Stage-2 program pins \(v_k = 0\) for every \(k \in \mathcal{S} \cup \mathcal{A}(\mathcal{S})\), so a treated unit’s spillover neighbours cannot enter its synthetic control and contaminate the counterfactual.
If no conflict-free \(m\)-tuple exists (e.g. \(m\) exceeds the number of
clusters), or the exclusions empty every donor pool, the fit raises
MlsynthConfigError instead of returning
a degenerate
design. With no cluster_col / adjacency supplied, \(\mathbf{A} =
\mathbf{0}\) and the behaviour is exactly the unconstrained search.
# at most one treated unit per state, and no treated unit's
# same-state neighbours used as its donors
LEXSCM({"df": df, "outcome": "y", "unitid": "unit", "time": "year",
"candidate_col": "eligible", "m": 2, "cluster_col": "state"}).fit()
Coverage quotas and size bands#
Two further treatment criteria from the same design checklist restrict which units may be treated – both, like the spillover “no interference” rule, act on the admissible supports and never enter the inner weight program.
Coverage / stratification. Give each unit a stratum (region / tier / segment) via
stratum_col.min_per_stratumrequires at least that many treated units from every stratum that has a candidate (“test in every region”);max_per_stratumallows at most that many (“a quota”). Formally the treated set \(\mathcal{S}\) must satisfy \(\texttt{min} \le |\{j \in \mathcal{S} : g(j) = s\}| \le \texttt{max}\) for each candidate stratum \(s\), where \(g(j)\) is unit \(j\)’s stratum. (Settingmax_per_stratum = 1mirrors acluster_colquota on the treated side, but unlike the spillover rule it does not also clean the donor pool.)Treated-unit size bands. With
size_colandmin_size/max_size, only units whose size lies in \([\texttt{min\_size}, \texttt{max\_size}]\) are eligible for treatment (they remain available as donors). The floor is a power / operational minimum; the ceiling encodes the convex-hull limit – a unit far larger than the rest cannot be reproduced by a convex combination of the others (Vives-i-Bastida excludes big cities for exactly this reason).
Either constraint raises MlsynthConfigError when it
is infeasible (e.g. min_per_stratum over more strata than \(m\), or fewer
than \(m\) candidates inside the size band).
# cover every region, at most two per region, and only mid-sized markets
LEXSCM({"df": df, "outcome": "y", "unitid": "unit", "time": "year",
"candidate_col": "eligible", "m": 4,
"stratum_col": "region", "min_per_stratum": 1, "max_per_stratum": 2,
"size_col": "population", "min_size": 50_000, "max_size": 2_000_000}).fit()
Forced-in and forbidden markets#
Two hard market lists complete the treatment-criteria vocabulary, matching SYNDES
and MAREX. to_be_treated pins specified markets into the treated set: every
candidate \(m\)-tuple the search considers must contain them, which is useful
when a market is decided in advance (a client insists on testing it) and the
design only has to choose the rest around it. not_to_be_treated does the
opposite – those markets are removed from the treatment pool but stay available
as donors – for a market already running another campaign, with poor data, or
earmarked as a clean control. A forced-in market must be a treatment candidate
(candidate_col true and within any size band), at most \(m\) may be
forced, and a market cannot appear in both lists; each violation raises
MlsynthConfigError. Forcing in a market that is hard
to synthesize costs imbalance, so check the achieved fit.
# always test DMA 501; never test DMA 635 (it stays an eligible donor)
LEXSCM({"df": df, "outcome": "y", "unitid": "unit", "time": "year",
"candidate_col": "eligible", "m": 4,
"to_be_treated": [501], "not_to_be_treated": [635]}).fit()
Stage 3 – Power analysis (minimum detectable effect)#
Stage 3 asks: how large must a sustained treatment effect be for this design to detect it? The answer is a minimum-detectable-effect (MDE) curve over post-treatment horizon lengths, computed entirely from the Stage 2 placebo residuals with one consistent resampling model for both the null and the alternative.
What “at each horizon” means – and what it does not#
The curve is indexed by the length \(h\) of the post-treatment
window (n_post_grid, default \(h = 2, \dots, 8\)), not by calendar
period. There is no “MDE at period \(t = 91\)”. Instead, for each
candidate duration \(h\), the analysis manufactures many synthetic
\(h\)-long gap windows by resampling the placebo residuals and asks what
constant effect a window of that length could detect. Reading the curve as
\(h\) grows tells you how detectability improves the longer the
experiment runs.
Moving-block resampling#
Let the placebo pool be one or more residual series – the chosen design’s
blank-window gaps \(e_B\), optionally pooled with donor-unit placebo
gaps. The scale \(\sigma\) is the standard deviation of the pooled
residuals (all series concatenated, ddof=1, floored at
\(10^{-12}\)). The block length defaults to
where \(\tilde L\) is the median series length: floored at 1, and
capped at the horizon \(h\) so a block never exceeds the window it
fills. To draw one length-\(h\) window
(block_resample_windows()):
pick a placebo series uniformly at random, then repeatedly cut contiguous
blocks of length \(\ell\) from a random offset with wraparound
(np.take(..., mode="wrap")), concatenate them, and truncate to
\(h\). Wrapping preserves the within-series autocorrelation
(Assumption 3); the random series choice reproduces the cross-unit placebo
distribution.
Test statistic and critical value#
The statistic is the mean absolute gap over the window,
Resampling \(n_\text{null}\) (default 4000) placebo windows gives the null distribution; its \((1 - \alpha)\) empirical quantile is the critical value \(c_\alpha\).
Power, the effect grid, and the MDE#
A treatment effect is modelled as a constant additive shift \(\tau\) applied to every point of a resampled window – the homogeneous-effect working assumption behind the MDE. Its power is estimated from a fresh draw of \(n_\text{power}\) (default 2000) windows (same residual model, new randomness), so the null and the alternative differ only by the shift:
The effect is swept on a grid of \(n_\text{grid} = 64\) points in
standard-deviation units, \(\tau / \sigma \in [0, \texttt{max\_sd}]\)
(default cap \(8\)). The search walks the grid until
\(\operatorname{power}(\tau)\) first reaches power_target, then
linearly interpolates between the last sub-threshold point
\((g_0, p_0)\) and the crossing point \((g, p)\) for a finer value:
If the grid is exhausted without reaching the target, the horizon is infeasible and returns \(\infty\).
Three reported scales#
Each horizon reports the MDE on three scales:
mde_sd\(= \mathrm{MDE}(h)/\sigma\) – effect-size units, the primary scale (matching Vives-i-Bastida’s “detect effects larger than \(0.1\) s.d.” convention) and numerically robust: nothing is divided by a fragile mean, so zero-mean or near-zero outcomes cannot blow the calculation up;mde_abs\(= \mathrm{MDE}(h)\) – outcome units;mde_pct– the manager-facing percentage, \(100 \cdot \mathrm{MDE}(h) / \lvert\text{baseline}\rvert\), where the baseline is the counterfactual level over the matching window, \(\text{baseline} = \operatorname{mean}\bigl(\texttt{synthetic\_treated} [-h:]\bigr)\). This is guarded: when \(\lvert\text{baseline}\rvert\) falls below a floor (default one \(\sigma\)) the percentage is returned asNaNdeliberately, so a near-zero or sign-flipping level cannot manufacture a spurious “we can detect a 0.3% effect.”
Detectability curve and horizon collapse#
detectability_curve() sweeps
the horizon grid and returns curve_sd
(\(h \mapsto \mathrm{MDE}(h)/\sigma\)), curve_pct (the percentage
curve), the per-horizon details, and min_horizon_mde_le_0p1sd – the
shortest horizon whose MDE falls to \(0.1\sigma\), a quick read on how
long the experiment must run to clear the conventional effect-size bar.
Stage 4 then collapses the curve to a single representative MDE per the
mde_horizon setting:
"late"(default) – the MDE at the longest horizon; the conservative choice for a sustained-exposure experiment."early_min"– the smallest MDE across feasible horizons (most optimistic detectability)."early_mean"– the mean MDE across feasible horizons (the percentage averaged only over horizons where it is defined).
Stage 4 – Final recommendation (lexicographic selection)#
The final stage turns the per-design metrics – imbalance (validity),
mde_sd (power), stability \(=\) out-of-sample
\(\text{NMSE}_B\) (fit robustness), and total_cost – into one
recommendation, applying the method’s priorities in strict order:
Validity gate. Keep only designs whose imbalance is within a relative slack of the best achievable balance,
\[\text{imbalance}(S) \le \bigl(1 + \texttt{imbalance\_tol}\bigr)\cdot \min_{S'} \text{imbalance}(S') ,\](default
imbalance_tol\(= 0.25\)). This is the set of designs that satisfy Assumption 1 well enough to be trustworthy. (A degenerate tolerance that would empty the gate falls back to the single best-balanced design.)Power. Among gated designs with a feasible MDE, choose the smallest
mde_sd– the most detectable valid design.Tie-breaks. Break ties by better out-of-sample stability (\(\text{NMSE}_B\)), then by lower
total_cost.
The recommendation is returned as a tuple of fields:
winner (the chosen design), shortlist (the top
max_shortlist ranked designs), pareto_ids (the Pareto frontier on
imbalance \(\downarrow\) vs. mde_sd \(\downarrow\), always
exposed for transparency), a status, a human-readable
explanation, and a per-design table (which becomes
results.search.shortlist). The status is one of:
OK– a valid, adequately powered design was found;POWER_NOT_ESTABLISHED– no gated design reached the power target within the effect grid, so the best-balanced design is recommended and the power caveat is flagged (the pipeline degrades gracefully instead of crashing);EMPTY– no candidate designs were supplied.
Worked numeric walkthrough#
To make the two core mechanisms concrete, here is a single tuple built and powered end to end on a deliberately tiny panel (\(T_0 = 6\) pre-period rows, \(N = 5\) units, treat \(m = 2\)). Every number below is the actual helper output, not an illustration.
Building the tuple (Stage 1)#
After \(f\)-centring and row-standardising the predictors, the Gram matrix’s diagonal – each unit’s squared distance to the population centroid – is
Unit 1 (0-indexed) sits closest to the target, so it seeds the AFW vertex
start. With \(\binom{5}{2} = 10\) the search enumerates exactly
(status OPTIMAL, 10 subsets scored). The winning tuple is
\(\mathcal{S} = \{0, 1\}\), and its inner simplex QP returns treatment weights
The standalone high-precision re-solve reproduces this loss with a Frank-Wolfe lower bound equal to it to \(10^{-6}\) – the QP is at its certified optimum. The synthetic treated unit is \(0.41\,\mathbf{x}_0 + 0.59\,\mathbf{x}_1\), and \(1.2838\) is the achieved imbalance on which the bias bound (Assumption 1-2) is conditional.
Powering the design (Stage 3)#
Take this design’s blank-window placebo gaps to be a length-\(L = 24\) series with \(\sigma = 0.8695\). The auto block length is \(\ell = \min\bigl(h, \operatorname{round}(24^{1/3})\bigr) = \min(h, 3)\), so at \(h = 2\) it is capped to \(2\) and at \(h \ge 3\) it is \(3\). With a counterfactual level of \(\text{baseline} = 12.0\), the per-horizon MDE is:
\(h\) |
\(\ell\) |
\(c_\alpha\) |
|
|
|
|---|---|---|---|---|---|
2 |
2 |
1.245 |
2.037 |
1.771 |
14.8% |
4 |
3 |
1.108 |
1.672 |
1.454 |
12.1% |
8 |
3 |
0.981 |
1.282 |
1.115 |
9.3% |
Reading down the table is the detectability curve: as the horizon
lengthens, averaging over more periods shrinks both the critical value
\(c_\alpha\) and the MDE, so a sustained effect of
\(\approx 9\%\) of the counterfactual becomes detectable by
\(h = 8\), versus \(\approx 15\%\) at \(h = 2\). Under
mde_horizon="late" Stage 4 carries the \(h = 8\) row
(mde_sd \(= 1.28\)) as this design’s power score; under
"early_min" it would carry the smallest feasible mde_sd.
Standardized Post-Fit and Power Analysis#
LEXSCM’s Stage 3 already produces the lexicographic MDE curve used to rank
designs against each other; the standardized post-fit attached to
LEXSCMResults.post_fit is the complementary surface used once a design
has been chosen – it is the same
SyntheticControlPostFit object SYNDES,
MAREX, and PANGEO expose, so downstream consumers (dashboards, paper-style
reports, comparison tables) read identical fields across the family.
pf = res.report.additional_outputs["post_fit"] # SyntheticControlPostFit
pf.ate, pf.ate_percent, pf.total_effect # treatment-effect scalars
pf.rmse_fit, pf.rmse_blank, pf.rmse_post # pre / blank / post fit quality
pf.p_value, pf.ci_lower, pf.ci_upper # conformal inference
pf.power # PowerAnalysis (see below)
The trajectories pf.treated_series and pf.control_series are the
winning candidate’s predictions.synthetic_treated and
predictions.synthetic_control – per-unit weighted aggregates of the
treated and control donors over the full timeline. The phase boundaries
(n_fit, n_blank, n_post) line up with the same E / B / post split LEXSCM
uses internally (so n_fit + n_blank = T0 and the 30%-of-pre-tail
holdout convention applies via frac_E = 0.7).
Where the unified power analysis fits in#
Stage 3’s detectability_curve()
is the design-selection workhorse – it converts the moving-block placebo
null on the B window into a per-horizon MDE used by Stage 4’s lexicographic
selector. The post-fit power analysis is the post-selection companion:
analytical Gaussian + AR(1) variance inflation on the realised gap residuals,
matching the MAREX / SYNDES / PANGEO power surfaces exactly so the same
diagnostic table can be produced for every family member.
with \(\widehat{\sigma}_{\text{placebo}}\) the SD of the gap on the B (blank / holdout) window, \(\widehat{\rho}\) the lag-1 autocorrelation of those residuals clipped to \((-0.99, 0.99)\), and \(\mathrm{VIF}(T, \rho) = \tfrac{1}{T}\bigl(1 + 2\sum_{k=1}^{T-1} (1-k/T)\rho^k\bigr)\) the standard AR(1) variance-inflation factor (textbook \(1/T\) when \(\rho = 0\)). See Synthetic Controls for Experimental Design (MAREX) for the full derivation; the same module powers all three estimators.
p = res.power # PowerAnalysis (single source)
p.headline.mde_absolute # MDE at the realised T_post
p.headline.mde_pct # ... as % of post-period baseline
p.headline.power_at_observed # power to detect res.report.att
p.curve # tuple of MDEPoint per horizon
p.sigma_placebo # σ̂ used (B window in LEXSCM)
p.serial_correlation # ρ̂ AR(1) of the B residuals
p.placebo_bias # mean B gap; 0 for an unbiased design
p.placebo_bias_pvalue # is that offset distinguishable from 0?
p.n_placebo, p.sigma_ci # periods σ̂ rests on, and its interval
p.headline.critical_value # t quantile the MDE is built at
p.headline.mde_ci # MDE at the ends of p.sigma_ci
The MDE is what a constant effect has to clear to be detected by a two-sided
test on the mean post-period gap. Compare an observed effect against
p.headline.critical_value, not 1.96: σ̂ comes from the B window, so the
pivot is Student-t on that window’s effective degrees of freedom. A non-zero
placebo_bias says the synthetic control misses on periods it was not fitted
to, and that offset is carried into the standard error, since it lands in the
post window on top of whatever effect is there.
Two MDEs, complementary roles#
Stage 3 MDE (
best_candidate.mde_results) – moving-block placebo null on the B window, used to rank designs against each other. Aggregated to a representative scalar bymde_horizon(late/early_min/early_mean) and consumed by Stage 4’s lexicographic gate.Post-fit MDE (
res.power) – closed-form AR(1) MDE for a two-sided Student-t test on the mean post-period gap, consumed after a design has been chosen, on the same surface that MAREX / SYNDES / PANGEO produce. Use this when reporting a single detectability number alongside the realised ATE / CI.
The two answer different questions and need not agree. Stage 3 tests
\(\frac{1}{|post|}\sum_t |e_t|\) against a moving-block placebo null;
the post-fit MDE tests the signed mean against a t quantile. A design whose
res.power headline sits below its realised ATE can still return a
non-significant p-value from the permutation test the design search ranked it
by, and the method field on res.power names which test its number
refers to.
Ranking on the B window, and why it is allowed#
Stage 4 ranks candidates by their B-window MDE and the inference is then built from those same B-window residuals, which looks like the thing Vives-i-Bastida (2022, Section 4) rules out: “we can’t decide the outcome pair on the basis of the fit in the blank period (otherwise we would bias our statistical analysis).” The concern is a winner’s curse – if the ranking sorts on noise, the winner’s B residuals are a minimum over candidates and the null built from them is too narrow.
Measured on simulated panels with a known factor structure, it does not bite.
Against a control that never consults the B window, the ranked design is 24
percent better on the post window – periods no design was ranked on – so what
Stage 4 finds is real donor-fit stability and not a lucky draw. Stage 1 is what
makes this hold: it hands Stage 4 designs that already meet the balance
objective, so the candidates differ in true quality by more than they differ by
noise. Remove that and the property goes with it. On a panel whose units share
one noise scale, where the candidates are equal in truth, the same ranking buys
nothing (a post-window ratio of 0.98 against 1.00). Both arms are pinned in
mlsynth/tests/test_lexscm_selection_validity.py, the second as the positive
control that gives the first the power to fail.
What the B window does understate is the post-window error, by about a sixth on
that configuration. This is a horizon effect and not a selection one – B
periods sit adjacent to the estimation window and post periods sit beyond them,
so the gap is the same size for a design chosen without ever consulting B. It
is priced by the terms on res.power, not by the ranking rule.
Power-analysis failures (e.g. degenerate B-window residuals) never break a
fit; res.power is simply left as None. To compute on a
non-default horizon grid or significance level call
compute_power_analysis() directly.
Cumulative effects after the intervention#
Everything above happens before the intervention: which markets to treat, how small an effect the design can detect, which candidate to recommend. Once the experiment has run the question changes. A design team rarely wants an average weekly lift; it wants the total – the incremental sales over the eight weeks the campaign ran – with an interval around it. That total is the cumulative effect through horizon \(h\),
and cumulative_path()
reports it for every \(h\) from one period to the end of the post window.
Why the closed form does not carry over#
Time-based regression (TBR), the design tool this capability is modelled on, reads its cumulative effect off a Student-\(t\) posterior. It can do so because its counterfactual comes from augmented DiD: an unconstrained fit of two parameters, an intercept and one slope. That makes the counterfactual an affine function of the observed series, and a sum of affine functions is affine, so the posterior for a running total is available in closed form.
A synthetic control does not have that property. Its weights are constrained to the simplex – non-negative and summing to one – and the fitted counterfactual is not an affine function of the data. The departure is measurable by refitting on a perturbed series and comparing against the affine prediction:
Weight constraint |
Departure from affinity |
|---|---|
Unconstrained least squares |
\(3 \times 10^{-16}\) |
Weights summing to one |
\(6 \times 10^{-16}\) |
Summing to one and non-negative |
\(3 \times 10^{-2}\) |
The first two are floating-point noise; the equality constraint costs nothing because it is linear. The inequality constraint is what breaks affinity, and it is also the constraint that makes a synthetic control interpretable as a weighted average of real units. So the closed form is unavailable, and the interval is built by inverting a pivot instead.
The block-sum pivot#
The point estimate is arithmetic: the running sum of the post-period gaps. Only the interval is inferential.
The pivot is the sum of \(h\) consecutive held-out residuals. Three properties recommend it. It matches the statistic being reported, so nothing is assumed about the shape of the effect path – an effect that arrives late and one that decays are priced alike. Blocks of consecutive periods carry the serial dependence that weekly panels have, so the dependence is priced by construction and not by a correction. And the sum over a window is the window length times its mean, so an interval for the total rescales exactly into an interval for the average.
The blocks are circular: each series is wrapped end-to-start, so a series of \(n\) periods contributes \(n\) blocks at every horizon and the tail quantiles do not thin out as \(h\) grows. They are drawn from a pool of held-out residual series – the treated aggregate’s own blank-window residual first, placebo series after it – with each series rescaled to the first one’s standard deviation. Both halves of that are needed:
Pool |
\(h = 1\) |
\(h = 4\) |
\(h = 8\) |
|---|---|---|---|
The treated unit’s own residual only |
0.826 |
0.784 |
0.724 |
Pooled, on each series’ own scale |
0.928 |
0.934 |
0.932 |
Pooled and rescaled |
0.890 |
0.908 |
0.910 |
Coverage at a nominal 0.90, 500 replications, no treatment effect. A single blank window holds too few blocks and the interval is too short, increasingly so as the horizon grows. Pooling fixes the count but overshoots, because the placebos are noisier than the unit under test and their blocks inflate the null. Doing both covers the nominal level and stays flat in the horizon.
An alternative construction was measured and discarded: inverting a sharp null of a constant per-period effect. It conflates the size of the cumulative with the flatness of the path, and returns an empty set for 1.7 to 2.5 per cent of post windows – an interval that excludes every candidate value, which is not a usable answer.
Approximability – whether the interval is centred at all#
A resampling interval prices noise. It cannot repair an offset. If the fitted weights reproduce the treated aggregate on the estimation window but miss it on periods they were not fitted to, the post-period gap carries that miss as a constant, which reads as a treatment effect and no resampling of residuals removes it. The usual cause is the treated aggregate sitting outside the convex hull of the donor pool, where a simplex-weighted average cannot reach it.
approximability() tests the
blank-window gap for a location offset and returns ok when the statistic
falls inside \(\pm 3\). Read it before the interval.
The statistic does not test hull membership, and it is a better guide than hull membership would be. Over 700 simulated designs whose membership is verified by a linear program:
Split |
Admitted |
Coverage, admitted |
Coverage, refused |
|---|---|---|---|
The gate, \(|t| \le 3\) |
81% |
0.875 |
0.540 |
Inside the donors’ hull |
47% |
0.897 |
0.732 |
Hull membership is the wrong cut because what decides the interval is the size of the realized offset against the noise. A design just outside the hull carries an offset too small to matter, and a design inside it can be fitted badly enough to carry a large one. The two statistics overlap heavily: the largest in-hull statistic is 8.2, the smallest out-of-hull one is 0.001, and 82 per cent of out-of-hull designs fall below 5.5. No threshold sorts designs by hull membership, so the gate sorts them by whether the interval will be centred, which is the question the interval needs answered.
Coverage falls away continuously as the statistic rises, which is where the threshold of 3 comes from:
\(|t|\) |
\([0, 1)\) |
\([2, 3)\) |
\([3, 4)\) |
\([4, 5)\) |
\([5, 6)\) |
|---|---|---|---|---|---|
Coverage |
0.901 |
0.844 |
0.842 |
0.719 |
0.513 |
At 3 the gate admits 84 per cent of designs, covering 0.883, and refuses a set covering 0.696. Moving it to 5 admits 93 per cent at 0.875. The trade is flat between 2 and 5, so the exact figure inside that range matters less than having a gate at all; past 5 the refused designs are the ones whose intervals miss by the largest margins.
The offset is charged against the Bartlett effective sample size \(n(1 - \rho)/(1 + \rho)\), where \(\rho\) is the lag-one correlation of the blank gap, and not against the raw period count. Serially correlated periods carry less information than independent ones, so dividing by \(\hat\sigma / \sqrt{n}\) understates the standard error and refuses designs the donors reproduce perfectly well. On zero-offset gaps of 20 periods, where a correctly sized test at this threshold would refuse 0.003:
\(\rho\) |
0.0 |
0.3 |
0.6 |
0.8 |
|---|---|---|---|---|
Refused, raw \(n\) |
0.007 |
0.043 |
0.173 |
0.385 |
Refused, Bartlett |
0.019 |
0.029 |
0.060 |
0.125 |
Refused, bounded at \(n\) (shipped) |
0.005 |
0.023 |
0.059 |
0.125 |
The correction earns its keep from \(\rho = 0.3\) upward, and on its own it costs something at \(\rho = 0\), where the middle row refuses 0.019 against the raw count’s 0.007. \((1 - \rho)/(1 + \rho)\) is convex, so a lag-one estimate scattered about zero averages to an effective sample size above the period count – \(1.24n\) over 20 periods and \(1.6n\) over 10 – and the test then charges the offset against more information than the window holds.
The effective count is therefore bounded above by the period count as well as below by two, which is the third row. The bound costs nothing where the correction is doing work, and the statistic is monotone in the count, so clipping can only move it toward zero: it removes refusals and adds none, and no design the bounded test refuses would have been admitted without it.
The gate fires often on panels whose factor loadings can be negative, because the treated aggregate then falls outside the hull for many treated pairs. That is the gate working. A design it refuses needs a different treated set or a wider donor pool, not a wider interval.
Three estimands#
Which total a user is entitled to quote depends on what the weights were fitted to, and the three answers separate as soon as the effect varies across treated units.
The treated aggregate. The gap estimates
\(\tau^T_t = \sum_j w_j \tau_{jt}\), the effect on the weighted treated
combination the design actually built. This is what
cumulative_path() returns,
and on a design with two treated markets it is the only thing LEXSCM’s own
output supports.
Per treated unit. Abadie and Zhao’s Unit-level design fits the treated aggregate to the population target and each treated unit to its own synthetic control at the same time. Stage 1 above chooses one weight vector; this design chooses a whole matrix alongside it. Write \(\mathcal{S} = \{j : w_j > 0\}\) for the treated set the weights pick out themselves, and \(v_{ij}\) for the weight unit \(i\) carries in the synthetic control built for treated unit \(j\). The design is their equation (10):
subject to
The first term is the Stage-1 objective unchanged: the treated combination reproduces the population target. The second is a sum of ordinary synthetic-control fits, one for each treated unit, each carrying that unit’s own share \(w_j\) of the treated aggregate, so a unit that contributes little to the headline also counts for little in how hard the design works to make it reproducible. The last line is bookkeeping with real consequences: no treated unit may serve as a donor for another treated unit, and units outside the treated set carry no synthetic control of their own.
\(\xi\) sets the exchange rate between the two terms. Small \(\xi\) buys treated units that match the population; large \(\xi\) buys treated units their own donors can reproduce. The two are in genuine tension, since the units that best fill out an aggregate are often the extreme ones that nothing else resembles. Abadie and Zhao report that this design concentrates on a small treated set even with no sparsity constraint (\(\underline{m} = 1\), \(\overline{m} = J - 1\)), and that large \(\xi\) sharpens that further; pushed far enough it returns a single treated unit that a convex combination of the others reproduces closely.
Given a solution \(\{w^*_j, v^*_{ij}\}\), the per-unit control weights aggregate into one control vector, which is their equation (11):
The estimate can then be read two ways that are the same number,
so the aggregate gap is the weighted mean of the per-unit gaps.
LEXSCM does not solve this program, though its two-stage shape is not a departure from the family either. Vives-i-Bastida (2022) supplies the lexicographic reading of the joint design: enumerate the candidate treated tuples, fit \(\mathbf{w}\) for each, keep the best few, and only then fit control weights for those. He notes this is the joint program in the limit as the weight on the control-fit term goes to zero, so representativeness is settled before any control is chosen. That limit is Stage 1 followed by Stage 2, and it is the sense in which this estimator is lexicographic.
Two symbols do not carry over, and one of them is a trap for anyone reading
both papers. targeting_penalty is a ridge toward equal weights, not the
\(\beta\) of the Weakly targeted design in equation (9), which penalises
the distance between the aggregate treated and aggregate control units. And
\(\xi\) names different quantities in the two sources: in Abadie and
Zhao’s equation (10) above it weights the per-unit control fits, while in
Vives-i-Bastida’s program it weights the aggregate treated-against-control
discrepancy, which is Abadie and Zhao’s \(\beta\). Neither has a field in
LEXSCMConfig – the first because nothing in the pipeline fits a per-unit
control, the second because the lexicographic order has already taken it to
its limit. The formulation is here because it
says what object the per-unit gaps are: one residual series per treated unit
from its own synthetic control, with the treated units excluded from each
other’s donor pools. A caller who builds those series some other way satisfies
the same contract.
unit_level_cumulative()
takes period-by-unit gaps and returns both, computing the aggregate from the
parts so a market-level breakdown and a headline number cannot contradict each
other. Each unit’s interval reads its null from the other treated units, which
the design has already built synthetic controls for; extra_pool adds further
held-out series, which a small treated group needs. Drawing on them as placebos
is consistent with the constraint that excludes them as donors: the design
forbids treated unit \(i\) from appearing in treated unit \(j\)’s
synthetic control, and says nothing about unit \(i\)’s own residual serving
as a draw from the null.
Every series in that pool has to be approximable itself#
The interval inverts a pivot on blocks drawn from the pool, which assumes each member is a draw from the null – what the treated unit’s gap would look like with no effect. A donor its own peers cannot reproduce does not qualify. Its residual is dominated by the fit’s systematic miss, and that miss is a constant.
Rescaling does not repair it, and the way it fails is specific.
_prepare_pool puts every series on the treated unit’s standard deviation,
which matches the scale and leaves the ratio of offset to spread where it was.
Summing a block of \(h\) periods then accumulates the offset as \(h\)
while the noise it is supposed to represent accumulates as \(\sqrt{h}\), so
one such member comes to set the tail quantiles, and it does so most at the long
horizons the cumulative path exists to report.
unit_level_cumulative and population_cumulative therefore run the same
approximability() gate over
the pool that the treated units themselves have to pass, dropping what fails,
recording it on the result as dropped_treated and dropped_extra, and
naming it in a warning. A unit’s own residual is never screened: it sets the
scale the rest are put on, and its own offset is what the caller checks with
approximability directly. Pass screen_pool=False to keep everything.
The effect is large because the offending series are not marginal. On a twelve-market panel calibrated to a 141-week weekly geo panel, per-unit interval width falls from 199.6 to 88.0 with the point estimates unchanged; at sixty-two markets it falls from 94.6 to 85.3. Nor are such donors unusual. In a panel whose market sizes span a factor of 57, the largest cannot be a convex combination of smaller ones, and about 21 per cent of donors fail the gate at twelve markets against 6 per cent at sixty-two – fewer in the larger panel because each donor has more peers to be spanned by, not because the sizes are closer together.
Screening is cheap and assumes nothing: it removes series that were never valid draws from the null. A second route to the same problem is to relax the constraint that creates it, since a donor is unreachable only under simplex weights with no intercept. Both were measured on the same panels, at twelve markets, against the plain simplex fit:
Fit |
SC |
MSCa |
MSCb |
MSCc |
|---|---|---|---|---|
Donors failing the gate |
22.3% |
14.0% |
8.0% |
5.0% |
Per-unit width, unscreened |
230.5 |
114.8 |
100.1 |
99.8 |
Per-unit width, screened |
97.8 |
96.9 |
93.2 |
96.8 |
Three things follow. Dropping the adding-up constraint (MSCb) helps more than adding an intercept (MSCa), because markets here differ almost entirely by scale: the largest market needs its donor mix scaled up, not shifted up, and an intercept that matches the level leaves the amplitude wrong. No relaxation drives the failures to zero, because what survives is a fit whose level or scale drifts between the fitting window and the blank window, which in-sample flexibility cannot fix. And the two routes are substitutes, not complements – screening the simplex fit reaches 97.8 and relaxing without screening reaches 100.1, while doing both reaches 93.2.
Relaxing also costs something the width column hides. A fit free of adding-up chases pre-treatment noise and extrapolates worse, so the treated units themselves fail the gate more often: an MSCb design needed 38 draws to yield 30 that passed, against 33 to 34 for the others. Which constraint a panel wants is a specification question, and Two-Step Synthetic Control answers it with a test instead of a default.
The population. A representative design weights its treated units so the
aggregate stands in for a wider population, whose effect is
\(\tau_t = \sum_j f_j \tau_{jt}\) under the population weights
\(\mathbf{f}\). The two estimands differ by
\(\sum_j (w_j - f_j)\tau_{jt}\). Both weight vectors sum to one, so when the
per-unit effects share a common mean that difference has expectation zero: it
calls for a variance term and not a bias correction. Its variance is
\(\operatorname{var}(\tau_j)\,\lVert \mathbf{w} - \mathbf{f} \rVert^2\), and
the Unit-level design supplies the first factor because it estimates an effect
for every treated unit.
population_cumulative()
carries it. The point estimate is unchanged; only the interval widens, and it
widens with the horizon, because a constant per-period offset accumulates as
\(h\) and so enters the variance as \(h^2\) against the gap term’s
\(h\).
The separation is not academic. On a design whose per-unit effects have standard deviation 0.8 about a mean of 1.0, the two estimands differ by 0.30 per period, and the gap-only interval covers \(\tau^T\) 0.880 of the time and \(\tau\) 0.714. Adding the representation term takes population coverage from 0.775 to 0.895 at a nominal 0.90, costing 40 per cent more width. Where the effects are in fact identical it costs 8 per cent more width for nothing and covers 0.931 against 0.913, because the spread estimate retains about 0.13 it cannot resolve. The correction errs wide.
Two assumptions carry it, and neither is testable from the treated units alone. The untreated units’ effects are drawn from the same distribution as the treated ones, which is what makes the offset mean zero. And \(\operatorname{var}(\tau_j)\) is read off however many units were treated, so a design with two or three of them estimates it poorly; the interval is then honest about the correction’s form and vague about its size.
Centring the per-unit level#
One treated unit’s synthetic control sits at its own fitted level, and that
offset runs through the post window unchanged. center=True subtracts each
unit’s blank-window mean from both windows and charges the noise in that mean
back as the path accumulates, which level_scale reports.
It is off by default, because removing the offset costs more than it buys. Measured at a nominal 0.90 on a four-unit design:
Estimand |
|
|
|---|---|---|
Treated aggregate |
0.913 |
0.858 |
Population |
0.895 |
0.849 |
Per treated unit |
0.833 |
0.838 |
The offsets also sit in the residuals the null is built from, so taking them out
contracts the null – the aggregate interval narrows by a third – and the bias
removed is worth less than the width lost. The exception is marginal and applies
to a single unit, whose offset is largest relative to its noise. Centring also
assumes the offset is the same in both windows, which
approximability() tests.
Measured coverage#
Against a known truth of 1.0 per period, 300 replications, nominal 0.90:
Panel |
Coverage |
Mean width |
Share gated |
|---|---|---|---|
In hull, independent periods |
0.915 |
11.3 |
10% |
In hull, AR(1) \(\rho = 0.3\) |
0.881 |
13.2 |
19% |
In hull, AR(1) \(\rho = 0.6\) |
0.863 |
17.7 |
33% |
Far outside the hull |
0.037 |
64.1 |
100% |
“Share gated” is the fraction of designs approximability refuses before the
interval is read. Blocks absorb dependence shorter than the horizon they span,
and at \(\rho = 0.6\) some of it outlives them, which is where the 0.863
comes from.
Two cautions on reading that table. The last row is a deliberately extreme draw, pushed far enough outside the hull that every design is refused; it shows what the gate exists to prevent and not what leaving the hull costs in general. On a milder draw, with membership verified by a linear program, out-of-hull designs cover 0.732 in aggregate and the gate refuses 30 per cent of them – the harmful part, since the refused ones cover 0.463 and the admitted ones 0.850. Leaving the hull is a matter of degree, and the gate sorts by degree.
The levels themselves belong to the panel that produced them. The same construction, measured on two generators that differ only in whether the latent factors follow a random walk or are stationary, covers 0.968 and 0.856 for in-hull designs with independent noise, and 0.928 and 0.823 at \(\rho = 0.6\). The 0.915 above sits inside that spread. What is stable across generators is the ordering – coverage falls as dependence rises and as the approximability statistic rises – and not the number. Read the table for the shape, and measure your own panel for the level.
The thickness of the pool trades the two levels off against each other. On a four-unit design, varying the number of extra held-out series:
Extra series |
0 |
4 |
10 |
20 |
|---|---|---|---|---|
Aggregate coverage |
0.874 |
0.901 |
0.913 |
0.932 |
Per-unit coverage |
0.774 |
0.805 |
0.833 |
0.852 |
Four extra series bring the aggregate to its nominal level and twenty carry it past, so a thicker pool is not uniformly better for the headline. The per-unit paths improve throughout and still fall short.
What this does not give you#
A per-market interval cannot be quoted at face value. At the pool thicknesses above the per-unit paths cover 0.774 to 0.852 against a nominal 0.90, because a unit’s synthetic control sits at its own fitted level and that offset is not separable from its effect using post-period data alone. Centring removes the offset and loses more in width than it gains in coverage. Treat a per-unit path as a decomposition of the aggregate – which market contributed what – and the aggregate interval as the inferential statement.
unit_level_cumulative and population_cumulative need one gap series per
treated unit, which means one synthetic control per treated unit. LEXSCM fits a
single synthetic control to the treated aggregate, so those two entry points
take gaps the caller constructs; they are not reachable from a LEXSCM result
on its own.
cumulative_path() is.
Coverage above 0.90 is reported where it was measured, and the construction is calibrated and not exact. A 90 per cent interval here is a 90 per cent interval to within a couple of points under moderate dependence, and less than that at an AR(1) coefficient of 0.6.
Example: the cumulative lift over a campaign#
import numpy as np
import pandas as pd
from mlsynth import LEXSCM
from mlsynth.utils.fast_scm_helpers.post_inference import (
approximability, cumulative_path)
rng = np.random.default_rng(13)
n, T, T_post = 24, 72, 8
Lam = rng.normal(size=(n, 2))
F = np.cumsum(rng.normal(size=(T, 2)), 0)
Y = 100 + rng.normal(0, 5, n) + F @ Lam.T + rng.normal(0, 1.0, (T, n))
names = [f"m{j:02d}" for j in range(n)]
cand = sorted(rng.choice(n, 12, replace=False).tolist())
df = pd.DataFrame([
{"market": names[j], "week": t, "sales": Y[t, j],
"eligible": int(j in cand), "post": int(t >= T - T_post)}
for j in range(n) for t in range(T)
])
res = LEXSCM(dict(df=df, outcome="sales", unitid="market", time="week",
candidate_col="eligible", post_col="post",
m=2, top_K=8, verbose=False)).fit()
win, layout = res.search.winner, res.panel.time
post_gap = win.predictions.effects[-layout.n_post:]
blank_gap = win.predictions.residuals_B # the held-out B window
check = approximability(blank_gap)
if not check.ok:
raise SystemExit(
f"the donors cannot reproduce {res.selected_units} off-sample "
f"(t = {check.t_stat:.2f}); the interval would be biased.")
for point in cumulative_path(post_gap, [blank_gap], level=0.90):
print(f"week {point.horizon}: {point.estimate:8.2f} "
f"[{point.lower:8.2f}, {point.upper:8.2f}]")
On this panel the design treats m18 and m19, the approximability
statistic is 0.13, and the eight-week cumulative lands at 0.83 with a 90 per
cent interval of \([-3.14, 2.97]\) – a null result on a panel with no
planted effect, which is what it should be. Supplying placebo_pool with
held-out residuals from untreated markets alongside blank_gap tightens that
interval; the single blank window is the floor, not the recommended pool.
Verification#
LEXSCM is validated against the synthetic-experimental-design framework it
implements (Abadie & Zhao 2026; Vives-i-Bastida 2022). Path A: the Walmart
45-store placebo design tracks pre-period to ~2.7% of mean sales and yields a
placebo effect of ~0.9% whose permutation test fails to reject (CI covers zero)
– the paper’s “no spurious effect” result. Path B: on the paper’s exact
Section-5 linear-factor DGP (J = 15, T_E = 20) the design recovers the
planted effect with MAE far below its scale (~0.24 for the single-treated-unit
design, falling to ~0.16 at m = 2). Pinned in
benchmarks/cases/lexscm_walmart.py and lexscm_design_mc.py; see the
dedicated page LEXSCM — Synthetic Experimental Design (Abadie-Zhao 2026; Vives-i-Bastida 2022).
The cumulative paths above carry no external referent – the block-sum pivot is
this library’s own construction – so they are validated against their nominal
level on a simulation whose truth is set in the case, in
benchmarks/cases/lexscm_cumulative_coverage.py. At a nominal 0.90 and a
horizon of eight periods, on a four-unit design with a small fitted-level
offset: the aggregate path covers the effect on the treated 0.800 of the time
reading its null from the three peers alone and 0.890 with twenty held-out
series added, while the per-unit paths go 0.826 to 0.878 over the same pool and
stay short of the aggregate. The residual is the location offset each unit’s
synthetic control carries, which resampling residuals cannot shift. For the
population estimand the representation term is what does the work: the treated
interval covers the population effect 0.490 of the time and the population
interval 0.785, on the same draws.
Run it with:
python benchmarks/run_benchmarks.py --case lexscm_cumulative_coverage
The same question on the data-generating process the design method was
published with – Abadie and Zhao’s Section 5, which supplies both potential
outcomes so every treated unit has a known effect – is
benchmarks/cases/lexscm_cumulative_az_mc.py. A design of three treated
units is chosen from the untreated outcomes alone, the experiment realises the
treated outcome on exactly those units, and the paths are read at the final
horizon against a nominal 0.90. On the paper’s own dimensions the aggregate
covers 0.880 and the per-unit paths 0.778; on a larger panel with a fifteen
period horizon, 0.793 and 0.680.
What separates the per-unit paths that cover from those that do not is the approximability gate. Conditioning on it, coverage is 0.820 and 0.848 among the units the gate admits – stable across panel size and horizon – against 0.642 and 0.379 among the units it refuses. Read the gate before the interval.
Core API#
- class mlsynth.estimators.lexscm.LEXSCM(config)#
Bases:
objectLexicographic Synthetic Control (LEXSCM) estimator.
This estimator automatically designs synthetic control experiments by jointly optimizing:
Pre-treatment fit (validity)
Statistical power (detectability of effects)
The user interacts with the estimator through a single entry point: fit(), while all modeling choices are controlled via the configuration object.
- Parameters:
config (dict or LEXSCMConfig) – Configuration object specifying data inputs, identification strategy, synthetic control settings, search budget, and inference parameters.
The following fields are supported:
Required
df(pd.DataFrame): Panel dataset containing unit-level observations over time.outcome(str): Name of the outcome variable.unitid(str): Column identifying observational units.time(str): Time index column.candidate_col(str): Boolean (0/1 or True/False) column indicating units eligible for treatment assignment.m(int): Number of units selected per treated tuple.
Identification / Design
post_col(str, optional): Indicator for post-treatment period (0/1).frac_E(float): Fraction of pre-treatment period used for estimation window E.unit_cost_col(str, optional): Per-unit treatment cost column (must be constant within unit).budget(float, optional): Total budget constraint on selected treated units.seed(int): Random seed for reproducibility.
Synthetic Control Specification
weight_col(str, optional): Unit-level weights (e.g., population, revenue).covariates(list of str, optional): Covariates included in synthetic control construction.lambda_penalty(float): Regularization penalty for control mismatch in quadratic program.
Search / Optimization Budget
top_K(int): Number of top candidate tuples returned by the treated-design selection search.top_P(int): Deprecated/unused (the multi-start search seeds itself); retained for backward-compatible configs.
Inference / Power Analysis
alpha(float): Significance level for statistical testing.n_post_grid(list of int): Post-treatment horizons used for MDE detectability curves.n_sims(int): Number of Monte Carlo simulations for null distribution.post_imputation({“mean”, “max”, “double_max”}): Method for imputing post-treatment outcomes.test_statistic({“mean_abs”, “mean”, “rms”}): Test statistic used for treatment effect evaluation.delta(float): Absolute minimum detectable effect threshold.relative_delta(float, optional): Relative MDE threshold (> 1.0).target_mde_horizon(str): Target horizon for MDE evaluation (e.g., early_mde_avg).max_shortlist(int): Maximum number of candidate designs retained after filtering.
System / Logging
verbose(bool): If True, enables progress logging.
- Returns:
LEXSCM – Initialized estimator ready for .fit() execution.
See also
LEXSCM.fitExecutes the full optimization and estimation pipeline
Notes
The pipeline integrates: search, estimation, evaluation, power analysis, selection.
Designed for fully automated experimental design under constraints.
References
This implementation is based on the synthetic experimental design framework:
https://economics.mit.edu/sites/default/files/2026-02/Synthetic%20Controls%20for%20Experimental%20Design%20Feb%202026.pdf Develops the formal framework for selecting treated units using synthetic controls to reduce bias and improve experimental design in aggregate settings.
https://ivalua.cat/sites/default/files/2023-03/Vives-i-Bastida_2022_anon.pdf Provides a practical guide to synthetic experimental design in policy contexts, including inference and design trade-offs.
- fit(**kwargs) LEXSCMResults#
Run the full Synthetic Experiment Design pipeline.
- This method executes the end-to-end workflow:
prepares panel data
searches over candidate treated unit sets
fits synthetic controls
evaluates pre-treatment fit
estimates statistical power (MDE)
selects the optimal design
- Parameters:
**kwargs (dict, optional) – Reserved for future extensions (currently unused).
- Returns:
LEXSCMResults – Object containing: - summary : ranked table of candidate designs - best_candidate : selected experimental design - all_candidates : full evaluated set - selection_metadata : search diagnostics - additional dataset and inference diagnostics
Configuration#
- class mlsynth.config_models.LEXSCMConfig(*, df: ~pandas.DataFrame, outcome: str, unitid: str, time: str, candidate_col: str, m: ~typing.Annotated[int, ~annotated_types.Gt(gt=0)], post_col: str | None = None, weight_col: str | None = None, unit_cost_col: str | None = None, budget: ~typing.Annotated[float | None, ~annotated_types.Gt(gt=0.0)] = None, targeting_penalty: ~typing.Annotated[float, ~annotated_types.Ge(ge=0)] = 0.0, unit_level_penalty: ~typing.Annotated[float, ~annotated_types.Ge(ge=0)] = 0.0, min_treated_weight: ~typing.Annotated[float, ~annotated_types.Ge(ge=0.0), ~annotated_types.Lt(lt=1.0)] = 0.0, cluster_col: str | None = None, adjacency: ~typing.Any | None = None, spillover_threshold: ~typing.Annotated[float, ~annotated_types.Ge(ge=0)] = 0.0, stratum_col: str | None = None, min_per_stratum: ~typing.Annotated[int | None, ~annotated_types.Ge(ge=1)] = None, max_per_stratum: ~typing.Annotated[int | None, ~annotated_types.Ge(ge=1)] = None, size_col: str | None = None, min_size: float | None = None, max_size: float | None = None, to_be_treated: ~typing.List | None = None, not_to_be_treated: ~typing.List | None = None, seed: int = 42, frac_E: float = 0.7, covariates: ~typing.List[str] | None = None, lambda_penalty: float = 0.1, top_K: int = 20, top_P: int = 10, alpha: ~typing.Annotated[float, ~annotated_types.Gt(gt=0.0), ~annotated_types.Lt(lt=1.0)] = 0.05, n_post_grid: ~typing.List[int] = <factory>, n_sims: int = 1000, post_imputation: ~typing.Literal['mean', 'max', 'double_max'] = 'mean', test_statistic: ~typing.Literal['mean_abs', 'mean', 'rms'] = 'mean_abs', mde_horizon: ~typing.Literal['early_mean', 'early_min', 'late'] = 'late', max_shortlist: ~typing.Annotated[int, ~annotated_types.Gt(gt=0)] = 5, power_target: ~typing.Annotated[float, ~annotated_types.Gt(gt=0.0), ~annotated_types.Lt(lt=1.0)] = 0.8, imbalance_tol: ~typing.Annotated[float, ~annotated_types.Ge(ge=0)] = 0.25, display_graph: bool = False, verbose: bool = True)#
Configuration for LEXSCM - Fast Synthetic Experiment Design pipeline.
- mde_horizon: Literal['early_mean', 'early_min', 'late']#
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'forbid'}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- post_imputation: Literal['mean', 'max', 'double_max']#
- test_statistic: Literal['mean_abs', 'mean', 'rms']#
Result Containers#
LEXSCM.fit() returns a
LEXSCMResults, which is a
DesignResult – the experimental-design half of
mlsynth’s two-family result contract. LEXSCM chooses which units to treat
before any intervention, so it returns a design that resolves to an effect
report. The surface is small and grouped – one obvious home for each thing.
Front door (the standardized contract):
res.report– the realized effect as anEffectResult: the single source for the ATT / CI / pre-fit (flatatt/att_ci/counterfactual/gapplus the standard sub-models). Itsadditional_outputs['post_fit']carries the fullSyntheticControlPostFit– the MAREX-family shared bundle with per-period effects and covariate-balance SMDs – alongsideate_per_periodandcumulative_effect.res.power– the design’s MDE / power analysis (the single source for power;Noneif power analysis failed).res.selected_units/res.assignment/res.design_weights– the chosen design (the treated units, the treated/control split, and the impliedWeightsResults).res.metadata– the lexicographic recommendation diagnostics.
Grouped detail:
res.search(LEXSCMSearch) – how the design was chosen:shortlist(the ranked table),candidates(every evaluated design),winner(the chosenSEDCandidatewith its weights / predictions / losses / raw MDE curve), andselection(the Stage-1 treated-design selection diagnostics).res.panel(LEXSCMPanel) – the panel structure:time,units,outcome,population_mean.
- class mlsynth.utils.fast_scm_helpers.structure.LEXSCMResults(*, report: BaseEstimatorResults | None = None, assignment: Any | None = None, selected_units: Any | None = None, design_weights: WeightsResults | None = None, power: Any | None = None, metadata: Dict[str, Any] | None = None, windows: DesignWindows | None = None, composite: DesignComposite | None = None, cluster_composites: Dict[str, DesignComposite] | None = None, inference_settings: DesignInferenceSettings | None = None, search: LEXSCMSearch, panel: LEXSCMPanel, unit_effects: Tuple[TreatedUnitEffect, ...] = (), **extra_data: Any)#
Bases:
DesignResultTop-level container returned by
mlsynth.LEXSCM.fit().A
DesignResult(the experimental-design family) with a deliberately small, grouped surface – one obvious home for each thing:Front door (the standardized contract).
report– the realized effect as anEffectResult(the single source for ATT / CI / p-value / pre-fit; itsadditional_outputs['post_fit']holds the fullSyntheticControlPostFitwith the per-period effects and covariate-balance SMDs).power– the design’s MDE / power analysis (the single source for power;Noneif power analysis failed).selected_units/assignment/design_weights– the design.metadata– the lexicographic recommendation diagnostics.
Grouped detail.
search– the candidate search (LEXSCMSearch):shortlist,candidates,winner,selection.panel– the panel structure (LEXSCMPanel):time,units,outcome,population_mean.unit_effects– oneTreatedUnitEffectper treated unit carrying weight: its own synthetic control over the design’s control pool, its gap over the fit, blank and post windows, and the blank-window approximability verdict. The design’s own numbers are the aggregate; these are the per-unit decomposition of Abadie and Zhao’s equation (10), and they are whatunit_level_cumulative()consumes.
- model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True, 'extra': 'allow', 'frozen': True, 'json_encoders': {<class 'numpy.ndarray'>: <function MlsynthResult.Config.<lambda>>}}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- panel: LEXSCMPanel#
- search: LEXSCMSearch#
- class mlsynth.utils.fast_scm_helpers.structure.LEXSCMSearch(shortlist: Any, candidates: List[SEDCandidate], winner: SEDCandidate, selection: Dict[str, Any])#
The design search – how the treated set was chosen.
- shortlist#
Ranked table of candidate designs (the recommendation shortlist).
- Type:
pd.DataFrame
- winner#
The selected design (winner of the lexicographic search), with its weights / predictions / losses / inference / raw MDE curve.
- Type:
SEDCandidate
- selection#
Treated-design selection diagnostics (the imbalance-minimizing enumerate-or-local-search) plus the recommendation record.
- Type:
- winner: SEDCandidate#
- class mlsynth.utils.fast_scm_helpers.structure.LEXSCMPanel(time: TimeInfo, units: UnitInfo, outcome: str, population_mean: ndarray)#
The panel structure the design was built on.
- time#
Fitting / blank / post period layout.
- Type:
TimeInfo
- units#
Treated / control unit rosters.
- Type:
UnitInfo
- population_mean#
Population-mean outcome series over time (the design target).
- Type:
np.ndarray
- population_mean: ndarray#
- time: TimeInfo#
- units: UnitInfo#
- class mlsynth.utils.post_fit.SyntheticControlPostFit(treated_series: ndarray, control_series: ndarray, gap_series: ndarray, n_fit: int, n_blank: int, n_post: int, ate: float | None = None, total_effect: float | None = None, ate_percent: float | None = None, ate_per_period: ndarray | None = None, cumulative_effect: ndarray | None = None, p_value: float | None = None, ci_lower: float | None = None, ci_upper: float | None = None, inference_method: str | None = None, rmse_fit: float | None = None, rmse_blank: float | None = None, rmse_post: float | None = None, covariate_names: Tuple[str, ...] = (), covariate_smd: Dict[str, float] | None = None, covariate_smd_abs_max: float | None = None, covariate_smd_squared_sum: float | None = None, covariate_smd_treated_vs_pop: Dict[str, float] | None = None, covariate_smd_treated_vs_pop_abs_max: float | None = None, covariate_smd_treated_vs_pop_squared_sum: float | None = None, covariate_smd_control_vs_pop: Dict[str, float] | None = None, covariate_smd_control_vs_pop_abs_max: float | None = None, covariate_smd_control_vs_pop_squared_sum: float | None = None, power: PowerAnalysis | None = None)#
Bases:
objectStandardized post-fit diagnostics for a single synthetic control design.
Field semantics are estimator-agnostic; every MAREX-family adapter populates the same shape. Any field that isn’t naturally computable for the producing estimator is left
None.- control_series: ndarray#
- gap_series: ndarray#
- power: PowerAnalysis | None = None#
- treated_series: ndarray#
- class mlsynth.utils.post_fit.PowerAnalysis(headline: MDEPoint, curve: Tuple[MDEPoint, ...], alpha: float, power_target: float, sigma_placebo: float, serial_correlation: float, baseline: float, method: str = 'analytical_ar1_mean_gap', placebo_bias: float = 0.0, placebo_bias_pvalue: float = nan, n_placebo: int = 0, sigma_ci: Tuple[float, float] = (nan, nan))#
Bases:
objectStandardized power-analysis output attached to
SyntheticControlPostFit.Built from the placebo / blank-period gap variance and an analytical Gaussian approximation, with AR(1) variance inflation to handle serial correlation in the gap residuals. The intent matches the per-estimator power modules already in the library (PangeoPower, SPCDPowerAnalysis, SYNDESPower) but consumes the same
SyntheticControlPostFitshape so every covariate-aware SCM-family estimator gets the surface for free.- curve#
MDE / power values across the requested
post_gridhorizons (so callers can read a detectability curve).
- sigma_placebo#
Standard deviation of the placebo gap series, about that window’s own mean, used as the noise scale. The offset itself is reported separately as
placebo_biasand enters the standard error there.- Type:
- serial_correlation#
Lag-1 (AR(1)) autocorrelation of the placebo gap residuals, estimated on the demeaned series and small-sample corrected, used to inflate the variance for serial dependence.
- Type:
- placebo_bias#
Mean of the placebo gap. Zero for a design that is unbiased on the window it was not fitted to; otherwise the offset the MDE must clear alongside the noise.
- Type:
- sigma_ci#
Chi-square confidence interval for
sigma_placeboatalpha, on the placebo window’s effective degrees of freedom.
- baseline#
Mean of the control trajectory on the post window (denominator for
mde_pct). NaN when no post window exists.- Type:
- method#
"analytical_ar1_mean_gap": the closed-form MDE for a two-sided test on the mean post-period gap, with AR(1) variance inflation and a Student-t critical value. This is a different test from the moving-block permutation onmean|e|that LEXSCM’s design search ranks candidates by (Vives-i-Bastida 2022, Section 2), so the two MDEs need not agree. Reserved for future"monte_carlo"extensions.- Type:
Helper Modules#
Treated-tuple selection (Stage 1)#
Exact enumeration / multi-start local search over treated \(m\)-tuples, with the budget presolve and the away-step Frank-Wolfe simplex solver.
Treated-unit selection for synthetic-control experimental design.
Drop-in replacement for the branch-and-bound search in
fast_scm_helpers.fast_scm_bb. It selects the top_K candidate
treated m-tuples that best reproduce the population predictor vector,
i.e. that minimise the imbalance
- L(S) = min_{w in simplex(S)} || sum_{j in S} w_j Xtilde_j ||^2
= w’ G_SS w, G = Xtilde’ Xtilde (PSD Gram matrix)
where Xtilde are the f-centred, standardised predictors over the
estimation window E. Because the design is f-centred, the population
target is the donor centroid (the origin), so L is the squared
distance from the origin to the convex hull of the selected donors and
sqrt(L) is the achieved imbalance.
Why this is a search and not an exact MIP#
Abadie & Zhao (2026, “Synthetic Controls for Experimental Design”, p.13)
do not require global optimality of the weights – only that the
chosen design is feasible and approximately balanced,
Xbar - sum_j w_j X_j ~= 0. Validity of the bias bound and the
inference is conditional on the achieved imbalance, a goodness-of-fit
quantity reported here, not on a certificate of optimality.
Moreover, an exact bound is structurally weak: because the origin lies inside the convex hull of all candidates, the convex relaxation lower bound is ~0 over the upper half of any branch-and-bound tree, so it cannot prune there. We therefore:
enumerate exactly when
C(M, m)is small (the gold standard), andotherwise run a strengthened multi-start local search, which in Monte Carlo lands on the exact optimum 83-100% of the time and within ~1% (mean) / ~7% (worst) of the minimal imbalance – immaterial under the approximate-balance criterion above.
The achieved imbalance of every returned design is reported so the
analyst can check the ~= 0 condition that actually licenses the
method.
- class mlsynth.utils.fast_scm_helpers.lexsearch.TreatedDesign(indices: 'List[int]', weights: 'np.ndarray', loss: 'float', imbalance: 'float', total_cost: 'float' = 0.0, label: 'str' = '', labels: 'List[Any]' = <factory>, weight_dict: 'Dict[Any, float]' = <factory>, full_weights: 'Optional[np.ndarray]' = None)#
-
- weights: ndarray#
- mlsynth.utils.fast_scm_helpers.lexsearch.select_treated_designs(G: ndarray, candidate_idx: Sequence[int], m: int, top_K: int = 20, *, unit_costs: ndarray | None = None, budget: float | None = None, method: str = 'auto', enumerate_max: int = 3000000, n_starts: int = 16, iters: int = 80, random_state: int = 0, unit_index: Any | None = None, conflict: ndarray | None = None, strata: ndarray | None = None, min_per_stratum: int | None = None, max_per_stratum: int | None = None, size_band: Any | None = None, targeting_penalty: float = 0.0, min_treated_weight: float = 0.0, forced: Sequence[int] | None = None) Dict[str, Any]#
Select the
top_Ktreated m-tuples with smallest imbalance.targeting_penalty(gamma >= 0) makes the design weakly targeted: it addsgamma || w - (1/m) 1 ||^2to the treated QP, pulling the weights toward the group’s own equal-weight aggregate so the treated group looks more like itself than like the population mean (trading some population representativeness – the ATE – for a deployable equal-weight group closer to its own ATT). On the simplex this is exactly a diagonal ridge,min_w w'(G_SS + gamma I) w; the reportedimbalanceremains the true targeting distancesqrt(w' G w)at the penalizedw.gamma=0(the default) is the fully targeted design, unchanged.- Parameters:
G ((n, n) PSD Gram matrix of f-centred standardised predictors.)
candidate_idx (indices eligible for treatment.)
m (number of treated units per design.)
top_K (how many designs to return.)
unit_costs, budget (optional per-unit costs and total budget.)
method (“enumerate” forces exact; “heuristic” forces local search;) – “auto” enumerates when C(M, m) <=
enumerate_maxelse searches.
- Returns:
dict –
Dictionary with:
"top_designs" : list[TreatedDesign] sorted by imbalance. "stats" : diagnostics, incl. whether the result is the exact global top-K and the achieved-imbalance range.
Control fit (Stage 2)#
- mlsynth.utils.fast_scm_helpers.fast_scm_control.evaluate_candidates(candidates: List[Solution], X: ndarray, X_E: ndarray, Y: ndarray, f: ndarray, E_idx: ndarray, B_idx: ndarray, lambda_penalty: float, index_set: IndexSet, conflict: ndarray | None = None) List[SEDCandidate]#
Evaluate candidate treated-unit subsets by constructing synthetic controls.
- Parameters:
candidates (list of Solution) – Candidate subsets from branch-and-bound, each containing indices and weights.
X (np.ndarray, shape (T, N)) – Full feature matrix (outcomes + covariates).
X_E (np.ndarray, shape (T_E, N)) – Standardized feature matrix over the estimation period.
Y (np.ndarray, shape (T, J)) – Outcome matrix.
f (np.ndarray, shape (N,)) – Weight vector used to construct the target series.
E_idx (np.ndarray) – Indices for estimation period.
B_idx (np.ndarray) – Indices for validation/backcast period.
lambda_penalty (float) – Regularization strength for control QP.
- Returns:
results (list of SEDCandidate) – Evaluated candidates including weights, predictions, and loss metrics.
Notes
For each candidate subset:
Construct synthetic treated unit:
treated_vec_E = X_E[:, treated_idx] @ w
Solve for synthetic control weights
v.Compute full time-series:
synthetic treated
synthetic control
treatment effects
Evaluate fit using NMSE on estimation and validation periods.
Additional Details#
Target series is defined as:
target = X[:, :J] @ f[:J]
(weighted average over outcome units).
Candidates with failed QP solves are skipped.
Results are packaged into structured SEDCandidate objects.
Power / MDE (Stage 3)#
Moving-block placebo minimum-detectable-effect analysis.
Power / minimum-detectable-effect (MDE) analysis for LEXSCM designs.
Rebuilt to be a consistent nonparametric permutation analysis, replacing the
earlier version whose null was quasi-empirical while its alternative was a pure
Gaussian shift (an internally inconsistent power calc), and whose effect was
normalised by mean(synth_treated[-n_post:]) floored at 1e-8 (which exploded
on zero-mean outcomes).
Design#
One residual model for null and alternative. Both are built by moving-block resampling the placebo residuals (the held-out blank-period gaps treated - control, which are pure noise under H0), so serial dependence is preserved – matching Abadie & Zhao’s time-series-robust inference.
MDE in standard-deviation units (effect size), the scale used by Vives-i-Bastida (“detect effects > 0.1 s.d.”), plus the absolute outcome-unit value. No division by a fragile level, so zero-mean outcomes are fine.
Adaptive effect grid: the search extends until
power_targetis reached (or a cap), so a detectable-but-large effect never returns NaN.Multi-series placebo pooling: pass the candidate’s blank gaps and the donor-unit placebo gaps; the null resamples within a randomly chosen series, giving the paper’s cross-unit placebo distribution.
Test statistic: S(e) = mean(|e_t|) over the post window (matches the
permutation test used for inference).
- mlsynth.utils.fast_scm_helpers.lexpower.block_resample_windows(series_list, n_post, n_draws, block_len, rng)#
Return an (n_draws, n_post) array of moving-block resampled windows.
Each draw: pick a random placebo series, then tile moving blocks (with wraparound) until the window has length
n_post. Preserves within-series autocorrelation and pools across series.
- mlsynth.utils.fast_scm_helpers.lexpower.compute_mde(noise_pool, n_post: int, *, alpha: float = 0.05, power_target: float = 0.8, block_len: int | None = None, n_null: int = 4000, n_power: int = 2000, max_sd: float = 8.0, n_grid: int = 64, random_state: int = 0, baseline: float | None = None, baseline_floor: float | None = None) Dict#
Minimum detectable (constant) effect at horizon
n_post.- Parameters:
noise_pool (1D array, or list of 1D arrays) – Placebo residual series (held-out blank-period gaps; optionally also the donor-unit placebo gaps). Outcome units.
n_post (int) – Post-treatment window length.
alpha, power_target (float) – Test level and target power (e.g. 0.05 and 0.80).
block_len (int, optional) – Moving-block length; default ~ L**(1/3).
max_sd (float) – Largest effect (in residual-SD units) the adaptive grid will probe.
baseline (float, optional) – Counterfactual outcome level the effect is expressed relative to (e.g.
mean(synthetic_treated[-n_post:])). When supplied, a percentage MDE is reported – the manager-facing “we can detect a Y% effect” number.baseline_floor (float, optional) – Minimum
abs(baseline)for which a percentage is trustworthy; defaults to one residual SD (sigma). Below the floormde_pctisNaN, deliberately, so zero-mean / sign-flipping outcomes cannot reproduce the spurious blow-up that motivated dropping level-normalisation here.
- Returns:
dict –
Dictionary with:
mde_sd : MDE in residual-standard-deviation units (np.inf if not reached within ``max_sd``). mde_abs : MDE in outcome units (= mde_sd * sigma). mde_pct : MDE as a percentage of ``baseline`` (NaN if no baseline was given or it falls below ``baseline_floor``). baseline : the level used for the percentage (NaN if none). sigma : residual SD scale. c_alpha : critical value of the placebo null statistic. power_at_mde: achieved power at the reported MDE. feasible : whether power_target was reached within max_sd. curve : list of (effect_sd, power) probed.
- mlsynth.utils.fast_scm_helpers.lexpower.detectability_curve(noise_pool, n_post_grid, *, baseline_series=None, **kw) Dict#
MDE at each horizon in
n_post_grid.Returns
{'details', 'curve_sd', 'curve_pct', ...}. Ifbaseline_series(the synthetic-treated counterfactual level) is supplied, each horizonwalso gets a percentage MDE relative tomean(baseline_series[-w:])– the level over the matching post window. Seecompute_mde()for the guard that returnsNaNwhen that level is not a trustworthy magnitude.
Recommendation (Stage 4)#
Lexicographic design selection.
Final design recommendation for LEXSCM (lexicographic selection).
Replaces the old select_best_tuple, whose efficiency score multiplied an
MDE that was often NaN/Inf and whose Pareto frame could empty out and raise an
opaque IndexError.
The selection is lexicographic, matching the method’s premise (Abadie & Zhao: approximate balance first, then power):
Validity gate. Keep designs whose imbalance is within
imbalance_tol(relative) of the best achievable balance – the set of “good treated fits”.Power. Among the gated designs, pick the smallest MDE (most detectable).
Tie-breaks. Then better out-of-sample stability, then lower cost.
If no gated design has a feasible MDE, it still returns the best-by-balance
design and flags status='POWER_NOT_ESTABLISHED' instead of crashing. A
Pareto frontier (imbalance vs MDE) is always exposed for transparency.
- class mlsynth.utils.fast_scm_helpers.lexselect.DesignMetrics(design_id: 'str', indices: 'List[int]', imbalance: 'float', mde_sd: 'float' = inf, mde_abs: 'float' = inf, mde_pct: 'float' = nan, mde_feasible: 'bool' = False, stability: 'float' = nan, total_cost: 'float' = 0.0, labels: 'List[Any]' = <factory>)#
- class mlsynth.utils.fast_scm_helpers.lexselect.Recommendation(winner: 'Optional[DesignMetrics]', shortlist: 'List[DesignMetrics]', pareto_ids: 'List[str]', status: 'str', explanation: 'str', table: 'List[Dict[str, Any]]')#
-
- shortlist: List[DesignMetrics]#
- winner: DesignMetrics | None#
- mlsynth.utils.fast_scm_helpers.lexselect.select_design(designs: List[DesignMetrics], *, imbalance_tol: float = 0.25, imbalance_abs: float | None = None, max_shortlist: int = 5) Recommendation#
Lexicographic recommendation: balance gate -> power -> stability -> cost.
Data preparation and matrix construction#
Utilities for constructing the design matrices, the population target, the estimation/blank split, and the Gram matrix.
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.build_X_tilde(X: ndarray, f: ndarray, idx: ndarray)#
Standardize the feature matrix X over the estimation period, returning a normalized matrix and its Gram matrix.
- Parameters:
X (np.ndarray, shape (T, N)) – Full feature matrix including outcomes and covariates.
f (np.ndarray, shape (N,)) – Weight vector (typically group means) used for centering.
idx (np.ndarray, shape (T_E,)) – Indices corresponding to the estimation period.
J (int) – Number of outcome columns in X (the first J columns).
- Returns:
X_E (np.ndarray, shape (T_E, N)) – Standardized X over the estimation period. Each row is centered by the weighted mean mu = X[:, :J] @ f[:J] and scaled by per-time standard deviation across all columns.
G (np.ndarray, shape (N, N)) – Gram matrix of the standardized X: G = X_E.T @ X_E.
Notes
Standardization is performed only over the estimation period rows (idx).
Weighted mean is applied to the first J columns (outcome variables) only.
Rows with near-zero standard deviation are scaled with sigma=1.0 to avoid division by zero.
This function is fully vectorized using NumPy for efficiency.
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.build_Y_matrix(working_df, outcome, time, unitid, unit_index)#
Construct outcome matrix in (time × unit) format.
- Parameters:
working_df (pd.DataFrame) – Input panel data.
outcome (str) – Outcome variable name.
time (str) – Time index column.
unitid (str) – Unit identifier column.
unit_index (IndexSet) – Ordered unit index.
- Returns:
np.ndarray, shape (T, J) – Wide-format outcome matrix.
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.build_Z_matrix(working_df, covariates, time, unitid, unit_index)#
Construct stacked covariate matrix, collapsing time-invariant variables to a single row to prevent unintended over-weighting.
- Parameters:
working_df (pd.DataFrame) – Input panel data.
covariates (list of str) – List of covariate column names.
time (str) – Time column.
unitid (str) – Unit identifier column.
unit_index (IndexSet) – Unit ordering.
- Returns:
np.ndarray or None – Stacked covariate matrix (N rows x Units).
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.build_candidate_mask(working_df, candidate_col, unit_index, unitid)#
Construct boolean mask identifying candidate units.
- Parameters:
working_df (pd.DataFrame) – Preprocessed panel data.
candidate_col (str) – Column indicating eligibility for treatment.
unit_index (IndexSet) – Full set of unit labels.
unitid (str) – Unit identifier column name.
- Returns:
np.ndarray – Boolean mask of candidate units aligned with unit_index.
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.build_f_vector(working_df, weight_col, unitid, unit_index)#
Construct unit weighting vector.
- Parameters:
working_df (pd.DataFrame) – Input dataset.
weight_col (str or None) – Optional column defining unit weights.
unitid (str) – Unit identifier column.
unit_index (IndexSet) – Unit ordering.
- Returns:
np.ndarray – Normalized weight vector over units.
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.prepare_experiment_inputs(Y: ndarray, Z: ndarray | None = None, f: ndarray | None = None, candidate_mask: ndarray | None = None, m: int = 5) Tuple[ndarray, ndarray, ndarray, int, int]#
Combine inputs into a unified feature matrix and aligned selection structures.
- Parameters:
Y (np.ndarray, shape (T, J)) – Outcome matrix.
Z (np.ndarray, optional) – Covariate matrix. If provided, concatenated with Y along columns.
f (np.ndarray, optional) – Weight vector of length N. Defaults to uniform weights if None.
candidate_mask (np.ndarray of bool, optional) – Boolean mask indicating candidate columns. If shorter than N, it will be extended with False values. If None, defaults to selecting only Y columns.
m (int, default=5) – Minimum required number of candidate units.
- Returns:
X (np.ndarray, shape (T, N)) – Combined feature matrix (Y and optionally Z).
f (np.ndarray, shape (N,)) – Weight vector aligned with X.
candidate_idx (np.ndarray) – Indices of candidate columns in X.
T (int) – Number of time periods.
N (int) – Total number of columns in X.
- Raises:
ValueError – If candidate_mask is longer than N or if fewer than m candidates are available.
Notes
Candidate mask is automatically extended to match the full feature dimension.
By default, only outcome columns (Y) are considered candidates.
- mlsynth.utils.fast_scm_helpers.fast_scm_setup.split_periods(T0: int, n_covariates: int, frac_E: float = 0.7, post_df: DataFrame | None = None, time_col: str = 'time') Tuple[ndarray, ndarray, ndarray, int, int]#
Returns indices and time-period counts.
Standardized post-fit (shared across the MAREX family) – the
compute_post_fit() and
compute_power_analysis() helpers that
populate the post-fit bundle live outside this package so SYNDES, MAREX,
and PANGEO all consume the same diagnostics module:
Standardized post-fit diagnostics for synthetic control designs and the matching power-analysis surface that consumes them.
After any MAREX-family estimator (LEXSCM, MAREX, SYNDES, PANGEO, …) solves its design problem, downstream consumers (the SAGE dashboard, paper-style reports, comparison tables) all need the same numbers:
the post-treatment ATT, total effect, percentage lift, per-period gap;
pre / blank / post root-mean-squared-error of the synthetic gap;
inference scalars (p-value, CI bounds) when computed;
covariate-balance standardized mean differences (SMDs) when covariates were used in the design.
This module exposes one frozen dataclass (SyntheticControlPostFit)
and three free functions:
compute_smd()– standalone, panel-independent SMDfrom any (cov_matrix, treated_w, control_w);
compute_post_fit()– the full diagnostic bundle fromtrajectories + boundaries + (optional) covariate matrix + (optional) inference;
compute_post_fit_marex()– adapter that builds the bundle from a
MAREXResults+MAREXPanelpair.
The free-function entry points are deliberately small and reusable, so the LEXSCM / SYNDES / PANGEO equivalents can be added one-at-a-time without touching this module: they just compose the same primitives.
- class mlsynth.utils.post_fit.MDEPoint(post_periods: int, mde_absolute: float, mde_pct: float, se: float, power_at_observed: float | None = None, critical_value: float = nan, mde_ci: Tuple[float, float] = (nan, nan))#
Bases:
objectMinimum detectable effect at a single post-treatment horizon.
- class mlsynth.utils.post_fit.PowerAnalysis(headline: MDEPoint, curve: Tuple[MDEPoint, ...], alpha: float, power_target: float, sigma_placebo: float, serial_correlation: float, baseline: float, method: str = 'analytical_ar1_mean_gap', placebo_bias: float = 0.0, placebo_bias_pvalue: float = nan, n_placebo: int = 0, sigma_ci: Tuple[float, float] = (nan, nan))#
Bases:
objectStandardized power-analysis output attached to
SyntheticControlPostFit.Built from the placebo / blank-period gap variance and an analytical Gaussian approximation, with AR(1) variance inflation to handle serial correlation in the gap residuals. The intent matches the per-estimator power modules already in the library (PangeoPower, SPCDPowerAnalysis, SYNDESPower) but consumes the same
SyntheticControlPostFitshape so every covariate-aware SCM-family estimator gets the surface for free.- curve#
MDE / power values across the requested
post_gridhorizons (so callers can read a detectability curve).
- sigma_placebo#
Standard deviation of the placebo gap series, about that window’s own mean, used as the noise scale. The offset itself is reported separately as
placebo_biasand enters the standard error there.- Type:
- serial_correlation#
Lag-1 (AR(1)) autocorrelation of the placebo gap residuals, estimated on the demeaned series and small-sample corrected, used to inflate the variance for serial dependence.
- Type:
- placebo_bias#
Mean of the placebo gap. Zero for a design that is unbiased on the window it was not fitted to; otherwise the offset the MDE must clear alongside the noise.
- Type:
- sigma_ci#
Chi-square confidence interval for
sigma_placeboatalpha, on the placebo window’s effective degrees of freedom.
- baseline#
Mean of the control trajectory on the post window (denominator for
mde_pct). NaN when no post window exists.- Type:
- method#
"analytical_ar1_mean_gap": the closed-form MDE for a two-sided test on the mean post-period gap, with AR(1) variance inflation and a Student-t critical value. This is a different test from the moving-block permutation onmean|e|that LEXSCM’s design search ranks candidates by (Vives-i-Bastida 2022, Section 2), so the two MDEs need not agree. Reserved for future"monte_carlo"extensions.- Type:
- class mlsynth.utils.post_fit.SyntheticControlPostFit(treated_series: ndarray, control_series: ndarray, gap_series: ndarray, n_fit: int, n_blank: int, n_post: int, ate: float | None = None, total_effect: float | None = None, ate_percent: float | None = None, ate_per_period: ndarray | None = None, cumulative_effect: ndarray | None = None, p_value: float | None = None, ci_lower: float | None = None, ci_upper: float | None = None, inference_method: str | None = None, rmse_fit: float | None = None, rmse_blank: float | None = None, rmse_post: float | None = None, covariate_names: Tuple[str, ...] = (), covariate_smd: Dict[str, float] | None = None, covariate_smd_abs_max: float | None = None, covariate_smd_squared_sum: float | None = None, covariate_smd_treated_vs_pop: Dict[str, float] | None = None, covariate_smd_treated_vs_pop_abs_max: float | None = None, covariate_smd_treated_vs_pop_squared_sum: float | None = None, covariate_smd_control_vs_pop: Dict[str, float] | None = None, covariate_smd_control_vs_pop_abs_max: float | None = None, covariate_smd_control_vs_pop_squared_sum: float | None = None, power: PowerAnalysis | None = None)#
Bases:
objectStandardized post-fit diagnostics for a single synthetic control design.
Field semantics are estimator-agnostic; every MAREX-family adapter populates the same shape. Any field that isn’t naturally computable for the producing estimator is left
None.- control_series: ndarray#
- gap_series: ndarray#
- power: PowerAnalysis | None = None#
- treated_series: ndarray#
- mlsynth.utils.post_fit.compute_post_fit(treated_series: ndarray, control_series: ndarray, *, n_fit: int, n_blank: int = 0, n_post: int | None = None, cov_matrix: ndarray | None = None, cov_names: Sequence[str] | None = None, cov_scales: ndarray | None = None, treated_weights: ndarray | None = None, control_weights: ndarray | None = None, population_weights: ndarray | None = None, inference: Any | None = None, n_treated_units: int | None = None) SyntheticControlPostFit#
Compute a
SyntheticControlPostFitfrom trajectories + boundaries.The trajectories
treated_seriesandcontrol_seriesare the estimator’s own synthetic constructs (Σⱼ wⱼ Yⱼ and Σⱼ vⱼ Yⱼ in Abadie-Zhou notation).n_postdefaults tolen(treated_series) - n_fit - n_blank.Covariate balance fields are populated when
cov_matrix+treated_weights+control_weightsare all supplied (the natural inputs for any MAREX-family design). Thecompute_smd()helper does the work, so the SMD numbers are exactly consistent with a standalone call tocompute_smd().Inference scalars are pulled from the estimator’s inference object via
_extract_inference(), which knows about the four common shapes (LEXSCMInference, MAREXMAREXInference, SYNDESSYNDESInference, or a plain dict). All inference fields are optional.
- mlsynth.utils.post_fit.compute_post_fit_marex(raw, panel, *, cov_scales: ndarray | None = None) SyntheticControlPostFit#
Adapt a
MAREXResults+MAREXPanelpair into aSyntheticControlPostFit.Pulls the aggregate synthetic-treated / synthetic-control trajectories from
raw.globres, the (T0, blank_periods) split frompanel.T0andpanel.blank_periods, the inference object fromraw.globres.inference, and the covariate matrix frompanel.covariates(when present).
- mlsynth.utils.post_fit.compute_post_fit_tbrmm(treated_series, control_series, *, n_fit: int, n_post: int, n_treated_units: int, ci: Tuple[float, float] | None = None, inference_method: str | None = None) SyntheticControlPostFit#
Adapt a measured TBRMM design into a
SyntheticControlPostFit.A sibling of
compute_post_fit_marex(), for the same reason: the generic builder takes two trajectories, and TBR has no weight vector to build them from. Its counterfactual comes from the augmented DiD regression instead, socontrol_serieshere is the fitted projection \(\hat\delta_1 + \hat\delta_2 \bar{y}_{co,t}\) averaged over the treated geos, not a weighted donor combination.There is no blank window: the search holds pretest periods out inside its own A/A gate, and those periods are part of the fit here.
- Parameters:
treated_series, control_series (np.ndarray) – The treated geos’ average observed path and the average of their counterfactuals, over the whole panel.
n_fit (int) – Pretest periods the regressions were fitted on.
n_post (int) – Realized periods measured.
n_treated_units (int) – Treated geos behind the average.
ci (tuple of float, optional) – Lower and upper bound for the realized effect, in the units
ateis reported in. The design supplies TBR’s posterior interval rescaled to the mean per-period effect, soreport.inferenceandreport.effects.attsit on one scale.inference_method (str, optional) – What produced
ci, recorded on the result.
- Returns:
SyntheticControlPostFit – The standard bundle, so
reportis built by the same adapter every other design estimator uses.
- mlsynth.utils.post_fit.compute_power_analysis(post_fit: SyntheticControlPostFit, *, alpha: float = 0.05, power_target: float = 0.8, post_grid: Sequence[int] | None = None) PowerAnalysis#
Analytical MDE + power curve for a design’s
SyntheticControlPostFit.Uses the placebo / blank-period gap residuals (or the pre-period gap when no blank window was carved out) to estimate the noise standard deviation
sigma_placeboand the AR(1) autocorrelationrho, then computes the minimum detectable effect for each horizonTinpost_gridvia the Gaussian formulaMDE(T) = (z_{1-alpha/2} + z_{power}) * sigma_placebo * sqrt(VIF(T, rho)),
where
VIF(T, rho) = Var(mean of T AR(1) periods) / sigma_placebo^2. The headline MDE usesT = post_fit.n_post(the realised post window).- Parameters:
post_fit (SyntheticControlPostFit) – The standardized post-fit from any MAREX-family estimator.
alpha (float, default 0.05) – Two-sided significance level.
power_target (float, default 0.80) – Target power for the MDE.
post_grid (sequence of int, optional) – Post-treatment horizons at which to compute MDE. Defaults to a small geometric grid centered on
post_fit.n_postso users see the detectability tradeoff vs. running the experiment longer.
- Returns:
PowerAnalysis – Headline MDE + a curve over the requested horizons.
- mlsynth.utils.post_fit.compute_smd(cov_matrix: ndarray, treated_weights: ndarray, control_weights: ndarray, *, cov_names: Sequence[str] | None = None, cov_scales: ndarray | None = None) Dict[str, Any]#
Standardized mean differences between weighted treated and control means.
- Parameters:
cov_matrix (ndarray, shape (N, M)) – Per-unit covariate values; rows align to
treated_weightsandcontrol_weights.treated_weights, control_weights (ndarray, shape (N,)) – Non-negative weights with disjoint supports. They are renormalised to sum to 1 internally (so callers may pass raw sums-to-K weights).
cov_names (sequence of str, optional) – Names for the M covariates. Defaults to
("cov_0", "cov_1", ...).cov_scales (ndarray, shape (M,), optional) – Pre-computed per-covariate standardization scales (cross-unit std). Defaults to the std of
cov_matrixcolumns. Passing the value already cached bybuild_covariate_matrixis the right move.
- Returns:
dict with keys
smd(the per-covariate dict),smd_abs_max,and
smd_squared_sum. Returns empty / NaN summaries if either weightvector is all-zero.
- mlsynth.utils.post_fit.to_effect_result(pf: SyntheticControlPostFit, *, time_periods: ndarray | None = None, intervention_time: Any | None = None, method_name: str | None = None, donor_weights: Dict[str, float] | None = None) Any#
Convert a
SyntheticControlPostFitinto a standardizedEffectResult.The single, family-wide adapter from the rich post-fit bundle to the contract’s
EffectResultview, so every MAREX-family estimator (LEXSCM, MAREX, SYNDES, PANGEO) getsreportfor free instead of hand-copying fields. The realized effect’s standard scalars populate the standard sub-models; everything the contract has no slot for (per-period effects, cumulative effect, covariate SMDs, and the fullpost_fitobject itself) is carried inadditional_outputsso it remains discoverable.
Cumulative effects (post-intervention)#
The cumulative effect path and its block-sum interval, the approximability gate that says whether that interval is centred, and the Unit-level and population estimands.
Post-inference update module for synthetic experimental design.
- class mlsynth.utils.fast_scm_helpers.post_inference.Approximability(bias: float, scale: float, t_stat: float, p_value: float, ok: bool, serial_correlation: float = 0.0, effective_n: float = 0.0)#
Whether the donor pool can reproduce the treated unit off-sample.
- class mlsynth.utils.fast_scm_helpers.post_inference.CumulativePoint(horizon: int, estimate: float, lower: float, upper: float)#
The cumulative effect through one horizon, with its interval.
- class mlsynth.utils.fast_scm_helpers.post_inference.PopulationCumulative(aggregate: List[CumulativePoint], effect_dispersion: float, weight_distance: float, estimand: str = 'population', dropped_treated: Tuple[int, ...] = (), dropped_extra: Tuple[int, ...] = ())#
A cumulative path for the population effect, and what it cost to get it.
- aggregate: List[CumulativePoint]#
- class mlsynth.utils.fast_scm_helpers.post_inference.UnitLevelCumulative(aggregate: List[CumulativePoint], per_unit: Tuple[List[CumulativePoint], ...], weights: Tuple[float, ...], estimand: str = 'treated', centered: bool = False, level_scale: Tuple[float, ...] = (), dropped_treated: Tuple[int, ...] = (), dropped_extra: Tuple[int, ...] = ())#
A cumulative path per treated unit, and the aggregate they compose.
- aggregate: List[CumulativePoint]#
- dropped_treated: Tuple[int, ...] = ()#
Treated units whose own gap failed the approximability gate and was therefore kept out of every other unit’s null.
- per_unit: Tuple[List[CumulativePoint], ...]#
- mlsynth.utils.fast_scm_helpers.post_inference.approximability(blank_gaps: Sequence[float], *, threshold: float = 3.0) Approximability#
Test the held-out gap for a location offset.
The cumulative interval is centred only when the fitted weights reproduce the treated unit on periods they were not fitted to. A miss on those periods runs through the post window as a constant, reads as a treatment effect, and no resampling of residuals removes it. Read this before the interval, not beside it.
The statistic predicts interval failure better than the convex-hull condition it stands in for. Over 700 designs whose hull membership is verified by a linear program, the gate admits 81 per cent of them with coverage 0.875 and refuses the rest at 0.540; splitting the same designs on hull membership admits 47 per cent at 0.897 and leaves 0.732 in the refused half. Hull membership is the wrong cut because what decides the interval is the size of the realized offset against the noise: a design just outside the hull carries an offset too small to matter, and a design inside it can be fitted badly enough to carry a large one. The two overlap heavily – the largest in-hull statistic is 8.2, the smallest out-of-hull one is 0.001, and 82 per cent of out-of-hull designs fall below 5.5 – so no threshold sorts designs by hull membership, and this one does not try to.
Serially correlated periods carry less information than independent ones, so the offset is tested on the Bartlett effective sample size
n (1 - rho) / (1 + rho)and not the raw period count. On zero-offset gaps of 20 periods the raw count refuses 0.007 of designs atrho = 0, 0.043 at 0.3, 0.173 at 0.6 and 0.385 at 0.8, against the 0.003 a correctly sized test would refuse at this threshold; the correction brings the two dependent cases to 0.060 and 0.125.The effective count is clipped at the period count, because
(1 - rho)/(1 + rho)is convex and a lag-one estimate scattered about zero averages above one – 1.24 n over 20 periods, 1.6 n over 10 – which would charge the offset against more information than the window holds. Without the clip the test refuses 0.019 of centred 20-period windows against the raw count’s 0.007, and 0.046 of 10-period windows against 0.014. With it the refusals are 0.005 and 0.011, and the cases the correction exists for are untouched: 0.059 atrho = 0.6against 0.060 unclipped. The statistic is monotone in the count, so clipping can only move it toward zero and the clip removes refusals without adding any.
- mlsynth.utils.fast_scm_helpers.post_inference.cumulative_path(post_gaps: Sequence[float], placebo_pool: Sequence[Sequence[float]], *, level: float = 0.9, standardize: bool = True) List[CumulativePoint]#
The cumulative effect through each horizon, with a block-sum interval.
post_gapsare the treated unit’s post-period gaps.placebo_poolholds held-out residual series, the first being the treated unit’s own and setting the scale for the rest.The point estimate is arithmetic – the running sum of the gaps. Only the interval is inferential, and it inverts the distribution of the sum over blocks of the same length drawn from the pool. Because the sum over a window is the window length times its mean, an interval for one rescales exactly into an interval for the other; the dependence between periods is already priced by the blocks.
Coverage holds under moderate serial dependence and degrades under strong dependence: 0.915 at an AR(1) coefficient of 0, 0.881 at 0.3 and 0.863 at 0.6, against a nominal 0.90. Blocks absorb dependence shorter than the horizon they span, and at 0.6 some outlives them.
The estimand is the effect on the treated aggregate the gaps were formed from, Abadie and Zhao’s
tau^T. It is not the population effect. Under an experimental design whose treated units are weighted to represent a wider population, the two separate as soon as the effect varies across units: at a cross-unit standard deviation of 0.8 around a mean effect of 1.0 they differ by 0.30 per period, and this interval covers the treated effect 0.880 of the time and the population effect 0.714. Reporting the result as a national number needs a second term for the representation error, which this does not carry.
- mlsynth.utils.fast_scm_helpers.post_inference.population_cumulative(post_gaps: ndarray, blank_gaps: ndarray, weights: Sequence[float], population_weights: Sequence[float], treated_index: Sequence[int], *, level: float = 0.9, center: bool = False, extra_pool: Sequence[Sequence[float]] | None = None, screen_pool: bool = True) PopulationCumulative#
The cumulative effect on the population, not on the treated group.
The gap estimates
tau^T = w . tau. The population effect istau = f . tau, and the two differ bysum_j (w_j - f_j) tau_j. Both weight vectors sum to one, so when the per-unit effects are exchangeable with a common mean that difference has expectation zero: it needs a variance and not a bias correction. Its variance isvar(tau_j) * || w - f ||^2, and the Unit-level design supplies the first factor, since it estimates an effect for every treated unit.The point estimate is therefore the same as
unit_level_cumulative()’s; only the interval widens. Overhperiods a constant per-period offset accumulates ash, so the representation term enters the variance ash^2against the gap term’shand the widening grows with the horizon. That is the shape of the shortfall it exists to repair: carrying only the gap term, the interval coverstau^T0.880 of the time andtau0.714 at a nominal 0.90, on a design whose per-unit effects have standard deviation 0.8 about a mean of 1.0.Measured against a known truth on a four-unit design, the term takes population coverage from 0.775 to 0.895 at a nominal 0.90, costing 40 per cent more width. Where the effects are in fact identical it costs 8 per cent more width for nothing and covers 0.931 against 0.913, because the spread estimate retains about 0.13 it cannot resolve: the correction errs wide.
Two assumptions carry it, and neither is testable from the treated units alone. The untreated units’ effects are drawn from the same distribution as the treated ones, which is what makes the offset mean zero. And
var(tau_j)is read off however many units were treated, so a design with two or three of them estimates it poorly; the interval is then honest about the correction’s form and vague about its size.
- mlsynth.utils.fast_scm_helpers.post_inference.unit_level_cumulative(post_gaps: ndarray, blank_gaps: ndarray, weights: Sequence[float], *, level: float = 0.9, center: bool = False, extra_pool: Sequence[Sequence[float]] | None = None, screen_pool: bool = True) UnitLevelCumulative#
Cumulative paths under Abadie and Zhao’s Unit-level design, equation (10).
That design fits the treated aggregate to the population and each treated unit to its own synthetic control at once, so the estimate decomposes by their equation (11): the aggregate gap is the weighted mean of the per-unit gaps. Both are returned, and the aggregate is computed from the parts, so a breakdown and a headline cannot contradict each other.
post_gapsandblank_gapsare period-by-unit. Each unit’s own interval reads its null from the other treated units, which the design has already built proper synthetic controls for;extra_pooladds further held-out series, which a small treated group needs because a handful of short series leaves the tail quantiles thin.The aggregate and the per-unit paths do not respond to that alike. Measured on a four-unit design at a nominal 0.90, with the number of extra series running 0, 4, 10, 20, the aggregate covers 0.874, 0.901, 0.913, 0.932 and each unit covers its own effect 0.774, 0.805, 0.833, 0.852. Four extra series bring the aggregate to its nominal level and twenty carry it past, so a thicker pool is not uniformly better.
A thicker pool leaves the per-unit paths short of their nominal level, because one treated unit’s synthetic control sits at its own fitted level and no resampling shifts a location offset.
centersubtracts each unit’s blank-window mean from both windows and charges the noise in that mean back as the path accumulates, whichlevel_scalereports.It is off by default, because removing the offset costs more than it buys. Measured at a nominal 0.90 on a four-unit design: the aggregate covers 0.913 without it and 0.858 with it, the population 0.895 and 0.849, and the per-unit paths 0.833 and 0.838. The offsets also sit in the residuals the null is built from, so taking them out contracts the null – the aggregate interval narrows by a third – and the bias removed is worth less than the width lost, except marginally for a single unit, whose offset is largest relative to its noise. Reach for it only for a per-unit reading.
Centring also assumes the offset is the same in both windows, which
approximability()tests on the blank gap.The estimand is
tau^T, the weighted effect on the treated. Seecumulative_path()for why that is not the population effect.
Example: choosing treated markets under a budget#
This end-to-end example simulates a panel of sales markets, flags a subset as treatment-eligible with heterogeneous treatment costs, and lets LEXSCM choose which \(m = 3\) markets to treat under a fixed budget – then inspects the solver-style search diagnostics and the lexicographic recommendation.
Generate a synthetic panel#
import numpy as np
import pandas as pd
from mlsynth import LEXSCM
def generate_synthetic_sales_panel(
n_units: int = 100,
n_time_periods: int = 100,
n_candidates: int = 40,
treatment_start: int = 80,
seed: int = 42,
sigma: float = 3.0,
sales_scale: float = 100.0,
) -> pd.DataFrame:
np.random.seed(seed)
unit_fe = np.random.normal(0, 8, size=n_units)
unit_trend = np.random.normal(0.3, 0.15, size=n_units)
unit_sensitivity = np.random.uniform(0.8, 1.2, size=n_units)
t = np.arange(n_time_periods)
common_factor = (
1.5 * t + 120
+ 12 * np.sin(2 * np.pi * t / 52)
+ np.random.normal(0, 3.5, n_time_periods)
)
sales = np.zeros((n_time_periods, n_units))
for j in range(n_units):
sales[:, j] = (
sales_scale
+ common_factor * unit_sensitivity[j]
+ unit_fe[j]
+ unit_trend[j] * t
+ np.random.normal(0, sigma, n_time_periods)
)
sales = np.maximum(sales, 5.0)
# heterogeneous per-market treatment costs
base_cost = np.random.lognormal(mean=12, sigma=0.6, size=n_units) * 5
size_factor = np.random.uniform(0.7, 1.4, n_units)
treatment_cost = np.round((base_cost * size_factor) / 10) * 10
unit_ids = np.repeat(np.arange(n_units), n_time_periods)
time_ids = np.tile(np.arange(n_time_periods), n_units)
sales_flat = sales.ravel(order="F")
df = pd.DataFrame({
"unitid": unit_ids,
"time": time_ids,
"sales": sales_flat,
"treatment_cost": np.repeat(treatment_cost, n_time_periods),
})
candidate_mask = np.zeros(n_units, dtype=bool)
candidate_idx = np.random.choice(n_units, size=n_candidates, replace=False)
candidate_mask[candidate_idx] = True
df["candidate"] = np.repeat(candidate_mask, n_time_periods)
df["post"] = (df["time"] >= treatment_start).astype(int)
return df
Run LEXSCM and inspect the design#
df = generate_synthetic_sales_panel(
n_units=120,
n_candidates=40,
n_time_periods=100,
treatment_start=90,
seed=4545,
)
config = {
"df": df,
"outcome": "sales",
"unitid": "unitid",
"time": "time",
"candidate_col": "candidate",
"m": 3, # treat 3 of the 40 eligible markets
"post_col": "post",
"unit_cost_col": "treatment_cost",
"budget": 4_000_000, # hard knapsack cap on selected costs
"lambda_penalty": 0.5,
"mde_horizon": "late", # conservative MDE at the longest horizon
"power_target": 0.8,
"imbalance_tol": 0.25, # validity gate: within 25% of best balance
"top_K": 20,
}
results = LEXSCM(config).fit()
# --- Stage 1: solver-style search diagnostics ---
stats = results.search.selection["stats"]
print("status :", stats["termination"]["status"]) # OPTIMAL / FEASIBLE
print("method :", stats["search"]["method"])
print("subsets scored :", stats["search"]["subsets_evaluated"])
print("C(M,m) :", stats["problem"]["feasible_region_C(M,m)"])
print("best imbalance :", round(stats["incumbent"]["imbalance"], 4))
# --- Stage 4: lexicographic recommendation tuple ---
rec = results.search.selection["recommendation"]
print("rec status :", rec["status"]) # OK / POWER_NOT_ESTABLISHED
print("winner :", rec["winner"])
print("pareto ids :", rec["pareto_ids"])
print(rec["explanation"])
# --- the recommended design ---
print(results.search.shortlist) # ranked per-design table
best = results.search.winner
print("treated weights:", best.treated_weight_dict)
print("control weights:", best.control_weight_dict)
With \(M = 40\) eligible markets and \(m = 3\),
\(\binom{40}{3} = 9{,}880\) is well under enumerate_max, so the
search enumerates exactly and reports status = OPTIMAL – a
certified global top_K. Raising \(M\) and \(m\) until
\(\binom{M}{m}\) exceeds the cap flips the search to the multi-start
local solver (status = FEASIBLE), at which point the consensus
block under stats["search"] reports how many independent starts agreed
on the incumbent.
Example: spillover-aware selection#
Suppose the markets sit inside larger regions and treating two markets in the
same region would let the intervention spill across them (and onto each other’s
donors). Assign every unit a region and pass it as cluster_col: LEXSCM
then refuses to co-treat two markets from the same region (Stage 1) and
refuses to build a treated market’s synthetic control from its own-region peers
(Stage 2).
import numpy as np
df = generate_synthetic_sales_panel(n_units=120, n_candidates=40,
n_time_periods=100, treatment_start=90,
seed=4545)
# assign each unit to one of 8 regions (constant within unit)
rng = np.random.default_rng(0)
region_of = rng.integers(0, 8, size=df["unitid"].nunique())
df["region"] = df["unitid"].map(lambda u: f"R{region_of[u]}")
config = {
"df": df, "outcome": "sales", "unitid": "unitid", "time": "time",
"candidate_col": "candidate", "m": 3, "post_col": "post",
"cluster_col": "region", # <-- the only new line vs the budget example
"mde_horizon": "late", "top_K": 20,
}
results = LEXSCM(config).fit()
treated = list(results.selected_units)
treated_regions = {df.set_index("unitid")["region"].loc[int(u)] for u in treated}
donor_regions = {df.set_index("unitid")["region"].loc[int(d)]
for d in results.design_weights.donor_weights}
print("treated markets :", treated)
print("treated regions :", treated_regions) # all distinct -- one per region
print("donor regions :", donor_regions)
assert treated_regions.isdisjoint(donor_regions) # donors avoid treated regions
Instead of clusters you can hand LEXSCM a spillover/adjacency matrix – a
pandas.DataFrame indexed and columned by unit id, with a positive entry
wherever two units interfere – via adjacency=... and an optional
spillover_threshold (units conflict when the entry exceeds it). A
cluster_col and an adjacency may be supplied together; their conflicts
combine. If the constraint admits no conflict-free m-tuple (for instance
m larger than the number of regions), the fit raises
MlsynthConfigError.
A ready-made matrix ships with mlsynth – you don’t need a shapefile or GIS
toolchain. basedata/markets/dma_adjacency.csv is a 206 x 206 border matrix
over US Nielsen Designated Market Areas (built from the public TopoJSON via
shared-arc contiguity; see basedata/markets/build_dma_adjacency.py), so
“no two treated markets may share a border” is one read_csv away:
import pandas as pd
adj = pd.read_csv("basedata/markets/dma_adjacency.csv", index_col=0)
meta = pd.read_csv("basedata/markets/dma_metadata.csv") # state, lat, long
se = [n for n in meta.loc[meta.state.isin({"FL","GA","AL","MS","SC","NC","TN"}),
"dma_name"] if n in adj.index]
A = adj.loc[se, se] # induced subgraph
LEXSCM({"df": df, "outcome": "sales", "unitid": "dma", "time": "week",
"candidate_col": "eligible", "m": 4, "adjacency": A}).fit()
On a spatially-correlated Southeast panel this is the difference between an unconstrained design that co-treats a cluster of bordering Gulf-Coast markets (Hattiesburg, Jackson, Mobile-Pensacola, Montgomery – with Biloxi-Gulfport and Dothan as donors, both bordering treated markets) and the spillover-aware design, which spreads treatment across non-adjacent markets and keeps every donor clear of a treated market.
Example: South & Midwest design with grouped factors#
This example exercises every selection constraint at once on real geography. We take the South and Midwest DMAs from the bundled map, group them by census division (South Atlantic, East/West South Central, East/West North Central), and draw outcomes from a grouped linear factor model (after the grouped factor structure of Liao, Shi & Zheng [RelaxSC]): markets in the same division share factor loadings, so they co-move – the latent-group structure the constraints are designed for. Each division is both a stratum (for coverage / quota) and, through the DMA borders, a source of spillover.
Build the panel#
import numpy as np
import pandas as pd
from mlsynth import LEXSCM
DIVISION = { # census divisions across the South + Midwest
"FL": "S. Atlantic", "GA": "S. Atlantic", "NC": "S. Atlantic",
"SC": "S. Atlantic", "VA": "S. Atlantic", "WV": "S. Atlantic",
"MD": "S. Atlantic",
"KY": "E.S. Central", "TN": "E.S. Central", "MS": "E.S. Central",
"AL": "E.S. Central",
"AR": "W.S. Central", "LA": "W.S. Central", "OK": "W.S. Central",
"TX": "W.S. Central",
"OH": "E.N. Central", "IN": "E.N. Central", "IL": "E.N. Central",
"MI": "E.N. Central", "WI": "E.N. Central",
"MN": "W.N. Central", "IA": "W.N. Central", "MO": "W.N. Central",
"ND": "W.N. Central", "SD": "W.N. Central", "NE": "W.N. Central",
"KS": "W.N. Central",
}
meta = pd.read_csv("basedata/markets/dma_metadata.csv")
adj = pd.read_csv("basedata/markets/dma_adjacency.csv", index_col=0)
meta = meta[meta.state.isin(DIVISION)].copy()
meta["division"] = meta.state.map(DIVISION)
names = [n for n in meta.dma_name if n in adj.index] # 145 DMAs
meta = meta[meta.dma_name.isin(names)].reset_index(drop=True)
A = adj.loc[names, names] # border matrix
# --- grouped linear factor model: same division -> shared loadings ---
rng = np.random.default_rng(11)
n, T, T_post, r = len(names), 60, 12, 4
div = meta.set_index("dma_name").loc[names, "division"].values
Lam_div = {d: rng.normal(size=r) for d in sorted(set(div))}
Lam = np.array([Lam_div[d] for d in div]) + 0.15 * rng.normal(size=(n, r))
F = np.cumsum(rng.normal(size=(T, r)), 0) # latent factors
population = np.round(rng.lognormal(12.5, 0.8, n)).astype(int)
cost = population * 5.0 # $5/person intervention cost
Y = 100 + rng.normal(0, 5, n) + F @ Lam.T + rng.normal(0, 1.0, (T, n))
cand = sorted(rng.choice(n, 18, replace=False).tolist()) # eligible markets
df = pd.DataFrame([
{"market": names[j], "week": t, "sales": Y[t, j],
"eligible": int(j in cand), "post": int(t >= T - T_post),
"division": div[j], "population": int(population[j]),
"cost": float(cost[j])}
for j in range(n) for t in range(T)
])
base = dict(df=df, outcome="sales", unitid="market", time="week",
candidate_col="eligible", post_col="post", top_K=8, verbose=False)
Each constraint, one at a time#
# 1. Spillover: no two treated markets share a border, and no donor borders a
# treated market (the DMA adjacency matrix).
LEXSCM({**base, "m": 3, "adjacency": A}).fit()
# 2. Coverage: at least one treated market in EVERY census division
# (m must be >= the 5 divisions).
LEXSCM({**base, "m": 5, "stratum_col": "division", "min_per_stratum": 1}).fit()
# 3. Quota: at most one treated market per division (a spread-out design).
LEXSCM({**base, "m": 4, "stratum_col": "division", "max_per_stratum": 1}).fit()
# 4. Size band: only treat markets at or above the median population.
LEXSCM({**base, "m": 3, "size_col": "population",
"min_size": int(np.median(population))}).fit()
# 5. Compose constraints: cover all five divisions (min 1) AND at most two
# per division (max 2), at m=6 -- exactly one division carries two.
LEXSCM({**base, "m": 6, "stratum_col": "division",
"min_per_stratum": 1, "max_per_stratum": 2}).fit()
# 6. With a size band on top, a division may lose all its eligible markets;
# coverage then only requires the divisions that still HAVE a large enough
# market (here East-South-Central drops out, so four divisions are covered).
LEXSCM({**base, "m": 4, "stratum_col": "division", "min_per_stratum": 1,
"size_col": "population", "min_size": int(np.median(population))}).fit()
With the grouped factors, an unconstrained design tends to concentrate treatment
in whichever division is easiest to balance; the coverage and quota constraints
force it to spread across divisions (so the experiment speaks to the whole
footprint), the adjacency rule keeps treated markets from contaminating each
other, and the size band drops markets too small to power – or too large to
reproduce from the rest. All compose on the same Stage-1 admissible-tuple layer,
and an impossible combination (for example min_per_stratum=1 with m below
the division count) raises MlsynthConfigError.
Under a budget#
Experiments cost money, and the cost is largely driven by market size – a
bigger DMA means a bigger population to treat. Here cost is \(\$5\) per
person, and the program carries a hard \(\$10\text{M}\) knapsack
(unit_cost_col + budget) that composes with everything above.
BUDGET = 10_000_000
bud = {**base, "unit_cost_col": "cost", "budget": BUDGET}
# budget alone: the most balanced affordable design (here ~$6.9M of $10M)
LEXSCM({**bud, "m": 4}).fit()
# budget + coverage: one market per division AND total cost <= $10M -- the
# knapsack pushes the design toward the *cheaper* (smaller) market in each
# division (here ~$8.3M, all five divisions covered)
LEXSCM({**bud, "m": 5, "stratum_col": "division", "min_per_stratum": 1}).fit()
The constraints can genuinely conflict: requiring coverage of all five divisions and only above-median markets and a $10M cap asks for five large (expensive) markets that the budget cannot afford, so the fit raises instead of dropping a criterion.
from mlsynth.exceptions import MlsynthConfigError
try:
LEXSCM({**bud, "m": 5, "stratum_col": "division", "min_per_stratum": 1,
"size_col": "population",
"min_size": int(np.median(population))}).fit()
except MlsynthConfigError as e:
print(e)
prints the binding constraint and by how much, not just “infeasible”:
LEXSCM design is infeasible -- the binding constraint(s):
- budget: the 5 cheapest eligible markets cost $10,100,765, over the
$10,000,000 budget by $100,765. Raise the budget to >= $10,100,765,
reduce m, or relax the size band.
Every infeasibility – candidate pool, budget, coverage, quota, or spillover –
is audited up front and reported in this same have vs need -> minimal fix
shape, and all binding constraints are listed together (so you fix them in
one pass, not one error at a time). The audit only reports: it never
silently relaxes a constraint you set. All of them raise the one
MlsynthConfigError, so a caller catches a single
type and surfaces exactly which design ask was impossible and the smallest change
that would satisfy it.
References#
Abadie, A., & Zhao, J. (2026). “Synthetic Controls for Experimental Design.” arXiv Working Paper 2108.02196. (The February 2026 revision develops the design-based bias bound and the approximate-balance criterion used here.) https://arxiv.org/abs/2108.02196
Vives-i-Bastida, J. (2022). “Synthetic Experimental Design for a UBI pilot study.” Working paper (https://ivalua.cat/sites/default/files/2023-03/Vives-i-Bastida_2022_anon.pdf)
Doudchenko, N., Khosravi, K., Pouget-Abadie, J., Lahaie, S., et al. “Synthetic Design: An Optimization Approach to Experimental Design with Synthetic Controls.” – related optimization view of synthetic experimental design.