Skip to content

Commit

Permalink
compute the noise uncertainties only at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
abhisrkckl committed Oct 28, 2023
1 parent 2977542 commit 2d511c6
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/pint/fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ def _fit_toas(
def fit_toas(
self,
maxiter=20,
noise_fit_niter=5,
noise_fit_niter=3,
required_chi2_decrease=1e-2,
max_chi2_increase=1e-2,
min_lambda=1e-3,
Expand Down Expand Up @@ -1345,16 +1345,22 @@ def fit_toas(
)
else:
log.debug("Will fit for noise parameters.")
for _ in range(noise_fit_niter):
for idx in range(noise_fit_niter):
self._fit_toas(
maxiter=maxiter,
required_chi2_decrease=required_chi2_decrease,
max_chi2_increase=max_chi2_increase,
min_lambda=min_lambda,
debug=debug,
)
values, errors = self._fit_noise(noisefit_method=noisefit_method)
self._update_noise_params(values, errors)

compute_noise_uncertainties = idx == noise_fit_niter - 1
values, errors = self._fit_noise(
noisefit_method=noisefit_method,
compute_uncertainties=compute_noise_uncertainties,
)
if compute_noise_uncertainties:
self._update_noise_params(values, errors)

return self._fit_toas(
maxiter=maxiter,
Expand Down Expand Up @@ -1383,7 +1389,7 @@ def _update_noise_params(self, values, errors):
getattr(self.model, fp).value = val
getattr(self.model, fp).uncertainty_value = err

def _fit_noise(self, noisefit_method="Newton-CG"):
def _fit_noise(self, noisefit_method="Newton-CG", compute_uncertainties=True):
"""Estimate noise parameters and their uncertainties. Noise parameters
are estimated by numerically maximizing the log-likelihood function including
the normalization term. The uncertainties thereof are computed using the
Expand Down Expand Up @@ -1418,8 +1424,11 @@ def _mloglike_grad(xs):
_mloglike, xs0, method=noisefit_method, jac=_mloglike_grad
)

hess = Hessdiag(_mloglike)
errs = np.sqrt(1 / hess(maxlike_result.x))
if compute_uncertainties:
hess = Hessdiag(_mloglike)
errs = np.sqrt(1 / hess(maxlike_result.x))
else:
errs = None

return maxlike_result.x, errs

Expand Down

0 comments on commit 2d511c6

Please sign in to comment.