スキップしてメイン コンテンツに移動

Theorem of Logistic regression.

Introduction


Today, I will write about Logistic regression. Logistic regression is the basis of Machine Learning. Logistic regression is the model to classify two value

Overview

This post is written by using PRML for reference.
Optimization is used for Iterative reweighted least squares method.

  • First, I will introduce the sigmoid function
  • Second, I will define probability to classify
  • Third, I will write cross-entropy error function
  • Fourth, I will explain IRLS
Firstly, We define Sigmoid function.
sigmoid function is following.

\[\sigma(a) = \frac{1}{1+\exp(a)}\]

I will compute differential of this function.

\begin{eqnarray*} \frac{d}{d a} \frac{1}{1+\exp(a)} &=& \frac{\exp(-a)}{(1+\exp(-a))^2} \\ &=& \frac{1}{1+\exp(-a)} \frac{\exp(-a)}{1+\exp(-a)}\\ &=& \frac{1}{1+\exp(-a)} \{ \frac{1+\exp(-a)}{1+\exp(-a)} - \frac{1}{1+\exp(-a)} \} \\ &=& \sigma(a)(1-\sigma(a)) \end{eqnarray*}

This function is very important in terms in terms of Machine Learning.
Because Sigmoid function has the following form.
enter image description here
The sigmoid function has the following characteristic.
  • Sigmoid function is defined on \((-\infty,\infty)\)
  • and, range of y is (0,1).
  • The sigmoid function is monotonic increase function.
  • Sigmoid function is point-symmetry in (0,0.5)
We regard the value of the sigmoid function as the probability.

Probability of classifying

First, We think of probability that data is properly classified.
enter image description here

This line is a hyperplane.
a hyperplane is expressed as follows.

\[w^T x= 0\]

w is a normal vector of the hyperplane.
  • Data point exist red domain when \(w^T x > 0\)
  • Data point exist blue domain when \(w^T x < 0\)
If data is exists in red domain, the data is belong \(C_1\).
If data exists in the blue domain, the data is belonging \(C_2\)
\(C_1\) have 1 as a label. \(C_2\) have 0 as a label.

We can have confidence that I know the class of data exist far from the hyperplane, but we do not know the class of data when data point exists near hyperplane.

We want not to believe the class of data predicted near hyper plane.

Therefore!!, I use disposition of Sigmoid function. I change the distance from
the hyperplane to data point into probability.
  • I handle that data point exist red domain\((C_1)\) when the probability is higher than 0.5.
  • When probability has just 0.5, I do not know the class of data. (The data exist on the hyperplane)
  • I handle that data point exist blue domain \((C_2)\) when the probability is lower than 0.5.
I summarize this paragraph. I want to handle as follows.
  • The farther the distance between a data point and the hyperplane is the farther keep probability away from 0.5.
  • The nearer the distance between a data point and the hyperplane is, the nearer probability is 0.5.
  • When the probability is near 1, I have confidence that class of the data is \(C_1\)
  • When the probability is near 0, I have confidence that class of the data is \(C_2\)
I implement this way of thinking by using the sigmoid function.
Sigmoid function fulfill these way of thinking.

Thus, I handle probability value which output of sigmoid function by input distance between datapoint and hyperplane.

Cross-entropy error function

when Data set is \(\{\phi_n,t_n\}\), I define probability by using Sigmoid function.
here, \(t_n \in {0,1}\)
I define likelihood function as follows.
\[p(t|w) = \Pi_{n=1}^{N} y_{n}^{t_n} \{1-y_n\}^{1-t_n}\]
here,\[y_n = \sigma(w^T \phi_n) \]
\[t = (t_1,t_2,,,t_n)^T\]
\(w^T x\)is distance between \(\phi\) and hyper plane.
y_n is probability that \(\phi_n\) is \(C_1\)
I get negative logarithm about likelihood function.
\[E(w) = -\log p(t|w) = - \sum_{n=1}^{N} \{t_n \log(y_n) + (1-t_n ) \log(1-y_n)\}\]

