[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cov.m function behaves different from Matlab

From: Nicola De Quattro
Subject: Re: cov.m function behaves different from Matlab
Date: Thu, 26 Mar 2009 14:49:43 +0100

> Why in the output the matrix size is different and result is also different? 
> I am using Octave 3.0.2

The problem is that the two commands are different because of the
different interpretation of input. You can understand this reading the
help for both softwares:

1) Under Octave:

octave:7> help cov
 -- Function File:  cov (X, Y)
     Compute covariance.

     If each row of X and Y is an observation and each column is a
     variable, the (I, J)-th entry of `cov (X, Y)' is the covariance
     between the I-th variable in X and the J-th variable in Y.  If
     called with one argument, compute `cov (X, X)'.

2) Under Matlab

>> help cov
 COV Covariance matrix.
    COV(X), if X is a vector, returns the variance.  For matrices,
    where each row is an observation, and each column a variable,
    COV(X) is the covariance matrix.  DIAG(COV(X)) is a vector of
    variances for each column, and SQRT(DIAG(COV(X))) is a vector
    of standard deviations. COV(X,Y), where X and Y are matrices with
    the same number of elements, is equivalent to COV([X(:) Y(:)]).

    COV(X) or COV(X,Y) normalizes by (N-1) if N>1, where N is the number of
    observations.  This makes COV(X) the best unbiased estimate of the
    covariance matrix if the observations are from a normal distribution.
    For N=1, COV normalizes by N.

    COV(X,1) or COV(X,Y,1) normalizes by N and produces the second
    moment matrix of the observations about their mean.  COV(X,Y,0) is
    the same as COV(X,Y) and COV(X,0) is the same as COV(X).

    The mean is removed from each column before calculating the

Theoretically both are correct but Octave allows you to have the same
results of Matlab (vice versa isn't true).
You can force the Octave results by computing

octave:9> cov([x(:) y(:)])
ans =

   6.1720e+04  -5.0283e+02
  -5.0283e+02   5.0300e+02

as suggested in the Matlab help.

So, from this point of view, Octave is more versatile than Matlab ;)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]