モーメント

# 初めに
Just a moment, but what is actually a moment? ．．．なんてね.

# Methods
## 期待値（Expectation），分散（Variance）
連続型だけ考える．

期待値，分散を
```math
\begin{align}
    E \ [ X ]
    &= \int_{-\infty}^{\infty} x p \left( x \right) \, dx
    \\
    V \ [ X ]
    &= \int_{-\infty}^{\infty} \left( x - \mu \right)^2 p \left( x \right) \, dx
\end{align}
```
と定めると，
```math
\begin{align}
    V \ [ X ]
    &= \int_{-\infty}^{\infty} \left( x - \mu \right)^2 p \left( x \right) \, dx
    \\
    &= \int_{-\infty}^{\infty} \left( x^2 - 2 \mu x + \mu^2 \right) p \left( x \right) \, dx
    \\
    &= \int_{-\infty}^{\infty} x^2 p \left( x \right) \, dx
        - 2 \mu \int_{-\infty}^{\infty} x p \left( x \right) \, dx
        + \mu^2 \int_{-\infty}^{\infty} p \left( x \right) \, dx
    \\
    &= E \ [ X^2 ]
        - 2 \mu E \ [ X ]
        + \mu^2
    \\
    &= E \ [ X^2 ] - \left( E \ [ X ] \right)^2
        \quad \left( \because \mu = E \ [ X ] \right)
\end{align}
```
が成り立つ．

## Moment (raw/central)
ところで，
```math
\begin{align}
    E \ [ X ]
    &= \int_{-\infty}^{\infty} x p \left( x \right) \, dx
    \\
    V \ [ X ]
    &= \int_{-\infty}^{\infty} \left( x - \mu \right)^2 p \left( x \right) \, dx
\end{align}
```
を見つめると，
```math
\begin{align}
    E \ [ X ]
    &= E \ [ \left( X - 0 \right)^1 ]
    \\
    V \ [ X ]
    &= E \ [ \left( X - \mu \right)^2 ]
\end{align}
```
と書いても差し支えないことに気付く．ここで，
```math
\begin{align}
    m_{0}^{(n)}
    &= E \ [ \left( X - 0 \right)^{n} ]
    \\
    m_{\mu}^{(n)}
    &= E \ [ \left( X - \mu \right)^{n} ]
\end{align}
```
を導入し，それぞれ，

* $m_{0}^{(n)}$: 原点周りの$n$次モーメント（$n$-th raw moment）
* $m_{\mu}^{(n)}$: 期待値周りの$n$次モーメント（$n$-th central moment）

と呼ぶ．これに従えば，

* 期待値: 原点周りの1次モーメント
* 分散　: 期待値周りの2次モーメント

である．

## 歪み度（Skewness），尖り度（Kurtosis）
特に，
```math
\begin{align}
    S \ [ X ]
    &= \frac{E \ [ \left( X - \mu \right)^3 ]}{\sigma^3}
     = \frac{m_{\mu}^{(3)}}{\sigma^3}
    \\
    K \ [ X ]
    &= \frac{E \ [ \left( X - \mu \right)^4 ]}{\sigma^4}
     = \frac{m_{\mu}^{(4)}}{\sigma^4}
\end{align}
```
を，歪み度，尖り度と呼ぶ（本当は，歪度（わいど），尖度（せんど）と呼ぶのだけれど．．．音からイメージするのが難しいので，暫くの間はこう呼んでおく）．

## Moment-generating function
$t \in \mathbb{R}$に対して，
```math
\begin{align}
    M_{X} \left( t \right)
    &= E \ [ e^{t X} ]
    \\
    &= \int_{-\infty}^{\infty} e^{t x} p \left( x \right) \, dx
\end{align}
```
を，$X$のMoment母関数（Moment-generating function）と呼ぶ．原点周りで多項式展開すると，
```math
\begin{align}
    M_{X} \left( t \right)
    &= \int_{-\infty}^{\infty} e^{t x} p \left( x \right) \, dx
    \\
    &= \int_{-\infty}^{\infty}
        \left(
            1 + t x + \frac{(t x)^2}{2!} + \frac{(t x)^3}{3!} + \cdots
        \right)
         p \left( x \right)
    \, dx
    \\
    &= \int_{-\infty}^{\infty} p \left( x \right) \, dx
        + t              \int_{-\infty}^{\infty} x   p \left( x \right) \, dx
        + \frac{t^2}{2!} \int_{-\infty}^{\infty} x^2 p \left( x \right) \, dx
        + \cdots
    \\
    &= 1
        + t              E \ [ X   ]
        + \frac{t^2}{2!} E \ [ X^2 ]
        + \cdots
    \\
    &= 1
        + t              m_{0}^{(1)}
        + \frac{t^2}{2!} m_{0}^{(2)}
        + \cdots
\end{align}
```
より，
```math
\begin{align}
    M_{X} \left( t=0 \right)
    &= 1
    \\
    \frac{d}{d t} M_{X} \left( t=0 \right)
    &= m_{0}^{(1)}
    \\
    \frac{d^2}{d t^2} M_{X} \left( t=0 \right)
    &= m_{0}^{(2)}
    \\
    \vdots
    \\
    \frac{d^n}{d t^n} M_{X} \left( t=0 \right)
    &= m_{0}^{(n)}
     = E \ [ X^{n} ]
\end{align}
```
として，確かにモーメントたちが生まれている．

