{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<p align=\"center\">\n",
    "    <img src=\"https://github.com/GeostatsGuy/GeostatsPy/blob/master/TCG_color_logo.png?raw=true\" width=\"220\" height=\"240\" />\n",
    "\n",
    "</p>\n",
    "\n",
    "## Interactive Simple Kriging Behavoir Demonstration\n",
    "\n",
    "\n",
    "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n",
    "\n",
    "\n",
    "### The Interactive Workflow\n",
    "\n",
    "Here's a simple workflow for demonstrating the behavoir of simple kriging with regard spatial continuity impacting:\n",
    "\n",
    "* data closeness\n",
    "* data redudancy\n",
    "\n",
    "See my YouTube channel for lectures on [spatial stationarity](https://youtu.be/QwxQ9xuUHlU), [trend modeling](https://youtu.be/BCnivpSKF18), and [kriging](https://youtu.be/CVkmuwF8cJ8).\n",
    "\n",
    "#### Spatial Estimation\n",
    "\n",
    "Consider the case of making an estimate at some unsampled location, $𝑧(\\mathbf{u}_0)$, where $z$ is the property of interest (e.g. porosity etc.) and $𝐮_0$ is a location vector describing the unsampled location.\n",
    "\n",
    "How would you do this given data, $𝑧(\\mathbf{𝐮}_1)$, $𝑧(\\mathbf{𝐮}_2)$, and $𝑧(\\mathbf{𝐮}_3)$?\n",
    "\n",
    "It would be natural to use a set of linear weights to formulate the estimator given the available data.\n",
    "\n",
    "\\begin{equation}\n",
    "z^{*}(\\mathbf{u}) = \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} z(\\mathbf{u}_{\\alpha})\n",
    "\\end{equation}\n",
    "\n",
    "We could add an unbiasedness constraint to impose the sum of the weights equal to one.  What we will do is assign the remainder of the weight (one minus the sum of weights) to the global average; therefore, if we have no informative data we will estimate with the global average of the property of interest.\n",
    "\n",
    "\\begin{equation}\n",
    "z^{*}(\\mathbf{u}) = \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} z(\\mathbf{u}_{\\alpha}) + \\left(1-\\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} \\right) \\overline{z}\n",
    "\\end{equation}\n",
    "\n",
    "We will make a stationarity assumption, so let's assume that we are working with residuals, $y$. \n",
    "\n",
    "\\begin{equation}\n",
    "y^{*}(\\mathbf{u}) = z^{*}(\\mathbf{u}) - \\overline{z}(\\mathbf{u})\n",
    "\\end{equation}\n",
    "\n",
    "If we substitute this form into our estimator the estimator simplifies, since the mean of the residual is zero.\n",
    "\n",
    "\\begin{equation}\n",
    "y^{*}(\\mathbf{u}) = \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} y(\\mathbf{u}_{\\alpha})\n",
    "\\end{equation}\n",
    "\n",
    "while satisfying the unbaisedness constraint.  \n",
    "\n",
    "#### Kriging\n",
    "\n",
    "Now the next question is what weights should we use?  \n",
    "\n",
    "We could use equal weighting, $\\lambda = \\frac{1}{n}$, and the estimator would be the average of the local data applied for the spatial estimate. This would not be very informative.\n",
    "\n",
    "We could assign weights considering the spatial context of the data and the estimate:\n",
    "\n",
    "* **spatial continuity** as quantified by the variogram (and covariance function)\n",
    "* **redundancy** the degree of spatial continuity between all of the available data with themselves \n",
    "* **closeness** the degree of spatial continuity between the avaiable data and the estimation location\n",
    "\n",
    "The kriging approach accomplishes this, calculating the best linear unbiased weights for the local data to estimate at the unknown location.  The derivation of the kriging system and the resulting linear set of equations is available in the lecture notes. \n",
    "\n",
    "\\begin{equation}\n",
    "\\sum_{i=1}^n \\lambda_i C( \\mathbf{u}_i, \\mathbf{u}_j) = C( \\mathbf{u},\\mathbf{u}_i), i = 1, \\ldots, n \n",
    "\\end{equation}\n",
    "\n",
    "\n",
    "In matrix notation we can write this system of equations for 3 data, $n = 3$, as:\n",
    "\n",
    "$$\n",
    "\\left[\\begin{array}{cc} \n",
    "C( \\mathbf{u}_1, \\mathbf{u}_1) & C( \\mathbf{u}_1, \\mathbf{u}_2) & C( \\mathbf{u}_1, \\mathbf{u}_3) \\\\ \n",
    "C( \\mathbf{u}_2, \\mathbf{u}_1) & C( \\mathbf{u}_2, \\mathbf{u}_2) & C( \\mathbf{u}_2, \\mathbf{u}_3) \\\\ \n",
    "C( \\mathbf{u}_3, \\mathbf{u}_1) & C( \\mathbf{u}_3, \\mathbf{u}_2) & C( \\mathbf{u}_3, \\mathbf{u}_3) \n",
    "\\end{array}\\right]\n",
    "\\left[\\begin{array}{cc} \n",
    "\\lambda_1 \\\\\n",
    "\\lambda_2 \\\\\n",
    "\\lambda_3 \\\\ \n",
    "\\end{array}\\right]\n",
    "=\n",
    "\\left[\\begin{array}{cc} \n",
    "C( \\mathbf{u}_1, \\mathbf{u}_0) \\\\\n",
    "C( \\mathbf{u}_2, \\mathbf{u}_0) \\\\\n",
    "C( \\mathbf{u}_3, \\mathbf{u}_0) \\\\ \n",
    "\\end{array}\\right]\n",
    "$$ \n",
    "\n",
    "To solve for the weights $\\lambda_1$, $\\lambda_2$ and $\\lambda_3$ we invert the left handside and multiply by the righthand side. \n",
    "\n",
    "Furthermore kriging provides a measure of the accuracy of the estimate!  This is the kriging estimation variance (sometimes just called the kriging variance).\n",
    "\n",
    "\\begin{equation}\n",
    "\\sigma^{2}_{E}(\\mathbf{u}) = C(0) - \\sum^{n}_{\\alpha = 1} \\lambda_{\\alpha} C(\\mathbf{u}_0, \\mathbf{u}_{\\alpha})\n",
    "\\end{equation}\n",
    "\n",
    "What is 'best' about this estimate? Kriging estimates are best in that they minimize the above estimation variance. \n",
    "\n",
    "#### Properties of Kriging\n",
    "\n",
    "Here are some important properties of kriging:\n",
    "\n",
    "* **Exact interpolator** - kriging estimates with the data values at the data locations\n",
    "* **Kriging variance** can be calculated before getting the sample information, as the kriging estimation variance is not dependent on the values of the data nor the kriging estimate, i.e. the kriging estimator is homoscedastic. \n",
    "* **Spatial context** - kriging takes into account, furthermore to the statements on spatial continuity, closeness and redundancy we can state that kriging accounts for the configuration of the data and structural continuity of the variable being estimated.\n",
    "* **Scale** - kriging may be generalized to account for the support volume of the data and estimate. We will cover this later.\n",
    "* **Multivariate** - kriging may be generalized to account for multiple secondary data in the spatial estimate with the cokriging system. We will cover this later.\n",
    "* **Smoothing effect** of kriging can be forecast. We will use this to build stochastic simulations later.\n",
    "\n",
    "#### Spatial Continuity \n",
    "\n",
    "**Spatial Continuity** is the correlation between values over distance.\n",
    "\n",
    "* No spatial continuity – no correlation between values over distance, random values at each location in space regardless of separation distance.\n",
    "\n",
    "* Homogenous phenomenon have perfect spatial continuity, since all values as the same (or very similar) they are correlated. \n",
    "\n",
    "We need a statistic to quantify spatial continuity! A convenient method is the Semivariogram.\n",
    "\n",
    "#### The Semivariogram\n",
    "\n",
    "Function of difference over distance.\n",
    "\n",
    "* The expected (average) squared difference between values separated by a lag distance vector (distance and direction), $\\mathbf{h}$:\n",
    "\n",
    "\\begin{equation}\n",
    "\\gamma(\\mathbf{h}) = \\frac{1}{2 N(\\mathbf{h})} \\sum^{N(\\mathbf{h})}_{\\alpha=1} (z(\\mathbf{u}_\\alpha) - z(\\mathbf{u}_\\alpha + \\mathbf{h}))^2  \n",
    "\\end{equation}\n",
    "\n",
    "where $z(\\mathbf{u}_\\alpha)$ and $z(\\mathbf{u}_\\alpha + \\mathbf{h})$ are the spatial sample values at tail and head locations of the lag vector respectively.\n",
    "\n",
    "* Calculated over a suite of lag distances to obtain a continuous function.\n",
    "\n",
    "* the $\\frac{1}{2}$ term converts a variogram into a semivariogram, but in practice the term variogram is used instead of semivariogram.\n",
    "* We prefer the semivariogram because it relates directly to the covariance function, $C_x(\\bf{h})$ and univariate variance, $\\sigma^2_x$:\n",
    "\n",
    "\\begin{equation}\n",
    "C_x(\\mathbf{h}) = \\sigma^2_x - \\gamma(\\mathbf{h})\n",
    "\\end{equation}\n",
    "\n",
    "Note the correlogram is related to the covariance function as:\n",
    "\n",
    "\\begin{equation}\n",
    "\\rho_x(\\mathbf{h}) = \\frac{C_x(\\mathbf{h})}{\\sigma^2_x}\n",
    "\\end{equation}\n",
    "\n",
    "The correlogram provides of function of the $z(\\mathbf{u}+\\mathbf{h})$ vs $z(\\mathbf{u})$ scatter plot correlation vs. lag offset $\\mathbf{h}$.  \n",
    "\n",
    "\\begin{equation}\n",
    "-1.0 \\le \\rho_x(\\mathbf{h}) \\le 1.0\n",
    "\\end{equation} \n",
    "\n",
    "#### Getting Started\n",
    "\n",
    "Here's the steps to get setup in Python with the GeostatsPy package:\n",
    "\n",
    "1. Install Anaconda 3 on your machine (https://www.anaconda.com/download/). \n",
    "2. From Anaconda Navigator (within Anaconda3 group), go to the environment tab, click on base (root) green arrow and open a terminal. \n",
    "3. In the terminal type: pip install geostatspy. \n",
    "4. Open Jupyter and in the top block get started by copy and pasting the code block below from this Jupyter Notebook to start using the geostatspy functionality. \n",
    "\n",
    "You will need to copy the data file to your working directory.  They are available here:\n",
    "\n",
    "* Tabular data - sample_data.csv at https://git.io/fh4gm.\n",
    "\n",
    "There are exampled below with these functions. You can go here to see a list of the available functions, https://git.io/fh4eX, other example workflows and source code. \n",
    "\n",
    "#### Load the required libraries\n",
    "\n",
    "The following code loads the required libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import geostatspy.GSLIB as GSLIB                       # GSLIB utilies, visualization and wrapper\n",
    "import geostatspy.geostats as geostats                 # GSLIB methods convert to Python    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will also need some standard packages. These should have been installed with Anaconda 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import os                                               # to set current working directory \n",
    "import sys                                              # supress output to screen for interactive variogram modeling\n",
    "import io\n",
    "import numpy as np                                      # arrays and matrix math\n",
    "import pandas as pd                                     # DataFrames\n",
    "import matplotlib.pyplot as plt                         # plotting\n",
    "from matplotlib.pyplot import cm                        # color maps\n",
    "from matplotlib.patches import Ellipse                  # plot an ellipse\n",
    "from matplotlib.patches import Rectangle\n",
    "import math                                             # sqrt operator\n",
    "from scipy.stats import norm\n",
    "from ipywidgets import interactive                      # widgets and interactivity\n",
    "from ipywidgets import widgets                            \n",
    "from ipywidgets import Layout\n",
    "from ipywidgets import Label\n",
    "from ipywidgets import VBox, HBox\n",
    "plt.rc('axes', axisbelow=True)                          # grid behind plotting elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you get a package import error, you may have to first install some of these packages. This can usually be accomplished by opening up a command window on Windows and then typing 'python -m pip install [package-name]'. More assistance is available with the respective package docs.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Simple, Simple Kriging Function\n",
    "\n",
    "Let's write a fast Python function to take data points and unknown location and provide the:\n",
    "\n",
    "* **simple kriging estimate**\n",
    "\n",
    "* **simple kriging variance / estimation variance**\n",
    "\n",
    "* **simple kriging weights**\n",
    "\n",
    "This provides a fast method for small datasets, with less parameters (no search parameters) and the ability to see the simple kriging weights "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def simple_simple_krige(df,xcol,ycol,vcol,dfl,xlcol,ylcol,vario,skmean):\n",
    "# load the variogram\n",
    "    nst = vario['nst']; pmx = 9999.9\n",
    "    cc = np.zeros(nst); aa = np.zeros(nst); it = np.zeros(nst)\n",
    "    ang = np.zeros(nst); anis = np.zeros(nst)\n",
    "    nug = vario['nug']; sill = nug \n",
    "    cc[0] = vario['cc1']; sill = sill + cc[0]\n",
    "    it[0] = vario['it1']; ang[0] = vario['azi1']; \n",
    "    aa[0] = vario['hmaj1']; anis[0] = vario['hmin1']/vario['hmaj1'];\n",
    "    if nst == 2:\n",
    "        cc[1] = vario['cc2']; sill = sill + cc[1]\n",
    "        it[1] = vario['it2']; ang[1] = vario['azi2']; \n",
    "        aa[1] = vario['hmaj2']; anis[1] = vario['hmin2']/vario['hmaj2'];    \n",
    "\n",
    "# set up the required matrices\n",
    "    rotmat, maxcov = geostats.setup_rotmat(nug,nst,it,cc,ang,pmx)    \n",
    "    ndata = len(df); a = np.zeros([ndata,ndata]); r = np.zeros(ndata); s = np.zeros(ndata); rr = np.zeros(ndata)\n",
    "    nest = len(dfl)\n",
    "\n",
    "    est = np.zeros(nest); var = np.full(nest,sill); weights = np.zeros([nest,ndata])\n",
    "\n",
    "# Make and solve the kriging matrix, calculate the kriging estimate and variance \n",
    "    for iest in range(0,nest):\n",
    "        for idata in range(0,ndata):\n",
    "            for jdata in range(0,ndata):\n",
    "                a[idata,jdata] = geostats.cova2(df[xcol].values[idata],df[ycol].values[idata],df[xcol].values[jdata],df[ycol].values[jdata],\n",
    "                                        nst,nug,pmx,cc,aa,it,ang,anis,rotmat,maxcov)\n",
    "            r[idata] = geostats.cova2(df[xcol].values[idata],df[ycol].values[idata],dfl[xlcol].values[iest],dfl[ylcol].values[iest],\n",
    "                                        nst,nug,pmx,cc,aa,it,ang,anis,rotmat,maxcov)\n",
    "            rr[idata] = r[idata]\n",
    "        \n",
    "        s = geostats.ksol_numpy(ndata,a,r)    \n",
    "        sumw = 0.0\n",
    "        for idata in range(0,ndata):                          \n",
    "            sumw = sumw + s[idata]\n",
    "            weights[iest,idata] = s[idata]\n",
    "            est[iest] = est[iest] + s[idata]*df[vcol].values[idata]\n",
    "            var[iest] = var[iest] - s[idata]*rr[idata]\n",
    "        est[iest] = est[iest] + (1.0-sumw)*skmean\n",
    "    return est,var,weights \n",
    "\n",
    "def calculate_new_point(prev_x,prev_y, offset, azimuth):\n",
    "    azimuth_rad = math.radians(azimuth)\n",
    "    x = prev_x + offset*math.sin(azimuth_rad)\n",
    "    y = prev_y + offset*math.cos(azimuth_rad)    \n",
    "    return x,y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Interactive Simple Kriging Closeness Method\n",
    "\n",
    "The following code includes:\n",
    "\n",
    "* dashboard with variogram model, data locations via subtended angle and radius \n",
    "* data locations with point scaled by weights and kriging weights over all subtended angles from 0 to 360"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings; warnings.simplefilter('ignore')\n",
    "\n",
    "# interactive calculation of the sample set (control of source parametric distribution and number of samples)\n",
    "style = {'description_width': 'initial'}\n",
    "l = widgets.Text(value='                                              Simple Kriging Closeness, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "nug = widgets.FloatSlider(min = 0, max = 1.0, value = 0.0, step = 0.1, description = '$c_{nug}$',orientation='vertical',\n",
    "                          readout_format='.0f',layout=Layout(width='25px', height='218px'))\n",
    "nug.style.handle_color = 'gray'\n",
    "it1 = widgets.Dropdown(options=['Sph', 'Exp', 'Gaus'],value='Sph',\n",
    "    description='$it_1$',disabled=False,layout=Layout(width='120px', height='30px'), style=style,continuous_update=False)\n",
    "\n",
    "azi = widgets.FloatSlider(min=0, max = 360, value = 0, step = 22.5, description = '$Azi$',\n",
    "                        orientation='vertical',readout_format='.0f',layout=Layout(width='40px', height='178px'),continuous_update=False)\n",
    "azi.style.handle_color = 'gray'\n",
    "hmaj1 = widgets.FloatSlider(min=0.01, max = 10000.0, value = 300.0, step = 25.0, description = '$a_{maj}$',\n",
    "                        orientation='vertical',readout_format='.0f',layout=Layout(width='40px', height='178px'),continuous_update=False)\n",
    "hmaj1.style.handle_color = 'gray'\n",
    "hmin1 = widgets.FloatSlider(min = 0.01, max = 10000.0, value = 300.0, step = 25.0, description = '$a_{min}$',\n",
    "                        orientation='vertical',readout_format='.0f',layout=Layout(width='40px', height='178px'),continuous_update=False)\n",
    "hmin1.style.handle_color = 'gray'\n",
    "uikvar = widgets.HBox([azi,hmaj1,hmin1],)                   # basic widget formatting\n",
    "uikvar2 = widgets.VBox([it1,uikvar],)                       # basic widget formatting\n",
    "uikvar3 = widgets.HBox([nug,uikvar2],)                      # basic widget formatting\n",
    "\n",
    "distance = widgets.FloatSlider(min=0.01, max = 501, value = 45.0, step = 25.0, description = r'distance',orientation='horizontal',\n",
    "                         layout=Layout(width='600px', height='30px'),readout_format = '.0f',style=style,continuous_update=False)\n",
    "distance.style.handle_color = 'blue'\n",
    "\n",
    "uidata = widgets.VBox([distance],) \n",
    "\n",
    "uipars = widgets.HBox([uikvar3,uidata],) \n",
    "\n",
    "uik_closeness = widgets.VBox([l,uipars],)\n",
    "\n",
    "def convert_type(it):\n",
    "    if it == 'Spherical': \n",
    "        return 1\n",
    "    elif it == 'Exponential':\n",
    "        return 2\n",
    "    else: \n",
    "        return 3\n",
    "\n",
    "def calc_krige_weights_distance(nug,it1,azi,hmaj1,hmin1,distance,):\n",
    "    text_trap = io.StringIO()\n",
    "    sys.stdout = text_trap\n",
    "    it1 = convert_type(it1)\n",
    "    nst = 1; xlag = 10; nlag = int(hmaj1/xlag); c1 = 1.0-nug\n",
    "    vario = GSLIB.make_variogram(nug,nst,it1,c1,azi,hmaj1,hmin1) # make model object\n",
    "    index_maj,h_maj,gam_maj,cov_maj,ro_maj = geostats.vmodel(nlag,xlag,azi,vario)   # project the model in the major azimuth                                                  # project the model in the 135 azimuth\n",
    "    index_min,h_min,gam_min,cov_min,ro_min = geostats.vmodel(nlag,xlag,azi+90.0,vario) # project the model in the minor azimuth\n",
    "    \n",
    "    xu = 500.0; yu = 500.0\n",
    "    x2 = 300; y2 = 500.0\n",
    "    x1,y1 = calculate_new_point(xu,yu,distance,90)\n",
    "    x = [x1,x2]; y = [y1,y2]; value = [1.0,2.0]\n",
    "    df = pd.DataFrame({'X':x,'Y':y,'Value':value})\n",
    "\n",
    "    xl = [xu,0,1]; yl = [yu,0,1]; value1 = [0,0,0]\n",
    "    dfl = pd.DataFrame({'X':xl,'Y':yl, 'Value':value1})\n",
    "    vmean = 2.0\n",
    "    sk_est, sk_var, sk_weights =  simple_simple_krige(df,'X','Y','Value',dfl,'X','Y',vario,skmean=vmean)\n",
    "    return sk_weights[0]\n",
    "    \n",
    "def f_make_krige_closeness(nug,it1,azi,hmaj1,hmin1,distance,): # function to take parameters, make sample and plot\n",
    "    text_trap = io.StringIO()\n",
    "    sys.stdout = text_trap\n",
    "    it1 = convert_type(it1)\n",
    "    nst = 1; xlag = 10; nlag = int(hmaj1/xlag); c1 = 1.0-nug\n",
    "    vario = GSLIB.make_variogram(nug,nst,it1,c1,azi,hmaj1,hmin1) # make model object\n",
    "    index_maj,h_maj,gam_maj,cov_maj,ro_maj = geostats.vmodel(nlag,xlag,azi,vario)   # project the model in the major azimuth                                                  # project the model in the 135 azimuth\n",
    "    index_min,h_min,gam_min,cov_min,ro_min = geostats.vmodel(nlag,xlag,azi+90.0,vario) # project the model in the minor azimuth\n",
    "    \n",
    "    xu = 500.0; yu = 500.0\n",
    "    x2 = 300; y2 = 500.0\n",
    "    x1,y1 = calculate_new_point(xu,yu,distance,90)\n",
    "\n",
    "    clist = ['blue','red']\n",
    "    x = [x1,x2]; y = [y1,y2]; value = [1.0,2.0]\n",
    "    df = pd.DataFrame({'X':x,'Y':y,'Value':value})\n",
    "\n",
    "    xl = [xu,0,1]; yl = [yu,0,1]; value1 = [0,0,0]\n",
    "    dfl = pd.DataFrame({'X':xl,'Y':yl, 'Value':value1})\n",
    "    vmean = 2.0\n",
    "    sk_est, sk_var, sk_weights =  simple_simple_krige(df,'X','Y','Value',dfl,'X','Y',vario,skmean=vmean)\n",
    "    \n",
    "    plt.subplot(1,2,1)\n",
    "        \n",
    "    ax = plt.gca()\n",
    "    plt.xlabel('X (m)'); plt.ylabel('Y (m)')\n",
    "    plt.title('Simple Kriging - Data and Unknown Locations')\n",
    "    plt.xlim([0,1000])\n",
    "    plt.ylim([0,1000])\n",
    "    for i, txt in enumerate(np.round(sk_weights[0],2)):\n",
    "        plt.annotate('$\\lambda$' + ' ' + '$=$' + str(txt), (x[i]+20, y[i]+20),color = clist[i])\n",
    "        plt.annotate(i+1, (x[i]+40, y[i]+15),fontsize=6,color = clist[i])\n",
    "#     for i, txt in enumerate(value):\n",
    "#         plt.annotate('$z(\\mathbf{u}$' + ' ' + '$ )$  = ' + str(txt), (x[i]+20, y[i]-40),color = clist[i])\n",
    "#         plt.annotate(i+1, (x[i]+80, y[i]-45),fontsize=6,color = clist[i])\n",
    "    plt.annotate('Sum of Weights:', (20, 60))\n",
    "    plt.annotate('$\\sum \\lambda_{\\\\alpha} = $'+ str(np.round(np.sum(sk_weights[0]),2)), (20, 20))\n",
    "    plt.annotate('Weight on Mean:', (280, 60))\n",
    "    plt.annotate('$1 - \\sum \\lambda_{\\\\alpha} = $'+ str(np.round(1-np.sum(sk_weights[0]),2)), (280, 20))\n",
    "    \n",
    "    plt.annotate('Distance, $\\mathbf{u}_2 - \\mathbf{u}_0$:', (20, 960))\n",
    "    plt.annotate(str(np.round(xu-x2)), (20, 920))\n",
    "    plt.annotate('Distance, $\\mathbf{u}_0 - \\mathbf{u}_1$:', (280, 960))\n",
    "    plt.annotate(' ' + str(np.round(x1-xu)), (280, 920))\n",
    "#     plt.annotate('$z^*(\\mathbf{u}_0)$ = '+ str(np.round(sk_est[0],2)), (xu+20, yu-40))\n",
    "#     plt.annotate('?', (xu-20, yu + 30))\n",
    "\n",
    "    ax = plt.gca()\n",
    "#     ellipse = Ellipse((xu, yu),width=radius*2,height=radius*2,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='black',ls='--',zorder=1)\n",
    "#     ax.add_patch(ellipse)\n",
    "    ellipse = Ellipse((xu, yu),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='gray',alpha = 0.1,edgecolor='black',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    ellipse = Ellipse((xu, yu),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='black',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    rectangle = Rectangle((xu-20, yu-20),width=40,height=40,angle = 0,facecolor='none',alpha = 1.0,edgecolor='black',zorder=1)\n",
    "    ax.add_patch(rectangle)\n",
    "    \n",
    "    #ellipse = Ellipse((x1, y1),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='blue',alpha = 0.1,zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x1, y1),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='blue',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x2, y2),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='red',alpha = 0.1,zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x2, y2),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='red',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x3, y3),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='green',alpha = 0.1,zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x3, y3),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='green',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "   \n",
    "    if sk_weights[0,0] > 0.01:\n",
    "        plt.scatter(x1,y1,color = 'blue', edgecolors = 'black', s = sk_weights[0,0]*2000, alpha = 0.3,zorder=100)\n",
    "        plt.scatter(x1,y1,color = 'none', edgecolors = 'black', s = sk_weights[0,0]*2000, alpha = 1.0,zorder=100)\n",
    "        plt.scatter(x1,y1,color = 'blue', edgecolors = 'black', marker = '+',s=50,alpha = 1.0,zorder=1,label=r'$\\bf{u}_1$')\n",
    "    else:\n",
    "        plt.scatter(x1,y1,color = 'blue', edgecolors = 'black', marker = 'x', alpha = 0.3,zorder=100,label=r'$\\bf{u}_1$')\n",
    "    if sk_weights[0,1] > 0.01:    \n",
    "        plt.scatter(x2,y2,color = 'red', edgecolors = 'black', s = sk_weights[0,1]*2000,alpha = 0.3,zorder=100)\n",
    "        plt.scatter(x2,y2,color = 'none', edgecolors = 'black', s = sk_weights[0,1]*2000,alpha = 1.0,zorder=100)\n",
    "        plt.scatter(x2,y2,color = 'red', edgecolors = 'black', marker = '+',s=50,alpha = 1.0,zorder=1,label=r'$\\bf{u}_2$')\n",
    "\n",
    "    else:\n",
    "        plt.scatter(x2,y2,color = 'red', edgecolors = 'black', marker = 'x',alpha = 0.3,zorder=100,label=r'$\\bf{u}_2$')\n",
    "\n",
    "    scatter = plt.scatter(xu,yu,color = 'black', edgecolors = 'black', marker = '+',s=50,alpha = 1.0,zorder=1,label=r'$\\bf{u}_0$')\n",
    "    plt.legend(loc = 'upper right'); plt.grid()\n",
    "    nbin = 100\n",
    "    ndistance = np.linspace(0.01,600,nbin)\n",
    "    wt1 = np.zeros(nbin); wt2 = np.zeros(nbin)\n",
    "    for iangle, cdistance in enumerate(ndistance):\n",
    "        wt1[iangle],wt2[iangle] = calc_krige_weights_distance(nug,it1,azi,hmaj1,hmin1,cdistance,)\n",
    "\n",
    "    plt.subplot(1,2,2)\n",
    "    plt.plot(ndistance,wt1,color='blue',alpha=0.6,lw=2,zorder=1,label='$\\lambda_1$')\n",
    "    plt.plot(ndistance,wt2,color='red',alpha=0.6,lw=2,zorder=1,label='$\\lambda_2$')\n",
    "    plt.plot(ndistance,wt1+wt2,color='purple',alpha=0.6,lw=2,zorder=1,label='$\\lambda_1+\\lambda_2$')\n",
    "    plt.plot(ndistance,1.0-(wt1+wt2),color='black',alpha=0.6,lw=2,zorder=1,label='$1-\\sum_{i=1}^{n}\\lambda_i$')\n",
    "    plt.scatter(distance,sk_weights[0,0],color='blue',s=100,edgecolor='black',alpha=0.3,zorder=10)\n",
    "    plt.scatter(distance,sk_weights[0,1],color='red',s=100,edgecolor='black',alpha=0.3)\n",
    "    plt.scatter(distance,1.0-(sk_weights[0,0]+sk_weights[0,1]),color='black',s=100,edgecolor='black',alpha=0.3)\n",
    "    plt.xlabel('Distance (m)'); plt.ylabel('Kriging Weight'); plt.title('Closeness Impact on Weights')\n",
    "    plt.vlines(distance,0.0,1.2,color='black',zorder=1)\n",
    "    plt.ylim([0.0,1.2]); plt.xlim([0,500]); plt.grid()\n",
    "    plt.legend(loc='upper right'); \n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.0, top=1.2, wspace=0.3, hspace=0.3)\n",
    "    plt.show()\n",
    "    \n",
    "# connect the function to make the samples and plot to the widgets    \n",
    "interactive_plot_closeness = widgets.interactive_output(f_make_krige_closeness, {'nug':nug, 'it1':it1, 'azi':azi, 'hmaj1':hmaj1, 'hmin1':hmin1, \n",
    "                                                      'distance':distance})\n",
    "interactive_plot_closeness.clear_output(wait = True)               # reduce flickering by delaying plot updating"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Interactive Simple Kriging Closeness Demonstration\n",
    "\n",
    "* select the variogram model and the data closeness to the unknown location\n",
    "\n",
    "#### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1) | [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy)\n",
    "\n",
    "### The Inputs\n",
    "\n",
    "Select the variogram model and the data locations:\n",
    "\n",
    "* $c_{nug}$, $C_1 = 1.0 - c_{nug}$, nugget effect contribution to the sill\n",
    "\n",
    "* $hmaj1$ / $hmin1$: range in the major and minor direction\n",
    "\n",
    "* $distance$: spatial data locations, subtended angle and radius "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ad9c70d9791a40028200482ff1a001bb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                              Simple Kriging Closeness, Michael Pyr…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ce50ff0ab640414ca43341ca1937921b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(outputs=({'output_type': 'display_data', 'data': {'text/plain': '<Figure size 432x288 with 2 Axes>', 'i…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(uik_closeness, interactive_plot_closeness)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Interactive Simple Kriging Redundancy Method\n",
    "\n",
    "The following code includes:\n",
    "\n",
    "* dashboard with variogram model, data locations via subtended angle and radius \n",
    "* data locations with point scaled by weights and kriging weights over all subtended angles from 0 to 360"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings; warnings.simplefilter('ignore')\n",
    "\n",
    "# interactive calculation of the sample set (control of source parametric distribution and number of samples)\n",
    "style = {'description_width': 'initial'}\n",
    "l = widgets.Text(value='                                              Simple Kriging, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "nug = widgets.FloatSlider(min = 0, max = 1.0, value = 0.0, step = 0.1, description = '$c_{nug}$',orientation='vertical',\n",
    "                          readout_format='.0f',layout=Layout(width='25px', height='218px'))\n",
    "nug.style.handle_color = 'gray'\n",
    "it1 = widgets.Dropdown(options=['Sph', 'Exp', 'Gaus'],value='Sph',\n",
    "    description='$it_1$',disabled=False,layout=Layout(width='120px', height='30px'), style=style,continuous_update=False)\n",
    "\n",
    "azi = widgets.FloatSlider(min=0, max = 360, value = 0, step = 22.5, description = '$Azi$',\n",
    "                        orientation='vertical',readout_format='.0f',layout=Layout(width='40px', height='178px'),continuous_update=False)\n",
    "azi.style.handle_color = 'gray'\n",
    "hmaj1 = widgets.FloatSlider(min=0.01, max = 10000.0, value = 600.0, step = 25.0, description = '$a_{maj}$',\n",
    "                        orientation='vertical',readout_format='.0f',layout=Layout(width='40px', height='178px'),continuous_update=False)\n",
    "hmaj1.style.handle_color = 'gray'\n",
    "hmin1 = widgets.FloatSlider(min = 0.01, max = 10000.0, value = 600.0, step = 25.0, description = '$a_{min}$',\n",
    "                        orientation='vertical',readout_format='.0f',layout=Layout(width='40px', height='178px'),continuous_update=False)\n",
    "hmin1.style.handle_color = 'gray'\n",
    "uikvar = widgets.HBox([azi,hmaj1,hmin1],)                   # basic widget formatting\n",
    "uikvar2 = widgets.VBox([it1,uikvar],)                       # basic widget formatting\n",
    "uikvar3 = widgets.HBox([nug,uikvar2],)                      # basic widget formatting\n",
    "\n",
    "angle = widgets.FloatSlider(min=0.01, max = 359, value = 45.0, step = 10.0, description = r'$\\alpha$',orientation='horizontal',\n",
    "                         layout=Layout(width='600px', height='30px'),readout_format = '.0f',style=style,continuous_update=False)\n",
    "angle.style.handle_color = 'blue'\n",
    "\n",
    "radius = widgets.FloatSlider(min=0.0, max = 1000.0, value = 200.0, step = 10.0, description = '$r$',orientation='horizontal',\n",
    "                         layout=Layout(width='600px', height='30px',margin='0 0 0 10px'),readout_format = '.0f',style=style,continuous_update=False)\n",
    "radius.style.handle_color = 'blue'\n",
    "\n",
    "uidata = widgets.VBox([angle,radius],) \n",
    "\n",
    "uipars = widgets.HBox([uikvar3,uidata],) \n",
    "\n",
    "uik_redundancy = widgets.VBox([l,uipars],)\n",
    "\n",
    "def convert_type(it):\n",
    "    if it == 'Spherical': \n",
    "        return 1\n",
    "    elif it == 'Exponential':\n",
    "        return 2\n",
    "    else: \n",
    "        return 3\n",
    "\n",
    "def calc_krige_weights(nug,it1,azi,hmaj1,hmin1,angle,radius,):\n",
    "    text_trap = io.StringIO()\n",
    "    sys.stdout = text_trap\n",
    "    it1 = convert_type(it1)\n",
    "    nst = 1; xlag = 10; nlag = int(hmaj1/xlag); c1 = 1.0-nug\n",
    "    vario = GSLIB.make_variogram(nug,nst,it1,c1,azi,hmaj1,hmin1) # make model object\n",
    "    index_maj,h_maj,gam_maj,cov_maj,ro_maj = geostats.vmodel(nlag,xlag,azi,vario)   # project the model in the major azimuth                                                  # project the model in the 135 azimuth\n",
    "    index_min,h_min,gam_min,cov_min,ro_min = geostats.vmodel(nlag,xlag,azi+90.0,vario) # project the model in the minor azimuth\n",
    "    \n",
    "    xu = 500.0; yu = 500.0\n",
    "    x3 = 500.0 + radius; y3 = 500.0\n",
    "    x1,y1 = calculate_new_point(xu,yu,radius,270-angle/2)\n",
    "    x2,y2 = calculate_new_point(xu,yu,radius,270+angle/2) \n",
    "    x = [x1,x2,x3]; y = [y1,y2,y3]; value = [1,2,3]\n",
    "    df = pd.DataFrame({'X':x,'Y':y,'Value':value})\n",
    "\n",
    "    xl = [xu,0,1]; yl = [yu,0,1]; value1 = [0,0,0]\n",
    "    dfl = pd.DataFrame({'X':xl,'Y':yl, 'Value':value1})\n",
    "    vmean = 2.0\n",
    "    sk_est, sk_var, sk_weights =  simple_simple_krige(df,'X','Y','Value',dfl,'X','Y',vario,skmean=vmean)\n",
    "    return sk_weights[0]\n",
    "    \n",
    "def f_make_krige_redudancy(nug,it1,azi,hmaj1,hmin1,angle,radius,): # function to take parameters, make sample and plot\n",
    "    text_trap = io.StringIO()\n",
    "    sys.stdout = text_trap\n",
    "    it1 = convert_type(it1)\n",
    "    nst = 1; xlag = 10; nlag = int(hmaj1/xlag); c1 = 1.0-nug\n",
    "    vario = GSLIB.make_variogram(nug,nst,it1,c1,azi,hmaj1,hmin1) # make model object\n",
    "    index_maj,h_maj,gam_maj,cov_maj,ro_maj = geostats.vmodel(nlag,xlag,azi,vario)   # project the model in the major azimuth                                                  # project the model in the 135 azimuth\n",
    "    index_min,h_min,gam_min,cov_min,ro_min = geostats.vmodel(nlag,xlag,azi+90.0,vario) # project the model in the minor azimuth\n",
    "    \n",
    "    xu = 500.0; yu = 500.0\n",
    "    x3 = 500.0 + radius; y3 = 500.0\n",
    "    x1,y1 = calculate_new_point(xu,yu,radius,270-angle/2)\n",
    "    x2,y2 = calculate_new_point(xu,yu,radius,270+angle/2)\n",
    " \n",
    "    clist = ['blue','red','green']\n",
    "    x = [x1,x2,x3]; y = [y1,y2,y3]; value = [1,2,3]\n",
    "    df = pd.DataFrame({'X':x,'Y':y,'Value':value})\n",
    "\n",
    "    xl = [xu,0,1]; yl = [yu,0,1]; value1 = [0,0,0]\n",
    "    dfl = pd.DataFrame({'X':xl,'Y':yl, 'Value':value1})\n",
    "    vmean = 2.0\n",
    "    sk_est, sk_var, sk_weights =  simple_simple_krige(df,'X','Y','Value',dfl,'X','Y',vario,skmean=vmean)\n",
    "    \n",
    "    plt.subplot(1,2,1)\n",
    "        \n",
    "    ax = plt.gca()\n",
    "    plt.xlabel('X (m)'); plt.ylabel('Y (m)')\n",
    "    plt.title('Simple Kriging - Data and Unknown Locations')\n",
    "    plt.xlim([0,1000])\n",
    "    plt.ylim([0,1000])\n",
    "    for i, txt in enumerate(np.round(sk_weights[0],2)):\n",
    "        plt.annotate('$\\lambda$' + ' ' + '$=$' + str(txt), (x[i]+20, y[i]+20),color = clist[i])\n",
    "        plt.annotate(i+1, (x[i]+46, y[i]+15),fontsize=6,color = clist[i])\n",
    "#     for i, txt in enumerate(value):\n",
    "#         plt.annotate('$z(\\mathbf{u}$' + ' ' + '$ )$  = ' + str(txt), (x[i]+20, y[i]-40),color = clist[i])\n",
    "#         plt.annotate(i+1, (x[i]+80, y[i]-45),fontsize=6,color = clist[i])\n",
    "    plt.annotate('$\\sum \\lambda_{\\\\alpha} = $'+ str(np.round(np.sum(sk_weights[0]),2)), (xu+20, yu+20))\n",
    "#     plt.annotate('$z^*(\\mathbf{u}_0)$ = '+ str(np.round(sk_est[0],2)), (xu+20, yu-40))\n",
    "    plt.annotate('?', (xu-20, yu + 30))\n",
    "\n",
    "    ax = plt.gca()\n",
    "    ellipse = Ellipse((xu, yu),width=radius*2,height=radius*2,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='black',ls='--',zorder=1)\n",
    "    ax.add_patch(ellipse)\n",
    "    ellipse = Ellipse((xu, yu),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='gray',alpha = 0.1,edgecolor='black',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    ellipse = Ellipse((xu, yu),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='black',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x1, y1),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='blue',alpha = 0.1,zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x1, y1),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='blue',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x2, y2),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='red',alpha = 0.1,zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x2, y2),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='red',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x3, y3),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='green',alpha = 0.1,zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "    #ellipse = Ellipse((x3, y3),width=hmin1*2.0,height=hmaj1*2.0,angle = 360-azi,facecolor='none',alpha = 1.0,edgecolor='green',zorder=10)\n",
    "    ax.add_patch(ellipse)\n",
    "   \n",
    "    if sk_weights[0,0] > 0.01:\n",
    "        plt.scatter(x1,y1,color = 'blue', edgecolors = 'black', s = sk_weights[0,0]*1000, alpha = 0.3,zorder=100,label=r'$\\bf{u}_1$')\n",
    "    else:\n",
    "        plt.scatter(x1,y1,color = 'blue', edgecolors = 'black', marker = 'x', alpha = 0.3,zorder=100,label=r'$\\bf{u}_1$')\n",
    "    if sk_weights[0,1] > 0.01:    \n",
    "        plt.scatter(x2,y2,color = 'red', edgecolors = 'black', s = sk_weights[0,1]*1000,alpha = 0.3,zorder=100,label=r'$\\bf{u}_2$')\n",
    "    else:\n",
    "        plt.scatter(x2,y2,color = 'red', edgecolors = 'black', marker = 'x',alpha = 0.3,zorder=100,label=r'$\\bf{u}_2$')\n",
    "    if sk_weights[0,2] > 0.01:\n",
    "        plt.scatter(x3,y3,color = 'green', edgecolors = 'black', s = sk_weights[0,2]*1000, alpha = 0.3,zorder=100,label=r'$\\bf{u}_3$')\n",
    "    else:\n",
    "        plt.scatter(x3,y3,color = 'green', edgecolors = 'black', marker = 'x', alpha = 0.3,zorder=100,label=r'$\\bf{u}_3$')\n",
    "       \n",
    "    scatter = plt.scatter(xu,yu,color = 'black', edgecolors = 'black', marker = 'x',s = 40,alpha = 0.3,zorder=100,label=r'$\\bf{u}_0$')\n",
    "    plt.legend(loc = 'upper right')\n",
    "    nbin = 100\n",
    "    nangle = np.linspace(0.01,359,nbin)\n",
    "    wt1 = np.zeros(nbin); wt2 = np.zeros(nbin); wt3 = np.zeros(nbin) \n",
    "    for iangle, cangle in enumerate(nangle):\n",
    "        wt1[iangle],wt2[iangle],wt3[iangle] = calc_krige_weights(nug,it1,azi,hmaj1,hmin1,cangle,radius,)\n",
    "\n",
    "    plt.subplot(1,2,2)\n",
    "    plt.plot(nangle,wt1,color='blue',alpha=0.6,ls='--',label='$\\lambda_1$')\n",
    "    plt.plot(nangle,wt2,color='red',alpha=0.6,ls='-.',label='$\\lambda_2$')\n",
    "    plt.plot(nangle,wt3,color='green',alpha=0.6,lw=2,label='$\\lambda_3$')\n",
    "    plt.plot(nangle,wt1+wt2,color='purple',alpha=0.6,lw=2,label='$\\lambda_1+\\lambda_2$')\n",
    "    plt.scatter(angle,sk_weights[0,0],color='blue',s=100,edgecolor='black',alpha=0.3)\n",
    "    plt.scatter(angle,sk_weights[0,1],color='red',s=100,edgecolor='black',alpha=0.3)\n",
    "    plt.scatter(angle,sk_weights[0,2],color='green',s=100,edgecolor='black',alpha=0.3)\n",
    "    plt.xlabel('Subtended Angle'); plt.ylabel('Kriging Weight'); plt.title('Redundancy Impact on Weights')\n",
    "    plt.vlines(angle,0.1,1.0,color='black',zorder=1)\n",
    "    plt.ylim([0.1,1.0]); plt.xlim([0,360]); plt.grid()\n",
    "    plt.legend(loc='upper right')\n",
    "    \n",
    "#     plt.hist(samples,histtype = 'stepfilled',cumulative = True, bins = np.linspace(vmin,vmax,200),alpha=0.8,color=\"darkorange\",edgecolor='black',density=True)\n",
    "#     plt.xlim([vmin,vmax]); plt.ylim([0,1.0])\n",
    "#     plt.title('Kriging Uncertainty Model at Unknown Location')\n",
    "#     plt.xlabel('Value'); plt.ylabel('Frequency')\n",
    "    \n",
    "#     ax = plt.gca()\n",
    "#     ax.annotate(r'$z^*(\\mathbf{u}_0)$ = ' + str(np.round(sk_est[0],2)), (0.05*(vmax-vmin)+vmin, 0.9))\n",
    "#     ax.annotate(r'$\\sigma_z^2(\\mathbf{u}_0)$ = ' + str(np.round(sk_var[0],2)), (0.05*(vmax-vmin)+vmin, 0.83))\n",
    "#     ax.annotate(r'$P10_z(\\mathbf{u}_0)$ = ' + str(np.round(np.percentile(samples,10),2)), (0.05*(vmax-vmin)+vmin, 0.76))\n",
    "#     ax.annotate(r'$P90_z(\\mathbf{u}_0)$ = ' + str(np.round(np.percentile(samples,90),2)), (0.05*(vmax-vmin)+vmin, 0.69))\n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.0, top=1.2, wspace=0.3, hspace=0.3)\n",
    "    plt.show()\n",
    "    \n",
    "# connect the function to make the samples and plot to the widgets    \n",
    "interactive_plot_redundancy = widgets.interactive_output(f_make_krige_redudancy, {'nug':nug, 'it1':it1, 'azi':azi, 'hmaj1':hmaj1, 'hmin1':hmin1, \n",
    "                                                      'angle':angle, 'radius':radius})\n",
    "interactive_plot_redundancy.clear_output(wait = True)               # reduce flickering by delaying plot updating"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Interactive Simple Kriging Redudancy Demonstration\n",
    "\n",
    "* select the variogram model and the data radius subtended angle of the data\n",
    "\n",
    "#### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "\n",
    "##### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1) | [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy)\n",
    "\n",
    "### The Inputs\n",
    "\n",
    "Select the variogram model and the data locations:\n",
    "\n",
    "* $c_{nug}$, $C_1 = 1.0 - c_{nug}$, nugget effect contribution to the sill\n",
    "\n",
    "* $hmaj1$ / $hmin1$: range in the major and minor direction\n",
    "\n",
    "* $\\alpha$, $r$: spatial data locations, subtended angle and radius "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "51eb0633a2094b59b286976283d5aab6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                              Simple Kriging, Michael Pyrcz, Associ…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e44b734dc63a45cda066102600114a6f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output(outputs=({'output_type': 'display_data', 'data': {'text/plain': '<Figure size 432x288 with 2 Axes>', 'i…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(uik_redundancy, interactive_plot_redundancy)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comments\n",
    "\n",
    "This was an interactive demonstration of simple kriging behavoir, the impact of data closeness and redudancy on kriging weights for spatial data analytics. Much more could be done, I have other demonstrations on the basics of working with DataFrames, ndarrays, univariate statistics, plotting data, declustering, data transformations and many other workflows available at https://github.com/GeostatsGuy/PythonNumericalDemos and https://github.com/GeostatsGuy/GeostatsPy. \n",
    "  \n",
    "#### The Author:\n",
    "\n",
    "### Michael Pyrcz, Associate Professor, University of Texas at Austin \n",
    "*Novel Data Analytics, Geostatistics and Machine Learning Subsurface Solutions*\n",
    "\n",
    "With over 17 years of experience in subsurface consulting, research and development, Michael has returned to academia driven by his passion for teaching and enthusiasm for enhancing engineers' and geoscientists' impact in subsurface resource development. \n",
    "\n",
    "For more about Michael check out these links:\n",
    "\n",
    "#### [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)\n",
    "\n",
    "#### Want to Work Together?\n",
    "\n",
    "I hope this content is helpful to those that want to learn more about subsurface modeling, data analytics and machine learning. Students and working professionals are welcome to participate.\n",
    "\n",
    "* Want to invite me to visit your company for training, mentoring, project review, workflow design and / or consulting? I'd be happy to drop by and work with you! \n",
    "\n",
    "* Interested in partnering, supporting my graduate student research or my Subsurface Data Analytics and Machine Learning consortium (co-PIs including Profs. Foster, Torres-Verdin and van Oort)? My research combines data analytics, stochastic modeling and machine learning theory with practice to develop novel methods and workflows to add value. We are solving challenging subsurface problems!\n",
    "\n",
    "* I can be reached at mpyrcz@austin.utexas.edu.\n",
    "\n",
    "I'm always happy to discuss,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "Michael Pyrcz, Ph.D., P.Eng. Associate Professor The Hildebrand Department of Petroleum and Geosystems Engineering, Bureau of Economic Geology, The Jackson School of Geosciences, The University of Texas at Austin\n",
    "\n",
    "#### More Resources Available at: [Twitter](https://twitter.com/geostatsguy) | [GitHub](https://github.com/GeostatsGuy) | [Website](http://michaelpyrcz.com) | [GoogleScholar](https://scholar.google.com/citations?user=QVZ20eQAAAAJ&hl=en&oi=ao) | [Book](https://www.amazon.com/Geostatistical-Reservoir-Modeling-Michael-Pyrcz/dp/0199731446) | [YouTube](https://www.youtube.com/channel/UCLqEr-xV-ceHdXXXrTId5ig)  | [LinkedIn](https://www.linkedin.com/in/michael-pyrcz-61a648a1)  \n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
