# 随机多个色子的期望和方差

T 个均匀的六面体色子，把所有的点数加起来，记为 C
C 个均匀的八面体色子，把所有的点数加起来，记为 O
O 个均匀的十二面体色子，把所有的点数加起来，记为 D
D 个均匀的二十面体色子，把所有的点数加起来，记为 I
I 的方差。

\begin{aligned} \mathbb{E}_n[X] =& \frac{1}{n}\cdot \frac{n(n+1)}{2} = \frac{n+1}{2}\\ \mathbb{D}_n[X] =& \mathbb{E}_n[X^2] – \mathbb{E}_n[X]^2 \\ = & \frac{1}{n}\cdot \sum\limits_{k=1}^nk^2 – \left(\frac{n+1}{2}\right)^2\\ = & \frac{1}{n}\cdot \frac{1}{6}n(n+1)(2n+1) – \frac{(n+1)^2}{4} = \frac{n^2-1}{12} \end{aligned}

$\mathbb{E}_Y \big[ \mathbb{E}_{X|Y}[X|Y] \big]$ 这个东西实际上就是所谓的全期望公式

\begin{aligned}\mathbb{E} \big[ \mathbb{E}[X|Y] \big] =&\textstyle \sum\limits_y \mathbb{E}[X|Y=y] \cdot \mathbb{P}(Y=y) \\ =&\textstyle\sum\limits_y \big[ \sum\limits_x x \cdot \mathbb{P}(X=x|Y=y) \big] \cdot \mathbb{P}(Y=y) \\ =&\textstyle\sum\limits_y \sum\limits_x x \cdot \mathbb{P}(X=x|Y=y) \cdot \mathbb{P}(Y=y) \\ =&\textstyle\sum\limits_y \sum\limits_x x \cdot \mathbb{P}(X=x,Y=y) \\ =&\textstyle\sum\limits_x x\cdot\sum\limits_y\mathbb{P}(X=x,Y=y) \\ =&\textstyle\sum\limits_x x\cdot \mathbb{P}(X=x) = \textstyle\mathbb{E}[X].\end{aligned}

$\textstyle\mathbb{E} \bigg[ {\sum\limits_{k=1}^y x_k} \bigg] = \textstyle \mathbb{E}\bigg[\mathbb{E}\bigg[\sum\limits_{k=1}^y x_k \bigg| Y\bigg]\bigg]$

$= \mathbb{E}\big[Y\cdot \mathbb{E}[X]\big]= \mathbb{E}[X]\cdot\mathbb{E}[Y]$ 这样就可以轻松得到两次扔色子的点数期望
$\mathbb{E}\Big[{\textstyle\sum} c\Big]=\mathbb{E}[{T}]\cdot\mathbb{E}[C]=\frac{5}{2}\cdot\frac{7}{2}=\frac{35}{4}$ 接下来求方差，我们知道

\begin{aligned} \mathbb{D}[X] =&\textstyle \mathbb{E}[X^2] – \mathbb{E}[X]^2 \\ =&\textstyle \mathbb{E}\big[\mathbb{E}[X^2|Y]\big] – \mathbb{E}\big[\mathbb{E}[X|Y]\big]^2\\ =&\textstyle \mathbb{E}\big[\mathbb{D}[X|Y] + \mathbb{E}[X|Y]^2\big] – \mathbb{E}[\mathbb{E}[X|Y]]^2 \\ =&\textstyle \mathbb{E}\big[\mathbb{D}[X|Y]\big] + \big[\mathbb{E}\big[\mathbb{E}[X|Y]^2] – \mathbb{E}[\mathbb{E}[X|Y]\big]^2\big] \\ =&\textstyle \mathbb{E}\big[\mathbb{D}[X|Y]\big] + \mathbb{D}\big[\mathbb{E}[X|Y]\big] \end{aligned}.

\begin{aligned}\textstyle\mathbb{D} \bigg[ {\sum\limits_{k=1}^y x_k} \bigg] =&\textstyle \mathbb{E}\bigg[\mathbb{D}\bigg[\sum\limits_{k=1}^y x_k \bigg| Y\bigg]\bigg] + \mathbb{D}\bigg[\mathbb{E}\bigg[\sum\limits_{k=1}^y x_k \bigg| Y\bigg]\bigg]\\ =&\textstyle \mathbb{E}\big[Y\cdot \mathbb{D}[X]\big] + \mathbb{D}\big[Y\cdot \mathbb{E}[X]\big] \\ =&\textstyle \mathbb{D}[X]\cdot \mathbb{E}[Y] + \mathbb{E}[X]^2\cdot \mathbb{D}[Y] \end{aligned} 所以分别扔过四面体和六面体色子之后，$$E_4 = \frac {5}{2}, E_6 = \frac{7}{2}, D_4 = \frac{5}{4}, D_6 = \frac{35}{12}$$,
$\begin{split} \mathbb{D}\Big[{\textstyle\sum} c\Big]=&\mathbb{E}[{T}]\cdot\mathbb{D}[C] + \mathbb{E}[{C}]^2\cdot\mathbb{D}[T] \\ =&\frac{5}{2}\cdot\frac{35}{12}+ \left(\frac{7}{2}\right)^2\frac{5}{4}=\frac{1085}{48} \end{split}$ 再扔八面体色子时，$$E_8 = \frac {9}{2}, D_8 = \frac{64-1}{12} = \frac{21}{4}$$,
$\begin{split} \mathbb{D}\Big[{\textstyle\sum} o\Big] =&\mathbb{E}\Big[{\textstyle\sum} c\Big]\cdot\mathbb{D}[O] + \mathbb{E}[{O}]^2\cdot\mathbb{D}\Big[{\textstyle\sum} c\Big] \\ =&\frac{35}{4}\cdot\frac{21}{4}+ \left(\frac{9}{2}\right)^2\frac{1085}{48}=\frac{32235}{64} \end{split}$ 反正一直这样算下去就好啦，最后的答案我就不给出了，有兴趣的同学可以自己写个程序来完成这个过程。

[1] Bertsekas, Dimitri P., and John N. Tsitsiklis. Introduction to probability. Belmont, MA: Athena Scientific, 2002.
[2] 杨振明，概率论. 科学出版社, 2004.