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:
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.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!