Traffine I/O

日本語

2022-12-16

確率分布の歪度と尖度

確率分布の歪度(Skewness)

確率分布の歪度(Skewness)は分布の歪み具合(非対称の度合い)を表す指標です。歪度により分布の歪みが変化します。

  • skewness < 0
    分布が右に歪む
  • skewness = 0
    分布が左右対称
  • skewness > 0
    分布が左に歪む

下図は正規分布の歪度を変化させた標準正規分布を示しています。

Skewness

正規分布の場合、歪度は以下の式で求めることができます。

skewness = \frac{n}{(n-1)(n-2)} \sum^n_{i=1} ({\frac{x_i - \bar{x}}{s}}^3)

確率分布の尖度(Kurtosis)

確率分布の尖度(Kurtosis)は正規分布を基準とした分布の尖り具合や裾の広がり具合を表す指標です。正規分布よりも尖った分布では尖度は正の値、正規分布よりも緩やかな分布では尖度は負の値になります。

下図は尖度2.228のラプラス分布、尖度0.045の正規分布、尖度-1.161の一様分布を示しています。

Kurtosis

正規分布の場合、尖度は以下の式で求めることができます。

kurtosis = \frac{n(n+1)}{(n-1)(n-2)(n-3)} \sum^n_{i=1} \frac{(x_i - \bar{x})^4}{s^4} - \frac{3(n-1)^2}{(n-2)(n-3)}

Python コード

今回の確率分布の歪度と尖度を描画したPythonコードは以下になります。

from scipy.stats import skewnorm
import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')
fig, ax = plt.subplots(facecolor="w", figsize=(10, 10))

skews = [-4, 0, 4]

for i, skew in enumerate(skews):
    plt.subplot(3, 1, i+1)
    x = np.linspace(skewnorm.ppf(0.01, skew),
                    skewnorm.ppf(0.99, skew), 100)
    plt.plot(x, skewnorm.pdf(x, skew), lw=5, alpha=0.5, label=f'norm dist (skew={skew})')
    r = skewnorm.rvs(skew, size=1000)
    plt.hist(r, density=True, histtype='stepfilled', alpha=0.2)
    plt.legend(loc='best', frameon=False)
plt.show()

Skewness

import matplotlib.pyplot as plt
import scipy.stats as stats
from scipy.stats import kurtosis

plt.style.use('ggplot')
fig, ax = plt.subplots(facecolor="w", figsize=(10, 5))

x = np.linspace(-5, 5, 100)
ax = plt.subplot()
distnames = ['laplace', 'norm', 'uniform']

for distname in distnames:
    if distname == 'uniform':
        dist = getattr(stats, distname)(loc=-2, scale=4)
    else:
        dist = getattr(stats, distname)
    data = dist.rvs(size=1000)
    kur = kurtosis(data, fisher=True)
    y = dist.pdf(x)
    ax.plot(x, y, lw=5, alpha=0.5, label="{} dist (kurtosis={})".format(distname, round(kur, 3)))
    ax.legend()

Kurtosis

参考

https://www.kaggle.com/code/sandhyakrishnan02/normal-distribution-skewness-and-kurtosis/notebook#Kurtosis
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skewnorm.html
https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kurtosis.html

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!