File size: 6,267 Bytes
306ab7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Recompute every statistic referenced in the paper from the released CSV
snapshot. Single source of truth for paper updates.

Outputs:
  - Spearman rho_s + two-sided p for Stars / VS Code installs / SO questions
  - Pearson r on log-targets (Appendix robustness check 2)
  - Leave-one-out range on the GitHub-stars correlation (robustness check 1)
  - Single- vs combined-factor sub-composite (robustness check 3)
  - Dirichlet bootstrap (1000 draws from Dir(3.5, 2.5, 2.0, 2.0))
  - Inter-factor Spearman matrix on n=50
"""

import csv, json, math, random, sys
from pathlib import Path
from reproduce_table3 import (
    spearman, two_sided_p,
    load_latest_scores, load_latest_signals, load_registry,
    benchmark_sentiment_subcomposite,
)


def pearson(xs, ys):
    n = len(xs)
    mx, my = sum(xs)/n, sum(ys)/n
    num = sum((xs[i]-mx)*(ys[i]-my) for i in range(n))
    dx = math.sqrt(sum((xs[i]-mx)**2 for i in range(n)))
    dy = math.sqrt(sum((ys[i]-my)**2 for i in range(n)))
    return num/(dx*dy) if dx and dy else float("nan")


def main():
    csv_dir = Path(__file__).parent.parent / "data" / "csv"
    scores  = load_latest_scores(csv_dir)
    signals = load_latest_signals(csv_dir)
    registry = load_registry(csv_dir)

    eligible = [n for n, c in registry.items() if c.get("github_repo") and n in scores and n in signals]
    print(f"\n=== Sample size ===")
    print(f"50 agents in registry, n = {len(eligible)} have github_repo + scores + signals")

    bs, stars_log, installs_log, so_q = [], [], [], []
    for name in eligible:
        s = signals[name]
        bs.append(benchmark_sentiment_subcomposite(scores[name], s))
        stars_log.append(math.log10((s.get("github_stars") or 0) + 1))
        installs_log.append(math.log10((s.get("vscode_installs") or 0) + 1))
        so_q.append(s.get("so_questions") or 0)

    # --- Table 3 (Spearman) ---
    print(f"\n=== Table 3: Spearman correlations (n={len(bs)}) ===")
    for label, ys in [("GitHub stars (log)", stars_log),
                      ("VS Code installs (log)", installs_log),
                      ("Stack Overflow question volume", so_q)]:
        rho = spearman(bs, ys)
        p = two_sided_p(rho, len(bs))
        sig = "p<0.001" if p<0.001 else f"p={p:.3f}"
        print(f"  {label:<32}  rho_s={rho:+.3f}  {sig}")

    # --- Pearson on log targets (Appendix Robustness check 2) ---
    so_log = [math.log10(q + 1) for q in so_q]
    print(f"\n=== Robustness check 2: Pearson on log targets ===")
    for label, ys in [("GitHub stars (log)", stars_log),
                      ("VS Code installs (log)", installs_log),
                      ("Stack Overflow questions (log)", so_log)]:
        r = pearson(bs, ys)
        print(f"  {label:<32}  r={r:+.3f}")

    # --- Robustness check 3: alternative sub-composites ---
    print(f"\n=== Robustness check 3: alternative sub-composites vs stars ===")
    bench_only, sent_only = [], []
    for name in eligible:
        sub = json.loads(scores[name].get("sub_scores") or "[0.5, 0, 0]")
        bench_only.append(sub[0] if sub else 0.5)
        sent_raw = float(scores[name].get("sentiment") or 0.0)
        sent_only.append(max(0.0, min(1.0, (sent_raw + 1.0) / 2.0)))
    print(f"  Benchmark only:  rho_s={spearman(bench_only, stars_log):+.3f}")
    print(f"  Sentiment only:  rho_s={spearman(sent_only,  stars_log):+.3f}")
    print(f"  B+S combined:    rho_s={spearman(bs,         stars_log):+.3f}")

    # --- Robustness check 1: leave-one-out ---
    print(f"\n=== Robustness check 1: leave-one-out (GitHub stars) ===")
    rhos = []
    for i in range(len(bs)):
        bs_loo = bs[:i] + bs[i+1:]
        sl_loo = stars_log[:i] + stars_log[i+1:]
        rhos.append(spearman(bs_loo, sl_loo))
    print(f"  n iterations: {len(rhos)}")
    print(f"  range: [{min(rhos):.3f}, {max(rhos):.3f}]")
    print(f"  median: {sorted(rhos)[len(rhos)//2]:.3f}")
    full = spearman(bs, stars_log)
    print(f"  full sample: {full:.3f}")
    print(f"  max |delta from full|: {max(abs(r-full) for r in rhos):.3f}")

    # --- Dirichlet bootstrap ---
    print(f"\n=== Dirichlet bootstrap (1000 draws from Dir(3.5,2.5,2.0,2.0)) ===")
    # We perturb the four-factor weights, then re-derive a B+S sub-composite using
    # the perturbed B and S weights renormalized to sum to 1 over {B, S}.
    random.seed(12345)

    def dirichlet(alphas):
        ys = [random.gammavariate(a, 1.0) for a in alphas]
        s = sum(ys)
        return [y/s for y in ys]

    rho_samples = []
    for _ in range(1000):
        w_B, w_A, w_S, w_E = dirichlet([3.5, 2.5, 2.0, 2.0])
        # Resampled B+S sub-composite, renormalized over {B, S}
        denom = w_B + w_S
        if denom == 0:
            continue
        wb, ws = w_B / denom, w_S / denom
        bs_pert = [wb * bench_only[i] + ws * sent_only[i] for i in range(len(eligible))]
        rho_samples.append(spearman(bs_pert, stars_log))
    rho_samples.sort()
    lo, hi = rho_samples[25], rho_samples[975]   # 95% interval
    median = rho_samples[len(rho_samples)//2]
    print(f"  95% interval: [{lo:.3f}, {hi:.3f}]")
    print(f"  median:       {median:.3f}")
    print(f"  min, max:     [{min(rho_samples):.3f}, {max(rho_samples):.3f}]")
    print(f"  fraction >0:  {sum(1 for r in rho_samples if r>0)/len(rho_samples):.3f}")

    # --- Inter-factor Spearman on n=50 (Table 2) ---
    print(f"\n=== Table 2: inter-factor Spearman correlations (n=50) ===")
    # Build per-agent factor vectors
    all_agents = [n for n in registry if n in scores]
    Bs, Ss, As, Es = [], [], [], []
    for name in all_agents:
        sub = json.loads(scores[name].get("sub_scores") or "[0.5, 0, 0]")
        Bs.append(sub[0] if len(sub) > 0 else 0.5)
        sent_raw = float(scores[name].get("sentiment") or 0.0)
        Ss.append(max(0.0, min(1.0, (sent_raw + 1.0) / 2.0)))
        As.append(float(scores[name].get("adoption") or 0))
        Es.append(float(scores[name].get("reliability") or 0))
    pairs = [("B-A", Bs, As), ("B-S", Bs, Ss), ("B-E", Bs, Es),
             ("A-S", As, Ss), ("A-E", As, Es), ("S-E", Ss, Es)]
    for label, x, y in pairs:
        print(f"  {label}:  rho={spearman(x, y):+.3f}")
    print(f"  n = {len(all_agents)}")


if __name__ == "__main__":
    main()