Metadata-Version: 2.5
Name: fewlabels
Version: 0.1.0
Summary: Design and robustness for prediction-powered inference: label budgets, non-random labelling, correlated AI annotators and clustered data.
Project-URL: Homepage, https://github.com/yunusimanov/fewlabels
Project-URL: Documentation, https://yunusimanov.github.io/fewlabels
Project-URL: Repository, https://github.com/yunusimanov/fewlabels
Project-URL: Issues, https://github.com/yunusimanov/fewlabels/issues
Project-URL: Changelog, https://github.com/yunusimanov/fewlabels/blob/main/CHANGELOG.md
Author-email: Yunus Imanov <yunus.imanov.fz@student.aztu.edu.az>
Maintainer-email: Yunus Imanov <yunus.imanov.fz@student.aztu.edu.az>
License-Expression: MPL-2.0
License-File: LICENSE
Keywords: ai-predictions,cluster-sampling,inverse-probability-weighting,label-budget,llm-annotation,power-analysis,ppi,prediction-powered-inference,sample-size,sensitivity-analysis
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
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 :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: numpy>=1.24
Provides-Extra: dev
Requires-Dist: mkdocs-material<10,>=9.5; extra == 'dev'
Requires-Dist: mkdocs<2,>=1.6; extra == 'dev'
Requires-Dist: mkdocstrings[python]>=0.26; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: nox>=2024.4; extra == 'dev'
Requires-Dist: ppi-python>=0.2; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs-material<10,>=9.5; extra == 'docs'
Requires-Dist: mkdocs<2,>=1.6; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.26; extra == 'docs'
Provides-Extra: lint
Requires-Dist: mypy>=1.10; extra == 'lint'
Requires-Dist: ruff>=0.6; extra == 'lint'
Provides-Extra: ppi
Requires-Dist: ppi-python>=0.2; extra == 'ppi'
Provides-Extra: test
Requires-Dist: pytest-cov>=5.0; extra == 'test'
Requires-Dist: pytest>=8.0; extra == 'test'
Description-Content-Type: text/markdown

# fewlabels

