PaC Exercise 2.6
import numpy as np
Exercise 2.6
Suppose that we independently roll two standard six-sided dice. Let X1 be the number that shows on the first die, X2 the number on the second die, and X the sum of the numbers on the two dice.
- What is E[X|X1 is even]?
Solution: By linearity
$$
E[X | X_1 \text{ is even}] = E[X_1 | X_1 \text{ is even}] + E[X_2 | X_1 \text{ is even}]
= \frac{2+4+6}{3} + \frac{7}{2} = \frac{15}{2} = 7.5.
$$
Intuitively, this makes sense since we expect this expectation value to be slightly larger than E[X] = 7 since the even numbers are larger on average than the odd numbers on a standard die.
- What is E[X|X1 = X2]?
Note that if X1 = X2 then X is even. Thus,
$$
E[X | X_1 = X_2] = \sum_{i=2}^{12} i \cdot P(X=i | X_1 = X_2) = \frac{2+4+\dots+12}{6} = 7.
$$
Here we used that P(X = i|X1 = X2) = 1/6 for even i.
- What is E[X1|X = 9]?
If X = 9 then there are the only 4 possible values for (X1, X2): (3, 6), (4, 5), (5, 4), (6, 3). Thus,
$$
E[X_1 | X=9] = \frac{3 + 4+ 5 + 6}{4} = \frac 9 2 = 4.5.
$$
- What is E[X1 − X2|X = k] for k in the range [2, 12]?
Note that X1 − X2 ∈ { − 5, 4, …, 5} and that P(X1 − X2 = i|X = k) = P(X1 − X2 = − i|X = k) for i ∈ { − 5, 4, …, 5}. Therefore, we have
$$
E[X_1 - X_2 | X=k] = \sum_{i=-5}^5 i \cdot P(X_1-X_2=i | X=k) = 0.
$$
= 10**7
num_samples
= np.random.randint(low=1, high=7, size=(num_samples, 2))
samples
# part (a)
print(f"sampled answer for (a): {samples[samples[:, 0] % 2 == 0, :].sum(axis=1).mean()}")
# part (b)
print(f"\nsampled answer for (b): {samples[samples[:, 0] == samples[:, 1], :].sum(axis=1).mean()}")
# part (c)
print(f"\nsampled answer for (b): {samples[samples.sum(axis=1) == 9][:, 0].mean()}")
# part (d)
print("\nsampled answer for (d)")
for k in range(2, 13):
= samples[samples.sum(axis=1) == k]
cond_samples print(f"k= {k}: {(cond_samples[:, 0] - cond_samples[:, 1]).mean()}")
sampled answer for (a): 7.499272007332312
sampled answer for (b): 7.00297383088835
sampled answer for (b): 4.501544227521886
sampled answer for (d)
k= 2: 0.0
k= 3: -0.000497820910275419
k= 4: -0.00014398934478848565
k= 5: 0.0037980995102071674
k= 6: -0.0030300434929995917
k= 7: -0.002319737212486128
k= 8: 0.003350165815196208
k= 9: 0.0030884550437718554
k= 10: -0.0009050957853026369
k= 11: 4.496265401957134e-05
k= 12: 0.0