Example to use the Sherpa minimizer plugin with a JeSeT model

Example to use the Sherpa minimizer plugin with a JeSeT model#

In this tutorial we show how to import a jetset model into sherpa, and finally we perform a model fitting. To run this plugin you have to install Sherpa: https://sherpa.readthedocs.io/en/latest/install.html

import jetset
print('tested with',jetset.__version__)
tested with 1.4.0rc0
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
test_SEDs
['/Users/orion/miniforge3/envs/jetset/lib/python3.12/site-packages/jetset/test_data/SEDs_data/SED_3C345.ecsv',
 '/Users/orion/miniforge3/envs/jetset/lib/python3.12/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk421_EBL_DEABS.ecsv',
 '/Users/orion/miniforge3/envs/jetset/lib/python3.12/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_ABS.ecsv',
 '/Users/orion/miniforge3/envs/jetset/lib/python3.12/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_DEABS.ecsv']

Loading data#

see the Data format and SED data user guide for further information about loading data

print(test_SEDs[2])
data=Data.from_file(test_SEDs[2])
/Users/orion/miniforge3/envs/jetset/lib/python3.12/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_ABS.ecsv
%matplotlib inline
sed_data=ObsData(data_table=data)
sed_data.group_data(bin_width=0.2)

sed_data.add_systematics(0.1,[10.**6,10.**29])
p=sed_data.plot_sed()
================================================================================

*  binning data  *
---> N bins= 88
---> bin_width= 0.2
================================================================================
../../../../_images/sherpa-plugin-jetset-interface_9_1.png
sed_data.save('Mrk_501.pkl')

phenomenological model constraining#

see the Phenomenological model constraining: application user guide for further information about phenomenological constraining

spectral indices#

from jetset.sed_shaper import  SEDShape
my_shape=SEDShape(sed_data)
my_shape.eval_indices(minimizer='lsb',silent=True)
p=my_shape.plot_indices()
p.setlim(y_min=1E-15,y_max=1E-6)
================================================================================

* evaluating spectral indices for data *
================================================================================
../../../../_images/sherpa-plugin-jetset-interface_14_1.png

sed shaper#

mm,best_fit=my_shape.sync_fit(check_host_gal_template=True,
                  Ep_start=None,
                  minimizer='lsb',
                  silent=True,
                  fit_range=[10.,21.])
================================================================================

* Log-Polynomial fitting of the synchrotron component *
---> first blind fit run,  fit range: [10.0, 21.0]
---> class:  HSP

---> class:  HSP
Table length=6
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
LogCubicb-5.589204e-02-5.589204e-026.234447e-03---5.237376e-02-1.000000e+010.000000e+00False
LogCubicc-3.292704e-04-3.292704e-048.967926e-04--7.639964e-03-1.000000e+011.000000e+01False
LogCubicEp1.698160e+011.698160e+018.582826e-02--1.556163e+010.000000e+003.000000e+01False
LogCubicSp-1.030620e+01-1.030620e+011.607947e-02---1.019011e+01-3.000000e+010.000000e+00False
host_galaxynuFnu_p_host-9.972939e+00-9.972939e+003.177493e-02---1.019011e+01-1.219011e+01-8.190114e+00False
host_galaxynu_scale-2.290459e-03-2.290459e-031.811351e-03--0.000000e+00-2.908697e-032.908697e-03False
---> sync       nu_p=+1.698160e+01 (err=+8.582826e-02)  nuFnu_p=-1.030620e+01 (err=+1.607947e-02) curv.=-5.589204e-02 (err=+6.234447e-03)
================================================================================
my_shape.IC_fit(fit_range=[23.,29.],minimizer='minuit',silent=True)
p=my_shape.plot_shape_fit()
p.setlim(y_min=1E-15)
================================================================================

* Log-Polynomial fitting of the IC component *
---> fit range: [23.0, 29.0]
---> LogCubic fit
Table length=4
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
LogCubicb-1.627265e-01-1.627265e-012.483991e-02---1.000000e+00-1.000000e+010.000000e+00False
LogCubicc-4.569970e-02-4.569970e-021.984551e-02---1.000000e+00-1.000000e+011.000000e+01False
LogCubicEp2.526444e+012.526444e+011.736457e-01--2.531190e+010.000000e+003.000000e+01False
LogCubicSp-1.057790e+01-1.057790e+014.914805e-02---1.000000e+01-3.000000e+010.000000e+00False
---> IC         nu_p=+2.526444e+01 (err=+1.736457e-01)  nuFnu_p=-1.057790e+01 (err=+4.914805e-02) curv.=-1.627265e-01 (err=+2.483991e-02)
================================================================================
../../../../_images/sherpa-plugin-jetset-interface_17_3.png

