LAL  7.5.0.1-89842e6
SkymapTest.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <math.h>
3 #include <time.h>
4 #include <stdlib.h>
5 
6 #include <lal/LALConstants.h>
7 #include <lal/Skymap.h>
8 #include <lal/Random.h>
9 #include <lal/Sort.h>
10 
11 #ifdef __GNUC__
12 #define UNUSED __attribute__ ((unused))
13 #else
14 #define UNUSED
15 #endif
16 
17 static void numericApply(
18  XLALSkymapPlanType* plan,
20  double* wSw,
21  XLALSkymapKernelType UNUSED *kernel,
22  double** xSw,
23  double tau,
24  double* logPosterior
25  )
26 {
27  // this function has almost the same interface as XLALSkymap2Apply,
28  // but is implemented with numerical integration that should converge
29  // on the result from XLALSkymap2Apply
30 
31  // parameters
32  double a[2];
33 
34  // the step over them
35  double da = 0.01;
36 
37  // accumulate probability in this
38  double p = 0.0;
39 
40  // range from +/- 5 sigma in prior on a[i] for adequate accuracy
41  for (a[0] = -5.0; a[0] <= 5.0; a[0] += da)
42  {
43  for (a[1] = - 5.0; a[1] <= 5.0; a[1] += da)
44  {
45  double x[XLALSKYMAP_N];
46 
47  // start accumulating inner products with the priors
48  double q = (a[0] * a[0] + a[1] * a[1]);
49 
50  // for each detector...
51  int j;
52  for (j = 0; j != plan->n; ++j)
53  {
54  int k;
55  // get the time-shifted data
56  x[j] = XLALSkymapInterpolate((tau + properties->delay[j]) * plan->sampleFrequency, xSw[j]);
57 
58  // subtract the inner product
59  q -= x[j] * x[j] / wSw[j];
60  // for each polarization
61  for (k = 0; k != 2; ++k)
62  {
63  // subtract the postulated signal out
64  x[j] -= a[k] * properties->f[j][k] * wSw[j];
65  }
66  // add the inner product after signal removed
67  q += x[j] * x[j] / wSw[j];
68  }
69  // accumulate the piece of the integral
70  p += da * da * exp(-0.5*q);
71  }
72  }
73 
74  // normalization from the priors on a
75  p *= pow(LAL_TWOPI, -1);
76 
77  // return the log
78  *logPosterior = log(p);
79 
80 }
81 
82 static void numerical(void)
83 {
84  XLALSkymapPlanType plan;
85  double direction[2];
87  double wSw[5] = { 100., 100., 100., 100., 100. };
88  XLALSkymapKernelType kernel;
89  double *xSw[5];
91  RandomParams* rng;
92  int n;
93 
94  rng = XLALCreateRandomParams(0);
95 
96  for (n = 1; n != 6; ++n)
97  {
98 
99  XLALSkymapPlanConstruct(8192, n, siteNumbers, &plan);
100 
101  direction[0] = LAL_PI * XLALUniformDeviate(rng);
102  direction[1] = LAL_TWOPI * XLALUniformDeviate(rng);
103 
104  XLALSkymapDirectionPropertiesConstruct(&plan, direction, &properties);
105 
106  XLALSkymapKernelConstruct(&plan, &properties, wSw, &kernel);
107 
108  {
109  int i;
110 
111  for (i = 0; i != n; ++i)
112  {
113  int j;
114  xSw[i] = malloc(sizeof(*xSw[i]) * plan.sampleFrequency);
115  for (j = 0; j != plan.sampleFrequency; ++j)
116  {
117  xSw[i][j] = XLALNormalDeviate(rng) * sqrt(wSw[i]);
118  }
119  }
120  }
121 
122  {
123  double logPosteriorAnalytic;
124  double logPosteriorNumerical;
125 
126  XLALSkymapApply(&plan, &properties, &kernel, xSw, 0.5, &logPosteriorAnalytic);
127 
128  numericApply(&plan, &properties, wSw, &kernel, xSw, 0.5, & logPosteriorNumerical);
129 
130  if (fabs(logPosteriorAnalytic - logPosteriorNumerical) > 1e-3)
131  {
132  fprintf(stderr, "Analytic expression does not match numerical result to expected accuracy\n");
133  exit(1);
134  }
135 
136  }
137 
138  {
139  int i;
140  for(i = 0; i != n; ++i)
141  free(xSw[i]);
142  }
143 
144  }
145 
146 }
147 
148 static void interpolation(void)
149 {
150  double* x;
151  int n;
152 
153  n = 1000;
154 
155  x = (double*) malloc(sizeof(double) * n);
156 
157  for (int i = 0; i != n; ++i)
158  {
159  x[i] = sin(i);
160  }
161 
162  for (double t = n * 0.25; t < n * 0.75; t += 0.1)
163  {
164  if (fabs(XLALSkymapInterpolate(t, x) - sin(t)) > 0.03)
165  {
166  fprintf(stderr, "Interpolation error larger than expected\n");
167  exit(1);
168  }
169  }
170 
171  for (int t = n / 4; t < (n * 3) / 4; ++t)
172  {
173  if (fabs(XLALSkymapInterpolate(t, x) - sin(t)) > LAL_REAL8_EPS)
174  {
175  fprintf(stderr, "Interpolation does not pass through data points\n");
176  exit(1);
177  }
178  }
179 }
180 
181 static void uncertain(void)
182 {
183 
184  // Check that the kernel with calibration error goes to the simple kernel for zero
185  // error
186 
187  XLALSkymapPlanType plan;
188  double direction[2];
190  double wSw[5] = { 100., 100., 100., 100., 100. };
191  XLALSkymapKernelType kernel;
192  double error[5] = { 0., 0., 0., 0., 0. };
193  XLALSkymapKernelType kernel2;
195  RandomParams* rng;
196  int n;
197 
198  rng = XLALCreateRandomParams(0);
199 
200  for (n = 3; n != 4; ++n)
201  {
202  XLALSkymapPlanConstruct(8192, n, siteNumbers, &plan);
203 
204  direction[0] = LAL_PI * XLALUniformDeviate(rng);
205  direction[1] = LAL_TWOPI * XLALUniformDeviate(rng);
206  XLALSkymapDirectionPropertiesConstruct(&plan, direction, &properties);
207 
208  XLALSkymapKernelConstruct(&plan, &properties, wSw, &kernel);
209  XLALSkymapUncertainKernelConstruct(&plan, &properties, wSw, error, &kernel2);
210 
211  for (int i = 0; i != n; ++i)
212  {
213  for (int j = 0; j != n; ++j)
214  {
215  // fprintf(stderr, "%e =?= %e ", kernel.k[i][j], kernel2.k[i][j]);
216  if (fabs(kernel.k[i][j] - kernel2.k[i][j]) > 1e-10)
217  {
218  fprintf(stderr, "Loose kernel does not match simple kernel for zero error\n");
219  exit(1);
220  }
221  }
222  // fprintf(stderr, "\n");
223  }
224  // fprintf(stderr, "%e =?= %e\n", kernel.logNormalization, kernel2.logNormalization);
225  if (fabs(kernel.logNormalization - kernel2.logNormalization) > 1e-10)
226  {
227  fprintf(stderr, "Loose normalization does not match simple normalization for zero error\n");
228  exit(1);
229  }
230  }
231 }
232 
233 
234 int main(void)
235 {
236 
237  // check the fast analytic bayesian statistic against simpler but
238  // slower numerical integration
239 
240  numerical();
241 
242  // check the quality of the interpolation
243 
244  interpolation();
245 
246  // check that for zero error the uncertain statistic goes to the exact statistic
247 
248  uncertain();
249 
250  return 0;
251 }
252 
void XLALSkymapDirectionPropertiesConstruct(XLALSkymapPlanType *plan, double directions[2], XLALSkymapDirectionPropertiesType *properties)
Definition: Skymap.c:238
void XLALSkymapUncertainKernelConstruct(XLALSkymapPlanType *plan, XLALSkymapDirectionPropertiesType *properties, double *wSw, double *error, XLALSkymapKernelType *kernel)
Definition: Skymap.c:332
double XLALSkymapInterpolate(double t, double *x)
Definition: Skymap.c:180
void XLALSkymapApply(XLALSkymapPlanType *plan, XLALSkymapDirectionPropertiesType *properties, XLALSkymapKernelType *kernel, double **xSw, double tau, double *posterior)
Definition: Skymap.c:408
void XLALSkymapPlanConstruct(int sampleFrequency, int n, int *detectors, XLALSkymapPlanType *plan)
Definition: Skymap.c:219
void XLALSkymapKernelConstruct(XLALSkymapPlanType *plan, XLALSkymapDirectionPropertiesType *properties, double *wSw, XLALSkymapKernelType *kernel)
Definition: Skymap.c:265
#define XLALSKYMAP_N
Definition: Skymap.h:45
static void numericApply(XLALSkymapPlanType *plan, XLALSkymapDirectionPropertiesType *properties, double *wSw, XLALSkymapKernelType UNUSED *kernel, double **xSw, double tau, double *logPosterior)
Definition: SkymapTest.c:17
static void numerical(void)
Definition: SkymapTest.c:82
int main(void)
Definition: SkymapTest.c:234
static void uncertain(void)
Definition: SkymapTest.c:181
static void interpolation(void)
Definition: SkymapTest.c:148
#define fprintf
static REAL8TimeSeries * error(const REAL8TimeSeries *s1, const REAL8TimeSeries *s0)
#define LAL_REAL8_EPS
Difference between 1 and the next resolvable REAL8 2^-52.
Definition: LALConstants.h:61
#define LAL_PI
Archimedes's constant, pi.
Definition: LALConstants.h:179
#define LAL_TWOPI
2*pi is circumference of a circle divided by its radius
Definition: LALConstants.h:180
@ LAL_LLO_4K_DETECTOR
Definition: LALDetectors.h:175
@ LAL_VIRGO_DETECTOR
Definition: LALDetectors.h:171
@ LAL_LHO_2K_DETECTOR
Definition: LALDetectors.h:173
@ LAL_GEO_600_DETECTOR
Definition: LALDetectors.h:172
@ LAL_LHO_4K_DETECTOR
Definition: LALDetectors.h:174
REAL4 XLALNormalDeviate(RandomParams *params)
Definition: Random.c:234
static const INT4 q
Definition: Random.c:81
RandomParams * XLALCreateRandomParams(INT4 seed)
Definition: Random.c:102
REAL4 XLALUniformDeviate(RandomParams *params)
Definition: Random.c:146
static const INT4 a
Definition: Random.c:79
This structure contains the parameters necessary for generating the current sequence of random number...
Definition: Random.h:86
double f[XLALSKYMAP_N][2]
Definition: Skymap.h:77
double delay[XLALSKYMAP_N]
Definition: Skymap.h:78
double logNormalization
Definition: Skymap.h:93
double k[XLALSKYMAP_N][XLALSKYMAP_N]
Definition: Skymap.h:92
int sampleFrequency
Definition: Skymap.h:60