Programming assignment#
This page shows how you can recreate the classical Jupyter Notebook assignments that are given in many courses 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
Note
Keep in mind that Jupyter Book sections might not be the best environment for programming assignments you have in mind for your course due to the limitation posed by jupyter book. In fact, in jupyter book, python runs in the browser of the students’ laptop. Nothing needs to be installed which can be an advantage but also a disadvantage depending on the learning goals of your course. TeachBooks suggests using it’s live coding feature to support the theory in textbooks and not as a replacement for Jupyter Notebook.
Gumbel Distribution Exercise#
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:
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%.
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).
import matplotlib.pyplot as plt
from math import log, e
import numpy as np
from example_gumbel import check_example
Task 0: fill in the appropriate fitting points:
x_1 = _
p_1 = _
x_2 = _
p_2 = _
Task 1: derive the distribution parameters:
def gumbel_2_points(x_1, p_1, x_2, p_2):
"""Compute Gumbel distribution parameters from two points of the CDF.
Arguments:
x_1 (float): point one
p_1 (float): cumulative probability for point 1
x_2 (float): point two
p_2 (float): cumulative probability for point 2
"""
# YOUR CODE GOES HERE #
beta = _
mu = _
#######################
return mu, beta
The cells below will print your parameter values and create a plot to help confirm you have the right implementation.
def plot_distribution(mu, beta,
x_1, p_1, x_2, p_2):
plt.title("Gumbel Distribution, $1-F_X(x)$")
plt.xlabel("Contaminant Concentration, $X$ [ppm]")
plt.ylabel("Exceedance Probability [--]")
plt.grid(color='black', linestyle='-', linewidth=0.3)
x_axis = np.arange(0, 20, 0.1)
plt.plot(x_axis, np.vectorize(gumbel_distribution)(x_axis), linewidth=2)
plt.plot(x_1, 1 - p_1, 'ro')
plt.annotate("Point 1", (x_1 + 0.4, 1 - p_1 + 0.02))
plt.plot(x_2, 1 - p_2, 'ro')
plt.annotate("Point 2", (x_2 + 0.4, 1 - p_2 + 0.001))
plt.yscale("log")
plt.show()
mu, beta = gumbel_2_points(x_1, p_1, x_2, p_2)
print(f"Your mu: {mu:0.5f}\nYour beta: {beta:0.5f}")
gumbel_distribution = lambda x: 1 - e**(-e**(-(x - mu)/beta))
plot_distribution(mu, beta, x_1, p_1, x_2, p_2)
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).
def find_x_with_probability_p(p):
""" Compute point in the gumbel distribution for which the CDF is p
Use the variables mu and beta defined above!
Hint: they have been defined globally, so you don't need to
include them as arguments.
"""
# YOUR CODE GOES HERE #
x = _
#######################
return x
Task 3: use the function find_x_with_probability_p
to evaluate the random variable value with exceedance probabiltiy of 0.001.
# YOUR CODE GOES HERE #
x = _
#######################
print(f"The value of x with probability of exceedance 0.001 os {x:0.5f}")
check_example(globals())