関数Gaussian_multiDimension マニュアル

(The documentation of function Gaussian_multiDimension)

Last Update: 2021/12/1


◆機能・用途(Purpose)

多次元ガウス分布 \[\begin{equation} f(\posx) =\frac{1}{\sqrt{(2\pi)^D|\myvector{\Sigma}|}} \exp\left[ -\frac{1}{2}(\posx-\myvector{x_{ave}})^T\myvector{\Sigma}^{-1} (\posx-\myvector{x_{ave}}) \right] \label{eq.definition} \end{equation}\] の特定の\(\posx\)における値を求める。 ここで\(D\)は次元、\(\posx\)は\(D\)次元ベクトル、 \(\myvector{x_{ave}}\)はガウス分布の平均値(\(D\)次元ベクトル)、 \(\myvector{\Sigma}\)はガウス分布の共分散行列(サイズ:\(D\times D\))である。
Compute the value of a multi-dimension Gaussian distribution (eq. \ref{eq.definition}) for a certain \(\posx\) value, where \(D\) is the dimension, \(\posx\) is a \(D\)-dimension vector, \(\myvector{x_{ave}}\) is an average of the Gaussian distribution which is also a \(D\)-dimension vector, and \(\myvector{\Sigma}\) is a covariance matrix of the Gaussian distribution which has a size of \(D\times D\).


◆形式(Format)

#include <gaussian.h>
inline double Gaussian_multiDimension
(const struct columnvector average,const struct matrix covariance,
  const struct columnvector x)


◆引数(Arguments)

average ガウス分布の平均値ベクトル\(\myvector{x_{ave}}\)。
The average value vector \(\myvector{x_{ave}}\) of the Gaussian distribution.
covariance ガウス分布の共分散行列\(\myvector{\Sigma}\)。
The covariance matrix \(\myvector{\Sigma}\) of the Gaussian distribution.
x ガウス分布の値を求めたい変数値ベクトル\(\posx\)。
The variable value vector \(\posx\) for which the value of the Gaussian distribution is needed.


◆戻り値(Return value)

(\ref{eq.definition})式に基づいて計算される\(f(\posx)\)の値。
The value of \(f(\posx)\) calculated based on eq. (\ref{eq.definition}).


◆使用例(Example)

struct columnvector x,average;
struct matrix covariance;
double value=Gaussian_multiDimension(average,covariance,x);


◆計算方法(Computation method)

関数LU_decomposition (matrix/inverse.h) を用いて\(\myvector{\Sigma}\)のLU分解を求める。 次にその結果を用いて 関数solution_LU_givenLUdecomposition (matrix/inverse.h) により\(\myvector{\Sigma}^{-1}(\posx-\myvector{x_{ave}})\)を、 関数matrix_determinant_givenLU (matrix/inverse.h) により\(|\myvector{\Sigma}|\)を求める。 これらを用いて(\ref{eq.definition})式により\(f(\posx)\)を計算する。
An LU decomposition of \(\myvector{\Sigma}\) is first computed by function LU_decomposition (matrix/inverse.h). Next, using the decomposition, \(\myvector{\Sigma}^{-1}(\posx-\myvector{x_{ave}})\) and \(|\myvector{\Sigma}|\) are computed by functions solution_LU_givenLUdecomposition and matrix_determinant_givenLU (both in matrix/inverse.h), respectively. Finally, using these quantities, \(f(\posx)\) is calculated by eq. (\ref{eq.definition}).


◆検証(Validation)

この関数で求めた値が正しいことを以下の場合について検証した。
The values obtained by this function are validated for the following cases:


(1)1次元ガウス分布 (A 1-D Gaussian distribution)

平均\(x_{ave}=5\)、標準偏差\(\sigma=2\)の1次元ガウス分布の \(x=8\)における値を計算する。この場合、正解は
\[\begin{eqnarray} f(x) &=& \frac{1}{\sqrt{2\pi}\sigma} \exp\left[-\frac{(x-x_{ave})^2}{2\sigma^2}\right] \nonumber \\ &=& \frac{1}{\sqrt{2\pi}\times 2}\exp\left[-\frac{(8-5)^2}{2\times 2^2}\right] \nonumber \\ &=& \frac{1}{2\sqrt{2\pi}}\exp\left(-\frac{9}{8}\right) \nonumber \\ &\sim& 6.475880\times 10^{-2} \label{eq.validation1.correct_answer} \end{eqnarray}\] である。関数を用いてこれを計算するには \[\begin{equation} \posx= \begin{pmatrix} 8 \end{pmatrix} ,\hspace{1em} \myvector{x_{ave}}= \begin{pmatrix} 5 \end{pmatrix} ,\hspace{1em} \myvector{\Sigma}= \begin{pmatrix} 4 \end{pmatrix} \label{eq.validation1.input} \end{equation}\] とすれば良い。実際にこの検証を行い、結果が6.475880e-02となることを確認した。
Compute the value at \(x=8\) for a 1-D Gaussian distribution with an average \(x_{ave}=5\) and a standard deviation \(\sigma=2\). In this case, the correct answer is given by eq. (\ref{eq.validation1.correct_answer}). To compute the answer by the function, the input should be given by eq. (\ref{eq.validation1.input}). This test yielded 6.475880e-02, indicating that the function worked correctly in this case.


