GRASS Programmer's Manual  6.5.svn(2014)-r66266
andrsndn.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "local_proto.h"
5
6
7 double *anderson_darling(double *x, int n)
8 {
9  int i;
10  static double y[2];
11  double sqrt2, mean = 0.0, sdx = 0.0, *xcopy, fx;
12
13  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
14  fprintf(stderr, "Memory error in anderson_darling\n");
15  exit(EXIT_FAILURE);
16  }
17
18  sqrt2 = sqrt((double)2.0);
19  y[0] = y[1] = 0.0;
20
21  for (i = 0; i < n; ++i) {
22  xcopy[i] = x[i];
23  mean += x[i];
24  sdx += x[i] * x[i];
25  }
26  sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
27  mean /= n;
28
29  qsort(xcopy, n, sizeof(double), dcmp);
30
31  for (i = 0; i < n; ++i)
32  xcopy[i] = (xcopy[i] - mean) / sdx;
33
34  for (i = 0; i < n; ++i) {
35  fx = 0.5 + normp(xcopy[i] / sqrt2) / 2.0;
36  if (fx <= 1e-5)
37  fx = 1e-5;
38
39  if (fx >= .99999)
40  fx = 0.99999;
41
42  y[1] +=
43  (2.0 * i + 1.0) * log(fx) + (2.0 * (n - i) - 1.0) * log(1 - fx);
44  }
45  y[1] = -n - y[1] / n;
46  y[0] = y[1] * (0.75 / n + 1.0 + 2.25 / (n * n));
47
48 #ifdef NOISY
49  fprintf(stdout, " TEST8 AD(N) =%10.4f\n", y[0]);
50 #endif /* NOISY */
51  free(xcopy);
52
53  return y;
54 }
int dcmp(const void *i, const void *j)
Definition: dcmp.c:1
double * anderson_darling(double *x, int n)
Definition: andrsndn.c:7
int y
Definition: plot.c:34
log
Definition: wxnviz.py:54
void * malloc(YYSIZE_T)
double normp(double)
Definition: normp.c:23
return NULL
Definition: dbfopen.c:1394
void free(void *)
int n