Skip to content
fn

var

→Tensor
var(input: Tensor, dim: _int | Sequence[_int] | None = None, unbiased: _bool | None = None, keepdim: _bool = False, correction: _int | _float | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Compute the variance along dim.

The sum of squared deviations from the mean, divided by max⁡(n−c,0)\max(n - c, 0) for n reduced elements and correction cc. correction=1 (the default) is Bessel's unbiased sample variance; correction=0 the population variance.

Parameters

inputTensor
Input tensor.
dimint or sequence of int= None
Dimension(s) to reduce. None reduces every element. A dimension named twice is refused.
unbiasedbool= None
True is correction=1, False correction=0; it overrides correction when given. A bool in the place of dim — var(x, False) — is read as this.
keepdimbool= False
Whether to retain reduced dims with size 1.
correctionint or float= None
Degrees-of-freedom correction cc. Default 1.

Returns

Tensor

Reduced floating-point tensor.

Raises

ValueError
If dim names one dimension twice.

Notes

σ2  =  1max⁡(n−c,0)∑i=1n(xi−xˉ)2.\sigma^2 \;=\; \frac{1}{\max(n - c, 0)} \sum_{i=1}^{n} (x_i - \bar{x})^2 .

With n≤cn \le c the divisor is 0: a slice with no spread gives nan and one with spread inf — so var of a single element is nan under the default correction, as is any empty reduction.

Examples

>>> import lucid
>>> x = lucid.tensor([[1.0, 2.0, 3.0, 4.0], [2.0, 4.0, 6.0, 8.0]])
>>> lucid.var(x, 1)
tensor([1.667, 6.667])
>>> lucid.var(x, 1, False, True)
tensor([[1.25], [5.]])