{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nAssessing Stockdon et al (2006) runup model\n===========================================\n\n\nIn this example, we will evaluate the accuracy of the Stockdon et al (2006) runup\nmodel. To do this, we will use the compiled wave runup observations provided by Power\net al (2018).\n\nThe Stockdon et al (2006) wave runup model comprises of two relationships, one for\ndissipative beaches (i.e. $\\zeta < 0.3$) Eqn (18):\n\n\\begin{align}R_{2} = 0.043(H_{s}L_{p})^{0.5}\\end{align}\n\nand a seperate relationship for intermediate and reflective beaches (i.e.\n$\\zeta > 0.3$):\n\n\\begin{align}R_{2} = 1.1 \\left( 0.35 \\beta (H_{s}L_{p})^{0.5} + \\frac{H_{s}L_{p}(\n    0.563 \\beta^{2} +0.004)^{0.5}}{2} \\right)\\end{align}\n\nFirst, let's import our required packages:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.metrics import mean_squared_error, r2_score\n\nimport py_wave_runup"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's import the Power et al (2018) runup data, which is included in this\npackage.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "df = py_wave_runup.datasets.load_power18()\nprint(df.head())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can see that this dataset gives us $H_{s}$ (significant wave height),\n$T_{p}$ (peak wave period), $\\tan \\beta$ (beach slope). Let's import\nthe Stockdon runup model and calculate the estimated $R_{2}$ runup value for\neach row in this dataset.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Initalize the Stockdon 2006 model with values from the dataset\nsto06 = py_wave_runup.models.Stockdon2006(Hs=df.hs, Tp=df.tp, beta=df.beta)\n\n# Append a new column at the end of our dataset with Stockdon 2006 R2 estimations\ndf[\"sto06_r2\"] = sto06.R2\n\n# Check the first few rows of observed vs. modelled R2\nprint(df[[\"r2\", \"sto06_r2\"]].head())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now let's create a plot of observed R2 values vs. predicted R2 values:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Plot data\nfig, ax1 = plt.subplots(1, 1, figsize=(4, 4), dpi=300)\nax1.plot(df.r2, df.sto06_r2, \"b.\", markersize=2, linewidth=0.5)\n\n# Add 1:1 line to indicate perfect fit\nax1.plot([0, 12], [0, 12], \"k-\")\n\n# Add axis labels\nax1.set_xlabel(\"Observed R2 (m)\")\nax1.set_ylabel(\"Modelled R2 (m)\")\nax1.set_title(\"Stockdon et al. (2006) Runup Model\")\n\nplt.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can see there is a fair amount of scatter, especially as we get larger wave\nrunup heights. This indicates that the model might not be working as well as we\nmight have hoped.\n\nLet's also check RMSE and coefficient of determination values:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(f\"R2 Score: {r2_score(df.r2, df.sto06_r2):.2f}\")\nprint(f\"RMSE: {np.sqrt(mean_squared_error(df.r2, df.sto06_r2)):.2f} m\")"
      ]
    }
  ],
  "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.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}