TPCCLIB
Loading...
Searching...
No Matches
runs_test.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6#include "libtpcmodel.h"
7/*****************************************************************************/
14 double *data1,
16 double *data2,
18 int N,
20 double alpha,
22 double *p
23) {
24 int R=1, Nneg=0, Npos=0, NN;
25 double expect, var, Z, pvalue=0.0, a, b;
26 if(alpha<=0.0) alpha=5.0;
27
28 residuals(data1, data2, N, &R, &Nneg, &Npos);
29 //printf("* R=%d Nneg=%d Npos=%d\n", R, Nneg, Npos);
30
31 /* Expectation value for R. See Cobelli p. 262*/
32 NN=2*Nneg*Npos;
33 if(NN<1) {if(p!=NULL) *p=pvalue; return -1;}
34 expect=(double)NN/(double)N + 1.0;
35
36 /* Variance for R. See Cobelli Eq 8.6.4 p. 262*/
37 a=(double)NN*(double)(NN-N); b=(double)(N-1)*(double)N*(double)N;
38 var=a/b;
39
40 /* R->Z that tends to _standardized_ normal distribution */
41 Z=((double)R-expect)/sqrt(var); // printf(" Z=%g\n", Z);
42 /* Get propability of getting value Z from normal distribution*/
43 pvalue=ndtr(Z); if(p!=NULL) *p=pvalue;
44
45 if (pvalue<0.01*alpha) return -1;
46 else return 0;
47}
48/*****************************************************************************/
49
50/*****************************************************************************/
58 double *d1,
60 double *d2,
62 int Nr,
64 int *Rnr,
66 int *Nminus,
68 int *Nplus
69) {
70 if(d1==NULL || d2==NULL || Nr<1 || Rnr==NULL || Nminus==NULL || Nplus==NULL) return(1);
71
72 int i, *sign;
73 double *res;
74
75 /* Allocate memory for residuals */
76 res=(double*)malloc((Nr)*sizeof(double));
77 sign=(int*)malloc((Nr)*sizeof(int));
78 if(res==NULL || sign==NULL) return(2);
79
80 /* get residuals into res, nr of negative residuals in Nminus */
81 /* nr of positive residuals into Nplus */
82 /* Sign of each residual is saved into sign */
83 /* and if sign is different from the last residual, increase runs nr */
84 *Nminus=0; *Nplus=0; *Rnr=1;
85 for(i=0; i<Nr; i++) {
86 res[i]=d1[i]-d2[i];
87 if(res[i]<0.0) {
88 *Nminus+=1; sign[i]=-1;
89 } else {
90 *Nplus+=1; sign[i]=1;
91 }
92 if(i>0) if(sign[i]!=sign[i-1]) *Rnr+=1;
93 }
94 free(res); free(sign);
95 return(0);
96}
97/*****************************************************************************/
98
99/*****************************************************************************/
104 /* Pointer to the first data array */
105 double y1[],
106 /* Pointer to the second data array */
107 double y2[],
108 /* Length of data arrays */
109 int n
110) {
111 int mrl=0, rl=0;
112 char last_sign=0, sign;
113
114 if(y1==NULL || y2==NULL) return(0);
115 for(int i=0; i<n; i++) {
116 if(isnan(y1[i]) || isnan(y2[i])) continue;
117 if(y1[i]>y2[i]) sign=1; else if(y1[i]<y2[i]) sign=-1; else sign=0;
118 if(sign!=last_sign) {
119 rl=0; last_sign=sign;
120 } else {
121 if(sign!=0) {rl++; if(rl>mrl) mrl=rl;}
122 }
123 }
124 return(mrl);
125}
126/*****************************************************************************/
127
128/*****************************************************************************/
Header file for libtpcmodel.
double ndtr(double a)
Definition normaldistr.c:17
int residuals(double *d1, double *d2, int Nr, int *Rnr, int *Nminus, int *Nplus)
Definition runs_test.c:56
int runs_test(double *data1, double *data2, int N, double alpha, double *p)
Definition runs_test.c:12
int mrl_between_tacs(double y1[], double y2[], int n)
Definition runs_test.c:103