ケィオスの時系列解析メモランダム

時系列解析、生体情報解析などをやわらかく語ります

【線形代数の基礎】ベクトルと行列の基本的な微分公式

行列微分は、多変量最適化、機械学習、信号処理、統計学などの多くの分野で基本となる道具です。今回は、ベクトルや行列に関する代表的な微分公式をまとめ、その考え方を説明してみます。

0. ベクトル・行列表記の基礎

 みなさんわかっていると思いますが、念のため基礎の基礎から確認していきます。

■ ベクトル

 ここでは、「ベクトル」を、列ベクトル \displaystyle{
\mathbf{a} \in \mathbb{R}^ m
}

\displaystyle{
\mathbf{a} =
\begin{pmatrix}
a_1\\
a_2\\
\vdots\\
a_m
\end{pmatrix}
}

の形で表すことにします。

 成分は \displaystyle{
a _ i
} で、上から \displaystyle{
i
} 行目の値を表します。

 ベクトル \displaystyle{
\mathbf{x} \in \mathbb{R}^ n
} については、

\displaystyle{
\mathbf{x} =
\begin{pmatrix}
x_1\\
x_2\\
\vdots\\
x_n
\end{pmatrix}.
}

 成分は \displaystyle{
x _ i
} です。

■ 行列

 行列 \displaystyle{
A \in \mathbb{R}^ {m \times n}
}

\displaystyle{
A =
\begin{pmatrix}
A_{11} & A_{12} & \cdots & A_{1n} \\
A_{21} & A_{22} & \cdots & A_{2n} \\
\vdots & \vdots &        & \vdots \\
A_{m1} & A_{m2} & \cdots & A_{mn}
\end{pmatrix}
}

 成分 \displaystyle{
A _ {ij}
} は「\displaystyle{
i
}\displaystyle{
j
} 列」の実数です。

 正方行列 \displaystyle{
B \in \mathbb{R}^ {n \times n}
} は、行数と列数が同じ行列で、

\displaystyle{
B =
\begin{pmatrix}
B_{11} & B_{12} & \cdots & B_{1n}\\
B_{21} & B_{22} & \cdots & B_{2n}\\
\vdots & \vdots &        & \vdots\\
B_{n1} & B_{n2} & \cdots & B_{nn}
\end{pmatrix}
}

スカラー値関数

 スカラー値関数 \displaystyle{
f(\mathbf{x})
}は、

\displaystyle{
f:\mathbb{R}^n \to \mathbb{R}
}

であり、ベクトル

\displaystyle{
\mathbf{x} =
\begin{pmatrix}
x_1\\
x_2\\
\vdots\\
x_n
\end{pmatrix}
\in \mathbb{R}^n
}

を入力として実数値(スカラー)を出力する関数です。

 入力は \displaystyle{
n
} 次元ベクトル、出力は 1 つの実数です。

 たとえば、以下のような関数は、スカラー値関数です。

\displaystyle{
f(\mathbf{x}) = a^\top \mathbf{x},
\qquad
f(x)=\mathbf{x}^\top B \mathbf{x}.
}

それぞれ、線形、二次形式と呼ばれる関数で、1次式、2次式のようなものです。

■ 転置の定義

 行列 \displaystyle{
A \in \mathbb{R}^ {m\times n}
} の転置 \displaystyle{
A^ \top \in \mathbb{R}^ {n\times m}
} を次で定義します。

\displaystyle{
(A^\top)_{ij} = A_{ji} \qquad (1 \le i \le n,\ 1 \le j \le m)
}

 すなわち、行と列を入れ替えた行列です。

 特に、列ベクトル \displaystyle{
\mathbf{x} \in \mathbb{R}^ n
} の転置は行ベクトルで、 \displaystyle{
\mathbf{x}^ \top = (x _ 1,\ x _ 2,\ \dots,\ x _ n)
}

となります。

■ 行列とベクトルの積

 行列 \displaystyle{
A \in \mathbb{R}^ {m\times n}
}、ベクトル \displaystyle{
\mathbf{x} \in \mathbb{R}^ n
} について、 積 \displaystyle{
A\, \mathbf{x} \in \mathbb{R}^ m
} を次で定義します。

