Sorting and Robust Statistics

The functions listed here are defined in robuststat.c and percentile.c. They are declared in cfsemshare.h, which includes mrcslice.h and is included by b3dutil.h.

Functions for Sorting and Robust Statistics


void rsSortInts(int *x, int n)
void rssortints(int *x, int *n)
void rsSortFloats(float *x, int n)
void rssortfloats(float *x, int *n)
void rsSortIndexedFloats(float *x, int *index, int n)
void rssortindexedfloats(float *x, int *index, int *n)
void rsSetSortIndexOffset(int offset)
void rsMedianOfSorted(float *x, int n, float *median)
void rsmedianofsorted(float *x, int *n, float *median)
void rsMedian(float *x, int n, float *xsort, float *median)
void rsmedian(float *x, int *n, float *xsort, float *median)
void rsFastMedianInPlace(float *x, int n, float *median)
void rsfastmedianinplace(float *x, int *n, float *median)
void rsFastMedian(float *x, int n, float *xjumble, float *median)
void rsfastmedian(float *x, int *n, float *xjumble, float *median)
void rsPercentileOfSorted(float *x, int n, float fraction, float *pctile)
void rspercentileofsorted(float *x, int *n, float *fraction, float *pctile)
void rsMADN(float *x, int n, float median, float *tmp, float *MADN)
void rsmadn(float *x, int *n, float *median, float *tmp, float *MADN)
void rsFastMADN(float *x, int n, float median, float *tmp, float *MADN)
void rsfastmadn(float *x, int *n, float *median, float *tmp, float *MADN)
void rsMadMedianOutliers(float *x, int n, float kcrit, float *out)
void rsmadmedianoutliers(float *x, int *n, float *kcrit, float *out)
void rsTrimmedMeanOfSorted(float *x, int n, float gamma, float *trmean)
void rstrimmedmeanofsorted(float *x, int *n, float *gamma, float *median)
void rsTrimmedMean(float *x, int n, float gamma, float *xsort, float *trmean)
void rstrimmedmean(float *x, int *n, float *gamma, float *xsort, float *median)

Functions for Finding a Percentile


float percentileFloat(int s, float *r, int num)
double percentilefloat(int *s, float *r, int *num)
int percentileInt(int s, int *r, int num)

Functions for Sorting and Robust Statistics

void rsSortInts(int *x, int n)

Uses qsort to sort the n ints in the array x.

void rssortints(int *x, int *n)

Fortran wrapper for rsSortInts

void rsSortFloats(float *x, int n)

Uses qsort to sort the n floats in the array x.

void rssortfloats(float *x, int *n)

Fortran wrapper for rsSortFloats

void rsSortIndexedFloats(float *x, int *index, int n)

Uses qsort to sort indexes in index to the n floats in the array x.  Indexes will be used directly when calling from C.  The routine stores a static pointer to the array so it is not thread-safe.

void rssortindexedfloats(float *x, int *index, int *n)

Fortran wrapper for rsSortIndexedFloats.  Indexes will be reduced by one to index the array in the C comparison routine, so they can still be Fortran indexes.

void rsSetSortIndexOffset(int offset)

Sets an offset to subtract from indexes passed to the next call of rsSortIndexedFloats.  This can be used to sort a portion of an array; offset should be the index at the start of that portion.

void rsMedianOfSorted(float *x, int n, float *median)

Computes the median of the n values in the array x, which has already been sorted, and returns the value in median.

void rsmedianofsorted(float *x, int *n, float *median)

Fortran wrapper for rsMedianOfSorted

void rsMedian(float *x, int n, float *xsort, float *median)

Computes the median of the n values in x.  The value is returned in median and xsort is used for sorting the array and returned with the sorted values.

void rsmedian(float *x, int *n, float *xsort, float *median)

Fortran wrapper for rsMedian

void rsFastMedianInPlace(float *x, int n, float *median)

Computes the median of the n values in x in linear time.  The value is returned in median and x is rearranged by one or two calls to percentileFloat.  This routine is faster than rsMedian even for small n, and much faster for large n.

void rsfastmedianinplace(float *x, int *n, float *median)

Fortran wrapper for rsFastMedianInPlace

void rsFastMedian(float *x, int n, float *xjumble, float *median)

Computes the median of the n values in x in linear time.  The value is returned in median, and xjumble is used for calling rsFastMedianInPlace.

void rsfastmedian(float *x, int *n, float *xjumble, float *median)

Fortran wrapper for rsFastMedian

void rsPercentileOfSorted(float *x, int n, float fraction, float *pctile)

Computes the percentile of the n sorted values in x indicated by fraction, with interpolation between between adjacent values, and returns the result in pctile.

void rspercentileofsorted(float *x, int *n, float *fraction, float *pctile)

Fortran wrapper for rsPercentileOfSorted

void rsMADN(float *x, int n, float median, float *tmp, float *MADN)

Computes the normalized median absolute deviation from the median for the n values in x, using the value already computed for the median in median.  The median absolute deviation is divided by .6745, which makes it estimate sigma for a normal distribution (Wilcox, R.R., Robust Estimation and Hypthesis Testing, 2nd edition, P. 78).  The result is returned in MADN, and tmp is returned with sorted values of the absolute deviations.

void rsmadn(float *x, int *n, float *median, float *tmp, float *MADN)

Fortran wrapper for rsMADN

void rsFastMADN(float *x, int n, float median, float *tmp, float *MADN)

Computes the normalized median absolute deviation from the median for the n values in x in linear time, using the value already computed for the median in median.  The result is returned in MADN, and tmp is used for storing absolute deviations.

void rsfastmadn(float *x, int *n, float *median, float *tmp, float *MADN)

Fortran wrapper for rsFastMADN

void rsMadMedianOutliers(float *x, int n, float kcrit, float *out)

Selects outliers among the n values in the array x by testing whether the absolute deviation from the median is greater than normalized median absolute deviation by the criterion kcrit.  out is used for temporary storage and is returned with -1 or 1 for outliers in the negative or positive direction from the median, 0 otherwise.  A typical value for kcrit would be 2.24, the square root of the 0.975 quantile of a chi-square distribution with one degree of freedom (Wilcox, P. 101, see rsMADN).

void rsmadmedianoutliers(float *x, int *n, float *kcrit, float *out)

Fortran wrapper for rsMadMedianOutliers

void rsTrimmedMeanOfSorted(float *x, int n, float gamma, float *trmean)

Computes a trimmed mean of the n already sorted values in x, trimming off the fraction gamma on each end of the distribution.  The value is returned in trmean.

void rstrimmedmeanofsorted(float *x, int *n, float *gamma, float *median)

Fortran wrapper for rsTrimmedMeanOfSorted

void rsTrimmedMean(float *x, int n, float gamma, float *xsort, float *trmean)

Computes a trimmed mean of the n values in x, trimming off the fraction gamma on each end of the distribution.  The value is returned in trmean and xsort is used for sorting the array and returned with the sorted values.

void rstrimmedmean(float *x, int *n, float *gamma, float *xsort, float *median)

Fortran wrapper for rsTrimmedMean

Functions for Finding a Percentile

float percentileFloat(int s, float *r, int num)

Selects item number s (numbered from 1) out of num items in the array r, where items are considered in order from low to high.  r is partially rearranged while finding the item.  The algorithm runs in linear time and is faster than sorting the array first.

double percentilefloat(int *s, float *r, int *num)

Fortran wrapper to percentileFloat

int percentileInt(int s, int *r, int num)

Same as percentileFloat but with an integer array r