[![CI](https://github.com/yunusimanov/fewlabels/actions/workflows/ci.yml/badge.svg)](https://github.com/yunusimanov/fewlabels/actions/workflows/ci.yml)
[![Docs](https://github.com/yunusimanov/fewlabels/actions/workflows/docs.yml/badge.svg)](https://yunusimanov.github.io/fewlabels)
[![PyPI](https://img.shields.io/pypi/v/fewlabels?label=PyPI&color=blue)](https://pypi.org/project/fewlabels/)
[![Python](https://img.shields.io/pypi/pyversions/fewlabels?label=Python)](https://pypi.org/project/fewlabels/)
[![License: MPL 2.0](https://img.shields.io/badge/License-MPL_2.0-blue.svg)](LICENSE)

**Design and robustness for inference with AI predictions.**

Prediction-powered inference (PPI) combines a small set of gold-standard
human labels with many AI predictions and still gives valid confidence
intervals. The core estimator already exists in
[ppi_py](https://github.com/aangelopoulos/ppi_py). fewlabels answers the
questions that decide whether a PPI study works in practice:

- **How many labels do I need?** Sample size, power and cost-optimal
  allocation between human labelling and model predictions, planned from a
  small pilot with conservative assumptions.
- **What if the labels were not chosen at random?** Inverse probability
  weighted PPI for known selection probabilities (stratified or active
  labelling), and a sensitivity analysis with a tipping point for unknown
  selection.
- **What if I have several AI annotators?** Optimal combination that
  accounts for correlated errors, plus a diagnostic report.
- **What if my data are clustered?** Cluster-robust variances for patients
  with several images, documents from the same site, and so on.

In short: not *how to compute* PPI, but *how to plan it and when to trust it*.

## Installation

```bash
pip install fewlabels
```

Python 3.10 or newer is required. The only runtime dependency is NumPy.

## Quick start

### Plan the label budget

A classifier with sensitivity 0.90 and specificity 0.95 will be used to
estimate a prevalence of about 20 % in 50 000 records. How many records must
be labelled by hand for a margin of error of 2 percentage points?

```python
import fewlabels as fl

params = fl.DesignParameters.from_proportion(prevalence=0.2, sensitivity=0.9, specificity=0.95)
fl.required_labels(0.02, params, n_unlabeled=50_000)  # 512 labels with PPI
fl.required_labels(0.02, fl.DesignParameters(params.sd_y, r2=0.0))  # 1537 labels without
```

With a pilot sample, plan with pessimistic parameters:

```python
params = fl.DesignParameters.from_pilot(y_pilot, preds_pilot, conservative=True, seed=0)
```

### Estimate

```python
result = fl.ppi_mean(y, preds_labeled, preds_unlabeled, alpha=0.05)
print(result.estimate, result.ci, result.effective_labels)
```

`preds_labeled` and `preds_unlabeled` may have several columns, one per AI
annotator. Pass `clusters_labeled` and `clusters_unlabeled` for clustered
data.

### Labels chosen with known probabilities

```python
pi = fl.inclusion_probabilities(uncertainty, budget=500, floor=0.01)
labeled = fl.poisson_sample(pi, seed=0)
# ... send the selected cases for labelling ...
result = fl.ipw_ppi_mean(y_all, preds_all, labeled, pi)
```

### Check robustness

```python
fl.selection_sensitivity(y, preds_labeled, preds_unlabeled, gamma=[1, 1.5, 2])
fl.tipping_point(y, preds_labeled, preds_unlabeled, null=0.25)
print(
    fl.annotator_report(y, preds_labeled_matrix, names=["model_a", "model_b", "model_c"]).summary()
)
```

### Already using ppi_py?

```python
from fewlabels.compat import ppi_mean_ci  # same signature and numbers as ppi_py
```

## What is inside

| Module | Purpose | Main objects |
| --- | --- | --- |
| `fewlabels.design` | Sample size, power and budget allocation | `DesignParameters`, `required_labels`, `required_labels_for_power`, `cost_optimal_allocation`, `design_effect` |
| `fewlabels.estimation` | PPI++ for one or several annotators, clusters, known selection probabilities | `ppi_mean`, `ipw_ppi_mean`, `classical_mean`, `inclusion_probabilities` |
| `fewlabels.robustness` | Non-random labelling and correlated annotators | `selection_sensitivity`, `tipping_point`, `annotator_report` |
| `fewlabels.compat` | The mean estimation interface of ppi_py | `ppi_mean_ci`, `ppi_mean_pointestimate`, `ppi_mean_pval`, `classical_mean_ci` |
| `fewlabels.datasets` | Synthetic data for examples and simulations | `make_ppi_data`, `make_annotator_data`, `make_clustered_data` |

## Command line

```bash
fewlabels plan --prevalence 0.2 --sensitivity 0.9 --specificity 0.95 \
    --half-width 0.02 --unlabeled 50000

fewlabels plan --sd-y 0.4 --r2 0.7 --budget 10000 --cost-label 20 --cost-prediction 0.05
```

## Documentation

The user guide, the theory behind each method and the API reference are at
<https://yunusimanov.github.io/fewlabels>.

## Related work

fewlabels is the second library in a series on statistically sound use of
AI systems. Its sibling [anywatch](https://github.com/yunusimanov/anywatch)
provides anytime-valid post-market monitoring of deployed models.

## Citing

If you use fewlabels in academic work, please cite it using the metadata in
[CITATION.cff](CITATION.cff) or the "Cite this repository" button on GitHub,
together with the original PPI and PPI++ papers listed in the documentation.

## Contributing

Contributions are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) and
the [Code of Conduct](CODE_OF_CONDUCT.md).

## License

fewlabels is distributed under the [Mozilla Public License 2.0](LICENSE).

## Author

**Yunus Imanov**, Researcher, Department of Engineering Mathematics and
Artificial Intelligence, Azerbaijan Technical University, Baku, Azerbaijan.
Contact: yunus.imanov.fz@student.aztu.edu.az
