Repository navigation
Expand file tree
/
Copy pathfinal_metrics.py
More file actions
53 lines (49 loc) · 2.59 KB
/
Copy pathfinal_metrics.py
File metadata and controls
53 lines (49 loc) · 2.59 KB
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
"""#8 — Metric FINAL: so sánh 4 variant trên test 1273 (mẫu số cố định)."""
import sys, json
sys.path.insert(0, ".")
import metrics as M
N = 1273
VARIANTS = [
("Direct LLM (không RAG)", "index/eval_direct_results.DIRECT.json"),
("RAG-only (single-LLM+RAG)", "index/eval_rag_results.json"),
("V2 (3 agent, ZERO memory)", "index/eval_v2_results.V2.json"),
("V3 (3 agent, +cả 2 memory)", "index/eval_agents_results.ON.json"),
]
data = {name: M.load_results(path) for name, path in VARIANTS}
print("=" * 78)
print("METRIC FINAL — MedQA-USMLE test (mẫu số cố định = 1273) — 4 variant, cùng model gemma-4-12B")
print("=" * 78)
print(f"\n{'Variant':<32}{'Accuracy':>16}{'95% CI':>16}{'Invalid':>9}{'Lat med':>9}")
print("-" * 82)
rank = []
for name, _ in VARIANTS:
r = data[name]
acc, corr, _ = M.accuracy(r, N)
ir, inv, _ = M.invalid_rate(r, N)
lo, hi = M.bootstrap_ci(M.correct_flags(r))
cl = M.cost_latency(r)
lat = cl.get("latency_median_s", 0)
rank.append((name, acc, corr))
print(f"{name:<32}{corr:>5}/{N} = {acc*100:>5.2f}%{f'{lo*100:.1f}-{hi*100:.1f}%':>16}{f'{inv}':>9}{f'{lat:.0f}s':>9}")
print("\n" + "-" * 82)
print("XẾP HẠNG:", " > ".join(f"{n.split('(')[0].strip()} {a*100:.1f}%" for n, a, _ in sorted(rank, key=lambda x: -x[1])))
# so sánh từng cặp có ý nghĩa (theo trình tự nâng cấp pipeline)
PAIRS = [
("+RAG", "Direct LLM (không RAG)", "RAG-only (single-LLM+RAG)", "RAG có giúp so với Direct?"),
("+multi-agent", "RAG-only (single-LLM+RAG)", "V2 (3 agent, ZERO memory)", "Multi-agent có giúp so với RAG-only?"),
("+memory", "V2 (3 agent, ZERO memory)", "V3 (3 agent, +cả 2 memory)", "Memory (cả 2 loại) có giúp so với zero-memory?"),
]
print("\n" + "=" * 78)
print("SO SÁNH GHÉP CẶP (paired McNemar exact + bootstrap CI của hiệu)")
print("=" * 78)
for label, base_name, new_name, q in PAIRS:
al = M.align(data[base_name], data[new_name]) # v0=base, v3=new
wlt = M.win_loss_tie(al); mc = M.mcnemar(al)
v3f = [1 if a["v3_ok"] else 0 for a in al]; v0f = [1 if a["v0_ok"] else 0 for a in al]
lo, hi = M.bootstrap_ci_diff(v3f, v0f)
gain = (sum(v3f) - sum(v0f)) / len(al) * 100
sig = "CÓ ý nghĩa" if mc["p_value"] < 0.05 else "chưa ý nghĩa"
print(f"\n▸ {label}: {q}")
print(f" {base_name.split('(')[0].strip()} → {new_name.split('(')[0].strip()}")
print(f" GAIN = {gain:+.2f}đ | Win/Loss/Tie = {wlt['win']}/{wlt['loss']}/{wlt['tie']}")
print(f" McNemar p = {mc['p_value']:.4f} ({sig}) | bootstrap 95% CI hiệu = [{lo*100:+.2f}, {hi*100:+.2f}]đ")