\section{Computing Limits: Limit Laws}\label{sec:ComputingLimitsAlg}
\subsection*{Properties of limits}

In Section \ref{sec:LimitsWorkingDefn} we explored the concept of the limit without a strict definition, meaning we could only make approximations. In the previous section we gave the definition of the limit and demonstrated how to use it to verify our approximations were correct. Thus far, our method of finding a limit is 1) make a really good approximation either graphically or numerically, and 2) verify our approximation is correct using a $\epsilon$-$\delta$ proof.

Recognizing that $\epsilon$-$\delta$ proofs are cumbersome, this section gives a series of theorems which allow us to find limits much more quickly and intuitively. \\
%\vskip \baselineskip

Suppose that $\lim_{x\to 2} f(x)=2$ and $\lim_{x\to 2} g(x) = 3$. What is $\lim_{x\to 2}(f(x)+g(x))$? Intuition tells us that the limit should be $ 5 $, as we expect limits to behave in a nice way. The following theorem states that already established limits do behave nicely.

%\enlargethispage{4\baselineskip}

\begin{theorem}{Basic Limit Properties}{limit_algebra}
{
Let $a$, $c$, $L$ and $K$ be real numbers, let $n$ be a positive integer, and let $f$ and $g$ be functions with the following limits: \index{limit!properties}
$$\lim_{x\to a}f(x) = L \text{\ and\ } \lim_{x\to a} g(x) = K.$$
The following limits hold.
\begin{enumerate}
\item \parbox{160pt}{Constants:} $\displaystyle \lim_{x\to a} c = c$
\item	\parbox{160pt}{Identity }						$\displaystyle \lim_{x\to a} x = a$
\item	\parbox{160pt}{Sum/Difference Rules:} $\displaystyle \lim_{x\to a}(f(x)\pm g(x)) = L\pm K$
\item	\parbox{160pt}{Scalar Multiple Rule:}	$\displaystyle \lim_{x\to a} c\cdot f(x) = cL$
\item	\parbox{160pt}{Limit Product Rule:}	$\displaystyle \lim_{x\to a} f(x)\cdot g(x) = LK$
\item	\parbox{160pt}{Limit Quotient Rule:} $\displaystyle \lim_{x\to a} f(x)/g(x) = L/K$, ($K\neq 0)$
\item	\parbox{160pt}{Limit Power Rule:} 	$\displaystyle \lim_{x\to a} f(x)^n = L^n$
\item	\parbox{160pt}{Continuity of Roots:}		\parbox[t]{185pt}{$\displaystyle \lim_{x\to a} \sqrt[n]{f(x)} = \sqrt[n]{L}$}% \qquad \small (if $n$ is even then $L$ must be greater than 0; when $n$ is odd, it is true for all $L$.)}
\item	\parbox{80pt}{Compositions:} \parbox[t]{240pt}{Adjust our previously given limit assumptions to: $$\lim_{x\to a}g(x) = L \text{\ and\ } \lim_{x\to L} f(x) = f(L).$$ Then $\ds \lim_{x\to c}f(g(x)) = f(L)$.}
\end{enumerate}
}
\end{theorem}

We make a note about Property \#8: when $n$ is even, $L$ must be greater than 0. If $n$ is odd, then the statement is true for all $L$.

Regarding Property \#9, note the special form of the condition on $f$: it is not enough to
know that $\ds\lim_{x\to L}f(x) = M$, though it is a bit tricky to see
why. We have included an example in the exercise section to illustrate this tricky
point for those who are interested. As we shall eventually see, many of the most familiar functions do have this property, so this result can therefore be applied.

Roughly speaking, these rules say that to compute the limit of an algebraic expression, it is enough to compute the limits of the ``innermost bits'' and then combine these limits. This often means that it is possible to simply plug in a value for the variable, since
$\ds \lim_{x\to a} x =a$.

\begin{example}{Limit Properties}{LimitProperties}
Compute $\ds\lim_{x\to 1}{x^2-3x+5\over x-2}$.
\end{example}

\begin{solution}
 If we apply the theorem in all its gory detail, we get
\begin{eqnarray*}
\lim_{x\to 1}{x^2-3x+5\over x-2}&=&
{\ds\lim_{x\to 1}(x^2-3x+5)\over \ds\lim_{x\to1}(x-2)}\cr
\\
&=&{(\ds\lim_{x\to 1}x^2)-(\ds\lim_{x\to1}3x)+(\ds\lim_{x\to1}5)\over
  (\ds\lim_{x\to1}x)-(\ds\lim_{x\to1}2)}\cr
\\
&=&{(\ds\lim_{x\to 1}x)^2-3(\ds\lim_{x\to1}x)+5\over (\ds\lim_{x\to1}x)-2}\cr
\\
&=&{1^2-3\cdot1+5\over 1-2}\cr
\\
&=&{1-3+5\over -1} = -3
\end{eqnarray*}
\end{solution}

\begin{example}{Using basic limit properties}{ex_basic_limit_1}{
Let $$\lim_{x\to 2} f(x)=2,\quad\lim_{x\to 2} g(x) = 3\quad \text{\ and \ }\quad p(x) = 3x^2-5x+7.$$ Find the following limits:

\noindent\begin{minipage}[t]{.5\textwidth}
\begin{enumerate}
\item		$\ds \lim_{x\to 2} \big(f(x) + g(x)\big)$
\item		$\ds \lim_{x\to 2} \big(5f(x) + g(x)^2\big)$
\end{enumerate}
\end{minipage}
\begin{minipage}[t]{.5\textwidth}
\begin{enumerate}\addtocounter{enumi}{2}
\item		$\ds \lim_{x\to 2} p(x)$
\end{enumerate}
\end{minipage}}
\end{example}

\begin{solution}
{\begin{enumerate}
\item		Using the Sum/Difference rule, we know that $\ds \lim_{x\to 2} \big(f(x) + g(x)\big) = 2+3 =5$.
\item		Using the Scalar Multiple and Sum/Difference rules, we find that $\ds \lim_{x\to 2} \big(5f(x) + g(x)^2\big) = 5\cdot 2 + 3^2 = 19.$
\item		Here we combine the Power, Scalar Multiple, Sum/Difference and Constant Rules. We show quite a few steps, but in general these can be omitted:
				\begin{align*}
				\lim_{x\to 2} p(x) &= \lim_{x\to 2} (3x^2-5x+7) \\
				&= \lim_{x\to 2} 3x^2-\lim_{x\to 2} 5x+\lim_{x\to 2}7 \\
				 &= 3\cdot 2^2 - 5\cdot 2+7 \\
				 &= 9
				\end{align*}
\end{enumerate}
}
\end{solution}

%
Part 3 of the previous example demonstrates how the limit of a quadratic polynomial can be determined using the properties of Theorem \ref{thm:limit_algebra}. Not only that, recognize that $$\lim_{x\to 2} p(x) = 9 = p(2);$$ i.e., the limit at $ 2 $ was found just by plugging $ 2 $ into the function. This holds true for all polynomials, and also for rational functions (which are quotients of polynomials), as stated in the following theorem.

\begin{theorem}{Limits of Polynomial and Rational Functions}{poly_rat}
{Let $p(x)$ and $q(x)$ be polynomials and $c$ a real number. Then:
\begin{enumerate}
\item	$\ds \lim_{x\to c} p(x) = p(c)$
\item	$\ds \lim_{x\to c} \frac{p(x)}{q(x)} = \frac{p(c)}{q(c)}$, where $q(c) \neq 0$.
\end{enumerate}
}
\end{theorem}

\begin{example}{Finding a limit of a rational function}{ex_limit_rat}
{
Using Theorem \ref{thm:poly_rat}, find $$\lim_{x\to -1} \frac{3x^2-5x+1}{x^4-x^2+3}.$$}
\end{example}

\begin{solution}
{Using Theorem \ref{thm:poly_rat}, we can quickly state that
	\begin{align*} \lim_{x\to -1}\frac{3x^2-5x+1}{x^4-x^2+3} &= \frac{3(-1)^2-5(-1)+1}{(-1)^4-(-1)^2+3} \\
												&= \frac{9}{3} =3.
	\end{align*}
}
\end{solution}

It was likely frustrating in Example \ref{exa:ex_compute_lim2} to do a lot of work to prove that $$\lim_{x\to 2} x^2 = 4$$ as it seemed fairly obvious. The previous theorems state that many functions behave in such an ``obvious'' fashion, as demonstrated by the rational function in Example \ref{exa:ex_limit_rat}.

Polynomial and rational functions are not the only functions to behave in such a predictable way. The following theorem gives a list of functions whose behavior is particularly ``nice'' in terms of limits. In the next section, we will give a formal name to these functions that behave ``nicely.''

\begin{theorem}{Special Limits}{lim_continuous}{%
Let $c$ be a real number in the domain of the given function and let $n$ be a positive integer. The following limits hold:

\noindent\begin{minipage}[t]{.33\textwidth}
\begin{enumerate}
\item		$\ds \lim_{x\to c} \sin x = \sin c$
\item		$\ds \lim_{x\to c} \cos x = \cos c$
\item		$\ds \lim_{x\to c} \tan x = \tan c$
\end{enumerate}
\end{minipage}
\begin{minipage}[t]{.33\textwidth}
\begin{enumerate}\addtocounter{enumi}{3}
\item		$\ds \lim_{x\to c} \csc x = \csc c$
\item		$\ds \lim_{x\to c} \sec x = \sec c$
\item		$\ds \lim_{x\to c} \cot x = \cot c$
\end{enumerate}
\end{minipage}
\begin{minipage}[t]{.33\textwidth}
\begin{enumerate}\addtocounter{enumi}{6}
\item		$\ds \lim_{x\to c} a^x = a^c$ ($a>0$)
\item		$\ds \lim_{x\to c} \ln x = \ln c$
\item		$\ds \lim_{x\to c} \sqrt[n]{x} = \sqrt[n]{c}$\end{enumerate}
\end{minipage}
}
\end{theorem}

