GRASS Programmer's Manual  6.4.2(2012)
chisqn.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 *chi_square(double *x, int n)
00008 {
00009     static double y[2];
00010     double mean = 0.0, sdx = 0.0, sum3 = 0.0, *v;
00011     int i, j, k, *f;
00012 
00013     k = rint(4.0 * pow(0.75 * (n - 1.0) * (n - 1.0), 0.2));
00014 
00015     while ((double)(n / k) < 5.0)
00016         --k;
00017 
00018     if ((f = (int *)calloc(k, sizeof(int))) == NULL) {
00019         fprintf(stderr, "Memory error in chi_square\n");
00020         exit(EXIT_FAILURE);
00021     }
00022     if ((v = (double *)malloc((k + 1) * sizeof(double))) == NULL) {
00023         fprintf(stderr, "Memory error in chi_square\n");
00024         exit(EXIT_FAILURE);
00025     }
00026 
00027     for (i = 0; i < n; ++i) {
00028         mean += x[i];
00029         sdx += x[i] * x[i];
00030     }
00031     sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
00032     mean /= n;
00033 
00034     v[0] = -1e9;
00035     for (i = 1; i < k; ++i)
00036         v[i] = mean + xinormal((double)i / k) * sdx;
00037 
00038     v[k] = 1e9;
00039 
00040     for (i = 0; i < n; ++i) {
00041         j = 0;
00042         while (j < k) {
00043             if (x[i] > v[j] && x[i] <= v[j + 1]) {
00044                 f[j]++;
00045                 j = k;
00046             }
00047             j++;
00048         }
00049     }
00050 
00051     for (i = 0; i < k; ++i)
00052         sum3 += f[i] * f[i];
00053 
00054     y[0] = sum3 * k / n - n;
00055     y[1] = (double)k - 3.0;
00056 
00057 #ifdef NOISY
00058     fprintf(stdout, "  TEST12 CS(N)  =%10.4f   DOF    =%10.4f\n", y[0], y[1]);
00059 #endif /* NOISY */
00060 
00061     free(f);
00062     free(v);
00063 
00064     return y;
00065 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines