関数generate_gaussian2D_random_value マニュアル

(The documentation of function generate_gaussian2D_random_value)

Last Update: 2025/8/7


◆機能・用途(Purpose)

頻度分布が2次元ガウス分布となる2次元空間内の乱数(の1つの値)を生成する。
Generate a single random value in a 2D space that obeys a 2D Gaussian frequency distribution.


◆形式(Format)

#include <random.h>
inline double ∗generate_gaussian2D_random_value
(const double mu_x,const double mu_y,
 const double sigma_1,const double sigma_2,const double theta)


◆引数(Arguments)

mu_x ガウス分布の平均値の\(x\)成分\(\mu_x\)。
The \(x\)-component, \(\mu_x\), of the average of the Gaussian distribution.
mu_y ガウス分布の平均値の\(y\)成分\(\mu_y\)。
The \(y\)-component, \(\mu_y\), of the average of the Gaussian distribution.
sigma_1 ガウス分布の共分散行列の最大固有値の平方根\(\sigma_1(> 0)\)。
The square root of the largest principal value, \(\sigma_1 (> 0)\), of the covariance matrix of the Gaussian distribution.
sigma_2 ガウス分布の共分散行列の最小固有値の平方根\(\sigma_2(> 0)\)。
The square root of the smallest principal value, \(\sigma_2 (> 0)\), of the covariance matrix of the Gaussian distribution.
theta \(\sigma_1\)に対応する固有ベクトルの方向\(\theta\) (\(x\)軸から反時計回り、\(^{\circ}\)単位)。
The direction, \(\theta\), of the principal vector corresponding to \(\sigma_1\), measured counterclockwise from the \(x\)-axis in degrees.


◆戻り値(Return value)

生成した乱数(\(x\), \(y\)の値を並べた配列)。 出現頻度がガウス分布((\ref{eq.distribution})式)になるように生成する。
The random values generated (an array composed of \(x\) and \(y\) values); the frequency distribution of the value is given by the Gaussian distribution (eq. \ref{eq.distribution}).


◆使用例(Example)

srand(time(NULL));
for(n=1;n<=100;n++){
   double ∗random =generate_gaussian2D_random_value(3.0,-4.0,2.0,1.0,30.0);
}


◆アルゴリズム(The algorithm)

2次元ガウス分布 \[\begin{equation} f(\posx)=\frac{1}{2\pi\sigma_1\sigma_2} \exp\left[-\frac{1}{2}(\posx-\myvector{\mu})^T \Sigma^{-1} (\posx-\myvector{\mu})\right] \label{eq.distribution} \end{equation}\] を考える。ここで \[\begin{equation} \posx= \begin{pmatrix} x \\ y \end{pmatrix} \label{eq.x} \end{equation}\] は座標を表し、 \[\begin{equation} \myvector{\mu}= \begin{pmatrix} \mu_x \\ \mu_y \end{pmatrix} \label{eq.mu} \end{equation}\] はガウス分布の平均値ベクトル、 \[\begin{equation} \myvector{\Sigma}= \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} \sigma_1^2 & 0 \\ 0 & \sigma_2^2 \end{pmatrix} \begin{pmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{pmatrix} \label{eq.Sigma} \end{equation}\] はガウス分布の共分散行列(\(\sigma_1 \geq \sigma_2 > 0\))とする。
Let us consider a 2D Gaussian distribution (eq. \ref{eq.distribution}), where \(\posx\) (eq. \ref{eq.x}), \(\myvector{\mu}\) (eq. \ref{eq.mu}), and \(\myvector{\Sigma}\) (eq. \ref{eq.Sigma}) are coordinates, the average value vector of the Gaussian distribution, and the covariance matrix of the distribution (\(\sigma_1 \geq \sigma_2 > 0\)), respectively.

