.. _composite_models: Composite Models and depending pars =================================== .. code:: ipython3 from jetset.jet_model import Jet from jetset.plot_sedfit import PlotSED from jetset.model_manager import FitModel Composite models allow combining different models, such as Jet, and templates, including additive or multiplicative models and give the user the possibility to define the functional form of the model composition using simple and intuitive expressions such as: .. code-block:: ipython3 'jet1+jet2'*Franceschini_2008 which sums two jet models SEDs, and applies to both of them the ``Franceschini_2008`` EBL absorption. Building composite models is very easy. Composite models are handled by the :class:`.FitModel` class, as shown by the following examples. Combine a Jet model with the EBL model (Multiplicative case) ------------------------------------------------------------ We start by combining a Jet model with the EBL absorption model, i.e. a multiplicative model. First, we define our Jet model .. code:: ipython3 from jetset.jet_model import Jet my_jet=Jet(electron_distribution='lppl',name='jet_flaring') .. parsed-literal:: ===> setting C threads to 12 Second, we define the EBL model, and we use in this case the ``Franceschini_2008`` model ( read the section :ref:`ebl_model` for more info regarding the EBL models) .. code:: ipython3 from jetset.template_2Dmodel import EBLAbsorptionTemplate ebl_franceschini=EBLAbsorptionTemplate.from_name('Franceschini_2008') Now we add the components models to the the :class:`.FitModel` class, using the :class:`.FitModel.add_component()` method .. code:: ipython3 composite_model=FitModel(nu_size=500,name='EBL corrected') composite_model.add_component(my_jet) composite_model.add_component(ebl_franceschini) .. parsed-literal:: /Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/model_manager.py:158: UserWarning: no cosmology defined, using FlatLambdaCDM(name="Planck13", H0=67.77 km / (Mpc s), Om0=0.30712, Tcmb0=2.7255 K, Neff=3.046, m_nu=[0. 0. 0.06] eV, Ob0=0.048252) warnings.warn(m) the waring message is just telling that you are not passing any specific cosmology model to the ``FitModel`` class, so it is using a default one .. code:: ipython3 composite_model.show_pars() .. raw:: html Table length=14
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_flaringRregion_sizecm5.000000e+151.000000e+031.000000e+30FalseFalse
jet_flaringR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_flaringBmagnetic_fieldgauss1.000000e-010.000000e+00--FalseFalse
jet_flaringNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_flaringbeam_objbeaming1.000000e+011.000000e-04--FalseFalse
jet_flaringz_cosmredshift1.000000e-010.000000e+00--FalseFalse
jet_flaringgminlow-energy-cut-offlorentz-factor*2.000000e+001.000000e+001.000000e+09FalseFalse
jet_flaringgmaxhigh-energy-cut-offlorentz-factor*1.000000e+061.000000e+001.000000e+15FalseFalse
jet_flaringNemitters_density1 / cm31.000000e+020.000000e+00--FalseFalse
jet_flaringgamma0_log_parabturn-over-energylorentz-factor*1.000000e+041.000000e+001.000000e+09FalseFalse
jet_flaringsLE_spectral_slope2.000000e+00-1.000000e+011.000000e+01FalseFalse
jet_flaringrspectral_curvature4.000000e-01-1.500000e+011.500000e+01FalseFalse
Franceschini_2008scale_factorscale_factor1.000000e+000.000000e+00--FalseTrue
Franceschini_2008z_cosmredshift1.000000e+000.000000e+00--FalseTrue
Since, both the Jet model the EBL share the same parameter, i.e. the redshift, we link the two parameters .. code:: ipython3 composite_model.link_par(par_name='z_cosm', from_model=my_jet.name, to_model='Franceschini_2008') .. parsed-literal:: adding par: z_cosm to z_cosm .. code:: ipython3 composite_model.show_pars() .. raw:: html Table length=14
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_flaringRregion_sizecm5.000000e+151.000000e+031.000000e+30FalseFalse
jet_flaringR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_flaringBmagnetic_fieldgauss1.000000e-010.000000e+00--FalseFalse
jet_flaringNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_flaringbeam_objbeaming1.000000e+011.000000e-04--FalseFalse
jet_flaringz_cosm(L,Franceschini_2008)redshift------FalseTrue
jet_flaringgminlow-energy-cut-offlorentz-factor*2.000000e+001.000000e+001.000000e+09FalseFalse
jet_flaringgmaxhigh-energy-cut-offlorentz-factor*1.000000e+061.000000e+001.000000e+15FalseFalse
jet_flaringNemitters_density1 / cm31.000000e+020.000000e+00--FalseFalse
jet_flaringgamma0_log_parabturn-over-energylorentz-factor*1.000000e+041.000000e+001.000000e+09FalseFalse
jet_flaringsLE_spectral_slope2.000000e+00-1.000000e+011.000000e+01FalseFalse
jet_flaringrspectral_curvature4.000000e-01-1.500000e+011.500000e+01FalseFalse
Franceschini_2008scale_factorscale_factor1.000000e+000.000000e+00--FalseTrue
Franceschini_2008z_cosm(M)redshift1.000000e+000.000000e+00--FalseTrue
As you can see, now the paramter ``z_cosm`` in ``jet_flaring`` is the ``master`` parameter (flagged by the M in parenthesis), and the one belonging to the ``Franceschini_2008`` component is the linked one (flagged by the L in parenthesis). Setting parameters ------------------ .. note:: composite model (:class:`.FitModel` class) requires to set parameters by specifying the model component, this is different from versions<1.2.0 These methods are alternative and equivalent ways to set a parameter in a composite model: a) accessing the model component member of the b) using ``set_par`` and passing as first argument the model component name c) using ``set_par`` and passing as first argument the model component object .. code:: ipython3 #a composite_model.Franceschini_2008.parameters.z_cosm.val=0.1 #b composite_model.set_par('Franceschini_2008','z_cosm',0.1) #c composite_model.set_par(ebl_franceschini,'z_cosm',0.1) And now, we can define the functional form of the model composition, just by writing the mathematical expression as a string, using the model names reported in the model description table, and that’s it! .. code:: ipython3 composite_model.show_model_components() .. parsed-literal:: -------------------------------------------------------------------------------- Composite model description -------------------------------------------------------------------------------- name: EBL corrected type: composite_model components models: -model name: jet_flaring model type: jet -model name: Franceschini_2008 model type: table2D -------------------------------------------------------------------------------- .. code:: ipython3 composite_model.composite_expr='jet_flaring*Franceschini_2008' .. code:: ipython3 composite_model.jet_flaring.IC_nu_size=150 composite_model.eval() p=composite_model.plot_model() .. image:: Composite_model_files/Composite_model_24_0.png .. code:: ipython3 composite_model.save_model('composite.pkl') cm=FitModel.load_model('composite.pkl') .. parsed-literal:: ===> setting C threads to 12 Sum of two jets (steady and flaring) and application of the EBL absorption to both (Multiplicative and additive) ---------------------------------------------------------------------------------------------------------------- Assume that now we want to sum to jet models (a steady and flaring component) and apply to both of them the EBL absorption. .. code:: ipython3 composite_model=FitModel(nu_size=500,name='EBL corrected flaring+steady') composite_model.add_component(my_jet) composite_model.add_component(ebl_franceschini) .. parsed-literal:: /Users/orion/miniforge3/envs/jetset/lib/python3.10/site-packages/jetset/model_manager.py:158: UserWarning: no cosmology defined, using FlatLambdaCDM(name="Planck13", H0=67.77 km / (Mpc s), Om0=0.30712, Tcmb0=2.7255 K, Neff=3.046, m_nu=[0. 0. 0.06] eV, Ob0=0.048252) warnings.warn(m) .. code:: ipython3 steady_jet=Jet(electron_distribution='plc',name='steady_jet') composite_model.add_component(steady_jet) composite_model.show_model_components() .. parsed-literal:: ===> setting C threads to 12 -------------------------------------------------------------------------------- Composite model description -------------------------------------------------------------------------------- name: EBL corrected flaring+steady type: composite_model components models: -model name: jet_flaring model type: jet -model name: Franceschini_2008 model type: table2D -model name: steady_jet model type: jet -------------------------------------------------------------------------------- now we link the same parameter ``z_cosm`` from the ``steady_jet`` to the same ``master`` parameter used for the ``EBL`` model, i.e. to the ``jet_flaring`` model .. code:: ipython3 composite_model.link_par(par_name='z_cosm',from_model=['steady_jet'],to_model='Franceschini_2008') .. parsed-literal:: adding par: z_cosm to z_cosm .. code:: ipython3 composite_model.show_pars() .. raw:: html Table length=25
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_flaringRregion_sizecm5.000000e+151.000000e+031.000000e+30FalseFalse
jet_flaringR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_flaringBmagnetic_fieldgauss1.000000e-010.000000e+00--FalseFalse
jet_flaringNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_flaringbeam_objbeaming1.000000e+011.000000e-04--FalseFalse
jet_flaringz_cosm(L,Franceschini_2008)redshift------FalseTrue
jet_flaringgminlow-energy-cut-offlorentz-factor*2.000000e+001.000000e+001.000000e+09FalseFalse
jet_flaringgmaxhigh-energy-cut-offlorentz-factor*1.000000e+061.000000e+001.000000e+15FalseFalse
jet_flaringNemitters_density1 / cm31.000000e+020.000000e+00--FalseFalse
jet_flaringgamma0_log_parabturn-over-energylorentz-factor*1.000000e+041.000000e+001.000000e+09FalseFalse
jet_flaringsLE_spectral_slope2.000000e+00-1.000000e+011.000000e+01FalseFalse
jet_flaringrspectral_curvature4.000000e-01-1.500000e+011.500000e+01FalseFalse
Franceschini_2008scale_factorscale_factor1.000000e+000.000000e+00--FalseTrue
Franceschini_2008z_cosm(M)redshift1.000000e-010.000000e+00--FalseTrue
steady_jetRregion_sizecm5.000000e+151.000000e+031.000000e+30FalseFalse
steady_jetR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
steady_jetBmagnetic_fieldgauss1.000000e-010.000000e+00--FalseFalse
steady_jetNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
steady_jetbeam_objbeaming1.000000e+011.000000e-04--FalseFalse
steady_jetz_cosm(L,Franceschini_2008)redshift------FalseTrue
steady_jetgminlow-energy-cut-offlorentz-factor*2.000000e+001.000000e+001.000000e+09FalseFalse
steady_jetgmaxhigh-energy-cut-offlorentz-factor*1.000000e+061.000000e+001.000000e+15FalseFalse
steady_jetNemitters_density1 / cm31.000000e+020.000000e+00--FalseFalse
steady_jetgamma_cutturn-over-energylorentz-factor*1.000000e+041.000000e+001.000000e+09FalseFalse
steady_jetpLE_spectral_slope2.000000e+00-1.000000e+011.000000e+01FalseFalse
.. code:: ipython3 composite_model.steady_jet.IC_nu_size=150 .. code:: ipython3 composite_model.composite_expr='(jet_flaring + steady_jet) * Franceschini_2008' .. code:: ipython3 composite_model.eval() p=composite_model.plot_model() p.setlim(y_max=1E-12) .. image:: Composite_model_files/Composite_model_35_0.png .. code:: ipython3 composite_model.save_model('composite.pkl') .. code:: ipython3 cm=FitModel.load_model('composite.pkl') .. parsed-literal:: ===> setting C threads to 12 ===> setting C threads to 12 .. code:: ipython3 cm.show_pars() .. raw:: html Table length=25
model namenamepar typeunitsvalphys. bound. minphys. bound. maxlogfrozen
jet_flaringgminlow-energy-cut-offlorentz-factor*2.000000e+001.000000e+001.000000e+09FalseFalse
jet_flaringgmaxhigh-energy-cut-offlorentz-factor*1.000000e+061.000000e+001.000000e+15FalseFalse
jet_flaringNemitters_density1 / cm31.000000e+020.000000e+00--FalseFalse
jet_flaringgamma0_log_parabturn-over-energylorentz-factor*1.000000e+041.000000e+001.000000e+09FalseFalse
jet_flaringsLE_spectral_slope2.000000e+00-1.000000e+011.000000e+01FalseFalse
jet_flaringrspectral_curvature4.000000e-01-1.500000e+011.500000e+01FalseFalse
jet_flaringRregion_sizecm5.000000e+151.000000e+031.000000e+30FalseFalse
jet_flaringR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
jet_flaringBmagnetic_fieldgauss1.000000e-010.000000e+00--FalseFalse
jet_flaringNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
jet_flaringbeam_objbeaming1.000000e+011.000000e-04--FalseFalse
jet_flaringz_cosm(L,Franceschini_2008)redshift------FalseTrue
Franceschini_2008scale_factorscale_factor1.000000e+000.000000e+00--FalseTrue
Franceschini_2008z_cosm(M)redshift1.000000e-010.000000e+00--FalseTrue
steady_jetgminlow-energy-cut-offlorentz-factor*2.000000e+001.000000e+001.000000e+09FalseFalse
steady_jetgmaxhigh-energy-cut-offlorentz-factor*1.000000e+061.000000e+001.000000e+15FalseFalse
steady_jetNemitters_density1 / cm31.000000e+020.000000e+00--FalseFalse
steady_jetgamma_cutturn-over-energylorentz-factor*1.000000e+041.000000e+001.000000e+09FalseFalse
steady_jetpLE_spectral_slope2.000000e+00-1.000000e+011.000000e+01FalseFalse
steady_jetRregion_sizecm5.000000e+151.000000e+031.000000e+30FalseFalse
steady_jetR_Hregion_positioncm1.000000e+170.000000e+00--FalseTrue
steady_jetBmagnetic_fieldgauss1.000000e-010.000000e+00--FalseFalse
steady_jetNH_cold_to_rel_ecold_p_to_rel_e_ratio1.000000e+000.000000e+00--FalseTrue
steady_jetbeam_objbeaming1.000000e+011.000000e-04--FalseFalse
steady_jetz_cosm(L,Franceschini_2008)redshift------FalseTrue