Metadata-Version: 2.4
Name: qe-stats
Version: 0.1.3.9001
Summary: Python bindings for libqe-stats — classification metrics, sample sizes, metric derivation, bootstrap comparison and Shaffer's rho for qualitative coding
Author-Email: Cody L Marquart <cody.marquart@wisc.edu>
License-Expression: GPL-3.0-only
Project-URL: Homepage, https://qe-libs.org/py/project/qe-stats/
Project-URL: Documentation, https://qe-libs.org/py/docs/qe-stats/
Project-URL: Repository, https://gitlab.com/epistemic-analytics/qe-packages/libqe-stats
Requires-Python: >=3.9
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Description-Content-Type: text/markdown

# qe-stats

Python bindings for [libqe-stats](https://qe-libs.org/conan/libqe-stats/), the
header-only C++ statistics for checking qualitative coding that the `qestats`
R package and `@qe-libs/libqe-stats-wasm` share: classification metrics,
hypergeometric intervals and perfect-sample sizes, metric derivation from
known metrics, bootstrap comparison of two coders, and Shaffer's rho (ported
from rhoR).

```sh
pip install qe-stats --index-url https://qe-libs.org/py/simple/
```

## Quick start

```python
import qestats

# Metrics from a confusion matrix
m = qestats.calculate_metrics(tp=50, fp=10, fn=5, tn=100)
m["kappa"], m["precision"], m["FDR"]

# Shaffer's rho for an observed kappa of 0.88 on an 80-item test set
# from a population with base rate 0.2 (rhoR's rhoK)
qestats.rho(0.88, 0.2, 80, seed=1)          # 0.1025

# How large must a perfect sample be to show kappa > 0.65?
qestats.ps_kappa(0.65, baserate=0.12)       # {'n': ..., 'power': ..., 'cost': ...}

# Compare two coders' kappa
qestats.p_diff({"TP": 50, "FP": 10, "FN": 5, "TN": 100},
               {"TP": 40, "FP": 20, "FN": 10, "TN": 100},
               n_sample=0, metric="kappa")
```

Metric results are dicts keyed as in R (`baserate`, `baserate2`, `precision`,
`recall`, `accuracy`, `FDR`, `FOR`, `kappa`, and the four cells). A confusion
matrix argument is a dict with `TP`, `FP`, `FN`, `TN` or a sequence
`(tp, fp, fn, tn)`.

## API

| Function | R (`qestats`) |
|----------|---------------|
| `calculate_metrics(tp, fp, fn, tn)` | `qestats_calculate_metrics` |
| `kappa_from_counts(tp, fp, fn, tn)` | — |
| `derive(name1, val1, name2, val2, name3, val3)` | `qestats_derive` |
| `derive_from_rates(baserate, fdr, for_rate)` | `qestats_derive_from_rates` |
| `range(name1, val1, name2, val2, target)` | `qestats_range` |
| `phyper(q, K, N_minus_K, n)` | `qestats_phyper` |
| `hyper_range(n_pos, n, N, alpha=0.05)` | `qestats_hyper_range` |
| `metrics_range(tp, fp, fn, tn, N, baserate, alpha=0.05)` | `qestats_metrics_range` |
| `ps_proportion(x0, x1=0, N=0, allowed_errors=0, alpha=0.05)` | `qestats_ps_proportion` |
| `ps_est_proportion(n, N=0, allowed_errors=0, alpha=0.05)` | `qestats_ps_est_proportion` |
| `ps_kappa(kappa0, baserate, kappa1=0, N=0, allowed_errors=0, alpha=0.05)` | `qestats_ps_kappa` |
| `ps_est_kappa(n, baserate, N=0, allowed_errors=0, alpha=0.05)` | `qestats_ps_est_kappa` |
| `bootstrap_metrics(cm, n_sample, n_set=0, n_bootstrap=1000, conf_level=0.95)` | `qestats_bootstrap_metrics` |
| `p_diff(cm1, cm2, n_sample, metric, n_set=0, n_bootstrap=1000, alpha=0.05)` | `qestats_p_diff` |
| `n_diff2(cm1, cm2, metric, N, d=0.1, alpha=0.05)` | `qestats_N_Diff2` |
| `rho(x, baserate, test_set_length, ..., method="standard", seed=None)` | `qestats_rho` |

`range` is called as `qestats.range(...)`; it is not exported by
`from qestats import *`, so the builtin is not replaced.

## rho and random numbers

`rho` runs either of rhoR's methods: `"standard"` (rhoR's default, written in
R) or `"fast"` (its C++ path). It draws from libqe-stats' own generator
(mt19937_64), so a `seed` gives the same rho in Python, Julia (QEStats.jl) and
C++ on every platform; without one, each call draws a fresh seed. It cannot
reproduce a seeded rhoR call, which uses R's generator: for that, use the R
package (`qestats_rho` or rhoR itself).
