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

Least median of squares estimate for single data. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

double least_median_of_squares (double *data, int n)
 Fit a constant (horisontal straight line) to the data by minimising the median of squared residuals.
 
int lmsQSort (const void *par1, const void *par2)
 

Detailed Description

Least median of squares estimate for single data.

Author
Kaisa Sederholm, Vesa Oikonen

Definition in file lms.c.

Function Documentation

◆ least_median_of_squares()

double least_median_of_squares ( double * data,
int n )

Fit a constant (horisontal straight line) to the data by minimising the median of squared residuals.

The algorithm is described in P.J. Rousseeuw: Least Median of Squares Regression, Journal of the American Statistical Association, Vol. 79, No. 388 (1984), 871-880.

Returns
Returns the LMS estimate.
Parameters
dataData array
nNumber of data values

Definition at line 21 of file lms.c.

26 {
27 int i, odd=1, half=floor(n/2), smallnr;
28 double small, *help;
29 if(fmod((double)n, 2.0)<1e-99) odd=0;
30 double *halfs_data;
31
32 halfs_data=(double*)malloc((half+odd) * sizeof(double));
33
34 help=data;
35 /* sort data in ascending order*/
36 qsort(help, n, sizeof(double), lmsQSort);
37
38 /* if n is even number */
39 for(i=0; i<half+odd; i++) {
40 halfs_data[i]=data[half+i]-data[i];
41 }
42
43 i=smallnr=0;
44 for(i=1, small=halfs_data[0]; i<half+odd; i++) {
45 if(halfs_data[i]<small) {
46 small=halfs_data[i];
47 smallnr=i;
48 }
49 }
50
51 return (data[half+smallnr]+data[smallnr])/2.0;
52}
int lmsQSort(const void *par1, const void *par2)
Definition lms.c:59

◆ lmsQSort()

int lmsQSort ( const void * par1,
const void * par2 )

Compares two numbers.

Returns
Returns -1 if value1<value2, 1 if value1>value2 and 0 otherwise.
Parameters
par1value nr 1
par2value nr 2

Definition at line 59 of file lms.c.

64 {
65 if( *((double*)par1) < *((double*)par2)) return(-1);
66 else if( *((double*)par1) > *((double*)par2)) return(1);
67 else return(0);
68}

Referenced by least_median_of_squares().