\chapter{Statistical Mechanics and Thermodynamics}

\section{Laws of Thermodynamics}
\begin{enumerate}
\item The change in \emph{internal} energy of a system is equal to the heat added minus the work it does
\begin{align}
dE = \delta Q - \delta W
\end{align}
The reason why we write the squiggles is because this is true no matter how you change the system, as we must still have conservation of energy, but if we have cases where we move between equilibriums we can then write that $\delta Q = T dS$ and $\delta W = PdV$. In general $TdS \ge \delta Q$, for instance a freely expanding gas, which does not exchange heat, but since it has more places for each molecule to be, increases in entropy.

\item Entropy of the universe always increases

$$\Delta S \ge 0$$ Is a statement that the equilibrium state is preferred for a system, so if a system is changed, it is at first not in equilibrium, then moves towards equilibrium, where the entropy is maximized, so the change in entropy will be increased. Equilibrium can be thought of as a state where any process and its time reversal have equal probability. \textbf{Detailed Balance} says that if you have two processes (1,2) $\rightarrow$ (3,4) they are just as likely as (3,4) $\rightarrow$ (1,2)

\item Entropy of a perfect crystal (one state) goes to zero as temperature goes to zero
 $$S\rightarrow 0 ~~\rm{as}~~ T\rightarrow 0$$

\end{enumerate}

\section{Thermodynamics}
Thermodynamics has to do with huge numbers of particles that we can't easily keep track of individually. For classical mechanics, our "state variables" were things like positions and momentum in the Hamiltonian formulation. In Thermodynamics,  instead of trying to keep track of $10^{23}$ pairs of position and momenta, we just keep track of a few macroscopic things that can tell us things about the system from a macroscopic point of view.

