Skip to content

Commit

Permalink
fix broken merge and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhisrkckl committed Aug 18, 2023
1 parent 48a3d13 commit 22716c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
41 changes: 22 additions & 19 deletions src/pint/bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, model, toas, use_pulse_numbers=False, prior_info=None):

self._validate_priors()

self._decide_likelihood_method()
self.likelihood_method = self._decide_likelihood_method()

self.track_mode = "use_pulse_numbers" if use_pulse_numbers else "nearest"

Expand Down Expand Up @@ -104,19 +104,16 @@ def _decide_likelihood_method(self):
)
else:
return "wls"
# return "gls"

def lnprior(self, params):
"""Basic implementation of a factorized log prior.
More complex priors must be separately implemented.
Args:
params : (array-like)
Parameters
params (array-like): Parameters
Returns:
float :
Value of the log-prior at params
float: Value of the log-prior at params
"""
if len(params) != self.nparams:
raise IndexError(
Expand All @@ -138,12 +135,11 @@ def prior_transform(self, cube):
More complex prior transforms must be separately implemented.
Args:
cube : (array-like)
Sample drawn from a uniform distribution defined in an nparams-dimensional unit hypercube.
cube (array-like): Sample drawn from a uniform distribution defined in an
nparams-dimensional unit hypercube.
Returns:
ndarray :
Sample drawn from the prior distribution
ndarray : Sample drawn from the prior distribution
"""
return np.array([param.prior._rv.ppf(x) for x, param in zip(cube, self.params)])

Expand All @@ -155,26 +151,33 @@ def lnlikelihood(self, params):
is the generalized least-squares metric. For reference, see, e.g., Lentati+ 2013.
Args:
params : (array-like)
Parameters
params (array-like): Parameters
Returns:
float :
The value of the log-likelihood at params
float: The value of the log-likelihood at params
"""
return self._lnlikelihood(params)
if self.likelihood_method == "wls":
return (
self._wls_wb_lnlikelihood(params)
if self.is_wideband
else self._wls_nb_lnlikelihood(params)
)
elif self.likelihood_method == "gls":
raise NotImplementedError(
"GLS likelihood for correlated noise is not yet implemented."
)
else:
raise ValueError(f"Unknown likelihood method '{self.likelihood_method}'.")

def lnposterior(self, params):
"""Log-posterior function. If the prior evaluates to zero, the likelihood
is not evaluated.
Args:
params : (array-like)
Parameters
params (array-like): Parameters
Returns:
float :
The value of the log-posterior at params
float: The value of the log-posterior at params
"""
lnpr = self.lnprior(params)
return lnpr + self.lnlikelihood(params) if np.isfinite(lnpr) else -np.inf
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ def test_no_noise(data_NGC6440E):
bt = BayesianTiming(model, toas)
maxlike_params = np.array([param.value for param in bt.params], dtype=float)
lnl = bt.lnlikelihood(maxlike_params)
assert bt.likelihood_method == "wls-nb" and not np.isnan(lnl)
assert bt.likelihood_method == "wls" and not np.isnan(lnl)


def test_white_noise(data_NGC6440E_efac):
model, toas = data_NGC6440E_efac
bt = BayesianTiming(model, toas)
maxlike_params = np.array([param.value for param in bt.params], dtype=float)
lnl = bt.lnlikelihood(maxlike_params)
assert bt.likelihood_method == "wls-nb" and not np.isnan(lnl)
assert bt.likelihood_method == "wls" and not np.isnan(lnl)


def test_lnlikelihood_unit_efac(data_NGC6440E, data_NGC6440E_efac):
Expand Down Expand Up @@ -182,7 +182,7 @@ def test_wideband_data(data_J0740p6620_wb):
model, toas = data_J0740p6620_wb
bt = BayesianTiming(model, toas)

assert bt.is_wideband and bt.likelihood_method == "wls-wb"
assert bt.is_wideband and bt.likelihood_method == "wls"

test_cube = 0.5 * np.ones(bt.nparams)
test_params = bt.prior_transform(test_cube)
Expand Down

0 comments on commit 22716c4

Please sign in to comment.