\begin{example}{Evaluating limits analytically}{ex_limit_1}{
Evaluate the following limits.

\noindent\begin{minipage}[t]{.5\textwidth}
\begin{enumerate}
\item		$\ds \lim_{x\to \pi} \cos x$
\item		$\ds \lim_{x\to 3} (\sec^2x - \tan^2 x)$
\item		$\ds \lim_{x\to \pi/2} \cos x\sin x$
\end{enumerate}
\end{minipage}
\begin{minipage}[t]{.5\textwidth}
\begin{enumerate}\addtocounter{enumi}{3}
\item		$\ds \lim_{x\to 1} e^{\ln x}$
\item		$\ds \lim_{x\to 0} \frac{\sin x}{x}$
\end{enumerate}
\end{minipage}
}
\end{example}

\begin{solution}
{
\begin{enumerate}
\item		This is a straightforward application of Theorem \ref{thm:lim_continuous}. $\ds \lim_{x\to \pi} \cos x = \cos \pi = -1$.
\item		We can approach this in at least two ways. First, by directly applying Theorem \ref{thm:lim_continuous}, we have:
				$$\lim_{x\to 3} (\sec^2x - \tan^2 x) = \sec^23-\tan^23.$$ Using the Pythagorean Theorem, this last expression is 1; therefore $$\lim_{x\to 3} (\sec^2x - \tan^2 x) = 1.$$

				We can also use the Pythagorean Theorem from the start. $$\lim_{x\to 3} (\sec^2x - \tan^2 x) = \lim_{x\to 3} 1 = 1,$$ using the Constant limit rule. Either way, we find the limit is 1.

\item		Applying the Product limit rule of Theorem \ref{thm:limit_algebra} and Theorem \ref{thm:lim_continuous} gives $$\ds \lim_{x\to \pi/2} \cos x\sin x = \cos (\pi/2)\sin(\pi/2) = 0\cdot 1 = 0.$$

\item		Again, we can approach this in two ways. First, we can use the exponential/logarithmic identity that $e^{\ln x} = x$ and evaluate $\ds \lim_{x\to 1} e^{\ln x} = \lim_{x\to 1} x = 1.$

We can also use the Composition limit rule of Theorem \ref{thm:limit_algebra}. Using Theorem \ref{thm:lim_continuous}, we have $\ds \lim_{x\to 1}\ln x = \ln 1 = 0$. Applying the Composition rule, $$\ds \lim_{x\to 1} e^{\ln x} = \lim_{x\to 0} e^x = e^0 = 1.$$ Both approaches are valid, giving the same result.

\item		We encountered this limit in Section \ref{sec:LimitsWorkingDefn}. Applying our theorems, we attempt to find the limit as $$\lim_{x\to 0}\frac{\sin x}{x}\rightarrow \frac{\sin 0}{0} \rightarrow \raisebox{8pt}{\text{``\ }}\frac{0}{0}\raisebox{8pt}{\text{\ ''}}.$$ This, of course, violates a condition of Theorem \ref{thm:limit_algebra}, as the limit of the denominator is not allowed to be 0. Therefore, we are still unable to evaluate this limit with tools we currently have at hand.
\end{enumerate}
}
\end{solution}

Our final theorem for this section will be motivated by the following example.\\

\begin{example}{Using algebra to evaluate a limit}{ex_limit_onept}
{
Evaluate the following limit: $$\lim_{x\to 1}\frac{x^2-1}{x-1}.$$
}
\end{example}

\begin{solution}
{We begin by attempting to apply Theorem \ref{thm:lim_continuous} and substituting $ 1 $ for $x$ in the quotient. This gives:
		$$\lim_{x\to 1}\frac{x^2-1}{x-1} = \frac{1^2-1}{1-1} = \raisebox{8pt}{\text{``\ }}\frac{0}{0}\raisebox{8pt}{\text{\ ''}},$$ and indeterminate form. We cannot apply the theorem.

\mfigure{.6}{Graphing $f$ in Example \ref{exa:ex_limit_onept} to understand a limit.}{fig:limitxplus1}{\begin{tikzpicture}
\begin{axis}[,minor x tick num=1,axis y line=middle,axis x line=middle,ymin=-.1,ymax=3.2,xmin=-.1,xmax=2.2,name=myplot]
\addplot [{\colorone},smooth,thick] coordinates {(0,1) (2,3)};
\fill[white,draw=black,thick] (axis cs:1,2) circle (1.5pt);
%\draw[thin,dashed,{\colortwo}] (axis cs:0,1.5) -- (axis cs:2.25,1.5);
%\draw[thin,dashed,{\colortwo}] (axis cs:0,2.5) -- (axis cs:6.25,2.5);
%\draw (axis cs:-.1,1.75) node [right]{\tiny$\left.\rule{0pt}{7.5pt}\right\}\epsilon = .5$};
%\draw (axis cs:-.1,2.25) node [right]{\tiny$\left.\rule{0pt}{7.5pt}\right\}\epsilon = .5$};
%\fill[{\colortwo}] (axis cs:2.25,1.5) circle (1pt);
%\fill[{\colortwo}] (axis cs:6.25,2.5) circle (1pt);
%\draw (axis cs:4,1) node [text width = 80pt,align=center] {\footnotesize Choose $\epsilon>0$. Then ...};
\end{axis}
%\fill[{\colortwo}] (1,1) circle (1pt);
\node [right] at (myplot.right of origin) { $x$};
\node [above] at (myplot.above origin) {$y$};
\end{tikzpicture}}

		By graphing the function, as in Figure \ref{fig:limitxplus1}, we see that the function seems to be linear, implying that the limit should be easy to evaluate. Recognize that the numerator of our quotient can be factored:
		$$\frac{x^2-1}{x-1} = \frac{(x-1)(x+1)}{x-1}.$$
		The function is not defined when $x=1$, but for all other $x$, $$\frac{x^2-1}{x-1} = \frac{(x-1)(x+1)}{x-1} = \frac{\hbox{\sout{$(x-1)$}}(x+1)}{\hbox{\sout{$x-1$}}}= x+1.$$
		Clearly $\ds \lim_{x\to 1}x+1 = 2$. Recall that when considering limits, we are not concerned with the value of the function at 1, only the value the function approaches as $x$ approaches 1. Since $(x^2-1)/(x-1)$ and $x+1$ are the same at all points except $x=1$, they both approach the same value as $x$ approaches 1. Therefore we can conclude that $$\lim_{x\to 1}\frac{x^2-1}{x-1}=2.$$
}
\end{solution}

The key to the above example is that the functions $y=(x^2-1)/(x-1)$ and $y=x+1$ are identical except at $x=1$. Since limits describe a value the function is approaching, not the value the function actually attains, the limits of the two functions are always equal.

\begin{theorem}{Limits of Functions Equal At All But One Point}{limit_allbut1}
{Let $g(x) = f(x)$ for all $x$ in an open interval, except possibly at $c$, and let $\ds \lim_{x\to c} g(x) = L$ for some real number $L$. Then $$\lim_{x\to c}f(x) = L.$$}
\end{theorem}

The Fundamental Theorem of Algebra tells us that when dealing with a rational function of the form $g(x)/f(x)$ and directly evaluating the limit $\ds \lim_{x\to c} \frac{g(x)}{f(x)}$ returns ``$ 0/0 $'', % $\ds\raisebox{8pt}{\text{``\ }}\frac{0}{0}\raisebox{8pt}{\text{\ ''}}$,
then $(x-c)$ is a factor of both $g(x)$ and $f(x)$. One can then use algebra to factor this term out, cancel, then apply Theorem \ref{thm:limit_allbut1}. We demonstrate this once more.\\

\begin{example}{Evaluating a limit using Theorem \ref{thm:limit_allbut1}}{ex_limit_allbut1}
{Evaluate $\ds \lim_{x\to 3} \frac{x^3-2 x^2-5 x+6}{2 x^3+3 x^2-32 x+15}$.}
\end{example}

\begin{solution}
{We begin by applying Theorem \ref{thm:lim_continuous} and substituting 3 for $x$. This returns the familiar indeterminate form of ``0/0''. %\zerooverzero.
Since the numerator and denominator are each polynomials, we know that $(x-3)$ is factor of each. Using whatever method is most comfortable to you, factor out $(x-3)$ from each (using polynomial division, synthetic division, a computer algebra system, etc.). We find that $$\frac{x^3-2 x^2-5 x+6}{2 x^3+3 x^2-32 x+15} = \frac{(x-3)(x^2+x-2)}{(x-3)(2 x^2+9 x-5)}.$$ We can cancel the $(x-3)$ terms as long as $x\neq 3$. Using Theorem \ref{thm:limit_allbut1} we conclude:
		\begin{align*}
		\lim_{x\to 3} \frac{x^3-2 x^2-5 x+6}{2 x^3+3 x^2-32 x+15} &= \lim_{x\to 3}\frac{(x-3)(x^2+x-2)}{(x-3)(2 x^2+9 x-5)} \\
																															&=	\lim_{x\to 3} \frac{(x^2+x-2)}{(2 x^2+9 x-5)}\\
																															&= \frac{10}{40} = \frac14.
		\end{align*}
}
\end{solution}

