第22章付録:深層学習のための数学
22.8.4. Binomial22.8.4 二項分布
Let’s make things a little more complex and examine the binomial random variable. This random variable originates from performing a sequence of independent experiments, each of which has probability of succeeding, and asking how many successes we expect to see.
英語のヒント
もう少し複雑にして、二項分布の確率変数を調べよう。この確率変数は、それぞれが成功する確率をもつ回の独立な実験の系列を行い、何回の成功が見られると期待するかを問うことから生まれる。
Let’s express this mathematically. Each experiment is an independent random variable where we will use to encode success, and to encode failure. Since each is an independent coin flip which is successful with probability , we can say that . Then, the binomial random variable is
英語のヒント
これを数学的に表そう。各実験は独立な確率変数であり、成功を、失敗をで表す。各実験は確率で成功する独立なコイン投げなので、と言える。すると、二項分布の確率変数は次のとおりである。
(22.8.8)
In this case, we will write
英語のヒント
この場合、次のように書く。
(22.8.9)
To get the cumulative distribution function, we need to notice that getting exactly successes can occur in ways each of which has a probability of of occurring. Thus the cumulative distribution function is
英語のヒント
累積分布関数を得るには、ちょうど回成功することは通りの仕方で起こりえて、そのそれぞれが確率で起きることに気づく必要がある。したがって、累積分布関数は次のとおりである。
(22.8.10)
Let’s first plot the probability mass function.
英語のヒント
まず、確率質量関数をプロットしよう。
n, p = 10, 0.2
# Compute binomial coefficient
def binom(n, k):
comb = 1
for i in range(min(k, n - k)):
comb = comb * (n - i) // (i + 1)
return comb
pmf = torch.tensor([p**i * (1-p)**(n - i) * binom(n, i) for i in range(n + 1)])
d2l.plt.stem([i for i in range(n + 1)], pmf, use_line_collection=True)
d2l.plt.xlabel('x')
d2l.plt.ylabel('p.m.f.')
d2l.plt.show()x = torch.arange(-1, 11, 0.01)
cmf = torch.cumsum(pmf, dim=0)
def F(x):
return 0 if x < 0 else 1 if x > n else cmf[int(x)]
d2l.plot(x, torch.tensor([F(y) for y in x.tolist()]), 'x', 'c.d.f.')If , then:
英語のヒント
なら、次が成り立つ。
This follows from the linearity of expected value over the sum of Bernoulli random variables, and the fact that the variance of the sum of independent random variables is the sum of the variances. This can be sampled as follows.
英語のヒント
これは、個のベルヌーイ確率変数の和についての期待値の線形性と、独立な確率変数の和の分散は分散の和であるという事実から従う。これは次のように抽出できる。
m = torch.distributions.binomial.Binomial(n, p)
m.sample(sample_shape=(10, 10))tensor([[6., 3., 4., 3., 3., 1., 3., 3., 3., 3.],
[3., 1., 2., 2., 3., 2., 1., 3., 1., 4.],
[6., 1., 0., 3., 0., 3., 1., 0., 1., 1.],
[1., 2., 3., 1., 2., 2., 2., 2., 3., 2.],
[2., 2., 5., 4., 1., 3., 4., 3., 2., 0.],
[2., 0., 2., 2., 3., 1., 1., 4., 3., 1.],
[1., 1., 3., 2., 4., 2., 2., 2., 1., 0.],
[0., 3., 2., 1., 1., 3., 2., 1., 1., 3.],
[2., 3., 2., 3., 4., 3., 1., 2., 1., 2.],
[1., 2., 1., 1., 3., 2., 4., 3., 3., 2.]])