{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Programming assignment\n", "\n", "This page shows how you can convert the classical Jupyter Notebook assignment to a jupyter book format with live coding. A widget has been implemented to check the answers, of which the data is included in a separate python file. The page automatically loads and a custom download page has been added\n", "\n", "```{custom_download_link} ./example_gumbel_stripped.ipynb\n", ":replace_default: \"True\"\n", "```" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Gumbel Distribution Exercise\n", "\n", "Imagine you are concerned with the concentration of an airborne contaminant, $X$, measured in ppm. A previous benchmark study estimates that there is a 10% and 1% chance, respectively, of exceeding 4 and 10 ppm, respectively. You have been asked by the regulatory agency to estimate the contaminant concentration with 0.1% probability of being exceeded. Prior studies suggest that a Gumbel distribution can be used to model contaminant concentration, given by the CDF:\n", "\n", "$$\n", "F_X(x) = \\exp{\\Bigg( -\\exp{\\Big(-\\frac{x-\\mu}{\\beta}\\Big)}\\Bigg)}\n", "$$\n", "\n", "Using the cell blocks below as a guide (and also to check your analysis): Task 1) find the parameters of a Gumbel distribution that matches the information provided above, then, Tasks 2-3) use it to estimate the concentration with exceedance probability 0.1%.\n", "\n", "To complete this assignment, you can use `numpy`, `matplotlib` and from the `math` library, `log` and `e` (these are imported for you when you inialize the notebook)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "thebe-remove-input-init" ] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from math import log, e\n", "import numpy as np\n", "from example_gumbel import check_example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task 0:** fill in the appropriate fitting points:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "auto-execute-page" ] }, "outputs": [], "source": [ "x_1 = _\n", "p_1 = _\n", "x_2 = _\n", "p_2 = _" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task 1:** derive the distribution parameters:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "def gumbel_2_points(x_1, p_1, x_2, p_2): \n", " \"\"\"Compute Gumbel distribution parameters from two points of the CDF.\n", " Arguments:\n", " x_1 (float): point one\n", " p_1 (float): cumulative probability for point 1\n", " x_2 (float): point two\n", " p_2 (float): cumulative probability for point 2\n", " \"\"\"\n", "\n", " # YOUR CODE GOES HERE #\n", " beta = _\n", " mu = _\n", " #######################\n", "\n", " return mu, beta" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The cells below will print your parameter values and create a plot to help confirm you have the right implementation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "thebe-remove-input-init" ] }, "outputs": [], "source": [ "def plot_distribution(mu, beta,\n", " x_1, p_1, x_2, p_2):\n", " plt.title(\"Gumbel Distribution, $1-F_X(x)$\")\n", " plt.xlabel(\"Contaminant Concentration, $X$ [ppm]\")\n", " plt.ylabel(\"Exceedance Probability [--]\")\n", " plt.grid(color='black', linestyle='-', linewidth=0.3)\n", " x_axis = np.arange(0, 20, 0.1)\n", " plt.plot(x_axis, np.vectorize(gumbel_distribution)(x_axis), linewidth=2)\n", " plt.plot(x_1, 1 - p_1, 'ro')\n", " plt.annotate(\"Point 1\", (x_1 + 0.4, 1 - p_1 + 0.02))\n", " plt.plot(x_2, 1 - p_2, 'ro')\n", " plt.annotate(\"Point 2\", (x_2 + 0.4, 1 - p_2 + 0.001))\n", " plt.yscale(\"log\") \n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu, beta = gumbel_2_points(x_1, p_1, x_2, p_2)\n", "\n", "print(f\"Your mu: {mu:0.5f}\\nYour beta: {beta:0.5f}\")\n", "\n", "gumbel_distribution = lambda x: 1 - e**(-e**(-(x - mu)/beta))\n", "\n", "plot_distribution(mu, beta, x_1, p_1, x_2, p_2)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task 2:** write a function to solve for the random variable value (it will be tested for you with the Check answer button, along with the distribution parameters)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def find_x_with_probability_p(p): \n", " \"\"\" Compute point in the gumbel distribution for which the CDF is p\n", " Use the variables mu and beta defined above!\n", " Hint: they have been defined globally, so you don't need to \n", " include them as arguments.\n", " \"\"\"\n", " # YOUR CODE GOES HERE #\n", " x = _\n", " #######################\n", "\n", " return x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task 3:** use the function `find_x_with_probability_p` to evaluate the random variable value with exceedance probabiltiy of 0.001." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# YOUR CODE GOES HERE #\n", "x = _\n", "#######################\n", "\n", "print(f\"The value of x with probability of exceedance 0.001 os {x:0.5f}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "thebe-remove-input-init" ] }, "outputs": [], "source": [ "check_example(globals())" ] }, { "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.9.18" } }, "nbformat": 4, "nbformat_minor": 2 }