1

In MATLAB there is a function called cov. If I insert a matrix X into cov like this cov(X), then cov will return a square matrix of covariance.

My question is very simple: How can I, with MATLAB, plot that matrix cov(X) onto a 2D plot like this. I can see a lot of covariance matrix plots at Google. But how do they create them?

enter image description here

euraad
  • 2,467
  • 5
  • 30
  • 51
  • Is your matrix 2x2? – James Tursa Jan 17 '22 at 20:33
  • What do you mean by "plotting the matrix"? Do you mean that you want the eigenvectors pointing along the cloud, [like this](https://en.wikipedia.org/wiki/Covariance_matrix#/media/File:GaussianScatterPCA.svg)? If not, perhaps you could link to an example of what you're talking about. – Ben Grossmann Jan 17 '22 at 21:00
  • @BenGrossmann I don't really know. But I have seen people plotting a matrix as it was on a 2D plot. So I guess it's some kind of principal component. – euraad Jan 18 '22 at 10:59
  • @MrYui Again, it would be helpful if you could verify whether the link in my previous comment has the kind of plot that you're talking about and, if it is not, give another link that has the kind of graph you're looking for. – Ben Grossmann Jan 18 '22 at 13:17
  • @BenGrossmann Here for example https://www.visiondummy.com/2014/04/geometric-interpretation-covariance-matrix/ – euraad Jan 18 '22 at 13:31
  • @MrYui Okay, so it seems like I was on the right track – Ben Grossmann Jan 18 '22 at 14:33
  • @BenGrossmann Yes! So in this case, you plot the columns of the covariance matrix `X`. I assume that the only way to display a 2D matrix at a 2D plot. – euraad Jan 18 '22 at 14:34
  • It is not the *only way* to display a matrix in a 2D plot, but it is the most common way to do so and it is a meaningful way to do so in this context. – Ben Grossmann Jan 18 '22 at 14:36
  • @BenGrossmann I don't know how I can compare two matricies with each other. But with PCA, I can see the differences between two scatter plots. That's the reason why I asked this question. If I can compare two matricies with each other...some smart and robust way, then I have found my solution. – euraad Jan 18 '22 at 14:39
  • Well if you plot two matrices in this way, then you can indeed see the difference between them. Two covariant matrices will be the same if and only if they have the same eigenvalues and eigenvectors – Ben Grossmann Jan 18 '22 at 14:56
  • @BenGrossmann Hmm...can I see similarities between two covariance matrices even if they are not the same, by using eigenvalues and eigenvectors? – euraad Jan 18 '22 at 14:57
  • @BenGrossmann Or should I use colleration matrices instead? – euraad Jan 18 '22 at 14:58
  • @MrYui Yes, that's right. Something similar with correlation matrices probably would also work – Ben Grossmann Jan 18 '22 at 16:53

1 Answers1

1

My best guess is that you're trying to add the principal components to the plot. To do that, you could do something like this.

%% generate data points

S_tru = [2 1; 1 1];
N = 1000;
%% compute mean, covariance, principal components
X = mvnrnd([0,0],S_tru,N);
mu = mean(X);
S = cov(X);
[U,D] = eig(S);

%% specify base points/directions for arrows
base = [mu;mu];
vecs = sqrt(D)*U';
vecs = 2 * vecs;

%% plot
plot(X(:,1),X(:,2), 'r.')
axis equal
hold on
quiver(base(:,1),base(:,2),vecs(:,1),vecs(:,2),'blue','LineWidth',2)

Resulting graph:

covariance plot

Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16