Mercurial > hg > ltpda
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/+utils/@math/crank.m Wed Nov 23 19:22:13 2011 +0100 @@ -0,0 +1,54 @@ +% CRANK calculate ranks for Spearman correlation +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Given the data series w +% Calculate: +% rw: the ranks (ties are treated by midranking) +% s: the sum(fk^3 - fk), where fk are the number of kth group of ties +% +% References: +% [1] W. H. Press, S. A. Teukolsky, W. T. Vetterling, B. P. Flannery, +% Numerical Recipes 3rd Edition: The Art of Scientific Computing, +% Cambridge University Press; 3 edition (September 10, 2007) +% +% +% L Ferraioli 06-12-2010 +% +% $Id: crank.m,v 1.1 2011/03/07 10:46:53 luigi Exp $ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +function [rw,s] = crank(w) + + [sw,idx] = sort(w); + + n = numel(sw); + s = 0; + jj = 2; + + while jj < n+1 + if sw(jj) ~= sw(jj-1); + sw(jj-1) = jj-1; + jj = jj+1; + else + jt = jj+1; + while jt<=n+1 && sw(jt-1)==sw(jj-1) + jt = jt + 1; + end + rnk = 0.5*(jj+jt-3); + for ji=jj:jt-1 + sw(ji-1)=rnk; + end + t = jt-jj; + s = s + (t*t*t-t); + jj = jt; + end + end + + if jj==n+1 + sw(n)=n+1; + end + + rw = zeros(size(sw)); + for ii=1:numel(sw) + rw(idx(ii)) = sw(ii); + end + +end \ No newline at end of file