標準的な回帰モデルは二乗誤差の和や期待値を最小化するようにモデルを学習して条件付き期待値を予測する
\newcommand{\argmin} attempting to redefine \argmin; use \renewcommand
\newcommand{\argmin}{\mathop{\rm arg~min}\limits}
E(y_i|X_i) = \argmin_{m(X_i)}
E\big[ (y_i - m(X_i))^2 \big]分位点回帰 (quantile regression)モデルはpinball lossの和や期待値を最小化するようにモデルを学習させ、条件付き分位関数を予測する
pinball lossは -tiled absolute value function や 検定関数(check function)とも呼ばれる(グラフを描くとチェックマークに似てるため)
あるいは
と書かれる
Source
import numpy as np
import matplotlib.pyplot as plt
def pinball_loss(x, tau):
return (tau - 1 * (x <= 0)) * x
x = np.linspace(-1, 1, 100)
fig, axes = plt.subplots(figsize=[10, 2], ncols=3)
for i, tau in enumerate([0.1, 0.5, 0.9]):
y = pinball_loss(x, tau=tau)
axes[i].plot(x, y)
if i == 0:
axes[i].set(title=f"τ={tau}", xlabel=r"$x$", ylabel=r"$y = (\tau - 1(x <= 0)) x$")
else:
axes[i].set(title=f"τ={tau}", xlabel=r"$x$")
fig.show()
なお、pinball lossはのとき
と、絶対誤差と比例する形になる。
絶対誤差の和を目的関数にとった線形モデルは統計学においてleast absolute deviations (LAD) と呼ばれ、その解は条件付き中央値になる
Source
import numpy as np
import matplotlib.pyplot as plt
def pinball_loss(x, tau):
return (tau - 1 * (x <= 0)) * x
x = np.linspace(-3, 3, 100)
fig, ax = plt.subplots()
ax.plot(x, pinball_loss(x, tau=0.5), label=r"$\rho_{0.5}(x)$")
ax.plot(x, abs(x), label="|x|")
ax.legend()
fig.show()
モデルの評価¶
D2 pinball score¶
はの一般化
ここでは切片のみのモデルの最適解(例:二乗誤差ならの平均値、絶対誤差ならの中央値、pinball lossならの指定されたquantile)
このに
を代入したものが pinball score
interval score¶
分位点回帰モデルの実践¶
statsmodelsでは quantreg() で実行できる
Source
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
data = sm.datasets.engel.load_pandas().data
fig, ax = plt.subplots()
ax.scatter(data["income"], data["foodexp"])
ax.set(xlabel="income", ylabel="foodexp", title="Quantile Linear Regression")
x = np.linspace(data["income"].min(), data["income"].max(), 10)
model = smf.quantreg("foodexp ~ income", data)
for q in [0.1, 0.5, 0.9]:
res = model.fit(q=q)
y_hat = res.predict(pd.DataFrame({"income": x}))
ax.plot(x, y_hat, label=fr"$\tau = {q}$")
ax.legend()
fig.show()/tmp/ipykernel_1497/2518785238.py:2: DeprecationWarning:
Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),
(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)
but was not found to be installed on your system.
If this would cause problems for you,
please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466
import pandas as pd