\displaystyle{
(A\, \mathbf{x})_i = \sum_{j=1}^n A_{ij} x_j
}

 また、行ベクトル \displaystyle{
\mathbf{x}^ \top
} と行列 \displaystyle{
A
} の積 \displaystyle{
\mathbf{x}^ \top A \in \mathbb{R}^ {1\times n}
}

\displaystyle{
(\mathbf{x}^\top A)_j = \sum_{i=1}^n x_i A_{ij}
}

で与えられます。

内積の定義

 ベクトル \displaystyle{
\mathbf{x}, \mathbf{a} \in \mathbb{R}^ n
} に対して、内積

\displaystyle{
\mathbf{a}^\top \mathbf{x} = \sum_{i=1}^n a_i x_i
}

で定義します。

■ ナブラ

 偏微分の列ベクトルをナブラ \displaystyle{
\nabla
} として定義します:

\displaystyle{
\nabla_{\mathbf{x}} 
=
\begin{pmatrix}
\frac{\partial }{\partial x_1}\\
\frac{\partial }{\partial x_2}\\
\vdots\\
\frac{\partial }{\partial x_n}
\end{pmatrix}.
}

■ 勾配(gradient)

 スカラー値関数 \displaystyle{
f(\mathbf{x})
} に対して、ベクトル \displaystyle{
\mathbf{x}
} の各成分で偏微分したものを縦に並べた列ベクトルを、勾配(gradient)と呼び、次で定義します。

\displaystyle{
\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}}
=
\begin{pmatrix}
\frac{\partial f(\mathbf{x})}{\partial x_1}\\
\frac{\partial f(\mathbf{x})}{\partial x_2}\\
\vdots\\
\frac{\partial f(\mathbf{x})}{\partial x_n}
\end{pmatrix}
}

 これは、ナブラ (\displaystyle{
\nabla
}) を使えば、

\displaystyle{
\nabla_{\mathbf{x}} f (\mathbf{x})
=
\begin{pmatrix}
\frac{\partial f(\mathbf{x})}{\partial x_1}\\
\frac{\partial f(\mathbf{x})}{\partial x_2}\\
\vdots\\
\frac{\partial f(\mathbf{x})}{\partial x_n}
\end{pmatrix}.
}

 以下では、この表記は使いません。

1. 微分の公式

スカラー関数 \displaystyle{f(\mathbf{x})}微分

● 線形関数
\displaystyle{
\frac{\partial}{\partial \mathbf{x}} (\mathbf{a}^\top \mathbf{x})
\;=\;
\mathbf{a}
}

 成分表示で確認してみます。線形関数は成分表示で

\displaystyle{
\begin{aligned}
f(\mathbf{x})
&= \mathbf{a}^\top \mathbf{x}\\
&=
\begin{pmatrix}
a_1 & a_2 & \cdots & a_n
\end{pmatrix}
\begin{pmatrix}
x_1\\ x_2\\ \vdots\\ x_n
\end{pmatrix}\\
&= \sum_{i=1}^n a_i x_i
\end{aligned}
}

ですので、

\displaystyle{
\frac{\partial f}{\partial x_i} = a_i.

}

これを勾配ベクトルにまとめると、

\displaystyle{
\frac{\partial f}{\partial \mathbf{x}}=
\begin{pmatrix}
a_1\\
a_2\\
\vdots\\
a_n
\end{pmatrix}
= \mathbf{a}.
}
● 二次形式

 ここでは、行列 \displaystyle{
A
} を、正方行列 \displaystyle{
A \in \mathbb{R}^ {n\times n}
} とします。

\displaystyle{
\frac{\partial}{\partial \mathbf{x}}(\mathbf{x}^\top A \mathbf{x}) \;=\;
(A + A^\top)\,\mathbf{x}
}

 特に、\displaystyle{
A
} が対称行列のときは

\displaystyle{
\nabla_{\mathbf{x}}(\mathbf{x}^\top A \mathbf{x}) = 2A\mathbf{x}
}

 成分を書き下して確認してみます。まず,ベクトル \displaystyle{
\mathbf{x}
} と正方行列 \displaystyle{
A\in\mathbb{R}^ {n\times n}
} を成分表示します。

\displaystyle{
\mathbf{x}=
\begin{pmatrix}
x_1\\ x_2\\ \vdots\\ x_n
\end{pmatrix},
\qquad
A =
\begin{pmatrix}
A_{11} & A_{12} & \cdots & A_{1n} \\
A_{21} & A_{22} & \cdots & A_{2n} \\
\vdots & \vdots &        & \vdots \\
A_{n1} & A_{n2} & \cdots & A_{nn}
\end{pmatrix}.
}

二次形式のスカラー値関数

\displaystyle{
\begin{aligned}
f(\mathbf{x})
&= \mathbf{x}^\top A \mathbf{x}\\
&=
\begin{pmatrix}
x_1 & x_2 & \cdots & x_n
\end{pmatrix}
\begin{pmatrix}
A_{11} & A_{12} & \cdots & A_{1n} \\
A_{21} & A_{22} & \cdots & A_{2n} \\
\vdots & \vdots &        & \vdots \\
A_{n1} & A_{n2} & \cdots & A_{nn}
\end{pmatrix}
\begin{pmatrix}
x_1\\x_2\\ \vdots\\ x_n
\end{pmatrix}\\
&= \sum_{i=1}^n\sum_{j=1}^n x_i\, A_{ij}\, x_j
\end{aligned}
}

\displaystyle{
x _ k
}偏微分します。

\displaystyle{
\frac{\partial f(\mathbf{x})}{\partial x_k}
=
\sum_{j=1}^n A_{kj} x_j
+
\sum_{i=1}^n x_i A_{ik}
}

ここで、

\displaystyle{
(A\mathbf{x})_k = \sum_{j=1}^n A_{kj}x_j,\qquad
(A^\top\mathbf{x})_k = \sum_{i=1}^n A_{ik}x_i
}

なので、

\displaystyle{
\frac{\partial f(\mathbf{x})}{\partial x_k}
= (A\mathbf{x})_k + (A^\top\mathbf{x})_k
}

 各成分を並べると、

\displaystyle{
\frac{\partial f(\mathbf{x})}{\partial \mathbf{x}}=
A\mathbf{x} + A^\top\mathbf{x}
}

 すなわち、

\displaystyle{
\nabla_{\mathbf{x}}(\mathbf{x}^\top A \mathbf{x})
=(A + A^\top),\mathbf{x}
}

 A が対称であれば、\displaystyle{
A = A^ \top
}なので、\displaystyle{
2A\mathbf{x}
} になります。

● 行列を含む線形形式の微分
\displaystyle{
\frac{\partial (A\mathbf{x})}{\partial \mathbf{x}}
\;=\;
A
}

 成分表示で確認してみます。行列とベクトルを成分で書くと、

\displaystyle{
A=
\begin{pmatrix}
A_{11} & A_{12} & \cdots & A_{1n}\\
A_{21} & A_{22} & \cdots & A_{2n}\\
\vdots & \vdots &        & \vdots\\
A_{m1} & A_{m2} & \cdots & A_{mn}
\end{pmatrix},\qquad
\mathbf{x}=
\begin{pmatrix}
x_1\\ x_2\\ \vdots\\ x_n
\end{pmatrix}
}

線形写像 \displaystyle{
A\mathbf{x}
}

\displaystyle{
A\mathbf{x}
=
\begin{pmatrix}
\sum_{j=1}^n A_{1j} x_j\\
\sum_{j=1}^n A_{2j} x_j\\
\vdots\\
\sum_{j=1}^n A_{mj} x_j
\end{pmatrix}.
}

です.ここでは、各成分を \displaystyle{
(A\mathbf{x}) _ i
} と表すことにして、

