2 A script to run lalpulsar_parameter_estimation_nested with increasing amplitude prior
3 ranges. For a range of h0 ranges the nested sampling will be run and the odds ratio extracted.
4 It will calculate the value of the log(odds ratio)-log(prior) and check that it is roughly
5 flat as the prior range increases.
12 import subprocess
as sp
15 if os.environ[
"LALINFERENCE_ENABLED"] ==
"false":
16 print(
"Skipping test: requires LALInference")
20 import matplotlib
as mpl
23 import matplotlib.pyplot
as pl
26 except ModuleNotFoundError:
27 print(
"matplotlib unavailable; skipping plot")
32 execu =
"./lalpulsar_parameter_estimation_nested"
35 os.environ[
"LAL_DEBUG_LEVEL"] = os.environ[
"LAL_DEBUG_LEVEL"].replace(
"memdbg",
"")
36 print(
"Modified LAL_DEBUG_LEVEL='%s'" % os.environ[
"LAL_DEBUG_LEVEL"])
54 datafile =
"data.txt.gz"
57 startgps = 900000000.0
58 endgps = startgps + dt * (dlen - 1)
59 ds = np.zeros((dlen, 3))
60 ds[:, 0] = np.linspace(startgps, endgps, dlen)
62 ds[:, -2:] = noisesigma * np.random.randn(dlen, 2)
64 ulest = 10.8 * np.sqrt(noisesigma**2 / dlen)
65 print(
"Estimated upper limit is %.4e" % (ulest))
68 np.savetxt(datafile, ds, fmt=
"%.12e")
71 h0uls = np.logspace(np.log10(5.0 * ulest), np.log10(500.0 * ulest), 6)
78 outfile_SNR =
"test_SNR"
79 outfile_Znoise =
"test_Znoise"
83 proposals = [
"",
"--ensembleWalk 1 --uniformprop 0"]
84 labels = [
"Default",
"Walk"]
86 max_nsigma = [2.0, 3.0]
88 for i, proplabel
in enumerate(labels):
89 if __file__.endswith(
"_%s.py" % proplabel.lower()):
90 print(f
"Running {__file__} with proposal={proplabel} extracted from filename")
91 proposals = proposals[i : i + 1]
92 labels = labels[i : i + 1]
93 pcolor = pcolor[i : i + 1]
94 max_nsigma = max_nsigma[i : i + 1]
97 print(f
"Running {__file__} with full proposal list")
102 fig, ax = pl.subplots(1, 1)
104 walltime = time.time()
106 for i, prop
in enumerate(proposals):
111 for h, h0ul
in enumerate(h0uls):
113 priorfile =
"H0 uniform 0 %e\n" % h0ul
115 priorf =
"test.prior"
116 f = open(priorf,
"w")
120 logprior = -np.log(h0ul * np.pi * 2.0 * (np.pi / 2.0))
121 logpriors.append(logprior)
125 for j
in range(Ntests):
126 elapsed_walltime = time.time() - walltime
128 "--- proposal=%i/%i h0=%i/%i test=%i/%i elapsed=%0.1fs ---"
143 "%s --detectors %s --par-file %s --input-files %s --outfile %s --prior-file %s --Nlive %d --Nmcmcinitial %d %s"
157 sp.check_call(commandline, shell=
True)
160 f = h5py.File(outfile,
"r")
161 a = f[
"lalinference"][
"lalinference_nest"]
162 hodds.append(a.attrs[
"log_bayes_factor"])
166 for fs
in (outfile, outfile_SNR, outfile_Znoise):
169 odds_prior.append(np.mean(hodds) - logprior)
170 std_odds_prior.append(np.std(hodds))
173 ns = np.array(odds_prior)
174 p = np.sum((ns - np.mean(ns)) ** 2 / np.array(std_odds_prior) ** 2) / float(
177 stdchi = np.sqrt(2.0 * float(len(h0uls))) / float(
180 nsigma = np.abs(p - 1.0) / stdchi
182 print(
"Reduced chi-squared test for linear relation = %f" % (p))
184 if nsigma > max_nsigma[i]:
186 "This is potentially significantly (%f sigma > %f max sigma) different from a flat line"
187 % (nsigma, max_nsigma[i])
194 -np.array(logpriors),
201 ax.set_xlabel(
"log(prior volume)")
202 ax.set_ylabel(
"log(odds ratio)-log(prior)")
206 fig.savefig(
"odds_prior.png")
207 print(
"Saved plot to 'odds_prior.png'")
210 for fs
in (priorf, parf, datafile):