\chapter{ELECTROMAGNETIC INDUCTION}
\section{Magnetic Flux}
Magnetic flux is a measurement of total magnetic field which passes through a given area. It is the product of the average magnetic field times the perpendicular area that it penetrates.Consider a uniform magnetic field passing through a surface S, as shown in Figure .\ref{magnetic flux}.\\
\begin{minipage}{0.75\textwidth}
	\begin{align}
	\intertext{Let the area vector be ${\vec{A}}=A \hat{{n}}$, where $A$ is the area of the surface and $\hat{{n}}$ its unit normal. The magnetic flux through the surface is given by,}
	\Phi_{B}&=\vec{{B}} \cdot \vec{{A}}=B A \cos \theta
	\intertext{Where $\theta$ is the angle between $\vec{{B}}$ and $\hat{{n}}$. If the field is non-uniform, $\Phi_{B}$ then becomes,}
	\Phi_{B}&=\iint_{S} \overrightarrow{{B}} \cdot d \vec{{A}}
	\intertext{The SI unit of magnetic flux is the weber $({Wb})$ :}\notag
	1 \mathrm{~Wb}&=1 \mathrm{~T} \cdot \mathrm{m}^{2}
	\end{align}
\end{minipage}
\begin{minipage}{0.25\textwidth}
	\begin{figure}[H]
		\centering
		\includegraphics[height=4cm,width=5cm]{magneticflux}
		\caption{Magnetic flux.}
		\label{magnetic flux}
	\end{figure}
\end{minipage}
\section{Electromotive Force }
\section{Motional emf}
\par When we consider static electric field $E$, the potential difference between any two point $a$ and $b$ is given
\begin{minipage}{0.5\textwidth}
$$v=-\int_{a}^{b} E\cdot dl$$
\end{minipage}
	\begin{minipage}{0.50\textwidth}
		\includegraphics[width=0.45\textwidth]{diagram-20210427(3)-crop}
	\end{minipage}
And work done by the electric field along a closed path is zero.
\begin{align*}
i.e., \quad \oint E\cdot dl&=0\\
\rightarrow \quad \nabla \times E&=0
\end{align*}
\begin{minipage}{.45\textwidth}
	\begin{center}
		\includegraphics[width=0.7\textwidth]{flux 2}
	\end{center}
\end{minipage}\\
\\Suppose the magnetic field $B$ in the shaded region is pointing in to the page, we are moving a wire having a resistor connected at the end through the field towards right with a speed $v$, the charges in the segment $a$\ $b$ experience a magnetic force (Lorentz force) whose vertical component $qvB$ drives a current around the loop in the clockwise direction, so the e.m.f generated,
\begin{align*}
\varepsilon&=\oint F_{mag} \cdot dl
\intertext{Here $F_{mag}$ is the magnetic Lorentz force per unit charge. We know that,}
 {F_{Lorentz}}&=q(v\times B)
 \intertext{Since $q=1\ $\ unit and,\ $ v $ \ and $ B $\  are $ \perp^{lr} $}
