Note
Click here to download the full example code
MCMC search with fully coherent BSGL statisticΒΆ
Targeted MCMC search for an isolated CW signal using the fully coherent line-robust BSGL-statistic.
9 import os
10
11 import numpy as np
12
13 import pyfstat
14
15 label = os.path.splitext(os.path.basename(__file__))[0]
16 outdir = os.path.join("PyFstat_example_data", label)
17 logger = pyfstat.set_up_logger(label=label, outdir=outdir)
18
19 # Properties of the GW data - first we make data for two detectors,
20 # both including Gaussian noise and a coherent 'astrophysical' signal.
21 data_parameters = {
22 "sqrtSX": 1e-23,
23 "tstart": 1000000000,
24 "duration": 100 * 86400,
25 "detectors": "H1,L1",
26 "SFTWindowType": "tukey",
27 "SFTWindowBeta": 0.001,
28 }
29 tend = data_parameters["tstart"] + data_parameters["duration"]
30 mid_time = 0.5 * (data_parameters["tstart"] + tend)
31
32 # Properties of the signal
33 depth = 10
34 signal_parameters = {
35 "F0": 30.0,
36 "F1": -1e-10,
37 "F2": 0,
38 "Alpha": np.radians(83.6292),
39 "Delta": np.radians(22.0144),
40 "tref": mid_time,
41 "h0": data_parameters["sqrtSX"] / depth,
42 "cosi": 1.0,
43 }
44
45 data = pyfstat.Writer(
46 label=label, outdir=outdir, **data_parameters, **signal_parameters
47 )
48 data.make_data()
49
50 # Now we add an additional single-detector artifact to H1 only.
51 # For simplicity, this is modelled here as a fully modulated CW-like signal,
52 # just restricted to the single detector.
53 SFTs_H1 = data.sftfilepath.split(";")[0]
54 data_parameters_line = data_parameters.copy()
55 signal_parameters_line = signal_parameters.copy()
56 data_parameters_line["detectors"] = "H1"
57 data_parameters_line["sqrtSX"] = 0 # don't add yet another set of Gaussian noise
58 signal_parameters_line["F0"] += 1e-6
59 signal_parameters_line["h0"] *= 10.0
60 extra_writer = pyfstat.Writer(
61 label=label,
62 outdir=outdir,
63 **data_parameters_line,
64 **signal_parameters_line,
65 noiseSFTs=SFTs_H1,
66 )
67 extra_writer.make_data()
68
69 # The predicted twoF, given by lalapps_predictFstat can be accessed by
70 twoF = data.predict_fstat()
71 logger.info("Predicted twoF value: {}\n".format(twoF))
72
73 # MCMC prior ranges
74 DeltaF0 = 1e-5
75 DeltaF1 = 1e-13
76 theta_prior = {
77 "F0": {
78 "type": "unif",
79 "lower": signal_parameters["F0"] - DeltaF0 / 2.0,
80 "upper": signal_parameters["F0"] + DeltaF0 / 2.0,
81 },
82 "F1": {
83 "type": "unif",
84 "lower": signal_parameters["F1"] - DeltaF1 / 2.0,
85 "upper": signal_parameters["F1"] + DeltaF1 / 2.0,
86 },
87 }
88 for key in "F2", "Alpha", "Delta":
89 theta_prior[key] = signal_parameters[key]
90
91 # MCMC sampler settings - relatively cheap setup, may not converge perfectly
92 ntemps = 2
93 log10beta_min = -0.5
94 nwalkers = 50
95 nsteps = [100, 100]
96
97 # we'll want to plot results relative to the injection parameters
98 transform_dict = dict(
99 F0=dict(subtractor=signal_parameters["F0"], symbol="$f-f^\\mathrm{s}$"),
100 F1=dict(
101 subtractor=signal_parameters["F1"], symbol="$\\dot{f}-\\dot{f}^\\mathrm{s}$"
102 ),
103 )
104
105 # first search: standard F-statistic
106 # This should show a weak peak from the coherent signal
107 # and a larger one from the "line artifact" at higher frequency.
108 mcmc_F = pyfstat.MCMCSearch(
109 label=label + "_twoF",
110 outdir=outdir,
111 sftfilepattern=os.path.join(outdir, "*{}*sft".format(label)),
112 theta_prior=theta_prior,
113 tref=mid_time,
114 minStartTime=data_parameters["tstart"],
115 maxStartTime=tend,
116 nsteps=nsteps,
117 nwalkers=nwalkers,
118 ntemps=ntemps,
119 log10beta_min=log10beta_min,
120 BSGL=False,
121 )
122 mcmc_F.transform_dictionary = transform_dict
123 mcmc_F.run(
124 walker_plot_args={"plot_det_stat": True, "injection_parameters": signal_parameters}
125 )
126 mcmc_F.print_summary()
127 mcmc_F.plot_corner(add_prior=True, truths=signal_parameters)
128 mcmc_F.plot_prior_posterior(injection_parameters=signal_parameters)
129
130 # second search: line-robust statistic BSGL activated
131 mcmc_F = pyfstat.MCMCSearch(
132 label=label + "_BSGL",
133 outdir=outdir,
134 sftfilepattern=os.path.join(outdir, "*{}*sft".format(label)),
135 theta_prior=theta_prior,
136 tref=mid_time,
137 minStartTime=data_parameters["tstart"],
138 maxStartTime=tend,
139 nsteps=nsteps,
140 nwalkers=nwalkers,
141 ntemps=ntemps,
142 log10beta_min=log10beta_min,
143 BSGL=True,
144 )
145 mcmc_F.transform_dictionary = transform_dict
146 mcmc_F.run(
147 walker_plot_args={"plot_det_stat": True, "injection_parameters": signal_parameters}
148 )
149 mcmc_F.print_summary()
150 mcmc_F.plot_corner(add_prior=True, truths=signal_parameters)
151 mcmc_F.plot_prior_posterior(injection_parameters=signal_parameters)
Total running time of the script: ( 0 minutes 0.000 seconds)