Mercurial > hg > octave-lyh
comparison scripts/statistics/base/cor.m @ 11436:e151e23f73bc
Overhaul base statistics functions and documentation of same.
Add or improve input validation.
Add input validation tests.
Add functional tests.
Improve or re-write documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 03 Jan 2011 21:23:08 -0800 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
comparison
equal
deleted
inserted
replaced
11435:20f53b3a558f | 11436:e151e23f73bc |
---|---|
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} cor (@var{x}, @var{y}) | 21 ## @deftypefn {Function File} {} cor (@var{x}) |
22 ## Compute correlation. | 22 ## @deftypefnx {Function File} {} cor (@var{x}, @var{y}) |
23 ## Compute matrix of correlation coefficients. | |
23 ## | 24 ## |
24 ## The (@var{i}, @var{j})-th entry of @code{cor (@var{x}, @var{y})} is | 25 ## This is an alias for @code{corrcoef}. |
25 ## the correlation between the @var{i}-th variable in @var{x} and the | 26 ## @seealso{corrcoef} |
26 ## @var{j}-th variable in @var{y}. | |
27 ## | |
28 ## @tex | |
29 ## $$ | |
30 ## {\rm corrcoef}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)} | |
31 ## $$ | |
32 ## @end tex | |
33 ## @ifnottex | |
34 ## | |
35 ## @example | |
36 ## corrcoef(x,y) = cov(x,y)/(std(x)*std(y)) | |
37 ## @end example | |
38 ## | |
39 ## @end ifnottex | |
40 ## For matrices, each row is an observation and each column a variable; | |
41 ## vectors are always observations and may be row or column vectors. | |
42 ## | |
43 ## @code{cor (@var{x})} is equivalent to @code{cor (@var{x}, @var{x})}. | |
44 ## | |
45 ## Note that the @code{corrcoef} function does the same as @code{cor}. | |
46 ## @end deftypefn | 27 ## @end deftypefn |
47 | 28 |
48 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> | 29 function retval = cor (x, y = []) |
49 ## Description: Compute correlations | |
50 | |
51 function retval = cor (x, y) | |
52 | 30 |
53 if (nargin < 1 || nargin > 2) | 31 if (nargin < 1 || nargin > 2) |
54 print_usage (); | 32 print_usage (); |
55 endif | 33 endif |
56 | 34 |
57 if (nargin == 2) | 35 retval = corrcoef (x, y); |
58 c = cov (x, y); | |
59 s = std (x)' * std (y); | |
60 retval = c ./ s; | |
61 elseif (nargin == 1) | |
62 c = cov (x); | |
63 s = reshape (sqrt (diag (c)), 1, columns (c)); | |
64 retval = c ./ (s' * s); | |
65 endif | |
66 | 36 |
67 endfunction | 37 endfunction |
38 |