Engee documentation

var

The variance.

Library

EngeeDSP

Syntax

Function call

  • V,M = var(A) — returns the variance V array elements A according to the first dimension, the size of which is not equal to 1. By default, the variance is normalized with respect to the number N-1, where N — the number of values. It also returns the mathematical expectation. M elements A, used to calculate the variance. If Vweighted variance, then Mweighted average.

    • If A is a vector of quantities, then V — a scalar.

    • If A is a matrix whose columns are random variables and rows are scalar quantities, then V — a row vector containing the variance corresponding to each column.

    • If A — a multidimensional array, then var(A) it acts on the first dimension of an array, the size of which is not equal to 1 considering the elements as vectors. Size V in this dimension , it becomes equal to 1, while the dimensions of all other dimensions remain the same as in A.

    • If A — a scalar, then V equal to 0.

    • If A — an empty array of size 0 on 0, the value of V equally NaN.

  • V,M = var(A,w) — defines the weight scheme. By w = 0 (by default) the variance has a normalization factor N-1, where N — the number of values. By w = 1 The variance has a normalization coefficient equal to the number of values. Argument w It can also be a weight vector containing non-negative elements. In this case, the length is w It must be equal to the length of the measurement it is working on. var.

  • V,M = var(A,w,"all") — returns the variance for all elements A When w equally 0 or 1.

  • V,M = var(A,w,dim) — returns the variance of the measurement dim. To keep the default normalization when specifying the operation dimension, set w = 0 in the second argument.

  • V,M = var(A,w,vecdim) — returns the variance of the measurements specified in the vector vecdim When w equally 0 or 1. For example, if A — the matrix, then var(A,0,[1 2]) returns the variance for all elements in A since each element of the matrix is contained in a slice of the array defined by the dimensions 1 and 2.

  • V,M = var(___,nanflag) — determines whether to include or exclude values NaN in A for any of the previous syntax options. For example, var(A,"omitnan") ignores values NaN when calculating the variance. By default var includes values NaN.

Arguments

Input arguments

# A — input array

+ vector | the matrix | multidimensional array

Details

An input array specified as a vector, matrix, or multidimensional array. If A — a scalar, then var(A) returns 0. If A — an empty array of size 0 on 0 Then var(A) returns NaN.

Типы данных

Float32, Float64

Support for complex numbers

Yes

# w — weight

+ 0 (default) | 1 | vector

Details

The weight set by one of the following values:

  • 0 — normalize by N-1, where N — the number of values. If there is only one value, the weight is 1.

  • 1 — normalize by N.

  • A vector consisting of non-negative scalar weights corresponding to the dimension A, which is used to calculate the variance.

Типы данных

Float32, Float64

# dim — the measurement for which the operation is performed

+ a positive integer scalar

Details

The dimension on which the operation is performed is specified as a positive integer scalar. If no dimension is specified, the first dimension of the array is used by default, the size of which is not equal to 1.

Argument dim specifies a dimension whose length is reduced to 1. size(S,dim) equally 1, while the dimensions of all other dimensions remain the same.

Consider the input matrix A size m on n:

  • var(A,0,1) calculates the variance of the elements in each column of the matrix A and returns a vector string of the size 1 on n.

    var 1

  • var(A,0,2) calculates the variance of the elements in each row of the matrix A and returns a column vector of size m on 1.

    var 2

If dim more ndims(A) Then var(A) returns an array of zeros of the same size as A.

# vecdim — measurement vector

+ a vector of positive integers

Details

A vector of dimensions defined as a vector of positive integers. Each element represents a dimension of the input array. The length of the output data in the specified operating measurements is 1, while the other dimensions remain unchanged.

Consider the input array A size 2×3×3. Then var(A,0,[1 2]) returns an array of size 1×1×3, the elements of which represent the variances calculated for each layer A.

std 3 en

# nanflag — condition for missing value

+ "includemissing" (by default) | "includenan" | "omitmissing" | "omitnan"

Details

The condition for missing a value set by one of the following values:

  • "includemissing" or "includenan" — enable values NaN in A when calculating the variance. If any element in the working dimension is equal to NaN, then the corresponding element in V is also equal to NaN. "includemissing" and "includenan" they behave the same way.

  • "omitmissing" or "omitnan" — ignore the values NaN in A and w and calculate the variance for fewer points. If all the elements in the working dimension are equal NaN, then the corresponding element in V is also equal to NaN. "omitmissing" and "omitnan" they behave the same way.

Output arguments

# V is the variance

+ scalar | vector | the matrix | multidimensional array

Details

