1.概率
import torch
from torch.distributions import multinomial
from d2l import torch as d2l
fair_probs = torch.ones([6])/6
fair_probs
<frozen importlib._bootstrap>:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216 from C header, got 232 from PyObject
tensor([0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667])
counts = multinomial.Multinomial(10,fair_probs).sample((500,))
counts,counts.shape
(tensor([[3., 0., 3., 0., 1., 3.],
[2., 4., 1., 0., 2., 1.],
[0., 2., 2., 0., 2., 4.],
...,
[2., 1., 3., 2., 0., 2.],
[3., 0., 1., 4., 2., 0.],
[0., 2., 3., 1., 2., 2.]]),
torch.Size([500, 6]))
cum_counts = counts.cumsum(dim=0)
estimates = cum_counts/cum_counts.sum(dim=1,keepdims=True)
estimates
tensor([[0.3000, 0.0000, 0.3000, 0.0000, 0.1000, 0.3000],
[0.2500, 0.2000, 0.2000, 0.0000, 0.1500, 0.2000],
[0.1667, 0.2000, 0.2000, 0.0000, 0.1667, 0.2667],
...,
[0.1697, 0.1661, 0.1635, 0.1657, 0.1679, 0.1673],
[0.1699, 0.1657, 0.1633, 0.1661, 0.1679, 0.1669],
[0.1696, 0.1658, 0.1636, 0.1660, 0.1680, 0.1670]])
d2l.set_figsize((6,4.5))
for i in range(6):
d2l.plt.plot(estimates[:,i].numpy(),label=("P(die=)"+str(i+1)+")"))
d2l.plt.axhline(y=0.167,color="black",linestyle="dashed")
d2l.plt.gca().set_xlabel("Groups of experiments")
d2l.plt.gca().set_ylabel("Extimated probability")
d2l.plt.legend()
<matplotlib.legend.Legend at 0x243fd4b63d0>
![在这里插入图片描述](https://img-blog.csdnimg.cn/ab0626a2d16745709f398de8b8399eb1.jpeg#pic_center)