標本経路と有限次元分布の数学

実行環境

Python 3.14.6、NumPy 2.4.6、SciPy 1.18.1、Matplotlib 3.11.2(Jupyter Notebook)。

本記事は2021年頃の記事をリライトし、上記の環境で計算したものである。

概要

確率過程を標本経路と複数時刻の同時分布から調べる。ランダムウォークの数え上げ、ブラウン運動の共分散計算、経路の連続性に関する反例を扱う。

書籍『確率過程の理論と実践』の第1章「確率過程の基礎」に対応する。同時分布と共分散の基本を前提とする。拡張定理の証明は参考文献に譲る。

計算の準備

コードは上から順に実行する。数値は表示時だけ有効数字3桁に整える。

実行環境の確認・ライブラリの読み込み・描画設定
from pathlib import Path
from pprint import pprint
import platform

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import scipy
from scipy import stats
from IPython.display import Math, display
from matplotlib.ticker import FuncFormatter, MaxNLocator
from matplotlib_inline.backend_inline import set_matplotlib_formats

set_matplotlib_formats("svg")


def sig(value):
    # 表示だけを3桁に丸める。
    return "0" if value == 0 else format(float(value), "#.3g")


np.set_printoptions(formatter={"float_kind": sig})
plt.rcParams.update({"font.size": 10, "svg.fonttype": "none",
                     "axes.spines.top": False, "axes.spines.right": False})
cwd = Path.cwd()
base = cwd / "series/stochastic-processes/102" if (cwd / "pyproject.toml").exists() else cwd
image_dir = base / "102_nb_files"
image_dir.mkdir(parents=True, exist_ok=True)


def save(fig, name):
    fig.savefig(image_dir / name, format="svg", bbox_inches="tight",
                metadata={"Date": None})
    plt.show()
    plt.close(fig)


def ticks(ax, which="y"):
    for name in which:
        getattr(ax, name + "axis").set_major_formatter(
            FuncFormatter(lambda value, _: sig(value)))


print("カーネル: wa-src(3.14.6)")
print("Python:", platform.python_version())
print("NumPy:", np.__version__, "SciPy:", scipy.__version__)
print("Matplotlib:", matplotlib.__version__)
assert platform.python_version() == "3.14.6"
カーネル: wa-src(3.14.6)
Python: 3.14.6
NumPy: 2.4.6 SciPy: 1.18.1
Matplotlib: 3.11.2

1. 標本経路は試行結果を固定した関数

確率過程$X_t(\omega)$には時刻$t$と試行結果$\omega$の2つの引数がある。

$$ \begin{aligned} t\text{を固定}:&\quad \omega\longmapsto X_t(\omega)\quad\text{(確率変数)},\\ \omega\text{を固定}:&\quad t\longmapsto X_t(\omega)\quad\text{(標本経路)}. \end{aligned} $$

独立な増分$\xi_j$が確率$1/2$ずつで$-1$と$1$を取るランダムウォーク$S_n=\sum_{j=1}^{n}\xi_j$、$S_0=0$を考える。3歩の経路は$2^3=8$通りで、各確率は$1/8$である。

from itertools import product

# 増分を全列挙する。
steps = np.array(list(product([-1, 1], repeat=3)))
paths = np.column_stack([np.zeros(8, dtype=int), steps.cumsum(axis=1)])
pprint(paths)

fig, axes = plt.subplots(1, 2, figsize=(9, 3), layout="constrained")
axes[0].plot(np.arange(4), paths.T, "o-", alpha=0.350)
axes[0].plot(np.arange(4), paths[3], "o-", color="black", label="One outcome")
axes[0].set(xlabel="Time n", ylabel="S(n)", xticks=range(4))
axes[0].legend()
axes[1].hist(paths[:, 3], bins=np.arange(-4, 5, 2), weights=np.full(8, 1 / 8),
             rwidth=0.700)
axes[1].set(xlabel="S(3)", ylabel="Probability", xticks=[-3, -1, 1, 3])
ticks(axes[1])
save(fig, "01_paths_and_marginal.svg")
array([[ 0, -1, -2, -3],
       [ 0, -1, -2, -1],
       [ 0, -1,  0, -1],
       [ 0, -1,  0,  1],
       [ 0,  1,  0, -1],
       [ 0,  1,  0,  1],
       [ 0,  1,  2,  1],
       [ 0,  1,  2,  3]])

