# @elastic/statistics 中文文档

规范地址：https://npmcn.com/package/%40elastic/statistics/

版本：0\.0\.4
npm 元数据更新时间：2026-11-05T10:12:59.925Z
中文译文更新时间：2026-11-13T10:41:28.949Z
软件包许可：Elastic-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40elastic/statistics
原文对照：https://npmcn.com/package/%40elastic/statistics/?lang=original
项目主页：https://github.com/elastic/statistics#readme
源码仓库：https://github.com/elastic/statistics

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：227 kB，87 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=22
- 运行依赖：1 个
- 首次发布：2026-09-24T14:16:55.045Z
- 最新版本发布：2026-11-05T10:12:59.694Z
- 近 30 天下载量：129038（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 5 个）

## 中文简介

LLM评价的纯统计功能

## 安装

```sh
npm install @elastic/statistics
```

也可以使用：`pnpm add @elastic/statistics`、`yarn add @elastic/statistics`、`bun add @elastic/statistics`

## README

# @ 弹性/ 统计

TypeScript和JavaScript的纯统计函数.

## 安装

```bash
npm install @elastic/statistics
```

## 使用量

### 测试

镜像 [`scipy.stats.ttest_rel`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_rel.html)。 。 。 . 当每个差异都是一样的时,统计是无限的`NaN` 如果差异为零)和 p 值为 1, scipy 返回 0 (`NaN`) (中文(简体) ).

```ts
import { pairedT } from '@elastic/statistics';

const { statistic, pValue } = pairedT([0.9, 0.85, 0.7, 0.95, 0.8], [0.8, 0.75, 0.72, 0.88, 0.7]);
// statistic and pValue are null when fewer than two pairs are given
```

### Wilcoxon 签名排名测试

镜像 [`scipy.stats.wilcoxon`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.wilcoxon.html)· ; p值与浮点精度相匹配.

```ts
import { wilcoxonSignedRank } from '@elastic/statistics';

const target = [0.9, 0.85, 0.7, 0.95, 0.8, 0.6, 0.75];
const baseline = [0.8, 0.75, 0.72, 0.88, 0.7, 0.6, 0.7];

const { statistic, pValue, zStatistic } = wilcoxonSignedRank(target, baseline);
// { statistic: 1, pValue: 0.0625, zStatistic: null }

// Is target stochastically greater than baseline? Use the normal approximation with continuity correction.
wilcoxonSignedRank(target, baseline, {
  alternative: 'greater',
  method: 'asymptotic',
  correction: true,
});
```

| 选项 | 数值 | 默认 | 页:1 |
| --- | --- | --- | --- |
| `alternative` | `'two-sided'`, (中文(简体) ), `'less'`, (中文(简体) ), `'greater'` | `'two-sided'` | 关于差异的假设 `target - baseline`。 。 。 . |
| `zeroMethod` | `'wilcox'`, (中文(简体) ), `'pratt'`, (中文(简体) ), `'zsplit'` | `'wilcox'` | 丢弃零分之差,排出他们的分级但降出他们的分级,或者将他们的分级分成两笔. |
| `method` | `'auto'`, (中文(简体) ), `'exact'`, (中文(简体) ), `'asymptotic'` | `'auto'` | `'auto'` 使用精确分布到50对,没有带或零,对最多13对进行详尽的穿透试验,以及上面的正常近似. |
| `correction` | `boolean` | `false` | 正常近似值的持续修正. |

