hqs_nmr.analysis.montecarlo
Routines and tools for montecarlo minimization.
Copyright © 2025 HQS Quantum Simulations GmbH. All Rights Reserved.
Functions
|
Metropolis acceptance criterion. |
|
Run a simulated annealing minimization. |
|
Perform simulated annealing runs on decreasingly broadened signals. |
Classes
Collection of the (for now) hardcoded parameters for the multiscale simulated annealing. |
|
Collect results from a simulated annealing run. |
|
|
Class keeping track of stats in a certain window. |
- class hqs_nmr.analysis.montecarlo.HardcodedSAMultiscaleParameters
Collection of the (for now) hardcoded parameters for the multiscale simulated annealing.
- Parameters:
fit_score_power – power used when computing cumulative fit scores.
num_shifts_fit_scores – number of shifts at which to compute the fit scores.
intrisic_width_to_cfs_threshold_ratio – ratio of intrinsic width to cfs threshold. This seem to work quite well, but the connection between the two has to be investigated further, since they are clearly related.
- fit_score_power: float = 2
- num_shifts_fit_scores: int = 300
- intrisic_width_to_cfs_threshold_ratio: float = 1.3
- __init__() None
- class hqs_nmr.analysis.montecarlo.SimulatedAnnealingStatsWindow(acceptance_condition: Callable[[float, float], bool], use_last_n: int | None = None)
Class keeping track of stats in a certain window.
- Fields:
- acceptance_condition (Callable[[float, float], bool]): Function to check if a guess is
accepted.
was_accepted (deque[bool]): Deque of booleans indicating whether a guess was accepted. delta_losses (deque[float]): Deque of differences in loss between consecutive guesses. use_last_n (Optional[int]): Number of last iterations to keep track of. None means all.
- acceptance_condition: Callable[[float, float], bool]
- was_accepted: Deque[bool]
- delta_losses: Deque[float]
- use_last_n: int | None = None
- reset() None
Reset stats.
- update(delta_loss: float, temperature: float) bool
Update stats and return whether the guess has been accepted.
- Parameters:
delta_loss (float) – difference in the loss compared to best guess.
temperature (float) – current temperature.
- Returns:
whether the guess has been accepted.
- Return type:
bool
- property n_iterations: int
Total number of iterations.
- property n_iterations_up: int
Numebr of uphill iterations.
- property n_accepted: int
Total number of accepted iterations.
- property rate_accepted: float
Rate of accepted iterations.
- property n_accepted_up: int
Number of accepted uphill iterations.
- property rate_accepted_uphill: float
Rate of accepted uphill iterations recorded.
- property probability_uphill: float
Probability of uphill iterations.
- property probability_downhill: float
Probability of downhill iterations.
- property delta_losses_up: list[float]
differences of loss function between consecutive guesses.
- Type:
Positive delta losses
- __init__(acceptance_condition: Callable[[float, float], bool], use_last_n: int | None = None) None
- class hqs_nmr.analysis.montecarlo.SimulatedAnnealingResults
Collect results from a simulated annealing run.
- accepted_losses: list[float]
- rate_accepted: list[float]
- rate_accepted_uphill: list[float]
- probability_downhill: list[float]
- temperatures: list[float]
- update(loss: float, has_been_accepted: bool, rate_accepted: float, rate_accepted_uphill: float, probability_downhill: float, temperature: float) None
Update results.
- Parameters:
loss (float) – registered loss.
has_been_accepted (bool) – whether a guess has been accepted or not.
rate_accepted (float) – current rate of accepted guesses.
rate_accepted_uphill (float) – current rate of accepted uphill guesses.
probability_downhill (float) – current probability of a downhill guess.
temperature (float) – current temperature.
- __init__() None
- hqs_nmr.analysis.montecarlo.metropolis_acceptance(delta_loss: float, temperature: float) bool
Metropolis acceptance criterion.
- Parameters:
delta_loss (float) – difference in the loss function.
temperature (float) – Temperature.
- Returns:
whether the criterion is satisfied or not.
- Return type:
bool
- hqs_nmr.analysis.montecarlo.simulated_annealing(initial_guess: ~numpy.ndarray, loss_function: ~typing.Callable[[~numpy.ndarray], float], temperatures: ~numpy.ndarray, sampler_method: ~typing.Callable[[~numpy.ndarray, ~numpy.ndarray], None], acceptance_criterion: ~typing.Callable[[float, float], bool] = <function metropolis_acceptance>, collect_stats: bool = True) tuple[ndarray, SimulatedAnnealingResults]
Run a simulated annealing minimization.
- Parameters:
initial_guess (np.ndarray) – initial guess for the minimization.
loss_function (Callable[[np.ndarray], float]) – loss function to minimize.
temperatures (np.ndarray) – temperatures for the simulated annealing.
sampler_method (Callable[[np.ndarray, np.ndarray], None]) – method generating a new guess from the current guess. Signature (current_guess, preallocated_new_guess).
acceptance_criterion (Callable[[float, float], bool], optional) – acceptance criterion for new guesses. Defaults to metropolis_acceptance.
collect_stats (bool, optional) – Whether statistics about the simulated annealing should be collected. Defaults to True.
- Returns:
- best guess and statistics of the
minimization. The latter will be empty if collect_stat is False.
- Return type:
tuple[np.ndarray, SimulatedAnnealingResults]
- hqs_nmr.analysis.montecarlo.simulated_annealing_multiscale(initial_guess: ndarray, reference: ndarray, reference_x: ndarray, greens_functions_reduced: ndarray, omegas_gf: ndarray, intrinsic_width: float, shifts_boundaries: ndarray, run_parameters: MultiscaleAnnealingParams | None = None, verbose: bool = False) ndarray
Perform simulated annealing runs on decreasingly broadened signals.
This method performs consecutive simulated annealing runs on decreasingly broadened reference and simulated spectra. This helps smoothen the loss function, fixed to be the integral of the absolute value of the difference between the reference and simulated spectra. This approach provides a smoother path for the search of the best solution. It also allows for dynamic adjustments to the convergence parameters: temperature, step size, minimum temperature and cooling schedule. For example, since we always start from the best solution from the previous run, we can start a lower temperatures as we decrease the broadening: we want to avoid too much exploration since we assume we have done it in larger broadening runs.
- Parameters:
initial_guess (np.ndarray) – initial guess for the minimization.
reference (np.ndarray) – reference spectrum.
reference_x (np.ndarray) – x-axis of the reference spectrum.
greens_functions_reduced (np.ndarray) – greens functions of the simulated spectrum.
omegas_gf (np.ndarray) – frequencies of the greens functions.
intrinsic_width (float) – intrinsic width of the simulated spectrum.
shifts_boundaries (np.ndarray) – Boundaries for the shifts.
run_parameters (Optional[MultiscaleAnnealingParams]) – parameters for the multiscale run. If None, default values are used for initial and minimum temperatures and cooling rates.
verbose (bool) – controls whether to print and plot some debug information. Defaults to False.
- Returns:
best guess minimizing the loss function found by the simulated annealing.
- Return type:
np.ndarray