プリンキピア

第22章付録:深層学習のための数学

22.8.1. Bernoulli22.8.1 ベルヌーイ分布

This is the simplest random variable usually encountered. This random variable encodes a coin flip which comes up 1 with probability p and 0 with probability 1−p. If we have a random variable X with this distribution, we will write
英語のヒント
これはふつう出会う最も単純な確率変数である。この確率変数は、確率pで1、確率1−pで0が出るコイン投げを表す。この分布に従う確率変数Xがあるとき、次のように書く。
X∼Bernoulli(p).(22.8.1)
The cumulative distribution function is
英語のヒント
累積分布関数は次のとおりである。
F(x)={0x<0,1−p0≤x<1,1x>=1.(22.8.2)
The probability mass function is plotted below.
英語のヒント
確率質量関数を以下にプロットする。
p = 0.3

d2l.set_figsize()
d2l.plt.stem([0, 1], [1 - p, p], 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.2).
英語のヒント
では、累積分布関数(式(22.8.2))をプロットしよう。
x = torch.arange(-1, 2, 0.01)

def F(x):
    return 0 if x < 0 else 1 if x > 1 else 1 - p

d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.')
If X∼Bernoulli(p), then:
英語のヒント
X∼Bernoulli(p)なら、次が成り立つ。
  • μX=p,
  • σX2=p(1−p).
We can sample an array of arbitrary shape from a Bernoulli random variable as follows.
英語のヒント
ベルヌーイ確率変数から、任意の形の配列を次のように抽出できる。
1*(torch.rand(10, 10) < p)
tensor([[0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
        [1, 1, 0, 0, 1, 1, 1, 1, 1, 0],
        [1, 0, 0, 0, 1, 0, 1, 1, 0, 0],
        [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
        [1, 0, 1, 1, 1, 1, 0, 1, 0, 0]])