Metadata-Version: 2.4
Name: stability-matched
Version: 1.0.0
Summary: Feature-selection stability (Nogueira's estimator) with its exact chance level for candidate pools chosen inside each resample
Author: Hassan Nasiraee, Hajar Abbaszadeh, Parvin Farzanegi
License: MIT
Keywords: feature selection,stability,reproducibility,chance correction,bioinformatics
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.22
Requires-Dist: scipy>=1.8
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Provides-Extra: examples
Requires-Dist: scikit-learn>=1.2; extra == "examples"
Dynamic: license-file

# stability-matched

Feature-selection stability with its **exact chance level when the candidate
pool is chosen inside each resample** (for example a variance or univariate
filter fitted on the training rows).

Nogueira's estimator (Nogueira, Sechidis & Brown, JMLR 2018) corrects for
chance as if every feature could be selected in every resample. When each
resample first filters its own pool, features that pass every filter are
favoured even by a selector that picks at random inside its pool, and part of
the reported stability belongs to the filter. This package gives, in closed
form and without simulation, the expected value of the estimator under random
selection of the observed sizes from the observed pools, and the excess over
it, together with a Monte Carlo test, a variant that keeps co-expression
modules, and an exact chance level for the agreement between two selectors.

## Install

```bash
pip install stability-matched
```

Only NumPy and SciPy are required.

## Use

```python
from stability_matched import matrices_from_indices, phi_matched, report

# supports[i]: indices selected in resample i; pools[i]: its candidate pool
Z, P = matrices_from_indices(supports, pools, d)
print(report(Z, P))                 # phi, interval, chance level, excess, p
res = phi_matched(Z, P, B=999)      # the same as a dictionary
```

From the command line, with `Z` and `P` saved as `.npy`, `.csv`, or one
`.npz` holding arrays `Z` and `P`:

```bash
stability-matched selections.npz
```

`examples/example_sklearn.py` runs the whole workflow for a cross-validated
lasso with a variance filter fitted inside each resample.

## Functions

| Function | Returns |
|---|---|
| `phi_stability(Z)` | Nogueira's estimator, asymptotic variance, 95% interval |
| `phi0_matched(Z, P)` | exact expected estimator under random selection within each resample's pool |
| `phi0_stratified(Z, P, groups)` | the same, keeping how many features each resample takes from every group (e.g. co-expression module) |
| `matched_null_test(Z, P, B)` | Monte Carlo p-value against B complete null matrices |
| `agreement_exact(A, B, P)` | mean Jaccard between two selectors in the same resample and its exact hypergeometric chance level |
| `phi_matched(Z, P, B)` | all of the above for one matrix |
| `matrices_from_indices`, `report`, `main` | helpers and command line |

Empty supports are kept (they lower stability); a matrix with no defined
stability returns NaN. Tests: `pytest tests/`.

## Cite

H. Nasiraee, H. Abbaszadeh, P. Farzanegi. How much of feature-selection
stability is chance? An exact correction, a re-evaluation of two published
tools, and a multi-tissue audit. Code: DOI 10.5281/zenodo.22662385. MIT
licence.