(2)座標軸に平行な2次元ガウス分布 (A 2-D Gaussian distribution parallel to coordinate axes)

\(x\)軸方向のガウス分布と\(y\)軸方向のガウス分布の積 \[\begin{eqnarray} f(x,y) &=& \frac{1}{\sqrt{2\pi}\sigma_x} \exp\left[-\frac{(x-x_{ave})^2}{2\sigma_x^2}\right] \frac{1}{\sqrt{2\pi}\sigma_y} \exp\left[-\frac{(y-y_{ave})^2}{2\sigma_y^2}\right] \nonumber \\ &=& \frac{1}{\sqrt{2\pi}^2\sigma_x\sigma_y} \exp\left[-\frac{1}{2}\left\{ \frac{(x-x_{ave})^2}{\sigma_x^2}+\frac{(y-y_{ave})^2}{\sigma_y^2} \right\}\right] \label{eq.validation2.problem} \end{eqnarray}\] を考える。\(x_{ave}=5\), \(y_{ave}=7\), \(\sigma_x=2\), \(\sigma_y=3\)とし、 \((x,y)=\)(8,7), (5,11), (7,10)の3地点について\(f(x,y)\)を計算する。 この場合、正解は \[\begin{eqnarray} f(8,7) &=& \frac{1}{\sqrt{2\pi}\times 2} \exp\left[-\frac{(8-5)^2}{2\times 2^2}\right] \frac{1}{\sqrt{2\pi}\times 3} \exp\left[-\frac{(7-7)^2}{2\times 3^2}\right] \nonumber \\ &=& \frac{1}{12\pi}\exp\left(-\frac{9}{8}\right) \nonumber \\ &\sim& 8.611674\times 10^{-3} \label{eq.validation2.correct_answer1} \end{eqnarray}\] \[\begin{eqnarray} f(5,11) &=& \frac{1}{\sqrt{2\pi}\times 2} \exp\left[-\frac{(5-5)^2}{2\times 2^2}\right] \frac{1}{\sqrt{2\pi}\times 3} \exp\left[-\frac{(11-7)^2}{2\times 3^2}\right] \nonumber \\ &=& \frac{1}{12\pi}\exp\left(-\frac{16}{18}\right) \nonumber \\ &\sim& 1.090509\times 10^{-2} \label{eq.validation2.correct_answer2} \end{eqnarray}\] \[\begin{eqnarray} f(7,10) &=& \frac{1}{\sqrt{2\pi}\times 2} \exp\left[-\frac{(7-5)^2}{2\times 2^2}\right] \frac{1}{\sqrt{2\pi}\times 3} \exp\left[-\frac{(10-7)^2}{2\times 3^2}\right] \nonumber \\ &=& \frac{1}{12\pi}\exp\left(-\frac{4}{8}\right)\exp\left(-\frac{9}{18}\right) \nonumber \\ &\sim& 9.758305\times 10^{-3} \label{eq.validation2.correct_answer3} \end{eqnarray}\] である。関数を用いてこれらを計算するには \[\begin{equation} \posx= \begin{pmatrix} x \\ y \end{pmatrix} ,\hspace{1em} \myvector{x_{ave}}= \begin{pmatrix} x_{ave} \\ y_{ave} \end{pmatrix} ,\hspace{1em} \myvector{\Sigma}= \begin{pmatrix} \sigma_x^2 & 0 \\ 0 & \sigma_y^2 \end{pmatrix} \label{eq.validation2.input} \end{equation}\] としてこれらに上記の値を代入すれば良い。 実際に関数を用いてこれらを計算し、 \(f(8,7)=\)8.611674e-03, \(f(5,11)=\)1.090509e-02, \(f(7,10)=\)9.758305e-03 となることを確認した。
Consider a product of Gaussian distributions along \(x\)- and \(y\)-axes given by eq. (\ref{eq.validation2.problem}). Compute \(f(x,y)\) for three points \((x,y)=\) (8,7), (5,11), and (7,10) in case of \(x_{ave}=5\), \(y_{ave}=7\), \(\sigma_x=2\), and \(\sigma_y=3\). The correct answers are given by eqs (\ref{eq.validation2.correct_answer1})-(\ref{eq.validation2.correct_answer3}). To compute the Gaussian values by the function, the input should be given by eq. (\ref{eq.validation2.input}). The test using the function yielded \(f(8,7)=\)8.611674e-03, \(f(5,11)=\)1.090509e-02, and \(f(7,10)=\)9.758305e-03, indicating that the function worked correctly in these cases.


