Metadata-Version: 2.5
Name: fbcert
Version: 1.0.0
Summary: Tell from a batch of scores whether the label-shift likelihood puts its optimum above a chosen floor.
Project-URL: Repository, https://github.com/MaxwellNi/fbcert
Project-URL: Paper, https://doi.org/10.1145/3862322.3863596
Project-URL: Issues, https://github.com/MaxwellNi/fbcert/issues
Author-email: Pin Ni <ni.pin@uni.lu>, Angela Altmeier <angela.altmeier@uni.lu>, Christian Fisch <christian.fisch@uni.lu>
License-Expression: MIT
License-File: LICENSE
Keywords: event rates,expectation maximization,label shift,quantification
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
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: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: numpy<3,>=1.24
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: ruff==0.15.1; extra == 'dev'
Requires-Dist: twine>=6; extra == 'dev'
Provides-Extra: figures
Requires-Dist: matplotlib==3.10.8; extra == 'figures'
Provides-Extra: quantifiers
Requires-Dist: clarabel==0.11.1; extra == 'quantifiers'
Requires-Dist: cvxpy==1.7.5; extra == 'quantifiers'
Provides-Extra: recurrent
Requires-Dist: torch==2.10.0; extra == 'recurrent'
Provides-Extra: reproduction
Requires-Dist: joblib==1.5.3; extra == 'reproduction'
Requires-Dist: lightgbm==4.6.0; extra == 'reproduction'
Requires-Dist: numpy==1.26.4; extra == 'reproduction'
Requires-Dist: pandas==2.3.3; extra == 'reproduction'
Requires-Dist: pyarrow==23.0.1; extra == 'reproduction'
Requires-Dist: scikit-learn==1.5.1; extra == 'reproduction'
Requires-Dist: scipy==1.15.3; extra == 'reproduction'
Description-Content-Type: text/markdown

# fbcert

Tell from a batch of scores whether the label-shift likelihood puts its optimum above a chosen floor.

Pin Ni, Angela Altmeier and Christian Fisch. *Certify Before Recalibrating: Exact Support Tests for Label-Shift EM in Rare Web Streams*. WSDM '27, to appear. [DOI 10.1145/3862322.3863596](https://doi.org/10.1145/3862322.3863596) · [Mathematical supplement](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/paper/mathematical_supplement.pdf) · [Empirical supplement](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/paper/empirical_supplement.pdf)

A classifier can rank cases well and still predict the wrong number of events. When prevalence changes, label-shift EM estimates a new rate from the scores. For rare events, that estimate can fall to zero. Given the source prevalence and a minimum rate you care about, one pass over the scores determines which side of that floor the likelihood favors.

## Installation

Install the library and its command line with pip.

```sh
pip install fbcert
```

For the study programs and example files, install from the repository checkout.

```sh
git clone https://github.com/MaxwellNi/fbcert
cd fbcert
pip install .
```

The library depends on NumPy. To draw the paper's figures, add Matplotlib and install the [figure fonts](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/docs/reproducing.md#figures).

```sh
pip install '.[figures]'
```

Install `reproduction` for the fitting programs. The recurrent comparator also needs `recurrent`. The comparison with the `abstention` package needs that package and the `quantifiers` extra. The [version table](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/docs/reproducing.md#software-versions) lists the versions used in the study and in the rebuild.

## Quick start

The source event rate was 1% and the workload floor is 0.5%. The CSV needs a header named `score` and one probability per row in that column.

```sh
csv=$(mktemp)
printf '%s\n' score 0.001 0.002 0.004 0.007 0.013 0.029 > "$csv"
fbcert "$csv" --source-prior 0.01 --floor 0.005
rm "$csv"
```

The command prints this JSON report.

```json
{
  "n": 6,
  "source_prior": 0.01,
  "mu": 0.9422780735594726,
  "decision": {
    "floor": 0.005,
    "support": -0.062555139576431,
    "side": "below",
    "numerical_tolerance": 1e-12
  },
  "optimum": {
    "kind": "zero",
    "lower": 0.0,
    "upper": 0.0,
    "unique": true
  },
  "em": {
    "prevalence": 1.6131924435684172e-07,
    "iterations": 183,
    "converged": true,
    "movement": 9.882098080347236e-09,
    "tolerance": 1e-08,
    "max_iter": 1000
  }
}
```

The negative slope at the floor puts the optimum below 0.5%. The EM-map slope at zero, `mu`, equals the mean relative odds. In exact arithmetic a nonflat likelihood has its optimum at zero whenever this mean is at most one. If the reported mean rounds to one, read `optimum.kind` for the boundary decision. When rounding prevents a definite sign at the floor, the command reports `decision.side` as `unresolved`. The function `fbcert.exact_support` evaluates the same slope in exact rational arithmetic.

```python
from fbcert import BinaryBatch

scores = [0.001, 0.002, 0.004, 0.007, 0.013, 0.029]
batch = BinaryBatch(scores, source_prior=0.01)
print(batch.mu, batch.decide(floor=0.005).side, batch.optimum().kind)
# 0.9422780735594726 below zero
```

For several classes, an excluded class can still improve a fit. Here the second class has zero target weight. The returned step gives it positive weight and supplies a lower bound on the gain in log likelihood.

```python
from fbcert import MulticlassBatch

scores = [[0.20, 0.50, 0.30], [0.10, 0.60, 0.30]]
batch = MulticlassBatch(scores, source_prior=[0.4, 0.3, 0.3])
q = [0.5, 0.0, 0.5]
print(batch.diagnostics(q).global_gap_upper_bound)
print(batch.escape(q, excluded=1).improvement_lower_bound)
# 3.4222222222222243
# 0.23113207547169537
```

For examples that read the scores from the CSV files in the repository, see the [reproduction guide](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/docs/reproducing.md#score-files).

## Reproducing the paper

| Task | Command from the repository root | Inputs and runtime |
|---|---|---|
| Check all 1,335 registered claims | `python -m scripts.check_claims` | Bundled records, seconds |
| Run the tests | `python -m unittest discover -s tests` | NumPy for the core tests. Fitting tests need `reproduction`. Add `recurrent` for the recurrent tests, or `quantifiers` and the `abstention` package for the comparison tests. |
| Draw the three figures | `python -m scripts.draw_figures` | Bundled records, Matplotlib and Linux Libertine, seconds |
| Rebuild from the raw archives | Follow the commands in the reproduction guide below. Use `python -m scripts.study_pipeline --list-stages` to preview the stages. | GLEIF, SEC Form D and Kickstarter archives. About 8 hours elapsed and 25 CPU-hours in the reference run. |

Starting from the raw archives on Intel Xeon Gold 6132 processors, the study programs regenerated 54 of 56 records in full. A further record combines three measured checks with three observations from data collection. The last record supplies the study settings.

Of the 1,331 values registered for that run, 1,315 matched at printed precision. Fourteen differences came from the logistic comparator, and two were numerical residuals within their stated bounds. On the pooled registry development data, the four predictors kept the same ordering by PR-AUC, log loss and Brier score. The [reproduction guide](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/docs/reproducing.md) gives the commands, timings and precision criteria.

## Repository layout

| Path | Contents |
|---|---|
| `fbcert/`, `examples/` | Library, command line and sample score batches |
| `records/`, `claims/` | Numerical results, their field definitions and the expressions for printed values |
| `scripts/` | Obtain and verify source archives, evaluate predictions, rebuild the study, check the published values, draw the figures and replay the result tables |
| `data/`, `experiments/` | Input manifests and fitting programs |
| `paper/` | Mathematical and empirical supplements |
| `tests/`, `docs/` | Test suite, reproduction guide and data guide |

## Data

The study uses GLEIF registry publications, SEC Form D filings and a Web Robots Kickstarter snapshot. The manifests identify the source files. Obtain the raw files from the sources listed in the [data guide](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/docs/data.md). The repository contains aggregate results and model-fitting code.

## Scope

The test locates the likelihood optimum for the batch. One registry estimate passed at a floor of half the mean of the last three observed rates and still overestimated the event rate by 688%. In the paper's replay, acting on the test did not improve prediction. Files from the first four days of the registry holdout had been parsed for counts, including event counts, before the design was final.

## Citation

```bibtex
@inproceedings{ni2027certify,
  author    = {Ni, Pin and Altmeier, Angela and Fisch, Christian},
  title     = {Certify Before Recalibrating: Exact Support Tests for Label-Shift {EM} in Rare Web Streams},
  booktitle = {Proceedings of the Twentieth ACM International Conference on Web Search and Data Mining},
  year      = {2027},
  doi       = {10.1145/3862322.3863596},
  publisher = {Association for Computing Machinery}
}
```

## License

This repository is released under the [MIT License](https://github.com/MaxwellNi/fbcert/blob/v1.0.0/LICENSE). Source data remain subject to their providers' terms.
