\documentclass[t,aspectratio=169]{beamer}
\usetheme{metropolis}
\metroset{block=fill}

\input{../../_assets/basicslide1}
\input{../../_assets/macros}

\author{Samuel Vaiter}
\date{}
\institute{
Created: 2023-11-08.\\
Last update: 2023-11-08.\\
Status: inprogress.}
\title{Convex function}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
  \frametitle{Graph \& Epigraph}

  \begin{center}
    \begin{tikzpicture}[scale=0.5]
      \begin{axis}[
        domain=-1:1,
        xlabel={$x$},
        ylabel={$f(x)$},
        axis y line=left,
        axis x line=center,
        axis on top,
        ticks=none
        ]

        %% The curve
        \addplot [no marks,smooth,name path=curve] plot {x^3-exp(x)+2};
        %% The line
        \addplot [no marks,white!40,smooth,name path=topborder] plot {1.5};
        %% filling
        \addplot[gray!50] fill between[of=curve and topborder];

        \node at (axis cs:0.5,1) {$\epigraph f$};
      \end{axis}
    \end{tikzpicture}
  \end{center}

  \begin{definition}[Graph]
    \[
      \graph f
      =
      \setcond{(x,t) \in \RR^{n} \times \RR}{t = f(x)}
    \]
  \end{definition}

  \begin{definition}[Epigraph]
    \[
      \epigraph f
      =
      \setcond{(x,t) \in \RR^{n} \times \RR}{t \alert{\geq} f(x)}
    \]
  \end{definition}
\end{frame}

\begin{frame}
  \frametitle{Convex function}
  Convex: the line joining $f(x)$ and $f(y)$ lies \alert{above} the graph of $f$ between $x$ and $y$
  \begin{columns}
    \begin{column}{0.5\linewidth}
      \begin{center}
        \begin{tikzpicture}[scale=0.7]
          \begin{axis}[
            domain=-1:1,
            axis y line=left,
            axis x line=bottom,
            axis on top,
            ticks=none,
            clip=false %otherwise the x and y labels are truncated
            ]

            %% The curve
            \addplot [no marks,smooth,name path=curve] plot {x^2+0.2};
            %% The line
            \addplot [no marks,white!40,smooth,name path=topborder] plot {1.5};
            %% filling
            \addplot[gray!50] fill between[of=curve and topborder];

            \node at (axis cs:0.5,1) {$\epigraph f$};

            \coordinate (origin) at (0,0);
            \coordinate[label=right:$f(x)$] (fx) at (axis cs:-0.8, 0.84);
            \coordinate[label=above:$f(y)$] (fy) at (axis cs:0.3, 0.29);
            \coordinate[label=below:$x$] (x) at (fx |- origin);
            \coordinate[label=below:$y$] (y) at (fy |- origin);
            \fill (fx) circle [radius=2pt];
            \fill (fy) circle [radius=2pt];
            \draw (fx) -- (fy);

            \draw (fx) -- (x);
            \draw (fy) -- (y);
          \end{axis}
        \end{tikzpicture}
      \end{center}
    \end{column}
    \begin{column}{0.5\linewidth}
      \begin{definition}[Convex function]
          A function $f: \RR^{p} \to \RRb$ such that $f \neq +\infty$ is \emph{convex} iff its epigraph $\epigraph f$ is convex.
      \end{definition}
    \end{column}
  \end{columns}

  Jensen's inequality
  \[
    \forall x,y \in \mathbb{R}^{n}, \forall \lambda \in [0,1],
    \qquad
    f(\lambda x + (1-\lambda) y) \leq \lambda f(x) + (1-\lambda) f(y)
  \]
\end{frame}

\begin{frame}
  \frametitle{Convexity and differentiability}
  \begin{proposition}
    A differentiable function $f$ is convex iff
    \[
      \forall x, \bar x \in \dom f,\quad
      f(x) \geq f(\bar x) + \dotp{\nabla f(\bar x)}{x-\bar x}
    \]
  \end{proposition}


  \begin{center}
    \begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-0.7,xscale=0.7]
      % that's cheating because it is cubic ...
      \coordinate (fx) at (171,163);
      \coordinate (x) at (171,241);
      % Axis
      \draw[->] (50,241) -- (389,241);
      \draw[->] (83.9,261.84) -- (83.9,56);
      % f(x)
      \draw (143,55.84) .. controls (131.72,93.08) and (139.9,143.22) .. (fx) .. controls (202.1,182.78) and (317.05,182.56) .. (342,163.84) node[right] {$f(x)$};
      % tangent
      \draw[red,thick] (90,115) -- (278.67,228.33) node[right] {$f(\bar x) + \dotp{\nabla f(\bar x)}{x - \bar x}$};
      % (bar x, f(bar x))
      \draw[dotted] (fx) -- (x) node[below] {$\bar x$};
      \fill (fx) circle (2pt) node[anchor=south west] {$f(\bar x)$};
    \end{tikzpicture}
  \end{center}

  \note{
    \begin{itemize}
    \item A differentiable function is convex iff it is above all its tangent.
    \end{itemize}
  }
\end{frame}

\begin{frame}
  \frametitle{Convexity and differentiability}
  \begin{proposition}
    A differentiable function $f$ is convex iff
    \[
      \forall x, \bar x \in \dom f,\quad
      f(x) \geq f(\bar x) + \dotp{\nabla f(\bar x)}{x-\bar x}
    \]
  \end{proposition}
  \metroset{block=transparent}
  \begin{block}{Proof idea}
    \begin{itemize}
    \item (Convex $\implies$ tangent). Divide by $\lambda$ the relation
      \[ f(\lambda x + (1-\lambda)\bar x) - f(\bar x) \leq \lambda (f(x) - f(\bar x)) . \]
    \item (Tangent $\implies$ convex). Apply the tangent relation twice to $z = \lambda x + (1-\lambda)y$ and $x$ (resp. $y$), and use a convex combination of the two relationships.
    \end{itemize}
  \end{block}
  \metroset{block=fill}
\end{frame}

%% TODO: full proof

\end{document}