TPCCLIB
Loading...
Searching...
No Matches
runs_test.c File Reference

Runs test: defines if residuals of two data arrays are independent. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

int runs_test (double *data1, double *data2, int N, double alpha, double *p)
 
int residuals (double *d1, double *d2, int Nr, int *Rnr, int *Nminus, int *Nplus)
 
int mrl_between_tacs (double y1[], double y2[], int n)
 

Detailed Description

Runs test: defines if residuals of two data arrays are independent.

Author
Kaisa Liukko, Vesa Oikonen

Definition in file runs_test.c.

Function Documentation

◆ mrl_between_tacs()

int mrl_between_tacs ( double y1[],
double y2[],
int n )

Calculates the maximum run length between given two arrays of data.

Returns
Returns the MRL.

Definition at line 103 of file runs_test.c.

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}

◆ residuals()

int residuals ( double * d1,
double * d2,
int Nr,
int * Rnr,
int * Nminus,
int * Nplus )

Residuals function calculates the nr of runs (a sequence of consecutive positive or negative residuals) and nr of negative and positive residuals for two data arrays.

Returns
Returns 0, if ok.
Parameters
d1double array with data points
d2double array with data points
Nrnumber of data points in both arrays
Rnrnr of runs
Nminusnr of negative residuals
Nplusnr of positive residuals

Definition at line 56 of file runs_test.c.

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}

Referenced by runs_test().

◆ runs_test()

int runs_test ( double * data1,
double * data2,
int N,
double alpha,
double * p )

Runs_test function tests if residuals of two data arrays are independent.

Returns
Returns 0, if residuals are independent, -1, if residuals are dependent and >0 in case of an error.
Parameters
data1double array with data points
data2double array with data points
Nnumber of data points in both arrays
alphasignificance level percentage (set to negative value to use default 5%)
pcalculated probability p; enter NULL if not needed

Definition at line 12 of file runs_test.c.

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}
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