From 61f325b0bc2b2466f2fec9fbf2332eb603ed12aa Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Mon, 6 Nov 2017 12:51:43 +0100
Subject: [PATCH] Adds basic marginal bayes factor calculation

---
 pyfstat/grid_based_searches.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/pyfstat/grid_based_searches.py b/pyfstat/grid_based_searches.py
index 8f4a9be..61ff8d4 100644
--- a/pyfstat/grid_based_searches.py
+++ b/pyfstat/grid_based_searches.py
@@ -9,6 +9,7 @@ from collections import OrderedDict
 import numpy as np
 import matplotlib
 import matplotlib.pyplot as plt
+from scipy.misc import logsumexp
 
 import pyfstat.helper_functions as helper_functions
 from pyfstat.core import (BaseSearchClass, ComputeFstat,
@@ -707,6 +708,20 @@ class EarthTest(GridSearch):
         np.savetxt(self.out_file, data, delimiter=' ')
         self.data = data
 
+    def marginalised_bayes_factor(self, prior_widths):
+        ndims = self.data.shape[1] - 1
+        params = [np.unique(self.data[:, j]) for j in range(ndims)]
+        twoF = self.data[:, -1].reshape(tuple([len(p) for p in params]))
+        F = twoF / 2.0
+        max_F = np.max(F)
+        for i, x in enumerate(params[::-1]):
+            if len(x) > 1:
+                dx = x[1] - x[0]
+                F = logsumexp(F, axis=-1)+np.log(dx)-np.log(prior_widths[i])
+            else:
+                F = np.squeeze(F, axis=-1)
+        return np.atleast_1d(F)[0], max_F
+
 
 class DMoff_NO_SPIN(GridSearch):
     """ DMoff test using SSBPREC_NO_SPIN """
-- 
GitLab