# Results
## 色々な分布から
$X \sim \mathcal{N} \ (\mu=0, \sigma^2=1), \ \sim \mathcal{t} \ (\nu=5), \ \Gamma \ (k=2, \theta=1), \ \mathcal{L} \ (\mu=0, b=1)$

![fig.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2529612/2158426c-cb69-4d1f-a9ac-3a661d78ba4d.png)

歪み，尖りが確認できる．

# 終わりに
あれ，重み付き残差法でも，冪乗でテストする方針を，method of momentsと呼んでいたっけ．

そう言えば，構造力学にも，断面1次・2次モーメントという概念がある．

うーん，みんな，冪を積分する形をmomentと呼んでいる．何か背景があるのかな．

# Appendix
```python
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import seaborn as sns

################################################################################

plt.style.use("default")
plt.style.use("classic")
plt.rcParams["font.family"] = "STIXGeneral"
plt.rcParams["mathtext.fontset"] = "cm"
plt.rcParams["figure.figsize"] = (7, 5)
plt.rcParams["figure.autolayout"] = True
plt.rcParams["axes.grid"] = True
plt.rcParams["axes.axisbelow"] = True
plt.rcParams["grid.alpha"] = .3
plt.rcParams["legend.framealpha"] = .8
plt.rcParams["legend.facecolor"] = "w"
plt.rcParams["savefig.dpi"] = 300

################################################################################

def compute_moments(data, name, about=None):
    if about==0. or about=="zero" or about=="origin" or about=="raw":
        # print("\nraw moments")
        m1 = sp.stats.moment(data, order=1, center=0.)
        m2 = sp.stats.moment(data, order=2, center=0.)
        m3 = sp.stats.moment(data, order=3, center=0.)
        m4 = sp.stats.moment(data, order=4, center=0.)
    elif about==None or about=="mean" or about=="center" or about=="central":
        # print("\ncentral moments")
        m1 = sp.stats.moment(data, order=1, center=None)
        m2 = sp.stats.moment(data, order=2, center=None)
        m3 = sp.stats.moment(data, order=3, center=None)
        m4 = sp.stats.moment(data, order=4, center=None)
    else:
        raise ValueError(f"about {about} is not a valid argument")

    print(f"\n--- {name} ---")
    print(f"1st moment: {m1:.6f}")
    print(f"2nd moment: {m2:.6f}")
    print(f"3rd moment: {m3:.6f}")
    print(f"4th moment: {m4:.6f}")

    return m1, m2, m3, m4

################################################################################

size = 1000
data_gauss = np.random.normal(loc=0., scale=1., size=size)
data_student = np.random.standard_t(df=5., size=size)
data_gamma = np.random.gamma(shape=2., scale=1., size=size)
data_laplace = np.random.laplace(loc=0., scale=1., size=size)

data = [data_gauss, data_student, data_gamma, data_laplace]
names = ["Gaussian", "Student's t", "Gamma", "Laplace"]
colors = ["C0", "C1", "C2", "C3"]

bins = 32
density = True
alpha = .8
nrows, ncols = 1, 4
fig, ax = plt.subplots(nrows, ncols, figsize=(20, 5))

for i, (data, name, color) in enumerate(zip(data, names, colors)):
    ax[i].hist(data, bins=bins, density=density, alpha=alpha, color=color, label=name)
    sns.kdeplot(data, ax=ax[i], color=color, label=f"{name}: KDE", fill=False, linestyle="--", linewidth=5)

    m1, m2, m3, m4 = compute_moments(data, name, about="raw")

    ax[i].legend()
    ax[i].set(
        xlabel="Value",
        ylabel="Density",
        xlim=(-10., 10.),
        ylim=(0., .5),
        title=rf"$m_{{0}}^{{(1)}}: {m1:.2f}, m_{{0}}^{{(2)}}: {m2:.2f}, m_{{0}}^{{(3)}}: {m3:.2f}, m_{{0}}^{{(4)}}: {m4:.2f}$",
        # title=rf"$m_{{\mu}}^{{(1)}}: {m1:.2f}, m_{{\mu}}^{{(2)}}: {m2:.2f}, m_{{\mu}}^{{(3)}}: {m3:.2f}, m_{{\mu}}^{{(4)}}: {m4:.2f}$",
    )
fig.tight_layout()
fig.savefig("./fig.png")
plt.show()
```