\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}

\title{Generating functions and counting problems}
\author{Dave Neary}
\date{October 2021}

\begin{document}

\maketitle

\section{Problem}

How many solutions are there to the equation $n = a + b + c + d$, where $a \leq b \leq c \leq d$ and $a, b, c, d, n \in \mathbb{N}$?

\section{Solution}

The natural numbers $\mathbb{N} = \{1,2,3,\cdots\}$ — that is, the positive integers (not 0). It will be easier to work on this problem if our solutions are in the non-negative integer, including 0.

We can rewrite the equation as:
\[ x_1= a'+b'+c'+d' \]
where: $x_1=x-4, a'=a-1, b'=b-1, c' = c-1, d' = d-1$, and the inequality for $a,b,c,d$ still holds for $a',b',c',d'$.

We can go one step further, and remove this inequality, by focusing on the
differences between the variables. Since we know that $a'\leq b'\leq c'\leq d'$ we can rewrite
$a_1 = a', b_1 = b' - a', c_1 = c' - b', d_1 = d' - c'$, and substituting these in to the equation,
we get:

\[ x_1= 4a_1 + 3b_1 + 2c_1 + d_1 \]

where each of $a_1,b_1,c_1,d_1 \geq 0$.

There's a nice method of calculating this using generating functions. Consider:

\[P(x) = (1+x^4+x^8+\cdots)(1+x^3+x^6+\cdots)(1+x^2+x^4+\cdots)(1+x+x^2+\cdots)\]

This expands to an infinite series:
\[P(x) = a_0 + a_1x + a_2x^2 + a_3x^3 + \cdots\]

where each coefficient corresponds to the number of solutions for $x_1 = n, x = n+4$.

To get the coefficient in the expansion for $x^{12}$ for example, we need to look at all
the ways that you can combine multiples of 1,2,3, and 4 to add to 12. We can quickly find
that the possible solutions are:

\[(a_1,b_1,c_1,d_1) \in \{(3,0,0,0),(2,1,0,1),(2,0,2,0),(2,0,1,2),(2,0,0,4), \cdots\} \]

where the solution $(2,1,0,1)$ for example corresponds to taking an $x^8$ term from
$(1+x^4+x^8+\cdots)$, an $x^3$ from $(1+x^3+x^6+\cdots)$, a 1 from $(1+x^2+x^4+\cdots)$,
and an $x$ from $(1+x+x^2+\cdots)$.

Then we can sum these geometric series to get:

\[P(x) = \frac{1}{(1-x^4)(1-x^3)(1-x^2)(1-x)} \]

We can use partial fraction decomposition on this to get a closed form for $a_n$.

Our denominator can be factored:
\begin{align*}
 P(x) &= \frac{1}{(1-x^4)(1-x^3)(1-x^2)(1-x)} \\
      &= \frac{1}{(1-x)^4(1+x)^2(1+x^2)(1+x+x^2)} \\
      &= \frac{1}{(1-x)^4(1+x)^2(1+ix)(1-ix)(1-\omega x)(1 - \omega^2 x)}
\end{align*}

where $\omega = \frac{1}{2}(-1+\sqrt{3}i)$, a primitive cube root of unity.

Then we can write this as:
\begin{multline*}
    P(x) = \frac{A_1}{1 - x} + \frac{A_2}{(1 - x)^2} + \frac{A_3}{(1 - x)^3} + \frac{A_4}{(1 - x)^4} \\
    + \frac{A_5}{1 + x} + \frac{A_6}{(1 + x)^2} \\
    + \frac{A_7}{1 + ix} + \frac{A_8}{1 - ix} + \frac{A_9}{1 - \omega x} + \frac{A_{10}}{1 - \omega^2 x}
\end{multline*}

Calculating the $A_i$ values is tedious but straightforward using the Heaviside cover-up
method. Clearing denominators, we get:
\begin{align*}
	1 &= A_1(1-x)^3(1+x)^2(1+x^2)(1+x+x^2) \\
	&+ A_2(1-x)^2(1+x)^2(1+x^2)(1+x+x^2) \\
	&+ A_3(1-x)(1+x)^2(1+x^2)(1+x+x^2) \\
	&+ A_4(1+x)^2(1+x^2)(1+x+x^2) \\
    	&+ A_5(1-x)^4(1+x)(1+x^2)(1+x+x^2) \\
	&+ A_6(1-x)^4(1+x^2)(1+x+x^2) \\
   	&+ A_7(1-x)^4(1+x)^2(1-ix)(1+x+x^2) \\
	&+ A_8(1-x)^4(1+x)^2(1+ix)(1+x+x^2) \\
    	&+ A_9(1-x)^4(1+x)^2(1+x^2)(1-\omega^2 x) \\
	&+ A_{10}(1-x)^4(1+x)^2(1+x^2)(1 - \omega x)
\end{align*}

Now we can set $x$ to various values to isolate and calculate the coefficients (since the equation
above must hold for all values of $x$).

Setting $x=1, x=-1, x=i, x=-i, x=\omega^2, x=\omega$ in order we get
\[
A_4 = \frac{1}{24}, A_6 = \frac{1}{32}, A_7 = \frac{1}{16}, A_8 = \frac{1}{16}, A_9 = \frac{1-\omega}{27}, A_{10} = \frac{1-\omega^2}{27}
\]

Then I set $x=2, -2, 0, 3$ respectively to get four simultaneous equations in $A_1, A_2, A_3, A_5$. When all is said and done, I get:

\begin{multline*}
	P(x) = \frac{17}{72(1-x)} + \frac{59}{288(1-x)^2} + \frac{1}{8(1-x)^3} \\
	+ \frac{1}{24(1-x)^4} + \frac{1}{8(1+x)} + \frac{1}{32(1+x)^2} + \frac{1}{16(1-ix)} \\
	+ \frac{1}{16(1+ix)} + \frac{1-\omega}{27(1-\omega x)} + \frac{1-\omega^2}{27(1-\omega^2 x)}
\end{multline*}

And if I haven’t made a mistake, after turning each of these simple fractions into its own
infinite series as follows:

\begin{align*}
\frac{1}{1-x} &= 1 + x + x^2 + \cdots \\
\frac{1}{(1-x)^2} &= 1 + 2x + 3x^2 + 4x^3 + \cdots \\
\frac{1}{(1-x)^3} &= 1 + \binom{3}{2}x + \binom{4}{2}x^2 + \binom{5}{2}x^3 + \cdots \\
\frac{1}{(1-x)^4} &= 1 + \binom{4}{3}x + \binom{5}{3}x^2 + \binom{6}{3}x^3 + \cdots \\
\frac{1}{1+x} &= 1 - x + x^2 - \cdots \\
\frac{1}{(1+x)^2} &= 1 - 2x + 3x^2 - 4x^3 + \cdots \\
\frac{1}{1+ix} &= 1 + ix - x^2 - ix^3 + x^4 + \cdots \\
\frac{1}{1-ix} &= 1 - ix - x^2 + ix^3 + x^4 - \cdots \\
\frac{1}{1-\omega^2 x} &= 1 + \omega^2 x + \omega x^2 + x^3 + \omega^2 x^4 + \omega x^5 + x^6 + \cdots \\
\frac{1}{1-\omega x} &= 1 + \omega x + \omega^2 x^2 + x^3 + \omega x^4 + \omega^2 x^5 + x^6 + \cdots
\end{align*}

And when we plug everything in, we get a coefficient for $a_n$ (reminder, this is the number of
solutions for partitions in four ordered natural numbers for $x=n+4$) of:
\begin{multline*}
a_n = \frac{17}{72} + \frac{59}{288}\binom{n+1}{1} + \frac{1}{8}\binom{n+2}{2} + \frac{1}{24}\binom{n+3}{3} \\
+ \frac{1}{8}(-1)^n + \frac{1}{32}\binom{n+1}{1}(-1)^n + \frac{1}{16}(i^n + (-i)^n) \\
+ \frac{1}{27}\left(\omega^{n} + \omega^{2n} - \omega^{n+1} - \omega^{2n+2}\right)
\end{multline*}

The $\frac{1}{16}(i^n + (-i)^n)$ terms equal 0 for odd terms, $\frac{1}{8}$ for terms
where $n$ is divisible by 4, and $-\frac{1}{8}$ for other even terms.

Similarly, the $\frac{1}{27}\left(\omega^{n} + \omega^{2n} - \omega^{n+1} - \omega^{2n+2}\right)$
terms equal zero, $-\frac{1}{9}$, or $\frac{1}{9}$, depending on whether $n$ has a remainder of
0, 1, or 2 when divided by 3.

Let’s check our closed form solution for $n=8$ - that is, for $x=12$. By inspection, we can see
that the solutions are:

\begin{multline*}
(a_1,b_1,c_1,d_1) \in \{(2,0,0,0), (1,1,0,1), (1,0,2,0), (1,0,1,2),\\
(1,0,0,4), (0,2,1,0), (0,2,0,2), (0,1,2,1),(0,1,1,3), (0,1,0,5),\\
(0,0,4,0), (0,0,3,2), (0,0,2,4), (0,0,1,6), (0,0,0,8)\}
\end{multline*}

which gives 15 solutions. Again, the solution $(0,1,2,1)$ (for example) corresponds to
$a_1 = 0, b_1=1, c_1=2, d_1=1$, which translates to $a'=0, b'=1, c'=3, d'=4$, or $a=1, b=2, c=4, d=5$.

Using our formula, we get:
\begin{multline*}
	a_8 = \frac{17}{72} + \frac{59}{288}\binom{9}{1} + \frac{1}{8}\binom{10}{2} + \frac{1}{24}\binom{11}{3} \\
	+ \frac{1}{8}(-1)^8 + \frac{1}{32}\binom{9}{1}(-1)^8 + \frac{1}{16}(i^8 + (-i)^8) \\
	+ \frac{1}{27}\left(\omega^{8} + \omega^{16} - \omega^{9} - \omega^{18}\right)
\end{multline*}

\[a_8 = \frac{17}{72} + \frac{59}{32} + \frac{45}{8} + \frac{55}{8} + \frac{1}{8} + \frac{9}{32} + \frac{1}{8} - \frac{1}{9} = 15 \]

It’s pretty amazing that this complicated fractional expression including binomial
coefficients works, but it does!

We can also expand the binomial coefficients and simplify further to get the formula:
\begin{multline*}
	a_n = \frac{1}{288}(2n^3 + 30n^2 + 133n + 175) + (-1)^n(\frac{n+5}{32}) \\
	+ \frac{1}{16}(i^n + (-i)^n) + \frac{1}{27}\left(\omega^{n} + \omega^{2n} - \omega^{n+1} - \omega^{2n+2}\right)
