var
The variance.
| Library |
|
Syntax
Function call
-
V,M = var(A)— returns the varianceVarray elementsAaccording to the first dimension, the size of which is not equal to1. By default, the variance is normalized with respect to the numberN-1, whereN— the number of values. It also returns the mathematical expectation.MelementsA, used to calculate the variance. IfV— weighted variance, thenM— weighted average.-
If
Ais a matrix whose columns are random variables and rows are scalar quantities, thenV— a row vector containing the variance corresponding to each column. -
If
A— a multidimensional array, thenvar(A)it acts on the first dimension of an array, the size of which is not equal to1considering the elements as vectors. SizeVin this dimension , it becomes equal to1, while the dimensions of all other dimensions remain the same as inA. -
If
A— an empty array of size0on0, the value ofVequallyNaN.
-
V,M = var(A,w)— defines the weight scheme. Byw = 0(by default) the variance has a normalization factorN-1, whereN— the number of values. Byw = 1The variance has a normalization coefficient equal to the number of values. ArgumentwIt can also be a weight vector containing non-negative elements. In this case, the length iswIt must be equal to the length of the measurement it is working on.var.
-
V,M = var(A,w,vecdim)— returns the variance of the measurements specified in the vectorvecdimWhenwequally0or1. For example, ifA— the matrix, thenvar(A,0,[1 2])returns the variance for all elements inAsince each element of the matrix is contained in a slice of the array defined by the dimensions1and2.
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.
| Типы данных |
|
| Support for complex numbers |
Yes |
# w — weight
+
0 (default) | 1 | vector
Details
The weight set by one of the following values:
-
0— normalize byN-1, whereN— the number of values. If there is only one value, the weight is1. -
1— normalize byN. -
A vector consisting of non-negative scalar weights corresponding to the dimension
A, which is used to calculate the variance.
| Типы данных |
|
# 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 matrixAand returns a vector string of the size1onn. -
var(A,0,2)calculates the variance of the elements in each row of the matrixAand returns a column vector of sizemon1.
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.
# 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 valuesNaNinAwhen calculating the variance. If any element in the working dimension is equal toNaN, then the corresponding element inVis also equal toNaN."includemissing"and"includenan"they behave the same way. -
"omitmissing"or"omitnan"— ignore the valuesNaNinAandwand calculate the variance for fewer points. If all the elements in the working dimension are equalNaN, then the corresponding element inVis also equal toNaN."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
Ais a matrix whose columns are random variables and rows are scalar quantities, thenV— a row vector containing the variance corresponding to each column. -
If
A— a multidimensional array, thenvar(A)it acts on the first dimension of an array, the size of which is not equal to1considering the elements as vectors. SizeVin this dimension , it becomes equal to1, while the dimensions of all other dimensions remain the same as inA.
# M — mathematical expectation
+
scalar | vector | the matrix | multidimensional array
Details
The mathematical expectation returned as a scalar, vector, matrix, or multidimensional array.
-
If
Ais a vector of quantities, thenM— a scalar. -
If
Ais a matrix whose columns are random variables and rows are scalar quantities, thenM— vector is a row containing the mathematical expectation for each column. -
If
A— a multidimensional array, thenvar(A)it acts on the first dimension of an array, the size of which is not equal to1considering the elements as vectors. SizeMin this dimension , it becomes equal to1, while the dimensions of all other dimensions remain the same as inA. -
If
A— an empty array of size0on0ThenMequallyNaN.
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 .