{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Conditioning with Ordinary Kriging\n\nHere we use ordinary kriging in 1D (for plotting reasons)\nwith 5 given observations/conditions,\nto generate an ensemble of conditioned random fields.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\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]\ngridx = np.linspace(0.0, 15.0, 151)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The conditioned spatial random field class depends on a Krige class in order\nto handle the conditions.\nThis is created as described in the kriging tutorial.\n\nHere we use a Gaussian covariance model and ordinary kriging for conditioning\nthe spatial random field.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model = gs.Gaussian(dim=1, var=0.5, len_scale=1.5)\nkrige = gs.krige.Ordinary(model, cond_pos, cond_val)\ncond_srf = gs.CondSRF(krige)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fields = []\nfor i in range(100):\n    fields.append(cond_srf(gridx, seed=i))\n    label = \"Conditioned ensemble\" if i == 0 else None\n    plt.plot(gridx, fields[i], color=\"k\", alpha=0.1, label=label)\nplt.plot(gridx, cond_srf.krige(gridx, only_mean=True), label=\"estimated mean\")\nplt.plot(gridx, np.mean(fields, axis=0), linestyle=\":\", label=\"Ensemble mean\")\nplt.plot(gridx, cond_srf.krige.field, linestyle=\"dashed\", label=\"kriged field\")\nplt.scatter(cond_pos, cond_val, color=\"k\", zorder=10, label=\"Conditions\")\n# 99 percent confidence interval\nconf = gs.tools.confidence_scaling(0.99)\nplt.fill_between(\n    gridx,\n    cond_srf.krige.field - conf * np.sqrt(cond_srf.krige.krige_var),\n    cond_srf.krige.field + conf * np.sqrt(cond_srf.krige.krige_var),\n    alpha=0.3,\n    label=\"99% confidence interval\",\n)\nplt.legend()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As you can see, the kriging field coincides with the ensemble mean of the\nconditioned random fields and the estimated mean\nis the mean of the far-field.\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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}