GRASS Programmer's Manual  6.4.2(2012)
chisqe.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 
00005 
00006 double *chi_square_exp(double *x, int n)
00007 {
00008     static double y[2];
00009     double mean = 0.0, sum3 = 0.0, *v;
00010     int i, j, k, *f;
00011 
00012     k = rint(4.0 * pow(0.75 * (n - 1.0) * (n - 1.0), 0.2));
00013 
00014     while ((double)(n / k) < 5.0)
00015         --k;
00016 
00017     if ((f = (int *)calloc(k, sizeof(int))) == NULL) {
00018         fprintf(stderr, "Memory error in chi_square\n");
00019         exit(EXIT_FAILURE);
00020     }
00021     if ((v = (double *)malloc((k + 1) * sizeof(double))) == NULL) {
00022         fprintf(stderr, "Memory error in chi_square\n");
00023         exit(EXIT_FAILURE);
00024     }
00025 
00026     for (i = 0; i < n; ++i)
00027         mean += x[i];
00028 
00029     mean = n / mean;
00030     v[0] = 0.0;
00031 
00032     for (i = 1; i < k; ++i)
00033         v[i] = -log(1.0 - (double)i / k) / mean;
00034 
00035     v[k] = 1e9;
00036 
00037     for (i = 0; i < n; ++i) {
00038         j = 0;
00039         while (j < k) {
00040             if (x[i] > v[j] && x[i] <= v[j + 1]) {
00041                 f[j]++;
00042                 j = k;
00043             }
00044             j++;
00045         }
00046     }
00047 
00048     for (i = 0; i < k; ++i)
00049         sum3 += f[i] * f[i];
00050 
00051     y[0] = sum3 * k / n - n;
00052     y[1] = (double)k - 2.0;
00053 
00054 #ifdef NOISY
00055     fprintf(stdout, "  TEST21 CS(E)  =%10.4f   DOF    =%10.4f\n", y[0], y[1]);
00056 #endif /* NOISY */
00057 
00058     free(f);
00059     free(v);
00060 
00061     return y;
00062 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines