{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<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",
    "## Data Analytics \n",
    "\n",
    "### Interactive Parametric Distributions  in Python \n",
    "\n",
    "\n",
    "#### Michael Pyrcz, Professor, The 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"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's a demonstration of making and general use of parametric distributions in Python. This demonstration is part of the resources that I include for my courses in Spatial / Subsurface Data Analytics at the Cockrell School of Engineering at the University of Texas at Austin.  \n",
    "\n",
    "#### Parametric Distributions\n",
    "\n",
    "I provide an set of interactives with thses parametric distributions:\n",
    "\n",
    "* Uniform\n",
    "* Triangular\n",
    "* Binomial\n",
    "* Poisson\n",
    "* Gaussian\n",
    "* Lognormal\n",
    "\n",
    "Change the distribution parameters and watch the probability density functions and cumulative distribution functions changes.\n",
    "\n",
    "I have a lecture on these parametric distributions available on [YouTube](https://www.youtube.com/watch?v=U7fGsqCLPHU&t=1687s).   \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",
    "#### Importing Packages\n",
    "\n",
    "We will need some standard packages. These should have been installed with Anaconda 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np                        # ndarrys for gridded data\n",
    "import pandas as pd                       # DataFrames for tabular data\n",
    "import os                                 # set working directory, run executables\n",
    "import matplotlib.pyplot as plt           # for plotting\n",
    "from matplotlib.ticker import (MultipleLocator, AutoMinorLocator) # control of axes ticks\n",
    "plt.rc('axes', axisbelow=True)            # set axes and grids in the background for all plots\n",
    "from scipy import stats                   # summary statistics\n",
    "import math                               # trigonometry etc.\n",
    "import scipy.signal as signal             # kernel for moving window calculation\n",
    "import random                             # for randon numbers\n",
    "import seaborn as sns                     # for matrix scatter plots\n",
    "from scipy import linalg                  # for linear regression\n",
    "from sklearn import preprocessing\n",
    "import geostatspy.GSLIB as GSLIB\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"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Uniform Distribution\n",
    "\n",
    "We start with the uniform distribution.\n",
    "\n",
    "* a continuous, maximum uncertainty distribution, with constant density between the minimum and maximum values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e075b713d22a4c2cab7fd2d5d23e8f08",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='        Uniform Parametric Distribution Demonstration, Michael Pyrcz, Associate Pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d9949455235c45638ff203791b31740a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='        Uniform Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zmin = widgets.FloatSlider(min=0.0, max = 100.0, value = 10.0, description = 'Min',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmin.style.handle_color = 'red'\n",
    "zmax = widgets.FloatSlider(min=0.0, max = 100.0, value = 30.0, description = 'Max',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmax.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zmin,zmax],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zmin,zmax):\n",
    "    xvals = np.linspace(0.0,100.0,1000)\n",
    "    pdf = stats.uniform.pdf(xvals, loc = zmin, scale = zmax-zmin)\n",
    "    cdf = stats.uniform.cdf(xvals, loc = zmin, scale = zmax-zmin)\n",
    "\n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Uniform Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    " \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Uniform Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zmin':zmin,'zmax':zmax})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Binomial Distribution\n",
    "\n",
    "Now the binomial distribution.\n",
    "\n",
    "* a discrete distribution of the probablity of a specific number of successes given $P(success)$ and a number of trails, $n$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "632051a6ea18494c8f49eb64fcb89188",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Binomial Parametric Distribution Demonstratio…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "43b7d1906a0441199f5f85395a31beab",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Binomial Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zp = widgets.FloatSlider(min=0.0, max = 1.0, value = 0.5, step = 0.01, description = '$P(Success)$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zp.style.handle_color = 'red'\n",
    "zn = widgets.IntSlider(min=2, max = 100, value = 10, description = '$N_{trials}$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zn.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zp,zn],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zp,zn):\n",
    "    xvals = np.linspace(0,101,102)\n",
    "    pdf = stats.binom.pmf(xvals, zn, zp)\n",
    "    cdf = stats.binom.cdf(xvals, zn, zp)\n",
    "    \n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([1,zn],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Probability\"); plt.title(\"Binomial Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks    \n",
    "    \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,zn],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Binomial Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zp':zp,'zn':zn})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Poisson Distribution\n",
    "\n",
    "Now the Poisson distribution.\n",
    "\n",
    "* a discrete distribution of the probablity of a specific number of successes given the average number of successes, $\\lambda$, over an interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "87b31a418b594d9f80f9443587c23890",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Poisson Parametric Distribution Demonstration…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7a8da6e12ab242b796f9aebc71617f19",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Poisson Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zlambda = widgets.IntSlider(min=1, max = 15, value = 5, step = 1.0, description = '$\\\\lambda$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zlambda.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zlambda],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zlambda):\n",
    "    n = 30\n",
    "    xvals = np.linspace(0,n+1,n+2)\n",
    "    pdf = stats.poisson.pmf(xvals, zlambda)\n",
    "    cdf = stats.poisson.cdf(xvals, zlambda)\n",
    "    \n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,n],[0,0],color='black')\n",
    "    plt.xlim(0,n); plt.ylim(0,0.40)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Probability\"); plt.title(\"Poisson Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "     \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,n],[0,0],color='black')\n",
    "    plt.xlim(1,n); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Poisson Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "        \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zlambda':zlambda})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Gaussian / Normal Distribution\n",
    "\n",
    "Now the Gaussian distribution.\n",
    "\n",
    "* an unbounded distribution (infinite tails) distribution parameterized by mean and standard deviation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ddf7abee469a40ca8b9f22c7e2afdde8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Gaussian Parametric Distribution Demonstratio…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b9f9ad202dbb476fbfa86e705c8454f3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Gaussian Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zmean = widgets.FloatSlider(min=0.0, max = 100.0, value = 50.0, description = '$\\overline{x}$/$\\\\mu$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmean.style.handle_color = 'red'\n",
    "zstdev = widgets.FloatSlider(min=0.0, max = 30.0, value = 5.0, description = '$s$/$\\\\sigma$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zstdev.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zmean,zstdev],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zmean,zstdev):\n",
    "    xvals = np.linspace(0.0,100.0,1000)\n",
    "    pdf = stats.norm.pdf(xvals, loc = zmean, scale = zstdev)\n",
    "    cdf = stats.norm.cdf(xvals, loc = zmean, scale = zstdev)\n",
    "\n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Gaussian Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Gaussian Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "    \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zmean':zmean,'zstdev':zstdev})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lognormal Distribution\n",
    "\n",
    "Now the lognormal distribution.\n",
    "\n",
    "* an unbounded distribution (infinite tails) distribution parameterized by mu and sigma."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "eed2f3b8c78a44aba8518cb30243d02d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Text(value='                                      Lognormal Parametric Distribution Demonstrati…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "efa4afa4a1e6448c88db537ae1cff6e0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive calculation of the random sample set (control of source parametric distribution and number of samples)\n",
    "l = widgets.Text(value='                                      Lognormal Parametric Distribution Demonstration, Michael Pyrcz, Associate Professor, The University of Texas at Austin',layout=Layout(width='950px', height='30px'))\n",
    "\n",
    "zmu = widgets.FloatSlider(min=0.1, max = 10.0, value = 1.0, description = 'Mu/$\\\\mu$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zmu.style.handle_color = 'red'\n",
    "zsigma = widgets.FloatSlider(min=0.1, max = 3.0, value = 1.0, description = 'Sigma/$\\\\sigma$',orientation='horizontal',layout=Layout(width='400px', height='50px'),continuous_update=False)\n",
    "zsigma.style.handle_color = 'red'\n",
    "\n",
    "ui1 = widgets.HBox([zmu,zsigma],kwargs = {'justify_content':'center'}) \n",
    "ui = widgets.VBox([l,ui1],kwargs = {'justify_content':'center'})\n",
    "\n",
    "def f_make(zmu,zsigma):\n",
    "    xvals = np.linspace(0.0,100.0,1000)\n",
    "    pdf = stats.lognorm.pdf(xvals, s = zsigma, scale = math.exp(zmu))\n",
    "    cdf = stats.lognorm.cdf(xvals, s = zsigma, scale = math.exp(zmu))\n",
    "\n",
    "    plt.subplot(121)\n",
    "    plt.plot(xvals,pdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.fill_between(xvals, 0, pdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.gca().set_ylim(bottom=0.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Density\"); plt.title(\"Lognormal Probability Density Function, $f_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "      \n",
    "    plt.subplot(122)\n",
    "    plt.fill_between(xvals, 0, cdf, facecolor='darkorange', interpolate=True, alpha = 0.7)\n",
    "    plt.plot(xvals,cdf,color='black',alpha=0.8,linewidth=2)\n",
    "    plt.plot([0,100],[0,0],color='black')\n",
    "    plt.xlim(0,100); plt.ylim(0,1.0)\n",
    "    plt.xlabel(\"$x$\"); plt.ylabel(\"Cumulative Probability\"); plt.title(\"Lognormal Cumulative Distribution Function, $F_x(x)$\")\n",
    "    plt.grid(True, which='major',linewidth = 1.0); plt.grid(True, which='minor',linewidth = 0.2) # add y grids\n",
    "    plt.tick_params(which='major',length=7); plt.tick_params(which='minor', length=4)\n",
    "    plt.gca().xaxis.set_minor_locator(AutoMinorLocator()); plt.gca().yaxis.set_minor_locator(AutoMinorLocator()) # turn on minor ticks\n",
    "      \n",
    "    plt.subplots_adjust(left=0.0, bottom=0.0, right=2.3, top=1.2, wspace=0.2, hspace=0.3)  \n",
    "    plt.plot()\n",
    "\n",
    "interactive_plot = widgets.interactive_output(f_make, {'zmu':zmu,'zsigma':zsigma})\n",
    "interactive_plot.clear_output(wait = True)                # reduce flickering by delaying plot updating    \n",
    "    \n",
    "display(ui, interactive_plot)                            # display the interactive plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are many other parametric distributions that we could have included. Also we could have demonstrated the distribution fitting. \n",
    "\n",
    "#### Comments\n",
    "\n",
    "This was a basic interactive demonstration of common parametric distributions. \n",
    "\n",
    "I have other demonstrations on the basics of working with DataFrames, ndarrays, univariate statistics, plotting data, declustering, data transformations, trend modeling and many other workflows available at [Python Demos](https://github.com/GeostatsGuy/PythonNumericalDemos) and a Python package for data analytics and geostatistics at [GeostatsPy](https://github.com/GeostatsGuy/GeostatsPy). \n",
    "  \n",
    "I hope this was helpful,\n",
    "\n",
    "*Michael*\n",
    "\n",
    "#### The Author:\n",
    "\n",
    "### Michael Pyrcz, Professor, The 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. Professor, Cockrell School of Engineering and 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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
