GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
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 double *Cdhc_anderson_darling(double *x, int n)
7 {
8  int i;
9  static double y[2];
10  double sqrt2, mean = 0.0, sdx = 0.0, *xcopy, fx;
11 
12  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
13  fprintf(stderr, "Memory error in Cdhc_anderson_darling\n");
14  exit(EXIT_FAILURE);
15  }
16 
17  sqrt2 = sqrt((double)2.0);
18  y[0] = y[1] = 0.0;
19 
20  for (i = 0; i < n; ++i) {
21  xcopy[i] = x[i];
22  mean += x[i];
23  sdx += x[i] * x[i];
24  }
25  sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
26  mean /= n;
27 
28  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
29 
30  for (i = 0; i < n; ++i)
31  xcopy[i] = (xcopy[i] - mean) / sdx;
32 
33  for (i = 0; i < n; ++i) {
34  fx = 0.5 + Cdhc_normp(xcopy[i] / sqrt2) / 2.0;
35  if (fx <= 1e-5)
36  fx = 1e-5;
37 
38  if (fx >= .99999)
39  fx = 0.99999;
40 
41  y[1] += (2.0 * i + 1.0) * log(fx) + (2.0 * (n - i) - 1.0) * log(1 - fx);
42  }
43  y[1] = -n - y[1] / n;
44  y[0] = y[1] * (0.75 / n + 1.0 + 2.25 / (n * n));
45 
46 #ifdef NOISY
47  fprintf(stdout, " TEST8 AD(N) =%10.4f\n", y[0]);
48 #endif /* NOISY */
49  free(xcopy);
50 
51  return y;
52 }
double * Cdhc_anderson_darling(double *x, int n)
Definition: andrsndn.c:6
#define NULL
Definition: ccmath.h:32
int Cdhc_dcmp(const void *i, const void *j)
Definition: dcmp.c:1
double Cdhc_normp(double)
Definition: normp.c:22
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
void * malloc(YYSIZE_T)
void free(void *)
#define x