Metadata-Version: 2.4
Name: pysynthcon
Version: 0.1.0b5
Summary: PySynthCon provides a common Python interface for synthetic control, SCUL, individual synthetic control, and synthetic difference-in-differences
Author: Charles Rahal, Anda Raluca Epure, Daniel Valdenegro
License-Expression: GPL-3.0-only
Project-URL: Documentation, https://pysynthcon.readthedocs.io/en/latest/
Project-URL: Changelog, https://github.com/crahal/pysynthcon/blob/main/CHANGELOG.md
Project-URL: Repository, https://github.com/crahal/pysynthcon
Project-URL: Issues, https://github.com/crahal/pysynthcon/issues
Keywords: synthetic control,causal inference,panel data,econometrics
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
License-File: src/pysynth/fonts/SourceSans3-LICENSE.md
License-File: src/pysynth/fonts/STIXTwoText-OFL.txt
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Requires-Dist: pandas>=2.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: joblib>=1.3
Requires-Dist: requests>=2.31
Provides-Extra: backends
Requires-Dist: pysyncon==1.7.0; extra == "backends"
Requires-Dist: SyntheticControlMethods==1.1.17; extra == "backends"
Requires-Dist: cvxpy>=1.6.7; extra == "backends"
Requires-Dist: clarabel>=0.11.1; extra == "backends"
Provides-Extra: r-synth
Requires-Dist: rpy2<3.7,>=3.6; extra == "r-synth"
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == "plot"
Requires-Dist: fonttools>=4.22.0; extra == "plot"
Provides-Extra: notebooks
Requires-Dist: matplotlib>=3.7; extra == "notebooks"
Requires-Dist: fonttools>=4.22.0; extra == "notebooks"
Requires-Dist: jupyterlab>=4; extra == "notebooks"
Requires-Dist: nbclient>=0.10; extra == "notebooks"
Requires-Dist: nbformat>=5.9; extra == "notebooks"
Requires-Dist: ipykernel>=6; extra == "notebooks"
Provides-Extra: docs
Requires-Dist: sphinx>=7; extra == "docs"
Requires-Dist: myst-parser>=3; extra == "docs"
Requires-Dist: nbconvert<8,>=7.16; extra == "docs"
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: pytest-cov>=5; extra == "dev"
Requires-Dist: ruff>=0.9; extra == "dev"
Requires-Dist: build>=1.2; extra == "dev"
Dynamic: license-file

<h1 align="center">
  <a href="https://github.com/crahal/pysynthcon/blob/9d3648f9f1c1b3872d7275974f22425ba9cd780a/simulated_examples/donor_spillovers.ipynb">
    <picture>
      <source media="(max-width: 720px)" srcset="https://raw.githubusercontent.com/crahal/pysynthcon/9d3648f9f1c1b3872d7275974f22425ba9cd780a/docs/assets/pysynthcon-header-compact.svg">
      <img src="https://raw.githubusercontent.com/crahal/pysynthcon/9d3648f9f1c1b3872d7275974f22425ba9cd780a/docs/assets/pysynthcon-header.svg" width="100%" alt="PySynthCon. All flavours of synthetic control in python. Ten estimators measure a simulated regional jobs programme with a known six-point employment gain, first with unaffected neighbours and then with four neighbours gaining three points.">
    </picture>
  </a>
</h1>

