> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 计算数据集的样本方差。与 `varSamp` 不同，此函数采用数值稳定的算法。虽然运行较慢，但计算误差更低。

# varSampStable

<h2 id="varSampStable">
  varSampStable
</h2>

引入版本：v1.1.0

计算数据集的样本方差。与 [`varSamp`](/reference/functions/aggregate-functions/varSamp) 不同，此函数采用[数值稳定](https://en.wikipedia.org/wiki/Numerical_stability)算法，运行速度较慢，但计算误差更小。

样本方差的计算公式与 [`varSamp`](/reference/functions/aggregate-functions/varSamp) 相同：

$$
\frac{\Sigma{(x - \bar{x})^2}}{n-1}
$$

<br />

其中：

* $x$ 为数据集中的各个数据点
* $\bar{x}$ 为数据集的算术平均值
* $n$ 为数据集中的数据点总数

**语法**

```sql theme={null}
varSampStable(x)
```

**参数**

* `x` — 需要计算样本方差的数据集。[`(U)Int*`](/reference/data-types/int-uint)、[`Float*`](/reference/data-types/float) 或 [`Decimal*`](/reference/data-types/decimal)

**返回值**

返回输入数据集的样本方差。[`Float64`](/reference/data-types/float)

**示例**

**计算数值稳定的样本方差**

```sql title=Query theme={null}
DROP TABLE IF EXISTS test_data;
CREATE TABLE test_data
(
    x Float64
)
ENGINE = Memory;

INSERT INTO test_data VALUES (10.5), (12.3), (9.8), (11.2), (10.7);

SELECT round(varSampStable(x),3) AS var_samp_stable FROM test_data;
```

```response title=Response theme={null}
┌─var_samp_stable─┐
│           0.865 │
└─────────────────┘
```
