Metadata-Version: 2.4
Name: defacto-eval
Version: 0.2.0
Summary: Checks whether an LLM evaluation result is true. Detects benchmark leakage, measures LLM-judge and reward-model reliability, flags reward hacking and score drift, and writes governance-mapped audit reports.
Author-email: Sigma Jahan <sigma.jahan1@gmail.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/SigmaJahan/defacto-eval
Project-URL: Documentation, https://github.com/SigmaJahan/defacto-eval#readme
Project-URL: Issues, https://github.com/SigmaJahan/defacto-eval/issues
Keywords: llm,evaluation,reliability,reward-model,llm-as-judge,data-leakage,contamination,ai-safety,ai-governance,model-validation,drift,monitoring
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
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: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Provides-Extra: viz
Requires-Dist: matplotlib>=3.7; extra == "viz"
Provides-Extra: pandas
Requires-Dist: pandas>=2.0; extra == "pandas"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: pytest-cov>=4; extra == "dev"
Requires-Dist: ruff>=0.4; extra == "dev"
Requires-Dist: mypy>=1.8; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Provides-Extra: all
Requires-Dist: matplotlib>=3.7; extra == "all"
Requires-Dist: pandas>=2.0; extra == "all"
Dynamic: license-file

# DeFacto

[![CI](https://github.com/SigmaJahan/defacto-eval/actions/workflows/ci.yml/badge.svg)](https://github.com/SigmaJahan/defacto-eval/actions/workflows/ci.yml)
[![PyPI version](https://img.shields.io/pypi/v/defacto-eval.svg)](https://pypi.org/project/defacto-eval/)
[![Python versions](https://img.shields.io/pypi/pyversions/defacto-eval.svg)](https://pypi.org/project/defacto-eval/)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)

DeFacto checks whether an LLM evaluation result is true. It finds benchmark
leakage, measures how reliable an LLM judge or reward model is, flags reward
hacking, tracks distribution drift, and writes an audit report mapped to AI
governance frameworks.

![Judge dashboard](docs/_static/dashboard.png)

A benchmark can look good because its test set leaked into training. A judge
can agree with itself and not with people. A reward model can be gamed into
praising answers that fail the task. These are silent failures. The results
are fluent and well scored, and they are still wrong. DeFacto makes them
measurable.

## Install

```bash
pip install defacto-eval             # core, NumPy only
pip install "defacto-eval[viz]"      # add plots
pip install "defacto-eval[pandas]"   # add DataFrame export
pip install "defacto-eval[all,dev]"  # everything for development
```

Python 3.10 or newer. The package installs as `defacto-eval` and imports as
`defacto`.

## Quickstart

### Command line

```bash
defacto judge records.json -o report.md --plot dashboard.png
defacto contamination --train train.txt --test test.txt --n 13
defacto probe --texts items.txt --groups groups.txt
defacto hack rewards.json -o report.md
defacto drift --expected reference.json --actual live.json
defacto report findings.json --format json
```

Every command accepts `--format markdown`, `--format json`, or `--format csv`.

### Python

```python
from defacto import analyze_judge, detect_hacking, ngram_containment, psi

result = analyze_judge(gold, judge, confidence=confidence)
print(result["agreement"]["kappa"])
print(result["classification"]["macro_f1"])

contamination = ngram_containment(train_texts, test_texts)
hacking = detect_hacking(rewards, successes)
drift = psi(reference_scores, live_scores)
```

See `examples/quickstart.py` and `docs/quickstart.md`.

## What DeFacto checks

| Question | Module | Output |
|---|---|---|
| Did the test data leak into training? | `defacto.leakage` | n-gram containment, MinHash near-duplicates, a held-out ridge identity probe with a permutation test and cluster-bootstrap CI |
| Is the judge or reward model reliable? | `defacto.judge` | Cohen's and Fleiss' kappa, weighted kappa, calibration error, risk-coverage, confident errors, abstention, subgroup gaps, precision, recall, F1 |
| Is the reward being gamed? | `defacto.hack` | reward vs verifiable success, reward gap, drift over time |
| Did the score distribution shift? | `defacto.monitoring` | Population Stability Index, two-sample Kolmogorov-Smirnov |
| What does this mean for compliance? | `defacto.report` | Markdown, JSON, or CSV audit mapped to NIST AI RMF, OSFI E-23, and the EU AI Act |

## Does it work

A controlled experiment in `docs/case-study.md` injects a known amount of
contamination into a test set and shows the metrics track it. The held-out
identity probe reports 0.933 balanced accuracy against 0.500 chance on held-out
documents.

`docs/gsm8k-overlap.md` runs the same checks on the real GSM8K train and test
questions, with no GPU and no model. No test question reaches 0.8 13-gram
containment against the train split. No near-duplicate pair crosses the split.
The identity probe scores 0.522 against 0.500 chance (p = 0.14).

Leakage detection covers verbatim and near-verbatim overlap. A paraphrased test
item needs an embedding model to catch, and DeFacto does not include one.

## Plots

Every check has a plot. The `viz` extra adds these:

![Reward vs success](docs/_static/reward_scatter.png)

- `reliability_diagram`: binned confidence against observed accuracy
- `plot_confusion_matrix`: gold labels against predictions
- `risk_coverage_curve`: error rate against coverage
- `reward_success_scatter` and `reward_drift_plot`
- `containment_histogram`, `identity_probe_bar`, `bootstrap_histogram`
- `judge_dashboard`: one figure with three panels

Generate the examples with `python examples/plot_gallery.py`.

## Why the numbers are reproducible

DeFacto has one hard dependency, NumPy. Every statistic is implemented from
its definition on NumPy plus the Python standard library, so the results are
auditable and easy to install.

- Cluster bootstrap with bias-corrected and accelerated, percentile, or basic
  intervals.
- Exact permutation p-values. The formula `(1 + count) / (n + 1)` never reports
  a zero.
- A seed on every stochastic routine.

The methods extend published work on evaluation validity: within-program
evaluation leaking program identity, silent versus detectable failures, and
confident errors that pass the checks, packaged here as tooling.

## Project layout

```
src/defacto/
├── _stats.py          # bootstrap, permutation, correlation, ranks
├── judge/             # agreement, calibration, metrics, risk-coverage
├── leakage/           # contamination, near-duplicates, identity probe
├── hack/              # reward-hacking and drift
├── monitoring/        # PSI and Kolmogorov-Smirnov
├── viz/               # matplotlib plots
├── report/            # findings, governance mapping, Markdown/JSON/CSV
└── cli.py             # command-line interface
```

## Citing

A `CITATION.cff` file is included. Cite DeFacto with it, or link to the
repository.

## Further reading

- `docs/case-study.md`: the contamination-recovery experiment
- `docs/gsm8k-overlap.md`: train/test overlap on GSM8K
- `docs/comparison.md`: how DeFacto compares to Evidently, Deepchecks, Cleanlab, and lm-eval-harness
- `docs/evaluation-validity.md`: the research idea behind the checks

## Roadmap

- A verifier fine-tuning recipe (SFT then DPO on a small open model) that
  DeFacto audits in one loop.
- More governance frameworks: ISO/IEC 42001, Singapore FEAT, Canada's AIDA.

## Contributing

Contributions are welcome, new checks and governance mappings especially. See
`CONTRIBUTING.md`.

## License

Apache-2.0. See `LICENSE`.
