2.6 Probability and Statistics

1
2
3
4
5
%matplotlib inline
import random
import torch
from torch.distributions.multinomial import Multinomial
from d2l import torch as d2l
1
2
3
4
5
num_toss = 100
heads = sum([random.random() > 0.5 for _ in range(100)])#random() between 0 and 1
tails = num_toss - heads
print("heads tails: ", [heads, tails])
#完成了一个抽样过程,利用random函数
heads tails:  [59, 41]
1
2
3
fair_probs = torch.tensor([0.5, 0.5])
Multinomial(100, fair_probs).sample()#We take 100 draws and set the probility with head0.5 and tail0.5,
#then we use Multinomial to sample and get the estimating results.
tensor([40., 60.])
1
Multinomial(100, fair_probs).sample() / 100
tensor([0.4700, 0.5300])
1
2
#we can increase the draws number.
Multinomial(10000, fair_probs).sample() / 100
tensor([49.9900, 50.0100])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#我们知道中心极限定理规定随着样本大小n增长,错误以1/sqrt(n)下降,我们从1-10000次投掷来观察这个现象。
counts = Multinomial(1, fair_probs).sample((10000, ))
print(counts)#输出10000次的结果!
cum_counts = counts.cumsum(dim=0)#在0维度上累加,可以看到,加到最后就是近乎两两相等的结果
print(cum_counts)
estimates = cum_counts / cum_counts.sum(dim=1, keepdims=True)#dim=1也就是横着加起来!也就是每一个行向量进行归一化!看概率分布!
estimates = estimates.numpy()
print(estimates)

d2l.set_figsize((4.5, 3.5))
d2l.plt.plot(estimates[:, 0], label=("P(coins = heads)"))
d2l.plt.plot(estimates[:, 1], label=("P(coins = tails)"))
#因为estimates是概率分布,第一列是heads的,第二列是tails的
d2l.plt.axhline(y = 0.5, color = "black", linestyle="dashed")
d2l.plt.gca().set_xlabel('Samples')#注意这里我们的set_xlabel是属于matplotlib里面的函数!不用定义!
#gca()表示获取当前的轴!
d2l.plt.gca().set_ylabel('Estimated probability')
tensor([[0., 1.],
        [1., 0.],
        [1., 0.],
        ...,
        [1., 0.],
        [1., 0.],
        [1., 0.]])
tensor([[0.0000e+00, 1.0000e+00],
        [1.0000e+00, 1.0000e+00],
        [2.0000e+00, 1.0000e+00],
        ...,
        [5.0200e+03, 4.9780e+03],
        [5.0210e+03, 4.9780e+03],
        [5.0220e+03, 4.9780e+03]])
[[0.         1.        ]
 [0.5        0.5       ]
 [0.6666667  0.33333334]
 ...
 [0.5021004  0.4978996 ]
 [0.50215024 0.4978498 ]
 [0.5022     0.4978    ]]

Text(0, 0.5, 'Estimated probability')

svg

2.6.2 A More Formal Treatment

1
#下面全是讲解probability内容,详见http://d2l.ai/chapter_preliminaries/probability.html