Logistic回帰についての考察

\[ % to cope with definition mismatch between MathJax and LaTeX \newcommand{\coloneq}{\mathrel{:=}} \newcommand{\eqcolon}{\mathrel{=:}} % general purpose \newcommand{\ctext}[1]{\raise0.2ex\hbox{\textcircled{\scriptsize{#1}}}} % mathematics % general purpose \DeclarePairedDelimiterX{\parens}[1]{\lparen}{\rparen}{#1} \DeclarePairedDelimiterX{\braces}[1]{\lbrace}{\rbrace}{#1} \DeclarePairedDelimiterX{\bracks}[1]{\lbrack}{\rbrack}{#1} \DeclarePairedDelimiterX{\verts}[1]{|}{|}{#1} \DeclarePairedDelimiterX{\Verts}[1]{\|}{\|}{#1} \DeclarePairedDelimiterX{\setComprehension}[2]{\lbrace}{\rbrace}{#1\,\delimsize\vert\,#2} \newcommand{\as}{{\quad\textrm{as}\quad}} \newcommand{\st}{{\textrm{ s.t. }}} \newcommand{\naturalNumbers}{\mathbb{N}} \newcommand{\integers}{\mathbb{Z}} \newcommand{\rationalNumbers}{\mathbb{Q}} \newcommand{\realNumbers}{\mathbb{R}} \newcommand{\nonNegRealNumbers}{\mathbb{R}_{\geq 0}} \newcommand{\posRealNumbers}{\mathbb{R}_{> 0}} \newcommand{\complexNumbers}{\mathbb{C}} \newcommand{\field}{\mathbb{F}} \newcommand{\EuclideanSpace}{\mathbb{E}} \newcommand{\argmax}{\operatorname*{arg~max}} \newcommand{\argmin}{\operatorname*{arg~min}} % set theory \newcommand{\range}[2]{\braces*{#1,\dotsc,#2}} \renewcommand{\complement}{\mathrm{c}} \newcommand{\ind}[2]{\mathbbm{1}_{#1}\parens*{#2}} \newcommand{\indII}[1]{\mathbbm{1}\braces*{#1}} % number theory \newcommand{\abs}[1]{\verts*{#1}} \newcommand{\combi}[2]{{_{#1}\mathrm{C}_{#2}}} \newcommand{\perm}[2]{{_{#1}\mathrm{P}_{#2}}} \newcommand{\GaloisField}{\mathrm{GF}} % real analysis \newcommand{\NapierE}{\mathrm{e}} \newcommand{\sgn}{\operatorname{sgn}} % sign function \newcommand{\rect}{\operatorname{rect}} % rectangular function \newcommand{\cl}{\operatorname{cl}} % closure of a set \newcommand{\img}{\operatorname{im}} % image of a function \newcommand{\dom}{\operatorname{dom}} % domain of a function \newcommand{\LittleO}[2]{\underset{#1}{o}\parens*{#2}} % little-o notation \newcommand{\norm}[1]{\Verts*{#1}} % norm of a vector or function \newcommand{\floor}[1]{\left\lfloor #1\right\rfloor} \newcommand{\ceil}[1]{\left\lceil#1\right\rceil} \newcommand{\sinc}{\operatorname{sinc}} \newcommand{\nrmSinc}{\operatorname{nsinc}} % normalized sinc function \newcommand{\erf}{\operatorname{erf}} % inverse trigonometric functions \newcommand{\asin}{\operatorname{Sin}^{-1}} \newcommand{\acos}{\operatorname{Cos}^{-1}} \newcommand{\atan}{\operatorname{Tan}^{-1}} % derivative \newcommand{\deriv}[3]{\frac{\mathrm{d}^{#3}#1}{\mathrm{d}{#2}^{#3}}} \newcommand{\derivLong}[3]{\frac{\mathrm{d}^{#3}}{\mathrm{d}{#2}^{#3}}#1} \newcommand{\partDeriv}[3]{\frac{\mathrm{\partial}^{#3}#1}{\mathrm{\partial}{#2}^{#3}}} \newcommand{\partDerivLong}[3]{\frac{\mathrm{\partial}^{#3}}{\mathrm{\partial}{#2}^{#3}}#1} \newcommand{\partDerivIIHetero}[3]{\frac{\mathrm{\partial}^2#1}{\partial#2\mathrm{\partial}#3}} \newcommand{\partDerivIIHeteroLong}[3]{{\frac{\mathrm{\partial}^2}{\partial#2\mathrm{\partial}#3}#1}} % integral \newcommand{\pv}{{\textrm{ p.v. }}} \newcommand{\integrate}[5]{\int_{#1}^{#2}{#3}{\;\mathrm{d}^{#4}}#5} \newcommand{\LebInteg}[4]{\int_{#1} {#2} {#3}\parens*{\;\mathrm{d}#4}} % complex analysis \newcommand{\conj}[1]{\overline{#1}} \renewcommand{\Re}{\operatorname{Re}} \renewcommand{\Im}{\operatorname{Im}} \newcommand{\Arg}{\operatorname{Arg}} \newcommand{\Log}{\operatorname{Log}} % Laplace transform \newcommand{\LPLC}{\operatorname{\mathcal{L}}} \newcommand{\ILPLC}{\operatorname{\mathcal{L}}^{-1}} % discrete fourier transform \newcommand{\DFT}{\operatorname{DFT}} \newcommand{\IDFT}{\operatorname{IDFT}} % Z-transform \newcommand{\ZTrans}{\operatorname{\mathcal{Z}}} \newcommand{\IZTrans}{\operatorname{\mathcal{Z}}^{-1}} \newcommand{\SSZTrans}{\underset{\text{s.s.}}{\mathcal{Z}}} % single-sided Z-transform % linear algebra \newcommand{\bm}[1]{{\boldsymbol{#1}}} \newcommand{\vecEntry}[2]{\bm{#1}\bracks*{#2}} \newcommand{\matEntry}[3]{#1\bracks*{#2}\bracks*{#3}} \newcommand{\matPart}[5]{\matEntry{#1}{#2:#3}{#4:#5}} \newcommand{\minor}[3]{{#1}\bracks*{\setminus #2}\bracks*{\setminus #3}} % the minor of a matrix: row #2 and column #3 deleted \newcommand{\diag}{\operatorname{diag}} \newcommand{\transpose}[1]{{#1}^\top} \newcommand{\HerConj}[1]{{#1}^*} \newcommand{\tr}{\operatorname{tr}} \newcommand{\inProd}[2]{\left\langle#1,#2\right\rangle} \newcommand{\dotProd}[2]{#1 \cdot #2} \newcommand{\HadamardProd}{\odot} \newcommand{\HadamardDiv}{\oslash} \newcommand{\vecSpan}{\operatorname{span}} \newcommand{\rank}{\operatorname{rank}} % vector % unit vector \newcommand{\vix}{\bm{i}_x} \newcommand{\viy}{\bm{i}_y} \newcommand{\viz}{\bm{i}_z} % graph theory \newcommand{\neighborhood}{\mathcal{N}} % probability theory \newcommand{\PDF}{\operatorname{PDF}} \newcommand{\Ber}{\operatorname{Ber}} \newcommand{\Beta}{\operatorname{Beta}} \newcommand{\ExpDist}{\operatorname{ExpDist}} \newcommand{\ErlangDist}{\operatorname{ErlangDist}} \newcommand{\PoissonDist}{\operatorname{PoissonDist}} \newcommand{\GammaDist}{\operatorname{Gamma}} \newcommand{\cind}[2]{\ind{#1\left| #2\right.}} % conditional indicator function \renewcommand{\Pr}{\operatorname{Pr}} \DeclarePairedDelimiterX{\cPrParens}[2]{(}{)}{#1\,\delimsize\vert\,#2} \newcommand{\Ev}{\operatorname{E}} % expected value \newcommand{\Var}{\operatorname{Var}} \newcommand{\Cov}{\operatorname{Cov}} % physics % unit of measurement \newcommand{\second}{\text{s}} \newcommand{\hertz}{\text{Hz}} \newcommand{\decibel}{\text{dB}} % signal processing % Discrete Time Fourier Transform \newcommand{\DTFT}{\operatorname{DTFT}} \newcommand{\IDTFT}{\operatorname{IDTFT}} % computer science % fixed-point arithmetic \newcommand{\IntPartBW}[1]{\underbracket[0.140ex]{#1}_\mathrm{i}} % 0.140ex is half of the default thickness. See: [How to make underbracket thinner](https://tex.stackexchange.com/questions/559078/how-to-make-underbracket-thinner) \newcommand{\DecPartBW}[1]{\underbracket[0.140ex]{#1}_\mathrm{d}} \newcommand{\TotalBW}[1]{\underbracket[0.140ex]{#1}} \newcommand{\Rat}{\operatorname{Rat}} % Maps a fixed-point number to a corresponding rational number. % programming \newcommand{\plpl}{\mathrel{++}} \newcommand{\pleq}{\mathrel{+}=} \newcommand{\asteq}{\mathrel{*}=} \]

はじめに

前回の記事「Probitモデルについての考察」に続き、2値分類問題を解く方法の一つである「Logisticモデル」について調べた。納得するのに手数を要した事柄について備忘録代わりにまとめておく。本記事で使う記号は英語版Wikipediaの記事に従う。

なぜOddsの対数を使うのか?

  • 確率の値域である、[0,1]の範囲の値を実数全体にマップしたい。
  • Oddsによってまず[0,∞)にマップできる。
  • それの対数をとれば(-∞,∞)にマップできる。

潜在変数モデルについて

潜在変数モデルでは、独立変数$\bm{X} = [X_0,X_1,\dots,X_n]^\top \in \mathbb{R}^n$に対して潜在変数は$Y’ = \bm{\beta}^\top\bm{X} + \varepsilon,\;(\bm{\beta} = [\beta_0,\beta_1,\dots,\beta_n]^\top \in \mathbb{R}^n)$と仮定される。$\varepsilon$は標準ロジスティック分布に従確率変数である。ただし$\bm{X}$の最初の要素$X_0$は1に固定されている(これによって分布のオフセットを表現している)。従属変数$Y$は次の規則で決まるものと仮定される。

$$ Y = \begin{cases} 1 & Y’ > 0 \\ 0 & \text{otherwise} \end{cases} $$

すなわち

$$ \Pr(Y=1\;|\;\bm{\beta},\bm{X}) = \Pr(-\varepsilon < \bm{\beta}^\top\bm{X}\;|\;\bm{\beta},\bm{X}) = \Pr(\varepsilon < \bm{\beta}^\top\bm{X}\;|\;\bm{\beta},\bm{X}) = \Phi(\bm{\beta}^\top\bm{X}) $$

最後から二番目の等号は、標準ロジスティック分布が原点に関して対称であることから成り立つ。また、$\Phi(x) = 1/(1+e^{-x})$は標準ロジスティック分布の累積分布関数である。

対数尤度関数が上に凸であること

以下に述べるように、対数尤度関数は大域的に上に凸である。このため、勾配降下法ベースの最適化アルゴリズムによって$\bm{\beta}$の推定値を計算できる。

独立変数と従属変数の観測値の組$\{\bm{x}_i,y_i\},\;(i=1,2,\dots,N)$から最尤推定により$\bm{\beta}$を推定するための尤度関数は次式である。

$$ \prod_{i=1}^N \Phi(\bm{\beta}^\top\bm{X}_i)^{y_i} \left(1-\Phi(\bm{\beta}^\top\bm{X}_i)\right)^{1-y_i} $$

上式の対数をとり、対数尤度関数$f$を得る(次式)。

$$ f(\bm{\beta}) = \sum_{i=1}^N \left[y_i \log(\Phi(\bm{\beta}^\top\bm{X}_i)) + (1-y_i)\log\left(1-\Phi(\bm{\beta}^\top\bm{X}_i)\right)\right] $$

これが$\bm{\beta}$に関して上に凸であることを示す。個々の項が上に凸であることを示せばよい。なぜならば、上に凸な関数の和もまた上に凸であるから。証明をさらに省力化するため、次の性質を使う。

$$ \begin{aligned} 1-\Phi(\bm{\beta}^\top\bm{X}) &= \Pr(Y=0\;|\;\bm{\beta},\bm{X}) = \Pr(\bm{\beta}^\top\bm{X} + \varepsilon \leq 0\;|\;\bm{\beta},\bm{X}) \\ &= \Pr(\varepsilon \leq -\bm{\beta}^\top\bm{X}\;|\;\bm{\beta},\bm{X}) = \Pr(\varepsilon < -\bm{\beta}^\top\bm{X}\;|\;\bm{\beta},\bm{X}) \\ &= \Phi(-\bm{\beta}^\top\bm{X}) \end{aligned} $$

最後から2番目の等号はロジスティック分布が連続型分布であることから従う。$\log\left(\Phi(-\bm{\beta}^\top\bm{X})\right)$は$\log\left(\Phi(\bm{\beta}^\top\bm{X})\right)$を原点に関して対称に折り返したものであるから、後者が上に凸であれば前者もそうである。

結局、$f$が$\bm{\beta}$に関して上に凸であることを言うには$g(\bm{\beta}) := \log\left(\Phi(\bm{\beta}^\top\bm{X})\right)$がそうであることを示せば良い。

まず$g$の勾配$\nabla g$を求める。$\phi(x) := \frac{\mathrm{d}\Phi(x)}{\mathrm{d}x}$を標準ロジスティック分布の確率密度関数とする。

$$ \begin{aligned} \frac{\partial g(\bm{\beta})}{\partial \beta_i} &= \frac{\phi(\bm{\beta}^\top\bm{X})}{\Phi(\bm{\beta}^\top\bm{X})} X_i = \left(1+\exp(-\bm{\beta}^\top\bm{X})\right)\frac{\exp(-\bm{\beta}^\top\bm{X})}{\left(1+\exp(-\bm{\beta}^\top\bm{X})\right)^2}X_i \\ &= \frac{\exp(-\bm{\beta}^\top\bm{X})}{1+\exp(-\bm{\beta}^\top\bm{X})} X_i = \frac{X_i}{1+\exp(\bm{\beta}^\top\bm{X})} = X_i\Phi(-\bm{\beta}^\top\bm{X}) \\ \therefore \nabla g(\bm{\beta}) &= \Phi(-\bm{\beta}^\top\bm{X}) \bm{X} \end{aligned} $$

次に$g$のHesse行列$\nabla^2 g$を求める。

$$ \begin{aligned} \frac{\partial^2 g(\bm{\beta})}{\partial \beta_i \partial \beta_j} &= X_i\frac{\partial}{\partial \beta_j} \Phi(-\bm{\beta}^\top\bm{X}) = X_i\phi(-\bm{\beta}^\top\bm{X})(-X_j) = -X_i X_j \phi(-\bm{\beta}^\top\bm{X}) \\ \therefore \nabla^2 g(\bm{\beta}) &= -\phi(-\bm{\beta}^\top\bm{X})\bm{X}\bm{X}^\top \end{aligned} $$

$\phi > 0,\;\bm{X}\bm{X}^\top \succeq O$であるから$\nabla^2 g \preceq O$であり、$g$は上に凸である。ここに、$\succeq$は半正定, $\preceq$は半負定を表す。

以上より$f$は少なくとも上に凸である。実用上は多数のランダムサンプル$\bm{X}_i$を用いるため、$f$のHesse行列は厳密に負定となることが普通である。このとき$f$は上に狭義凸である。

Mathematicaによる数値例

[-0.25,0.5,-0.5]^\top$としてランダムサンプルを生成し、$\bm{\beta}$を推定してみた。

コメントを残す