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
- Parameters:
fit_params (npt.ArrayLike) – Parameter values.
data (npt.NDArray[np.float64]) – Observed data.
datastd (npt.NDArray[np.float64]) – Observed data standard deviation.
- Returns:
Chi-squared value.
- Return type:
float
- 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.
- Parameters:
solution (int) – Solution index.
- Returns:
Dictionary of derived parameters.
- Return type:
t.Dict[str, AnyValType]
- 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.
- disable_derived(parameter: str) None[source]¶
Disables computation of derived parameter.
- Parameters:
parameter (str) – Parameter name to disable derivation for.
- disable_fit(parameter: str) None[source]¶
Disables fitting of the parameter.
- Parameters:
parameter (str) – Parameter name to disable fitting for.
- enable_derived(parameter: str) None[source]¶
Enables computation of derived parameter.
- Parameters:
parameter (str) – Parameter name to enable derivation for.
- enable_fit(parameter: str) None[source]¶
Enables fitting of the parameter.
- Parameters:
parameter (str) – Parameter name to enable fitting for.
- 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.
- Parameters:
output_size (OutputSize, optional) – Size of output, by default OutputSize.heavy
- Returns:
Solution dictionary.
- Return type:
t.Dict[str, AnyValType]
- 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.
- Parameters:
solution (int) – Solution index.
binning (npt.NDArray[np.float64]) – Binning wavenumber grid.
- Returns:
Profile and spectrum error dictionaries.
- Return type:
t.Tuple
- 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.
Generates posteriors, spectra, profiles and other useful parameters.
- Parameters:
output_size (OutputSize, optional) – Size of output, by default OutputSize.heavy
- Returns:
Solution dictionary.
- Return type:
t.Dict[str, AnyValType]
- get_samples(solution_id: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Get traces for a particular solution.
- Parameters:
solution_id (int) – Solution index.
- Returns:
Array of samples.
- Return type:
npt.NDArray[np.float64]
- 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.
- Parameters:
solution_id (int) – Solution index.
- Returns:
Array of weights.
- Return type:
npt.NDArray[np.float64]
- 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.
- Parameters:
parameters (npt.ArrayLike) – Parameter values.
- Returns:
Log likelihood.
- Return type:
float
- 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.
- Parameters:
cube (npt.ArrayLike) – Unit cube.
- Returns:
Transformed cube.
- Return type:
npt.NDArray[np.float64]
- sample_parameters(solution: int) Generator[Tuple[ndarray[tuple[int, ...], dtype[float64]], float], None, None][source]¶
Read traces and weights.
Reads traces and weights and returns 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.
- Parameters:
parameter (str) – Parameter name to set boundary for.
new_boundaries (t.List[float]) – New boundary values.
- set_factor_boundary(parameter: str, factors: List[float]) None[source]¶
Sets the boundary of the parameter based on a factor.
- Parameters:
parameter (str) – Parameter name to set boundary for.
factors (t.List[float]) – Factors to multiply the current value by.
- set_mode(parameter: str, new_mode: Literal['linear', 'log']) None[source]¶
Sets the fitting mode of a parameter.
- Parameters:
parameter (str) – Parameter name to set mode for.
new_mode (t.Literal["linear", "log"]) – New fitting mode.
- 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
- set_prior(parameter: str, prior: Prior) None[source]¶
Sets the prior of a parameter.
- Parameters:
parameter (str) – Parameter name to set prior for.
prior (Prior) – Prior object.
- 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
- Return type:
- write_optimizer(output: OutputGroup) OutputGroup[source]¶
Writes optimizer to file.
Writes settings under the ‘Optimizer’ heading in an output file.
- Parameters:
output – Group (or root) in output file to write to
- Return type:
- 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.
- Parameters:
fitparams (t.Dict[str, FittingType]) – Dictionary of fitting parameters.
driveparams (t.Dict[str, DerivedType]) – Dictionary of derived parameters.
fit_priors (t.Dict[str, Prior], optional) – Dictionary of fitting priors, by default None.
- Returns:
List of FitParam and list of DerivedParam.
- Return type:
Tuple
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.
- 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.
- compute_fit() None[source]¶
Computes the fit using nestle.
The fit is performed using the settings provided during the initialization of the class.
- get_samples(solution_idx: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Returns the samples from the fit.
- Parameters:
solution_idx (int) – Index of the solution.
- Returns:
Array of samples.
- Return type:
npt.NDArray[np.float64]
- 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.
- Parameters:
solution_idx (int) – Index of the solution.
- Returns:
Array of weights.
- Return type:
npt.NDArray[np.float64]
- property numLivePoints: int¶
Number of live points to use in the fit.
- store_nestle_output(result: Result) NestleSolutionOutput[source]¶
Turn the output from nestle into a dictionary.
Contains summary statistics and the solution.
- Parameters:
result (nestle.Result) – Nestle result object.
- Returns:
Formatted nestle output.
- Return type:
- property tolerance: float¶
Tolerance value for stopping the fit.
- write_fit(output: OutputGroup) OutputGroup[source]¶
Writes the fit to the output group.
- Parameters:
output (
OutputGroup) – Output group to write to.- Return type:
- write_optimizer(output: OutputGroup) OutputGroup[source]¶
Writes the optimizer to the output group.
- Parameters:
output (
OutputGroup) – Output group to write to.- Return type:
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:
OptimizerAn optimizer that uses the MultiNest library.
- BIBTEX_ENTRIES = ['\n @article{ refId0,\n author = {{Buchner, J.} and {Georgakakis, A.} and\n {Nandra, K.} and {Hsu, L.}\n and {Rangel, C.} and {Brightman, M.} and {Merloni, A.}\n and {Salvato, M.}\n and {Donley, J.} and {Kocevski, D.}},\n title = {X-ray spectral modelling of the AGN obscuring\n region in the CDFS: Bayesian model selection\n 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,\n 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 = {\n https://ui.adsabs.harvard.edu/abs/2009MNRAS.398.1601F\n },\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.
- Parameters:
solution_idx (int) – Index of the solution.
- Returns:
Array of samples.
- Return type:
npt.NDArray[np.float64]
- 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.
- Yields:
t.Generator – Yields solution index, map values, median values, and extra information.
- get_weights(solution_idx: int) ndarray[tuple[int, ...], dtype[float64]][source]¶
Get weights from solution.
- Parameters:
solution_idx (int) – Index of the solution.
- Returns:
Array of weights.
- Return type:
npt.NDArray[np.float64]
- store_nest_solutions() NestOutputType[source]¶
Store the multinest results.
- write_fit(output)[source]¶
Write fit to output.
- Parameters:
output (
OutputGroup) – Output group to write to.- Return type:
- write_optimizer(output: OutputGroup) OutputGroup[source]¶
Write optimizer to output.
- Parameters:
output (
OutputGroup) – Output group to write to.- Returns:
The written output group.
- Return type:
- 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