\end{multline*}
and we can notice that:
\[
	-\frac{17}{72} \leq
	\frac{1}{16}(i^n + (-i)^n) + \frac{1}{27}\left(\omega^{n} + \omega^{2n} - \omega^{n+1} - \omega^{2n+2}\right)
	\leq \frac{17}{72}
\]

so we can take $a_n$ to be the positive integer closest to $\frac{1}{288}(2n^3 + 30n^2 + 133n + 175) + (-1)^n(\frac{n+5}{32})$

\[
	a_n =
	\begin{cases}
		\frac{1}{144} (n^3 + 15 n^2 + 62 n + 65) & x \text{ odd} \\
		\frac{1}{144} (n^3 + 15 n^2 + 71 n + 110) & x \text{ even}
	\end{cases}
\]

\section{Recurrence relation}

It is possible to calculate the number of partitions also using a recurrence relation. If we define:
$P_k(n)$ to be the number of ordered partitions of the number $n$ into exactly $k$ non-zero partitions, we can deduce the following:
\begin{itemize}
    \item $P_0(0) = 1$ (by definition - similar to defining $0! = 1$, this ensures the recurrence relationship below terminates in all cases).
    \item $P_k(n) = 0$ if $k\leq 0, n\leq 0$ and $k,n$ are not both $0$.
    \item $P_k(n) = P_k(n-k) + P_{k-1}(n-1)$ - that is, we have a choice to increment the size of all partitions by 1 and leave $n-k$ items to distribute across exactly $k$ buckets, or we can fix one bucket, and we have $n-1$ items to distribute across the other $k-1$ buckets.
\end{itemize}

We can come up with some quick short-cuts for $P_k(n)$ for small values of $k$:
\begin{itemize}
    \item $P_1(n) = 1$ for all $n \geq 0$ - that is, with exactly 1 partition, there is only one possible representation.
    \item $P_n(n) = 1$ is the transposed equivalent - with $n$ buckets and $n$ items, there is only one way to distribute the items so that no bucket is empty.
    \item $P_k(n) = 0$ if $k > 0, n< k$.
    \item $P_2(n) = \lfloor \frac{n}{2} \rfloor$
    \item $P_3(n) = \lfloor \frac{n-1}{2} \rfloor + \lfloor \frac{n-4}{2} \rfloor + \lfloor \frac{n-7}{2} \rfloor  + \cdots $
\end{itemize}

From this, we can reproduce the result above, albeit a little awkwardly:
\begin{align*}
    P_4(12) &= P_4(8) + P_3(11) \\
    &= P_4(4) + P_3(7) + P_3(8) + P_2(10) \\
    &= 1 + \lfloor \frac{6}{2} \rfloor + \lfloor \frac{3}{2} \rfloor + \lfloor \frac{7}{2} \rfloor + \lfloor \frac{4}{2} \rfloor + \lfloor \frac{10}{2} \rfloor \\
    &= 1 + 3 + 1 + 3 + 2 + 5 = 15
\end{align*}

as before. I have not found any nice closed form solution to this recurrence relation, however, and the recurrence relation, while easy to calculate by computer, becomes very unwieldy when calculating by hand.

\end{document}


\section{Tensor and Tor}
Office hours are: for Hood, today from 1:30 to 3:30 in 2-390, and for me on Tuesday from 1 to 3 in 2-478. Point-set topology is the hardest part of this course, sorry for messing up the question on this week's pset. I like to emphasize the algebraic part.
\subsection{Properties over $\otimes_R$}
\begin{enumerate}
\setcounter{enumi}{5}
\item A ring is precisely specified by a map $R\otimes_\Z R\xrightarrow{\mu}R\xleftarrow{\eta}\Z$. You can define a
ring purely diagrammatically. Associativity is commutativity of the following diagram:
\begin{equation*}
\xymatrix{R\otimes R\otimes R\ar[r]^{\mu\otimes 1}\ar[d]^{1\otimes \mu} & R\otimes R\ar[d]^{\mu}\\
R\otimes R\ar[r]^{\mu} & R}
\end{equation*}
The identity map is commutativity of the following diagram.
\begin{equation*}
\xymatrix{\Z\otimes R\ar[r]^{\eta\otimes 1}\ar[dr]_{\cong} & R\otimes R\ar[d]^{\mu} & R\otimes \Z\ar[l]^{1\otimes\eta}\ar[dl]_{\cong}\\
& R & }
\end{equation*}
In fact, an $R$-module is an abelian group with a map $R\otimes M\xrightarrow{\varphi}M$ such that the following diagram commutes.
\begin{equation*}
\xymatrix{R\otimes R\otimes M\ar[r]^{\mu\otimes 1}\ar[d]^{1\otimes \eta} & R\otimes M\ar[d]^{\varphi}\\
R\otimes M\ar[r]^{\varphi} & R}
\end{equation*}
If $A$ is an abelian group, then $R\otimes A$ is an $R$-module, where the multiplication is: $R\otimes(R\otimes A)\to (R\otimes R)\otimes A\xrightarrow{\mu\otimes 1}R\otimes A$. If $A$ is an abelian group, then $A\to R\otimes A$ sending $a\mapsto 1\otimes a$ is universal for maps from $A$ to an $R$-module. We say that it's ``initial''. This means that if $M$ is an $R$-module, there is a factorization:
\begin{equation*}
\xymatrix{A\ar[r]\ar[d]^f & R\otimes A\ar@{-->}[dl]\\
M}
\end{equation*}
Where the map $R\otimes A\to M$ is an $R$-module homomorphism and the map $A\to M$ is an abelian group homomorphism. Why is this true? We have a map $R\otimes A\xrightarrow{1\otimes f}R\otimes M$, so the multiplication $\varphi:R\otimes M\to M$ is what we want. I.e., the extension is the composition:
\begin{equation*}
\xymatrix{A\ar[r]\ar[d]_f & R\otimes A\ar[d]^{1\otimes f}\ar[dl]|{\varphi\circ(1\otimes f)}\\
M & R\otimes M\ar[l]^{\varphi}}
\end{equation*}
\begin{example}
What if we let $A=\Z/n\Z$? Then if $B$ is an abelian group (i.e., a $\Z$-module), $B\otimes \Z/n\Z\cong B/nB$.
\end{example}
\item Consider $0\to \Z\xrightarrow{2}\Z\to \Z/2\Z\to 0$. Let's tensor with $\Z/2\Z$, to get $0\to \Z/2\Z\to\Z/2\Z\to\Z/2\Z\to 0$. This cannot be a sexseq! But it's clear that the surjection $\Z\to\Z/2\Z$ gives an isomorphism $\Z/2\Z\to\Z/2\Z$, i.e., $0\to \Z/2\Z\xrightarrow{0}\Z/2\Z\xrightarrow{\cong}\Z/2\Z\to 0$. This is one of the major tragedies, that tensoring isn't exact. Exact means preserves exact sequences. The moral is that tensoring isn't generally exact, but preserves cokernels. More precisely:
\begin{prop}
The functor $N\otimes M\otimes_R N$ preserves cokernels. What do I mean? This means that this functor is \emph{right exact}, i.e., if $N^\prime\xrightarrow{i} N\xrightarrow{p} N^{\prime\prime}\to 0$ is exact, then so is $M\otimes_R N^\prime\to M\otimes_R N\to M\otimes_R N^{\prime\prime}\to 0$.
\end{prop}
\begin{proof}
We have:
\begin{equation*}
\xymatrix{M\otimes_R N^\prime\ar[r]^{1\otimes i} & M\otimes_R N\ar[r]\ar[d] & M\otimes_R N^{\prime\prime}\\
 & M\otimes_R N/(\img(1\otimes i))=M\otimes_R N/I\ar@{-->}[ur]_{\overline{\phi}}}
\end{equation*}
At least we know that the composite $M\otimes_R N^\prime\to M\otimes_R N\to M\otimes_R N^{\prime\prime}$ is zero. But this means that the dotted map exists, because the image $I$ has to be sent to zero. The claim is that $\overline{\phi}$ is an isomorphism. We can construct an inverse to $\overline{\phi}$. It's easy to construct maps \emph{out} of tensor products. This inverse will be a map $M\otimes_R N^{\prime\prime}\xrightarrow{q}M\otimes_R N/I$. How do we construct maps out of a tensor product? Consider:
\begin{equation*}
\xymatrix{M\otimes_R N^{\prime\prime}\ar@{-->}[r]^{\overline{q}} & M\otimes_R N/I\\
M\times N^{\prime\prime}\ar[u]\ar[ur]^q}
\end{equation*}
Where will $x\otimes y$ be sent? Let's pick $\overline{y}\in N$ such that $p\overline{y}=y$. I can do that because I supposed that $p$ was surjective in the first place. Maybe I'm using the axiom of choice. (By the way, if I had a split exact sequence, tensoring will preserve split exact sequences, but not general exact sequences.) Anyway, map $x\otimes y\mapsto x\otimes\overline{y}+I$. That's the only thing I can think of doing, and so we pray and hope that it works. Let's check that this is well-defined first.

We know that $\overline{y}$ is only well-defined up to the image of something from $N^\prime$. So consider $\overline{y}^\prime=\overline{y}+iz$ for $z\in N^\prime$. These are the only possible lifts. Then we get $x\otimes\overline{y}^\prime=x\otimes(\overline{y}+iz)+x\otimes\overline{y}+x\otimes i(z)=x\otimes\overline{y}+(1\otimes i)(x\otimes z)\in x\otimes\overline{y}+I$. Luckily, we divided out by $I$. There's \emph{four} other things I have to check. I have to check that this is linear in each variable. This is just fussing around with the formula. Let's assume we've done that.

Pretty much by construction, $\overline{q}$ is the inverse for $\overline{p}$. This is because $p$ takes $x\otimes\overline{y}+I$ to $x\otimes y$ because that's what $\overline{p}$ does -- it just applies $p$ to the second factor.
\end{proof}
\end{enumerate}
How about this failure of exactness? What can we do about that? Failure of exactness is bad, so let's try to repair it.

