comparison m-toolbox/classes/+utils/@math/crank.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % CRANK calculate ranks for Spearman correlation
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % Given the data series w
4 % Calculate:
5 % rw: the ranks (ties are treated by midranking)
6 % s: the sum(fk^3 - fk), where fk are the number of kth group of ties
7 %
8 % References:
9 % [1] W. H. Press, S. A. Teukolsky, W. T. Vetterling, B. P. Flannery,
10 % Numerical Recipes 3rd Edition: The Art of Scientific Computing,
11 % Cambridge University Press; 3 edition (September 10, 2007)
12 %
13 %
14 % L Ferraioli 06-12-2010
15 %
16 % $Id: crank.m,v 1.1 2011/03/07 10:46:53 luigi Exp $
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18 function [rw,s] = crank(w)
19
20 [sw,idx] = sort(w);
21
22 n = numel(sw);
23 s = 0;
24 jj = 2;
25
26 while jj < n+1
27 if sw(jj) ~= sw(jj-1);
28 sw(jj-1) = jj-1;
29 jj = jj+1;
30 else
31 jt = jj+1;
32 while jt<=n+1 && sw(jt-1)==sw(jj-1)
33 jt = jt + 1;
34 end
35 rnk = 0.5*(jj+jt-3);
36 for ji=jj:jt-1
37 sw(ji-1)=rnk;
38 end
39 t = jt-jj;
40 s = s + (t*t*t-t);
41 jj = jt;
42 end
43 end
44
45 if jj==n+1
46 sw(n)=n+1;
47 end
48
49 rw = zeros(size(sw));
50 for ii=1:numel(sw)
51 rw(idx(ii)) = sw(ii);
52 end
53
54 end