Model constraining#

In this step we are not fitting the model, we are just obtaining the phenomenological pre_fit model, that will be fitted in using minuit ore least-square bound, as shown below

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=1E12,
                        SEDShape=my_shape)


prefit_jet=sed_obspar.constrain_SSC_model(electron_distribution_log_values=False,silent=True)
prefit_jet.save_model('prefit_jet.pkl')
================================================================================

*  constrains parameters from observable *
/Users/orion/miniforge3/envs/jetset/lib/python3.12/site-packages/jetset/obs_constrain.py:1514: RankWarning: Polyfit may be poorly conditioned
  p=polyfit(nu_p_IC_model_log,B_grid_log,2)
Table length=12
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_leptonicRregion_sizecm1.216622e+161.000000e+031.000000e+30FalseFalse
jet_leptonicR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_leptonicBmagnetic_fieldgauss5.050000e-020.000000e+00--FalseFalse
jet_leptonicNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_leptonicbeam_objbeaming2.500000e+011.000000e-04--FalseFalse
jet_leptonicz_cosmredshift3.360000e-020.000000e+00--FalseFalse
jet_leptonicgminlow-energy-cut-offlorentz-factor*4.703917e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*2.121873e+061.000000e+001.000000e+15FalseFalse
jet_leptonicNemitters_density1 / cm35.583988e+000.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*6.673207e+031.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.251647e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature2.794602e-01-1.500000e+011.500000e+01FalseFalse
================================================================================
pl=prefit_jet.plot_model(sed_data=sed_data)
pl.add_model_residual_plot(prefit_jet,sed_data)
pl.setlim(y_min=1E-15,x_min=1E7,x_max=1E29)
../../../../_images/sherpa-plugin-jetset-interface_21_0.png

Model fitting with Sherpa#

from jetset.template_2Dmodel import EBLAbsorptionTemplate
ebl_franceschini=EBLAbsorptionTemplate.from_name('Franceschini_2008')
from jetset.jet_model import Jet
prefit_jet=Jet.load_model('prefit_jet.pkl')
composite_model=FitModel(nu_size=500,name='EBL corrected',template=my_shape.host_gal)
composite_model.add_component(prefit_jet)
composite_model.add_component(ebl_franceschini)
composite_model.link_par(par_name='z_cosm', from_model='Franceschini_2008', to_model='jet_leptonic')
composite_model.composite_expr='(jet_leptonic+host_galaxy)*Franceschini_2008'
composite_model.eval()
composite_model.plot_model()
<jetset.plot_sedfit.PlotSED at 0x13583a750>
../../../../_images/sherpa-plugin-jetset-interface_25_1.png
composite_model.freeze('jet_leptonic','z_cosm')
composite_model.freeze('jet_leptonic','R_H')
composite_model.jet_leptonic.parameters.beam_obj.fit_range=[5., 50.]
composite_model.jet_leptonic.parameters.R.fit_range=[10**15.5,10**17.5]
composite_model.jet_leptonic.parameters.gmax.fit_range=[1E5,1E7]
composite_model.host_galaxy.parameters.nuFnu_p_host.frozen=False
composite_model.host_galaxy.parameters.nu_scale.frozen=True
from jetset.minimizer import ModelMinimizer
composite_model.jet_leptonic.parameters.z_cosm.frozen=True

model_minimizer_lsb=ModelMinimizer('sherpa')
best_fit=model_minimizer_lsb.fit(composite_model,sed_data,1E11,1E29,fitname='SSC-best-fit-sherpa',repeat=1)
filtering data in fit range = [1.000000e+11,1.000000e+29]
data length 31
================================================================================

* start fit process *
-----
0it [00:00, ?it/s]
jetset model name R renamed to  R_sh due to sherpa internal naming convention
- best chisq=1.39904e+01

-------------------------------------------------------------------------
Fit report

