Mercurial > octave
annotate scripts/statistics/corr.m @ 30564:796f54d4ddbf stable
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2021.
In all .txi and .texi files except gpl.txi and gpl.texi in the
doc/liboctave and doc/interpreter directories, change the copyright
to "Octave Project Developers", the same as used for other source
files. Update copyright notices for 2022 (not done since 2019). For
gpl.txi and gpl.texi, change the copyright notice to be "Free Software
Foundation, Inc." and leave the date at 2007 only because this file
only contains the text of the GPL, not anything created by the Octave
Project Developers.
Add Paul Thomas to contributors.in.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 Dec 2021 18:22:40 -0500 |
parents | 7854d5752dd2 |
children | 5d3faba0342e |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 1996-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
7 ## |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
8 ## This file is part of Octave. |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
9 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
14 ## |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
15 ## Octave is distributed in the hope that it will be useful, but |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
19 ## |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
20 ## You should have received a copy of the GNU General Public License |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24512
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
25 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## @deftypefn {} {} corr (@var{x}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## @deftypefnx {} {} corr (@var{x}, @var{y}) |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
29 ## Compute matrix of correlation coefficients. |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
30 ## |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
31 ## If each row of @var{x} and @var{y} is an observation and each column is |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
32 ## a variable, then the @w{(@var{i}, @var{j})-th} entry of |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
33 ## @code{corr (@var{x}, @var{y})} is the correlation between the |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
34 ## @var{i}-th variable in @var{x} and the @var{j}-th variable in @var{y}. |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
35 ## @tex |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
36 ## $$ |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
37 ## {\rm corr}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) \, {\rm std}(y)} |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
38 ## $$ |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
39 ## @end tex |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
40 ## @ifnottex |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
41 ## |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
42 ## @example |
24511
4f0e6ee6c9b8
Make documentation Sec 26.1 more consistent and Sec 25.4 clearer (bug #52685)
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
23219
diff
changeset
|
43 ## corr (@var{x},@var{y}) = cov (@var{x},@var{y}) / (std (@var{x}) * std (@var{y})) |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
44 ## @end example |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
45 ## |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
46 ## @end ifnottex |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
47 ## If called with one argument, compute @code{corr (@var{x}, @var{x})}, |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
48 ## the correlation between the columns of @var{x}. |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
49 ## @seealso{cov} |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
50 ## @end deftypefn |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
51 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
52 function retval = corr (x, y = []) |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
53 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27985
diff
changeset
|
54 if (nargin < 1) |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
55 print_usage (); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
56 endif |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
57 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
58 ## Input validation is done by cov.m. Don't repeat tests here |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
59 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
60 ## Special case, scalar is always 100% correlated with itself |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
61 if (isscalar (x)) |
14336
34af9f9ff98b
Use Octave coding conventions (double-quote " in preference to single quote ')
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
62 if (isa (x, "single")) |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
63 retval = single (1); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
64 else |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
65 retval = 1; |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
66 endif |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
67 return; |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
68 endif |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
69 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
70 ## No check for division by zero error, which happens only when |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
71 ## there is a constant vector and should be rare. |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
72 if (nargin == 2) |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
73 c = cov (x, y); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
74 s = std (x)' * std (y); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
75 retval = c ./ s; |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
76 else |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
77 c = cov (x); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
78 s = sqrt (diag (c)); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
79 retval = c ./ (s * s'); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
80 endif |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
81 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
82 endfunction |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
83 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
84 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
85 %!test |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
86 %! x = rand (10); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
87 %! cc1 = corr (x); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
88 %! cc2 = corr (x, x); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
89 %! assert (size (cc1) == [10, 10] && size (cc2) == [10, 10]); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
90 %! assert (cc1, cc2, sqrt (eps)); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
91 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
92 %!test |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
93 %! x = [1:3]'; |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
94 %! y = [3:-1:1]'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
95 %! assert (corr (x, y), -1, 5*eps); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
96 %! assert (corr (x, flipud (y)), 1, 5*eps); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
97 %! assert (corr ([x, y]), [1 -1; -1 1], 5*eps); |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
98 |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
99 %!test |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
100 %! x = single ([1:3]'); |
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
101 %! y = single ([3:-1:1]'); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
102 %! assert (corr (x, y), single (-1), 5*eps); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
103 %! assert (corr (x, flipud (y)), single (1), 5*eps); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
104 %! assert (corr ([x, y]), single ([1 -1; -1 1]), 5*eps); |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
105 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
106 %!assert (corr (5), 1) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
107 %!assert (corr (single (5)), single (1)) |
12856
cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
108 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
109 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
110 %!error <Invalid call> corr () |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
111 %!error corr ([1; 2], ["A", "B"]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
112 %!error corr (ones (2,2,2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14336
diff
changeset
|
113 %!error corr (ones (2,2), ones (2,2,2)) |