\displaystyle{
A\mathbf{x}
=
\begin{pmatrix}
(A\mathbf{x})_1\\
(A\mathbf{x})_2\\
\vdots\\
(A\mathbf{x})_m
\end{pmatrix}
=
\begin{pmatrix}
\sum_{j=1}^n A_{1j} x_j\\
\sum_{j=1}^n A_{2j} x_j\\
\vdots\\
\sum_{j=1}^n A_{mj} x_j
\end{pmatrix}.
}

とします。この式の偏微分は,すべての \displaystyle{
i=1,\dots,m
}\displaystyle{
k=1,\dots,n
} について並べて計算することを意味するので,次のような行列になります.

\displaystyle{ \frac{\partial(A\mathbf{x})}{\partial\mathbf{x}} = \begin{pmatrix} \dfrac{\partial (A\mathbf{x})_1}{\partial x_1} & \dfrac{\partial (A\mathbf{x})_1}{\partial x_2} & \cdots & \dfrac{\partial (A\mathbf{x})_1}{\partial x_n} \\ \dfrac{\partial (A\mathbf{x})_2}{\partial x_1} & \dfrac{\partial (A\mathbf{x})_2}{\partial x_2} & \cdots & \dfrac{\partial (A\mathbf{x})_2}{\partial x_n} \\ \vdots & \vdots & \ddots & \vdots \\ \dfrac{\partial (A\mathbf{x})_m}{\partial x_1} & \dfrac{\partial (A\mathbf{x})_m}{\partial x_2} & \cdots & \dfrac{\partial (A\mathbf{x})_m}{\partial x_n} \end{pmatrix} }

\displaystyle{
A\mathbf{x}
}\displaystyle{
i
} 行目の成分を \displaystyle{
x _ k
}微分すると、

\displaystyle{
\frac{\partial (A\mathbf{x})_i}{\partial x_k}
= A_{ik}
}

となります.

 したがって、偏微分の結果を並べれば

\displaystyle{
\frac{\partial(A\mathbf{x})}{\partial\mathbf{x}}
=
A
}

となります。

\displaystyle{
\frac{\partial (\mathbf{x}^\top A)}{\partial \mathbf{x}}
\;=\;
A^\top
}

 成分表示で計算して確認してみます。まず、行×列の積を明示して書きます。

\displaystyle{
\begin{aligned}
\mathbf{x}^\top A
&=
\begin{pmatrix}
x_1 & x_2 & \cdots & x_n
\end{pmatrix}
\begin{pmatrix}
A_{11} & A_{12} & \cdots & A_{1n}\\
A_{21} & A_{22} & \cdots & A_{2n}\\
\vdots & \vdots &        & \vdots\\
A_{n1} & A_{n2} & \cdots & A_{nn}
\end{pmatrix}\\
&=
\begin{pmatrix}
\sum_{i=1}^n x_i A_{i1} &
\sum_{i=1}^n x_i A_{i2} &
\cdots &
\sum_{i=1}^n x_i A_{in}
\end{pmatrix}
\end{aligned}
}

ここでは、各成分を \displaystyle{
(\mathbf{x}^ \top A)_k
} と表すことにして、

\displaystyle{
\begin{aligned}
\mathbf{x}^\top A
&=
\begin{pmatrix}
(\mathbf{x}^ \top A)_1 &
(\mathbf{x}^ \top A)_2 &
\cdots &
(\mathbf{x}^ \top A)_n
\end{pmatrix}\\
&=
\begin{pmatrix}
\sum_{i=1}^n x_i A_{i1} &
\sum_{i=1}^n x_i A_{i2} &
\cdots &
\sum_{i=1}^n x_i A_{in}
\end{pmatrix}
\end{aligned}
}

とします。この式の偏微分は、

\displaystyle{
\frac{\partial (\mathbf{x}^\top A)}{\partial \mathbf{x}}
=
\begin{pmatrix}
\frac{\partial (\mathbf{x}^\top A)_1}{\partial x_1} &
\frac{\partial (\mathbf{x}^\top A)_2}{\partial x_1} &
\cdots &
\frac{\partial (\mathbf{x}^\top A)_n}{\partial x_1} \\
\frac{\partial (\mathbf{x}^\top A)_1}{\partial x_2} &
\frac{\partial (\mathbf{x}^\top A)_2}{\partial x_2} &
\cdots &
\frac{\partial (\mathbf{x}^\top A)_n}{\partial x_2}
\\
\vdots & \vdots & \ddots & \vdots
\\
\frac{\partial (\mathbf{x}^\top A)_1}{\partial x_n} &
\frac{\partial (\mathbf{x}^\top A)_2}{\partial x_n} &
\cdots &
\frac{\partial (\mathbf{x}^\top A)_n}{\partial x_n}
\end{pmatrix}
}