Think of a sexseq of chain complexes (that are bounded below by $0$ and are nonnegatively graded) $0\to N^\prime_\bullet\to N_\bullet\to N^{\prime\prime}_\bullet\to 0$. We get an exact sequence $ H_0 N^\prime\to H_0 N\to H_0 N^\prime\prime\to 0$. We already know that this isn't exact on the left because we have a lexseq in homology (because $ H_1 N^\prime\prime$ need not be trivial). Let's imagine $M\otimes_R-$ as analogous to $ H_0$. We already have an example of a functor that is right exact but not left exact (namely $ H_0$), so this isn't unreasonable. I think I'll write down a theorem and finish the proof on Wednesday.
\begin{theorem}
There are functors $\Tor^R_n(M,-):\mathbf{Mod}_R\to\mathbf{Mod}_R$ for $n\geq 0$, where I have a fixed ring $R$ and a fixed $R$-module $M$, and natural transformations sending a sexseq $0\to N^\prime\to N\to N^{\prime\prime}\to 0$ to $\partial:\Tor^R_n(M,N^{\prime\prime})\to \Tor^R_{n-1}(M,N^\prime)$ such that you get a lexseq:
\begin{equation*}
\xymatrix{\cdots\ar[r] & \Tor^R_n(M,N)\ar[r] & \Tor^R_n(M,N^{\prime\prime})\ar[dll]\\
\Tor^R_{n-1}(M,N^\prime)\ar[r] & \Tor^R_{n-1}(M,N)\ar[r] & \cdots}
\end{equation*}
such that $\Tor^R_0(M,N)=M\otimes_R N$. Basically, $\Tor$ fulfills the same role as homology.
\end{theorem}
Some properties are as follows.
\begin{itemize}
\item $\Tor^R_q(M,N)=0$ for $q>1$ is $R$ is a PID.
\item $\Tor^R_q(M,F)=0$ for $q>0$ if $F$ is a free $R$-module.
\end{itemize}
Let's explore what this gives us before we construct it.
\begin{example}
Let $R=\Z$, and consider the sexseq $0\to \Z\xrightarrow{n}\Z\to\Z/n\Z\to 0$. Because $\Z$ is free, you have:
\begin{equation*}
\xymatrix{\cdots\ar[r] & 0\ar[r] & \Tor^\Z_n(C_\bullet)(M,\Z/n\Z)\ar[dll]\\
\Tor^\Z_{0}(M,\Z)=M\otimes\Z\ar[r]^{\times n} & \Tor^\Z_{0}(M,\Z)=M\otimes\Z\ar[r] & \Tor^\Z_0(M,\Z/n\Z)=M/nM\ar[r] & 0}
\end{equation*}
So $\Tor^\Z_1(M,\Z/n\Z)=\ker(M\xrightarrow{n}M)$. This is the \emph{$n$-torsion} of $M$. That's the origin of $\Tor$. This is the key example to keep in mind. He said something like ``In general, $\Tor$ isn't free, but it is here because $\Z$ is a PID.''
\end{example}
Take a general $R$-module $N$. You can always take a free module $F_0$ that surjects onto $N$, i.e., $F_0\to N\to 0$. For example, you can let $F_0$ be the free $R$-module on the underlying set of $N$. Form a sexseq $0\to K_0\to F_0\to N\to 0$. You have an exact sequence for $n>1$:
\begin{equation*}
\xymatrix{\cdots\ar[r] & 0\ar[r] & \Tor^R_n(M,N)\ar[dll]\\
\Tor^R_{n-1}(M,K_0)\ar[r] & 0\ar[r] & \cdots}
\end{equation*}
So for $n>1$, $\Tor^R_n(M,N)\to \Tor^R_{n-1}(M,K_0)$ is an isomorphism. If $n=1$, then:
\begin{equation*}
\xymatrix{\cdots\ar[r] & 0\ar[r] & \Tor^R_1(M,N)\ar[dll]\\
M\otimes_R K_0\ar[r] & M\otimes_R F_0\ar[r] & M\otimes_R N\ar[r] & 0}
\end{equation*}
The maps between the tensors might be hard to compute, but you can compute this as a kernel.

But I've not constructed the functors yet. I just said to assume that it exists. This was so much fun, taking a free module and surjecting it onto $N$. What I'm trying to do is:
\begin{equation*}
\xymatrix{\cdots\ar@{-->}[rr] & & F_2\ar[dr]\ar@{-->}[rr]^d & & F_1\ar[dr]\ar@{-->}[rr]^d & & F_0\ar[dr]\\
& K_2\ar[ur]\ar[dr] & & K_1\ar[ur]\ar[dr] & & K_0\ar[ur]\ar[dr] & & N\ar[dr]\\
0\ar[ur] & & 0\ar[ur] & & 0\ar[ur] & & 0\ar[ur] & & 0}
\end{equation*}
Where $F_{i+1}$ surjects onto $K_i$ and the $F_i$ are free $R$-modules. Splicing these exact sequences gives you a exact sequence in the top row. This is what's called a \emph{free resolution of $N$}. You can actually write this as:
\begin{equation*}
\xymatrix{\cdots\ar[r] & F_2\ar[r] & F_1\ar[r] & F_0\ar[r]\ar[d] & 0\\
 & & & N & }
\end{equation*}
The $F_0$ are generators of $N$, the $F_1$ are relations, the $F_2$ are relations between relations, and so on. We say that these are syzygies. The singular term is syzygy.


\documentclass[9pt,twocolumn]{extarticle}

\usepackage[hmargin=0.5in,tmargin=0.5in]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{times}
\usepackage{graphicx}
\usepackage{subfigure}

