pyLIMA.microlfits module

Created on Thu Aug 27 16:39:32 2015

@author: ebachelet

class pyLIMA.microlfits.MLFits(event)[source]

Bases: object

######## Fitter module ########

This class contains the method to fit the event with the selected attributes.

WARNING: All fits (and so results) are made using data in flux.

Attributes :

event : the event object on which you perform the fit on. More details on the event module.

model : The microlensing model you want to fit. Has to be an object define in
microlmodels module. More details on the microlmodels module.

method : The fitting method you want to use for the fit.

guess : The guess you can give to the fit or the guess return by the initial_guess function.

fit_results : the fit parameters returned by method LM and DE.

fit_covariance : the fit parameters covariance matrix returned by method LM and DE.

fit_time : the time needed to fit.

MCMC_chains : the MCMC chains returns by the MCMC method

MCMC_probabilities : the objective function computed for each chains of the MCMC method

fluxes_MCMC_method : a string describing how you want to estimate the model fluxes for the MCMC method.

outputs : the standard pyLIMA outputs. More details in the microloutputs module.

Parameters:event (object) – the event object on which you perform the fit on. More details on the event module.
LM_Jacobian(fit_process_parameters)[source]

Return the analytical Jacobian matrix, if requested by method LM. Available only for PSPL and FSPL without second_order.

Parameters:fit_process_parameters (list) – the model parameters ingested by the correpsonding fitting routine.
Returns:a numpy array which represents the jacobian matrix
Return type:array_like
MCMC()[source]

The MCMC method. Construct starting points of the chains around the best solution found by the ‘DE’ method. The objective function is chichi_MCMC(). Telescope flux (fs and g), can be optimized thanks to MCMC if flux_estimation_MCMC is ‘MCMC’, either they are derived through np.polyfit.

Based on the emcee python package : ” emcee: The MCMC Hammer” (Foreman-Mackey et al. 2013). Have a look here : http://dan.iel.fm/emcee/current/

Returns:a tuple containing (MCMC_chains, MCMC_probabilities)
Return type:tuple
WARNING :
nwalkers is set to 100 nlinks is set to 300 5*nwalkers*nlinks MCMC steps in total
all_telescope_residuals(pyLIMA_parameters)[source]

Compute the residuals of all telescopes according to the model.

Parameters:pyLIMA_parameters (object) – object containing the model parameters, see microlmodels for more details
Returns:the residuals in flux,
Return type:list, a list of array of residuals in flux
check_fit()[source]

Check if the fit results and covariance make sens.

0.0 terms or a negative term in the diagonal covariance matrix indicate the fit is not reliable.

A negative source flux is also counted as a bad fit.

A negative rho or rho> 0.1 is also consider as a bad fit

Returns:a flag indicated good or bad fit (‘Good Fit’ or ‘Bad Fit’)
Return type:string
chichi_MCMC(fit_process_parameters)[source]

Return the chi^2 for the MCMC method. There is some priors here.

Parameters:fit_process_parameters (list) – the model parameters ingested by the correpsonding fitting routine.
Returns:here, the return is -chi^2/2 (likelihood)
Return type:float
chichi_differential_evolution(fit_process_parameters)[source]

Return the chi^2 for the DE method.

Parameters:fit_process_parameters (list) – the model parameters ingested by the correpsonding

fitting routine.

Returns:the chi^2
Return type:float
chichi_grids(moving_parameters, *fix_parameters)[source]

Compute chi^2. ON CONSTRUCTION.

chichi_telescopes(fit_process_parameters)[source]

Return a list of chi^2 (float) for individuals telescopes.

Parameters:fit_process_parameters (list) – the model parameters ingested by the correpsonding fitting routine.
Returns:the chi^2 for each telescopes
Return type:list
construct_the_hyper_grid(parameters)[source]

Define the grid. ON CONSTRUCTION.

differential_evolution()[source]

The DE method. Differential evolution algorithm. The objective function is chichi_differential_evolution(). The flux parameters are estimated through np.polyfit.

Based on the scipy.optimize.differential_evolution. Look Storn & Price (1997) : “Differential Evolution – A Simple and Efficient Heuristic for global Optimization over Continuous Spaces”

return:a tuple containing (fit_results, fit_covariance, computation_time)
rtype:tuple
WARNING :

tol (relative standard deviation of the objective function) is set to 10^-4

popsize (the total number of individuals is : popsize*number_of_paczynski_parameters) is set to DE_population_size

mutation is set to (0.1, 1.5)

recombination is set to 0.7

These parameters can avoid the fit to properly converge (expected to be rare :)). Just relaunch should be fine.

find_fluxes(fit_process_parameters, model)[source]

Find telescopes flux associated (fs,g) to the model. Used for initial_guess and LM method.

