# stack-a: first-print mixture estimator and silence variance The frozen combination has a positive paired gain exceeding two standard errors; treat it as a research candidate. The held-out paired change is **+0.099902354 ± 0.023650143 SE** over **2,000 seeds**, 2840000–2841999. This run does not establish a top-10 or first-place result. `blocked: null`. Setup, compilation, calibration, and measurement completed locally. No leaderboard submission was made. NP2 was reconstructed exactly from [TWO](https://api.imd.fun/artifacts/528a6da78583c6142644a552c86670f9d25c20e39e92f4de44deae05ddc8cecd) and the specified [NP routing block](https://api.imd.fun/artifacts/da313379a2e69e9286668865e06b18dfcae25732c82b934b0fd9c2ef0d8c2ff5): SHA-256 `51f1c1a9d4999ee460eb8bbc66880da8074b173399a4f5d71f96671482e18ac0`. The reconstruction extracts NP code from `// Interior retail routing` through the character before `uint256 tradeRatio =`, then inserts that substring before the first such marker in TWO code. Its 526.1156 calibration score is the requester's supplied figure, not an additional local claim. The pinned simulator is [6b6921f8d463314aa7c96234e1be25a25245f704](https://github.com/benedictbrady/amm-challenge/tree/6b6921f8d463314aa7c96234e1be25a25245f704). Local CH calibration on 0–999 returned **525.5258821780834**, matching the required 525.525882178 to the stated precision. [CH source](https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol) SHA-256: `6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3`. ## Frozen mechanism Selected tuning point: `h0`. Parameters and selection evidence are below. First-print `P(arb)` is a tuned heuristic mixture weight, not a claim of calibrated probability. The final code uses `z=abs(pImplied/pHat-1)/sigmaHat` and the 1.22 threshold, elapsed silence, and trade size. Accumulated variance enters `wA=.5+boost*v/(v+.0067²)` with the selected boost, keeping wA between .5 and 0.55; .0067 is the retail noise assumption supplied in the brief. The actual gain is `P*wA + (1-P)*wR`; later prints have no arbitrage component. Both later implied-price updates and inherited normalizer-price updates are allowed only in the first-print direction. Posterior variance in slot 13 receives `sigmaHat²*elapsed` before the first print and shrinks by `1-alpha` after observation. It is explicitly initialized; silence is not capped in the frozen code. The existing NP2 fee equations remain, with the reported stale coefficient changes. Original unused constants are retained to keep the edit narrow. All numerical results below are local simulator measurements; citations identify external code inputs, not external certification of these results. ## Paired confirmation ```json { "sha256": "f54f9db7a54e54b751bb4bc61bc35af70b5b7696d677f35b78117d9d0405ee75", "seed_start": 2840000, "seed_end": 2841999, "paired": { "n": 2000, "mean_diff": 0.09990235436130175, "se": 0.0236501428799468, "worst": [ { "seed": 2841455, "diff": -9.950956029918757, "cand": 585.6236964909019, "np2": 595.5746525208207, "cand_arb": -46.25709836942683, "cand_retail": 631.8807948603301, "np2_arb": -39.34063132722426, "np2_retail": 634.9152838480428 }, { "seed": 2840494, "diff": -3.5162997674873395, "cand": 618.7151107475344, "np2": 622.2314105150217, "cand_arb": -38.73612222944173, "cand_retail": 657.4512329769758, "np2_arb": -41.124301498996054, "np2_retail": 663.3557120140193 }, { "seed": 2841126, "diff": -3.3322911443501653, "cand": 586.2852246637116, "np2": 589.6175158080617, "cand_arb": -24.220295714293016, "cand_retail": 610.5055203780031, "np2_arb": -28.12881099027293, "np2_retail": 617.7463267983364 }, { "seed": 2840291, "diff": -3.1449924931436044, "cand": 579.8771897071173, "np2": 583.0221822002609, "cand_arb": -26.13043899618381, "cand_retail": 606.0076287032998, "np2_arb": -30.173234229101187, "np2_retail": 613.1954164293627 }, { "seed": 2840924, "diff": -3.000665085547439, "cand": 544.0468789119955, "np2": 547.0475439975429, "cand_arb": -26.42631195568596, "cand_retail": 570.4731908676819, "np2_arb": -30.0308613253756, "np2_retail": 577.0784053229178 } ] }, "split": { "cand": { "arb": -27.02953407892355, "retail": 552.5355318064236, "edge": 525.5059977274999 }, "np2": { "arb": -30.9215232507197, "retail": 556.3276186238583, "edge": 525.4060953731387 } }, "blocked": null } ``` The confirmation split changes are **+3.891989172 arb edge** and **-3.792086817 retail edge** per seed. Most of the reduction in arb losses is offset by lower retail edge. The net gain is small compared with the requested +5.8/+9.6 targets. Split values are mean submission edge per seed, not normalizer edge or traded volume. Instrumentation adds separate accumulators at the [original arb and retail edge additions](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/simulation/engine.rs): arb edge is negative arb profit; retail edge is the original trade edge. Each seed's components were checked to sum to total edge within 1e-8. Telemetry-only CH matched the original build exactly on seeds 0–19, and split sums were also checked on 20 NP2 tuning seeds. Confirmation never used candidate versus NP2 in one market. ## Tuning and ablation Initial discovery grids used seeds starting at 10000. Subsequent tuning blocks were 12000–12159 and 14000–14159. All tuning stayed within 10000–99999, with at most 224 scored tuning seeds for any one candidate (64 discovery plus 160 selection), and at most 160 for each final point normalized by raw sigmaHat. Tables show the actual sample size, paired mean difference from NP2, and sample-standard-deviation SE. They are exploratory in-sample results. The 2840000 confirmation block was used once after source freezing, and no parameter changed afterward. The initial grids used the inherited retail routing rule. The `f` grid gates the *actual* normalizer-derived price update as well as the direct later-print update. Its classifier normalized by predictive uncertainty was then corrected to the requested raw sigmaHat normalization: the final `h`/`i` points retain the direction gates and put accumulated variance into wA instead. Non-final grids should not be mistaken for the delivered mechanism. Point parameters: `a`=wA, `r`=wR, `H/L`=small/large-innovation mixture weights, `V`=variance-normalized classifier, `D`=same-direction direct later update (0 means none), `R`=routing (0 off, 1 inherited, 2 gated by implied direction, `same_actual` gated by actual routed estimate), `S/A`=STALE_DIR_COEF/STALE_ATTRACT_FRAC in WAD units. Initial defaults are H=.9, L=.05. Over elapsed gaps >1 the threshold classifier multiplies its weight by `(elapsed+2)/(elapsed+3)`; tradeRatio>2*sigmaHat halves it. The exploratory tree family (`t`, and `u` points marked tree) uses normalized residual cutoffs .76/.78, 1.22, 1.69; leaves for one-step gaps are .84/.67/.41/.11 and for longer gaps .47/.24/.10/.02. TradeRatio>sigmaHat multiplies the weight by .75. Power 2 squares that weight; power 3 cubes it. These heuristic leaves were informed by first-print observations from 20 NP2 tuning seeds 11000–11019; they were not used in the frozen threshold candidate. ### tuning.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | p0 | 128 | 0.5 | 0.03 | 0.9/0.05 | 0,0,0 | 0.685/1.05 | threshold/1 | -19.08370056 | 0.44677152 | | p1 | 128 | 0.5 | 0.03 | 0.9/0.05 | 0,0,1 | 0.685/1.05 | threshold/1 | -18.60500958 | 0.43703179 | | p2 | 128 | 0.5 | 0.03 | 0.9/0.05 | 0,0,2 | 0.685/1.05 | threshold/1 | -18.70177460 | 0.45474688 | | p3 | 128 | 0.75 | 0.03 | 0.9/0.05 | 0,0,0 | 0.685/1.05 | threshold/1 | -24.86271990 | 0.51367336 | | p4 | 128 | 0.75 | 0.03 | 0.9/0.05 | 0,0,1 | 0.685/1.05 | threshold/1 | -24.93120702 | 0.49080659 | | p5 | 128 | 0.75 | 0.03 | 0.9/0.05 | 0,0,2 | 0.685/1.05 | threshold/1 | -24.78568565 | 0.49735779 | | p6 | 128 | 1.0 | 0.03 | 0.9/0.05 | 0,0,0 | 0.685/1.05 | threshold/1 | -32.70633602 | 0.68417900 | | p7 | 128 | 1.0 | 0.03 | 0.9/0.05 | 0,0,1 | 0.685/1.05 | threshold/1 | -32.68448438 | 0.66704660 | | p8 | 128 | 1.0 | 0.03 | 0.9/0.05 | 0,0,2 | 0.685/1.05 | threshold/1 | -32.91413091 | 0.71789231 | | p9 | 128 | 0.5 | 0.03 | 0.9/0.05 | 1,0,0 | 0.685/1.05 | threshold/1 | -5.60423090 | 0.27900943 | | p10 | 128 | 0.5 | 0.03 | 0.9/0.05 | 1,0,1 | 0.685/1.05 | threshold/1 | -5.13219788 | 0.27291858 | | p11 | 128 | 0.5 | 0.03 | 0.9/0.05 | 1,0,2 | 0.685/1.05 | threshold/1 | -5.22097836 | 0.27307639 | | p12 | 128 | 0.75 | 0.03 | 0.9/0.05 | 1,0,0 | 0.685/1.05 | threshold/1 | -13.49031095 | 0.28869725 | | p13 | 128 | 0.75 | 0.03 | 0.9/0.05 | 1,0,1 | 0.685/1.05 | threshold/1 | -13.28547370 | 0.27676292 | | p14 | 128 | 0.75 | 0.03 | 0.9/0.05 | 1,0,2 | 0.685/1.05 | threshold/1 | -13.31068820 | 0.29224212 | | p15 | 128 | 1.0 | 0.03 | 0.9/0.05 | 1,0,0 | 0.685/1.05 | threshold/1 | -26.44836706 | 0.45807880 | | p16 | 128 | 1.0 | 0.03 | 0.9/0.05 | 1,0,1 | 0.685/1.05 | threshold/1 | -26.32431919 | 0.47132469 | | p17 | 128 | 1.0 | 0.03 | 0.9/0.05 | 1,0,2 | 0.685/1.05 | threshold/1 | -26.55127278 | 0.48851417 | ### tuning2.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | q0 | 128 | 0.5 | 0.06 | 0.25/0.05 | 0,1,1 | 0.685/1.05 | threshold/1 | -6.17707189 | 0.21824217 | | q1 | 128 | 0.5 | 0.06 | 0.25/0.05 | 1,1,1 | 0.685/1.05 | threshold/1 | -2.38877162 | 0.13430787 | | q2 | 128 | 0.5 | 0.06 | 0.25/0.2 | 0,1,1 | 0.685/1.05 | threshold/1 | -2.64074101 | 0.13275822 | | q3 | 128 | 0.5 | 0.06 | 0.25/0.2 | 1,1,1 | 0.685/1.05 | threshold/1 | -2.14813255 | 0.12533604 | | q4 | 128 | 0.5 | 0.06 | 0.25/0.4 | 0,1,1 | 0.685/1.05 | threshold/1 | -1.60427931 | 0.10809546 | | q5 | 128 | 0.5 | 0.06 | 0.25/0.4 | 1,1,1 | 0.685/1.05 | threshold/1 | -2.27123089 | 0.11890994 | | q6 | 128 | 0.5 | 0.06 | 0.4/0.05 | 0,1,1 | 0.685/1.05 | threshold/1 | -5.17494919 | 0.20413763 | | q7 | 128 | 0.5 | 0.06 | 0.4/0.05 | 1,1,1 | 0.685/1.05 | threshold/1 | -0.73135548 | 0.12566700 | | q8 | 128 | 0.5 | 0.06 | 0.4/0.2 | 0,1,1 | 0.685/1.05 | threshold/1 | -1.50110967 | 0.12747753 | | q9 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.685/1.05 | threshold/1 | -0.26248841 | 0.11497412 | | q10 | 128 | 0.5 | 0.06 | 0.4/0.4 | 0,1,1 | 0.685/1.05 | threshold/1 | -0.12356094 | 0.09924622 | | q11 | 128 | 0.5 | 0.06 | 0.4/0.4 | 1,1,1 | 0.685/1.05 | threshold/1 | -0.12356094 | 0.09924622 | | q12 | 128 | 0.5 | 0.06 | 0.55/0.05 | 0,1,1 | 0.685/1.05 | threshold/1 | -5.45682271 | 0.19818671 | | q13 | 128 | 0.5 | 0.06 | 0.55/0.05 | 1,1,1 | 0.685/1.05 | threshold/1 | -0.68634645 | 0.12121206 | | q14 | 128 | 0.5 | 0.06 | 0.55/0.2 | 0,1,1 | 0.685/1.05 | threshold/1 | -1.54645376 | 0.13635297 | | q15 | 128 | 0.5 | 0.06 | 0.55/0.2 | 1,1,1 | 0.685/1.05 | threshold/1 | -0.08981846 | 0.10451217 | | q16 | 128 | 0.5 | 0.06 | 0.55/0.4 | 0,1,1 | 0.685/1.05 | threshold/1 | +0.13025216 | 0.09003443 | | q17 | 128 | 0.5 | 0.06 | 0.55/0.4 | 1,1,1 | 0.685/1.05 | threshold/1 | +0.20143974 | 0.08502310 | ### tuning3.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | r0 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.3/0.6 | threshold/1 | -45.25261268 | 0.81912453 | | r1 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.3/1.0 | threshold/1 | -40.29249523 | 0.70224590 | | r2 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.3/1.4 | threshold/1 | -38.27954763 | 0.65086479 | | r3 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.5/0.6 | threshold/1 | -14.66792978 | 0.29154890 | | r4 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.5/1.0 | threshold/1 | -9.93455609 | 0.23107743 | | r5 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.5/1.4 | threshold/1 | -9.66240169 | 0.22132957 | | r6 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.8/0.6 | threshold/1 | -14.06812799 | 1.28347485 | | r7 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.8/1.0 | threshold/1 | -13.72774148 | 1.37320172 | | r8 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.8/1.4 | threshold/1 | -15.18001767 | 1.32561373 | | r9 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 1.1/0.6 | threshold/1 | -193.13554858 | 4.96508282 | | r10 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 1.1/1.0 | threshold/1 | -194.55177547 | 4.83961623 | | r11 | 128 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 1.1/1.4 | threshold/1 | -196.40911594 | 5.08894994 | ### tuning4.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | t0 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.5/1.05 | tree/1 | -7.65986719 | 0.21041798 | | t1 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.9/1.05 | tree/1 | -147.73280754 | 5.16067536 | | t2 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 1.3/1.05 | tree/1 | -305.84831870 | 5.47848475 | | t3 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 0.5/1.05 | tree/1 | -8.03070673 | 0.21290300 | | t4 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 0.9/1.05 | tree/1 | -95.82049589 | 3.29660441 | | t5 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 1.3/1.05 | tree/1 | -275.23275137 | 4.99565240 | | t6 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.5/1.05 | tree/2 | -8.81060343 | 0.21307061 | | t7 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.9/1.05 | tree/2 | -110.87041761 | 4.32942390 | | t8 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 1.3/1.05 | tree/2 | -286.33868431 | 5.17505610 | | t9 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 0.5/1.05 | tree/2 | -7.68056706 | 0.21443931 | | t10 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 0.9/1.05 | tree/2 | -81.14065785 | 3.00203546 | | t11 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 1.3/1.05 | tree/2 | -267.07837884 | 4.92949464 | | t12 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.5/1.05 | tree/3 | -10.07081092 | 0.21679240 | | t13 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.9/1.05 | tree/3 | -92.48830091 | 3.71554432 | | t14 | 128 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 1.3/1.05 | tree/3 | -274.74276897 | 5.30434141 | | t15 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 0.5/1.05 | tree/3 | -7.96945868 | 0.21586107 | | t16 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 0.9/1.05 | tree/3 | -73.56751903 | 3.02911278 | | t17 | 128 | 0.5 | 0.06 | 0.9/0.05 | 1,1,1 | 1.3/1.05 | tree/3 | -266.55586834 | 5.45975793 | ### tuning5.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | u0 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.58/0.85 | threshold/1 | -4.17685713 | 0.18259760 | | u1 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.58/1.05 | threshold/1 | -3.31644954 | 0.17310501 | | u2 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.58/1.25 | threshold/1 | -3.38982480 | 0.17508301 | | u3 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.64/0.85 | threshold/1 | -1.18435853 | 0.14412991 | | u4 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.64/1.05 | threshold/1 | -0.72519709 | 0.13723819 | | u5 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.64/1.25 | threshold/1 | -1.02993968 | 0.13944103 | | u6 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.7/0.85 | threshold/1 | -0.36001250 | 0.19970549 | | u7 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.7/1.05 | threshold/1 | -0.30947639 | 0.19442309 | | u8 | 64 | 0.5 | 0.06 | 0.4/0.2 | 1,1,1 | 0.7/1.25 | threshold/1 | -0.85296849 | 0.18949662 | | u9 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.58/0.85 | tree/1 | -3.03318104 | 0.17783850 | | u10 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.58/1.05 | tree/1 | -2.85594880 | 0.18001175 | | u11 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.58/1.25 | tree/1 | -3.30050903 | 0.18526904 | | u12 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.64/0.85 | tree/1 | -2.11173495 | 0.19212888 | | u13 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.64/1.05 | tree/1 | -2.25253401 | 0.19752320 | | u14 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.64/1.25 | tree/1 | -2.75641887 | 0.20125617 | | u15 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.7/0.85 | tree/1 | -4.63086743 | 0.39385964 | | u16 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.7/1.05 | tree/1 | -4.76226019 | 0.34282638 | | u17 | 64 | 0.5 | 0.06 | 0.9/0.05 | 0,1,1 | 0.7/1.25 | tree/1 | -5.27135125 | 0.30487187 | ### tuning6.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | v0 | 64 | 0.5 | 0.1 | 0.4/0.2 | 1,1,1 | 0.62/1.05 | threshold/1 | -1.46881938 | 0.14102514 | | v1 | 64 | 0.5 | 0.1 | 0.4/0.2 | 1,1,1 | 0.66/1.05 | threshold/1 | -0.13440999 | 0.12049360 | | v2 | 64 | 0.5 | 0.1 | 0.4/0.2 | 1,1,1 | 0.7/1.05 | threshold/1 | +0.12577768 | 0.16286737 | | v3 | 64 | 0.5 | 0.1 | 0.5/0.2 | 1,1,1 | 0.62/1.05 | threshold/1 | -1.41809366 | 0.13799029 | | v4 | 64 | 0.5 | 0.1 | 0.5/0.2 | 1,1,1 | 0.66/1.05 | threshold/1 | -0.17689845 | 0.12501294 | | v5 | 64 | 0.5 | 0.1 | 0.5/0.2 | 1,1,1 | 0.7/1.05 | threshold/1 | -0.17041695 | 0.17919053 | | v6 | 64 | 0.5 | 0.1 | 0.5/0.45 | 1,1,1 | 0.62/1.05 | threshold/1 | -2.15262516 | 0.15172008 | | v7 | 64 | 0.5 | 0.1 | 0.5/0.45 | 1,1,1 | 0.66/1.05 | threshold/1 | -0.65127397 | 0.11763845 | | v8 | 64 | 0.5 | 0.1 | 0.5/0.45 | 1,1,1 | 0.7/1.05 | threshold/1 | -0.13655737 | 0.16126944 | | v9 | 64 | 0.5 | 0.1 | 0.6/0.2 | 1,1,1 | 0.62/1.05 | threshold/1 | -1.55029579 | 0.14533053 | | v10 | 64 | 0.5 | 0.1 | 0.6/0.2 | 1,1,1 | 0.66/1.05 | threshold/1 | -0.49922349 | 0.13744320 | | v11 | 64 | 0.5 | 0.1 | 0.6/0.2 | 1,1,1 | 0.7/1.05 | threshold/1 | -0.64234468 | 0.19465350 | ### tuning7.json | point | n | a | r | H/L | V,D,R | S/A | family/power | mean_diff | se | |---|---:|---:|---:|---|---|---|---|---:|---:| | f0 | 64 | 0.5 | 0.1 | 0.35/0.2 | 1,1,same_actual | 0.64/1.05 | threshold/1 | -0.71336524 | 0.13803073 | | f1 | 64 | 0.5 | 0.1 | 0.35/0.2 | 1,1,same_actual | 0.67/1.05 | threshold/1 | -0.17691934 | 0.12918322 | | f2 | 64 | 0.5 | 0.1 | 0.35/0.2 | 1,1,same_actual | 0.7/1.05 | threshold/1 | -0.20104595 | 0.17083164 | | f3 | 64 | 0.5 | 0.1 | 0.45/0.2 | 1,1,same_actual | 0.64/1.05 | threshold/1 | -0.59527890 | 0.13008785 | | f4 | 64 | 0.5 | 0.1 | 0.45/0.2 | 1,1,same_actual | 0.67/1.05 | threshold/1 | +0.04374156 | 0.14072280 | | f5 | 64 | 0.5 | 0.1 | 0.45/0.2 | 1,1,same_actual | 0.7/1.05 | threshold/1 | -0.17155847 | 0.18057615 | | f6 | 64 | 0.5 | 0.1 | 0.55/0.2 | 1,1,same_actual | 0.64/1.05 | threshold/1 | -0.74270616 | 0.13522037 | | f7 | 64 | 0.5 | 0.1 | 0.55/0.2 | 1,1,same_actual | 0.67/1.05 | threshold/1 | -0.30192063 | 0.15096963 | | f8 | 64 | 0.5 | 0.1 | 0.55/0.2 | 1,1,same_actual | 0.7/1.05 | threshold/1 | -0.53543668 | 0.19348541 | | f9 | 64 | 0.5 | 0.1 | 0.45/0.2 | 1,1,same_actual | 0.67/0.95 | threshold/1 | -0.00966386 | 0.13503455 | | f10 | 64 | 0.5 | 0.1 | 0.45/0.2 | 1,1,same_actual | 0.67/1.15 | threshold/1 | -0.04430395 | 0.13001439 | ### Additional tuning selection ```json { "rule": "Final candidates preserve the requested residual/sigmaHat classifier. Variance instead increases wA from .5 by boost*v/(v+.0067^2). Select the highest paired mean among eight h points on tuning seeds 14000..14159; freeze before holdout. Then compare attract fractions .95 and 1.15 on the best h point, on the same tuning block; choose highest mean among these ten and freeze.", "prior_predictive_normalization_comparison": { "rule": "Top three final f-grid points by 64-seed discovery mean; select highest mean on 160 additional tuning seeds, then freeze source before holdout.", "points": [ { "point": { "name": "f4", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "seed_start": 12000, "n": 160, "mean_diff": -0.06553736518388006, "se": 0.07560041703653175, "discovery": { "point": { "name": "f4", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "n": 64, "mean_diff": 0.0437415574289135, "se": 0.1407228032858577, "seconds": 79.1095073223114 } }, { "point": { "name": "f9", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 0.95, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "seed_start": 12000, "n": 160, "mean_diff": -0.1441296245392305, "se": 0.07918184117908272, "discovery": { "point": { "name": "f9", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 0.95, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "n": 64, "mean_diff": -0.009663855777065855, "se": 0.1350345454528538, "seconds": 38.131449460983276 } }, { "point": { "name": "f10", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.15, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "seed_start": 12000, "n": 160, "mean_diff": -0.1481698298205849, "se": 0.08196154933599874, "discovery": { "point": { "name": "f10", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.15, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "n": 64, "mean_diff": -0.04430394852836628, "se": 0.13001438675692573, "seconds": 38.11395883560181 } } ], "chosen": { "name": "f4", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": 1, "route": "same_actual", "ph": 0.45, "pl": 0.2 }, "sha256": "8cc37d94f94bb9d18500d5e163818e6a66b7eafe3b8614f059ae005283b6f76a" }, "points": [ { "point": { "name": "h0", "wa": 0.5, "wr": 0.06, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.05, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": 0.2324770466971664, "se": 0.08895728442647775 }, { "point": { "name": "h1", "wa": 0.5, "wr": 0.06, "sd": 0.685, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.05, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": 0.1636054319400852, "se": 0.10037849112614469 }, { "point": { "name": "h2", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.45, "pl": 0.2, "wa_boost": 0.05, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": -0.2932086942996445, "se": 0.0937748914036068 }, { "point": { "name": "h3", "wa": 0.5, "wr": 0.1, "sd": 0.685, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.45, "pl": 0.2, "wa_boost": 0.05, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": -0.362394407424388, "se": 0.10708113024813674 }, { "point": { "name": "h4", "wa": 0.5, "wr": 0.06, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.25, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": -0.3325840225687102, "se": 0.09886237377412807 }, { "point": { "name": "h5", "wa": 0.5, "wr": 0.06, "sd": 0.685, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.25, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": -0.43877120440689765, "se": 0.10885405007017286 }, { "point": { "name": "h6", "wa": 0.5, "wr": 0.1, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.45, "pl": 0.2, "wa_boost": 0.25, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": -0.31903313205379646, "se": 0.098234781536812 }, { "point": { "name": "h7", "wa": 0.5, "wr": 0.1, "sd": 0.685, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.45, "pl": 0.2, "wa_boost": 0.25, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": -0.4871002940387054, "se": 0.1137702886301108 }, { "point": { "name": "i0", "wa": 0.5, "wr": 0.06, "sd": 0.67, "af": 0.95, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.05, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": 0.22950522285208982, "se": 0.08687800055031623 }, { "point": { "name": "i1", "wa": 0.5, "wr": 0.06, "sd": 0.67, "af": 1.15, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.05, "seed_start": 14000 }, "seed_start": 14000, "n": 160, "mean_diff": 0.05415937409659506, "se": 0.08796683402691839 } ], "chosen": { "name": "h0", "wa": 0.5, "wr": 0.06, "sd": 0.67, "af": 1.05, "prob": 1, "later": 1, "var": "gain", "route": "same_actual", "ph": 0.55, "pl": 0.4, "wa_boost": 0.05, "seed_start": 14000 }, "sha256": "f54f9db7a54e54b751bb4bc61bc35af70b5b7696d677f35b78117d9d0405ee75" } ``` ### Ablations All ablations use tuning seeds 14000–14159. They are conditional changes to the combined mechanism; their effects are not additive. Positive mean_diff_vs_frozen means removing or restoring that part improved the tuning score. no_first_classifier restores first-print alpha=.27; no_variance_gain fixes wA=.5; no_silence_accumulation adds only one step of process variance while retaining elapsed priors; restore_NP2_later_updates restores .06/.12 direct updates and unrestricted interior routing; no_later_updates disables both later paths; restore_NP2_stale_coefficients restores .685 and 1.05. ```json [ { "part": "no_first_classifier", "n": 160, "seed_start": 14000, "mean_diff_vs_np2": -1.6875088759320422, "se_vs_np2": 0.10455480901080008, "mean_diff_vs_frozen": -1.9199859226292086, "se_vs_frozen": 0.0784120036837684, "sha256": "b76c6da586aaa9bf14a9814a3a95e68410ce79e882b79cd0219e642fc5bbd684" }, { "part": "no_variance_gain", "n": 160, "seed_start": 14000, "mean_diff_vs_np2": 0.22403826093871934, "se_vs_np2": 0.08784706187117622, "mean_diff_vs_frozen": -0.008438785758447054, "se_vs_frozen": 0.025225269498241144, "sha256": "270598f862c8ea5ad758d1615d14320e21b2be8af020235e20b254fb7ca4c792" }, { "part": "no_silence_accumulation", "n": 160, "seed_start": 14000, "mean_diff_vs_np2": 0.2243993415432321, "se_vs_np2": 0.088887931973164, "mean_diff_vs_frozen": -0.00807770515393429, "se_vs_frozen": 0.011004332799172612, "sha256": "82ee9109a5ddbdfee20d20ae12e1dec4ae89d5d10b06c4fd7e81ced292cd4fdd" }, { "part": "restore_NP2_later_updates", "n": 160, "seed_start": 14000, "mean_diff_vs_np2": 0.17775690655765358, "se_vs_np2": 0.0717237827127935, "mean_diff_vs_frozen": -0.054720140139512805, "se_vs_frozen": 0.06593269916838605, "sha256": "5fd7a992fd1556a71ae9b7f45c490ffe9d9d168a3acc6f7152a20c847abcef88" }, { "part": "no_later_updates", "n": 160, "seed_start": 14000, "mean_diff_vs_np2": -0.670798702342984, "se_vs_np2": 0.11018683598305533, "mean_diff_vs_frozen": -0.9032757490401504, "se_vs_frozen": 0.07090036103444657, "sha256": "fd8be02f79c97dc0c8ce00535c44eabfc5231e9d00a405ab792fac251c5819cb" }, { "part": "restore_NP2_stale_coefficients", "n": 160, "seed_start": 14000, "mean_diff_vs_np2": 0.1636054319400852, "se_vs_np2": 0.10037849112614469, "mean_diff_vs_frozen": -0.06887161475708119, "se_vs_frozen": 0.03488439769231273, "sha256": "e5d3ea334e67516a77e7bffad591dd3cd60705f6c47355026ba8a303596c6d38" } ] ``` ## Measurement and reproduction Setup was the item-specific script supplied in the request, run once from the pinned checkout under `test/scratch/`. Subsequent checkout commands sourced `scratch/env.sh`. The frozen source passed `amm-match validate scratch/Frozen.sol` (strategy stack_a). Solc was 0.8.24 with the simulator's optimizer/viaIR/Paris settings, through its [compiler](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/evm/compiler.py). The [VanillaStrategy](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/contracts/src/VanillaStrategy.sol) was compiled separately as the normalizer. For each seed `s`, use `g=np.random.default_rng(s)` and draw size `g.uniform(19,21)`, then rate `g.uniform(.6,1)`, then sigma `g.uniform(.000882,.001008)`. Pass all three to `build_config(seed=s, gbm_sigma=sigma, retail_arrival_rate=rate, retail_mean_size=size)`. This follows [MatchRunner's draw order](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py) and the [canonical 10,000-step configuration](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/config.py). Price seed is s and retail seed is s+1 in the pinned engine. Use separate `run_batch(candidate_bytecode, vanilla_bytecode, cfgs, 8)` and `run_batch(np2_bytecode, vanilla_bytecode, cfgs, 8)` calls, in chunks of 20 configurations (tuning grids have a final partial chunk). Read only `edges['submission']` for scoring. `d_s=edge_cand-edge_np2`; mean is the arithmetic mean and SE is `std(d, ddof=1)/sqrt(n)`. Confirmation used exactly 100 full chunks per strategy. The five worst entries are ordered by d_s, not by standalone candidate edge. The following is the exact confirmation runner used; the helper compilation/configuration functions follow immediately afterward. ```python from measure import * import statistics p='scratch/Frozen.sol';source=Path(p).read_bytes();sha=hashlib.sha256(source).hexdigest() a=compile(p);b=compile('scratch/NP2.sol');v=compile('contracts/src/VanillaStrategy.sol','VanillaStrategy') rows=[];start=2840000;n=2000 for offset in range(0,n,20): cfg=configs(start+offset,20) ar=amm_sim_rs.run_batch(a,v,cfg,8).results br=amm_sim_rs.run_batch(b,v,cfg,8).results assert [r.seed for r in ar]==[r.seed for r in br]==list(range(start+offset,start+offset+20)) for x,y in zip(ar,br): for r in [x,y]: assert abs(r.arb_edges['submission']+r.retail_edges['submission']-r.edges['submission'])<1e-8 rows.append(dict(seed=x.seed,diff=x.edges['submission']-y.edges['submission'],cand=x.edges['submission'],np2=y.edges['submission'],cand_arb=x.arb_edges['submission'],cand_retail=x.retail_edges['submission'],np2_arb=y.arb_edges['submission'],np2_retail=y.retail_edges['submission'])) Path('scratch/confirm_rows.json').write_text(json.dumps(rows)) if (offset+20)%200==0: print('completed',offset+20,flush=True) assert Path(p).read_bytes()==source mean=lambda k:statistics.mean(r[k] for r in rows) sd=statistics.stdev(r['diff'] for r in rows) out=dict(sha256=sha,seed_start=start,seed_end=start+n-1,paired=dict(n=n,mean_diff=mean('diff'),se=sd/(n**.5),worst=sorted(rows,key=lambda r:r['diff'])[:5]),split=dict(cand=dict(arb=mean('cand_arb'),retail=mean('cand_retail'),edge=mean('cand')),np2=dict(arb=mean('np2_arb'),retail=mean('np2_retail'),edge=mean('np2'))),blocked=None) Path('scratch/confirm.json').write_text(json.dumps(out,indent=2));print(json.dumps(out,indent=2),flush=True) ``` ```python import sys,json,time,hashlib from pathlib import Path import numpy as np import amm_sim_rs from amm_competition.evm.compiler import SolidityCompiler from amm_competition.competition.config import build_config C=SolidityCompiler() def compile(p,name='Strategy'): s=Path(p).read_text(); key=hashlib.sha256(s.encode()).hexdigest(); cache=Path('scratch/'+key+'.bin') if cache.exists():return list(cache.read_bytes()) r=C.compile(s,name);assert r.success,r.errors cache.write_bytes(r.bytecode);return list(r.bytecode) def configs(start,n): a=[] for s in range(start,start+n): g=np.random.default_rng(s);size=g.uniform(19,21);rate=g.uniform(.6,1);sigma=g.uniform(.000882,.001008) a.append(build_config(seed=s,gbm_sigma=sigma,retail_arrival_rate=rate,retail_mean_size=size)) return a def run(p,start,n): bc=compile(p);v=compile('contracts/src/VanillaStrategy.sol','VanillaStrategy');out=[] for k in range(0,n,20): rs=amm_sim_rs.run_batch(bc,v,configs(start+k,min(20,n-k)),8).results out.extend({'seed':r.seed,'edge':r.edges['submission']} for r in rs) return out if __name__=='__main__': p,start,n,out=sys.argv[1:];t=time.time();r=run(p,int(start),int(n));Path(out).write_text(json.dumps(r));print(p,len(r),np.mean([x['edge'] for x in r]),'seconds',time.time()-t,flush=True) ``` ## Delta (exact source edit) ```diff --- NP2.sol +++ Strategy.sol @@ -39,7 +39,7 @@ uint256 constant ACT_COEF = 91843 * BPS; uint256 constant DIR_COEF = 20 * BPS; uint256 constant DIR_TOX_COEF = 100 * BPS; - uint256 constant STALE_DIR_COEF = 6850 * BPS; + uint256 constant STALE_DIR_COEF = 670000000000000000; uint256 constant SIGMA_TOX_COEF = 500 * BPS; uint256 constant TAIL_KNEE = 600 * BPS; uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000; @@ -49,7 +49,7 @@ slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0; slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD; slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000; - slots[9] = 0; slots[10] = 0; slots[11] = 0; slots[12] = 0; + slots[9] = 0; slots[10] = 0; slots[11] = 0; slots[12] = 0; slots[13] = 0; return (BASE_FEE, BASE_FEE); } @@ -87,38 +87,42 @@ { uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0; - uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL; - uint256 prePhat = pHat; - bool secondPrint = !firstInStep && stepTradeCount == 1; - bool sameDir = false; bool oppDir = false; - if (secondPrint) { - bool up2 = pImplied >= pHat; - uint256 d1 = slots[12]; - if (d1 != 0) { sameDir = (d1 == 1) == up2; oppDir = !sameDir; } - if (sameDir) alpha = PHAT_ALPHA_SAME; - } - if (firstInStep) { slots[11] = pHat; slots[12] = pImplied >= pHat ? 1 : 2; } + uint256 alpha; + if (firstInStep) { + uint256 elapsed = trade.timestamp > lastTs ? trade.timestamp - lastTs : 1; + uint256 variance = slots[13] + wmul(sigmaHat, sigmaHat) * elapsed; + + uint256 z = sigmaHat > 0 ? wdiv(ret, sigmaHat) : 10 * WAD; + uint256 ratio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0; + // Approximate mixture posterior: small innovations are more likely arbitrage. + uint256 prior = z <= 122 * WAD / 100 ? 550000000000000064 : 400000000000000000; + if (elapsed > 1) prior = prior * (elapsed + 2) / (elapsed + 3); + if (ratio > 2 * sigmaHat) prior = prior / 2; + + uint256 arbWeight = 500000000000000000 + wmul(50000000000000000, wdiv(variance, variance + 44890000000000)); + alpha = wmul(prior, arbWeight) + wmul(WAD - prior, 60000000000000000); + slots[11] = pHat; + slots[12] = pImplied >= pHat ? 1 : 2; + slots[13] = wmul(variance, WAD - alpha); + uint256 sigmaRet = ret > RET_CAP ? RET_CAP : ret; + sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(sigmaRet, WAD - SIGMA_DECAY); + } else { + if ((slots[12] == 1) == (pImplied >= pHat)) alpha = 60000000000000000; + } uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT); if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE; - if (ret <= adaptiveGate) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); } - if (oppDir && REVERT_U > 0) { - uint256 pre = slots[11]; - if (pre > pHat) pHat = pHat + wmul(pre - pHat, REVERT_U); - else pHat = pHat - wmul(pHat - pre, REVERT_U); - } - if (firstInStep) { - if (ret > RET_CAP) ret = RET_CAP; - sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY); - } + if (ret <= adaptiveGate) pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); } // Interior retail routing equalizes fee-adjusted marginal prices. - // Subsequent trades in a step cannot be the initial arbitrage trade. + // Subsequent trades are retail; accept only observations in the first-print direction. if (!firstInStep && feeUsed > 30 * BPS && gamma > 0) { uint256 normalSpot = trade.isBuy ? wdiv(wmul(spot, gamma), WAD - 30 * BPS) : wdiv(wmul(spot, WAD - 30 * BPS), gamma); - pHat = wmul(pHat, 90 * WAD / 100) + wmul(normalSpot, 10 * WAD / 100); + if ((slots[12] == 1) == (normalSpot >= pHat)) { + pHat = wmul(pHat, 90 * WAD / 100) + wmul(normalSpot, 10 * WAD / 100); + } } uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0; @@ -223,5 +227,5 @@ return below < WAD ? WAD - below : 0; } - function getName() external pure override returns (string memory) { return "no_cubic"; } + function getName() external pure override returns (string memory) { return "stack_a"; } } ``` ## Full Strategy.sol SHA-256 of exactly the following code bytes, including the single final LF: `f54f9db7a54e54b751bb4bc61bc35af70b5b7696d677f35b78117d9d0405ee75`. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; contract Strategy is AMMStrategyBase { uint256 constant ELAPSED_CAP = 8; uint256 constant SIGNAL_THRESHOLD = WAD / 650; uint256 constant DIR_DECAY = 750000000000000000; uint256 constant ACT_DECAY = 530000000000000000; uint256 constant SIZE_DECAY = 530000000000000000; uint256 constant TOX_DECAY = 910000000000000000; uint256 constant SIGMA_DECAY = 690000000000000000; uint256 constant LAMBDA_DECAY = 990000000000000000; uint256 constant SIZE_BLEND_DECAY = 828000000000000000; uint256 constant TOX_BLEND_DECAY = 51000000000000000; uint256 constant ACT_BLEND_DECAY = 985000000000000000; uint256 constant PHAT_ALPHA = 270000000000000000; uint256 constant PHAT_ALPHA_RETAIL = 60000000000000000; uint256 constant PHAT_ALPHA_SAME = 120000000000000000; uint256 constant REVERT_U = 0; uint256 constant DIR_IMPACT_MULT = 4; uint256 constant GATE_SIGMA_MULT = 7200000000000000000; uint256 constant MIN_GATE = 30000000000000000; uint256 constant TRADE_TOX_BOOST = 3500 * BPS; uint256 constant STALE_ATTRACT_FRAC = 1050000000000000000; uint256 constant RET_CAP = WAD / 35; uint256 constant TOX_CAP = WAD / 5; uint256 constant TRADE_RATIO_CAP = WAD / 5; uint256 constant LAMBDA_CAP = 1820000000000000000; uint256 constant STEP_COUNT_CAP = 64; uint256 constant BASE_FEE = 4 * BPS; uint256 constant SIGMA_COEF = 200000000000000000; uint256 constant LAMBDA_COEF = 12 * BPS; uint256 constant FLOW_SIZE_COEF = 4842 * BPS; uint256 constant TOX_COEF = 300 * BPS; uint256 constant TOX_QUAD_COEF = 15000 * BPS; uint256 constant ACT_COEF = 91843 * BPS; uint256 constant DIR_COEF = 20 * BPS; uint256 constant DIR_TOX_COEF = 100 * BPS; uint256 constant STALE_DIR_COEF = 670000000000000000; uint256 constant SIGMA_TOX_COEF = 500 * BPS; uint256 constant TAIL_KNEE = 600 * BPS; uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000; uint256 constant TAIL_SLOPE_ATTRACT = 850000000000000000; function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) { slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0; slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD; slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000; slots[9] = 0; slots[10] = 0; slots[11] = 0; slots[12] = 0; slots[13] = 0; return (BASE_FEE, BASE_FEE); } function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) { uint256 prevBidFee = slots[0]; uint256 prevAskFee = slots[1]; uint256 lastTs = slots[2]; uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5]; uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8]; uint256 toxEma = slots[9]; uint256 stepTradeCount = slots[10]; bool isNewStep = trade.timestamp > lastTs; if (isNewStep) { uint256 elapsedRaw = trade.timestamp - lastTs; uint256 elapsed = elapsedRaw > ELAPSED_CAP ? ELAPSED_CAP : elapsedRaw; dirState = _decayCentered(dirState, DIR_DECAY, elapsed); actEma = wmul(actEma, _powWad(ACT_DECAY, elapsed)); sizeHat = wmul(sizeHat, _powWad(SIZE_DECAY, elapsed)); toxEma = wmul(toxEma, _powWad(TOX_DECAY, elapsed)); if (stepTradeCount > 0 && elapsedRaw > 0) { uint256 lambdaInst = (stepTradeCount * WAD) / elapsedRaw; if (lambdaInst > LAMBDA_CAP) lambdaInst = LAMBDA_CAP; lambdaHat = wmul(lambdaHat, LAMBDA_DECAY) + wmul(lambdaInst, WAD - LAMBDA_DECAY); } stepTradeCount = 0; } bool firstInStep = stepTradeCount == 0; uint256 spot = trade.reserveX > 0 ? wdiv(trade.reserveY, trade.reserveX) : pHat; if (pHat == 0) pHat = spot; uint256 feeUsed = trade.isBuy ? prevBidFee : prevAskFee; uint256 gamma = feeUsed < WAD ? WAD - feeUsed : 0; uint256 pImplied; if (gamma == 0) { pImplied = spot; } else { pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma); } { uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0; uint256 alpha; if (firstInStep) { uint256 elapsed = trade.timestamp > lastTs ? trade.timestamp - lastTs : 1; uint256 variance = slots[13] + wmul(sigmaHat, sigmaHat) * elapsed; uint256 z = sigmaHat > 0 ? wdiv(ret, sigmaHat) : 10 * WAD; uint256 ratio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0; // Approximate mixture posterior: small innovations are more likely arbitrage. uint256 prior = z <= 122 * WAD / 100 ? 550000000000000064 : 400000000000000000; if (elapsed > 1) prior = prior * (elapsed + 2) / (elapsed + 3); if (ratio > 2 * sigmaHat) prior = prior / 2; uint256 arbWeight = 500000000000000000 + wmul(50000000000000000, wdiv(variance, variance + 44890000000000)); alpha = wmul(prior, arbWeight) + wmul(WAD - prior, 60000000000000000); slots[11] = pHat; slots[12] = pImplied >= pHat ? 1 : 2; slots[13] = wmul(variance, WAD - alpha); uint256 sigmaRet = ret > RET_CAP ? RET_CAP : ret; sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(sigmaRet, WAD - SIGMA_DECAY); } else { if ((slots[12] == 1) == (pImplied >= pHat)) alpha = 60000000000000000; } uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT); if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE; if (ret <= adaptiveGate) pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); } // Interior retail routing equalizes fee-adjusted marginal prices. // Subsequent trades are retail; accept only observations in the first-print direction. if (!firstInStep && feeUsed > 30 * BPS && gamma > 0) { uint256 normalSpot = trade.isBuy ? wdiv(wmul(spot, gamma), WAD - 30 * BPS) : wdiv(wmul(spot, WAD - 30 * BPS), gamma); if ((slots[12] == 1) == (normalSpot >= pHat)) { pHat = wmul(pHat, 90 * WAD / 100) + wmul(normalSpot, 10 * WAD / 100); } } uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0; if (tradeRatio > TRADE_RATIO_CAP) tradeRatio = TRADE_RATIO_CAP; if (tradeRatio > SIGNAL_THRESHOLD) { uint256 push = tradeRatio * DIR_IMPACT_MULT; if (push > WAD / 4) push = WAD / 4; if (trade.isBuy) { dirState = dirState + push; if (dirState > 2 * WAD) dirState = 2 * WAD; } else { dirState = dirState > push ? dirState - push : 0; } actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY); sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY); if (sizeHat > WAD) sizeHat = WAD; } uint256 tox = pHat > 0 ? wdiv(absDiff(spot, pHat), pHat) : 0; if (tox > TOX_CAP) tox = TOX_CAP; toxEma = wmul(toxEma, TOX_BLEND_DECAY) + wmul(tox, WAD - TOX_BLEND_DECAY); uint256 toxSignal = toxEma; stepTradeCount = stepTradeCount + 1; if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP; uint256 flowSize = wmul(lambdaHat, sizeHat); uint256 fBase = BASE_FEE + wmul(SIGMA_COEF, sigmaHat) + wmul(LAMBDA_COEF, lambdaHat) + wmul(FLOW_SIZE_COEF, flowSize); uint256 fMid = fBase + wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma); fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal)); uint256 dirDev; bool sellPressure; if (dirState >= WAD) { dirDev = dirState - WAD; sellPressure = true; } else { dirDev = WAD - dirState; sellPressure = false; } uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal)); uint256 bidFee; uint256 askFee; if (sellPressure) { bidFee = fMid + skew; askFee = fMid > skew ? fMid - skew : 0; } else { askFee = fMid + skew; bidFee = fMid > skew ? fMid - skew : 0; } { uint256 staleShift = wmul(STALE_DIR_COEF, toxSignal); uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC); if (spot >= pHat) { bidFee = bidFee + staleShift; askFee = askFee > attractShift ? askFee - attractShift : 0; } else { askFee = askFee + staleShift; bidFee = bidFee > attractShift ? bidFee - attractShift : 0; } } { bool tradeAligned = (trade.isBuy && spot >= pHat) || (!trade.isBuy && spot < pHat); if (tradeAligned) { uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio); if (trade.isBuy) { bidFee = bidFee + tradeBoost; } else { askFee = askFee + tradeBoost; } } } if (sellPressure) { bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_PROTECT)); askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_ATTRACT)); } else { askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_PROTECT)); bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_ATTRACT)); } slots[0] = bidFee; slots[1] = askFee; slots[2] = trade.timestamp; slots[3] = dirState; slots[4] = actEma; slots[5] = pHat; slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat; slots[9] = toxEma; slots[10] = stepTradeCount; return (bidFee, askFee); } function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) { if (fee <= TAIL_KNEE) return fee; return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope); } function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) { result = WAD; while (exp > 0) { if (exp & 1 == 1) result = wmul(result, factor); factor = wmul(factor, factor); exp >>= 1; } } function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) { uint256 mul = _powWad(decayFactor, elapsed); if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); } uint256 below = wmul(WAD - centered, mul); return below < WAD ? WAD - below : 0; } function getName() external pure override returns (string memory) { return "stack_a"; } } ``` ## Next Evaluate this small incremental gain for merging; no board result is claimed. The edge split suggests testing a separate retail fee anchor alongside the first-print fair-price state, seeking to recover the lost retail edge. Keep this source frozen and confirm any revision on a different fresh block. The tuning ablations do not establish a benefit from the variance addition alone.