← 返回编程题库
coding-min-spread-for-fill-rate中等免费版2000ms未尝试

Minimum Spread Tolerance for Target Fill Rate

Minimum Spread Tolerance for Target Fill Rate

开始编码

做市算法的报价台需要确定价差预算:限价单的价格容忍窗口要开多宽,才能让算法在昨天的中间价 tape 上至少有 ρN\rho \cdot N 个历史 mid 跨过限价并成交?开得太窄成交率崩盘;开得太宽执行成本飙升。答案是最小阈值 TT,使得在至少 ρN\lceil \rho N \rceil 个样本上 pirT|p_i - r| \le T 成立——其中 rr 是算法参考价(如理论公允价)。

实现 solution(prices, reference, target_ratio, abs_tol) -> float。返回满足成交率约束的最小 TT

结构是对连续阈值的 binary-search-on-answer

  1. 定义 fill(T)=#i:pirT\mathrm{fill}(T) = \#\\{i : |p_i - r| \le T\\}。该函数关于 TT 单调不降,因此谓词 feasible(T) = fill(T) >= ceil(target_ratio * N) 也单调。
  2. 区间:[lo,hi]=[0,maxipir][\mathrm{lo}, \mathrm{hi}] = [0,\, \max_i |p_i - r|]。下端通常不可行(除非所有偏差均为零),上端总是可行。
  3. 二分:mid=(lo+hi)/2\mathrm{mid} = (\mathrm{lo} + \mathrm{hi})/2,可行 himid\mathrm{hi} \leftarrow \mathrm{mid},否则 lomid\mathrm{lo} \leftarrow \mathrm{mid}hilo<abs_tol\mathrm{hi} - \mathrm{lo} < \mathrm{abs\_tol} 时停止。返回 hi\mathrm{hi}——已证明可行的最小阈值。

输入校验。pricestarget_ratio <= 0target_ratio > 1abs_tol <= 0ValueError。当 target_ratio = 1.0(或经向上取整后 required 等于 NN),直接返回 maxipir\max_i |p_i - r|——bisection 同样会收敛到那里,但直接返回避免了等式边界上的浮点漂移。

示例

solution([97.0, 99.0, 101.0, 103.0], 100.0, 0.5, 1e-6) 返回约 1.0。相对 r=100r=100 的偏差为 [3,1,1,3][3, 1, 1, 3],排序后 [1,1,3,3][1, 1, 3, 3]ρ=0.5\rho = 0.5 需要 0.54=2\lceil 0.5 \cdot 4 \rceil = 2 个成交,第二小的偏差 T=1.0T = 1.0 即为答案。bisection 在 [0,3][0, 3] 上收敛到 abs_tol=106\mathrm{abs\_tol} = 10^{-6} 内。

solution([100.1, 100.2, 100.3, 150.0], 100.0, 0.75, 1e-6) 返回约 0.3。偏差 [0.1,0.2,0.3,50.0][0.1, 0.2, 0.3, 50.0]0.754=3\lceil 0.75 \cdot 4 \rceil = 3 个成交,第三小偏差 0.30.3 胜出。离群值 50.050.0 被忽略——这正是基于分位数的价差预算相对均值预算的核心优势。

solution([99.0, 100.0, 101.0, 102.0], 100.0, 1.0, 1e-6) 返回 2.0ρ=1.0\rho = 1.0 强制全部成交,TT 至少为最大偏差 max(99100,100100,101100,102100)=2\max(|99-100|, |100-100|, |101-100|, |102-100|) = 2。求解器在 required==N 时直接返回最大值,避免 bisection 在边界上的浮点漂移。

参见 stubs/stub.py 的函数骨架。

实战背景

执行算法——VWAP、TWAP、POV、Implementation Shortfall——的标定需要历史成交统计:给定昨天的中间价 tape 相对公允价的偏差分布,限价单容忍窗口要开多宽,算法才能达到目标参与率?基于偏差均值设阈值非常脆弱:一根肥尾(一笔过期报价、一次 5 秒集合竞价不平衡)就把阈值拉高,算法在剩余时段普遍报价过宽。基于成交率的阈值则是偏差分布的分位数——天生抗离群。用 binary-search-on-answer 求解是经典模式:它直接推广到滑点预算(使 95% 执行滑点 <σ< \sigma 的最小 σ\sigma)、风险带尺寸(突破 α\le \alpha 历史日的最小 VaR 阈值),以及任何"满足频率目标的最紧阈值"型规约。本题归到 binary-search-on-answer cell,是因为 bisection 跑在答案空间(连续阈值 TT)上,而非输入数组——每个候选 TT 处的 feasibility 才是内层谓词。同样模式在 coding-smallest-position-meeting-risk-budget(离散答案空间)以及做市台上任何"满足计数/单调约束的最紧参数"问题中反复出现。