F_{mag}&=vB\\
\varepsilon&=\oint F_{mag}\cdot dl\\
\varepsilon&=\oint vB\cdot dl\\
\varepsilon&= vBh \hspace{2cm}\text{$\oint  dl=h$ $ \rightarrow $ width of the loop}
\intertext{If $\phi$ is the flux of $B$ through the shaded region }
\phi=&\int B \cdot da\quad\rightarrow da=hx\\
=&Bhx
\intertext{When the  loop moves, flux decreases.}
\frac{d\phi}{dt}&=Bh\frac{dx}{dt}=-Bhv \hspace{2cm}\rightarrow\frac{dx}{dt}=-v\\
\frac{d\phi}{dt}&=-Bhv
\intertext{Both equation for emf are same.So precisely the emf generated in the loop is the negative rate of change of flux through the loop. }
\varepsilon&=\frac{-d \phi}{dt}
\intertext{This is called \textbf{Flux rule} for motional emf.}
\end{align*}
\subsection{Fleming's Right Hand Rule}
The Fleming's right hand rule is used to determine the direction of induced current. Accorrding to this rule, stretch the thumb, forefinger and centre finger of right hand in mutually perpendicular directions such that the forefinger points in the direction of magnetic field and thumb is along the direction of motion of the conductor, the centre finger will point in the direction of the induced current.
\begin{exercise}
	A metal stock of length, $2 m/s$ moves with speed $2 m/s$ in a direction making $30^{\circ}$ with its length. If a magnetic field of $0.2$ Tesla exists in the region perpendicular to the rod then p.d developed between it's ends is........ volts.
\end{exercise}
\begin{answer}
	The potential difference developed between the ends of the rods is,\\
	\begin{minipage}{0.60\textwidth}\hfill
		\begin{align*}
		\varepsilon&=B l v \sin \theta\\
		&=0.2 \times 2 \times 2 \times \sin 30 \\
		&=0 .4 \text{ volt}\\
		\end{align*}
	\end{minipage}
	\begin{minipage}{0.40\textwidth}\hfill
		\begin{align*}
		\theta&=30^{\circ}\\
		l&=2 m\\
		v&=2 m/s\\
		B&=0.2 \text{ Tesla}
		\end{align*}
	\end{minipage}
\end{answer}
\subsection{Faraday's law}
The electric fields and magnetic fields considered , produced by stationary charges and moving charges (currents), respectively. Imposing an electric field on a conductor gives rise to a current which in turn generates a magnetic field.  In 1831, Michael Faraday discovered that, by varying magnetic field with time, an electric field could be generated. The phenomenon is known as electromagnetic induction.
\begin{figure}[H]
	\begin{minipage}{0.30\textwidth}
	\centering
	\includegraphics[height=3.2cm,width=5cm]{faraday1}
	\end{minipage}
\begin{minipage}{0.30\textwidth}
	\centering
	\includegraphics[height=3.2cm,width=5cm]{faraday2}
\end{minipage}
\begin{minipage}{0.30\textwidth}
	\centering
	\includegraphics[height=3.7cm,width=5cm]{faraday3}
\end{minipage}
\caption{Faraday's Experiment.}
\end{figure}
Faraday's experiment demonstrates that an electric current is induced in the loop by changing the magnetic field. The coil behaves as if it were connected to an emf source. It is found that the induced emf depends on the rate of change of magnetic flux through the coil.\\
An emf is generated in loop if,
\begin{enumerate}
\item
If you pull a loop of wire through a magnetic field.\\
In the first case emf is induced due to change in flux exposed by flux rule.
\begin{align*}
\varepsilon&=\frac{-d\phi}{dt}
\end{align*}
Here the emf is magnetic that is emf is induced by Lorentz force.
\item
If you move the magnet near a loop holding the loop still.\\
There will be no motion of charges since the loop is at rest so the force can't be magnetic. Here the reason for emf is the electric field which is induced by the changing magnetic field.
\begin{align*}
\varepsilon=\oint E\cdot dl&=\frac{-d\phi}{dt}\\
\oint E\cdot dl&=-\oint \frac{dB}{dt}\cdot da
\intertext{By applying Stoke's theorem}
\nabla\times E&=\frac{-dB}{dt}\\
\end{align*}
\item
Changing the magnetic field by keeping the loop and the magnet at rest.\\
Magnetic and loop are moving giving rise to an emf, $ \frac{-d\phi}{dt} $
\end{enumerate}
\begin{align*}
\intertext{Now the those cases summerise in to a single form}
\varepsilon&=\frac{-d\phi}{dt}
\end{align*}
The direction of the induced current is given by Lenz's law.
\subsection{Lenz's law }
The direction of the induced current in electromagnetic induction is given by Lenz's law.\\
\begin{definition}
The induced current produces magnetic fields which tend to oppose the change in magnetic flux that induces such currents.\\
\end{definition}
\textbf{\large Explanation:}\\
\begin{minipage}{.60\textwidth}
	\begin{center}
		\includegraphics[height=2cm,width=9cm]{04-crop}
	\end{center}
\end{minipage}\\\\\\
Suppose we have a coil which is moving towards a constant magnetic field pointing out of the page. When it reaches the field the flux in it is increasing. So there should be an induced current in the loop. Since the flux is increasing the induced current will flow, in such a way that the magnetic field produced by it should oppose the original field. So inside the coil the field must pointing in to the page. Therefore current must flow in clockwise direction.\\\\
\begin{minipage}{.60\textwidth}
	\begin{center}
		\includegraphics[height=2.5cm,width=8cm]{05-crop}
	\end{center}
\end{minipage}\\\\
Consider a loop which is initially in a magnetic field pointing into the page.Suppose we are moving it towards right with a velocity $V$,then the flux in it is going to decrease. So magnetic field should be produced in such a way that it will oppose the change. So the magnetic field should be produced in the same direction as the original one. So it must be pointing into the page inside the coil. Therefore the current must flow in clockwise direction.\\\\\	Lenz's Law is not a fundamental law. It is a direct consequence that comes from both Fleming's Right and Left Hand rules . Fleming's Rules are the most fundamental principles of charge/field/motion/force interaction.
\begin{note}
The Right Hand (generator) Rule shows the direction of the induced emf and the resulting(conventional) current. This is all you need to find the direction of induced current. (Just remember that it is conventional current and it is really the emf that is induced and that a current will flow if there is a conduction path)\\
The Left Hand (motor) Rule shows the direction of the force that results from the above current and, therefore the opposition force to the original movement.\\
The latter will be seen as opposition to the original
	movement, when you apply everything correctly. This is Lenz's Law.\\\\
	To apply this:
\\	Pick a point on the conductor.\\
Given the magnetic field and motion at that point, use the Right hand Rule to determine the induced current direction.\\
At that point, given the field and current, use the Left Hand Rule to determine the resulting force. It will be opposite to the original motion.
\end{note}
\subsection{ Various formulae of induced EMF}
\begin{enumerate}
	\item  When a conducting rod of length $L$ is rotated in a perpendicur field of strength $B$, the induced emf generated is
	\begin{minipage}{0.5\textwidth}
	$$
	\begin{aligned}
	&e=-\frac{B \omega L^{2}}{2}=\frac{-B(2 \pi n) L^{2}}{2} \\
	&e=-B n\left(\pi L^{2}\right)=-B n A
	\end{aligned}
	$$
	\end{minipage}
\begin{minipage}{0.5\textwidth}
	\begin{figure}[H]
	\begin{center}
		\includegraphics[width=0.25\textwidth]{emf}
	\end{center}
\end{figure}
\end{minipage}
	where $\omega$ is angular frequency and $n$ is frequency of $\operatorname{rod} A=\pi L^{2}$.
	\item When a conducting solid disc of radius $r$ is rotating with a uniform angular velocity $\omega$ in a perpendicular magnetic field $B$, the emf induced between the centre and rim of disc is
	$$
	e=\frac{-B \omega r^{2}}{2}=-B n A
	$$
	If rotation is anticlockwise, then -ve charge accumulates at the rim and +ve charge accumulates at the centre.
	\item When a conducting rod of length $l$, moves with a velocity $v$ in a uniform magnetic field $B$, the induced emf is $e=B l v \sin \theta$,
	\item When a coil moves linearly in a uniform magnetic field, but it remains wholly within the field, $d \phi=0$. $\therefore e=0 .$ However, when the coil enters the field partially or leaves the field partially, flux linked with the coil changes and induced emf $e=B l v$ develops.
\end{enumerate}
\begin{exercise}
	A conducting rod of length $l$ is rotated with uniform angular velocity $\omega$ about one end in uniform magnetic field $B$. Electric field induced inside the rod at a distance $x$ from fixed end is ?
\end{exercise}
\begin{answer}$\left. \right. $\\
	\begin{minipage}{0.35\textwidth}
		\begin{align*}
		\text{emf at a distance x,}\\
		\text{From center ,}\  \varepsilon&=\frac{-d \phi}{d t}=-\frac{B \cdot d A}{d t}\\
		\text{Area} &=\frac{1}{2} x^{2} \theta \\
		\frac{d A}{d t}&=\frac{1}{2} x^{2} \frac{d \theta}{d t} \hspace{2cm}\frac{d \theta}{d t}=\omega\\
		&=\frac{1}{2} x^{2}\omega\\
		\varepsilon&=\frac{-1}{2} B x^{2}\omega\\
		\text{Electric field at $x$ , }\ E&=\frac{-d v}{d x}\\
		&=\frac{1}{2} B\omega\frac{d x^{2}}{d x}\\
		&=B\omega x
		\end{align*}
	\end{minipage}
	\begin{minipage}{0.35\textwidth}
	\begin{figure}[H]
		\centering
		\includegraphics[height=4cm,width=4cm]{06-crop}
	\end{figure}
\end{minipage}
\end{answer}
\begin{exercise}
	A uniform magnetic field $\vec{B}(t)$, pointing straight up, fills the shaded circular region. If $B$ is changing with time, what is the induced electric field?\\
	\begin{minipage}{.45\textwidth}
		\begin{center}
			\includegraphics[width=0.4\textwidth]{diagram-20210426(2)-crop}
		\end{center}
	\end{minipage}\\
\end{exercise}
\begin{answer}
	$\vec{E}$ points in the circumferential direction, just like the magnetic field inside a long straight wire carrying a uniform current density. Draw an Amperian loop of radius $s$, and apply Faraday's law:
	\begin{align*}
	\oint \vec{E} \cdot d \vec{l}&=E(2 \pi s)\\&=-\frac{d \Phi}{d t}\\&=-\frac{d}{d t}\left(\pi s^{2} B(t)\right)\\&=-\pi s^{2} \frac{d B}{d t}
	\intertext{	Therefore}
	E(2 \pi s)&=-\pi s^{2} \frac{d B}{d t}\\
	\vec{E}&=-\frac{s}{2} \frac{d B}{d t} \hat{\boldsymbol{\phi}}
	\intertext{If $\vec{B}$ is increasing, $\vec{E}$ runs clockwise, as viewed from above.}
	\end{align*}
\end{answer}
\subsection{Mutual Inductance and Self Inductance}
\subsection{Mutual Inductance}
We have two loops $1$ and $2$ as shown in the figure.\ref{mutual inductance}. Suppose the current flowing through the loop $1$ is $I_1$, it produces a magnetic field $B_1$ which is propotional to current $I_1$ by Biot-Savart law.\\
\begin{minipage}{0.65\textwidth}
	\begin{align*}
B_1&=\frac{\mu_{0}}{4\pi}I_1\oint \frac{dl \times\hat{r}}{r^2}
\intertext{These field lines pan through second loop and flux through it is $\phi_2$}
\phi_2&=\oint B_1 \cdot da_2\\
\text{So,} \ \phi_2 &\propto  I_1\\
\phi_2&=M_{21}I_1
\end{align*}
\end{minipage}
\begin{minipage}{0.20\textwidth}
\begin{figure}[H]
	\centering
	\includegraphics[height=5cm,width=5cm]{diagram-20210426(3)-crop}
	\caption{}
	\label{mutual inductance}
\end{figure}
\end{minipage}
\begin{align*}
\intertext{$M_{21}$ is constant of proportionality, is called as mutual inductance of the two loops,}
\phi_2&=\int B_1\cdot da_2\\
\text{But,} \ B_1&=\nabla\times A_1 \hspace{0.6cm}
\text{$A_1$ is the magnetic vector potential.} \\
\therefore\phi_2&=\int\nabla \times A_1\cdot da_2\\
\text{By Stoke's theorem,}\ \phi_2&=\oint A_1\cdot dl_2
\end{align*}

\begin{align*}
\intertext{Magnetic vector potential due to small length element $dl$ can be calculated as,}
A_1&=\frac{\mu_{0} I_1}{4\pi}\oint \frac{dl_1}{r}\\
\therefore\phi_2&=\frac{\mu_{0}I_1}{4\pi}\oint\left( \oint\frac{dl}{r}\right) dl_2 \\
\intertext{Comparing  both equations for $\phi_2$  we  get,}\\
M_{21}&=\frac{\mu_{0}}{4 \pi}\oint \oint \frac{dl_1 \cdot dl_2}{r}\quad\Rrightarrow\quad\text{Neumann formula}
\end{align*}

\begin{note}\\
	\textbf{(a.)} 	Mutual Inductance is a geometrical quantity depends only on the size , shape and relative positions of the two loops. \\\\
	\textbf{(b.)} If we change the rules of loop $1$ and $2$ we will get the same equation as $M_{21}$\ for\ $M_{12}$.
\end{note}
\begin{align*}
M_{12}&=\frac{\mu_{0}}{4 \pi}\oint \oint \frac{dl_1 \cdot dl_2}{r}\\
M_{12}&=M_{21}\hspace{1cm} \text{Let's say  M} \\
\end{align*}
Now suppose if we vary the current $I$ in loop $1$ . An emf is induced in loop $2$ By Faraday's law,
\begin{align*}
\varepsilon_2&=\frac{-d\phi_2}{dt}\\
\phi_2&=\mu_{0}I_1\\
\varepsilon_2&= -M \frac{dI_1}{dt}
\end{align*}
Whenever we vary the current through the loop $1$ an $emf$ is induced in second loop without any actual contact between them.\\
\subsection{Self Inductance}
Suppose, instead of two circuits,we  have just a
single loop and we change current in that loop. This change in current changes the magnetic field
associated with the current and the flux changes.
The given loop itself will intercept the flux and the
changing flux would result in an emf in the circuit itself.

\begin{align*}
\phi & \propto I\\
\therefore \phi&= LI
\end{align*}
Where $L$ is called self inductance,and its value depends on the geometry of the loop only. The $emf$ generated when the current changes is given by\\
\begin{align*}
\varepsilon&=\frac{-d\phi}{dt}\\
\varepsilon&=\frac{-Ld\phi}{dt} \hspace{1cm}\text{Unit of $L$ is Henries(H)}\\
1 H&=1 V.S/Ampere
\end{align*}
By Lenz's law we can say that direction of induced $emf$ is opposite to the direction of original current .Therefore it is also called Back emf.
\subsubsection{Self inductance of a solenoid.}
Consider a solenoid has a length $l$, number of turns $N$ and area of cross section $A$. The  self inductance $L$ of the solenoid is given as,\\
\begin{minipage}{0.65\textwidth}
	\begin{align*}
	B&=\mu_{0}n I\hspace{1cm}n=\frac{N}{l}\\
	&=\frac{{\mu_{0}NI}}{l}\\
	\phi&=N B A=\frac{\mu_{0} N^{2} I A}{l}\\
	\phi&=L I\\
	\therefore L I&=\frac{\mu_{0} N^{2} I A}{l}\\
	\therefore L &=\frac{\mu_{0} N^{2}  A}{l}
	\end{align*}
\end{minipage}
\begin{minipage}{0.35\textwidth}
	\begin{figure}[H]
		\centering
		\includegraphics[height=3cm,width=5cm]{solenoid}
		\caption{Solenoid}
		\label{Solenoid}
	\end{figure}
\end{minipage}
\begin{exercise}
	A current $l$ is flowing in a toroidal coil of circular cross section of radius $R$ with $N$ number of turns distributed uniformly over its circumference if $A$ is the cross sectional area of the toroid, $I B$ Self inductance will be
\end{exercise}
\begin{answer}
	\begin{align*}
	B&=\frac{\mu_{0} N I}{2 \pi R}\\
	\phi&=N B A=\frac{\mu_{0} N^{2} I A}{2 \pi R}\\
	\phi&=L I\\
	L I&=\frac{\mu_{0} N^{2} I A}{2 \pi R}\\
	\therefore L&=\frac{\mu_{0} N^{2}  A}{2 \pi R}
	\end{align*}
\end{answer}
\subsection{Combination of inductors}
\begin{enumerate}
	\item When two coils are connected in series and current in both is in the same direction,
	$$
	L=L_{1}+L_{2}+2 M
	$$
	When current in the two coils flows in opposite directions.
	$$
	L=L_{1}+L_{2}-2 M
	$$
	If we take $M=0$, then
	$$
	L=L_{1}+L_{2}
	$$
	\item When two coils are connected in parallel, then
	$$
	\begin{aligned}
	&\frac{1}{L}=\frac{1}{\left(L_{1}+M\right)}+\frac{1}{\left(L_{2}+M\right)} \\
	&L=\frac{L_{1} L_{2}+M^{2}+M\left(L_{1}+L_{2}\right)}{L_{1}+L_{2}+2 M}
	\end{aligned}
	$$
	$M=0$\\
	$$L=\frac{L_{1} L_{2}}{L_{1}+L_{2}}$$
\end{enumerate}
\newpage
\begin{abox}
	Practice set-1
\end{abox}
\begin{enumerate}
	\begin{minipage}{\textwidth}
		\item A uniform magnetic field in the positive $z$-direction passes through a circular wire loop of radius $1 \mathrm{~cm}$ and resistance $1 \Omega$ lying in the $x y$-plane. The field strength is reduced from 10 tesla to 9 tesla in $1 s$. The charge transferred across any point in the wire is approximately
		\exyear{NET JUNE 2015}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}]$3.1 \times 10^{-4}$ coulomb
		\task[\textbf{B.}] $3.4 \times 10^{-4}$ coulomb
		\task[\textbf{C.}] $4.2 \times 10^{-4}$ coulomb
		\task[\textbf{D.}]$5.2 \times 10^{-4}$ coulomb
	\end{tasks}
	\begin{minipage}{\textwidth}
		\item A magnetic field $B$ is $B \hat{z}$ in the region $x>0$ and zero elsewhere. A rectangular loop, in the $x y$-plane, of sides $l$ (along the $x$-direction) and $h$ (along the $y$ - direction) is inserted into the $x>0$ region from the $x<0$ region at constant velocity $v=v \hat{x}$. Which of the following values of $l$ and $h$ will generate the largest EMF?
		\exyear{NET JUNE 2016}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $l=8, h=3$
		\task[\textbf{B.}]$l=4, h=6$
		\task[\textbf{C.}]$l=6, h=4$
		\task[\textbf{D.}]$l=12, h=2$
	\end{tasks}
\end{enumerate}

\colorlet{ocre1}{ocre!70!}
\colorlet{ocrel}{ocre!30!}
\setlength\arrayrulewidth{1pt}
\begin{table}[H]
	\centering
	\arrayrulecolor{ocre}

	\begin{tabular}{|p{1.5cm}|p{1.5cm}||p{1.5cm}|p{1.5cm}|}
		\hline
		\multicolumn{4}{|c|}{\textbf{Answer key}}\\\hline\hline
		\rowcolor{ocrel}Q.No.&Answer&Q.No.&Answer\\\hline
		1&\textbf{a}&2&\textbf{b}\\\hline
	\end{tabular}
\end{table}
\newpage
\begin{abox}
	Practice set-2
	\end{abox}
\begin{enumerate}
	\begin{minipage}{\textwidth}
		\item Consider a conducting loop of radius $a$ and total loop resistance $R$ placed in a region with a magnetic field $B$ thereby enclosing a flux $\phi_{0}$. The loop is connected to an electronic circuit as shown, the capacitor being initially uncharged\\
		\begin{figure}[H]
			\centering
			\includegraphics[height=2.8cm,width=6cm]{diagram-20210817(14)-crop}
		\end{figure}
		If the loop is pulled out of the region of the magnetic field at a constant speed $u$, the final output voltage $V_{\text {out }}$ is independent of
		\exyear{GATE 2010}
	\end{minipage}
	\begin{tasks}(4)
		\task[\textbf{A.}] $\phi_{0}$
		\task[\textbf{B.}]$u$
		\task[\textbf{C.}]$R$
		\task[\textbf{D.}] $C$
	\end{tasks}
\begin{minipage}{\textwidth}
	\item A circular loop made of a thin wire has radius $2 \mathrm{~cm}$ and resistance $2 \Omega$. It is placed perpendicular to a uniform magnetic field of magnitude $\left|\vec{B}_{0}\right|=0.01$ Tesla. At time $t=0$ the field starts decaying as $\vec{B}=\vec{B}_{0} e^{-t / t_{0}}$, where $t_{0}=1 s .$ The total charge that passes through a cross section of the wire during the decay is $Q$. The value of $Q$ in $\mu C$ (rounded off to two decimal places) is
	\exyear{GATE 2019}
\end{minipage}
\begin{minipage}{\textwidth}
	\item A circular conducting ring of radius $R$ rotates with constant angular velocity $\omega$ about its diameter placed along the $x$-axis. A uniform magnetic field $B$ is applied along the $y$-axis. If at time $t=0$ the ring is entirely in the $x y$-plane, the emf induced in the ring at time $t>0$ is
	\exyear{JEST 2012}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $B \omega^{2} \pi R^{2} t$
	\task[\textbf{B.}]$B \omega \pi R^{2} \tan (\omega t)$
	\task[\textbf{C.}]$B \omega \pi R^{2} \sin (\omega t)$
	\task[\textbf{D.}]$B \omega \pi R^{2} \cos (\omega t)$
\end{tasks}
\begin{minipage}{\textwidth}
	\item Two parallel rails of a railroad track are insulated from each other and from the ground. The distance between the rails is 1 meter. A voltmeter is electrically connected between the rails. Assume the vertical component of the earth's magnetic field to the $0.2$ gauss. What is the voltage developed between the rails when a train travels at a speed of $180 \mathrm{~km} / \mathrm{h}$ along the track? Give the answer in milli-volts.
	\exyear{JEST 2018}
\end{minipage}
\begin{minipage}{\textwidth}
	\item A circular metal loop of radius $a=1 \mathrm{~m}$ spins with a constant angular velocity $\omega=20 \pi \mathrm{rad} / \mathrm{s}$ in a magnetic field $B=3$ Tesla, as shown in the figure. The resistance of the loop is 10 ohms. Let $P$ be the power dissipated in one complete cycle.
\begin{figure}[H]
	\centering
	\includegraphics[height=4cm,width=3cm]{nimi2-crop}
\end{figure}
	 What is the value of $\frac{P}{\pi^{4}}$ in Watts?
	\exyear{JEST 2019}
\end{minipage}
\end{enumerate}
\colorlet{ocre1}{ocre!70!}
\colorlet{ocrel}{ocre!30!}
\setlength\arrayrulewidth{1pt}
\begin{table}[H]
	\centering
	\arrayrulecolor{ocre}

	\begin{tabular}{|p{1.5cm}|p{1.5cm}||p{1.5cm}|p{1.5cm}|}
		\hline
		\multicolumn{4}{|c|}{\textbf{Answer key}}\\\hline\hline
		\rowcolor{ocrel}Q.No.&Answer&Q.No.&Answer\\\hline
		1&\textbf{a}&2&\textbf{6.28}\\\hline
		3&\textbf{d}&4&\textbf{1}\\\hline
		5&\textbf{18}&&\\\hline
	\end{tabular}
\end{table}


\documentclass{article}

\usepackage{mathrsfs,amssymb,yfonts}

\begin{document}

\section{Show the relationship between Commutative Noetherian Rings (CNDs) and the class inclusions on wikipedia}

\begin{center}
\begin{tabular}{ |c|c| }
 \hline
 Class & Relationship to Noetherian Rings\\
 \hline
 Commutative Rings & $\supset$ \\
  Integral Domains & Incomparable  \\
  Integrally closed domains & Incomparable  \\
  GCD domains & Incomparable  \\
  Unique Factorization domains & Incomparable  \\
  Principle Ideal Domains & $\subset$  \\
  Euclidean Domains & $\subset$  \\
  Fields & $\subset$  \\
 \hline
\end{tabular}
\end{center}

\subsection{Let $(X_0, X_2, ..., X_{n+1})$ be a finite sequence of sets such that $X_{i+1} \subset X_i$ for all $i$ where $0 \le i \le n$ and a set $Y$ such that $X_0 \supset Y$ and $X_{n+1} \subset Y$. Then there exists integers $j, k, l \ge 0$ such that $j+k+l=n$ and the first $j$ sets of X contain Y, the next $k$ are incomparable to Y in the sense that they intersect but neither is the subset of the other, and the last $l$ are contained by Y}

Two non-identical sets X and Y are in one of four relations: subset, superset, incomparable, and disjoint. If $X_i$ is a subset or disjoint of $Y$, then $X_{i+1}$ must be a subset or disjoint respectively. If $X_i$ is a superset of $Y$, then $X_{i+1}$ could be any four options. And if $X_i$ is incomparable with $Y$, then all but superset are options.

Putting this together, we start with a superset followed by zero or more supersets, then we then transition to zero or more incomparables. Because we know we end with a subset, there can be no disjoints, since once $X_i$ is disjoint all sets after it must be disjoint.

\subsection{1 follows the setup of 1.1}

$X_0$ is Commutative Rings, $Y$ is Noetherian Rings, $n=6$, and $X_7$ is Fields. Clearly a Commutative Noetherian Ring is a commutative ring, so $X_0 \subset Y$. A field is a commutative Noetherian Ring since in an Noetherian Ring every ideal is finitely generated, and in a field the only two ideals are (0) and (1), both generated by a single element.

\subsection{In the language of 1.1, $j=0$, $k=4$, and $l=2$}

To show this, we just need to show that Integral Domains and UFDs are Incomparable, and that PIDs are subsets, since those are the transitions.

\subsection{Integral Domains and CNDs are Incomparable}

The zero ring is Noetherian, since its only ideal is (0). However it is not an Integral Domain, as it is explicitly excluded in the definition.

The ring of the integers if Noetherian since each of its ideals are generated by a single element, and it isn Integral Domain since any two non-zero elements multiplied together is non-zero.

The polynomial ring over countably infinite unknowns is an integral domain, since any two non-zero polynomials of degree $n$ and $m$ multiplied together will have degree $n+m$, and therefore not be zero. However, it is not Noetherian, since the chain of strictly inclusive ideals $(X_1), (X_1, X_2), (X_1, X_2, X_3), ... $ does not terminate.

\subsection{Unique Factorization Domains and CNDs are Incomparable}

UFDs and CNDs are not disjoint and CNDs are not a subset of UFDs for the same reason above. What remains to show is that there exists a UFD that is not Noetherian.

The ring $Z[\sqrt -5]$ is not a UFD since 6 can be written as $2 \times 3$ or $(1+ \sqrt -5)(1- \sqrt -5)$. However $Z[\sqrt -5]$ is Noetherian since its Krull dimension is 2 (how to prove this?)

\subsection{Principle Ideal Domains are CNDs}

PIDs are commutative, and each ideal is generated by a single element, hence finitely generated.

\end{document}


\documentclass{article}
\usepackage{enumerate}
\usepackage{amsmath, amsthm, amssymb}
\usepackage[margin=1in]{geometry}
\usepackage[parfill]{parskip}
\DeclareMathOperator*{\argmax}{arg\,max}

\title{Econ C103 Problem Set 3}
\author{Sahil Chinoy}

\begin{document}
\maketitle{}

\subsection*{Exercise 1}

Consider an incentive-compatible direct mechanism, and let $v(\theta) = u(a(\theta), \theta)$. Since $v(\theta)$ is uniformly continuous

\begin{equation}
v(\theta) = v(\underline{\theta}) + \int \limits_{\underline{\theta}} ^\theta v'(s) \, ds
\end{equation}

Incentive compatibility requires that agents of type $\theta$ weakly prefer to report their true type rather than any other type $\theta'$, and vice versa

\begin{gather*}
v(\theta) \geq u(a(\theta'), \theta) \\
v(\theta') \geq u(a(\theta), \theta')
\end{gather*}

This implies, for $\theta' \neq \theta$

\begin{equation*}
\frac{u(a(\theta'), \theta) - u(a(\theta'), \theta')}{\theta - \theta'} \leq \frac{v(\theta) - v(\theta')}{\theta - \theta'} \leq \frac{u(a(\theta), \theta) - u(a(\theta), \theta')}{\theta - \theta'}
\end{equation*}

Taking the limit as $\theta' \to \theta$

\begin{gather*}
u_\theta(a(\theta),\theta) \leq v'(\theta) \leq u_\theta(a(\theta),\theta) \\
v'(\theta) = u_\theta(a(\theta),\theta)
\end{gather*}

Substituting into (1)

\begin{gather}
v(\theta) = v(\underline{\theta}) + \int \limits_{\underline{\theta}} ^\theta u_\theta(a(s),s) \, ds \nonumber \\
u(a(\theta), \theta) = u(a(\underline{\theta}), \underline{\theta}) + \int \limits_{\underline{\theta}} ^\theta u_\theta(a(s),s) \, ds
\end{gather}

By the revelation principle, this characterization of incentive-compatible direct mechanisms applies to the set of outcomes of \textit{all} mechanisms.

\subsection*{Exercise 2}

\begin{enumerate}[(a)]

\item

With $v(\theta) = u((x,t),\theta) = \sqrt{x(\theta)}\theta - t(\theta)$, we have $u_\theta = \sqrt{x(\theta)}$, so from (2)

\begin{gather*}
\sqrt{x(\theta)}\theta - t(\theta) = v(\underline{\theta}) + \int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds \\
t(\theta) = \sqrt{x(\theta)}\theta - \left( \int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds + v(\underline{\theta})  \right)
\end{gather*}

\item

This structure implies that the utility of type $\theta$ derived from participating in the mechanism is

\begin{equation*}
v(\theta) = \int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds + v(\underline{\theta})
\end{equation*}

For the participation constraint to hold, we need $v(\theta) \geq 0$ for all $\theta$. We know that $x(\theta)$ is monotonically increasing in incentive compatible direct mechanisms. This implies $\sqrt{x(\theta)}$ is increasing in $\theta$, so $\int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds \geq 0$ for any $\theta > \underline{\theta}$. Thus for the participation constraint to hold, we simply require $v(\underline{\theta}) \geq 0$.

\item

The information rent for type $\theta$ is

\begin{equation*}
\int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds
\end{equation*}

This is the utility derived from the agent's private information. Because the principal does not know the agent's type, they must incentivize the agent to reveal their type by paying an amount that makes it preferable to report $\theta$ rather than any other $\theta' < \theta$, which is precisely the integral over every other type the agent \textit{might} choose to report, $\theta' \in [\underline{\theta}, \theta]$, of the marginal utility derived from the allocation if the agent were actually that type.

\item

The expected revenue is

\begin{align*}
\mathbb{E}[t(\theta)] &= \int \limits_{\underline{\theta}} ^{\bar{\theta}} f(\theta) t(\theta) \, d\theta \\
&= \int \limits_{\underline{\theta}} ^{\bar{\theta}} f(\theta) \left( \sqrt{x(\theta)}\theta -  \int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds - v(\underline{\theta})  \right) \, d\theta \\
&= \int \limits_{\underline{\theta}} ^{\bar{\theta}} f(\theta) \sqrt{x(\theta)} \, d\theta - \int \limits_{\underline{\theta}} ^{\bar{\theta}} f(\theta) \left( \int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds \, \right) d\theta - v(\underline{\theta})
\end{align*}

Integrating by parts

\begin{align*}
\int \limits_{\underline{\theta}} ^{\bar{\theta}} f(\theta) \left( \int \limits_{\underline{\theta}} ^\theta \sqrt{x(s)} \, ds \, \right) d\theta &= F(z) \int \limits_{\underline{\theta}}^z \sqrt{x(s)} \, ds \bigg\rvert_{z=\underline{\theta}}^{z=\bar{\theta}} - \int \limits_{\underline{\theta}} ^{\bar{\theta}} F(\theta) \sqrt{x(\theta)} \, d\theta \\
&= \int \limits_{\underline{\theta}} ^{\bar{\theta}} (1 - F(\theta)) \sqrt{x(\theta)} \, d\theta
\end{align*}

Substituting

\begin{align*}
\mathbb{E}[t(\theta)] &= \int \limits_{\underline{\theta}} ^{\bar{\theta}}  f(\theta) \left( \theta - \frac{1-F(\theta)}{f(\theta)} \right) \sqrt{x(\theta)} \, d\theta - v(\underline{\theta})
\end{align*}

Since $\mathbb{E}[t(\theta)]$ is decreasing in $v(\underline{\theta})$, to maximize expected revenue subject to the participation constraint $v(\underline{\theta}) \geq 0$, we set $v(\underline{\theta}) = 0$. Then

\begin{equation*}
\max \{ \mathbb{E}[t(\theta)] \} = \int \limits_{\underline{\theta}} ^{\bar{\theta}}  f(\theta) \left( \theta - \frac{1-F(\theta)}{f(\theta)} \right) \sqrt{x(\theta)} \, d\theta
\end{equation*}

\end{enumerate}

\end{document}


\section{Latent Spaces and Embeddings}
\label{sec:LatentSpacesAndEmbeddings}

% ##############################################################################
\subsection{Learning Metric Embedding}
\label{ssec:LearningMetricEmbedding}

As Hermans~\etal{}~\cite{hermans2017triplet} describe, the goal of learning metric embedding is to learn a function $\func{f_\theta}{x}: \mathbb{R}^F \to \mathbb{R}^D$ which maps semantically similar points from the data manifold in $\mathbb{R}^F$ onto metrically close points in $\mathbb{R}^D$. Analogously, $\func{f_\theta}{\cdot}$ should map semantically different points in $\mathbb{R}^F$ onto metrically distant points in $\mathbb{R}^D$.

Suppose the use of this transformation for vehicle \gls{reid}. The corresponding embedding vector would be produced by a learned function that would map the images of vehicles into a latent space where images of the same vehicle would be mapped closer together. Moreover, such mapping should be invariant to variations in lighting conditions, vehicle rotations, and many others. Among other things, embedding trained this way can be used to produce a feature vector for classification, one-shot learning tasks~\cite{koch2015siameseoneshot}, clustering~\cite{schroff2015facenet}, face recognition~\cite{parkhi2015deepface} and last, but not least, object \gls{reid}~\cite{kuma2019vehiclereid}.

% ##############################################################################
\subsection{Embedding Vector Similarity}
\label{ssec:EmbeddingVectorSimilarity}

The two most common approaches to evaluating the degree of similarity between embedding vectors are Euclidean distance and cosine similarity. Let $\vect{u}$ and $\vect{v}$ be arbitrary $D$-dimensional vectors representing our embedding vectors. The Euclidean distance between the vector $\vect{u}$ and $\vect{v}$ is defined as
\begin{equation}
    \label{eq:EuclideanDistance}
    \euclnorm{\vect{u} - \vect{v}} =
    \sqrt{
        \sum_{i = 0}^{D} \rbrackets{\vect{u}_i - \vect{v}_i}^2
    },
\end{equation}
and the cosine similarity is defined as
\begin{equation}
    \label{eq:CosineSimilarity}
    \func{\cos \angle}{\vect{u}, \vect{v}} =
    \func{\cos}{\theta} =
    \frac{
        \vect{u} \cdot \vect{v}
    }{
        \euclnorm{\vect{u}} \euclnorm{\vect{v}}
    },
\end{equation}
where $\theta$ is the angle between the vectors $\vect{u}$ and $\vect{v}$.

% ##############################################################################
\subsection{Siamese and Triplet Networks}
\label{ssec:SiameseAndTripletNetworks}

% ------------------------------------------------------------------------------
\begin{figure}[t]
    \centering
    \begin{subfigure}[b]{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/theoretical_foundations/siamese_architecture.pdf}
        \caption[]{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/theoretical_foundations/triplet_architecture.pdf}
        \caption[]{}
    \end{subfigure}
    \caption[Contrastive and triplet loss]{Comparison of the Siamese \imgpartdesc{a} and the triplet \imgpartdesc{b} network architectures. The concept of weight sharing implies that only one set of model weights is trained.}
    \label{fig:SiameseAndTripletArchitectures}
\end{figure}
% ------------------------------------------------------------------------------

For the upcoming discussion, let $\func{D}{x, y}: \mathbb{R}^D \times \mathbb{R}^D \to \mathbb{R}$ be a metric function measuring distances in the embedding space. Without a loss of generality, we resort to use of the Euclidean distance ($L_2$ norm), so $\func{D}{x, y} = \euclnorm{x - y}$.

\subsubsection{Contrastive Loss}

Consider a sample $\rbrackets{x_0, x_1, y}$, where $x_0$ and $x_1$ represent the input, and the label $y = 1$ if $x_0$ and $x_1$ belong to the same category, otherwise $y = 0$. Let $\alpha$ be the margin representing the minimum distance in the metric space to separate positive from negative samples. The contrastive function for any sample is then defined as~\cite{hadsell2006dimreduction}
\begin{equation}
    \label{eq:ContrastiveLoss}
    \func{\mathcal{L}_{contr}}{\theta} =
    \frac{1}{2}y
    {\func{D}{\func{f_\theta}{x_0}, \func{f_\theta}{x_1}}}^2 +
    \frac{1}{2}
    \rbrackets{1 - y} {\rbrackets{
            \sbrackets{\alpha - \func{D}{\func{f_\theta}{x_0}, \func{f_\theta}{x_1}}}_{+}}}^2.
\end{equation}
The two inputs $x_0$ and $x_1$ are fed to the shared model at the same time. The output is then evaluated by the contrastive loss function (\figtext{}~\ref{fig:SiameseAndTripletArchitectures} \imgpartdesc{a}). Positive samples should have a small distance between each other as measured by the $\func{D}{\cdot}$ to decrease the loss towards $0$. Conversely, negative samples should have a distance beyond the threshold $\alpha$.

\subsubsection{Triplet Loss}

Apart from the contrastive loss, this time three samples are required to compute the loss. The rationale is to supply additional context when forming the metric space. Siamese networks are usually implemented using shared model weights, but there are better approaches when the triplet loss is used. Conceptually speaking, the model could be implemented as shown in \figtext{}~\ref{fig:SiameseAndTripletArchitectures} \imgpartdesc{b}. However, as we will discuss later, triplet mining strategies are required for the triplet loss to work properly.

Let $N$ be the number of all possible valid triplets $\rbrackets{x_a^i, x_p^i, x_n^i}$ for a given dataset. For any $i$-th triplet, let $x_a^i$ be the \emph{anchor} for a specific object (person, vehicle, etc.) with label $\func{y}{x_a^i}$, $x_p^i$ be the positive sample of the same object with label $\func{y}{x_p^i}$, such that $x_a^i \neq x_p^i \land \func{y}{x_a^i} = \func{y}{x_p^i}$, and let $x_n^i$ with label $\func{y}{x_n^i}$ be a sample of any other object, satisfying $\func{y}{x_a^i} \neq \func{y}{x_n^i}$, $\forall i = 1, \dots, N$. Let $\alpha$ be the margin value that is enforced between positive and negative pairs. Then, we want the relationship
\begin{equation}
    \label{eq:TripletDistanceConstraint}
    \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}} + \alpha < \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_n^i}}, \forall i = 1, \dots, N,
\end{equation}
to hold true. The triplet loss function is therefore defined as
\begin{equation}
    \label{eq:TripletLoss}
    \func{\mathcal{L}_{triplet}}{\theta} =
    \sum_{i = 1}^{N}
    {\sbrackets{
        \alpha +
        \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}} -
        \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_n^i}}
    }}_+.
\end{equation}
During the training, the model should learn to push negative samples further away from the positive samples, ideally exceeding the margin $\alpha$. When a negative sample is mapped closer than a positive sample, the training should result in the desired situation of bringing the positive sample closer while pushing the negative one further (\figtext{}~\ref{fig:TripletLossLearningProcess}).

% ------------------------------------------------------------------------------
\begin{figure}[t]
    \centerline{\includegraphics[width=0.6\linewidth]{figures/theoretical_foundations/triplet_loss_learning_process.pdf}}
    \caption[Triplet loss learning]{The objective is to learn embeddings such that the anchor is closer to the positive example than it is to the negative example by some specified margin value. \externalsrc{\cite{schroff2015facenet}}}
    \label{fig:TripletLossLearningProcess}
\end{figure}
% ------------------------------------------------------------------------------

% ##############################################################################
\subsection{Triplet Mining Strategies}
\label{ssec:TripletMiningStrategies}

Contrastive (\eqtext{}~\ref{eq:ContrastiveLoss}) and triplet (\eqtext{}~\ref{eq:TripletLoss}) loss functions play an important role in training an embedding model. However, the way that pairs or triplets are selected is crucial as it may significantly influence the training~\cite{hermans2017triplet, manmatha2017samplingmatters}. Moreover, as the dataset gets larger, then the number of possible triplets grows cubically, rendering the use of all of them impractical. The majority of those triplets would be so-called \emph{easy triplets}. To paraphrase the analogy from~\cite{hermans2017triplet}, showing the model that people with different clothes are not the same person after a certain point does not bring any new information. On the other hand, explicitly \emph{mining} images of similar-looking yet different people with the same clothes (\emph{hard negatives}) or of the same person with dramatically different poses (\emph{hard positives}) vastly contributes to an understanding of the notion of the \emph{same person}. As suggested, there are different kinds of triplets which are defined in \tabletext{}~\ref{tab:TripletCategoriesDefinitions}, using a general $\rbrackets{x_a^i, x_p^i, x_n^i}$ triplet for clarity. We encourage the reader to observe \figtext{}~\ref{fig:PositiveAndNegativeTripletsCategories}, too.

\begin{table}[t]
    \centering

    \begin{tabular}{cc}
        \toprule
        \tblcolname{triplet} & \tblcolname{constraint}                                                                                \\

        \midrule
        \emph{easy}          &
        $\func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}} + \alpha < \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_n^i}}$                                                            \\

        \emph{semi-hard}     &
        $\func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}} < \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_n^i}} < \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}} + \alpha$ \\

        \emph{hard}          & $\func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_n^i}} < \func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}}$                                              \\
        \bottomrule
    \end{tabular}

    \caption[Triplet categories.]{Definitions of various categories of triplets (regardless whether it is positive or negative) as imposed by their distance relationship.}
    \label{tab:TripletCategoriesDefinitions}
\end{table}

% ------------------------------------------------------------------------------
\begin{figure}[t]
    \centering
    \begin{subfigure}[b]{0.35\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/theoretical_foundations/triplet_positives_categories.pdf}
        \caption[]{}
    \end{subfigure}
    \begin{subfigure}[b]{0.35\textwidth}
        \centering
        \includegraphics[width=\textwidth]{figures/theoretical_foundations/triplet_negatives_categories.pdf}
        \caption[]{}
    \end{subfigure}
    \caption[Triplet loss categories visualization.]{Given a fixed anchor $x_a^i$ and positive sample $x_p^i$ as well as some positive margin value $\alpha$, we discriminate between three different types of categories in terms of their level of \emph{difficulty}. These categories vary in relation to positive \imgpartdesc{a} or negative \imgpartdesc{b} perspective.}
    \label{fig:PositiveAndNegativeTripletsCategories}
\end{figure}
% ------------------------------------------------------------------------------

In order to attain an effective convergence during the training, it is necessary to select triplets that violate the triplet constraint in \eqtext{}~\ref{eq:TripletDistanceConstraint}. This means that given $x_a^i$, the goal is to select a \emph{hard positive} $x_p^i$ given by $\argmax_{x_p^i}\cbrackets{\func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_p^i}}}$ and a \emph{hard negative} $x_n^i$ as a result of $\argmin_{x_n^i}\cbrackets{\func{D}{\func{f_\theta}{x_a^i}, \func{f_\theta}{x_n^i}}}$. Admittedly, it is often infeasible to compute the $\argmin\cbrackets{\cdot}$ and $\argmax\cbrackets{\cdot}$ over the entire training set. In this regard, there are two possible approaches to tackle this problem, either by selecting these hard triplets online or doing it offline~\cite{schroff2015facenet}.