\usepackage{cleveref}
\usepackage{color}
\newcommand{\TODO}[1]{\textcolor{red}{#1}}

\newcommand{\FPP}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\argmin}{\operatornamewithlimits{arg\ min}}
\author{Siwang Li}

\title{Solve LCP with Extened MPRGP}

%% document begin here
\begin{document}
\maketitle

\setlength{\parskip}{0.5ex}

\section{LCP}
\begin{equation}
  A x = b \quad \mbox{ s.t. } \quad J x \ge c
\end{equation}

\section{MPRGP Extension}
\subsection{Projection}
The projection process is to find an vector $y$ which satisfies the constraints while is cloest to the un-projected vector $x$:
\begin{equation}
  P_{\Omega}(x) = \min_{y}\frac{1}{2}\|y-x\|_2^2 \quad \mbox{ s.t. } \quad J y \ge c
\end{equation}
Considering its KKT condition, we have
\begin{equation}
  y = x + J^T\lambda  \quad \mbox{ s.t. } \quad 0 \le \lambda \perp J y \ge c
\end{equation}
We can solve the following LCP sub problem to obtain $\lambda$,
\begin{equation}
   0 \le \lambda \perp (JJ^T)\lambda \ge c - Jx
\end{equation}
\TODO{which is equal to solving (why?)}
\begin{equation}
  (JJ^T)\lambda=c - Jx \quad \mbox{s.t.} \quad \lambda \ge 0
\end{equation}
As $J^TJ$ is a \TODO{SPD matrix (why?)}, we can use traditional MPRGP solver to solve it. Further more, as $J$ is sparse and constant, we can use Incomplete Cholesky Factorization as a pre-conditioner. \TODO{(But how to combine it with MPRGP?)}

\subsection{Step Limit}
Given a feasible point $x$ and a direction $p$, the step limit $\alpha$ is a positive scalar which satisfies
\begin{equation}
  J(x-\alpha p) \ge c
\end{equation}
Thus we can compute $\alpha$ by using
\begin{equation}
 \alpha = \min_{\alpha} \alpha_i
\end{equation}
where
\begin{equation}
  \alpha_i = \left\{
  \begin{array}{l l}
    +\infty & \quad \text{if $-J_ip \ge 0$}\\
    \frac{J_ix-c_i}{J_ip} & \quad \text{if $-J_ip < 0$}
  \end{array} \right
\end{equation}
Here, $J_i$ is the $i$-th row of matrix $J$.

\subsection{Free gradient}
We suppose the constraints active constraints is given by $\hat{J}x = c$, then the free gradient $\phi(x)$ is in the NULL space of $\hat{J}$ and should be as close to the gradient $g = Ax-b$ as possible, and we further require $g^T\phi \ge \epsilon$ to ensure $-\phi$ is a decrease direction:
\begin{equation}
  \phi = \min_\phi \frac{1}{2} \|\phi - g\|_2^2  \quad \mbox{ s.t. } \quad g^T\phi \ge 0, \quad \hat{J} \phi = 0
\end{equation}
The KKT condition for this problem is
\begin{equation}
  \phi = g + g\lambda_1 + \hat{J}^T \lambda_2 \quad \mbox{ s.t. } \quad \epsilon \le \lambda_1 \perp g^T\phi = 0, \quad \hat{J}\phi = 0
\end{equation}
we can obtain $\lambda$ by solving
\begin{equation}\label{}
  \left\{ \begin{array}{rl}
    g^Tg \lambda_1 + (\hat{J}g)^T\lambda_2 = -g^Tg+\epsilon, \quad \mbox{s.t.} \quad \lambda_1 \ge 0\\
    (\hat{J}g)\lambda_1+(\hat{J}\hat{J}^T) \lambda_2 = -\hat{J}g
  \end{array} \right.
\end{equation}
Let $\xi$ be the solution of
\begin{equation}
  (\hat{J}\hat{J}^T) \xi = -\hat{J}g
\end{equation}
Then we have $\lambda_2 = (1+\lambda_1)\xi$, and we can obtain $\lambda_1$ through
\begin{equation}
  \lambda_1 = \max(0,\frac{\epsilon}{\sigma} - 1)
\end{equation}
where $\sigma = g^Tg-(Jg)^T\xi$.

As $\hat{J}\hat{J}^T$ is a SPD matrix, we can use Conjugate Gradient method to solve this problem. However, as $\hat{J}^T\hat{J}$ is not constant, we need a more cheap preconditioner rather than Incomplete Cholesky Factorization, such as $SOR$ preconditioner. (\TODO{Any more efficient method?})

\subsection{Chopped gradient}
The chopped gradient $\beta(x)$ should be as close to the gradient $g$ as possible, and it is required that $x-\alpha \beta$ should always satisfies the constraints for any $\alpha > 0$. Thus we compute $\phi(x)$ by solving
\begin{equation}
  \beta = \min_\beta \frac{1}{2} \|\beta - g_\beta\|_2^2  \quad \mbox{ s.t. } \quad -\hat{J} \beta \ge 0
\end{equation}
where $g_\beta = g-\phi$. The KKT condition of this problem is
\begin{equation}
  \beta = g_\beta - \hat{J}^T\lambda  \quad \mbox{ s.t. } \quad 0 \le \lambda \perp -\hat{J} \beta \ge 0
\end{equation}
By replacing $\beta$ with $\lambda$ in the constraints, we obtain
\begin{equation}
  \hat{J}\hat{J}^T\lambda = \hat{J}g_\beta \quad \mbox{s.t.} \quad \lambda \ge 0
\end{equation}
As $\hat{J}^T\hat{J}$ is a \TODO{SPD matrix (why?)}, we can use traditional MPRGP solver to solve for $\lambda$, and \TODO{use SOR as preconditioner}.

\subsection{Precondition}
We suppose the preconditioning matrix is $M = LL^T$, then we convert the original LCP problem into
\begin{equation}
  \tilde{A} \tilde{x} = \tilde{b} \quad {s.t.} \quad \tilde{J}\tilde{x} \ge c
\end{equation}
where $\tilde{A} = L^{-1}AL^{-T}$, $\tilde{b} = L^{-1}b$, $\tilde{x} = L^{T}x$ and $\tilde{J} = JL^{-T}$. The corresponding free gradient is obtained by solving
\begin{equation}
  \tilde{\phi} = \tilde{g} + \hat{\tilde{J}}^T\lambda \quad {s.t.} \quad \hat{\tilde{J}}\tilde{\phi} = 0
\end{equation}
e.g.
\begin{equation}
  \tilde{\phi} = L^{-1}g + L^{-1}\hat{J}^T\lambda \quad {s.t.} \quad \hat{J}L^{-T}\tilde{\phi} = 0
\end{equation}
Thus we can solve the following equation for $\lambda$
\begin{equation}
  (\hat{J}M^{-1}\hat{J}^T)\lambda = -\hat{J}M^{-1}g
\end{equation}
Then the pesudoresidual for PCG is
\begin{equation}
  z = L^{-T}\tilde{\phi} = M^{-1}(g + \hat{J}^T\lambda)
\end{equation}
If $J = I$, we have
\begin{equation}
  z = M^{-1} \phi
\end{equation}

%% references
% \begin{thebibliography}{99}
% \bibitem{sig2011} Fast simulation of skeleton-driven deformable body
%   characters.
% \end{thebibliography}

\end{document}


\section{Principal bundles, associated bundles}
\subsection{$I$-invariance}
We will denote by $\Vect(B)$ the set of isomorphism classes of vector bundles
over $B$. (Justify the use of the word ``set''!)

Consider a vector bundle $\xi\downarrow B$. If $f:B^\prime\to B$, taking the
pullback gives a vector bundle denoted $f^\ast\xi$. This operation descends to
a map $f^\ast:\Vect(B)\to \Vect(B^\prime)$; we therefore obtain a functor
$\Vect:\Top^{op}\to \Set$. One might expect this functor to give some
interesting invariants of topological spaces.
%\begin{warning}
%    In order to say anything meaningful about vector bundles, you've to assume
%    that the vector bundles are numerable. If $B$ is paracompact (eg a
%    CW-complex), this is automatic.
%\end{warning}
\begin{theorem}\label{Iinvariance}
    Let $I = \Delta^1$. Then $\Vect$ is $I$-invariant. In other words, the
    projection $X\times I\to X$ induces an isomorphism $\Vect(X)\to
    \Vect(X\times I)$.
\end{theorem}
One important corollary of this result is:
\begin{corollary}
    $\Vect$ is a homotopy functor.
\end{corollary}
\begin{proof}
    Consider two homotopic maps $f,g:B\to B^\prime$, so there exists a homotopy
    $H:B^\prime\times I\to B$. If $\xi\downarrow B$, we need to prove that
    $f^\ast_0\xi\simeq f_1^\ast\xi$. This is far from obvious.

    Consider the following diagram.
    \begin{equation*}
	\xymatrix{
	    B^\prime\times I\ar[r]^H\ar[d]_{\pr} & B\\
	    B^\prime &
	    }
    \end{equation*}
    The leftmost map is an isomorphism under $\Vect$, by Theorem
    \ref{Iinvariance}. Let $\eta\downarrow B$ be a vector bundle such that
    $\pr^\ast\eta \simeq f^\ast\xi$. For any $t\in I$, define a map
    $\in_t:B^\prime\to B^\prime\times I$ sends $x\mapsto(x,t)$. We then have
    isomorphisms:
    $$f_t^\ast\xi \simeq \in_t^\ast f^\ast\xi \simeq \in_t^\ast \pr^\ast\eta
    \simeq (\pr\circ \in_t)^\ast\eta \simeq \eta,$$
    as desired.
\end{proof}
It is easy to see that $\Vect(X)\to \Vect(X\times I)$ is injective. In the next
lecture, we will prove surjectivity, allowing us to conclude Theorem
\ref{Iinvariance}.
\subsection{Principal bundles}
%There's a famous video of J.-P.~Serre talking about mathematics. He says you have to know the difference between ``principle'' and ``principal''. He contemplated ``bundles of principles'' (like politics, or society, or something).
\begin{definition}\label{principaldefn}
    Let $G$ be a topological group\footnote{We will only care about discrete
    groups and Lie groups.}. A \emph{principal $G$-bundle} is a right action of
    $G$ on $P$ such that:
    \begin{itemize}
	\item $G$ acts freely.
	\item The orbit projection $P\to P/G$ is a fiber bundle.
    \end{itemize}
\end{definition}
These are not unfamiliar objects, as the next example shows.
\begin{example}\label{principalcovering}
    Suppose $G$ is discrete. Then the fibers of the orbit projection $P\to P/G$
    are all discrete. Therefore, the condition that $P\to P/G$ is a fiber
    bundle is simply that it's a covering projection (the action is ``properly
    discontinuous'').

    As a special case, let $X$ be a space with universal cover
    $\widetilde{X}\downarrow X$. Then $\pi_1(X)$ acts freely on
    $\widetilde{X}$, and $\widetilde{X}\downarrow X$ is the orbit projection.
    It follows from our discussion above that this is a principal bundle.
    Explicit examples include the principal $\Z/2$-bundle
    $S^{n-1}\downarrow\RP^{n-1}$, and the Hopf fibration $S^{2n-1}\downarrow
    \CP^{n-1}$, whcih is a principle $S^1$-bundle.
\end{example}
By looking at the universal cover, we can classify covering spaces of $X$.
Remember how that goes: if $F$ is a set with left $\pi_1(X)$-action, the dotted
map in the diagram below is the desired covering space.
\begin{equation*}
    \xymatrix{
	\widetilde{X}\times F\ar[r]\ar[d]_{p\circ \pr_1} & \widetilde{X}\times
	F/\sim\ar[dl]^q\\
	X &
    }
\end{equation*}
Here, we say that $(y,gz)\sim (yg,z)$, for elements $y\in\widetilde{X}$, $z\in
F$, and $g\in\pi_1(X)$.

Fix $y_0\in\widetilde{X}$ over $\ast\in X$. Then it is easy to see that
$F\xrightarrow{\sim}q^{-1}(\ast)$, via the map $z\mapsto (y_0,z)$. This is all
neatly summarized in the following theorem from point-set topology.
\begin{theorem}[Covering space theory]
    There is an equivalence of categories:
    $$\{\text{Left $\pi_1(X)$-sets}\}\xrightarrow{\simeq}\{\text{Covering
    spaces of }X\},$$
    with inverse functor given by taking the fiber over the basepoint and
    lifting a loop in $X$ to get a map from the fiber to itself.
\end{theorem}
Example \ref{principalcovering} shows that covering spaces are special examples
of principal bundles. The above theorem therefore motivates finding a more
general picture.
\begin{construction}
    Let $P\downarrow B$ is a principal $G$-bundle. If $F$ is a left $G$-space,
    we can define a new fiber bundle, exactly as above:
    \begin{equation*}
	\xymatrix{
	    P\times F\ar[r]\ar[d] & P\times F/\sim\ar[dl]^q\\
	    B &
	    }
    \end{equation*}
    This is called an \emph{associated bundle}, and is denoted $P\times_G F$.
\end{construction}
We must still justify that the resulting space over $B$ is indeed a new fiber
bundle with fiber $F$. Let $x\in B$, and let $y\in P$ over $x$. As above, we
have a map $F\to q^{-1}(\ast)$ via the map $z\mapsto[y,z]$. We claim that this
is a homeomorphism. Indeed, define a map $q^{-1}(\ast)\to F$ via
$$[y^\prime,z^\prime]=[y,gz^\prime]\mapsto gz^\prime,$$
where $y^\prime = yg$ for some $g$ (which is necessarily unique).
\begin{exercise}
    Check that these two maps are inverse homeomorphisms.
\end{exercise}
\begin{definition}
    A vector bundle $\xi\downarrow B$ is said to be an $n$-plane bundle if the
    dimensions of all the fibers are $n$.
\end{definition}
Let $\xi\downarrow B$ be an $n$-plane bundle. Construct a principal
$\GL_n(\RR)$-bundle $P(\xi)$ by defining
$$P(\xi)_b = \{\text{bases for }E(\xi)_b = \mathrm{Iso}(\RR^n, E(\xi)_b)\}.$$
To define the topology, note that (topologically) we have
$$P(B\times \RR^n) = B\times \mathrm{Iso}(\RR^n,\RR^n),$$
where $\mathrm{Iso}(\RR^n,\RR^n) = \GL_n(\RR)$ is given the usual topology as a
subspace of $\RR^{n^2}$.

There is a right action of $\GL_n(\RR)$ on $P(\xi)\downarrow B$, given by
precomposition. It is easy to see that this action is free and simply
transitive. One therefore has a {principal action} of $\GL_n(\RR)$ on $P(\xi)$.
The bundle $P(\xi)$ is called the \emph{principalization} of $\xi$.

Given the principalization $P(\xi)$, we can recover the total space $E(\xi)$.
Consider the associated bundle $P(\xi)\times_{\GL_n(\RR)}\RR^n$ with fiber $F =
\RR^n$, with $\GL_n(\RR)$ acting on $\RR^n$ from the left. Because this is a
linear action, $P(\xi)\times_{\GL_n(\RR)}\RR^n$ is a vector bundle. One can
show that
$$P(\xi)\times_{\GL_n(\RR)}\RR^n\simeq E(\xi).$$

Fix a topological group $G$. Define $\Bun_G(B)$ as the set of isomorphism
classes of $G$-bundles over $B$. An isomorphism is a $G$-equivariant
homeomorphism over the base. Again, arguing as above, this begets a functor
$\Bun_G:\Top\to\Set$. The above discussion gives a natural isomorphism of
functors:
$$\Bun_{\GL_n(\RR)}(B) \simeq \Vect(B).$$
The $I$-invariance theorem will therefore follow immediately from:
\begin{theorem}
    $\Bun_G$ is $I$-invariant.
\end{theorem}
\begin{remark}
    Principal bundles allow a description of ``geometric structures on $\xi$''.
    Suppose, for instance, that we have a metric on $\xi$. Instead of looking
    at all ordered bases, we can attempt to understand all ordered orthonormal
    bases in each fiber. This give the \emph{frame bundle}
    $$\mathrm{Fr}(B) = \{\text{ordered orthonormal bases of }E(\xi)_b\};$$
    these are isometric isomorphisms $\RR^n\to E(\xi)_b$. Again, there is an
    action of the orthogonal group on $\mathrm{Fr}(B)$: in fact, this begets a
    principal $O(n)$-bundle. Such examples are in abundance: consistent
    orientations give an $SO(n)$-bundle. Trivializations of the vector bundle
    also give principal bundles. This is called ``reduction of the structure
    group''.
\end{remark}
% This wasn't proven in the lectures, but is extremely important.
One useful fact about principal $G$-bundles (which should not be too
surprising) is the following statement.
\begin{theorem}\label{morphismiso}
    Every morphism of principal $G$-bundles is an isomorphism.
\end{theorem}
\begin{proof}
    Let $p:P\to B$ and $p^\prime:P^\prime\to B$ be two principal $G$-bundles
    over $B$, and let $f:P\to P^\prime$ be a morphism of principal $G$-bundles.
    For surjectivity of $f$, let $y\in P^\prime$. Consider $x\in P$ such that
    $p(x) = p^\prime(y)$. Since $p(x) = p^\prime f(x)$ we conclude that $y =
    f(x)g$ for some $g\in G$. But $f(x)g = f(xg)$, so $xg$ maps to $y$, as
    desired.  To see that $f$ is injective, suppose $f(x) = f(y)$. Now $p(x) =
    p^\prime f(x) = p(y)$, so there is some $g\in G$ such that $xg = y$. But
    $f(y) = f(xg) = f(x)g$, so $g=1$, as desired.  We will leave the continuity
    of $f^{-1}$ as an exercise to the reader.
\end{proof}
Theorem \ref{morphismiso} says that if we view $\Bun_G(B)$ as a category where
the morphisms are given by morphisms of principal $G$-bundles, then it is a
groupoid.


\subsection{Practice Problems}
\begin{enumerate}
\item If we think back to the ideal gas law, a term that we could add to make it more general (applicable to non-ideal gases) is to add a term correcting for molecular interactions. Doing so, we get the equation:
\begin{equation}
    \label{eqn:(33)}
    \left(P+a\frac{n^2}{V^2}\right)V = nRT
\end{equation}
where $a\frac{n^2}{V^2}$ is the interaction term. Note that if we also add an $-nb$ term to the volume to account for the size of the molecules, we end up with the very general Van der Waals equation:
\begin{equation}
    \left(P+a \frac{n^{2}}{V^{2}}\right)\left(V-n b\right)=n R T
\end{equation}
But for the purposes of this question, let's assume $b=0$ to make our lives easier.
\begin{enumerate}
    \item Manipulate the equation \ref{eqn:(33)} to obtain pressure as a function of temperature, volume, and amount of gas.
    \item Suppose that I isothermally compress the gas given in equation \ref{eqn:(33)} from $V_1$ to $V_2$. How much work do I do on the gas?
\end{enumerate}

\item For each of the 4 (8) processes of isochoric heating, isochoric cooling, isobaric expansion, isobaric compression, isothermal expansion, isothermal compression, adiabatic expansion, and adiabatic compression, consider:
    \begin{enumerate}
        \item Is work done on/done by the gas, or is it zero?
        \item Does heat flow into or out of the gas, or is it zero?
        \item Does the temperature of the gas increase, decrease, or stay the same?
    \end{enumerate}
\item Consider the following heat engine, composed of 10 moles of monoatomic gas, below:
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
        axis x line=bottom,
        axis y line=left,
        xmin=0, xmax=10,
        ymin=0, ymax=10,
%        % (made labels more common)
%        % (because of the "sketch" type of the plot these should not be needed)
%        xlabel={Volume $(\mathrm{m}^3)$},
%        ylabel={Pressure (Pa)},
        % (changed ticks + labels to normal ticks instead of extra ticks)
        xtick={3,6},
        xticklabels={$2$,$4$},
        ytick={1.5,6},
        yticklabels={$5$,$15$},  % <-- (changed order of entries)
    ]
        % fill the area below the curve
        % (draw it first, so it is below everything else)

        % draw the dashed lines
        % (using two different approaches)
        \addplot [dashed,domain=0:3,samples=2] {6};
        \addplot [dashed,domain=0:3,samples=2] {1.5};

        \draw [dashed,thin] (axis cs:6,1.5) -- (axis cs:6,0);
        \draw [dashed,thin] (axis cs:3,1.5)   -- (axis cs:3,0);

        % now draw the curve
        \draw [
            fleche={0.55:black}              % <-- added
        ] (axis cs:3,6) to
            % store start and end coordinates
            coordinate [pos=1] (start)
            coordinate [pos=0] (end)
        (axis cs:6,1.5);
        \draw[fleche={0.55:black}] (axis cs:6,1.5) to (axis cs:3,1.5);
        \draw[fleche={0.5:black}] (axis cs:3,1.5) to (axis cs:3,6);

        % draw start and end point
        \fill [radius=2pt]
            (start) circle[]
            (end)   circle[];
        \filldraw[fill=black] (axis cs: 3,1.5) circle (2pt);
    \end{axis}
        \node[below] at (6.5,0) {$V (m^3)$};
        \node[left] at (0,5.5) {$P (kPa)$};
        \node[above] at (2.05,3.5) {$3$};
        \node[right] at (4.25,0.9) {$1$};
        \node[left] at (2.1,1.15) {$2$};
\end{tikzpicture}
\end{center}
\begin{enumerate}
    \item What is the net change in the internal energy of the heat engine after one cycle?
    \item What is the work done by the heat engine in one cycle? (in kiloJoules... note that $1\textrm{Pa}*1\textrm{m}^3 = 1\textrm{J}$)
    \item What is the net heat flow of the heat engine in one cycle? (in kiloJoules)
    \item Solve for the temperature (in Kelvin) at each of the three points.
    \item For each step, determine the work done by the engine, and how much heat flows into/out of the engine.
\end{enumerate}
\item Consider the following heat engine, composed of monoatomic gas. Treat the process $3 \rightarrow 1$ as isothermal.
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
        axis x line=bottom,
        axis y line=left,
        xmin=0, xmax=10,
        ymin=0, ymax=10,
%        % (made labels more common)
%        % (because of the "sketch" type of the plot these should not be needed)
%        xlabel={Volume $(\mathrm{m}^3)$},
%        ylabel={Pressure (Pa)},
        % (changed ticks + labels to normal ticks instead of extra ticks)
        xtick={3,6},
        xticklabels={$V_0/2$,$V_0$},
        ytick={1.5},
        yticklabels={$P_0$},  % <-- (changed order of entries)
    ]
        % fill the area below the curve
        % (draw it first, so it is below everything else)

        % draw the dashed lines
        % (using two different approaches)
        \addplot [dashed,domain=0:3,samples=2] {1.5};

        \draw [dashed,thin] (axis cs:6,1.5) -- (axis cs:6,0);
        \draw [dashed,thin] (axis cs:3,1.5)   -- (axis cs:3,0);

        % now draw the curve
        \draw [
            fleche={0.55:black}              % <-- added
        ] (axis cs:3,6) to [bend right = 30]
            % store start and end coordinates
            coordinate [pos=1] (start)
            coordinate [pos=0] (end)
        (axis cs:6,1.5);
        \draw[fleche={0.55:black}] (axis cs:6,1.5) to (axis cs:3,1.5);
        \draw[fleche={0.5:black}] (axis cs:3,1.5) to (axis cs:3,6);

        % draw start and end point
        \fill [radius=2pt]
            (start) circle[]
            (end)   circle[];
        \filldraw[fill=black] (axis cs: 3,1.5) circle (2pt);
    \end{axis}
        \node[below] at (6.5,0) {$V$};
        \node[left] at (0,5.5) {$P$};
        \node[above] at (2.05,3.5) {$3$};
        \node[right] at (4.25,0.9) {$1$};
        \node[left] at (2.1,1.15) {$2$};