`statistic` 的正和负级金额中 `'two-sided'`和正数 否则,军衔总数. `zStatistic` 当使用正常近似时设置。 三个都是 `null` 当试验不能计算的时候(没有对子, `NaN` 在样本中,或者没有为正常状态留下差异 近似值,例如每个相差为零;函数从未为这种输入投出.

差异用浮点计算,所以对接时可以得到不同的分级 因圆相取( 二`0.825 - 0.775 !== 0.5 - 0.45`) (中文(简体) ). 当确切的领带重要时,第一回合得分.

### 曼-惠特尼U测试

两个独立样本的非参数测试。 镜像 [`scipy.stats.mannwhitneyu`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mannwhitneyu.html)· ; p值与浮点精度相匹配.

```ts
import { mannWhitney } from '@elastic/statistics';

const target = [0.9, 0.85, 0.7, 0.95, 0.8];
const baseline = [0.8, 0.75, 0.72, 0.6];

const { statistic, pValue, zStatistic } = mannWhitney(target, baseline);
// { statistic: 16.5, pValue: 0.1399825237583975, zStatistic: 1.475856111043418 }

mannWhitney(target, baseline, { alternative: 'greater', method: 'asymptotic' });
// { statistic: 16.5, pValue: 0.06999126187919875, zStatistic: 1.475856111043418 }
```

| 选项 | 数值 | 默认 | 页:1 |
| --- | --- | --- | --- |
| `alternative` | `'two-sided'`, (中文(简体) ), `'less'`, (中文(简体) ), `'greater'` | `'two-sided'` | 是否隐藏分布 `target` 不同之处,是比 `baseline`对. |
| `method` | `'auto'`, (中文(简体) ), `'exact'`, (中文(简体) ), `'asymptotic'` | `'auto'` | `'auto'` 使用一个样本最多有8个值且没有关系时的精确分布,通常的近似. |
| `continuity` | `boolean` | `true` | 正常近似值的持续修正. |

`statistic` 是 U 统计 `target`· ; `baseline`这是 `target.length * baseline.length - statistic`。 。 。 . `zStatistic` 当使用正常近似时设置。 三个都是 `null` 当测试时 无法计算(一个空样本, `NaN` 在样本中,或者在正常近似下捆绑的每个值.

### 麦克尼马尔测试

二进制结果`true`编号 :`false` 或者说 `1`编号 :`0`)在相同的例子上得分. 只有不和谐的人 双数 : `b` (第一个样本成功,第二个失败)和 `c` (倒转). 输入要么是 两个对齐的数组或一个 2x2 表格 `[[a, b], [c, d]]` 在统计模型布局中( 第一组) 样本成功/失败,列:第二个样本成功/失败.

```ts
import {
  mcnemar,
  mcnemarExact,
  mcnemarMidP,
  mcnemarStratified,
  pairedTable,
} from '@elastic/statistics';

const target = [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1];
const baseline = [1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0];

mcnemar(target, baseline);
// { statistic: 0, pValue: 0.0625, method: 'mid-p' }
pairedTable(target, baseline);
// [[5, 4], [0, 3]]
mcnemarExact([
  [5, 4],
  [0, 3],
]);
// { statistic: 0, pValue: 0.125, method: 'exact' }
mcnemar(
  [
    [5, 4],
    [0, 3],
  ],
  { method: 'asymptotic' },
);
// { statistic: 2.25, pValue: 0.13361440253771584, method: 'asymptotic' }

// Categorical labels: one-vs-rest McNemar per class, in order of first appearance.
mcnemarStratified(
  ['cat', 'dog', 'dog', 'bird', 'cat', 'cat'],
  ['cat', 'cat', 'dog', 'bird', 'dog', 'bird'],
);
// [
//   { label: 'cat', statistic: 1, pValue: 0.625, method: 'mid-p' },
//   { label: 'dog', statistic: 1, pValue: 1, method: 'mid-p' },
//   { label: 'bird', statistic: 0, pValue: 0.5, method: 'mid-p' },
// ]
```

| 选项 | 数值 | 默认 | 页:1 |
| --- | --- | --- | --- |
| `method` | `'mid-p'`, (中文(简体) ), `'exact'`, (中文(简体) ), `'asymptotic'` | `'mid-p'` | 精确条件测试的中p变体(Fagerland, Lydersen & Laake, 2013),精确条件测试,或chi-平方相近. |
| `correction` | `boolean` | `true` | 不对称统计持续更正1. |

`'exact'` 财务报告和审定财务报表 `'asymptotic'` 镜像 [`statsmodels.stats.contingency_tables.mcnemar`](https://www.statsmodels.org/stable/generated/statsmodels.stats.contingency_tables.mcnemar.html)。 。 。 . `statistic` 实值 `min(b, c)` 页:1 `'mid-p'` 财务报告和审定财务报表 `'exact'`,和chi `'asymptotic'`· ; `method` 报告所使用的变体。 无相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相 统计 0和p-值 1(统计模型的不对称变体在那里没有定义). `mcnemarExact` 财务报告和审定财务报表 `mcnemarMidP` 强制一个变体. `mcnemarStratified` 使用对齐的绝对标签并运行 在一vs-rest设置中,每类测试相同;其p-值不作多相比较调整.

### 费舍尔的测试

2x2表的独立性精确测试,按列(如成功)给出为行(如组) / 失败)。 镜像 [`scipy.stats.fisher_exact`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.fisher_exact.html), (中文(简体) ), 包括它的双面p-值(表格的总概率最多与观察到的概率相同).

```ts
import { fisherExact } from '@elastic/statistics';

fisherExact([
  [8, 2],
  [1, 5],
]);
// { statistic: 20, pValue: 0.03496503496503496 }
fisherExact(
  [
    [8, 2],
    [1, 5],
  ],
  { alternative: 'greater' },
);
// { statistic: 20, pValue: 0.024475524475524472 }
```

`statistic` 样本概率比 `(a * d) / (b * c)` 页:1 `[[a, b], [c, d]]`数字 : `Infinity` 何时 `b` 或者说 `c` 为零, `null` 当一行或一列为空时(p值为 1). 职能 抛出不是2x2的表格,或持有负数或非整数.

### 千平天下的独立考验

皮尔逊对一个RxC表的基平测试作为行给出. 镜像 [`scipy.stats.chi2_contingency`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2_contingency.html)· ; p值与浮点精度相匹配.

```ts
import { chiSquared } from '@elastic/statistics';

chiSquared([
  [10, 10, 20],
  [20, 20, 20],
]);
// { statistic: 2.7777777777777777, pValue: 0.2493522087772962, dof: 2,
//   expected: [[12, 12, 16], [18, 18, 24]], minimumExpected: 12 }

// Yates' continuity correction is applied to 2x2 tables unless disabled.
chiSquared([
  [12, 3],
  [17, 16],
]).pValue; // 0.12063113097759684
chiSquared(
  [
    [12, 3],
    [17, 16],
  ],
  { correction: false },
).pValue; // 0.061412253987091166
```

`statistic` 财务报告和审定财务报表 `pValue` 为: `null` 当一行或一列的预期数为零时(小数上升) (d) 将第1款改为第1款; `dof`, (中文(简体) ), `expected` 财务报告和审定财务报表 `minimumExpected` 总是被报告。 大约是 通常认为适当 `minimumExpected` 至少为 5; 使用 `fisherExact` 2x2 个表格 ê娩 该函数用于空或挂起的表格和负数.

### Shapiro- Wilk 常态测试

测试样本,一般是配对差异是否正常分布. 镜像 [`scipy.stats.shapiro`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.shapiro.html)。 。 。 .

```ts
import { shapiroWilk } from '@elastic/statistics';

shapiroWilk([0.12, -0.05, 0.08, 0.02, -0.11, 0.07, 0.01, 0.15, -0.03, 0.04]);
// { statistic: 0.9875422022224214, pValue: 0.992829252967651 }
shapiroWilk([0.01, 0.02, 0.02, 0.03, 0.05, 0.09, 0.2, 0.4, 0.9, 2.5]);
// { statistic: 0.6044891945571944, pValue: 0.00006255986205030777 }
```

`statistic` 是 W, 单位 `(0, 1]`;1附近的值与常态一致。 测试至少需要 3 个值, 和 scipy 一样, 给出任何大小的精确 W , 但大约 5 000 以上的 p 值 数值。 两个领域都是 `null` 不到三个数值, `NaN` 在样本中,或恒定样本。 W 统计匹配为 浮点精度; p 值通过 `log(1 - W)` 并匹配到大约 1e- 10 相对 w非常接近1时精确.

## 脚本

```bash
npm ci
npm run build
npm test
npm run lint
```

## 汇兑政策

此软件包随后 [语义版本](https://semver.org/)数字 :

-   **补丁** ——错误修正,不会改变公共 API
-   **次要** - 新功能或不可破损的添加
-   **少校** ——打破导出类型或函数签名的更改

直至 `1.0.0`,则 `0.x` 系列可能引入小版本的分解更改.

## 许可证

弹性许可证2.0. 在公布一揽子计划时,可重新讨论这个问题.

## 常见问题

### @elastic/statistics 是什么？

LLM评价的纯统计功能

### 如何安装 @elastic/statistics？

在项目目录运行 npm install @elastic/statistics，或使用 pnpm add / yarn add / bun add。

### @elastic/statistics 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @elastic/statistics 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @elastic/statistics 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=22。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @elastic/statistics 可以商用吗？

@elastic/statistics 使用 Elastic-2\.0 许可。声明的许可为 Elastic-2\.0，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @elastic/statistics 的下载量有多少？

根据 npm 官方下载统计，@elastic/statistics 在 2026/10/14 – 2026/11/12 这 30 天内被下载 129,038 次，日均约 4,301 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @elastic/statistics 还在维护吗？

最新版本 0\.0\.4 发布于 2026/11/05（8 天前）；至今共发布 5 个版本，其中过去 12 个月发布了 5 个；首次发布于 2026/09/24。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