[![PyPI beta](https://img.shields.io/pypi/v/pysynthcon?include_prereleases&label=PyPI)](https://pypi.org/project/pysynthcon/)
[![Documentation](https://readthedocs.org/projects/pysynthcon/badge/?version=latest)](https://pysynthcon.readthedocs.io/en/latest/)
[![Tests and documentation](https://github.com/crahal/pysynthcon/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/crahal/pysynthcon/actions/workflows/ci.yml)
[![Python 3.10 and newer](https://img.shields.io/badge/Python-3.10%2B-3776AB)](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/pyproject.toml)
[![License GPL-3.0-only](https://img.shields.io/badge/License-GPL--3.0--only-blue.svg)](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/LICENSE)
[![Purpose Research](https://img.shields.io/badge/Purpose-Research-yellow.svg)](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/replication.md)

**[Quick start](#quick-start) · [Six worked examples](#six-worked-examples) · [Estimators](#estimators) · [Documentation](#documentation)**

PySynthCon lets you fit several synthetic controls to the same panel and see how the estimated effect changes with the method or donor pool. Compare held-out pretreatment predictions, refit after excluding donors, and put published benchmarks beside the estimates that reproduce them.

The header follows a simulated jobs programme. A district gains six employment-index points. How much of that gain can ten estimators recover if four neighbouring districts also benefit? [Run the simulation](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/simulated_examples/donor_spillovers.ipynb) · [Compact jobs figure](https://raw.githubusercontent.com/crahal/pysynthcon/9d3648f9f1c1b3872d7275974f22425ba9cd780a/docs/assets/pysynthcon-header-compact.svg)

## Quick start

Install the 0.1 beta with Python 3.10 or newer. The distribution is `pysynthcon` and the Python import is `pysynth`.

```bash
python -m pip install "pysynthcon[plot]==0.1.0b5"
```

The header uses a simulated jobs programme with a known effect. For a real-data example, compare West Germany’s GDP per capita after reunification in 1990 with two estimates of its path without reunification. Outcome-matching synthetic control (SCM) and synthetic difference-in-differences (SDID) use the same GDP panel.

```python
from pysynth import SyntheticControl, SyntheticDID, compare, plot_effects, save_figure
from pysynth.datasets import DATASETS, load_dataset

panel = DATASETS["germany"].panel(load_dataset("germany"))
fits = {"SCM": SyntheticControl().fit(panel), "SDID": SyntheticDID().fit(panel)}

print(compare(fits)[["att", "pre_rmse"]])
fig, axes = plot_effects(fits, ylabel="GDP gap per capita (current US$, PPP-adjusted)", xlabel="Year")
save_figure(fig, "outputs/custom/germany-effects")
```

The mean annual GDP gaps over 1990–2003 are **−$1,297 per person with SCM** and **−$1,471 with SDID**, in current US dollars adjusted for purchasing power parity. Each gap is observed GDP per capita minus the fitted counterfactual. The notebook also reconstructs the original study’s predictor-based specification. [West Germany notebook](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/germany.ipynb) · [Interpreting SDID iteration-limit warnings](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/inference.md#handling-iteration-limit-warnings)

For your own data, `Panel.from_frame` takes unit, time and outcome columns, an explicit treatment date and an optional donor list. The treatment date is the first affected observation. Panels are balanced and use a common onset within each panel. ISC and 2SISC also accept case-specific panels and aggregate in event time. [Prepare a panel](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/data.md)

## Six worked examples

| Open the notebook | What to look for |
| --- | --- |
| [Simulation · a regional jobs programme](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/simulated_examples/donor_spillovers.ipynb) | The programme raises the treated district’s employment index by six points. Across 200 simulated economies, outcome SCM estimates 5.98 points with unaffected donors and 3.20 when neighbouring districts also benefit. |
| [West Germany · reunification](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/germany.ipynb) | Rebuild the historical predictor specification and compare the GDP gap with the paper’s roughly US$1,600 annual loss per person (1990–2003, PPP-adjusted). |
| [California · tobacco control](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/california.ipynb) | Recover the published SDID effect of −15.6 packs per person and SE of 8.4. With the original tobacco predictors, both R Synth and the Python optimizer rank California first of 39. Allowing one state to be twice as likely to receive treatment puts the reassignment p-value between 0.013 and 0.050. |
| [Basque Country · conflict](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/basque.ipynb) | Recover the published SCM donor weights for Catalonia (0.8508) and Madrid (0.1492), and compare the GDP path with the original R fit. |
| [Kansas · tax cuts](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/kansas.ipynb) | Match the authors’ ridge-augmented SCM vignette at −0.0401 log points and check all 16 quarterly effects. |
| [Rhode Island · decriminalization of indoor sex work](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/rhode_island.ipynb) | Test a six-year effect path against every cyclic shift in time. Reproduce the joint p-value of 0.04 and the six annual pointwise confidence sets for female gonorrhea incidence. |

Four studies compare eleven estimators on a common panel. Rhode Island focuses on inference for outcome-matching SCM. All five empirical notebooks download hash-verified data into memory. Rhode Island and the main simulation run entirely in Python. The simulation generates its own data. Its optional ten-estimator header comparison also uses the local reference backends for SCUL and ISC. Figures are embedded in the notebooks. Executing them also writes PDF, SVG, PNG and numerical tables under `outputs/empirical_examples/<study>/` or `outputs/simulated_examples/donor_spillovers/`. [Reference targets and tolerances](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/replication.md) · [Figure exports](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/plotting.md)

```bash
git clone --branch v0.1.0b5 --depth 1 https://github.com/crahal/pysynthcon.git
cd pysynthcon
python -m pip install -e ".[notebooks]"
python scripts/execute_notebooks.py --examples rhode_island donor_spillovers
```

For all six notebooks, use Python 3.11 or 3.12, install the [pinned replication requirements](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/requirements-replication.txt) and provision the separately supplied local R reference bundle. SCUL and vanilla ISC use the original R backends. The California reassignment comparison also requires local R Synth 1.1-10 and the optional rpy2 bridge. Reproducing the California paper’s exact placebo sampling plan also uses the bundle. Once provisioned, `python scripts/execute_notebooks.py` runs all six. [Local R setup](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/reference_backends.md)

## Estimators

| Method | Python interface | What determines the counterfactual |
| --- | --- | --- |
| Outcome-matching SCM | `SyntheticControl()` | Convex weights match the full pretreatment outcome path, with equal weight on each period. This is an outcome-only specification of [classical SCM](https://doi.org/10.1198/jasa.2009.ap08746). |
| Predictor-based SCM | `PredictorSyntheticControl()` | Convex weights balance predictors. Their relative importance can be fitted against pretreatment outcomes. [Abadie & Gardeazabal (2003)](https://doi.org/10.1257/000282803321455188) · [Abadie, Diamond & Hainmueller (2010)](https://doi.org/10.1198/jasa.2009.ap08746) |
| Ridge-augmented SCM | `AugmentedSyntheticControl()` | A ridge outcome model corrects residual imbalance. Donor weights can be negative. [Ben-Michael, Feller & Rothstein (2021)](https://doi.org/10.1080/01621459.2021.1929245) |
| Penalized SCM | `PenalizedSyntheticControl(lambda_=0.01)` | A pairwise penalty discourages weight on donors far from the treated unit in predictor space. [Abadie & L’Hour (2021)](https://doi.org/10.1080/01621459.2021.1971535) |
| Differenced SCM | `DifferencedSyntheticControl()` | Fit changes, then restore the average pretreatment level gap. The default uses zero pairwise penalty. [Engelbrektson (2021), working paper](https://arxiv.org/abs/2111.10784) |
| Robust SCM | `RobustSyntheticControl(lambda_=0.1, sv_count=3)` | Singular-value truncation denoises donor outcomes. Ridge weights fitted before treatment combine the denoised donor series. [Amjad, Shah & Shen (2018)](https://jmlr.org/papers/v19/17-777.html) |
| SCUL | `SCUL()` | Lasso with rolling-origin tuning, signed weights and an intercept. [Hollingsworth & Wing (2020)](https://doi.org/10.31235/osf.io/fc9xt) |
| Individual synthetic control | `IndividualSC()` | Fit each treated individual against the full eligible donor pool. [Vagni & Breen (2021)](https://academic.oup.com/esr/article/37/5/834/6325498) |
| Two-stage individual synthetic control | `TwoStageIndividualSC(n_neighbors=10)` | Screen the nearest donors on pretreatment features, then fit SCM in that group. [Petrillo et al. (2025)](https://eprints.whiterose.ac.uk/id/eprint/224118/1/Binder2025004.pdf) |
| Synthetic difference-in-differences | `SyntheticDID()` | Weight both donors and pretreatment periods. [Arkhangelsky et al. (2021)](https://doi.org/10.1257/aer.20190159) |
| Difference-in-differences | `DifferenceInDifferences()` | The comparison baseline uses uniform weights across donors and pretreatment periods. |

The penalty and rank values shown above are example choices. Use `python -m pip install "pysynthcon[backends]==0.1.0b5"` for predictor-based, augmented, penalized and differenced SCM and the default 2SISC fit. [Method definitions and tuning](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/methods.md) · [Implementation credits and backend details](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/backends.md)

## Compare specifications

`select_estimator` scores each candidate on the same withheld pretreatment periods and refits the selected candidate on the full panel. `donor_sensitivity` refits after each donor exclusion. Pass named groups to examine a possible spillover region, as in the simulation.

```python
from pysynth import donor_sensitivity, select_estimator

candidates = {"SCM": SyntheticControl, "SDID": SyntheticDID}
selection = select_estimator(panel, candidates, initial_window=20, horizon=4)
print(selection.validation.summary)
print(donor_sensitivity(panel, candidates).summary)
```

`plot_comparison` aligns ATT estimates with pretreatment RMSE, and `plot_effects` preserves a common outcome scale across methods. Both return editable Matplotlib figures. [Evaluation guide](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/evaluation.md) · [Plotting guide](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/plotting.md)

## Is the effect unusual across units or across time?

Use state reassignments when the comparison is across units. When the question concerns the timing of a particular effect path, impose that path on the treated outcome and compare its residuals across time. The methods make different assumptions. [Inference guide](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/inference.md)

```python
from pysynth import conformal_test
from pysynth.datasets import DATASETS, load_dataset

ri = DATASETS["rhode_island"].panel(load_dataset("rhode_island"))
test = conformal_test(ri, null_effect=0)
print(test.p_value)  # 0.04: one of 25 cyclic windows is at least as extreme
print(test.rotations.nlargest(3, "statistic"))
```

The Rhode Island notebook also tests each annual effect on the authors’ full grid. The spillover simulation applies the same sharp-path test to a known treatment effect and measures what happens when donors themselves respond to treatment. [Rhode Island replication](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/empirical_examples/rhode_island.ipynb) · [Spillover experiment](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/simulated_examples/donor_spillovers.ipynb)

## Documentation

The [full manual on Read the Docs](https://pysynthcon.readthedocs.io/en/latest/) includes the statistical methods, inference conventions, API and worked examples. The [versioned documentation sources](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs) accompany the beta.

- [Getting started](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/getting-started.md) · [Data preparation](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/data.md) · [API reference](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/api.rst)
- [Research workflows](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/differentiation.md) · [Inference](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/inference.md) · [Estimator and replication audit](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/audit.md)
- [Contributing and running tests](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/CONTRIBUTING.md) · [Changelog](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/CHANGELOG.md) · [Third-party notices](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/THIRD_PARTY_NOTICES.md)

**Authors** · Charles Rahal, Anda Raluca Epure, and Daniel Valdenegro.

When publishing, cite PySynthCon together with the method papers and upstream packages used in your analysis. [Software citation](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/CITATION.cff) · [Method references](https://github.com/crahal/pysynthcon/blob/v0.1.0b5/docs/references.md)
