compute.ndarray.var¶
Summary¶
compute.ndarray.var measures variance over selected axes. Use compute.ndarray.std for its square root. For complex input, variance uses the squared magnitude of each difference from the complex mean.
Syntax¶
var<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayVarianceOptions): T
std<T extends CPUndarray | GPUndarray>(a: T, options?: NdarrayVarianceOptions): T
const sampleVariance = nd.var(a, { ddof: 1 });
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
a |
CPUndarray | GPUndarray |
Yes | Live real or complex input. |
options |
NdarrayVarianceOptions |
No | Reduction axes, NaN policy, divisor adjustment, output dtype and GPU settings. |
Returns¶
T - Packed real result in the input residency. The dtype defaults to f64 for f64 or c128 input and to f32 otherwise. Only those two real output types are allowed. GPU variance and standard deviation reject f64 and c128 inputs and require f32 output.
Type Details¶
ddof is a finite non-negative number and defaults to zero. The divisor is n - ddof, where n counts the included elements. A non-positive divisor or an empty slice produces NaN. With nan: "omit", an element with either component NaN is excluded. The default propagates NaN.
By default all axes are reduced and removed. Supply axes to select dimensions or set keepDims to retain those dimensions at length one. See compute.ndarray.sum for shared axis, limit and GPU settings.
Example¶
const wgpu = await WasmGPU.create(document.querySelector("canvas"));
const nd = wgpu.compute.ndarray;
const a = nd.fromArray("f32", [3], [1, 2, 3]);
const variance = nd.var(a, { ddof: 1 });
console.log(variance.get()); // 1
variance.destroy();
a.destroy();