Skip to content
Snippets Groups Projects
Commit 81be0ead authored by sheepmax's avatar sheepmax
Browse files

Added gumbel exercise skeleton

parent 5a4975d5
No related branches found
No related tags found
2 merge requests!51Blank book,!34Exercise checking
Pipeline #206420 passed
......@@ -67,6 +67,7 @@ parts:
- file: cookbook/benchmarks/benchmark_load_dont_execute.ipynb
- file: cookbook/widgets_dont_execute.ipynb
- file: cookbook/exercise_checking_dont_execute.ipynb
- file: cookbook/example_gumbel_dont_execute.ipynb
- caption: Old Material
chapters:
- file: old/blank
......
from grading import check
from math import isclose
tolerance = 0.001
# Using rel_tol is more reliable in the face of aliasing, but to fit the instructions give we use abs_total
check_float = lambda a, b: isclose(a, b, rel_tol=0, abs_tol=tolerance)
# We have to pass in the globals object since we need to have access to changes as well
@check
def check_example(glob):
mu, beta = glob["mu"], glob["beta"]
if (check_float(mu, 5.0) and check_float(beta, 3.0)):
print(f"You got the parameters right, well done! (checked with tolerance {tolerance})")
else:
print(f"Your parameters are incorrect, your inverse won't be graded until these are right. (checked with tolerance {tolerance})")
return
function = glob["find_x_with_probability_p"]
test_inputs = [(0.1, 2.49790266426), (0.2, 3.57234501402), (0.9, 11.7511019819)]
failed = []
for x, out in test_inputs:
result = function(x)
if not check_float(result, out):
failed.append((x, out, result))
if len(failed) == 0:
print(f"Well done, your inverse function is correct! (checked with tolerance {tolerance})")
else:
print(f"Your function failed some tests. Keep in mind the tolerance is {tolerance}")
print("Failed inputs:")
for case in failed:
print(f"{case[0]} gave {case[2]}, expected {case[1]}")
\ No newline at end of file
%% Cell type:markdown id: tags:
# Gumbel Distribution Exercise
%% Cell type:code id: tags:thebe-init
``` python
%pip install ipywidgets
import matplotlib.pyplot as plt
from math import log, e
import numpy as np
from example_gumbel import check_example
```
%% Cell type:code id: tags:thebe-init
``` python
x_1 = 4
p_e_1 = 0.7523186963342055
x_2 = 10
p_e_2 = 0.17211051260760846
```
%% Cell type:code id: tags:
``` python
def gumbel_2_points(x_1, p_e_1, x_2, p_e_2):
"""Compute Gumbel distribution parameters from two points of the CDF.
Arguments:
x_1 (float): point one
p_e_1 (float): probability of exceedance for point one
x_2 (float): point two
p_e_2 (float): probability of exceedance for point two
"""
# YOUR CODE GOES HERE #
beta = _
mu = _
#######################
return mu, beta
```
%% Cell type:code id: tags:
``` python
mu, beta = gumbel_2_points(x_1, p_e_1, x_2, p_e_2)
print(f"Your mu: {mu}\nYour beta: {beta}")
gumbel_distribution = lambda x: e**(-e**(-(x-mu)/beta))
plt.title("Gumbel distribution")
plt.ylabel("Cumulative probability")
plt.grid(color='black', linestyle='-', linewidth=0.1)
plt.plot(x_1, 1-p_e_1, 'ro')
plt.annotate("Point 1", (x_1 + 0.4, 1-p_e_1-0.03))
plt.plot(x_2, 1-p_e_2, 'ro')
plt.annotate("Point 2", (x_2 + 0.4, 1-p_e_2-0.03))
x_axis = np.arange(0, 20, 0.1)
plt.plot(x_axis, np.vectorize(gumbel_distribution)(x_axis))
plt.show()
```
%% Cell type:code id: tags:
``` python
def find_x_with_probability_p(p):
""" Compute point in the gumbel distribution for which the CDF is p
Use the global variables mu and beta defined above!
"""
# YOUR CODE GOES HERE #
p = _
#######################
return p
```
%% Cell type:code id: tags:thebe-remove-input-init
``` python
check_example(globals())
```
import ipywidgets as widgets
from IPython.display import display
def check(f):
def wrapper(*args, **kwargs):
output = widgets.Output()
button = widgets.Button(description="Check answer")
@output.capture(clear_output=True,wait=True)
def _inner_check(button):
f(*args, **kwargs)
button.on_click(_inner_check)
display(button, output)
return wrapper
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment