Thread Subject: cov function gives wrong covariance matrix

Subject: cov function gives wrong covariance matrix

From: Martin Kolar

Date: 9 Jan, 2012 12:09:09

Message: 1 of 3

When trying to reconstruct the covariance matrix from data, I'm getting a wrong estimate using the cov function. Should I report this as a major bug? How can I get the right covariance matrix?

The example below demonstrates the problem:
Mu = [3 ;0]
Sigma = [ 1.0000 -0.6195; -0.6195 1.0000]

%get samples
for i=1:1000
    standard_random_vector(:,i) = randn(2,1);
    normal_random_vector(:,i) = Sigma * standard_random_vector(:,i) + Mu;
end

Mu2=mean(normal_random_vector')'
Sigma2 = cov(normal_random_vector')

for i=1:1000
    reconstructed_vector(:,i)=Sigma2*standard_random_vector(:,i)+Mu2;
end

plot(normal_random_vector(1,:),normal_random_vector(2,:),'r.')
hold on
plot(reconstructed_vector(1,:),reconstructed_vector(2,:),'.')
legend('original','reconstructed')

Subject: cov function gives wrong covariance matrix

From: Rune Allnor

Date: 9 Jan, 2012 12:15:06

Message: 2 of 3

On 9 Jan, 13:09, "Martin Kolar" <martasko...@gmail.com> wrote:
> When trying to reconstruct the covariance matrix from data, I'm getting a wrong estimate using the cov function. Should I report this as a major bug?

There is nothing to stop you, but before you
do that, do take some timne to consider
questions like:

- How many people are likely to use this function?
- For how long has that function been supplied
  with matlab?
- If that many have used the function for that
  long time, how come this 'bug' is still there?
- Could it be that *you* might have missed something?

> How can I get the right covariance matrix?

You can't. The best you can do is to get an
estimate for the covariance matrix.

Rune

Subject: cov function gives wrong covariance matrix

From: Torsten

Date: 9 Jan, 2012 12:37:52

Message: 3 of 3

On 9 Jan., 13:09, "Martin Kolar" <martasko...@gmail.com> wrote:
> When trying to reconstruct the covariance matrix from data, I'm getting a wrong estimate using the cov function. Should I report this as a major bug? How can I get the right covariance matrix?
>
> The example below demonstrates the problem:
> Mu = [3 ;0]
> Sigma =  [ 1.0000   -0.6195;  -0.6195    1.0000]
>
> %get samples
> for i=1:1000
>     standard_random_vector(:,i) = randn(2,1);
>     normal_random_vector(:,i) = Sigma * standard_random_vector(:,i) + Mu;

You must take the square root of Sigma here.
Take a look at example 2 under
http://www.mathworks.de/help/techdoc/ref/randn.html

> end
>
> Mu2=mean(normal_random_vector')'
> Sigma2 = cov(normal_random_vector')
>
> for i=1:1000
>     reconstructed_vector(:,i)=Sigma2*standard_random_vector(:,i)+Mu2;
> end
>
> plot(normal_random_vector(1,:),normal_random_vector(2,:),'r.')
> hold on
> plot(reconstructed_vector(1,:),reconstructed_vector(2,:),'.')
> legend('original','reconstructed')

Best wishes
Torsten.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
normal Martin Kolar 9 Jan, 2012 07:18:06
covariance Martin Kolar 9 Jan, 2012 07:17:59
cov Martin Kolar 9 Jan, 2012 07:17:51
rssFeed for this Thread

Contact us at files@mathworks.com