{
"item": "b",
"batch": "e04-size-tail-design",
"kind": "static source analysis + offline design; NO market simulation, NO confirmation-seed access",
"title": "Size-tail (Jensen/clipping) residual on V10: distribution-aware expected execution edge of routed retail with exact clipped router, marginalized competitor spot and charged one-step arb cost",
"paired": {
"n": 0,
"mean_diff": null,
"se": null,
"worst": null
},
"gas": null,
"retail_edge_delta": null,
"arb_edge_delta": null,
"baseline": {
"name": "V10 (censored_10)",
"artifact": "https://api.imdnotdead.fun/artifacts/33f54e81f3372088e9eced52c0174f310ac348af98ef5727b73785c088679194",
"code_sha256_verified": "cd9bdb45ab0537ad25c4fd90ca8618898cef6386de3df65f192eaef65fbca245 (sha256 of the artifact's code field, recomputed locally)"
},
"cited_sources": [
{
"path": "amm_sim_rs/src/market/router.rs",
"lines": "40-74 (buy split; numerator L61, clamp L71), 82-116 (sell split; L103, L113), 188 (MIN_AMOUNT=0.0001), 221 (total_x = order.size / fair_price)",
"url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/market/router.rs"
},
{
"path": "amm_sim_rs/src/market/retail.rs",
"lines": "59 (mu = ln(mean) - 0.5 sigma^2), 87 (lognormal size), 90 (side)",
"url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/market/retail.rs"
},
{
"path": "amm_sim_rs/src/amm/cfmm.rs",
"lines": "119-298 (fee-on-input, gamma=1-fee, execute_buy_x L200, execute_buy_x_with_y L262)",
"url": "https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_sim_rs/src/amm/cfmm.rs"
},
{
"path": "rules artifact (arb_rule, retail_split, retail_size, step_order, edge)",
"url": "https://api.imdnotdead.fun/artifacts/ddbbaac58282566e0505d895c83daf348487cc322e8c482e61d4d87e6e4628de",
"note": "used for arb closed form, step order and edge definition; it cites arbitrageur.rs L35-146 and engine.rs L136-204 which I did not re-open"
},
{
"path": "V10 Strategy.sol (artifact code field)",
"lines": "L94-106 silence band-center, L137-144 interior-route normalizer inference, L146-151 _posterior, L171-211 tox and stale lean (staleHat=0.7 pHat+0.3 fastHat, STALE_DIR_COEF, STALE_ATTRACT_FRAC), L223-237 tail compression and 30/36 bps caps, L252-308 censored posterior (fastHat, uncertainty)"
}
],
"assumptions": [
"A1 Both pools start x=100,y=10000 (rules fixed_baseline), fees go to separate buckets, so k1=k2=1e6 stays constant up to float error: competitor reserves follow from its spot s2 alone, x2=sqrt(k2/s2), y2=sqrt(k2*s2). Our own reserves are observed in TradeInfo.",
"A2 Normalizer fee is fixed at 30 bps (VanillaStrategy per rules). Its spot after its own arb sits on its band edge p*0.997 or p/0.997, or inside the band. Prior on u=ln(s2/p): 3 nodes {-0.003,0,+0.003} with weights {.25,.5,.25}. This is a declared prior that was not fitted.",
"A3 Order size T (Y notional) ~ LogNormal(ln M - z^2/2, z), M~U(19,21) (taken as 20), z=1.2. Sell orders give T/p X at the true fair p (router.rs L221), so the X size of a sell depends on the hidden p.",
"A4 One order is evaluated against the current reserves. Later orders in the same step, and the next step's retail, are ignored in the central calculation (the rules say there is no arb between retail orders).",
"A5 The future arb cost is one step ahead: p'=p*exp(sigma*Z), sigma=0.00095, 7-node Gauss-Hermite, with the exact arb closed form from the rules (incl. 0.99x cap) at the fees being evaluated. It ignores the normalizer-side arb and the reversion that later retail causes. lambda scales it and is a search parameter.",
"A6 p (fair) is hidden on-chain. On-chain it is replaced by V10's posterior (fastHat, uncertainty) or by the stale blend staleHat. Offline it is the declared synthetic p=100."
],
"equations": {
"units": "Y units for edge and T; X units for sell-side router input; fees are fractions (1 bp=1e-4); s=y/x in Y per X.",
"router_affine": "Buy X: Y1(T)=clip(a+bT,0,T), r=sqrt(x1 g1 y1)/sqrt(x2 g2 y2), a=(r y2 - y1)/(g1+r g2), b=r g2/(g1+r g2). Sell X: the same with x<->y, input X=T/p, so in Y notional a_Y=p*a_X. If a<0 (we are worse at the margin) the order is censored: Y1=b(T-c)+ with c=-a/b. If a>=0 we capture every small order: Y1=min(T,a+bT), kink c=a/(1-b). A leg <=1e-4 is skipped (router.rs L188).",
"exact_edge_buy": "e_b(Y)=Y - p*(x1 - k1/(y1+g1 Y)) [Y]",
"exact_edge_sell": "e_s(X)=p X - (y1 - k1/(x1+g1 X)) [Y]",
"second_order": "e_b ~ alpha_b Y + beta_b Y^2, alpha_b=1-g p/s1, beta_b=g^2 p/(s1 y1); e_s ~ alpha_s X + beta_s X^2, alpha_s=p-g s1, beta_s=g^2 s1/x1. The relative cubic term is O(Y/y)~0.2% at mean size.",
"lognormal_partial_moments": "E[T^n 1{T>c}] = exp(n mu + n^2 z^2/2) * Phi((mu + n z^2 - ln c)/z); P(T>c)=Phi((mu-ln c)/z).",
"closed_form_value": "Censored (a<0): E[Y1]=a P(T>c)+b E[T1{T>c}]; E[Y1^2]=a^2 P+2ab E[T1{T>c}]+b^2 E[T^2 1{T>c}]. Captured (a>=0): E[Y1]=E[T]-E[T1{T>c}]+aP+bE[T1{T>c}], with the matching second moment. V(f)=alpha E[Y1]+beta E[Y1^2].",
"objective_per_side": "J_side(f)=E_u E_T[ e(Y1(T;f,u)) - lambda*(F(s1'(Y1),p) - F(s1,p)) ], F(s,p)=E_Z[arbProfit(s, p e^{sigma Z}; fb, fa)].",
"jensen_factor": "E[T^2]/M^2 = exp(z^2) = 4.2207 at z=1.2. Wherever the slippage term matters, mean-size evaluation understates the quadratic edge about 4.2x before clipping; the clip moves it further.",
"residual_policy": "f_side = f_V10_side + clamp(kappa*(fstar_dist(state) - fstar_ref(state)), -D, +D). fstar_dist=argmax_f J_side, fstar_ref=argmax of the same objective evaluated at T=M (mean size). This keeps V10's level, stale lean and caps, and adds only the shape correction from the size distribution and route censoring."
},
"observable_state_and_update_order": [
"1 afterSwap(trade): run V10 unchanged through its fee computation (V10 L56-237), which gives bidV10, askV10, pHat, fastHat, uncertainty and staleHat.",
"2 Observed: reserveX, reserveY -> s1, k1=x1*y1; timestamp; isBuy; amountX/Y; prior fees slots[0..1].",
"3 pEst = staleHat (V10 L199), then sd_u = sqrt(0.003^2/3 + uncertainty).",
"4 For each side and each u-node: s2=pEst*e^u, then x2,y2 from k2=1e6 (A1), then a,b,c from the affine router at f=f_V10, then a table lookup of the residual (below).",
"5 Weighted sum over the u-nodes gives Delta_side; apply the clamp \u00b1D; then reapply V10's tail compression and 30/36 bps caps (L223-237) so V10's limits stay binding.",
"6 Store nothing new (stateless residual), or optionally one slot for s2Hat (alternative S4)."
],
"hidden_variables_offline_only": [
"true fair price p",
"normalizer reserves (x2,y2) / u=ln(s2/p)",
"actual order size T and side before routing",
"next-step price p'"
],
"structural_alternatives": [
{
"id": "S0",
"name": "V10 identity",
"rule": "kappa=0, Delta=0",
"role": "control; must reproduce V10 edges bit-exactly"
},
{
"id": "S1",
"name": "Jensen residual (central)",
"rule": "Delta=kappa*(fstar_dist - fstar_meansize), with the closed-form second-order J and a lambda arb charge; tabulated",
"distinct_from_share_optimizer": "it never targets fee*share. The objective is concave edge integrated over a clipped heavy-tailed size law. The table shows argmax_exact differs from argmax_feeXshare in 18/18 marginalized states (e.g. low_edge sell lam0: 2 vs 24 bps; low_edge buy lam1: 72 vs 50)."
},
{
"id": "S2",
"name": "Censoring-threshold targeting",
"rule": "on the censored side, choose f so that the standardized kink zc=(ln c - mu)/z equals a target q (search q in [-0.5,2.0]); on the captured side leave V10",
"mechanism": "large orders still reach a censored pool (P(T>c)); it prices the tail instead of the mean"
},
{
"id": "S3",
"name": "Tail-convexity premium",
"rule": "Delta_side = eta*beta*(E[Y1^2]-E[Y1]^2... evaluated at f_V10)/E[Y1], eta in [0,1]; one closed-form eval, no argmax",
"mechanism": "a first-order linearization of S1; cheapest gas"
},
{
"id": "S4",
"name": "S1 with an observed competitor spot",
"rule": "replace the u-prior by s2Hat from V10's interior-route inference (L139-144, normalSpot) when the previous print was a non-first retail print",
"mechanism": "tests whether marginalization or inference matters"
},
{
"id": "S5_reject_control",
"name": "full replacement f=fstar_dist",
"rule": "drops V10 levels",
"expected": "likely to repeat the failed from-scratch / greedy-share results; only an ablation"
}
],
"static_results_label": "STATIC CALCULATIONS on synthetic declared latent states (p=100, k=1e6 both pools, s1 in {p*0.997, p, p/0.997}, u nodes as in A2). These are NOT simulation gains and NOT board estimates.",
"identity_sign_limit_checks": {
"share_equal_reserves_equal_30bps_T20": 0.5000000000000128,
"expected": 0.5,
"share_10bps_vs_30bps_T20": 0.7507526959628944,
"expected_from_rules_table": 0.750752695963,
"share_monotone_nonincreasing_in_own_fee_0_100bps": true,
"point_mass_size_limit_exact_minus_meansize": 0.0,
"expected_limit": 0.0,
"arb_zero_inside_band": true,
"mirror_symmetry_low_edge_buy_eq_high_edge_sell": true,
"mid_state_buy_eq_sell": true,
"closed_form_vs_exact_max_abs_err_Y": 0.00106,
"closed_form_vs_exact_max_rel_err": 0.0403,
"bug_found_and_fixed": "the first closed-form version used 1-Phi for P(T>c); it disagreed with the exact integral on censored rows and was fixed before these numbers were produced"
},
"jensen_clipping_quantification": {
"cols": [
"state",
"u_norm_logspot",
"side",
"fee_bps",
"E_T_exact",
"meansize",
"fee_x_share_at_M",
"closed_form_2nd"
],
"rows_subset_fee_5_30_60": [
[
"edge_after_buyX_arb",
-0.003,
"buy",
5,
0.01986,
-0.01428,
0.00813,
0.02066
],
[
"edge_after_buyX_arb",
-0.003,
"buy",
30,
0.04146,
0.01,
0.03003,
0.04217
],
[
"edge_after_buyX_arb",
-0.003,
"buy",
60,
0.04643,
0.00801,
0.01477,
0.04705
],
[
"edge_after_buyX_arb",
-0.003,
"sell",
5,
0.10476,
0.08316,
0.00813,
0.10555
],
[
"edge_after_buyX_arb",
-0.003,
"sell",
30,
0.10089,
0.06971,
0.02997,
0.10159
],
[
"edge_after_buyX_arb",
-0.003,
"sell",
60,
0.07756,
0.02228,
0.0145,
0.07817
],
[
"edge_after_buyX_arb",
0.0,
"buy",
5,
0.02945,
-0.01009,
0.01,
0.03037
],
[
"edge_after_buyX_arb",
0.0,
"buy",
30,
0.058,
0.03062,
0.05258,
0.05881
],
[
"edge_after_buyX_arb",
0.0,
"buy",
60,
0.07125,
0.03993,
0.05991,
0.07195
],
[
"edge_after_buyX_arb",
0.0,
"sell",
5,
0.0697,
0.03817,
0.00437,
0.07038
],
[
"edge_after_buyX_arb",
0.0,
"sell",
30,
0.06204,
0.01534,
0.00738,
0.06266
],
[
"edge_after_buyX_arb",
0.0,
"sell",
60,
0.05457,
0.0,
0.0,
0.05512
],
[
"edge_after_buyX_arb",
0.003,
"buy",
5,
0.03872,
-0.01009,
0.01,
0.03978
],
[
"edge_after_buyX_arb",
0.003,
"buy",
30,
0.07237,
0.03986,
0.06,
0.07331
],
[
"edge_after_buyX_arb",
0.003,
"buy",
60,
0.10195,
0.08312,
0.10511,
0.10276
],
[
"edge_after_buyX_arb",
0.003,
"sell",
5,
0.0463,
0.00445,
0.00061,
0.04691
],
[
"edge_after_buyX_arb",
0.003,
"sell",
30,
0.04457,
0.0,
0.0,
0.04513
],
[
"edge_after_buyX_arb",
0.003,
"sell",
60,
0.04148,
0.0,
0.0,
0.04198
],
[
"interior",
-0.003,
"buy",
5,
0.0435,
0.01202,
0.00438,
0.04419
],
[
"interior",
-0.003,
"buy",
30,
0.04642,
0.00806,
0.00745,
0.04704
],
[
"interior",
-0.003,
"buy",
60,
0.04458,
0.0,
0.0,
0.04514
],
[
"interior",
-0.003,
"sell",
5,
0.07833,
0.04988,
0.01,
0.07925
],
[
"interior",
-0.003,
"sell",
30,
0.10187,
0.08307,
0.05258,
0.10268
],
[
"interior",
-0.003,
"sell",
60,
0.10084,
0.06966,
0.05984,
0.10154
],
[
"interior",
0.0,
"buy",
5,
0.06238,
0.03452,
0.00813,
0.06317
],
[
"interior",
0.0,
"buy",
30,
0.07125,
0.03993,
0.03,
0.07195
],
[
"interior",
0.0,
"buy",
60,
0.06204,
0.01523,
0.01464,
0.06266
],
[
"interior",
0.0,
"sell",
5,
0.06238,
0.03452,
0.00813,
0.06317
],
[
"interior",
0.0,
"sell",
30,
0.07125,
0.03993,
0.03,
0.07195
],
[
"interior",
0.0,
"sell",
60,
0.06204,
0.01523,
0.01464,
0.06266
],
[
"interior",
0.003,
"buy",
5,
0.07833,
0.04988,
0.01,
0.07925
],
[
"interior",
0.003,
"buy",
30,
0.10187,
0.08307,
0.05258,
0.10268
],
[
"interior",
0.003,
"buy",
60,
0.10084,
0.06966,
0.05984,
0.10154
],
[
"interior",
0.003,
"sell",
5,
0.0435,
0.01202,
0.00438,
0.04419
],
[
"interior",
0.003,
"sell",
30,
0.04642,
0.00806,
0.00745,
0.04704
],
[
"interior",
0.003,
"sell",
60,
0.04458,
0.0,
0.0,
0.04514
]
],
"summary": "Over 60 synthetic rows, E_T[edge] - edge(M) ranges from +0.0188 to +0.0581 Y per order, always positive. On the censored side the mean-size value is 0 at 45-60 bps when u=+0.003, but the exact value is 0.041-0.058 Y, because orders above the kink c still route to us. fee*share-at-M misses the slippage term entirely, e.g. low_edge sell u=-0.003 at 5 bps: 0.0081 vs exact 0.1048.",
"argmax_fee_bps_marginalized_u": {
"low_edge|buy|lam0.0": {
"exact": 62,
"meansize": 54,
"feeXshare": 50,
"exact_at_feeXshare_opt_loss": 0.00214
},
"low_edge|buy|lam0.5": {
"exact": 68,
"meansize": 54,
"feeXshare": 50,
"exact_at_feeXshare_opt_loss": 0.00346
},
"low_edge|buy|lam1.0": {
"exact": 72,
"meansize": 54,
"feeXshare": 50,
"exact_at_feeXshare_opt_loss": 0.00523
},
"low_edge|sell|lam0.0": {
"exact": 2,
"meansize": 0,
"feeXshare": 24,
"exact_at_feeXshare_opt_loss": 0.00312
},
"low_edge|sell|lam0.5": {
"exact": 30,
"meansize": 18,
"feeXshare": 24,
"exact_at_feeXshare_opt_loss": 0.00064
},
"low_edge|sell|lam1.0": {
"exact": 62,
"meansize": 24,
"feeXshare": 24,
"exact_at_feeXshare_opt_loss": 0.00807
},
"mid|buy|lam0.0": {
"exact": 32,
"meansize": 24,
"feeXshare": 34,
"exact_at_feeXshare_opt_loss": 4e-05
},
"mid|buy|lam0.5": {
"exact": 46,
"meansize": 26,
"feeXshare": 34,
"exact_at_feeXshare_opt_loss": 0.00116
},
"mid|buy|lam1.0": {
"exact": 52,
"meansize": 28,
"feeXshare": 34,
"exact_at_feeXshare_opt_loss": 0.00398
},
"mid|sell|lam0.0": {
"exact": 32,
"meansize": 24,
"feeXshare": 34,
"exact_at_feeXshare_opt_loss": 4e-05
},
"mid|sell|lam0.5": {
"exact": 46,
"meansize": 26,
"feeXshare": 34,
"exact_at_feeXshare_opt_loss": 0.00116
},
"mid|sell|lam1.0": {
"exact": 52,
"meansize": 28,
"feeXshare": 34,
"exact_at_feeXshare_opt_loss": 0.00398
},
"high_edge|buy|lam0.0": {
"exact": 2,
"meansize": 0,
"feeXshare": 24,
"exact_at_feeXshare_opt_loss": 0.00312
},
"high_edge|buy|lam0.5": {
"exact": 30,
"meansize": 18,
"feeXshare": 24,
"exact_at_feeXshare_opt_loss": 0.00064
},
"high_edge|buy|lam1.0": {
"exact": 62,
"meansize": 24,
"feeXshare": 24,
"exact_at_feeXshare_opt_loss": 0.00807
},
"high_edge|sell|lam0.0": {
"exact": 62,
"meansize": 54,
"feeXshare": 50,
"exact_at_feeXshare_opt_loss": 0.00214
},
"high_edge|sell|lam0.5": {
"exact": 68,
"meansize": 54,
"feeXshare": 50,
"exact_at_feeXshare_opt_loss": 0.00346
},
"high_edge|sell|lam1.0": {
"exact": 72,
"meansize": 54,
"feeXshare": 50,
"exact_at_feeXshare_opt_loss": 0.00523
}
},
"interpretation": "Distribution-awareness changes the optimal bid/ask SHAPE: (i) on the captured, cheap side the optimum rises (62-72 vs 50 bps share-opt) because tail orders pay convex slippage; (ii) on the censored side with lam=0 the optimum falls to ~0-2 bps (keep tail flow), but with lam=1 it rises to 62 bps because the arb charge dominates. The sign therefore depends on lambda, which only measurement can fix. The per-order loss of using the share-optimal fee under the exact objective is 0.00004-0.0081 Y (static, single order)."
},
"risks_and_honest_caveats": [
"The quadratic edge from large orders is partly a mark-to-fair gain that the next arb takes back. lambda (A5) is a one-step proxy, and the lam=1 censored-side optimum (62 bps) resembles the extra exposed-side protection that already failed.",
"The edge-state optima exceed V10's 30/36 bps caps (V10 L231-237). If kept, those caps clip most of S1's correction; it was not measured whether the caps or the residual should win.",
"The u-prior weights were not fitted. The sell-side X size uses the hidden p.",
"Per-order deltas are not additive across a step, because reserves update sequentially."
],
"solidity_pseudocode": "// slots: none new for S1/S3 (stateless). S4: slots[15]=s2Hat (WAD), slots[16]=s2Ts.\n// constants: TAB_CENS[16], TAB_CAPT[16] int16 bps residuals packed into 2 uint256; ZC grid -2..+2.5 step .3\nfunction _residual(uint256 x1,uint256 y1,uint256 f,uint256 pEst,bool sellSide) internal pure returns(int256 d){\n  int256 acc; for(uint i=0;i<3;i++){ // u nodes -h,0,+h, weights 1,2,1 /4\n    uint256 s2 = i==0? wmul(pEst,UDN): i==1? pEst : wmul(pEst,UUP);\n    uint256 x2=_isqrt(K2*WAD/s2*WAD)/1; uint256 y2=wdiv(K2,x2); // k2=1e6 WAD^2 scaled\n    (uint256 inA,uint256 inO,uint256 nA,uint256 nO)= sellSide?(x1,y1,x2,y2):(y1,x1,y2,x2);\n    uint256 g1=WAD-f; uint256 r=wdiv(_isqrt(wmul(wmul(inA,g1),inO)*WAD),_isqrt(wmul(wmul(nA,G2),nO)*WAD));\n    int256 a=(int(wmul(r,nA))-int(inA))*int(WAD)/int(g1+wmul(r,G2)); uint256 b=wdiv(wmul(r,G2),g1+wmul(r,G2));\n    // kink in Y-notional, standardized: zc=(ln c - MU)/SIG ; ln via 6-term atanh series on [0.5,2] after bit-shift\n    int256 zc=_stdKink(a,b,sellSide?pEst:WAD); int256 v=_lerp(a<0?TAB_CENS:TAB_CAPT, zc);\n    acc+= (i==1?2:1)*v; }\n  d = acc/4 * int(KAPPA)/int(WAD); if(d>int(DMAX))d=int(DMAX); if(d<-int(DMAX))d=-int(DMAX);\n}\n// in afterSwap after V10 L229 and before caps L231: bidFee=_add(bidFee,_residual(..,bidFee,staleHat,true)); askFee=_add(askFee,_residual(..,askFee,staleHat,false));",
"gas_design_unmeasured": "UNMEASURED estimate: per side 3 u-nodes x (3 isqrt ~ 3x600, 1 ln ~1200, lerp ~300, arithmetic ~800) ~ 12k; both sides ~25k. Option: 1 u-node (S1-lite) ~8k. STACK peaks at 214k of 250k (brief); V10's own peak was not available to me. A worker must run gas_stats() and use S1-lite or S3 if the margin is under 30k.",
"table_construction": "Offline: for zc grid (16 pts) x regime (censored/captured) x lambda, compute fstar_dist - fstar_meansize with proto.side_value at the V10 typical fee band; residual table in bps (int16). The table is built from synthetic states only, so it is a design artifact.",
"worker_plan_55min": [
"0-8 min: setup per brief; compile V10, assert sha, run S0 on 200 tuning seeds and assert a bit-identical edge vs V10.",
"8-18 min: build the residual tables with proto.py for lambda in {0,0.5,1}; implement S1-lite (1 u-node), S1, S3; check gas_stats oog==0 on 50 seeds.",
"18-40 min: paired screen vs V10 on tuning seeds 10000-10511 (512): grid kappa {0.25,0.5,1}, lambda {0,0.5,1}, DMAX {4,8,16} bps, with caps kept vs caps relaxed to 45 bps (2 arms). Record retail and arb edge deltas separately.",
"40-50 min: best 2 arms plus S2 (q in {0,0.75,1.5}) on 1024 fresh tuning seeds 20000-21023; keep only if mean_diff>2*se.",
"50-55 min: one confirmation vs STACK on >=2000 fresh seeds from 5,000,000+ (never reused); report paired{n,mean_diff,se,worst}, gas{max_ok,oog}."
],
"required_inputs": [
"V10 code (artifact above)",
"STACK code for confirmation (brief artifact c3387208...)",
"amm_sim_rs wheel per brief",
"proto.py (below)"
],
"objective_and_ranges": {
"objective": "paired mean edge diff vs V10 (screen) then vs STACK (confirm)",
"kappa": "[0,1.5]",
"lambda": "[0,1.5]",
"DMAX_bps": "[2,20]",
"u_half_width": "[0.0015,0.0045]",
"cap_relax_bps": "{30/36 V10, 45, none}",
"S2_q": "[-0.5,2.0]",
"S3_eta": "[0,1]"
},
"falsification_and_ablation": [
"F1 If S1 with kappa in {0.5,1} at its best lambda gives mean_diff <= +0.05 (2-SE upper bound <0.2) on 512 seeds, the size-tail mechanism is dead at V10's level.",
"F2 Ablate the clip: replace the exact clipped Y1 with share(M)*T (no clip, no Jensen). If S1's gain survives, the gain is not from size-tail/censoring; reject the causal claim.",
"F3 Ablate lambda=0 vs 1: if only lambda=1 gains and the gain is all arb with retail down (like the shadow overlay: arb +.132, retail -.156), it is exposed-side protection in disguise; reject.",
"F4 Replace the u-prior with u=0 (point): if unchanged, the marginalization is irrelevant; simplify.",
"F5 Shuffle the table rows (random residual of the same magnitude): must lose; otherwise the gain is noise or a level shift.",
"F6 Collapse test: if the fitted kappa*table correlates >0.9 with (fstar_feeXshare - fV10), it collapsed to the share optimizer; reject."
],
"prototype_python_source": "\"\"\"Offline static prototype: distribution-aware execution edge of routed retail.\nExact router split (router.rs L40-117), exact CFMM fee-on-input, lognormal sizes\n(retail.rs L59: mu=ln M - s^2/2). Synthetic declared latent states only; no simulation.\"\"\"\nimport math\nM, SIG, K, F2 = 20.0, 1.2, 1e6, 0.003\nMU = math.log(M) - SIG * SIG / 2\nNZ = 1201; ZS = [-8 + 16 * i / (NZ - 1) for i in range(NZ)]\nWZ = [math.exp(-z * z / 2) / math.sqrt(2 * math.pi) * 16 / (NZ - 1) for z in ZS]\nTS = [math.exp(MU + SIG * z) for z in ZS]\nPhi = lambda z: 0.5 * (1 + math.erf(z / math.sqrt(2)))\ndef res(s, k=K): return math.sqrt(k / s), math.sqrt(k * s)  # x,y from spot, k\ndef split(i1, i2, g1, g2, T):\n    \"\"\"generic: input reserves (i), other reserves (o); returns pool-1 input (router.rs L53-71)\"\"\"\n    (a1, o1), (a2, o2) = i1, i2\n    A1, A2 = math.sqrt(a1 * g1 * o1), math.sqrt(a2 * g2 * o2)\n    r = A1 / A2; d = g1 + r * g2\n    return max(0.0, min(T, (r * (a2 + g2 * T) - a1) / d))\ndef affine(i1, i2, g1, g2):\n    (a1, o1), (a2, o2) = i1, i2\n    r = math.sqrt(a1 * g1 * o1) / math.sqrt(a2 * g2 * o2); d = g1 + r * g2\n    return (r * a2 - a1) / d, r * g2 / d  # Y1 = clip(a + b T)\ndef edge_buyX(s1, f, Y, p):  # retail buys X with gross Y from pool 1 (AMM sells X)\n    x, y = res(s1); g = 1 - f; xo = x - K / (y + g * Y); return Y - p * xo, K / (x - xo) ** 2 * 0 + (y + g * Y) / (x - xo)\ndef edge_sellX(s1, f, X, p):  # retail sells X gross X to pool 1 (AMM buys X)\n    x, y = res(s1); g = 1 - f; yo = y - K / (x + g * X); return p * X - yo, (y - yo) / (x + g * X)\ndef arb_profit(s, p, fb, fa):  # rules arb_rule, k fixed\n    x, y = res(s); ga, gb = 1 - fa, 1 - fb\n    if s < ga * p:\n        q = min(x - math.sqrt(K / (ga * p)), 0.99 * x); return max(0.0, p * q - (K / (x - q) - y) / ga)\n    if s > p / gb:\n        q = (math.sqrt(K * gb / p) - x) / gb; return max(0.0, (y - K / (x + gb * q)) - p * q)\n    return 0.0\nGH = [(-2.651961356835233, 0.000971781245099519), (-1.673551628767471, 0.05451558281912703),\n      (-0.816287882858965, 0.4256072526101278), (0.0, 0.8102646175568073), (0.816287882858965, 0.4256072526101278),\n      (1.673551628767471, 0.05451558281912703), (2.651961356835233, 0.000971781245099519)]\ndef F(s, p, fb, fa, sd):  # expected next-step arb loss, p' = p*exp(sd*sqrt2*t)\n    return sum(w * arb_profit(s, p * math.exp(sd * math.sqrt(2) * t), fb, fa) for t, w in GH) / math.sqrt(math.pi)\ndef side_value(side, s1, s2, f, p, fb, fa, lam=0.0, sd=0.00095, sizes=None):\n    \"\"\"exact E_T of edge - lam*(F(s')-F(s)) for one order of given side; returns (exact, meansize, feeXshare)\"\"\"\n    x1, y1 = res(s1); x2, y2 = res(s2); g1, g2 = 1 - f, 1 - F2\n    fb2, fa2 = (f, fa) if side == 'sell' else (fb, f)\n    F0 = F(s1, p, fb2, fa2, sd) if lam else 0.0\n    def val(T):\n        if side == 'buy':\n            Y = split((y1, x1), (y2, x2), g1, g2, T)\n            if Y <= 1e-4: return 0.0, 0.0\n            e, sp = edge_buyX(s1, f, Y, p); return e - (lam * (F(sp, p, fb2, fa2, sd) - F0) if lam else 0), f * Y\n        X = split((x1, y1), (x2, y2), g1, g2, T / p)\n        if X <= 1e-4: return 0.0, 0.0\n        e, sp = edge_sellX(s1, f, X, p); return e - (lam * (F(sp, p, fb2, fa2, sd) - F0) if lam else 0), f * X * p\n    ts = sizes or TS; ws = [1.0 / len(sizes)] * len(sizes) if sizes else WZ\n    ex = sum(w * val(T)[0] for T, w in zip(ts, ws)); vm, fs = val(M)\n    return ex, vm, fs\ndef closed_form(side, s1, s2, f, p):\n    \"\"\"second-order edge alpha*Y+beta*Y^2 with clipped-affine Y and lognormal partial moments\"\"\"\n    x1, y1 = res(s1); x2, y2 = res(s2); g = 1 - f\n    if side == 'buy': a, b = affine((y1, x1), (y2, x2), g, 1 - F2); al, be, sc = 1 - g * p / s1, g * g * p / (s1 * y1), 1.0\n    else: a, b = affine((x1, y1), (x2, y2), g, 1 - F2); al, be, sc = p - g * s1, g * g * s1 / x1, 1 / p\n    a = a / sc  # express kinks in Y-notional T\n    def pm(n, c):  # E[T^n 1{T>c}]\n        if c <= 0: return math.exp(n * MU + n * n * SIG * SIG / 2)\n        return math.exp(n * MU + n * n * SIG * SIG / 2) * Phi((MU + n * SIG * SIG - math.log(c)) / SIG)\n    E = lambda n: math.exp(n * MU + n * n * SIG * SIG / 2)\n    # Y(T)/sc: a<0 -> (a+bT)+ ; a>=0 -> min(T, a+bT) (kink at a/(1-b))\n    if a < 0:\n        c = -a / b; m1 = a * (Phi((MU - math.log(c)) / SIG)) + b * pm(1, c)\n        m2 = a * a * (Phi((MU - math.log(c)) / SIG)) + 2 * a * b * pm(1, c) + b * b * pm(2, c)\n    else:\n        c = a / (1 - b) if b < 1 else float('inf'); P = 1.0 if c <= 0 else (Phi((MU - math.log(c)) / SIG) if c < float('inf') else 0)\n        m1 = E(1) - pm(1, c) + a * P + b * pm(1, c)\n        m2 = E(2) - pm(2, c) + a * a * P + 2 * a * b * pm(1, c) + b * b * pm(2, c)\n    m1, m2 = m1 * sc, m2 * sc * sc\n    return al * m1 + be * m2\nif __name__ == '__main__':\n    import json; out = {}; p = 100.0\n    # identity: equal reserves, equal 30bps fee -> share 0.5 at T=20\n    x, y = res(100.0); out['id_share_equal'] = split((y, x), (y, x), 0.997, 0.997, 20.0) / 20\n    out['id_share_10bps'] = split((y, x), (y, x), 0.999, 0.997, 20.0) / 20\n    # sign: share decreasing in own fee\n    out['sign_share_monotone'] = all(split((y, x), (y, x), 1 - f / 1e4, 0.997, 20) >= split((y, x), (y, x), 1 - (f + 1) / 1e4, 0.997, 20) for f in range(0, 99))\n    # limit sigma->0: exact equals mean-size; emulate with sizes=[M]\n    e, vm, _ = side_value('buy', 100 * 0.9985, 100.0, 0.002, p, 0.002, 0.002, sizes=[M]); out['limit_point_mass_diff'] = e - vm\n    out['arb_band_zero'] = arb_profit(100 * 0.998, 100.0, 0.002, 0.002) < 1e-9\n    rows = []\n    for name, fa_used in [('edge_after_buyX_arb', 0.003), ('interior', None)]:\n        for u in (-0.003, 0.0, 0.003):\n            for side in ('buy', 'sell'):\n                for fbps in (5, 15, 30, 45, 60):\n                    f = fbps / 1e4; s1 = p * (1 - fa_used) if fa_used else p; s2 = p * math.exp(u)\n                    ex, vm, fs = side_value(side, s1, s2, f, p, f, f); cf = closed_form(side, s1, s2, f, p)\n                    rows.append([name, u, side, fbps, round(ex, 5), round(vm, 5), round(fs, 5), round(cf, 5)])\n    out['rows_cols'] = ['state', 'u_norm_logspot', 'side', 'fee_bps', 'E_T_exact', 'meansize', 'fee_x_share_at_M', 'closed_form_2nd']\n    out['rows'] = rows\n    # argmax fee per objective, edge state, sell (censored) side and buy (captured) side, lam 0 / 1\n    am = {}\n    for side in ('buy', 'sell'):\n        for lam in (0.0, 1.0):\n            for u in (-0.003, 0.0, 0.003):\n                s1 = p * 0.997; s2 = p * math.exp(u); best = {}\n                grid = [i / 1e4 for i in range(0, 101, 1)]\n                vals = [(side_value(side, s1, s2, f, p, f, f, lam=lam) if lam else side_value(side, s1, s2, f, p, f, f)) for f in grid]\n                for j, nm in enumerate(['exact', 'meansize', 'feeXshare']):\n                    k = max(range(len(grid)), key=lambda i: vals[i][j]); best[nm] = [round(grid[k] * 1e4), round(vals[k][j], 5)]\n                am[f'{side}|lam{lam}|u{u}'] = best\n    out['argmax_bps_value'] = am\n    print(json.dumps(out))\n",
"marginalization_script_source": "import sys, json, math\nsys.path.insert(0, sys.argv[1]); import proto as P\nP.NZ = 401; P.ZS = [-7 + 14*i/400 for i in range(401)]\nP.WZ = [math.exp(-z*z/2)/math.sqrt(2*math.pi)*14/400 for z in P.ZS]; P.TS = [math.exp(P.MU+P.SIG*z) for z in P.ZS]\np = 100.0; U = [(-0.003, .25), (0.0, .5), (0.003, .25)]; out = {}\nfor sn, s1 in [('low_edge', p*0.997), ('mid', p), ('high_edge', p/0.997)]:\n  for side in ('buy', 'sell'):\n    for lam in (0.0, 0.5, 1.0):\n      grid = [i/1e4 for i in range(0, 101, 2)]; V = []\n      for f in grid:\n        acc = [0, 0, 0]\n        for u, w in U:\n          v = P.side_value(side, s1, p*math.exp(u), f, p, f, f, lam=lam)\n          acc = [a + w*b for a, b in zip(acc, v)]\n        V.append(acc)\n      best = {}\n      for j, nm in enumerate(['exact', 'meansize', 'feeXshare']):\n        k = max(range(len(grid)), key=lambda i: V[i][j]); best[nm] = round(grid[k]*1e4)\n      best['exact_at_feeXshare_opt_loss'] = round(max(v[0] for v in V) - V[grid.index(best['feeXshare']/1e4)][0], 5)\n      out[f'{sn}|{side}|lam{lam}'] = best\nprint(json.dumps(out))\n",
"notes": "Commands that were run: python3 -I proto.py and python3 -I marg.py (pure python, stdlib only). No simulator was installed or run. Arb-rule details come from the rules artifact; I did not re-read arbitrageur.rs/engine.rs myself."
}