#!/usr/bin/env python # coding: utf-8 # # Tutorial 5 for JetSeT v1.2.0-rc1 # # Model fitting # In[1]: from IPython.core.display import display, HTML display(HTML("")) import warnings warnings.filterwarnings('ignore') import matplotlib.pylab as plt import jetset from jetset.test_data_helper import test_SEDs from jetset.data_loader import ObsData,Data from jetset.plot_sedfit import PlotSED from jetset.test_data_helper import test_SEDs import numpy as np import warnings warnings.filterwarnings('ignore') # In[2]: test_SEDs # ## loading data # # # In[3]: data=Data.from_file(test_SEDs[2]) # In[4]: data.table # In[5]: get_ipython().run_line_magic('matplotlib', 'inline') sed_data=ObsData(data_table=data) myPlot=sed_data.plot_sed() sed_data.group_data(bin_width=0.2) sed_data.add_systematics(0.1,[10.**6,10.**29]) myPlot.add_data_plot(sed_data,label='rebinned') myPlot.rescale(y_min=-14,y_max=-9,x_min=9,x_max=29) # In[6]: sed_data.table # ## phenomenological model constraining # ### spectral indices # In[7]: from jetset.sed_shaper import SEDShape my_shape=SEDShape(sed_data) my_shape.eval_indices(silent=True) p=my_shape.plot_indices() p.rescale(y_min=-15,y_max=-6) # ### sed shaper # In[8]: mm,best_fit=my_shape.sync_fit(check_host_gal_template=True, Ep_start=None, minimizer='lsb', silent=True, fit_range=[10,21]) # In[9]: best_fit.show_report() # In[10]: my_shape.IC_fit(fit_range=[23,29],minimizer='minuit') p=my_shape.plot_shape_fit() p.rescale(y_min=-15) # ### model constraining # In[11]: from jetset.obs_constrain import ObsConstrain from jetset.model_manager import FitModel sed_obspar=ObsConstrain(beaming=25, B_range=[0.001,0.1], distr_e='lppl', t_var_sec=3*86400, nu_cut_IR=1E11, SEDShape=my_shape) prefit_jet=sed_obspar.constrain_SSC_model(electron_distribution_log_values=False, silent=False) prefit_jet.save_model('prefit_jet_gal_templ.pkl') # In[12]: #this is needed only on the binder prefit_jet.eval() # pl=prefit_jet.plot_model(sed_data=sed_data) pl.add_residual_plot(prefit_jet,sed_data) pl.rescale(y_min=-15,x_min=7,x_max=29) # ## Model fitting We remind that we can use different ``minimizers`` for the model fitting. In the following we will use the ``minuit`` minimizer and the ``lsb`` (least square bound scipy minimizer). Using ``minuit`` we notice that sometimes (as in the case below) the fit will converge, but the quality will not be enough (``valid==false``) to run ``minos``. Anyhow, as shown in the :ref:`MCMC sampling`, it still possible to estimate asymmetric errors by means of MCMC sampling We freeze some parameters, and we also set some `fit_range` values. Setting fit_range can speed-up the fit convergence but should be judged by the user each time according to the physics of the particular source. # ### Model fitting with LSB # In[13]: from jetset.minimizer import ModelMinimizer from jetset.model_manager import FitModel from jetset.jet_model import Jet jet_lsb=Jet.load_model('prefit_jet_gal_templ.pkl') jet_lsb.set_gamma_grid_size(200) fit_model_lsb=FitModel( jet=jet_lsb, name='SSC-best-fit-lsb',template=my_shape.host_gal) fit_model_lsb.freeze(jet_lsb,'z_cosm') fit_model_lsb.freeze(jet_lsb,'R_H') fit_model_lsb.jet_leptonic.parameters.beam_obj.fit_range=[5,50] fit_model_lsb.jet_leptonic.parameters.R.fit_range=[10**15.5,10**17.5] fit_model_lsb.jet_leptonic.parameters.gmax.fit_range=[1E4,1E8] #fit_model_lsb.jet_leptonic.parameters.gmax.val=2.6E7 #fit_model_lsb.jet_leptonic.parameters.gmin.val=100 fit_model_lsb.host_galaxy.parameters.nuFnu_p_host.frozen=False fit_model_lsb.host_galaxy.parameters.nu_scale.frozen=True fit_model_lsb.jet_leptonic.nu_size=200 fit_model_lsb.jet_leptonic.IC_nu_size=100 fit_model_lsb.jet_leptonic._blob.adaptive_e_binning=0 model_minimizer_lsb=ModelMinimizer('lsb') best_fit_lsb=model_minimizer_lsb.fit(fit_model_lsb,sed_data,1E11,1E29,fitname='SSC-best-fit-lsb',repeat=3) # In[14]: best_fit_lsb.save_report() best_fit_lsb.bestfit_table # In[15]: get_ipython().run_line_magic('matplotlib', 'inline') fit_model_lsb.set_nu_grid(1E6,1E30,200) fit_model_lsb.eval() p2=fit_model_lsb.plot_model(sed_data=sed_data) p2.rescale(y_min=-13,x_min=9,x_max=28.5) # ### Model fitting with a bkn pl # # In[16]: from jetset.obs_constrain import ObsConstrain from jetset.minimizer import fit_SED sed_obspar=ObsConstrain(beaming=25, B_range=[0.001,0.1], distr_e='bkn', t_var_sec=3*86400, nu_cut_IR=1E11, SEDShape=my_shape) prefit_jet=sed_obspar.constrain_SSC_model(electron_distribution_log_values=False,silent=True) prefit_jet.save_model('prefit_jet_bkn_gal_templ.pkl') # In[17]: prefit_jet.eval() pl=prefit_jet.plot_model(sed_data=sed_data) pl.add_residual_plot(prefit_jet,sed_data) pl.rescale(y_min=-15,x_min=7,x_max=29) # In[18]: jet_minuit_bkn=Jet.load_model('prefit_jet_bkn_gal_templ.pkl') jet_minuit_bkn.set_gamma_grid_size(200) fit_model_lsb_bkn=FitModel( jet=jet_minuit_bkn, name='SSC-best-fit-bkn-lsb',template=my_shape.host_gal) fit_model_lsb_bkn.freeze(jet_lsb,'z_cosm') fit_model_lsb_bkn.freeze(jet_lsb,'R_H') fit_model_lsb_bkn.jet_leptonic.parameters.beam_obj.fit_range=[5,50] fit_model_lsb_bkn.jet_leptonic.parameters.R.fit_range=[10**15.5,10**17.5] fit_model_lsb_bkn.jet_leptonic.parameters.gmax.fit_range=[1E4,1E8] fit_model_lsb_bkn.host_galaxy.parameters.nuFnu_p_host.frozen=False fit_model_lsb_bkn.host_galaxy.parameters.nu_scale.frozen=True model_minimizer_lsb_bkn=ModelMinimizer('lsb') best_fit_lsb_bkn=model_minimizer_lsb_bkn.fit(fit_model_lsb_bkn,sed_data,1E11,1E29,fitname='SSC-best-fit-lsb',repeat=3) # In[19]: get_ipython().run_line_magic('matplotlib', 'inline') fit_model_lsb_bkn.set_nu_grid(1E6,1E30,200) fit_model_lsb_bkn.eval() p2=fit_model_lsb_bkn.plot_model(sed_data=sed_data) p2.rescale(y_min=-13,x_min=9,x_max=28.5) # In[20]: get_ipython().run_line_magic('matplotlib', 'inline') from jetset.plot_sedfit import PlotSED fit_model_lsb_bkn.set_nu_grid(1E6,1E30,200) fit_model_lsb_bkn.eval() p2=PlotSED() p2.add_data_plot(sed_data,fit_range=[ 11,29]) p2.add_model_plot(fit_model_lsb,color='red') p2.add_residual_plot(fit_model_lsb,sed_data,fit_range=[ 11,29],color='red') p2.add_model_plot(fit_model_lsb_bkn,color='green') p2.add_residual_plot(fit_model_lsb_bkn,sed_data,fit_range=[ 11,29],color='green') p2.rescale(y_min=-13,x_min=9,x_max=28.5) # # MCMC # In[21]: from jetset.mcmc import McmcSampler # We used a flat prior centered on the best fit value. Setting `bound=5.0` and `bound_rel=True` means that: # # 1) the prior interval will be defined as [best_fit_val - delta_m , best_fit_val + delta_p] # # 2) with delta_p=delta_m=best_fit_val*bound # # If we set `bound_rel=False` then delta_p = delta_m = best_fit_err*bound # # It is possible to define asymmetric boundaries e.g. `bound=[2.0,5.0]` meaning that # # 1) for `bound_rel=True` # # delta_p = best_fit_val*bound[1] # # delta_m =b est_fit_val*bound[0] # # 2) for `bound_rel=False` # # delta_p = best_fit_err*bound[1] # # delta_m = best_fit_err*bound[0] # # In the next release a more flexible prior interface will be added, including different type of priors # # Given the large parameter space, we select a sub sample of parameters using the `use_labels_dict`. If we do not pass the 'use_labels_dict' the full set of free parameters will be used # # In[22]: mcmc=McmcSampler(model_minimizer_lsb) labels=['N','B','beam_obj','s','gamma0_log_parab'] model_name='jet_leptonic' use_labels_dict={model_name:labels} mcmc.run_sampler(nwalkers=128,burnin=10,steps=50,bound=5.0,bound_rel=True,threads=None,walker_start_bound=0.005,use_labels_dict=use_labels_dict) # In[23]: print(mcmc.acceptance_fraction) # In[24]: f=mcmc.corner_plot() # In[25]: p=mcmc.plot_model(sed_data=sed_data,fit_range=[11.,27.4],size=50) p.rescale(y_min=-13,x_min=6,x_max=28.5) # In[26]: f=mcmc.plot_chain('s',log_plot=False) # In[27]: mcmc.save('test_run_mcmc.pkl') # In[28]: ms=McmcSampler.load('test_run_mcmc.pkl') # In[29]: p=mcmc.corner_plot() # In[30]: p=ms.plot_par('beam_obj',log_plot=False) # In[31]: p=ms.plot_par('B',log_plot=False) # In[32]: f=ms.plot_par('gamma0_log_parab',log_plot=True) # In[33]: f=mcmc.plot_chain('s',log_plot=False) # In[34]: p=mcmc.plot_model(sed_data=sed_data,fit_range=[11.,27.4],size=50) p.rescale(y_min=-13,x_min=6,x_max=28.5) # In[ ]: