.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_modeling_gallery_spectral_plot_compound.py>`     to download the full example code
    .. rst-class:: sphx-glr-example-title

    .. _sphx_glr_modeling_gallery_spectral_plot_compound.py:


.. _compound-spectral-model:

Compound Spectral Model
=======================

This model is formed by the arithmetic combination of any two other spectral models.

Example plot
------------
Here is an example plot of the model:


.. code-block:: default


    import operator
    from astropy import units as u
    import matplotlib.pyplot as plt
    from gammapy.modeling.models import (
        CompoundSpectralModel,
        LogParabolaSpectralModel,
        Models,
        PowerLawSpectralModel,
        SkyModel,
    )

    energy_range = [0.1, 100] * u.TeV
    pwl = PowerLawSpectralModel(
        index=2.0, amplitude="1e-12 cm-2 s-1 TeV-1", reference="1 TeV"
    )
    lp = LogParabolaSpectralModel(
        amplitude="1e-12 cm-2 s-1 TeV-1", reference="10 TeV", alpha=2.0, beta=1.0
    )
    model = CompoundSpectralModel(pwl, lp, operator.add)
    model.plot(energy_range)
    plt.grid(which="both")




.. image:: /modeling/gallery/spectral/images/sphx_glr_plot_compound_001.png
    :alt: plot compound
    :class: sphx-glr-single-img





YAML representation
-------------------
Here is an example YAML file using the model:


.. code-block:: default


    model = SkyModel(spectral_model=model, name="compound-model")
    models = Models([model])

    print(models.to_yaml())




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    components:
    -   name: compound-model
        type: SkyModel
        spectral:
            type: CompoundSpectralModel
            model1:
                type: PowerLawSpectralModel
                parameters:
                -   name: index
                    value: 2.0
                -   name: amplitude
                    value: 1.0e-12
                    unit: cm-2 s-1 TeV-1
                -   name: reference
                    value: 1.0
                    unit: TeV
                    frozen: true
            model2:
                type: LogParabolaSpectralModel
                parameters:
                -   name: amplitude
                    value: 1.0e-12
                    unit: cm-2 s-1 TeV-1
                -   name: reference
                    value: 10.0
                    unit: TeV
                    frozen: true
                -   name: alpha
                    value: 2.0
                -   name: beta
                    value: 1.0
            operator: add






.. _sphx_glr_download_modeling_gallery_spectral_plot_compound.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_compound.py <plot_compound.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_compound.ipynb <plot_compound.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_