update week 46
This commit is contained in:
@@ -37,6 +37,14 @@ doconce format html week46.do.txt --html_style=bootstrap --pygments_html_style=d
|
||||
<!-- tocinfo
|
||||
{'highest level': 2,
|
||||
'sections': [('Overview of week 46', 2, None, 'overview-of-week-46'),
|
||||
('Eventual mini-workshop on project 3, Friday November 18',
|
||||
2,
|
||||
None,
|
||||
'eventual-mini-workshop-on-project-3-friday-november-18'),
|
||||
('Workshop topics 2021 (partly online)',
|
||||
2,
|
||||
None,
|
||||
'workshop-topics-2021-partly-online'),
|
||||
('Support Vector Machines, overarching aims',
|
||||
2,
|
||||
None,
|
||||
@@ -129,33 +137,35 @@ MathJax.Hub.Config({
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs001.html#overview-of-week-46" style="font-size: 80%;">Overview of week 46</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs002.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs003.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs004.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs005.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs006.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs007.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs008.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs009.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs010.html#code-example" style="font-size: 80%;">Code Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs011.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs012.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs013.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs014.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs015.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs022.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs017.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs018.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs019.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs020.html#kernels-and-non-linearity" style="font-size: 80%;">Kernels and non-linearity</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs021.html#the-equations" style="font-size: 80%;">The equations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs022.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs023.html#different-kernels-and-mercer-s-theorem" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs024.html#the-moons-example" style="font-size: 80%;">The moons example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs025.html#mathematical-optimization-of-convex-functions" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs026.html#how-do-we-solve-these-problems" style="font-size: 80%;">How do we solve these problems?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs028.html#back-to-the-more-realistic-cases" style="font-size: 80%;">Back to the more realistic cases</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs002.html#eventual-mini-workshop-on-project-3-friday-november-18" style="font-size: 80%;">Eventual mini-workshop on project 3, Friday November 18</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs003.html#workshop-topics-2021-partly-online" style="font-size: 80%;">Workshop topics 2021 (partly online)</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs004.html#support-vector-machines-overarching-aims" style="font-size: 80%;">Support Vector Machines, overarching aims</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs005.html#hyperplanes-and-all-that" style="font-size: 80%;">Hyperplanes and all that</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs006.html#what-is-a-hyperplane" style="font-size: 80%;">What is a hyperplane?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs007.html#a-p-dimensional-space-of-features" style="font-size: 80%;">A \( p \)-dimensional space of features</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs008.html#the-two-dimensional-case" style="font-size: 80%;">The two-dimensional case</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs009.html#getting-into-the-details" style="font-size: 80%;">Getting into the details</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs010.html#first-attempt-at-a-minimization-approach" style="font-size: 80%;">First attempt at a minimization approach</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs011.html#solving-the-equations" style="font-size: 80%;">Solving the equations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs012.html#code-example" style="font-size: 80%;">Code Example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs013.html#problems-with-the-simpler-approach" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs014.html#a-better-approach" style="font-size: 80%;">A better approach</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs015.html#a-quick-reminder-on-lagrangian-multipliers" style="font-size: 80%;">A quick Reminder on Lagrangian Multipliers</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs016.html#adding-the-multiplier" style="font-size: 80%;">Adding the Multiplier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs017.html#setting-up-the-problem" style="font-size: 80%;">Setting up the Problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs024.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs019.html#the-last-steps" style="font-size: 80%;">The last steps</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs020.html#a-soft-classifier" style="font-size: 80%;">A soft classifier</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs021.html#soft-optmization-problem" style="font-size: 80%;">Soft optmization problem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs022.html#kernels-and-non-linearity" style="font-size: 80%;">Kernels and non-linearity</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs023.html#the-equations" style="font-size: 80%;">The equations</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs024.html#the-problem-to-solve" style="font-size: 80%;">The problem to solve</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs025.html#different-kernels-and-mercer-s-theorem" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs026.html#the-moons-example" style="font-size: 80%;">The moons example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="#mathematical-optimization-of-convex-functions" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs028.html#how-do-we-solve-these-problems" style="font-size: 80%;">How do we solve these problems?</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs029.html#a-simple-example" style="font-size: 80%;">A simple example</a></li>
|
||||
<!-- navigation toc: --> <li><a href="._week46-bs030.html#back-to-the-more-realistic-cases" style="font-size: 80%;">Back to the more realistic cases</a></li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
@@ -167,88 +177,26 @@ MathJax.Hub.Config({
|
||||
<p> </p><p> </p><p> </p> <!-- add vertical space -->
|
||||
<a name="part0027"></a>
|
||||
<!-- !split -->
|
||||
<h2 id="a-simple-example" class="anchor">A simple example </h2>
|
||||
<h2 id="mathematical-optimization-of-convex-functions" class="anchor">Mathematical optimization of convex functions </h2>
|
||||
|
||||
<p>We remind ourselves about the general problem we want to solve</p>
|
||||
<p>A mathematical (quadratic) optimization problem, or just optimization problem, has the form</p>
|
||||
$$
|
||||
\begin{align*}
|
||||
&\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}\boldsymbol{x}^T\boldsymbol{P}\boldsymbol{x}+\boldsymbol{q}^T\boldsymbol{x},\\ \nonumber
|
||||
&\mathrm{subject\hspace{0.1cm} to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{x} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{x}=f.
|
||||
&\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber
|
||||
&\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f.
|
||||
\end{align*}
|
||||
$$
|
||||
|
||||
<p>Let us show how to perform the optmization using a simple case. Assume we want to optimize the following problem</p>
|
||||
$$
|
||||
\begin{align*}
|
||||
&\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}x^2+5x+3y \\ \nonumber
|
||||
&\mathrm{subject to} \\ \nonumber
|
||||
&x, y \geq 0 \\ \nonumber
|
||||
&x+3y \geq 15 \\ \nonumber
|
||||
&2x+5y \leq 100 \\ \nonumber
|
||||
&3x+4y \leq 80. \\ \nonumber
|
||||
\end{align*}
|
||||
$$
|
||||
|
||||
<p>The minimization problem can be rewritten in terms of vectors and matrices as (with \( x \) and \( y \) being the unknowns)</p>
|
||||
$$
|
||||
\frac{1}{2}\begin{bmatrix} x\\ y \end{bmatrix}^T \begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix}3\\ 4 \end{bmatrix}^T \begin{bmatrix}x \\ y \end{bmatrix}.
|
||||
$$
|
||||
|
||||
<p>Similarly, we can now set up the inequalities (we need to change \( \geq \) to \( \leq \) by multiplying with \( -1 \) on bot sides) as the following matrix-vector equation</p>
|
||||
$$
|
||||
\begin{bmatrix} -1 & 0 \\ 0 & -1 \\ -1 & -3 \\ 2 & 5 \\ 3 & 4\end{bmatrix}\begin{bmatrix} x \\ y\end{bmatrix} \preceq \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}.
|
||||
$$
|
||||
|
||||
<p>We have collapsed all the inequalities into a single matrix \( \boldsymbol{G} \). We see also that our matrix </p>
|
||||
$$
|
||||
\boldsymbol{P} =\begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix}
|
||||
$$
|
||||
|
||||
<p>is clearly positive semi-definite (all eigenvalues larger or equal zero).
|
||||
Finally, the vector \( \boldsymbol{h} \) is defined as
|
||||
</p>
|
||||
$$
|
||||
\boldsymbol{h} = \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}.
|
||||
$$
|
||||
|
||||
<p>Since we don't have any equalities the matrix \( \boldsymbol{A} \) is set to zero
|
||||
The following code solves the equations for us
|
||||
<p>subject to some constraints for say a selected set \( i=1,2,\dots, n \).
|
||||
In our case we are optimizing with respect to the Lagrangian multipliers \( \lambda_i \), and the
|
||||
vector \( \boldsymbol{\lambda}=[\lambda_1, \lambda_2,\dots, \lambda_n] \) is the optimization variable we are dealing with.
|
||||
</p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class="highlight" style="background: #f8f8f8">
|
||||
<pre style="line-height: 125%;"><span style="color: #408080; font-style: italic"># Import the necessary packages</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> matrix
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> solvers
|
||||
P <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>diag([<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]), tc<span style="color: #666666">=</span>’d’)
|
||||
q <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([<span style="color: #666666">3</span>,<span style="color: #666666">4</span>]), tc<span style="color: #666666">=</span>’d’)
|
||||
G <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([[<span style="color: #666666">-1</span>,<span style="color: #666666">0</span>],[<span style="color: #666666">0</span>,<span style="color: #666666">-1</span>],[<span style="color: #666666">-1</span>,<span style="color: #666666">-3</span>],[<span style="color: #666666">2</span>,<span style="color: #666666">5</span>],[<span style="color: #666666">3</span>,<span style="color: #666666">4</span>]]), tc<span style="color: #666666">=</span>’d’)
|
||||
h <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">-15</span>,<span style="color: #666666">100</span>,<span style="color: #666666">80</span>]), tc<span style="color: #666666">=</span>’d’)
|
||||
<span style="color: #408080; font-style: italic"># Construct the QP, invoke solver</span>
|
||||
sol <span style="color: #666666">=</span> solvers<span style="color: #666666">.</span>qp(P,q,G,h)
|
||||
<span style="color: #408080; font-style: italic"># Extract optimal value and solution</span>
|
||||
sol[’x’]
|
||||
sol[’primal objective’]
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="output_wrapper">
|
||||
<div class="output">
|
||||
<div class="output_area">
|
||||
<div class="output_subarea output_stream output_stdout output_text">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>In our case we are particularly interested in a class of optimization problems called convex optmization problems.
|
||||
In our discussion on gradient descent methods we discussed at length the definition of a convex function.
|
||||
</p>
|
||||
|
||||
<p>Convex optimization problems play a central role in applied mathematics and we recommend strongly <a href="http://web.stanford.edu/~boyd/cvxbook/" target="_self">Boyd and Vandenberghe's text on the topics</a>.</p>
|
||||
|
||||
<p>
|
||||
<!-- navigation buttons at the bottom of the page -->
|
||||
@@ -266,6 +214,8 @@ sol[’primal objective’]
|
||||
<li><a href="._week46-bs026.html">27</a></li>
|
||||
<li class="active"><a href="._week46-bs027.html">28</a></li>
|
||||
<li><a href="._week46-bs028.html">29</a></li>
|
||||
<li><a href="._week46-bs029.html">30</a></li>
|
||||
<li><a href="._week46-bs030.html">31</a></li>
|
||||
<li><a href="._week46-bs028.html">»</a></li>
|
||||
</ul>
|
||||
<!-- ------------------- end of main content --------------- -->
|
||||
|
||||
Reference in New Issue
Block a user