Here our "state variables" are $p,T,V,S$ out of which we are able to describe everything. %\footnote{I'm not entirely sure why these were chosen., as entropy itself is non-trivial to measure, maybe its the leftover variable?}
The issue is that these variables  are all netted together in often mathematically unfriendly ways and in general are \emph{not independent} of one another. We get around this by just taking a bunch of partial derivatives all over the places, holding this or that constant.

We start with the first law of Thermodynamics
\begin{align}
dE = \delta Q - \delta W
\end{align}
So long as each time we infinitesimally change the system, we wait for it to equilibrate before touching it again (a \emph{quasistatic process}),  we can use the equalities
\begin{align}
dE = TdS - p dV
\end{align}
In general
\begin{align}
T\delta S \ge \delta Q
\end{align}
This is because entropy can change without heat being added to the system, for instance a free expansion of a gas. Rearranging the first law to find entropy, we get
\begin{align}
dS &= \frac{1}{T}dE +\frac{p}{T}dV
\end{align}
We are also free to think of the energy as being a function of $T$ and $V$, so
\begin{align}\label{thermenergy}
dE = \Big(\frac{dE}{dT}\Big)_VdT + \Big(\frac{dE}{dV}\Big)_TdV
\end{align}
Which tells us
\begin{align}
dS &= \frac{1}{T}\Big(\frac{dE}{dT}\Big)_VdT + \frac{1}{T}\Big[p + \Big(\frac{dE}{dV}\Big)_T\Big]dV
\end{align}
But also
\begin{align}
dS &= \Big(\frac{dS}{dT}\Big)_V dT + \Big(\frac{dS}{dV}\Big)_T dV
\end{align}
Which shows us one of the many funny relationships we get with the partial derivatives
\begin{align}
 \Big(\frac{dS}{dT}\Big)_V &= \frac{1}{T}\Big(\frac{dE}{dT}\Big)_V\\
\Big(\frac{dS}{dV}\Big)_T &= \frac{1}{T}\Big[p + \Big(\frac{dE}{dV}\Big)_T\Big]\label{partialentropy}
\end{align}
Going back to equation \ref{thermenergy}, by definition, we have the specific heat as
Part of this is easy since by definition
\begin{align}
c_v \equiv \Big(\frac{dQ}{dT}\Big)_V = \Big(\frac{dE}{dT}\Big)_V
\end{align}
For the next one, we can rearrange equation \ref{partialentropy} after using the Maxwell square (Section \ref{square}) to find
\begin{align}
\Big(\frac{\partial S}{\partial V}\Big)_T &= \Big(\frac{\partial p}{\partial T}\Big)_V
\end{align}
Plugging this in we get
\begin{align}
T\Big(\frac{\partial p}{\partial T}\Big)_V - p = \Big(\frac{dE}{dV}\Big)_T
\end{align}
Which is often useful when given an equation of state i.e. function that contains $P$ as a function of the other variables. I still don't have a solid grasp as to the best algorithm for finding these relationships, it seems like trying to get somewhere by random walk.

\subsection{Thermodynamic Potentials}
The idea here is that somehow from the second law, you can write all of these different kinds of potentials which are at a minimum when we
hold certain things about the system as constant. \\

\begin{center}
\begin{tabular}{ c c }
Constant & Potential Minimized \\ \hline
$S,V$ & $U$ - Internal Energy\\
$T,V$ & $F$ - Helmholtz Free Energy\\
$T,P$ & $G$ - Gibbs Free Energy \\
$S,P$ & $H$ - Enthalpy\\
$T,V,\mu$ & $\Phi$ - Grand Potential
\end{tabular}
\end{center}
So what we do with each of these guys is, since we know two of the things are constant, we take the derivative of the potential with respect to either of the other two state variables and set it to zero. We can then solve that equation for whatever we are looking for. The Helmholtz free energy can be written as

\begin{align}
F = U - TS = -kT\ln Z
\end{align}

\subsection{Thermodynamic Square}\label{square}

As a reminder
\begin{align}
U=&~\textrm{Internal Energy}\\
H =&~\textrm{Ent\textbf{h}alpy}\\
G =&~\textrm{\textbf{G}ibbs Free Energy}\\
F =&~\textrm{Helmholtz \textbf{F}ree Energy}
\end{align}
A nice mnemonic for remembering:\\ \\
\centerline{Good Physicists Have Studied Under Very Fine Teachers.}
\\
\\
\centerline{\includegraphics[width=0.3\textwidth]{physics/images/Thermodynamic_square}}
\begin{enumerate}
\item Start with a potential (e.g. $F$)
\item Pick the two variables at the opposite corners, noting their signs (e.g. $-P$, $-S$)
\item Now multiply those terms by their term furthest from them, forgetting about the sign. This gives us $$dF = -S dT -pdV$$
\item If you have non-fixed number of particles, just add $+\mu dN$ to whichever potential you are looking at
\end{enumerate}

This gives us the natural way to express whatever potential we obtained, in terms of the differentials, so
\begin{align}
F = F(T,V)
\end{align}

We can also obtain the maxwell relations with the square once we find the definition of the potential we chose, we can say
\begin{align}
dF = \Big(\frac{\partial F}{\partial T}\Big)_V dT + \Big(\frac{\partial F}{\partial V}\Big)_T dV
\end{align}
We know by definition that
\begin{align}
\frac{\partial^2 F}{\partial T\partial V} = \frac{\partial}{\partial V}\Big(\frac{\partial F}{\partial T}\Big)_V &= \frac{\partial}{\partial T}\Big(\frac{\partial F}{\partial V}\Big)_T
\end{align}
So looking at the prefactors on our initial expression for the potential, we can find that
\begin{align}
-\Big(\frac{\partial S}{\partial V}\Big)_T &= -\Big(\frac{\partial p}{\partial T}\Big)_V
\end{align}
We can do the same trick to find the rest, there is one for each potential. You can also look and see you can obtain these factors by taking a ''U" shape in the box. In the above case we take the top row, then the bottom right corner, is equal to the bottom row, then the top right corner.

\section{Quasi-Static (Reversible) Process}

$$\Delta S = 0$$
As the system changes, it does so by changing between equilibrium states the entire time. Because it is in equilibrium the entire time, we don't change entropy.

\section{Ideal Gas}
An ideal gas is just a gas that doesn't interact with the other molecules in the gas, and behaves as if each of the molecules were just alone. We calculate the partition function for just one of the particles with
\begin{align}
    Z_1 = \frac{1}{h^3}\int dx^3 \int dp^3 e^{-\beta p^2/2m}
\end{align}
Which is just adding up all the possible states it could be in. It's energy is just kinetic. We can actually evaluate this integral explicitly using a Gaussian integral trick giving us
\begin{align}
    Z_1 = \frac{V}{h^3}\Big(2\pi m kT\Big)^{3/2}
\end{align}
Since the particles don't interact, the total partition function, which is the normalizing factor considering \emph{all} states, is just all of the individual ones multiplied together, normalized by $1/N!$ due to a strange paradox called the Gibb's paradox.
\begin{align}
    Z = \frac{1}{N!} Z_1^N = \frac{V^N}{h^3N!}\Big(2\pi m kT\Big)^{3N/2}
\end{align}
The free energy of a system is defined as
\begin{align}
    F = -kT\ln Z
\end{align}
And we know that one of Maxwell's relations tells us
\begin{align}
    P = -\Big(\frac{dF}{dV}\Big)_{T,N}
\end{align}
This formula in general tells us the \emph{equation of state} and is true for any type of system. Using our expression for the partition function and plugging into this, we can find the equation of state for an ideal gas, which we all know and love
\begin{align}
    PV = NkT
\end{align}
\subsection{Isotherms}
From the equation of state, we see that if temperature is constant for a process, we must have that the product of pressure and volume is also constant
\begin{align}
    PV = \textrm{const}
\end{align}

\subsection{Adiabats}
Adiabatic expansion happens when there is no heat transferred to or from the system, so
\begin{align}
    \delta Q = 0
\end{align}
So looking at the first law, we see
\begin{align}
    dU = -PdV
\end{align}
We know for an ideal gas, the internal energy $U$ is only dependent on temperature, and after also plugging in for the ideal gas law
\begin{align}
    \frac{3}{2}Nk dT = -\frac{NkT}{V}dV
\end{align}
Rearranging and integrating we see that
\begin{align}
    \int \frac{dT}{T} &= -\frac{2}{3} \int \frac{dV}{V}\\
    \ln T &= -\frac{2}{3}\ln V + C
\end{align}
Exponentiating, we have
\begin{align}
    TV^{2/3} = e^C
\end{align}
Now plugging in the equation of state once more, we arrive at
\begin{align}
    PV^{5/3} = Nke^C
\end{align}
Or, since the number of particles is held fixed, we have that
\begin{align}
    PV^{5/3} = \textrm{const}
\end{align}

\subsection{Free Expansion}
In a free expansion, the gas does no work, so
\begin{align}
    dW = 0
\end{align}
and if the process is adiabatic
\begin{align}
    dQ = 0
\end{align}
Which tells us the change in internal energy is zero
\begin{align}
    dU = dQ - dW
\end{align}
So with the ideal gas law, we get
\begin{align}
    P_iV_i = P_fV_f
\end{align}

\section{Entropy}

Entropy can be thought of as a unit of information. The mathematical definition of entropy is given by

\begin{align}\label{entropy}
S = k_B\ln\Omega
\end{align}
Where $\Omega$ is the number of possible ways a given system could be arranged, each with equal probabilities. The entropy is a state variable, and can be evaluated explicitly for a given system, i.e. a system will have a definite entropy. What we mean by "number of ways the system could be arranged" is the amount of configurations we could put all of the small pieces together that make up the system (typically all the molecules) that give us the exact same macroscopic quantities. %\todo{elaborate}

 The second law tells us that number of ways that the universe could be organized is \emph{always} increasing in time until it reaches equilibrium. There is speculation that this should in some way tell us more about time itself, as the two are entwined together in way a which is still one of Physics great mysteries\cite{weinert}. Another more general definition is given by

\begin{align}\label{gen-entropy}
S = -\sum_{i=1}^N P_i\ln P_i
\end{align}

Where we sum over the probabilities that the system is in the state $1,2,... N$. For instance if there are two energies ($0,\epsilon$) possible, you will sum over those states for the entropy. This definition must also include states which are degenerate in energy. The reason for the minus sign is that the logarithm of a fraction is negative, so to get entropy positive, we swap signs. This expression reduces to equation \ref{entropy} in the case where all the probabilities are equal.

The entire game of statistical mechanics boils down to try to maximize this quantity (equation \ref{gen-entropy}) under constraints using Lagrange multipliers. Two simple constraints are

\begin{align}\label{sumprob}
\sum_{i=1}^N P_i -1 &= 0\\ \label{sumenergy}
\sum_{i=1}^N E_iP_i - E &= 0\\
\end{align}
These are the constraints for the canonical ensemble. So using the multipliers
\begin{align}
S' = -\sum_{i=1}^N P_i\ln P_i - \alpha\Big[\sum_{i=1}^N P_i -1\Big] - \beta\Big[\sum_{i=1}^N E_iP_i - E\Big]
\end{align}
We now look for the extremum of the entropy, which will be positive since $P_i$ is always positive, and $-\ln P_i$ is also always positive.
\begin{align}
\frac{\partial S'}{\partial P_i} &= -\ln P_i  + 1 -\alpha - \beta E_i\\
 &= 0
\end{align}
We see we have now almost accidentally obtained an expression for each $P_i$, which we can get by shifting variables and exponentiating. We first define $1-\alpha = \ln Z$

\begin{align}\label{stat-probability}
P_i = \frac{1}{Z}e^{-\beta E_i}
\end{align}
This is the \textbf{Boltzmann distribution}, found only using the constraint of maximum entropy, total energy, and probability conservation. This allows us to define the \textbf{partition function} $Z$ plugging into equation \ref{sumprob} with
\begin{align}
\sum_{i=1}^N e^{-\beta E_i} = Z
\end{align}
Which acts like a normalization factor for the probabilities. Now plugging into equation \ref{sumenergy}, we get

\begin{align}
\sum_{i=1}^N \frac{1}{Z}e^{-\beta E_i}E_i = E
\end{align}
We now see that the following identity holds, which lets us express the average energy as a function of \emph{only} the partition function
\begin{align}\label{partenergy}
E = -\frac{1}{Z}\frac{\partial Z}{\partial \beta} = -\frac{\partial \ln Z}{\partial \beta}
\end{align}
In the continuum limit, we get

\begin{align}
S = -\int dV \rho \ln\rho
\end{align}

Where $\rho$ is the \emph{probability density} and we integrate over the volume in phase space $p,q$ as we define the states as when an atom has some fixed $p, q$. This has an identical form to the density matrices  used in Quantum Mechanics (Equation \ref{density}), which is pretty weird.

\section{Temperature}
Temperature is the measure of the average kinetic energy of a system. It is defined as

\begin{align}
\frac{1}{T}= \frac{\partial S}{\partial E}
\end{align}

It tells you how much the entropy changes by when you change the average energy of the system. It can be shown using equation \ref{stat-probability} that the entropy looks like
\begin{align}
S = k(\beta E + \ln Z)
\end{align}
Thus
\begin{align}
dS &= Ed\beta + \beta dE +\frac{\partial \ln Z}{\partial \beta}d\beta\\
&= Ed\beta + \beta dE -Ed\beta\\
&= \beta dE
\end{align}
Which tells us that
\begin{align}
\frac{1}{T} = \beta
\end{align}
So temperature is in fact just a Lagrange Multiplier.

\section{Partition Function}
The real secret behind getting good at statistical mechanics is just getting fancy with how you take derivative of the partition function. For instances let's look at the specific heat capacity
\begin{align}
    C_v = \frac{\partial \langle E\rangle}{\partial T} &= \frac{\partial}{\partial T}\Big[\frac{1}{Z}\sum_{i=1}^N E_i e^{-E_i/kT}\Big]\\
    &= -\frac{1}{Z^2}\sum_{i=1}^N E_i e^{-E_i/kT}\frac{\partial Z}{\partial T} + \frac{1}{kT^2Z}\sum_{i=1}^N E_i^2 e^{-E_i/kT}
\end{align}
It can be shown that eventually we get an expression that relates the specific heat capacity to the \emph{average fluctuations in energy} which is super weird.
\begin{align}
    \langle \Delta E^2\rangle= C_vkT^2
\end{align}

\section{Ensembles}
Usually when we need statistical mechanics, it is because we care about how fast a block of ice melts or the spreading out of a gas. These objects of interest are called \emph{systems}. %TODO Because statistical mechanics is all about averages, \footnote{Kittel pg. 9}

\subsection{Microcanonical Ensemble}
Here, because the system can have only one energy level, we look for states within that energy $E$, again using Lagrange multipliers with
\begin{align}
    S' = -\sum_{i=1}^N P_i\ln P_i - \alpha\Big[\sum_{i=1}^N P_i -1\Big]
\end{align}
Taking the derivative, we see that
\begin{align}
    \frac{\partial S'}{\partial P_i} &= -\ln P_i  + 1 -\alpha\\
 &= 0
\end{align}
So the probability that the system is in any of it's states with total energy $E$ is given by a constant, with
\begin{align}
    P_i = e^{1-\alpha} = \frac{1}{Z}
\end{align}

%\subsection{Canonical Ensemble}

\subsection{Grand Canonical Ensemble}% \todo{this is garbage}
We now pretend we have a system $A$ with $N_r$ at energy $E_s$ which is free to exchange particles and everything with another system $A'$ at $E_s'$. Under the constraints that
\begin{align}
E_i + E_i' = E^{(0)}\\
N_r + N_r' = N^{(0)}
\end{align}
We have different indices for the energy and particle number because there can be states with differing number of particles $N_r$ which all have the same energy $E_i$. Our task is to find the probability $P_{i,r}$ that the system $A$ is in some given state $E_i$ with number of particles in it $N_r$. It is legitimate to think that this probability should be proportional to the amount of ways you can organizes all the molecules and their energy to get exactly the energy in the \emph{other} system $A'$ to be $E^{(0)} - E_i$
\begin{align}
P_{i,r}(E_i,N_r) \propto \Omega'(E^{(0)} - E_i , N^{(0)}- N_r)
\end{align}
We are also going to pretend the system $A$ is much smaller than the other, so $E_i\ll E^{(0)}$ and $N_r \ll N^{(0)}$, which lets us Taylor expand the number of ways we can arrange the system, after taking the natural log to keep it's extensive nature.
\begin{align}
\ln\Omega'(E^{(0)} - E_i , N^{(0)}- N_r) = \ln\Omega'(E^{(0)},N^{(0)}) - \Big[\frac{\partial\ln\Omega'}{\partial E'}\Big] E_i - \Big[\frac{\partial\ln\Omega'}{\partial N'}\Big]N_r
\end{align}
From here, we identify constants as
%\footnote{There is likely a better way to do all of this, but I haven't found it}
\begin{align}
\Big[\frac{\partial\ln\Omega'}{\partial E'}\Big] = \frac{1}{kT}&&\Big[\frac{\partial\ln\Omega'}{\partial N'}\Big] = -\frac{\mu}{kT}
\end{align}
Exponentiating the expression, we see that
\begin{align}
P_{i,r}\propto\exp\Big[-\beta(E_i-\mu N_r)\Big]
\end{align}
We can then normalize everything to find that
\begin{align}
P_{i,r} = \frac{e^{-\beta(E_i-\mu N_r)}}{\sum_{i,r}e^{-\beta(E_i-\mu N_r)}}
\end{align}

\section{Distributions}
To obtain the distributions, we discretize each of the energies, which is what happens from Quantum Mechanics. For a given energy level of the entire system $E_i$, it should be the sum of the number of particles in each discretize energy level $\epsilon$ multiplied by $\epsilon$ itself.

\begin{align}
    E_i = n_1\epsilon_1 + n_2\epsilon_2 + n_3\epsilon_3 + ...
\end{align}

We also assume that there is some function $\mu$ called the \emph{chemical potential} which tells us how much energy it takes to add another particle to the system. The total energy from the chemical potential $\mu$ is given by
\begin{align}
    \mu N = \mu(n_1 + n_2 + n_3 + ... )
\end{align}
This gives us our full partition function as
\begin{align}
Q= \sum e^{-\beta(n_1\epsilon_1  +  n_2\epsilon_2 +  n_3\epsilon_3 + ...) + \beta\mu(n_1 + n_2 + n_3 + ...)}
\end{align}
Where the different $n$'s are how many particles are in each of the discretized energy levels. We of course also have the constraint that
\begin{align}
n_1 + n_2 + n_3 + ... = N
\end{align}
This partition function is actually huge in general, because each of the $n_1, n_2,$ etc are currently \emph{unbounded} and could be any number. So the sum has to be over all possible combinations of them.
Thankfully however, we are free to break up the partition function however we like as long as we eventually enumerate over all the possible states. To show this, let's pretend that we have only two energy levels $\epsilon_1, \epsilon_2$ making the full partition function
\begin{align}
    Q_T = \sum_{n_1}\sum_{n_2}e^{-\beta[n_1\epsilon_1 + n_2\epsilon_2 - \mu(n_1 + n_2)]} = \sum_{n_1} e^{-\beta(\epsilon_1-\mu)n_1} \sum_{n_2} e^{-\beta(\epsilon_2 - \mu)n_2} = Q_1Q_2
\end{align}
So in fact the total partition function is just the \emph{product} of each of the individual ones. Lets break up the partition function for how many particles $n_k$ are in the $k$th energy level $\epsilon_k$, so
\begin{align}
Q = \prod_k Q_k
\end{align}
This gives us the partition function for the number of particles in each discrete energy level $\epsilon_k$ as
\begin{align}
Q_k = \sum_{n} e^{-\beta(\epsilon_k - \mu)n}
\end{align}
Where we sum over $n$, which can take on as many values as are allowed for that discrete level. From here, we can do some playing around with the mini partition function $Q_k$ to find the average amount of particles in the discretized state $\epsilon_k$ is
\begin{align}
    \langle n_k \rangle = \sum_n nP_n = \frac{1}{Q_k} \sum_n n e^{-\beta(\epsilon_k-\mu)n}
\end{align}
We see that this is basically the same thing as taking the derivative of the partition function $Q_k$. Working everything out, we find

\begin{align}\label{occupation}
\langle n_k \rangle  =\frac{1}{\beta Q_k}\frac{\partial Q_k}{\partial \mu} = \frac{1}{\beta}\frac{\partial \ln Q_k}{\partial \mu}
\end{align}

\subsection{Fermi-Dirac}
In Fermi Dirac statistics, because of the Pauli Exclusion principle, we have to have that
\begin{align}
n_k = 0~\rm{or}~1
\end{align}
I.e. there can only be a maximum of one particle in each of the given energy levels.
The partition function $Q_k$ is calculated looking at all possible permutations of states and values of $n_k$, but there are only two possibilities, giving us
\begin{align}
Q_k= 1 + e^{-\beta(\epsilon_k-\mu)}
\end{align}
To calculate the average number of particles at the given energy level, we can just plug this into equation \ref{occupation} to find the average number of particles at a given energy to be

\begin{align}
\langle n_k \rangle =\frac{1}{e^{\beta(\epsilon_k - \mu)} + 1}
\end{align}
 This makes sense as it will never be less than 1 in most standard cases.

\subsection{Boltzmann}
This is the classical distribution expressed as
\begin{align}
    p_i\propto e^{-\epsilon_i/kT}
\end{align}
\subsection{Bose-Einstein}
Here there can be \emph{any} number of particles in the same state, so the sum goes to infinity. Making the partition function look like
\begin{align}
    Q_k = \sum_{n=0}^\infty e^{-\beta(\epsilon_k - \mu)n} = \sum_{n=0}^\infty \Big(e^{-\beta(\epsilon_k - \mu)}\Big)^n
\end{align}
We recognize that the number inside the exponent $n$ will always be less than $1$, so this is in fact a Geometric series, giving us
\begin{align}
    Q_k = \frac{1}{1 - e^{-\beta(\epsilon_k - \mu)}}
\end{align}
Taking the same derivative with equation \ref{occupation}, we get
\begin{align}
\langle n_k\rangle = \frac{1}{e^{\beta(\epsilon_k-\mu)}-1}
\end{align}
We see that as $\beta = 1/T \rightarrow \infty$ the occupation number will explode and there will be tons of particles in the state $n_k$. Another important fact is that for photons, chemical potential $\mu = 0$.

\subsection{Bose-Einstein Condensate} %\todo{might have some errors here}
Since there is not limit to how many bosons can occupy the same state, there is a certain temperature at which nearly all the particles are "condensed" to the lowest possible state. This is the Bose- Einstein Condensate. The math to figure this out is very tricky. How we will find this temperature is first looking at the average number of molecules in all the excited states. We do this by simply adding up the average number of particles in each energy level. In integral form
\begin{align}
    N_e = \int d\epsilon_i \frac{g_i}{e^{\beta(\epsilon_i - \mu)} -1}
\end{align}
Where we have already used the average number of particles in each energy level as
\begin{align}
    \langle n_i\rangle = \frac{1}{e^{\beta(\epsilon_k-\mu)}-1}
\end{align}
The real interesting thing about the Bose Einstein condensate come from the \emph{dimensionality of the phase space}. We will get different results for the critical temperature depending on the phase space allocated to the particles themselves. Let's calculate the the number of particles in the excited state in three dimensions, and just normal kinetic energy
\begin{align}
    N_e &= \frac{1}{h^3}\int dp^3\int dx^3 \frac{1}{e^{\beta(p^2/2m - \mu)} -1}\\
    &=\frac{4\pi V}{h^3} \int_0^\infty  dp \frac{p^2}{e^{\beta(p^2/2m - \mu)} -1}
\end{align}
Remember, the ground state does \emph{not} contribute to this sum since the energy is zero. The integral looks quite intimidating, so we have to use some tricks to evaluate it. Let's start by factoring out the denominator
\begin{align}
    N_e &= \frac{4\pi V}{h^3} \int dp \frac{p^2}{e^{\beta(p^2/2m - \mu)}}\frac{1}{1-e^{-\beta(p^2/2m - \mu)}}
\end{align}
Now the energy is always going to be greater than the chemical potential $\mu$, so the right term is in fact a Geometric series, in the same way we first wrote the Bose - Einstein distribution
\begin{align}
    N_e &= \frac{4\pi V}{h^3} \int dp \frac{p^2}{e^{\beta(p^2/2m - \mu)}}\sum_{n=0}^\infty e^{-\beta n(p^2/2m - \mu)}\\
    &=\frac{4\pi V}{h^3} \int dp ~p^2 \sum_{n=0}^\infty e^{-\beta (n+1)(p^2/2m - \mu)}
\end{align}
Now we shift the order of the sum and the integral, and shift the start of the sum, so we get
\begin{align}
    N_e &= \frac{4\pi V}{h^3}\sum_{n=1}^\infty e^{\beta n\mu}\int_0^\infty dp~p^2  e^{-\beta n p^2/2m}
\end{align}
We can evaluate this integral just doing our normal Gaussian integration after taking the derivative (see the math section) to find
\begin{align}
    N_e &= \frac{4\pi V}{h^3}\sum_{n=1}^\infty e^{\beta n\mu} \frac{\sqrt{\pi}}{4}\Big(\frac{2m}{\beta n}\Big)^{3/2}\\
    &= V\Big(\frac{2\pi m}{h^2\beta}\Big)^{3/2}\sum_{n=1}^\infty \frac{e^{\beta n\mu}}{n^{3/2}}
\end{align}
Since the ground state energy is zero, and it is necessary that $\epsilon - \mu > 0$ for all energies, we need that
\begin{align}
    \mu < 0
\end{align}
Looking the largest possible value this sum could be (where $\mu = 0$), we get the definition of the Riemann Zeta function (See ...)
\begin{align}
    N_e = V\Big(\frac{2\pi m}{h^2\beta}\Big)^{3/2}\sum_{n=1}^\infty \frac{1}{n^{3/2}} &= \Big(\frac{2\pi m kT}{h^2}\Big)^{3/2} \zeta(3/2)
\end{align}
So this is the \emph{maximum} number of particles that we can fit into a volume $V$ at temperature $T$ in the excited states when there is no chemical potential between the molecules, meaning we are free to add or subtract as many as we want. The question becomes, where do they go? Into the ground state. We must have that
\begin{align}
    N = N_0 + N_e
\end{align}
The first fill up the excited states before condensing to the ground state. We defined a critical temperature at which all the particles go into the excited states before the condensate starts to form with %\todo{fix}
\begin{align}
    N &= \Big(\frac{2\pi m kT_c}{h^2}\Big)^{3/2} \zeta(3/2)
\end{align}
Which let's us find the critical temperature with
\begin{align}
    T_c = \Big(\frac{N}{V\zeta(3/2)}\Big)^{2/3} \frac{2\pi\hbar^2}{mk}
\end{align}
Thus
\begin{align}
    N_0 = N\Big[1-\Big(\frac{T}{T_c}\Big)^{3/2}\Big]&& T < T_c
\end{align}
With these types of integrals we will \emph{always} get something with the Zeta function, the smallest dimensionality that barely won't converge is
\begin{align}
    \zeta(1) = 1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + ...
\end{align}
Which is the Harmonic series, which does not converge, which tells us that the Bose-Einstein condensate cannot form in one dimension. Apparently the evolution of many complex systems, including the World Wide Web follow Bose statistics and can under go Bose-Einstein condensation \cite{biancoli}.

\subsection{Stefan-Boltzmann Law}
From the partition function for a photon gas, we can get a function for the average energy of a photon gas as usual. Remember that the energy of a photon is $\epsilon = |\textbf{p}|c$, since they don't have mass. Eventually we find that the power, which is energy per unit time goes as
\begin{align}
    P = A\varepsilon \sigma T^4
\end{align}
This tells us the power radiated from a black-body, (which just means it absorbs everything that hits it) is proportional to the temperature to the fourth power. $A$ is the surface area of the object, $\varepsilon$ is the emissivity, which is 1 if it is a perfect blackbody, and $\sigma$ is a constant from the partition function.

\subsection{Gas - Liquid - Solids}
An Einstein solid assumes all atoms oscillate in all directions with the same frequency $\omega$. We can easily calculate the partition function for one of particles in one of the dimensions, knowing that the energy goes as a harmonic oscillator
\begin{align}
    Z_0 &= \sum_{n=0}^\infty e^{-\beta\hbar\omega(n+1/2)}\\
    &= e^{-\beta\hbar\omega/2} \sum_{n=0}^\infty e^{-\beta\hbar\omega n}\\
    &= \frac{e^{-\beta\hbar\omega/2}}{1-e^{-\beta\hbar\omega}}\\
    &= \frac{1}{2\sinh(\beta\hbar\omega/2)}
\end{align}
We assume all of these oscillators are independent, so to get the full wave function, we just have to exponentiate this 3 times, for each dimension of the single atom, then $N$ to consider all of the atoms in the solid.
\begin{align}
    Z = Z_0^{3N}
\end{align}
Einstein solid particles are said to be distinguishable, so the partition function is not weighted by $1/N!$

\subsection{Chemical Potential}
We know that if we add up the average number of particles in each energy level, we should simply get back the total number of particles, which is a constant if particles are not allowed to leave. For both Fermi and Bose statistics we have
\begin{align}
    N = \sum_{i=0}^\infty \frac{1}{e^{\beta(\epsilon_i - \mu)} \pm 1}
\end{align}
 Let's first consider the Fermi case (with the plus sign). If we look at zero temperature, or $\beta = \infty$, we see that if
\begin{align}
    \epsilon_i < \mu  && n_i \sim \frac{1}{e^{-\infty} +1} = 1\\
    \epsilon_i > \mu  && n_i \sim \frac{1}{e^{\infty} +1} = 0
\end{align}
Trying the same thing for the Bose case
\begin{align}
    \epsilon_i < \mu  && n_i \sim \frac{1}{e^{-\infty} -1} = -1
\end{align}
We obviously can't have an expectation value of a negative particle
%\footnote{Could this actually account for antiparticles?}
, so the previous expression is wrong, the only valid energies must be
\begin{align}
    \epsilon_i > \mu && n_i\sim \frac{1}{e^{\infty} -1} = 0
\end{align}
But since we need to conserve particle number, we must have that all the particles  have the same energy with
\begin{align}
    \epsilon_i = \mu && n_i = N
\end{align}
This is another way to see Bose-Einstein condensation. %\todo{Verify this last part.}

\subsection{Averages}
For some reason if we want to find the average of a quantity that obeys Fermi or Bose statistics, we can use
\begin{align}
    \langle A \rangle = \frac{1}{(2\pi\hbar)^3}\int d^3p \int d^3r A~\frac{g(E_{pr})}{e^{\beta(E_{pr} - \mu)}\pm 1}
\end{align}

\section{Fermi Energy}
Within a box of length $L = L_x = L_y = L_z$, from quantum mechanics, we know we can expression the energy of any given state as
\begin{align}
E = \frac{\hbar^2\pi^2}{2mL^2}\Big(n_x^2+n_y^2+n_z^2\Big)
\end{align}
We can see these energy levels are degenerate, so it is conventional to imagine something called the "Fermi Sphere" which shifts the $n$'s to spherical geometry, with
\begin{align}
n^2 = n_x^2 + n_y^2+n_z^2
\end{align}
Since the $n$'s must always be positive, we take only the positive quadrant, so $1/8$th of the sphere. Since an electron can be in two spin states, this gives the total amount of electrons in the spherical portion as
\begin{align}
N = 2\cdot\frac{1}{8}\cdot\frac{4}{3}\pi n_F^3
\end{align}
Where $n_F$ is the maximum in magnitude occupied state.
Rearranging to solve for this maximum magnitude, we have that
\begin{align}
n_F = \Big(\frac{3N}{\pi}\Big)^{1/3}
\end{align}
The Fermi energy is defined as the energy at this maximum occupied state, so plugging in we have
\begin{align}
E_F &= \frac{\hbar^2\pi^2}{2mL^2}\Big(\frac{3N}{\pi}\Big)^{1/3}\\
&=\frac{\hbar^2}{2m}\Big(\frac{3\pi^2N}{V}\Big)^{2/3}
\end{align}

\section{Liouville Theorem}
We first imagine that we have some arbitrary number of systems that obey the same Hamitonian (a ton of harmonic oscillators for example) that are each started with different initial conditions (one is moving at $5~m/s$ started at the origin, one starts at $\theta = \pi/2$ with no speed, ...) we can take note of each of their positions $x_i$ and momenta $p_i$ and draw them all together in phase space. When drawn together we get something that looks like a density $\rho$ that is all the points plotted together, with more in one location than another etc. What we look for is how this density changes in time from the continuity equation, with
\begin{align}
\frac{d\rho}{dt} &= \frac{\partial\rho}{\partial t} + \nabla\cdot(\rho\textbf{v})\\
&=\frac{\partial\rho}{\partial t} + \sum_{i=1}^n \frac{\partial (\rho x_i)}{\partial x_i}\dot{x}_i + \frac{\partial (\rho p_i)}{\partial p_i}\dot{p}_i
\end{align}

This theorem turns out to be valid for both equilibrium and non-equilibrium systems.
%\todo{finish!}

\section{Ising Model}
Used to model spin systems which have a favored state. Each object has a given "spin" ($s_1, s_2, ..., s_N$) and has an interaction energy with its neighbors.

% TODO $$picture$$

The Hamiltonian is given by
\begin{align}
H = -\varepsilon\sum_{i=1}^N s_is_{i+1} - \sum_{i=1}^Nh s_i
\end{align}
We see that when the spins are aligned (directions are the same), we minimize the energy, thus alignment is favorable. The second term is just the interaction of the spin itself with the magnetic field. Using the mean field approximation, we just pretend they are all uncorrelated and get

\begin{align}
h' &= \varepsilon\nu \langle s\rangle + h\\
\implies H &= -\sum_{i=1}^N h's_i
\end{align}

Where $\nu$ is the number of nearest neighbors (i.e. in a 2D square lattice, it is 4). %TODO Will's thoughts - you have an field created by the thing $h$ which is created by the totality of the spins, and the interation between the spins given by the first terms... This is why there is hysteresis for ferromagnets. Feynman might have something good for this?
Expanding

\begin{align}
m = \tanh\Big(\frac{h + \varepsilon\nu m}{T}\Big)
\end{align}
With $T\approx T_c$, $h=0$, $m\ll 1$ We get
\begin{align}
m = \sqrt{3}\Big(1-\frac{T}{T_c}\Big)^{1/2}
\end{align}
Where $T_c = \varepsilon\nu$. Actual $T_C = 2.269\varepsilon$ in 2D, but our approximation gives us that it is $\nu/2 = 4/2 = 2$ considering only the nearest neighbors. % TODO Check Kamil's notes

%TODO
%\section{Brownian Motion}
%A botanist named Robert Brown first noticed that particles will jiggle around, eventually moving an appreciable amount from where they started, but didn't know why. In 1905, Einstein published a paper explaining this mechanism as a product atomic motion. We first look at the density of particles (here in one dimension) as a function of position and time. In a small time, we can take the first order in the Taylor expansion, with
%\begin{align}
%   \rho(x,t + \tau) = \rho(x,t) + \tau\frac{\partial\rho}{\partial t} + ...
%\end{align}
%Our goal is to somehow relate this to how far they go, and match what Brown had seen. To do this we look at ... expand probability density
%
%\begin{itemize}
%\item \begin{align}
% \frac{dv(t)}{dt} = -\frac{1}{\tau} v(t) + \Gamma(t)
% \end{align}
%
%\item Diffusion, EInstein Relation
%\item Solving for velocity as a function of time, given some arbitrary stocastic force $\Gamma(t)$
%\item Obtained using Green's functions...
%\begin{align}
% v(t) = v(0)e^{-t/\tau} + e^{-t/\tau}\int_0^t dt' e^{t'/\tau}\Gamma(t')
% \end{align}
% \item This gives us that
% \begin{align}
% \langle v(t)^2\rangle &= v(0)^2 e^{-2t/\tau} + 2v(0)e^{2t/\tau}\int dt'\langle  \Gamma \rangle + e^{-2t/\tau}\int_0^t dt'\int_0^t dt'' e^{(t'+t'')/\tau} \langle \Gamma(t')\Gamma(t'')\rangle
% \end{align}
% The second term is zero since the $\langle \Gamma(t)\rangle = 0$. But we now have to find the correlation function between the stocastic term. We should have that the function is uncorrelated with itself after some significant amount of time has passed $s \ge \tau_0$, since it is a random function.
% \begin{align}
% \langle \Gamma(t)\Gamma(t+s)\rangle = \langle \Gamma\rangle\langle\Gamma\rangle = 0
% \end{align}
%
% Thus we can Taylor expand this integral since the correlation happens only when the times in question are close to each other.
% \begin{align}
% \int_0^t dt'\int_0^t dt'' e^{(t'+t'')/\tau} \langle \Gamma(t')\Gamma(t'')\rangle &= \int_0^t dt'\int^{-t'+t}_{-t'} du e^{(2t'+u)/\tau}\langle \Gamma(t')\Gamma(t'+u)\rangle\\
% &=
% \end{align}
%
%\begin{align}
%\langle v^2(t)\rangle &= v^2(0)e^{-2t/\tau} + \frac{\tau}{2}(1-e^{-2t/\tau})\int_{-\infty}^\infty du \langle \Gamma(t)\Gamma(t+u)\rangle
%\end{align}
%But we know that
%\begin{align}
%\frac{1}{2}m\langle v^2(t)\rangle &= \frac{3}{2}kT
%\end{align}
%Plugging everything in, you get (might have another factor of $k$)
%\begin{align}
%\int_{-\infty}^\infty du \langle \Gamma(0)\Gamma(u)\rangle  = \frac{6Tm}{\tau}
%\end{align}
%
%
%
%\end{itemize}
%\section{TODO}
%
%\begin{itemize}
%
%
%\item Laser works because normally, for high temperatures, all states are equally likely, but we can pump extra stuff into higher states with lasers, which are non-equilibrium states, then weird stuff happens...
%
%
%
%\item Joule Thompson Coefficient / Process
%\item Poisson Distribution -Grand Canonical Ensemble
%\item Hysteresis in Ising model with Magnetization
%
%\item Pg 43 Schroeder
%\item Density of air molecules, Comp 2011, 6
%\end{itemize}
%


\documentclass{article}

\usepackage[preprint]{./template/neurips_2020}

\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc}    % use 8-bit T1 fonts
\usepackage{hyperref}       % hyperlinks
\usepackage{url}            % simple URL typesetting
\usepackage{booktabs}       % professional-quality tables
\usepackage{amsfonts}       % blackboard math symbols
\usepackage{nicefrac}       % compact symbols for 1/2, etc.
\usepackage{microtype}      % microtypography
\usepackage{amssymb}
\usepackage{physics}
\usepackage{amsmath}

\title{Chapter 2: Introduction to Quantum Mechanics Examples}

\author{
  John Martinez \\
  \texttt{john.r.martinez14@gmail.com} \\
  % examples of more authors
  % \And
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
  % \AND
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
  % \And
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
  % \And
  % Coauthor \\
  % Affiliation \\
  % Address \\
  % \texttt{email} \\
}

\begin{document}
\maketitle

%\begin{abstract}
%  The abstract paragraph should be indented \nicefrac{1}{2}~inch (3~picas) on
%  both the left- and right-hand margins. Use 10~point type, with a vertical
%  spacing (leading) of 11~points.  The word \textbf{Abstract} must be centered,
%  bold, and in point size 12. Two line spaces precede the abstract. The abstract
%  must be limited to one paragraph.
%\end{abstract}
\section{Intro to Quantum Mechanics}
\subsection{Linear Dependence}
Show that $(1, -1), (1, 2)$ and $(2, 1)$ are linearly dependent.

\textbf{Solution}

The solution to the linear system is $x = y = -z$.  So let
$x = y = 2, z = -2$ then $ 2(1, -1) + 2(1, 2) - 2(2, 1) = (0, 0)\blacksquare$

\subsection{Matrix Representations}
Suppose $V$ is a vector space with basis vectors $\ket{0}$ and $\ket{1}$, and
$A$ is a linear operator from $V$ to $V$ such that $A\ket{0} = \ket{1}$
and $A\ket{1} = \ket{0}$. Give a matrix representation for $A$, with respect
to the input basis $(\ket{0}, \ket{1})$ and the output basis
$(\ket{0}, \ket{1})$.

\textbf{Solution}

$\sigma_{x} \equiv X \equiv \begin{bmatrix} 0 & 1 \\ 1 & 0\end{bmatrix}$ will
flip a qubit in the standard basis. Let $\ket{0} =
\begin{bmatrix}1 \\ 0\end{bmatrix}$ and let $\ket{1} =
\begin{bmatrix}0 \\ 1\end{bmatrix}$ then $X\ket{0} =
\begin{bmatrix}0 & 1 \\ 1 & 0\end{bmatrix}\begin{bmatrix} 1 \\ 0\end{bmatrix} =
\begin{bmatrix}0 \\ 1\end{bmatrix}$ and $X\ket{1} =
\begin{bmatrix}0 & 1 \\ 1 & 0\end{bmatrix}\begin{bmatrix} 0 \\ 1\end{bmatrix} =
\begin{bmatrix}1 \\ 0\end{bmatrix}\blacksquare$

\subsection{Matrix Representation for Operator Products}
Suppose $A$ is a linear operator from vector space $V$ to vector space $W$,
and $B$ is a linear operator from vector space $W$ to vector space $X$.
Let $\ket{v_{i}}, \ket{w_{j}}$ and $\ket{x_{k}}$ be bases for the vector
spaces $V$, $W$, and $X$, respectively. Show that the matrix
representation for the linear transformation $BA$ is the matrix product
of the matrix representation for $B$ and $A$, with respect to the appropriate
bases.

\textbf{Solution}
$\blacksquare$

\end{document}


\chapter{Theoretical Analysis of Domain Adaptation with Sinkhorn Divergence}

In this chapter, we discuss some of our first results when using a entropic-based divergence. In the past, generalization bounds were provided with respect to the Wasserstein metric, but the empirical computation involved a regularization step, which was previously unaccounted for when deriving bounds. In this work, we provide a theoretical analysis of the generalization bound with respect to the entropic regularization in the unsupervised domain adaptation setting.

\section*{Why Use Optimal Transport in Domain Adaptation?}
Optimal transport is capable of taking into consideration the geometry of the data. In domain adaptation problems, this is helpful, especially since when dealing with a source and target distribution, a natural idea is to look for a nonlinear transformation between the two distributions. This makes optimal transport distances (i.e. Wasserstein) highly promising. Another concern is that the source and target distributions lack a shared support. It makes sense to use a distance that does not require a shared support and the Wasserstein is one such distance. This property distinguishes it from other divergences such as Maximum Mean Discrepancy or Kullback-Leibler, which usually require a common support.

\section{Notation and Preliminaries}

\begin{definition}{Reproducing Kernel Hilbert Space}

	Let $X$ be an arbitrary set and $H$ a Hilbert space of real-valued functions on $X$.  The evaluation functional over the Hilbert space of functions $H$ is a linear functional that evaluates each function at a point $x$,

	\[
	L_{x} : f \mapsto f(x)  \text{   } \forall f \in H.
	\]

	We say that $H$ is a reproducing kernel Hilbert space if, for all $x \in X$, $L_{x}$ is continuous at any $f\in H$ or, equivalently, if $L_x$ is a bounded operator on $H$, i.e. there exists some $M > 0$ such that

	\[
	|L_{x}(f)| := |f(x)| \le M \|f\|_H \quad \forall f \in H.
	\]

\end{definition}

\begin{definition}{Kullback-Leibler Divergence}
	If $P$ and $Q$ are probability measures on a set $\mathcal{X}$, and $P$ is absolutely continuous with respect to $Q$, then the Kullback-Liebler divergence from $Q$ to $P$ is
	\[
	D_{KL}(P \parallel Q)=\int_{\mathcal{X}} \log \left( \frac{dP}{dQ}\right) \, dP.
	\]
\end{definition}

\begin{definition}{Maximum mean discrepancy}

	MMD represents distances between distributions as distances between mean embeddings of features. If we have distributions $p$ and $q$ over a set $\mathcal{X}$, the MMD is defined by a feature map $\varphi: \mathcal{X}\to \mathcal{H}$ where $\mathcal{H}$ is a reproducing kernel Hilbert space.
	\[
	MMD(p,q) = \norm{ \E_{X\sim p}[\varphi(X)] - \E_{Y\sim q}[\varphi(Y)] }_{\mathcal{H}}
	\]

	We can alternatively characterize the MMD as follows:
	\begin{align*}
	MMD(p,q) &= \norm{ \E_{X\sim p}[\varphi(X)] - \E_{Y\sim q}[\varphi(Y)] }_{\mathcal{H}} \\
	&= \sup_{f\in \mathcal{H}:\norm{f}_{\mathcal{H}}\leq 1} \ang*{f,\E_{X\sim p}[\varphi(X)] - \E_{Y\sim q}[\varphi(Y)]}_{\mathcal{H}} \\
	&= \sup_{f\in \mathcal{H}:\norm{f}_{\mathcal{H}}\leq 1} \bkt*{\ang*{f,\E_{X\sim p}[\varphi(X)]}_{\mathcal{H}} - \ang*{f,\E_{Y\sim q}[\varphi(Y)]}_{\mathcal{H}} } \\
	&= \sup_{f\in \mathcal{H}:\norm{f}_{\mathcal{H}}\leq 1} \bkt*{ \E_{X\sim p}[f(X)] - \E_{Y\sim q}[f(Y)] }
	\end{align*}
\end{definition}

The alternative characterization holds because of the reproducing property:
$\ang{f,\varphi(x)}_{\mathcal{H}}=f(x)$ for any
$f\in \mathcal{H}$. The second line holds since $\sup_{f:\norm{f}\leq 1} \ang{f,g}_{\mathcal{H}} = \norm{g}$ is attained when $f=g/\norm{g}$. The fourth relies on Bochner integrability, but assuming our kernel or distributional support is bounded, this is true. The last line is a byproduct of the reproducing property.

The following extension of Wasserstein to empirical measures will be used when contrasting empirical to theoretical distances.

\begin{definition}{Discrete Wasserstein} \cite{Redko2017}

	If we deal with empirical measures $\hat{\mu}_{S}=\frac{1}{N_{S}}\sum_{i=1}^{N_{S}}\delta_{x_{\mathrm{s}}^{i}}$ and $\hat{\mu}_{T}=\frac{1}{N_{T}}\sum_{i=1}^{N_{T}}\delta_{x_{T}^{i}}$ represented by the uniformly weighted sums of $N_{S}$ and $N_{T}$ Diracs with mass at locations $x_{S}^{i}$ and $x_{T}^{i}$ respectively, then the Kantorovich problem is defined in terms of the inner product between the coupling matrix $\gamma$ and the cost matrix $C$:

	$$
	W_{1}(\hat{\mu}_{S},\hat{\mu}_{T})=\min_{\gamma\in\Pi(\hat{\mu}_s,\hat{\mu}_{T})} \langle C,\ \gamma\rangle_{F}
	$$

	where $\langle \, , \, \rangle_F$ denotes the Frobenius inner product, $\Pi(\hat{\mu}_s,\hat{\mu}_{T})=\{\gamma\in \mathbb{R}_{+}^{N_{\mathrm{S}}\times N_{T}}|\gamma 1=\hat{\mu}_{S}, \gamma^{T}1= \hat{\mu}_{T}\}$ is a set of doubly stochastic matrices and $C$ is a dissimilarity matrix, i.e., $C_{ij}= c(x_{S}^{i},\ x_{T}^{j})$, defining the energy needed to move a probability mass from $x_{S}^{i}$ to $x_{T}^{j}$.

\end{definition}

\begin{definition}[Expected Loss]
	Let $l$ be a convex loss-function. Given a distribution $\mu_D$, a hypothesis $h\in H$ and a labeling function $f_D$ (which may be a hypothesis), the expected loss is defined as

	\[
	\epsilon_D(h,f_D) = \E_{X\sim \mu_D}[l(h(x),f_D(x))].
	\]
\end{definition}

\medskip

Our source and target spaces are denoted by $S$ and $T$ respectively. $S$ has a distribution $\mu_S$ and $T$ has as its underlying distribution, $\mu_T$. Our loss function is denoted by $\ell: \mathbb{R}\times \mathcal{Y}\to \mathbb{R}^{+}$.

We also use a $\Gamma$ operator to represent expectation, i.e. $\Gamma f = E_{\pi}[f(X)]$ where $X\sim \pi$.

\medskip

The following interpretation of expected loss using RKHS properties was used to derive earlier bounds.

\begin{definition}[RKHS interpretation]
	Assume $l \in \mathcal{H}_{k^q}$ where $\mathcal{H}_{k^q}$ is an RKHS with kernel $k^q: \Omega\times \Omega\to \mathbb{R}$ induced by $\phi: \Omega\to \mathcal{H}_{k^q}$ and $k^q(x,y) = \ang*{\phi(x),\phi(y)}_{\mathcal{H}_{k^q}}$.

	With this definition, it is immediate that,
	$$ \epsilon_S(h,f_S) = \E_{x\sim \mu_S}\bkt*{l(h(x),f_S(x))} = \E_{x\sim \mu_S}\bkt*{\ang*{\phi(x),l}_{\mathcal{H}_{k^q}}} $$

	and

	$$ \epsilon_T(h,f_T) = \E_{y\sim \mu_T}\bkt*{l(h(y),f_T(y))} = \E_{y\sim \mu_T}\bkt*{\ang*{\phi(y),l}_{\mathcal{H}_{k^q}}} .$$
\end{definition}

\section*{Prior Work}

First, we introduce some past results pertaining to risk bounds with respect to the Wasserstein distance. As we will see, the choice of the cost function is key in deriving theoretical bounds.

\subsection*{First Theoretical Bounds \cite{Redko2017}}
The key assumption here is that of the cost function. Another assumption here is that the true labeling function $f$ lies within a unit ball of an RKHS, i.e. \[\mathcal{F} = \{f\in \mathcal{H}_k: \, \norm{f}_{\mathcal{H}_k}\leq 1\},\] where $\mathcal{H}_k$ is an RKHS with kernel $k$.

In this scenario, we have the following specifications:
\begin{itemize}
	\item Let $\mu_S, \mu_T\in \mathcal{P}(X)$ be two probability measures on $\mathbb{R}^d$.
	\item \[c(x,x') = \norm*{\phi(x)-\phi(x')}_{\mathcal{H}_{k_l}}.\]
	\begin{itemize}
		\item $\mathcal{H}_{k_l}$ is an RKHS.
		\item $k_l: \Omega\times \Omega\to \mathbb{R}$ is a kernel function such that \[k_l(x,x')=\ang*{\phi(x),\phi(x')}_{\mathcal{H}_{k_l}}\]
	\end{itemize}
	\item \[l(h(x),f(x)) = \abs*{h(x)-f(x)}^q,\, q>0\] and thus the loss function is convex, bounded, symmetric, and satisfies triangle inequality
	\item If $\Omega$ is separable and $k_l(x,x')\in [0,K]$ for some $K\in \mathbb{R}$, then for all $x,x'\in \Omega$,
	\[
	\E_D[\sqrt{k_l(x,x')}]<\infty
	\] for $D=S$ or $T$.
\end{itemize}

Before we continue, let us briefly discuss the use of the aforementioned cost function. One sees that

\begin{align*}
c(x,x')&=\norm{\phi(x)-\phi(x')}_{\mathcal{H}} \\
&= \sqrt{\ang{\phi(x)-\phi(x'),\phi(x)-\phi(x')}_{\mathcal{H}}} \\
&= \sqrt{k(x,x)-2k(x,x')+k(x',x')}.
\end{align*}

One can show there is a one-to-one relationship between the choice of a positive-definite kernel $k$ and the cost function $c$.

Secondly, $l_{h,f}:x\to l(h(x),f(x))$ belongs to an RKHS. $(h,f)\in \mathcal{F}^2$ and $l$ is a nonlinear mapping of $\mathcal{H}_k$.

\medskip

\begin{lemma}
	If the above assumptions hold, then, for all $h,f\in \mathcal{H}_{k_l}$,
	\[ \epsilon_T(h,f)\leq \epsilon_S(h,f) + W_1(\mu_S,\mu_T). \]
\end{lemma}

With the use of a concentration inequality on Wasserstein distances \cite{Bolley2007}, empirical risk bounds are obtained.

\begin{theorem}
	Let $\mu$ be a probability measure on $\mathbb{R}^d$ such that
	\[
	\int_{\mathbb{R}^d} e^{\alpha \norm{x}^2}\,d\mu < \infty, \, \exists \alpha > 0
	\]
	and let $\hat{\mu}=\frac{1}{N}\sum_{i=1}^N \delta_{X_i}$ be the empirical measure on $\{x_i\}$.

	Then for all $d'>d$ and all $\xi < \sqrt{2}$, there exists $N_0(d')$ and $\alpha > 0$ with
	\[
	\int e^{\alpha c(x,x')}\,d\mu(x) < \infty
	\]
	for a fixed $x'$ such that for all $\epsilon > 0$ and all $N\geq N_0 \max \{\epsilon^{-(d'+2),1}\}$,

	\[
	\Pr[W_1(\mu,\hat{\mu})>\epsilon] \leq e^{-\frac{\xi}{2} N\epsilon^2}
	\]
\end{theorem}

\subsection*{Joint Distribution Domain Adaptation (JDOT)}
This is the approach taken in \cite{Courty2017} and also the problem we study in the rest of this chapter with regularization. In this setting, one works with unsupervised domain adaptation between joint distributions. The inspiration behind this method is that the assumption that  conditional distributions are preserved, i.e. $P_S(Y|T(X))\approx P_T(Y|T(X))$, may not necessarily hold.
\begin{itemize}
	\item Now the cost function used is \[\alpha d(x_s,x_t) + L(y_s,y_t).\]
	\item The unsupervised domain adaptation problem is studied here, so the target labels represented by $y_t$ are not known. Thus, one cannot find an optimal coupling.
	\item It does not matter that an optimal coupling is not found since the goal here is to estimate a mapping on the target data.
\end{itemize}
%%EDIT THIS MORE
%fix citation

%end
\section*{Uniform empirical risk bounds on RKHS}
Using properties of the Rademacher complexities of functions in an RKHS, we can avoid bounding empirical risks w.r.t Wasserstein distance.

$$ \sup_{f\in F} \abs{\E f(X) - \frac{1}{n}\sum_{i=1}^n f(X_i)} = \norm{P-P_n}_F. $$

$$ \E \norm{P-P_n}_{F}\leq 2\norm{R_n}_F $$

Let $F = \left\{f\in \mathcal{H} | \norm{f}_{H}\leq B\right\}$

$$ \E \bkt*{\norm{R_n}_{F} | X_1,\ldots,X_n} \leq \frac{B}{\sqrt{n}} \sqrt{\frac{tr(K)}{n}}. $$

\begin{proof}
	\begin{align*}
	\norm{R_n}_F &= \sup_{f\in F} \abs*{ \frac{1}{n} \sum_{i=1}^n \epsilon_i f(X_i)} \\
	&= \sup_{f\in F} \abs{\frac{1}{n} \sum_{i=1}^n \epsilon_i \ang*{k(X_i,\cdot),f}} \\
	&= \sup_{f\in \mathcal{H}:\norm{f}_{\mathcal{H}}\leq B} \abs*{\ang*{\frac{1}{n}\sum_{i=1}^n \epsilon_i k(X_i,\cdot),f}} \\
	&= B\sqrt{\frac{1}{n^2} \sum_{i,j} \epsilon_i \epsilon_j k(X_i,X_j)}
	\end{align*}

	By Jensen's

	\begin{align*}
	\E \bkt*{ \norm{R_n}_F | X_{1}^{n}} &\leq B\sqrt{\E \bkt*{\frac{1}{n^2} \sum_{i,j} \epsilon_i \epsilon_j k(X_i,X_j)|X_{1}^{n}}} \\
	&= B \sqrt{\frac{1}{n^2} \sum_i k(X_i,X_i)} \\
	&= \frac{B}{\sqrt{n}} \sqrt{\frac{tr(K)}{n}}
	\end{align*}
\end{proof}

%\begin{align*}
%\norm{R_n}_F &= \sup_{f\in F} \abs*{ \frac{1}{n} \sum_{i=1}^n \epsilon_i f(X_i) \\
%	&= \sup_{f\in F} \abs{\frac{1}{n} \sum_{i=1}^n \epsilon_i \ang*{k(X_i,\cdot),f}} \\
%	&= \sup_{f\in \mathcal{H}:\norm{f}_{\mathcal{H}}\leq B} \abs*{\ang*{\frac{1}{n}\sum_{i=1}^n \epsilon_i k(X_i,\cdot),f}}
%\end{align*}

Taking expectations of both sides,
$$ \E \bkt*{\norm{R_n}_{F}}\leq \frac{B}{n}\E \bkt*{\sqrt{tr(K)}} $$

and if our kernel $k$ is bounded above by $M$, then
$$ \E \bkt*{ \norm{R_n}_F } \leq \frac{B}{n} \sqrt{nM} = B\sqrt{M/n}. $$

\section{Generalization Bounds}
\label{generalization-bounds}

Our claim is that we can get guarantees when doing a dual minimization of the Wasserstein distance with the specified cost function $\alpha d(x_s,x_t) + l(y_s,f(x_t))$ from $S$ to $T_{f}$.
\[
(\hat{\Gamma},\hat{f})=\arg\min_{\Gamma,f}\Gamma\left[\alpha d(x_{s},x_{t})+l(y_{s},f(x_{t}))\right]
\]
over all couplings that preserve the marginals on $S$ ($x_{s},y_{s})$ and $T_{X}$ $(x_{t})$. Let $\hat{\Gamma}$ be the optimal joint distribution over $(x_{s},y_{s},x_{t})$ above. Now define $\hat{\hat{\Gamma}}$ on $(x_{s},y_{s},x_{t},y_{t})$ to be a coupling with the same marginal over $(x_{s},y_{s},x_{t})$ as $\hat{\Gamma}$ and the correct marginals on $T$ ($x_t,y_t$). Let $\Gamma^{*}$ be an arbitrary coupling on the same variables which preserves the marginal distributions on $S$ ($x_{s},y_{s}$)and $T$ (i.e., $x_{t},y_t$). For now, let's assume we're dealing with absolutely continuous distributions here so we don't have existence issues. Fix $f^*\in F$.

The main results in this paper are bounds on the expected loss with respect to Sinkhorn and Wasserstein divergences.

\subsection{Error bounds with respect to Wasserstein distance}
\begin{theorem}
	\[
	err_{T}(\hat{f}) \leq err_{T}(f^{*})+W(S,T)+\hat{\hat{\Gamma}}\left(-\alpha d(x_{s},x_{t})+l(y_{s},y_{t})\right)
	\] where $\hat{\hat\Gamma}$ is the expectation operator with respect to a coupling with matching marginals on $T$ and the same marginal on $(x_s,y_s,x_t)$ as $\hat{\Gamma}$.
\end{theorem}

\begin{proof}
	\begin{align*}
	err_{T}(\hat{f}) &= \hat{\hat{\Gamma}} l (y_t,\hat f(x_t)) \\
	& \leq \Gamma^{*}\left[\alpha
	d(x_{s},x_{t})+l(y_{t},f^{*}(x_{t}))+l(y_{t},y_{s})\right]
	- \hat{\hat{\Gamma}}\left[\alpha d(x_{s},x_{t})-l(y_{s},y_{t})\right] \\
	& = err_{T}(f^{*})+\alpha\left(\Gamma^{*}d(x_{s},x_{t})
	-\hat{\hat{\Gamma}}d(x_{s},x_{t})\right)
	+\Gamma^{*}l(y_{s},y_{t})+\hat{\hat{\Gamma}}l(y_{s},y_{t}).
	\end{align*}

	There, by choosing $\Gamma^*$ as the optimal coupling between $S$ and $T$, that is,
	\[
	W(S,T) = \Gamma^*\left(\alpha d(x_{s},x_{t}) +
	l(y_{s},y_{t})\right),
	\]
	then we get the upper bound
	\begin{align*}
	err_{T}(\hat{f})
	& \leq err_{T}(f^{*})+W(S,T)+\hat{\hat{\Gamma}}\left(-\alpha
	d(x_{s},x_{t})+l(y_{s},y_{t})\right).
	\end{align*}
\end{proof}

\subsection{Entropic Regularization}

One means of addressing the intractability of OT is by using an entropic regularization.

\begin{definition}[Entropic Regularization of Wasserstein]
	$$W_{\epsilon}(\alpha,\beta) = \min_{\pi \in \Pi(\alpha,\beta)} \int_{\mathcal{X}\times \mathcal{Y}} c(x,y)d\pi(x,y) + \epsilon H(\pi | \alpha \otimes \beta)$$
	where $$H(\pi | \alpha \otimes \beta) = \int_{\mathcal{X}\times \mathcal{Y}} \log \paren{\frac{d\pi(x,y)}{d\alpha(x)d\beta(y)}}d\pi(x,y).$$
\end{definition}

If we use the relative entropy as a regularizer, then we can formulate the dual of regularized OT as the maximization of an expectation problem \cite{Genevay2018}.

\begin{align*}
W_{\epsilon}(\alpha,\beta) &=\max_{u\in C(\mathcal{X}),v\in C(\mathcal{Y})}\int_{\mathcal{X}} u(x)\mathrm{d}\alpha(x)+\int_{y}v(y)\mathrm{d}\beta(y) \\
&-\varepsilon \int_{\mathcal{X}\times \mathcal{Y}} e^{\frac{u(x)+v(y)-c(x,y)}{\epsilon}}\mathrm{d}\alpha(x)\mathrm{d}\beta(y) + \varepsilon\\
&= \max_{u\in C(\mathcal{X}),v\in C(\mathcal{Y})} \mathrm{E}_{\alpha \otimes \beta} [f_{\epsilon}^{XY}(u,\ v)]+\epsilon
\end{align*}

where $f_{\epsilon}^{xy}(u,\ v)=u(x)+v(y)- \epsilon e^{\frac{u(x)+v(y)[minus]\mathrm{c}(x,y)}{\epsilon}}.$

Since $W_{\epsilon}(\alpha,\alpha)\neq 0$, we can normalize this by defining the Sinkhorn divergence as in the definition below.
\begin{definition}[Sinkhorn Divergence]
\[
\bar{W}_{\epsilon}(\alpha,\beta)=W_{\epsilon}(\alpha,\beta)-\frac{1}{2}(W_{\epsilon}(\alpha,\alpha)+W_{\epsilon}(\beta,\beta))
\]
\end{definition}

\subsection{Sample Dependent Sinkhorn Bounds}

The keys for achieving generalization bounds in this section are the following two lemmas \cite{Genevay2018}:
\begin{lemma}
	\label{divergence_sinkhorn_from_wasserstein}
	Let $\alpha$ and $\beta$ be probability measures on $\mathcal{X}$ and $\mathcal{Y}$ subsets of $\mathbb{R}^d$ such that $\abs{X}=\abs{Y}\leq D$ and assume that $c$ is L-Lipschitz w.r.t $x$ and $y$. Then
	$$W_{\epsilon}(\alpha,\beta)-W(\alpha,\beta) \leq 2\epsilon d \log \paren*{\frac{\epsilon^2 L D}{\sqrt{d}\epsilon}}$$ where $\mathcal{X}$ and $\mathcal{Y}$ are subsets of $\mathbb{R}^d$ with diameters at most $D$ and $c$ is $L$-lipschitz w.r.t $x$ and $y$.
\end{lemma}

and

\begin{lemma}
	Let $\hat{\alpha_n}$ and $\hat{\beta_n}$ be empirical measures for $\alpha$ and $\beta$ with size $n$ for each.

	\[
	\abs{W_{\epsilon}(\hat{\alpha_n},\hat{\beta_n})-W_{\epsilon}(\alpha,\beta)} \leq 6B \frac{\lambda K}{\sqrt{n}} + C\sqrt{\frac{2 \log\frac{1}{\delta}}{n}}
	\] with probability at least $1-\delta$.
\end{lemma}

\subsection*{New Generalization Bound}

\subsection*{Applying regularization to our minimization problem}

%Let's try to minimize
%\[
%(\hat{\Gamma},\hat{f})=\arg\min_{\Gamma,f}\Gamma\left[\alpha d(x_{s},x_{t})+l(y_{s},f(x_{t}))\right]+H(\pi(\Gamma)|S\otimes T_X)
%\]
%
%and see how the expression compares to \[
%\Gamma_n \left[\alpha d(x_{s},x_{t})+l(y_{s},f(x_{t}))\right]+H(\pi(\Gamma_n)|S\otimes T_X)
%\]
%
%To clarify here, $\Gamma$ is the operator representing expectation w.r.t the joint distribution, $\pi(\Gamma)$.
%
%\begin{align*}
%\hat{\hat{\Gamma}}\left[\alpha
%d(x_{s},x_{t})+l(y_{t},\hat{f}(x_{t}))-l(y_{s},y_{t})\right] + H(\pi(\hat{\hat{\Gamma}})|S\otimes T_X) & \leq
%\hat{\hat{\Gamma}}\left[\alpha
%d(x_{s},x_{t})+l(y_{s},\hat{f}(x_{t}))\right] + H(\pi(\hat{\hat{\Gamma}})|S\otimes T_X) \\
%& =\hat{\Gamma}\left[\alpha d(x_{s},x_{t})+l(y_{s},\hat{f}(x_{t}))\right] + H(\pi(\hat{\Gamma})|S\otimes T_X)\\
%&\leq \hat{\Gamma}_n (\alpha d(x_s,x_t) + l(y_s,\hat{f}(x_t))) + \epsilon + ? \\
%& \leq \Gamma_n^{*} (\alpha d(x_s,x_t) + l(y_s,f^{*}(x_t))) + \epsilon + \epsilon + ?\\
%&\leq\Gamma^{*}\left[\alpha d(x_{s},x_{t})+l(y_{s},f^{*}(x_{t}))\right] + 2\epsilon + ?\\
%& \leq\Gamma^{*}\left[\alpha d(x_{s},x_{t})
%+l(y_{t},f^{*}(x_{t}))+l(y_{t},y_{s})\right] + 2\epsilon + ?
%\end{align*}

Letting $c_f(x_s,y_s,x_t) = \alpha d(x_s,x_t) + l(y_s,f(x_t))$, we'll apply the technique from Geneway's paper "Sample Complexity of Sinkhorn Distances" to give sample bounds on the Sinkhorn distance.

The difference here is we'll also show that our $f_{\epsilon}$ is lipschitz in $f$, which comes up in $c_f$ defined earlier.

$$W_{\epsilon}(S,T_X) = \max_{u\in c(S),v\in c(T_X)} \E_{S\otimes T_X} [f_{\epsilon}^{S,T_X}(u,v)]+\epsilon$$

$$f_{\epsilon}^{S,T_X}(u,v) = u(x_s,y_s)+v(x_t)-\epsilon \exp \paren*{\frac{u(x_s,y_s)+v(x_t)-c(x_s,y_s,x_t)}{\epsilon}}$$

$$c_f(x_s,y_s,x_t) = \alpha d(x_s,x_t)+l(y_s,f(x_t))$$
$$u(x_s,y_s)\leq L\norm{(x_s,y_s)}$$
$$v(x_t) \leq \max_{x_s,y_s} u(x_s,y_s)-c_f(x_s,y_s,x_t)$$

Things to note here:
I need to check if it matters that $S$ and $T_X$ have different dimensions because in the paper, the two had equal dimension. Now, I'm using $S$ to replace $\mathcal{X}$ and $T_X$ for $\mathcal{Y}$.

We also know that on the subset where $u$ and $v$ are optimal, $u\oplus v\leq 2L\norm{S}+\norm{c}_{\infty}$. Call this subset $\mathcal{A}$.

And let $Q=\exp \paren*{\frac{u(x_s,y_s)+v(x_t)-c(x_s,y_s,x_t)}{\epsilon}}$ so $\abs{Q}\leq \exp\paren{2\frac{L\abs{S}+\norm{c}_{\infty}}{\epsilon}}.$

$$\frac{\partial f_{\epsilon}}{\partial f} = -\epsilon \frac{\partial Q}{\partial f} = -\epsilon \cdot Q \cdot (1/\epsilon) \cdot \frac{\partial c}{\partial f} = -Q \frac{\partial c(f)}{\partial f} = -Q \frac{\partial l(f)}{\partial f}$$

Thus $$\abs{\frac{\partial f_{\epsilon}}{\partial f}}\leq \abs{Q}M,$$ as long as $\abs{l'(f)} \leq M.$

The above shows that if our loss function $l$ is Lipschitz w.r.t. $f$, then so is $f_{\epsilon}$ and we can then apply \ref{divergence_sinkhorn_from_wasserstein}, as long as $f$ is in an RKHS. To be more precise, $\norm{\nabla f^{\epsilon}(u,v)}\leq \max(1+M,\abs{Q}M)$.

Also recall $\norm{u}_{H^s}=O(1+\frac{1}{\epsilon^{s-1}})$ and $\norm{v}_{H^s}=O(1+\frac{1}{\epsilon^{s-1}})$ and we have, by using

$$ \E \bkt{\sup_{g\in G} \E l(g,X) - \frac{1}{n} \sum l(g,X_i)} \leq 2B\E R(G(X_1^n)) $$,

$$ \E \abs{W_{\epsilon}(\alpha,\beta) - W_{\epsilon}(\hat{\alpha_n},\hat{\beta_n})} \leq 3 \frac{2B\lambda}{n} \E \sqrt{\sum_{i=1}^n k(X_i,X_i)} $$

where $B\leq \max(1+M,\abs{Q}M)$, $\lambda$ is bounded since the norms of the potentials are bounded by $O(\max(1,\frac{1}{\epsilon^{d/2}}))$ and $f$ has bounded norm as well (still need to figure out an exact upper bound here).

After justifying the above two lemmas, we can then provide bounds w.r.t $W_{\epsilon}$ because

$$err_{T}(\hat{f}) + 4\epsilon d + 2\epsilon d \log(\frac{LD}{\sqrt{d}\epsilon}) \leq err_{T}(f^{*})+W_{\epsilon}(S,T)+\hat{\hat{\Gamma}}\left(-\alpha
d(x_{s},x_{t})+l(y_{s},y_{t})\right)$$

%\section*{Interpolation Between OT and MMD}
%Depending on the regularization strength $\epsilon$, we can %interpolate between OT and MMD using the Sinkhorn.


\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{braket}%量子力学符号
\usepackage{geometry}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{float}%稳定图片位置
\usepackage{graphicx,subfig}%画图
\usepackage{caption}
\usepackage[english]{babel}
\usepackage{indentfirst}%缩进
\usepackage{enumerate}%加序号
\usepackage{multirow}%合并行
\usepackage{hyperref}
\hypersetup{hypertex=true, colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black}
\title{\Large \textbf{VE485 HomeWork 6}\\
\author{\textbf{Pan, Chongdan ID:516370910121}\\
}
}
\begin{document}
\maketitle
\section{LS Problem}
\noindent To $\min_x ||Ax-b||$, it's equal to minimize $f(x)=(Ax-b)^T(Ax-b)$
\\It's gradient is $\bigtriangledown f(x)=2A^T(Ax-b)$
\\When $f(x)=0$, we get the pseudo inverse $\hat{x}=A^T(AA^T)^{-1}b$
\\I set the $A,X,b$ randomly, stopping critera $\epsilon=0.01$
\\For Backtracking search $\alpha=0.2,\beta=0.25$
\\After a lot of test, I get results similar to following table
\begin{table}[H]
    \centering
    \begin{tabular}{|l|l|l|l}
    \cline{1-3}
    \multicolumn{1}{|c|}{Input} & \multicolumn{1}{c|}{Final Error} & \multicolumn{1}{c|}{Iterations} &  \\ \cline{1-3}
    $\hat{x}$       &  31.57    &0      &  \\ \cline{1-3}
    $0.5\hat{x}$    &  31.57    &30     &  \\ \cline{1-3}
    $0.75x$         &  7.892    &28     &  \\ \cline{1-3}
    $0.25x$         &  23.67    &31     &  \\ \cline{1-3}
    Random          &  44.61    &33     &  \\ \cline{1-3}
    $x$+Random      &  30.81    &30     &  \\ \cline{1-3}
    1               &  43.64    &33     &  \\ \cline{1-3}
    0               &  31.57    &31     &  \\ \cline{1-3}
    0.5             &  54.87    &31     &  \\ \cline{1-3}
    \end{tabular}
\end{table}
\section{Analysis}
For $X_{input}=\hat{x}$, I think it may be a saddle point since it has 0 iterations but still have error between $X$. It may also because $A$ is not symmetric. What's more, if $X_{input}=k\hat{x}$, it will lead to same error after some iterations.
\par For $X_{input}=kX$, it's surprising that $kX$'s error is always lower when $X_{input}=\hat{x}$, hence, they can also lead to similar saddle point, but it's closer to the optimal value, hence there are multiple saddle points. When $k\rightarrow1$, the error is smaller because it's saddle point is closer to the original solution.
\par For $X_{input}=X_{random}$, we can find it always achieves the largest error with most iterations, it may because there are too much saddle points away from $X$ and the random X will lead to it. But I also found that when $X_{input}=X_{random}+X$, error will be smaller.
\par For constant matrix, we find it will lead to results similar to $\hat{x}$, this may have relation with sparsity.
\section{With Noise}
\subsection{Model for $Ax=b$}
    If we use the old model for $Ax=b$, we will get similar result when the noise $\sigma^2$ is small, such as $\sigma\in(0,10)$
    \begin{table}[H]
        \centering
        \begin{tabular}{|l|l|l|l}
        \cline{1-3}
        \multicolumn{1}{|c|}{Input} & \multicolumn{1}{c|}{Final Error} & \multicolumn{1}{c|}{Iterations} &  \\ \cline{1-3}
        $\hat{x}$       &  30.52    &0      &  \\ \cline{1-3}
        $0.5\hat{x}$    &  30.52    &29    &  \\ \cline{1-3}
        $0.75x$         &  7.839    &29    &  \\ \cline{1-3}
        $0.25x$         &  22.93    &31    &  \\ \cline{1-3}
        Random          &  43.33    &32    &  \\ \cline{1-3}
        $x$+Random      &  31.26    &31    &  \\ \cline{1-3}
        1               &  43.24    &32    &  \\ \cline{1-3}
        0               &  30.52    &31    &  \\ \cline{1-3}
        0.5             &  34.17    &30    &  \\ \cline{1-3}
        \end{tabular}
        \caption{When $\sigma=10$}
    \end{table}
    However, when $\sigma$ increase bigger, all error will approach the same value.
    \begin{table}[H]
        \centering
        \begin{tabular}{|l|l|l|l}
        \cline{1-3}
        \multicolumn{1}{|c|}{Input} & \multicolumn{1}{c|}{Final Error} & \multicolumn{1}{c|}{Iterations} &  \\ \cline{1-3}
        $\hat{x}$       &  2230     &0      &  \\ \cline{1-3}
        $0.5\hat{x}$    &  2230     &43     &  \\ \cline{1-3}
        $0.75x$         &  2230     &44     &  \\ \cline{1-3}
        $0.25x$         &  2230     &44     &  \\ \cline{1-3}
        Random          &  2230     &44     &  \\ \cline{1-3}
        $x$+Random      &  2230     &44     &  \\ \cline{1-3}
        1               &  2230     &44     &  \\ \cline{1-3}
        0               &  2230     &44     &  \\ \cline{1-3}
        0.5             &  2230     &44     &  \\ \cline{1-3}
        \end{tabular}
        \caption{When $\sigma=100$}
    \end{table}
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Loss.png}
        \caption{Loss vs. iteration for GD model without considering $\sigma=100$}
    \end{figure}
    The iteration for $\hat{x}$ is still 0, which means it's still at the saddle point. The error will also increase with the increase of $\sigma$. It also takes more iterations to get the optimal value.
\subsection{Model for $Ax+\varepsilon=b$}
    In this case, our objective is to minimize $||Ax+\varepsilon-b||$, hence the gradient $\bigtriangledown f(x)=2A^T(Ax+\varepsilon-b)$, $\hat{x}$ is also $\hat{x}=A^T(AA^T)^{-1}(b-\varepsilon)$ now.
    \par After we change $f(x),\bigtriangledown f(x)$, and $\hat{x}$, we can get same result as $\varepsilon=0$ no matter how large $\varepsilon$
    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{Loss2.png}
        \caption{Loss vs. iteration for GD model considering $\sigma=100$}
    \end{figure}
    \begin{table}[H]
        \centering
        \begin{tabular}{|l|l|l|l}
        \cline{1-3}
        \multicolumn{1}{|c|}{Input} & \multicolumn{1}{c|}{Final Error} & \multicolumn{1}{c|}{Iterations} &  \\ \cline{1-3}
        $\hat{x}$       &  31.77    &0      &  \\ \cline{1-3}
        $0.5\hat{x}$    &  31.77    &29     &  \\ \cline{1-3}
        $0.75x$         &  7.94     &27     &  \\ \cline{1-3}
        $0.25x$         &  23.83    &30     &  \\ \cline{1-3}
        Random          &  45.17    &31     &  \\ \cline{1-3}
        $x$+Random      &  32.37    &30     &  \\ \cline{1-3}
        1               &  43.60    &32     &  \\ \cline{1-3}
        0               &  31.77    &30     &  \\ \cline{1-3}
        0.5             &  34.91    &30     &  \\ \cline{1-3}
        \end{tabular}
        \caption{When $\sigma=100$}
    \end{table}
\section*{Python Code}
\begin{verbatim}
    import numpy as np
    import matplotlib.pyplot as plt
    # loss function
    def loss(A, b, X, delta):
        y = np.dot(A, X) + delta - b # 50*1
        return np.dot(y.T, y)

    # GD algorithm
    def GD(A, b, X0, delta):
        e = 1e-2
        a = 0.2
        beta=0.25
        k = 0
        X = X0
        g = 2*np.dot(A.T,np.dot(A,X) + delta - b)
        n = []
        m = []
        while np.linalg.norm(g)>e: # When the gradient is not small enough
            g=2*np.dot(A.T,np.dot(A,X) + delta - b) # Calculate the negative gradient 1000*1
            t=1
            while 1: # Backtracking
                Xt = X-t*g # 1000*1
                if loss(A, b, Xt, delta) < loss(A, b, X, delta) + a*t*np.dot(g.T, (Xt - X)):
                    break
                t *= beta
            X = Xt
            n.append(k)
            m.append(np.linalg.norm(np.dot(A,X) + delta - b))
            k += 1
        plt.plot(n, m)
        return [X,k]

    # generate the dataset
    delta = 1 * (100**2)*np.random.randn(50, 1)
    X = np.random.randn(1000, 1)
    A = np.random.randn(50, 1000)
    b = np.dot(A, X) + delta

    # find the optimal solution through pseudo inverse directly
    xhat = np.linalg.inv(np.dot(A, A.T))
    xhat = np.dot(A.T, xhat)
    xhat = np.dot(xhat, b-delta)
    # print the rank of A
    print('The rank of A is', np.linalg.matrix_rank(A))
    # find the solution through GD
    Result = GD(A, b, xhat, delta)
    print('error for x0=xhat is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    Result = GD(A, b, 0.5*xhat, delta)
    print('error for x0=0.5xhat is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    Result = GD(A, b, 0.75*X, delta)
    print('error for x0=0.75x is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    Result = GD(A, b, 0.25*X, delta)
    print('error for x0=0.25x is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    X0 = np.random.randn(1000, 1)
    Result = GD(A, b, X0, delta)
    print('error for x0=random matrix is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    Result = GD(A, b, X+X0, delta)
    print('error for x0=x+random matrix is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    X0 = np.ones((1000, 1))
    Result = GD(A, b, X0, delta)
    print('error for x0=1 is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    Result = GD(A, b, 0*X0, delta)
    print('error for x0=0 is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    # find the solution through GD
    Result = GD(A, b, 0.5*X0, delta)
    print('error for x0=0.5 is', np.linalg.norm(X-Result[0]), ' with ', Result[1], 'iterations')

    plt.xlabel('iterations')
    plt.ylabel('loss')
    plt.axis([0,10,0,200])
    plt.show()
\end{verbatim}
\end{document}


\documentclass{memoir}
\usepackage{notestemplate}

% \begin{figure}[ht]
%     \centering
%     \incfig{riemmans-theorem}
%     \caption{Riemmans theorem}
%     \label{fig:riemmans-theorem}
% \end{figure}

\begin{document}

\subsection{Normal Subgroups}
\label{sec:normal_subgroups}

Cosets are such a vital construction to visualize subgroups, as they will become key to understanding quotient groups. However, we notice one flaw that makes this characterization difficult-- left and right cosets of a subgroup are not necessarily equivalent. This property is important enough to earn itself a name.

\begin{defn}[Normal Subgroup]
	A subgroup \(N\) of \(G\) is \textbf{normal} if the left and right cosets are the same; i.e.
\begin{align*}
	N\leq G \text{ and }gN = Ng
\end{align*}
for every \(g \in G\). We denote this by \(N \triangleleft G\).
\end{defn}
We can also think of this as \(Ng \subset gN\) and \(gN \subset Ng\), or in other words, \(g^{-1}ng \in N\) and \(gng^{-1} \in N\).
\begin{align*}
	N \triangleleft G \iff N \leq G \text{ and } g^{-1}ng \in N \text{for every }g \in G, n \in N
\end{align*}
We call \(g^{-1}ng\) a \textbf{conjugate} of \(n\).\\

Observe that, because the trivial subgroups are trivially normal, then all subgroups \(H\) of index 2 are normal.

\begin{thm}
	Let \(N \leq G\) be a subgroup of a group \(G\). The following are equivalent:
	\begin{itemize}
		\item \(N \triangleleft G\)
		\item \(gN = Ng\) for all \(g \in G\)
		\item The left cosets of \(N\) in \(G\) form a group by the natural group operation
			\begin{align*}
				gN \cdot g'N = (gg')N
			\end{align*}
		\item \(gNg^{-1} \subset N\) for all \(g \in G\)
	\end{itemize}
\end{thm}

Every group also has a special normal subgroup called the \textbf{center}.

\begin{defn}[Center]
	The \textbf{center} of a group is the normal subgroup given by
\begin{align*}
	Z(G) = \left\{g \in G \mid xg = gx \text{ for every }x \in G \right\} .
\end{align*}
\end{defn}
Every subgroup of the center is normal.
\end{document}

Abstract Algebra - Introductory/Algebra I/Notes/source/2020-03-31-NormalSubgroups.tex


\section{Approach}
Our approach was to use a Long Short Term Memory neural network.
Long Short Term Memory networks are a special kind of RNN, capable of learning long-term dependencies~\cite{Hochreiter1997}.
They work tremendously well on a large variety of problems (such as sentiment analysis) and are now widely used. Their architecture allows for the accumulation of information during operation and uses feedback to remember previous network call states~\cite{Christopher2015}.

Figure~\ref{fig:approach_model} demonstrates the architecture of the neural network.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.25\textwidth]{approach_model}
    \caption{Long Short Term Memory for clickbait
    prediction}
    \label{fig:approach_model}
\end{figure}

Given an article whose title contains $N$ tokens, we first mapped each token $w_n$, where $n \in [1, N]$, to its corresponding word embedding $x_n$, through a word embedding matrix $W_E \in \mathbb{R}^{V \times d_0}$, where $d_0$ denotes the dimensionality of the word embedding, $V$ denotes the size of the vocabulary.

After that, we used an uni-directional LSTM to encode the contextual information into its hidden state:
\[
    \vec{h_n} = LSTM(x_n, \vec{h_{n-1}}),
\]
where $\vec{h_n} \in \mathbb{R}^{d_1}$.

The token level attention vector $\alpha \in \mathbb{R}^N$, which represents the weights of tokens the title when predicting the annotation distribution, can be calculated as:
\[
    \alpha = softmax(\tanh(H W_H) \cdot v),
\]
where $H = \{ h_1, h_2, \dots, h_N \}$ and $H \in \mathbb{R}^{N \times 2d_1}$.

The vector representation of the tweet’s title can be calculated as:
\[
    s = H^T \alpha,
\]
where $s \in \mathbb{R}^{2d_1}$.

The predicted annotation distribution of this tweet on two categories $p = { p_1, p_2 }$ can be calculated as:
\[
    p = softmax(W_s s + b_s),
\]
where $W_s \in \mathbb{R}^{4\times 2d_1}$ and the bias term $b_s \in \mathbb{R}$ are the parameters to train.


\section{Parsing and printing}
\label{sec:parsing-printing}

\subsection{Grammar}

At the time of writing this thesis, LISA does not have a formalized concrete syntax grammar for its language but rather a simple printer that is used to display proofs in a more readable way. In this project I defined a grammar for the first-order logic language with sequents of the front-end (\autoref{fig:grammar}). Unlike the former printer, the grammar is designed to be unambiguous so that we could use it for serialization/persistence later on, and is as close as possible to the original printer of LISA. The main challenge in designing a grammar for our language is due to the fact that the distinction between terms and formulas is made early on, in other words it is not possible to construct ill-typed trees. This subtlety opens the door to potential ambiguities where it is unclear if a node should be interpreted as a term or a formula. The front-end avoids this issue thanks to a carefully designed grammar and a type checker that resolves an intermediate representation into well-typed trees.

\begin{figure}[H]
  \centering
  \begin{framed}
  \grammarindent6.2cm
  \begin{grammar}
  <char-alpha> ::= `a'-`z' | `A'-`Z' | `_'

  <char-alphanumeric> ::= <char-alpha> | `0'-`9'

  <identifier> ::= <char-alpha> <char-alphanumeric>*

  <schematic-identifier> ::= `?' <identifier>

  <schematic-connector-identifier> ::= `??' <identifier>

  <label-identifier> ::= <identifier>
  \alt <schematic-identifier>
  \alt <schematic-connector-identifier>

  <infix-label> ::= `\(\Leftrightarrow\)' | `\(\Rightarrow\)' | `\(\lor\)' | `\(\land\)' | `\(=\)'

  <prefix-label> ::= `\(\neg\)'

  <binder-label> ::= `\(\forall\)' | `\(\exists\)' | `\(\exists!\)'

  <term> ::= <term> <infix-label> <term>
  \alt <prefix-label> <term>
  \alt <binder-label> <identifier> `.' <term>
  \alt <label-identifier>
  \alt <label-identifier> `(' <term> (`,' <term>)* `)'
  \alt `(' <term> `)'

  <context> ::= `\\' <identifier> (`,' <identifier>)* `.'

  <top-term> ::= <context>? <term>

  <terms> ::= <term> (`;' <term>)*

  <sequent> ::= <context>? <terms>? `\(\vdash\)' <terms>?

  <partial-sequent> ::= <context>? ((`...' (`;' <term>)*) | <terms>) `\(\vdash\)' \\
  (((<term> `;')* `...') | <terms>)
  \end{grammar}
  \end{framed}
  \caption[BNF grammar]{BNF grammar for the front. The precedence of operators is the usual one. The current system uses an extended syntax, not shown here, which simply adds convenient aliases for common symbols of set theory (empty, singleton and pair sets, constants, etc.).}
  \label{fig:grammar}
\end{figure}

\subsection{Parsing}

A parser for this grammar was written using \code{scala-parser-combinators} \cite{Moors2008}, a recursive-descent parsing library for Scala. The parser alone is not sufficient to generate the final trees; an intermediate phase called \textbf{resolution} performs type checking and resolves the intermediate representation into its final form. The procedure is dictated by the rules listed in \autoref{fig:typing-rules}. Given the type of the top-level term, we can show by induction that it is possible to recover all the remaining types in the tree, and thus reconstruct the final representation of the tree.

\begin{figure}[H]
  \centering
  \begin{framed}
  \begin{gather}
  \frac{\Gamma, x_1, ..., x_n \vdash t_{l1}: \mathcal{F} \quad ... \quad \Gamma, x_1, ..., x_n \vdash t_{rm}: \mathcal{F}}{\Gamma \vdash \backslash x_1, ..., x_n. t_{l1}, ..., t_{ln} ``\vdash'' t_{r1}, ..., t_{rm}: \mathcal{S}} \tag{\textsc{(partial) Sequent}} \\[1em]
  \frac{\Gamma, x_1, ..., x_n \vdash t: \mathcal{F}}{\Gamma \vdash \backslash x_1, ..., x_n. t: \mathcal{F}} \tag{\textsc{Top-Level Formula}} \\[1em]
  \frac{\Gamma, x \vdash t: \mathcal{F}}{\Gamma \vdash B x. t: \mathcal{F}} \mkern3mu B \in \{\forall, \exists, \exists!\} \tag{\textsc{Binder}} \\[1em]
  \frac{\Gamma \vdash t: \mathcal{F}}{\Gamma \vdash !t: \mathcal{F}} \quad\quad
  \frac{\Gamma \vdash t_1: \mathcal{F} \quad \Gamma \vdash t_2: \mathcal{F}}{\Gamma \vdash t_1 \ast t_2 : \mathcal{F}} \mkern3mu \ast \in \{\Leftrightarrow, \Rightarrow, \lor, \land\}
  \tag{$\textsc{Connector}_{1,2}$} \\[1em]
  \frac{\Gamma \vdash t_1: \mathcal{F} \quad ... \quad \Gamma \vdash t_n: \mathcal{F}}{\Gamma \vdash {??x}(t_1, ..., t_n) : \mathcal{F}} \quad\quad
  \frac{}{\Gamma \vdash {??x}: \mathcal{F}}
  \tag{$\textsc{Connector}_{3,4}$} \\[1em]
  \frac{\Gamma \vdash t_1: \mathcal{T} \quad ... \quad \Gamma \vdash t_n: \mathcal{T}}{\Gamma \vdash (?)x(t_1, ..., t_n): \mathcal{T}} \quad\quad
  \frac{}{\Gamma \vdash {?x}: \mathcal{T}} \quad\quad
  \frac{}{\Gamma \vdash x: \mathcal{T}} \mkern3mu x \notin \Gamma
  \tag{$\textsc{Function}_{1,2,3}$} \\[1em]
  \frac{\Gamma \vdash t_1: \mathcal{T} \quad \Gamma \vdash t_2: \mathcal{T}}{\Gamma \vdash t_1 \ast t_2: \mathcal{F}} \mkern3mu \ast \in \{=\} \quad\quad
  \frac{\Gamma \vdash t_1: \mathcal{T} \quad ... \quad \Gamma \vdash t_n: \mathcal{T}}{\Gamma \vdash (?)x(t_1, ..., t_n): \mathcal{F}}
  \tag{$\textsc{Predicate}_{1,2}$} \\[1em]
  \frac{}{\Gamma \vdash (?)x: \mathcal{F}} \tag{$\textsc{Predicate}_3$} \\[1em]
  \frac{}{\Gamma, x \vdash x: \mathcal{T}} \tag{\textsc{Variable}}
  \end{gather}
  \end{framed}
  \caption[Type inference rules]{Type inference rules. $\mathcal{T}$, $\mathcal{F}$ and $\mathcal{S}$ represent term, formula and sequent types respectively. The meta symbol $x$ represents identifiers, while $t$ represents parsed terms. An optional question mark symbol can precede identifiers. Given a well-formed top-level formula, it is always possible to unambiguously type all the children terms.}
  \label{fig:typing-rules}
\end{figure}

We also implemented a printer and parser for kernel proofs (\autoref{fig:grammar-kernel}). Such proofs look like the one in \autoref{fig:simple-lisa-proof}.

\begin{figure}[H]
  \centering
  \begin{framed}
  \grammarindent6.2cm
  \begin{grammar}
  <indentation> ::= ` '*

  <line-feed> ::= `\\n'

  <integer> ::= `-'? (`1'-`9') (`0'-`9')*

  <step-name> ::= ...

  <kernel-step> ::= <indentation> <integer> \\
  <step-name> \\
  <integer> (`,' <integer>)* \\
  <sequent> \\
  (`[' <top-term> (`;' <top-term>)* `]')?

  <kernel-proof> ::= <kernel-step> (<line-feed> <kernel-step>)*
  \end{grammar}
  \end{framed}
  \caption[BNF grammar for kernel proofs]{BNF grammar for kernel proofs. The symbol $\langle\textit{step-name}\rangle$ is defined by an expression containing only terminals, but omitted for conciseness.}
  \label{fig:grammar-kernel}
\end{figure}

\subsection{Compile-time string interpolation}
\label{sec:parsing-printing-string-interpolation}

While the parser was originally designed for runtime usage, it can also be used by the compiler at compilation-time. This is possible thanks to the fact that the Scala 3 allows multi-stage metaprogramming. The user can manipulate values (\code{T}), expressions for these values (\code{Expr[T]}) or even expressions of expressions for these values (\code{Expr[Expr[T]]}); all within the same program.

The idea was to exploit this mechanism to guarantee safe parsing at compile-time. Thus, if the user attempts to parse an invalid string literal, the compiler would raise a type error. The implementation of that feature is relatively straight forward and is done within a macro. First we extract the string literal value from the expression, then we call our parser on it and finally convert the resulting tree to an expression (at the meta-level, that is converting a \code{T} to an \code{Expr[T]}). The last step requires defining conversion methods for all ADT members.

It turns out we can do better than that. Scala offers a feature called \textit{string interpolator} which additionally allows variables to be ``inserted'' within the string literal (\autoref{fig:string-interpolation-general}). Moreover, it only works on string literals thus guaranteeing the recovery of the value at compile-time.

\begin{lstlisting}[language=Scala,caption={[String interpolation general example]{Simple demonstration of the string interpolation mechanism in Scala. The \code{s} interpolator simply calls \code{toString} on each variable passed and concatenates all the parts together.}},label={fig:string-interpolation-general},captionpos=b]
val s1: String = "world"
val s2: String = s"Hello $s1!" // Hello world!
\end{lstlisting}

This feature has a nice application in our case: not only can we enforce interpolated variables to be terms or formulas but we can also check their type with respect to the context they appear in. For instance, in the expression \code{formula"\$a /\textbackslash\ b"}, the variable \code{a} cannot be a term (in fact, it must be a formula). In addition to terms and formulas, we may also allow the interpolation of labels which is very useful to bind names dynamically, e.g. \code{formula"\$p(s)"}. Notice that the previous expression is structurally different from \code{formula"\${p(term"s")}"}, although it results in the same formula.

\begin{figure}[H]
  \captionsetup[subfigure]{margin=0cm}
  \centering
  \begin{subfigure}{0.25\linewidth}
  \centering
  % Runtime
  \begin{tikzpicture}[auto, on grid, node distance=1.5cm and 2cm, block/.style = {draw, fill=white, rectangle, minimum height=1cm, minimum width=2cm}, none/.style = {draw=none}]
  \node [none] (input) {$\small\code{String}\normalsize$};
  \node [block, below = of input] (lexer) {Lexer};
  \node [block, below = of lexer] (parser) {Parser};
  \node [block, below = of parser] (typer1) {Typer 1};
  \node [none, below = of typer1, draw=none] (output) {$\small\code{T}\normalsize$};
  \draw [->] (input) -- (lexer);
  \draw [->] (lexer) -- (parser);
  \draw [->] (parser) -- (typer1);
  \draw [->] (typer1) -- (output);
  \end{tikzpicture}
  \caption{Runtime parsing}
  \label{fig:parsing-runtime}
  \end{subfigure}
  \qquad\qquad
  \begin{subfigure}{0.4\linewidth}
  \centering
  % Compile-time
  \begin{tikzpicture}[auto, on grid, node distance=1.5cm and 2.25cm, block/.style = {draw, fill=white, rectangle, minimum height=1cm, minimum width=2cm}, none/.style = {draw=none}]
  \node [none] (input) {$\small\code{Expr[StringContext]}\normalsize$};
  \node [block, below left = of input] (lexer) {Lexer};
  \node [block, below right = of lexer] (parser) {Parser};
  \node [block, below = of parser] (typer1) {Typer 1};
  \node [block, below = of typer1] (typer2) {Typer 2};
  \node [block, below = of typer2] (converter) {Converter};
  \node [none, below = of converter, draw=none] (output) {$\small\code{Expr[T]}\normalsize$};
  \draw [->] (input) -| (lexer);
  \draw [->] (lexer) |- (parser);
  \draw [->] (input) -- (parser);
  \draw [->] (parser) -- (typer1);
  \draw [->] (typer1) -- (typer2);
  \draw [->] (input) -- ++(2.25cm, 0) -- ++(0, -6cm) -- (typer2);
  \draw [->] (typer2) -- (converter);
  \draw [->] (converter) -- (output);
  \end{tikzpicture}
  \caption{Compile-time parsing, with variable interpolation}
  \label{fig:parsing-compile-time}
  \end{subfigure}
  \caption[Parsing phases]{Phases for regular runtime parsing (a) and compile-time string interpolation (b). \code{T} is one of \code{Sequent}, \code{Formula} or \code{Term}, depending on the interpolator that was called.}
  \label{fig:multi-stage-parsing}
\end{figure}

To implement this feature, we must execute the different parsing phases separately (\autoref{fig:multi-stage-parsing}). One can observe that in a well-formed interpolator, tokens (as output by the lexer) cannot possibly overlap different string parts. That property let us do lexing on each string part independently. From this step we can then identify all the potentially taken identifiers allowing us to safely create fresh ones. Then, all the variables are replaced by fresh schemas; still represented as tokens at this point. Finally, we can proceed to the parsing and type checking phases. The first type checking phase will assign types to all of our fresh schemas, while the second will ensure that these assigned types agree with the Scala types of the interpolated variables. A disagreement or an error at an earlier phase is mapped to a positioned Scala type error. Finally all that is left to do is substitute those schemas by their actual value, and convert the value into an expression tree. Notice that we cannot do that at compilation-time as we don't have access to the variables' values: only their type. But this makes sense: types model the range of allowed values, the rest of the information is to be discovered at runtime. Therefore the tree should be constructed at compilation-time, while the final substitution must happen at runtime.

We also studied the possibility of implementing \code{unapply} macros for pattern matching, which could be useful in a tactic language. We concluded that the implementation would be somewhat similar to \code{apply}. We could also make use of the matcher, for instance when matching partial sequents. However, the support for inlining on string interpolators \code{unapply} is currently only partial\footnote{\href{https://github.com/lampepfl/dotty/issues/8577}{github.com/lampepfl/dotty/issues/8577}}.

\begin{lstlisting}[language=Scala,caption={[String interpolation unapply]{An example of the possibilities offered by unapply and string interpolators. This is not currently implemented.}},label={lst:string-interpolator-unapply}]
formula"(?b \/ ?c) => p(?x)" match {
  case formula"$f => $p($x)" => ...
}
\end{lstlisting}