// Critical-value tables (z, Student t, chi-square; upper tail) calculated by this site's own distribution code // (assets/js/lib/p-value-calculator.js → stats.js, the code behind the calculators), not copied from a book. // node scripts/critical-value-table.mjs → static/critical-values/critical-values.csv, static/critical-values/make-critical-values.mjs.txt (a copy of this file), // research/critical-value-tables.md.part (markdown tables pasted into the calculator page, see HANDOFF) // Checked against scipy by tests/critical-value-table.test.mjs using static/critical-values/test-vectors.json (scipy 1.13.1 isf values, full precision). import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import { distribution } from '../assets/js/lib/p-value-calculator.js'; export const ALPHAS = [0.10, 0.05, 0.025, 0.01, 0.005, 0.001]; export const T_DF = [...Array.from({ length: 30 }, (_, i) => i + 1), 40, 50, 60, 80, 100, 120]; export const CHI_DF = [...Array.from({ length: 30 }, (_, i) => i + 1), 40, 50, 60, 80, 100]; /** Upper-tail critical value c with P(X ≥ c) = alpha. */ export const crit = (dist, df, alpha) => distribution(dist, df).invSf(alpha); export function tables() { return { z: ALPHAS.map((a) => crit('z', null, a)), t: Object.fromEntries(T_DF.map((d) => [d, ALPHAS.map((a) => crit('t', d, a))])), chisq: Object.fromEntries(CHI_DF.map((d) => [d, ALPHAS.map((a) => crit('chisq', d, a))])), }; } const f = (x, n) => x.toFixed(n); if (process.argv[1] === fileURLToPath(import.meta.url)) { const T = tables(); const root = fileURLToPath(new URL('..', import.meta.url)); const csv = ['distribution,df,' + ALPHAS.map((a) => `alpha_${a}`).join(',')]; csv.push('z,,' + T.z.map((x) => f(x, 6)).join(',')); for (const d of T_DF) csv.push(`t,${d},` + T.t[d].map((x) => f(x, 6)).join(',')); for (const d of CHI_DF) csv.push(`chisq,${d},` + T.chisq[d].map((x) => f(x, 6)).join(',')); fs.writeFileSync(root + 'static/critical-values/critical-values.csv', csv.join('\n') + '\n'); fs.copyFileSync(fileURLToPath(import.meta.url), root + 'static/critical-values/make-critical-values.mjs.txt'); const head = '| df | ' + ALPHAS.join(' | ') + ' |\n|---|' + ALPHAS.map(() => '---:').join('|') + '|'; const pick = (dfs, tab, n) => dfs.map((d) => `| ${d} | ` + tab[d].map((x) => f(x, n)).join(' | ') + ' |').join('\n'); const md = `Upper-tail t critical values (P(T ≥ c) = α):\n\n${head}\n${pick([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 60, 120], T.t, 3)}\n| ∞ (z) | ${T.z.map((x) => f(x, 3)).join(' | ')} |\n\n` + `Upper-tail chi-square critical values (P(χ² ≥ c) = α):\n\n${head}\n${pick([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 60, 100], T.chisq, 3)}\n`; fs.writeFileSync(root + 'research/critical-value-tables.md.part', md); console.log('written', csv.length - 1, 'rows'); }