9 Convex optimization
Below is an example of a convex optimization problem in the plane .
where is the subset of points in satisfying
Sketch the subset in Example
9.1
. Show that Example
9.1
really is a
convex optimization problem and solve it.
Given points
, what is the center and
radius of the smallest circle containing these points?
We can write this optimization problem as
where
Upon rewriting this turns into the optimization problem
where
and .
Prove that
(9.1)
and
(9.2)
both are convex optimization problems.
Explain how
(9.1)
is rewritten into
(9.2)
.
Hint
Expand
and put .
9.1 The optimal separating hyperplane
How do you go from
(9.4)
to
(9.5)
?
Suppose that
Let
Show that and . How can and be applied in
constructing and ?
Consider
What is special about ?
Let be the hyperplane in given by and let .
The point closest to in can be found by solving the optimization problem
Explain why
(9.6)
is a convex optimization problem.
Show how Theorem
7.39
can be used to solve this optimization problem by
first deducing the equations
for the Lagrange multiplier . Notice here that
above really contains equations, whereas
is only one equation in , where
. Solve the equations
(9.7)
for and .
How can we be sure that really is a minimum in
(9.6)
?
Finally show that the distance from to is given by the formula
Let the points be labeled by . Then the optimal hyperplane
separating the points
is given by the optimization problem
for .
Let us explicitly write up the optimization problem in Theorem
9.8
in
a very simple situation: finding the best line separating the points
and . In the notation of
(9.5)
, we have (without the
stars on and )
so that
The points are
where and .
Therefore the optimization problem in Theorem
9.8
becomes
Solve the optimization problem
(9.8)
and verify that the best line
from the optimization problem is the one we expect it to be. Also, check how
WolframAlpha solves this optimization problem.
Hint
You could maybe use Fourier-Motzkin elimination to show that
implies .
Notice that the dual optimization problem is an optimization problem in , where is the number of data points.
This can be in stark contrast to the original optimization problem in Theorem
9.8
, which is an optimization problem in , where is the dimension of the data points.
Sometimes the data points are high dimensional and it pays to solve the dual optimization problem.
Let us write down the dual optimization problem for the points in Example
9.9
. Here
so that the dual optimization problem becomes
This reduces to the optimization problem of maximizing subject to ,
which has the solution . Therefore the optimal hyperplane has normal vector
The constraints hold with equality at the two support vectors,
so and the optimal hyperplane is the line
— the perpendicular bisector between the two
points, exactly as geometry demands.
the prompt carries the passage above
9.2 Support vector machines
Three small observations that explain what sklearn is
doing.
- Check that for the constraint is exactly the constraint of Theorem 9.8 , for both values of the label .
- Show that the soft margin problem is feasible for any labeled data — no separability needed. So unlike the problem in Theorem 9.8 , it always has something to optimize.
- Suppose the data can be separated, and let be the optimum of Theorem 9.8 . Show that the slack at the soft margin optimum satisfies so the total slack vanishes as grows — the hard margin reappears.
For the second part: , and
satisfy every constraint. For the third: with
all is one candidate in the soft margin problem, so the
optimum costs at most — and the term
is part of that cost.
9.2.1 Two clusters in the plane
Live Python
The optimal separating hyperplane is completely determined by its support vectors.
Any four points in the plane, two labeled and two labeled , can be separated by a line.
The dual optimization problem
(9.10)
has one variable per data point.
Moving any of the data points a little always changes the optimal hyperplane.
9.2.2 Reading handwritten digits
Live Python
9.2.3 Separating sentences
Live Python
Live Python
In the training cell, replace the machine by
SVC(kernel="linear", C=1e6) — the hard margin of the
digits example — and rerun.
- Check that the list of sacrificed training reviews is now empty: all training reviews are on their correct sides.
- Now read the verdicts on the six unseen reviews. Compare with the soft margin's verdicts. Which figure of Chapter 2 does this pair of observations redraw (see Figure 2.41 )?
The machine misjudged "I want to watch it again tomorrow". Fix it
the practitioner's way: move that review into the training data,
Hint
Xtr = VECS[:41]
ytr = np.array([1]*20 + [-1]*20 + [1])
Xte = VECS[41:]
and retrain. The repaired review is now judged correctly — but
watch the two hesitant negative reviews in our run drift across
to the wrong side. Explain how adding a single training point can
move the boundary against other points, and why the honest
cure is more data rather than one patch per mistake.
The new point lands inside the old margin on the wrong side, so
the optimization must tilt the hyperplane to accommodate it —
and a tilt moves the decision value of every sentence,
including the ones that were barely correct. A machine patched
one complaint at a time plays whack-a-mole; a machine trained on
hundreds of reviews pins the hyperplane down from all sides at
once.
Why is almost every review a support vector, when only a few dozen
of the digits were? Explain in terms of the dimension and
the number of data points, using the polynomial analogy above:
separating labeled points in leaves a hyperplane
enormous freedom, just as threading a degree- polynomial
through data points is always possible (Chapter 2). What
would you expect to happen to the fraction of support vectors if
the training set grew to thousands of reviews?
9.2.4 Separating by non-linear functions
If no hyperplane separates the data, map the data into a higher
dimensional space, where a hyperplane can do the job.
Live Python
Live Python
9.2.5 Kernel functions
Let be given by
Then
One gleans from
(9.15)
that is a kernel function, since
(9.14)
is
satisfied for given by
Live Python
9.2.6 The Gaussian kernel and infinite dimension
Live Python
9.2.7 The kernel perceptron algorithm
Use the kernel function in Example
9.18
and the kernel perceptron algorithm to
separate
Sketch the points and the separating curve.
9.3 Interior point methods
Prove that is a convex function if and are
convex functions.
Hint
Prove and use that if is a decreasing convex function (in one variable) and is a convex function, then
is a convex function, where we assume the composition makes sense.
Let be a point in with
for and . Then
and as . If
(9.17)
has a unique optimum ,
then by using we obtain a sequence as .
9.3.1 Quadratic function with polyhedral constraints
Show that the optimization problem in Theorem
9.8
also
has the form
(9.19)
by writing up the matrices and and
the vectors and for the labeled points
with .
Quadratic optimization problems, such as the one in Theorem
9.8
,
can be solved numerically in python. The code below uses the function
minimize from scipy. It attempts (in general) to
solve the optimization problem
In the cell below the optimization problem
has been entered.
Live Python
Take a look at the input format in Example
9.24
. Can you tell which
optimization problem in this chapter is solved below? Compare the numerical
solution with your solution to the exercise below
Example
9.9
.
Live Python
Live Python
Solve the two optimization problems in each of the Exercises
7.48
,
7.49
and
7.50
numerically with
minimize from scipy as in Example
9.24
(a maximum is found by minimizing ). Check the numerical output
by actually solving the exercises.
Compute the best line separating the labeled data
((1, 0), +1), ((2, 0), +1), ((3, 0), +1), ((3, 2), +1), ((1, 1), -1), ((2, 2), -1).
using the support vector machine cell from
Section
9.2
. Which of the six points are the support
vectors?
9.4 A geometric optimality criterion
If is an optimal solution and , then
for every with , where denotes the
epsilon function in the definition of differentiability (see
Definition
7.3
). Therefore
for . This is only possible if . We have silently applied the convexity of
and the differentiability of at .
If in addition is convex and
(9.21)
holds, then
Theorem
8.23
shows that is an optimal solution.
Let us test drive this on the convex optimization problem
where and
. The constraints are linear:
is the triangle with vertices , and .
Is the vertex an optimal solution? Here
, so the linear program to solve is
A linear function attains its minimum over the triangle at a vertex
(this is the content of the exercise below), so we only have to check
three points: takes the values , and at ,
and . The optimal value is smaller than
, so the test fails — and it fails
informatively. For we have
, so by Proposition
9.28
the vertex is not optimal: decreases along
the edge from toward .
Let us instead try ,
which lies on the same edge. Now and the
linear program is
with the values , and at the three vertices: the optimal
value is . Since
the test succeeds and Proposition
9.28
(here we use that
is convex) certifies that is
an optimal solution — no further theory needed, just one linear
program.
The cell below shows both verdicts geometrically. The level sets of
are circles around the unconstrained minimum . The dotted
red circle is the level set through : it dips into the
triangle, so contains points with smaller -value and
cannot be optimal — the red arrow is the descent along the edge found
by the failed test. The green circle is the level set through
: it only touches the
triangle. It is tangent to the edge , and the tangent line
is exactly the hyperplane
from
Proposition
9.28
— compare with the picture for a curved
boundary in the next example.
Live Python
Sketch how Proposition
9.28
applies to show that an optimum in a linear programming
problem
in the plane always can be found in a vertex.
Let be a differentiable convex function and
Suppose that for . Prove that is a minimum for defined on
.
Guess the solution to the optimization problem
Show that your guess was correct!
9.5 The KKT conditions
The KKT conditions associated with the convex optimization problem in Example
9.1
are
Verify that the KKT conditions of the optimization problem in Example
9.1
are the
ones given in Example
9.34
.
The optimization problem
(9.22)
is called strictly
feasible if there exists with
If in a solution of the KKT conditions, then the -th constraint is active i.e., .
If is an optimal solution, then the KKT conditions are satisfied at , even when
(9.22)
is not strictly feasible.
The KKT conditions can be satisfied at only one point .
If the KKT conditions are satisfied at , then is an optimal solution — no strict feasibility needed.
Give an example of a convex optimization problem as in
(9.22)
, which is not strictly feasible and with an optimal solution that does
not satisfy the KKT conditions. Such an example shows that strict feasibility is necessary in Theorem
9.37
(ⅰ)
.
9.5.1 Strategy
So where exactly in
(9.23)
is the above claim verified?
Why possibilities above?
How do you solve the optimization problem (or decide there is no solution)
if ?
9.5.2 Example
9.5.3 Solving the KKT conditions by computer
Live Python
9.5.4 Closing the circle: KKT and the optimal hyperplane
Return to the two points of Example
9.9
: with
label and with label . This
time we keep all three variables
and of Theorem
9.8
and treat the problem as
(9.22)
with
and the two convex constraint functions
The problem is strictly feasible — the optimal line scaled up, say
and , satisfies both constraints
strictly — so by Theorem
9.37
the KKT conditions
characterize the optimum exactly.
The gradients with respect to the three variables
are
so the last KKT condition
splits
into two parts. The -part reads
— the mysterious linear combination
(9.9)
! That the
normal vector is built from the data points, weighted by multipliers
and labels, is not an assumption of the dual; it is forced by the
KKT conditions. The -part reads
which is exactly the dual constraint .
Next, complementary slackness. The multipliers cannot vanish: if
, the -part would give
, and then and would demand
and at once. So
, and forces
both constraints to hold with equality:
These are the support vector equations. The claim that both dashed
lines carry data points — argued after Theorem
9.8
with
a paragraph of shifting and rescaling — is here simply complementary
slackness.
The rest is arithmetic. Write . The
-part gives
,
and subtracting the two support vector equations gives
, that is
, so . Then
and by Theorem
9.37
(ⅱ)
the line is
certifiably optimal — the same answer as the dual computation
in Example
9.12
, now with a
certificate attached. (The multiplier is twice the
dual variable found in Example
9.12
; the factor is a bookkeeping
constant coming from the objective versus
, absorbed by
(9.9)
.)
9.6 Optimization exercises
- Show that (9.27) is a convex optimization problem.
- Sketch the set of constraints in and show that cannot be an optimal solution to (9.27) .
- Write up the KKT conditions for (9.27) and explain theoretically (without actually solving them) why they must have a solution.
- Now solve (9.27) . Is the solution unique?
- Show that is strictly convex.
- Let denote the subset of points satisfying Show that is a closed convex subset.
- Solve the optimization problem
-
Why does the optimization problemhave a solution?
- Find all optimal solutions to (9.29) .
- Let , where at least one of is non-zero. Show that an optimal solution to belongs to .
- Use the KKT conditions to solve the minimization problem
- Use the KKT conditions to solve the minimization problem
Solve the optimization problem
- State the KKT conditions for for .
- Suppose now that . For which and does have optimum in ? State the KKT conditions when .
- Show that is a convex function.
- Find . Is this minimum
unique? Is a strictly convex function.Let
- Apply the KKT-conditions to decide if is an optimal solution to
- Find and
- Show that is not a convex function.
- Show that is a convex function on the open subset and conclude that is convex on .
- Show that is an optimal solution for the optimization problem . Is a unique optimal solution here?
- Show that is a convex function. Is strictly convex?
- Show that is a convex subset of .
- Does there exist an optimal point for the minimization problem with ?
- Does there exist an optimal point for the minimization problem with ?
- Show that is a convex function and solve the minimization
problem .Now let and consider the minimization problem (P) given by
- Show using the KKT conditions that is not optimal for
(P).
- Find an optimal solution for (P). Is it unique?