TPCCLIB
Loading...
Searching...
No Matches
median.c File Reference

Calculation of median value. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

double d_kth_smallest (double *data, int n, int k)
 
double dmedian (double *data, int n)
 
double dmean (double *data, int n, double *sd)
 
double dmean_nan (double *data, int n, double *sd, int *vn)
 

Detailed Description

Calculation of median value.

Author
Vesa Oikonen

Definition in file median.c.

Function Documentation

◆ d_kth_smallest()

double d_kth_smallest ( double * data,
int n,
int k )

Returns the kth smallest value in data[0..n-1]. Array is partially sorted. Algorithm is based on the book Wirth N. Algorithms + data structures = programs. Englewood Cliffs, Prentice-Hall, 1976.

Returns
Returns the kth smallest value in data[0..n-1].
Parameters
dataPointer to data; array is partially sorted.
nLength of data array.
kkth smallest value will be returned.

Definition at line 15 of file median.c.

22 {
23 int i, j, l, m;
24 double x, s;
25
26 l=0; m=n-1;
27 while(l<m) {
28 x=data[k]; i=l; j=m;
29 do {
30 while(data[i]<x) i++;
31 while(x<data[j]) j--;
32 if(i<=j) {s=data[i]; data[i]=data[j]; data[j]=s; i++; j--;}
33 } while(i<=j);
34 if(j<k) l=i;
35 if(k<i) m=j;
36 }
37 return(data[k]);
38}

Referenced by dmedian().

◆ dmean()

double dmean ( double * data,
int n,
double * sd )

Returns the mean in array data[0..n-1], and optionally calculates also the (sample) standard deviation of the mean.

See also
dmedian, mean, dmean_nan, fmean
Returns
Returns the mean in array data[0..n-1].
Parameters
dataPointer to data; data is not changed in any way.
nLength of data array.
sdPointer to variable where SD will be written; enter NULL if not needed.

Definition at line 73 of file median.c.

80 {
81 int i;
82 double sumsqr=0.0, sqrsum=0.0, avg;
83
84 if(n<1 || data==NULL) {if(sd!=NULL) *sd=0.0; return(0.0);}
85
86 for(i=0; i<n; i++) {sumsqr+=data[i]*data[i]; sqrsum+=data[i];}
87 avg=sqrsum/(double)n; if(sd==NULL) return(avg);
88 if(n==1) {
89 *sd=0.0;
90 } else {
91 sqrsum*=sqrsum;
92 *sd=sqrt( (sumsqr - sqrsum/(double)n) / (double)(n-1) );
93 }
94 return(avg);
95}

◆ dmean_nan()

double dmean_nan ( double * data,
int n,
double * sd,
int * vn )

Returns the mean in array data[0..n-1], and optionally calculates also the standard deviation of the mean. Data may contain missing samples marked as NaNs.

See also
dmedian, mean, dmean
Returns
Returns the mean in array data[0..n-1].
Parameters
dataPointer to data; data is not changed in any way.
nLength of data array.
sdPointer to variable where SD will be written; enter NULL if not needed.
vnPointer to variable where number of valid (not NaN) samples will be written; enter NULL if not needed.

Definition at line 104 of file median.c.

114 {
115 int i, m;
116 double sumsqr=0.0, sqrsum=0.0, avg;
117
118 if(n<1 || data==NULL) {
119 if(sd!=NULL) *sd=0.0;
120 if(vn!=NULL) *vn=0;
121 return(0.0);
122 }
123 for(i=m=0; i<n; i++) if(!isnan(data[i])) m++;
124 if(vn!=NULL) *vn=m;
125 if(m<1) {
126 if(sd!=NULL) *sd=nan("");
127 return(nan(""));
128 }
129
130
131 for(i=0; i<n; i++) if(!isnan(data[i])) {
132 sumsqr+=data[i]*data[i]; sqrsum+=data[i];
133 }
134 avg=sqrsum/(double)m; if(sd==NULL) return(avg); // SD not requested
135 if(m==1) {
136 *sd=0.0;
137 } else {
138 sqrsum*=sqrsum;
139 *sd=sqrt( (sumsqr - sqrsum/(double)m) / (double)(m-1) );
140 }
141 return(avg);
142}

◆ dmedian()

double dmedian ( double * data,
int n )

Returns the median in array data[0..n-1]. Array is partially sorted. Algorithm is based on the book Wirth N. Algorithms + data structures = programs. Englewood Cliffs, Prentice-Hall, 1976.

See also
dmean, mean, fmedian
Returns
Returns the median in array data[0..n-1].
Parameters
dataPointer to data; array is partially sorted.
nLength of data array.

Definition at line 48 of file median.c.

53 {
54 int k;
55 double d1, d2;
56
57 if(n<1) return(0.0);
58 if(n%2) {
59 k=(n-1)/2; return(d_kth_smallest(data, n, k));
60 } else {
61 k=n/2; d1=d_kth_smallest(data, n, k-1); d2=d_kth_smallest(data, n, k);
62 return(0.5*(d1+d2));
63 }
64}
double d_kth_smallest(double *data, int n, int k)
Definition median.c:15

Referenced by dftRobustMinMaxTAC(), least_trimmed_square(), and mEstim().