図1では同じ終点に複数の経路が集まり、右の分布から途中の順序は復元できない。

2. 有限次元分布は複数時刻の同時分布

任意の有限個の時刻$t_1,\ldots,t_k$を選び、列ベクトル

$$ \mathbf{X}_{\mathbf{t}}=(X_{t_1},\ldots,X_{t_k})^{\mathsf T} $$

の同時分布の族を有限次元分布という。「有限」は選んだ時刻の個数を指し、状態の個数ではない。

ランダムウォークで$S_1=a$、$S_3=b$となる確率を求める。最初の増分が$a\in\{-1,1\}$となる確率は$1/2$である。残り2歩のうち$+1$が$r$回なら、増分の合計は$r-(2-r)=2r-2$だから、$r=(b-a+2)/2$となる。独立性より、

$$ \begin{aligned} \mathbb{P}(S_1=a,S_3=b) &=\mathbb{P}(S_1=a)\mathbb{P}(\xi_2+\xi_3=b-a)\\ &=\frac12\binom{2}{r}\left(\frac12\right)^r \left(\frac12\right)^{2-r} =\frac18\binom{2}{r}. \end{aligned} $$

$r$が$0,1,2$以外なら確率は$0$である。行を$a=-1,1$、列を$b=-3,-1,1,3$の順にすると、同時確率は

$$ \mathbf{P}=\frac18 \begin{bmatrix}1&2&1&0\\0&1&2&1\end{bmatrix}. $$

1行目の4要素を足すと$\mathbb{P}(S_1=-1)=(1+2+1+0)/8=1/2$となる。各列の2要素を足すと、$S_3$の確率は$(1,3,3,1)/8$になる。このように不要な時刻を足し上げて周辺分布を得る。

first, last = np.array([-1, 1]), np.array([-3, -1, 1, 3])
joint = np.array([[( (paths[:, 1] == a) & (paths[:, 3] == b)).mean()
                   for b in last] for a in first])
expected = np.array([[1, 2, 1, 0], [0, 1, 2, 1]]) / 8
np.testing.assert_allclose(joint, expected)
np.testing.assert_allclose(joint.sum(axis=0), stats.binom.pmf(np.arange(4), 3, 1 / 2))
np.testing.assert_allclose(joint.sum(axis=1), [1 / 2, 1 / 2])
pprint(joint)

fig, ax = plt.subplots(figsize=(6, 3), layout="constrained")
ax.imshow(joint, cmap="Blues", vmin=0, vmax=0.300)
for (i, j), value in np.ndenumerate(joint):
    ax.text(j, i, sig(value), ha="center", va="center")
ax.set(xticks=range(4), xticklabels=last, yticks=range(2), yticklabels=first,
       xlabel="S(3)", ylabel="S(1)", title="Joint probability")
save(fig, "02_joint_distribution.svg")
array([[0.125, 0.250, 0.125, 0],
       [0, 0.125, 0.250, 0.125]])

$$ \displaystyle \mathbb{P}(S_1=-1,S_3=1)=\frac18=0.125 $$

3. 1時刻の分布が同じでも過程は異なる

独立な$A_n\sim\operatorname{Bernoulli}(1/2)$と、1個の$Z\sim\operatorname{Bernoulli}(1/2)$を全時刻で使う$D_n=Z$を比較する。どの時刻でも両者の成功確率は$1/2$である。しかし、行を時刻1の値$0,1$、列を時刻2の値$0,1$とすると、

$$ \mathbf{P}_A=\begin{bmatrix}1/4&1/4\\1/4&1/4\end{bmatrix},\qquad \mathbf{P}_D=\begin{bmatrix}1/2&0\\0&1/2\end{bmatrix}. $$

独立な場合は$\mathbb{P}(A_1=1,A_2=1)=(1/2)(1/2)=1/4$である。同じ$Z$を使う場合は$\mathbb{P}(D_1=1,D_2=1)=\mathbb{P}(Z=1)=1/2$となる。したがって、

$$ \begin{aligned} \operatorname{Cov}(A_1,A_2)&=1/4-(1/2)(1/2)=0,\\ \operatorname{Cov}(D_1,D_2)&=1/2-(1/2)(1/2)=1/4. \end{aligned} $$

図3の各行は1本の経路である。$D_n$の時間平均は$0$か$1$であり、各時刻の期待値$1/2$とは一致しない。

