GRASS Programmer's Manual  6.4.2(2012)
cvmw2e.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_exp(double *x, int n)
00008 {
00009     static double y[2];
00010     double *xcopy, mean = 0.0, fx, fn2, sum4 = 0.0;
00011     int i;
00012 
00013     if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
00014         fprintf(stderr, "Memory error in cramer_von_mises_exp\n");
00015         exit(EXIT_FAILURE);
00016     }
00017 
00018     for (i = 0; i < n; ++i) {
00019         xcopy[i] = x[i];
00020         mean += x[i];
00021     }
00022     mean /= n;
00023 
00024     qsort(xcopy, n, sizeof(double), dcmp);
00025 
00026     for (i = 0; i < n; ++i) {
00027 
00028     /*-
00029     a = (2 * i + 1) * log (fx);
00030     b = (2 * i + 1) * (xcopy[n-i-1] * (-1.0 / mean));
00031     sum3 += a + b;
00032     */
00033         fx = 1 - exp(xcopy[i] * (-1.0 / mean));
00034         fn2 = (double)(2.0 * i + 1) / (2 * n);
00035         sum4 += (fx - fn2) * (fx - fn2);
00036     }
00037 
00038   /*-
00039   cvm = 1.0 / (n * 12) + sum4;
00040   cvmod = cvm * (0.16 / n + 1.0);
00041   */
00042     y[0] = (1.0 / (n * 12) + sum4) * (0.16 / n + 1.0);
00043 
00044 #ifdef NOISY
00045     fprintf(stdout, "  TEST16 CVM(E) =%10.4f\n", y[0]);
00046 #endif /* NOISY */
00047 
00048     free(xcopy);
00049 
00050     return y;
00051 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines