replacing hats with boldmath

This commit is contained in:
mhjensen
2018-11-05 21:21:26 +01:00
parent 401104c7ff
commit 4945a43ae7
18 changed files with 387 additions and 343 deletions
+63 -57
View File
@@ -58,11 +58,11 @@ b+w_1x_1+w_2x_2=0,
!et
where $b$ is the intercept and $w_1$ and $w_2$ define the elements of a vector orthogonal to the line
$b+w_1x_1+w_2x_2=0$.
In two dimensions we define the vectors $\hat{x} =[x1,x2]$ and $\hat{w}=[w1,w2]$.
In two dimensions we define the vectors $\boldmath{x} =[x1,x2]$ and $\boldmath{w}=[w1,w2]$.
We can then rewrite the above equation as
!bt
\[
\hat{w}^T\hat{x}+b=0.
\boldmath{w}^T\boldmath{x}+b=0.
\]
!et
@@ -77,28 +77,28 @@ b+wx_1+w_2x_2+\dots +w_px_p=0.
\]
!et
If we define a
matrix $\hat{X}=\left[\hat{x}_1,\hat{x}_2,\dots, \hat{x}_p\right]$
of dimension $n\times p$, where $n$ represents the observations for each feature and each vector $x_i$ is a column vector of the matrix $\hat{X}$,
matrix $\boldmath{X}=\left[\boldmath{x}_1,\boldmath{x}_2,\dots, \boldmath{x}_p\right]$
of dimension $n\times p$, where $n$ represents the observations for each feature and each vector $x_i$ is a column vector of the matrix $\boldmath{X}$,
!bt
\[
\hat{x}_i = \begin{bmatrix} x_{i1} \\ x_{i2} \\ \dots \\ \dots \\ x_{ip} \end{bmatrix}.
\boldmath{x}_i = \begin{bmatrix} x_{i1} \\ x_{i2} \\ \dots \\ \dots \\ x_{ip} \end{bmatrix}.
\]
!et
If the above condition is not met for a given vector $\hat{x}_i$ we have
If the above condition is not met for a given vector $\boldmath{x}_i$ we have
!bt
\[
b+w_1x_{i1}+w_2x_{i}2+\dots +w_px_{ip} >0,
\]
!et
if our output $y_i=1$.
In this case we say that $\hat{x}_i$ lies on one of the sides of the hyperplane and if
In this case we say that $\boldmath{x}_i$ lies on one of the sides of the hyperplane and if
!bt
\[
b+w_1x_{i1}+w_2x_{i}2+\dots +w_px_{ip} < 0,
\]
!et
for the class of observations $y_i=-1$,
then $\hat{x}_i$ lies on the other side.
then $\boldmath{x}_i$ lies on the other side.
Equivalently, for the two classes of observations we have
!bt
@@ -137,35 +137,35 @@ for our data sample.
Let us define the function
!bt
\[
f(x) = \hat{w}^T\hat{x}+b = 0,
f(x) = \boldmath{w}^T\boldmath{x}+b = 0,
\]
!et
as the function that determines the line $L$ that separates two classes (our two features), see the figure here.
Any point defined by $\hat{x}_i$ and $\hat{x}_2$ on the line $L$ will satisfy $\hat{w}^T(\hat{x}_1-\hat{x}_2)=0$.
Any point defined by $\boldmath{x}_i$ and $\boldmath{x}_2$ on the line $L$ will satisfy $\boldmath{w}^T(\boldmath{x}_1-\boldmath{x}_2)=0$.
The signed distance $\delta$ from any point defined by a vector $\hat{x}$ and a point $\hat{x}_0$ on the line $L$ is then
The signed distance $\delta$ from any point defined by a vector $\boldmath{x}$ and a point $\boldmath{x}_0$ on the line $L$ is then
!bt
\[
\delta = \frac{1}{\vert\vert \hat{w}\vert\vert}(\hat{w}^T\hat{x}+b).
\delta = \frac{1}{\vert\vert \boldmath{w}\vert\vert}(\boldmath{w}^T\boldmath{x}+b).
\]
!et
!split
===== First attempt at a minimization approach =====
How do we find the parameter $b$ and the vector $\hat{w}$? What we could
How do we find the parameter $b$ and the vector $\boldmath{w}$? What we could
do is to define a cost function which now contains the set of all
misclassified points $M$ and attempt to minimize this function
!bt
\[
C(\hat{w},b) = -\sum_{i\in M} y_i(\hat{w}^T\hat{x}_i+b).
C(\boldmath{w},b) = -\sum_{i\in M} y_i(\boldmath{w}^T\boldmath{x}_i+b).
\]
!et
We could now for example define all values $y_i =1$ as misclassified in case we have $\hat{w}^T\hat{x}_i+b < 0$ and the opposite if we have $y_i=-1$. Taking the derivatives gives us
We could now for example define all values $y_i =1$ as misclassified in case we have $\boldmath{w}^T\boldmath{x}_i+b < 0$ and the opposite if we have $y_i=-1$. Taking the derivatives gives us
!bt
\[
\frac{\partial C}{\partial b} = -\sum_{i\in M} y_i,
@@ -174,7 +174,7 @@ We could now for example define all values $y_i =1$ as misclassified in case we
and
!bt
\[
\frac{\partial C}{\partial \hat{w}} = -\sum_{i\in M} y_ix_i.
\frac{\partial C}{\partial \boldmath{w}} = -\sum_{i\in M} y_ix_i.
\]
!et
@@ -190,7 +190,7 @@ b \leftarrow b +\eta \frac{\partial C}{\partial b},
and
!bt
\[
\hat{w} \leftarrow \hat{w} +\eta \frac{\partial C}{\partial \hat{w}},
\boldmath{w} \leftarrow \boldmath{w} +\eta \frac{\partial C}{\partial \boldmath{w}},
\]
!et
where $\eta$ is our by now well-known learning rate.
@@ -210,12 +210,12 @@ at all.
A better approach is rather to try to define a large margin between
the two classes (if they are well separated from the beginning).
Thus, we wish to find a margin $M$ with $\hat{w}$ normalized to
$\vert\vert \hat{w}\vert\vert =1$ subject to the condition
Thus, we wish to find a margin $M$ with $\boldmath{w}$ normalized to
$\vert\vert \boldmath{w}\vert\vert =1$ subject to the condition
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, p.
y_i(\boldmath{w}^T\boldmath{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, p.
\]
!et
All points are thus at a signed distance from the decision boundary defined by the line $L$. The parameters $b$ and $w_1$ and $w_2$ define this line.
@@ -223,24 +223,24 @@ All points are thus at a signed distance from the decision boundary defined by t
We seek thus the largest value $M$ defined by
!bt
\[
\frac{1}{\vert \vert \hat{w}\vert\vert}y_i(\hat{w}^T\hat{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, n,
\frac{1}{\vert \vert \boldmath{w}\vert\vert}y_i(\boldmath{w}^T\boldmath{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, n,
\]
!et
or just
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b) \geq M\vert \vert \hat{w}\vert\vert \hspace{0.1cm}\forall i.
y_i(\boldmath{w}^T\boldmath{x}_i+b) \geq M\vert \vert \boldmath{w}\vert\vert \hspace{0.1cm}\forall i.
\]
!et
If we scale the equation so that $\vert \vert \hat{w}\vert\vert = 1/M$, we have to find the minimum of
$\hat{w}^T\hat{w}=\vert \vert \hat{w}\vert\vert$ (the norm) subject to the condition
If we scale the equation so that $\vert \vert \boldmath{w}\vert\vert = 1/M$, we have to find the minimum of
$\boldmath{w}^T\boldmath{w}=\vert \vert \boldmath{w}\vert\vert$ (the norm) subject to the condition
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
y_i(\boldmath{w}^T\boldmath{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
\]
!et
We have thus defined our margin as the invers of the norm of $\hat{w}$. We want to minimize the norm in order to have a as large as possible margin $M$. Before we proceed, we need to remind ourselves about Lagrangian multipliers.
We have thus defined our margin as the invers of the norm of $\boldmath{w}$. We want to minimize the norm in order to have a as large as possible margin $M$. Before we proceed, we need to remind ourselves about Lagrangian multipliers.
!split
===== A quick reminder on Lagrangian multipliers =====
@@ -346,12 +346,12 @@ If we have a set of constraints $\phi_k$ we have the equations
In order to solve the above problem, we define the following Lagrangian function to be minimized
!bt
\[
{\cal L}(\lambda,b,\hat{w})=\frac{1}{2}\hat{w}^T\hat{w}-\sum_{i=1}^n\lambda_i\left[y_i(\hat{w}^T\hat{x}_i+b)-1\right],
{\cal L}(\lambda,b,\boldmath{w})=\frac{1}{2}\boldmath{w}^T\boldmath{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldmath{w}^T\boldmath{x}_i+b)-1\right],
\]
!et
where $\lambda_i$ is a so-called Lagrange multiplier subject to the condition $\lambda_i \geq 0$.
Taking the derivatives with respect to $b$ and $\hat{w}$ we obtain
Taking the derivatives with respect to $b$ and $\boldmath{w}$ we obtain
!bt
\[
\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0,
@@ -360,25 +360,25 @@ Taking the derivatives with respect to $b$ and $\hat{w}$ we obtain
and
!bt
\[
\frac{\partial {\cal L}}{\partial \hat{w}} = 0 = \hat{w}-\sum_{i} \lambda_iy_i\hat{x}_i.
\frac{\partial {\cal L}}{\partial \boldmath{w}} = 0 = \boldmath{w}-\sum_{i} \lambda_iy_i\boldmath{x}_i.
\]
!et
Inserting these constraints into the equation for ${\cal L}$ we obtain
!bt
\[
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\hat{x}_i^T\hat{x}_j,
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldmath{x}_i^T\boldmath{x}_j,
\]
!et
subject to the constraints $\lambda_i\geq 0$ and $\sum_i\lambda_iy_i=0$.
We must in addition satisfy the "Karush-Kuhn-Tucker":"https://en.wikipedia.org/wiki/Karush%E2%80%93Kuhn%E2%80%93Tucker_conditions" (KKT) condition
!bt
\[
\lambda_i\left[y_i(\hat{w}^T\hat{x}_i+b) -1\right] \hspace{0.1cm}\forall i.
\lambda_i\left[y_i(\boldmath{w}^T\boldmath{x}_i+b) -1\right] \hspace{0.1cm}\forall i.
\]
!et
o If $\lambda_i > 0$, then $y_i(\hat{w}^T\hat{x}_i+b)=1$ and we say that $x_i$ is on the boundary.
o If $y_i(\hat{w}^T\hat{x}_i+b)> 1$, we say $x_i$ is not on the boundary and we set $\lambda_i=0$.
When $\lambda_i > 0$, the vectors $\hat{x}_i$ are called support vectors. They are the vectors closest to the line (or hyperplane) and define the margin $M$.
o If $\lambda_i > 0$, then $y_i(\boldmath{w}^T\boldmath{x}_i+b)=1$ and we say that $x_i$ is on the boundary.
o If $y_i(\boldmath{w}^T\boldmath{x}_i+b)> 1$, we say $x_i$ is not on the boundary and we set $\lambda_i=0$.
When $\lambda_i > 0$, the vectors $\boldmath{x}_i$ are called support vectors. They are the vectors closest to the line (or hyperplane) and define the margin $M$.
!split
===== The problem to solve =====
@@ -386,22 +386,22 @@ When $\lambda_i > 0$, the vectors $\hat{x}_i$ are called support vectors. They a
We can rewrite
!bt
\[
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\hat{x}_i^T\hat{x}_j,
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldmath{x}_i^T\boldmath{x}_j,
\]
!et
and its constraints in terms of a matrix-vector problem where we minimize w.r.t. $\lambda$ the following problem
!bt
\[
\frac{1}{2} \hat{\lambda}^T\begin{bmatrix} y_1y_1\hat{x}_1^T\hat{x}_1 & y_1y_2\hat{x}_1^T\hat{x}_2 & \dots & \dots & y_1y_n\hat{x}_1^T\hat{x}_n \\
y_2y_1\hat{x}_2^T\hat{x}_1 & y_2y_2\hat{x}_2^T\hat{x}_2 & \dots & \dots & y_1y_n\hat{x}_2^T\hat{x}_n \\
\frac{1}{2} \boldmath{\lambda}^T\begin{bmatrix} y_1y_1\boldmath{x}_1^T\boldmath{x}_1 & y_1y_2\boldmath{x}_1^T\boldmath{x}_2 & \dots & \dots & y_1y_n\boldmath{x}_1^T\boldmath{x}_n \\
y_2y_1\boldmath{x}_2^T\boldmath{x}_1 & y_2y_2\boldmath{x}_2^T\boldmath{x}_2 & \dots & \dots & y_1y_n\boldmath{x}_2^T\boldmath{x}_n \\
\dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots \\
y_ny_1\hat{x}_n^T\hat{x}_1 & y_ny_2\hat{x}_n^T\hat{x}_2 & \dots & \dots & y_ny_n\hat{x}_n^T\hat{x}_n \\
\end{bmatrix}\hat{\lambda}-\mathbb{1}\hat{\lambda},
y_ny_1\boldmath{x}_n^T\boldmath{x}_1 & y_ny_2\boldmath{x}_n^T\boldmath{x}_2 & \dots & \dots & y_ny_n\boldmath{x}_n^T\boldmath{x}_n \\
\end{bmatrix}\boldmath{\lambda}-\mathbb{1}\boldmath{\lambda},
\]
!et
subject to $\hat{y}^T\hat{\lambda}=0$. Here we defined the vectors $\hat{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n]$ and
$\hat{y}=[y_1,y_2,\dots,y_n]$.
subject to $\boldmath{y}^T\boldmath{\lambda}=0$. Here we defined the vectors $\boldmath{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n]$ and
$\boldmath{y}=[y_1,y_2,\dots,y_n]$.
!split
@@ -411,25 +411,31 @@ Solving the above problem, yields the values of $\lambda_i$.
To find the coefficients of your hyperplane we need simply to compute
!bt
\[
\hat{w}=\sum_{i} \lambda_iy_i\hat{x}_i.
\boldmath{w}=\sum_{i} \lambda_iy_i\boldmath{x}_i.
\]
!et
With our vector $\hat{w}$ we can in turn find the value of the intercept $b$ (here in two dimensions) via
With our vector $\boldmath{w}$ we can in turn find the value of the intercept $b$ (here in two dimensions) via
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b)=1,
y_i(\boldmath{w}^T\boldmath{x}_i+b)=1,
\]
!et
resulting in
!bt
\[
b = \frac{1}{y_i}-\hat{w}^T\hat{x}_i.
b = \frac{1}{y_i}-\boldmath{w}^T\boldmath{x}_i,
\]
!et
or if we write it out in terms of the support vectors only, with $N_s$ being their number, we have
!bt
\[
b = \frac{1}{N_s}\sum_{j\in N_s}\left(y_j-\sum_{i=1}^n\lambda_iy_i\boldmath{x}_i^T\boldmath{x}_j\right).
\]
!et
With our hyperplane coefficients we can use our classifier to assign any observation by simply using
!bt
\[
y_i = \mathrm{sign}(\hat{w}^T\hat{x}_i+b).
y_i = \mathrm{sign}(\boldmath{w}^T\boldmath{x}_i+b).
\]
!et
Below we discuss how to find the optimal values of $\lambda_i$. Before we proceed however, we discuss now the so-called soft classifier.
@@ -444,22 +450,22 @@ figure here. One way to deal with this problem before we define the
so-called _kernel approach_, is to allow a kind of slack in the sense
that we allow some points to be on the wrong side of the margin.
We introduce thus the so-called _slack_ variables $\hat{\xi} =[\xi_1,x_2,\dots,x_n]$ and
We introduce thus the so-called _slack_ variables $\boldmath{\xi} =[\xi_1,x_2,\dots,x_n]$ and
modify our previous equation
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b)=1,
y_i(\boldmath{w}^T\boldmath{x}_i+b)=1,
\]
!et
to
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b)=1-\xi_i,
y_i(\boldmath{w}^T\boldmath{x}_i+b)=1-\xi_i,
\]
!et
with the requirement $\xi_i\geq 0$. The total violation is now $\sum_i\xi$.
The value $\xi_i$ in the constraint the last constraint corresponds to the amount by which the prediction
$y_i(\hat{w}^T\hat{x}_i+b)=1$ is on the wrong side of its margin. Hence by bounding the sum $\sum_i \xi_i$,
$y_i(\boldmath{w}^T\boldmath{x}_i+b)=1$ is on the wrong side of its margin. Hence by bounding the sum $\sum_i \xi_i$,
we bound the total amount by which predictions fall on the wrong side of their margins.
Misclassifications occur when $\xi_i > 1$. Thus bounding the total sum by some value $C$ bounds in turn the total number of
@@ -472,18 +478,18 @@ misclassifications.
This has in turn the consequences that we change our optmization problem to finding the minimum of
!bt
\[
{\cal L}=\frac{1}{2}\hat{w}^T\hat{w}-\sum_{i=1}^n\lambda_i\left[y_i(\hat{w}^T\hat{x}_i+b)-(1-\xi_)\right]+C\sum_{i=1}^n\xi_i-\sum_{i=1}^n\gamma_i\xi_i,
{\cal L}=\frac{1}{2}\boldmath{w}^T\boldmath{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldmath{w}^T\boldmath{x}_i+b)-(1-\xi_)\right]+C\sum_{i=1}^n\xi_i-\sum_{i=1}^n\gamma_i\xi_i,
\]
!et
subject to
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b)=1-\xi_i \hspace{0.1cm}\forall i,
y_i(\boldmath{w}^T\boldmath{x}_i+b)=1-\xi_i \hspace{0.1cm}\forall i,
\]
!et
with the requirement $\xi_i\geq 0$.
Taking the derivatives with respect to $b$ and $\hat{w}$ we obtain
Taking the derivatives with respect to $b$ and $\boldmath{w}$ we obtain
!bt
\[
\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0,
@@ -492,7 +498,7 @@ Taking the derivatives with respect to $b$ and $\hat{w}$ we obtain
and
!bt
\[
\frac{\partial {\cal L}}{\partial \hat{w}} = 0 = \hat{w}-\sum_{i} \lambda_iy_i\hat{x}_i,
\frac{\partial {\cal L}}{\partial \boldmath{w}} = 0 = \boldmath{w}-\sum_{i} \lambda_iy_i\boldmath{x}_i,
\]
!et
and
@@ -504,14 +510,14 @@ and
Inserting these constraints into the equation for ${\cal L}$ we obtain the same equation as before
!bt
\[
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\hat{x}_i^T\hat{x}_j,
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldmath{x}_i^T\boldmath{x}_j,
\]
!et
but now subject to the constraints $\lambda_i\geq 0$, $\sum_i\lambda_iy_i=0$ and $0\leq\lambda_i \leq C$.
We must in addition satisfy the Karush-Kuhn-Tucker condition which now reads
!bt
\[
\lambda_i\left[y_i(\hat{w}^T\hat{x}_i+b) -(1-\xi_)\right]=0 \hspace{0.1cm}\forall i,
\lambda_i\left[y_i(\boldmath{w}^T\boldmath{x}_i+b) -(1-\xi_)\right]=0 \hspace{0.1cm}\forall i,
\]
!et
!bt
@@ -522,7 +528,7 @@ We must in addition satisfy the Karush-Kuhn-Tucker condition which now reads
and
!bt
\[
y_i(\hat{w}^T\hat{x}_i+b) -(1-\xi_) \geq 0 \hspace{0.1cm}\forall i.
y_i(\boldmath{w}^T\boldmath{x}_i+b) -(1-\xi_) \geq 0 \hspace{0.1cm}\forall i.
\]
!et