\subsubsection{Offline Triplet Mining}

Given a training set, the task is to produce reasonable triplets off-line, for instance, at the epoch beginning. First, a list of $N$ different valid triplets is randomly generated, then separated into $\lfloor \nicefrac{N}{B} \rfloor$ batches of $B$ triplets, followed by computation of $3N$ embeddings using the most recent model checkpoint. Then, hard or semi-hard triplets may be selected. Since this strategy has been shown on multiple occasions~\cite{schroff2015facenet, hermans2017triplet, kuma2019vehiclereid} as an inferior choice compared to online triplet mining, we will not discuss this approach further.

\subsubsection{Online Triplet Mining}

Online mining is performed by selecting the hard positive/negative exemplars from within a minibatch (\figtext{}~\ref{fig:TripletArchitectureOnlineMining}). A condition that minimum number of exemplars for any identity is present in each minibatch has to be met. For example, Schroff~\etal{}~\cite{schroff2015facenet} used $40$ different images of a single person (an identity) per minibatch. Let $P$ be the number of different objects/identities (\egtext{}, people, vehicles) and $K$ be the number of different images for a concrete identity (\egtext{} different views of the same vehicle). There are two prominent approaches to online mining: \emph{batch all} and \emph{batch hard}.

% ------------------------------------------------------------------------------
\begin{figure}[t]
    \centerline{\includegraphics[width=0.5\linewidth]{figures/theoretical_foundations/triplet_architecture_online_mining.pdf}}
    \caption[Triplet loss online mining architecture]{A triplet network with an online triplet loss function. In this architecture, no weight sharing is required as the triplet selection happens \emph{online} solely in the loss function.}
    \label{fig:TripletArchitectureOnlineMining}
