GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
chisqe.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 
5 
6 double *chi_square_exp(double *x, int n)
7 {
8  static double y[2];
9  double mean = 0.0, sum3 = 0.0, *v;
10  int i, j, k, *f;
11 
12  k = rint(4.0 * pow(0.75 * (n - 1.0) * (n - 1.0), 0.2));
13 
14  while ((double)(n / k) < 5.0)
15  --k;
16 
17  if ((f = (int *)calloc(k, sizeof(int))) == NULL) {
18  fprintf(stderr, "Memory error in chi_square\n");
19  exit(EXIT_FAILURE);
20  }
21  if ((v = (double *)malloc((k + 1) * sizeof(double))) == NULL) {
22  fprintf(stderr, "Memory error in chi_square\n");
23  exit(EXIT_FAILURE);
24  }
25 
26  for (i = 0; i < n; ++i)
27  mean += x[i];
28 
29  mean = n / mean;
30  v[0] = 0.0;
31 
32  for (i = 1; i < k; ++i)
33  v[i] = -log(1.0 - (double)i / k) / mean;
34 
35  v[k] = 1e9;
36 
37  for (i = 0; i < n; ++i) {
38  j = 0;
39  while (j < k) {
40  if (x[i] > v[j] && x[i] <= v[j + 1]) {
41  f[j]++;
42  j = k;
43  }
44  j++;
45  }
46  }
47 
48  for (i = 0; i < k; ++i)
49  sum3 += f[i] * f[i];
50 
51  y[0] = sum3 * k / n - n;
52  y[1] = (double)k - 2.0;
53 
54 #ifdef NOISY
55  fprintf(stdout, " TEST21 CS(E) =%10.4f DOF =%10.4f\n", y[0], y[1]);
56 #endif /* NOISY */
57 
58  free(f);
59  free(v);
60 
61  return y;
62 }
int y
Definition: plot.c:34
log
Definition: wxnviz.py:54
void * malloc(YYSIZE_T)
return NULL
Definition: dbfopen.c:1394
void free(void *)
double * chi_square_exp(double *x, int n)
Definition: chisqe.c:6
int n
Definition: dataquad.c:291