<jupyter_start><jupyter_text>Preparing `fig:figure__cplx-gaussian` [Campese (2015)](http://arxiv.org/abs/1511.00547) is a good reference for $\mathbb{C}$-related stuff: Gaussian and Wirtinger derivatives.<jupyter_code>import numpy as np

%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>A $z \in \mathbb{C}^m$ is a $\mathbb{C}$ gaussian vector iff$$  \label{eq:cn-paired-real-density}\begin{pmatrix}  \Re z \\ \Im z\end{pmatrix}  \sim \mathcal{N}_{2 m}\biggl(    \begin{pmatrix}      \Re \mu \\ \Im \mu    \end{pmatrix},    \frac12      \begin{pmatrix}        \Re{(\Gamma + C)} & \Im{(C - \Gamma)} \\        \Im{(\Gamma + C)} & \Re{(\Gamma - C)}      \end{pmatrix}  \biggr)  \,,$$with $\mu \in \mathbb{C}^m$, $\Gamma, C \in \mathbb{C}^{m\times m}$,$\Gamma \succeq 0$, $\Gamma = \overline{\Gamma}^\top$, $C = C^\top$ and $  \overline{\Gamma} \succeq \overline{C}^\top \Gamma^{-1} C$. A univariate complex Gaussian $    z\sim \mathcal{CN}(        z \mid \mu, \sigma^2, \gamma    )$ iff$$  % \label{eq:cn-paired-real-density}\begin{pmatrix}  \Re z \\ \Im z\end{pmatrix}  \sim \mathcal{N}_{2}\biggl(    \begin{pmatrix}      \Re \mu \\ \Im \mu    \end{pmatrix},    \frac12      \begin{pmatrix}        \sigma^2 + \Re{\gamma} & \Im{\gamma} \\        \Im{\gamma} & \sigma^2 - \Re{\gamma}      \end{pmatrix}  \biggr)  \,,$$with $\mu, \gamma \in \mathbb{C}$ and $  \sigma^2 \geq \lvert \gamma \rvert$. Let $\gamma = \sigma^2 \eta e^{i \phi}$, with $\lvert \eta \rvert \leq 1$.Then $    z\sim \mathcal{CN}(z\mid\mu, \sigma^2, \gamma)$ is equivalent to$$  % \label{eq:cn-paired-real-density}\begin{pmatrix}  \Re z \\ \Im z\end{pmatrix}  \sim \mathcal{N}_{2}\biggl(    \begin{pmatrix}      \Re \mu \\ \Im \mu    \end{pmatrix},    \frac{\sigma^2}2      \begin{pmatrix}        1 + \eta \cos\phi & \eta \sin\phi \\        \eta \sin\phi & 1 - \eta \cos\phi      \end{pmatrix}  \biggr)  \,.$$ Consdier a standard zero-mean $\mathbb{C}$-Gaussian rv ($\mu=0$, $\sigma^2=1$,$\eta\in [-1, 1]$ and $\phi \in [-\pi, +\pi]$). Then $    \mathcal{CN}(0, 1, \eta e^{i\phi})$ is equivalent in distribution to a Gaussain $\mathbb{R}^2$ rv with density:$$  % \label{eq:cn-paired-real-density}\mathcal{N}_{2}\biggl(    0,    \frac12      \begin{pmatrix}        1 + \eta \cos\phi & \eta \sin\phi \\        \eta \sin\phi & 1 - \eta \cos\phi      \end{pmatrix}  \biggr)\,.$$<jupyter_code>def cov(eta, phi):
    ''' Cov matrix for geometric eqivalent of univariate complex gaissian rv.'''
    s, c = np.sin(phi), np.cos(phi)
    return 0.5 * (np.eye(2) + eta * np.c_[[c,  s], [s, -c]])<jupyter_output><empty_output><jupyter_text>A Cholesky factor of a $\mathbb{R}$ $2\times 2$ matrix $    M = \begin{pmatrix} a & b \\ c & d\end{pmatrix}$ is$$\sqrt{M}  = \frac1{t} \begin{pmatrix}    a + s & b \\ c & d + s  \end{pmatrix}  \,, $$for $s = \sqrt{ad - bc}$ and $t = \sqrt{a + d + 2 s}$<jupyter_code>def sqrt_cov(eta, phi):
    '''Closed form for R with M = RR. R is symmetric whenever M is.'''
    s, c = np.sin(phi), np.cos(phi)
    # emm = 1/2 * np.c_[[1 + eta * c,  eta * s], [eta * s, 1 - eta * c]]

    ss = np.sqrt(1 - eta * eta)
    tt = np.sqrt(1 + ss)
    return 0.5 * np.c_[
        [1 + eta * c + ss, eta * s],
        [eta * s, 1 - eta * c + ss],
    ] / tt<jupyter_output><empty_output><jupyter_text>Verifying:$$\frac1{t^2} \begin{pmatrix}    a + s & b \\ c & d + s  \end{pmatrix} \begin{pmatrix}    a + s & b \\ c & d + s  \end{pmatrix}  = \frac1{t^2} \begin{pmatrix}    (a + s)^2 + b c        & (a + s) b + b (d + s) \\     c (a + s) + (d + s) c        & c b + (d + s)^2  \end{pmatrix}%   = \frac1{a + d + 2 s} \begin{pmatrix}%     (a + s)^2 + b c%         & (a + s) b + b (d + s) \\%      c (a + s) + (d + s) c%         & c b + (d + s)^2%   \end{pmatrix}%   = \frac1{a + d + 2 s} \begin{pmatrix}%     a^2 + 2 a s + s^2 + b c%         & a b + s b + b d + b s \\%      c a + c s + d c + s c%         & c b + d^2 + 2 s d + s^2%   \end{pmatrix}%   = \frac1{a + d + 2 s} \begin{pmatrix}%     a^2 + 2 a s + a d%         & a b + s b + b d + b s \\%      c a + c s + d c + s c%         & d^2 + 2 s d + a d%   \end{pmatrix}  = \frac1{a + d + 2 s} \begin{pmatrix}    a (a + 2 s + d)        & (a + s + d + s) b  \\     (a + s + d + s) c        & (d + 2 s + a) d  \end{pmatrix}\,. $$<jupyter_code>from itertools import product

for eta, phi in product(np.linspace(-1, +1, num=101),
                        np.linspace(-1, +1, num=101) * np.pi):

    C = sqrt_cov(eta, phi)
    assert np.allclose(C @ C, cov(eta, phi))<jupyter_output><empty_output><jupyter_text>A handy standard cplx-gausain generator<jupyter_code>def cplx_stdgauss(*shape, eta=0., phi=0.):
    z = np.random.randn(*shape, 2)
    re_im = z @ sqrt_cov(eta, phi)
    return re_im[..., 0] + 1j * re_im[..., 1]<jupyter_output><empty_output><jupyter_text>A small "grid" of $\eta$ and $\phi$ for illustration<jupyter_code># etas, phis = np.r_[0., +0.5, +1.], np.r_[0., +0.5, +1.] * np.pi
# etas, phis = np.r_[0.25, +0.95, +.99], np.r_[0., np.pi / 6, np.pi / 2]
etas, phis = np.r_[0.25, +0.95, +.99], np.r_[-45, 0, 90]

common = np.random.randn(10000, 2)<jupyter_output><empty_output><jupyter_text>An excessive plot<jupyter_code>xlim, ylim = (-2, +2), (-2, +2)

fig, axes = plt.subplots(3, 3, figsize=(4.5, 4.5), squeeze=False, sharex=True, sharey=True,
                        subplot_kw=dict(xlim=xlim, xticks=[], ylim=ylim, yticks=[]), dpi=300)
fig.patch.set_alpha(1.)

for row, eta in zip(axes, etas):
    row[0].set_ylabel(rf'$\eta \approx {eta:.1f}$')
    for ax, phi in zip(row, phis):
        # z = cplx_stdgauss(10000, eta=eta, phi=phi)
        re_im = common @ sqrt_cov(eta, phi * np.pi / 180)
        z = re_im[..., 0] + 1j * re_im[..., 1]  # + (1j - 1)*.5

        H, _, _ = np.histogram2d(z.real, z.imag, bins=51, range=[xlim, ylim])
        im = ax.imshow(H.T, origin='lower', interpolation=None,
                       extent=[*xlim, *ylim], cmap=plt.cm.hot)

#         ax.hist2d(z.real, z.imag, bins=51, cmap=plt.cm.hot, range=[xlim, ylim])
#         ax.hexbin(z.real, z.imag, gridsize=51, cmap=plt.cm.hot,
#                   extent=(*xlim, *ylim))  # (left, right, bottom, top)

# last row creates x-labels
for ax, phi in zip(row, phis):
    ax.set_xlabel(rf'$\phi \approx {phi:.0f}^\circ$')

# title = r'$\mathbb{C}\mathcal{N}(0, 1, \eta e^{\jmath \phi})$'
# fig.text(0.125, 1.025, title, ha='center', size='xx-large')

plt.tight_layout(pad=-.15, rect=(0.02, 0.02, 0.92, 0.92))
fig.text(0.50, 0.935, 'real', ha='center', size='xx-large')
fig.text(0.93, 0.50, 'imag', va='center', size='xx-large', rotation=270)

plt.savefig('../../assets/figure__cplx_gaussian.pdf', dpi=300)<jupyter_output><empty_output><jupyter_text><jupyter_code>assert False<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21860671498299067, 'fraction_numerical': 0.03372282206774146, 'mean_word_length': 2.4963805584281284, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '189442', 'license': 'MIT', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2800, 'n_words': 700, 'path': 'experiments/extra/figure__cplx-gaussian.ipynb', 'repo_name': 'ivannz/complex_paper', 'size': '6761'}