\begin{example}{Left and Right Limit}{leftright}
Evaluate $\ds\lim_{x\to 0}{x\over|x|}$.
\end{example}

\begin{solution}
The function $f(x)=x/|x|$ is undefined at 0; when $x>0$, $|x|=x$ and
so $f(x)=1$; when $x<0$, $|x|=-x$ and $f(x)=-1$. Thus
$$\ds \lim_{x\to 0^-}{x\over|x|}=\lim_{x\to 0^-}-1=-1$$
while
$$\ds \lim_{x\to 0^+}{x\over|x|}=\lim_{x\to 0^+}1=1.$$
The limit of $f(x)$ must be equal to both the left and right limits; since they are
different, the limit $\ds \lim_{x\to 0}{x\over|x|}$ does not exist.
\end{solution}

Another of the most common algebraic tricks is called \textit{rationalization}.
Rationalizing makes use of the difference of squares formula $(a-b)(a+b)=a^2-b^2$.
Here is an example.

\begin{example}{Rationalizing}{Rationalizing}
Compute $\ds\lim_{x\to-1} {\sqrt{x+5}-2\over x+1}$.
\end{example}

\begin{solution}
\begin{eqnarray*}
\lim_{x\to-1} {\sqrt{x+5}-2\over x+1}&=&
\lim_{x\to-1} {\sqrt{x+5}-2\over x+1}\cdot{\sqrt{x+5}+2\over \sqrt{x+5}+2}\cr
\\
&=&\lim_{x\to-1} {x+5-4\over (x+1)(\sqrt{x+5}+2)}\cr
\\
&=&\lim_{x\to-1} {x+1\over (x+1)(\sqrt{x+5}+2)}\cr
\\
&=&\lim_{x\to-1} {1\over \sqrt{x+5}+2}={1\over4}
\end{eqnarray*}
At the very last step we have used the last two parts of Theorem  \ref{limit_algebra}.
\end{solution}

We end this section by revisiting a limit first seen in Section \ref{sec:LimitsWorkingDefn}, a limit of a difference quotient. Let $f(x) = -1.5x^2+11.5x$; we approximated the limit $\ds \lim_{h\to 0}\frac{f(1+h)-f(1)}{h}\approx 8.5.$ We formally evaluate this limit in the following example.\\

\begin{example}{Evaluating the limit of a difference quotient}{ex_limit_diffquot}{
Let $f(x) = -1.5x^2+11.5x$; find $\ds \lim_{h\to 0}\frac{f(1+h)-f(1)}{h}.$}
\end{example}

\begin{solution}
{Since $f$ is a polynomial, our first attempt should be to employ Theorem \ref{thm:lim_continuous} and substitute 0 for $h$. However, we see that this gives us ``$0/0$.'' %\zerooverzero.
 Knowing that we have a rational function hints that some algebra will help. Consider the following steps:
		\begin{align*}
		\lim_{h\to 0}\frac{f(1+h)-f(1)}{h} 	&= 	\lim_{h\to 0}\frac{-1.5(1+h)^2 + 11.5(1+h) - \left(-1.5(1)^2+11.5(1)\right)}{h} \\
																				&=	\lim_{h\to 0}\frac{-1.5(1+2h+h^2) + 11.5+11.5h - 10}{h}\\
																				&=	\lim_{h\to 0}\frac{-1.5h^2 +8.5h}{h}\\
																				&= 	\lim_{h\to 0}\frac{h(-1.5h+8.5)}h\\
																				&=	\lim_{h\to 0}(-1.5h+8.5) \quad (\text{\small using Theorem \ref{thm:limit_allbut1}, as $h\neq 0$}) \\
																				&= 	8.5 \quad (\text{\small using Theorem \ref{thm:lim_continuous}})
		\end{align*}
This matches our previous approximation.
}
\end{solution}

This section contains several valuable tools for evaluating limits. One of the main results of this section is Theorem \ref{thm:lim_continuous}; it states that many functions that we use regularly behave in a very nice, predictable way.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:ComputingLimitsAlg}}

\begin{enumialphparenastyle}

%%%%%%%%%%
% % % % % % % % % % %
\begin{ex}
{Explain in your own words, without using $\epsilon$-$\delta$ formality, why $\ds \lim_{x\to c} b = b$.}

\begin{sol}
{Answers will vary.
}
\end{sol}

\end{ex}
% % % % % % % % % % % %
% % % % % % % % % % %
\begin{ex}
{Explain in your own words, without using $\epsilon$-$\delta$ formality, why $\ds \lim_{x\to c} x = c$.}

\begin{sol}
{Answers will vary.
}
\end{sol}

\end{ex}
% % % % % % % % % % % %
% % % % % % % % % % %
\begin{ex}
{What does the text mean when it says that certain functions' ``behavior is `nice' in terms of limits''? What, in particular, is ``nice''?}

\begin{sol}
{Answers will vary.
}
\end{sol}

\end{ex}
% % % % % % % % % % % %

% % % % % % % % % % %
\begin{ex}
Using:

\begin{tabular}{lll}
$\ds \lim_{x\to9}f(x) = 6$ & \quad\quad &$\ds \lim_{x\to6} f(x) = 9$\\
$\ds \lim_{x\to9}g(x) = 3$ &  & $\ds \lim_{x\to6} g(x) = 3$
\end{tabular}

\noindent evaluate the following limits, where possible. If it is not possible to know, state so.
\begin{enumerate}
\item {$\ds \lim_{x\to9}(f(x)+g(x))$}
\item  {$\ds \lim_{x\to9}(3f(x)/g(x))$}
\item {$\ds \lim_{x\to9}\left(\frac{f(x)-2g(x)}{g(x)}\right)$}
\item {$\ds \lim_{x\to6}\left(\frac{f(x)}{3-g(x)}\right)$}
\item  {$\ds \lim_{x\to9}g\big(f(x)\big)$}
\item {$\ds \lim_{x\to6}f\big(g(x)\big)$}
\item {$\ds \lim_{x\to6}g\big(f(f(x))\big)$}
\item {$\ds \lim_{x\to6}f(x)g(x)-f\,^2(x)+g^2(x)$}
\end{enumerate}

\begin{sol}
\begin{enumerate}
\item
{9}
\item
{6}
\item
{0}
\item
{Limit does not exist.}
\item
{3}
\item
{Not possible to know.}
\item
{3}
\item
{$-45$}
\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

% % % % % % % % % % %
\begin{ex}
\noindent Using:

\begin{tabular}{lll}
$\ds \lim_{x\to1}f(x) = 2$ & \quad\quad &$\ds \lim_{x\to10} f(x) = 1$\\
$\ds \lim_{x\to1}g(x) = 0$ &  & $\ds \lim_{x\to10} g(x) = \pi$
\end{tabular}

\noindent evaluate the limits given, where possible. If it is not possible to know, state so.
\begin{enumerate}
\item {$\ds \lim_{x\to1}f(x)^{g(x)}$}

\item {$\ds \lim_{x\to10}\cos \big(g(x)\big)$}

\item {$\ds \lim_{x\to1}f(x)g(x)$}

\item {$\ds \lim_{x\to1}g\big(5f(x)\big)$}

\end{enumerate}

\begin{sol}
\begin{enumerate}
\item {$1$}
\item {$-1$}
\item {$0$}
\item {$\pi$}
\end{enumerate}
\end{sol}

\end{ex}
% % % % % % % % % % % %

\begin{ex}
Compute the limits. If a limit does not exist, explain why.
\begin{multicols}{2}
\begin{enumerate}
	\item	$\ds \lim_{x\to 3}{x^2+x-12\over x-3}$
	\item	$\ds \lim_{x\to 1}{x^2+x-12\over x-3}$
	\item	$\ds \lim_{x\to -4}{x^2+x-12\over x-3}$
	\item {$\ds \lim_{x\to\pi}\frac{3x+1}{1-x}$}
	\item {$\ds \lim_{x\to\pi}\frac{x^2+3x+5}{5x^2-2x-3}$}

	\item {$\ds \lim_{x\to\pi}\left(\frac{x-3}{x-5}\right)^7$}

	\item {$\ds \lim_{x\to\pi/4}\cos x\sin x$}

	\item {$\ds \lim_{x\to0}\ln x$}

	\item  {$\ds \lim_{x\to3}4^{x^3-8x}$}

	\item	$\ds \lim_{x\to 2} {x^2+x-12\over x-2}$
	\item	$\ds \lim_{x\to 1} {\sqrt{x+8}-3\over x-1}$
	\item	$\ds \lim_{x\to 0^+} \sqrt{{1\over x}+2} - \sqrt{1\over x}$
	\item	$\ds\lim _{x\to 2} 3$
	\item	$\ds\lim _{x\to 4 } 3x^3 - 5x $
	\item	$\ds \lim _{x\to 0 } {4x - 5x^2\over x-1}$
	\item	$\ds\lim _{x\to 1 } {x^2 -1 \over x-1 }$
	\item	$\ds\lim _{x\to 0^ + } {\sqrt{2-x^2 }\over x}$
	\item	$\ds\lim _{x\to 0^ + } {\sqrt{2-x^2}\over x+1}$
	\item	$\ds\lim _{x\to a } {x^3 -a^3\over x-a}$
	\item	$\ds\lim _{x\to 2 } (x^2 +4)^3$
