diff --git a/bilby/core/prior.py b/bilby/core/prior.py index f67c7fa770e9618343b2dae5e028f6040f086f2b..6594684b76e911977e8cfe182917a0bfb04d9646 100644 --- a/bilby/core/prior.py +++ b/bilby/core/prior.py @@ -536,6 +536,19 @@ class Prior(object): """ return np.nan + def cdf(self, val): + """ Generic method to calculate CDF, can be overwritten in subclass """ + if np.any(np.isinf([self.minimum, self.maximum])): + raise ValueError( + "Unable to use the generic CDF calculation for priors with" + "infinite support") + x = np.linspace(self.minimum, self.maximum, 1000) + pdf = self.prob(x) + cdf = cumtrapz(pdf, x, initial=0) + interp = interp1d(x, cdf, assume_sorted=True, bounds_error=False, + fill_value=(0, 1)) + return interp(val) + def ln_prob(self, val): """Return the prior ln probability of val, this should be overwritten diff --git a/bilby/core/result.py b/bilby/core/result.py index 7c02b5e4f9ee1c7f4e0a667f16e1c28aa84338cd..718f921b449d181896a503218272c3613e4bb9ed 100644 --- a/bilby/core/result.py +++ b/bilby/core/result.py @@ -679,7 +679,10 @@ class Result(object): if isinstance(prior, Prior): theta = np.linspace(ax.get_xlim()[0], ax.get_xlim()[1], 300) - ax.plot(theta, prior.prob(theta), color='C2') + if cumulative is False: + ax.plot(theta, prior.prob(theta), color='C2') + else: + ax.plot(theta, prior.cdf(theta), color='C2') if save: fig.tight_layout()