groupval 0.1.0__py3-none-any.whl
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.
- groupval/__init__.py +4 -0
- groupval/core.py +51 -0
- groupval-0.1.0.dist-info/METADATA +46 -0
- groupval-0.1.0.dist-info/RECORD +6 -0
- groupval-0.1.0.dist-info/WHEEL +4 -0
- groupval-0.1.0.dist-info/licenses/LICENSE +21 -0
groupval/__init__.py
ADDED
groupval/core.py
ADDED
|
@@ -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,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,6 @@
|
|
|
1
|
+
groupval/__init__.py,sha256=jM9b03Co08CyUso__3vaQ7712ZHNFslNcUtBRBTdi_k,133
|
|
2
|
+
groupval/core.py,sha256=pAqku7Vam_cSK6gZHprsolfTAGF0w7kAfa_6j9A_UDw,2212
|
|
3
|
+
groupval-0.1.0.dist-info/METADATA,sha256=Z6YkLjpw6T0SQrKOsIcYY0Q7w6eAnYHFYkmImsLaKqM,1799
|
|
4
|
+
groupval-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
groupval-0.1.0.dist-info/licenses/LICENSE,sha256=HnNkj0gJyscmCuRfb3m3XEqpbrq3J6ET0PkDES7eBt8,1086
|
|
6
|
+
groupval-0.1.0.dist-info/RECORD,,
|
|
@@ -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.
|