Download scripts/lib-metrics.mjs from PeterKol/llm-questionnaire-protocol-sensitivity: direct link, hf CLI and curl.
- Browser
- Download file 5.37 kB
-
https://huggingface.co/datasets/PeterKol/llm-questionnaire-protocol-sensitivity/resolve/main/scripts/lib-metrics.mjs
- Command line
-
hf download hf://datasets/PeterKol/llm-questionnaire-protocol-sensitivity/scripts/lib-metrics.mjs
-
curl -L -o lib-metrics.mjs https://huggingface.co/datasets/PeterKol/llm-questionnaire-protocol-sensitivity/resolve/main/scripts/lib-metrics.mjs
5.37 kB
| /** | |
| * Metrics computed IDENTICALLY for a human answer vector and a model answer vector, so a model's | |
| * number is always read against the same statistic taken over humans. Scale scores come from the | |
| * repo's own scoreSubmission (lib-common.mjs); everything else here is descriptive and defined below. | |
| */ | |
| import { TESTS, loadItems, loadHumans, scoreSubmission, mean, sd, pearson, midrankPct } from './lib-common.mjs' | |
| export const NEUTRAL = 2 // "Neither" / "Unsure" / "Neutral": the middle option of all three 5-point scales | |
| export function scoreAnswers(testId, answers) { | |
| const t = TESTS[testId] | |
| if (answers.length !== t.nItems || answers.some((a) => !Number.isInteger(a) || a < 0 || a > 4)) throw new Error('bad answer vector') | |
| const r = scoreSubmission(t.slug, answers) | |
| return { scores: r.scores, top: r.topResult } | |
| } | |
| /** Descriptive style metrics. items = loadItems(testId). answers = 0..4 indices. */ | |
| export function styleMetrics(items, answers) { | |
| const n = answers.length | |
| const byScale = {} | |
| items.forEach((it, i) => { | |
| const k = it.reverse ? 4 - answers[i] : answers[i] | |
| ;(byScale[it.scale] ||= { keyed: [], pos: [], rev: [] }).keyed.push(k) | |
| ;(it.reverse ? byScale[it.scale].rev : byScale[it.scale].pos).push(answers[i]) | |
| }) | |
| const scales = Object.values(byScale) | |
| const keyedSD = mean(scales.map((s) => sd(s.keyed))) | |
| const balanced = scales.filter((s) => s.pos.length && s.rev.length) | |
| const acq = balanced.length ? mean(balanced.map((s) => (mean(s.pos) + mean(s.rev)) / 2)) / 4 : null // 0.5 = neutral; only defined where both key directions exist | |
| return { | |
| keyedSD, | |
| acq, | |
| extreme: answers.filter((a) => a === 0 || a === 4).length / n, | |
| mid: answers.filter((a) => a === NEUTRAL).length / n, | |
| upperSide: answers.filter((a) => a > NEUTRAL).length / n, // agree / like side | |
| lowerSide: answers.filter((a) => a < NEUTRAL).length / n, | |
| meanRaw: mean(answers), | |
| } | |
| } | |
| /** Human reference for one test: comparator subsets + per-item counts + leave-one-out agreement stats. */ | |
| export function buildHumanRef(testId, { minSecPerItem = null } = {}) { | |
| const t = TESTS[testId] | |
| const items = loadItems(testId) | |
| const all = loadHumans(testId) | |
| const rows = all.map((r) => { | |
| // answers outside the 5-option index range 0..4 exist in the published CSVs; they cannot be scored | |
| const invalidRange = r.q.some((a) => !Number.isInteger(a) || a < 0 || a > 4) | |
| const s = invalidRange ? { scores: {} } : scoreAnswers(testId, r.q) | |
| const reproduces = !invalidRange && t.scales.every((k) => s.scores[k] === r.stored[k]) | |
| const sameAll = r.q.every((a) => a === r.q[0]) | |
| const fast = r.duration != null && r.duration < t.nItems * 1.0 | |
| return { ...r, scores: s.scores, reproduces, invalidRange, straight: sameAll, fast } | |
| }) | |
| const subsets = { | |
| en_repro: rows.filter((r) => r.locale === 'en' && r.reproduces), | |
| all_repro: rows.filter((r) => r.reproduces), | |
| en_repro_clean: rows.filter((r) => r.locale === 'en' && r.reproduces && !r.straight && !r.fast), | |
| en_all: rows.filter((r) => r.locale === 'en'), | |
| } | |
| return { testId, t, items, all: rows, subsets } | |
| } | |
| /** Per-item counts, modal answers, item means and leave-one-out per-respondent statistics for a subset. */ | |
| export function itemStats(items, subset) { | |
| const n = subset.length | |
| const nOpt = 5 | |
| const counts = items.map(() => Array(nOpt).fill(0)) | |
| subset.forEach((r) => r.q.forEach((a, i) => counts[i][a]++)) | |
| const modal = counts.map((c) => c.indexOf(Math.max(...c))) | |
| const modalShare = counts.map((c, i) => c[modal[i]] / n) | |
| const itemMean = counts.map((c) => c.reduce((s, x, k) => s + x * k, 0) / n) | |
| // leave-one-out: agreement of each respondent with the modal answer of everyone else | |
| const looAgree = [], looWithin1 = [], looR = [] | |
| for (const r of subset) { | |
| let ag = 0, w1 = 0 | |
| const others = [] | |
| for (let i = 0; i < items.length; i++) { | |
| const c = counts[i].slice(); c[r.q[i]]-- | |
| const m = c.indexOf(Math.max(...c)) | |
| if (m === r.q[i]) ag++ | |
| if (Math.abs(m - r.q[i]) <= 1) w1++ | |
| others.push((itemMean[i] * n - r.q[i]) / (n - 1)) | |
| } | |
| looAgree.push(ag / items.length) | |
| looWithin1.push(w1 / items.length) | |
| looR.push(pearson(r.q, others)) | |
| } | |
| const sortNum = (a) => [...a].sort((x, y) => x - y) | |
| return { n, counts, modal, modalShare, itemMean, looAgree: sortNum(looAgree), looWithin1: sortNum(looWithin1), looR: sortNum(looR.filter((x) => !Number.isNaN(x))) } | |
| } | |
| /** Sorted per-respondent metric arrays for percentile lookups. */ | |
| export function styleDist(items, subset) { | |
| const ms = subset.map((r) => styleMetrics(items, r.q)) | |
| const col = (k) => ms.map((m) => m[k]).filter((x) => x != null).sort((a, b) => a - b) | |
| return { keyedSD: col('keyedSD'), acq: col('acq'), extreme: col('extreme'), mid: col('mid'), upperSide: col('upperSide'), meanRaw: col('meanRaw') } | |
| } | |
| export function scaleDist(t, subset) { | |
| const out = {} | |
| for (const s of t.scales) { | |
| const v = subset.map((r) => r.scores[s]).sort((a, b) => a - b) | |
| out[s] = { sorted: v, mean: mean(v), sd: sd(v), n: v.length } | |
| } | |
| return out | |
| } | |
| export function pctAndZ(dist, scale, score) { | |
| const d = dist[scale] | |
| return { pct: midrankPct(d.sorted, score), z: (score - d.mean) / d.sd } | |
| } | |