.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/12_sum_model/01_fitting_sum_model.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_12_sum_model_01_fitting_sum_model.py: Fitting a Sum Model -------------------- In this tutorial, we demonstrate how to fit a sum model consisting of two covariance models to an empirical variogram. We will generate synthetic data, compute an empirical variogram, and fit a sum model combining a Spherical and Gaussian model to it. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import gstools as gs x = y = range(100) .. GENERATED FROM PYTHON SOURCE LINES 17-19 First, we create a synthetic random field based on a known sum model. This will serve as the ground truth for fitting. .. GENERATED FROM PYTHON SOURCE LINES 19-29 .. code-block:: Python # Define the true sum model m0 = gs.Spherical(dim=2, var=2.0, len_scale=5.0) m1 = gs.Gaussian(dim=2, var=1.0, len_scale=10.0) true_model = m0 + m1 # Generate synthetic field srf = gs.SRF(true_model, seed=20250405) field = srf.structured((x, y)) .. GENERATED FROM PYTHON SOURCE LINES 30-31 Next, we calculate the empirical variogram from the synthetic data. .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python # Compute empirical variogram bin_center, gamma = gs.vario_estimate((x, y), field) .. GENERATED FROM PYTHON SOURCE LINES 36-41 Now we define a sum model to fit to the empirical variogram. Initially, the parameters of the models are arbitrary. A sum model can also be created by a list of model classes together with the common arguments (like dim in this case). .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python fit_model = gs.SumModel(gs.Spherical, gs.Gaussian, dim=2) .. GENERATED FROM PYTHON SOURCE LINES 45-48 We fit the sum model to the empirical variogram using GSTools' built-in fitting capabilities. We deactivate the nugget fitting to not overparameterize our model. .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: Python fit_model.fit_variogram(bin_center, gamma, nugget=False) print(f"{true_model=}") print(f" {fit_model=}") .. rst-class:: sphx-glr-script-out .. code-block:: none true_model=SumModel(Spherical, Gaussian, dim=2, vars=[2.0, 1.0], len_scales=[5.0, 10.0]) fit_model=SumModel(Spherical, Gaussian, dim=2, vars=[2.1, 0.923], len_scales=[3.99, 14.2]) .. GENERATED FROM PYTHON SOURCE LINES 54-58 The variance of a sum model is the sum of the sub variances (:any:`SumModel.vars`) from the contained models. The length scale is a weighted sum of the sub length scales (:any:`SumModel.len_scales`) where the weights are the ratios of the sub variances to the total variance of the sum model. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python print(f"{true_model.var=:.2}, {true_model.len_scale=:.2}") print(f" {fit_model.var=:.2}, {fit_model.len_scale=:.2}") .. rst-class:: sphx-glr-script-out .. code-block:: none true_model.var=3.0, true_model.len_scale=6.7 fit_model.var=3.0, fit_model.len_scale=7.1 .. GENERATED FROM PYTHON SOURCE LINES 63-66 After fitting, we can visualize the empirical variogram alongside the fitted sum model and its components. A Sum Model is subscriptable to access the individual models its contains. .. GENERATED FROM PYTHON SOURCE LINES 66-77 .. code-block:: Python ax = fit_model.plot(x_max=max(bin_center)) ax.scatter(bin_center, gamma) # Extract individual components fit_model[0].plot(x_max=max(bin_center), ax=ax) fit_model[1].plot(x_max=max(bin_center), ax=ax) # True models true_model.plot(x_max=max(bin_center), ax=ax, ls="--", c="C0", label="") true_model[0].plot(x_max=max(bin_center), ax=ax, ls="--", c="C1", label="") true_model[1].plot(x_max=max(bin_center), ax=ax, ls="--", c="C2", label="") .. image-sg:: /examples/12_sum_model/images/sphx_glr_01_fitting_sum_model_001.png :alt: 01 fitting sum model :srcset: /examples/12_sum_model/images/sphx_glr_01_fitting_sum_model_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-81 As we can see, the fitted sum model closely matches the empirical variogram, demonstrating its ability to capture multi-scale variability effectively. The "true" variograms are shown with dashed lines for comparison. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.630 seconds) .. _sphx_glr_download_examples_12_sum_model_01_fitting_sum_model.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01_fitting_sum_model.ipynb <01_fitting_sum_model.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01_fitting_sum_model.py <01_fitting_sum_model.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01_fitting_sum_model.zip <01_fitting_sum_model.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_