GRASS Programmer's Manual  6.4.2(2012)
cvmw2n.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 *cramer_von_mises(double *x, int n)
00008 {
00009     int i;
00010     static double y[2];
00011     double mean = 0.0, sdx = 0.0, fx, sqrt2, *xcopy;
00012 
00013     sqrt2 = sqrt((double)2.0);
00014     y[1] = 0.0;
00015 
00016     if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
00017         fprintf(stderr, "Memory error in cramer_von_mises\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.0)));
00027     mean /= n;
00028 
00029     qsort(xcopy, n, sizeof(double), dcmp);
00030 
00031     for (i = 0; i < n; ++i) {
00032         fx = 0.5 + normp((xcopy[i] - mean) / sdx / sqrt2) / 2.0;
00033         if (fx <= 1e-5)
00034             fx = 1e-5;
00035 
00036         if (fx >= 0.99999)
00037             fx = 0.99999;
00038 
00039         fx -= (2.0 * i + 1.0) / (2.0 * n);
00040         y[1] += fx * fx;
00041     }
00042     y[1] += 1.0 / (double)(n * 12);
00043     y[0] = y[1] * (0.5 / n + 1.0);
00044 
00045 #ifdef NOISY
00046     fprintf(stdout, "  TEST9  CVM(N) =%10.4f\n", y[0]);
00047 #endif /* NOISY */
00048 
00049     free(xcopy);
00050 
00051     return y;
00052 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines