.. _sec_distributions: 分布 ==== これまで、離散的な場合と連続的な場合の両方で確率を扱う方法を学んできた。ここでは、よく出会う代表的な分布について見ていきよう。機械学習の分野によっては、これらよりはるかに多くの分布に精通している必要があるかもしれないし、深層学習のある分野ではまったく不要なこともある。しかし、基本的な一覧として知っておくのは有益である。まず、いくつかの共通ライブラリをインポートしよう。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline from d2l import torch as d2l from IPython import display from math import erf, factorial import torch torch.pi = torch.acos(torch.zeros(1)) * 2 # Define pi in torch .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline from d2l import mxnet as d2l from IPython import display from math import erf, factorial import numpy as np .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python %matplotlib inline from d2l import tensorflow as d2l from IPython import display from math import erf, factorial import tensorflow as tf import tensorflow_probability as tfp tf.pi = tf.acos(tf.zeros(1)) * 2 # Define pi in TensorFlow .. raw:: html
.. raw:: html
ベルヌーイ分布 -------------- これは通常最初に出会う最も単純な確率変数である。この確率変数は、コイン投げを表し、\ :math:`1` が確率 :math:`p`\ 、\ :math:`0` が確率 :math:`1-p` で出ることを表する。この分布に従う確率変数 :math:`X` は .. math:: X \sim \textrm{Bernoulli}(p). と書く。 累積分布関数は .. math:: F(x) = \begin{cases} 0 & x < 0, \\ 1-p & 0 \le x < 1, \\ 1 & x >= 1 . \end{cases} :label: eq_bernoulli_cdf である。 確率質量関数を以下に描く。 .. raw:: html
pytorchmxnetjaxtensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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() .. figure:: output_distributions_1214ac_18_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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() .. figure:: output_distributions_1214ac_21_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python n = 5 d2l.plt.stem([i+1 for i in range(n)], n*[1 / n], use_line_collection=True) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.show() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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() .. figure:: output_distributions_1214ac_27_0.svg .. raw:: html
.. raw:: html
では、累積分布関数 :eq:`eq_bernoulli_cdf` を描いてみよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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.') .. figure:: output_distributions_1214ac_33_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = np.arange(-1, 2, 0.01) def F(x): return 0 if x < 0 else 1 if x > 1 else 1 - p d2l.plot(x, np.array([F(y) for y in x]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_36_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = tf.range(-1, 2, 0.01) def F(x): return 0 if x < 0 else 1 if x > 1 else 1 - p d2l.plot(x, tf.constant([F(y) for y in x]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_39_0.svg .. raw:: html
.. raw:: html
:math:`X \sim \textrm{Bernoulli}(p)` ならば、 - :math:`\mu_X = p`\ 、 - :math:`\sigma_X^2 = p(1-p)`\ 。 ベルヌーイ確率変数から任意の形状の配列をサンプルするには、次のようにする。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 1*(torch.rand(10, 10) < p) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[0, 0, 0, 1, 0, 0, 1, 0, 0, 1], [0, 1, 1, 1, 0, 0, 1, 1, 1, 0], [0, 0, 1, 0, 0, 0, 0, 1, 0, 1], [0, 0, 0, 1, 0, 1, 0, 0, 0, 1], [0, 1, 1, 0, 1, 1, 1, 1, 0, 1], [1, 0, 0, 0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 1, 0, 0, 0, 1], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 1*(np.random.rand(10, 10) < p) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[0, 0, 1, 0, 0, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 0, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 1, 1, 0, 1, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0, 0]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.cast(tf.random.uniform((10, 10)) < p, dtype=tf.float32) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
離散一様分布 ------------ 次によく現れる確率変数は離散一様分布である。ここでは、\ :math:`\{1, 2, \ldots, n\}` の整数上に台を持つものを考えるが、他の任意の値集合を自由に選んでも構いない。この文脈で *一様* というのは、取りうるすべての値が等確率であることを意味する。各値 :math:`i \in \{1, 2, 3, \ldots, n\}` の確率は :math:`p_i = \frac{1}{n}` である。この分布に従う確率変数 :math:`X` は .. math:: X \sim U(n). と表する。 累積分布関数は .. math:: F(x) = \begin{cases} 0 & x < 1, \\ \frac{k}{n} & k \le x < k+1 \textrm{ with } 1 \le k < n, \\ 1 & x >= n . \end{cases} :label: eq_discrete_uniform_cdf である。 まず確率質量関数を描きよう。 .. raw:: latex \diilbookstyleinputcell .. code:: python n = 5 d2l.plt.stem([i+1 for i in range(n)], n*[1 / n], use_line_collection=True) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.show() .. figure:: output_distributions_1214ac_55_0.svg では、累積分布関数 :eq:`eq_discrete_uniform_cdf` を描いてみよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = torch.arange(-1, 6, 0.01) def F(x): return 0 if x < 1 else 1 if x > n else torch.floor(x) / n d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_59_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = np.arange(-1, 6, 0.01) def F(x): return 0 if x < 1 else 1 if x > n else np.floor(x) / n d2l.plot(x, np.array([F(y) for y in x]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_62_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = tf.range(-1, 6, 0.01) def F(x): return 0 if x < 1 else 1 if x > n else tf.floor(x) / n d2l.plot(x, [F(y) for y in x], 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_65_0.svg .. raw:: html
.. raw:: html
:math:`X \sim U(n)` ならば、 - :math:`\mu_X = \frac{1+n}{2}`\ 、 - :math:`\sigma_X^2 = \frac{n^2-1}{12}`\ 。 離散一様確率変数から任意の形状の配列をサンプルするには、次のようにする。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.randint(1, n, size=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[3, 3, 4, 2, 1, 4, 3, 3, 3, 1], [2, 3, 4, 4, 1, 1, 4, 3, 4, 2], [1, 2, 1, 2, 2, 3, 2, 4, 3, 3], [3, 4, 1, 4, 2, 3, 1, 2, 3, 3], [1, 2, 4, 1, 2, 4, 1, 3, 2, 1], [3, 2, 2, 1, 2, 4, 2, 4, 2, 1], [4, 2, 2, 1, 3, 4, 3, 3, 1, 4], [4, 4, 4, 1, 1, 3, 3, 2, 3, 4], [4, 3, 3, 3, 4, 4, 4, 2, 2, 4], [2, 2, 2, 2, 1, 2, 2, 1, 4, 2]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.random.randint(1, n, size=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[2, 2, 2, 3, 4, 1, 4, 2, 1, 2], [1, 1, 3, 3, 4, 1, 2, 4, 3, 1], [4, 2, 1, 3, 2, 2, 1, 4, 1, 2], [3, 4, 1, 1, 2, 4, 2, 4, 4, 1], [1, 2, 2, 1, 4, 1, 1, 1, 4, 4], [4, 4, 2, 3, 1, 2, 2, 2, 2, 1], [2, 3, 1, 4, 2, 3, 2, 4, 4, 3], [4, 1, 3, 1, 3, 2, 2, 4, 2, 1], [4, 3, 2, 4, 4, 3, 1, 1, 1, 2], [2, 2, 1, 4, 1, 2, 4, 1, 1, 1]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.random.uniform((10, 10), 1, n, dtype=tf.int32) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
連続一様分布 ------------ 次に、連続一様分布について説明しよう。この確率変数の考え方は、離散一様分布の :math:`n` を増やし、それを区間 :math:`[a, b]` に収まるようにスケーリングすると、\ :math:`[a, b]` の中の任意の値をすべて等確率で選ぶ連続確率変数に近づく、というものである。この分布は .. math:: X \sim U(a, b). と表する。 確率密度関数は .. math:: p(x) = \begin{cases} \frac{1}{b-a} & x \in [a, b], \\ 0 & x \not\in [a, b].\end{cases} :label: eq_cont_uniform_pdf である。 累積分布関数は .. math:: F(x) = \begin{cases} 0 & x < a, \\ \frac{x-a}{b-a} & x \in [a, b], \\ 1 & x >= b . \end{cases} :label: eq_cont_uniform_cdf である。 まず確率密度関数 :eq:`eq_cont_uniform_pdf` を描きよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a, b = 1, 3 x = torch.arange(0, 4, 0.01) p = (x > a).type(torch.float32)*(x < b).type(torch.float32)/(b-a) d2l.plot(x, p, 'x', 'p.d.f.') .. figure:: output_distributions_1214ac_83_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a, b = 1, 3 x = np.arange(0, 4, 0.01) p = (x > a)*(x < b)/(b - a) d2l.plot(x, p, 'x', 'p.d.f.') .. figure:: output_distributions_1214ac_86_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a, b = 1, 3 x = tf.range(0, 4, 0.01) p = tf.cast(x > a, tf.float32) * tf.cast(x < b, tf.float32) / (b - a) d2l.plot(x, p, 'x', 'p.d.f.') .. figure:: output_distributions_1214ac_89_0.svg .. raw:: html
.. raw:: html
では、累積分布関数 :eq:`eq_cont_uniform_cdf` を描いてみよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def F(x): return 0 if x < a else 1 if x > b else (x - a) / (b - a) d2l.plot(x, torch.tensor([F(y) for y in x]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_95_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def F(x): return 0 if x < a else 1 if x > b else (x - a) / (b - a) d2l.plot(x, np.array([F(y) for y in x]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_98_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def F(x): return 0 if x < a else 1 if x > b else (x - a) / (b - a) d2l.plot(x, [F(y) for y in x], 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_101_0.svg .. raw:: html
.. raw:: html
:math:`X \sim U(a, b)` ならば、 - :math:`\mu_X = \frac{a+b}{2}`\ 、 - :math:`\sigma_X^2 = \frac{(b-a)^2}{12}`\ 。 一様確率変数から任意の形状の配列をサンプルするには、次のようにする。デフォルトでは :math:`U(0,1)` からサンプルされるので、別の範囲が欲しい場合はスケーリングする必要がある。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python (b - a) * torch.rand(10, 10) + a .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[2.0679, 2.4100, 2.5762, 1.8499, 1.0621, 1.9996, 2.3631, 2.6283, 1.6981, 1.3658], [2.2581, 2.8063, 2.5712, 2.9981, 1.2781, 1.3680, 2.5652, 2.8932, 1.1143, 1.6694], [2.9813, 2.2677, 2.7297, 1.4408, 2.9802, 2.4943, 2.5363, 1.6275, 1.4729, 2.5822], [2.9420, 2.5521, 2.1417, 1.2070, 1.2284, 2.7954, 2.9322, 1.7272, 2.8715, 1.5982], [2.3660, 1.6054, 2.5989, 2.2134, 1.2444, 2.1781, 1.5832, 2.1513, 1.8993, 2.8935], [1.7960, 2.7038, 1.1465, 1.3827, 1.9866, 2.1409, 2.0720, 1.9597, 2.5218, 2.1478], [1.8626, 2.3913, 2.0165, 2.2057, 1.9050, 2.0541, 1.1899, 2.6062, 2.8070, 1.3668], [2.4310, 1.1270, 1.9386, 2.7942, 1.9744, 2.6841, 2.5107, 1.2452, 2.3955, 1.8620], [2.0176, 2.9634, 1.2320, 2.4971, 1.4820, 2.8725, 2.6219, 2.1171, 2.1623, 1.8533], [2.1182, 1.2056, 1.6436, 1.7542, 1.7792, 2.2062, 2.9191, 1.1343, 2.0459, 1.9995]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python (b - a) * np.random.rand(10, 10) + a .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[1.10493389, 2.18332184, 2.55880868, 2.40644506, 1.98823581, 2.03304566, 2.54314285, 2.28893216, 1.96832242, 1.21947864], [1.00116734, 1.87327363, 2.06307085, 1.37704206, 2.98520983, 2.85361327, 2.614708 , 1.67971392, 2.31319471, 2.08720314], [2.9417875 , 2.58942767, 1.36652719, 2.76520312, 1.6291505 , 2.57007544, 2.18738455, 1.08755762, 1.69884709, 1.9310117 ], [1.82504201, 1.80920424, 1.52510532, 2.17378476, 1.211283 , 2.15483981, 2.14476987, 1.30869007, 2.7514197 , 2.93051749], [1.46794909, 1.1367775 , 1.35302173, 2.6377266 , 2.43745649, 2.98864716, 1.26447563, 1.48047024, 2.08178225, 1.32105626], [1.25845706, 2.03397717, 2.73034409, 2.70469505, 2.34734372, 1.74027474, 2.96683054, 1.37055974, 1.53226889, 2.36267969], [2.94902185, 1.77360701, 1.30736801, 2.21610289, 2.60674295, 1.97389815, 1.96275176, 2.9917009 , 2.66415546, 2.43568684], [1.41549567, 2.78340407, 1.49017239, 1.92110928, 2.44749996, 1.94183467, 2.55685336, 2.06993835, 1.62822726, 1.99831404], [2.4445657 , 2.44027101, 2.11419221, 1.94853489, 1.81220577, 1.74003385, 1.05746194, 1.94546557, 2.53363203, 1.74536491], [2.2648276 , 2.99783037, 2.15039751, 1.75704603, 1.46434226, 2.05093236, 1.80039954, 1.37730793, 2.03797871, 1.62589064]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python (b - a) * tf.random.uniform((10, 10)) + a .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
二項分布 -------- 少し複雑にして、\ *二項* 確率変数を見てみよう。この確率変数は、成功確率 :math:`p` の独立な試行を :math:`n` 回行い、成功が何回起こるかを数えることから生まれる。 これを数学的に表しよう。各試行は独立な確率変数 :math:`X_i` であり、成功を :math:`1`\ 、失敗を :math:`0` で表する。それぞれが成功確率 :math:`p` の独立なコイン投げなので、\ :math:`X_i \sim \textrm{Bernoulli}(p)` と書ける。すると、二項確率変数は .. math:: X = \sum_{i=1}^n X_i. である。 このとき、 .. math:: X \sim \textrm{Binomial}(n, p). と書く。 累積分布関数を得るには、ちょうど :math:`k` 回成功する場合が :math:`\binom{n}{k} = \frac{n!}{k!(n-k)!}` 通りあり、それぞれが確率 :math:`p^k(1-p)^{n-k}` で起こることに注意する必要がある。したがって、累積分布関数は .. math:: F(x) = \begin{cases} 0 & x < 0, \\ \sum_{m \le k} \binom{n}{m} p^m(1-p)^{n-m} & k \le x < k+1 \textrm{ with } 0 \le k < n, \\ 1 & x >= n . \end{cases} :label: eq_binomial_cdf である。 まず確率質量関数を描きよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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 = d2l.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() .. figure:: output_distributions_1214ac_119_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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 = np.array([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() .. figure:: output_distributions_1214ac_122_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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 = tf.constant([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() .. figure:: output_distributions_1214ac_125_0.svg .. raw:: html
.. raw:: html
では、累積分布関数 :eq:`eq_binomial_cdf` を描いてみよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python 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.') .. figure:: output_distributions_1214ac_131_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = np.arange(-1, 11, 0.01) cmf = np.cumsum(pmf) def F(x): return 0 if x < 0 else 1 if x > n else cmf[int(x)] d2l.plot(x, np.array([F(y) for y in x.tolist()]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_134_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = tf.range(-1, 11, 0.01) cmf = tf.cumsum(pmf) def F(x): return 0 if x < 0 else 1 if x > n else cmf[int(x)] d2l.plot(x, [F(y) for y in x.numpy().tolist()], 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_137_0.svg .. raw:: html
.. raw:: html
:math:`X \sim \textrm{Binomial}(n, p)` ならば、 - :math:`\mu_X = np`\ 、 - :math:`\sigma_X^2 = np(1-p)`\ 。 これは、\ :math:`n` 個のベルヌーイ確率変数の和に対する期待値の線形性と、独立な確率変数の和の分散が分散の和になることから従う。これは次のようにサンプルできる。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python m = torch.distributions.binomial.Binomial(n, p) m.sample(sample_shape=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[2., 2., 2., 1., 3., 1., 2., 2., 1., 2.], [3., 3., 2., 1., 2., 4., 1., 3., 1., 2.], [1., 2., 4., 1., 4., 4., 1., 0., 0., 1.], [1., 1., 0., 4., 2., 2., 2., 1., 2., 2.], [2., 3., 2., 1., 0., 2., 4., 3., 1., 2.], [1., 1., 5., 2., 3., 2., 3., 1., 3., 2.], [1., 6., 2., 1., 0., 3., 4., 4., 2., 4.], [2., 3., 2., 1., 2., 2., 3., 2., 2., 3.], [2., 1., 5., 0., 1., 3., 1., 2., 2., 5.], [0., 2., 5., 0., 0., 2., 3., 3., 0., 2.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.random.binomial(n, p, size=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[5, 3, 2, 1, 3, 1, 1, 1, 3, 1], [2, 2, 2, 0, 1, 1, 1, 3, 0, 3], [1, 2, 3, 2, 1, 1, 2, 2, 2, 1], [1, 1, 1, 2, 2, 2, 0, 3, 4, 1], [2, 1, 2, 3, 1, 2, 1, 5, 2, 4], [4, 3, 2, 4, 0, 1, 2, 4, 3, 2], [1, 3, 1, 4, 1, 0, 2, 2, 3, 0], [2, 3, 1, 1, 3, 4, 3, 2, 1, 3], [5, 1, 4, 2, 2, 2, 0, 0, 2, 4], [3, 1, 3, 2, 0, 6, 2, 0, 2, 1]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python m = tfp.distributions.Binomial(n, p) m.sample(sample_shape=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output WARNING:tensorflow:From /home/ci/.local/lib/python3.10/site-packages/tensorflow_probability/python/internal/batched_rejection_sampler.py:102: calling while_loop_v2 (from tensorflow.python.ops.control_flow_ops) with back_prop=False is deprecated and will be removed in a future version. Instructions for updating: back_prop=False is deprecated. Consider using tf.stop_gradient instead. Instead of: results = tf.while_loop(c, b, vars, back_prop=False) Use: results = tf.nest.map_structure(tf.stop_gradient, tf.while_loop(c, b, vars)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
ポアソン分布 ------------ では、思考実験をしてみよう。私たちはバス停に立っていて、次の1分間に何台のバスが到着するかを知りたいとする。まず、\ :math:`X^{(1)} \sim \textrm{Bernoulli}(p)` を考える。これは、1分間の窓の中でバスが到着する確率そのものである。都市中心部から離れたバス停では、これはかなり良い近似かもしれない。1分間に2台以上のバスを見ることはまずないだろう。 しかし、混雑した地域にいるなら、2台のバスが到着することもありえますし、むしろ起こりやすいかもしれない。これを、最初の30秒と次の30秒に分けて確率変数を分割することでモデル化できる。この場合、 .. math:: X^{(2)} \sim X^{(2)}_1 + X^{(2)}_2, と書ける。ここで :math:`X^{(2)}` は合計であり、\ :math:`X^{(2)}_i \sim \textrm{Bernoulli}(p/2)` である。すると全体の分布は :math:`X^{(2)} \sim \textrm{Binomial}(2, p/2)` になる。 ここで止める必要はあるだろうか。さらにその1分を :math:`n` 個に分割してみよう。上と同じ推論により、 .. math:: X^{(n)} \sim \textrm{Binomial}(n, p/n). :label: eq_eq_poisson_approx が得られる。 これらの確率変数を考える。前節より、:eq:`eq_eq_poisson_approx` の平均は :math:`\mu_{X^{(n)}} = n(p/n) = p`\ 、分散は :math:`\sigma_{X^{(n)}}^2 = n(p/n)(1-(p/n)) = p(1-p/n)` である。\ :math:`n \rightarrow \infty` とすると、これらの値は :math:`\mu_{X^{(\infty)}} = p`\ 、分散 :math:`\sigma_{X^{(\infty)}}^2 = p` に安定することがわかる。これは、この無限分割の極限で定義できる確率変数が *存在するかもしれない* ことを示している。 現実世界ではバスの到着回数を数えればよいだけなので、これはそれほど驚くことではないが、数学的モデルがきちんと定義されていることを見るのは良いことである。この議論は *稀な事象の法則* として形式化できる。 この推論を丁寧にたどると、次のモデルに到達できる。\ :math:`X \sim \textrm{Poisson}(\lambda)` とは、\ :math:`\{0,1,2, \ldots\}` の値を確率 .. math:: p_k = \frac{\lambda^ke^{-\lambda}}{k!}. :label: eq_poisson_mass でとる確率変数のことである。 :math:`\lambda > 0` は *率*\ (あるいは *形状* パラメータ)と呼ばれ、1単位時間あたりに期待される到着回数を表する。 この確率質量関数を和をとって累積分布関数を得ることができる。 .. math:: F(x) = \begin{cases} 0 & x < 0, \\ e^{-\lambda}\sum_{m = 0}^k \frac{\lambda^m}{m!} & k \le x < k+1 \textrm{ with } 0 \le k. \end{cases} :label: eq_poisson_cdf まず確率質量関数 :eq:`eq_poisson_mass` を描きよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python lam = 5.0 xs = [i for i in range(20)] pmf = torch.tensor([torch.exp(torch.tensor(-lam)) * lam**k / factorial(k) for k in xs]) d2l.plt.stem(xs, pmf, use_line_collection=True) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.show() .. figure:: output_distributions_1214ac_155_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python lam = 5.0 xs = [i for i in range(20)] pmf = np.array([np.exp(-lam) * lam**k / factorial(k) for k in xs]) d2l.plt.stem(xs, pmf, use_line_collection=True) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.show() .. figure:: output_distributions_1214ac_158_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python lam = 5.0 xs = [i for i in range(20)] pmf = tf.constant([tf.exp(tf.constant(-lam)).numpy() * lam**k / factorial(k) for k in xs]) d2l.plt.stem(xs, pmf, use_line_collection=True) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.show() .. figure:: output_distributions_1214ac_161_0.svg .. raw:: html
.. raw:: html
では、累積分布関数 :eq:`eq_poisson_cdf` を描いてみよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = torch.arange(-1, 21, 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.') .. figure:: output_distributions_1214ac_167_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = np.arange(-1, 21, 0.01) cmf = np.cumsum(pmf) def F(x): return 0 if x < 0 else 1 if x > n else cmf[int(x)] d2l.plot(x, np.array([F(y) for y in x.tolist()]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_170_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python x = tf.range(-1, 21, 0.01) cmf = tf.cumsum(pmf) def F(x): return 0 if x < 0 else 1 if x > n else cmf[int(x)] d2l.plot(x, [F(y) for y in x.numpy().tolist()], 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_173_0.svg .. raw:: html
.. raw:: html
上で見たように、平均と分散は特に簡潔である。\ :math:`X \sim \textrm{Poisson}(\lambda)` ならば、 - :math:`\mu_X = \lambda`\ 、 - :math:`\sigma_X^2 = \lambda`\ 。 これは次のようにサンプルできる。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python m = torch.distributions.poisson.Poisson(lam) m.sample((10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[ 1., 7., 6., 3., 1., 4., 3., 5., 6., 5.], [ 3., 5., 4., 5., 8., 8., 5., 2., 6., 8.], [ 6., 7., 7., 6., 8., 5., 6., 6., 4., 4.], [ 4., 6., 5., 3., 3., 4., 8., 8., 5., 5.], [ 3., 7., 7., 6., 6., 3., 8., 8., 7., 3.], [ 7., 4., 4., 8., 5., 3., 4., 5., 5., 3.], [ 4., 7., 3., 2., 5., 9., 4., 6., 4., 8.], [ 5., 5., 4., 4., 4., 4., 3., 7., 2., 6.], [ 4., 8., 10., 4., 6., 7., 5., 10., 5., 5.], [ 2., 5., 4., 5., 7., 4., 2., 4., 5., 8.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.random.poisson(lam, size=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[ 5, 5, 6, 4, 2, 4, 8, 3, 4, 7], [ 0, 3, 11, 5, 3, 2, 7, 8, 3, 1], [ 6, 5, 4, 9, 7, 2, 1, 6, 6, 5], [ 6, 5, 5, 3, 6, 4, 5, 3, 6, 4], [ 6, 6, 5, 5, 5, 5, 5, 9, 7, 7], [ 5, 4, 4, 6, 2, 5, 4, 7, 7, 2], [ 1, 4, 7, 2, 4, 8, 1, 4, 6, 4], [ 3, 4, 6, 8, 8, 4, 3, 4, 5, 3], [ 4, 6, 6, 10, 6, 4, 6, 3, 8, 2], [ 2, 9, 8, 3, 7, 5, 6, 4, 5, 3]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python m = tfp.distributions.Poisson(lam) m.sample((10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
ガウス分布 ---------- では、別の、しかし関連した実験をしてみよう。ここでも、\ :math:`n` 個の独立な :math:`\textrm{Bernoulli}(p)` 測定 :math:`X_i` を行うとする。これらの和の分布は :math:`X^{(n)} \sim \textrm{Binomial}(n, p)` である。\ :math:`n` を増やし :math:`p` を減らす極限をとる代わりに、\ :math:`p` を固定して :math:`n \rightarrow \infty` としてみよう。この場合、\ :math:`\mu_{X^{(n)}} = np \rightarrow \infty` かつ :math:`\sigma_{X^{(n)}}^2 = np(1-p) \rightarrow \infty` となるので、この極限がうまく定義されるとは考えにくいである。 しかし、希望はまだある。平均と分散がうまく振る舞うように、次を定義しよう。 .. math:: Y^{(n)} = \frac{X^{(n)} - \mu_{X^{(n)}}}{\sigma_{X^{(n)}}}. これは平均0、分散1を持つことがわかるので、何らかの極限分布に収束すると考えるのはもっともらしいである。これらの分布がどのように見えるかを描いてみると、さらにそれがうまくいきそうだと確信できるだろう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python p = 0.2 ns = [1, 10, 100, 1000] d2l.plt.figure(figsize=(10, 3)) for i in range(4): n = ns[i] pmf = torch.tensor([p**i * (1-p)**(n-i) * binom(n, i) for i in range(n + 1)]) d2l.plt.subplot(1, 4, i + 1) d2l.plt.stem([(i - n*p)/torch.sqrt(torch.tensor(n*p*(1 - p))) for i in range(n + 1)], pmf, use_line_collection=True) d2l.plt.xlim([-4, 4]) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.title("n = {}".format(n)) d2l.plt.show() .. figure:: output_distributions_1214ac_191_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python p = 0.2 ns = [1, 10, 100, 1000] d2l.plt.figure(figsize=(10, 3)) for i in range(4): n = ns[i] pmf = np.array([p**i * (1-p)**(n-i) * binom(n, i) for i in range(n + 1)]) d2l.plt.subplot(1, 4, i + 1) d2l.plt.stem([(i - n*p)/np.sqrt(n*p*(1 - p)) for i in range(n + 1)], pmf, use_line_collection=True) d2l.plt.xlim([-4, 4]) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.title("n = {}".format(n)) d2l.plt.show() .. figure:: output_distributions_1214ac_194_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python p = 0.2 ns = [1, 10, 100, 1000] d2l.plt.figure(figsize=(10, 3)) for i in range(4): n = ns[i] pmf = tf.constant([p**i * (1-p)**(n-i) * binom(n, i) for i in range(n + 1)]) d2l.plt.subplot(1, 4, i + 1) d2l.plt.stem([(i - n*p)/tf.sqrt(tf.constant(n*p*(1 - p))) for i in range(n + 1)], pmf, use_line_collection=True) d2l.plt.xlim([-4, 4]) d2l.plt.xlabel('x') d2l.plt.ylabel('p.m.f.') d2l.plt.title("n = {}".format(n)) d2l.plt.show() .. figure:: output_distributions_1214ac_197_0.svg .. raw:: html
.. raw:: html
注意すべき点が1つある。ポアソンの場合と比べると、ここでは標準偏差で割っているため、取りうる結果をより狭い領域に押し込めている。これは、この極限がもはや離散的ではなく、連続的になることを示している。 何が起こるかの導出はこの文書の範囲を超えるが、\ *中心極限定理* によれば、\ :math:`n \rightarrow \infty` のとき、これはガウス分布(あるいは正規分布)を与える。より明示的には、任意の :math:`a, b` に対して .. math:: \lim_{n \rightarrow \infty} P(Y^{(n)} \in [a, b]) = P(\mathcal{N}(0,1) \in [a, b]), が成り立ちる。ここで、確率変数が平均 :math:`\mu`\ 、分散 :math:`\sigma^2` を持つ正規分布に従うとき、\ :math:`X \sim \mathcal{N}(\mu, \sigma^2)` と書き、その密度は .. math:: p_X(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}. :label: eq_gaussian_pdf である。 まず確率密度関数 :eq:`eq_gaussian_pdf` を描きよう。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mu, sigma = 0, 1 x = torch.arange(-3, 3, 0.01) p = 1 / torch.sqrt(2 * torch.pi * sigma**2) * torch.exp( -(x - mu)**2 / (2 * sigma**2)) d2l.plot(x, p, 'x', 'p.d.f.') .. figure:: output_distributions_1214ac_203_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mu, sigma = 0, 1 x = np.arange(-3, 3, 0.01) p = 1 / np.sqrt(2 * np.pi * sigma**2) * np.exp(-(x - mu)**2 / (2 * sigma**2)) d2l.plot(x, p, 'x', 'p.d.f.') .. figure:: output_distributions_1214ac_206_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python mu, sigma = 0, 1 x = tf.range(-3, 3, 0.01) p = 1 / tf.sqrt(2 * tf.pi * sigma**2) * tf.exp( -(x - mu)**2 / (2 * sigma**2)) d2l.plot(x, p, 'x', 'p.d.f.') .. figure:: output_distributions_1214ac_209_0.svg .. raw:: html
.. raw:: html
では、累積分布関数を描いてみよう。ここでは詳しく述べないが、ガウス分布の c.d.f. は、より初等的な関数を用いた閉じた形の式を持たない。そこで、\ ``erf`` を使ってこの積分を数値的に計算する。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def phi(x): return (1.0 + erf((x - mu) / (sigma * torch.sqrt(d2l.tensor(2.))))) / 2.0 d2l.plot(x, torch.tensor([phi(y) for y in x.tolist()]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_215_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def phi(x): return (1.0 + erf((x - mu) / (sigma * np.sqrt(2)))) / 2.0 d2l.plot(x, np.array([phi(y) for y in x.tolist()]), 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_218_0.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python def phi(x): return (1.0 + erf((x - mu) / (sigma * tf.sqrt(tf.constant(2.))))) / 2.0 d2l.plot(x, [phi(y) for y in x.numpy().tolist()], 'x', 'c.d.f.') .. figure:: output_distributions_1214ac_221_0.svg .. raw:: html
.. raw:: html
注意深い読者なら、これらの項のいくつかに見覚えがあるだろう。実際、この積分は :numref:`sec_integral_calculus` で扱いた。まさにその計算によって、この :math:`p_X(x)` の全体の面積が1であり、したがって有効な密度であることがわかる。 コイン投げを用いたのは計算を簡単にするためでしたが、その選択自体に本質的な意味はない。実際、独立同分布な確率変数 :math:`X_i` の任意の集まりを取り、 .. math:: X^{(N)} = \sum_{i=1}^N X_i. としたとき、 .. math:: \frac{X^{(N)} - \mu_{X^{(N)}}}{\sigma_{X^{(N)}}} はおおよそガウス分布に従う。うまく成り立つためには追加の条件が必要で、最も一般的には :math:`E[X^4] < \infty` であるが、考え方は明確である。 中心極限定理は、ガウス分布が確率、統計、機械学習において基本的である理由である。何か測定したものが多くの小さな独立な寄与の和だと言えるなら、その測定対象はガウス分布に近いと仮定できる。 ガウス分布にはさらに多くの興味深い性質があるが、ここではもう1つだけ述べよう。ガウス分布は *最大エントロピー分布* として知られている。エントロピーについては :numref:`sec_information_theory` でより深く扱うが、ここで知っておくべきことは、それがランダムさの尺度であるということだけである。厳密な数学的意味では、ガウス分布は平均と分散が固定された確率変数の中で *最も* ランダムな選択だと考えられる。したがって、確率変数の平均と分散がわかっているなら、ガウス分布はある意味で最も保守的な分布の選択である。 節を閉じる前に、\ :math:`X \sim \mathcal{N}(\mu, \sigma^2)` ならば、 - :math:`\mu_X = \mu`\ 、 - :math:`\sigma_X^2 = \sigma^2`\ 。 であることを思い出そう。 ガウス分布(あるいは標準正規分布)からのサンプルは以下のように得られる。 .. raw:: html
pytorchmxnettensorflow
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python torch.normal(mu, sigma, size=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[ 5.6402e-01, -2.9638e+00, -4.0225e-01, -1.0502e+00, -8.5967e-01, -1.8663e+00, 1.2591e+00, -4.8977e-01, -1.8643e-01, -8.4658e-01], [ 9.8634e-01, 2.5091e-01, 5.5886e-01, -4.9260e-01, -1.7324e-01, -4.1209e-01, -5.2245e-01, 5.0280e-01, -6.3664e-01, -9.0722e-01], [-1.1246e+00, -1.2719e+00, -1.0566e+00, 1.5562e+00, -3.7938e-01, 8.7909e-02, 1.5184e+00, -6.9584e-01, -1.4542e+00, -2.9577e+00], [-1.3921e-02, 1.1291e+00, 2.1045e-01, 1.5291e+00, 2.8422e-02, 1.3347e+00, 2.0202e+00, 2.0668e-01, 8.9387e-01, 2.1965e-03], [ 1.3032e+00, 8.9880e-01, -3.0384e-01, 7.6841e-01, 4.2073e-02, -3.1455e-01, -1.5442e+00, -1.0123e-01, -2.5143e-01, -1.8960e+00], [-2.0620e+00, 7.7326e-01, -9.0571e-02, 6.0874e-01, 3.1394e-01, -3.9894e-01, -3.0525e-01, -9.5778e-01, 1.6019e-01, -5.7222e-02], [-5.1813e-01, -1.4124e-01, 4.8078e-01, 1.9416e+00, 8.5896e-01, 5.8826e-01, 6.5373e-01, 5.8058e-01, -1.4395e+00, 4.8357e-01], [-1.5158e+00, -2.5533e-01, -6.5353e-01, 1.0617e+00, 4.6570e-01, 1.8963e-01, 1.6243e+00, 5.5373e-01, 1.8233e-01, 1.3987e+00], [ 1.7548e+00, -5.5406e-02, 7.8020e-01, -1.9451e+00, -1.1238e+00, 1.3765e+00, 4.4821e-01, -8.6358e-01, -7.7124e-01, -1.8096e-01], [ 8.7309e-01, 6.8673e-01, 6.5082e-01, 1.7901e+00, -6.1880e-01, -3.6823e-01, 3.9230e-01, -7.9143e-01, 8.1201e-01, -2.5098e-01]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python np.random.normal(mu, sigma, size=(10, 10)) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[-0.70057691, -1.08498021, 0.61045712, 0.38959336, -0.39842221, 0.44038231, 0.71311424, 0.87899763, -1.04249347, -1.99314222], [-1.19042449, 1.42323131, -2.03115771, -0.55207844, -0.34944086, -0.88541313, 0.68772012, -1.08336504, 0.0043346 , -0.84555549], [-1.01220272, -0.18602191, -0.96093954, -0.06832284, 0.67980878, -0.84498207, 0.12785912, 0.42840493, -1.02172581, 0.94296419], [-1.47794148, -1.0995401 , 0.32091134, -1.38076821, 0.53228056, 1.37388727, 0.85008316, -0.8838563 , 0.25436875, 0.8664244 ], [-0.86416781, 0.68150441, 0.91070442, -0.31040679, -1.24914887, -1.09228335, 0.23755172, 0.46968028, -1.38901965, -1.3061524 ], [ 0.92143486, 2.25051919, 0.93860495, -0.04962321, -0.94616495, -0.30157155, 0.25928661, 0.52036172, -0.33842228, -1.16051394], [ 0.39067333, -0.29209444, 2.04488421, 1.26964076, 0.02560868, 0.09079073, -0.89648959, -0.805082 , 0.0373519 , 1.77227377], [-1.30955062, 1.55305581, -0.87499763, -0.37879967, 1.41977948, -1.56414618, -0.19597413, 0.17618799, 1.39520775, 0.8540339 ], [ 2.00466515, 0.78036987, 1.50414668, 0.8129593 , -0.33577407, -0.74829381, -0.51340285, -0.16907909, 0.26541118, -0.52972822], [-0.12352982, 1.31952519, -0.64136017, -0.11811265, 1.37662606, -0.09667546, 0.41606841, -0.06347404, 0.78929749, -0.40645727]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python tf.random.normal((10, 10), mu, sigma) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
.. _subsec_exponential_family: 指数型分布族 ------------ 上で挙げた分布に共通する性質の1つは、すべてが *指数型分布族* に属することである。指数型分布族は、密度を次の形で表せる分布の集合である。 .. math:: p(\mathbf{x} \mid \boldsymbol{\eta}) = h(\mathbf{x}) \cdot \exp \left( \boldsymbol{\eta}^{\top} \cdot T(\mathbf{x}) - A(\boldsymbol{\eta}) \right) :label: eq_exp_pdf この定義は少し繊細なので、詳しく見ていきよう。 まず、\ :math:`h(\mathbf{x})` は *基底測度* あるいは *ベース測度* と呼ばれる。これは、指数重みで修正する元の測度とみなせる。 次に、\ :math:`\boldsymbol{\eta} = (\eta_1, \eta_2, ..., \eta_l) \in \mathbb{R}^l` というベクトルがあり、これを *自然パラメータ* または *標準パラメータ* と呼ぶ。これらは、基底測度をどのように修正するかを定める。自然パラメータは、\ :math:`\mathbf{x}= (x_1, x_2, ..., x_n) \in \mathbb{R}^n` のある関数 :math:`T(\cdot)` に対してこれらのパラメータとの内積を取り、それを指数化することで新しい測度に入る。ベクトル :math:`T(\mathbf{x})= (T_1(\mathbf{x}), T_2(\mathbf{x}), ..., T_l(\mathbf{x}))` は、\ :math:`\boldsymbol{\eta}` に対する *十分統計量* と呼ばれる。この名前は、\ :math:`T(\mathbf{x})` に表される情報だけで確率密度を計算するのに十分であり、サンプル :math:`\mathbf{x}` の他の情報は不要だからである。 第三に、\ :math:`A(\boldsymbol{\eta})` がある。これは *累積関数* と呼ばれ、上の分布 :eq:`eq_exp_pdf` が1に積分されること、すなわち .. math:: A(\boldsymbol{\eta}) = \log \left[\int h(\mathbf{x}) \cdot \exp \left(\boldsymbol{\eta}^{\top} \cdot T(\mathbf{x}) \right) d\mathbf{x} \right]. を保証する。 具体的に、ガウス分布を考えてみよう。\ :math:`\mathbf{x}` が1変数であるとすると、その密度は .. math:: \begin{aligned} p(x \mid \mu, \sigma) &= \frac{1}{\sqrt{2 \pi \sigma^2}} \cdot \exp \left\{ \frac{-(x-\mu)^2}{2 \sigma^2} \right\} \\ &= \frac{1}{\sqrt{2 \pi}} \cdot \exp \left\{ \frac{\mu}{\sigma^2}x -\frac{1}{2 \sigma^2} x^2 - \left( \frac{1}{2 \sigma^2} \mu^2 +\log(\sigma) \right) \right\}. \end{aligned} これは指数型分布族の定義に次のように対応する。 - *基底測度*: :math:`h(x) = \frac{1}{\sqrt{2 \pi}}`\ 、 - *自然パラメータ*: :math:`\boldsymbol{\eta} = \begin{bmatrix} \eta_1 \\ \eta_2 \end{bmatrix} = \begin{bmatrix} \frac{\mu}{\sigma^2} \\ \frac{1}{2 \sigma^2} \end{bmatrix}`\ 、 - *十分統計量*: :math:`T(x) = \begin{bmatrix}x\\-x^2\end{bmatrix}`\ 、および - *累積関数*: :math:`A({\boldsymbol\eta}) = \frac{1}{2 \sigma^2} \mu^2 + \log(\sigma) = \frac{\eta_1^2}{4 \eta_2} - \frac{1}{2}\log(2 \eta_2)`\ 。 上の各項の正確な選び方は、ある程度任意であることに注意する価値がある。重要なのは、分布がこの形で表せることであって、その厳密な形そのものではない。 :numref:`subsec_softmax_and_derivatives` でほのめかしたように、広く使われる手法の1つは、最終出力 :math:`\mathbf{y}` が指数型分布族に従うと仮定することである。指数型分布族は、機械学習で頻繁に現れる、一般的で強力な分布族である。 まとめ ------ - ベルヌーイ確率変数は、はい/いいえの結果を持つ事象をモデル化できる。 - 離散一様分布は、有限個の候補からの選択をモデル化する。 - 連続一様分布は、区間からの選択をモデル化する。 - 二項分布は、ベルヌーイ確率変数の系列をモデル化し、成功回数を数える。 - ポアソン確率変数は、稀な事象の到着をモデル化する。 - ガウス確率変数は、多数の独立な確率変数を足し合わせた結果をモデル化する。 - 上記の分布はすべて指数型分布族に属する。 演習 ---- 1. 独立な二項確率変数 :math:`X, Y \sim \textrm{Binomial}(16, 1/2)` の差 :math:`X-Y` である確率変数の標準偏差はいくつか。 2. ポアソン確率変数 :math:`X \sim \textrm{Poisson}(\lambda)` を取り、\ :math:`\lambda \rightarrow \infty` のときの :math:`(X - \lambda)/\sqrt{\lambda}` を考えると、これが近似的にガウス分布になることを示せる。なぜこれは自然なのか。 3. :math:`n` 個の要素を持つ2つの離散一様確率変数の和の確率質量関数は何か。