This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
randomExcursions.c
122 lines (111 loc) · 4.3 KB
/
randomExcursions.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "../include/externs.h"
#include "../include/cephes.h"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
R A N D O M E X C U R S I O N S T E S T
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
RandomExcursions(int n)
{
int b, i, j, k, J, x;
int cycleStart, cycleStop, *cycle = NULL, *S_k = NULL;
int stateX[8] = { -4, -3, -2, -1, 1, 2, 3, 4 };
int counter[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
double p_value, sum, constraint, nu[6][8];
double pi[5][6] = { {0.0000000000, 0.00000000000, 0.00000000000, 0.00000000000, 0.00000000000, 0.0000000000},
{0.5000000000, 0.25000000000, 0.12500000000, 0.06250000000, 0.03125000000, 0.0312500000},
{0.7500000000, 0.06250000000, 0.04687500000, 0.03515625000, 0.02636718750, 0.0791015625},
{0.8333333333, 0.02777777778, 0.02314814815, 0.01929012346, 0.01607510288, 0.0803755143},
{0.8750000000, 0.01562500000, 0.01367187500, 0.01196289063, 0.01046752930, 0.0732727051} };
if ( ((S_k = (int *)calloc(n, sizeof(int))) == NULL) ||
((cycle = (int *)calloc(MAX(1000, n/100), sizeof(int))) == NULL) ) {
printf("Random Excursions Test: Insufficent Work Space Allocated.\n");
if ( S_k != NULL )
free(S_k);
if ( cycle != NULL )
free(cycle);
return;
}
J = 0; /* DETERMINE CYCLES */
S_k[0] = 2*(int)epsilon[0] - 1;
for( i=1; i<n; i++ ) {
S_k[i] = S_k[i-1] + 2*epsilon[i] - 1;
if ( S_k[i] == 0 ) {
J++;
if ( J > MAX(1000, n/100) ) {
printf("ERROR IN FUNCTION randomExcursions: EXCEEDING THE MAX NUMBER OF CYCLES EXPECTED\n.");
free(S_k);
free(cycle);
return;
}
cycle[J] = i;
}
}
if ( S_k[n-1] != 0 )
J++;
cycle[J] = n;
fprintf(stats[TEST_RND_EXCURSION], "\t\t\t RANDOM EXCURSIONS TEST\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\t--------------------------------------------\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\tCOMPUTATIONAL INFORMATION:\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\t--------------------------------------------\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\t(a) Number Of Cycles (J) = %04d\n", J);
fprintf(stats[TEST_RND_EXCURSION], "\t\t(b) Sequence Length (n) = %d\n", n);
constraint = MAX(0.005*pow(n, 0.5), 500);
if (J < constraint) {
fprintf(stats[TEST_RND_EXCURSION], "\t\t---------------------------------------------\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\tWARNING: TEST NOT APPLICABLE. THERE ARE AN\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\t\t INSUFFICIENT NUMBER OF CYCLES.\n");
fprintf(stats[TEST_RND_EXCURSION], "\t\t---------------------------------------------\n");
for(i = 0; i < 8; i++)
fprintf(results[TEST_RND_EXCURSION], "%f\n", 0.0);
}
else {
fprintf(stats[TEST_RND_EXCURSION], "\t\t(c) Rejection Constraint = %f\n", constraint);
fprintf(stats[TEST_RND_EXCURSION], "\t\t-------------------------------------------\n");
cycleStart = 0;
cycleStop = cycle[1];
for ( k=0; k<6; k++ )
for ( i=0; i<8; i++ )
nu[k][i] = 0.;
for ( j=1; j<=J; j++ ) { /* FOR EACH CYCLE */
for ( i=0; i<8; i++ )
counter[i] = 0;
for ( i=cycleStart; i<cycleStop; i++ ) {
if ( (S_k[i] >= 1 && S_k[i] <= 4) || (S_k[i] >= -4 && S_k[i] <= -1) ) {
if ( S_k[i] < 0 )
b = 4;
else
b = 3;
counter[S_k[i]+b]++;
}
}
cycleStart = cycle[j]+1;
if ( j < J )
cycleStop = cycle[j+1];
for ( i=0; i<8; i++ ) {
if ( (counter[i] >= 0) && (counter[i] <= 4) )
nu[counter[i]][i]++;
else if ( counter[i] >= 5 )
nu[5][i]++;
}
}
for ( i=0; i<8; i++ ) {
x = stateX[i];
sum = 0.;
for ( k=0; k<6; k++ )
sum += pow(nu[k][i] - J*pi[(int)fabs(x)][k], 2) / (J*pi[(int)fabs(x)][k]);
p_value = cephes_igamc(2.5, sum/2.0);
if ( isNegative(p_value) || isGreaterThanOne(p_value) )
fprintf(stats[TEST_RND_EXCURSION], "WARNING: P_VALUE IS OUT OF RANGE.\n");
fprintf(stats[TEST_RND_EXCURSION], "%s\t\tx = %2d chi^2 = %9.6f p_value = %f\n",
p_value < ALPHA ? "FAILURE" : "SUCCESS", x, sum, p_value);
fprintf(results[TEST_RND_EXCURSION], "%f\n", p_value); fflush(results[TEST_RND_EXCURSION]);
}
}
fprintf(stats[TEST_RND_EXCURSION], "\n"); fflush(stats[TEST_RND_EXCURSION]);
free(S_k);
free(cycle);
}