The variance returned as a scalar, vector, matrix, or multidimensional array.

  • If A is a vector of quantities, then V — a scalar.

  • If A is a matrix whose columns are random variables and rows are scalar quantities, then V — a row vector containing the variance corresponding to each column.

  • If A — a multidimensional array, then var(A) it acts on the first dimension of an array, the size of which is not equal to 1 considering the elements as vectors. Size V in this dimension , it becomes equal to 1, while the dimensions of all other dimensions remain the same as in A.

  • If A — a scalar, then V equal to 0.

  • If A — an empty array of size 0 on 0 Then V equal to NaN.

# M — mathematical expectation

+ scalar | vector | the matrix | multidimensional array

Details

The mathematical expectation returned as a scalar, vector, matrix, or multidimensional array.

  • If A is a vector of quantities, then M — a scalar.

  • If A is a matrix whose columns are random variables and rows are scalar quantities, then M — vector is a row containing the mathematical expectation for each column.

  • If A — a multidimensional array, then var(A) it acts on the first dimension of an array, the size of which is not equal to 1 considering the elements as vectors. Size M in this dimension , it becomes equal to 1, while the dimensions of all other dimensions remain the same as in A.

  • If A — a scalar, then M equally A.

  • If A — an empty array of size 0 on 0 Then M equally NaN.

If V — weighted variance, then M — weighted average.

Examples

Matrix variance

Details

Let’s create a matrix and calculate its variance.

import EngeeDSP.Functions: var

A = [4 -7 3; 1 4 -2; 10 7 9]
var(A).V
1×3 Matrix{Float64}:
 21.0  54.3333  30.3333

Array variance

Details

Let’s create a three-dimensional array and calculate its variance.

import EngeeDSP.Functions: var

A = cat([1 3; 8 4], [3 -4; 1 2], dims=3)
var(A).V
1×2×2 Array{Float64, 3}:
[:, :, 1] =
 24.5  0.5

[:, :, 2] =
 2.0  18.0

Specifying the weight vector of the variance

Details

Let’s create a matrix and calculate its variance according to the weight vector w.

import EngeeDSP.Functions: var

A = [5 -4 6; 2 3 9; -1 1 2]
w = [0.5 0.25 0.25]
var(A, w).V
1×3 Matrix{Float64}:
 6.1875  9.5  6.1875

Specifying the dimension of the variance

Details

Let’s create a matrix and calculate its variance in the first dimension.

import EngeeDSP.Functions: var

A = [4 -2 1; 9 5 7]
var(A, 0, 1).V
1×3 Matrix{Float64}:
 12.5  24.5  18.0

Calculate the variance But in the second dimension.

var(A, 0, 2).V
2×1 Matrix{Float64}:
 9.0
 4.0

Variance of the array layer

Details

Let’s create a three-dimensional array and calculate the variance for each data layer (rows and columns).

import EngeeDSP.Functions: var

A = cat([2 4; -2 1], [9 13; -5 7], [4 4; 8 -3], dims=3)
V = var(A, 0, [1 2]).V
1×1×3 Array{Float64, 3}:
[:, :, 1] =
 6.25

[:, :, 2] =
 60.0

[:, :, 3] =
 20.916666666666664

Variance without missing values

Details

Create a matrix containing the values NaN.

A = [1.77 NaN -2.95; NaN 0.34 0.19]
2×3 Matrix{Float64}:
   1.77  NaN     -2.95
 NaN       0.34   0.19

Calculate the variance of the matrix, excluding the values NaN. For matrix columns containing any value NaN, function var calculates values other than NaN.

import EngeeDSP.Functions: var

V = var(A, "omitnan").V
1×3 Matrix{Float64}:
 0.0  0.0  4.9298

Variance and mathematical expectation

Details

Let’s create a matrix and calculate the variance and mathematical expectation for each column.

import EngeeDSP.Functions: var

A = [4 -7 3; 1 4 -2; 10 7 9]
V, M = var(A)
(V = [21.0 54.33333333333334 30.333333333333336], M = [5.0 1.3333333333333333 3.3333333333333335])

Let’s create a matrix and calculate the weighted variance and weighted average of each column according to the weight vector w.

A = [5 -4 6; 2 3 9; -1 1 2]
w = [0.5 0.25 0.25]
V, M = var(A, w)
(V = [6.1875 9.5 6.1875], M = [2.75 -1.0 5.75])

Additional Info

Variance

Details

For a random variable vector , consisting of for scalar quantities, the variance is defined as

where — mathematical expectation :

Some definitions of variance use a normalization factor. Instead of . A normalization factor can be used by specifying the weight 1, which gives the second moment of the sample relative to its mathematical expectation.

Regardless of the normalization coefficient of variance, it is assumed that the mathematical expectation has a normalization coefficient .

Weighted variance

Details

For the vector of finite length, consisting of scalar quantities, and a weight scheme , the weighted variance is defined as

where weighted average .

Weighted average

Details

For the vector of finite length, consisting of scalar quantities, and a weight scheme , the weighted average is defined as