1 /*************************************************************************\
2  *
3  * Package:        TestU01
4  * File:           fres.c
5  * Environment:    ANSI C
6  *
7  * Copyright (c) 2002 Pierre L'Ecuyer, DIRO, Université de Montréal.
8  * e-mail: lecuyer@iro.umontreal.ca
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted without a fee for private, research,
13  * academic, or other non-commercial purposes.
14  * Any use of this software in a commercial environment requires a
15  * written licence from the copyright owner.
16  *
17  * Any changes made to this package must be clearly identified as such.
18  *
19  * In scientific publications which used this software, a reference to it
20  * would be appreciated.
21  *
22  * Redistributions of source code must retain this copyright notice
23  * and the following disclaimer.
24  *
25  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28  *
29 \*************************************************************************/
30 
31 #include "util.h"
32 #include "bitset.h"
33 #include "fres.h"
34 #include "ftab.h"
35 
36 #include <string.h>
37 
38 #define LEN 100
39 
40 
41 
42 
43 /*=========================================================================*/
44 
fres_InitCont(ffam_Fam * fam,fres_Cont * res,int N,int Nr,int f1,int f2,int fstep,char * nam)45 void fres_InitCont (ffam_Fam *fam, fres_Cont *res, int N,
46    int Nr, int f1, int f2, int fstep, char *nam)
47 {
48    int i, j;
49    char str[LEN + 1] = {0};
50    size_t len1;
51    char *p;
52 
53    res->name = util_Realloc (res->name, 1 + strlen (nam) * sizeof (char));
54    strcpy (res->name, nam);
55    Nr = util_Min (Nr, fam->Ng);
56    res->Active = 0;
57 
58    for (j = 0; j < gofw_NTestTypes; j++) {
59       if ((gofw_Mean == j) ||
60             (N > 1 && (bitset_TestBit (gofw_ActiveTests, j)))) {
61          strncpy (str, nam, (size_t) LEN);
62          len1 = strlen (str);
63 	 strncat (str, ": ", 3);
64          p = strstr (res->PVal[j]->Desc, "p-value");
65          if (p)
66 	    strncat (str, p, (size_t) LEN - len1);
67          ftab_DeleteTable (res->PVal[j]);
68 	 res->PVal[j] = ftab_CreateTable (Nr, f1, f2, fstep, str,
69                         ftab_pVal2, 0);
70 	 ftab_InitMatrix (res->PVal[j], -1.0);
71          bitset_SetBit (res->Active, j);
72          for (i = 0; i < Nr; i++)
73 	    res->PVal[j]->LSize[i] = fam->LSize[i];
74       }
75    }
76    if (N > 1)
77       bitset_ClearBit (res->Active, gofw_Mean);
78 }
79 
80 
81 /*-------------------------------------------------------------------------*/
82 
fres_CreateCont(void)83 fres_Cont * fres_CreateCont (void)
84 {
85    fres_Cont *res;
86    char str[LEN + 1];
87    gofw_TestType j;
88    size_t m;
89 
90    res = util_Malloc (sizeof (fres_Cont));
91    res->name = util_Calloc (1, sizeof (char));
92 
93    m = strlen ("p-value for statistic ");
94    for (j = 0; j < gofw_NTestTypes; j++) {
95       if ((gofw_Mean == j) || (bitset_TestBit (gofw_ActiveTests, j))) {
96 	 strncpy (str, "p-value for ", (size_t) LEN);
97 	 if (gofw_Mean != j)
98             strncat (str, gofw_TestNames[j], (size_t) LEN - m);
99 	 strncat (str, " statistic", (size_t) LEN - m);
100 	 res->PVal[j] = ftab_CreateTable (1, 0, 1, 1, str, ftab_pVal2, 0);
101       }
102    }
103    return res;
104 }
105 
106 
107 /*-------------------------------------------------------------------------*/
108 
fres_DeleteCont(fres_Cont * res)109 void fres_DeleteCont (fres_Cont *res)
110 {
111    gofw_TestType j;
112 
113    if (res == NULL)
114       return;
115    res->name = util_Free (res->name);
116 
117    for (j = 0; j < gofw_NTestTypes; j++) {
118       if ((gofw_Mean == j) || (bitset_TestBit (gofw_ActiveTests, j))) {
119          ftab_DeleteTable (res->PVal[j]);
120          res->PVal[j] = NULL;
121       }
122    }
123    util_Free (res);
124 }
125 
126 
127 /*=========================================================================*/
128 
fres_PrintCont(fres_Cont * res)129 void fres_PrintCont (fres_Cont *res)
130 {
131    gofw_TestType j;
132 
133    for (j = 0; j <= gofw_Mean; j++) {
134       if (bitset_TestBit (res->Active, j))
135          ftab_PrintTable (res->PVal[j]);
136    }
137 }
138 
139 
140 /*=========================================================================*/
141 
fres_FillTableEntryC(fres_Cont * fres,gofw_TestArray pval,int N,int i,int j)142 void fres_FillTableEntryC (fres_Cont *fres, gofw_TestArray pval,
143    int N, int i, int j)
144 /*
145  * Writes the results of one test in the tables.
146  */
147 {
148    gofw_TestType k;
149 
150    if (N == 1) {
151       fres->PVal[gofw_Mean]->Mat[i][j] = pval[gofw_Mean];
152 
153    } else {
154       for (k = 0; k <= gofw_Mean; k++) {
155          if (bitset_TestBit (gofw_ActiveTests, k)) {
156             fres->PVal[k]->Mat[i][j] = pval[k];
157          }
158       }
159    }
160 }
161 
162 
163 /*=========================================================================*/
164 
fres_InitDisc(ffam_Fam * fam,fres_Disc * res,int Nr,int f1,int f2,int fstep,char * nam)165 void fres_InitDisc (ffam_Fam *fam, fres_Disc *res,
166    int Nr, int f1, int f2, int fstep, char *nam)
167 {
168    char str[LEN + 1] = {0};
169    char str2[LEN + 1] = {0};
170    size_t len1;
171    int i;
172 
173    res->name = util_Realloc (res->name, 1 + strlen (nam) * sizeof (char));
174    strcpy (res->name, nam);
175    strncpy (str, nam, (size_t) LEN);
176    len1 = strlen (nam);
177 
178    Nr = util_Min (Nr, fam->Ng);
179 
180    ftab_DeleteTable (res->PVal2);
181    ftab_DeleteTable (res->PRight);
182    ftab_DeleteTable (res->PLeft);
183 
184    strncpy (str2, nam, (size_t) LEN);
185    strncat (str2, ", Left p-value", (size_t) LEN - len1);
186    res->PLeft = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_pVal1, 0);
187 
188    strncpy (str2, nam, (size_t) LEN);
189    strncat (str2, ", Right p-value", (size_t) LEN - len1);
190    res->PRight = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_pVal1, 0);
191 
192    strncpy (str2, nam, (size_t) LEN);
193    strncat (str2, ", p-value for discrete statistic", (size_t) LEN - len1);
194    res->PVal2 = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_pVal2, 0);
195 
196    ftab_InitMatrix (res->PLeft, -1.0);
197    ftab_InitMatrix (res->PRight, -1.0);
198    ftab_InitMatrix (res->PVal2, -1.0);
199 
200    for (i = 0; i < Nr; i++) {
201       res->PLeft->LSize[i] = fam->LSize[i];
202       res->PRight->LSize[i] = fam->LSize[i];
203       res->PVal2->LSize[i] = fam->LSize[i];
204    }
205 }
206 
207 
208 /*-------------------------------------------------------------------------*/
209 
fres_CreateDisc(void)210 fres_Disc * fres_CreateDisc (void)
211 {
212    fres_Disc *res;
213 
214    res = util_Malloc (sizeof (fres_Disc));
215    res->name = util_Calloc (1, sizeof (char));
216 
217    res->PLeft = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal1, 0);
218    res->PRight = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal1, 0);
219    res->PVal2 = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal2, 0);
220    return res;
221 }
222 
223 
224 /*-------------------------------------------------------------------------*/
225 
fres_DeleteDisc(fres_Disc * res)226 void fres_DeleteDisc (fres_Disc *res)
227 {
228    if (res == NULL)
229       return;
230    res->name = util_Free (res->name);
231    ftab_DeleteTable (res->PVal2);
232    ftab_DeleteTable (res->PRight);
233    ftab_DeleteTable (res->PLeft);
234    util_Free (res);
235 }
236 
237 
238 /*=========================================================================*/
239 
fres_PrintDisc(fres_Disc * res,lebool LR)240 void fres_PrintDisc (fres_Disc *res, lebool LR)
241 {
242    if (LR) {
243       ftab_PrintTable (res->PLeft);
244       ftab_PrintTable (res->PRight);
245    }
246    ftab_PrintTable (res->PVal2);
247 }
248 
249 
250 /*=========================================================================*/
251 
fres_FillTableEntryD(fres_Disc * fres,double pLeft,double pRight,double pVal2,int i,int j)252 void fres_FillTableEntryD (fres_Disc *fres,
253    double pLeft, double pRight, double pVal2, int i, int j)
254 /*
255  * Writes the results of one test in the tables.
256  */
257 {
258    fres->PLeft->Mat[i][j] = pLeft;
259    fres->PRight->Mat[i][j] = pRight;
260    fres->PVal2->Mat[i][j] = pVal2;
261 }
262 
263 
264 /*=========================================================================*/
265 
fres_InitPoisson(ffam_Fam * fam,fres_Poisson * res,int Nr,int f1,int f2,int fstep,char * nam)266 void fres_InitPoisson (ffam_Fam *fam, fres_Poisson *res,
267    int Nr, int f1, int f2, int fstep, char *nam)
268 {
269    char str[LEN + 1] = {0};
270    char str2[LEN + 1] = {0};
271    size_t len1;
272    int i;
273 
274    res->name = util_Realloc (res->name, 1 + strlen (nam) * sizeof (char));
275    strcpy (res->name, nam);
276    strncpy (str, nam, (size_t) LEN);
277    len1 = strlen (nam);
278 
279    Nr = util_Min (Nr, fam->Ng);
280 
281    ftab_DeleteTable (res->Obs);
282    ftab_DeleteTable (res->Exp);
283    ftab_DeleteTable (res->PVal2);
284    ftab_DeleteTable (res->PRight);
285    ftab_DeleteTable (res->PLeft);
286 
287    strncpy (str2, nam, (size_t) LEN);
288    strncat (str2, ", Expected numbers", (size_t) LEN - len1);
289    res->Exp = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_Real, 0);
290 
291    strncpy (str2, nam, (size_t) LEN);
292    strncat (str2, ", Observed numbers", (size_t) LEN - len1);
293    res->Obs = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_Integer, 0);
294 
295    strncpy (str2, nam, (size_t) LEN);
296    strncat (str2, ", Left p-value", (size_t) LEN - len1);
297    res->PLeft = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_pVal1, 0);
298 
299    strncpy (str2, nam, (size_t) LEN);
300    strncat (str2, ", Right p-value", (size_t) LEN - len1);
301    res->PRight = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_pVal1, 0);
302 
303    strncpy (str2, nam, (size_t) LEN);
304    strncat (str2, ", p-value for discrete statistic", (size_t) LEN - len1);
305    res->PVal2 = ftab_CreateTable (Nr, f1, f2, fstep, str2, ftab_pVal2, 0);
306 
307    ftab_InitMatrix (res->Exp, -1.0);
308    ftab_InitMatrix (res->Obs, -1.0);
309    ftab_InitMatrix (res->PLeft, -1.0);
310    ftab_InitMatrix (res->PRight, -1.0);
311    ftab_InitMatrix (res->PVal2, -1.0);
312 
313    for (i = 0; i < Nr; i++) {
314       res->PLeft->LSize[i] = fam->LSize[i];
315       res->PRight->LSize[i] = fam->LSize[i];
316       res->PVal2->LSize[i] = fam->LSize[i];
317       res->Exp->LSize[i] = fam->LSize[i];
318       res->Obs->LSize[i] = fam->LSize[i];
319    }
320 }
321 
322 
323 /*-------------------------------------------------------------------------*/
324 
fres_CreatePoisson(void)325 fres_Poisson * fres_CreatePoisson (void)
326 {
327    fres_Poisson *res;
328 
329    res = util_Malloc (sizeof (fres_Poisson));
330    res->name = util_Calloc (1, sizeof (char));
331 
332    res->Obs = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal1, 0);
333    res->Exp = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal1, 0);
334    res->PLeft = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal1, 0);
335    res->PRight = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal1, 0);
336    res->PVal2 = ftab_CreateTable (1, 0, 1, 1, "", ftab_pVal2, 0);
337    return res;
338 }
339 
340 
341 /*-------------------------------------------------------------------------*/
342 
fres_DeletePoisson(fres_Poisson * res)343 void fres_DeletePoisson (fres_Poisson *res)
344 {
345    if (res == NULL)
346       return;
347    res->name = util_Free (res->name);
348    ftab_DeleteTable (res->PVal2);
349    ftab_DeleteTable (res->PRight);
350    ftab_DeleteTable (res->PLeft);
351    ftab_DeleteTable (res->Obs);
352    ftab_DeleteTable (res->Exp);
353    util_Free (res);
354 }
355 
356 
357 /*=========================================================================*/
358 
fres_PrintPoisson(fres_Poisson * res,lebool LR,lebool Ratio)359 void fres_PrintPoisson (fres_Poisson *res, lebool LR, lebool Ratio)
360 {
361    ftab_PrintTable2 (res->Exp, res->Obs, Ratio);
362    if (LR) {
363       ftab_PrintTable (res->PLeft);
364       ftab_PrintTable (res->PRight);
365    }
366    ftab_PrintTable (res->PVal2);
367 }
368 
369 
370 /*=========================================================================*/
371 
fres_FillTableEntryPoisson(fres_Poisson * fres,double Exp,double Obs,double pLeft,double pRight,double pVal2,int i,int j)372 void fres_FillTableEntryPoisson (fres_Poisson *fres, double Exp, double Obs,
373    double pLeft, double pRight, double pVal2, int i, int j)
374 /*
375  * Writes the results of one test in the tables.
376  */
377 {
378    fres->Obs->Mat[i][j] = Obs;
379    fres->Exp->Mat[i][j] = Exp;
380    fres->PLeft->Mat[i][j] = pLeft;
381    fres->PRight->Mat[i][j] = pRight;
382    fres->PVal2->Mat[i][j] = pVal2;
383 }
384 
385 
386 /*=========================================================================*/
387