Densities and the uniform distribution
Densities, distribution functions and the uniform distribution (lesson 5.4.1)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
The exponential distribution
The exponential distribution (lesson 5.4.2)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
The normal distribution
The normal distribution (lesson 5.4.3)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Lognormal, Student t and chi-squared
The lognormal, Student t and chi-squared distributions (lesson 5.4.4)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
A quantile function for any distribution
Write quantile(cdf, p, lo, hi, tol=1e-10). It returns the -quantile of a distribution whose distribution function is cdf: the in with (lesson 5.4.1).
- Find it by bisection (lesson 1.3.2) on : halve the bracket until it is narrower than
tol, then return its midpoint. - Raise
ValueErrorunless : the 0- and 1-quantiles can be infinite. - Raise
ValueErrorif the bracket does not contain the quantile, that is if or .
The tests compare your answers with SciPy's ppf for a uniform, an exponential, a normal, a lognormal and a Student t. One bracket, for the 0.99-quantile of the standard normal, is too narrow.
See the tests
import math
from scipy.stats import expon, lognorm, norm, t, uniform
def test_uniform():
"""The 0.25-quantile of the uniform distribution on [2, 6] is 3"""
expect_close(quantile(uniform(loc=2, scale=4).cdf, 0.25, 2, 6), 3.0, what="quantile of U(2, 6) at 0.25", abs_tol=1e-8)
def test_exponential_median():
"""The median of an exponential with rate 0.5 is 2 ln 2, about 1.386294"""
expect_close(quantile(expon(scale=2).cdf, 0.5, 0, 50), 2 * math.log(2), what="median of Exp(0.5)", abs_tol=1e-8)
def test_normal_low_quantile():
"""The 0.01-quantile of N(500, 4²) is about 490.6946, as norm.ppf says"""
X = norm(500, 4)
expect_close(quantile(X.cdf, 0.01, 450, 550), X.ppf(0.01), what="0.01-quantile of N(500, 16)", abs_tol=1e-7)
def test_lognormal():
"""The 0.95-quantile of a lognormal with σ = 0.5 and median 1 is about 2.276017"""
X = lognorm(s=0.5)
expect_close(quantile(X.cdf, 0.95, 0, 20), X.ppf(0.95), what="0.95-quantile of the lognormal", abs_tol=1e-7)
def test_student_t():
"""The 0.975-quantile of a Student t with 4 degrees of freedom is about 2.776445"""
expect_close(quantile(t(4).cdf, 0.975, -50, 50), t(4).ppf(0.975), what="0.975-quantile of t(4)", abs_tol=1e-7)
def test_tolerance():
"""A looser tolerance still lands within it: the standard normal's 0.975-quantile to 0.001"""
x = quantile(norm.cdf, 0.975, -10, 10, tol=1e-3)
if abs(x - norm.ppf(0.975)) > 1e-3:
raise AssertionError(f"expected within 0.001 of 1.959964, got {x}")
def test_p_outside():
"""p of 0, 1 or 1.2 raises ValueError: those quantiles can be infinite or do not exist"""
for p in [0, 1, 1.2]:
try:
quantile(norm.cdf, p, -10, 10)
except ValueError:
continue
raise AssertionError(f"expected ValueError for p = {p}")
def test_bracket_too_narrow():
"""The 0.99-quantile of the standard normal, 2.326, is not in [−1, 1]: ValueError"""
try:
quantile(norm.cdf, 0.99, -1, 1)
except ValueError:
return
raise AssertionError("expected ValueError: F(1) = 0.8413 is below 0.99, so [-1, 1] misses the quantile")