GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <math.h> 00004 #include "local_proto.h" 00005 00006 00007 double *anderson_darling_exp(double *x, int n) 00008 { 00009 static double y[2]; 00010 double sqrt2, mean = 0.0, *xcopy, fx, sum3 = 0.0; 00011 int i; 00012 00013 if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) { 00014 fprintf(stderr, "Memory error in anderson_darling\n"); 00015 exit(EXIT_FAILURE); 00016 } 00017 00018 sqrt2 = sqrt((double)2.0); 00019 00020 for (i = 0; i < n; ++i) { 00021 xcopy[i] = x[i]; 00022 mean += x[i]; 00023 } 00024 mean /= n; 00025 qsort(xcopy, n, sizeof(double), dcmp); 00026 00027 for (i = 0; i < n; ++i) { 00028 fx = 1 - exp(-xcopy[i] / mean); 00029 sum3 += (2.0 * i + 1) * (log(fx) - xcopy[n - i - 1] / mean); 00030 } 00031 00032 y[0] = (1.0 + 0.3 / n) * (-n - sum3 / n); 00033 #ifdef NOISY 00034 fprintf(stdout, " TEST20 AD(E) =%10.4f\n", y[0]); 00035 #endif /* NOISY */ 00036 00037 free(xcopy); 00038 00039 return y; 00040 }