(3)座標軸に平行でない2次元ガウス分布 (A 2-D Gaussian distribution not parallel to coordinate axes)

上記(2)で考えたガウス分布と\((x,y)\)を 位置関係を保ったまま丸ごと原点のまわりに反時計回りに角度\(\theta\)だけ 回転させることを考える。回転後のパラメータは回転前の \(x_{ave}\), \(y_{ave}\), \(\sigma_x\), \(\sigma_y\), \(x\), \(y\) を用いて \[\begin{equation} \posx= \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} \label{eq.validation3.input} \end{equation}\] \[\begin{equation} \myvector{x_{ave}}= \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} x_{ave} \\ y_{ave} \end{pmatrix} \label{eq.validation3.input.ave} \end{equation}\] \[\begin{equation} \myvector{\Sigma}= \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} \sigma_x^2 & 0 \\ 0 & \sigma_y^2 \end{pmatrix} \begin{pmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{pmatrix} \label{eq.validation3.input.std} \end{equation}\] と書ける。(2)と全く同じ \(x_{ave}\), \(y_{ave}\), \(\sigma_x\), \(\sigma_y\), \(x\), \(y\) を使用し、\(\theta=30^{\circ}\)の場合についてこれらを計算すると \[\begin{equation} \posx= \begin{pmatrix} 3.4282032 \\ 10.062178 \end{pmatrix} ,\hspace{1em} \begin{pmatrix} -1.1698730 \\ 12.026279 \end{pmatrix} ,\hspace{1em} \begin{pmatrix} 1.0621778 \\ 12.160254 \end{pmatrix} \label{eq.validation3.input.x} \end{equation}\] \[\begin{equation} \myvector{x_{ave}}= \begin{pmatrix} 0.83012702 \\ 8.5621778 \end{pmatrix} \label{eq.validation3.input.x_ave} \end{equation}\] \[\begin{eqnarray} \myvector{\Sigma} &=& \begin{pmatrix} \sigma_x^2\cos\theta & -\sigma_y^2\sin\theta \\ \sigma_x^2\sin\theta & \sigma_y^2\cos\theta \end{pmatrix} \begin{pmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{pmatrix} \nonumber \\ &=& \begin{pmatrix} \sigma_x^2\cos^2\theta+\sigma_y^2\sin^2\theta & (\sigma_x^2-\sigma_y^2)\sin\theta\cos\theta \\ (\sigma_x^2-\sigma_y^2)\sin\theta\cos\theta & \sigma_x^2\sin^2\theta+\sigma_y^2\cos^2\theta \end{pmatrix} \nonumber \\ &=& \begin{pmatrix} 5.25 & -2.1650635 \\ -2.1650635 & 7.75 \end{pmatrix} \label{eq.validation3.input.Sigma} \end{eqnarray}\] となる。 単に全体を回転させただけなのでガウス分布の値は(2)と変わらないはずである。 そこで関数に(\ref{eq.validation3.input.x_ave})式の\(\myvector{x_{ave}}\)と (\ref{eq.validation3.input.Sigma})式の\(\myvector{\Sigma}\)を代入し、 (\ref{eq.validation3.input.x})式の3つの\(\posx\)について ガウス分布の値を計算してみた。 その結果、\(f(3.4282032,10.062178)=\)8.611674e-03, \(f(-1.1698730,12.026279)=\)1.090509e-02, \(f(1.0621778,12.160254)=\)9.758305e-03 となって(2)と同じ値が得られた。
Let us consider to rotate the entire system of the Gaussian distribution and \((x,y)\) considered in (2) above by angle \(\theta\) counterclockwise about the coordinate origin. The parameters after the rotation are given by eqs. (\ref{eq.validation3.input})-(\ref{eq.validation3.input.std}), where \(x_{ave}\), \(y_{ave}\), \(\sigma_x\), \(\sigma_y\), \(x\), and \(y\) represent the quantities before the rotation. Using exactly the same values of \(x_{ave}\), \(y_{ave}\), \(\sigma_x\), \(\sigma_y\), \(x\), and \(y\) as in (2), the parameters after the rotation in case of \(\theta=30^{\circ}\) are calculated as (\ref{eq.validation3.input.x})-(\ref{eq.validation3.input.Sigma}). The values of the Gaussian distribution should be same as those in (2) because no change other than the rotation of the entire system was applied. A test using \(\myvector{x_{ave}}\) in eq. (\ref{eq.validation3.input.x_ave}) and \(\myvector{\Sigma}\) in eq. (\ref{eq.validation3.input.Sigma}) yielded \(f(3.4282032,10.062178)=\)8.611674e-03, \(f(-1.1698730,12.026279)=\)1.090509e-02, and \(f(1.0621778,12.160254)=\)9.758305e-03, which were all same as the results in (2), indicating that the function worked correctly in this case.