please sanity check my function for calculating Kendall’s tau
Hi, I’m trying to make my own function for calculating Kendall’s tau in a practice purpose, although I know there is Matlab function ( corr(A, ‘type’, ‘Kendall’) ). But I get two different answers when I run the Matlab function and my function. I don’t know the reason…
Here are example verctors:
xx = [1 0 1 1 0 1];
yy = [0 1 1 1 1 0];
Matlab corr function gives me -0.5, but my own function below gives me -0.26667
function taua=myKendall(a,b)
%% preparations
a=a(:);b=b(:);
validEntryIs = ~isnan(a)&~isnan(b);
a=a(validEntryIs);b=b(validEntryIs);
n=size(a,1);
%% compute Kendall rank correlation coefficient tau-a
K = 0;
for k = 1:n-1
pairRelations_a=sign(a(k)-a(k+1:n));
pairRelations_b=sign(b(k)-b(k+1:n));
K = K + sum(pairRelations_a.*pairRelations_b);
end
taua=K/(n*(n-1)/2); % normalise by the total number of pairs
end%functionHi, I’m trying to make my own function for calculating Kendall’s tau in a practice purpose, although I know there is Matlab function ( corr(A, ‘type’, ‘Kendall’) ). But I get two different answers when I run the Matlab function and my function. I don’t know the reason…
Here are example verctors:
xx = [1 0 1 1 0 1];
yy = [0 1 1 1 1 0];
Matlab corr function gives me -0.5, but my own function below gives me -0.26667
function taua=myKendall(a,b)
%% preparations
a=a(:);b=b(:);
validEntryIs = ~isnan(a)&~isnan(b);
a=a(validEntryIs);b=b(validEntryIs);
n=size(a,1);
%% compute Kendall rank correlation coefficient tau-a
K = 0;
for k = 1:n-1
pairRelations_a=sign(a(k)-a(k+1:n));
pairRelations_b=sign(b(k)-b(k+1:n));
K = K + sum(pairRelations_a.*pairRelations_b);
end
taua=K/(n*(n-1)/2); % normalise by the total number of pairs
end%function Hi, I’m trying to make my own function for calculating Kendall’s tau in a practice purpose, although I know there is Matlab function ( corr(A, ‘type’, ‘Kendall’) ). But I get two different answers when I run the Matlab function and my function. I don’t know the reason…
Here are example verctors:
xx = [1 0 1 1 0 1];
yy = [0 1 1 1 1 0];
Matlab corr function gives me -0.5, but my own function below gives me -0.26667
function taua=myKendall(a,b)
%% preparations
a=a(:);b=b(:);
validEntryIs = ~isnan(a)&~isnan(b);
a=a(validEntryIs);b=b(validEntryIs);
n=size(a,1);
%% compute Kendall rank correlation coefficient tau-a
K = 0;
for k = 1:n-1
pairRelations_a=sign(a(k)-a(k+1:n));
pairRelations_b=sign(b(k)-b(k+1:n));
K = K + sum(pairRelations_a.*pairRelations_b);
end
taua=K/(n*(n-1)/2); % normalise by the total number of pairs
end%function correlation, kendall’s tau MATLAB Answers — New Questions