{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Log-Normal Kriging\n\nLog Normal kriging is a term to describe a special workflow for kriging to\ndeal with log-normal data, like conductivity or transmissivity in hydrogeology.\n\nIt simply means to first convert the input data to a normal distribution, i.e.\napplying a logarithic function, then interpolating these values with kriging\nand transforming the result back with the exponential function.\n\nThe resulting kriging variance describes the error variance of the log-values\nof the target variable.\n\nIn this example we will use ordinary kriging.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport gstools as gs\n\n# condtions\ncond_pos = [0.3, 1.9, 1.1, 3.3, 4.7]\ncond_val = [0.47, 0.56, 0.74, 1.47, 1.74]\n# resulting grid\ngridx = np.linspace(0.0, 15.0, 151)\n# stable covariance model\nmodel = gs.Stable(dim=1, var=0.5, len_scale=2.56, alpha=1.9)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In order to result in log-normal kriging, we will use the :any:`LogNormal`\nNormalizer. This is a parameter-less normalizer, so we don't have to fit it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "normalizer = gs.normalizer.LogNormal"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we generate the interpolated field as well as the mean field.\nThis can be done by setting `only_mean=True` in :any:`Krige.__call__`.\nThe result is then stored as `mean_field`.\n\nIn terms of log-normal kriging, this mean represents the geometric mean of\nthe field.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "krige = gs.krige.Ordinary(model, cond_pos, cond_val, normalizer=normalizer)\n# interpolate the field\nkrige(gridx)\n# also generate the mean field\nkrige(gridx, only_mean=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And that's it. Let's have a look at the results.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ax = krige.plot()\n# plotting the geometric mean\nkrige.plot(\"mean_field\", ax=ax)\n# plotting the conditioning data\nax.scatter(cond_pos, cond_val, color=\"k\", zorder=10, label=\"Conditions\")\nax.legend()"
      ]
    }
  ],
  "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
}