rng = np.random.default_rng(102)
independent = rng.binomial(1, 0.500, size=(4000, 40))
shared = np.repeat(rng.binomial(1, 0.500, size=(4000, 1)), 40, axis=1)
fig, axes = plt.subplots(1, 2, figsize=(9, 3), layout="constrained")
for ax, values, title in zip(axes, [independent, shared], ["Independent A(n)", "Shared D(n)"]):
    ax.imshow(values[:12], aspect="auto", cmap="Blues", vmin=0, vmax=1,
              extent=[0.500, 40.5, 12.5, 0.500])
    ax.set(title=title, xlabel="Time n (white: 0, blue: 1)", ylabel="Path index")
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
save(fig, "03_same_marginals.svg")

4. ブラウン運動の有限次元分布

標準ブラウン運動$W_t$を考える。$0\le s\le t$なら、$W_t=W_s+(W_t-W_s)$であり、$W_s$と増分$W_t-W_s$は独立、両者の平均は$0$である。よって、

$$ \begin{aligned} \operatorname{Cov}(W_s,W_t) &=\mathbb{E}[W_sW_t]\\ &=\mathbb{E}[W_s^2]+\mathbb{E}[W_s(W_t-W_s)]\\ &=s+\mathbb{E}[W_s]\mathbb{E}[W_t-W_s]=s. \end{aligned} $$

$s>t$でも共分散は対称なので、一般に$\operatorname{Cov}(W_s,W_t)=\min(s,t)$である。

$0=t_0<t_1<\cdots<t_k$、$\Delta t_j=t_j-t_{j-1}$と置く。独立な増分$\Delta W_j\sim\mathcal{N}(0,\Delta t_j)$を足すため、

$$ \mathbf{W}_{\mathbf{t}}\sim\mathcal{N}_k(\mathbf{0},\mathbf{K}),\qquad K_{ij}=\min(t_i,t_j). $$

正規ベクトルの線形変換なので多変量正規分布になる。一般の過程は平均と共分散だけでは決まらない。

時刻を$(1/4,1/2,1)$とした場合は

$$ \mathbf{K}=\begin{bmatrix} 1/4&1/4&1/4\\1/4&1/2&1/2\\1/4&1/2&1 \end{bmatrix}. $$

同時密度も増分から得られる。$x_0=0$とし、増分から位置への変換を$x_j=\sum_{i=1}^{j}d_i$とする。変換行列は対角成分がすべて$1$の下三角行列なので、ヤコビアンの絶対値は$1$である。したがって、独立な増分の密度を掛ければ

$$ f_{\mathbf{t}}(x_1,\ldots,x_k) =\prod_{j=1}^{k}\frac{1}{\sqrt{2\pi\Delta t_j}} \exp\left[-\frac{(x_j-x_{j-1})^2}{2\Delta t_j}\right] $$

となる。次は増分から3時刻の値を生成する。

times = np.array([1 / 4, 1 / 2, 1])
delta = np.diff(np.r_[0, times])
w = rng.normal(size=(20000, 3)) * np.sqrt(delta)
w = w.cumsum(axis=1)
kernel = np.minimum.outer(times, times)
estimated = np.cov(w, rowvar=False)
pprint({"理論共分散": kernel, "標本共分散": estimated})
# 共分散の2通りの計算を照合する。
lower = np.tril(np.ones((3, 3)))
np.testing.assert_allclose(lower @ np.diag(delta) @ lower.T, kernel)

fig, ax = plt.subplots(figsize=(6, 4), layout="constrained")
ax.scatter(w[:2000, 0], w[:2000, 2], s=4, alpha=0.200)
xx, yy = np.meshgrid(np.linspace(-2, 2, 120), np.linspace(-4, 4, 120))
covariance = kernel[np.ix_([0, 2], [0, 2])]
density = stats.multivariate_normal.pdf(np.dstack([xx, yy]), mean=[0, 0], cov=covariance)
ax.contour(xx, yy, density, levels=[0.0200, 0.0800, 0.200, 0.300], colors="black")
ax.set(xlabel="W(0.250)", ylabel="W(1)", title="Samples and theoretical density")
ticks(ax, "xy")
save(fig, "04_brownian_joint.svg")
{'標本共分散': array([[0.249, 0.248, 0.248],
       [0.248, 0.504, 0.509],
       [0.248, 0.509, 1.02]]),
 '理論共分散': array([[0.250, 0.250, 0.250],
       [0.250, 0.500, 0.500],
       [0.250, 0.500, 1.00]])}

$$ \displaystyle \det\mathbf{K}=\frac14\cdot\frac14\cdot\frac12=\frac1{32} $$

図4は時刻$0.250$と$1$の組である。共通の増分を含むため相関がある。標本共分散には有限標本による誤差が残るが、公式は上の計算から得られる。

5. 整合性と経路の連続性

時刻を取り除く操作と周辺化、時刻の並べ替えと座標の並べ替えは、それぞれ一致する必要がある。これを整合性という。密度がある場合は、例えば

$$ f_{t_1,t_3}(x_1,x_3)=\int_{\mathbb{R}} f_{t_1,t_2,t_3}(x_1,x_2,x_3)\,dx_2 $$

が成立する。実数値の場合、すべての有限時刻集合に整合的な分布を与えると、Kolmogorovの拡張定理により過程が存在する。その法則は$\mathbb{R}^{T}$上の積$\sigma$加法族、すなわち有限時刻の観測条件から生成される事象の集まりで一意になる。ただし、経路の連続性までは保証しない。詳細はShaliziの第2章 を参照する。

$U\sim\operatorname{Uniform}(0,1)$として、$t\in[0,1]$上で

$$ X_t=0,\qquad Y_t=\mathbf{1}_{\{t=U\}} $$

と置く。固定した$t$では$\mathbb{P}(U=t)=0$である。任意の有限個の固定時刻に対しても、

$$ \mathbb{P}\bigl(\exists j:Y_{t_j}\ne0\bigr) =\mathbb{P}\bigl(U\in\{t_1,\ldots,t_k\}\bigr) \le\sum_{j=1}^{k}\mathbb{P}(U=t_j)=0. $$

したがって、$X$と$Y$はすべての有限次元分布が同じである。しかし、$X$の経路は常に連続で、$Y$の経路は必ず時刻$U$で不連続になる。

u = rng.uniform()
grid = np.linspace(0, 1, 11)
fig, axes = plt.subplots(1, 2, figsize=(9, 3), layout="constrained")
axes[0].plot([0, 1], [0, 0], color="C0")
axes[0].scatter([u], [0], facecolors="white", edgecolors="C0", zorder=3)
axes[0].scatter([u], [1], color="C1", zorder=3)
axes[0].annotate("Y(U) = 1", (u, 1), xytext=(0.100, 0.750),
                 arrowprops={"arrowstyle": "->"})
axes[0].set(title="One path: zero except at U")
axes[1].scatter(grid, (grid == u).astype(int), color="C0")
axes[1].set(title="Values on a fixed observation grid")
for ax in axes:
    ax.set(xlabel="Time t", ylabel="Y(t)", ylim=(-0.100, 1.20), yticks=[0, 1])
    ticks(ax, "x")
save(fig, "05_random_spike.svg")

図5では$Y_U=1$を明示的に描いた。固定格子では、ほとんど確実にこの1点を見落とす。これは模式図であり、有限精度の乱数実験は確率$0$の証明にはならない。

次の2つが両立する。

$$ \begin{aligned} &\text{各 }t:\quad\mathbb{P}(X_t=Y_t)=1,\\ &\mathbb{P}(X_t=Y_t\text{ がすべての }t\text{ で成立})=0. \end{aligned} $$

前者を修正(modification)、後者の確率が$1$となる関係を識別不能という。非可算個の時刻では「各時刻で確率1」を「全時刻で同時に確率1」へ交換できない。両方の経路が連続なら、有理数時刻の一致を全時刻へ広げられる。詳細はShaliziの第7章 を参照する。

この反例は積$\sigma$加法族での一意性と矛盾しない。連続性を扱うには、経路空間とその上の可測な事象も指定する必要がある。

結論

この記事では、標本経路と有限次元分布の関係を、数え上げと共分散計算、連続性の反例で確認した。

1時刻の分布だけでは依存関係は分からない。有限次元分布には整合性が必要で、経路の連続性は別に確認する。格子点での性質と経路全体の性質を分けて確認すると、連続性についての誤解を避けやすい。

参考文献

  • Shalizi, C. R., Stochastic Processes, Chapter 2(本文リンク):有限次元分布、整合性、拡張定理。
  • 同 Chapter 7(本文リンク):修正、識別不能、経路の連続性。