\end{tikzpicture}
\begin{enumerate}
    \item What is the ratio of the temperatures between point $3$ and point $1$?
    \item Solve for the pressure at point $3$ in terms of $P_0$ and $V_0$
    \item Mark the highest temperature $T_H$ and the lowest temperature $T_C$ on the graph.
    \item Solve for the efficiency of the heat engine. Your answer should not depend on $P_0$, $V_0$, or the amount of gas in the heat engine. (Note: Solving for the efficiency is quite a bit more difficult than the other parts; a partial solution of the work and heat done during certain steps would be a good partial solution.)
\end{enumerate}
\end{center}
\item Question 4 again, except this time treat process $3 \rightarrow 1$ as adiabatic (Note that part (d) is \textit{quite} difficult).
\item Use what you know of adiabatic and isothermal processes to finish deriving the Carnot efficiency.

\end{enumerate}


\documentclass[12pt,a4paper]{article}
\setlength\parindent{0pt}
\thispagestyle{empty}

\begin{document}

\setcounter{secnumdepth}{0}
\section{Back Propagation}

$w_{ji}^l$ Weight from the i neuron in the l-1 layer to the j neuron in the l layer. \\
$b_j^l$ Bias from the j neuron in the l layer. \\

$a_j^l$ Activation from the j neuron in the l layer. \\
$a_j^l = \sum_{i} w_{ji}^l a_i^{l-1} + b_j^l = \sigma(z_j^l)$ \\
$a^l = w^l a^{l-1} + b^l = \sigma(z^l)$ \\

Cost function : $C = \frac{1}{2n} \sum_{i} (target_i - a_i^L)^2$ \\
Cost function derivative : $\frac{\partial C}{\partial a_i^L} = (a_i^L - target_i)$ \\

$\frac{\partial C}{\partial w}$ and $\frac{\partial C}{\partial b}$ represent how quickly Cost function changes with respect to weight w and bias b. Back Propagation is to relate $\delta^l_i$ to $\frac{\partial C}{\partial w}$ and $\frac{\partial C}{\partial b}$. \\

$delta^l_i$ Error in the i neuron in the l layer. \\

BP1 : $\delta _i^L = \frac{\partial C}{\partial a_i^L} \sigma'(z_i^L) = (a_i^L - target_i) \sigma'(z_i^L)$ \\
Matrix : $\delta ^L = \nabla _a C \odot \sigma'(z^L) = (a^L - target) \odot \sigma'(z^L)$ \\