Parameters:
  • fit_process_parameters (list) – the model parameters ingested by the correpsonding fitting routine.
  • model (object) – a microlmodels which you want to compute the fs,g parameters.
Returns:

a list of tuple with the (fs,g) telescopes flux parameters.

Return type:

list

grids()[source]

Compute models on a grid. ON CONSTRUCTION.

initial_guess()[source]

Try to estimate the microlensing parameters. Only use for PSPL and FSPL models. More details on microlguess module.

Return guess_parameters:
 a list containing parameters guess related to the model.
Return type:list
lmarquardt()[source]

The LM method. This is based on the Levenberg-Marquardt algorithm:

“A Method for the Solution of Certain Problems in Least Squares” Levenberg, K. Quart. Appl. Math. 2, 1944, p. 164-168 “An Algorithm for Least-Squares Estimation of Nonlinear Parameters” Marquardt, D. SIAM J. Appl. Math. 11, 1963, p. 431-441

Based on scipy.optimize.leastsq python routine, which is based on MINPACK’s lmdif and lmder algorithms (fortran based).

The objective function is residuals_LM(). The starting point parameters are self.guess. the Jacobian is given by LM_Jacobian().

The fit is performed on all parameters : Paczynski parameters and telescopes fluxes.

Returns:a tuple containing (fit_results, covariance_matrix, computation_time)
Return type:tuple
WARNING:
ftol (relative error desired in the sum of square) is set to 10^-6 maxfev (maximum number of function call) is set to 50000 These limits can avoid the fit to properly converge (expected to be rare :))
mlfit(model, method, DE_population_size=10, flux_estimation_MCMC='MCMC', fix_parameters_dictionnary=None, grid_resolution=10, computational_pool=None, binary_regime=None)[source]

This function realize the requested microlensing fit, and set the according results attributes.

Parameters:
  • model (object) – the model object requested. More details on the microlmodels module.
  • method (string) –

    The fitting method you want to use. Has to be a string in :

    ‘LM’ –> Levenberg-Marquardt algorithm. Based on the scipy.optimize.leastsq routine.

    WARNING : the parameter maxfev (number of maximum iterations) is set to 50000 the parameter ftol (relative precision on the chi^2) is set to 0.00001 your fit may not converge because of these limits. The starting points of this method are found using the initial_guess method. Obviously, this can fail. In this case, switch to method ‘DE’.

    ‘DE’ –> Differential evolution algoritm. Based on the scipy.optimize.differential_evolution.

    Look Storn & Price (1997) : “Differential Evolution – A Simple and Efficient Heuristic for global Optimization over Continuous Spaces” Because this method is heuristic, it is not 100% sure a satisfying solution is found. Just relaunch :) The result is then use as a starting point for the ‘LM’ method.

    ‘MCMC’ –> Monte-Carlo Markov Chain algorithm. Based on the emcee python package :

    ” emcee: The MCMC Hammer” (Foreman-Mackey et al. 2013). The inital population is computed around the best solution returned by the ‘DE’ method.
  • DE_population_size (int) – The population factor desired for the DE method. Default is 10.
  • flux_estimation_MCMC (string) – The desired method to estimate the fluxes (f_source and g) of the telescopes. ‘MCMC’ will do this through an MCMC method (default) when everything else will do this thanks to a 1D polyfit through np.polyfit.

Note that a sanity check is done post-fit to assess the fit quality with the check_fit function.

model_residuals(telescope, pyLIMA_parameters)[source]

Compute the residuals of a telescope lightcurve according to the model.

Parameters:
  • telescope (object) – a telescope object. More details in telescopes module.
  • pyLIMA_parameters (object) – object containing the model parameters, see microlmodels for more details
Returns:

the residuals in flux, the priors

Return type:

array_like, float

optimization_on_grid_pixel(grid_pixel_parameters)[source]
produce_fit_statistics()[source]

Produce the standard outputs for a fit. More details in microloutputs module.

produce_outputs()[source]

Produce the standard outputs for a fit. More details in microloutputs module.

produce_pdf(output_directory)[source]

ON CONSTRUCTION

reconstruct_fit_process_parameters(moving_parameters, fix_parameters)[source]

Reconstruc parameters. ON CONSTRUCTION.

redefine_parameters_boundaries()[source]

Recompute the parameters boundaries. ON CONSTRUCTION.

residuals_LM(fit_process_parameters)[source]

The normalized residuals associated to the model and parameters.

Parameters:fit_process_parameters (list) – the model parameters ingested by the correpsonding fitting routine.
Returns:a numpy array which represents the residuals_i for each telescope, residuals_i=(data_i-model_i)/sigma_i
Return type:array_like

The sum of square residuals gives chi^2.

trust_region_reflective()[source]