约束条件

  • 1 <= len(prices) <= 10^5
  • 0 < target_ratio <= 1.0
  • 1e-12 <= abs_tol <= 1e-3
  • Floating-point comparison tolerance: rel_tol = 1e-6, abs_tol = 1e-6
  • Empty prices、target_ratio <= 0、target_ratio > 1 或 abs_tol <= 0 抛 ValueError
  • 若 target_ratio 折算后要求所有 N 个全部成交(例如 target_ratio=1.0),答案恰为 max |price - reference|

样例

Case 1 · single price exactly at reference

输入: [[100],100,1,0.000001]

期望: 0

单一价格且等于 reference,偏差恒为 0,因此最小 spread 容差为 0。target_ratio=1.0 时直接返回最大偏差 max(|p-ref|)=0。

Case 2 · all-equal prices at reference, target 0.95

输入: [[100,100,100,100],100,0.95,0.000001]

期望: 0

所有报价都等于 reference,偏差全为 0。任何 T≥0 都满足 95% 成交率,因此最小 T = 0。bisection 在 [0, 0] 区间立即收敛。

Case 3 · target_ratio=1.0 returns the max deviation

输入: [[99,100,101,102],100,1,0.000001]

期望: 2

target_ratio=1.0 要求所有报价都成交,因此 T 必须 ≥ max|p-ref|。偏差为 [1,0,1,2],最大为 2.0。求解器在 required==n 时直接返回 max_dev,避免边界等式上的浮点漂移。

Case 4 · uniform spread target=0.5, even count

输入: [[97,99,101,103],100,0.5,0.000001]

期望: 1

偏差排序为 [1,1,3,3]。target=0.5,需要 ceil(0.5*4)=2 个成交。第二小的偏差是 1.0,所以最小 T = 1.0。bisection 在 abs_tol=1e-6 下收敛到该值附近。

Case 5 · outlier present, target 0.75 ignores the outlier

输入: [[100.1,100.2,100.3,150],100,0.75,0.000001]

期望: 0.3

偏差为 [0.1, 0.2, 0.3, 50.0]。target=0.75 需要 ceil(3)=3 个成交,按升序取第 3 个偏差 0.3 即可。50.0 这个离群值被丢弃 — 这正是分位数式阈值相对均值阈值的核心优势。

Case 6 · outlier with target_ratio=1.0 forces full coverage

输入: [[100.1,100.2,100.3,150],100,1,0.000001]

期望: 50

target_ratio=1.0 必须全部成交,T 至少为 max=50.0。这个测试和 0.75 的对照展示了 fill-rate 目标对 spread 预算的非线性放大效应:偏差从 0.3 跃升到 50。

最近提交

还没有提交记录。

编码区

实现 solution(...)。本地运行当前支持 Python 可见样例;服务端提交会运行可见样例和隐藏测试。

加载编辑器...
计时0:00

默认展示公开样例。点击「运行样例」后会在这里显示实际输出;点击「提交评测」会进入隐藏测试。

Case 1 · single price exactly at reference

输入: [[100],100,1,0.000001]

期望: 0

单一价格且等于 reference,偏差恒为 0,因此最小 spread 容差为 0。target_ratio=1.0 时直接返回最大偏差 max(|p-ref|)=0。

Case 2 · all-equal prices at reference, target 0.95

输入: [[100,100,100,100],100,0.95,0.000001]

期望: 0

所有报价都等于 reference,偏差全为 0。任何 T≥0 都满足 95% 成交率,因此最小 T = 0。bisection 在 [0, 0] 区间立即收敛。

Case 3 · target_ratio=1.0 returns the max deviation

输入: [[99,100,101,102],100,1,0.000001]

期望: 2

target_ratio=1.0 要求所有报价都成交,因此 T 必须 ≥ max|p-ref|。偏差为 [1,0,1,2],最大为 2.0。求解器在 required==n 时直接返回 max_dev,避免边界等式上的浮点漂移。

Case 4 · uniform spread target=0.5, even count

输入: [[97,99,101,103],100,0.5,0.000001]

期望: 1

偏差排序为 [1,1,3,3]。target=0.5,需要 ceil(0.5*4)=2 个成交。第二小的偏差是 1.0,所以最小 T = 1.0。bisection 在 abs_tol=1e-6 下收敛到该值附近。

Case 5 · outlier present, target 0.75 ignores the outlier

输入: [[100.1,100.2,100.3,150],100,0.75,0.000001]

期望: 0.3

偏差为 [0.1, 0.2, 0.3, 50.0]。target=0.75 需要 ceil(3)=3 个成交,按升序取第 3 个偏差 0.3 即可。50.0 这个离群值被丢弃 — 这正是分位数式阈值相对均值阈值的核心优势。

Case 6 · outlier with target_ratio=1.0 forces full coverage

输入: [[100.1,100.2,100.3,150],100,1,0.000001]

期望: 50

target_ratio=1.0 必须全部成交,T 至少为 max=50.0。这个测试和 0.75 的对照展示了 fill-rate 目标对 spread 预算的非线性放大效应:偏差从 0.3 跃升到 50。