From d4aecb3ccc1c4f4b5e67e3974c68034d98447613 Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Fri, 25 Nov 2016 16:06:08 +0100
Subject: [PATCH] Initial commit of Directed MC study

---
 Paper/DirectedMC/generate_data.py | 80 +++++++++++++++++++++++++++++++
 Paper/DirectedMC/plot_data.py     | 45 +++++++++++++++++
 Paper/DirectedMC/plot_recovery.py | 30 ++++++++++++
 Paper/DirectedMC/repeat.sh        |  5 ++
 4 files changed, 160 insertions(+)
 create mode 100644 Paper/DirectedMC/generate_data.py
 create mode 100644 Paper/DirectedMC/plot_data.py
 create mode 100644 Paper/DirectedMC/plot_recovery.py
 create mode 100755 Paper/DirectedMC/repeat.sh

diff --git a/Paper/DirectedMC/generate_data.py b/Paper/DirectedMC/generate_data.py
new file mode 100644
index 0000000..9feb16e
--- /dev/null
+++ b/Paper/DirectedMC/generate_data.py
@@ -0,0 +1,80 @@
+import pyfstat
+import numpy as np
+import os
+
+# Properties of the GW data
+sqrtSX = 2e-23
+tstart = 1000000000
+Tspan = 100*86400
+tend = tstart + Tspan
+
+# Fixed properties of the signal
+F0_center = 30
+F1_center = 1e-10
+F2 = 0
+Alpha = 5e-3
+Delta = 6e-2
+tref = .5*(tstart+tend)
+
+data_label = 'temp_data_{}'.format(os.getpid())
+results_file_name = 'MCResults.txt'
+
+VF0 = VF1 = 100
+DeltaF0 = VF0 * np.sqrt(3)/(np.pi*Tspan)
+DeltaF1 = VF1 * np.sqrt(45/4.)/(np.pi*Tspan**2)
+
+depths = np.linspace(100, 250, 13)
+
+run_setup = [((10, 0), 16, False),
+             ((10, 0), 5, False),
+             ((10, 10), 1, False)]
+for depth in depths:
+    h0 = sqrtSX / float(depth)
+    r = np.random.uniform(0, 1)
+    theta = np.random.uniform(0, 2*np.pi)
+    F0 = F0_center + 3*np.sqrt(r)*np.cos(theta)/(np.pi**2 * Tspan**2)
+    F1 = F1_center + 45*np.sqrt(r)*np.sin(theta)/(4*np.pi**2 * Tspan**4)
+
+    psi = np.random.uniform(-np.pi/4, np.pi/4)
+    phi = np.random.uniform(0, 2*np.pi)
+    cosi = np.random.uniform(-1, 1)
+
+    data = pyfstat.Writer(
+        label=data_label, outdir='data', tref=tref,
+        tstart=tstart, F0=F0, F1=F1, F2=F2, duration=Tspan, Alpha=Alpha,
+        Delta=Delta, h0=h0, sqrtSX=sqrtSX, psi=psi, phi=phi, cosi=cosi,
+        detector='H1,L1')
+    data.make_data()
+    predicted_twoF = data.predict_fstat()
+
+    theta_prior = {'F0': {'type': 'unif',
+                          'lower': F0-DeltaF0/2.,
+                          'upper': F0+DeltaF0/2.},
+                   'F1': {'type': 'unif',
+                          'lower': F1-DeltaF1/2.,
+                          'upper': F1+DeltaF1/2.},
+                   'F2': F2,
+                   'Alpha': Alpha,
+                   'Delta': Delta
+                   }
+
+    ntemps = 1
+    log10temperature_min = -1
+    nwalkers = 50
+    nsteps = [50, 50]
+
+    mcmc = pyfstat.MCMCFollowUpSearch(
+        label='temp', outdir='data',
+        sftfilepath='data/*'+data_label+'*sft', theta_prior=theta_prior,
+        tref=tref, minStartTime=tstart, maxStartTime=tend, nsteps=nsteps,
+        nwalkers=nwalkers, ntemps=ntemps,
+        log10temperature_min=log10temperature_min)
+    mcmc.run(run_setup=run_setup, create_plots=False, log_table=False,
+             gen_tex_table=False)
+    d, maxtwoF = mcmc.get_max_twoF()
+    dF0 = F0 - d['F0']
+    dF1 = F1 - d['F1']
+    with open(results_file_name, 'a') as f:
+        f.write('{} {:1.8e} {:1.8e} {:1.8e} {:1.8e} {:1.8e}\n'
+                .format(depth, h0, dF0, dF1, predicted_twoF, maxtwoF))
+    os.system('rm data/temp*')
diff --git a/Paper/DirectedMC/plot_data.py b/Paper/DirectedMC/plot_data.py
new file mode 100644
index 0000000..63da41c
--- /dev/null
+++ b/Paper/DirectedMC/plot_data.py
@@ -0,0 +1,45 @@
+import matplotlib.pyplot as plt
+import pandas as pd
+import numpy as np
+import scipy.stats
+
+Tspan = 100 * 86400
+
+
+def Recovery(Tspan, Depth, twoFstar=60):
+    rho2 = 4*Tspan/25./Depth**2
+    twoF_Hs = scipy.stats.distributions.ncx2(df=4, nc=rho2)
+    return 1 - twoF_Hs.cdf(twoFstar)
+
+results_file_name = 'MCResults.txt'
+
+df = pd.read_csv(
+    results_file_name, sep=' ', names=['depth', 'h0', 'dF0', 'dF1',
+                                       'twoF_predicted', 'twoF'])
+
+twoFstar = 60
+depths = np.unique(df.depth.values)
+recovery_fraction = []
+recovery_fraction_std = []
+for d in depths:
+    twoFs = df[df.depth == d].twoF.values
+    print d, len(twoFs)
+    n = float(len(twoFs))
+    rf = np.sum(twoFs > twoFstar) / n
+    rf_bars = [np.sum(np.concatenate((twoFs[:i-1], twoFs[i:]))>twoFstar)/(n-1)
+               for i in range(int(n))]
+    var = (n-1)/n * np.sum([(rf_bar - rf)**2 for rf_bar in rf_bars])
+    recovery_fraction.append(rf)
+    recovery_fraction_std.append(np.sqrt(var))
+
+fig, ax = plt.subplots()
+ax.errorbar(depths, recovery_fraction, yerr=recovery_fraction_std)
+#ax.plot(depths, recovery_fraction)
+
+depths_smooth = np.linspace(min(depths), max(depths), 100)
+recovery_analytic = [Recovery(Tspan, d) for d in depths_smooth]
+ax.plot(depths_smooth, recovery_analytic)
+
+ax.set_xlabel(r'Signal depth', size=16)
+ax.set_ylabel('Recovered fraction [%]', size=12)
+fig.savefig('directed_recovery.png')
diff --git a/Paper/DirectedMC/plot_recovery.py b/Paper/DirectedMC/plot_recovery.py
new file mode 100644
index 0000000..cdcc231
--- /dev/null
+++ b/Paper/DirectedMC/plot_recovery.py
@@ -0,0 +1,30 @@
+import matplotlib.pyplot as plt
+import numpy as np
+import scipy.stats
+
+
+def Recovery(Tspan, Depth, twoFstar=60):
+    rho2 = 4*Tspan/25./Depth**2
+    twoF_Hs = scipy.stats.distributions.ncx2(df=4, nc=rho2)
+    return 1 - twoF_Hs.cdf(twoFstar)
+
+N = 500
+Tspan = np.linspace(0.1, 365*86400, N)
+Depth = np.linspace(10, 300, N)
+
+X, Y = np.meshgrid(Tspan, Depth)
+X = X / 86400
+
+Z = [[Recovery(t, d) for t in Tspan] for d in Depth]
+
+fig, ax = plt.subplots()
+pax = ax.pcolormesh(X, Y, Z, cmap=plt.cm.viridis)
+CS = ax.contour(X, Y, Z, [0.95])
+plt.clabel(CS, inline=1, fontsize=12, fmt='%s', manual=[(200, 180)])
+plt.colorbar(pax, label='Recovery fraction')
+ax.set_xlabel(r'$T_{\rm span}$ [days]', size=16)
+ax.set_ylabel(r'Depth=$\frac{\sqrt{S_{\rm n}}}{h_0}$', size=14)
+ax.set_xlim(min(Tspan)/86400., max(Tspan)/86400.)
+ax.set_ylim(min(Depth), max(Depth))
+
+fig.savefig('recovery.png')
diff --git a/Paper/DirectedMC/repeat.sh b/Paper/DirectedMC/repeat.sh
new file mode 100755
index 0000000..8038f08
--- /dev/null
+++ b/Paper/DirectedMC/repeat.sh
@@ -0,0 +1,5 @@
+for ((n=0;n<100;n++))
+do
+echo $n
+python generate_data.py --quite --no-template-counting
+done
-- 
GitLab