\(E(w)\) is called Cross-entropy error function

IRLS

IRLS is iterative reweighted least squares method. This method estimate w by using the Newton-Raphson method.

I get gradient of E(w).
\begin{eqnarray*} \nabla E(w) &=& \nabla - \sum_{n=1}^{N} \{t_n \log(y_n) + (1-t_n ) \log(1-y_n)\}\\ &=& \nabla - \sum_{n=1}^{N} \{t_n \log(\sigma(w^T \phi_n) + (1-t_n ) \log(1-\sigma(w^T \phi_n)\}\\ &=&-\sum_{n=1}^{N} \{ \frac{t_n \sigma(w^T \phi_n)(1-\sigma(w^T \phi_n))}{\sigma(w^T \phi_n)}\phi_n - \frac{(1-t_n) \sigma(w^T \phi_n)(1-\sigma(w^T \phi_n))}{1-\sigma(w^T \phi_n)}\phi_n \}\\ &=& -\sum_{n=1}^{N} \frac{t_n (1-\sigma(w^T \phi_n))-(1-t_n)\sigma(w^T \phi_n)}{\sigma(w^T \phi_n) (1-\sigma(w^T \phi_n))} \{\sigma(w^T \phi_n) (1-\sigma(w^T \phi_n))\} \phi_n \\ &=& -\sum_{n=1}^{N} \{t_n (1-\sigma(w^T \phi_n))-(1-t_n)\sigma(w^T \phi_n)\} \phi_n \\ &=& -\sum_{n=1}^{N} \{t_n - t_n \sigma(w^T \phi_n) - \sigma(w^T \phi_n) - \sigma(w^T \phi_n) + t_n \sigma(w^T \phi)\} \phi_n\\ &=&\sum_{n=1}^{N} \{\sigma(w^T \phi_n) - t_n \} \phi_n\\ &=& \sum_{n=1}^{N} (y_n - t_n) \phi_n\\ &=& \phi^T (y - t)\\ \end{eqnarray*}
I get Hessian matrix of E(w)
\begin{eqnarray*} H &=& \nabla \nabla E(w) \\ &=& \nabla \phi^T(y-t) \\ &=& \nabla \sum_{n=1}^{N} \phi_{n}^T (y_n-t_n) \\ &=& \nabla\sum_{n=1}^{N} (y_n-t_n) \phi_{n}^T \\ &=&\sum_{n=1}^{N} y_n(1-y_n) \phi_n \phi^T = \phi^T R \phi \end{eqnarray*}
here, R is daiagonal matrix and have \(y_n(1-y_n)\) element of (n,n)
I use These form to estimate by Newton-Raphson method.
\begin{eqnarray*} w_{new} &=& w_{old} - \{ \phi^ T R \phi \}^ {-1} \phi^T (y-t) \\ &=& \{ \phi^ T R \phi \}^ T \{ \phi^ T R \phi w_{old} - \phi^T(y-t)\} \\ &=& \{ \phi^ T R \phi \} ^ {-1} \phi^ T Rz \end{eqnarray*}

Thus, w is updated by this form.
This post is Theory ed of Logistic Regression.
Next, I implement Logistic Regression.
I am glad to see my next post.

*I wrote the implementation of Logistic Regression.
Implementation of Logistic Regression

コメント

このブログの人気の投稿

カーネルk-meansの実装

Introduction   English ver 今日はカーネルk-meansの実装をしました。k-menasアルゴリズムはクラスタリングのためのアルゴリズムです。僕がカーネルk-meansを実装しようと思ったのには一つ理由があります。それは僕の友人がk-meansのプレゼンを、僕がカーネルのプレゼンをしていた時に、k-meansにカーネルを適応できないかと思ったからです。そこで、カーネルk-meansについての論文を探しました。 ここのpdf を主に参考にさせていただきました。うまくカーネルk-meansを実装できたと思います。ここでは、普通のk-meansとカーネルを用いた,kernel k-meansについての実装の結果を紹介します。 また、この記事では実装結果のみ書きますが、理論のほうも別の記事で書くつもりです。書き終えたらリンクをこの記事にも貼っておきます。 #  理論編書きました。K-means 理論編 概要 dataset   ちょっとだけ理論の説明  k-means    kernel k-means   Dataset   English ver 今回使うのは二つのデータセットです。一つ目は、普通のk-means用のデータです。二つ目はkernel k-means用のデータセットです。 一つ目のデータは、三つのグループで構成されており、次元は2で、サンプル数は300です。以下のような分布になっています。 二つ目のデータは二つのグループで構成されており、次元は2でサンプル数は300です。   this page にデータセットを作ったコードを載せています。 ちょっとだけ理論の説明 k-meansとは、k-平均法とも呼ばれています。初めに、適当なクラスに分け、各クラスの中で平均となるベクトルを求めます。そして、各データに対して、すべての平均ベクトルとの距離を求めます。そして、最小となる距離になるクラスに改めて、そのデータをクラスタリングします。そして、新たに得られたクラスの中でそれぞれ平均ベクトルを求め、これを繰り返し、平均ベクトルが動かな...

大学院試験 -外部への道しるべ-

始めに この度、 京都大学大学院情報学研究科システム科学専攻 に合格することができました!!! 僕は現在、立命館大学という関西の私立大学に通っているので、外部受験をしたことになります。 さらに、学部は数学専攻で、大学院からは情報学(の中でも機械学習)専攻になるので、専門も変えることになります。 この記事では、外部の大学院、もしくは専攻替えを考えている人向けに書こうと思っているので、目次で気になった項目があれば、ぜひ、読んでいってくださいませ。( *´艸`) ちなみに、予測点数は線形微積6~7割、専門科目満点、英語かなり低いので内緒です。(笑) 得点開示を要求するので、得点がわかったら、また追記します。 目次 外部受験を目指すまで、目指したきっかけ 外部受検の大変さ 専攻替えの大変さ 合格するために 英語が苦手な人へ 数学科の学部から情報学(機械学習)の大学院を目指す人へ 応援 外部受検を目指すまで、目指したきっかけ ここでは、自分の大学生活がどんなだったかを書いてるだけなので、興味のない人は飛ばしましょう。(笑) 僕が学部二回生頃に、当時数理科には機械学習の研究をされている先生が一人だけ所属されていました。その先生に、直接弟子入りさせていただき、僕の機械学習への道は始まりました。。。(メインは遺伝統計学の研究でした。) 弟子入りした直後は、タイピングもなめくじのように遅かったですし、gitもpullする前にpushしたこともありました。。。 しかし、その先生は、目的に最先端で届く道のりを用意してくださいました。 プログラミングを初めて一か月ほどで、t-SNEの実装をしたり(遺伝統計学の研究で必要だった)、四か月ほどで、カーネルc-SVMの実装をしたり(やってみなとゆわれて(笑))することができました。その後も、学部二回生、三回生ながら、論文を読んで実装してきました。 学部二回生冬には、遺伝統計学の研究を 株式会社パーソルキャリア さん主催のハッチングフェスというデータサイエンティストのためのイベントで、発表しました。 このイベントでは、企業の方もたくさん来られて、知り合えるチャンスがかなりあります!! (名刺を作っておくと、「えっ、学生なのに名刺持ってるの?!」ってなるので、覚えてもらえます。...

Discrete Fourier transform

Introduction 日本語 ver I will write about Discrete Fourier transform. Discrete Fourier transform is Abbreviated DFT. I am making pdf about Audio Signal Processing. I publish pdf at  github . However, I write tex in Japanese. I take a lecture about the signal processing. There is lecture at  thie page . I update this pdf.