IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Mojo function

variance

def variance[dtype: DType](src: Span[Scalar[dtype]], mean_value: Scalar[dtype], correction: Int = Int(1)) -> Scalar[dtype]

Given a mean, computes the variance of elements in a buffer.

The mean value is used to avoid a second pass over the data:

variance(x) = sum((x - E(x))^2) / (size - correction)

Parameters:

  • ​dtype (DType): The dtype of the input.

Args:

  • ​src (Span[Scalar[dtype]]): The buffer.
  • ​mean_value (Scalar[dtype]): The mean value of the buffer.
  • ​correction (Int): Normalize variance by size - correction.

Returns:

Scalar[dtype]: The variance value of the elements in a buffer.

Raises:

If the operation fails.

def variance[dtype: DType, input_fn_1d: def[dtype_: DType, width: Int](idx: Int) capturing thin -> SIMD[dtype_, width]](length: Int, mean_value: Scalar[dtype], correction: Int = Int(1)) -> Scalar[dtype]

Computes the variance of values generated by a function.

Variance is calculated as:

variance⁑(X)=βˆ‘i=0lengthβˆ’1(Xiβˆ’E⁑(Xi))2sizeβˆ’correction\operatorname{variance}(X) = \frac{ \sum_{i=0}^{length-1} (X_i - \operatorname{E}(X_i))^2}{size - correction}

where E represents the deviation of a sample from the mean.

This version takes the mean value as an argument to avoid a second pass over the data.

Parameters:

  • ​dtype (DType): The data type of the elements.
  • ​input_fn_1d (def[dtype_: DType, width: Int](idx: Int) capturing thin -> SIMD[dtype_, width]): A function that generates SIMD values at each index.

Args:

  • ​length (Int): The number of elements.
  • ​mean_value (Scalar[dtype]): The pre-computed mean value.
  • ​correction (Int): Normalize variance by size - correction (default: 1 for sample variance).

Returns:

Scalar[dtype]: The variance value.

Raises:

If length is less than or equal to correction.

def variance[dtype: DType](src: Span[Scalar[dtype]], correction: Int = Int(1)) -> Scalar[dtype]

Computes the variance value of the elements in a buffer.

variance(x) = sum((x - E(x))^2) / (size - correction)

Parameters:

  • ​dtype (DType): The dtype of the input.

Args:

  • ​src (Span[Scalar[dtype]]): The buffer.
  • ​correction (Int): Normalize variance by size - correction (Default=1).

Returns:

Scalar[dtype]: The variance value of the elements in a buffer.

Raises:

If the operation fails.

def variance[dtype: DType, input_fn_1d: def[dtype_: DType, width: Int](idx: Int) capturing thin -> SIMD[dtype_, width]](length: Int, correction: Int = Int(1)) -> Scalar[dtype]

Computes the variance of values generated by a function.

This version computes the mean automatically in a first pass.

Parameters:

  • ​dtype (DType): The data type of the elements.
  • ​input_fn_1d (def[dtype_: DType, width: Int](idx: Int) capturing thin -> SIMD[dtype_, width]): A function that generates SIMD values at each index.

Args:

  • ​length (Int): The number of elements.
  • ​correction (Int): Normalize variance by size - correction (default: 1 for sample variance).

Returns:

Scalar[dtype]: The variance value.

Raises:

If length is less than or equal to correction.