LAL  7.5.0.1-fe68b98
XLALChisqTest.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 Jolien Creighton, Kipp Cannon, Patrick Brady
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 /*-----------------------------------------------------------------------
21  *
22  * File Name: ThresholdsTest.c
23  *
24  * Author: Eanna Flanagan
25  *
26  *
27  *-----------------------------------------------------------------------
28  *
29  * NAME
30  * main()
31  *
32  * SYNOPSIS
33  *
34  * DESCRIPTION
35  * Test suite for functions in Thresholds.c
36  *
37  * DIAGNOSTICS
38  * Writes PASS or FAIL to stdout as tests are passed or failed.
39  *
40  * CALLS
41  * LALOverlap()
42  * LALSCreateVector()
43  * LALSDestroyVector()
44  * FindRoot()
45  *
46  * NOTES
47  *
48  *-----------------------------------------------------------------------
49  */
50 
51 
52 #include <math.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <stdlib.h>
56 
57 #include <lal/LALStdlib.h>
58 #include <lal/LALgetopt.h>
59 #include <lal/LALChisq.h>
60 
61 int verbose = 1;
62 
63 
64 /*
65  * Usage()
66  *
67  * Prints a usage message for program program and exits with code exitcode.
68  */
69 
70 static void Usage(const char *program, int exitcode)
71 {
72  fprintf(stderr, "Usage: %s [options]\n", program);
73  fprintf(stderr, "Options:\n");
74  fprintf(stderr, " -h print this message\n");
75  fprintf(stderr, " -q quiet: run silently\n");
76  fprintf(stderr, " -v verbose: print extra information\n");
77  fprintf(stderr, " -d level set lalDebugLevel to level\n");
78  exit(exitcode);
79 }
80 
81 
82 /*
83  * ParseOptions()
84  *
85  * Parses the argc - 1 option strings in argv[].
86  */
87 
88 static void ParseOptions(int argc, char *argv[])
89 {
90  FILE *fp;
91  int c;
92 
93  while(1) {
94  c = LALgetopt(argc, argv, "hqvd:");
95  if(c == -1)
96  break;
97  switch(c) {
98  case 'd':
99  break;
100 
101  case 'v':
102  ++verbose;
103  break;
104 
105  case 'q':
106  fp = freopen("/dev/null", "w", stderr);
107  if (fp == NULL)
108  {
109  fprintf(stderr, "Error: Unable to open /dev/null\n");
110  exit(1);
111  }
112  fp = freopen("/dev/null", "w", stdout);
113  if (fp == NULL)
114  {
115  fprintf(stderr, "Error: Unable to open /dev/null\n");
116  exit(1);
117  }
118  break;
119 
120  case 'h':
121  Usage(argv[0], 0);
122  break;
123 
124  default:
125  Usage(argv[0], 1);
126  }
127 
128  }
129 
130  if(LALoptind < argc)
131  Usage(argv[0], 1);
132 
133  return;
134 }
135 
136 
137 /*
138  * Check the output of functions
139  */
140 
141 #define CHECKOUTPUT(msg, expr, value, acc) do { \
142  double result = expr; \
143  if(fabs((result - value) / value) > acc) { \
144  fprintf(stderr, "%s: expected %.17g, got %.17g (fractional error is %g; upto %g allowed)\n", msg, value, result, fabs((result - value) / value), acc); \
145  exit(1); \
146  } else if(verbose) { \
147  fprintf(stderr, "%s: expected %.17g, got %.17g (fractional error is %g; upto %g allowed)\n", msg, value, result, fabs((result - value) / value), acc); \
148  } \
149  if(XLALGetBaseErrno()) { \
150  fprintf(stderr, "%s: returned error\n", msg); \
151  exit(1); \
152  } \
153 } while(0)
154 
155 
156 #define CHECKXLALLogChisqCCDF(chi2, dof, value, acc) do { \
157  char msg[100]; \
158  sprintf(msg, "XLALLogChisqCCDF(%.17g, %.17g)", chi2, dof); \
159  CHECKOUTPUT(msg, XLALLogChisqCCDF(chi2, dof), value, acc); \
160 } while(0)
161 
162 
163 /*
164  * Entry point
165  */
166 
167 int main(int argc, char *argv[])
168 {
169  /*
170  * Parse the command line options
171  */
172 
173  ParseOptions(argc, argv);
174 
175  /*
176  * Check to make sure the functions return the correct values.
177  * "Correct" values obtained with Mathematica, computing
178  * intermediate results to 1000 digits. E.g., the second result
179  * can be obtained with Mathematica using the expression
180  *
181  * N[Log[1-Q[8 / 2, 0, 2.3 / 2]], 1000]
182  */
183 
184  CHECKXLALLogChisqCCDF(2., 64., -1.4417345421413976e-36, 1e-15);
185  CHECKXLALLogChisqCCDF(2.3, 8., -0.030040797756978235, 1e-15);
186  CHECKXLALLogChisqCCDF(8., 8., -0.83593241162679427, 1e-15);
187  CHECKXLALLogChisqCCDF(2.3, 0.5, -2.9095189371057191, 1e-15);
188  CHECKXLALLogChisqCCDF(1.2e3, 8., -582.59596635081904, 1e-15);
189  CHECKXLALLogChisqCCDF(2e4, 1e4, -1539.4420486763690, 1e-15);
190 
191  /*
192  * Done.
193  */
194 
195  if(verbose)
196  printf("PASS: all tests\n");
197 
198  return 0;
199 }
const char * program
int LALgetopt(int argc, char *const *argv, const char *optstring)
Definition: LALgetopt.c:172
int LALoptind
Definition: LALgetopt.c:79
#define fprintf
int verbose
Definition: XLALChisqTest.c:61
int main(int argc, char *argv[])
static void Usage(const char *program, int exitcode)
Definition: XLALChisqTest.c:70
#define CHECKXLALLogChisqCCDF(chi2, dof, value, acc)
static void ParseOptions(int argc, char *argv[])
Definition: XLALChisqTest.c:88
FILE * fp
Definition: tconvert.c:105