-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
andrsndn.c
54 lines (42 loc) · 1.12 KB
/
andrsndn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "local_proto.h"
double *Cdhc_anderson_darling(double *x, int n)
{
int i;
static double y[2];
double sqrt2, mean = 0.0, sdx = 0.0, *xcopy, fx;
if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
fprintf(stderr, "Memory error in Cdhc_anderson_darling\n");
exit(EXIT_FAILURE);
}
sqrt2 = sqrt((double)2.0);
y[0] = y[1] = 0.0;
for (i = 0; i < n; ++i) {
xcopy[i] = x[i];
mean += x[i];
sdx += x[i] * x[i];
}
sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
mean /= n;
qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
for (i = 0; i < n; ++i)
xcopy[i] = (xcopy[i] - mean) / sdx;
for (i = 0; i < n; ++i) {
fx = 0.5 + Cdhc_normp(xcopy[i] / sqrt2) / 2.0;
if (fx <= 1e-5)
fx = 1e-5;
if (fx >= .99999)
fx = 0.99999;
y[1] +=
(2.0 * i + 1.0) * log(fx) + (2.0 * (n - i) - 1.0) * log(1 - fx);
}
y[1] = -n - y[1] / n;
y[0] = y[1] * (0.75 / n + 1.0 + 2.25 / (n * n));
#ifdef NOISY
fprintf(stdout, " TEST8 AD(N) =%10.4f\n", y[0]);
#endif /* NOISY */
free(xcopy);
return y;
}