Files
worldmonitor/scripts/validate-resilience-correlation.mjs
Elie Habib 184e82cb40 feat(resilience): PR 3A — net-imports denominator for sovereignFiscalBuffer (#3380)
PR 3A of cohort-audit plan 2026-04-24-002. Construct correction for
re-export hubs: the SWF rawMonths denominator was gross imports, which
double-counted flow-through trade that never represents domestic
consumption. Net-imports fix:

  rawMonths = aum / (grossImports × (1 − reexportShareOfImports)) × 12

applied to any country in the re-export share manifest. Countries NOT
in the manifest get gross imports unchanged (status-quo fallback).

Plan acceptance gates — verified synthetically in this PR:

  Construct invariant. Two synthetic countries, same SWF, same gross
  imports. A re-exports 60%; B re-exports 0%. Post-fix, A's rawMonths
  is 2.5× B's (1/(1-0.6) = 2.5). Pinned in
  tests/resilience-net-imports-denominator.test.mts.

  SWF-heavy exporter invariant. Country with share ≤ 5%: rawMonths
  lift < 5% vs baseline (negligible). Pinned.

What shipped

1. Re-export share manifest infrastructure.
   - scripts/shared/reexport-share-manifest.yaml (new, empty) — schema
     committed; entries populated in follow-up PRs with UNCTAD
     Handbook citations.
   - scripts/shared/reexport-share-loader.mjs (new) — loader + strict
     validator, mirrors swf-manifest-loader.mjs.
   - scripts/seed-recovery-reexport-share.mjs (new) — publishes
     resilience:recovery:reexport-share:v1 from manifest. Empty
     manifest = valid (no countries, no adjustment).

2. SWF seeder uses net-imports denominator.
   - scripts/seed-sovereign-wealth.mjs exports computeNetImports(gross,
     share) — pure helper, unit-tested.
   - Per-country loop: reads manifest, computes denominatorImports,
     applies to rawMonths math.
   - Payload records annualImports (gross, audit), denominatorImports
     (used in math), reexportShareOfImports (provenance).
   - Summary log reports which countries had a net-imports adjustment
     applied with source year.

3. Bundle wiring.
   - Reexport-Share runs BEFORE Sovereign-Wealth in the recovery
     bundle so the SWF seeder reads fresh re-export data in the same
     cron tick.
   - tests/seed-bundle-resilience-recovery.test.mjs expected-entries
     updated (6 → 7) with ordering preservation.

4. Cache-prefix bump (per cache-prefix-bump-propagation-scope skill).
   - RESILIENCE_SCORE_CACHE_PREFIX: v11 → v12
   - RESILIENCE_RANKING_CACHE_KEY: v11 → v12
   - RESILIENCE_HISTORY_KEY_PREFIX: v6 → v7 (history rotation prevents
     30-day rolling window from mixing pre/post-fix scores and
     manufacturing false "falling" trends on deploy day).
   - Source of truth: server/worldmonitor/resilience/v1/_shared.ts
   - Mirrored in: scripts/seed-resilience-scores.mjs,
     scripts/validate-resilience-correlation.mjs,
     scripts/backtest-resilience-outcomes.mjs,
     scripts/validate-resilience-backtest.mjs,
     scripts/benchmark-resilience-external.mjs, api/health.js
   - Test literals bumped in 4 test files (26 line edits).
   - EXTENDED tests/resilience-cache-keys-health-sync.test.mts with
     a parity pass that reads every known mirror file and asserts
     both (a) canonical prefix present AND (b) no stale v<older>
     literals in non-comment code. Found one legacy log-line that
     still referenced v9 (scripts/seed-resilience-scores.mjs:342)
     and refactored it to use the RESILIENCE_RANKING_CACHE_KEY
     constant so future bumps self-update.

Explicitly NOT in this PR

- liquidReserveAdequacy denominator fix. The plan's PR 3A wording
  mentions both dims, but the RESERVES ratio (WB FI.RES.TOTL.MO) is a
  PRE-COMPUTED WB series; applying a post-hoc net-imports adjustment
  mixes WB's denominator year with our manifest-year, and the math
  change belongs in PR 3B (unified liquidity) where the α calibration
  is explicit. This PR stays scoped to sovereignFiscalBuffer.
- Live re-export share entries. The manifest ships EMPTY in this PR;
  entries with UNCTAD citations are one-per-PR follow-ups so each
  figure is individually auditable.

Verified

- tests/resilience-net-imports-denominator.test.mts — 9 pass (construct
  contract: 2.5× ratio gate, monotonicity, boundary rejections,
  backward-compat on missing manifest entry, cohort-proportionality,
  SWF-heavy-exporter-unchanged)
- tests/reexport-share-loader.test.mts — 7 pass (committed-manifest
  shape + 6 schema-violation rejections)
- tests/resilience-cache-keys-health-sync.test.mts — 5 pass (existing 3
  + 2 new parity checks across all mirror files)
- tests/seed-bundle-resilience-recovery.test.mjs — 17 pass (expected
  entries bumped to 7)
- npm run test:data — 6714 pass / 0 fail
- npm run typecheck / typecheck:api — green
- npm run lint / lint:md — clean

Deployment notes

Score + ranking + history cache prefixes all bump in the same deploy.
Per established v10→v11 precedent (and the cache-prefix-bump-
propagation-scope skill):
- Score / ranking: 6h TTL — the new prefix populates via the Railway
  resilience-scores cron within one tick.
- History: 30d ring — the v7 ring starts empty; the first 30 days
  post-deploy lack baseline points, so trend / change30d will read as
  "no change" until v7 accumulates a window.
- Legacy v11 keys can be deleted from Redis at any time post-deploy
  (no reader references them). Leaving them in place costs storage
  but does no harm.
2026-04-24 18:14:04 +04:00

223 lines
8.4 KiB
JavaScript

#!/usr/bin/env node
import { loadEnvFile, getRedisCredentials } from './_seed-utils.mjs';
// Source of truth: server/worldmonitor/resilience/v1/_shared.ts → RESILIENCE_SCORE_CACHE_PREFIX
const RESILIENCE_SCORE_CACHE_PREFIX = 'resilience:score:v12:';
// Mirror of server/worldmonitor/resilience/v1/_shared.ts#currentCacheFormula.
// Must stay in lockstep with the server-side definition so this script
// skips cross-formula cache entries for the same reasons the server
// does — correlations benchmarked against a mixed-formula cohort of
// d6 + pc entries would be meaningless.
function currentCacheFormulaLocal() {
const combine = (process.env.RESILIENCE_PILLAR_COMBINE_ENABLED ?? 'false').toLowerCase() === 'true';
const v2 = (process.env.RESILIENCE_SCHEMA_V2_ENABLED ?? 'true').toLowerCase() === 'true';
return combine && v2 ? 'pc' : 'd6';
}
const REFERENCE_INDICES = {
ndgain: {
NO: 0.76, IS: 0.72, NZ: 0.71, DK: 0.74, SE: 0.73, FI: 0.72, CH: 0.73, AU: 0.70,
CA: 0.70, US: 0.67, DE: 0.68, GB: 0.67, FR: 0.65, JP: 0.66, KR: 0.63, IT: 0.59,
ES: 0.60, PL: 0.57, BR: 0.45, MX: 0.44, TR: 0.47, TH: 0.44, MY: 0.50, CN: 0.47,
IN: 0.37, ZA: 0.41, EG: 0.38, PK: 0.30, NG: 0.26, KE: 0.33, BD: 0.31, VN: 0.40,
PH: 0.38, ID: 0.42, UA: 0.43, RU: 0.44, AF: 0.20, YE: 0.17, SO: 0.15, HT: 0.22,
SS: 0.14, CF: 0.18, SD: 0.19, ML: 0.25, NE: 0.22, TD: 0.20, SY: 0.21, IQ: 0.30,
MM: 0.28, VE: 0.30, IR: 0.35, ET: 0.26,
},
inform: {
NO: 1.8, IS: 1.5, NZ: 2.1, DK: 1.7, SE: 1.9, FI: 1.6, CH: 1.4, AU: 2.3,
CA: 2.0, US: 3.1, DE: 2.2, GB: 2.4, FR: 2.5, JP: 3.0, KR: 2.3, IT: 2.6,
ES: 2.4, PL: 2.1, BR: 4.1, MX: 4.5, TR: 4.0, TH: 3.5, MY: 3.0, CN: 4.2,
IN: 5.5, ZA: 4.3, EG: 4.8, PK: 6.2, NG: 6.5, KE: 5.0, BD: 5.8, VN: 3.8,
PH: 5.2, ID: 4.8, UA: 5.5, RU: 4.5, AF: 8.0, YE: 8.5, SO: 8.8, HT: 7.2,
SS: 8.3, CF: 8.1, SD: 8.4, ML: 6.8, NE: 7.0, TD: 7.5, SY: 7.8, IQ: 6.8,
MM: 7.0, VE: 5.8, IR: 5.0, ET: 7.2,
},
};
const SAMPLE_COUNTRIES = Object.keys(REFERENCE_INDICES.ndgain);
async function redisPipeline(url, token, commands) {
const resp = await fetch(`${url}/pipeline`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify(commands),
signal: AbortSignal.timeout(30_000),
});
if (!resp.ok) throw new Error(`Redis pipeline HTTP ${resp.status}`);
return resp.json();
}
function toRanks(values) {
const indexed = values.map((v, i) => ({ v, i }));
indexed.sort((a, b) => a.v - b.v);
const ranks = new Array(values.length);
let pos = 0;
while (pos < indexed.length) {
let end = pos + 1;
while (end < indexed.length && indexed[end].v === indexed[pos].v) end++;
const avgRank = (pos + end + 1) / 2;
for (let k = pos; k < end; k++) ranks[indexed[k].i] = avgRank;
pos = end;
}
return ranks;
}
function pearson(x, y) {
const n = x.length;
if (n < 3) return NaN;
let sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0, sumY2 = 0;
for (let i = 0; i < n; i++) {
sumX += x[i];
sumY += y[i];
sumXY += x[i] * y[i];
sumX2 += x[i] * x[i];
sumY2 += y[i] * y[i];
}
const numerator = n * sumXY - sumX * sumY;
const denominator = Math.sqrt((n * sumX2 - sumX * sumX) * (n * sumY2 - sumY * sumY));
if (denominator === 0) return 0;
return numerator / denominator;
}
function spearmanRho(x, y) {
return pearson(toRanks(x), toRanks(y));
}
async function fetchWorldMonitorScores(url, token, countryCodes) {
const commands = countryCodes.map((c) => ['GET', `${RESILIENCE_SCORE_CACHE_PREFIX}${c}`]);
const results = await redisPipeline(url, token, commands);
const current = currentCacheFormulaLocal();
const skipped = { staleFormula: 0, noOverallScore: 0, malformed: 0 };
const scores = new Map();
for (let i = 0; i < countryCodes.length; i++) {
const raw = results[i]?.result;
if (typeof raw !== 'string') continue;
try {
const parsed = JSON.parse(raw);
// Cross-formula gate: the benchmark/validation scripts run off
// live cache entries. A mixed-formula cohort (some countries
// scored under d6, others under pc because their cache entries
// landed on either side of a flag flip) would produce a
// meaningless Spearman. Skip stale-formula entries so the
// correlation runs only against same-formula peers.
if (parsed?._formula !== current) {
skipped.staleFormula++;
continue;
}
if (typeof parsed?.overallScore === 'number' && parsed.overallScore > 0) {
scores.set(countryCodes[i], parsed.overallScore);
} else {
skipped.noOverallScore++;
}
} catch {
skipped.malformed++;
}
}
if (skipped.staleFormula > 0) {
console.warn(`[validate-resilience-correlation] skipped ${skipped.staleFormula} stale-formula entries (current=${current})`);
}
return scores;
}
function computeCorrelation(wmScores, referenceScores, invert = false) {
const paired = [];
for (const [iso2, wmScore] of wmScores.entries()) {
const refScore = referenceScores[iso2];
if (refScore == null) continue;
paired.push({
iso2,
wm: wmScore,
ref: invert ? -refScore : refScore,
});
}
if (paired.length < 10) {
return { rho: NaN, n: paired.length, divergences: [] };
}
const wmValues = paired.map((p) => p.wm);
const refValues = paired.map((p) => p.ref);
const rho = spearmanRho(wmValues, refValues);
const wmRanks = toRanks(wmValues);
const refRanks = toRanks(refValues);
const divergences = paired.map((p, i) => ({
iso2: p.iso2,
wmRank: Math.round(wmRanks[i]),
refRank: Math.round(refRanks[i]),
delta: Math.abs(Math.round(wmRanks[i]) - Math.round(refRanks[i])),
}));
divergences.sort((a, b) => b.delta - a.delta);
return { rho, n: paired.length, divergences };
}
function padRight(str, len) {
return str + ' '.repeat(Math.max(0, len - str.length));
}
async function run() {
loadEnvFile(import.meta.url);
const { url, token } = getRedisCredentials();
console.log(`Fetching WorldMonitor resilience scores for ${SAMPLE_COUNTRIES.length} countries...`);
const wmScores = await fetchWorldMonitorScores(url, token, SAMPLE_COUNTRIES);
console.log(`Retrieved scores for ${wmScores.size}/${SAMPLE_COUNTRIES.length} countries\n`);
if (wmScores.size < 20) {
console.error('Too few scores available. Ensure resilience scores are cached in Redis.');
process.exit(1);
}
const ndgainResult = computeCorrelation(wmScores, REFERENCE_INDICES.ndgain, false);
const informResult = computeCorrelation(wmScores, REFERENCE_INDICES.inform, true);
console.log('=== EXTERNAL INDEX CORRELATION ===\n');
const ndgainPass = ndgainResult.rho > 0.65;
const informPass = informResult.rho > 0.60;
console.log(`WorldMonitor vs ND-GAIN Readiness: rho = ${ndgainResult.rho.toFixed(3)} (n=${ndgainResult.n}, target > 0.65) ${ndgainPass ? 'PASS' : 'FAIL'}`);
console.log(`WorldMonitor vs INFORM Risk: rho = ${informResult.rho.toFixed(3)} (n=${informResult.n}, target > 0.60, inverted) ${informPass ? 'PASS' : 'FAIL'}`);
const passingCount = [ndgainPass, informPass].filter(Boolean).length;
const gatePass = passingCount >= 2;
console.log(`\nGATE CHECK: rho > 0.6 for at least 2 indices? ${gatePass ? 'YES' : 'NO'} (${passingCount}/2 passing)\n`);
for (const [label, result] of [['ND-GAIN', ndgainResult], ['INFORM', informResult]]) {
console.log(`Top divergences vs ${label} (countries that rank very differently):`);
const top5 = result.divergences.slice(0, 5);
for (const d of top5) {
console.log(` ${padRight(d.iso2 + ':', 5)} WM rank ${padRight(String(d.wmRank), 3)}, ${label} rank ${padRight(String(d.refRank), 3)} (delta ${d.delta})`);
}
console.log('');
}
const allCountriesSorted = [...wmScores.entries()]
.sort((a, b) => b[1] - a[1])
.map(([iso2, score], i) => ({ iso2, score, rank: i + 1 }));
console.log('WorldMonitor score ranking (sample):');
console.log(' Rank ISO2 Score');
for (const entry of allCountriesSorted) {
console.log(` ${padRight(String(entry.rank), 6)}${padRight(entry.iso2, 6)}${entry.score.toFixed(1)}`);
}
return { ndgainRho: ndgainResult.rho, informRho: informResult.rho, gatePass };
}
const isMain = process.argv[1]?.endsWith('validate-resilience-correlation.mjs');
if (isMain) {
run().catch((err) => {
console.error(`FATAL: ${err instanceof Error ? err.message : String(err)}`);
process.exit(1);
});
}
export { run, spearmanRho, toRanks, pearson, computeCorrelation, REFERENCE_INDICES, SAMPLE_COUNTRIES };