\end{figure}
% ------------------------------------------------------------------------------

\subsubsection{Online Triplet Mining: Batch All}

This strategy aims for selecting all valid triplets and averaging the loss only on the hard and semi-hard triplets. Easy triplets, \ietext{}, those for which the loss function equals $0$, are not taken into account. The reason is that averaging on them would result in a very small loss, since they would usually vastly outnumber the set of hard triplets~\cite{hermans2017triplet}. This approach produces a total of $PK \rbrackets{K - 1} \rbrackets{PK - K}$ triplets ($PK$ anchors, $K - 1$ positives per anchors, $PK - K$ negatives) incorporated in the loss function as
\begin{equation}
    \label{eq:BatchAllLossFunction}
    \begin{aligned}
        \func{\mathcal{L}_{batchall}}{\theta} =
        \sum_{i = 1}^P
        \sum_{a = 1}^K
        \sum_{\substack{p = 1 \\p \neq a}}^K
        \sum_{\substack{j = 1 \\j \neq i}}^P
        \sum_{n = 1}^K
        \Bigg[
         & \alpha +           \\
         & \func{D}{
            \func{f_\theta}{x_a^i},
            \func{f_\theta}{x_p^i}
        } -                   \\
         & \func{D}{
            \func{f_\theta}{x_a^i},
            \func{f_\theta}{x_n^j}
        }
        {\Bigg]}_{+}.
    \end{aligned}
\end{equation}

\subsubsection{Online Triplet Mining: Batch Hard}

In this strategy, the goal is to find the hardest positive and hardest negative for each anchor. The total number of triplets is $PK$. The selected triplets are the hardest among the given batch and can be considered moderate since they are the hardest within a small subset of the data. Therefore, the mining can be formulated as
\begin{equation}
    \label{eq:BatchHardMining}
    \begin{aligned}
        \func{\mathcal{L}_{batchhard}}{\theta} =
        \sum_{i = 1}^P
        \sum_{a = 1}^K
        \Bigg[
         & \alpha +                            \\
         & \underset{p = 1, \dots, K} {\max}
        \cbrackets{
            \func{D}{
                \func{f_\theta}{x_a^i},
                \func{f_\theta}{x_p^i}
            }
        } -                                    \\
         & \underset{\substack{j = 1, \dots, P \\n = 1, \dots, K\\j \neq i}} {\min}
        \cbrackets{
            \func{D}{
                \func{f_\theta}{x_a^i},
                \func{f_\theta}{x_n^j}
            }
        }
        {\Bigg]}_{+}
    \end{aligned}
\end{equation}


% !TEX root = altosaar-2020-thesis.tex
\chapter{Background}
\label{ch:background}
\lettrine[image=true,lines=3]{design/T}{his} chapter describes probabilistic models and probabilistic inference, taking as examples models from statistical physics and recommender systems.

\input{fig/fig_graphical_model_ising}

\section{Probabilistic Models}
Probability models assign probability to configurations of random variables. The random variables in a probability model might correspond to observed variables in a physical system, or to latent properties representing patterns in data collected from the world, or a combination of both. To define a probability model, it is necessary to specify the density $p$ of a collection of random variables $\mbz$. We focus on probabilistic models $p(\mbz)$ where relationships between random variables can be encoded as edges in a graph, or  probabilistic graphical models~\citep{jordan2004graphical}.

\subsection{Example: Ising Model}
\label{sec:ising}
For example, consider a model used in statistical physics: the Ising model. The Ising model can be used to model interactions between atoms in a material~\citep{henelius2016refrustration} to study how the material behaves in different conditions, paving the way toward material design. This probabilistic model has binary random variables $z_n$ with density
\begin{equation}
  p(\mbz; \beta) = \frac{\exp(-\beta E(\mbz))}{\cZ}\, .
  \label{eq:boltzmann}
\end{equation}
The semicolon in \Cref{eq:boltzmann} denotes that the model has a parameter $\beta$, representing the reciprocal temperature of the system of random variables (a physical quantity). The energy function $E(\mbz)$ encodes the relationships between random variables, and $\cZ$, the normalizing constant, ensures that this probability distribution sums to one over all configurations of random variables~\citep{chandler1987introduction}. The energy function of the Ising model is\footnote{Bold letters can denote collections of random variables $\mbz = \{z_1, z_2,...,z_N\}$, or vectors, depending on the context.}
\begin{equation}
\label{eq:ising-energy}
  E(\mbz) = -\frac{1}{2}\sum_{i, j} J_{ij}z_i z_j - H\sum_i z_i\, .
\end{equation}
The interaction strength $J_{ij}$ defines the interactions between random variables. In a simple Ising model, only nearest neighbors interact, so $J_{ij}$ is nonzero if the random variables $z_i$ and $z_j$ are neighbors. The parameter $H$ increases or decreases the energy in proportion to the values of the random variables $z_i$; we give its physical interpretation later.

The Ising model can be represented as a probabilistic graphical model, shown in \Cref{fig:graphical-model-ising}. Two variables $z_i$ and $z_j$ interact (changing the value of one leads to a change in probability of the other) only if they share an edge in the graph. This representation works in conjuction with the density in \Cref{eq:boltzmann}, as the presence of an edge in the graph corresponds to two variables interacting in the energy function $E$. In this model, the energy function (and hence graph) is such that only neighboring random variables interact.

The Ising model can be used to study physical systems such as magnetic materials, where interactions between atoms can be encoded into the interaction strength $J_{ij}$. The interactions between random variables encoded in this manner contain the necessary information to model the properties of a material. In modeling a material, the random variables $\mbz$ can be referred to as spins. Spin is a type of angular momentum carried by particles comprising atoms, and such angular momentum causes a magnetic field. Although the random variables $\mbz$ are binary, taking on values of $-1$ and $+1$, they can be re-scaled to the magnetic strength of the atoms in a particular material of interest if comparison to experimental data is required. The parameter $H$ can be interpreted as the magnitude of an external magnetic field that interacts with the magnetic strength and orientation of every atom~\citep{chandler1987introduction}.

To see how well an Ising model mirrors a physical material, a property such as magnetization can be measured in the material, and calculated using the model. Magnetization is the average orientation of the magnetic strength of every atom or random variable in the material,
\begin{equation}
  M(\mbz) = \frac{1}{N}\sum_{i=1}^N z_i\, .
\end{equation}
By measuring the magnetization $M$ and computing its value in the Ising model, a practitioner can deduce how accurately the model reproduces experimental data. For example, if an Ising model with nearest neighbors ($J_{ij} \neq 0$ if $i$ neighbors $j$) does not accurately reproduce the magnetization of a physical material, it may be necessary to include second-nearest neighbor effects ($J_{ij}\neq 0$ if $i$ and $j$ are connected by a path of length at most two).

Another example of a quantity that can be measured experimentally and computed in a probabilistic model is the thermodynamic free energy $F$,
\begin{equation}
  \label{eq:free-energy}
  F = -\frac{1}{\beta} \log \cZ\, .
\end{equation}
The free energy of a system relates to the amount of energy that can be extracted from a system by its surroundings. For example, the free energy of a protein is used to understand its stability, and can be measured by the amount of energy needed to destroy its structure by denaturing it~\citep{stone2013the-theory}. In modeling a magnetic material or biological material, the free energy can be derived from the normalizing constant $\cZ$~\citep{chandler1987introduction}.

\input{fig/tab_example_meals_regression}
\subsection{Example: Binary Classification}
\input{fig/fig_graphical_model_regression}
Another example of a probabilistic model is a binary classifier~\citep{bishop2006pattern}, represented as a graphical model in \Cref{fig:graphical-model-regression}. Consider $N$ datapoints of the form $(x_n, y_n)$ consisting of covariates $x_n$ and binary responses $y_n$. As illustrated in \Cref{tab:example-binary}, the covariates $x_n$ might represent information about items such as foods in a meal, and $y_n$ may indicate whether a single user ate a meal with those foods. A binary classifier would then classify whether the user would eat a new meal $\hat{x}_n$ based on its constituent foods.

A binary classifier is defined using a regression function $f$ with parameters $\mbtheta$. The logistic function $\sigma$ applied to the regression function defines the probability model for a binary classifier,
\begin{equation}
  p(y_n \mid x_n; \mbtheta) = \frac{\exp\left( \sigma(f(x_n; \mbtheta)) \cdot y_n\right)}{\cZ}\, .
  \label{eq:binary-classification}
\end{equation}
The logistic function constrains the output of $f$ to the unit interval, and $\cZ$ is again the normalizing constant. The regression function $f$ uses information about a datapoint to classify whether the response $y_n$ is positive. An example of a regression function is an inner product, defined by
\begin{equation}
  f(x_n; \mbtheta) = \mbtheta^\top x_n\, ,
\end{equation}
which corresponds to logistic regression~\citep{bishop2006pattern}. Alternatively, a more flexible model can be built using a deep neural network~\citep{lecun2015deep}.
%A binary classifier can form the basis of a recommender system as we study in \Cref{ch:rfs}.

\section{Inference}
In a probability model, computing---or, inferring---properties of the probability distribution is a central task. One inference problem is to ascertain likely configurations of random variables. Another is to compute the sum of a probability distribution over a set of random variables, for example, to compute the normalizing constant~\citep{jordan2004graphical}.

\subsection{Computing Likely Configurations of Random Variables}
In the study of a probability model such as a binary classifier in \Cref{eq:binary-classification}, one question of interest is: for a set of observations $(x_n, y_n)$, what is a likely value of $\theta$? Maximum likelihood estimation is one way to answer this question~\citep{bishop2006pattern}.

A probability distribution like $p(\mby \mid \mbx; \mbtheta)$ is also known as a likelihood function. It defines the likelihood of a random variable $\mby$ conditional on the value of data $\mbx$, with the current setting of the parameters $\mbtheta$. The maximum likelihood estimate of the parameters of this probability model for the data $(\mbx, \mby)$ is given by
\begin{equation}
  \mbtheta^* = \argmax_{\mbtheta} p(\mby \mid \mbx; \mbtheta)\, .
\end{equation}
This maximum likelihood estimate of the parameters $\mbtheta^*$ can be computed using stochastic optimization if the data is large~\citep{robbins1951a-stochastic}. % what if the argmax is intractable, or an integral? #todo details

\subsection{Computing the Normalizing Constant}

The second central inference task in probabilistic modeling is summing a probability model over a set of random variables. One example of this is computing the normalizing constant $\cZ$. This inference problem requires computing a sum: the normalizing constant ensures a probability distribution sums to $1$ over values the random variables can take.

Consider computing the normalizing constant for the binary classifier in \Cref{eq:binary-classification}.  To compute the normalizing constant $\cZ$ for this probability model, we can sum over the binary values the random variable $y_n$ can take,
\begin{align}
1 &= \sum_{y_n \in \{0, 1\}} \frac{\exp\left( \sigma(f(x_n; \mbtheta)) \cdot y_n\right)}{\cZ} \\
\Rightarrow \cZ &= \sum_{y_n \in \{0, 1\}} \exp\left( \sigma(f(x_n; \mbtheta) \cdot y_n)\right)\\
\cZ &= 1 + \exp\left( \sigma(f(x_n; \mbtheta))\right)\, .
\end{align}
Inference of the normalizing constant $\cZ$ is straightforward in this probability model. The random variable $y_n$ is binary, so there are only two terms in the sum needed to compute the normalizing constant.

Next, consider computing the normalizing constant or partition function for the Ising model in \Cref{eq:boltzmann}. The random variables $z_n$ in this model also take on binary values. The partition function is computed by summing over all the values associated with all random variables in the system, $\mbz = \{z_1, \ldots, z_N\}$:
\begin{align}
1 &= \sum_{z_1 \in \{-1, +1\}} \ldots \sum_{z_N \in \{-1, +1\}} \frac{\exp(-\beta E(\mbz))}{\cZ}\\
\Rightarrow \cZ &= \sum_{z_1 \in \{-1, +1\}} \ldots \sum_{z_N \in \{-1, +1\}} \exp(-\beta E(\mbz))\, .
\label{eq:intractable-partition}
\end{align}
There are $N$ binary-valued random variables and $2^N$ terms in the sum required to compute the partition function, so inference in the Ising model is difficult. For Ising models used to study materials, the partition function is intractable to compute for most model sizes practitioners want to study and compare to physical realizations.

One way to address the issue of an intractable partition function is with sampling methods, such as Markov chain Monte Carlo~\citep{metropolis1953equation}. These algorithms enable inference by simulating likely configurations of random variables. These samples of likely configurations are used to approximate quantities of interest such as the partition function. But, Markov chain Monte Carlo methods are difficult to scale to probabilistic models with large numbers of correlated random variables. In this thesis, we instead use variational inference, an approximate inference algorithm that relies on optimization instead of sampling.
% Calculating the partition function can be difficult, and there are many ways around computing the partition function. For example, sampling methods and variational methods can be used to approximate properties of distributions such as properties derived from the partition function. Markov chain Monte Carlo~\citep{metropolis1953equation} allows sampling system configurations from the Boltzmann distribution of a model; these samples can be used to approximate physical quantities. Variational inference relies on optimizing (varying) functionals to derive approximations of distributions of interest, these approximations can be used to compute properties of a model. Variational inference has roots in mean field methods in physics~\citep{saul1996mean,hoffman2013stochastic,blei2017variational} as described in \Cref{ch:background}.

\section{Variational Inference}
\input{fig/fig_vi_cartoon}
Instead of working with a probability model $p(\mbz)$ directly, \acrfull{vi} posits a family of distributions $q(\mbz; \mblambda)$ indexed by parameters $\mblambda$~\citep{blei2017variational}. The goal of \gls{vi} is to find the closest member of the variational family $q$ to the target distribution $p$. The algorithm consists of varying the parameters $\mblambda$ to improve the quality of the approximation, as illustrated in \Cref{fig:vi-cartoon}. One way to measure the distance between the variational approximation and the target distribution is with the \acrfull{kl} divergence, or relative entropy~\citep{mackay2003information,ranganath2018black}.

The intractable partition function in $p(\mbz)$ appears in the \gls{kl} divergence \gls{vi} uses to assess distance,
\begin{equation}
\label{eq:kl}
    \KL{q(\mbz; \mblambda)}{p(\mbz)} = \E_q[\log q(\mbz ; \mblambda)] -\E_q[\log p(\mbz)] \\
\end{equation}
But it is possible to derive an objective function that does not depend on the partition function, starting from the \gls{kl} divergence. Taking the Ising model in \Cref{eq:boltzmann} as an example,
\begin{align}
  \KL{q(\mbz; \mblambda)}{p(\mbz)} &= \E_q[\log q(\mbz ; \mblambda)] -\E_q[\log p(\mbz)] \\
 \KL{q(\mbz; \mblambda)}{p(\mbz)} &= \E_q[\log q(\mbz ; \mblambda)] -\E_q[-\beta E(\mbz) - \log \cZ] \\
 \log \cZ &= \E_q[-\beta E(\mbz)] - \E_q[\log q(\mbz ; \mblambda)] + \KL{q(\mbz; \mblambda)}{p(\mbz)} \label{eq:second-last}  \\
 \Rightarrow \log \cZ \geq \cL(\mblambda) &\coloneq  \E_q[-\beta E(\mbz)] - \E_q[\log q(\mbz ; \mblambda)]\, .
 \label{eq:llbo}
\end{align}
This lower bound $\cL$ on the log normalizing constant is also called the \acrfull{elbo}, and serves as the objective function for \gls{vi}. In deriving this lower bound from \Cref{eq:second-last} to \Cref{eq:llbo}, we used the fact that the \gls{kl} is greater than or equal to zero. To show this fact, we start from Jensen's inequality for a convex function $f$, or
\begin{equation}
  f(\E[\mbz]) \leq \E[f(\mbz)]\, .
\end{equation}
The logarithm in the \gls{kl} is concave, so its negative is convex. We apply Jensen's inequality to the negative \gls{kl} in \Cref{eq:kl}:
\begin{align}
  -\KL{q(\mbz)}{p(\mbz)} &= \E_q\left[\log \frac{p(\mbz)}{q(\mbz )}\right] \\
  &\leq \log \E_q\left[\frac{p(\mbz)}{q(\mbz )}\right]\\
  &= \log \int  q(\mbz) \frac{p(\mbz)}{q(\mbz )}d\mbz \\
  &= \log \int  p(\mbz)d\mbz \\
  &= 0 \, .
\end{align}
This shows that the \gls{kl} is greater than or equal to zero~\citep{cover2012elements}.

The left-hand-side in \Cref{eq:llbo} does not change as the variational parameters $\mblambda$ are varied in $\cL(\mblambda)$. In words, maximizing the lower bound $\cL(\mblambda)$ is equivalent to minimizing the \gls{kl} divergence between the variational approximation and target probability model.

\subsection{Example: Mean Field Variational Inference in the Ising model}
\label{sec:ising-mean-field}

To demonstrate \gls{vi}, we use the Ising model described in \Cref{sec:ising} with probability distribution $p(\mbz)$ defined in \Cref{eq:boltzmann} and energy function $E(\mbz)$ in \Cref{eq:ising-energy}. Inspecting the intractable partition function of the Ising model can help construct a variational family $q(\mbz; \mblambda)$ to approximate the Ising model.

The Ising model partition function in \Cref{eq:intractable-partition} is intractable because the sums do not decompose by random variables: every sum must be carried out in order, because the result of the $N$th sum over the random variable $z_N$ depends on the results of the sums over the previous $N-1$ random variables. This is because of interactions between dependent random variables. The first term in the energy function of the Ising model represents nearest neighbor interactions, $z_iz_j$, and is graphically equivalent to the links between nearest neighbors in \Cref{fig:graphical-model-ising}.

However, the second term in the Ising energy function in \Cref{eq:ising-energy}, $H\sum_i z_i$, does decompose by random variable. Physically, this corresponds to a magnetic field applied to the system as a whole, so every random variable is subject to the same force. Mathematically, there is an outer sum over every configuration of random variables, and in this term the results of the summation over a variable $z_i$ do not affect the summation over another variable $z_j$. So this magnetic field term can be evaluated for systems with many random variables.