\[\begin{equation} \myvector{\Sigma}^{-1}= \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} 1/\sigma_1^2 & 0 \\ 0 & 1/\sigma_2^2 \end{pmatrix} \begin{pmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{pmatrix} \label{eq.Sigma_inv} \end{equation}\] であるので、 \[\begin{equation} \begin{pmatrix} x’ \\ y’ \end{pmatrix} = \begin{pmatrix} \cos\theta & \sin\theta \\ -\sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} x-\mu_x \\ y-\mu_y \end{pmatrix} \label{eq.xdash_ydash} \end{equation}\] と座標変換すると \[\begin{eqnarray} f(\posx) &=& \frac{1}{2\pi\sigma_1\sigma_2} \exp\left[-\frac{1}{2} \begin{pmatrix} x’ & y’ \end{pmatrix} \begin{pmatrix} 1/\sigma_1^2 & 0 \\ 0 & 1/\sigma_2^2 \end{pmatrix} \begin{pmatrix} x’ \\ y’ \end{pmatrix} \right] \nonumber \\ &=& \frac{1}{2\pi\sigma_1\sigma_2} \exp\left[-\frac{1}{2} \left(\frac{{x’}^2}{\sigma_1^2} +\frac{{y’}^2}{\sigma_2^2}\right)\right] \label{eq.distribution.xdash_ydash} \end{eqnarray}\] が得られる。ここで更に \[\begin{equation} x’=r\sigma_1\cos\phi, \hspace{1em} y’=r\sigma_2\sin\phi \label{eq.r_phi} \end{equation}\] と変数変換すると \[\begin{equation} f(\posx)=\frac{1}{2\pi\sigma_1\sigma_2} \exp\left(-\frac{r^2}{2}\right) \label{eq.distribution.r_phi} \end{equation}\] となる。またヤコビアンは \[\begin{eqnarray} \begin{vmatrix} \PartialDiff{x’}{r} & \PartialDiff{x’}{\phi} \\ \PartialDiff{y’}{r} & \PartialDiff{y’}{\phi} \end{vmatrix} &=& \begin{vmatrix} \sigma_1\cos\phi & -r\sigma_1\sin\phi \\ \sigma_2\sin\phi & r\sigma_2\cos\phi \end{vmatrix} \nonumber \\ &=& r\sigma_1\sigma_2\cos^2\phi+r\sigma_1\sigma_2\sin^2\phi \nonumber \\ &=& r\sigma_1\sigma_2 \label{eq.Jacobian} \end{eqnarray}\] であるので、\(dr\), \(d\phi\)を微少量として \(\posx\)が\(r_0-dr/2 < r < r_0+dr/2\), \(\phi_0-d\phi/2 < \phi < \phi_0+d\phi/2\) を満たす範囲に入る確率は \[\begin{equation} p(r_0,\phi_0) =f(\posx)r\sigma_1\sigma_2 dr d\phi =\frac{r dr d\phi}{2\pi}\exp\left(-\frac{r^2}{2}\right) \label{eq.probability_density} \end{equation}\] と書ける。
Since the inverse of \(\myvector{\Sigma}\) is given by eq. (\ref{eq.Sigma_inv}), the distribution can be rewritten as (\ref{eq.distribution.xdash_ydash}) using a coordinate conversion defined by eq. (\ref{eq.xdash_ydash}). We further convert the variables as (\ref{eq.r_phi}), resulting in (\ref{eq.distribution.r_phi}). The Jacobian for this conversion is given by (\ref{eq.Jacobian}). The probability that \(\posx\) falls into a range \(r_0-dr/2 < r < r_0+dr/2\) and \(\phi_0-d\phi/2 < \phi < \phi_0+d\phi/2\), where \(dr\) and \(d\phi\) are infinitely small quantities, is thus (\ref{eq.probability_density}).

ガウス分布(\ref{eq.distribution})に従う乱数を生成するには まず(\ref{eq.probability_density})式の確率に従って \((r_0,\phi_0)\)の組を生成する。 \(r_0\)については\(r\leq r_0\)となる確率が \[\begin{eqnarray} P(r_0) &=& \iint_{0\leq r \leq r_0, 0\leq\phi < 2\pi} \frac{r dr d\phi}{2\pi}\exp\left(-\frac{r^2}{2}\right) \nonumber \\ &=& \frac{1}{2\pi}\int_0^{r_0}dr\int_0^{2\pi}d\phi r\exp\left(-\frac{r^2}{2}\right) \nonumber \\ &=& \int_0^{r_0}dr r\exp\left(-\frac{r^2}{2}\right) \nonumber \\ &=& \left[-\exp\left(-\frac{r^2}{2}\right)\right]_0^{r_0} \nonumber \\ &=& 1-\exp\left(-\frac{r_0^2}{2}\right) \label{eq.Pr0} \end{eqnarray}\] であることを用いて、まず[0,1]の範囲の一様乱数を生成した上で その値を\(P(r_0)\)として(\ref{eq.Pr0})式を\(r_0\)について解けば良い。 その解は \[\begin{equation} r_0=\sqrt{-2\ln\left[1-P(r_0)\right]} \label{eq.r0} \end{equation}\] である。 \(\phi_0\)については\([0,2\pi]\)の範囲の一様乱数を (\(r_0\)の生成に用いた乱数とは独立に)生成してその値をそのまま用いれば良い。 得られた\(r_0\), \(\phi_0\)を用いて (\ref{eq.r_phi})式により\(x’\), \(y’\)を求め、 最後にそれらを(\ref{eq.xdash_ydash})式に代入すれば\(x\), \(y\)が得られる。
To generate a random value based on eq. (\ref{eq.distribution}), first generate a set of \((r_0,\phi_0)\) at a probability given by eq. (\ref{eq.probability_density}). Here, \(r_0\) can be generated by creating a uniform random value in [0,1], using the result as \(P(r_0)\), and solving eq. (\ref{eq.Pr0}) with respect to \(r_0\), the solution of which is (\ref{eq.r0}). For \(\phi_0\), a uniform random value in \([0,2\pi]\) independent of the random value used to generate (\(r_0\) can directly be used. Using the \(r_0\) and \(\phi_0\) values, \(x’\) and \(y’\) are calculated with eq. (\ref{eq.r_phi}), and then finally \(x\) are \(y\) computed using eq. (\ref{eq.xdash_ydash}).


◆検証(Validation)

この関数で生成するのは乱数であるので厳密な検証は難しいが、この関数を用いて \(\mu_x=3\), \(\mu_x=-4\), \(\sigma_1=2\), \(\sigma_2=1\), \(\theta=30^{\circ}\) の場合の乱数を1万個作成してプロットしてみたところ 妥当と思われる分布が得られた。 下図において青点は生成した乱数、 黒線は\(\sigma_1\), \(\sigma_2\)の範囲を示す。
Although a strict validation of this function is difficult since this function generates random values, 10000 random values generated by this function for \(\mu_x=3\), \(\mu_x=-4\), \(\sigma_1=2\), \(\sigma_2=1\), \(\theta=30^{\circ}\) showed a reasonable distribution; the blue dots in the figure below represent the random values, and the black lines represent \(\sigma_1\) and \(\sigma_2\) ranges.