Model: SSC-best-fit-sherpa
Table length=16
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
host_galaxynuFnu_p_hostnuFnu-scaleerg / (s cm2)-9.967750e+00-2.000000e+012.000000e+01FalseFalse
host_galaxynu_scalenu-scaleHz-2.290459e-03-2.000000e+002.000000e+00FalseTrue
jet_leptonicgminlow-energy-cut-offlorentz-factor*2.850763e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*1.938561e+061.000000e+001.000000e+15FalseFalse
jet_leptonicNemitters_density1 / cm36.150849e+000.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*2.581035e+031.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.195949e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature1.773625e-01-1.500000e+011.500000e+01FalseFalse
jet_leptonicRregion_sizecm1.666152e+161.000000e+031.000000e+30FalseFalse
jet_leptonicR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_leptonicBmagnetic_fieldgauss1.158370e-020.000000e+00--FalseFalse
jet_leptonicNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_leptonicbeam_objbeaming4.369859e+011.000000e-04--FalseFalse
jet_leptonicz_cosm(M)redshift3.360000e-020.000000e+00--FalseTrue
Franceschini_2008scale_factorscale_factor1.000000e+000.000000e+00--FalseTrue
Franceschini_2008z_cosm(L,jet_leptonic)redshift------FalseTrue
converged=True
calls=365
mesg=
<Fit results instance>
dof=21
chisq=13.990405, chisq/red=0.666210 null hypothesis sig=0.870011

best fit pars
Table length=16
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
host_galaxynuFnu_p_host-9.967750e+00-9.967750e+003.084511e-02---9.972939e+00-1.219011e+01-8.190114e+00False
host_galaxynu_scale-2.290459e-03-------2.290459e-03-2.908697e-032.908697e-03True
jet_leptonicgmin2.850763e+022.850763e+021.356548e+02--4.703917e+021.000000e+001.000000e+09False
jet_leptonicgmax1.938561e+061.938561e+060.000000e+00--2.121873e+061.000000e+051.000000e+07False
jet_leptonicN6.150849e+006.150849e+002.289278e+00--5.583988e+000.000000e+00--False
jet_leptonicgamma0_log_parab2.581035e+032.581035e+030.000000e+00--6.673207e+031.000000e+001.000000e+09False
jet_leptonics2.195949e+002.195949e+001.055377e-01--2.251647e+00-1.000000e+011.000000e+01False
jet_leptonicr1.773625e-011.773625e-013.221555e-02--2.794602e-01-1.500000e+011.500000e+01False
jet_leptonicR1.666152e+161.666152e+160.000000e+00--1.216622e+163.162278e+153.162278e+17False
jet_leptonicR_H1.000000e+17------1.000000e+170.000000e+00--True
jet_leptonicB1.158370e-021.158370e-022.763668e-03--5.050000e-020.000000e+00--False
jet_leptonicNH_cold_to_rel_e1.000000e+00------1.000000e+000.000000e+00--True
jet_leptonicbeam_obj4.369859e+014.369859e+016.269930e+00--2.500000e+015.000000e+005.000000e+01False
jet_leptonicz_cosm(M)3.360000e-02------3.360000e-020.000000e+00--True
Franceschini_2008scale_factor1.000000e+00------1.000000e+000.000000e+00--True
Franceschini_2008z_cosm(L,jet_leptonic)3.360000e-02--------0.000000e+00--True
-------------------------------------------------------------------------

================================================================================
composite_model.set_nu_grid(1E6,1E30,200)
composite_model.eval()
p=composite_model.plot_model(sed_data=sed_data)
../../../../_images/sherpa-plugin-jetset-interface_28_0.png

Using the sherpa_fitter you can access all the sherpa fetarues https://sherpa.readthedocs.io/en/latest/fit/index.html

model_minimizer_lsb.minimizer.sherpa_fitter.est_errors()
WARNING: hard minimum hit for parameter EBL corrected.gmax
WARNING: hard maximum hit for parameter EBL corrected.gmax
WARNING: hard minimum hit for parameter EBL corrected.B
WARNING: hard maximum hit for parameter EBL corrected.B
<covariance results instance>
from sherpa.plot import IntervalProjection
iproj = IntervalProjection()
iproj.prepare(fac=5, nloop=15)
iproj.calc(model_minimizer_lsb.minimizer.sherpa_fitter, model_minimizer_lsb.minimizer._sherpa_model.s)
iproj.plot()
WARNING: hard minimum hit for parameter EBL corrected.gmax
WARNING: hard maximum hit for parameter EBL corrected.gmax
WARNING: hard minimum hit for parameter EBL corrected.B
WARNING: hard maximum hit for parameter EBL corrected.B
../../../../_images/sherpa-plugin-jetset-interface_31_1.png