{
 "item": "np-lowfee",
 "batch": "r4-d1",
 "approach": "Deepen NP's low-fee handling of the 30 bps pool's implied-spot blend (nS). NP blends pHat toward nS with weight 0.1 only on non-first trades in a step when feeUsed > 30 bps. Tested (a) blending for every feeUsed with gamma>0, and (c) a weight that rises linearly from 0 at feeUsed=30 bps to 0.1 at feeUsed>=50 bps. Both lose to NP on the tuning seeds; (a) loses heavily (-2.17). That fits the brief's reasoning: when we were cheaper, the normalizer probably did not trade, so nS is not evidence of the fair price. (c) was frozen as the item's single candidate, being the least-bad variant, and measured once on fresh seeds, where it also loses (mean_diff -0.0373, se 0.0091; see paired). Recommendation: do NOT promote; keep NP. The evidence points away from damping the weight near 30 bps; if anything, a weight above 0.1 on feeUsed>30 bps may be worth testing.",
 "tried": [
  {
   "variant": "(a) blend for every feeUsed with gamma>0 (dropped feeUsed>30*BPS)",
   "seeds": "30000..30999",
   "n": 1000,
   "mean_diff": -2.173553660372121,
   "se": 0.0338568602756237
  },
  {
   "variant": "(c) weight bw = 0.1*min(feeUsed-30bps,20bps)/20bps, applied only when feeUsed>30bps",
   "seeds": "30000..30999",
   "n": 1000,
   "mean_diff": -0.03310192918347184,
   "se": 0.008324260603949186
  },
  {
   "variant": "(b) separate small weight when feeUsed<=30bps",
   "seeds": "not run",
   "n": 0,
   "mean_diff": null,
   "se": null,
   "note": "not run for lack of time; (a) at weight 0.1 for feeUsed<=30bps lost 2.17, which suggests any positive low-fee weight hurts"
  }
 ],
 "deviations_from_brief": [
  "Tuning cells used 1000 seeds (30000..30999), not >=2000: at about 0.43 s/sim on 4 cores there was not enough time. The paired SEs (0.034 and 0.008) still separate both variants from 0.",
  "Fresh measurement covered n=1000 (800000..800999) of the requested 2000, in chunks of 500.",
  "NP was not re-run on seeds 0..999, so the NP reference 526.0337302497852 is not reproduced here. The harness was validated by reproducing CH on 0..999 exactly: 525.5258821780834 vs the brief's 525.525882178.",
  "The candidate was not scored on 0..999 unless score.n > 0 below."
 ],
 "reproduction": {
  "CH_seeds_0_999_mean_edge": 525.5258821780834,
  "CH_expected": 525.525882178,
  "CH_sha256": "6dd68f77a70900319460215374143dbd5d924198450a46d9f5f91f6b1e3d35c3",
  "NP_sha256": "95d21646a5b6d293c86c36a184769477be7b1ff39ab2adde7668ed79fb22dd38",
  "NP_seeds_0_999": "not run (time)"
 },
 "next": "Keep NP as best. Do not pursue lower nS weights near or below 30 bps. Next delta: raise the weight for feeUsed>30bps (e.g. 0.15, 0.2), or ramp it up with feeUsed-30bps, paired on 30000.. with >=2000 seeds.",
 "code": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport {AMMStrategyBase} from \"./AMMStrategyBase.sol\";\nimport {TradeInfo} from \"./IAMMStrategy.sol\";\n\ncontract Strategy is AMMStrategyBase {\n    uint256 constant ELAPSED_CAP = 8;\n    uint256 constant SIGNAL_THRESHOLD = WAD / 650;\n    uint256 constant DIR_DECAY = 750000000000000000;\n    uint256 constant ACT_DECAY = 530000000000000000;\n    uint256 constant SIZE_DECAY = 530000000000000000;\n    uint256 constant TOX_DECAY = 910000000000000000;\n    uint256 constant SIGMA_DECAY = 690000000000000000;\n    uint256 constant LAMBDA_DECAY = 990000000000000000;\n    uint256 constant SIZE_BLEND_DECAY = 828000000000000000;\n    uint256 constant TOX_BLEND_DECAY = 51000000000000000;\n    uint256 constant ACT_BLEND_DECAY = 985000000000000000;\n    uint256 constant PHAT_ALPHA = 270000000000000000;\n    uint256 constant PHAT_ALPHA_RETAIL = 60000000000000000;\n    uint256 constant DIR_IMPACT_MULT = 4;\n    uint256 constant GATE_SIGMA_MULT = 7200000000000000000;\n    uint256 constant MIN_GATE = 30000000000000000;\n    uint256 constant TRADE_TOX_BOOST = 3500 * BPS;\n    uint256 constant STALE_ATTRACT_FRAC = 1050000000000000000;\n    uint256 constant RET_CAP = WAD / 35;\n    uint256 constant TOX_CAP = WAD / 5;\n    uint256 constant TRADE_RATIO_CAP = WAD / 5;\n    uint256 constant LAMBDA_CAP = 1820000000000000000;\n    uint256 constant STEP_COUNT_CAP = 64;\n    uint256 constant BASE_FEE = 4 * BPS;\n    uint256 constant SIGMA_COEF = 200000000000000000;\n    uint256 constant LAMBDA_COEF = 12 * BPS;\n    uint256 constant FLOW_SIZE_COEF = 4842 * BPS;\n    uint256 constant TOX_COEF = 300 * BPS;\n    uint256 constant TOX_QUAD_COEF = 15000 * BPS;\n    uint256 constant ACT_COEF = 91843 * BPS;\n    uint256 constant DIR_COEF = 20 * BPS;\n    uint256 constant DIR_TOX_COEF = 100 * BPS;\n    uint256 constant STALE_DIR_COEF = 6850 * BPS;\n    uint256 constant SIGMA_TOX_COEF = 500 * BPS;\n    uint256 constant TAIL_KNEE = 600 * BPS;\n    uint256 constant TAIL_SLOPE_PROTECT = 930000000000000000;\n    uint256 constant TAIL_SLOPE_ATTRACT = 850000000000000000;\n\n    function afterInitialize(uint256 initialX, uint256 initialY) external override returns (uint256, uint256) {\n        slots[0] = BASE_FEE; slots[1] = BASE_FEE; slots[2] = 0; slots[3] = WAD; slots[4] = 0;\n        slots[5] = initialX > 0 ? wdiv(initialY, initialX) : 100 * WAD;\n        slots[6] = 950000000000000; slots[7] = 800000000000000000; slots[8] = 2000000000000000;\n        slots[9] = 0; slots[10] = 0;\n        return (BASE_FEE, BASE_FEE);\n    }\n\n    function afterSwap(TradeInfo calldata trade) external override returns (uint256, uint256) {\n        uint256 prevBidFee = slots[0]; uint256 prevAskFee = slots[1]; uint256 lastTs = slots[2];\n        uint256 dirState = slots[3]; uint256 actEma = slots[4]; uint256 pHat = slots[5];\n        uint256 sigmaHat = slots[6]; uint256 lambdaHat = slots[7]; uint256 sizeHat = slots[8];\n        uint256 toxEma = slots[9]; uint256 stepTradeCount = slots[10];\n\n        bool isNewStep = trade.timestamp > lastTs;\n        if (isNewStep) {\n            uint256 elapsedRaw = trade.timestamp - lastTs;\n            uint256 elapsed = elapsedRaw > ELAPSED_CAP ? ELAPSED_CAP : elapsedRaw;\n            dirState = _decayCentered(dirState, DIR_DECAY, elapsed);\n            actEma = wmul(actEma, _powWad(ACT_DECAY, elapsed));\n            sizeHat = wmul(sizeHat, _powWad(SIZE_DECAY, elapsed));\n            toxEma = wmul(toxEma, _powWad(TOX_DECAY, elapsed));\n            if (stepTradeCount > 0 && elapsedRaw > 0) {\n                uint256 lambdaInst = (stepTradeCount * WAD) / elapsedRaw;\n                if (lambdaInst > LAMBDA_CAP) lambdaInst = LAMBDA_CAP;\n                lambdaHat = wmul(lambdaHat, LAMBDA_DECAY) + wmul(lambdaInst, WAD - LAMBDA_DECAY);\n            }\n            stepTradeCount = 0;\n        }\n\n        bool firstInStep = stepTradeCount == 0;\n        uint256 spot = trade.reserveX > 0 ? wdiv(trade.reserveY, trade.reserveX) : pHat;\n        if (pHat == 0) pHat = spot;\n\n        uint256 feeUsed = trade.isBuy ? prevBidFee : prevAskFee;\n        uint256 gamma = feeUsed < WAD ? WAD - feeUsed : 0;\n        uint256 pImplied;\n        if (gamma == 0) { pImplied = spot; }\n        else { pImplied = trade.isBuy ? wmul(spot, gamma) : wdiv(spot, gamma); }\n\n        {\n            uint256 ret = pHat > 0 ? wdiv(absDiff(pImplied, pHat), pHat) : 0;\n            uint256 alpha = firstInStep ? PHAT_ALPHA : PHAT_ALPHA_RETAIL;\n            uint256 adaptiveGate = wmul(sigmaHat, GATE_SIGMA_MULT);\n            if (adaptiveGate < MIN_GATE) adaptiveGate = MIN_GATE;\n            if (ret <= adaptiveGate) { pHat = wmul(pHat, WAD - alpha) + wmul(pImplied, alpha); }\n            if (firstInStep) {\n                if (ret > RET_CAP) ret = RET_CAP;\n                sigmaHat = wmul(sigmaHat, SIGMA_DECAY) + wmul(ret, WAD - SIGMA_DECAY);\n            }\n        }\n\n        if(!firstInStep&&feeUsed>30*BPS&&gamma>0){uint256 nS=trade.isBuy?wdiv(wmul(spot,gamma),WAD-30*BPS):wdiv(wmul(spot,WAD-30*BPS),gamma);uint256 bw=feeUsed-30*BPS;if(bw>20*BPS)bw=20*BPS;bw=(WAD/10)*bw/(20*BPS);pHat=wmul(pHat,WAD-bw)+wmul(nS,bw);}\n        uint256 tradeRatio = trade.reserveY > 0 ? wdiv(trade.amountY, trade.reserveY) : 0;\n        if (tradeRatio > TRADE_RATIO_CAP) tradeRatio = TRADE_RATIO_CAP;\n\n        if (tradeRatio > SIGNAL_THRESHOLD) {\n            uint256 push = tradeRatio * DIR_IMPACT_MULT;\n            if (push > WAD / 4) push = WAD / 4;\n            if (trade.isBuy) {\n                dirState = dirState + push;\n                if (dirState > 2 * WAD) dirState = 2 * WAD;\n            } else {\n                dirState = dirState > push ? dirState - push : 0;\n            }\n            actEma = wmul(actEma, ACT_BLEND_DECAY) + wmul(tradeRatio, WAD - ACT_BLEND_DECAY);\n            sizeHat = wmul(sizeHat, SIZE_BLEND_DECAY) + wmul(tradeRatio, WAD - SIZE_BLEND_DECAY);\n            if (sizeHat > WAD) sizeHat = WAD;\n        }\n\n        uint256 tox = pHat > 0 ? wdiv(absDiff(spot, pHat), pHat) : 0;\n        if (tox > TOX_CAP) tox = TOX_CAP;\n        toxEma = wmul(toxEma, TOX_BLEND_DECAY) + wmul(tox, WAD - TOX_BLEND_DECAY);\n        uint256 toxSignal = toxEma;\n\n        stepTradeCount = stepTradeCount + 1;\n        if (stepTradeCount > STEP_COUNT_CAP) stepTradeCount = STEP_COUNT_CAP;\n\n        uint256 flowSize = wmul(lambdaHat, sizeHat);\n        uint256 fBase = BASE_FEE + wmul(SIGMA_COEF, sigmaHat) + wmul(LAMBDA_COEF, lambdaHat) + wmul(FLOW_SIZE_COEF, flowSize);\n        uint256 fMid = fBase + wmul(TOX_COEF, toxSignal) + wmul(TOX_QUAD_COEF, wmul(toxSignal, toxSignal)) + wmul(ACT_COEF, actEma);\n        fMid = fMid + wmul(SIGMA_TOX_COEF, wmul(sigmaHat, toxSignal));\n\n        uint256 dirDev; bool sellPressure;\n        if (dirState >= WAD) { dirDev = dirState - WAD; sellPressure = true; }\n        else { dirDev = WAD - dirState; sellPressure = false; }\n\n        uint256 skew = wmul(DIR_COEF, dirDev) + wmul(DIR_TOX_COEF, wmul(dirDev, toxSignal));\n        uint256 bidFee; uint256 askFee;\n        if (sellPressure) {\n            bidFee = fMid + skew;\n            askFee = fMid > skew ? fMid - skew : 0;\n        } else {\n            askFee = fMid + skew;\n            bidFee = fMid > skew ? fMid - skew : 0;\n        }\n\n        {\n            uint256 staleShift = wmul(STALE_DIR_COEF, toxSignal);\n            uint256 attractShift = wmul(staleShift, STALE_ATTRACT_FRAC);\n            if (spot >= pHat) {\n                bidFee = bidFee + staleShift;\n                askFee = askFee > attractShift ? askFee - attractShift : 0;\n            } else {\n                askFee = askFee + staleShift;\n                bidFee = bidFee > attractShift ? bidFee - attractShift : 0;\n            }\n        }\n\n        {\n            bool tradeAligned = (trade.isBuy && spot >= pHat) || (!trade.isBuy && spot < pHat);\n            if (tradeAligned) {\n                uint256 tradeBoost = wmul(TRADE_TOX_BOOST, tradeRatio);\n                if (trade.isBuy) { bidFee = bidFee + tradeBoost; }\n                else { askFee = askFee + tradeBoost; }\n            }\n        }\n\n        if (sellPressure) {\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_PROTECT));\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_ATTRACT));\n        } else {\n            askFee = clampFee(_compressTailWithSlope(askFee, TAIL_SLOPE_PROTECT));\n            bidFee = clampFee(_compressTailWithSlope(bidFee, TAIL_SLOPE_ATTRACT));\n        }\n\n        slots[0] = bidFee; slots[1] = askFee; slots[2] = trade.timestamp;\n        slots[3] = dirState; slots[4] = actEma; slots[5] = pHat;\n        slots[6] = sigmaHat; slots[7] = lambdaHat; slots[8] = sizeHat;\n        slots[9] = toxEma; slots[10] = stepTradeCount;\n\n        return (bidFee, askFee);\n    }\n\n    function _compressTailWithSlope(uint256 fee, uint256 slope) internal pure returns (uint256) {\n        if (fee <= TAIL_KNEE) return fee;\n        return TAIL_KNEE + wmul(fee - TAIL_KNEE, slope);\n    }\n\n    function _powWad(uint256 factor, uint256 exp) internal pure returns (uint256 result) {\n        result = WAD;\n        while (exp > 0) {\n            if (exp & 1 == 1) result = wmul(result, factor);\n            factor = wmul(factor, factor);\n            exp >>= 1;\n        }\n    }\n\n    function _decayCentered(uint256 centered, uint256 decayFactor, uint256 elapsed) internal pure returns (uint256) {\n        uint256 mul = _powWad(decayFactor, elapsed);\n        if (centered >= WAD) { return WAD + wmul(centered - WAD, mul); }\n        uint256 below = wmul(WAD - centered, mul);\n        return below < WAD ? WAD - below : 0;\n    }\n\n    function getName() external pure override returns (string memory) { return \"no_cubic\"; }\n}\n",
 "code_sha256": "69277bdfe0bcd0ab9c0ea05fac65f07a93953647f751e00c02050e75139d442a",
 "delta": "One edit from NP inside the blend block: pHat=wmul(pHat,9*WAD/10)+wmul(nS,WAD/10); becomes uint256 bw=feeUsed-30*BPS;if(bw>20*BPS)bw=20*BPS;bw=(WAD/10)*bw/(20*BPS);pHat=wmul(pHat,WAD-bw)+wmul(nS,bw);",
 "score": {
  "seed_start": 0,
  "n": 0,
  "mean_edge": null,
  "se": null
 },
 "score_fresh_candidate": {
  "seed_start": 800000,
  "n": 1000,
  "mean_edge": 524.4814383356673
 },
 "score_fresh_NP": {
  "seed_start": 800000,
  "n": 1000,
  "mean_edge": 524.5187256368337
 },
 "score_tune_NP": {
  "seed_start": 30000,
  "n": 1000,
  "mean_edge": 520.4499503137589
 },
 "paired": {
  "reference": "NP (sha256 95d21646a5b6d293c86c36a184769477be7b1ff39ab2adde7668ed79fb22dd38)",
  "seed_start": 800000,
  "n": 1000,
  "n_requested": 2000,
  "mean_diff": -0.03728730116627941,
  "se": 0.009129019888569517,
  "worst": [
   {
    "seed": 800197,
    "diff": -2.467945
   },
   {
    "seed": 800186,
    "diff": -2.006037
   },
   {
    "seed": 800494,
    "diff": -1.890045
   },
   {
    "seed": 800227,
    "diff": -1.599359
   },
   {
    "seed": 800836,
    "diff": -1.581862
   }
  ]
 },
 "promote": false,
 "seconds_per_simulation": 0.43,
 "seconds_per_simulation_note": "wall time per simulation (candidate plus normalizer) with amm_sim_rs.run_batch on 4 workers, measured at 0.42-0.46",
 "validate_ok": true,
 "blocked": null,
 "method": "Configs rebuilt as in MatchRunner._build_configs: np.random.default_rng(seed) draws retail_mean_size U(19,21), retail_arrival_rate U(0.6,1.0), gbm_sigma U(0.000882,0.001008); SimulationConfig(seed=seed), so the engine uses price seed=seed and retail seed=seed+1. Both strategies were run with amm_sim_rs.run_batch against the vanilla 30 bps normalizer. The edge is r.edges['submission']; diff = candidate - NP per seed; se = sd/sqrt(n).",
 "citations": [
  "https://github.com/benedictbrady/amm-challenge/blob/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/match.py",
  "https://github.com/benedictbrady/amm-challenge/blob/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/competition/config.py",
  "https://raw.githubusercontent.com/ChengHua926/amm-challenge/d34a5199c59a4c44d5336a25f1436b4ae1f21104/contracts/src/Strategy.sol"
 ]
}