{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fitting variogram data\n\nThe model class comes with a routine to fit the model-parameters to given\nvariogram data. In the following we will use the self defined stable model\nfrom a previous example.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport gstools as gs\n\n\nclass Stab(gs.CovModel):\n    def default_opt_arg(self):\n        return {\"alpha\": 1.5}\n\n    def cor(self, h):\n        return np.exp(-(h**self.alpha))\n\n\n# Exemplary variogram data (e.g. estimated from field observations)\nbins = [1.0, 3.0, 5.0, 7.0, 9.0, 11.0]\nest_vario = [0.2, 0.5, 0.6, 0.8, 0.8, 0.9]\n# fitting model\nmodel = Stab(dim=2)\n# we have to provide boundaries for the parameters\nmodel.set_arg_bounds(alpha=[0, 3])\nresults, pcov = model.fit_variogram(bins, est_vario, nugget=False)\nprint(\"Results:\", results)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = model.plot()\nax.scatter(bins, est_vario, color=\"k\", label=\"sample variogram\")\nax.legend()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As you can see, we have to provide boundaries for the parameters.\nAs a default, the following bounds are set:\n\n- additional parameters: ``[-np.inf, np.inf]``\n- variance: ``[0.0, np.inf]``\n- len_scale: ``[0.0, np.inf]``\n- nugget: ``[0.0, np.inf]``\n\nAlso, you can deselect parameters from fitting, so their predefined values\nwill be kept. In our case, we fixed a ``nugget`` of ``0.0``, which was set\nby default. You can deselect any standard or\noptional argument of the covariance model.\nThe second return value ``pcov`` is the estimated covariance of ``popt`` from\nthe used scipy routine :any:`scipy.optimize.curve_fit`.\n\nYou can use the following methods to manipulate the used bounds:\n\n.. currentmodule:: gstools.covmodel\n\n.. autosummary::\n   CovModel.default_opt_arg_bounds\n   CovModel.default_arg_bounds\n   CovModel.set_arg_bounds\n   CovModel.check_arg_bounds\n\nYou can override the :any:`CovModel.default_opt_arg_bounds`\nto provide standard bounds for your additional parameters.\n\nTo access the bounds you can use:\n\n.. autosummary::\n   CovModel.var_bounds\n   CovModel.len_scale_bounds\n   CovModel.nugget_bounds\n   CovModel.opt_arg_bounds\n   CovModel.arg_bounds\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}