GRASS Programmer's Manual  6.4.2(2012)
andrsndn.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include "local_proto.h"
00005 
00006 
00007 double *anderson_darling(double *x, int n)
00008 {
00009     int i;
00010     static double y[2];
00011     double sqrt2, mean = 0.0, sdx = 0.0, *xcopy, fx;
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     y[0] = y[1] = 0.0;
00020 
00021     for (i = 0; i < n; ++i) {
00022         xcopy[i] = x[i];
00023         mean += x[i];
00024         sdx += x[i] * x[i];
00025     }
00026     sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
00027     mean /= n;
00028 
00029     qsort(xcopy, n, sizeof(double), dcmp);
00030 
00031     for (i = 0; i < n; ++i)
00032         xcopy[i] = (xcopy[i] - mean) / sdx;
00033 
00034     for (i = 0; i < n; ++i) {
00035         fx = 0.5 + normp(xcopy[i] / sqrt2) / 2.0;
00036         if (fx <= 1e-5)
00037             fx = 1e-5;
00038 
00039         if (fx >= .99999)
00040             fx = 0.99999;
00041 
00042         y[1] +=
00043             (2.0 * i + 1.0) * log(fx) + (2.0 * (n - i) - 1.0) * log(1 - fx);
00044     }
00045     y[1] = -n - y[1] / n;
00046     y[0] = y[1] * (0.75 / n + 1.0 + 2.25 / (n * n));
00047 
00048 #ifdef NOISY
00049     fprintf(stdout, "  TEST8  AD(N)  =%10.4f\n", y[0]);
00050 #endif /* NOISY */
00051     free(xcopy);
00052 
00053     return y;
00054 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines