Optimizers (taurex.optimizer)

Base

class Optimizer(name, observed=None, model=None, sigma_fraction=0.1)[source]

Bases: taurex.log.logger.Logger, taurex.data.citation.Citable

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.

Parameters
  • name (str) – Name to be used in logging

  • observed (BaseSpectrum, optional) – See set_observed()

  • model (ForwardModel, optional) – See set_model()

  • sigma_fraction (float, optional) – Fraction of weights to use in computing the error. (Default: 0.1)

chisq_trans(fit_params, data, datastd)[source]

Computes the Chi-Squared between the forward model and observation. The steps taken are:

  1. Forward model (FM) is updated with update_model()

  2. FM is then computed at its native grid then binned.

  3. Chi-squared between FM and observation is computed

Parameters
  • fit_params (list of parameter values) – List of new parameter values to update the model

  • data (obj:ndarray) – Observed spectrum

  • datastd (obj:ndarray) – Observed spectrum error

Returns

chi-squared

Return type

float

compile_params()[source]
compute_derived_trace(solution)[source]
compute_fit()[source]

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

Returns a list of the current values of a fitting parameter. This respects the mode setting

Returns

List of each value of a fitting parameter

Return type

list

property derived_names

Returns a list of the current values of a fitting parameter. This respects the mode setting

Returns

List of each value of a fitting parameter

Return type

list

property derived_values

Returns a list of the current values of a fitting parameter. This respects the mode setting

Returns

List of each value of a fitting parameter

Return type

list

disable_derived(parameter)[source]
disable_fit(parameter)[source]

Disables fitting of the parameter

Parameters

parameter (str) – Name of the parameter we do not want to fit

enable_derived(parameter)[source]
enable_fit(parameter)[source]

Enables fitting of the parameter

Parameters

parameter (str) – Name of the parameter we want to fit

fit(output_size=<OutputSize.heavy: 6>)[source]
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 tuple with the form ( bound_min , bound_max )

Return type

list

property fit_latex

Returns the names of the parameters in LaTeX format

Returns

List of parameter names in LaTeX format

Return type

list

property fit_names

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

Returns a list of the current values of a fitting parameter. This respects the mode setting

Returns

List of each value of a fitting parameter

Return type

list

property fit_values_nomode

Returns a list of the current values of a fitting parameter. Regardless of the mode setting

Returns

List of each value of a fitting parameter

Return type

list

generate_profiles(solution, binning)[source]
generate_solution(output_size=<OutputSize.heavy: 6>)[source]

Generates a dictionary with all solutions and other useful parameters

get_samples(solution_id)[source]
get_solution()[source]

** Requires implementation **

Generator for solutions and their median and MAP values

Yields
  • solution_no (int) – Solution number

  • map (array) – Map values

  • median (array) – Median values

  • extra (list) – List of tuples of extra information to store. Must be of form (name, data)

get_weights(solution_id)[source]
classmethod input_keywords()[source]
sample_parameters(solution)[source]

Read traces and weights and return a random sigma_fraction sample of them

Parameters

solution – a solution output from sampler

Yields
  • traces (array) – Traces of a particular sample

  • weight (float) – Weight of sample

set_boundary(parameter, new_boundaries)[source]

Sets the boundary of the parameter

Parameters
  • parameter (str) – Name of the parameter we want to change

  • new_boundaries (tuple of float) – New fitting boundaries, with the form ( bound_min , bound_max ). These should not take into account the mode setting of a fitting parameter.

set_factor_boundary(parameter, factors)[source]

Sets the boundary of the parameter based on a factor

Parameters
  • parameter (str) – Name of the parameter we want to change

  • factor (tuple of float) – To be written

set_mode(parameter, new_mode)[source]

Sets the fitting mode of a parameter

Parameters
  • parameter (str) – Name of the parameter we want to change

  • new_mode (linear or log) – Sets whether the parameter is fit in linear or log space

set_model(model)[source]
set_observed(observed)[source]
set_prior(parameter, prior)[source]
update_model(fit_params)[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 by fit_names()

write(output)[source]

Creates ‘Optimizer’ them respectively

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

write_fit(output)[source]

Writes basic fitting parameters into output

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

Returns

Group (or root) in output file written to

Return type

Output or OutputGroup

write_optimizer(output)[source]

Writes optimizer settings under the ‘Optimizer’ heading in an output file

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

Returns

Group (or root) in output file written to

Return type

Output or OutputGroup

compile_params(fitparams, driveparams, fit_priors=None)[source]

Nestle (taurex.optimizer.nestle)

class NestleOptimizer(observed=None, model=None, num_live_points=1500, method='multi', tol=0.5, sigma_fraction=0.1)[source]

Bases: taurex.optimizer.optimizer.Optimizer

An optimizer that uses the nestle library to perform optimization.

Parameters
  • observed (BaseSpectrum, optional) – Observed spectrum to optimize to

  • model (ForwardModel, optional) – Forward model to optimize

  • num_live_points (int, optional) – Number of live points to use in sampling

  • method (classic, single or multi) – Nested sampling method to use. classic uses MCMC exploration, single uses a single ellipsoid and multi uses multiple ellipsoids (similar to Multinest)

  • tol (float) – Evidence tolerance value to stop the fit. This is based on an estimate of the remaining prior volumes contribution to the evidence.

  • sigma_fraction (float, optional) – Fraction of weights to use in computing the error. (Default: 0.1)

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 }']
compute_fit()[source]

Computes the fit using nestle

get_samples(solution_idx)[source]
get_solution()[source]

Generator for solutions and their median and MAP values

Yields
  • solution_no (int) – Solution number (always 0)

  • map (array) – Map values

  • median (array) – Median values

  • extra (list) – Returns Statistics, fitting_params, raw_traces and raw_weights

get_weights(solution_idx)[source]
classmethod input_keywords()[source]
property numLivePoints
store_nestle_output(result)[source]

This turns the output fron nestle into a dictionary that can be output by Taurex

Parameters

result (dict) – Result from a nestle sample call

Returns

Formatted dictionary for output

Return type

dict

property tolerance
write_fit(output)[source]

Writes basic fitting parameters into output

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

Returns

Group (or root) in output file written to

Return type

Output or OutputGroup

write_optimizer(output)[source]

Writes optimizer settings under the ‘Optimizer’ heading in an output file

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

Returns

Group (or root) in output file written to

Return type

Output or OutputGroup

MultiNest (taurex.optimizer.multinest)

class MultiNestOptimizer(multi_nest_path=None, observed=None, model=None, sampling_efficiency='parameter', num_live_points=1500, max_iterations=0, search_multi_modes=True, num_params_cluster=None, maximum_modes=100, constant_efficiency_mode=False, evidence_tolerance=0.5, mode_tolerance=-1e+90, importance_sampling=False, resume=False, n_iter_before_update=100, multinest_prefix='1-', verbose_output=True, sigma_fraction=0.1)[source]

Bases: taurex.optimizer.optimizer.Optimizer

compute_fit()[source]

Unimplemented. When inheriting this should be overwritten to perform the actual fit.

Raises

NotImplementedError – Raised when a derived class does override this function

generate_solution(output_size=<OutputSize.heavy: 6>)[source]

Generates a dictionary with all solutions and other useful parameters

get_solution()[source]

** Requires implementation **

Generator for solutions and their median and MAP values

Yields
  • solution_no (int) – Solution number

  • map (array) – Map values

  • median (array) – Median values

  • extra (list) – List of tuples of extra information to store. Must be of form (name, data)

write_fit(output)[source]

Writes basic fitting parameters into output

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

Returns

Group (or root) in output file written to

Return type

Output or OutputGroup

write_optimizer(output)[source]

Writes optimizer settings under the ‘Optimizer’ heading in an output file

Parameters

output (Output or OutputGroup) – Group (or root) in output file to write to

Returns

Group (or root) in output file written to

Return type

Output or OutputGroup

PolyChord (taurex.optimizer.polychord)

dyPolyChord (taurex.optimizer.dypolychord)