{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# BEE 4750 Homework 4: Monte Carlo, Uncertainty, and Risk\n",
        "\n",
        "**Name**:\n",
        "\n",
        "**ID**:\n",
        "\n",
        "> **Due Date**\n",
        ">\n",
        "> Thursday, 10/08/26, 9:00pm\n",
        "\n",
        "## Overview\n",
        "\n",
        "### Instructions\n",
        "\n",
        "- Problem 1 consists of problems that should be solved by hand:\n",
        "  expectations, Monte Carlo error, and return periods.\n",
        "- Problem 2 is a short computational Monte Carlo analysis: propagate a\n",
        "  flood hazard distribution through a depth-damage function.\n",
        "\n",
        "### Load Environment\n",
        "\n",
        "The following code loads the environment and makes sure all needed\n",
        "packages are installed. This should be at the start of most Julia\n",
        "scripts."
      ],
      "id": "f8182d79-59b5-4e50-96d1-4cd010de8817"
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {},
      "outputs": [],
      "source": [
        "import Pkg\n",
        "Pkg.activate(@__DIR__)\n",
        "Pkg.instantiate()"
      ],
      "id": "2"
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {},
      "outputs": [],
      "source": [
        "using Random\n",
        "using Distributions\n",
        "using Statistics\n",
        "using Plots"
      ],
      "id": "4"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Problems (Total: 50 Points)\n",
        "\n",
        "### Problem 1 (30 points)\n",
        "\n",
        "#### Problem 1.1 (8 points)\n",
        "\n",
        "A treatment unit removes a fraction $R$ of the incoming load, where $R$\n",
        "is uncertain with mean $\\mathbb{E}[R] = 0.6$ and variance\n",
        "$\\text{Var}(R) = 0.01$. The incoming load is a known constant $B_0 = 50$\n",
        "mg/L, so the load leaving the unit is $Y = B_0(1 - R)$.\n",
        "\n",
        "1.  Find $\\mathbb{E}[Y]$ and $\\text{Var}(Y)$.\n",
        "\n",
        "2.  Now suppose the quantity you care about is the square of the\n",
        "    outgoing load, $Z = Y^2$. Is $\\mathbb{E}[Z]$ equal to\n",
        "    $\\left(\\mathbb{E}[Y]\\right)^2$? Compute both and state the\n",
        "    difference.\n",
        "\n",
        "3.  In one or two sentences, explain what this implies about running an\n",
        "    environmental model once at the mean value of an uncertain input.\n",
        "\n",
        "#### Problem 1.2 (7 points)\n",
        "\n",
        "You are estimating a probability by Monte Carlo. With $n = 2,000$\n",
        "samples your estimate is $\\hat{p} = 0.08$ and the standard error is\n",
        "$0.006$.\n",
        "\n",
        "1.  Write down the 95% confidence interval.\n",
        "\n",
        "2.  You need the standard error to be no larger than $0.002$. How many\n",
        "    samples will that take?\n",
        "\n",
        "3.  A colleague proposes reducing your error instead by using a finer\n",
        "    spatial grid in the underlying simulation. Will that help with\n",
        "    *this* source of error? Explain.\n",
        "\n",
        "#### Problem 1.3 (7 points)\n",
        "\n",
        "A pump station is designed against the 50-year flood.\n",
        "\n",
        "1.  What annual exceedance probability does that correspond to?\n",
        "\n",
        "2.  The station has a 20-year service life. Assuming years are\n",
        "    independent, write the expression for the probability that it is\n",
        "    overtopped at least once during that life. You do not need to\n",
        "    evaluate it.\n",
        "\n",
        "3.  Your client says: “the station was overtopped last year, so we have\n",
        "    another 49 years before it happens again.” Explain what is wrong\n",
        "    with this.\n",
        "\n",
        "#### Problem 1.4 (8 points)\n",
        "\n",
        "Classify each of the following as primarily **aleatory** or primarily\n",
        "**epistemic** uncertainty, and justify each in one sentence.\n",
        "\n",
        "1.  The daily variation in influent flow to a treatment plant.\n",
        "2.  The strength of the ice-albedo feedback.\n",
        "3.  The rate of sediment recycling in a shallow lake.\n",
        "4.  Which of next year’s storms will be the largest.\n",
        "\n",
        "### Problem 2 (20 points)\n",
        "\n",
        "Flood risk is usually quantified by propagating a distribution of flood\n",
        "depths through a depth-damage function, which converts a dflood epth\n",
        "into an economic loss. A common form for a house without a basement is a\n",
        "bounded logistic curve,\n",
        "\n",
        "$$d(h) = \\mathbb{1}_{h > 0} \\frac{L}{1 + \\exp\\left(-k(h - h_0)\\right)},$$\n",
        "\n",
        "where $h$ is the flood depth in metres and $d$ is the damage in dollars.\n",
        "We’ll use $L = \\$200,000$, $k = 0.8$, and $h_0 = 3$ in our depth-damage\n",
        "function specification.\n",
        "\n",
        "Assume the annual maximum flood depth at this structure is distributed\n",
        "as\n",
        "\n",
        "$$h \\sim \\text{LogNormal}(1.2,\\ 0.3).$$\n",
        "\n",
        "#### Problem 2.1 (5 points)\n",
        "\n",
        "Implement the depth-damage function and plot it over depths from 0 to\n",
        "8m. On a second panel, plot the density of the hazard distribution over\n",
        "the same range.\n",
        "\n",
        "Looking at the two panels together: over what range of depths does a\n",
        "small change in depth produce the largest change in damage?"
      ],
      "id": "eb0204a7-d978-4c63-a0bf-f0dd42496771"
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {},
      "outputs": [],
      "source": [
        "function depth_damage(depth)\n",
        "    # TODO: return the damage in dollars for a flood of this depth.\n",
        "    # A depth at or below zero causes no damage.\n",
        "    error(\"depth_damage has not been written yet -- replace this line\")\n",
        "end"
      ],
      "id": "6"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#### Problem 2.2 (8 points)\n",
        "\n",
        "Estimate the expected annual damage using Monte Carlo.\n",
        "\n",
        "Report your estimate, a 95% confidence interval, the sample size you\n",
        "used, and the seed you set. Then justify the sample size by doubling it\n",
        "and comparing the estimates.\n",
        "\n",
        "#### Problem 2.3 (7 points)\n",
        "\n",
        "1.  Now estimate the 99th percentile of annual damage. How much larger\n",
        "    is this than the mean?\n",
        "\n",
        "2.  For the same number of samples, your estimate of the 99th percentile\n",
        "    is less precise than your estimate of the mean. Explain why.\n",
        "\n",
        "3.  Your analysis produces a distribution of *damages*, not a\n",
        "    distribution of *depths*. In one or two sentences, explain why just\n",
        "    modeling depth does not fully capture risk, and identify which\n",
        "    component of risk the depth-damage function represents. Is anything\n",
        "    missing from this as a **risk analysis**?\n",
        "\n",
        "## References\n",
        "\n",
        "List any external references consulted, including classmates."
      ],
      "id": "90860dad-3e70-492b-8025-0afca8d86a6b"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernel_info": {
      "name": "julia"
    },
    "kernelspec": {
      "name": "julia",
      "display_name": "Julia",
      "language": "julia"
    },
    "language_info": {
      "name": "julia",
      "codemirror_mode": "julia",
      "version": "1.11.5"
    }
  }
}