です。

 \displaystyle{
\mathbf{x}^ \top A
} の第 \displaystyle{
k
} 成分(右側の列ベクトル方向)

\displaystyle{
(\mathbf{x}^\top A)_k = \sum_{i=1}^n x_i A_{ik}
}

に対する、\displaystyle{
x _ j
} についての偏微分は、

\displaystyle{
\frac{\partial}{\partial x_j} (\mathbf{x}^\top A)_k
= A_{jk}
}

となります。

 これを行列にまとめると、

\displaystyle{
\frac{\partial(\mathbf{x}^\top A)}{\partial \mathbf{x}}
=
A^\top
}

となります。

\displaystyle{
\frac{\partial}{\partial \mathbf{x}} \left[\mathbf{a}^\top B\mathbf{x}\right]
\;=\;
B^\top \mathbf{a}
}

 成分表示で確認してみます。まず、\displaystyle{
\mathbf{a}^ \top B\mathbf{x}
} を行×列の積として展開します。

\displaystyle{
\begin{aligned}
\mathbf{a}^\top B\mathbf{x}
&=
\begin{pmatrix}
a_1 & a_2 & \cdots & a_n
\end{pmatrix}
\begin{pmatrix}
B_{11} & B_{12} & \cdots & B_{1n} \\
B_{21} & B_{22} & \cdots & B_{2n} \\
\vdots & \vdots &        & \vdots \\
B_{n1} & B_{n2} & \cdots & B_{nn}
\end{pmatrix}
\begin{pmatrix}
x_1\\ x_2\\ \vdots\\ x_n
\end{pmatrix}
\\
&= \sum_{i=1}^n\sum_{j=1}^n a_i B_{ij} x_j
\end{aligned}
}

これを \displaystyle{
x _ k
}微分すると、

\displaystyle{
\frac{\partial}{\partial x_k}
\left(\sum_{i=1}^n\sum_{j=1}^n a_i B_{ij} x_j\right)
= \sum_{i=1}^n a_i B_{ik}
}

これは \displaystyle{
(B^ \top\mathbf{a}) _ k
} に等しいため、

\displaystyle{
\frac{\partial}{\partial \mathbf{x}}
\left(\mathbf{a}^\top B\mathbf{x}\right)
=
B^\top \mathbf{a}
}

となります。

2. おわりに:覚えようとせず、見たことのある形にしておく

 行列微分について最後に強調したいのは、公式を暗記する必要はまったくないということです。忘れたら、インターネットで検索すればすぐに出てきますし、ChatGPT や Gemini に聞けば一瞬で式は再現できます。

 今回の記事で扱った一次形式や二次形式の微分は、最適化や統計モデルの中で何度も登場する基本的な形ですが、だからといって、丸暗記しておくべき公式ではありません。むしろ大切なのは、それぞれの式がどういう仕組みで出てきているのかを、一度だけでよいので丁寧に眺めておくことです。計算過程を一度でも追っておけば、「覚えよう」と意識しなくても、実務で似た形の式に出会ったときに自然と思い出せます。おそらく、ね。

※ もし記事の中で「ここ違うよ」という点や気になるところがあれば、気軽に指摘していただけると助かります。質問や「このテーマも取り上げてほしい」といったリクエストも大歓迎です。必ず対応するとは約束できませんが、できるだけ今後の記事で扱いたいと思います。それと、下のはてなブログランキングはあまり信用できる指標ではなさそうですが (私のブログを読んでいる人は、実際とても少ないです)、押してもらえるとシンプルに励みになります。気が向いたときにポチッとしていただけたら嬉しいです。