deps/kb_meme/meme-5.0.1/src/spearman-rank-correlation.c
/*
 * 	spearman_rank_correlation:
 *		Returns the product moment correlation coefficient of the ranked input.
 */
Returns the product moment correlation coefficient of the ranked input.
Returns the product moment correlation coefficient of the ranked input .
double spearman_rank_correlation(
				int n,			
				double *x,			
				double *y			
				)
{
	int i;
	spearman_rank_t *sx, *sy; 
	double sumx, sumxsq, sumxy, sumy, sumysq;
	sumx = sumxsq = sumxy = sumy = sumysq = 0;
	sx = mm_malloc(n*sizeof(spearman_rank_t));
	sy = mm_malloc(n*sizeof(spearman_rank_t));
	for (i=0;i<n;i++) {
		sx[i].data = x[i];
		sy[i].data = y[i];
		sx[i].orig_rank = i;
		sy[i].orig_rank = i;
	}
	qsort(sx, n, sizeof(*sx), compare_spearman_rank_t_data);
	qsort(sy, n, sizeof(*sy), compare_spearman_rank_t_data);
	for (i=0;i<n;i++) {
		double rank = 0;
		int num_ties = 0;
		int j;
		for (j=i; j<n && sx[i].data == sx[j].data; j++) {
			rank += j+1; 
			num_ties++;
		}
		for (j=i; j<i+num_ties; j++) {
			sx[j].rank = rank / num_ties;
		}
		i += num_ties - 1; 
	}
	for (i=0;i<n;i++) {
		double rank = 0;
		int num_ties = 0;
		int j;
		for (j=i; j<n && sy[i].data == sy[j].data; j++) {
			rank += j+1; 
			num_ties++;
		}
		for (j=i; j<i+num_ties; j++) {
			sy[j].rank = rank / num_ties;
		}
		i += num_ties - 1; 
	}
	qsort(sx, n, sizeof(*sx), compare_spearman_rank_t_orig_rank);
	qsort(sy, n, sizeof(*sy), compare_spearman_rank_t_orig_rank);
	for (i=0;i<n;i++) {
		sumxy += sx[i].rank*sy[i].rank;
		sumx += sx[i].rank;
		sumy += sy[i].rank;
		sumxsq += sx[i].rank*sx[i].rank;
		sumysq += sy[i].rank*sy[i].rank;
	}
	free(sx);
	free(sy);
	return (n*sumxy - sumx*sumy) / (sqrt(n*sumxsq - sumx*sumx) * sqrt(n*sumysq - sumy*sumy));
}
double spearman_rank_correlation ( int n , double * x , double * y ) { int i ; spearman_rank_t * sx , * sy ; double sumx , sumxsq , sumxy , sumy , sumysq ; sumx = sumxsq = sumxy = sumy = sumysq = 0 ; sx = mm_malloc ( n * sizeof ( spearman_rank_t ) ) ; sy = mm_malloc ( n * sizeof ( spearman_rank_t ) ) ; for ( i = 0 ; i < n ; i ++ ) { sx [ i ] . data = x [ i ] ; sy [ i ] . data = y [ i ] ; sx [ i ] . orig_rank = i ; sy [ i ] . orig_rank = i ; } qsort ( sx , n , sizeof ( * sx ) , compare_spearman_rank_t_data ) ; qsort ( sy , n , sizeof ( * sy ) , compare_spearman_rank_t_data ) ; for ( i = 0 ; i < n ; i ++ ) { double rank = 0 ; int num_ties = 0 ; int j ; for ( j = i ; j < n && sx [ i ] . data == sx [ j ] . data ; j ++ ) { rank += j + 1 ; num_ties ++ ; } for ( j = i ; j < i + num_ties ; j ++ ) { sx [ j ] . rank = rank / num_ties ; } i += num_ties - 1 ; } for ( i = 0 ; i < n ; i ++ ) { double rank = 0 ; int num_ties = 0 ; int j ; for ( j = i ; j < n && sy [ i ] . data == sy [ j ] . data ; j ++ ) { rank += j + 1 ; num_ties ++ ; } for ( j = i ; j < i + num_ties ; j ++ ) { sy [ j ] . rank = rank / num_ties ; } i += num_ties - 1 ; } qsort ( sx , n , sizeof ( * sx ) , compare_spearman_rank_t_orig_rank ) ; qsort ( sy , n , sizeof ( * sy ) , compare_spearman_rank_t_orig_rank ) ; for ( i = 0 ; i < n ; i ++ ) { sumxy += sx [ i ] . rank * sy [ i ] . rank ; sumx += sx [ i ] . rank ; sumy += sy [ i ] . rank ; sumxsq += sx [ i ] . rank * sx [ i ] . rank ; sumysq += sy [ i ] . rank * sy [ i ] . rank ; } free ( sx ) ; free ( sy ) ; return ( n * sumxy - sumx * sumy ) / ( sqrt ( n * sumxsq - sumx * sumx ) * sqrt ( n * sumysq - sumy * sumy ) ) ; }
spearman_rank_correlation:
              Returns the product moment correlation coefficient of the ranked input.
spearman_rank_correlation : Returns the product moment correlation coefficient of the ranked input .
/* number of points */ /* x values */ /* y values */ //Sorted-x and sorted-y in our struct type. //Copy into the array of structs and the pointer array // Assign the ranks - we need to do this once per array due to the // fact that we're accounting for ties and may skip some. // Look-ahead for ties. // This will include itself as a tie, deliberately // as a sanity check to make sure that it's working. //ranks start at 1 //Set the ranks to the mean of the tied ranks. //skip those we've already assigned ranks to // Look-ahead for ties. // This will include itself as a tie, deliberately // as a sanity check to make sure that it's working. //ranks start at 1 //Set the ranks to the mean of the tied ranks. //skip those we've already assigned ranks to //Resort based on original ranks //Calculate the various components of the product moment correlation equ. /*
	 * Complete the score:
	 */