跳转到主要内容
P2主题库中等

LLM 评测指标的实现

题面

在编辑器打开

LLM 评测指标的实现

题目描述

评测一个大模型时,我们拿到的往往只是一堆「对/错」和「置信度」,而真正要写进评测报告的
是统计指标:采样多次后模型至少答对一次的概率(pass@k)、模型「自信程度」与真实
正确率之间的偏差(ECE)、以及小样本下正确率的置信区间(Wilson interval)。

本题要求你实现这三个指标的精确算法:

函数 指标 用途
pass_at_k(n, c, k) pass@k 无偏估计 代码生成评测(HumanEval 式多采样)
expected_calibration_error(confidences, correct, n_bins) 期望校准误差 置信度校准评估
wilson_interval(successes, total, z) Wilson score 置信区间 小样本正确率的区间估计

三个函数的算法、边界约定、数值稳定性要求都在下面「规则」一节中精确定义,
评测按此逐字判定。浮点比较容差为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>10</mn><mrow><mo>−</mo><mn>9</mn></mrow></msup></mrow><annotation encoding="application/x-tex">10^{-9}</annotation></semantics></math>10−9(见「评分」)。

输入格式

一行 JSON 对象(input_str 即该 JSON 文本),字段 op 决定要求解哪个指标:

op 字段 类型 说明
"pass_at_k" n 整数 每个问题的采样数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>≥</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">n \ge 0</annotation></semantics></math>n≥0
c 整数 其中通过的采样数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mi>c</mi><mo>≤</mo><mi>n</mi></mrow><annotation encoding="application/x-tex">0 \le c \le n</annotation></semantics></math>0≤c≤n
k 整数 报告指标时抽取的样本数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mo>≥</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">k \ge 0</annotation></semantics></math>k≥0
"ece" confidences 数字数组 每个样本的置信度,元素 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>∈</mo><mo stretchy="false">[</mo><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">\in [0, 1]</annotation></semantics></math>∈[0,1]
correct 布尔数组 每个样本是否正确,长度与 confidences 相同
n_bins 整数 等宽桶个数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>≥</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">B \ge 1</annotation></semantics></math>B≥1
"wilson" successes 整数 成功次数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mtext>successes</mtext><mo>≤</mo><mtext>total</mtext></mrow><annotation encoding="application/x-tex">0 \le \text{successes} \le \text{total}</annotation></semantics></math>0≤successes≤total
total 整数 试验次数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>total</mtext><mo>≥</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">\text{total} \ge 0</annotation></semantics></math>total≥0
z 数字 正态分位数,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>z</mi><mo>≥</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">z \ge 0</annotation></semantics></math>z≥0(95% 置信度取 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.959963984540054</mn></mrow><annotation encoding="application/x-tex">1.959963984540054</annotation></semantics></math>1.959963984540054)

输入保证满足上表的取值范围(不会出现 n_bins = 0、长度不匹配、confidences 越界等
非法输入),因此你无需做输入校验。

输出格式

一行 JSON 对象,字段名固定为 value:

{"value": 0.5333333333333334}
{"value": [0.10779126740630099, 0.6032218525388546]}
op value 形态
pass_at_k 单个数字
ece 单个数字
wilson 长度为 2 的数组 [low, high]

允许在结果行之前打印任意调试信息(评测时只取最后一行非空内容作为结果)。

规则

1. pass_at_k(n, c, k) -> float

pass@k 的标准无偏估计:从 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi></mrow><annotation encoding="application/x-tex">n</annotation></semantics></math>n 个采样中不放回地抽取 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math>k 个,其中至少有一个通过的
概率。

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>pass@</mtext><mi>k</mi><mo>=</mo><mn>1</mn><mo>−</mo><mfrac><mstyle scriptlevel="0" displaystyle="true"><mrow><mo fence="true">(</mo><mfrac linethickness="0px"><mrow><mi>n</mi><mo>−</mo><mi>c</mi></mrow><mi>k</mi></mfrac><mo fence="true">)</mo></mrow></mstyle><mstyle scriptlevel="0" displaystyle="true"><mrow><mo fence="true">(</mo><mfrac linethickness="0px"><mi>n</mi><mi>k</mi></mfrac><mo fence="true">)</mo></mrow></mstyle></mfrac></mrow><annotation encoding="application/x-tex">\text{pass@}k = 1 - \frac{\dbinom{n-c}{k}}{\dbinom{n}{k}}</annotation></semantics></math>pass@k=1−(kn​)(kn−c​)​

约定(按下列顺序判定,先命中先返回):

条件 返回 理由
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mo><</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">k < 1</annotation></semantics></math>k<1 0.0 不抽取任何样本,命中概率为 0
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>≤</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">n \le 0</annotation></semantics></math>n≤0 0.0 没有任何采样,不做无根据的推断
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mo>≤</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">c \le 0</annotation></semantics></math>c≤0 0.0 没有一个样本通过
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mo>≥</mo><mi>n</mi></mrow><annotation encoding="application/x-tex">c \ge n</annotation></semantics></math>c≥n 1.0 全部样本通过
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mo>≥</mo><mi>n</mi></mrow><annotation encoding="application/x-tex">k \ge n</annotation></semantics></math>k≥n 或 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>−</mo><mi>c</mi><mo><</mo><mi>k</mi></mrow><annotation encoding="application/x-tex">n - c < k</annotation></semantics></math>n−c<k 1.0 抽满全部样本(或样本池中「失败样本」已不足 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi></mrow><annotation encoding="application/x-tex">k</annotation></semantics></math>k 个),必然抽到通过样本

其余情况按下式计算,且必须数值稳定:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>pass@</mtext><mi>k</mi><mo>=</mo><mn>1</mn><mo>−</mo><munderover><mo>∏</mo><mrow><mi>i</mi><mo>=</mo><mn>0</mn></mrow><mrow><mi>k</mi><mo>−</mo><mn>1</mn></mrow></munderover><mfrac><mrow><mi>n</mi><mo>−</mo><mi>c</mi><mo>−</mo><mi>i</mi></mrow><mrow><mi>n</mi><mo>−</mo><mi>i</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex">\text{pass@}k = 1 - \prod_{i=0}^{k-1} \frac{n-c-i}{n-i}</annotation></semantics></math>pass@k=1−i=0∏k−1​n−in−c−i​

也可以直接用高精度整数比(如 math.comb 后用 Fraction),只要结果误差在容差内。
注意:直接把组合数转成 float 再相除是不可行的——例如 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><msup><mn>10</mn><mn>5</mn></msup></mrow><annotation encoding="application/x-tex">n = 10^5</annotation></semantics></math>n=105、<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mo>=</mo><mn>100</mn></mrow><annotation encoding="application/x-tex">k = 100</annotation></semantics></math>k=100 时
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo fence="true">(</mo><mfrac linethickness="0px"><mi>n</mi><mi>k</mi></mfrac><mo fence="true">)</mo></mrow><annotation encoding="application/x-tex">\binom{n}{k}</annotation></semantics></math>(kn​) 远超 float 上限(约 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1.8</mn><mo>×</mo><msup><mn>10</mn><mn>308</mn></msup></mrow><annotation encoding="application/x-tex">1.8 \times 10^{308}</annotation></semantics></math>1.8×10308),会溢出为 inf 或抛
OverflowError(该用例记 RuntimeError、0 分,其余用例照常评测)。

2. expected_calibration_error(confidences, correct, n_bins) -> float

记第 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math>i 个样本的置信度为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>p</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">p_i</annotation></semantics></math>pi​,是否正确为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>y</mi><mi>i</mi></msub><mo>∈</mo><mo stretchy="false">{</mo><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo stretchy="false">}</mo></mrow><annotation encoding="application/x-tex">y_i \in {0, 1}</annotation></semantics></math>yi​∈{0,1},样本总数 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi></mrow><annotation encoding="application/x-tex">N</annotation></semantics></math>N,
桶数 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi></mrow><annotation encoding="application/x-tex">B</annotation></semantics></math>B。

分桶约定:等宽分桶,桶编号 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo separator="true">,</mo><mo>…</mo><mo separator="true">,</mo><mi>B</mi><mo>−</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">0, 1, \dots, B-1</annotation></semantics></math>0,1,…,B−1,桶 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi></mrow><annotation encoding="application/x-tex">j</annotation></semantics></math>j 的区间为

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mrow><mo fence="true">[</mo><mfrac><mi>j</mi><mi>B</mi></mfrac><mo separator="true">,</mo><mtext> </mtext><mfrac><mrow><mi>j</mi><mo>+</mo><mn>1</mn></mrow><mi>B</mi></mfrac><mo fence="true">)</mo></mrow><mspace width="1em"/><mo stretchy="false">(</mo><mtext>左闭右开</mtext><mo stretchy="false">)</mo><mo separator="true">,</mo><mspace width="2em"/><mtext>桶 </mtext><mi>B</mi><mo>−</mo><mn>1</mn><mtext> 为</mtext><mrow><mo fence="true">[</mo><mfrac><mrow><mi>B</mi><mo>−</mo><mn>1</mn></mrow><mi>B</mi></mfrac><mo separator="true">,</mo><mtext> </mtext><mn>1</mn><mo fence="true">]</mo></mrow><mtext> </mtext><mo stretchy="false">(</mo><mtext>右闭</mtext><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">\left[\frac{j}{B},\ \frac{j+1}{B}\right)
\quad (\text{左闭右开}), \qquad
\text{桶 } B-1 \text{ 为} \left[\frac{B-1}{B},\ 1\right] \ (\text{右闭})</annotation></semantics></math>
[Bj​, Bj+1​)(左闭右开),桶 B−1 为[BB−1​, 1] (右闭)

即:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>p</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">p_i</annotation></semantics></math>pi​ 属于满足 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mi>j</mi><mi>B</mi></mfrac><mo>≤</mo><msub><mi>p</mi><mi>i</mi></msub><mo><</mo><mfrac><mrow><mi>j</mi><mo>+</mo><mn>1</mn></mrow><mi>B</mi></mfrac></mrow><annotation encoding="application/x-tex">\frac{j}{B} \le p_i < \frac{j+1}{B}</annotation></semantics></math>Bj​≤pi​<Bj+1​ 的那个桶 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi></mrow><annotation encoding="application/x-tex">j</annotation></semantics></math>j;
置信度恰好等于桶边界时归入下桶(较大的 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi></mrow><annotation encoding="application/x-tex">j</annotation></semantics></math>j);confidence == 1.0 落在最后一个桶。

计算:对每个非空桶 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi></mrow><annotation encoding="application/x-tex">j</annotation></semantics></math>j(记其中样本数为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mi>j</mi></msub></mrow><annotation encoding="application/x-tex">N_j</annotation></semantics></math>Nj​):

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mtext>conf</mtext><mi>j</mi></msub><mo>=</mo><mfrac><mn>1</mn><msub><mi>N</mi><mi>j</mi></msub></mfrac><munder><mo>∑</mo><mrow><mi>i</mi><mo>∈</mo><mi>j</mi></mrow></munder><msub><mi>p</mi><mi>i</mi></msub><mo separator="true">,</mo><mspace width="2em"/><msub><mtext>acc</mtext><mi>j</mi></msub><mo>=</mo><mfrac><mn>1</mn><msub><mi>N</mi><mi>j</mi></msub></mfrac><munder><mo>∑</mo><mrow><mi>i</mi><mo>∈</mo><mi>j</mi></mrow></munder><msub><mi>y</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">\text{conf}j = \frac{1}{N_j}\sum{i \in j} p_i, \qquad
\text{acc}j = \frac{1}{N_j}\sum{i \in j} y_i</annotation></semantics></math>
confj​=Nj​1​i∈j∑​pi​,accj​=Nj​1​i∈j∑​yi​

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>ECE</mtext><mo>=</mo><munder><mo>∑</mo><mrow><mi>j</mi><mo>:</mo><mtext> </mtext><msub><mi>N</mi><mi>j</mi></msub><mo>></mo><mn>0</mn></mrow></munder><mfrac><msub><mi>N</mi><mi>j</mi></msub><mi>N</mi></mfrac><mo>⋅</mo><mrow><mo fence="true">∣</mo><msub><mtext>acc</mtext><mi>j</mi></msub><mo>−</mo><msub><mtext>conf</mtext><mi>j</mi></msub><mo fence="true">∣</mo></mrow></mrow><annotation encoding="application/x-tex">\text{ECE} = \sum_{j:\ N_j > 0} \frac{N_j}{N} \cdot \left| \text{acc}_j - \text{conf}_j \right|</annotation></semantics></math>ECE=j: Nj​>0∑​NNj​​⋅∣accj​−confj​∣

约定:

  • 空桶必须跳过(<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mi>j</mi></msub><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">N_j = 0</annotation></semantics></math>Nj​=0 的桶不计 0、不计 1、不参与任何平均),桶内 acc 对
    空桶没有定义;
  • 权重是样本数 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>N</mi><mi>j</mi></msub><mi mathvariant="normal">/</mi><mi>N</mi></mrow><annotation encoding="application/x-tex">N_j / N</annotation></semantics></math>Nj​/N,不是 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mi mathvariant="normal">/</mi><mi>B</mi></mrow><annotation encoding="application/x-tex">1/B</annotation></semantics></math>1/B(不是「每个桶等权」,也不是「按桶数平均」);
  • <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>N</mi><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">N = 0</annotation></semantics></math>N=0(confidences 为空数组)时返回 0.0。

分桶的取值口径(判定依据,不是提示):<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>p</mi><mi>i</mi></msub></mrow><annotation encoding="application/x-tex">p_i</annotation></semantics></math>pi​ 取 JSON 文本中该数字的十进制字面量
所表示的实数。也就是说 0.29 就是 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>29</mn><mi mathvariant="normal">/</mi><mn>100</mn></mrow><annotation encoding="application/x-tex">29/100</annotation></semantics></math>29/100、0.3 就是 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>3</mn><mi mathvariant="normal">/</mi><mn>10</mn></mrow><annotation encoding="application/x-tex">3/10</annotation></semantics></math>3/10,不要把字面量先解析成
IEEE-754 binary64 再判定桶归属——那样得到的不是字面量本身。例如
0.29 的精确值是 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.28999999999999998</mn><mo>…</mo></mrow><annotation encoding="application/x-tex">0.28999999999999998\ldots</annotation></semantics></math>0.28999999999999998…,乘 100 后不足 29,会被判到 28 号桶,
而按本口径它落在 29 号桶;0.57、0.3(<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>=</mo><mn>10</mn></mrow><annotation encoding="application/x-tex">B = 10</annotation></semantics></math>B=10)同理。

换句话说,"先把数字读成 binary64、再用 int(p * B) 或 Fraction(p) 截断"这类实现都会
在字面量恰好等于桶边界的取值上判错桶(示例 5 即为此类用例)。

3. wilson_interval(successes, total, z) -> [float, float]

Wilson score 置信区间(Clopper-Pearson 的常用替代,小样本下比 Wald 区间可靠)。
记 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo>=</mo><mtext>successes</mtext><mi mathvariant="normal">/</mi><mtext>total</mtext></mrow><annotation encoding="application/x-tex">\hat{p} = \text{successes} / \text{total}</annotation></semantics></math>p^​=successes/total,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mtext>total</mtext></mrow><annotation encoding="application/x-tex">n = \text{total}</annotation></semantics></math>n=total:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>center</mtext><mo>=</mo><mfrac><mrow><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo>+</mo><mfrac><msup><mi>z</mi><mn>2</mn></msup><mrow><mn>2</mn><mi>n</mi></mrow></mfrac></mrow><mrow><mn>1</mn><mo>+</mo><mfrac><msup><mi>z</mi><mn>2</mn></msup><mi>n</mi></mfrac></mrow></mfrac><mo separator="true">,</mo><mspace width="2em"/><mtext>half</mtext><mo>=</mo><mfrac><mi>z</mi><mrow><mn>1</mn><mo>+</mo><mfrac><msup><mi>z</mi><mn>2</mn></msup><mi>n</mi></mfrac></mrow></mfrac><msqrt><mrow><mfrac><mrow><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo stretchy="false">(</mo><mn>1</mn><mo>−</mo><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo stretchy="false">)</mo></mrow><mi>n</mi></mfrac><mo>+</mo><mfrac><msup><mi>z</mi><mn>2</mn></msup><mrow><mn>4</mn><msup><mi>n</mi><mn>2</mn></msup></mrow></mfrac></mrow></msqrt></mrow><annotation encoding="application/x-tex">\text{center} = \frac{\hat{p} + \frac{z^2}{2n}}{1 + \frac{z^2}{n}},
\qquad
\text{half} = \frac{z}{1 + \frac{z^2}{n}} \sqrt{\frac{\hat{p}(1-\hat{p})}{n} + \frac{z^2}{4n^2}}</annotation></semantics></math>
center=1+nz2​p^​+2nz2​​,half=1+nz2​z​np^​(1−p^​)​+4n2z2​<svg xmlns="http://www.w3.org/2000/svg" width="400em" height="2.48em" viewBox="0 0 400000 2592" preserveAspectRatio="xMinYMin slice"><path d="M424,2478 c-1.3,-0.7,-38.5,-172,-111.5,-514c-73,-342,-109.8,-513.3,-110.5,-514 c0,-2,-10.7,14.3,-32,49c-4.7,7.3,-9.8,15.7,-15.5,25c-5.7,9.3,-9.8,16,-12.5,20 s-5,7,-5,7c-4,-3.3,-8.3,-7.7,-13,-13s-13,-13,-13,-13s76,-122,76,-122s77,-121,77,-121 s209,968,209,968c0,-2,84.7,-361.7,254,-1079c169.3,-717.3,254.7,-1077.7,256,-1081 l0 -0c4,-6.7,10,-10,18,-10 H400000 v40H1014.6 s-87.3,378.7,-272.6,1166c-185.3,787.3,-279.3,1182.3,-282,1185 c-2,6,-10,9,-24,9 c-8,0,-12,-0.7,-12,-2z M1001 80 h400000v40h-400000z"/></svg>​

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>low</mtext><mo>=</mo><mtext>center</mtext><mo>−</mo><mtext>half</mtext><mo separator="true">,</mo><mspace width="2em"/><mtext>high</mtext><mo>=</mo><mtext>center</mtext><mo>+</mo><mtext>half</mtext></mrow><annotation encoding="application/x-tex">\text{low} = \text{center} - \text{half}, \qquad \text{high} = \text{center} + \text{half}</annotation></semantics></math>low=center−half,high=center+half

约定:

  • <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>total</mtext><mo>≤</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">\text{total} \le 0</annotation></semantics></math>total≤0 时返回 [0.0, 1.0](没有任何试验 → 完全无知的区间);
  • 两个端点钳制到 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[0, 1]</annotation></semantics></math>[0,1](浮点残差可能让端点微微越界,例如
    <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>successes</mtext><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">\text{successes} = 0</annotation></semantics></math>successes=0 时 low 可能算出 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>±</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>19</mn></mrow></msup></mrow><annotation encoding="application/x-tex">\pm 10^{-19}</annotation></semantics></math>±10−19);
  • 返回顺序恒为 [low, high],即 low ≤ high;
  • z = 0 是合法的退化输入,此时区间收缩为点估计 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">[</mo><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo separator="true">,</mo><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">[\hat{p}, \hat{p}]</annotation></semantics></math>[p^​,p^​];
  • 不要依赖 scipy / statsmodels(评测镜像中不保证存在),请用标准库 math。

示例

示例 1(pass@k)

输入:

{"op": "pass_at_k", "n": 10, "c": 3, "k": 2}

输出:

{"value": 0.5333333333333334}

说明:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>−</mo><mrow><mo fence="true">(</mo><mfrac linethickness="0px"><mn>7</mn><mn>2</mn></mfrac><mo fence="true">)</mo></mrow><mi mathvariant="normal">/</mi><mrow><mo fence="true">(</mo><mfrac linethickness="0px"><mn>10</mn><mn>2</mn></mfrac><mo fence="true">)</mo></mrow><mo>=</mo><mn>1</mn><mo>−</mo><mn>21</mn><mi mathvariant="normal">/</mi><mn>45</mn><mo>=</mo><mn>0.5333</mn><mo>…</mo></mrow><annotation encoding="application/x-tex">1 - \binom{7}{2}/\binom{10}{2} = 1 - 21/45 = 0.5333\ldots</annotation></semantics></math>1−(27​)/(210​)=1−21/45=0.5333…

示例 2(ECE)

输入:

{"op": "ece", "confidences": [0.9, 0.8, 0.6, 0.4], "correct": [true, true, false, false], "n_bins": 5}

输出:

{"value": 0.32499999999999996}

说明:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>=</mo><mn>5</mn></mrow><annotation encoding="application/x-tex">B = 5</annotation></semantics></math>B=5 时桶宽为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.2</mn></mrow><annotation encoding="application/x-tex">0.2</annotation></semantics></math>0.2。<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.9</mn><mo separator="true">,</mo><mn>0.8</mn></mrow><annotation encoding="application/x-tex">0.9, 0.8</annotation></semantics></math>0.9,0.8 落 4 号桶(<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>conf</mtext><mo>=</mo><mn>0.85</mn></mrow><annotation encoding="application/x-tex">\text{conf}=0.85</annotation></semantics></math>conf=0.85,
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>acc</mtext><mo>=</mo><mn>1.0</mn></mrow><annotation encoding="application/x-tex">\text{acc}=1.0</annotation></semantics></math>acc=1.0);<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.6</mn></mrow><annotation encoding="application/x-tex">0.6</annotation></semantics></math>0.6 落 3 号桶(<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.6</mn></mrow><annotation encoding="application/x-tex">0.6</annotation></semantics></math>0.6 vs <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn></mrow><annotation encoding="application/x-tex">0</annotation></semantics></math>0);<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.4</mn></mrow><annotation encoding="application/x-tex">0.4</annotation></semantics></math>0.4 落 2 号桶(<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.4</mn></mrow><annotation encoding="application/x-tex">0.4</annotation></semantics></math>0.4 vs <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn></mrow><annotation encoding="application/x-tex">0</annotation></semantics></math>0);
0、1 号桶为空跳过。故
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>ECE</mtext><mo>=</mo><mfrac><mn>2</mn><mn>4</mn></mfrac><mo stretchy="false">(</mo><mn>0.15</mn><mo stretchy="false">)</mo><mo>+</mo><mfrac><mn>1</mn><mn>4</mn></mfrac><mo stretchy="false">(</mo><mn>0.6</mn><mo stretchy="false">)</mo><mo>+</mo><mfrac><mn>1</mn><mn>4</mn></mfrac><mo stretchy="false">(</mo><mn>0.4</mn><mo stretchy="false">)</mo><mo>=</mo><mn>0.325</mn></mrow><annotation encoding="application/x-tex">\text{ECE} = \frac{2}{4}(0.15) + \frac{1}{4}(0.6) + \frac{1}{4}(0.4) = 0.325</annotation></semantics></math>ECE=42​(0.15)+41​(0.6)+41​(0.4)=0.325。

示例 3(Wilson 区间)

输入:

{"op": "wilson", "successes": 3, "total": 10, "z": 1.959963984540054}

输出:

{"value": [0.10779126740630099, 0.6032218525388546]}

说明:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mover accent="true"><mi>p</mi><mo>^</mo></mover><mo>=</mo><mn>0.3</mn></mrow><annotation encoding="application/x-tex">\hat{p} = 0.3</annotation></semantics></math>p^​=0.3,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mn>10</mn></mrow><annotation encoding="application/x-tex">n = 10</annotation></semantics></math>n=10,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>z</mi><mo>=</mo><mn>1.95996</mn><mo>…</mo></mrow><annotation encoding="application/x-tex">z = 1.95996\ldots</annotation></semantics></math>z=1.95996…(双侧 95%)。

示例 4(ECE 桶边界)

输入:

{"op": "ece", "confidences": [0.25, 0.26], "correct": [true, false], "n_bins": 4}

输出:

{"value": 0.245}

说明:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>=</mo><mn>4</mn></mrow><annotation encoding="application/x-tex">B = 4</annotation></semantics></math>B=4 时桶宽为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.25</mn></mrow><annotation encoding="application/x-tex">0.25</annotation></semantics></math>0.25。<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.25</mn></mrow><annotation encoding="application/x-tex">0.25</annotation></semantics></math>0.25 恰好等于 1 号桶的左边界,按「左闭右开」
归入 1 号桶(不是 0 号桶);<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.26</mn></mrow><annotation encoding="application/x-tex">0.26</annotation></semantics></math>0.26 也在 1 号桶。该桶
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>conf</mtext><mo>=</mo><mn>0.255</mn></mrow><annotation encoding="application/x-tex">\text{conf} = 0.255</annotation></semantics></math>conf=0.255、<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>acc</mtext><mo>=</mo><mn>0.5</mn></mrow><annotation encoding="application/x-tex">\text{acc} = 0.5</annotation></semantics></math>acc=0.5,故 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>ECE</mtext><mo>=</mo><mi mathvariant="normal">∣</mi><mn>0.5</mn><mo>−</mo><mn>0.255</mn><mi mathvariant="normal">∣</mi><mo>=</mo><mn>0.245</mn></mrow><annotation encoding="application/x-tex">\text{ECE} = |0.5 - 0.255| = 0.245</annotation></semantics></math>ECE=∣0.5−0.255∣=0.245。
(若把边界归入上桶,结果会是 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.505</mn></mrow><annotation encoding="application/x-tex">0.505</annotation></semantics></math>0.505,将被判错。)

示例 5(ECE:十进制字面量口径)

输入:

{"op": "ece", "confidences": [0.29, 0.292], "correct": [true, false], "n_bins": 100}

输出:

{"value": 0.20900000000000002}

说明:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi><mo>=</mo><mn>100</mn></mrow><annotation encoding="application/x-tex">B = 100</annotation></semantics></math>B=100 时桶宽为 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.01</mn></mrow><annotation encoding="application/x-tex">0.01</annotation></semantics></math>0.01。按本口径 0.29 就是 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>29</mn><mi mathvariant="normal">/</mi><mn>100</mn></mrow><annotation encoding="application/x-tex">29/100</annotation></semantics></math>29/100,恰好是 29 号桶的左
边界
,与 0.292 同在 29 号桶:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>conf</mtext><mo>=</mo><mn>0.291</mn></mrow><annotation encoding="application/x-tex">\text{conf} = 0.291</annotation></semantics></math>conf=0.291、<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>acc</mtext><mo>=</mo><mn>0.5</mn></mrow><annotation encoding="application/x-tex">\text{acc} = 0.5</annotation></semantics></math>acc=0.5,故
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mtext>ECE</mtext><mo>=</mo><mi mathvariant="normal">∣</mi><mn>0.5</mn><mo>−</mo><mn>0.291</mn><mi mathvariant="normal">∣</mi><mo>=</mo><mn>0.209</mn></mrow><annotation encoding="application/x-tex">\text{ECE} = |0.5 - 0.291| = 0.209</annotation></semantics></math>ECE=∣0.5−0.291∣=0.209。若先把 0.29 读成 binary64(精确值
<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.28999999999999998</mn><mo>…</mo></mrow><annotation encoding="application/x-tex">0.28999999999999998\ldots</annotation></semantics></math>0.28999999999999998…)再乘 100 截断,它会落到 28 号桶,ECE 变成 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0.501</mn></mrow><annotation encoding="application/x-tex">0.501</annotation></semantics></math>0.501 而判错。

数据范围与限制

  • <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mi>n</mi><mo>≤</mo><msup><mn>10</mn><mn>6</mn></msup></mrow><annotation encoding="application/x-tex">0 \le n \le 10^6</annotation></semantics></math>0≤n≤106,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mi>c</mi><mo>≤</mo><mi>n</mi></mrow><annotation encoding="application/x-tex">0 \le c \le n</annotation></semantics></math>0≤c≤n,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mi>k</mi><mo>≤</mo><msup><mn>10</mn><mn>6</mn></msup></mrow><annotation encoding="application/x-tex">0 \le k \le 10^6</annotation></semantics></math>0≤k≤106
  • <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>≤</mo><mi>B</mi><mo>≤</mo><mn>1000</mn></mrow><annotation encoding="application/x-tex">1 \le B \le 1000</annotation></semantics></math>1≤B≤1000,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mtext>len(confidences)</mtext><mo>≤</mo><mn>1000</mn></mrow><annotation encoding="application/x-tex">0 \le \text{len(confidences)} \le 1000</annotation></semantics></math>0≤len(confidences)≤1000
  • <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mtext>total</mtext><mo>≤</mo><msup><mn>10</mn><mn>6</mn></msup></mrow><annotation encoding="application/x-tex">0 \le \text{total} \le 10^6</annotation></semantics></math>0≤total≤106,<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>0</mn><mo>≤</mo><mi>z</mi><mo>≤</mo><mn>10</mn></mrow><annotation encoding="application/x-tex">0 \le z \le 10</annotation></semantics></math>0≤z≤10
  • 时间限制:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>2000</mn><mtext>ms</mtext></mrow><annotation encoding="application/x-tex">2000\text{ms}</annotation></semantics></math>2000ms / 用例;内存限制:<math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>256</mn><mtext>MB</mtext></mrow><annotation encoding="application/x-tex">256\text{MB}</annotation></semantics></math>256MB

单用例超时(记 TimeLimitExceeded)与选手代码抛异常(记 RuntimeError)都只影响
该用例
(该用例 0 分),评测继续执行其余用例——一条用例崩溃不会连带丢掉其余用例的
得分;只有 Solution 进程崩溃、评测通道断开等评测侧故障才会使整份评测失败。

评分

  • 正式得分只来自隐藏用例,各用例等权,满分 100 分。
  • 判定标准:输出为合法 JSON 对象且含字段 value;value 必须是有限数字
    (wilson 为长度 2 的有限数字数组),与标准答案的逐元素绝对误差
    <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>≤</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>9</mn></mrow></msup></mrow><annotation encoding="application/x-tex">\le 10^{-9}</annotation></semantics></math>≤10−9
    ;wilson 还要求 low <= high。布尔值不算数字
    ({"value": true} 判错),NaN / Infinity 判错。
  • 容差取 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>10</mn><mrow><mo>−</mo><mn>9</mn></mrow></msup></mrow><annotation encoding="application/x-tex">10^{-9}</annotation></semantics></math>10−9 的理由:正确实现的不同代码路径(连乘比值、math.comb 整数比、
    对数空间)之间只差浮点末位(约 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>10</mn><mrow><mo>−</mo><mn>16</mn></mrow></msup><mo>∼</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>13</mn></mrow></msup></mrow><annotation encoding="application/x-tex">10^{-16} \sim 10^{-13}</annotation></semantics></math>10−16∼10−13),而任何语义偏差
    (把「规则」一节里的某个约定理解错)都远大于此,量级在 <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>10</mn><mrow><mo>−</mo><mn>2</mn></mrow></msup></mrow><annotation encoding="application/x-tex">10^{-2}</annotation></semantics></math>10−2 以上。
    <math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mn>10</mn><mrow><mo>−</mo><mn>9</mn></mrow></msup></mrow><annotation encoding="application/x-tex">10^{-9}</annotation></semantics></math>10−9 位于两者之间约 6 个数量级,既能容忍合法的实现差异,又不会放过实质性错误。
  • 题面中的 5 个示例会作为可见用例运行并展示调试信息,但不计分。
登录 后即可提交代码

题解与讨论

查看本题的公开题解,或在通过后分享思路。

查看全部
题解加载中…