# 矩阵统计(Matrix Stats)

**matrix\_stats**聚合是一个数字聚合，它会对一组文档字段计算以下统计数据：

| count       | 计算中每个字段样本的数量                 |
| ----------- | ---------------------------- |
| mean        | 每个字段的平均值。                    |
| variance    | 每个字段测量如何从mean 值展开样本。         |
| skewness    | 每个字段测量量化mean周围的不对称分布。        |
| kurtosis    | 每个字段测量量化分布的形状。               |
| covariance  | 一个矩阵描述一个字段与另一个相关联字段的变化。      |
| correlation | 协方差矩阵缩放到-1到1的范围。描述字段分布之间的关系。 |

以下示例演示了使用矩阵统计来描述收入与贫困之间的关系。

```
{
    "aggs": {
        "matrixstats": {
            "matrix_stats": {
                "fields": ["poverty", "income"]
            }
        }
    }
}
```

聚合类型是matrix\_stats，字段设置定义用于计算统计信息的字段集（作为数组）。上述请求返回以下响应：

```
{
    ...
    "aggregations": {
        "matrixstats": {
            "fields": [{
                "name": "income",
                "count": 50,
                "mean": 51985.1,
                "variance": 7.383377037755103E7,
                "skewness": 0.5595114003506483,
                "kurtosis": 2.5692365287787124,
                "covariance": {
                    "income": 7.383377037755103E7,
                    "poverty": -21093.65836734694
                },
                "correlation": {
                    "income": 1.0,
                    "poverty": -0.8352655256272504
                }
            }, {
                "name": "poverty",
                "count": 50,
                "mean": 12.732000000000001,
                "variance": 8.637730612244896,
                "skewness": 0.4516049811903419,
                "kurtosis": 2.8615929677997767,
                "covariance": {
                    "income": -21093.65836734694,
                    "poverty": 8.637730612244896
                },
                "correlation": {
                    "income": -0.8352655256272504,
                    "poverty": 1.0
                }
            }]
        }
    }
}
```

## Multi Value Fields

matrix\_stats聚合将每个文档字段视为独立样本。 mode参数控制聚合将用于数组或多值字段的数组值。此参数可以采取以下之一：

| avg    | （默认值）使用所有值的平均值。 |
| ------ | --------------- |
| min    | 选择最低值。          |
| max    | 选择最大值.          |
| sum    | 使用所有值的总和。       |
| median | 使用所有值的中值。       |

## Missing Values

缺少的参数定义了如何处理缺少值的文档。默认情况下，它们将被忽略，但也可以将它们视为具有值。这是通过添加一组fieldname：值映射来指定每个字段的默认值来完成的。

```
{
    "aggs": {
        "matrixstats": {
            "matrix_stats": {
                "fields": ["poverty", "income"],
                "missing": {"income" : 50000}  # 1
            }
        }
    }
}
```

| 1 | 收入字段中没有值的文档的默认值为50000。 |
| - | ---------------------- |

## Script

此聚合系列尚不支持脚本。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/364ju-zhen-ju-540828-matrix-aggregations/ju-zhen-tong-8ba128-matrix-stats.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
