GRASS Programmer's Manual  6.4.2(2012)
watsonun.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 *watson_u2(double *x, int n)
00008 {
00009     double *xcopy, mean = 0.0, sdx = 0.0, sqrt2, zbar = 0.0;
00010     double fn2, fx, sum4 = 0.0;
00011     static double y[2];
00012     int i;
00013 
00014     sqrt2 = sqrt((double)2.0);
00015 
00016     if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
00017         fprintf(stderr, "Memory error in anderson_darling\n");
00018         exit(EXIT_FAILURE);
00019     }
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)));
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         fn2 = (2.0 * (i + 1) - 1.0) / (2.0 * n);
00034         fx = 0.5 + normp(xcopy[i] / sqrt2) / 2.0;
00035 
00036         if (fx <= 0.0)
00037             fx = 1e-5;
00038 
00039         if (fx >= 1.0)
00040             fx = 0.99999;
00041 
00042         zbar += fx;
00043         sum4 += (fx - fn2) * (fx - fn2);
00044     }
00045 
00046     zbar /= n;
00047     y[0] = (1.0 / (n * 12) + sum4) - n * (zbar - .5) * (zbar - .5);
00048     y[0] *= 0.5 / n + 1.0;
00049 
00050 #ifdef NOISY
00051     fprintf(stdout, "  TEST6  WU2(N) =%10.4f\n", y[0]);
00052 #endif /* NOISY */
00053 
00054     free(xcopy);
00055 
00056     return y;
00057 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines