6060
6161import contextlib
6262import copy
63+ from functools import cached_property
6364from typing import Dict , List , Literal , Optional , Tuple , Union
6465from warnings import warn
65- from functools import cached_property
6666
6767import astropy .units as u
6868import numpy as np
7272from numdifftools import Hessian
7373
7474import pint
75- from pint .models .timing_model import TimingModel
7675from pint .exceptions import (
7776 ConvergenceFailure ,
7877 CorrelatedErrors ,
7978 DegeneracyWarning ,
79+ InvalidModelParameters ,
8080 MaxiterReached ,
8181 StepProblem ,
8282)
83- from pint .models .parameter import (
84- AngleParameter ,
85- InvalidModelParameters ,
86- Parameter ,
87- boolParameter ,
88- strParameter ,
89- )
83+ from pint .models .parameter import AngleParameter , Parameter , boolParameter , strParameter
84+ from pint .models .timing_model import TimingModel
9085from pint .pint_matrix import (
9186 CorrelationMatrix ,
9287 CovarianceMatrix ,
@@ -944,7 +939,7 @@ def _fit_toas(
944939 maxiter = 20 ,
945940 required_chi2_decrease = 1e-2 ,
946941 max_chi2_increase = 1e-2 ,
947- min_lambda = 1e-3 ,
942+ min_lambda = 1e-4 ,
948943 debug = False ,
949944 ) -> bool :
950945 """Downhill fit implementation for fitting the timing model parameters.
@@ -955,12 +950,11 @@ def _fit_toas(
955950 # setup
956951 self .model .validate ()
957952 self .model .validate_toas (self .toas )
953+
958954 current_state = self .create_state ()
959955 best_state = current_state
960956 self .converged = False
961- # algorithm
962957 exception = None
963-
964958 for i in range (maxiter ):
965959 step = current_state .step
966960 lambda_ = 1
@@ -973,14 +967,10 @@ def _fit_toas(
973967 best_state = new_state
974968 if chi2_decrease < - max_chi2_increase :
975969 raise InvalidModelParameters (
976- f"chi2 increased from { current_state .chi2 } to { new_state .chi2 } "
977- f"when trying to take a step with lambda { lambda_ } "
970+ f"chi2 increased from { current_state .chi2 } to { new_state .chi2 } when trying to take a step with lambda { lambda_ } "
978971 )
979972 log .trace (
980- f"Iteration { i } : "
981- f"Updating state, chi2 goes down by { chi2_decrease } "
982- f"from { current_state .chi2 } "
983- f"to { new_state .chi2 } "
973+ f"Iteration { i } : Updating state, chi2 goes down by { chi2_decrease } from { current_state .chi2 } to { new_state .chi2 } "
984974 )
985975 exception = None
986976 current_state = new_state
@@ -989,13 +979,9 @@ def _fit_toas(
989979 # This could be an exception evaluating new_state.chi2 or an increase in value
990980 # If bad parameter values escape, look in ModelState.resids for the except
991981 # that should catch them
992- lambda_ /= 2
982+ lambda_ /= 1.5
993983 log .trace (f"Iteration { i } : Shortening step to { lambda_ } : { e } " )
994984 if lambda_ < min_lambda :
995- log .warning (
996- f"Unable to improve chi2 even with very small steps, stopping "
997- f"but keeping best state, message was: { e } "
998- )
999985 exception = e
1000986 break
1001987 if (
@@ -1042,11 +1028,11 @@ def _fit_toas(
10421028 self .update_model (self .current_state .chi2 )
10431029
10441030 if exception is not None :
1045- raise StepProblem (
1046- "Unable to improve chi2 even with very small steps"
1047- ) from exception
1031+ warn ( "Unable to improve chi2 even with very small steps" , StepProblem )
1032+ return False
1033+
10481034 if not self .converged :
1049- raise MaxiterReached (f"Convergence not detected after { maxiter } steps." )
1035+ warn (f"Convergence not detected after { maxiter } steps." , MaxiterReached )
10501036
10511037 return self .converged
10521038
0 commit comments