\end{enumerate}
\end{multicols}
\begin{sol}
\begin{multicols}{2}
\begin{enumerate}
	\item	7
	\item	5
	\item	0
	\item {$\frac{3\pi+1}{1-\pi}$}
	\item {$\frac{\pi^2+3\pi+5}{5\pi^2-2\pi-3} \approx 0.6064$}
	\item 	{$-0.000000015\approx 0$}
	\item {$1/2$}
	\item 	{Limit does not exist}
	\item 	{$64$}
	\item	undefined
	\item	$1/6$
	\item	0
	\item	3
	\item	172
	\item	0
	\item	2
	\item	does not exist
	\item	$\ds \sqrt2$
	\item	$\ds 3a^2$
	\item	512
\end{enumerate}
\end{multicols}
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Let $f(x)=\left\{
\begin{array}{cc}
1 & \text{if }x\neq 0 \\
0 & \text{if }x=0%
\end{array}%
\right.$ and $g(x)=0$. What are the values of $
L=\lim_{x\to 0}g(x)$ and $M=\lim_{x\to L}f(x)$? Is it true that $\lim_{x\to	0}f(g(x))=M$? What are some noteworthy differences
between this example and part \# 9 of Theorem~\ref{thm:limit_algebra}?
\begin{sol}
	$L=0$ and $M=1.$ No.
\end{sol}
\end{ex}

\end{enumialphparenastyle}

\clearpage


\section{Dynamic Programming}

\subsection{Exercise 4.1}
\subsubsection*{Q}
In Example 4.1, if $\pi$ is the equiprobable random policy, what is $q_\pi(11, \mathtt{down})$? What is $q_\pi(7, \mathtt{down})$?

\subsubsection*{A}
$q_\pi(11, \mathtt{down}) = -1$ since goes to terminal state. $q_\pi(7, \mathtt{down}) = -15$.

\subsection{Exercise 4.2}
\subsubsection*{Q}
In Example 4.1, suppose a new state $15$ is added to the gridworld just below state $13$, and its actions, \texttt{left}, \texttt{up}, \texttt{right}, and \texttt{down}, take the agent to states $12$, $13$, $14$, and $15$, respectively. Assume that the transitions from the original states are unchanged. What, then, is $v_\pi(15)$ for the equiprobable random policy? Now suppose the dynamics of state $13$ are also changed, such that action down from state $13$ takes the agent to the new state $15$. What is $v_\pi(15)$ for the equiprobable random policy in this case?

\subsubsection*{A}
$v_\pi(15) = -20$ if dynamics unchanged. If dynamics changed then apparently the state value is the same, but you would need to verify Bellman equations for all states for this.

\subsection{Exercise 4.3}
\subsubsection*{Q}
What are the equations analogous to (4.3), (4.4), and (4.5) for the action-value function $q_\pi$ and its successive approximations by a sequence of functions $q_0, q_1, q_2, \dots$?

\subsubsection*{A}
\begin{equation}
    q_{k+1}(s, a) = \sum_{s', r} p(s', r | s, a)\left[r + \gamma \sum_{a'} \pi(a'|s)q_k(s', a')\right]
\end{equation}

\subsection{Exercise 4.4}
\subsubsection*{Q}
The policy iteration algorithm on the previous page has a subtle bug in that it may never terminate if the policy continually switches between two or more policies that are equally good. This is ok for pedagogy, but not for actual use. Modify the pseudocode so that convergence is guaranteed.

\subsubsection*{A}
One problem is that the $\argmax_a$ has ties broken arbitrarily, this means that the same value function can give rise to different policies.\\

The way to solve this is to change the algorithm to take the whole set of maximal actions on each step and see if this set is stable and see if the policy is stable with respect to choosing actions from this set.

\subsection{Exercise 4.5}
\subsubsection*{Q}
How would policy iteration be defined for action values? Give a complete algorithm for computing $q_*$, analogous to that on page 80 for computing $q_*$. Please pay special attention to this exercise, because the ideas involved will be used throughout the rest of the book.

\subsubsection*{A}
We know that
\begin{equation}
    v_\pi(s) = \sum_{a \in \mathcal{A}(s)} \pi(a|s)q_\pi(s, a)
\end{equation}
so we know that
\begin{equation}
    q_\pi(s, \pi'(s)) \geq \sum_{a \in \mathcal{A}(s)} \pi(a|s)q_\pi(s, a)
\end{equation}
if $\pi'$ is greedy with respect to $\pi$. So we know the algorithm still works for action values.\\

All there is now is to substitute the update for the action-value update and make the policy greedy with respect to the last iteration's action-values. Also need to make sure that the $\argmax_a$ is done consistently.

\subsection{Exercise 4.6}
\subsubsection*{Q}
Suppose you are restricted to considering only policies that are $\varepsilon$-soft, meaning that the probability of selecting each action in each state, $s$, is at least $\varepsilon / |\mathcal{A}(s)|$. Describe qualitatively the changes that would be required in each of the steps 3, 2, and 1, in that order, of the policy iteration algorithm for $v_\pi$ (page 80).

\subsubsection*{A}
\begin{enumerate}
    \item No change (but need policy to be able to be stochastic of course)
    \item Need to re-write the Bellman update $v(s) \longleftarrow \sum_{a \in \mathcal{A}(s)} \pi(a|s)\sum_{s', r}p(s', r|s, a)\left[ r + \gamma v(s') \right]$
    \item Construct a greedy policy that puts weight on the greedy actions but is $\varepsilon$-soft. Be careful with the consistency of the $\argmax$.
\end{enumerate}

\subsection{Exercise 4.7 (programming): Jack's Car Rental}

\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_questions/jacks_car_rental_example.png}

First we reproduce the original results.

\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_output/ex_4_7/jacks_car_rental/jacks_car_rental.png}

\subsubsection*{Q}
Write a program for policy iteration and re-solve Jack’s car rental problem with the following changes. One of Jack’s employees at the first location rides a bus home each night and lives near the second location. She is happy to shuttle one car to the second location for free. Each additional car still costs \$$2$, as do all cars moved in the other direction. In addition, Jack has limited parking space at each location. If more than $10$ cars are kept overnight at a location (after any moving of cars), then an additional cost of \$$4$ must be incurred to use a second parking lot (independent of how many cars are kept there). These sorts of nonlinearities and arbitrary dynamics often occur in real problems and cannot easily be handled by optimisation methods other than dynamic programming. To check your program, first replicate the results given for the original problem. If your computer is too slow for the full problem, cut all the numbers of cars in half.

\subsubsection*{A}
\ProgrammingExercise\\
\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_output/ex_4_7/altered_car_rental.png}

\subsection{Exercise 4.8}
\subsubsection*{Q}
Why does the optimal policy for the gambler’s problem have such a curious form? In particular, for capital of 50 it bets it all on one flip, but for capital of 51 it does not. Why is this a good policy?

\subsubsection*{A}
Since the coin is biased against us, we want to minimize the number of flips that we take. At 50 we can win with probability 0.4. At 51 if we bet small then we can get up to 52, but if we lose then we are still only back to 50 and we can again with with probability 0.4. (There is a whole paper on this problem called how to gamble if you must.)

\subsection{Exercise 4.9 (programming): Gambler's Problem}
\subsubsection*{Q}
Implement value iteration for the gambler's problem and solve it for $p_h = 0.25$ and $p_h = 0.55$. In programming, you may find it convenient to introduce two dummy states corresponding to termination with capital of $0$ and $100$, giving them values of $0$ and $1$ respectively. Show your results graphically, as in Figure 4.3. Are your results stable as $\theta \to 0$?

\subsubsection*{A}
\ProgrammingExercise\\

The process was stable as $\theta \to 0$ for $\P{}(\mathtt{win}) < 0.5$.

\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_output/ex_4_9/values_and_policy_pwin_25.eps}

\includegraphics[width=\textwidth]{\ProjectDir/data/exercise_output/ex_4_9/values_and_policy_pwin_55.eps}

\subsection{Exercise 4.10}
\subsubsection*{Q}
What is the analog of the value iteration update (4.10) for action values, $q_{k+1}(s, a)$?

\subsubsection*{A}
\begin{equation}
    q_{k+1} = \max_{a'} \sum_{s', r} p(s', r| s, a)\left[r + \gamma q_k(s', a')\right]
\end{equation}


\section{Clustering}

\ifCPy

\cvCPyFunc{KMeans2}
Splits set of vectors by a given number of clusters.

\cvdefC{int cvKMeans2(const CvArr* samples, int nclusters,\par
                      CvArr* labels, CvTermCriteria termcrit,\par
                      int attempts=1, CvRNG* rng=0, \par
                      int flags=0, CvArr* centers=0,\par
                      double* compactness=0);}
\cvdefPy{KMeans2(samples,nclusters,labels,termcrit)-> None}

\begin{description}
\cvarg{samples}{Floating-point matrix of input samples, one row per sample}
\cvarg{nclusters}{Number of clusters to split the set by}
\cvarg{labels}{Output integer vector storing cluster indices for every sample}
\cvarg{termcrit}{Specifies maximum number of iterations and/or accuracy (distance the centers can move by between subsequent iterations)}
\ifC
\cvarg{attempts}{How many times the algorithm is executed using different initial labelings. The algorithm returns labels that yield the best compactness (see the last function parameter)}
\cvarg{rng}{Optional external random number generator; can be used to fully control the function behaviour}
\cvarg{flags}{Can be 0 or \texttt{CV\_KMEANS\_USE\_INITIAL\_LABELS}. The latter
value means that during the first (and possibly the only) attempt, the
function uses the user-supplied labels as the initial approximation
instead of generating random labels. For the second and further attempts,
the function will use randomly generated labels in any case}
\cvarg{centers}{The optional output array of the cluster centers}
\cvarg{compactness}{The optional output parameter, which is computed as
$\sum_i ||\texttt{samples}_i - \texttt{centers}_{\texttt{labels}_i}||^2$
after every attempt; the best (minimum) value is chosen and the
corresponding labels are returned by the function. Basically, the
user can use only the core of the function, set the number of
attempts to 1, initialize labels each time using a custom algorithm
(\texttt{flags=CV\_KMEAN\_USE\_INITIAL\_LABELS}) and, based on the output compactness
or any other criteria, choose the best clustering.}
\fi
\end{description}

The function \texttt{cvKMeans2} implements a k-means algorithm that finds the
centers of \texttt{nclusters} clusters and groups the input samples
around the clusters. On output, $\texttt{labels}_i$ contains a cluster index for
samples stored in the i-th row of the \texttt{samples} matrix.

\ifC
% Example: Clustering random samples of multi-gaussian distribution with k-means
\begin{lstlisting}
#include "cxcore.h"
#include "highgui.h"

void main( int argc, char** argv )
{
    #define MAX_CLUSTERS 5
    CvScalar color_tab[MAX_CLUSTERS];
    IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
    CvRNG rng = cvRNG(0xffffffff);

    color_tab[0] = CV_RGB(255,0,0);
    color_tab[1] = CV_RGB(0,255,0);
    color_tab[2] = CV_RGB(100,100,255);
    color_tab[3] = CV_RGB(255,0,255);
    color_tab[4] = CV_RGB(255,255,0);

    cvNamedWindow( "clusters", 1 );

    for(;;)
    {
        int k, cluster_count = cvRandInt(&rng)%MAX_CLUSTERS + 1;
        int i, sample_count = cvRandInt(&rng)%1000 + 1;
        CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 );
        CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 );

        /* generate random sample from multigaussian distribution */
        for( k = 0; k < cluster_count; k++ )
        {
            CvPoint center;
            CvMat point_chunk;
            center.x = cvRandInt(&rng)%img->width;
            center.y = cvRandInt(&rng)%img->height;
            cvGetRows( points,
                       &point_chunk,
                       k*sample_count/cluster_count,
                       (k == (cluster_count - 1)) ?
                           sample_count :
                           (k+1)*sample_count/cluster_count );
            cvRandArr( &rng, &point_chunk, CV_RAND_NORMAL,
                       cvScalar(center.x,center.y,0,0),
                       cvScalar(img->width/6, img->height/6,0,0) );
        }

        /* shuffle samples */
        for( i = 0; i < sample_count/2; i++ )
        {
            CvPoint2D32f* pt1 =
                (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
            CvPoint2D32f* pt2 =
                (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
            CvPoint2D32f temp;
            CV_SWAP( *pt1, *pt2, temp );
        }

        cvKMeans2( points, cluster_count, clusters,
                   cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ));

        cvZero( img );

        for( i = 0; i < sample_count; i++ )
        {
            CvPoint2D32f pt = ((CvPoint2D32f*)points->data.fl)[i];
            int cluster_idx = clusters->data.i[i];
            cvCircle( img,
                      cvPointFrom32f(pt),
                      2,
                      color_tab[cluster_idx],
                      CV_FILLED );
        }

        cvReleaseMat( &points );
        cvReleaseMat( &clusters );

        cvShowImage( "clusters", img );

        int key = cvWaitKey(0);
        if( key == 27 )
            break;
    }
}
\end{lstlisting}

\cvCPyFunc{SeqPartition}
Splits a sequence into equivalency classes.

\cvdefC{
int cvSeqPartition( \par const CvSeq* seq,\par CvMemStorage* storage,\par CvSeq** labels,\par CvCmpFunc is\_equal,\par void* userdata );
}

\begin{description}
\cvarg{seq}{The sequence to partition}
\cvarg{storage}{The storage block to store the sequence of equivalency classes. If it is NULL, the function uses \texttt{seq->storage} for output labels}
\cvarg{labels}{Ouput parameter. Double pointer to the sequence of 0-based labels of input sequence elements}
\cvarg{is\_equal}{The relation function that should return non-zero if the two particular sequence elements are from the same class, and zero otherwise. The partitioning algorithm uses transitive closure of the relation function as an equivalency critria}
\cvarg{userdata}{Pointer that is transparently passed to the \texttt{is\_equal} function}
\end{description}

\begin{lstlisting}
typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);
\end{lstlisting}

The function \texttt{cvSeqPartition} implements a quadratic algorithm for
splitting a set into one or more equivalancy classes. The function
returns the number of equivalency classes.

% Example: Partitioning a 2d point set
\begin{lstlisting}

#include "cxcore.h"
#include "highgui.h"
#include <stdio.h>

CvSeq* point_seq = 0;
IplImage* canvas = 0;
CvScalar* colors = 0;
int pos = 10;

int is_equal( const void* _a, const void* _b, void* userdata )
{
    CvPoint a = *(const CvPoint*)_a;
    CvPoint b = *(const CvPoint*)_b;
    double threshold = *(double*)userdata;
    return (double)((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)) <=
        threshold;
}

void on_track( int pos )
{
    CvSeq* labels = 0;
    double threshold = pos*pos;
    int i, class_count = cvSeqPartition( point_seq,
                                         0,
                                         &labels,
                                         is_equal,
                                         &threshold );
    printf("%4d classes\n", class_count );
    cvZero( canvas );

    for( i = 0; i < labels->total; i++ )
    {
        CvPoint pt = *(CvPoint*)cvGetSeqElem( point_seq, i );
        CvScalar color = colors[*(int*)cvGetSeqElem( labels, i )];
        cvCircle( canvas, pt, 1, color, -1 );
    }

    cvShowImage( "points", canvas );
}

int main( int argc, char** argv )
{
    CvMemStorage* storage = cvCreateMemStorage(0);
    point_seq = cvCreateSeq( CV_32SC2,
                             sizeof(CvSeq),
                             sizeof(CvPoint),
                             storage );
    CvRNG rng = cvRNG(0xffffffff);

    int width = 500, height = 500;
    int i, count = 1000;
    canvas = cvCreateImage( cvSize(width,height), 8, 3 );

    colors = (CvScalar*)cvAlloc( count*sizeof(colors[0]) );
    for( i = 0; i < count; i++ )
    {
        CvPoint pt;
        int icolor;
        pt.x = cvRandInt( &rng ) % width;
        pt.y = cvRandInt( &rng ) % height;
        cvSeqPush( point_seq, &pt );
        icolor = cvRandInt( &rng ) | 0x00404040;
        colors[i] = CV_RGB(icolor & 255,
                           (icolor >> 8)&255,
                           (icolor >> 16)&255);
    }

    cvNamedWindow( "points", 1 );
    cvCreateTrackbar( "threshold", "points", &pos, 50, on_track );
    on_track(pos);
    cvWaitKey(0);
    return 0;
}
\end{lstlisting}

\fi

\fi

\ifCpp

\cvCppFunc{kmeans}
Finds the centers of clusters and groups the input samples around the clusters.
\cvdefCpp{double kmeans( const Mat\& samples, int clusterCount, Mat\& labels,\par
               TermCriteria termcrit, int attempts,\par
               int flags, Mat* centers );}
\begin{description}
\cvarg{samples}{Floating-point matrix of input samples, one row per sample}
\cvarg{clusterCount}{The number of clusters to split the set by}
\cvarg{labels}{The input/output integer array that will store the cluster indices for every sample}
\cvarg{termcrit}{Specifies maximum number of iterations and/or accuracy (distance the centers can move by between subsequent iterations)}

\cvarg{attempts}{How many times the algorithm is executed using different initial labelings. The algorithm returns the labels that yield the best compactness (see the last function parameter)}
\cvarg{flags}{It can take the following values:
\begin{description}
\cvarg{KMEANS\_RANDOM\_CENTERS}{Random initial centers are selected in each attempt}
\cvarg{KMEANS\_PP\_CENTERS}{Use kmeans++ center initialization by Arthur and Vassilvitskii}
\cvarg{KMEANS\_USE\_INITIAL\_LABELS}{During the first (and possibly the only) attempt, the
function uses the user-supplied labels instaed of computing them from the initial centers. For the second and further attempts, the function will use the random or semi-random centers (use one of \texttt{KMEANS\_*\_CENTERS} flag to specify the exact method)}
\end{description}}
\cvarg{centers}{The output matrix of the cluster centers, one row per each cluster center}
\end{description}

The function \texttt{kmeans} implements a k-means algorithm that finds the
centers of \texttt{clusterCount} clusters and groups the input samples
around the clusters. On output, $\texttt{labels}_i$ contains a 0-based cluster index for
the sample stored in the $i^{th}$ row of the \texttt{samples} matrix.

The function returns the compactness measure, which is computed as
\[
\sum_i \|\texttt{samples}_i - \texttt{centers}_{\texttt{labels}_i}\|^2
\]
after every attempt; the best (minimum) value is chosen and the
corresponding labels and the compactness value are returned by the function.
Basically, the user can use only the core of the function, set the number of
attempts to 1, initialize labels each time using some custom algorithm and pass them with
\par (\texttt{flags}=\texttt{KMEANS\_USE\_INITIAL\_LABELS}) flag, and then choose the best (most-compact) clustering.

\cvCppFunc{partition}
Splits an element set into equivalency classes.

\cvdefCpp{template<typename \_Tp, class \_EqPredicate> int\newline
    partition( const vector<\_Tp>\& vec, vector<int>\& labels,\par
               \_EqPredicate predicate=\_EqPredicate());}
\begin{description}
\cvarg{vec}{The set of elements stored as a vector}
\cvarg{labels}{The output vector of labels; will contain as many elements as \texttt{vec}. Each label \texttt{labels[i]} is 0-based cluster index of \texttt{vec[i]}}
\cvarg{predicate}{The equivalence predicate (i.e. pointer to a boolean function of two arguments or an instance of the class that has the method \texttt{bool operator()(const \_Tp\& a, const \_Tp\& b)}. The predicate returns true when the elements are certainly if the same class, and false if they may or may not be in the same class}
\end{description}

The generic function \texttt{partition} implements an $O(N^2)$ algorithm for
splitting a set of $N$ elements into one or more equivalency classes, as described in \url{http://en.wikipedia.org/wiki/Disjoint-set_data_structure}. The function
returns the number of equivalency classes.

\fi


% !TEX root =main.tex

%\section{Notations}\label{sec:notation-table}
%
%We summarise our notation in Table \ref{table:notation-table}.
%
%\vspace{-4mm}
%\begin{table}[!htbp]
%%\begin{footnotesize}
%\small{
%\begin{center}
%%\footnotesize{
%
%%\scalebox{.98}{
%%\begin{minipage}{.9\linewidth}
%\caption{ \small Notation Table.}\label{table:notation-table}
%\renewcommand{\arraystretch}{.7}
%%\resizebox{\columnwidth}{!}{
%
%% 1st table
%\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
%
%\hline
%
%\cellcolor{gray!30}\scriptsize \textbf{Setting} &\cellcolor{gray!30} \scriptsize \textbf{Symbol}&\cellcolor{gray!30} \scriptsize \textbf{Description}  \\
%    \hline
%
%     \hline
%
%%Generic
%\multirow{9}{*}{\rotatebox[origin=c]{90}{\scriptsize \textbf{Generic}}}
%
% &\cellcolor{white!20}\scriptsize$z$&\cellcolor{white!20}\scriptsize \text{Number of puzzles or delegated verifications}\\
%
%&\cellcolor{gray!20}\scriptsize$h$, $h_{\scriptscriptstyle j}$ &\cellcolor{gray!20}\scriptsize Hash values  \\
%
%&\cellcolor{white!20}\scriptsize$d$, $d_{\scriptscriptstyle j}$ &\cellcolor{white!20}\scriptsize Randomness  of commitment\\
%
%&\cellcolor{gray!20}\scriptsize$n$ &\cellcolor{gray!20}\scriptsize Number of file blocks\\
%&\cellcolor{white!20}\scriptsize$m$, $m_{\scriptscriptstyle j}$ &\cellcolor{white!20}\scriptsize Plaintext messages\\
%  &\cellcolor{gray!20}\scriptsize$\ddot{o}$ &\cellcolor{gray!20}\scriptsize Pair representation\\
%&\cellcolor{white!20}\scriptsize$\vv{\bm{o}}$ &\cellcolor{white!20}\scriptsize Vector representation\\
%&\cellcolor{gray!20}\scriptsize$\ddot{p}:(m_{\scriptscriptstyle j},d_{\scriptscriptstyle j})$ &\cellcolor{gray!20}\scriptsize Commitment opening\\
%
%&\cellcolor{white!20}\scriptsize$\mathtt{H}$ &\cellcolor{white!20}\scriptsize Hash function\\
%
%
% \hline
%
%  \hline
%
%
%    %CR-TLP
%\multirow{11}{*}{\rotatebox[origin=c]{90}{\scriptsize  \textbf{C-TLP}}}
%
%&\cellcolor{gray!20}\scriptsize$\lambda$&\cellcolor{gray!20}\cellcolor{gray!20}\scriptsize TLP security parameter\\
%
%&\cellcolor{white!20}\scriptsize$\Delta$&\cellcolor{white!20}\scriptsize Time win. message remains hidden\\
%
%&\cellcolor{gray!20}\scriptsize $S$&\cellcolor{gray!20}\scriptsize Max. squaring  done per sec.    \\
%
%&\cellcolor{white!20}\scriptsize $T$&\cellcolor{white!20}\scriptsize $T=S\Delta$    \\
%
%&\cellcolor{gray!20}\scriptsize $s_{\scriptscriptstyle j}$&\cellcolor{gray!20}\scriptsize $j\text{-th}$ solution\\
%
%&\cellcolor{white!20}\scriptsize $\ddot{o}_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize $j\text{-th}$ puzzle, $\ddot{o}_{\scriptscriptstyle j}:(o_{\scriptscriptstyle j,1},o_{\scriptscriptstyle j,2})$\\
%
%&\cellcolor{gray!20}\scriptsize $f_{\scriptscriptstyle j}$&\cellcolor{gray!20}\scriptsize Time  when  $j{\text{-th}}$ solution is found\\
%
%&\cellcolor{white!20}\scriptsize $k, k_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize Sym. key encryption keys\\
%
%&\cellcolor{gray!20}\scriptsize $pk, sk$&\cellcolor{gray!20}\scriptsize Public and secret keys\\
%
%&\cellcolor{white!20}\scriptsize $q_{\scriptscriptstyle 1}, q_{\scriptscriptstyle 2}$&\cellcolor{white!20}\scriptsize Large prime numbers\\
%
%&\cellcolor{gray!20}\scriptsize $N$&\cellcolor{gray!20}\scriptsize RSA modulus, $N=q_{\scriptscriptstyle 1} q_{\scriptscriptstyle 2}$\\
%
%\hline
%
%
%  \hline
%
%
%%SO-PoR
%  \multirow{20}{*}{\rotatebox[origin=c]{90}{\scriptsize \textbf{SO-PoR}}}
%&\cellcolor{gray!20}\scriptsize$\mathtt{PRF}$&\cellcolor{gray!20}\scriptsize Pseudorandom function\\
%
%    &\cellcolor{white!20}\scriptsize$\hat{k},v_{\scriptscriptstyle j},l_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize $\mathtt{PRF}$'s keys\\
%&\cellcolor{gray!20}\scriptsize$\iota$&\cellcolor{gray!20}\scriptsize Security parameter, $\iota=128$-bit\\
%&\cellcolor{white!20}\scriptsize$p$&\cellcolor{white!20}\scriptsize Large prime number, $|p|=\iota$\\
%
%&\cellcolor{gray!20}\scriptsize$w$&\cellcolor{gray!20}\scriptsize  Blockchain block index\\
%
%&\cellcolor{white!20}\scriptsize$g$&\cellcolor{white!20}\scriptsize Blockchain security parameter: chain quality    \\
%
%  &\cellcolor{gray!20}\scriptsize$\lambda'$&\cellcolor{gray!20}\scriptsize Blockchain generic security parameter\\
%&\cellcolor{white!20}\scriptsize${\bm{F}}$&\cellcolor{white!20}\scriptsize Outsourced encoded file\\
%&\cellcolor{gray!20}\scriptsize$F_{\scriptscriptstyle j}$&\cellcolor{gray!20}\scriptsize A file block\\
%&\cellcolor{white!20}\scriptsize$|{\bm{F}}|$&\cellcolor{white!20}\scriptsize Number of file blocks, $|{\bm{F}}|=n$\\
% &\cellcolor{gray!20}\scriptsize$||{\bm{F}}||$&\cellcolor{gray!20}\scriptsize File bit-size\\
%% \multirow{8}{*}{\rotatebox[origin=c]{90}{\scriptsize \textbf{SO-PoR}}}
%
%&\cellcolor{white!20}\scriptsize$\sigma_{\scriptscriptstyle i}$&\cellcolor{white!20}\scriptsize Permanent tag   \\
% &\cellcolor{gray!20}\scriptsize$\sigma_{\scriptscriptstyle b,j}$&\cellcolor{gray!20}\scriptsize Disposable tag   \\
%
%    &\cellcolor{white!20}\scriptsize$\alpha, \alpha_{\scriptscriptstyle j}, r_{\scriptscriptstyle i},r_{\scriptscriptstyle b,j}$&\cellcolor{white!20}\scriptsize Pseudorandom values  \\
%
%&\cellcolor{gray!20}\scriptsize$c$&\cellcolor{gray!20}\scriptsize Number of challenges \\
%
%&\cellcolor{white!20}\scriptsize$\mathcal {B}_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize Blockchain's $j{\text{-th}}$ block\\
%
%&\cellcolor{gray!20}\scriptsize$(\mu_{\scriptscriptstyle j},\xi_{\scriptscriptstyle j})$&\cellcolor{gray!20}\scriptsize $j{\text{-th}}$ PoR proof\\
%&\cellcolor{white!20}\scriptsize$\Delta_{\scriptscriptstyle 1}$&\cellcolor{white!20}\scriptsize Time taken to generate  a PoR\\
%
%&\cellcolor{gray!20}\scriptsize$\Delta_{\scriptscriptstyle 2}$&\cellcolor{gray!20}\cellcolor{gray!20}\scriptsize Time taken a contract gets a message\\
%
%&\cellcolor{white!20}\scriptsize$e$&\cellcolor{white!20}\scriptsize Coins paid for an accepting PoR\\
%
%\hline
%
%
%
%
%
%\end{tabular}
%
%\end{center}
%}
%%\end{footnotesize}
%\end{table}
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Notations}\label{sec:notation-table}

We summarise our notations in Table \ref{table:notation-table}.
\vspace{-6mm}

\begin{table*}[!htbp]
\begin{scriptsize}
\begin{center}
\footnotesize{

\caption{ \small Notation Table.}\label{commu-breakdown-party}
\renewcommand{\arraystretch}{.84}
\scalebox{0.86}{
% 1st table
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}

\hline

\cellcolor{gray!15}\scriptsize \textbf{Setting} &\cellcolor{gray!15} \scriptsize \textbf{Symbol}&\cellcolor{gray!15} \scriptsize \textbf{Description}  \\
    \hline

     \hline

%Generic
\multirow{9}{*}{\rotatebox[origin=c]{90}{\scriptsize \textbf{Generic}}}

 &\cellcolor{white!20}\scriptsize$z$&\cellcolor{white!20}\scriptsize \text{Number of puzzles or delegated verifications}\\

&\cellcolor{gray!20}\scriptsize$h$, $h_{\scriptscriptstyle j}$ &\cellcolor{gray!20}\scriptsize Hash values  \\

&\cellcolor{white!20}\scriptsize$d$, $d_{\scriptscriptstyle j}$ &\cellcolor{white!20}\scriptsize Randomness  of commitment\\

&\cellcolor{gray!20}\scriptsize$n$ &\cellcolor{gray!20}\scriptsize Number of file blocks\\
&\cellcolor{white!20}\scriptsize$m$, $m_{\scriptscriptstyle j}$ &\cellcolor{white!20}\scriptsize Plaintext messages\\
  &\cellcolor{gray!20}\scriptsize$\ddot{o}$ &\cellcolor{gray!20}\scriptsize Pair representation\\
&\cellcolor{white!20}\scriptsize$\vv{\bm{o}}$ &\cellcolor{white!20}\scriptsize Vector representation\\
&\cellcolor{gray!20}\scriptsize$\ddot{p}:(m_{\scriptscriptstyle j},d_{\scriptscriptstyle j})$ &\cellcolor{gray!20}\scriptsize Commitment opening\\

&\cellcolor{white!20}\scriptsize$\mathtt{H}$ &\cellcolor{white!20}\scriptsize Hash function\\

 \hline

  \hline

  %CR-TLP
  %CR-TLP
\multirow{11}{*}{\rotatebox[origin=c]{90}{\scriptsize  \textbf{C-TLP}}}

&\cellcolor{gray!20}\scriptsize$\lambda$&\cellcolor{gray!20}\cellcolor{gray!20}\scriptsize TLP security parameter\\

&\cellcolor{white!20}\scriptsize$\Delta$&\cellcolor{white!20}\scriptsize Time win. message remains hidden\\

&\cellcolor{gray!20}\scriptsize $S$&\cellcolor{gray!20}\scriptsize Max. squaring  done per sec.    \\

&\cellcolor{white!20}\scriptsize $T$&\cellcolor{white!20}\scriptsize $T=S\Delta$    \\

&\cellcolor{gray!20}\scriptsize $s_{\scriptscriptstyle j}$&\cellcolor{gray!20}\scriptsize $j\text{-th}$ solution\\

&\cellcolor{white!20}\scriptsize $\ddot{o}_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize $j\text{-th}$ puzzle, $\ddot{o}_{\scriptscriptstyle j}:(o_{\scriptscriptstyle j,1},o_{\scriptscriptstyle j,2})$\\

&\cellcolor{gray!20}\scriptsize $f_{\scriptscriptstyle j}$&\cellcolor{gray!20}\scriptsize Time  when  $j{\text{-th}}$ solution is found\\

&\cellcolor{white!20}\scriptsize $k, k_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize Sym. key encryption keys\\

&\cellcolor{gray!20}\scriptsize $pk, sk$&\cellcolor{gray!20}\scriptsize Public and secret keys\\

&\cellcolor{white!20}\scriptsize $q_{\scriptscriptstyle 1}, q_{\scriptscriptstyle 2}$&\cellcolor{white!20}\scriptsize Large prime numbers\\

&\cellcolor{gray!20}\scriptsize $N$&\cellcolor{gray!20}\scriptsize RSA modulus, $N=q_{\scriptscriptstyle 1} q_{\scriptscriptstyle 2}$\\

\hline

\end{tabular}

% 2nd table
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
    \hline
\cellcolor{gray!15}\scriptsize \textbf{Setting} &\cellcolor{gray!15} \scriptsize \textbf{Symbol}&\cellcolor{gray!15} \scriptsize \textbf{Description}  \\
    \hline

\hline

\hline

%SO-PoR right
 \multirow{20}{*}{\rotatebox[origin=c]{90}{\scriptsize \textbf{SO-PoR}}}
&\cellcolor{gray!20}\scriptsize$\mathtt{PRF}$&\cellcolor{gray!20}\scriptsize Pseudorandom function\\

    &\cellcolor{white!20}\scriptsize$\hat{k},v_{\scriptscriptstyle j},l_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize $\mathtt{PRF}$'s keys\\
&\cellcolor{gray!20}\scriptsize$\iota$&\cellcolor{gray!20}\scriptsize Security parameter, $\iota=128$-bit\\
&\cellcolor{white!20}\scriptsize$p$&\cellcolor{white!20}\scriptsize Large prime number, $|p|=\iota$\\

&\cellcolor{gray!20}\scriptsize$w$&\cellcolor{gray!20}\scriptsize  Blockchain block index\\

&\cellcolor{white!20}\scriptsize$g$&\cellcolor{white!20}\scriptsize Blockchain security parameter: chain quality    \\

  &\cellcolor{gray!20}\scriptsize$\lambda'$&\cellcolor{gray!20}\scriptsize Blockchain generic security parameter\\
&\cellcolor{white!20}\scriptsize${\bm{F}}$&\cellcolor{white!20}\scriptsize Outsourced encoded file\\
&\cellcolor{gray!20}\scriptsize$F_{\scriptscriptstyle j}$&\cellcolor{gray!20}\scriptsize A file block\\
&\cellcolor{white!20}\scriptsize$|{\bm{F}}|$&\cellcolor{white!20}\scriptsize Number of file blocks, $|{\bm{F}}|=n$\\
 &\cellcolor{gray!20}\scriptsize$||{\bm{F}}||$&\cellcolor{gray!20}\scriptsize File bit-size\\
% \multirow{8}{*}{\rotatebox[origin=c]{90}{\scriptsize \textbf{SO-PoR}}}

&\cellcolor{white!20}\scriptsize$\sigma_{\scriptscriptstyle i}$&\cellcolor{white!20}\scriptsize Permanent tag   \\
 &\cellcolor{gray!20}\scriptsize$\sigma_{\scriptscriptstyle b,j}$&\cellcolor{gray!20}\scriptsize Disposable tag   \\

    &\cellcolor{white!20}\scriptsize$\alpha, \alpha_{\scriptscriptstyle j}, r_{\scriptscriptstyle i},r_{\scriptscriptstyle b,j}$&\cellcolor{white!20}\scriptsize Pseudorandom values  \\

&\cellcolor{gray!20}\scriptsize$c$&\cellcolor{gray!20}\scriptsize Number of challenges \\

&\cellcolor{white!20}\scriptsize$\mathcal {B}_{\scriptscriptstyle j}$&\cellcolor{white!20}\scriptsize Blockchain's $j{\text{-th}}$ block\\

&\cellcolor{gray!20}\scriptsize$(\mu_{\scriptscriptstyle j},\xi_{\scriptscriptstyle j})$&\cellcolor{gray!20}\scriptsize $j{\text{-th}}$ PoR proof\\
&\cellcolor{white!20}\scriptsize$\Delta_{\scriptscriptstyle 1}$&\cellcolor{white!20}\scriptsize Time taken to generate  a PoR\\

&\cellcolor{gray!20}\scriptsize$\Delta_{\scriptscriptstyle 2}$&\cellcolor{gray!20}\cellcolor{gray!20}\scriptsize Time taken a contract gets a message\\

&\cellcolor{white!20}\scriptsize$e$&\cellcolor{white!20}\scriptsize Coins paid for an accepting PoR\\

\hline

\end{tabular}\label{table:notation-table}

}}
\end{center}
\end{scriptsize}
\end{table*}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% !TEX options=--shell-escape
\documentclass[french]{article}
\usepackage{common}

\usepackage{minted}
\usepackage{minted,xcolor} % [cache=false] if minted acts out

\setminted[python]{style=colorful}
\author{Gabriel Belouze}

\title{Kernel methods in machine learning : Homework 3}
\date{2021-2022}

\begin{document}
\maketitle

In case embedded links are not clickable on \textit{Gradescope}, all links from this document reference specific code section of the repository \href{https://github.com/gbelouze/mva-kernel-hw3}{https://github.com/gbelouze/mva-kernel-hw3}.

\section{Exercice 1. Support Vector Classifier}

\begin{enumerate}
\item
    \begin{enumerate}
    \item From the Representer theorem we can express $f = \sum_i \beta_i K_{x_i}$. We also write for convenience $Y = \diag(y)$. Then the Lagrangian writes
    \[ \frac{1}{2} \beta^T K \beta + C \cdot \xi^T\mathbf{1} - \mu^T \xi
    + \alpha^T (\mathbf{1} - \xi - b\cdot y - Y K \beta)
    \]
    \item
    The Lagrangian is affine in $b$, which yields the condition $\alpha^Ty = 0$ for the dual function not to be $-\infty$.\\
    The Lagrangian is affine in $\xi$, which yields the condition $\forall i, \; C = \mu_i + \alpha_i$ for the dual function not to be $-\infty$.\\
    The Lagrangian is convex, coercive in $\beta$, such that it reaches its infinimum at a point where the gradient $\nabla_\beta L = K \beta - K Y \alpha$ vanishes. This happens for $\hat{\beta} \defeq Y \alpha$. More generally, this happens for all $\hat{\beta} + \varepsilon$ where $\varepsilon$ is in the null space of $K$. It turns out that the value of $L$ does not change when adding such $\varepsilon$ (indeed all occurrences of $\beta$ are applied to $K$), and it is enough to look at $\hat{\beta}$ to compute the dual function.\\
    Finally, we obtain the dual function
    \[ l(\alpha, \mu) = \begin{cases}
    -\frac{1}{2} \alpha^T Y K Y \alpha + \alpha^T \mathbf{1} \quad \textrm{si $\mu_i+\alpha_i=C$, $\alpha^Ty=0$} \\
    -\infty \quad \textrm{sinon}
    \end{cases}
    \]
    The variable $\mu$ is redundant and we can express the dual problem in terms of the sole variable $\alpha$ (and as a minimization so long that we change the signs) :
    \begin{equation*}
    \begin{aligned}
    & \underset{\alpha}{\text{minimize}}
    & &  \frac{1}{2} \alpha^T Y K Y \alpha - \alpha^T \mathbf{1}\\
    & \text{subject to}
    & & \alpha^Ty = 0 \\
    & & & 0 \preceq \alpha \preceq C
    \end{aligned}
    \end{equation*}
    At optimal points, $\beta^*$ minimizes the Lagrangian, and as we saw earlier, we can choose without loss of generality $\beta^*_i \defeq y_i \alpha^*_i$. Then for a new input $x$, we have
    \[ f(x) = \sum_i y_i \alpha^*_i \K(x_i, x)
    \]
    Note that this is $KY\alpha$ for the (joint) original inputs.
    \item In the following, we look at optimal points but omit the star notations. Complementary slackness conditions state that for all $i$,
    \begin{align}
    - \mu_i \xi_i = 0 \quad \textrm{i.e.} \quad (C - \alpha_i) \xi_i &= 0 \label{eq1}\\
    (1  - y_i(b - f(x_i)) - \xi_i) \alpha_i &= 0 \label{eq2}
    \end{align}
    Three situations arise :\\
    \textbf{If $0 < \alpha_i < C$ --} From \ref{eq1} we get $\xi_i=0$ which in turn yields from \ref{eq2} $y_i(b - f(x_i)) = 1$, i.e. \textit{point $i$ is on the margin}.\\
    \textbf{If $\alpha_i = 0$ --} We can only ensure that $y_i(b - f(x_i)) \geq 1$.\\
    \textbf{If $\alpha_i = C$ --} We can only ensure that $y_i(b - f(x_i)) \leq 1$.
    \end{enumerate}
\item
    \begin{enumerate}
    \item See the full code \href{https://github.com/gbelouze/mva-kernel-hw3/blob/main/src/hw3/models/kernel.py}{on Github}
    \inputminted{python}{snippets/ex1_q2a.py}
    \item See the full code \href{https://github.com/gbelouze/mva-kernel-hw3/blob/b57749258006dcfc57c9f2e233fad94ef4b50f47/src/hw3/models/classify.py#L55}{on Github}
    \inputminted{python}{snippets/ex1_q2b.py}
    \item See the full code \href{https://github.com/gbelouze/mva-kernel-hw3/blob/b57749258006dcfc57c9f2e233fad94ef4b50f47/src/hw3/models/classify.py#L108}{on Github}
    \inputminted{python}{snippets/ex1_q2c.py}
    \item We test our implementation on the three provided datasets. The number of support vectors is
    $2$ for dataset 1, $25$ for dataset $2$ and $43$ for dataset $3$. See \autoref{fig:classifier}.
    \begin{figure}
        \centering
        \includegraphics[width=\textwidth]{figures/classifier.png}
        \caption{Kernel Support Vector Classifier}
        \label{fig:classifier}
    \end{figure}
    \end{enumerate}
\end{enumerate}

\section{Exercise 2. Kernel Support Vector Regression}

\begin{enumerate}
\item
    \begin{enumerate}
    \item From the Representer theorem we can express $f = \sum_i \beta_i K_{x_i}$. Then the Lagrangian writes
    \begin{align*}
    &\frac{1}{2} \beta^T K \beta + C \cdot \sum_{i=1}^N \xi^+_i + \xi^-_i \\
    +& \alpha^{+T} \big ( y - K\beta - b \mathbf{1} - \eta \mathbf{1} - \xi^+ \big ) \\
    +& \alpha^{-T} \big ( -y + K\beta + b\mathbf{1} - \eta \mathbf{1} - \xi^- \big ) \\
    -& \mu^{+T} \xi^+ - \mu^{-T} \xi^-
    \end{align*}
    \item
    The Lagrangian is affine in $b$, which yields the condition $\sum_i \alpha^+_i = \sum_i \alpha^-_i $ for the dual function not to be $-\infty$.\\
    The Lagrangian is affine in $\xi^+$, which yields the condition $\forall i$, $ \alpha^+_i + \mu_i^+= C $ for the dual function not to be $-\infty$.\\
    The Lagrangian is affine in $\xi^-$, which yields the condition $\forall i$, $ \alpha^-_i + \mu_i^-= C $ for the dual function not to be $-\infty$.\\
    The Lagrangian is convex, coercive in $\beta$. As in exercise $1$, we can restrict ourselves to a single point that nullifies the gradient $\nabla_\beta L = K\beta - K (\alpha^+ - \alpha^-)$, such as $\hat{\beta} \defeq \alpha^+ - \alpha^-$.\\
    Finally, we get the Lagrange dual function
    \[ \begin{cases}
        - \frac{1}{2}(\alpha^+ - \alpha^-)^T K (\alpha^+ - \alpha^-) + y^T (\alpha^+ - \alpha^-) - \eta \sum_i \alpha_i^+ + \alpha_i^- \\ \qquad \qquad \textrm{si $\sum_i \alpha^+_i = \sum_i \alpha^-_i $, $ \alpha^\pm_i + \mu_i^\pm= C $} \\
        - \infty \, \qquad \textrm{sinon}
    \end{cases} \]
    Again, the variables $\mu^\pm$ are redundant and we can express the dual problem in the sole variables $\alpha^\pm$, and by changing the signs as a minimization :
    \begin{equation*}
    \begin{aligned}
    & \underset{\alpha^+, \alpha^-}{\text{minimize}}
    & &  \frac{1}{2}(\alpha^+ - \alpha^-)^T K (\alpha^+ - \alpha^-) - y^T (\alpha^+ - \alpha^-) + \eta \sum_i \alpha_i^+ + \alpha_i^-\\
    & \text{subject to}
    & & \sum_i \alpha_i^+ = \sum_i \alpha_i^- \\
    & & & 0 \preceq \alpha^+ \preceq C\\
    & & & 0 \preceq \alpha^- \preceq C
    \end{aligned}
    \end{equation*}
    At optimal points, $\beta^*$ minimizes the Lagrangian, and as we saw earlier, we can choose without loss of generality $\beta^*_i \defeq \alpha^+_i - \alpha^-_i$. Then for a new input $x$, we have
    \[ f(x) = \sum_i (\alpha^+_i - \alpha^-_i) \K(x_i, x)
    \]
    Note that this is $K(\alpha^+ - \alpha^-)$ for the (joint) original inputs.
    \item The reasoning is similar as in exercise 1. Skipping the computations (which are not different from earlier), we have from complementary slackness conditions:\\
    \textbf{If $0 < \alpha^+_i < C$} then $y_i - f(x_i) - b = \eta$, i.e. $x_i$ is on the boundary\\
    \textbf{If $0 < \alpha^-_i < C$} then $-y_i + f(x_i) + b = \eta$, i.e. $x_i$ is on the boundary as well.\\
    Otherwise we cannot know for sure.
    \end{enumerate}
\item
    \begin{enumerate}
    \item See the full code \href{https://github.com/gbelouze/mva-kernel-hw3/blob/b57749258006dcfc57c9f2e233fad94ef4b50f47/src/hw3/models/classify.py#L137}{on Github}
    \inputminted{python}{snippets/ex2_q2a.py}
    \item See the full code \href{https://github.com/gbelouze/mva-kernel-hw3/blob/b57749258006dcfc57c9f2e233fad94ef4b50f47/src/hw3/models/classify.py#L209}{on Github}
    \inputminted{python}{snippets/ex2_q2b.py}
    \item We test our implementation on the provided dataset. The number of support vectors is
    $42$ (of course...). See \autoref{fig:regressor}.
    \begin{figure}
        \centering
        \includegraphics[width=\textwidth]{figures/regressor.png}
        \caption{Kernel Support Vector Regressor}
        \label{fig:regressor}
    \end{figure}
    \end{enumerate}
\end{enumerate}

\end{document}