プリンキピア

第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 n independent experiments, each of which has probability p of succeeding, and asking how many successes we expect to see.
英語のヒント
もう少し複雑にして、二項分布の確率変数を調べよう。この確率変数は、それぞれが成功する確率pをもつn回の独立な実験の系列を行い、何回の成功が見られると期待するかを問うことから生まれる。
Let’s express this mathematically. Each experiment is an independent random variable Xi where we will use 1 to encode success, and 0 to encode failure. Since each is an independent coin flip which is successful with probability p, we can say that Xi∼Bernoulli(p). Then, the binomial random variable is
英語のヒント
これを数学的に表そう。各実験は独立な確率変数Xiであり、成功を1、失敗を0で表す。各実験は確率pで成功する独立なコイン投げなので、Xi∼Bernoulli(p)と言える。すると、二項分布の確率変数は次のとおりである。
X=∑i=1nXi.(22.8.8)
In this case, we will write
英語のヒント
この場合、次のように書く。
X∼Binomial(n,p).(22.8.9)
To get the cumulative distribution function, we need to notice that getting exactly k successes can occur in (nk)=n!k!(n−k)! ways each of which has a probability of pk(1−p)n−k of occurring. Thus the cumulative distribution function is
英語のヒント
累積分布関数を得るには、ちょうどk回成功することは(nk)=n!k!(n−k)!通りの仕方で起こりえて、そのそれぞれが確率pk(1−p)n−kで起きることに気づく必要がある。したがって、累積分布関数は次のとおりである。
F(x)={0x<0,∑m≤k(nm)pm(1−p)n−mk≤x<k+1 with 0≤k<n,1x>=n.(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()
Now, let’s plot the cumulative distribution function (22.8.10).
英語のヒント
では、累積分布関数(式(22.8.10))をプロットしよう。
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 X∼Binomial(n,p), then:
英語のヒント
X∼Binomial(n,p)なら、次が成り立つ。
  • μX=np,
  • σX2=np(1−p).
This follows from the linearity of expected value over the sum of n 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.
英語のヒント
これは、n個のベルヌーイ確率変数の和についての期待値の線形性と、独立な確率変数の和の分散は分散の和であるという事実から従う。これは次のように抽出できる。
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.]])