// ============================================================================ // import-scanstat-04-score.js — SCORING THE PRE-REGISTERED PREDICTIONS // foreign-import experiment, IMPORT-MAP row 1 (scan statistics), part 4 of 4 // ============================================================================ // Computes the moving-sum family at T_23 (in memory) and at T_29 (segmented // streaming, the compute lever: 6.47e9 positions, 2.15e8 slots, ring buffer of // depth 64, no gap word ever materialised), then scores the two laws frozen by // research/import-scanstat-03-prereg.js into // research/history/staging/import-scanstat-prereg.md. // // MODEL A sd_m = c* m^H* the import's two-parameter fix // MODEL B sd_m = sd_1* sqrt(m) TODO 0c's incumbent // riders A' and B' anchored at the target level's EXACT sd_1, so that the // comparison is the exponent alone // maxsum_m = m*mbar + sd_m*sqrt(2 ln D) in every case // KILL A must beat B on ln-RMS of excess_m = maxsum_m - m*mbar at T_23 // // CUSTODY. The fit is recomputed here from T_13, T_17, T_19 by the same rule // and checked against the frozen file row by row before anything is scored; a // mismatch aborts. // // node --max-old-space-size=8192 research/import-scanstat-04-score.js // ============================================================================ 'use strict'; const T0=Date.now(); const el=()=>((Date.now()-T0)/1000).toFixed(1)+'s'; const fs=require('fs'), path=require('path'); const C2=require('./import-chaining-02.js'); const MS=[1,2,3,4,6,8,12,16,24,32,48,64]; const MMAX=64, RING=128, MASK=127; const FIT=[13,17,19]; function primesTo(n){ const s=[]; for(let i=2;i<=n;i++){ let p=true; for(let j=2;j*j<=i;j++) if(i%j===0){p=false;break;} if(p) s.push(i);} return s; } function tileDW(x){ let D=1,W=6; for(const p of primesTo(x)) if(p>=5){ D*=(p-2); W*=p; } return {D,W}; } function ols(xs,ys){ const n=xs.length; const mx=xs.reduce((a,b)=>a+b)/n, my=ys.reduce((a,b)=>a+b)/n; let sxy=0,sxx=0; for(let i=0;i2?Math.sqrt(ss/(n-2)/sxx):NaN; return {a,b,se}; } // ---- exact moving-sum family by streaming the slot sequence ----------------- function streamStats(x){ const PR=primesTo(x); const {D:Dexp,W}=tileDW(x); const SEG=100_000_000; const buf=new Uint8Array(Math.min(SEG,W)); const ring=new Float64Array(RING); const nM=MS.length; const mx=new Float64Array(nM).fill(-Infinity), sm=new Float64Array(nM), s2=new Float64Array(nM); const mn=new Float64Array(nM).fill(Infinity); const head=new Float64Array(MMAX); // first MMAX slots, for the wrap let j=0; // global slot index const emit=(s)=>{ ring[j&MASK]=s; if(j=0&&imx[t])mx[t]=v; if(vmxr) mxr=rel; } return {rms:Math.sqrt(s/actual.length), maxrel:mxr}; } function main(){ console.log('IMPORT-MAP row 1 / part 4 --- computing T_23 and T_29, scoring the frozen laws\n'); // ---- refit and check against the frozen file const fr=[]; for(const x of FIT){ const T=C2.tile(x); const st=C2.maxsumStats(T,MS); const f=ols(MS.map(Math.log), st.map(r=>Math.log(r.sd))); fr.push({x,D:T.D,lnD:Math.log(T.D),sd1:st[0].sd,a:f.a,H:f.b}); } const fa=ols(fr.map(r=>r.lnD), fr.map(r=>r.a)); const fH=ols(fr.map(r=>r.lnD), fr.map(r=>r.H)); const fb=ols(fr.map(r=>r.lnD), fr.map(r=>Math.log(r.sd1))); const frozen=fs.readFileSync(path.resolve(__dirname,'history','staging','import-scanstat-prereg.md'),'utf8'); console.log('(0) CUSTODY: refit vs the frozen pre-registration file'); let bad=0; for(const x of [23,29]){ const {D,W}=tileDW(x); const lnD=Math.log(D), mbar=W/D, tail=Math.sqrt(2*lnD); const A=Math.exp(fa.a+fa.b*lnD), H=fH.a+fH.b*lnD, B=Math.exp(fb.a+fb.b*lnD); for(const m of MS){ const sdA=A*Math.pow(m,H), sdB=B*Math.sqrt(m); const want=`| ${m} | ${sdA.toFixed(3)} | ${(m*mbar+sdA*tail).toFixed(1)} | ${sdB.toFixed(3)} | ${(m*mbar+sdB*tail).toFixed(1)} |`; if(!frozen.includes(want)){ bad++; console.log(' MISMATCH '+want); } } } if(bad) throw new Error(`${bad} pre-registered rows do not match the refit`); console.log(` all ${2*MS.length} pre-registered rows reproduce exactly. [${el()}]`); // ---- the two target levels const out={}; for(const x of [23,29]){ const t=streamStats(x); out[x]=t; console.log(`\n(${x===23?'1':'2'}) T_${x} COMPUTED. D = ${t.D} W = ${t.W} mbar = ${(t.W/t.D).toFixed(5)} sqrt(2 lnD) = ${Math.sqrt(2*Math.log(t.D)).toFixed(4)} [${el()}]`); const s1=t.rows[0].sd; const f=ols(MS.map(Math.log), t.rows.map(r=>Math.log(r.sd))); console.log(` measured exponent H(T_${x}) = ${f.b.toFixed(4)} +/- ${f.se.toFixed(4)} exact sd_1 = ${s1.toFixed(4)}`); console.log(' m | maxsum_m | excess_m | sd_m | sd_m/(sd_1 sqrt m) | excess/sd_m'); for(const r of t.rows){ const exc=r.maxsum-r.m*t.W/t.D; console.log(` ${String(r.m).padStart(3)} | ${String(r.maxsum).padStart(9)} | ${exc.toFixed(1).padStart(8)} | ${r.sd.toFixed(3).padStart(8)} | ${(r.sd/(s1*Math.sqrt(r.m))).toFixed(4)} | ${(exc/r.sd).toFixed(3)}`); } } // ---- the exponent, in and out of sample, in one place console.log('\n(3) THE EXPONENT SERIES AND THE PRE-REGISTERED PREDICTION'); console.log(' level | lnD | measured H +/- se | pre-registered H* | miss (in s.e.)'); for(const r of fr) console.log(` T_${String(r.x).padStart(2)} | ${r.lnD.toFixed(4)} | ${r.H.toFixed(4)} (in sample) | -- | --`); for(const x of [23,29]){ const t=out[x]; const lnD=Math.log(t.D); const f=ols(MS.map(Math.log), t.rows.map(r=>Math.log(r.sd))); const Hs=fH.a+fH.b*lnD; console.log(` T_${String(x).padStart(2)} | ${lnD.toFixed(4)} | ${f.b.toFixed(4)} +/- ${f.se.toFixed(4)} | ${Hs.toFixed(4)} | ${Math.abs(f.b-Hs).toFixed(4)} = ${(Math.abs(f.b-Hs)/f.se).toFixed(2)} s.e.`); } console.log('\n(4) THE OUT-OF-SAMPLE SCORE, against the frozen predictions'); console.log(' level | quantity | model | ln-RMS | max |rel err| | verdict'); for(const x of [23,29]){ const t=out[x], mbar=t.W/t.D, lnD=Math.log(t.D), tail=Math.sqrt(2*lnD), s1=t.rows[0].sd; const A=Math.exp(fa.a+fa.b*lnD), H=fH.a+fH.b*lnD, B=Math.exp(fb.a+fb.b*lnD); const actSd=t.rows.map(r=>r.sd), actEx=t.rows.map(r=>r.maxsum-r.m*mbar); const mk=(c,h)=>MS.map(m=>c*Math.pow(m,h)); const cases=[['A c* m^H* ', mk(A,H)], ['B sd_1* sqrt m ', mk(B,0.5)], ["A' sd_1 m^H* ", mk(s1,H)], ["B' sd_1 sqrt m ", mk(s1,0.5)]]; for(const [name,sd] of cases){ const ex=sd.map((v,i)=>v*tail); const a=score(actSd,sd), b=score(actEx,ex); console.log(` T_${String(x).padStart(2)} | sd_m | ${name} | ${a.rms.toFixed(4)} | ${(a.maxrel*100).toFixed(1)}% |`); console.log(` T_${String(x).padStart(2)} | excess_m | ${name} | ${b.rms.toFixed(4)} | ${(b.maxrel*100).toFixed(1)}% |`); } const exA=score(actEx, mk(A,H).map(v=>v*tail)), exB=score(actEx, mk(B,0.5).map(v=>v*tail)); const exA2=score(actEx, mk(s1,H).map(v=>v*tail)), exB2=score(actEx, mk(s1,0.5).map(v=>v*tail)); console.log(` T_${x} KILL CRITERION on excess_m: A ${exA.rms.toFixed(4)} vs B ${exB.rms.toFixed(4)} -> ${exA.rms ${exA2.rms A WINS, import survives // T_23 anchored rider : A' 0.2669 vs B' 0.4612 -> A' wins (ratio B'/A' = 1.73) // T_29 | sd_m | A c* m^H* | 0.1496 | 23.9% | // T_29 | excess_m | A c* m^H* | 0.1998 | 33.8% | // T_29 | sd_m | B sd_1* sqrt m | 0.5275 | 132.0% | // T_29 | excess_m | B sd_1* sqrt m | 0.5136 | 150.1% | // T_29 | sd_m | A' sd_1 m^H* | 0.0747 | 10.5% | // T_29 | excess_m | A' sd_1 m^H* | 0.3044 | 41.7% | // T_29 | sd_m | B' sd_1 sqrt m | 0.3608 | 92.0% | // T_29 | excess_m | B' sd_1 sqrt m | 0.4211 | 106.9% | // T_29 KILL CRITERION on excess_m: A 0.1998 vs B 0.5136 -> A WINS, import survives // T_29 anchored rider : A' 0.3044 vs B' 0.4211 -> A' wins (ratio B'/A' = 1.38) // // [38.9s] done // ============================================================ // READINGS // ============================================================ // // 1. THE ENGINE IS RIGHT, ON TWO INDEPENDENT CHECKS. [VERIFIED] The streaming // ring-buffer walker gives maxsum_1 = 204 at T_23 and 258 at T_29, which are // G2(23#) and G2(29#) as carried by research/exact-g2-ladder.js and by // research/05b-twin-jacobsthal-segmented.js. Its slot counts reproduce // prod(p-2) = 7952175 and 214708725 exactly, and it never materialises a gap // word: T_29 is W = 6469693230 positions in 38.9 s and constant memory. // // 2. THE T_23 EXPONENT REPRODUCES. [VERIFIED] H(T_23) = 0.3216 +/- 0.0096 here // against 0.3216 +/- 0.0096 in import-chaining-03.js (d), from a different // code path (streaming ring buffer against an in-memory slot array), and the // column sd_m/(sd_1 sqrt m) matches it to four figures at all twelve m. // // 3. THE BLIND LEVEL LANDS ON THE PREDICTION. [MEASURED] T_29 has never been // computed in this corpus for any moving-sum statistic. The pre-registered // H* was 0.3383; the measured H(T_29) is 0.3367 +/- 0.0080. The miss is // 0.0016, a fifth of one standard error. The exponent series is now 0.2661, // 0.2804, 0.3001, 0.3216, 0.3367 at five exactly computable levels. // // 4. THE KILL CRITERION IS PASSED, AT BOTH LEVELS, WIDELY. [MEASURED] On // excess_m the ln-RMS is A 0.1952 against B 0.5013 at T_23 and A 0.1998 // against B 0.5136 at T_29. On sd_m alone it is 0.0592 against 0.4532 and // 0.1496 against 0.5275. The anchored riders, which differ from each other // in exactly one number, give A' 0.2669 against B' 0.4612 at T_23 and A' // 0.3044 against B' 0.4211 at T_29. Every comparison goes the same way. // // 5. THE INCUMBENT IS NOT MERELY BEATEN, IT IS OUT BY A FACTOR OF TWO. // [MEASURED] Model B's worst relative error on excess_m is 159.6% at T_23 // and 150.1% at T_29; sd_m/(sd_1 sqrt m) reaches 0.4973 at T_23 and 0.5209 // at T_29 at m = 64. sigma sqrt(m) overstates the fluctuation by a factor // two at the top of the grid, at every level tested. // // 6. AND THE REPLACEMENT IS NOT A LAW EITHER. [MEASURED] Model A still carries // a 29.6% worst-case error at T_23 and 33.8% at T_29 on excess_m, because // the tail factor is not sqrt(2 ln D): excess/sd_m runs 9.039 down to 4.707 // at T_23 and 10.628 down to 5.747 at T_29 against sqrt(2 lnD) = 5.6372 and // 6.1943. The m-dependence has moved out of the sd factor and into the tail // factor; it has not been removed. What is established is the exponent, not // the law. // // 7. WHAT THIS SETTLES FOR TODO 0c. The sqrt(m) in // maxsum_m = m*mbar + sigma sqrt(2 m ln D) is refuted out of sample at two // further levels, one of them blind, and the replacement exponent is // level-dependent: H rises from 0.2661 at T_13 to 0.3367 at T_29 with no // sign of settling. "Fit H as a function of D and m" is now backed // by a prediction that worked, and no single-sigma form should be quoted.