{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Finding the best fitting variogram model\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom matplotlib import pyplot as plt\n\nimport gstools as gs"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate a synthetic field with an exponential model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.random.RandomState(19970221).rand(1000) * 100.0\ny = np.random.RandomState(20011012).rand(1000) * 100.0\nmodel = gs.Exponential(dim=2, var=2, len_scale=8)\nsrf = gs.SRF(model, mean=0, seed=19970221)\nfield = srf((x, y))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Estimate the variogram of the field with 40 bins and plot the result.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "bins = np.arange(40)\nbin_center, gamma = gs.vario_estimate((x, y), field, bins)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define a set of models to test.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "models = {\n    \"Gaussian\": gs.Gaussian,\n    \"Exponential\": gs.Exponential,\n    \"Matern\": gs.Matern,\n    \"Stable\": gs.Stable,\n    \"Rational\": gs.Rational,\n    \"Circular\": gs.Circular,\n    \"Spherical\": gs.Spherical,\n    \"SuperSpherical\": gs.SuperSpherical,\n    \"JBessel\": gs.JBessel,\n}\nscores = {}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Iterate over all models, fit their variogram and calculate the r2 score.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# plot the estimated variogram\nplt.scatter(bin_center, gamma, color=\"k\", label=\"data\")\nax = plt.gca()\n\n# fit all models to the estimated variogram\nfor model in models:\n    fit_model = models[model](dim=2)\n    para, pcov, r2 = fit_model.fit_variogram(bin_center, gamma, return_r2=True)\n    fit_model.plot(x_max=40, ax=ax)\n    scores[model] = r2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a ranking based on the score and determine the best models\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ranking = sorted(scores.items(), key=lambda item: item[1], reverse=True)\nprint(\"RANKING by Pseudo-r2 score\")\nfor i, (model, score) in enumerate(ranking, 1):\n    print(f\"{i:>6}. {model:>15}: {score:.5}\")\n\nplt.show()"
      ]
    }
  ],
  "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
}