.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_custom_fitter.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_plot_custom_fitter.py: ================================ Using a custom fitter or sampler ================================ How to use your own minimizer or MCMC sampler for fitting light curves. SNCosmo has three functions for model parameter estimation based on photometric data: `sncosmo.fit_lc`, `sncosmo.mcmc_lc` and `sncosmo.nest_lc`. These are wrappers around external minimizers or samplers (respectively: iminuit, emcee and nestle). However, one may wish to experiment with a custom fitting or sampling method. Here, we give a minimal example of using the L-BFGS-B minimizer from scipy. .. GENERATED FROM PYTHON SOURCE LINES 16-50 .. code-block:: Python import numpy as np from scipy.optimize import fmin_l_bfgs_b import sncosmo model = sncosmo.Model(source='salt2') data = sncosmo.load_example_data() # Define an objective function that we will pass to the minimizer. # The function arguments must comply with the expectations of the specfic # minimizer you are using. def objective(parameters): model.parameters[:] = parameters # set model parameters # evaluate model fluxes at times/bandpasses of data model_flux = model.bandflux(data['band'], data['time'], zp=data['zp'], zpsys=data['zpsys']) # calculate and return chi^2 return np.sum(((data['flux'] - model_flux) / data['fluxerr'])**2) # starting parameter values in same order as `model.param_names`: start_parameters = [0.4, 55098., 1e-5, 0., 0.] # z, t0, x0, x1, c # parameter bounds in same order as `model.param_names`: bounds = [(0.3, 0.7), (55080., 55120.), (None, None), (None, None), (None, None)] parameters, val, info = fmin_l_bfgs_b(objective, start_parameters, bounds=bounds, approx_grad=True) print(parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 5.03132699e-01 5.50980000e+04 1.19775631e-05 -2.59687074e-03 2.22018151e-01] .. GENERATED FROM PYTHON SOURCE LINES 51-57 The built-in parameter estimation functions in sncosmo take care of setting up the likelihood function in the way that the underlying fitter or sampler expects. Additionally, they set guesses and bounds and package results up in a way that is as consistent as possible. For users wishing use a custom minimizer or sampler, it can be instructive to look at the source code for these functions. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.521 seconds) .. _sphx_glr_download_examples_plot_custom_fitter.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_custom_fitter.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_custom_fitter.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_custom_fitter.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_