四分位数・パーセンタイル#

boxplotの外れ値の定義はなぜ「四分位範囲×1.5」なのか?#

boxplotとともに提案されたらしい。

正規分布における2.7σと概ね近似するらしい

Hide code cell source

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st

# 標準正規分布データ
rng = np.random.default_rng(0)
data = rng.normal(0, 1, 100_000)

# 四分位数とIQR
q1, q3 = np.percentile(data, [25, 75])
iqr = q3 - q1
lower = q1 - 1.5 * iqr
upper = q3 + 1.5 * iqr

# 各範囲
limits = {
    "95%範囲 (±1.96σ)": (-1.96, 1.96),
    "99%範囲 (±2.58σ)": (-2.58, 2.58),
    "Tukey閾値 (±1.5IQR≃±2.70σ)": (lower, upper),
}

# 確率密度
x = np.linspace(-4, 4, 500)
pdf = st.norm.pdf(x)

# 図を描画
plt.figure(figsize=(9, 5))
plt.plot(x, pdf, 'k-', lw=2, label="標準正規分布 N(0,1)")
plt.hist(data, bins=80, density=True, alpha=0.3, label="ヒストグラム")

# 範囲線の描画
colors = {"95%範囲 (±1.96σ)": "green", "99%範囲 (±2.58σ)": "orange", "Tukey閾値 (±1.5IQR≃±2.70σ)": "red"}

for label, (lo, hi) in limits.items():
    plt.axvline(lo, color=colors[label], linestyle='--', lw=2)
    plt.axvline(hi, color=colors[label], linestyle='--', lw=2, label=label)
    plt.fill_between(x, 0, pdf, where=(x >= lo) & (x <= hi), color=colors[label], alpha=0.1)

# 四分位数線
plt.axvline(q1, color='blue', linestyle=':', lw=1.5, label=f"Q1 = {q1:.2f}")
plt.axvline(q3, color='blue', linestyle=':', lw=1.5, label=f"Q3 = {q3:.2f}")

plt.title("標準正規分布における外れ値閾値と信頼範囲の比較")
plt.xlabel("Z")
plt.ylabel("確率密度")
plt.legend(loc="upper left", fontsize=9)
plt.tight_layout()
plt.show()

# 数値表示
print(f"Q1 = {q1:.3f}, Q3 = {q3:.3f}, IQR = {iqr:.3f}")
print(f"Tukey下限 = {lower:.3f}, 上限 = {upper:.3f}")
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 30906 (\N{CJK UNIFIED IDEOGRAPH-78BA}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 23494 (\N{CJK UNIFIED IDEOGRAPH-5BC6}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 27161 (\N{CJK UNIFIED IDEOGRAPH-6A19}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 28310 (\N{CJK UNIFIED IDEOGRAPH-6E96}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 35215 (\N{CJK UNIFIED IDEOGRAPH-898F}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 24067 (\N{CJK UNIFIED IDEOGRAPH-5E03}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12395 (\N{HIRAGANA LETTER NI}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12362 (\N{HIRAGANA LETTER O}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12369 (\N{HIRAGANA LETTER KE}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12427 (\N{HIRAGANA LETTER RU}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 22806 (\N{CJK UNIFIED IDEOGRAPH-5916}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12428 (\N{HIRAGANA LETTER RE}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 20516 (\N{CJK UNIFIED IDEOGRAPH-5024}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 38334 (\N{CJK UNIFIED IDEOGRAPH-95BE}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12392 (\N{HIRAGANA LETTER TO}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 20449 (\N{CJK UNIFIED IDEOGRAPH-4FE1}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 38972 (\N{CJK UNIFIED IDEOGRAPH-983C}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 31684 (\N{CJK UNIFIED IDEOGRAPH-7BC4}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 22258 (\N{CJK UNIFIED IDEOGRAPH-56F2}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12398 (\N{HIRAGANA LETTER NO}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 27604 (\N{CJK UNIFIED IDEOGRAPH-6BD4}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 36611 (\N{CJK UNIFIED IDEOGRAPH-8F03}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12498 (\N{KATAKANA LETTER HI}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12473 (\N{KATAKANA LETTER SU}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12488 (\N{KATAKANA LETTER TO}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12464 (\N{KATAKANA LETTER GU}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12521 (\N{KATAKANA LETTER RA}) missing from current font.
  plt.tight_layout()
/tmp/ipykernel_9148/3773550852.py:47: UserWarning: Glyph 12512 (\N{KATAKANA LETTER MU}) missing from current font.
  plt.tight_layout()
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 30906 (\N{CJK UNIFIED IDEOGRAPH-78BA}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 29575 (\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 23494 (\N{CJK UNIFIED IDEOGRAPH-5BC6}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 27161 (\N{CJK UNIFIED IDEOGRAPH-6A19}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 28310 (\N{CJK UNIFIED IDEOGRAPH-6E96}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 35215 (\N{CJK UNIFIED IDEOGRAPH-898F}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 20998 (\N{CJK UNIFIED IDEOGRAPH-5206}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 24067 (\N{CJK UNIFIED IDEOGRAPH-5E03}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12395 (\N{HIRAGANA LETTER NI}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12362 (\N{HIRAGANA LETTER O}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12369 (\N{HIRAGANA LETTER KE}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12427 (\N{HIRAGANA LETTER RU}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 22806 (\N{CJK UNIFIED IDEOGRAPH-5916}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12428 (\N{HIRAGANA LETTER RE}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 20516 (\N{CJK UNIFIED IDEOGRAPH-5024}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 38334 (\N{CJK UNIFIED IDEOGRAPH-95BE}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12392 (\N{HIRAGANA LETTER TO}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 20449 (\N{CJK UNIFIED IDEOGRAPH-4FE1}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 38972 (\N{CJK UNIFIED IDEOGRAPH-983C}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 31684 (\N{CJK UNIFIED IDEOGRAPH-7BC4}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 22258 (\N{CJK UNIFIED IDEOGRAPH-56F2}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12398 (\N{HIRAGANA LETTER NO}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 27604 (\N{CJK UNIFIED IDEOGRAPH-6BD4}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 36611 (\N{CJK UNIFIED IDEOGRAPH-8F03}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12498 (\N{KATAKANA LETTER HI}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12473 (\N{KATAKANA LETTER SU}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12488 (\N{KATAKANA LETTER TO}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12464 (\N{KATAKANA LETTER GU}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12521 (\N{KATAKANA LETTER RA}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
/home/runner/work/notes/notes/.venv/lib/python3.10/site-packages/IPython/core/pylabtools.py:170: UserWarning: Glyph 12512 (\N{KATAKANA LETTER MU}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)
../_images/54f210a045165a8b5ebfdeb8921c717cb8faf10e2e750f6464ebc6041875f45b.png
Q1 = -0.673, Q3 = 0.676, IQR = 1.349
Tukey下限 = -2.697, 上限 = 2.700