computing Variance between vectors
26 次查看(过去 30 天)
显示 更早的评论
I want to compute the variance between vectors, it's result should be a scalar number but matlab command "var" giving me a vector result. let suppose I have two vector x1= [ 2 2 2]' , x2= [4 4 4]' and X= [x1; x2]. The command "var" giving the result of a vector as var(X(1:3,1:2),0,2) = [2 2 2]. but by varriance sample formula
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230649/image.png)
because the square of a vector does not mean square of all its elements. I think the square of the vector is equal to the dot product of a vector with its self which is scalar
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/230650/image.png)
Am I right?
0 个评论
回答(2 个)
Bruno Luong
2019-7-21
编辑:Bruno Luong
2019-7-21
No you are not right, the variance computes the square of the "dispersion" of the random variable. In case of random vector variable the variance formula applies on component by component independently and the result is the vector of the same length than the input.
The variance is actually the vector of diagonal terms of the covariance matrix, which computes also the cross correlation of the components of the variable.
For
x1= [2; 2; 2];
x2= [4; 4; 4];
X = [x1,x2]
the command
var(X,0,2)
works along the 2nd dimension for three identical row-vectors [2,4]. For all component
N = 2;
The expectation (mean) is
mu = (2+4)/N = 3
variance is
1/(N-1) * ((2-mu)^2 + (4-mu)^2)) =
1/1 * ((2-3)^2 + (4-3)^2) = 2
The result is [2,2,2].
The quantity 6 in this example you pretend to be variance is actually the trace of the covariance matrix, which is some time called "total variance".
3 个评论
Bruno Luong
2019-7-21
编辑:Bruno Luong
2019-7-21
"so X contains two column vectors "
So does mine. And your example has an error since you cocatenate vertically the vector [x1; x2].
"your computation for means as a scalar"
The mean is a scalar but for each individual row of X. The three rows is the same vector [2,4]. I started my explanation with "For all component..."
If you want a formula, the variance var(X,0,2) is simply a row vector v where
v(i) = std(X(i,:))^2
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!