BP2 : $\delta _i^l = \frac{\partial C}{\partial z_i^l} = \sum_{j} \frac{\partial C}{\partial z_j^{l+1}} \frac{\partial z_j^{l+1}}{\partial z_i^l} = \sum_{j} \delta _j^{l+1} \frac{\partial z_j^{l+1}}{\partial z_i^l} = \sum_{j} \delta _j^{l+1} w_{ji}^{l+1} \sigma '(z_i^l)$ \\
$( \frac{\partial z_j^{l+1}}{\partial z_i^l} = w_{ji}^{l+1} \sigma '(z_j^l) )$ \\

BP1 compute the error in L layer, then apply BP2 to compute the layers back through. \\

BP3 : $\frac{\partial C}{\partial b_i^l} = \delta _i^l$ \\
Matrix : $\frac{\partial C}{\partial b} = \delta$ \\

BP4 : $\frac{\partial C}{\partial w_{ji}^l} = a_i^{l-1} \delta _j^l$ \\
Matrix : $\frac{\partial C}{\partial w} = a_{in} \delta _{out}$ \\
$a_{in}$ is the activation of the neuron input to the weight, and $\delta _{out}$ is the error of the neuron output from the weight. \\

update weights according to $w^l = w^l - \frac{\partial C}{\partial w_i_j^l}$. \\
update biases according to $b^l = b^l - \frac{\partial C}{\partial b_i^l}$. \\

\end{document}


%\documentclass{article}
%\usepackage[utf8]{inputenc}
%\usepackage{amsmath}

%\title{Toda Lattice, Role of $N$ and $y_k$}
%\author{Lusa Zhan}
%\date{November 2016}

%\begin{document}

%\maketitle

\subsubsection{Role of $N$}
The choice of $N = \text{diag} (n, n-1, \dots, 1)$ for analog sorting is clarified by Theorem 1.5 in~\cite{helmke}. To summarize, the theorem states the following:

For each $N$,  $\dot{H}(t) = [H,[H,N]]$ converges to an equilibrium $H_\infty$ as $t\rightarrow \infty$. If $N=\text{diag} (\mu_1 , \dots , \mu_n)$ where $\mu_1 > \dots > \mu_n$, then the Hessian of $f_N(H) = \frac{1}{2}\|N-H\|^2$ is nonsingular and negative definite.

The linearization of the double bracket flow at an equilibrium point $H_\infty = \text{diag}(\lambda_{\pi(1)},\dots,\lambda_{\pi(n)})$ is
\[\dot \xi_{ij} = -(\lambda_{\pi(i)}-\lambda_{\pi(j)})(\mu_i-\mu_j)\xi_{ij}\]
where $\xi = [H_\infty, N]$. Since the Hessian is negative definite, $\xi$ must be at its maximum. If $N = \text{diag} (n, n-1, \dots, 1)$, then $\xi$ will only reach its maximum if the diagonal entries of $H_\infty$ are sorted in descending order as well.

Thus the diagonal matrix $N$ determines the order of the diagonal entries in $H_\infty$. Letting $N = \text{diag}(n, ,n-1, \dots, 1)$, we will result in $x_1 \geq x_2 \geq \cdots \geq x_n$ in $H_\infty$.

\subsubsection{Role of off-diagonals $y_k$}
The choice of initial values for the off-diagonal $y_k$ values affects the results of analog sorting as well.

First of all, as mentioned previously, $y_0=y_n=0$.
To analyze the initial values of $y_k$, consider the relation to $\dot x_k$
\begin{align*}
    \dot{x_k} &=  2y^2_k-2y^2_{k-1} \\
    \dot{y_k} &= y_k(x_{k+1}-x_k)
\end{align*}

If $y_k=0$, then $\dot x_k=0$, meaning $x_k(t) = x_k(0)$ for all $t$. This means that the values along the diagonal of $H$ will remain constant and never get sorted. So, $y_k$ should be non-zero.

In addition, the $y_k$ values should be small (but still positive). Recall that the eigenvalues will be the values along the diagonal of $H$ as $t\rightarrow \infty$. Consider the determinant $f_n = \det (H-\lambda I)$, where $n$ is the size of the matrix. Since $H$ is a tridiagonal matrix, this determinant can be formulated using a recurrence relation on the size of the matrix $n$.

\begin{align*}
f_n &= (x_n-\lambda)f_{n-1}-y_{n-1}^2f_{n-2} \\
f_1 &= x_1-\lambda
\end{align*}

We can use this to calculate the first few $f_n$ and set them to $0$ (what we would do to calculate the eigenvalues):
\begin{align*}
f_1 &= x_1-\lambda = 0\\
f_2 &= (x_2-\lambda)(x_1-\lambda)-y_1^2 = 0\\
f_3 &= (x_3-\lambda)(x_2-\lambda)(x_1-\lambda)-(x_3-\lambda)y_1^2-(x_1-\lambda)y^2_2 = 0
\end{align*}

Intuitively, the eigenvalues will be closest to $x_k$ if the $y_k$ values are smaller. When setting up the analog sorter, we want the off-diagonals to be nonzero, but small enough to not significantly change the values to be sorted.

%\section{Related work}
%Below is a list of articles that are related to this topic:
%\begin{itemize}

%\item{
%    Brockett, R.w. ``Dynamical Systems That Sort Lists, Diagonalize Matrices, and Solve Linear Programming Problems.'' Linear Algebra and Its Applications 146 (1991): 79-91. }
%\item{
%    Helmke, Uwe, and John B. Moore. ``Double Bracket Isospectral Flows.'' Optimization and Dynamical Systems. London: Springer-Verlag, 1994. 43-80. Print.}
%\end{itemize}

%\end{document}


\section{Noetherian Rings}
We have seen in the proof of PID implying UFD that PIDs has the ascending chain condition:
\begin{definition}
    A ring $R$ is said to satisfy the ascending chain condition (ACC) if any ascending chain of ideals $I_1\subset I_2\subset\cdots$ eventually terminates.
\end{definition}
\begin{lemma}
    A ring $R$ satisfies ACC iff all ideals $I\in R$ are finitely generated.
\end{lemma}
\begin{proof}
    Trivial.
\end{proof}
\begin{definition}
    A ring $R$ is called Noetherian if it satisfies ACC.
\end{definition}
\begin{theorem}[Hilbert's Basis Theorem]
    If $R$ is Noetherian, then $R[X]$ is also Noetherian.
\end{theorem}
\begin{proof}
    Start with an ideal $J\unlhd R[X]$.
    Pick $f_1\in J$ with minimal degree.
    If $J=(f_1)$, we are done.
    Otherwise we can pick $f_2\in J\setminus (f_1)$ with minimal degree.
    Continuing this, if $J$ is not finitely generated, then there is a nested sequence
    $$(f_1)\subsetneq (f_1,f_2)\subsetneq\cdots, \deg f_1\le\deg f_2\le\cdots$$
    Let $a_i$ be the leading coefficient of $f_i$, then consider a chain of ideals $(a_1)\subset (a_1,a_2)\subset\cdots$.
    $R$ is Noetherian, so this sequence must eventually terminates, so in particular there is some $m\in\mathbb N$ such that $a_{m+1}\in(a_1,\ldots,a_m)$.
    So $a_{m+1}=\lambda_1a_1+\cdots+\lambda_ma_m$.
    Now consider
    $$g(X)=\sum_{i=1}^m\lambda_iX^{\deg f_{m+1}-\deg f_i}f_i$$
    So $g,f_{m+1}$ has the same degree and leading coefficient, so $\deg (f_{m+1}-g)<\deg f_{m+1}$.
    But $f_{m+1}-g\in J$, so since we chose $f_{m+1}$ to have the minimal degree in $J\setminus(f_1,\ldots,f_m)$, $f_{m+1}-g\in (f_1.\ldots,f_m)$, so $f_{m+1}\in (f_1,\ldots,f_m)$, contradiction.
\end{proof}
\begin{corollary}
    $R[X_1,\ldots,X_n]$ is Noetherian whenever $R$ is.
\end{corollary}
In particular, $\mathbb Z[X_1,\ldots,X_n],\mathbb F[X_1,\ldots,X_n]$ are Noetherian (where $\mathbb F$ is a field).
\begin{proof}
    Apply the preceding theorem recursively.
\end{proof}
\begin{example}
    Let $R=\mathbb C[X_1,\ldots,X_n]$.
    Let $V\subset\mathbb C^n$ be of the form
    $$V(\mathcal F)=\{(a_1,\ldots,a_n)\in\mathbb C^n:f(a_1,\ldots,a_n)=0,\forall f\in\mathcal F\}$$
    for some (possibly infinite) subset $\mathcal F\subset R$.
    Let
    $$I=\left\{\sum_{i=1}^m\lambda_if_i:m\in\mathbb N,\lambda_i\in R,f_i\in\mathcal F\right\}$$
    Then $I\unlhd R$ and $V(I)=V(\mathcal F)$, but $R$ is Noetherian by the preceding corollary, so $I$ is finitely generated and thus $V(\mathcal F)$ can be defined by only finitely many polynomials.
\end{example}
\begin{lemma}
    Any quotient ring of a Noetherian ring is again Noetherian.
\end{lemma}
\begin{proof}
    Suppose $R$ is Noetherian and $I\unlhd R$ is an ideal.
    Consider a chain of ideals $J_1\subset J_2\subset\cdots$ in $R/I$.
    But we know the correspondence between the ideals in $R/I$ and the ideals of $R$ containing $I$, so there are ideals $I_1,I_2,\ldots$ all containing $I$ with $J_i=I_i/I$.
    But then $I_1\subset I_2\subset\ldots$, so there is $N\in\mathbb N$ such that for any $m>N$, $I_m=I_N$, hence $J_m=I_m/I=I_N/I=J_N$, hence the sequence eventually terminates, thus $R/I$ is Noetherian.
\end{proof}
\begin{example}
    1. The Gaussian integers can be written as $\mathbb Z[i]\cong\mathbb Z[X]/(X^2+1)$ hence is Noetherian.\\
    2. If $R[X]$ is Noetherian, then $R$ is Noetherian since $R\cong R[X]/(X)$, so Hilbert's Basis Theorem is actually an ``if and only if''.
\end{example}
\begin{example}[Non-example]
    We shall give examples of a non-Noetherian rings.\\
    1. We consider the ring as the upper limit
    $$R=\mathbb Z[X_1,X_2,\ldots]=\bigcup_{n\in\mathbb N}\mathbb Z[X_1,\ldots,X_n]$$
    Then $(X_1)\subsetneq (X_1,X_2)\subsetneq\cdots$, so $R$ is not Noetherian.\\
    2. Consider the ring $R\le \mathbb Q[X]$ by collecting $R=\{f\in\mathbb Q[X]:f(0)\in\mathbb Z\}$, then $R$ is obviously a ring with
    $$(X)\subsetneq (2^{-1}X)\subsetneq (2^{-2}X)\subsetneq\cdots$$
    3. Consider the ring $R$ of infinitely differentiable functions $[-1,1]\to\mathbb R$ under pointwise operations, this is also not Noetherian (exercise).
\end{example}


\section{Preliminaries}

\subsection{The Probabilistic Model}

We are given a sequence $\cbr{x_t = (c_t, \mu_t)}_{t=1}^l$, where at each position $t$, $c_t \in \cbr{0,1,\ldots,N}$ is called the coverage and $\mu_t \in \cbr{0,1,\ldots,c_t}$ is called the methylation count. We model the data using a binomial hidden Markov model, that is, there are underlying hidden states $h_t$ in $[m]$ generating the observations $x_t$. In addition, The dynamics of $h_t$'s is modeled by a Markov chain. Each hidden state $h \in [m]$ is associated with a methylation probability $p_h \in [0,1]$.
In our model, $c_t$'s' are not modeled probablistically  but are observed and measure how hard it is to sample from location $t$ in the genome.
Given $c_t$ and $h_t$, $\mu_t$ is drawn from a binomial distribution, with the mean parameter $p = p_{h_t}$.
Formally,
\[ \mu_t | c_t, h_t \sim \bin(c_t, p_{h_t}) \]

To summarize, the binomial HMM model can be represented by parameters $(\pi, T, p)$, where $\pi \in \R^m$ is the initial probability distribution (that is, $\pi_i = \P(h_1 = i)$), $T \in \R^{m \times m}$ is the transition matrix of the Markov chain (that is, $T_{i,j} = \P(h_{t+1} = i | h_t = j)$), and $p \in \R^m$ is the methylation probability vector (that is, given $h_t = i$ and $c_t$, $\mu_t$ is drawn from $\bin(c_t, p_i)$).

\subsection{Matrix and Tensor Notations}
A $3$-dimensional array $T \in \R^{n_1 \times n_2 \times n_3}$ is called a 3rd-order tensor, and we use $T_{i_1, i_2, i_3}$ to denote its $(i_1, i_2, i_3)$-th entry. The tensor product of vectors $v_i$, $i = 1, 2, 3$ is denoted by $v_1 \otimes v_2 \otimes v_3$, where its
$(i_1, i_2, i_3)$-th entry is $(v_1)_{i_1} (v_2)_{i_2} (v_3)_{i_3}$. We say a tensor has rank $1$ if it can be
written as a tensor product of vectors. A tensor $T$ is called symmetric if $T_{i_1, i_2, i_3} = T_{\pi(i_1), \pi(i_2), \pi(i_3)}$ for any permutation $\pi: \cbr{1,2,3} \to \cbr{1,2,3}$.

We define tensor-matrix multiplication as follows. Given tensor $T \in \R^{n_1 \times n_2 \times n_3}$ and matrices $V_i \in \R^{n_i \times m_i}$, $i = 1,2,3$, $T(V_1, V_2, V_3)$ is a tensor of size $m_1 \times m_2 \times m_3$, whose $(i_1, i_2, i_3)$-th entry is computed by the formula $T(V_1, V_2, V_3)_{i_1, i_2, i_3} = \sum_{j_1, j_2, j_3} T_{j_1, j_2, j_3} (V_1)_{j_1,i_1} (V_2)_{j_2, i_2} (V_3)_{j_3,i_3}$.

\subsection{Basic Spectral Method for Categorical HMMs}
\cite{AGHKT12} proposes a spectral algorithm for parameter recovery for hidden Markov models. Its main idea is to reduce the learning problem to tensor decompositon, which is well-studied in the numerical linear algebra literature. First, the algorithm construct a co-occurence tensor for thee consecutive observations. Then, it make a series of transformations over the tensor to make it symmetric and orthogonal. Finally, it applies tensor power method to recover the columns of the observation matrix.

In the setting of~\cite{AGHKT12}, HMMs with categorical observations are considered. In this model, the hidden states $h_t$ are still drawn from a Markov chain, whereas given the hidden state $h_t$ in $[m]$, the observation $x_t$ is drawn from a categorical distribution on $[n]$ represented by $O_{h_t}$, where $O$ is a matrix in $\R^{n \times m}$.
Note that the model is not the same as our binomial HMM, and hence the results of~\cite{AGHKT12} does not directly apply to our setting. Nevertheless, as we show in Section 3, they can be adapted to our setting after a few novel modifications. We give a brief step-by-step overview of~\cite{AGHKT12}'s algorithm, abbreviated as \TD.

\paragraph{Step 1: Construct Matrices and Tensors.} In the hidden Markov model, for any $t$, observations $(x_{t-1}, x_t, x_{t+1})$ are conditionally independent given the hidden state $h_t$. We compute
$P_{i,j} := \E[x_i \otimes x_j]$~\footnote{Since we only have a finite sample, in practice, expecations are replaced by empirical averages over the data.} (where $i,j$ are distinct elements from $\cbr{1,2,3}$~\footnote{Although only the first three observations are used, the algorithm can be generalized to use all three consecutive observations in the sequences.}) and $T := \E[x_1 \otimes x_2 \otimes x_3]$. If we use matrix $C_l$ to denote a matrix where $(C_l)_{i,j} = \E[x_l = i | h_2 = j] \in \R^{n \times m}$ for $l \in \cbr{1,2,3}$, and use vector $w \in \R^m$ to denote a vector where $w_i = \P[h_2 = i]$,
then we can represent matrices $P_{i,j}$ and the tensor $T$ as follows:
\begin{equation}
  P_{i,j} = C_i \diag(w) C_j^T = \sum_{l=1}^m w_l (C_i)_l \otimes (C_j)_l
  \label{eqn:pij}
\end{equation}
and
\begin{equation}
  T = \sum_{l=1}^m w_l (C_1)_l \otimes (C_2)_l \otimes (C_3)_l
  \label{eqn:t}
\end{equation}

\paragraph{Step 2: Symmetrization.} Observe that the tensor $T$ is neither symmetric nor
orthogonal. We will symmetrize $T$ as follows.
Compute symmetrization matrices $S_1 := P_{2,3} P_{1,3}^\dagger$ and $S_3 := P_{2,1} P_{3,1}^\dagger$. It can be shown that $S_1 = C_2 C_1^\dagger$, and
$S_3 = C_2 C_3^\dagger$. Now we compute a trilinear transformation of $T$ using $S_1$ and $S_3$:
\[ G := T(S_1, I, S_3) = \sum_{l=1}^m w_l (C_2)_l \otimes (C_2)_l \otimes (C_2)_l = \sum_{l=1}^m w_l (C_2)_l^{\otimes 3} \]
Observe that $G$ is a symmetric tensor.

\paragraph{Step 3: Orthogonalization.} To orthogonalize $G$, we compute matrix $M := S_3 P_{3,2}$. It can be shown that $M = \sum_{l=1}^m w_l (C_2)_l \otimes (C_2)_l$.
Now, compute an SVD and take the top $m$ singular vectors $U_m$, and singular values in diagnoal matrix $S_m$, getting orthogonalization matrix $W = U_m S_m^{-1/2}$. It can be seen that $W^T M W = I$, that is, $v_l = W^T (C_2)_l w_l^{\frac 1 2}$ are orthogonal unit vectors.
Now we perform a trilinear transformation over $G$ using $M$, getting tensor $H = G(M, M, M) = \sum_{l=1}^m w_l^{-\frac 1 2} v_l^{\otimes 3}$.
This form is called a {\em symmetric orthogonal} decomposition, because $H$ is symmetric and $v_l$'s are orthogonal.

\paragraph{Step 4: Tensor Power Method.} The next step is to perform symmetric orthogonal decomposition to tensor $H$, to recover the vector $v_l$'s.
Starting with a random vector $v_0$, we perform tensor power iteration $v_{t+1} = H(v_t, v_t, I)$ until $\cbr{v_t}$ converges. After extracting a component $v_l$, a deflation step is performed, where we subtract the extracted rank-1 component ($H_l \gets H_{l-1} - \hat{w}_l \hat{v}_l \otimes \hat{v}_l \otimes \hat{v}_l$), and the algorithm recurses on the remaining tensor $H_l$. The detailed procedure is presented in Algorithm~\ref{alg:tpm}.

\begin{algorithm}
\caption{Tensor Power Method}
\begin{algorithmic}
\STATE input: tensor $H = \sum_{l=1}^m \lambda_l v_l \otimes v_l \otimes v_l$, number of components $m$, number of iterations per component $k$.
\STATE output: estimated factors $\cbr{\hat{v}_l}_{l=1}^m$, estimated coefficients $\cbr{\hat{w}_l}_{l=1}^m$.
\STATE $T_0 \gets T$
\FOR{$l=1,2,\ldots,m$}
    \STATE $v_l^0 \gets$ a vector drawn uniformly at random from $n$-dimensional unit sphere.
    \FOR{$t=1,2,\ldots,k$}
        \STATE $\tilde{v}_l^t \gets \frac{H_{l-1}(v_l^{t-1}, v_l^{t-1}, I)}{\|H_{l-1}(v_l^{t-1}, v_l^{t-1}, I)\|}$.
    \ENDFOR
    \STATE $\hat{v}_l \gets v_l^k$.
    \STATE $\hat{\lambda}_l \gets H_{l-1}(\hat{v}_l, \hat{v}_l, \hat{v}_l)$.
    \STATE Deflation: $H_l \gets H_{l-1} - \hat{\lambda}_l \hat{v}_l \otimes \hat{v}_l \otimes \hat{v}_l$.
\ENDFOR
\end{algorithmic}
\label{alg:tpm}
\end{algorithm}

\paragraph{Step 5: Observation Matrix Recovery.} Recall that $C_2$ is the observation matrix $O$.
The columns of $O$ can now be estimated by formula $\hat{O}_l = (W^T)^{\dagger} \hat\lambda_l \hat{v}_l$, for $l=1,2,\ldots,m$. %We remark that as the order of columns can be arbitrary, we cannot hope to recover matrix $C_2$ with correct column permutations.
Now we compute our estimate of the joint probablity of $h_2$ and $h_1$: $H_{21}:=\hat{O}^{T\dagger} P_21 \hat{O}^\dagger$. Then we compute our estimate of initial probability and trasition matrix by $\hat\pi := (\one^T H_{21}^T$ and $\hat{T} := H_21 \diag(\hat \pi)^{-1}$.

The above algorithm has two advantages over other algorithms such as EM: First, it only needs to make one pass over the data, and is thus computationally efficient if the size of the observation space is not too large. In contrast, EM proceeds iteratively and needs to make one pass over the data per iteration. Second, it achieves statistical consistency if the data is indeed generated from an HMM (see Theorem 1). In contrast, methods such as EM do not guarantee statistical consistency.

\begin{theorem}[Statistical Consistency~\cite{AGHKT12}]
Suppose the algorithm receives $m$ iid samples $(x_1, x_2, x_3)$ as input, which are drawn from the hidden Markov model represented by parameters $(\pi, T, O)$. Then, given parameters $\epsilon$ and $\delta$ in $(0,1)$, if the number of samples $m$ is at least $\poly( \frac{1}{\min_i \pi_i}, \frac{1}{\sigma_\mi(O)}, \frac{1}{\sigma_\mi(T)}, \frac{1}{\epsilon}, \ln \frac{1}{\delta})$,
then with probability $1-\delta$, the output $\hat{O}$, $\hat{T}$ and $\hat{\pi}$ satisfies that
\[ \| O - \hat{O} \Pi \|_F \leq \epsilon \]
\[ \| T - \Pi^\dagger \hat{T} \Pi \|_F \leq \epsilon \]
\[ \| \pi - \Pi^\dagger \hat  \pi \|_2 \leq \epsilon \]
for some permutation matrix $\Pi$, where $\sigma_\mi(M)$ is the minimum singular value of matrix $M$.
\end{theorem}


\section{Methods for intractable Gibbs distr.}

% Problem: How to sample from $p(c) = \frac{1}{\textcolor{red}{Z}} f(c)$?

% ===
\subsection{Sampling and SA}

\textit{Well behaving} \emph{Markov Chains} are
\begin{itemize}
    \item \textbf{irreducible:} can go from/to any state, and
    \item \textbf{aperiodic:} doesn't go ``back\&forth'' forever.
\end{itemize}

$\implies$ \textbf{Stationary dist.} \highlight{$p(c') = \sum_c \pi(c\mid c') p(c)$}

$\iff$ \textbf{det. balance} \highlight*{$\pi(c'\mid c) p(c) = \pi(c\mid c') p(c')$}

\emph{Metropolis-Hastings:}
Assume $p(c) \propto f(c)$.

$\pi(c'\mid c) \coloneqq
\begin{cases}
    q(c'\mid c) \: A(c,c') & c\neq c'\\
    1 - \sum_{c'\neq c} q(c'\mid c) \: A(c,c') & \text{otw.}
\end{cases}$

where\enskip
$q(c'\mid c) :$ prob. to propose the move $c \to c'$,\\
and\enskip
$A(c,c') \coloneqq \min\brace*{ 1, \frac{q(c\mid c') \: f(c') \: / \: \cancel{Z}}{q(c'\mid c) \: f(c) \: / \: \cancel{Z}} }$ prob. accept move

\emph{Metropolis Algorithm:}\enspace
Assume $p(c) \propto f(c)$ and $q(c'\mid c) = q(c\mid c')$, i.e. symmetric.

\begin{highlightbox}
\begin{enumerate}
    \item Define symmetric $\brace{ q(\cdot\mid c) }_{c\in\mathcal C}$ s.t. graph $G_q$ is connected and every vertex in $G_q$ has edge to itself.
    \item $c_0 \leftarrow \$$
        \qquad Then, for $t=1,2,\ldots$, do:
    \begin{itemize}
        \item $\tilde c \leftarrow q(\cdot\mid c_{t-1})$ \quad // sample
        \item $b \leftarrow \mathrm{Bern}\paren*{\min\brace*{ 1, \eu^{-\frac1T [R(\tilde c, X) - R(c_{t-1}, X)]} }}$ % A(c_{t-1}, c)
        \item If $b=1$ then $c_t \leftarrow \tilde c$ else $c_t \leftarrow c_{t-1}$.
    \end{itemize}
\end{enumerate}
\end{highlightbox}

$\pi(c'\mid c) = \{ \substack{\cdots\\\cdots} \enskip\color{gray} \leftarrow \text{c.f. scr. (2.7)}$

\iffalse
    % ===
    \subsection{Sampling Gibbs distributions}

    \emph{Markov Chains:}
    \textit{``Well behaving''} MC's are\\
    \textbf{irreducible}: can go from/to any state, and
    \textbf{aperiodic}: chain doesn't go ``back\&forth'' forever.% $\to p(c\vert c) > 0.5$.
    %\:\:$\Rightarrow$\:\:
    \\$\Rightarrow$
    \textbf{Stationary dist.} \highlight{$\sum_c \pi(c ) p(c\vert c') = \pi(c')$}\\ %$\pi P {=} \pi$
    iff. \textbf{det. balance:} \highlight{$\pi(c) p(c'\mid c) = \pi(c') p(c\mid c')$} %, $\forall c, c' {\in} \mathcal C$.

    \emph{Metroplolis Scheme:}
    $P(i,j) = q(i,j) A(i,j)$ for $i\neq j$ where $q(i,j)$ is the probability to propose move $i\to j$ and $A(i,j)$ is the probability to accept the move.

    \emph{Metropolis-Hastings (MCMC):}\\
    $\to$ disadvantage: is only fast when $T$ is high\\
    \begin{minipage}{\linewidth}
        \algobox{
            \begin{enumerate}
                \item Define $\brace{ q(\cdot\mid c) }_{c\in\mathcal C}$ s.t. $G_q$ is connected, and $q(c\mid c) > 0$, for $c \in \mathcal C$.
                \item
                    \begin{itemize}
                        \item $c_0 \leftarrow \$$
                        \item \texttt{for t=1,2,\ldots}
                        \begin{itemize}
                            \item $\tilde c \leftarrow q(\cdot\mid c_{t-1})$
                            \item $b \leftarrow \mathrm{Bern}\paren*{\min\brace*{1, \frac{q(c_{t-1}\,\vert\,\tilde c) \;\cdot\; f(\tilde c)}{q(\tilde c\,\vert\, c_{t-1}) \;\cdot\; f(c_{t-1})}}}$
                            \item if $b=1$ then $c_t \leftarrow \tilde c$ else $c_t \leftarrow c_{t-1}$
                        \end{itemize}
                    \end{itemize}
            \end{enumerate}
        }
        % ===
        \begin{minipage}{\linewidth}
            \vspace{-75pt}
            \hspace*{58pt}
            $\overbrace{
                \phantom{a\qquad\qquad\!\! a)}
            }^{
                \substack{
                    \text{acceptance}\\
                    \hspace{30pt}
                    A(c,\tilde c) \,=\, \exp(-\frac1T \brack{ R(\tilde c, X) - R(c,X)})
                }
                %A(c,\tilde c) \,\triangleq\, \text{acceptance}
            }$
        \end{minipage}
    \end{minipage}
\fi

\emph{Simulated annealing:}
Gradually decrease temp. $T$ to escape bad local minima.
$\to$ MH-sampling from Gibbs (DA does not sample!).

% ===
\subsection{Laplace's Method \quad\normalfont\sffamily (Least angle clust.)}

\begin{enumerate}
    \item \textit{Square the cost:}\quad
        $\eu^{-\frac1T R(c,X)} = \mathit{const} \cdot \eu^{g(c)^\top g(c)}$
    \item \textit{Complete the square:}\quad
        \highlight{$\int \eu^{-\frac1T (y - g(c))^2} \diff y = (\pi T)^{d/2}$}\\
        $\Rightarrow$ $\eu^{g(c)^\top g(c)} = (\pi T)^{-d/2} \int \exp^{-y^\top y + 2 y^\top g(c)} \diff y$
    \item \textit{Rewrite normalisation constant:}\\
        $Z = \sum_c \eu^{-\frac1T R(c,X)} = \ldots = \mathit{const} \int \eu^{-\frac1T f(y)} \diff y$
    \item \textit{Apply Laplace's method:}\\\vspace{-2pt}
        If $f$ has unique min. $y_0$ and Hessian $H\coloneqq \pderiv[2]{f}{y}\big\vert_{y_0}$\\\vspace{-2pt}
        \highlight*{$\int \eu^{-\frac1T f(y)}\diff y \overset{\scriptstyle(T\to0)}{\approx} \eu^{-\frac1T f(y_0)} \abs*{\frac{H}{2\pi T}}^{-1/2}$}
\end{enumerate}

% \todo{\texttt{slt21\_lecture05\_1.pdf}}

% ===
\subsection{Mean-field Approximation}

\textbf{Idea:}\enskip
Approximate $p_\beta$ (Gibbs) with a ``simple'', \\\phantom{\textbf{Idea:}}\enskip
factorisable distribution $p = p_1 \cdots p_N$.

\textbf{Approach:}\enskip
Minimise $D\ped{KL}(p\parallel p_\beta)$\\
$\iff$ Minimise \emph{Gibbs free energy:}\\
\hfill \highlight*{$G(p) = \frac1\beta D\ped{KL} (p\parallel p_\beta) + F(\beta) = \E[c\sim p]{R(c)} - \frac1\beta H[p]$}

\quad \textit{Note:}\enskip
\highlight{$H[p] = \sum_{i=1}^N H[p_i]$} \enskip \textit{and} \enskip \highlight{$F(\beta) \leq G(p)$}

\emph{Ising model:}\enskip
$R(c\mid J) = -\frac12 \sum_{i,j} J_{ij} c_i c_j - \sum_i h_i c_i$
\\
where $J_{ij}$:\, interaction between particles,\\\phantom{where}
$h_i$:\, noisy image,\enskip
$\sigma_i$:\, denoised image

\textbf{Problem:} \quad $\pderiv{G(p)}{p_{i\ell}} = 0$ \enspace s.t.\enspace $\sum_{\ell'} p_{i\ell'} = 1 \: \forall i$

\textbf{Solution:}\quad
with the \textit{mean field} $h_i = [\cdots h_{i\ell} \cdots]^\top$
\\
\highlight*{$h_{i\ell} \coloneqq \pderiv{\E{R(c)}}{p_{i\ell}} = \E[c\sim p_{\mid i\to\ell}]{R(c)}$} $\leftarrow \substack{\text{object }i\text{ chooses}\\\text{class } \ell}$
\\
\highlight*{$p_{i\ell} = \eu^{-\beta h_{i\ell}} / Z_i$}

\textbf{EM-like Algo:}\enspace
Iteratively \enspace
1.~Pick~random $i$ \enspace
2.~$h_i^{\mathrm{new}} {\leftarrow} p_j^{\mathrm{old}}$ \enspace
3.~$p_i^{\mathrm{new}} {\leftarrow} h_i^{\mathrm{new}}$ \enspace
until converged.

\subsubsection{Smooth \textit{\rmfamily k}-means
\qquad\normalfont\sffamily\color{gray}scr.20 (p. 39)}

$R(c\mid X) =
{\color{Green} \sum_i \lVert x_i - y_{c_i} \rVert^2}
+
{\color{OrangeRed} \frac\lambda2 \sum_i \sum_{j\in N(i)} \mathbb I_{\{c_i \neq c_j\}}}$
\\
where the second term measures {\color{OrangeRed} \#violations} of these neighbourhood constraints.

$\implies h_{i\ell} = \norm{x_i - y_\ell}^2 + \lambda \sum_{j\in N(i)} p_{j\ell} + \mathit{const}_i$

% ===