groupval 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ name: publish
2
+ on:
3
+ release:
4
+ types: [published]
5
+ jobs:
6
+ pypi:
7
+ runs-on: ubuntu-latest
8
+ environment: pypi
9
+ permissions:
10
+ id-token: write
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ - uses: actions/setup-python@v5
14
+ with:
15
+ python-version: "3.12"
16
+ - run: pip install build && python -m build
17
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,5 @@
1
+ __pycache__/
2
+ *.egg-info/
3
+ .venv/
4
+ dist/
5
+ build/
groupval-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jacob Crainic and Brandon Yee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,46 @@
1
+ Metadata-Version: 2.5
2
+ Name: groupval
3
+ Version: 0.1.0
4
+ Summary: Validate language model labels within each group before comparing groups
5
+ Project-URL: Repository, https://github.com/Nyxius-Labs/groupval
6
+ Author: Jacob Crainic, Brandon Yee
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Requires-Python: >=3.9
10
+ Requires-Dist: numpy
11
+ Requires-Dist: pandas
12
+ Requires-Dist: scipy
13
+ Description-Content-Type: text/markdown
14
+
15
+ # groupval
16
+
17
+ Validate language model labels within each group before comparing groups.
18
+
19
+ Language model labels can be accurate overall and still err more on one group's texts, which biases any comparison between groups. On the congressional tweets of Gilardi, Alizadeh and Kubli (2023), coders with accuracy near 0.87 overstate the Democrat minus Republican gap in political content by more than four times, because they miss more Republican political tweets. `groupval` reports sensitivity, specificity and error for each group, the bias in the between-group gap with an interval clustered by author, and the number of expert labels each group needs to estimate that bias to a chosen precision.
20
+
21
+ ## Install
22
+
23
+ ```
24
+ pip install git+https://github.com/Nyxius-Labs/groupval
25
+ ```
26
+
27
+ ## Use
28
+
29
+ ```python
30
+ from groupval import validate, plan_gold
31
+
32
+ r = validate(labels, gold, group, cluster=author, reference="D")
33
+ r.per_group
34
+ r.bias, r.interval
35
+ plan_gold(r.variance_by_group["D"], r.variance_by_group["R"], halfwidth=0.03)
36
+ ```
37
+
38
+ `labels` and `gold` are 0/1 arrays on the expert-labeled items, `group` holds the two groups being compared and `cluster` the author or account. `python examples/simulate.py` runs a small simulated case.
39
+
40
+ ## Reference
41
+
42
+ Crainic, J., and Yee, B. Accurate Overall, Biased Between Parties: Validating Language Model Labels Within Each Group. Extended abstract, 2026.
43
+
44
+ ## License
45
+
46
+ MIT
@@ -0,0 +1,32 @@
1
+ # groupval
2
+
3
+ Validate language model labels within each group before comparing groups.
4
+
5
+ Language model labels can be accurate overall and still err more on one group's texts, which biases any comparison between groups. On the congressional tweets of Gilardi, Alizadeh and Kubli (2023), coders with accuracy near 0.87 overstate the Democrat minus Republican gap in political content by more than four times, because they miss more Republican political tweets. `groupval` reports sensitivity, specificity and error for each group, the bias in the between-group gap with an interval clustered by author, and the number of expert labels each group needs to estimate that bias to a chosen precision.
6
+
7
+ ## Install
8
+
9
+ ```
10
+ pip install git+https://github.com/Nyxius-Labs/groupval
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```python
16
+ from groupval import validate, plan_gold
17
+
18
+ r = validate(labels, gold, group, cluster=author, reference="D")
19
+ r.per_group
20
+ r.bias, r.interval
21
+ plan_gold(r.variance_by_group["D"], r.variance_by_group["R"], halfwidth=0.03)
22
+ ```
23
+
24
+ `labels` and `gold` are 0/1 arrays on the expert-labeled items, `group` holds the two groups being compared and `cluster` the author or account. `python examples/simulate.py` runs a small simulated case.
25
+
26
+ ## Reference
27
+
28
+ Crainic, J., and Yee, B. Accurate Overall, Biased Between Parties: Validating Language Model Labels Within Each Group. Extended abstract, 2026.
29
+
30
+ ## License
31
+
32
+ MIT
@@ -0,0 +1,17 @@
1
+ import numpy as np
2
+
3
+ from groupval import plan_gold, validate
4
+
5
+ rng = np.random.default_rng(1)
6
+ n = 2000
7
+ group = np.where(rng.integers(0, 2, n) == 0, "D", "R")
8
+ gold = (rng.random(n) < 0.95).astype(float)
9
+ miss = np.where(group == "R", 0.18, 0.10)
10
+ labels = np.where((gold == 1) & (rng.random(n) < miss), 0.0, gold)
11
+
12
+ report = validate(labels, gold, group, reference="D")
13
+ print(report.per_group.round(3))
14
+ print(f"pooled accuracy {report.pooled_accuracy:.3f}")
15
+ print(f"bias in the D minus R gap {100 * report.bias:.1f} points, 90% interval {100 * report.interval[0]:.1f} to {100 * report.interval[1]:.1f}")
16
+ v = report.variance_by_group
17
+ print("gold labels per group for +/- 3 points:", plan_gold(v["D"], v["R"], 0.03))
@@ -0,0 +1,4 @@
1
+ from groupval.core import GroupReport, plan_gold, validate
2
+
3
+ __all__ = ["GroupReport", "plan_gold", "validate"]
4
+ __version__ = "0.1.0"
@@ -0,0 +1,51 @@
1
+ from dataclasses import dataclass
2
+
3
+ import numpy as np
4
+ import pandas as pd
5
+ from scipy.stats import norm
6
+
7
+
8
+ @dataclass
9
+ class GroupReport:
10
+ per_group: pd.DataFrame
11
+ gap_labels: float
12
+ gap_gold: float
13
+ bias: float
14
+ interval: tuple
15
+ pooled_accuracy: float
16
+ variance_by_group: dict
17
+
18
+
19
+ def validate(labels, gold, group, cluster=None, reference=None, draws=2000, level=0.90, seed=0):
20
+ d = pd.DataFrame({"c": np.asarray(labels, float), "y": np.asarray(gold, float), "g": np.asarray(group)})
21
+ d["k"] = np.arange(len(d)) if cluster is None else np.asarray(cluster)
22
+ d = d.dropna(subset=["c", "y"])
23
+ groups = sorted(d.g.unique())
24
+ if len(groups) != 2:
25
+ raise ValueError("validate compares exactly two groups")
26
+ a = groups[0] if reference is None else reference
27
+ b = [g for g in groups if g != a][0]
28
+ rows = []
29
+ for g in (a, b):
30
+ s = d[d.g == g]
31
+ rows.append(dict(group=g, n=len(s), clusters=s.k.nunique(), gold_share=s.y.mean(), label_share=s.c.mean(),
32
+ sensitivity=s[s.y == 1].c.mean(),
33
+ specificity=1 - s[s.y == 0].c.mean() if (s.y == 0).any() else np.nan,
34
+ error=(s.c != s.y).mean()))
35
+ per = pd.DataFrame(rows).set_index("group")
36
+ gap_labels = per.label_share[a] - per.label_share[b]
37
+ gap_gold = per.gold_share[a] - per.gold_share[b]
38
+ rng = np.random.default_rng(seed)
39
+ blocks = {g: [x for _, x in d[d.g == g].groupby("k")] for g in (a, b)}
40
+ stats = []
41
+ for _ in range(draws):
42
+ m = {g: pd.concat([blocks[g][i] for i in rng.integers(0, len(blocks[g]), len(blocks[g]))]) for g in (a, b)}
43
+ stats.append((m[a].c.mean() - m[b].c.mean()) - (m[a].y.mean() - m[b].y.mean()))
44
+ lo, hi = np.percentile(stats, [50 * (1 - level), 100 - 50 * (1 - level)])
45
+ var = {g: float(np.var(d[d.g == g].c - d[d.g == g].y)) for g in (a, b)}
46
+ return GroupReport(per, gap_labels, gap_gold, gap_labels - gap_gold, (lo, hi), float((d.c == d.y).mean()), var)
47
+
48
+
49
+ def plan_gold(variance_a, variance_b, halfwidth, level=0.90, design_effect=1.0):
50
+ z = norm.ppf(0.5 + level / 2)
51
+ return int(np.ceil(design_effect * z ** 2 * (variance_a + variance_b) / halfwidth ** 2))
@@ -0,0 +1,16 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "groupval"
7
+ version = "0.1.0"
8
+ description = "Validate language model labels within each group before comparing groups"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ authors = [{ name = "Jacob Crainic" }, { name = "Brandon Yee" }]
12
+ requires-python = ">=3.9"
13
+ dependencies = ["numpy", "pandas", "scipy"]
14
+
15
+ [project.urls]
16
+ Repository = "https://github.com/Nyxius-Labs/groupval"
@@ -0,0 +1,27 @@
1
+ import numpy as np
2
+
3
+ from groupval import plan_gold, validate
4
+
5
+
6
+ def test_no_bias_when_labels_equal_gold():
7
+ rng = np.random.default_rng(0)
8
+ gold = (rng.random(500) < 0.5).astype(float)
9
+ group = np.repeat(["a", "b"], 250)
10
+ r = validate(gold, gold, group, draws=200)
11
+ assert r.bias == 0
12
+ assert r.pooled_accuracy == 1
13
+
14
+
15
+ def test_detects_group_differential_misses():
16
+ rng = np.random.default_rng(1)
17
+ n = 4000
18
+ group = np.where(rng.integers(0, 2, n) == 0, "D", "R")
19
+ gold = np.ones(n)
20
+ labels = np.where((group == "R") & (rng.random(n) < 0.2), 0.0, 1.0)
21
+ r = validate(labels, gold, group, reference="D", draws=300)
22
+ assert 0.15 < r.bias < 0.25
23
+ assert r.interval[0] > 0
24
+
25
+
26
+ def test_plan_gold_scales_with_precision():
27
+ assert plan_gold(0.1, 0.1, 0.02) > plan_gold(0.1, 0.1, 0.04)