.. _model_fitting_3: Model fitting 4: Only Synchrotron ================================= .. code:: ipython3 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 .. code:: ipython3 print(jetset.__version__) .. parsed-literal:: 1.3.0rc7 .. code:: ipython3 test_SEDs .. parsed-literal:: ['/Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/test_data/SEDs_data/SED_3C345.ecsv', '/Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk421_EBL_DEABS.ecsv', '/Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_ABS.ecsv', '/Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk501_EBL_DEABS.ecsv'] Loading data ------------ see the :ref:`data_format` user guide for further information about loading data .. code:: ipython3 print(test_SEDs[1]) data=Data.from_file(test_SEDs[1]) .. parsed-literal:: /Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/test_data/SEDs_data/SED_MW_Mrk421_EBL_DEABS.ecsv .. code:: ipython3 %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() .. parsed-literal:: ================================================================================ *** binning data *** ---> N bins= 89 ---> bin_widht= 0.2 msk [False True False True True True True True False False False True False False False False False False False False False False False False True True True True True True True False False False False False False False True True True True True True True True True True True False False False False False False False False False False False False False False False False False True False True False True False True True False True False True False True True True True True True True True True False] ================================================================================ .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_8_1.png .. code:: ipython3 sed_data.save('Mrk_401.pkl') Phenomenological model constraining ----------------------------------- see the :ref:`phenom_constr` user guide for further information about loading data Spectral indices ~~~~~~~~~~~~~~~~ .. code:: ipython3 from jetset.sed_shaper import SEDShape my_shape=SEDShape(sed_data) my_shape.eval_indices(silent=True) p=my_shape.plot_indices() p.setlim(y_min=1E-15,y_max=1E-6) .. parsed-literal:: ================================================================================ *** evaluating spectral indices for data *** ================================================================================ .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_13_1.png Sed shaper ~~~~~~~~~~ .. code:: ipython3 mm,best_fit=my_shape.sync_fit(check_host_gal_template=False, Ep_start=None, minimizer='lsb', silent=True, fit_range=[10., 21.]) .. parsed-literal:: ================================================================================ *** Log-Polynomial fitting of the synchrotron component *** ---> first blind fit run, fit range: [10.0, 21.0] ---> class: HSP .. raw:: html Table length=4
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
LogCubicb-1.585748e-01-1.585748e-016.470535e-03---1.000000e+00-1.000000e+010.000000e+00False
LogCubicc-1.089513e-02-1.089513e-029.764985e-04---1.000000e+00-1.000000e+011.000000e+01False
LogCubicEp1.673177e+011.673177e+012.478677e-02--1.667298e+010.000000e+003.000000e+01False
LogCubicSp-9.489417e+00-9.489417e+001.853260e-02---1.000000e+01-3.000000e+010.000000e+00False
.. parsed-literal:: ---> sync nu_p=+1.673177e+01 (err=+2.478677e-02) nuFnu_p=-9.489417e+00 (err=+1.853260e-02) curv.=-1.585748e-01 (err=+6.470535e-03) ================================================================================ .. code:: ipython3 my_shape.IC_fit(fit_range=[23., 29.],minimizer='minuit',silent=True) p=my_shape.plot_shape_fit() p.setlim(y_min=1E-15) .. parsed-literal:: ================================================================================ *** Log-Polynomial fitting of the IC component *** ---> fit range: [23.0, 29.0] ---> LogCubic fit ====> simplex ====> migrad ====> simplex ====> migrad ====> simplex ====> migrad .. raw:: html Table length=4
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
LogCubicb-1.971111e-01-1.971111e-012.679732e-02---1.000000e+00-1.000000e+010.000000e+00False
LogCubicc-4.037544e-02-4.037544e-022.119803e-02---1.000000e+00-1.000000e+011.000000e+01False
LogCubicEp2.521789e+012.521789e+011.198160e-01--2.529262e+010.000000e+003.000000e+01False
LogCubicSp-1.012535e+01-1.012535e+012.996508e-02---1.000000e+01-3.000000e+010.000000e+00False
.. parsed-literal:: ---> IC nu_p=+2.521789e+01 (err=+1.198160e-01) nuFnu_p=-1.012535e+01 (err=+2.996508e-02) curv.=-1.971111e-01 (err=+2.679732e-02) ================================================================================ .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_16_3.png Model constraining ~~~~~~~~~~~~~~~~~~ .. code:: ipython3 from jetset.obs_constrain import ObsConstrain from jetset.model_manager import FitModel from jetset.minimizer import fit_SED 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') .. parsed-literal:: ================================================================================ *** constrains parameters from observable *** ===> setting C threads to 12 .. raw:: html Table length=12
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_leptonicRregion_sizecm3.460321e+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.080000e-020.000000e+00--FalseFalse
jet_leptonicgminlow-energy-cut-offlorentz-factor*4.697542e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*1.373160e+061.000000e+001.000000e+15FalseFalse
jet_leptonicNemitters_density1 / cm36.545152e-010.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*3.333017e+041.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.183468e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature7.928739e-01-1.500000e+011.500000e+01FalseFalse
.. parsed-literal:: ================================================================================ .. code:: ipython3 pl=prefit_jet.plot_model(sed_data=sed_data) pl.add_residual_plot(prefit_jet,sed_data) pl.setlim(y_min=1E-15,x_min=1E7,x_max=1E29) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_19_0.png Model fitting only Synchorotron component ----------------------------------------- .. note:: Please, read the introduction and the caveat :ref:`for the frequentist model fitting ` to understand the frequentist fitting workflow see the :ref:`composite_models` user guide for further information about the implementation of :class:`.FitModel`, in particular for parameter setting Model fitting with Minuit ~~~~~~~~~~~~~~~~~~~~~~~~~ .. code:: ipython3 from jetset.jet_model import Jet jet_minuit=Jet.load_model('prefit_jet.pkl') jet_minuit.set_gamma_grid_size(200) .. parsed-literal:: ===> setting C threads to 12 we switch off the IC component .. code:: ipython3 jet_minuit.spectral_components.SSC.state='off' jet_minuit.show_model() .. parsed-literal:: -------------------------------------------------------------------------------- model description: -------------------------------------------------------------------------------- type: Jet name: jet_leptonic geometry: spherical electrons distribution: type: lppl gamma energy grid size: 201 gmin grid : 4.697542e+02 gmax grid : 1.373160e+06 normalization: True log-values: False ratio of cold protons to relativistic electrons: 1.000000e+00 radiative fields: seed photons grid size: 100 IC emission grid size: 100 source emissivity lower bound : 1.000000e-120 spectral components: name:Sum, state: on name:Sum, hidden: False name:Sync, state: self-abs name:Sync, hidden: False name:SSC, state: off name:SSC, hidden: False external fields transformation method: blob SED info: nu grid size jetkernel: 1000 nu size: 500 nu mix (Hz): 1.000000e+06 nu max (Hz): 1.000000e+30 flux plot lower bound : 1.000000e-30 -------------------------------------------------------------------------------- .. raw:: html Table length=12
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_leptonicgminlow-energy-cut-offlorentz-factor*4.697542e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*1.373160e+061.000000e+001.000000e+15FalseFalse
jet_leptonicNemitters_density1 / cm36.545152e-010.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*3.333017e+041.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.183468e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature7.928739e-01-1.500000e+011.500000e+01FalseFalse
jet_leptonicRregion_sizecm3.460321e+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.080000e-020.000000e+00--FalseFalse
.. parsed-literal:: -------------------------------------------------------------------------------- .. code:: ipython3 fit_model_minuit=FitModel( jet=jet_minuit, name='Only-Synch-best-fit-minuit') fit_model_minuit.freeze('jet_leptonic','z_cosm') fit_model_minuit.freeze('jet_leptonic','R_H') fit_model_minuit.freeze('jet_leptonic','R') fit_model_minuit.freeze('jet_leptonic','gmax') fit_model_minuit.jet_leptonic.parameters.R.fit_range=[10**15.5, 10**17.5] fit_model_minuit.jet_leptonic.parameters.beam_obj.fit_range=[5., 50.] .. code:: ipython3 from jetset.minimizer import fit_SED,ModelMinimizer model_minimizer_minuit=ModelMinimizer('minuit') best_fit_minuit=model_minimizer_minuit.fit(fit_model_minuit,sed_data,10.0**15,10**20.0,fitname='SSC-best-fit-minuit',repeat=3) .. parsed-literal:: filtering data in fit range = [1.000000e+15,1.000000e+20] data length 13 ================================================================================ *** start fit process *** ----- fit run: 0 .. parsed-literal:: 0it [00:00, ?it/s] .. parsed-literal:: ====> simplex ====> migrad - best chisq=1.66861e+00 fit run: 1 - old chisq=1.66861e+00 .. parsed-literal:: 0it [00:00, ?it/s] .. parsed-literal:: ====> simplex ====> migrad - best chisq=1.66861e+00 fit run: 2 - old chisq=1.66861e+00 .. parsed-literal:: 0it [00:00, ?it/s] .. parsed-literal:: ====> simplex ====> migrad - best chisq=1.66860e+00 ------------------------------------------------------------------------- Fit report Model: SSC-best-fit-minuit .. raw:: html Table length=12
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_leptonicgminlow-energy-cut-offlorentz-factor*4.226997e+021.000000e+001.000000e+09FalseFalse
jet_leptonicgmaxhigh-energy-cut-offlorentz-factor*1.373160e+061.000000e+001.000000e+15FalseTrue
jet_leptonicNemitters_density1 / cm31.083644e+000.000000e+00--FalseFalse
jet_leptonicgamma0_log_parabturn-over-energylorentz-factor*4.047533e+041.000000e+001.000000e+09FalseFalse
jet_leptonicsLE_spectral_slope2.121360e+00-1.000000e+011.000000e+01FalseFalse
jet_leptonicrspectral_curvature6.256219e-01-1.500000e+011.500000e+01FalseFalse
jet_leptonicRregion_sizecm3.460321e+161.000000e+031.000000e+30FalseTrue
jet_leptonicR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_leptonicBmagnetic_fieldgauss1.422387e-020.000000e+00--FalseFalse
jet_leptonicNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_leptonicbeam_objbeaming3.627168e+011.000000e-04--FalseFalse
jet_leptonicz_cosmredshift3.080000e-020.000000e+00--FalseTrue
.. parsed-literal:: converged=True calls=165 mesg= .. raw:: html
Migrad
FCN = 1.669 Nfcn = 165
EDM = 3.3e-05 (Goal: 0.0002) time = 2.0 sec
Valid Minimum No Parameters at limit
Below EDM threshold (goal x 10) Below call limit
Covariance Hesse ok APPROXIMATE NOT pos. def. FORCED
Name Value Hesse Error Minos Error- Minos Error+ Limit- Limit+ Fixed
0 par_0 0.42e3 0.11e3 1 1E+09
1 par_1 1.08 0.30 0
2 par_2 40e3 7e3 1 1E+09
3 par_3 2.12 0.05 -10 10
4 par_4 0.63 0.07 -15 15
5 par_5 0.0142 0.0014 0
6 par_6 36.3 2.4 5 50
.. parsed-literal:: dof=6 chisq=1.668605, chisq/red=0.278101 null hypothesis sig=0.947519 best fit pars .. raw:: html Table length=12
model namenamevalbestfit valerr +err -start valfit range minfit range maxfrozen
jet_leptonicgmin4.226997e+024.226997e+021.139513e+02--4.697542e+021.000000e+001.000000e+09False
jet_leptonicgmax1.373160e+06------1.373160e+061.000000e+001.000000e+15True
jet_leptonicN1.083644e+001.083644e+003.031713e-01--6.545152e-010.000000e+00--False
jet_leptonicgamma0_log_parab4.047533e+044.047533e+046.973433e+03--3.333017e+041.000000e+001.000000e+09False
jet_leptonics2.121360e+002.121360e+004.939483e-02--2.183468e+00-1.000000e+011.000000e+01False
jet_leptonicr6.256219e-016.256219e-016.567015e-02--7.928739e-01-1.500000e+011.500000e+01False
jet_leptonicR3.460321e+16------3.460321e+163.162278e+153.162278e+17True
jet_leptonicR_H1.000000e+17------1.000000e+170.000000e+00--True
jet_leptonicB1.422387e-021.422387e-021.433089e-03--5.050000e-020.000000e+00--False
jet_leptonicNH_cold_to_rel_e1.000000e+00------1.000000e+000.000000e+00--True
jet_leptonicbeam_obj3.627168e+013.627168e+012.427675e+00--2.500000e+015.000000e+005.000000e+01False
jet_leptonicz_cosm3.080000e-02------3.080000e-020.000000e+00--True
.. parsed-literal:: ------------------------------------------------------------------------- ================================================================================ .. code:: ipython3 %matplotlib inline fit_model_minuit.set_nu_grid(1E6,1E30,200) fit_model_minuit.eval() p2=fit_model_minuit.plot_model(sed_data=sed_data) p2.setlim(y_min=1E-11,x_min=1E15,y_max=1E-9,x_max=3E19) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_28_0.png .. code:: ipython3 try: c=model_minimizer_minuit.minimizer.draw_contour('r','s') except: pass .. code:: ipython3 m=model_minimizer_minuit.minimizer.draw_profile('r') .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_30_0.png .. code:: ipython3 best_fit_minuit.save_report('SSC-best-fit-minuit.pkl') model_minimizer_minuit.save_model('model_minimizer_minuit.pkl') fit_model_minuit.save_model('fit_model_minuit.pkl') MCMC sampling ------------- .. note:: Please, read the introduction and the caveat :ref:`for the Bayesian model fitting ` to understand the MCMC sampler workflow. .. code:: ipython3 from jetset.mcmc import McmcSampler from jetset.minimizer import ModelMinimizer .. code:: ipython3 model_minimizer_minuit = ModelMinimizer.load_model('model_minimizer_minuit.pkl') mcmc=McmcSampler(model_minimizer_minuit) .. parsed-literal:: ===> setting C threads to 12 .. code:: ipython3 labels=['N','B','beam_obj','s','gamma0_log_parab'] model_name='jet_leptonic' use_labels_dict={model_name:labels} mcmc.set_labels(use_labels_dict=use_labels_dict) .. code:: ipython3 mcmc.set_bounds(bound=5.0,bound_rel=True) .. parsed-literal:: par: N best fit value: 1.08364419186145 mcmc bounds: [0, 6.501865151168699] par: B best fit value: 0.014223870997614777 mcmc bounds: [0, 0.08534322598568866] par: beam_obj best fit value: 36.271677271204155 mcmc bounds: [5.0, 50.0] par: s best fit value: 2.1213601289476283 mcmc bounds: [-8.485440515790513, 10] par: gamma0_log_parab best fit value: 40475.33090501091 mcmc bounds: [1, 242851.98543006548] .. code:: ipython3 mcmc.run_sampler(nwalkers=20, burnin=50,steps=500,progress='notebook') .. parsed-literal:: ===> setting C threads to 12 mcmc run starting .. parsed-literal:: 0%| | 0/500 [00:00 setting C threads to 12 ===> setting C threads to 12 .. code:: ipython3 ms.model.name .. parsed-literal:: 'Only-Synch-best-fit-minuit' .. code:: ipython3 p=ms.plot_model(sed_data=sed_data,fit_range=[1E15, 1E20],size=50) p.setlim(y_min=1E-14,x_min=1E6,x_max=2E20) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_54_0.png .. code:: ipython3 p=ms.plot_model(sed_data=sed_data,fit_range=[1E15, 1E20],size=100,quantiles=[0.05,0.95]) p.setlim(y_min=1E-14,x_min=1E6,x_max=2E20) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_55_0.png .. code:: ipython3 f=ms.plot_par('beam_obj',log_plot=False) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_56_0.png .. code:: ipython3 f=ms.plot_par('B',log_plot=True) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_57_0.png .. code:: ipython3 f=mcmc.plot_chain(log_plot=False) .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_58_0.png .. code:: ipython3 f=mcmc.corner_plot() .. image:: Jet_example_only_synchrotron_files/Jet_example_only_synchrotron_59_0.png