File size: 3,255 Bytes
48f76bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Audit omissions including unlabeled units, and propose a supported allowlist.

Usage: guide_audit.py TRAIN_JSONL PREDICTIONS_JSONL OUT_JSON
Uses the installed B2 thresholds (.38 probability, .5 confidence). A unit must
have both label classes in training and validation, at least five examples of
each on validation, and at least five omissions at 97% precision with 98% needed-guide retention
to qualify.
Labels are observational proxies; this does not establish instruction usefulness.
"""
import collections
import json
import sys
from pathlib import Path


def audit(train, predictions, threshold=.38, confidence=.5):
    training = collections.defaultdict(collections.Counter)
    for row in train:
        for name, value in row['labels']['guides'].items():
            if value is not None:
                training[name]['positive' if value else 'negative'] += 1
    stats = collections.defaultdict(collections.Counter)
    for row in predictions:
        for name, value in row['answers'].get('guides', {}).get('probabilities', {}).items():
            needed = row['labels']['guides'].get(name)
            omitted = value < threshold and max(value, 1 - value) >= confidence
            stats[name]['scored'] += 1
            stats[name]['omitted'] += omitted
            if needed is None:
                stats[name]['unknown'] += 1
                stats[name]['omitted_unknown'] += omitted
            else:
                stats[name]['positive' if needed else 'negative'] += 1
                stats[name]['omitted_needed' if needed else 'omitted_unneeded'] += omitted
    allowed = []
    for name, s in stats.items():
        known_omissions = s['omitted_needed'] + s['omitted_unneeded']
        if (training[name]['positive'] > 0 and training[name]['negative'] > 0
                and s['positive'] >= 5 and s['negative'] >= 5 and not s['omitted_unknown']
                and known_omissions >= 5 and s['omitted_unneeded'] / known_omissions >= .97
                and 1 - s['omitted_needed'] / s['positive'] >= .98):
            allowed.append(name)
    return {'omittable': sorted(allowed), 'omit_below': threshold, 'confidence_floor': confidence,
            'training': {k: dict(v) for k, v in sorted(training.items())},
            'validation': {k: dict(v) for k, v in sorted(stats.items())},
            'limitation': 'Observed tool use supplies guide labels; unlabeled guidance cannot be certified for omission.'}


def main():
    train, predictions, output = sys.argv[1:]
    def read(path):
        return [json.loads(s) for s in Path(path).read_text().splitlines() if s.strip()]

    training, scored = read(train), read(predictions)
    candidates = [audit(training, scored, step / 100) for step in range(1, 39)]
    result = max(candidates, key=lambda r: sum(r['validation'][n]['omitted'] for n in r['omittable']))
    if not result['omittable']:
        result['omit_below'] = 0
    result['original_threshold_audit'] = audit(training, scored)
    result['selection_rule'] = 'Maximize supported omissions on validation; per-unit precision >= .97 and needed retention >= .98.'
    Path(output).write_text(json.dumps(result, indent=2) + '\n')
    print(json.dumps(result, indent=2))


if __name__ == '__main__':
    main()