Metadata-Version: 2.4
Name: pymaxtwin
Version: 0.1.0
Summary: Robust deterministic data splitting for model validation
Author-email: "V. Roshan Joseph" <roshan@gatech.edu>, Youngseo Cho <ycho397@gatech.edu>
License-Expression: GPL-3.0-or-later
Project-URL: Homepage, https://github.com/ChoYoungSeo/pymaxtwin
Project-URL: Repository, https://github.com/ChoYoungSeo/pymaxtwin
Project-URL: Issues, https://github.com/ChoYoungSeo/pymaxtwin/issues
Project-URL: Paper, https://doi.org/10.1080/10618600.2026.2754089
Keywords: data-splitting,cross-validation,model-validation,twinning
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
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
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21
Requires-Dist: scipy>=1.7
Requires-Dist: twinning>=1.0
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Dynamic: license-file

# pymaxtwin

Python implementation of **MaxTwin** ([Joseph and Cho, 2026](https://doi.org/10.1080/10618600.2026.2754089)), a deterministic train/test split that preserves the distribution of the data while keeping testing points away from training points.

<p align="center">
  <img src="https://raw.githubusercontent.com/ChoYoungSeo/pymaxtwin/main/docs/figures/figure1.png" width="100%" alt="Three scatterplots compare 20-point testing sets selected by maximum separation, SPlit, and MaxTwin from the same 100 observations.">
</p>
<p align="center"><em>Twenty testing points (red) chosen from 100 by maximum separation, SPlit, and MaxTwin.</em></p>

## Installation

```bash
pip install pymaxtwin
```

## Usage

```python
import numpy as np
from pymaxtwin import maxtwin

X = np.random.rand(100, 5)
Y = np.sin(X[:, 0]) + X[:, 1] ** 2

test_idx = maxtwin(X, Y, splitratio=0.2)  # sorted, 0-indexed
train_idx = np.setdiff1d(np.arange(len(X)), test_idx)
```

| Argument | Default | Description |
|---|---|---|
| `splitratio` | — | Test fraction; must be `1/r` for an integer `r >= 2` (0.5, 0.25, 0.2, ...) |
| `initialization` | `True` | Start twinning from the point closest to the centroid |
| `standardization` | `True` | Standardize the augmented data before twinning |

`initialmaxtwin()` runs the first step only, without the separation improvement.

## Results

<p align="center">
  <img src="https://raw.githubusercontent.com/ChoYoungSeo/pymaxtwin/main/docs/figures/figure3.png" width="80%" alt="Two scatterplots compare initial and improved MaxTwin selections of 50 testing observations from 100 observations arranged in 50 close pairs.">
</p>
<p align="center"><em>Paired data: the improved algorithm assigns close pairs entirely to training or testing.</em></p>

<p align="center">
  <img src="https://raw.githubusercontent.com/ChoYoungSeo/pymaxtwin/main/docs/figures/figure4.png" width="60%" alt="Boxplots of energy distance and separation distance for Random, Twin, DUPLEX, and MaxTwin on unpaired and paired data.">
</p>
<p align="center"><em>MaxTwin keeps the energy distance low and the separation distance high on both unpaired and paired data.</em></p>

## R package

The same method is available on CRAN as [`maxtwin`](https://CRAN.R-project.org/package=maxtwin).

## Citation

```bibtex
@article{joseph2026maxtwin,
  author  = {Joseph, V. Roshan and Cho, Youngseo},
  title   = {A Robust Deterministic Data Splitting Method for Model Validation},
  journal = {Journal of Computational and Graphical Statistics},
  year    = {2026},
  doi     = {10.1080/10618600.2026.2754089}
}
```

## License

GPL-3.0-or-later