\input{fig/fig-ising-markov-blanket}
The structure of the Ising model energy function and corresponding graphical model can be used to build a variational approximation $q(\mbz; \mblambda)$ as follows. If the second term of the Ising model energy function does not lead to an intractable partition function due to every random variable being subject to a magnetic field, one can construct a variational approximation by extending this physical intuition and developing the concept of a `mean field'. Consider the central random variable $z_i$ in \Cref{fig:graphical-model-ising}. Fixing the values of its nearest neighbors renders this random variable independent of the rest of the graph as shown in \Cref{fig:markov-blanket-ising}. The nearest neighbors of the central random variable can then be interpreted as giving rise to a magnetic field. The strength of this magnetic field is unknown, so we can define this unknown strength as a variational parameter $\delta H$ that we will infer using \gls{vi}. This mean field is additive to the external magnetic field $H$ applied to the system as a whole, so the energy function for the central random variable $z_i$ under this mean field assumption can be written
\begin{equation}
  E_{\mf}(z_i; \delta H) = \delta H z_i + H z_i\, .
\end{equation}
Note that we have replaced the interaction term $J_{ij} z_iz_j$ in the Ising model energy function in \Cref{eq:ising-energy} by the mean field $\delta H$. The mean field assumption is that term can approximate the effects of neighboring nodes~\citep{chandler1987introduction}. If we repeat this argument for every node in the graph, we arrive at the mean field energy function
\begin{equation}
  E_{\mf}(\mbz; \delta H) = -(H + \delta H)\sum_{i = 1}^N z_i\, .
  \label{eq:mean-field-energy}
\end{equation}
The above construction starting from the mean field assumption corresponds to the variational approximation with density
\begin{equation}
  q(\mbz; \beta, \delta H) = \prod_{i=1}^N\frac{\exp(- \beta E_\mf(z_i; \delta H))}{\cZ_\mf} \, ,
  \label{eq:mean-field-distribution}
\end{equation}
and we see that the variational parameter $\mblambda$ is simply the mean field strength $\delta H$. The mean field variational approximation corresponds to a fully factorized probability distribution where every random variable is independent~\citep{wainwright2008graphical}. This is a useful property, as the partition function is tractable in this mean field variational approximation: we can compute the partition function for every random variable by itself. The partition function for a single random variable $z_i$ under the mean field assumption is straightforward,
\begin{align}
  \cZ_\textrm{\mf, i} &= \sum_{z_i \in \{-1, +1\}} \exp(- \beta (H + \delta H) z_i) \\
  &= 2 \cosh (\beta (H + \delta H)) \, ,
  \label{eq:partition-function-i}
\end{align}
and the partition function for the variational approximation for all variables is simply $\cZ_\mf = \cZ_\textrm{\mf, i}^N$. Similarly, the average of a random variable under the variational distribution is readily computed as
\begin{align}
\begin{split}
  \E_{q(z_i)}[z_i] &= \sum_{z_i \in \{-1, +1\}} \frac{z_i \exp(- \beta E_\mf(z_i; \delta H))}{\cZ_\textrm{\mf, i}} \\
 &= \sum_{z_i \in \{-1, +1\}} \frac{z_i \exp(- \beta (H + \delta H) z_i)}{2 \cosh(\beta (H + \delta H))} \\
 &= -\tanh(\beta (H + \delta H))\, .
 \label{eq:mf-mean}
 \end{split}
\end{align}
Now that we have constructed a variational family for the Ising model, we can proceed with the \gls{vi} algorithm. The next step is writing down and maximizing the lower bound on the log partition function to minimize the \gls{kl} between our approximating distribution and model.

The lower bound on the log partition function $\cL(\delta H)$  in \Cref{eq:llbo} becomes
\begin{align}
  \cL(\delta H) &= \E_q[-\beta E(\mbz)] - \E_q[\log q(\mbz; \delta H)] \\
  &= \E_q\left[-\frac{1}{2}\beta\sum_{i, j} J_{ij}z_i z_j - \beta H\sum_i z_i\right] - \E_q\left[ -\beta (H + \delta H)\sum z_i\right] + \log \cZ_{\mf} \\
  &= \E_q\left[-\frac{1}{2}\beta\sum_{i, j} J_{ij}z_i z_j + \beta\delta H\sum_i z_i\right] + \log \cZ_{\mf}\, ,
\intertext{and we can take the expectation inside the sum using the fact that the mean field variational distribution is fully factorized, so }
  \cL(\delta H) &= -\frac{1}{2}\beta \sum_{i, j}J_{ij}\E_{q(z_i)}[z_i]\E_{q(z_j)}[z_j] + \beta \delta H \sum_i \E_{q(z_i)} [z_i] + \log \cZ_{\mf}\, .\\
\end{align}
In the first term, recall that two random variables $z_i$ and $z_j$ have the same distribution under the mean field assumption, and that every variable interacts with its four nearest neighbors in the Ising model. The lower bound on the log partition function then becomes
\begin{align}
 \cL(\delta H) &= -\frac{1} {2} \beta 4JN \E_{q(z_i)}[z_i]^2 + \beta N \delta H \E_{q(z_i)} [z_i] + \log \cZ_{\mf}\, .
\end{align}
The next step in the \gls{vi} algorithm is maximizing this lower bound, to minimize the \gls{kl} divergence between the variational approximation and the model. Taking the derivative with respect to $\delta H$ and suppressing the subscript of the expectation operator, we get
\begin{align}
\frac{\partial\cL(\delta H)}{\partial \delta H} &= N\beta(-4J\E[z_i]\partial_{\delta H}\E[z_i] + \E[z_i] + \delta H \partial_{\delta H} \E[z_i]) + N\beta \tanh(\beta (H + \delta H))\, .
\end{align}
Next, setting this derivative to zero and cancelling out terms (and using \Cref{eq:mf-mean}) leads to
\begin{align}
 0 &= -4J\E[z_i]\partial_{\delta H}\E[z_i] + \delta H \partial_{\delta H} \E[z_i]) \\
 \Rightarrow \delta H \partial_{\delta H} \E[z_i]) &= 4J\E[z_i]\partial_{\delta H}\E[z_i] \\
 \Rightarrow \delta H^* &= 4J\E[z_i]\, .
\end{align}
This shows that under a mean field assumption, the variational parameter that maximizes the lower bound on the log partition function---and hence minimizes the \gls{kl} divergence between the approximation and model---is proportional to the mean field around any node in the system. The structure of the model informs our choice of variational approximation.

The quality of the variational approximation $q(\mbz; \beta, \delta H^*)$ from \gls{vi} can be assessed in several ways. For example, the magnetization $M$ or the free energy $F$ can be calculated using the variational approximation, and these values can be compared to Markov Chain Monte Carlo simulations in small systems. This can be viewed as a type of predictive check for a \gls{vi} algorithm~\citep{blei2014build}. However, the development of theoretical guarantees to assess the quality of variational approximations found with \gls{vi} is an open area of research~\citep{wang2019frequentist}. Practitioners must currently empirically evaluate the quality of variational approximations according to the task at hand, as we do in \Cref{ch:hvm,ch:pvi}.

\subsection{Variational Inference Originated in Statistical Physics}

Previously, we derived a variational approximation to the Ising model by making a mean field assumption. That the language of physics is used in machine learning algorithms such as \gls{vi} is no coincidence. In fact, \citet{feynman1972statistical,feynman2018statistical} derives the \gls{gbf} inequality for use in a variational principle for approximating intractable partition functions using mean field assumptions. Consider a model with energy function $E$ and partition function $\cZ$, and a mean field variational approximation with energy function $E_\mf$ (and corresponding partition function $\cZ_\mf$). Then the \gls{gbf} inequality reads~\citep{feynman1972statistical,feynman2018statistical}
\begin{equation}
\cZ \geq \cZ_\mf\exp\left(-\beta \braket{E - E_\mf}_\mf\right) \, .
\label{eq:gbf-inequality}
\end{equation}
In physics, bra-ket notation is used to denote expectations. For example, expectations with respect to \Cref{eq:mean-field-distribution} are written $\braket{\; \cdot \;}_\mf$. Rewriting the \gls{gbf} with statistics notation for the expectation $\E_q[\;\cdot\;]$ yields
\begin{align}
 \cZ &\geq \cZ_\mf\exp\left(-\beta \E_q[E - E_\mf]\right) \, .
\end{align}
Taking the logarithm, we recover the lower bound on the log partition function
\begin{align}
 \log \cZ &\geq \E_q[-\beta E] - \E_q[-\beta E_\mf] + \log \cZ_\mf\\
 &= \E_q[-\beta E] - \E_q[\log q_\mf(\mbz; \mblambda)] \\
 &= \cL(\mblambda)\, .
\end{align}
This is identical to the log partition function lower bound in \Cref{eq:llbo}. \citet{hoffman2013stochastic} review the historical roots of the variational principle in its machine learning incarnation.

To complete the connection to machine learning, we relate this log partition function lower bound to the evidence lower bound studied in the \gls{vi} literature~\citep{blei2017variational}. A probabilistic model of data might have the following process for generating data $\mbx$ using prior information in latent variables $\mbz$:
\begin{align*}
\mbz &\sim p(\mbz)\\
\mbx &\sim p(\mbx \mid \mbz)
\end{align*}
The posterior distribution of this model is computed using Bayes' rule,
\begin{equation*}
  p(\mbz \mid \mbx) = \frac{p(\mbx \mid \mbz) p(\mbz)}{p(\mbx)} \, .
\end{equation*}
The model evidence $p(\mbx)$ is the partition function of the posterior. Calculating the partition function is what makes posterior inference difficult, as it requires integration over the latent variables $\mbz$,
\begin{equation*}
  p(\mbx) = \int p(\mbx, \mbz) d\mbz \, ,
\end{equation*}
and the latent variables $\mbz$ are typically high-dimensional, such as the number of random variables in an Ising model. But \gls{vi} can be used to approximate this intractable integral. The lower bound on the log partition function becomes the \gls{elbo}:
\begin{align}
\log p(\mbx) &\geq \cL(\mblambda) \\
\cL(\mblambda) &= \E_q[\log p(\mbx, \mbz)] - \E_q[\log q(\mbz ; \mblambda)] \, .
\end{align}
An example of a latent variable model without data is the Ising model---in this case, the data is an empty set, $\mbx = \{\}$. In this case $\cL(\mblambda)$ is a lower bound on the log partition function as we derived in \Cref{eq:llbo} and identical to the \gls{gbf} inequality.
% \gls{vi} is an algorithm to find a good approximation to a target probability distribution that has an intractable integral, such as the sum needed to compute a partition function. We now turn to the second inference problem of computing likely configurations of variables in a probability model.

\section{Conclusion}
We reviewed probability models and gave examples of their use in statistical physics and recommender systems. The task of inference is central to working with probability models; we described variational inference and maximum likelihood estimation. The following chapters address the issue of building the structure of a problem into a performant probability model, whether that structure concerns the connectivity in a statistical physics model, the structure of datapoints in a recommender system, or information about a variational approximation useful in an optimization algorithm for this approximation.


\section{Cyclic Polytopes and Gale Strings}
\label{gale-def-sect}

We now apply the results of the previous section to unit vector games
for which the best response polytope is the dual of a cyclic polytope.
These polytopes are characterized by their representation as
a combinatorial structure, called Gale strings.
We will first define cyclic polytopes,
then Gale string, then we will give the theorem by Gale \cite{gale}
that shows the
equivalence of the two representations.

The {\em moment curve} in dimension $d$ is defined as
\begin{equation}
\mu_d:\reals\longrightarrow\reals^d\qquad\qquad
\mu_d:t\longmapsto (t,t^2,\ldots,t^d)\T .
\end{equation}
The {\em cyclic polytope} $C_d(n)$ in dimension~$d$ with $n$
vertices, where $n>d$, is given as the convex hull of any
$n$ points on the moment curve, that is, by $n$ arbitrary reals
$t_1,\ldots,t_n$, where $ t_1<\cdots<t_n$, according to
\begin{equation}
\label{cycdef}
% affine independence is automatic
%C_d(n)=\conv\{ \mu_d(t_i)\ \mid\\text{ affinely independent } \}.
C_d(n)=\conv\{\,\mu_d(t_i)\,\mid\,1\le i\le n \}\,.
\end{equation}

\begin{example}
\label{cyc36-ex}
Figure \ref{cyc36-fig} shows the cyclic polytope in dimension 3 with 6 facets.

\begin{figure}[hbt]
\strut\hfill
\includegraphics[width=30ex]{chapter-2/fig-gale-def/cyc36.pdf}
\hfill\strut
\caption[The cyclic polytope $C_3(6)$]{The cyclic polytope $C_3(6)$.}
\label{cyc36-fig}
\end{figure}
\end{example}

Given $k\in\naturals$ and a set $S$, we can represent the function
$f:[k]\to S$ as the string $s=s(1)s(2)\cdots s(k)$; we have a {\em bitstring}
if $S=\{0,1\}$. A maximal substring of consecutive
1's in a bitstring is called a {\em run}.
A run is called even if its length is even, and odd if its
length is odd.
We will use the notation $\1^k$ for a run of
length~$k$ and $0^k$ for a string of 0's of
length $k$. A {\em Gale string of length $n$ and
dimension $d$}, where $n>d$, is a bitstring $s$ that satisfies the
following conditions:
\begin{enumerate}
\item exactly $d$ bits of $s$ are equal to $\1$;
\item ({\em Gale Evenness Condition})
$
\qquad 0\1^k0\text{ is a substring of }s\quad
{\Rightarrow}\quad
k\text{ is even.}
$
\end{enumerate}
We denote by $G(d,n)$ be the set of Gale strings of length $n$ and
dimension $d$.

In general, the Gale Evenness Condition allows for Gale strings that start
or end with an odd-length run; if $d$ is even then $s$ can start with
an odd run if and only if it ends with an odd run.
When $d$ is even, we can therefore see the Gale strings in $G(d,n)$
as ``loops''
obtained by ``gluing together'' the endpoints of the strings; on these
``loops'' all runs are even.
Formally, we can see the bit positions in a Gale string $s\in G(d,n)$ with
$d$ even as equivalence classes modulo~$n$.

\begin{example}\label{gs-example}
As an example for even $d$, we have
\begin{align*}
G(4,6) = \{ & \1\1\1\100, \1\1\100\1, \1\100\1\1, \100\1\1\1, 00\1\1\1\1, \\
            & 0\1\1\1\10, \1\10\1\10, \10\1\10\1, 0\1\10\1\1 \}
\end{align*}
The strings $\1\1\1\100$, $\1\1\100\1$, $\1\100\1\1$, $\100\1\1\1$,
$00\1\1\1\1$ and $0\1\1\1\10$ are
equivalent under a cyclic shift (if considering the strings as ``loops'', the
$\1$'s are all consecutive), as are the strings $\1\10\1\10$, $\10\1\10\1$
and $0\1\10\1\1$ (two runs of two $\1$'s separated by a single $0$).
As an example for odd $d$, we have
\[
G(3,5) = \{ \1\1\100, \10\1\10, \100\1\1, \1\100\1,
0\1\10\1, 00\1\1\1 \}\,.
\]
Notice that because $d$ is odd, a cyclic shift is
not allowed: $0\10\1\1$ is a shift of $\10\1\10$ but it is not a Gale
string.
\end{example}

The relation between cyclic polytopes and Gale strings was given by
Gale~\cite{gale}.

\begin{theorem}{\rm (Gale \cite{gale})}
\label{origgale-thm}
For any $d,n\in\naturals$, where $n>d$,
a set $F$ is a facet of $C_d(n)$ if and only if
\begin{equation}
\label{facet-gs}
F = \conv\{ \mu(t_j)\,\mid\, s(j)=1 \quad\text{ for }s\in G(d,n) \}.
\end{equation}
\end{theorem}

\begin{proof}
First, a hyperplane in $\reals^d$ of the form
$\{x\in\reals^d\mid a\T x=a_0\}$ for some nonzero vector
$a=(a_1,\ldots,a_d)\T$ can contain at most $d$ points on the
moment curve, because otherwise the polynomial equation
with a polynomial of degree~$d$ given by
$-a_0+a_1t+a_2t^2+\cdots a_dt^d=0$ would have more than $d$
roots~$t$.
For the same reason, any $d$ points
on the moment curve are affinely independent and define a
unique hyperplane through them, which the moment curve {\em
crosses} at these points. Notice that if the curve were tangent to the
hyperplane at an intersection, then a slight perturbation of the hyperplane
could contain $d+1$ or $d-1$ points on the moment curve.

Let $\overline{t_{1}} < \cdots < \overline{t_{d}}$ be a
choice of $d$ of the $t_j$'s in the definition
(\ref{cycdef}) of $C_d(n)$; then the intersection of the moment curve and of
the hyperplane $H$ through the points
$\mu_d(\overline{t_1})$, \ldots, $\mu_d(\overline{t_d})$ coincides
exactly with the points $\mu_d(\overline{t_i})$.
Since the moment curve crosses the hyperplane at all intersections,
if $t,t'\notin\{ \overline{t_i} \}$ and
$t<\overline{t_i}<t'$ for exactly one of the $\overline{t_i}$'s then
$\mu_d(t)$ and $\mu_d(t')$ are on opposite sides of~$H$.

A facet $F$ of the cyclic polytope $C_d(n)$ is given by
$F=H\cap C_d(n)$. This corresponds to a choice of
$\overline{t_i}$'s such that for all the other
$t_k\notin \{ \overline{t_i}\,\mid\,i\in [d] \}$ in
the definition of $C_d(n)$,
the corresponding $\mu_d(t_k)$ are on the {\em same} side of~$H$.
This can happen only if for every pair of these $t_k$'s the moment
curve has an even number of crossings at $\mu(\overline{t_i})$
of $H$ between them. This is equivalent to ask that there is
an even number of $\overline{t_i}$'s between any two $t_k$'s.

Let $s$ be the bitstring in which the \1's correspond to the
$\overline{t_i}$'s and the 0's correspond to the other $t_k$'s.
Then the condition that the set $F$ in (\ref{facet-gs}) is a
facet is equivalent to the Gale Evenness Condition.
\end{proof}

Because the moment curve has at most $d$ points on any hyperplane,
each facet of $C_d(n)$ is a $d$-simplex, so $C_d(n)$ is simplicial
and the choice of the $t_j$'s in (\ref{cycdef}) is
irrelevant for the characterization of the facets of
$C_d(n)$ as Gale strings, as long as $t_1<\cdots<t_n$.

\begin{example}
Consider the facet $F$ of the cyclic polytope $C_3(6)$ marked in blue
in Figure \ref{cyc36fac-fig}.
Let us label the vertices on the moment curve as $t_i$, with $i\in [n]$, and
we set $s(i)=1$ if $t_i$ is a vertex of $F$ and $s(i)=0$
otherwise. Figure \ref{cyc36fac-hyper-fig} represents
the intersection of the moment curve and the hyperplane $H$ in $\reals^3$
defined by the $t_i$ such that $s(i)=1$. This shows
how the corresponding Gale string $s\in G(3,6)$ is $s=\100\1\10$.

\begin{figure}[h]
\strut\hfill
\includegraphics[width=25ex]{chapter-2/fig-gale-def/cyc36fac.pdf}%
\hfill\strut
\caption[A facet of the cyclic polytope $C_3(6)$]{%
The facet of the cyclic polytope $C_3(6)$ through the points
$\mu_3(t_1),\mu_3(t_4),\mu_3(t_5)$.
}
\label{cyc36fac-fig}
\end{figure}
\begin{figure}[h]
\strut\hfill
\includegraphics[width=48ex]{chapter-2/fig-gale-def/100110-hyper.pdf}%
\hfill\strut
\caption[A facet of $C_3(6)$ as zeroes of the moment curve]{%
The facet of the cyclic polytope $C_3(6)$ through the points
$\mu_3(t_1),\mu_3(t_4),\mu_3(t_5)$, as in Figure \ref{cyc36fac-fig},
seen from the side of the hyperplane.
This corresponds to the Gale string $s=\1 00 \1\1 0\in G(3,6)$.
}
\label{cyc36fac-hyper-fig}
\end{figure}
\end{example}

\clearpage

\begin{example}
\label{c46-ex}
Figure \ref{c46-fig} shows the cyclic polytope $C_4(6)$, with the exterior
facet corresponding to the Gale string $s=\1\1\1\100$.
Figure \ref{cyc46fac-hyper-fig} shows the correspondence between the
string $s=\1\1\1\100$ and the intersection of the moment curve and
the hyperplane $H$.
\begin{figure}[h]
\strut\hfill
\includegraphics[width=50ex]{chapter-2/fig-gale-def/c46.pdf}%
\hfill\strut
\caption[The cyclic polytope $C_4(6)$]{%
The cyclic polytope $C_4(6)$. The thin lines represent the edges inside
the exterior facet, in bold lines. Vertex $i$ corresponds to $t_i$.
}
\label{c46-fig}
\end{figure}
\begin{figure}[hbt]
\strut\hfill
\includegraphics[width=50ex]{chapter-2/fig-gale-def/111100-hyper.pdf}%
\hfill\strut
\caption[A facet of $C_4(6)$ via the moment curve]{%
The facet of the cyclic polytope $C_4(6)$ given by the intersection of
the moment curve and the hyperplane $H$ through the points
$\mu_4(t_1),\mu_4(t_2),\mu_4(t_3),\mu_4(t_4)$. This
corresponds to the Gale string $s=\1\1\1\1 00\in G(4,6)$.}
\label{cyc46fac-hyper-fig}
\end{figure}
\end{example}

\clearpage

\begin{example}
As a counterexample, consider Figure \ref{notfacet-even-hyper-fig}.
The points $t=t_3$ and $t'=t_5$ lie on the moment curve, but
$\mu_4(t_3)$ and $\mu_4(t_5)$ are on opposite
sides of the hyperplane~$H$ defined by the other four points.
The corresponding bitstring is $s=\1\10\10\1$, which is
not a Gale string. The violation of the Gale Evenness Condition corresponds
to the change of side with respect to the hyperplane between $t$ and $t'$.

\begin{figure}[hp]
\strut\hfill
\includegraphics[width=55ex]{chapter-2/fig-gale-def/notfacet-hyper.pdf}%
\hfill\strut
\caption[Not a facet of $C_4(6)$]{%
There is a change of side between two $\mu_4(t_j)$'s (for
the 0 bits). The bitstring $s=\1\10\1 0\1\1$
does not satisfy the Gale Evenness Condition, and the
set of $\mu_4(\overline{t_i})$'s (for the \1 bits)
does not define a facet of $C_4(6)$.
}
\label{notfacet-even-hyper-fig}
\end{figure}
\end{example}


\subsubsection{Supremum Augmented Dickey Fuller}
\label{sec:methods_features_sadf}

Section \ref{sec:intro_domain} briefly introduced the concept of structural
breaks and the index to be implemented and used in this research project, SADF.
In words of Lopez de Prado:

\say{In developing an ML-based investment strategy, we typically wish to bet
when there is a confluence of factors whose predicted outcome offers a favorable
risk-adjusted return. Structural breaks, like transition from one market regime
to another, is one example of particular interest.}

The problem appears when trying to quantitatively detect how a regime change
occurs. In \cite{sadf_paper} Phillips, Wu and Yu studied Nasdaq index in 1990
prior to the famous DotCom bubble and proposed a new index based on recursive
augmented Dickey-Fuller tests for unit root against the alternative of an
explosive root (the right-tailed). The objective of this test is to identify
the presence of exponential growth or collapse, while assuming an autoregressive
specification.

In price series, like the one used in this research project, not only
one, but many bubbles are prune to happen. Not all indexes are useful under this
assumption as many fail to detect recurrent bubbles. Nevertheless, we will start
analyzing the case of just one bubble in the series and then generalize it to
many as the it is described in chapter 17 of \cite{lopez_de_prado}. Suppose a
price series that follows a first order autoregressive process:

\[ y_{t} = \rho y_{t-1} + \epsilon_{t} \]

where $\epsilon_{t} \sim N(0, \sigma_{y}^{2})$, i.e. white noise. We can create a
test to evaluate the value of $\rho$ whose null hypothesis states that the price
series follows a random walk. In other words, $H_{0}: \rho = 1$ and the
alternative hypothesis is that $y_t$ starts as a random walk but at some point
in time $t^{*}T$ the process becomes explosive, such:

\begin{equation}
  H_{1} :
    \begin{cases}
      \rho = 1 & \text{if t = 1, ..., $\tau^{*}T$}\\
      \rho > 1 & \text{if t = $\tau^{*}T$, ..., T}\\
    \end{cases}
\end{equation}

where $\tau^{*} \in (0,1)$. At the end of the series, i.e. at $T$, one
could try to find the value $\tau^{*}$ where there was a change of regime from
random walk to an explosive process. To test this hypothesis, we should
consider:

\[ \Delta y_{t} = \delta y_{t-1} D_t[\tau^{*}] + \epsilon_{t}\]

where $D_t[\tau^{*}]$ is a dummy variable that takes the value of 0 when
$t < \tau^{*}T$ and 1 otherwise. $H_{0}: \delta = 0$ which is tested against the
one-sided alternative $H_{1}: \delta > 1$ leading to an statistic:

\[ DFC_{\tau^{*}} = \frac{\hat{\delta}}{\hat{\sigma}_{\delta}} \]

One needs to determine the value of $\tau^{*}$ because it is unknown. The
approach to determine it is to compute the supremum statistic for each possible
value of $\tau^{*}$ in the series. That would determine the start of the explosive
process yielding to a bubble. Note that only the beginning of the regime change
is determined with this method, i.e. there is no return to a random walk after
the bubble starts. This is where the novelty of Phillips, Wu and Yu appears by
a few key differences to the autoregressive model and test:

\begin{itemize}
  \item The regression specification becomes: $ \Delta y_t = \alpha + \beta y_{t-1} + \sum_{l=1}^{L} \gamma_{l} \Delta y_{t-l} + \epsilon_t$
  \item $H_{0}: \beta \le 0$, and $H_{1}: \beta > 0$
  \item $SADF_t = \sup_{t_{0} \in [1, t-\tau]} \{ADF_{t_{0},t}\} = \sup_{t_{0} \in [1, t-\tau]} \frac{\hat{\beta}_{t_{0},t}}{\hat{\sigma}_{\beta_{0},t}}$
\end{itemize}

A few differences with respect to the original, one-bubble model can be noted:

\begin{itemize}
  \item The regression is changed, there is no more a dummy $D_t[\tau^{*}]$
        variable. Instead, the regression starts at $t_{0} \in [1, t-\tau]$ and
        ends in $t \in [\tau, T]$.
  \item $SADF_t$ computes the supremum in a double nested loop for every
        possible value of $t_0$ and $t$ which are the indexes to segment the
        series.
\end{itemize}

The aforementioned characteristics allows SADF to vary provided that it is not
computed just for one time ($T$), but instead for many (every value of
$t \in [\tau, T]$).

Getting into the details of the augmented Dickey-Fuller statistic, the
confidence value should be set from the sample to yield the best results. In
\cite{sadf_paper} the authors refer to a value close to 4\% to deliver the best
performance but values between 1\% and 5\% are recommended. 5\% was used in this
case.

\paragraph{Implementation notes:} Lopez de Prado offers in his book almost
the entire algorithm (see chapter 17 of \cite{lopez_de_prado}), but leaves
behind the outer loop which allows to move forward the $SADF_{t}$ for each
$t \in [\tau, T]$. A modification was introduced in code to avoid excessive and
inefficient computation in the inner loop: an upper bound for the window was
introduced to so that the range of $t$ becomes $[max(\tau, t - \Delta t_{max}), T]$.
Although the asymptotic computational complexity of this series generation is
high ($O(n^5)$ at least, see \cite{lopez_de_prado} for a detailed analysis), one
way to saturate one order is to use $\Delta t_{max}$ which should be cautiously
selected to account for long lasting bubbles.

On a separate note, the algorithm allows researchers to introduce a constant, a
linear and a quadratic polynomial regression depending on the type of series to
analyze. All of them were computed in favor of feeding the model with more data
and experiment what yields the best results.

Finally, a recommendation in the article \cite{sadf_paper} and discussed in
\cite{lopez_de_prado} has been implemented. Instead of using raw prices, the
algorithm takes as input log-prices. Conceptually, by applying the logarithmic
transformation, the variance of the process gets stabilized and the
heteroscedastic assumption about the data can be fulfilled. Long time series
are expected to change their price levels but that does not directly cause a
regime change and the transformation facilitates to distinguish them. A regime
change implies a transition to an explosive behavior, e.g. exponential behavior
is identified.

Three pictures are shown to illustrate this index: \ref{fig:sadf_prices},
\ref{fig:sadf_prices_ffd}, and \ref{fig:sadf_prices_log}. They contrast the
index with raw bitcoin close price, fractionally differentiated bitcoin price
and log prices respectively. Note the progression in the pictures and how the
volatility of the log-prices series correlates with SADF spikes
\ref{fig:sadf_prices_log}.

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/sadf_prices.png}
    \caption{Raw close prices and SADF over time.}
    \label{fig:sadf_prices}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/sadf_prices_ffd.png}
    \caption{Fractionally differentiated prices and SADF over time.}
    \label{fig:sadf_prices_ffd}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{methods/images/sadf_prices_log.png}
    \caption{Close log-prices and SADF over time.}
    \label{fig:sadf_prices_log}
\end{figure}


\documentclass{article}

\usepackage{mathrsfs,amssymb,amsmath}

\begin{document}

Page 142
\section{If $x \in X$ and $\delta_x(f) = f(x)$ for all $f$ in $C_b(X)$, show that $||\delta_x|| = 1$}

\begin{align}
|| \delta_x ||  &= \sup\{ || \delta_x(f) || : f \in C_b(X), || f || \le 1 \}  \\
&= \sup\{ || f(x) || : f \in C_b(X), || f || \le 1 \} \\
&= 1
\end{align}

Since $|| f || \le 1$ are exactly those functions where $f(x) \le 1 $ for all $x$.

\end{document}