\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{enumitem}

\newcommand{\f}[1]{o_{#1}x_{#1}y_{#1}z_{#1}}
\newcommand{\fromslides}{{\\ \color{blue} \hspace*{\fill}(from lecture slides)} \\}
\newcommand{\bydefn}{{\\ \color{blue} \hspace*{\fill}(by definition)} \\}
\newcommand{\given}{{\\ \color{blue} \hspace*{\fill}(given)} \\}

\newcommand{\rx}[1]{\begin{bmatrix} 1 & 0 & 0 \\ 0 & cos(#1) & -sin(#1) \\ 0 & sin(#1) & cos(#1) \end{bmatrix}}
\newcommand{\ry}[1]{\begin{bmatrix} cos(#1) & 0 & sin(#1) \\ 0 & 1 & 0 \\ -sin(#1) & 0 & cos(#1) \end{bmatrix}}
\newcommand{\rz}[1]{\begin{bmatrix} cos(#1) & -sin(#1) & 0 \\ sin(#1) & cos(#1) & 0 \\ 0 & 0 & 1 \end{bmatrix}}

\title{CSci 5551 - HW1}
\author{Yashasvi Sriram Patkuri\\patku001@umn.edu}

\begin{document}
\maketitle
\pagebreak

%--------------------------------------------------------------------------------
\section{}
Let $ \f{\frac{1}{2}} $ represent the coordinate system formed by rotating $ \f{0} $ by $ \frac{\pi}{2} $ radians about the x-axis.
Let this rotation be denoted by $ R_1 $. Using basic matrices
\begin{equation}
  R_1 \equiv \rx{\frac{\pi}{2}} \equiv \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & -1 \\ 0 & 1 & 0 \end{bmatrix}
\end{equation}
\fromslides

$ \f{1} $ represents the coordinate system formed by rotating $ \f{\frac{1}{2}} $ by$ \frac{\pi}{2} $ radians about the fixed frame Y-axis.
Let this rotation be denoted by $ R_2 $. Using basic matrices
\begin{equation}
  R_2 \equiv \ry{\frac{\pi}{2}} \equiv \begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \end{bmatrix}
\end{equation}
\fromslides

For two consecutive fixed frame rotations denoted by $ R_1, R_2 $ rotation matrices, the composite rotation matrix, say R $ \equiv $ $ R_2 * R_1 $

\begin{equation}
  R
  \equiv R_2 * R_1
  \equiv \begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \end{bmatrix} * \begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & -1 \\ 0 & 1 & 0 \end{bmatrix}
  \equiv \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & -1 \\ -1 & 0 & 0 \end{bmatrix}
\end{equation}
\fromslides

The sketches of frames $ \f{1} $, $ \f{\frac{1}{2}} $ and $ \f{1} $ are drawn in figure \ref{fig:q1}.

\begin{figure}[htpb]
  \centering
  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
      \draw [->] (0,0,0) -- (4,0,0) node [right] {$x_0$};
      \draw [->] (0,0,0) -- (0,4,0) node [left] {$y_0$};
      \draw [->] (0,0,0) -- (0,0,4) node [left] {$z_0$};
  \end{tikzpicture}

  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
      \draw [->] (0,0,0) -- (4,0,0) node [right] {$x_{\frac{1}{2}}$};
      \draw [->] (0,0,0) -- (0,0,4) node [left] {$y_{\frac{1}{2}}$};
      \draw [->] (0,0,0) -- (0,-4,0) node [left] {$z_{\frac{1}{2}}$};
  \end{tikzpicture}

  \begin{tikzpicture}[x=1cm, y=1cm, z=-0.6cm]
      \draw [->] (0,0,0) -- (0,0,-4) node [left] {$x_1$};
      \draw [->] (0,0,0) -- (4,0,0) node [right] {$y_1$};
      \draw [->] (0,0,0) -- (0,-4,0) node [left] {$z_1$};
  \end{tikzpicture}
\caption{Rotations by $ \frac{\pi}{2} $ radians about fixed X and Y axes consecutively}
  \label{fig:q1}
\end{figure}

\pagebreak

%--------------------------------------------------------------------------------
\section{}

Given
\begin{equation}
  R_{12} \equiv \begin{bmatrix} 1 & 0 & 0 \\ 0 & \frac{1}{2} & -\frac{\sqrt{3}}{2} \\ 0 & \frac{\sqrt{3}}{2} & \frac{1}{2} \end{bmatrix}
\end{equation}
\begin{equation}
  R_{13} \equiv \begin{bmatrix} 0 & 0 & -1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{bmatrix}
\end{equation}

For any physical vector p, let $ p^{i} $ be its representation in ith coordinate system
\begin{equation}
  \label{eq:21}
  p^{2} \equiv R_{21} * p^{1}
\end{equation}
\begin{equation}
  \label{eq:22}
  p^{1} \equiv R_{13} * p^{3}
\end{equation}
\bydefn

Identities \ref{eq:21} and \ref{eq:22} imply
\begin{equation}
  \label{eq:23}
  p^{2} \equiv R_{21} * R_{13} * p^{3}
\end{equation}

But
\begin{equation}
  \label{eq:24}
  p^{2} \equiv R_{23} * p^{3}
\end{equation}
\bydefn

From identities \ref{eq:23} and \ref{eq:24}
\begin{equation}
  \label{eq:25}
  R_{23} \equiv R_{21} * R_{13}
\end{equation}

But
\begin{equation}
  \label{eq:26}
  R_{21} \equiv R_{12}^{T}
\end{equation}
\fromslides

Identities \ref{eq:25} and \ref{eq:26} imply
\begin{equation}
  \label{eq:27}
  R_{23} \equiv R_{12}^T * R_{13}
  \equiv \begin{bmatrix} 1 & 0 & 0 \\ 0 & \frac{1}{2} & \frac{\sqrt{3}}{2} \\ 0 & -\frac{\sqrt{3}}{2} & \frac{1}{2} \end{bmatrix} * \begin{bmatrix} 0 & 0 & -1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{bmatrix}
  \equiv \begin{bmatrix} 0 & 0 & -1 \\ \frac{\sqrt{3}}{2} & \frac{1}{2} & 0 \\ \frac{1}{2} & -\frac{\sqrt{3}}{2} & 0 \end{bmatrix}
\end{equation}

\pagebreak

%--------------------------------------------------------------------------------
\section{}

Let us represent the Z-X-Z angles by $ \theta_1, \theta_2, \theta_3 $ instead of $ \phi, \theta, \psi $ respectively for simplicity.

First we build rotation matrix corresponding to each Eular angle.
Using basic rotation matrices we get
\begin{equation}
  \label{eq:31}
  R_{\theta_1,z} \equiv \rz{\theta_1}
\end{equation}
\begin{equation}
  \label{eq:32}
  R_{\theta_2,x} \equiv \rx{\theta_2}
\end{equation}
\begin{equation}
  \label{eq:33}
  R_{\theta_3,z} \equiv \rz{\theta_3}
\end{equation}

Eular angles correspond to consecutive current frame rotations.
For consecutive current frame rotations $ R_1, R_2, R_3 $, the composite rotation matrix is say R is given by
\begin{equation}
  \label{eq:34}
  R \equiv R_1 * R_2 * R_3
\end{equation}
\fromslides

Substituting equations \ref{eq:31}, \ref{eq:32}, \ref{eq:33} in \ref{eq:34} we get,
\begin{equation}
  \label{eq:35}
  R_{ZXZ} \equiv R_{\theta_1,z} * R_{\theta_2,x} * R_{\theta_3,z}
\end{equation}
where $ R_{ZXZ} $ represents rotation matrix corresponding to the set of Z-X-Z eular angles.

\[
  R_{ZXZ} \equiv \rz{\theta_1} * \rx{\theta_2} * \rz{\theta_3}
\]
\[
  R_{ZXZ} \equiv \begin{bmatrix} c1 & -s1.c2 & s1.s2 \\ s1 & c1.c2 & -c1.s2 \\ 0 & s2 & c2 \end{bmatrix} * \rz{\theta_3}
\]
Where ci represents $ cos(\theta_i) $ and si represents $ sin(\theta_i) $
\begin{equation}
  \label{eq:36}
  R_{ZXZ} \equiv \begin{bmatrix} c1.c3 - s1.c2.s3 & -c1.s3 - s1.c2.c3 & s1.s2 \\ s1.c3 + c1.c2.s3 & -s1.s3 + c1.c2.c3 & -c1.s2 \\ s2.s3 & s2.c3 & c2 \end{bmatrix}
\end{equation}

Equation \ref{eq:36} is the rotation matrix corresponding to Z-X-Z Eular angles.

\subsection{$sin(\theta) = 0$}
For us $\theta_2$ represents $\theta$.
$sin(\theta_2) = 0$ implies $cos(\theta_2) = 1$. Substituting these in equation \ref{eq:36} we get
\begin{equation}
  \label{eq:37}
  R_{ZXZ} \equiv \begin{bmatrix} c1.c3 - s1.s3 & -c1.s3 - s1.c3 & 0 \\ s1.c3 + c1.s3 & -s1.s3 + c1.c3 & 0 \\ 0 & 0 & 1 \end{bmatrix}
\end{equation}
But
\begin{equation}
  \label{eq:38}
  sin(a + b) = sin(a)cos(b) + cos(a)sin(b)
\end{equation}
\begin{equation}
  \label{eq:39}
  cos(a + b) = cos(a)cos(b) - sin(a)sin(b)
\end{equation}

From equations \ref{eq:37}, \ref{eq:38} and \ref{eq:39} we can write
\begin{equation}
  \label{eq:310}
  R_{ZXZ} \equiv \rz{\theta_1 + \theta_3}
\end{equation}
Therefore, when $sin(\theta) = 0$ the composite rotation matrix is identical to rotation about Z-axis by $ \theta_1 + \theta_3 $.
This is an example of a gimbal lock.

\pagebreak

%--------------------------------------------------------------------------------
\section{}
\subsection{}
The general form of a transformation matrix is
\[
  H_{y}^{x} \equiv \begin{bmatrix} R_{y}^{x} & p_{y}^{x} \\ O_{1x3} & 1 \end{bmatrix}
\]
Where
\begin{enumerate}[nolistsep]
  \item $ R_{y}^{x} $ represents rotation matrix of coordinate system y w.r.t coordinate system x
  \item $ p_{y}^{x} $ represents position vector of origin of coordinate system y in coordinate system x
  \item $ O_{1x3} $ represents a 1x3 zero matrix
\end{enumerate}
\bydefn

As there is no rotation b/w coordinate systems 1 and 0 and 2 and 0, the rotation matrices $ R_{1}^{0}, R_{2}^{0} \equiv I_{3x3} $ where $ I_{3x3} $ represents identity matrix of order 3. Therefore to calculate $ H_{1}^{0}, H_{2}^{0} $ we only need $ p_{1}^{0}, p_{2}^{0} $.

\begin{figure}[h]
  \centering
  \includegraphics[width=0.8\textwidth]{p4-fig.png}
  \caption{Arrangement of coordinate systems 0, 1, 2, 3}
  \label{fig:p4}
\end{figure}

In coordinate system 0, the origin $ p_{1} $ of coordinate system 1 has
\begin{enumerate}[nolistsep]
  \item x-coordinate = 0, because it is in YZ plane (assumed from figure \ref{fig:p4})
  \item y-coordinate = 1, because it lies on nearest leg of the table which is 1m far from the origin of 0 in y direction.
  \item z-coordinate = 1, because it lies on table top which is 1m far from the origin of 0 in the z direction.
\end{enumerate}
Therefore
\[
  p_{1}^{0} \equiv \begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix}
\]
Which implies
\[
  H_{1}^{0}
  \equiv \begin{bmatrix} I_{3x3} & \begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix} \\ O_{1x3} & 1 \end{bmatrix}
  \equiv \begin{bmatrix}
          1 & 0 & 0 & 0\\
          0 & 1 & 0 & 1\\
          0 & 0 & 1 & 1\\
          0 & 0 & 0 & 1
        \end{bmatrix}
\]

In coordinate system 0, the origin $ p_{2} $ of coordinate system 2 has
\begin{enumerate}[nolistsep]
  \item x-coordinate = -0.5, because it is at the center of cube which is at the center of table top. Distance from origin of 0 to center of table to is 0.5m in the opposite direction of x-axis and hence the coordinate value.
  \item y-coordinate = 1.5, because it is at the center of cube which is at the center of table top. Distance from origin of 0 to center of table to is 1.5m in the direction of y-axis and hence the coordinate value.
  \item z-coordinate = 1.1, because it is at the center of cube which is at the center of table top. Distance from origin of 0 to table top in z-direction is 1m. But the origin of 2 is at center of cube of width 0.2m. This adds a (0.2 / 2)m height and hence the coordinate value 1 + 0.1 = 1.1
\end{enumerate}
Therefore
\[
  p_{2}^{0} \equiv \begin{bmatrix} -0.5 \\ 1.5 \\ 1.1 \end{bmatrix}
\]
Which implies
\[
  H_{2}^{0}
  \equiv \begin{bmatrix} I_{3x3} & \begin{bmatrix} -0.5 \\ 1.5 \\ 1.1 \end{bmatrix} \\ O_{1x3} & 1 \end{bmatrix}
  \equiv \begin{bmatrix}
          1 & 0 & 0 & -0.5\\
          0 & 1 & 0 & 1.5\\
          0 & 0 & 1 & 1.1\\
          0 & 0 & 0 & 1
        \end{bmatrix}
\]

In coordinate system 0, the origin $ p_{3} $ of coordinate system 3 has same x and y coordinates as $ p_{2}^{0} $ because the camera (the coordinate system 3 attached to it) is situated directly above center of cube 2m from table top.
\given
\begin{enumerate}[nolistsep]
  \item x-coordinate = -0.5
  \item y-coordinate = 1.5
  \item z-coordinate = 3, because it is 2m above the table top i.e. 1+2m above the ground
\end{enumerate}
Therefore
\[
  p_{1}^{0} \equiv \begin{bmatrix} -0.5 \\ 1.5 \\ 3 \end{bmatrix}
\]
Coordinate system 3 is rotated w.r.t coordinate system 0, therefore $ R_{3}^{0} \not\equiv I_{3x3} $.
\[
  R_{3}^{0}
  \equiv \begin{bmatrix}
        i_3.i_0 & j_3.i_0 & k_3.i_0\\
        i_3.j_0 & j_3.j_0 & k_3.j_0\\
        i_3.k_0 & j_3.k_0 & k_3.k_0\\
        \end{bmatrix}
  \equiv \begin{bmatrix}
        0 & 1 & 0\\
        1 & 0 & 0\\
        0 & 0 & -1\\
        \end{bmatrix}
\]
\fromslides

Which implies
\[
  H_{3}^{0}
  \equiv \begin{bmatrix} I_{3x3} & \begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix} \\ O_{1x3} & 1 \end{bmatrix}
  \equiv \begin{bmatrix}
          0 & 1 & 0 & -0.5\\
          1 & 0 & 0 & 1.5\\
          0 & 0 & -1 & 3\\
          0 & 0 & 0 & 1
        \end{bmatrix}
\]

\subsection{}

For any physical vector v,
\[
  v^{0} \equiv H_{2}^{0} * v^{2}
\]
\bydefn
Multiplying by $ H_{2}^{0}^{-1} $ on both sides from right hand.
\[
  H_{2}^{0}^{-1} * v^{0} \equiv H_{2}^{0}^{-1} * H_{2}^{0} * v^{2}
\]
\[
  H_{2}^{0}^{-1} * v^{0} \equiv v^{2}
\]
\[
  v^{2} \equiv H_{2}^{0}^{-1} * v^{0}
\]

But also
\[
  v^{2} \equiv H_{0}^{2} * v^{0}
\]
\bydefn
Therefore
\[
  H_{0}^{2} \equiv H_{2}^{0}^{-1}
  \equiv \begin{bmatrix}
          1 & 0 & 0 & -0.5\\
          0 & 1 & 0 & 1.5\\
          0 & 0 & 1 & 1.1\\
          0 & 0 & 0 & 1
        \end{bmatrix}^{-1}
  \equiv \begin{bmatrix}
          1 & 0 & 0 & 0.5\\
          0 & 1 & 0 & -1.5\\
          0 & 0 & 1 & -1.1\\
          0 & 0 & 0 & 1
        \end{bmatrix}
\]
But
\[
  v^{0} \equiv H_{3}^{0} * v^{3}
\]
\[
  v^{2} \equiv H_{0}^{2} * v^{0}
\]
\bydefn

Therefore combining above two equations
\[
  v^{2} \equiv H_{0}^{2} * H_{3}^{0} * v^{3}
\]
But
\[
  V^{2} \equiv H_{3}^{2} * v^{3}
\]
\bydefn

Therefore
\[
  H_{3}^{2} \equiv H_{0}^{2} * H_{3}^{0}
  \equiv \begin{bmatrix}
          1 & 0 & 0 & 0.5\\
          0 & 1 & 0 & -1.5\\
          0 & 0 & 1 & -1.1\\
          0 & 0 & 0 & 1
        \end{bmatrix}
        * \begin{bmatrix}
          0 & 1 & 0 & -0.5\\
          1 & 0 & 0 & 1.5\\
          0 & 0 & -1 & 3\\
          0 & 0 & 0 & 1
        \end{bmatrix}
  \equiv \begin{bmatrix}
          0 & 1 & 0 & 0\\
          1 & 0 & 0 & 0\\
          0 & 0 & -1 & 1.9\\
          0 & 0 & 0 & 1
        \end{bmatrix}
\]

\end{document}


%-----------------------------------------------------------------------------------------------
\section{Example Applications}

    %---------------------------------------------------------------------------------------
    \subsection{Iso-$s$ Process in const-$c_v$, van-der-Waals Substance}

    Consider         a         substance         whose         Helmholtz          characteristic
    function~\cite{1986-JonesJB+HawkinsGA-Wiley} in reduced coordinates is given by:
    %
    \begin{equation}
        a_r = \frac{8T_r}{3}\left[
                  \phi\left(1 - \log T_r\right) - \log(3v_r - 1)
              \right] - \frac{3}{v_r},
        \label{eq:ex.aTv}
    \end{equation}
    %
    \noindent where $a_r \equiv a / (P_{cr}v_{cr})$ is the reduced  specific  Helmholtz  energy,
    $T_r \equiv T / T_{cr}$ is the reduced temperature, $\phi \equiv \bar{c}_v / \bar{R}$  is  a
    constant dimensionless isochoric specific heat, $v_r \equiv  v  /  v_{cr}$  is  the  reduced
    specific volume, $P_{cr}$ is the substance critical  pressure,  $T_{cr}$  is  the  substance
    critical temperature, and $v_{cr} = Z_{cr}(RT_{cr}/P_{cr})$ is the substance model  critical
    specific volume, with $Z_{cr} = 3/8$.

    The  Equation  of  State,  EoS,  of  this   substance   is   obtained   by   differentiating
    Eq.~(\ref{eq:ex.aTv}) partially with respect to $v_r$~\cite{1986-JonesJB+HawkinsGA-Wiley}:
    %
    \begin{align}
        P_r & = \parxyz{a_r}{v_r}{T_r} & \rightharpoondown\\
        P_r & = \frac{8T_r}{3v_r - 1} - \frac{3}{v_r^2}, \label{eq:ex.EoS}
    \end{align}
    %
    \noindent where $P_r \equiv P / P_{cr}$ is the reduced pressure, and $P$  the  (dimensional)
    pressure. Eq.~(\ref{eq:ex.EoS}) is  known  as  the  universal~\cite{1899-MaxwellJC-Longmans}
    van-der-Waals~\cite{2006-BejanA-Wiley,                           2013-CengelYA+BolesMA-AMGH,
    1986-JonesJB+HawkinsGA-Wiley} equation of state in reduced coordinates.

    The entropy of this substance is obtained by differentiating Eq.~(\ref{eq:ex.aTv}) partially
    with respect to $T_r$~\cite{1986-JonesJB+HawkinsGA-Wiley}:
    %
    \begin{align}
        s_r & = -\parxyz{a_r}{T_r}{v_r} & \rightharpoondown\\
        s_r & = \frac{8}{3}\left[\phi\log T_r + \log(3v_r - 1)\right], \label{eq:ex.sTv}
    \end{align}
    %
    \noindent where $s_r \equiv s(T_{cr} / P_{cr}v_{cr})$, is the reduced  specific  entropy  of
    the substance and $s$ is the (dimensional) specific entropy.

    Let
    %
    \begin{equation}
        \omega_r \equiv v_r - \frac{1}{3},
        \label{eq:def.omega}
    \end{equation}
    %
    \noindent be the reduced specific voids volume---since the minimum  van-der-Waals  substance
    specific reduced volume, $v_{r,min} = 1/3$, for which $\omega_{r,min} = 0$.

    Therefore, \emph{isentropic processes} in this const-$c_v$, van-der-Waals substance, between
    states $(T_{r1}, \omega_{r1})$ and $(T_{r2}, \omega_{r2})$ are given by:
    %
    \begin{align}
        s_{r2} - s_{r1} & = 0 \qquad\rightharpoondown\\
        \phi\log T_{r2} + \log\omega_{r2} & = \phi\log T_{r1} + \log\omega_{r1},
    \end{align}
    %
    \noindent which, exponentiated and rearranged, leads to
    %
    \begin{equation}
        \omega_{r2}T_{r2}^{\phi} = \omega_{r1}T_{r1}^{\phi},
        \label{eq:vdW.polyek}
    \end{equation}
    %
    \noindent which is a polyekthetic process of constant $ij^n$ with $i  \equiv  \omega_r$,  $j
    \equiv T_r$, and $n \equiv \phi$.

    Since the process is also  isentropic,  one  has  the  following  constant-property  process
    polyekthetic exponent
    %
    \begin{equation}
        k_{\omega_rT_rs} = \phi \equiv \frac{\bar{c}_v}{\bar{R}},
        \label{eq:vdW.kwTs}
    \end{equation}
    %
    \noindent for this constant-$c_v$, van-der-Waals substance.

    The `K-polytropic' theorem of reference~\cite[p.~8]{2020-NaaktgeborenC-engrXiv} states

    \begin{quote}
        ``Internally reversible processes in constant-specific-heat  unreactive  closed  systems
        with negligible kinetic and potential energy changes  and  constant  heat-to-work  ratio
        interactions, $K$, are exact polytropic processes only if  the  substance  is  an  ideal
        gas.''
    \end{quote}

    Isentropic compressions and expansions have  constant  heat-to-work  ratio  interactions  of
    $K=0$, since isentropic processes are also adiabatic. Therefore, the `K-polytropic'  theorem
    forbids non-ideal substances (such  as  van-der-Waals  ones)  to  display  exact  polytropic
    processes under stated conditions.

    Nonetheless,  Eqs.~(\ref{eq:vdW.polyek})  and~(\ref{eq:vdW.kwTs})   show   that   isentropic
    processes in constant-$c_v$, van-der-Waals  substances  \emph{can}  be  written  as  exactly
    polyekthetic processes with constant polyekthetic exponents.

    This one example illustrates how the polytropic process generalization proposed in this work
    achieved the desired outcome of exactly representing a process that could not be  classified
    as an exact polytropic process~\cite{2020-NaaktgeborenC-engrXiv}.

    %---------------------------------------------------------------------------------------
    \subsection{General Const-Property Polytropic Processes}

    Since  polytropic  processes  are  also  polyekthetic  processes---by  fixing  the  base  as
    $Pv$---one  can  apply  either  Eq.~(\ref{eq:polyek.k})  or~(\ref{eq:k.bri})   in   deriving
    polytropic process exponents  $k_{Pv\ell}$  for  $\ell  \in  \{T,  u,  h,  s,  a,  g\}$  for
    (i)~generic  substance  models,  and  for  (ii)~particular  cases,  such  as  ideal   gases.
    Table~\ref{tab:kPv} brings such values \emph{mostly} in terms of properties that are  easily
    measurable in the laboratory, i.e., in terms of $P$, $T$, $v$, $c_p$, $\beta$, $\kappa$, and
    $\gamma \equiv c_p / c_v$, of which some definitions are given on  Eq.~(\ref{eq:auxs}),  but
    also in terms of the specific entropy $s$:

    \begin{table}[ht]
        \centering
        \caption{General values for $k_{Pv\ell}$ polyekthetic exponents, and for the  ideal  gas
            limit}
        \vspace{4pt}
        \begin{tabular}{ccc}
            \toprule
            Exponent    & General Value &
            Ideal gas limit \\
            \midrule
            $k_{PvP}$   &
            $0$ &
            $0$ \\[\bigskipamount]
            $k_{Pvv}$   &
            $\pm\infty$ &
            $\pm\infty$ \\[\bigskipamount]
            $k_{PvT}$   &
            \(\displaystyle\frac{1}{P\kappa}\) &
            $1$ \\[\bigskipamount]
            $k_{Pvu}$   &
            \(\displaystyle\frac{c_p - \beta Pv}{P(\kappa c_p - T\beta^2v)}\) &
            $1$ \\[\bigskipamount]
            $k_{Pvh}$   &
            \(\displaystyle\frac{c_p}{P[\kappa c_p + \beta v(1 - \beta T)]}\) &
            $1$ \\[\bigskipamount]
            $k_{Pvs}$   &
            \(\displaystyle\frac{c_p}{P(\kappa c_p - T\beta^2v)}\) &
            $\gamma$ \\[\bigskipamount]
            $k_{Pva}$   &
            \(\displaystyle\frac{s + \beta Pv}{P\kappa s}\) &
            \(\displaystyle 1 + \frac{R}{s}\) \\[\bigskipamount]
            $k_{Pvg}$   &
            \(\displaystyle\frac{s}{P(\kappa s - \beta v)}\) &
            \(\displaystyle\frac{s}{s - R}\) \\[\bigskipamount]
            \bottomrule
        \end{tabular}
        \label{tab:kPv}
    \end{table}

    \begin{equation}
        \beta \equiv \frac{1}{v}\parxyz vTP, \qquad
        \kappa \equiv \frac{-1}{v} \parxyz vPT.
        \label{eq:auxs}
    \end{equation}

    Results  show  an  agreement  between  the  polytropic  and  the  corresponding   $Pv$-based
    polyekthetic exponents. The polyekthetic exponent results also show  that  constant-$a$  and
    constant-$g$ process aren't exactly polytropic, even for constant-specific-heat ideal gases,
    given that such exponents are a function of the entropy.

%-----------------------------------------------------------------------------------------------
\section{Conclusions}

    In this work, a generalization of polytropic processes, inspired by their etymology (of many
    ways,  many  forms)  and   by   the   works   of   Bejan~\cite{2006-BejanA-Wiley}   and   of
    Nederstigt~\cite{2017-NederstigtP-TUDelft}, named `polyekthetic' (multi-exponent) processes,
    is proposed.

    It is shown that the  polytropic  process  set  is  a  proper  subset  of  the  polyekthetic
    one---meaning that every polytropic process is also a polyekthetic one but  not  necessarily
    the reverse.

    Moreover, a useful kind  (or  subset)  of  polyekthetic  processes  consisting  of  constant
    arbitrary  property,  and  their  respective  special-notation   $k_{ij\ell}$   polyekthetic
    exponents were defined. Their basic properties were determined, and a general  solution  was
    found   for   them,   stated   either   in   terms   of   general    partial    derivatives,
    Eq.~(\ref{eq:polyek.k}), or of Bridgman's primitives, Eq.~(\ref{eq:k.bri}).

    An example of exact polyekthetic process that is not an exact polytropic process  was  given
    for a van-der-Waals substance. Moreover, generalized polytropic relationships for \emph{any}
    substance model, and their corresponding ideal gas limit values were derived from Bridgman's
    relations  and  listed  on  Table~\ref{tab:kPv}.  Results  showed  that   constant-$a$   and
    constant-$g$ process aren't exactly polytropic, even for constant-specific-heat ideal gases,
    since the entropy explicitly appears in the polytropic exponent of such processes.

%-----------------------------------------------------------------------------------------------


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

%%% \documentclass{article}
%%% \usepackage{graphicx}
%%% \usepackage{color}

%%% \sloppy
%%% \definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

%%% \begin{document}

\subsection*{Signal Spectrum by means of Fast fourier transform}

\begin{par}
Example for algorithm SP-FFT.
\end{par} \vspace{1em}
\begin{par}
Calculates frequency and phase spectrum by means of Fast Fourier Transform algorithm. Result is normalized.
\end{par} \vspace{1em}

\subsubsection*{Contents}

\begin{itemize}
\setlength{\itemsep}{-1ex}
   \item Generate sample data
   \item Call algorithm
   \item Display results
\end{itemize}

\subsubsection*{Generate sample data}

\begin{par}
Two quantities are prepared: \lstinline{y} and \lstinline{fs}, representing 1 second of signal containing 5 harmonic components and one inter-harmonic component. Main signal component has nominal frequency 1 kHz, nominal amplitude 2 V, nominal phase 1 rad and offset 1 V sampled at sampling frequency 10 kHz.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
DI = [];
fsnom = 1e4; Anom = 2; fnom = 100; phnom = 1; Onom = 0.2;
t = [0:1/fsnom:1-1/fsnom];
DI.y.v = Anom*sin(2*pi*fnom*t + phnom);
for i = 2:45
        DI.y.v = DI.y.v + Anom./i*sin(2*pi*fnom*i*t + phnom + i - 1);
end
DI.y.v = DI.y.v + 1*sin(2*pi*fnom*1.456*t + phnom);
DI.fs.v = fsnom;
\end{lstlisting}

\subsubsection*{Call algorithm}

\begin{par}
Use QWTB to apply algorithm \lstinline{SP-FFT} to data \lstinline{DI}.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
DO = qwtb('SP-FFT', DI);
\end{lstlisting}

        \begin{lstlisting}[style=output]
QWTB: no uncertainty calculation
\end{lstlisting} \color{black}

\subsubsection*{Display results}

\begin{par}
Results is the amplitude and phase spectrum.
\end{par} \vspace{1em}
\begin{lstlisting}[style=mcode]
figure
plot(DO.f.v, DO.A.v, '-x')
xlabel('f (Hz)'); ylabel('A (V)'); title('Amplitude spectrum of the signal');
figure
plot(DO.f.v, DO.ph.v, '-x')
xlabel('f (Hz)'); ylabel('phase (rad)'); title('Phase spectrum of the signal');
\end{lstlisting}

\begin{center}
\includegraphics[width=0.7\textwidth]{algs_examples_published/SP-FFT_alg_example_01.pdf}
\end{center}

\begin{center}
\includegraphics[width=0.7\textwidth]{algs_examples_published/SP-FFT_alg_example_02.pdf}
\end{center}

%%% \end{document}


\chapter{Visualize spectral distances}
\label{ch:visualize-spectral-distances}

\newthought{Let's consider a spectrum}, or any other data entry for that matter, as a point in a multidimensional space. We can define distance metrics between these points and visualize the distance values from one another or from a selected reference point or reference spectrum. By doing so, we can explore how similar our measurements are to a selected reference. We can do this on a series of spectra or even on hyperspectral maps!

\begin{figure}[h]
    \centering
	\vspace{0cm}
    \includegraphics[width=\textwidth]{workflow.png}
    \caption{Load the \textit{'Liver cirrhosis - spectral image'} dataset from the \widget{Datasets} widget and calculate the \textit{Euclidean distances} from the average spectrum with the \widget{Neighbors} widget. Visualize them in \widget{Hyperspectra}.}
\end{figure}

\vspace{-0.5cm}

\noindent Can you reproduce the results below? Pay attention to the color scheme.

\begin{figure*}[h]
\centering
\vspace{-0.5cm}
\infinitewidthbox{
  \stackinset{r}{-0.5\linewidth}{t}{+0.1\linewidth}
  {\includegraphics[scale=0.4]{ch-visualize_spectral_distances-fig3.png}}
  {\includegraphics[scale=0.6]{ch-visualize_spectral_distances-fig2.png}}
  \hspace{8cm}
  }
%\caption{Try changing the parameters!}
\end{figure*}

\vspace{-0.5cm}

\noindent Explore different distance metrics, inspect distances in a \widget{Data Table} widget. Don't forget, you can select points on the top map and see the corresponding spectra on the bottom in \widget{Hyperspectra}.

\lecnotes{Possibility for discussion of the general mathematical properties of distance functions. See \url{https://en.wikipedia.org/wiki/Metric_(mathematics)}}


\chapter{Variational inference}
\section{Variational inference}
Variational inference \cite{Bishop_2007,Blei_2017,Murphy_2012,Zhang_2019} is an approximate inference technique that, as Laplace's approximation, tries to approximate a density $g(\theta)$ by some density $q(\theta)$, using the unnormalized density $\gu(\theta) = Z g(\theta)$. In the context of variational inference, this $q(\theta)$ will be called the \textit{variational approximation}. Usually in the context of Bayesian inference, $g(\theta) = p(\theta|\mathcal{D})$ and $\gu(\theta) = p(\mathcal{D}|\theta)p(\theta)$.

Unlike Laplace's approximation, variational inference is concerned in choosing $q(\theta)$ by minimizing a global measure of dissimilarity between the distributions $q(\theta)$ and $g(\theta)$, called a \textit{divergence}. A divergence $D$ on a space $S$ of probability densities with the same support is a function $D(\cdot || \cdot) : S \times S \to \mathbb{R}$ such that:
\begin{equation}
\begin{split}
& D(q||p) \geq 0, \, p,q \in S \\
& D(q||p) = 0 \iff p = q.
\end{split}
\end{equation}
Thus divergences are a weaker form of a distance, and in general most important classes of divergences do not satisfy neither symmetry nor the triangle inequality. The objective of variational inference is then, given the target distribution $g$, and a set of candidate distributions $\mathcal{Q}$, to use a divergence $D$ to find an approximation $q^* \in \mathcal{D}$ for $g$ such that
\begin{equation}\label{viobjective1}
 q^* = \argmin_{q \in \mathcal{Q}} D(q || g).
\end{equation}

If $g \in \mathcal{Q}$, then obviously $q^* = g$. However, $\mathcal{Q}$ is a set of distributions chosen so that its elements are easy to work with, and this is not in general the case for $g$. Usually $\mathcal{Q}$ is parameterized by a set of continuous parameters $\Lambda \subset \mathbb{R}^m$, such that $q(\theta) = q(\theta;\lambda)$. Then, the problem of minimizing \eqref{viobjective1} becomes a continuous optimization problem
\begin{equation}
 \lambda^* = \argmin_{\lambda \in \Lambda} D(q(\cdot;\lambda)||g),
\end{equation}
and $q^*(\theta) = q(\theta;\lambda^*)$

\subsection{KL divergence and evidence lower bound}
Arguably the most important divergence between probability distributions, widely used in information theory, is the \textit{Kullback-Leibner} (KL) divergence $D_{KL}$, given by \footnote{The KL divergence has origins in information theory, and for discrete distributions, it can be interpreted as the average additional information one has to transmit a receiver when you are modeling a random variable distributed according to $q$ by $p$ \cite{Murphy_2012}}
\begin{equation}
D_{KL}(q||p) = -\Ev_{\theta \sim q(\theta)} \left[ \log \frac{p(\theta)}{q(\theta)} \right].
\end{equation}
Variational inference has in general the minimization objective $D_{KL}(q||g)$, although some recent methods have been concerned with other divergence objectives  \cite{Hernandes-Lobato_2015,Yingzhen_2016,Wang_2018} \footnote{Some authors reserve the term variational inference just for the objective $D_{KL}(q||g)$}. Notice that, since $D_{KL}(q||g) \neq D_{KL}(g||q)$, minimizing $D_{KL}(q||g)$ is different from minimizing $D_{KL}(g||q)$. In fact, the later minimization objective ends up with the related \textit{expectation propagation} technique for approximate inference \cite{Bishop_2007}. In this work we are mainly concerned with the variational inference $D_{KL}(q||g)$ objective.

Since $g(\theta) = \gu(\theta)/Z$, $D_{KL}(q||g)$ can be rewritten as:
\begin{equation}
D_{KL}(q||g) = -\bigg(\Ev_{\theta \sim q(\theta)}[\log \gu(\theta)] -\Ev_{\theta \sim q(\theta)}[\log q(\theta)]\bigg) + \log Z,
\end{equation}
The quantity inside parenthesis is called the \textit{evidence lower bound} (ELBO)
\begin{equation}\label{elbodef}
\mathcal{L}_\gu(q) = \Ev_{\theta \sim q(\theta)}[\log \gu(\theta)] + \mathcal{H}(q),
\end{equation}
where $\mathcal{H}(q) := -\Ev_{\theta \sim q(\theta)}[\log(q(\theta))]$ is the differential entropy of $q$.

In general, the dependence on $\gu$ will be omitted until Section \ref{vbmc_section}, and the ELBO will be denoted as $\mathcal{L}(q)$. Minimizing $D_{KL}(q||g)$ is equivalent to maximizing $\mathcal{L}(q)$. This way, there is no need to calculate the normalization factor $Z$, thus markedly improving the flexibility of the method, and the goal becomes
\begin{equation}
q^* = \argmax_{q \in \mathcal{Q}} \mathcal{L}(q).
\end{equation}
The ELBO is so called because, considering again $p(\theta|\mathcal{D})$, we have that
\begin{equation}
\begin{split}
\log p(\mathcal{D}) & = \log \Ev_{\theta \sim p(\theta)} [p(\mathcal{D}|\theta)] \\
& = \log \Ev_{\theta \sim q(\theta)} \left[\frac{p(\mathcal{D}|\theta) p(\theta)}{q(\theta)} \right] \\
& \geq \Ev_{\theta \sim q(\theta)} \left[ \log \frac{p(\mathcal{D}|\theta) p(\theta)}{q(\theta)} \right] = \mathcal{L}(q).
\end{split}
\end{equation}
So the ELBO provides a lower bound for the evidence of the model. This means that, when doing model selection between various models $M_1$,\ldots,$M_t$, one can find their corresponding variational distributions $q^*_{M_1},\ldots,q^*_{M_t}$ and $\mathcal{L}(q^*_{M_t}),\ldots,\mathcal{L}(q^*_{M_t})$, and then choose the model $M$ with the maximum $\mathcal{L}(q^*_{M})$, as a proxy for \eqref{modelselectionobjective}. Notice that this is a heuristic, and there is no guarantee that the model with maximum ELBO is actually the one with maximum evidence.

\subsubsection{Qualitative interpretations}
One possible interpretation for maximizing the ELBO is that maximizing the first ELBO term
\begin{equation}\label{elboterm1}
 \Ev_{\theta \sim q(\theta)}[\log \gu(\theta)],
\end{equation}
is the algorithm \enquote{trying} to make $q$ have a high probability density wherever $\gu$ has a high unnormalized density, while maximizing the second ELBO term,
\begin{equation}
 \mathcal{H}(q) = -\Ev_{\theta \sim q(\theta)}[\log q(\theta)],
\end{equation}
acts as a sort of regularizer preventing $q$ to degenerate to a point mass at the maximum of $\gu$.

It is informative to understand qualitatively which kind of approximations of $g$ variational inference will seek. Suppose some algorithm minimizes $D_{KL}(q || g)$ for $q \in \mathcal{Q}$. Since the algorithm \enquote{wants} to make the integrand $q(\theta) (\log q(\theta) - \log g(\theta))$ small, where $g(\theta)$ is close to zero, the $-\log g(\theta)$ term will quickly become large, unless $q(\theta)$ is also close to zero there. However, where $g(\theta)$ is reasonable far away from zero, the algorithm will not \enquote{feel} as much pressure to match $q(\theta)$ to the same value, provided that the algorithm assign large values for $q(\theta)$ where $g(\theta)$ is already large. Then, variational inference will tend to underestimate the region where $g(\theta)$ is large. By contrast, expectation propagation will have the reverse behavior, overestimating the region where $g(\theta)$ is far from zero \cite{Bishop_2007}. This is shown in Figure \ref{vixepfigure}.

\begin{figure}
	\centering
	\subfloat[$D_{KL}(q||g)$ ]{\label{vixep1a}\includegraphics[width=0.3\textwidth]
		{figs/klil3a.png}}
	\subfloat[$D_{KL}(g||q)$]{\label{vixep1b}\includegraphics[width=0.3\textwidth]
		{figs/klil3b.png}}

	\caption[Difference of behavior when minimizing $D_{KL}(q||g)$ and $D_{KL}(g||q)$]{\label{vixepfigure} Difference of behavior when minimizing $D_{KL}(q||g)$ (a) and when minimizing $D_{KL}(g||q)$ (b). Here the true distribution (in blue) is approximated by a multivariate normal distribution with diagonal covariance (in red). Example inspired by \cite{Bishop_2007}. Generating code can be found in \url{https://github.com/DFNaiff/Dissertation/blob/master/illustrations_dissertation/kl_illustrative_2.py}.}
\end{figure}

\section{Mean field variational inference}
Traditionally, variational inference has mainly been concerned with factorized variational approximations of the form $q(\theta;\lambda) = \prod_{i=1}^D q_i(\theta_i;\lambda_i)$, called \textit{mean field approximation}. The use of such variational posterior greatly simplifies the optimization of ELBO by coordinate descent.

To see this, consider a single term $j$ of the variational approximation $q(\theta) = \prod_{i=1}^D q_i(\theta)$. The ELBO for $q(\theta)$ is:
\begin{equation} \label{mfviexpansion}
\begin{split}
\mathcal{L}(q) & = \int \log \gu(\theta) \prod_{i=1}^D q_i(\theta_i) d \theta - \int \left(\sum_{j=1}^D \log q_i(\theta_i) \right) \prod_{i=1}^D q_i(\theta_i) d \theta \\
	     & = \int q_j(\theta_j) \left( \int \log \gu(\theta) \prod_{i \neq j} q_i(\theta_i) d \theta_i \right) d\theta_j + \sum_{i=1}^D \mathcal{H}(q_i) \\
	     & = \int q_j(\theta_j) \log \phi_j (\theta_j) d\theta_j + \mathcal{H}(q_j) + \sum_{i\neq j} \mathcal{H}(q_i),
\end{split}
\end{equation}
With
\begin{displaymath}
\log \phi_j(\theta_j) := \int \log(\gu(\theta)) q_{-j}(\theta_{-j}) d\theta_{-j} = \Ev_{\theta_{-j} \sim q_{-j}} [ \log \gu(\theta) ],
\end{displaymath}
using the notation $q_{-j}(\theta_{-j}) = \prod_{i\neq j}q_i(\theta_i)$.
Now, assuming $\exp ( \log \phi_j(\theta_j))$ to be integrable over the support of $g(\theta)$, fix every $q_i$ for $i \neq j$, so that the only term to be maximized is $q_j$. Then, maximizing \eqref{mfviexpansion} in respect to $q_j$ is equivalent to maximizing the ELBO between $q_j$ and $\exp \left(\log \phi_j(\theta_j)\right) $. Since there are not any constraints in the choice for $q_j$
\begin{equation} \label{mfviqj}
q^{*}_j(\theta_j;q_{-j}) \propto \exp \Ev_{\theta_{-j} \sim q_{-j}} [ \log \gu(\theta)].
\end{equation}
This readily gives an algorithm to find $q^* = \prod q^*_j$: initialize $q_1,\ldots,q_D$ in a appropriate manner, and then optimize cyclically \eqref{mfviqj}.

Convergence is guaranteed due to the convexity of the bound with respect to each factor \cite{Bishop_2007,Boyd_2004}. This algorithm interacts well with target densities whose conditional distributions $g(\theta_j|\theta_{-j})$ belongs to the exponential family, that is,
\begin{equation}\label{exponential_family}
 g(\theta_j|\theta_{-j}) = h(\theta_j) \exp \left( \eta_j(\theta_{-j})^T t(\theta_j) - a(\eta_j(\theta_{-j})) \right),
\end{equation}
\eqref{mfviqj} reduces to \cite{Blei_2017}
\begin{equation}
 q^*_j(\theta_j;q_{-j}) \propto h(\theta_j) \exp \left( \Ev_{\theta_j \sim q_{-j}} [\eta_j(\theta_{-j})]^T t(\theta_{-j}) \right),
\end{equation}
thus if $\nu_j = \Ev_{\theta_{-j} \sim q_{-j}}[\eta_j(\theta_{-j})]^T$ is available analytically, the coordinate descent becomes relatively simple.

Research to extend mean field variational inference to large datasets or dimensionality exists \cite{Hensman_2012,Hoffman_2013,Zhang_2019}, however factorized limitations are limited, particularly in its independence assumption. Moreover, many distributions are not in the exponential family, which is the focus of the mean field method, so more generic methods are desirable.

\section{Generic variational inference}
Some recent advances in variational inference \cite{Zhang_2019} are concerned with expanding both the set of possible variational approximations $\mathcal{Q}$ and approximating general classes of posterior distributions $g(\theta)$.

Considering again $\mathcal{Q}$ to be parameterized by a continuous set of parameters $\Lambda$, and using the overloaded notation $\mathcal{L}(\lambda) = \mathcal{L}(q(\cdot;\lambda))$, return to the optimization problem
\begin{equation}
\begin{split}
\lambda^* = \argmax_{\lambda \in \Lambda} \mathcal{L}(\lambda) & = \argmax_{\lambda \in \Lambda} \left( \Ev_{\theta \sim q(\theta;\lambda)}[\log \gu(\theta)] + \mathcal{H}(q(\cdot;\lambda)) \right)\\
& =  \argmax_{\lambda \in \Lambda} \Ev_{\theta \sim q(\theta;\lambda)}\left[\log \left(\frac{\gu(\theta)}{q(\theta;\lambda)}\right)\right]
\end{split}
\end{equation}
In general the expectations involved cannot be calculated analytically. However, if one represents $\nabla \mathcal{L}(\lambda)$ as expectations, then by using Monte Carlo methods it is possible to use stochastic gradients methods \cite{Robbins_1951,Kingma_2014,Qian_1999,Ruder_2016} to maximize $\mathcal{L}(\lambda)$ \footnote{In this aspect, modern variational inference research benefits greatly from deep learning research, the later relying heavily on stochastic gradient descent, driving much of the recent development of these algorithms.}.

\subsection{REINFORCE}
One proposal for doing this is given in \cite{Wingate_2013} and \cite{Ranganath_2014}, where $\nabla \mathcal{L}(\lambda)$ is rewritten as \footnote{A quick derivation of this approximation is done in the Appendix \ref{appendixreinforce}.}
\begin{equation}\label{reinforce}
\nabla \mathcal{L}(\lambda) = \Ev_{\theta \sim q(\theta;\lambda)} \left[ \log \frac{\gu(\theta)}{q(\theta;\lambda)} \nabla_{\lambda} \log q(\theta;\lambda) \right],
\end{equation}
and is approximated with its Monte Carlo estimator
\begin{equation}\label{reinforcemc}
\nabla \mathcal{L}(\lambda) \approx \frac{1}{K} \sum_{i \in [K], \theta_i \sim q(\theta;\lambda)} \log \frac{\gu(\theta_i)}{q(\theta_i;\lambda)} \nabla_{\lambda} \log q(\theta_i;\lambda),
\end{equation}
where $[K] = \{1,\ldots,K\}$. In practice, this estimation suffers from high variance, which may hinder optimization. In \cite{Wingate_2013}, \eqref{reinforce} is substituted for
\begin{equation}
\nabla \mathcal{L}(\lambda) = \Ev_{\theta \sim q(\theta;\lambda)} \left[ \left( \log \left( \frac{\gu(\theta)}{q(\theta;\lambda)}\right) + C\right) \nabla_{\lambda} \log q(\theta;\lambda) \right],
\end{equation}
where $C$ is an arbitrary constant, which is adjusted to control variance \footnote{The reason that this constant can be added is because $\int C \nabla_\lambda \log q(\theta;\lambda) q(\theta;\lambda) d\theta = C \int \nabla_\lambda p(\theta;\lambda) d\theta = C \nabla_\lambda \int p(\theta;\lambda) d\theta = 0$.}. In \cite{Ranganath_2014}, the variance is controlled by Rao-Blackwellization and control variates instead. Other variance reduction methods for this gradient formulation are proposed in \cite{Titsias_2015} and \cite{Ruiz_2016}.

\subsection{Reparameterization trick}\label{reparameterizationsection}
An alternative to calculate the gradient of $\mathcal{L}(\lambda)$ as an expectation is known as the \textit{reparameterization trick} \cite{Kingma_2013}, which is a general technique for calculating gradients of expectations of continuous random variables.

To explain the general ideia, assume $X_\lambda$ is a continuous random variable distributed according to $f(x;\lambda)$, and one wants to calculate the gradient (in relation to $\lambda$) of
\begin{equation}\label{reparamderiv1}
\Ev_{X_\lambda} \left[ h(X_\lambda) \right] = \int h(x) f(x;\lambda) dx.
\end{equation}
We approximate \eqref{reparamderiv1} by Monte Carlo
\begin{equation}\label{reparamderiv2}
\Ev_{X_\lambda} \left[ h(X_\lambda) \right] \approx \sum_{i=1}^N h(x_{i,\lambda}), \quad x_{i,\lambda} \sim f(x;\lambda).
\end{equation}
Now suppose that there is some random variable $Y$, \textit{not depending on $\lambda$}, with density $r(y)$, such that $X_\lambda = s(Y;\lambda)$ (for instance, if $X_{\mu,\sigma} \sim \mathcal{N}(\mu,\sigma^2)$, then being $Y \sim \mathcal{N}(0,1)$, we have that $X_{\mu,\sigma} = s(Y;\mu,\sigma) = \sigma Y + \mu$). In this case, the Monte Carlo estimator \eqref{reparamderiv2} is rewritten as
\begin{equation}
\Ev_{X_\lambda} \left[ h(X_\lambda) \right] \approx \sum_{i=1}^N h(s(y_i,\lambda)), \quad y_i \sim r(y),
\end{equation}
which is an expression \textit{whose gradient in relation to $\lambda$ can be taken}, and is the Monte Carlo estimator of
\begin{equation}
\int h(s(y;\lambda)) r(y) dy = \Ev_Y [h(s(Y;\lambda))]
\end{equation}

Formally, by letting $A$ be the support of $Y$ and $B_\lambda$ be the support of $X_\lambda$, if $s(y;\lambda)$ if bijective with injective derivative:
\begin{equation}
\begin{split}
\Ev_{X_\lambda \sim f(x;\lambda)} [h(x)] & = \int_{B_\lambda} h(x) q(x;\lambda) dx \\
& = \int_A h(s(y;\lambda)) q(s(y;\lambda);\lambda) |\text{det} (s'(y;\lambda))| dy \\
& = \int_A h(s(y;\lambda)) r(y) dy = \Ev_{Y \sim r(y)} [h(s(y;\lambda))],
\end{split}
\end{equation}
which shows that the reparameterization trick is valid.

To apply this to $\mathcal{L}(\lambda)$, assume $\theta_\lambda \sim q(\theta;\lambda)$ is such that $\theta_\lambda = s(\epsilon;\lambda)$, with $\epsilon \sim r(\epsilon)$. Then, applying reparameterization, we have
\begin{equation}\label{mcreparameterization}
\begin{split}
 \nabla \mathcal{L}(\lambda) & = \nabla \left(\Ev_{\theta \sim  q(\theta;\lambda)}\left[\log \frac{\gu(\theta)}{q(\theta;\lambda)}\right] \right) \\
 & =\nabla \left( \Ev_{\epsilon \sim r(\epsilon)} \left[\log \frac{ \gu(s(\epsilon;\lambda))}{q(s(\epsilon;\lambda);\lambda)}\right]\right) \\
 & \approx \nabla_\lambda \left( \frac{1}{K} \sum_{i \in [K], \epsilon_i \sim r(\epsilon)} \log \frac{ \gu(s(\epsilon_i;\lambda))}{q(s(\epsilon_i;\lambda);\lambda)} \right)
\end{split}
\end{equation}
In \cite{Zhang_2019}, it is argued that the observed lower variance of this estimation methods, if compared to the one given by \eqref{reinforce}, may be due to the fact that reparameterization trick takes in account the gradient of the target distribution, instead of just the gradient of the variational distribution as in \eqref{reinforce}. Moreover, in cases that the entropy of $q(\cdot;\lambda)$ can be estimated analytically, the reparameterization trick can be applied only to $\gu(\theta)$, leading to lower variance. Finally, it is important to notice that this format is more readily integrated in an automatic differentiation package, since it suffices to calculate the sum inside the gradient and backpropagate it in relation to $\lambda$.

\iffalse
These recent advances in the field, allowing the application of variational inference for a general variety of target distributions, has resulted in the technique return to some popularity, with recent probabilistic programming language focusing on it, such as Pyro \cite{Bingham_2018} and Edward \cite{Tran_2016}.
\fi

\section{Mixtures of gaussians for variational approximations}\label{mixgaussiansvi}

The idea of using mixture distributions for variational inference dates back to the late 90s \cite{Bishop_1997,Jaakkola_1998}, originally developed for a limited number of target distributions, and later is explored for approximating general distributions in \cite{Gershman_2012,Salimans_2012}, leading to recent work in it \cite{Acerbi_2018,Arenz_2018,Guo_2016,Jankowiak_2019,Miller_2016}. The technique presented here uses the reparameterization trick, in a vein similar to the one presented in \cite{Miller_2016}. In general, the mixture distribution considered is one of Gaussian distributions (as it is in this work), although many of those extends to more general mixtures.

More formally, consider as the set of candidate proposals
\begin{equation}
\mathcal{Q}_k := \left\{\sum_{i=1}^k w_i f(\cdot;\lambda_i) \middle| f(\cdot;\lambda_i) \in \mathcal{Q}_1, 1 \leq i \leq k; (w_1,...,w_k) \in \Delta_k \subset \mathbb{R}^k \right\},
\end{equation}
wher $\Delta_k$ denotes the probability simplex $\{(x_1,\ldots,x_k) \in {\mathbb{R}^+}^k| \sum_{i=1}^k x_k = 1\}$, and $\mathcal{Q}_1 = \{f(\cdot;\lambda) | \lambda \in \Lambda \subset \mathbb{R}^m\}$ is a parameterized set of distributions. In the mixture of multivariate normals case,
\begin{equation}
\mathcal{Q}_1 := \{f(\cdot ; \mu,\Sigma) = \mathcal{N}(\cdot;\mu,\Sigma) | \mu \in \mathbb{R}^d,\Sigma \in \mathbb{R}^{d \times d}, \Sigma \geq 0\}
\end{equation}
is the set of multivariate normal distributions. In many cases, it is interesting to restrict $\mathcal{Q}_1$ further so that $\Sigma$ is diagonal.
Mixtures of distributions are interesting as variational approximations since their expectations are easily available
\begin{equation}
\Ev_{X \sim \sum_i w_i f_i} [h(X)] = \sum_i w_i \Ev_{X_i \sim f_i} [h(X_i)],
\end{equation}
as well as their covariances
\begin{equation}
\begin{split}
& \Cov_{X \sim \sum_i w_i f_i}(X) = \sum_i w_i \left( \Sigma_i +   \mu_i \mu_i^T \right) - \mu \mu^T, \\
& \Sigma_i = \Cov_{X_i \sim f_i}(X_i), \quad \mu_i = \Ev_{X_i \sim f_i}[X], \quad \mu = \Ev_{X \sim \sum_i w_i f_i} [X].
\end{split}
\end{equation}
Furthermore, samples of mixtures can be easily generated from the base distributions, by choosing mixture $i$ with probability $w_i$, and then sampling $X$ from $f(\cdot;\lambda_i)$ \footnote{Batching this process in order to sample many variables, escaping loops in interpreted languages with support for numerical operations (such as Python with Numpy) requires some care, but it is possible in few lines.}. Furthermore, letting $\mathcal{Q}_\infty = \cup_{i=1}^\infty \mathcal{Q}_i$ and $\mathcal{Q}_1$ be these set of Gaussian distributions, $\mathcal{Q}_\infty$ is dense in the set of continuous distributions \cite{Epanechnikov_1969}, so in this case any continuous distribution can be approximated arbitrarily close, in principle.

Considering mixtures of Gaussians, for fixed $k$, in order to find the parameters
\begin{displaymath}
\lambda = (w_1,\mu_1,\Sigma_1,\ldots,w_k,\mu_k,\Sigma_k)
\end{displaymath}
of
\begin{equation}
 q^*_k = \argmax_{q_k \in \mathcal{Q}_k} \mathcal{L}(q_k),
\end{equation}
one needs first to find suitable parameterizations for $\Sigma_i$ and $\mathbf{w} := (w_1,...,w_k)$. For the covariance matrix, one can either consider only diagonal matrices
\begin{equation}
\Sigma_i = \text{diag}(\sigma^2_{i,1},\ldots,\sigma^2_{i,D}),
\end{equation}
or consider matrices of the form
\begin{displaymath}
 \Sigma_i = \u_i \u_i^T + \text{diag}(\sigma^2_{i,1},\ldots,\sigma^2_{i,D}),
\end{displaymath}
or use more advanced parameterizations such as the ones found in \cite{Pinheiro_1996}. Let $\sigma_i$ be the parameters for $\Sigma_i$, so that $\Sigma_i = \Sigma_i(\sigma_i)$. For $\mathbf{w}$, using some monotone bijective differentiable function $\phi : \mathbb{R} \to \mathbb{R}^+$ (for example, $\phi = \exp$), one can then consider the corresponding differentiable map $\Phi : \mathbb{R}^k \to \Delta_k$ as
\begin{equation}
\Phi(\nu_i) = \frac{\phi(\nu_i)}{\sum_{i=1}^k \phi(\nu_k)} = w_i(\nu_i).
\end{equation}

The parameter of interest $\lambda$ becomes $(\nu_1,\mu_1,\sigma_1,\ldots,\nu_k,\mu_k,\sigma_k)$, and
\begin{equation}
q_k(\theta) = \sum_{i=1}^k w_i(\nu_i) f_{\mathcal{N}(\mu_i,\Sigma(\sigma_i))}(\theta).
\end{equation}
Thus, the ELBO objective becomes
\begin{equation}\label{elbomixturegaussians}
\begin{split}
\mathcal{L}(\lambda) & = \int \log \gu(\theta) q_k(\theta) d\theta - \int \log (q_k(\theta)) q_k(\theta) d \theta \\
& = \sum_{i=1}^k w_i(\nu_i) \Ev_{\theta_i \sim \mathcal{N}(\mu_i;\Sigma(\sigma_i))}\left[\log \frac{ \gu(\theta_i)}{q_k(\theta_i;\lambda)}\right].
\end{split}
\end{equation}

One can adapt the reparameterization trick to rewrite $\mathcal{L}(\lambda)$ in a manner suitable to stochastic gradient optimization. First notice that any gaussian random variable $X \sim \mathcal{N}(\mu,\Sigma)$ can be written as $X = \mu + A Z$, where $Z \sim \mathcal{N}(0,I)$ and $A$ is some matrix such that $\Sigma = A A^T$. For instance, $A$ can be the lower Cholesky factor of $\Sigma$, if the parameterization of $\Sigma$ only supports positive-definite matrices. Then $A(\sigma)$ is differentiable \cite{Smith_1995,Murray_2016}, and $s(\epsilon;\mu_i,\sigma_i) = \mu_i + A(\sigma) \epsilon$, and \eqref{elbomixturegaussians} is approximated by Monte Carlo,
\begin{equation}\label{mcmixturegaussians}
\begin{split}
\mathcal{L}(\lambda) & = \sum_{i=1}^k w_i(\nu_i) \Ev_{\epsilon \sim \mathcal{N}(0,I)}\left[\log \frac{ \gu(s(\epsilon;\mu_i,\sigma_i))}{q_k(s(\epsilon;\mu_i,\sigma_i);\lambda)}\right] \\
& \approx \sum_{i=1}^k w_i(\nu_i) \left( \frac{1}{K_i} \sum_{k \in [K_i], \epsilon_{i,j} \sim \mathcal{N}(0,I)} \log \frac{ \gu(s(\epsilon_{i,j};\mu_i,\sigma_i))}{q_k(s(\epsilon_{i,j};\mu_i,\sigma_i);\lambda)} \right),
\end{split}
\end{equation}
which is an expression that can be differentiated to find an approximation for $\nabla \mathcal{L}(\lambda)$.

In principle there is no way to know how many mixtures are necessary to return a good approximation. However, one can,for each $i \in \mathbb{N}$, starting with $i = 1$, sequentially find $q^*_i$ close to $\argmax_{q_i \in \mathcal{Q}_i} \mathcal{L}(q)$, and go to sequentially from $Q_i$ to $Q_{i+1} \supset Q_i$, until the variational approximation is good enough. However, this procedure runs into computational issues, namely the number of optimization parameters scale linearly with the number of mixtures $k$, in a non-convex problem with non-trivial gradient evaluation, whose cost also scales linearly with $k$. Hence, the cost of improving the mixtures this way quickly becomes rather large. We next present an approach that mitigates this problem, at the cost of making a greedy approximation, thus potentially more inefficient in the number of mixtures.

\subsection{Boosting mixtures of gaussians}\label{boostedvi_section}
Boosting \cite{Freund_1997,Freund_1999,Friedman_2000} is a standard technique in machine learning, usually used in classification problems, which tries to combine slighty better than chance algorithms, or \textit{weak learners} in a reliable, accurate algorithm (a \textit{strong learner}). The general framework is transferred to the problem of variational inference in concurrent works by \cite{Miller_2016} and \cite{Guo_2016}, using increasing mixtures distributions.

In this setting, start first with some distribution $q_1 \in \mathcal{Q}_1$. Then, recursively, given a proposal $q_{i-1}^*$,  it is considered the proposal set
\begin{equation}
\begin{split}
\mathcal{Q}_{i} = \mathcal{Q}_{i}(q_{i-1}) = \{ (& 1-w_{i}) q_{i-1} + w_{i}  f(\cdot;\mu_{i},\Sigma_{i}) | \\  & w_i \in [0,1], \mu_i \in \mathbb{R}^d, \Sigma_i \in \mathbb{R}^{d \times d}, \Sigma_i \geq 0\},
\end{split}
\end{equation}
and then some $q_i$ is chosen from $\mathcal{Q}_i$, in a manner that $\mathcal{L}(q_i)$ is reasonably greater than the previous value $\mathcal{L}(q_{i-1})$. One straightforward approach is to seek $\lambda_i (w_{i}^*,\mu_{i}^*,\Sigma_{i}^*)$ such as the maximization objective becomes
\begin{equation}\label{miller_objective}
 \mathcal{L}_{i}(\lambda_{i}) := \mathcal{L}((1-w_{i}) q_{i-1} + w_{i}  f(\cdot;\mu_{i},\Sigma_{i})),
\end{equation}
which is the procedure proposed in \cite{Miller_2016}. In \cite{Guo_2016}, it is shown that the KL divergence satisfies the conditions estabilished by \cite{Tong_Zhang_2003} that ensures if
\begin{equation}
 \mathcal{L}((1-w_i){i-1} + w_i f_i) \geq \sup_{f \in Q_1,w\in[0,1]} \mathcal{L}((1-w)q_{i-1} + w f) - \epsilon_i,
\end{equation}
as $\epsilon_i \to 0$, then
\begin{equation}
 \lim_{i \to \infty} \sup_{q \in Q_\infty} L(q) - \mathcal{L}(q_i) = 0,
\end{equation}
provided that every $q \in \mathcal{Q}_1$ is bounded from below. In \cite{Guo_2016}, it is shown that the KL divergence satisfies those two conditions, if every $q \in \mathcal{Q}_1$ is assumed to be bounded from below by a positive constant. Although this is not the case for Gaussian distributions, it is argued that since in actual implementations the practitioner works with a bounded set of interest, the result holds in practice.

\subsection{Gradient boosting mixture of gaussians}\label{gradboostsection}
Another boosting proposal, due to \cite{Guo_2016}, is to instead of trying to optimize jointly $(w_{i}^*,\mu_{i}^*,\Sigma_{i}^*)$ at each step, choosing first $f_{i} = f(\cdot;\mu_{i},\Sigma_{i})$, and then choose $w_i$ as to maximize:
\begin{equation}\label{boosting_objective_alpha}
\begin{split}
 \mathcal{L}_{i}(w_{i}) & := \mathcal{L}((1-w_{i}) q_{i-1} + w_{i}  f_{i}) \\
 & = \int \log \frac{\gu(\theta)}
				 {(1-w_{i}) q_{i-1}(\theta) + w_{i} f_{i}(\theta)} ((1-w_{i}) q_{i-1}(\theta) + w_{i} f_{i}(\theta)) d\theta.
 \end{split}
\end{equation}
Since the KL divergence is convex in $q$, the ELBO is concave, so minimizing $w$ is easy, provided we can easily calculated $\mathcal{L}'_i(w_i)$. Since we have
\begin{equation}\label{boosting_objective_dalpha}
\begin{split}
 \mathcal{L}'_{i}(w_{i}) & =
	 \int \frac{\partial}{\partial w_{i}} \left( \log  \frac{\gu(\theta)}
	 {(1-w_{i}) q_{i-1}(\theta) + w_{i} f_{i}(\theta)} ((1-w_{i}) q_{i-1}(\theta) + w_{i} f_{i}(\theta))\right) d\theta \\
	 & = \int \log (\gu(\theta)) (f_{i}(\theta) - q_{i-1}(\theta)) d\theta - \\
	 & \qquad{} \int \log((1-w_{i}) q_{i-1}(\theta) + w_{i} f_{i}(\theta)) (f_i(\theta) - q_{i-1}(\theta)) d\theta,
\end{split},
\end{equation}
we can then approximate the derivative by Monte Carlo
\begin{equation}
\begin{split}
\mathcal{L}'_i(w_i) = & \frac{1}{J} \sum_{j \in [J] \theta_{j} \sim f_i} \left(\log(\gu(\theta_j)) - \log((1-w_{i}) q_{i-1}(\theta_j) + w_{i} f_{i}(\theta_j))\right) \\
& -\frac{1}{K} \sum_{\theta_{k} \in [K], \theta_k \sim q_{i-1}} \left(\log(\gu(\theta_k)) - \log((1-w_{i}) q_{i-1}(\theta_k) + w_{i} f_{i}(\theta_k))\right),
\end{split}
\end{equation}
and using it to maximize $\mathcal{L}'_i(w_i)$.

The question becomes then how to choose $f_i$. In \cite{Guo_2016}, the technique of gradient boosting \cite{Friedman_2001} is borrowed for this purpose, so that $f_i$ is chosen as to minimize $\nabla D_{KL} (q_{i-1}||g)  \cdot f$, where $\nabla D_{KL}(q || g)$ is the functional derivative of $D_{KL}(q || g)$ as a function of $q$. For $D_{KL}(q || p)$, we can use Taylor expansion to find the functional derivative
\begin{equation}
\begin{split}
 D_{KL}(q + \delta h || p) & = \int (q + \delta h) \log \frac{q + \delta h}{p} \\
					  & = \int q \left(\log \frac{q}{p} + \frac{\delta h/p}{q/p} + \mathcal{O}(\delta^2) \right) + \delta \int h \log \frac{q}{p} + \mathcal{O}(\delta^2) \\
					  & = D(q||p) + \delta \int \left(1 + \log \frac{q}{p}\right) h + \mathcal{O}(\delta^2).
\end{split}
\end{equation}
Here the argument $\theta$ is omitted to simplify the expression. Hence
\begin{equation} \label{idealklgrad}
\begin{split}
f_i = \argmin_{f} \nabla D_{KL}(q_{i-1} || g) \cdot f & = \argmin_{f} \int \left(1 + \log \frac{q_{i-1}(\theta)}{g(\theta)}\right) f(\theta) d \theta = \\
& = \argmin_{f} \int \log \frac{q_{i-1}(\theta)}{g(\theta)} f(\theta) d\theta.
\end{split}
\end{equation}

Since the parameterization for $f_i(\theta)$ allows degeneration to a point mass at $\argmin_{\theta} \log (q_{i-1}(\theta) / g(\theta))$, and this point mass optimizes \eqref{idealklgrad}, further constraints over $f_i(\theta)$ are needed for getting a non-degenerate new basis function. In \cite{Guo_2016} \eqref{idealklgrad} is regularized by the logarithm of the $L_2$ norm of $f_i(\theta)$
\begin{equation}
f_i =  \argmin_{f} \left( \int \log \frac{q_{i-1}(\theta)}{g(\theta)} f(\theta) d\theta +  \frac{\lambda}{2} \log ||f||_2^2 \right)
\end{equation}
while in \cite{Locatelo_2018}, \eqref{idealklgrad} is regularized by negative of the entropy of $f$, as a proxy for the regularization of its $L_\infty$ norm. Since for $f(\theta) = \mathcal{N}(\theta|\mu,\Sigma)$, $\log ||f||_2^2 = -\frac{1}{2} \log |\Sigma| - \frac{1}{2} \log(2\pi)$ and $-\mathcal{H}(f) = -\frac{1}{2} \log |\Sigma| - \frac{1}{2} \log(2\pi e)$, both approaches are equivalent for mixture of Gaussians, yielding the maximization objective in relation to $\mu_i,\Sigma_i$:
\begin{equation}\label{relbogaussian}
\begin{split}
 \text{RELBO}(\mu_i,\Sigma_i) = & \int \log(\gu(\theta)) \mathcal{N}(\theta|\mu,\Sigma) d\theta - \int \log(q_{i-1}(\theta)) \mathcal{N}(\theta|\mu,\Sigma) d\theta + \\
 & \frac{\lambda}{4} \log |\Sigma|,
 \end{split}
\end{equation}
where $\lambda$ is a regularization constant (the name RELBO comes from \cite{Locatelo_2018}). This constant may be set either as a fixed value or decaying as $i$ increases, as to permit increasingly narrower distributions as the algorithm runs. For example, in \cite{Locatelo_2018} $\lambda$ is set as $1/\sqrt{i+1}$. \footnote{In \cite{Guo_2016} it is argued that this objective is still somewhat hard to maximize, so it is proposed an heuristic based on a local Laplace approximation. This heuristic was previously explored by the author of this work, however it ran into implementation issues, and moreover, it does not exactly attend the desiderata in this work, namely as few function evaluations as possible}. Finally, the gradients of \eqref{relbogaussian} in relation to $\mu_i,\Sigma_i$ can be estimated by the reparameterization trick.

The resulting pseudo-algorithm is shown in Figure \ref{vbalgorithm}.
\begin{Algorithm}
\begin{algorithmic}[1]\label{vbalgorithm}
\Procedure{VariationalBoosting}{$\log \gu,\mu_0$,$\Sigma_0$}
\LineComment{$\mu_0,\Sigma_0$ the are initial boosting values}
\State $w_0 := 1.0$
\For{$t=1,...,T$}
	\State $\mu_{t},\Sigma_{t} := \argmax RELBO(\mu_{t},\Sigma_{t})$ \Comment{Using reparameterization}
	\State $w_{t} := \argmax \mathcal{L}_i(w_i)$ \Comment{Using $\mathcal{L}'_t(w_t)$ for gradient descent}
	\For{$j=0,...,t-1$}
		\State $w_{j} \gets (1-w_t)w_j$
	\EndFor
\EndFor
\State \Return $\{(\mu_t,\Sigma_t,w_t)\}_{t=1}^T$
\EndProcedure
\end{algorithmic}
\caption{\label{vbalgorithm}Variational boosting algorithm.}
\end{Algorithm}

\section{Using Bayesian Monte Carlo in Variational Inference}\label{vbmc_section}

All the approaches previously presented suffers from one major flaw: the need for a large number of evaluations of the unnormalized posterior $\gu$ by Monte Carlo methods, in order to have a good estimation of the gradient. This is the same problem of integration that Bayesian Monte Carlo tries to solve, so one can try to use this technique when $\gu$ is expensive to evaluate. In \cite{Acerbi_2018}, it is developed a method called Variational Bayesian Monte Carlo (VBMC) that hinges on exactly this idea.

Consider a parameterized variational proposal $q(\theta;\lambda)$, and the corresponding ELBO for $\gu(\theta)$:
\begin{displaymath}
 \mathcal{L}(\lambda) = \mathcal{L}_\gu(\lambda) = \int \log \gu(\theta) q(\theta;\lambda) d\theta - \int \log(q(\theta;\lambda)) q(\theta;\lambda) d\theta.
\end{displaymath}
As in Chapter 4, setting as prior for $\log \gu(\theta)$ the Gaussian process $GP(m,k)$, and given set of evaluations $\mathcal{D}_0 = \{(x_i,f(x_i))\}_{i=1}^N$, $\mathcal{L}_\gu(\lambda)$ can be replaced by the Gaussian distributed random variable
\begin{equation}\label{vbmc_rvs}
\begin{split}
\mathcal{L}_\mathcal{D}(\lambda) := \mathcal{L}_{\gu_\mathcal{D}}(\lambda) &:= Z_\mathcal{D}(\lambda) - \int \log(q(\theta;\lambda)) q(\theta;\lambda) d\theta, \\
Z_\mathcal{D}(\lambda) & := \int \log \gu_\mathcal{D}(\theta) q(\theta;\lambda) d\theta,
\end{split}
\end{equation}
with mean
\begin{equation}
\begin{split}
\bar{\mathcal{L}}_\mathcal{D}(\lambda) := \Ev \left[ \mathcal{L}_{\log \gu_\mathcal{D}}(\lambda) \right] & = \Ev[Z_\mathcal{D}(\lambda)] - \int \log(q(\theta;\lambda)) q(\theta;\lambda) d\theta \\
& = \int \Ev[\log \gu_\mathcal{D}(\theta)] q(\theta;\lambda) d\theta - \int \log(q(\theta;\lambda)) q(\theta;\lambda) d\theta\\
& = \mathcal{L}_{\Ev[\log \gu_\mathcal{D}]} (\lambda), \\
\end{split}
\end{equation}
and variance
\begin{equation}
\Var(\mathcal{L}_\mathcal{D}(\lambda)) = \Var(Z_\mathcal{D}(\lambda)),
\end{equation}
and $\Ev[Z_\mathcal{D}(\lambda)]$ and $\Var[Z_\mathcal{D}(\lambda)]$ being given by \eqref{evvarbmc2}.
This approach enjoy the same benefits as the BMC approach:
\begin{itemize}
	\item If the evaluation of $\gu$ is expensive, it is not feasible to perform a Monte Carlo estimation of $\int \log \gu(\theta) q (\theta;\lambda) d \theta$ at each step in optimizing the ELBO.
	\item The function $\gu$ does not need to be differentiable, and its evaluations may be noisy (although this last case is not considered in this work, the extension is straightforward).
\end{itemize}
In case $q(\theta;\lambda)$ is a mixture of $i$ Gaussians $q_i(\theta;\lambda)$, as in \cite{Acerbi_2018}, the methods presented in \ref{mixgaussiansvi} and the one in Chapter 4, by the discussion in Section \ref{kerneldistributionbmc}, given a suitable kernel $k$, $\Ev[Z_\mathcal{D}(\lambda)]$ and $\Var[Z_\mathcal{D}(\lambda)]$ can be easily treated. In \cite{Acerbi_2018}, $k$ is the SQE kernel \eqref{sqekernel} with outputscale $r_0$ and lengthscales $l_1,\ldots,l_D$, which by \eqref{bmcmixgaussians} makes $\Ev[Z_\mathcal{D}(\lambda)]$ and $\Var[Z_\mathcal{D}(\lambda)]$ both analytical.

The entropy term $-\int \log(q(\theta;\lambda)) q(\theta;\lambda) d\theta$ does not have in general a closed form, and must still be treated. Approximating $-\log(q(\theta;\lambda))$ by BMC is possible, but since this term changes with $\lambda$, this will be costly. However, since $\log q(\theta;\lambda)$ is easy to evaluate, by construction, the entropy term can be treated with the reparameterization trick without much additional cost. Other possible approach to deal with this term, when using mixture of Gaussians, is to use one of the approximations proposed in \cite{Huber_2008}.

An important point is that, since, by in the first equation of \eqref{evvarbmc2}, only $\mathbf{z}$ and $\int m(\theta) q(\theta;\lambda) d\theta$ depends on $q(\theta;\lambda)$, one can rewrite
\begin{equation}
\begin{split}
\int \Ev[\log \gu_\mathcal{D}(\theta)] q_k(\theta;\lambda) d\theta & = M(\lambda) + \mathbf{z}^T \mathbf{w} \\
\mathbf{w} & = K^{-1} \mathbf{y} \\
M(\lambda) & = \int m(\theta) q(\theta;\lambda) d\theta \\
\mathbf{z}_i & = \int k(x,x_i) q(\theta;\lambda) dx.
\end{split}
\end{equation}
This way, with $\mathbf{w}$ being previously computed, the computation of $\bar{\mathcal{L}}_\mathcal{D}(\lambda)$ can be calculated in $\mathcal{O}(N)$ time for each $\lambda$, while $\Var(\mathcal{L}_\mathcal{D}(\lambda))$ can be calculated in $\mathcal{O}(N^2)$. The new variational objective becomes then simply $\bar{\mathcal{L}_\mathcal{D}}(\lambda)$.

\subsection{Quadratic mean function}\label{quadmeanfnsection}
With the objective $\bar{\mathcal{L}}_\mathcal{D}(\lambda)$, the actual distribution that is being approximated by mixture of Gaussians is proportional to $\exp \Ev[\log \gu_\mathcal{D}(\theta)]$. From the variational inference approximation's point of view, the only information that it has on the original distribution $\gu$ is by its GP approximation.

This raises some issues: if the mean function of the GP prior $m(\theta)$ is either zero or a constant, and the kernel is one that decays to zero as $|x - x'|$ goes to infinity, such as \eqref{sqekernel}, \eqref{maternkernel} or \eqref{spectralmixturekernel}, then $\exp \Ev[\log \gu_\mathcal{D}(\theta)]$ is not integrable, so it cannot define an unnormalized probability distribution. Hence, care must be taken with the mean function $m(\theta)$.

One approach is to simply let $m(\theta)$ have a very low value, so that in practice it resembles enough a probability distribution so that, for a reasonable number of mixtures $k$, convergence issues does not appear. This is the default approach used in the developed algorithm presented in Chapter 4. An alternative approach, used in \cite{Acerbi_2018}, is to change the mean function so that $\exp(m(\theta))$, thus $\exp \log \Ev[\log \gu_\mathcal{D}(\theta)]$ is an unnormalized probability distribution, while having the integral $\int m(\theta) q_k(\theta) d \theta$ being analytically tractable. In \cite{Acerbi_2018}, the following mean function is proposed:
\begin{equation}\label{vbmc_quadratic_mean}
 m_{Q}(\theta;l,c) = - \frac{1}{2} \sum_{i=1}^D \frac{(\theta_i - c_i)^2}{l_i^2},
\end{equation}
which then both makes $\exp(m_Q(\theta;l,c))$ integrable and yields, for $q_i(\theta)$ being a mixture of Gaussians,
\begin{equation}
 \int m_{Q}(\theta;l,c) q_k(\theta) d\theta =
 	-\frac{1}{2} \sum_{j=1}^k \sum_{i=1}^D \left[\frac{(c_i - \mu_{k,i})^2}{l_i^2} + \frac{\Sigma_{k,i,i}}{l_i^2} \right].
\end{equation}
This comes immediately from the expectation of quadratic forms of normal distribution formula \cite{Petersen_2012}.

\subsection{Remarks on acquisition functions for VBMC}
As in Bayesian Monte Carlo, a question that arises is one of active sampling. Notice that, fixed evaluations $\mathcal{D}$, and assuming a fixed set of mixtures of $k$ Gaussians $\mathcal{Q}_k$, associated with a fixed set of parameters $\Lambda_k$, and letting
\begin{equation}
\lambda^*(\mathcal{D}) = \argmin_{\lambda} \mathcal{L}_\mathcal{D}(\lambda),
\end{equation}
the final ELBO is a random variable $\mathcal{L}_{\mathcal{D}}(\lambda^*(\mathcal{D}))$, which is normally distributed with variance $\Var(Z_\mathcal{D}(\lambda^*))$. Since we want to have a high degree of certainty about the variational approximation's quality, one wants the final ELBO objective to have as lower variance as possible, since it is a measure of its uncertainty.

In this setting, one option would be, in a greedy approach, to choose $x_{N+1}$, so that, expanding $\mathcal{D}$ to $\mathcal{D}'(x,\log \gu(\theta_{N+1})) = \mathcal{D} \cup \{(x_{N+1},\log \gu(\theta_{N+1}))\}$, it minimizes the variance of $\mathcal{L}_{\mathcal{D'}}(\lambda^*(\mathcal{D'}))$. Since $\log \gu(\theta_{N+1})$ is not known in advance, one could substitute this instead for
\begin{equation}
\begin{split}
 & \Ev_\mathcal{D'} \left[ \Var(Z_{\mathcal{D'}}(\lambda^*(\mathcal{D'})) \right] \\
 & \mathcal{D}' = \mathcal{D} \cup \{(\theta_{N+1},\log \gu_\mathcal{D}(\theta_{N+1}))\}.
 \end{split}
\end{equation}
However, it is not clear how to work with this random variable, and minimize a measure of its uncertainty. However, this intuition helps with constructing heuristic acquisition functions to seek $\theta_{N+1}$.

\subsubsection{Uncertainty sampling and prospective prediction}
One option in order to tackle this problem is to consider the current variational proposal $q_k(\theta;\lambda)$ fixed, and consider the minimization objective
\begin{equation}
 \alpha^\mathcal{D}_\text{VR}(\theta_{N+1}) = \Var\left(Z_{\mathcal{D}'(\theta_{N+1})}(\lambda)\right).
\end{equation}
However, since this is an expensive minimization objective, a proxy for this approach is to consider the integrand of
\begin{displaymath}
 \int \log \gu_\mathcal{D}(\theta) q_k(\theta;\lambda) d\theta,
\end{displaymath}
and seek the integrand maximum variance instead, resulting in the \textit{uncertainty sampling} maximization objective
\begin{equation}\label{us_vbmc}
 \alpha^\mathcal{D}_{\text{US}}(\theta_{N+1}) = k_\mathcal{D}(\theta_{N+1},\theta_{N+1}) q_k(\theta_{N+1};\lambda)^2.
\end{equation}

The above objective puts much weight in the current variational proposal, which may hinder exploration. Considering that future proposals will seek regions with high posterior density, the other objective proposed in \cite{Acerbi_2018} is formed by multiplying $\alpha^\mathcal{D}_{\text{US}}(\theta_{N+1})$ by an exploration factor for high posterior denstiies  $\exp(m_\mathcal{D}(\theta_{N+1}))$. This result in the \textit{prospective prediction} maximization objective,
\begin{equation}\label{prospective_vbmc}
\alpha^\mathcal{D}_{\text{PROP}}(\theta_{N+1}) = k_\mathcal{D}(\theta_{N+1},\theta_{N+1}) \exp(m_\mathcal{D}(\theta_{N+1}))q_k(\theta_{N+1};\lambda)^2.
\end{equation}
It is reported in \cite{Acerbi_2018} that the prospective prediction objective results in better approximations than the uncertainty sampling one. The objectives \eqref{us_vbmc} and \eqref{prospective_vbmc}, along with some newly proposed ones (\eqref{soft_prospective_vbmc},\eqref{mmlt_vbmc} and \eqref{mmltprop_vbmc}), are used in the algorithm developed in this work, to be shown in the next chapter.


\section{Insights and Algorithms}

\subsection{Exercises}

\subsubsection{Exercise 3.1}
Assume $f(n) = 2^{n} - 1$. Then $f(n + 1) = 2^{n + 1} - 2 + 1 = 2^{n + 1} - 1$.

\subsubsection{Exercise 3.2}
We have that
\begin{align*}
        (QQ^* )_{ab} &= \frac{1}{n}\sum_{k = 0}^{n - 1} w_n^{ak} \overline{w_n^{kb}} \\
                     &= \frac{1}{n}\sum_{k = 0}^{n - 1} w_n^{k(i - j)}\\
\end{align*}
If $i = j$, then $(QQ^*)_{ab} = 1$. Otherwise, since $w_n^{(i - j)}$ is a root of unity,
\begin{align*}
        (QQ^*)_{ab} &= w_n^{i - j} (QQ^*)_{ab} \implies (1 - w_n^{i - j}) (QQ^*)_{ab} = 0 \implies
        (QQ^*)_{ab} = 0
\end{align*}
And we have that $Q^* = Q^{-1}$ as desired.

\subsubsection{Exercise 3.3}
Exercise seems ambiguous - if we're just talking about $f(1)$, then we can simply add an $if$ case for it.
Otherwise, we can return both $f_min$ and the index $j$. I don't think it's possible to
reconstruct full scores from just returning the index by itself (without wasting computation).

\subsubsection{Exercise 3.4}
If we let $g(n)$ be the number of calculations for $f(n)$ after calling $f(1)$, then we have by definition
of the algorithm that $g(n) = \sum_{k = 1}^{n - 1} g(k)$ (since every call to $f(k)$ calls $f(n)$).
We see that $g(2) = g(1) = 2^0$, and we assume that $g(n) = 2^{n - 2}$. Then we have that
\begin{align*}
        g(n + 1) = \sum_{k = 1}^{n} g(k) = g(n) + \sum_{k = 1}^{n - 1} g(k) = 2g(n) = 2^{n - 1}
\end{align*}
So we are done by induction.

\subsubsection{Exercise 3.5}
Suppose we pick a subset of $j$ characters from both $s$ and $t$. Then there is a unique alignment that
corresponds to assigning the subsets to one another (in the order of characters) and then deleting/inserting
everything else that's not aligned. This also covers all possible alignments, so we have that the number of
alignments is $\binom{2n}{n}$.

\subsubsection{Exercise 3.6}
There are $n$ choices for where to cut $s$ to make $s'$ and $n$ choices for where to cut $t$ to make $t'$,
hence $O(n^2)$.

\subsubsection{Exercise 3.7}
Calculating optimal edit distance finds an optimal alignment in the process; we only need to also return
with $d(s, t)$ the choice of operation that was used and then compose these operations across subproblems.

The edit distance problem is available to solve on
\href{https://leetcode.com/problems/edit-distance/}{LeetCode}.
My solution is as follows
\begin{lstlisting}[language=python]
    def minDistance(self, word1, word2):
        N, M = len(word1), len(word2)
        DP = [[0] * (M+1) for i in range(N+1)]
        for j in range(M):
            DP[N][j] = M - j
        for i in range(N):
            DP[i][M] = N - i
        for i in range(N-1, -1, -1):
            for j in range(M-1, -1, -1):
                not_eq = 1
                if word1[i] == word2[j]:
                    not_eq = 0
                DP[i][j] = min(DP[i][j+1] + 1,
                               DP[i+1][j] + 1,
                               DP[i+1][j+1] + not_eq)

        return DP[0][0]
\end{lstlisting}

\subsubsection{Exercise 3.8}
If there is a path $s \to t$, then there must be a product of the form $A_si A_ij ... A_kt$ that is non-zero
and contains a maximum of $n-1$ terms. Any such product can be extended to $n-1$ terms if we introduce
self-loops (since $A_ii = 1$), so we have that $(1 + A)_{st}^{n-1}$ is non-zero. The reverse direction can
be shown similarly.

\subsubsection{Exercise 3.9}
As hinted, we can maintain an array $V[i][j]$ that tracks which middle vertex $k$ was used to get the minimum
$B_{ij}(\log_2 n)$. Then, we can reconstruct the optimal path backwards by looking at
$V[i][j] = k_1, V[i][k_1] = k_2, ...$ until we get to $i$.

\subsubsection{Exercise 3.10}
If there is a cycle whose total length is negative, then there is no fixed point (since we can repeatedly go
through that cycle to decrease distance). Otherwise, there is no issue, since there is no way to reduce
distance by visiting a vertex more than once.

\subsubsection{Exercise 3.11}
By definition, $B_{ij}(m)$ must be an upper bound on the length of the shortest path from $i$ to $j$, as
otherwise we would be positing that there exists $k$ such that the shortest path from $i$ to $k$ to $j$ is
shorter than the shortest path from  $i$ to  $j$. The term $B_{ij}(m)$ is the composition of paths
$B{ik}(m-1)$ and  $B_{kj}(m-1)$, so the number of steps in $B_{ij}(m)$ is at most twice the maximum of
the number of steps in $B_{ik}(m-1)$ (over all $k$). From this it is clear that the number of steps after
$m$ outermost iterations is at most $2^m$ (since $m = 0$ corresponds to a single step). If there are no
negative cycles, then the shortest path between two vertices will take at most $n-1$ steps, so we only need
to iterate up to $m = \log_2 m$.

\subsubsection{Exercise 3.12}
Suppose we complete the for loop and there are two vertices that are not connected. Since the for loop
considers every edge, there must be no path between these two vertices. However, this contradicts the
assumption of the graph being connected, so the final result of the for loop must be a spanning tree.

\subsubsection{Exercise 3.13}
For $n = 2$, it is clear that a forest with $n-1$ edges must be a spanning tree. Now we assume the
same is true for $n$ and consider a forest with $n + 1$ vertices. By the inductive assumption, any sub-forest
consisting of $n - 1$ edges must be a spanning tree for the $n$ vertices it connects. Since these $n$ vertices
are already connected, adding an edge between any of them would introduce a cycle. Thus, the only way to grow
this forest of $n$ vertices to a forest of $n + 1$ vertices is to add an edge to vertex $n + 1$, so a forest
with  $n$ edges must be a spanning tree for its $n + 1$ vertices. This proves one direction; the other
direction can be proved similarly.

\subsubsection{Exercise 3.14}
Just add the bolded edges in Figure 3.16 in order of weight.

\subsubsection{Exercise 3.15}
The proof idea is the same as that of Lemma 3.1, except now replacing an edge $e$ with another edge $e'$ cannot
preserve a minimum spanning tree, since $e \neq e'$.

\subsubsection{Exercise 3.16}
Run Kruskal's but negate the edges in the graph. This has to be the maximum, since we already proved Kruskal's
to be optimal.

\subsubsection{Exercise 3.17}
The first axiom is vacuously true for linear independence. For the second axiom, if $Y$ were not linearly
independent then we could extend the nontrivial linear relation sending $Y$ to 0 to $X$, so the second axiom
must also be true. The third axiom holds similarly.

\subsubsection{Exercise 3.18}
Suppose we obtain a maximum flow using Ford-Fulkerson. At each stage of Ford-Fulkerson, we increase the current
flow $f$ by the minimum $c_f(e)$ in an augmenting path. Since the capacities are all integers, this means at
each stage we increase $f$ by an integral amount, so there is a max flow obtained via Ford-Fulkerson that is
an integer.

\subsubsection{Exercise 3.19}
No, the maximal flow need not be unique; consider a graph with only a single edge from the source to a fork
consisting of edges with equal capacity and connecting back to a single node (looks like a kite). The
difference between two max flows must be 0, since there must be some edge that they both share (otherwise we
could just combine both two get a larger flow).

\subsubsection{Exercise 3.20}
The min cut is not unique; we can remove the two edges from the source or we could remove the two edges to
the target. Additionally, we can also remove the lower edge from the source and the upper edge to the target.

\subsubsection{Exercise 3.21}
If there is a flow of value $m$, it must necessarily pass through $m$ of the original edges in the bipartite
graph $G$, since they were all assigned capacity 1. A matching consisting of $m$ edges in $G$ corresponds to
$m$ unique left nodes (connected to $s$) and  $m$ unique right nodes (connected to $t $), so we can send a
flow of $m$ through this matching.

\subsection{Problems}

\subsubsection{Problem 3.1}
We see that $n = 1$ requires only the single move of moving the one disk directly to the desired peg, and that
$n = 2$ cannot be done in fewer than the 3 moves indicated by the recursive algorithm. Suppose the recursive
algorithm is optimal for $n$. To move $n + 1$ disks, we must move disk $n + 1$ to the bottom of the desired peg.
Thus, we first move $n$ disks to the non-desired peg, which
requires $2^n - 1$ moves. Now there is no ``better'' move than moving disk $n + 1$ to the desired peg, since
it will be put in its final spot. After making this move, we again have to move the $n$ disks from the other
peg to the desired peg, which takes another $2^n - 1$ moves, for a total of $2(2^n - 1) + 1 = 2^{n+1} - 1$
moves, as desired.

\subsubsection{Problem 3.2}
Figure 3.24 is very helpful. The recursive solution for Towers of Hanoi can be translated to finding a
Hamiltonian path on an $n$-dimensionl cube as follows
\begin{enumerate}
        \item Find a Hamiltonian path on one face of the cube, which is itself an $n-1$-dimensional hypercube.
        \item Now move from this face to the opposite face; this requires moving once along a single edge
                connecting the two faces (this is how we create a hypercube in the first place).
        \item Now we can find a Hamiltonian path on the opposite face, and we are done.
\end{enumerate}
The vertices of the cube can be identified with different Hanoi states, and the edges can be identified with
moves.


 \documentclass{article}
\usepackage[margin=1in]{geometry}
\setlength{\parindent}{0in}

\usepackage[utf8]{inputenc}

\usepackage{latexsym,amsfonts,amssymb,amsthm,amsmath}
\usepackage {tikz}
\usetikzlibrary {positioning}
\usetikzlibrary{quantikz}

\usepackage{braket}

\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\title{Quantum Computing - Assignment 2}
\author{Kishlaya Jaiswal}

\begin{document}

\maketitle

\subsection*{Exercise 1}
\begin{proof} .

\begin{center}
\begin{quantikz}
\lstick{$\ket{0}^{\otimes n}$} & \gate{H^{\otimes n}} & \qw & \gate[wires=2]{U_f} & \qw & \gate{H^{\otimes n}} & \qw & \meter{} & \qw \\
\lstick{$\ket{1}$} & \gate{H} & \qw & & \qw \\
\end{quantikz}
\end{center}

where $f: \mathbb{Z}_2^n \rightarrow \mathbb{Z}_2$, $f(x) = \sum a_ix_i$ \\

First we prepare the state:
$$\ket{0}^{\otimes n} \otimes \ket{1}$$

Applying $n+1$ Hadamard gates, we get:
$$\left( \frac{1}{2^{n/2}} \sum_x \ket x \right) \otimes \left(\frac{\ket0 - \ket1}{\sqrt2}\right) = \frac{1}{2^{n/2}} \frac{1}{\sqrt2} \sum_x \ket x \ket0 - \ket x \ket1$$

Applying $U_f$, we get:
$$\frac{1}{2^{n/2}} \frac{1}{\sqrt2} \sum_x \ket x \ket{f(x)} - \ket x \ket{\overline{f(x)}} = \left( \frac{1}{2^{n/2}} \sum_x (-1)^{f(x)} \ket x \right) \otimes \left(\frac{\ket0 - \ket1}{\sqrt2}\right)$$

Let us carefully examine the first $n$ qubits at this point.

First, say $x = x_1\ldots x_n$, then
$$(-1)^{f(x)} \ket x = (-1)^{a_1x_1 + \cdots a_nx_n} \ket{x_1 \ldots x_n} = \bigg((-1)^{a_1x_1} \ket{x_1}\bigg) \otimes \cdots \otimes \bigg((-1)^{a_nx_n} \ket{x_n}\bigg)$$

Thus, the first $n$ qubits are:
\begin{align*}
    \frac{1}{2^{n/2}} \sum_x (-1)^{f(x)} \ket x &= \bigg(\sum_{x_1} \frac{(-1)^{a_1x_1} \ket{x_1}}{\sqrt2}\bigg) \otimes \cdots \otimes \bigg(\sum_{x_n} \frac{(-1)^{a_nx_n} \ket{x_n}}{\sqrt2}\bigg) \\
    &= \bigg(\frac{\ket0 + (-1)^{a_1}\ket1}{\sqrt2} \bigg) \otimes \cdots \otimes \bigg(\frac{\ket0 + (-1)^{a_n}\ket1}{\sqrt2} \bigg) \\
\end{align*}

Now we recall that $$H\bigg(\frac{\ket0 + (-1)^x\ket1}{\sqrt2}\bigg) = \ket x$$

Thus applying $n$ Hadamard gates to the first $n$ qubits, we get:
$$\ket{a_1 \ldots a_n}$$
\end{proof}

\subsection*{Exercise 2}
\begin{proof}
First we prove by induction on $n$ that for $x \in \{0,1\}^n$, $$H^{\otimes n}\ket x = \frac{1}{2^{n/2}} \sum_{z \in \{0,1\}^n} (-1)^{x.z} \ket z $$

For $n=1$, it follows immediately as
$$H\ket x = \frac{1}{\sqrt2} \sum_z (-1)^{x.z} \ket z = \frac{\ket0 + (-1)^x \ket1}{\sqrt2}$$

Suppose it is true for some $n \geq 1$, then for any $x' \in \{0,1\}^{n+1}$, write $x' = xx_{n+1}$ where $x \in \{0,1\}^n$, then

\begin{align*}
    H^{\otimes n+1}\ket{x'} &= H^{\otimes n+1}(\ket{x} \otimes \ket{x_{n+1}}) \\
    &= H^{\otimes n}\ket{x} \otimes H\ket{x_{n+1}} \\
    &= \frac{1}{2^{n/2}} \sum_{z \in \{0,1\}^n} (-1)^{x.z} \ket z \otimes \left(\frac{\ket0 + (-1)^{x_{n+1}}\ket1}{\sqrt2}\right) \\
    &= \frac{1}{2^{n+1/2}} \sum_{z \in \{0,1\}^n} (-1)^{x.z + + x_{n+1}.0} \ket z \otimes \ket0 + (-1)^{x.z + x_{n+1}.1} \ket z \otimes \ket1 \\
    &= \frac{1}{2^{n+1/2}} \sum_{z' \in \{0,1\}^{n+1}} (-1)^{x'.z'} \ket{z'}
\end{align*}

Now, we consider
\begin{align*}
    H\left(\frac{\ket x + \ket y}{\sqrt2}\right) &= H\left(\frac{\ket x + \ket{s \oplus x}}{\sqrt2}\right) \\
    &= \frac{1}{\sqrt2} H\ket{x} + \frac{1}{\sqrt2} H\ket{s \oplus x} \\
    &= \frac{1}{\sqrt2} \frac{1}{2^{n/2}} \sum_z (-1)^{x.z} \ket z +  \frac{1}{\sqrt2} \frac{1}{2^{n/2}} \sum_z (-1)^{x.z + s.z} \ket z \\
    &= \frac{1}{2^{n+1/2}} \sum_z (-1)^{x.z} \big(1 + (-1)^{s.z}\big) \ket z \\
    &= \frac{1}{2^{n-1/2}} \sum_{s.z = 0} (-1)^{x.z} \ket z \\
    &= \frac{1}{2^{n-1/2}} \sum_{z \perp s} (-1)^{x.z} \ket z
\end{align*}

\end{proof}

\subsection*{Exercise 3}
\begin{proof}
$\ket S = \sum_{s \in S} \frac{1}{2^{m/2}} \ket s$

\begin{align*}
    H\ket S &=  \sum_{s \in S} \frac{1}{2^{m/2}} H\ket s \\
    &=  \sum_{s \in S} \frac{1}{2^{m/2}} \frac{1}{2^{n/2}} \sum_{w} (-1)^{s.w} \ket w \\
    &=  \sum_{w} \frac{1}{2^{(n+m)/2}} \left(\sum_{s \in S} (-1)^{s.w} \right) \ket w \\
\end{align*}

\textbf{Claim}: $w \in S^{\perp} \implies \sum_{s \in S} (-1)^{s.w} = 2^m$

Because if $w \in S^{\perp} \implies s.w = 0, \forall s \in S \implies \sum_{s \in S} (-1)^{s.w} = |S| = 2^m$ \\

\textbf{Claim}: $w \not \in S^{\perp} \implies \sum_{s \in S} (-1)^{s.w} = 0$

Fix a basis $\{s_1, s_2, \ldots, s_m\}$ for $S$. Since $w \not \in S^{\perp}$, there exists $i$ such that $s_i.w = 1$. Now for any $s \in S$, $s = c_1s_1 + \cdots + c_ms_m$, where $c = (c_1, c_2, \ldots) \in \mathbb{Z}_2^m$. Thus,

\begin{align*}
    \sum_{s \in S} (-1)^{s.w} &= \sum_{c \in \mathbb{Z}_2^m} (-1)^{c_1 s_1.w}(-1)^{c_2 s_2.w} \ldots (-1)^{c_m s_m.w} \\
    &= (1 + (-1)^{s_1.w})(1 + (-1)^{s_2.w}) \ldots (1 + (-1)^{s_i.w}) \ldots (1 + (-1)^{s_m.w}) \\
    &= (1 + (-1)^{s_1.w})(1 + (-1)^{s_2.w}) \ldots (1 + (-1)) \ldots (1 + (-1)^{s_m.w}) \\
    &= 0
\end{align*}

Therefore,
$$H\ket S = \frac{1}{2^{(n-m)/2}} \sum_{w \in S^{\perp}} \ket w$$

Furthermore, for any $y \in \mathbb{Z}_2^n$

\begin{align*}
    H\ket{y+S} &=  \sum_{s \in S} \frac{1}{2^{m/2}} H\ket{y+s} \\
    &=  \sum_{s \in S} \frac{1}{2^{(n+m)/2}} \sum_{w} (-1)^{(y+s).w} \ket w \\
    &=  \frac{1}{2^{(n+m)/2}} \sum_{w} (-1)^{y.w} \left(\sum_{s \in S} (-1)^{s.w} \right) \ket w \\
    &= \frac{1}{2^{(n-m)/2}} \sum_{w \in S^{\perp}} (-1)^{y.w}\ket w
\end{align*}

\end{proof}

\subsection*{Exercise 4}

\begin{proof}

Suppose $X_j = i$ is known. So $V_i = \langle w_1, \ldots w_i \rangle$ has dimension $j$.

\begin{align*}
    P[X_{j+1} = i+1] = P[w_{i+1} \not \in V_i] = 1 - P[w_{i+1} \in V_i] = 1 - \frac{2^j}{2^m}
\end{align*}

\begin{align*}
    P[X_{j+1} = i+2] &= P[w_{i+2} \not \in V_i, w_{i+1} \in V_i] \\
    &= P[w_{i+2} \not \in V_i] P[w_{i+1} \in V_i] \\
    &= \frac{2^j}{2^m} \left(1 - \frac{2^j}{2^m}\right)
\end{align*}

Similarly,

\begin{align*}
    P[X_{j+1} = i+k] &= P[w_{i+k} \not \in V_i, w_{i+k-1} \in V_i, \ldots w_{i+1} \in V_i] \\
    &= P[w_{i+k} \not \in V_i] P[w_{i+k-1} \in V_i] \ldots P[w_{i+1} \in V_i] \\
    &= \left(1 - \frac{2^j}{2^m}\right)\left(\frac{2^j}{2^m}\right)^{k-1}
\end{align*}

Let $p_j = 2^{j}/2^m$, then

\begin{align*}
    E[X_{j+1} \mid X_{j} = i] &= \sum_{k \geq 1} (i+k) P[X_{j+1} = i+k] \\
    &= \sum_{k \geq 1} (i+k) \left(1 - p_j\right) p_j^{k-1} \\
    &= i \left(1 - p_j\right) \sum_{k \geq 1} p_j^{k-1} + (1 - p_j) \sum_{k \geq 1} k p_j^{k-1} \\
    &= i + \frac{1}{1-p_j} \\
\end{align*}

As $E[X_{j+1} \mid X_j] = X_j + \frac{1}{1-p_j}$ and $E[X_{j+1}] = E[E[X_{j+1} \mid X_j]]$, we get $E[X_{j+1}] = E[X_j] + \frac{1}{1-p_j}$

Similarly, we can calculate $E[X_1] = \frac{1}{1-p_0}$. Thus,

\begin{align*}
    E[X_m] &= E[X_{m-1}] + \frac{1}{1-p_{m-1}} \\
    &= \sum_{j=0}^{m-1} \frac{1}{1-p_j} \\
    &= \sum_{j=0}^{m-1} \frac{2^m}{2^m - 2^j} \\
    &= \sum_{j=0}^{m-1} 1 + \frac{2^j}{2^m - 2^j} \\
    &= m + \left(\sum_{j=1}^{m} \frac{1}{2^j - 1 }\right) \\
    &< m + \left(\sum_{j=1}^{m} \frac{1}{2^{j-1} }\right) & (\text{as } 1 \leq 2^{j-1} \forall j \geq 1) \\
    &< m + \left(\sum_{j=0}^{\infty} \frac{1}{2^{j} }\right) \\
    &= m + 2
\end{align*}

Thus, $E[X_m] < m+2$
\end{proof}

\end{document}


\label{chapter:basic_latex}

% Paragraph descriping what the chapter is about
\blindtext

\section{A section with a definition and a theorem}
\label{sec:a_section_with_a_definition_and_a_theorem}

The above is a section. Here's a definition.

\begin{definition}[Conditional probability]
	\label{def:conditional_probability}
	The conditional probability of $A$ given $B$ is defined as
	\begin{equation*}
		P(A | B) = \frac{P(A \cap B)}{P(B)},
	\end{equation*}
	where $A \cap B$ denotes the intersection of $A$ and $B$.
\end{definition}

\begin{theorem}[Bayes theorem]
	Given $P(A | B)$, $P(A)$ and $P(B)$, we can compute $P(B | A)$ using
	\begin{equation}
	\label{eqn:bayes_theorem}
	P(B | A) = \frac{P(A | B) P(B)}{P(A)}.
	\end{equation}
\end{theorem}
\begin{proof}
	Write $P(A \cap B)$ in two ways using the Definition \ref{def:conditional_probability} of conditional probability as follows.
	\begin{equation*}
		P(A \cap B) = P(A | B) P(B) \qquad P(B \cap A) = P(B | A) P(A)
	\end{equation*}
	The intersection is symmetric, meaning that $B \cap A = B \cap A$.
	Thus we can compare terms and write $P(A | B) P(B) = P(B | A) P(A)$, rearranging this gives Bayes theorem.
\end{proof}

Bayes theorem has many applications, such as the \emph{Naive Bayes Classifier}, which is a machine learning algorithm.
The classifier assigns a label to a piece of data, e.g. classifying an email as spam or not.
It's called ``naive'' since it assumes conditional independence.
Equation \eqref{eqn:bayes_theorem} has extensions when more variables are used.

\section{A section with an example}
In Section \ref{sec:a_section_with_a_definition_and_a_theorem} we gave a theorem, here's an example with a real world application.

\begin{example}[An example with numbers]
	Here's a little example with some numbers.
	\begin{align*}
		P(B | A) &= \frac{P(A | B) P(B)}{P(A)} \\
			 	 &= \frac{(0.3) (0.4)}{0.24} = \frac{0.12}{0.24} = \frac{1}{2}
	\end{align*}
	As you see, applications are everywhere.
\end{example}

\blindtext

\section{A section with a figure}
\blindtext
	\begin{figure}[ht!]
	\centering
	\includegraphics[width=0.3\linewidth]{figs/truncatedoctahedron}
	\caption{A pretty figure.}
	\label{fig:truncatedoctahedron}
	\end{figure}
\blindtext


% !TEX root = index.tex

\section*{Solutions to selected problems - 2}

\begin{proof}[Solution to Q.33 Part 1]
  For linear transformations $\call: V \rightarrow W$ and $\call': W \rightarrow U$,
  \begin{enumerate}
    \item \begin{align*}
      \call' \circ \call(c \vec{v})
      &= \call' ( \call(c \vec{v})) \\
      &= \call' ( c\call(\vec{v})) && \mbox{ as $\call$ is a linear transformation}\\
      &= c \call' ( \call(\vec{v})) && \mbox{ as $\call'$ is a linear transformation}
    \end{align*}
    \item \begin{align*}
      \call' \circ \call(\vec{v} + \vec{w})
      &= \call' ( \call(\vec{v} + \vec{w})) \\
      &= \call' ( \call(\vec{v}) + \call(\vec{w})) && \mbox{ as $\call$ is a linear transformation}\\
      &= \call' ( \call(\vec{v})) + \call' ( \call(\vec{w})) && \mbox{ as $\call'$ is a linear transformation}
    \end{align*}
  \end{enumerate}
  And so $\call' \circ \call$ is a linear transformation.
\end{proof}

\begin{proof}[Solution to Q.35]
  $\call: \bbr^2 \rightarrow \bbr^2$ sending $\begin{bmatrix} x \\y \end{bmatrix} \mapsto \begin{bmatrix} x + 1 \\ y \end{bmatrix}$ is not a linear operator, as it preserves neither scalar multiplication nor vector addition.

  $\call(\begin{bmatrix} x \\ y \end{bmatrix}) + \call(\begin{bmatrix} x' \\ y' \end{bmatrix}) = \begin{bmatrix} x+1 \\ y \end{bmatrix} + \begin{bmatrix} x'+1 \\ y' \end{bmatrix} = \begin{bmatrix} x+x' + 2 \\ y + y' \end{bmatrix}$

  $\call(\begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} x' \\ y' \end{bmatrix}) = \begin{bmatrix} x+x' \\ y + y' \end{bmatrix} = \begin{bmatrix} x+x' + 1 \\ y + y' \end{bmatrix}$

  so $\call(\begin{bmatrix} x \\ y \end{bmatrix}) + \call(\begin{bmatrix} x' \\ y' \end{bmatrix}) \neq \call(\begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} x' \\ y' \end{bmatrix})$.
\end{proof}

\begin{proof}[Solution to Q.36]
  A linear transformation $\call: \bbr^1 \rightarrow \bbr^1$ must preserve scalar multiplication.
  Hence,
  \begin{align}
    \label{eq:tag1}
    \tag{*}
    \call([c]) = \call(c[1]) = c \call([1])
  \end{align}
  The addition condition gives us
  \begin{align*}
    \call([c + d]) = \call([c]) + \call([d]) \\
    \implies (c+d) \call([1]) = c \call([1]) + d \call([1]) && \mbox{ by \eqref{eq:tag1}}
  \end{align*}
  But this is always true. So the addition condition does not provide us any new information about $\call$.

  There are no other conditions. Hence a linear transformation $\call: \bbr^1 \rightarrow \bbr^1$ is completely determined by $\call([1])$, which can be any real number, say $[\alpha]$.
  Then $\call([c])$ equals $ [c\alpha]$, i.e. $\call$ is a scaling by $\alpha$.
\end{proof}

\begin{proof}[Solution to Q.38 Part 2]
  In order to determine the matrix corresponding to $\rot$ we need to determine $\rot(\vec{e}_1)$ and $\rot(\vec{e}_2)$.
  By basic trigonometry,
  \begin{align*}
    \rot(\begin{bmatrix} 1 \\ 0 \end{bmatrix}) &= \begin{bmatrix} \cos \theta \\ \sin \theta \end{bmatrix}\\
    \rot(\begin{bmatrix} 0 \\ 1 \end{bmatrix}) &= \begin{bmatrix} -\sin \theta \\ \cos \theta \end{bmatrix}
  \end{align*}
  Hence, the corresponding matrix is
  \begin{align*}
    [\rot] = \begin{bmatrix} \cos \theta & - \sin \theta \\ \sin \theta & \cos \theta \end{bmatrix}
  \end{align*}
\end{proof}

\begin{proof}[Solution to Q.39 Part 2]
  Using the formula for matrix multiplication
  \begin{align*}
    [\rot] \begin{bmatrix} x \\ y \end{bmatrix}
    &= \begin{bmatrix} \cos \theta & - \sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}\\
    &= x \begin{bmatrix} \cos \theta \\ \sin \theta \end{bmatrix} + y \begin{bmatrix} -\sin \theta \\ \cos \theta \end{bmatrix} \\
    &= \begin{bmatrix} x \cos \theta - y \sin \theta \\ x \sin \theta + y \cos \theta \end{bmatrix}
  \end{align*}
\end{proof}