Optimizers (taurex.optimizer)¶
Base¶
Base class for optimization/retrieval.
- class DerivedParam(name: str, latex: str, fget: Callable[[], float], compute: bool)[source]¶
Bases:
objectHolds information about a derived parameter.
- compute: bool¶
- fget: Callable[[], float]¶
- latex: str¶
- name: str¶
- class FitParam(name: str, latex: str, fget: Callable[[], float], fset: Callable[[float], None], mode: Literal['linear', 'log'], to_fit: bool, bounds: Tuple[float, float], prior: Prior | None = None)[source]¶
Bases:
objectHolds information about a fitting parameter.
- property boundaries¶
Get boundaries of parameter.
- bounds: Tuple[float, float]¶
- fget: Callable[[], float]¶
- property fit_latex: str¶
Latex for fitting.
- property fit_name: str¶
Name for fitting.
- property fit_prior¶
Get prior for fitting.
- property fit_value: float¶
Get value of parameter considering its mode.
- fset: Callable[[float], None]¶
- latex: str¶
- mode: Literal['linear', 'log']¶
- name: str¶
- to_fit: bool¶
- property value: float¶
Get value of parameter.
- class FitParamOutput[source]¶
Bases:
TypedDictDictionary for storing fit parameter output.
- map: float¶
- mean: float¶
- sigma: float¶
- sigma_m: float¶
- sigma_p: float¶
- trace: float¶
- value: float¶
- class Optimizer(name: str, observed: BaseSpectrum | None = None, model: ForwardModel | None = None, sigma_fraction: float | None = 0.1)[source]¶
-
A base class that handles fitting and optimization of forward models.
The class handles the compiling and management of fitting parameters in forward models, in its current form it cannot fit and requires a class derived from it to implement the
compute_fit()function.- chisq_trans(fit_params: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], data: ndarray[tuple[int, ...], dtype[float64]], datastd: ndarray[tuple[int, ...], dtype[float64]]) float[source]¶
Computes the Chi-Squared of model and observation
Computes the Chi-Squared between the forward model and observation. The steps taken are:
Forward model (FM) is updated with
update_model()FM is then computed at its native grid then binned.
Chi-squared between FM and observation is computed
- compute_derived_trace(solution: int) Dict[str, float | int | float64 | int64 | ndarray | _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]][source]¶
Computes derived parameters from traces.
- compute_fit() Any[source]¶
Main compute fit function.
Unimplemented. When inheriting this should be overwritten to perform the actual fit.
- Raises:
NotImplementedError – Raised when a derived class does override this function
- property derived_latex: List[str]¶
Returns a list of the current values of a fitting parameter.
- property derived_names: List[str]¶
Return names for derived parameters.
- property derived_parameters: List[DerivedParam]¶
Returns the list of derived parameters.
- property derived_values: List[float]¶
Returns current values of derived parameters.
- fit(output_size=OutputSize.heavy) Dict[str, float | int | float64 | int64 | ndarray | _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]][source]¶
Performs retrieval.
- property fit_boundaries¶
Returns the fitting boundaries of the parameter
- Returns:
List of boundaries for each fitting parameter. It takes the form of a python
tuplewith the form (bound_min,bound_max)- Return type:
list
- property fit_latex: List[str]¶
Returns the names of the parameters in LaTeX format.
- property fit_names: List[str]¶
Returns the names of the model parameters we will be fitting
- Returns:
List of names of parameters that will be fit
- Return type:
list
- property fit_values: List[float]¶
Returns a list of the current values of a fitting parameter.
This respects the
modesetting- Returns:
List of each value of a fitting parameter
- Return type:
list
- property fit_values_nomode: List[float]¶
Returns a list of the current values of a fitting parameter. Regardless of the
modesetting- Returns:
List of each value of a fitting parameter
- Return type:
list
- property fitting_priors: List[Prior]¶
Returns the list of fitting priors.
This is mostly for compatibility with the old code.
- generate_profiles(solution: int, binning: ndarray[tuple[int, ...], dtype[float64]]) Tuple[Dict[str, ndarray[tuple[int, ...], dtype[float64]]], Dict[str, ndarray[tuple[int, ...], dtype[float64]]]][source]¶
Generates sigma plots for profiles
- generate_solution(output_size=OutputSize.heavy) Dict[str, float | int | float64 | int64 | ndarray | _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]][source]¶
Generates a dictionary with all solutions and other useful parameters.
- get_samples(solution_id: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Get traces for a particular solution.
- get_solution() Generator[Tuple[int, ndarray[tuple[int, ...], dtype[float64]], ndarray[tuple[int, ...], dtype[float64]], Dict[str, float | int | float64 | int64 | ndarray | _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]]], None, None][source]¶
Generator for solutions.
** Requires implementation **
Generator for solutions and their median and MAP values
- Yields:
solution_no – Solution number
map – Map values
median – Median values
extra – List of tuples of extra information to store. Must be of form
(name, data)
- get_weights(solution_id: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Get weights for a particular solution.
- log_likelihood(parameters: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) float[source]¶
Log likelihood function.
Computed as:
\[\log \mathcal{L} = -\sum_i \sigma_i \sqrt{2\pi}) - \frac{1}{2} \chi^2\]Employ mixins to override this method.
- prior_transform(cube: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) ndarray[tuple[int, ...], dtype[float64]][source]¶
Transforms a unit cube to a prior cube.
Employ mixins to override this method.
- sample_parameters(solution: int) Generator[Tuple[ndarray[tuple[int, ...], dtype[float64]], float], None, None][source]¶
Read traces and weights and return a random
sigma_fractionsample- Parameters:
solution – a solution output from sampler
- Yields:
traces (
array) – Traces of a particular sampleweight (float) – Weight of sample
- set_boundary(parameter: str, new_boundaries: List[float]) None[source]¶
Sets the boundary of the parameter.
- set_factor_boundary(parameter: str, factors: List[float]) None[source]¶
Sets the boundary of the parameter based on a factor of value.
- set_mode(parameter: str, new_mode: Literal['linear', 'log']) None[source]¶
Sets the fitting mode of a parameter.
- set_model(model: ForwardModel) None[source]¶
Sets the model to be optimized/fit
- Parameters:
model – The forward model we wish to optimize
- set_observed(observed: BaseSpectrum) None[source]¶
Sets the observation to optimize the model to.
- Parameters:
observed – Observed spectrum we will optimize to
- update_model(fit_params: List[float]) None[source]¶
Updates the model with new parameters
- Parameters:
fit_params (
list) – A list of new values to apply to the model. The list of values are assumed to be in the same order as the parameters given byfit_names()
- write(output: OutputGroup) None[source]¶
Creates ‘Optimizer’ and writes output
- Parameters:
output – Group (or root) in output file to write to
- write_fit(output: OutputGroup) OutputGroup[source]¶
Writes basic fitting parameters into output.
- Parameters:
output – Group (or root) in output file to write to
- write_optimizer(output: OutputGroup) OutputGroup[source]¶
Writes optimizer settings under the ‘Optimizer’ heading in an output file.
- Parameters:
output – Group (or root) in output file to write to
- compile_params(fitparams: Dict[str, Tuple[str, str, Callable[[], float], Callable[[float], None], Literal['linear', 'log'], bool, Tuple[float, float]]], driveparams: Dict[str, Tuple[str, str, Callable[[], float], bool]], fit_priors: Dict[str, Prior] | None = None)[source]¶
Compile fitting and derived parameters.
Nestle (taurex.optimizer.nestle)¶
Retrieval using nestle library.
- class NestleOptimizer(observed: BaseSpectrum | None = None, model: ForwardModel | None = None, num_live_points: int | None = 1500, method: Literal['single', 'multi', 'mcmc'] | None = 'multi', tol: float | None = 0.5, sigma_fraction: float | None = 0.1)[source]¶
Bases:
OptimizerAn optimizer that uses the nestle library to perform optimization.
- BIBTEX_ENTRIES = ['@misc{nestle,\n\n author = {Kyle Barbary},\n title = {Nestle sampling library},\n publisher = {GitHub},\n journal = {GitHub repository},\n year = 2015,\n howpublished = {https://github.com/kbarbary/nestle},\n }']¶
List of bibtex entries.
- get_samples(solution_idx: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Returns the samples from the fit.
- get_solution() Generator[Tuple[int, ndarray[tuple[int, ...], dtype[float64]], ndarray[tuple[int, ...], dtype[float64]], Tuple[Tuple[Literal['Statistics'], float], Tuple[Literal['fit_params'], Dict[str, FitParamOutput]], Tuple[Literal['tracedata'], ndarray[tuple[int, ...], dtype[float64]]], Tuple[Literal['weights'], ndarray[tuple[int, ...], dtype[float64]]]]], None, None][source]¶
Generator for solutions and their median and MAP values
- Yields:
solution_no – Solution number (always 0)
map – Map values
median – Median values
extra – statistics, fit_params, tracedata, weights
- get_weights(solution_idx: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Returns the weights of the samples.
- property numLivePoints: int¶
Number of live points to use in the fit.
- store_nestle_output(result: Result) NestleSolutionOutput[source]¶
This turns the output fron nestle into a dictionary
Contains summary statistics and the solution.
- property tolerance: float¶
Tolerance value for stopping the fit.
- write_fit(output: OutputGroup) OutputGroup[source]¶
Writes the fit to the output group.
- write_optimizer(output: OutputGroup) OutputGroup[source]¶
Writes the optimizer to the output group.
MultiNest (taurex.optimizer.multinest)¶
Optimizer using multinest library.
- class MultiNestOptimizer(multi_nest_path: str | bytes | PathLike | Path | None = None, observed: BaseSpectrum | None = None, model: ForwardModel | None = None, sampling_efficiency: Literal['parameter'] | None = 'parameter', num_live_points: int | None = 1500, max_iterations: int | None = 0, search_multi_modes: bool | None = True, num_params_cluster: int | None = None, maximum_modes: int | None = 100, constant_efficiency_mode: bool | None = False, evidence_tolerance: float | None = 0.5, mode_tolerance: float | None = -1e+90, importance_sampling: bool | None = False, resume: bool | None = False, only_finish: bool | None = False, n_iter_before_update: int | None = 100, multinest_prefix: str | None = '1-', verbose_output: bool | None = True, sigma_fraction: float | None = 0.1)[source]¶
Bases:
Optimizer- BIBTEX_ENTRIES = ['\n @article{ refId0,\n author = {{Buchner, J.} and {Georgakakis, A.} and {Nandra, K.} and {Hsu, L.}\n and {Rangel, C.} and {Brightman, M.} and {Merloni, A.} and {Salvato, M.}\n and {Donley, J.} and {Kocevski, D.}},\n title = {X-ray spectral modelling of the AGN obscuring region in the\n CDFS: Bayesian model selection and catalogue},\n DOI= "10.1051/0004-6361/201322971",\n url= "https://doi.org/10.1051/0004-6361/201322971",\n journal = {A\\&A},\n year = 2014,\n volume = 564,\n pages = "A125",\n month = "",\n }\n ', '\n @ARTICLE{Feroz_multinest,\n author = {{Feroz}, F. and {Hobson}, M.~P. and {Bridges}, M.},\n title = "{MULTINEST: an efficient and robust Bayesian\n inference tool for cosmology and particle physics}",\n journal = {MNRAS},\n keywords = {methods: data analysis, methods: statistical, Astrophysics},\n year = "2009",\n month = "Oct",\n volume = {398},\n number = {4},\n pages = {1601-1614},\n doi = {10.1111/j.1365-2966.2009.14548.x},\n archivePrefix = {arXiv},\n eprint = {0809.3437},\n primaryClass = {astro-ph},\n adsurl = {https://ui.adsabs.harvard.edu/abs/2009MNRAS.398.1601F},\n adsnote = {Provided by the SAO/NASA Astrophysics Data System}\n }\n ']¶
List of bibtex entries.
- generate_solution(output_size=OutputSize.heavy) Dict[str, float | int | float64 | int64 | ndarray | _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]][source]¶
Generate solution.
- get_samples(solution_idx: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Get samples from solution.
- get_solution() Generator[Tuple[int, ndarray[tuple[int, ...], dtype[float64]], ndarray[tuple[int, ...], dtype[float64]], Tuple[Tuple[Literal['Statistics'], Dict[Literal['local log-evidence', 'local log-evidence error'], float]], Tuple[Literal['fit_params'], Dict[str, NestFitParam]], Tuple[Literal['tracedata'], ndarray[tuple[int, ...], dtype[float64]]], Tuple[Literal['weights'], ndarray[tuple[int, ...], dtype[float64]]]]], None, None][source]¶
Get solution as generator.
- get_weights(solution_idx: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Get weights from solution.
- store_nest_solutions() NestOutputType[source]¶
Store the multinest results.
- write_fit(output)[source]¶
Writes basic fitting parameters into output.
- Parameters:
output – Group (or root) in output file to write to
- write_optimizer(output: OutputGroup) OutputGroup[source]¶
Write optimizer to output.
- class NestFitParam(name: str, latex: str, fget: Callable[[], float], fset: Callable[[float], None], mode: Literal['linear', 'log'], to_fit: bool, bounds: Tuple[float, float], prior: Prior | None = None)[source]¶
Bases:
FitParamFit parameter for multinest.
- class NestMarginalOutput¶
Bases:
dictMarginal output.
- class NestModeStatsOutput¶
Bases:
dictNested Mode stats output
- class NestStatsOutput¶
Bases:
dict