Joint, marginal and conditional distributions
Several questions use this joint pmf of and :
| 0.20 | 0.10 | 0.10 | |
| 0.05 | 0.25 | 0.30 |
Joint, marginal and conditional distributions (lesson 5.5.1)
0 of 9 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
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.
Covariance and correlation
Covariance, correlation and independence (lesson 5.5.2)
0 of 9 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
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 multivariate normal
The multivariate normal distribution (lesson 5.5.3)
0 of 8 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
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
Moments of a joint pmf, and a test of independence
A joint pmf is stored as a 2-D NumPy array p, with p[i, j] for the values xs[i] and ys[j]. Write two functions, as in lessons 5.5.1 and 5.5.2.
joint_moments(p, xs, ys)returns the tuple(mean_x, mean_y, var_x, var_y, cov, rho), from the marginals and .is_independent(p, tol=1e-12)returnsTruewhen every cell is withintolof the product of its two marginals, andFalseotherwise.
Both raise ValueError if p is not 2-D, has a negative entry, or does not add up to 1 within . joint_moments also raises it if the sizes of xs and ys do not match p. It raises it too if either variable is constant, since then is undefined.
The tests use lesson 5.5.1's three coins, the table above and case study 3.1.4's four values with covariance 0. They also try an independent table and each bad input.
See the tests
import numpy as np
COINS = np.array([[1, 1, 0], [1, 2, 1], [0, 1, 1]]) / 8
TABLE = np.array([[0.20, 0.10, 0.10], [0.05, 0.25, 0.30]])
FOUR = np.array([[0, 0.25], [0.25, 0], [0.25, 0], [0, 0.25]]) # X = 1..4, Y = 2 or 4
def _raises(f, *args, what=""):
try:
f(*args)
except ValueError:
return
raise AssertionError(f"expected ValueError: {what}")
def _moments(*args):
got = joint_moments(*args)
assert got is not None, "joint_moments returned None (did you forget to return?)"
return got
def test_coins():
"""Lesson 5.5.1's three coins: means 1, variances 1/2, covariance 1/4, correlation 1/2"""
expect_close(joint_moments(COINS, [0, 1, 2], [0, 1, 2]), (1, 1, 0.5, 0.5, 0.25, 0.5), what="joint_moments(coin table)")
def test_table():
"""The practice table: covariance 0.16 and correlation about 0.4123"""
got = joint_moments(TABLE, [0, 1], [0, 1, 2])
expect_close(got, (0.6, 1.15, 0.24, 0.6275, 0.16, 0.41229445762405846), what="joint_moments(practice table)")
def test_other_values():
"""With the values 10, 20 and 30 for Y: mean 21.5, variance 62.75, covariance 1.6, the same correlation"""
got = _moments(TABLE, [0, 1], [10, 20, 30])
expect_close(got, (0.6, 21.5, 0.24, 62.75, 1.6, 0.41229445762405846), what="joint_moments with ys = (10, 20, 30)")
def test_zero_covariance():
"""Case study 3.1.4's four values: covariance 0, yet not independent"""
got = _moments(FOUR, [1, 2, 3, 4], [2, 4])
expect_close(got[4], 0.0, what="covariance of the four values", abs_tol=1e-12)
expect_equal(is_independent(FOUR), False, what="is_independent(four values)")
def test_dependent():
"""The coin table and the practice table are not independent"""
expect_equal(is_independent(COINS), False, what="is_independent(coin table)")
expect_equal(is_independent(TABLE), False, what="is_independent(practice table)")
def test_independent():
"""A table built as a product of two marginals is independent, and its covariance is 0"""
p = np.outer([0.4, 0.6], [0.25, 0.5, 0.25])
expect_equal(is_independent(p), True, what="is_independent(product table)")
expect_close(_moments(p, [0, 1], [0, 1, 2])[4], 0.0, what="covariance of the product table", abs_tol=1e-12)
def test_tolerance():
"""A table off a product by 0.001 is independent only with a looser tolerance"""
p = np.outer([0.5, 0.5], [0.5, 0.5]) + np.array([[0.001, -0.001], [-0.001, 0.001]])
expect_equal(is_independent(p), False, what="is_independent with the default tolerance")
expect_equal(is_independent(p, tol=0.01), True, what="is_independent with tol=0.01")
def test_bad_tables():
"""A negative entry, a total that is not 1, or a 1-D array raises ValueError"""
_raises(joint_moments, [[0.6, -0.1], [0.3, 0.2]], [0, 1], [0, 1], what="a negative probability")
_raises(joint_moments, [[0.3, 0.3], [0.3, 0.3]], [0, 1], [0, 1], what="probabilities adding up to 1.2")
_raises(joint_moments, [0.5, 0.5], [0, 1], [0], what="a 1-D array")
_raises(is_independent, [[0.3, 0.3], [0.3, 0.3]], what="is_independent with a total of 1.2")
def test_bad_sizes():
"""Values that do not match the table's shape raise ValueError"""
_raises(joint_moments, TABLE, [0, 1, 2], [0, 1, 2], what="three values of x for two rows")
def test_constant():
"""A variable that takes one value has no correlation: ValueError"""
_raises(joint_moments, [[0.3, 0.7]], [5], [0, 1], what="X is always 5")