/usr/share/octave/packages/nan-2.5.9/cor.m is in octave-nan 2.5.9-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | function [r2] = cor(X,Y);
% COR calculates the correlation matrix
% X and Y can contain missing values encoded with NaN.
% NaN's are skipped, NaN do not result in a NaN output.
% (Its assumed that the occurence of NaN's is uncorrelated)
% The output gives NaN only if there are insufficient input data
%
% COR(X);
% calculates the (auto-)correlation matrix of X
% COR(X,Y);
% calculates the crosscorrelation between X and Y
%
% c = COR(...);
% c is the correlation matrix
%
% W weights to compute weighted mean (default: [])
% if W=[], all weights are 1.
% number of elements in W must match size(x,DIM)
% NOTE: Under certain circumstances (Missing values and small number of samples)
% abs(COR) can be larger than 1.
% If you need abs(COR)<=1, use CORRCOEF. CORRCOEF garantees abs(COR)<=1.
%
% see also: SUMSKIPNAN, COVM, COV, CORRCOEF
%
% REFERENCES:
% http://mathworld.wolfram.com/CorrelationCoefficient.html
% $Id: cor.m 8223 2011-04-20 09:16:06Z schloegl $
% Copyright (C) 2000-2004,2010 by Alois Schloegl <alois.schloegl@gmail.com>
% This function is part of the NaN-toolbox
% http://pub.ist.ac.at/~schloegl/matlab/NaN/
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; If not, see <http://www.gnu.org/licenses/>.
if nargin==1
Y = [];
elseif nargin==0
fprintf(2,'Error COR: Missing argument(s)\n');
end;
[r1,c1]=size(X);
if (c1>r1),
fprintf(2,'Warning COR: Covariance is ill-defined, because of too less observations (rows).\n');
end;
[r1,c1]=size(X);
if ~isempty(Y)
[r2,c2]=size(Y);
if r1~=r2,
fprintf(2,'Error COR: X and Y must have the same number of observations (rows).\n');
return;
end;
else
[r2,c2]=size(X);
end;
if (c1>r1) || (c2>r2),
fprintf(2,'Warning COR: Covariance is ill-defined, because of too less observations (rows).\n');
end;
if ~isempty(Y),
[S1,N1,SSQ1] = sumskipnan(X,1);
[S2,N2,SSQ2] = sumskipnan(Y,1);
NN = double(~isnan(X)')*double(~isnan(Y));
X(isnan(X)) = 0; % skip NaN's
Y(isnan(Y)) = 0; % skip NaN's
CC = X'*Y;
M1 = S1./N1;
M2 = S2./N2;
cc = CC./NN - M1'*M2;
r2 = cc./sqrt((SSQ1./N1-M1.*M1)'*(SSQ2./N2-M2.*M2));
else
[S,N,SSQ] = sumskipnan(X,1);
NN = double(~isnan(X)')*double(~isnan(X));
X(isnan(X)) = 0; % skip NaN's
CC = X'*X;
M = S./N;
cc = CC./NN - M'*M;
v = (SSQ./N- M.*M); %max(N-1,0);
r2 = cc./sqrt(v'*v);
end;
|