From c7c57cc671dfac7483d34c4a340c563d9edd03fd Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Thu, 26 Aug 2021 14:39:18 +0000
Subject: [PATCH] Resolve "bilby_mcmc not able to use priors with infinite
 support"

---
 bilby/bilby_mcmc/proposals.py     | 15 ++++++++++++---
 test/bilby_mcmc/test_proposals.py |  4 ++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/bilby/bilby_mcmc/proposals.py b/bilby/bilby_mcmc/proposals.py
index 4d1d33bdf..99daa824f 100644
--- a/bilby/bilby_mcmc/proposals.py
+++ b/bilby/bilby_mcmc/proposals.py
@@ -313,6 +313,10 @@ class DifferentialEvolutionProposal(BaseProposal):
 class UniformProposal(BaseProposal):
     """A proposal using uniform draws from the prior support
 
+    Note: for priors with infinite support, this proposal will not propose a
+    point, leading to inefficient sampling. You may wish to omit this proposal
+    if you have priors with infinite support.
+
     Parameters
     ----------
     priors: bilby.core.prior.PriorDict
@@ -330,9 +334,14 @@ class UniformProposal(BaseProposal):
     def propose(self, chain):
         sample = chain.current_sample
         for key in self.parameters:
-            sample[key] = np.random.uniform(
-                self.prior_minimum_dict[key], self.prior_maximum_dict[key]
-            )
+            width = self.prior_width_dict[key]
+            if np.isinf(width) is False:
+                sample[key] = np.random.uniform(
+                    self.prior_minimum_dict[key], self.prior_maximum_dict[key]
+                )
+            else:
+                # Unable to generate a uniform sample on infinite support
+                pass
         log_factor = 0
         return sample, log_factor
 
diff --git a/test/bilby_mcmc/test_proposals.py b/test/bilby_mcmc/test_proposals.py
index 08f37b1cd..3bb70b168 100644
--- a/test/bilby_mcmc/test_proposals.py
+++ b/test/bilby_mcmc/test_proposals.py
@@ -30,6 +30,8 @@ class TestBaseProposals(unittest.TestCase):
             for i in range(ndim)
         })
         priors["fixedA"] = bilby.core.prior.DeltaFunction(1)
+        priors["infinite_support"] = bilby.core.prior.Normal(0, 1)
+        priors["half_infinite_support"] = bilby.core.prior.HalfNormal(1)
         return priors
 
     def create_random_sample(self, ndim=2):
@@ -37,6 +39,8 @@ class TestBaseProposals(unittest.TestCase):
         p[LOGLKEY] = np.random.normal(0, 1)
         p[LOGPKEY] = -1
         p["fixedA"] = 1
+        p["infinite_support"] = np.random.normal(0, 1)
+        p["half_infinite_support"] = np.abs(np.random.normal(0, 1))
         return Sample(p)
 
     def create_chain(self, n=1000, ndim=2):
-- 
GitLab