\section{Cup product, continued}
We can construct an explicit map $S^p(X)\otimes S^q(Y)\xrightarrow{\times} S^{p+q}(Y)$ via:
\begin{equation*}
(f\times g)(\sigma)=f(\sigma_1\circ\alpha_p)g(\sigma_2\circ\omega_q)
\end{equation*}
where $\alpha_p:\Delta^p\to\Delta^{p+q}$ takes $k\mapsto k$ where $k\in[p]$, and $\omega_q:\Delta^q\to\Delta^{p+q}$ sends $\ell\mapsto \ell+p$ where $\ell\in[q]$.

I would like to modify this definition a bit, and correct what I said. I think it's more natural to put a sign so that:
\begin{equation*}
(f\times g)(\sigma)=(-1)^{pq}f(\sigma_1\circ\alpha_p)g(\sigma_2\circ\omega_q)
\end{equation*}
You'll see why we're adding the sign soon. The claim is that $\times$ is a chain map, so we get a map $ H(S^p(X)\otimes S^q(Y))\to H_{p+q}(X\times Y)$. There is a map $ H_p(X)\otimes H_q(Y)\xrightarrow{\mu} H(S^p(X)\otimes S^q(Y))$ defined as follows. Given chain complexes $C_\bullet$ and $D_\bullet$, define $\mu: H(C_\bullet)\otimes H(D_\bullet)\to H(C_\bullet\otimes D_\bullet)$. We've done this before, but let me just say: $\mu:[x]\otimes [y]\mapsto [x\otimes y]$. For some reason, he said that we'll not call this $\mu$. But I don't want to delete the $\mu$ so I'll just leave it there.

We get the cup product as follows. Given $\Delta:X\to X\times X$, we get $\cup: H_p(X)\otimes H_q(X)\to H_{p+q}(X\times X)\xrightarrow{\Delta^\ast} H_{p+q}(X)$. We proved that there was a class $1\in H_0(X)=\Map(\pi_0(X),R)\ni (\alpha\mapsto 1)$. This acts as a unit for the cup product. Also, the cup product is strictly associative.
\begin{definition}
Let $R$ be a commutative ring. A \emph{graded $R$-algebra} is a graded $R$-module $\cdots,A_{-1},A_0, A_1,A_2,\cdots$ (integer graded sequence) (some people take a direct sum, but I find no reason for doing that) with maps $A_p\otimes_R A_q\to A_{p+q}$ and a map $R\to A_0$ (that determines the unit), that makes the following diagram commute.
\begin{equation*}
\xymatrix{
	A_p\otimes_R (A_q\otimes_R A_r)\ar[r]\ar[d] & A_p\otimes_R A_{q+r}\ar[d]\\
	(A_{p+q})\otimes_R A_r \ar[r] & A_{p+q+r}
}
\end{equation*}
\end{definition}
\begin{definition}
A graded $R$-algebra $A$ is said to be (graded) commutative id the following diagram commutes:
\begin{equation*}
\xymatrix{
	x\otimes y\ar@{|->}[rr] & & (-1)^{|x|\cdot|y|}y\otimes x\\
	A\otimes A\ar[rr]^{\tau}\ar[dr] & & A\otimes A\ar[dl]\\
	 & A &
}
\end{equation*}
\end{definition}
We claim that $ H_\ast(X)$ forms a graded commutative ring under the cup product. This is nontrivial. On the cochain level, this is clearly not graded commutative. We're going to have to work hard -- in fact, so hard that you're going to do some of it for homework.

We'll do a chain level construction. I say there's a map $\alpha:S_n(X\times Y)\to \bigoplus_{p+q=n}S_p(X)\otimes S_n(Y)$. First I'll tell you what happens to $n$-simplices $\sigma:\Delta^n\to X\times Y$. Let $\sigma_1:\Delta^n\to X\times Y\to X$, and similarly for $\sigma_2$. Define $S_n(X\times Y)\xrightarrow{\alpha} \bigoplus_{p+q=n}S_p(X)\otimes S_n(Y)$ by sending:
\begin{equation*}
\sigma\mapsto\sum_{p+q=n}(\sigma_1\circ\alpha_p)\otimes(\sigma_2\otimes\omega_q)
\end{equation*}
We claim that this is a chain map, and that this induces the cross product on cochains.
\begin{remark}
This map $\alpha$ is called the Alexander-Whitney map.
\end{remark}
If I have two chain complexes, I want to consider a map $\mu:\Hom(C_\bullet,R)\otimes \Hom(D_\bullet,R)\to \Hom(C_\bullet\otimes D_\bullet,R)$ given by $f\otimes g\mapsto(x\otimes y\mapsto (-1)^{pq}f(x)g(y))$ where $|f|=|x|=p$ and $|g|=|y|=q$. I haven't quite done the right thing here, have I? I have to write:
\begin{equation*}
f\otimes g\mapsto\begin{cases}
(x\otimes y\mapsto (-1)^{pq}f(x)g(y)) & |x|=|f|=p, |y|=|g|=q\\
0 & \text{else}
\end{cases}
\end{equation*}
You should check that this is a chain map.

Recall that we have: $S^p(X)\otimes S^q(Y)=\Hom(S_p(X),R)\otimes_R \Hom(S_q(Y),R)$. The map $\mu$ we constructed just now gives a map $\Hom(S_p(X),R)\otimes_R \Hom(S_q(Y),R)\xrightarrow{\mu}\Hom(S_p(X)\otimes S_q(Y),R)\xrightarrow{\alpha}\Hom(S_{p+q}(X\times Y),R)=S^{p+q}(X\times Y)$. This is exactly the cross product $\times:S^p(X)\otimes S^q(Y)\to S^{p+q}(X\times Y)$.

Now, $\alpha$ is a natural transformation. Acyclic models comes into play. For homework, you're going to check that the following diagram commutes.
\begin{equation*}
\xymatrix{S_\ast(X\times Y)\ar[r]^{T_\ast}\ar[d]_{\alpha_{X,Y}} & S_\ast(Y\times X)\ar[d]^{\alpha_{Y,X}}\\
S_\ast(X)\otimes_R S_\ast(Y)\ar[r]^{\tau} & S_\ast(Y)\otimes_R S_\ast(X)}
\end{equation*}
Acyclic models helps us prove things like this.

All of this implies that $ H_\ast(X;R)$ is graded commutative. It's a theorem that you can't find a commutative multiplication. This is where Steenrod operations come from. They're called cohomology operations, and we'll talk more about this in 18.906.

My goal is to compute the cohomology of some space.
\begin{prop}
$ H^\ast(X)\otimes H^\ast(Y)\xrightarrow{\times} H^\ast(X\times Y)$ is a $R$-algebra homomorphism.
\end{prop}
If $A$ and $B$ are graded $R$-algebras, then $(A\otimes B)_n=\bigoplus_{p+q=n}A_p\otimes_R B_q$, and $(a\otimes b)(a^\prime\otimes b^\prime)=(-1)^{|a^\prime|\cdot|b|}aa^\prime\otimes bb^\prime$. This tensor product is graded commutative if $A$ and $B$ are.
\begin{proof}
I have $\Delta_X:X\to X\times X$ and $\Delta_Y:Y\to Y\times Y$. I also have $\Delta_{X\times Y}:X\times Y\to X\times Y\times X\times Y$, which factors as $(1\times T\times 1)\circ(\Delta_X\times \Delta_Y)$. Let $\alpha_1,\alpha_2\in H^\ast(X)$ and $\beta_1,\beta_2\in H^\ast(Y)$. Then $\alpha_1\times \beta_1,\alpha_2\times\beta_2\in H^\ast(X\times Y)$. I want to calculate what $(\alpha_1\times\beta_1)\cup(\alpha_2\times\beta_2)$ is. Let's see:
\begin{align*}
(\alpha_1\times\beta_1)\cup(\alpha_2\times\beta_2) & = \Delta_{X\times Y}^\ast(\alpha_1\times\beta_1\times\alpha_2\times\beta_2)\\
& = (\Delta_X\times\Delta_Y)^\ast(1\times T\times 1)^\ast(\alpha_1\times\beta_1\times\alpha_2\times\beta_2)\\
& = (\Delta_X\times\Delta_Y)^\ast(\alpha_1\times T^\ast(\beta_1\times\alpha_2)\times\beta_2)\\
& = (-1)^{|\alpha_2|\cdot|\beta_1|}(\Delta_X\times\Delta_Y)^\ast(\alpha_1\times\alpha_2\times\beta_1\times\beta_2)
\end{align*}
Now, I have a diagram:
\begin{equation*}
\xymatrix{
	 H^\ast(X\times Y) & \ar[l]^{\times_{X\times Y}} H^\ast(X)\otimes_R H^\ast(Y)\\
	 H^\ast(X\times X\times Y\times Y)\ar[u]^{(\Delta_X\times\Delta_Y)^\ast} & H^\ast(X\times X)\otimes H^\ast(Y\times Y)\ar[l]_{\times_{X\times X,Y\times Y}}\ar[u]^{\Delta_X^\times\otimes\Delta_Y^\ast}
}
\end{equation*}
This diagram commutes because the cross product is natural. This is exactly what commutativity of the diagram means. This means that:
\begin{align*}
(\alpha_1\times\beta_1)\cup(\alpha_2\times\beta_2) & = (-1)^{|\alpha_2|\cdot|\beta_1|}(\Delta_X\times\Delta_Y)^\ast(\alpha_1\times\alpha_2\times\beta_1\times\beta_2)\\
& = (-1)^{|\alpha_2|\cdot|\beta_1|}(\alpha_1\cup\alpha_2)\times(\beta_1\cup\beta_2)
\end{align*}
That's exactly what we wanted.
\end{proof}
\begin{example}
How about $ H^\ast(S^p)$? Let $p>0$. This is: $ H^k(S_p) = \begin{cases}\Z & k=0,p\\ 0 & \text{else}\end{cases}$. Say that $\sigma_p$ generates $ H^p(S^p)$. We now have $ H^\ast(S^p)\otimes H^\ast(S^q)\to H^\ast(S^p\times S^q)$. From the K\"unneth theorem, we know that $ H^k(S^p\times S^q)=\begin{cases}\Z & k=0,p,q,p+q\\ 0 & \text{else}\end{cases}$. This structure $ H^\ast(S^p)\otimes H^\ast(S^q)\cong\Z[\sigma_p,\sigma_q]/(\sigma_p^2,\sigma_q^2)\cong H^\ast(S^p\times S^q)$ where $\sigma_p\sigma_q=(-1)^{|p|\cdot|q|}\sigma_q\sigma_p$ where by $\Z[\sigma_p,\sigma_q]$ I mean the free $\Z$-algebra on $\sigma_p,\sigma_q$. When $p=q=1$, this gives an exterior algebra.
\end{example}
This is to be contrasted with $X=S^p\vee S^q\vee S^{p+q}$. This has the same homology as the product of two spheres. But in cohomology, the product of the generators must be zero. There is a diagram:
\begin{equation*}
\xymatrix{
	X\ar[r] & S^p\vee S^q\\
	S^p\ar@{^(->}[u]
}
\end{equation*}
On cohomology, notice that $\sigma_p\sigma_q=0$ in $ H^\ast(S^p\vee S^q)$ because there's no $(p+q)$-dimensional cohomology. Therefore we find that $S^p\vee S^q\vee S^{p+q}\not\simeq S^p\times S^q$.

It's very interesting to think about what the attaching map is:
\begin{equation*}
\xymatrix{
	S^{p+q-1}\ar[r]\ar[d] & S^p\vee S^q\ar[d]\\
	D^{p+q}\ar[r] & S^p\times S^q
}
\end{equation*}
On Wednesday we'll have a relaxed talk about surfaces. BTW I might not TeX that.


\documentclass{article}
\usepackage[section]{placeins}
\usepackage{graphicx, wrapfig, amsmath, amssymb, physics, hyperref, mathtools}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
    }

\author{Yaghoub Shahmari}
\title{Report - Problem Set No 6}
\date{\today}
\graphicspath{ {../Figs/} }

\begin{document}
    \maketitle
    \section{RC circuit equation}
    \textbf{Basic description:}

    In an RC circuit, the amount of electric charge passing through the circuit is as follows:

    $$R \frac{d q}{d t}+\frac{q}{C}=V_0 \Rightarrow \dot{q} = \frac{V_0}{R} - \frac{q}{RC}  \xRightarrow[q_{(0)} = 0]{q_0 \coloneqq V C} q_{(t)}=q_0 (1-e^{-t/RC})$$

    where C, V, and R are the potential difference, capacitor capacity, and circuit resistance, respectively.

    I have solved this equation with RK1(Euler), RK2, and RK4 methods. Below you can see the results next to the exact answer:

    \textbf{The results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.25]{/Q1/RCEu}
        \label{fig:1.1}
        \includegraphics[scale = 0.25]{/Q1/RKComp}
        \label{fig:1.2}
        \caption{Plot of the results of the mentioned methods.}
    \end{figure}

    \pagebreak

    And at last, we want to find and analyze the effect of $\Delta t$ in the amount of global error for each method.
    We calculated the global error for a range of $\Delta t$ and drew the results in the following graph:

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.3]{/Q1/error}
        \label{fig:1.3}
        \caption{Plot of the global error for each $\Delta t$.}
    \end{figure}

    To show the effect of floating-point error,
    I exported data in two types of float16 and float64.
    For float64 we can't find floating-point error well but in float16 type,
    it is easier to find the effect.

    As the final part of this section, we will solve the above equation with an unstable method presented in class. The results are as follow:

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.5]{/Q1/Instability}
        \label{fig:1.4}
        \caption{Plot of the results of the mentioned method and the exact answer.}
    \end{figure}

    \section{Simple Harmonic Osillation}
    \textbf{Basic description:}

    In this section, we want to solve the second-order equation of simple harmonic oscillation using numerical methods.
    The oscillator follows this equation:

    $$
    \ddot{x} = -\omega x \Rightarrow
    \begin{cases}
        x(t) = x_0\cos(\omega t) \\
        v(t) = x_0\omega\sin(\omega t)
    \end{cases}
    $$

    which the $\omega$ is angular velocity and $x_0$ is the initial position.

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.3]{/Q2/SHO}
        \label{fig:2.1}
        \caption{Plot of the numerical solution for each method and the exact answer for simple harmonic oscillation.}
    \end{figure}

    As you can see, the RK4 method has the best accuracy among the other methods.
    Also, it is obvious that RK1 and RK2 methods are unstable and do not follow the energy conservation.

    As the final part of this section,
    we drew the phase diagram of oscillation.

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.3]{/Q2/Phase}
        \label{fig:2.2}
        \caption{Phase diagram of the oscillation.}
    \end{figure}

    In this section as well, we can see the energy conservation for each method and we can see that RK1 and RK2 methods do not follow that.

    \section{Logistic Map and bifurcations}
    \textbf{Basic description:}

    For this section, I wrote a code that creates a standard logistic map,
    finds bifurcation points, and exports their data.
    Then, find the wanted constant using exported data.

    \textbf{The results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.4]{/Q3/logmap.jpg}
        \label{fig:3.1}
        \includegraphics[scale = 0.4]{/Q3/R-N}
        \label{fig:3.2}
        \caption{Logistic Map diagram and bifurcation points.}
    \end{figure}

    Calculated constants:
    $$\delta = 4.6316,\ \alpha = 2.7143$$(take a look at the notebooks)

    \pagebreak

    \centering
    \textbf{The whole data I gathered is in \href{https://github.com/shahmari/ComputationalPhysics-Fall2021/tree/main/ProblemSet7/Data}{this link}}

    Thanks for watching :)
\end{document}


\chapter{Introduction to partitions}

\section{Background}

A partition of a positive integer $n$ is a way of writing $n$ as the sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition. The notation $\lambda \vdash n$ means that $\lambda$ is a partition of $n$.

A summand in a partition is also called a part. The length of a partition refers to the number of parts in the partition and is denoted by $n(\lambda)$ for a partition $\lambda$. Its largest part, is denoted by $l(\lambda)$. As an example, 4 can be partitions in five distinct ways as follows
\begin{align*}
4 &= 4
  \\&= 3 + 1 = 2 + 2
  \\&= 2 + 1 + 1
  \\&= 1 + 1 + 1 + 1.
\end{align*}

Where, $n((2,1,1)) = 3$ and $l((2,1,1)) = 2$.
\newpage

\section{Definitions and Terminology}
\subsection{$q$-Pochhammer Symbol}

The $q$-Pochhammer symbol is defined as

\begin{equation*}
    (a)_0 := (a;q)_0 := 1, \quad (a; q)_n := \prod_{k=0}^{n-1} (1- aq^k) \quad n \geq 1,
\end{equation*}
\begin{equation*}
    (a)_\infty := (a;q)_\infty := \prod_{k=0}^{\infty} (1- aq^k) \quad |q| < 1.
\end{equation*}
If the identification of $q$ is clear, we omit $q$ from the notation.
We state several basic identities used for simplification later as mentioned in [5].
\begin{equation}
    (q^a;q^b)_\infty(-q^a;q^b)_\infty = (q^{2a};q^{2b})_\infty
\end{equation}
\begin{equation}
    (cq^a;q^{2b})_\infty(cq^{a+b};q^{2b})_\infty = (cq^a;q^b)_\infty
\end{equation}
We will be using the following shorthand notations throughout the report and are also used in [1, 18, 19]:
\begin{equation*}
    (a_1,...,a_k;q)_n := (a_1,q)_n...(a_k;q)_n
\end{equation*}
\begin{equation*}
    J_b := (q^b;q^b)_\infty
\end{equation*}
\begin{equation*}
    J_{a,b} := (q^a,q^{b-a},q^b;q^b)_\infty
\end{equation*}
\begin{equation*}
    \chi(q) := (-q;q^2)_\infty
\end{equation*}
\subsection{Ramanujan's General Theta Function}
The famous Ramanujan's Theta function used in various proofs is as follows:
\begin{equation}
    f(a, b) := \sum_{n = -\infty}^\infty a^{n(n+1)/2}b^{n(n-1)/2}, \quad |ab| < 1.
\end{equation}
Some shorthand related to the above generating functions will be used as [5]
\begin{align*}
    \varphi(q) &:= f(q,q) = (-q,-q,q^2;q^2)_\infty,
    \\ \psi(q) &:= f(q,q^3) = \frac{(q^2;q^2)_\infty}{(q;q^2)_\infty}.
\end{align*}

\subsection{Definitions Relating to Partitions}
\subsubsection{Partition Functions}
If $n$ is a positive integer, let $p(n)$ denote the number of unrestricted representations of $n$ as a sum of positive integers, where representations with different orders of the same summands are not regarded as distinct. We call $p(n)$ the partition function. We further use the notation $p_m(n)$ to denote the number of partitions of $n$ into parts that are no larger than $m$.

In general for a set $S$, $p(S, m, n)$ denotes the number of partitions of $n$ into exactly $m$ parts of $S$. Furthermore, we denote $p(m,n)$ as the number of partitions of $n$ into exactly $m$ parts.

We denote $Q(n)$ as the number of partitions into distinct parts. More generally, $Q( S, m, n)$ denotes the number of partitions of $n$ into $m$ distinct parts of $S$. Similarly $Q( m, n )$ is the number of partitions of $n$ into exactly $m$ distinct parts.

\subsubsection{Overpartitions}
An overpartition of $n$ is a partition of $n$ in which the first occurrence of a number may be over-lined. For example, the over partitions of 3 are:
\begin{align*}
      \{ 3, \overline 3
    , 2 + 1 , 2 + \overline 1 , \overline 2 + 1 , \overline 2 + \overline 1
    , 1 + 1 + 1 , \overline 1 + 1 + 1 \}
\end{align*}

The number of overpartitions of $n$ is denoted by $\overline p(n)$. This results in
    $$\overline p(n) = \sum_{n_1 + n _2 = n} p(n_1)*Q(n_2)$$

which is a discrete convolution between $p$ and $Q$, giving us $\overline p = p*Q$. Analogous to Dyson's rank function definitions for partitions we have $\overline N(s, n)$ and $\overline N(s, m, n)$ for overpartitions.

\subsection{Generating Functions}
The term generating function is used to describe an infinite sequence of numbers $(a_n)$ by treating them as the coefficients of a series expansion. This infinite series is the generating function.

The generating function for the partition function $p(n)$ is represented as
\begin{align*}
\sum_{n=0}^\infty p(n)q^n
&= \sum_{n_i \geq 0}q^{1n_1 + 2n_2 + \dots + kn_k + \dots}\\
&= \frac{1}{1-q} \frac{1}{1-q^2} \dots \frac{1}{1-q^k} \dots\\
&= \frac{1}{\prod_{k=0}^\infty (1-qq^k)}\\
&= \frac{1}{(q;q)_\infty}
\end{align*}
\newpage
For general partition functions, we get
\begin{align*}
\sum_{n=0}^\infty \sum_{m=0}^\infty p( S, m, n )z^mq^n &= \prod_{k \epsilon S} \frac{1}{1-zq^k},\\
\sum_{n=0}^\infty \sum_{m=0}^\infty Q( S, m, n )z^mq^n &= \prod_{k \epsilon S} (1+zq^k).
\end{align*}

The generating function for overpartitions,  $\sum_{n=0}^\infty \overline p(n)q^n$ is the product of general partitions and that for partitions with distinct parts as it is given by their convolution. Therefore,
\begin{align*}
\sum_{n=0}^\infty \overline p(n)q^n = \frac{(-q;q)_\infty}{(q;q)_\infty}
\end{align*}


\appendix
\section{Appendix}
%\pagenumbering{roman}
\subsection{Deriving the approximate expression of 2SLS Bias}
This derivation has been adopted from \cite{results}. Start with the representation of the 2SLS estimator as shown in equation (2.8).
\begin{equation}
\hat{\beta }_{2SLS}=(\textbf{X}'P_{Z}\textbf{X})^{-1}\textbf{X}'P_{Z}\textbf{Y}=\beta+(\textbf{X}'P_{Z}\textbf{X})^{-1}\textbf{X}'P_{Z}\varepsilon
\end{equation}
where $P_{Z}=\textbf{Z}(\textbf{Z}'\textbf{Z})^{-1}\textbf{Z}'$ is the projection matrix.
So the bias of $\hat{\beta }_{2SLS}$ will be
\begin{equation}
\hat{\beta }_{2SLS}-\beta = (\textbf{X}'P_{Z}\textbf{X})^{-1}(\pi'\textbf{Z}'+\eta')P_{Z}\varepsilon
                          = (\textbf{X}'P_{Z}\textbf{X})^{-1}\pi'\textbf{Z}'\varepsilon +(\textbf{X}'P_{Z}\textbf{X})^{-1}\eta' P_{Z}\varepsilon
\end{equation}
Using group asymptotics the expectation of this expression can presented as:
\begin{equation}
    E[\hat{\beta }_{2SLS}-\beta ]\approx (E[{\textbf{X}}'P_{Z}\textbf{X}])^{-1}E[{\pi}'{\textbf{Z}}'\varepsilon ]+(E[{\textbf{X}}'P_{Z}\textbf{X}])^{-1}E[{\eta }'P_{Z}\varepsilon ]
\end{equation}
$Z_{i}$ instruments are uncorrelated with $\varepsilon_{i}$ and $\eta_{i}$, so $E[{\pi }'{\textbf{Z}}'\varepsilon ]=0 $ and we will have
\begin{equation}
    E[\hat{\beta }_{2SLS}-\beta ]\approx (E[{\textbf{X}}'P_{Z}\textbf{X}])^{-1}E[{\pi}'{\textbf{Z}}'\varepsilon ]+(E[{\textbf{X}}'P_{Z}\textbf{X}])^{-1}E[{\eta }'P_{Z}\varepsilon ]=(E[{\textbf{X}}'P_{Z}\textbf{X}])^{-1}E[{\eta }'P_{Z}\varepsilon]
\end{equation}
Substituting the first stage equation $\textbf{X}=\textbf{Z}\pi +\eta$ we have
\begin{equation}
E[\hat{\beta }_{2SLS}-\beta ]\approx (E[({\pi }'{\textbf{Z}}'+{\eta }')P_{Z}(\textbf{Z}\pi +\eta )])^{-1}E[{\eta }'P_{Z}\varepsilon ]
\end{equation}
We have that $E[{\pi }'{\textbf{Z}}'\eta ]=0 $, so
\begin{equation}
    E[\hat{\beta }_{2SLS}-\beta ]\approx[E({\pi }'{\textbf{Z}}'\textbf{Z}\pi )+E({\eta }'P_{Z}\eta )]^{-1}E({\eta }'P_{Z}\varepsilon )
\end{equation}
Notice that ${\eta }'P_{Z}\eta$ is a scalar and is equal to its trace. $P_{Z}$ is an idempotent matrix, so its trace is equal to its rank, Q. So
\begin{equation}
    E({\eta }'P_{Z}\eta)=E[tr({\eta }'P_{Z}\eta)]=E[tr(P_{Z}\eta{\eta }')]=tr(P_{Z}E[\eta{\eta }'])=tr(P_{Z}\sigma _{\eta }^{2}I)=\sigma _{\eta }^{2}tr(P_{Z})=\sigma _{\eta }^{2}Q
\end{equation}
With a similar technique we can show that  $E({\eta }'P_{Z}\varepsilon)$ is equal to $\sigma _{\varepsilon \eta }Q$.
Substituting these results in equation (A.6) we have
\begin{equation}
   E[\hat{\beta }_{2SLS}-\beta]\approx\sigma _{\varepsilon \eta }Q[E({\pi }'{\textbf{Z}}'\textbf{Z}\pi )+\sigma _{\eta }^{2}Q]^{-1}=\frac{\sigma_{\varepsilon\eta }}{\sigma  _{\eta }^{2}}[\frac{E(\pi'\textbf{Z}'\textbf{Z}\pi)/Q}{\sigma _{\eta }^{2}}+1]^{-1}
\end{equation}
The population F-statistic for the first stage regression is the following
\begin{equation}
    F=\frac{E(\pi'\textbf{Z}'\textbf{Z}\pi)/Q}{\sigma _{\eta }^{2}}
    \end{equation}
So (A.8) can be expressed as
\begin{equation}
    E[\hat{\beta }_{2SLS}-\beta]\approx \frac{\sigma_{\varepsilon\eta }}{\sigma  _{\eta }^{2}}\frac{1}{F+1}
\end{equation}

Assume that the ${\pi}$ coefficients are zero and $F=0$. In this case $\sigma_{X}^{2}=\sigma_{\eta }^{2}$ and
\begin{equation}
    E[\hat{\beta }_{2SLS}-\beta]\approx \frac{\sigma_{\varepsilon\eta }}{\sigma  _{X}^{2}}
\end{equation}

Thus, when $\pi\neq 0$ and F is small, then 2SLS will be biased towards OLS.

Note that $\frac{\sigma_{\varepsilon\eta }}{\sigma  _{X}^{2}}$ is also the bias of OLS estimator, because when $\pi=0$, $cov(\varepsilon _{i},X_{i})=\sigma _{\eta \varepsilon }$ and
\begin{equation}
    \beta _{OLS} = \frac{cov(Y_{i},X_{i})}{var(X_{i})} = \frac{cov(\beta X_{i}+\varepsilon  _{i},X_{i})}{var(X_{i})} = \beta + \frac{cov(\varepsilon  _{i},X_{i})}{var(X_{i})} = \frac{\sigma _{\varepsilon \eta }}{\sigma _{X}^{2}}
\end{equation}
Hence in this case OLS and 2SLS estimators on average are the same. And if $\pi$ is different from zero, $\hat{\beta }_{2SLS}$ will be biased in the direction of OLS estimator. If we add weak instruments to the regression bias of 2SLS will only increase. \\

\newpage


\chapter{Axiomatic Construction of Rooted Trees}

Here is one way of considering rooted trees. We start with a set $N$, whose
elements we call \emph{nodes}.  We'll assume that $N \neq \emptyset$, because
empty trees are not very interesting. Then we introduce an irreflexive,
antisymmetric relation on $N$, noted $<$:
\begin{align}
n \nless n & & \forall \; n \in N & & \text{irreflexivity} \\
m < n \Rightarrow n \nless m &  & \forall \; m, n \in N & &  \text{antisymmetry}
\end{align}
In other words, $<$ is a binary relation on $N$ such that no node is
related to itself, and if node $n$ is related to $m$, then $m$ is \emph{not}
related to $n$. If $m < n$, we say that $m$ is a \emph{parent} of $n$.

Now we introduce axioms that produce rooted trees.
\begin{axiom}
There exists a node that has no parent:
\[ \exists \; r \in N \; | \; n \nless r \qquad \forall \; n \in N \]
\end{axiom}

\begin{axiom}
There is only one node that has no parent. Let $S = \{x \in N\,|\, n \nless x \; \forall \; n \in N \}$ be the set of parent-less nodes of $N$. Then:
\[ r, s \in S \quad \Rightarrow \quad r = s \qquad \forall \;r, s \in N \]
\end{axiom}

The unique parent-less node of $N$ is called the \emph{root}.

\begin{axiom}
Every node except the root has a parent.
\end{axiom}

\begin{axiom}
No node has more than one parent.
\end{axiom}

We can now speak of \emph{the} parent of node $n$ (unless $n$ is the root), and we will note it $\mathrm{par}(n)$.


\section{Definite Integrals \& Antiderivatives}

\subsection{Definition of a Definite Integral}
We saw several approximations for the areas under any type of curve.
We also saw how these approximations get better the narrower our ``strips''.
In the limit, we get exactly the area under the curve, which defines the definite integral.

\begin{definition}
	Let $f$ be continuous on the closed interval $[a,b]$.
	Let this interval be partitioned into $n$ equal\footnote{Technically, the intervals don't need to be of equal size, as long as the width of all intervals goes to 0 in the limit.} sub-intervals, each of length $\Delta x = \frac{b-a}{n}$.
	The definite integral of $f$ over $[a,b]$ is given by
	\begin{equation*}
		\int_{a}^{b}{f(x)\mathrm{d}x} = \lim_{n\to \infty}{\sum_{k=0}^{n-1}{f(c_k)\Delta x}}
	\end{equation*}
	where $c_k$ is any arbitrary value in the $k$th sub-interval.
	This particular type of limit of a sum is called a Riemann Sum.
\end{definition}

Note that the left, right, and midpoint approximations are all different ways of choosing $c_i$, all of which in the limit give the area under the curve and definite integral from $a$ to $b$.

\begin{example}
	Find the value of the following definite integral using Riemann sums.
	\begin{equation*}
		\int_{0}^{1}{x^2\d{x}}.
	\end{equation*}
\end{example}
\begin{answer}
	We'll use a Riemann sum with a left-endpoint approximation.
	\begin{align*}
		\int_{0}^{1}{x^2\d{x}} &= \lim_{n\to\infty}{\sum_{k=0}^{n-1}{f\left(0+\frac{k}{n}\right)\frac{1-0}{n}}} \\
		&= \lim_{n\to\infty}{\sum_{k=0}^{n-1}{\left(\frac{k}{n}\right)^2\frac{1}{n}}} \\
		&= \lim_{n\to\infty}{\sum_{k=0}^{n-1}{\frac{k^2}{n^3}}} \\
		&= \lim_{n \to \infty}{\frac{1}{n^3}\left(\frac{n(n-1)(2n-1)}{6}\right)} \\
		&= \lim_{n\to\infty}{\frac{1}{6}\left(2-\frac{3}{n}+\frac{1}{n^2}\right)} \\
		&= \frac{1}{3}.
	\end{align*}
\end{answer}

Note that area below the $x$-axis is counted as negative area.
In general
\begin{equation*}
	\int_{a}^{b}{f(x)\d{x}} = \text{ area above $x$-axis } - \text{ area below $x$-axis}.
\end{equation*}

\subsection{Basic Properties of Definite Integrals}
The following are properties of definite integrals.
Many should look familiar from properties of limits and derivatives.
Let $f$ and $g$ be continuous functions of $x$.
Let $a$, $b$, and $c$ be real constants where $a \neq b$.
Let $f_{[a,b]}$ be the values of $f$ on the interval $[a,b]$.
\begin{align*}
	\textbf{Order of Integration Rule: }& \int_{a}^{b}{f(x)\d{x}} = -\int_{b}^{a}{f(x)\d{x}} \\
	\textbf{Sum and Difference Rule: }& \int_{a}^{b}{(f(x) \pm g(x))\d{x}} = \int_{a}^{b}{f(x)\d{x}} \pm \int_{a}^{b}{g(x)\d{x}} \\
	\textbf{Zero Rule: }& \int_{a}^{a}{f(x)\d{x}} = 0 \\
	\textbf{Constant Multiple Rule: }& \int_{a}^{b}{cf(x)\d{x}} = c\int_{a}^{b}{f(x)\d{x}} \\
	\textbf{Additivity Rule: }& \int_{a}^{b}{f(x)\d{x}} + \int_{b}^{c}{f(x)\d{x}} = \int_{a}^{c}{f(x)\d{x}} \\
	\textbf{Max-Min Rule: }& (b-a)\min{f_{[a,b]}} \leq \int_{a}^{b}{f(x)\d{x}} \leq (b-a)\max{f_{[a,b]}} \\
	\textbf{Domination Rule: }& \min{f_{[a,b]}} \geq \max{g_{[a,b]}} \implies \int_{a}^{b}{f(x)\d{x}} \geq \int_{a}^{b}{g(x)\d{x}}.
\end{align*}

\subsubsection{Mean Value Theorem for Definite Integrals}
The mean value of $f$ over the interval $[a,b]$ is given by
\begin{equation*}
	\frac{1}{b-a}\int_{a}^{b}{f(x)\d{x}}.
\end{equation*}
You can effectively think of this as the definition of the average: adding up all the values and dividing by the number of values.
This is exactly what happens if you work through the Riemann sums.
Much like the Mean Value Theorem for Derivatives, since the function is continuous, it will take on its average value somewhere in the interval.
\begin{theorem}[Mean Value Theorem for Definite Integrals]
	If $f$ is continuous on the interval $[a,b]$, then there is some point $c$ in the interval such that
	\begin{equation*}
		f(c) = \frac{1}{b-a}\int_{a}^{b}{f(x)\d{x}}.
	\end{equation*}
\end{theorem}

\begin{figure}[H]
	\label{mvt_integrals}
	\centering
	\includegraphics[width = 0.33\textwidth]{./integrals/mvt.png}
	\caption{A continuous function always takes on its mean value}
\end{figure}

\begin{example}
	A plane's airspeed is given by $v(t) = t/8\text{, }t\geq 0$.
	What what time $t$ does the Mean Value Theorem guarantee that the plane's speed was 1?
	When is this time?
\end{example}
\begin{answer}
	Rather than go through the trouble of evaluating a limit to find the definite integral, we can notice that the graph of the plane's airspeed is a triangle, which we know how to find the area of geometrically.
	\begin{equation*}
		\text{Area}(t) = \frac{1}{2}t v(t).
	\end{equation*}

	Dividing by the length of the interval to get the average airspeed,
	\begin{equation*}
		\text{Avg}(t) = \frac{1}{t-0}\hspace{3pt}\frac{1}{2}tv(t) = \frac{1}{2}v(t).
	\end{equation*}

	Solving for $t$ when $\text{Avg}(t)=1$,
	\begin{equation*}
		\frac{1}{2}\hspace{3pt}\frac{t}{8} = 1 \implies t = 16.
	\end{equation*}

	So, at time $t=16$, the Mean Value Theorem tells us that the plane's speed was 1.
	Looking at the graph, we can see that at time $t=8$ the plane's speed was indeed 1.
\end{answer}

\subsection{Fundamental Theorem of Calculus}
Although it's nice to be able to evaluate definite integrals for numerical bounds, it'd be more convenient if we only had to do the work of integrating once and could then have a function that would tell us the area like so:
\begin{equation*}
	F(x) = \int_{a}^{x}{f(t)\d{t}}.
\end{equation*}
Let's try taking the derivative of $F$ using the limit definition.
\begin{align*}
	F^\prime(x) &= \lim_{\Delta x \to 0}{\frac{F(x+\Delta x)-F(x)}{\Delta x}} \\
	&= \lim_{\Delta x\to 0}{\frac{\int_{a}^{x+\Delta x}f(t)\d{t} - \int_{a}^{x}{f(t)\d{t}}}{\Delta x}} \\
	&= \lim_{\Delta x\to 0}{\frac{\int_{x}^{x+\Delta x}{f(t)\d{t}}}{\Delta x}}\text{ (by Integral Rules)}\\
	&= \lim_{\Delta x\to 0}{\frac{f(k)\Delta x}{\Delta x}}\text{, }x\leq k \leq x + \Delta x\text{ (by Mean Value Theorem)} \\
	&= \lim_{\Delta x\to 0}{f(k)} \\
	&= f(x) \text{ (by Sandwich Theorem)}.
\end{align*}
Taking the derivative seems to undo the integration.
The same fact applies if we take the integral of a derivative.
Starting with what we've just shown,
\begin{equation*}
	\dd{}{x}\int_{0}^{x}{f(t)\d{t}} = f(x).
\end{equation*}
Let $g(x) = \dd{}{x}f(x)$.
\begin{equation*}
	\dd{}{x}\int_{0}^{x}{g(t)\d{t}} = g = \dd{}{x}f(x).
\end{equation*}
Since the derivatives are equal, we know the functions must differ by at most a constant $C$.
\begin{align*}
	\int_{0}^{x}{g(t)\d{t}} &= f(x) + C \\
	\int_{0}^{x}{\left[\dd{}{x}f(t)\right]\d{t}} &= f(x) + C.
\end{align*}
So, the integral of the derivative gets us a function that differs from the original by at most a constant.

This idea that the integral and derivative undo each other is captured by the Fundamental Theorem of Calculus.
\begin{theorem}[Fundamental Theorem of Calculus]
	Let $f$ be a continuous function on the interval $[a,b]$.
	Then
	\begin{equation*}
		F(x) = \int_{a}^{x}{f(t)\d{t}}
	\end{equation*}
	has a derivative at every point in $[a,b]$, and
	\begin{equation*}
		\dd{F}{x} = \dd{}{x}\int_{a}^{x}{f(t)\d{t}} = f(x).
	\end{equation*}
	Further, if $F$ is the antiderivative of $f$ on $[a,b]$, then
	\begin{equation*}
		\int_{a}^{b}{f(x)\d{x}} = F(b) - F(a).
	\end{equation*}
\end{theorem}

This last equation is especially useful for calculating definite integrals.
Rather than evaluating a limit of a Riemann sum, if we know the antiderivative, we can just evaluate at two points.

\begin{example}
	Evaluate the following definite integral using antiderivatives.
	\begin{equation*}
		\int_{0}^{1}{\frac{\d{x}}{1+x^2}}.
	\end{equation*}
\end{example}
\begin{answer}
	We previously derived that the derivative of $\arctan{x}$ is $\frac{1}{1+x^2}$.
	So, $\arctan{x}$ is the antiderivative of $\frac{1}{1+x^2}$.
	Applying the Fundamental Theorem of Calculus (FTC),
	\begin{align*}
		\int_{0}^{1}{\frac{\d{x}}{1+x^2}} &= \arctan{1} - \arctan{0} \\
		&= \frac{\pi}{4} - 0 \\
		&= \frac{\pi}{4}.
	\end{align*}
\end{answer}

\subsection{Integrals of Symmetric Functions}
Suppose f is continuous on $[-a, a]$. Then we have
\begin{equation*}
	\int_{-a}^{a}{f(x)\d{x}} = 2 \int_{0}^{a}{f(x)\d{x}}
\end{equation*}
if f is even and
\begin{equation*}
	\int_{-a}^{a}{f(x)\d{x}} = 0
\end{equation*}
if f is odd.\bigskip

\noindent
Intuitively, since even functions are symmetric about the $y$-axis, it should make sense that the area under the curve from $-a$ to $a$ should be twice the area under the curve from 0 to $a$.
Likewise, odd functions are symmetric about the origin, so any area under the curve above the $x$-axis will be canceled out by some corresponding area underneath the $x$-axis on the opposite side of the axis.

\begin{example}
	Evaluate the following definite integral.
	\begin{equation*}
		\int_{-1}^{1}{\frac{\tan{x}}{1 + x^2 + x^4}\d{x}}
	\end{equation*}
\end{example}
\begin{answer}
	At first, this looks like a nasty integral to evaluate.
	However, we notice that $f(-x) = -\frac{\tan{x}}{1 + x^2 + x^4}$.
	Since $f(-x) = -f(x)$, the function is odd, and since the limits of integration are additive inverses of one another, we have
	\begin{equation*}
		\int_{-1}^{1}{\frac{\tan{x})}{1 + x^2 + x^4}} = 0.
	\end{equation*}
\end{answer}


\section{Notes on BCH formula}

Baker--Campbell--Hausdorff

\begin{nameddef}{Linear adjoint endomophism}
\begin{align}
	\ad_X^{(0)} Y &\coloneqq Y, \\
	\ad_X^{(n)} Y &\coloneqq \sbr{X, \ad_X^{(n-1)} Y}_-,\qquad\forall n\ge 1.\\
\end{align}
Specifically,
\begin{equation}
	\ad_X Y \coloneqq \ad_X^{(1)} Y \equiv \sbr{X, Y}_-.
\end{equation}
\end{nameddef} % Linear adjoint endomophism

\begin{namedthm}{Lemma}
\begin{equation}
	\ee^{X} Y \ee^{-X} = \sum_{n=0}^{+\infty} \frac{1}{n!}\ad_X^{(n)}Y
	\label{eq:bch-sandwich}
\end{equation}
\label{lem:bch-sandwich}
\end{namedthm} % Lemma

\begin{namedthm}{Corollary}
	For $\sbr{X, Y}_-$ central, i.e.\ commuting with both $X$ and $Y$,
	\begin{equation}
		\ee^X\ee^Y = \ee^{X+Y+\sbr{X, Y}_-/2}.
	\end{equation}
	\label{thm:bch-merging}
\end{namedthm} % Corollary

\begin{namedthm}{Theorem}[Braiding identity]
\begin{equation}
\ee^X \ee^Y = \rfun{\exp}{\sum_{n=0}^{+\infty} \frac{1}{n!}\ad_X^{(n)}Y}\ee^X.
\end{equation}
\label{thm:bch-brading}
\end{namedthm} % Braiding identity

\begin{nameddef}{A factorisation algorithm for quadratic $x$ and $p$}
Given $a_i\in\BbbR$, solve
\begin{align}
	U &= \cfun{\expi}{a_1 x^2 + a_2 \frac{xp+px}{2} + a_3 p^2}
	\nonumber \\
	&= \cfun{\expi}{b_1 x^2} \cfun{\expi}{b_2\frac{xp+px}{2}}
		\cfun{\expi}{b_3 p^2}
		\label{eq:to-be-factorised}
\end{align}
for $b_i$, $i = 1, 2, 3$.

Let
\begin{align}
\rfun{L}{t} &\coloneqq
\cfun{\expi}{t\rbr{a_1 x^2 + a_2 \frac{xp+px}{2} + a_3 p^2}}, \\
\rfun{R}{t} &\coloneqq
\cfun{\expi}{\rfun{c_1}{t} x^2} \cfun{\expi}{\rfun{c_2}{t}\frac{xp+px}{2}}
\cfun{\expi}{\frac{c_3}{t} p^2},
\end{align}
where $0 \le t \le 1$.
One hopes to solve
\begin{equation}
	\rfun{L}{t} = \rfun{R}{t}
	\label{eq:L=R}
\end{equation}
for $\rfun{c_i}{t}$. By setting $t = 0$ and observing $\rfun{L}{1} =
\rfun{R}{1} = U$, one recognises the boundary conditions
\begin{equation}
\rfun{c_i}{0} = 0,\qquad \rfun{c_i}{1} = b_i.
\end{equation}

By \eqref{eq:L=R} one has
\begin{equation}
	\dot{L}L^\dagger = \dot{R}R^\dagger.
	\label{eq:LdLdg=RdRdg}
\end{equation}
Evaluating the right hand side of \eqref{eq:LdLdg=RdRdg} with the formulas
above and comparing the corresponding coefficients, one derives
coefficients
\begin{equation}
	\left\{\begin{array}{l}
		a_1 = \dot c_1 - 2 c_1 \dot{c}_2 + 4 c_1^2 \ee^{-2 c_2}\dot{c}_3 \\
		a_2 = \dot{c}_2 - 4 c_1 \ee^{-2 c_2} \dot{c}_3 \\
		a_3 = \ee^{-2 c_2} \dot{c}_3,
	\end{array}\right.
\end{equation}
which further transforms to
\begin{equation}
	\left\{\begin{array}{l}
		\dot{c}_1 = a_1 + 2 a_2 c_1 + 4 a_3 c_1^2 \\
		\dot{c}_2 = a_2 + 4 a_3 c_1 \\
		\dot{c}_3 = a_3 \ee^{2 c_2}.
	\end{array}\right.
	\label{eq:one-by-one}
\end{equation}
\Cref{eq:one-by-one} can be integrated on by one.
\end{nameddef} % A factorisation algorithm


\section*{Problem 1 Solution}

First, let's note that we will use the shorthand U5 and U8 in subscripts to denote $^{235}$U and $^{238}$U respectively.

To begin we note that the densities of $^{235}$U and $^{238}$U can be found as fractions of the total UO$_2$. Since the partial densities are dependent on the mass of the components, the fractions are equivalent to the weight \% enrichment.
$$ \rho_{\text{UO}_2} = \rho_{\text{U5O}_2} + \rho_{\text{U8O}_2}$$
$$ \begin{array}{lr}
        \rho_{\text{U5O}_2} = w\rho_{\text{UO}_2}    \\
        \rho_{\text{U8O}_2} = (1-w)\rho_{\text{UO}_2}\\
        \end{array} \text{ where } w = 0.05 $$

Now, we can use the fact that the number density of a material, $n$, is equal to the material's density, $\rho$, multiplied by Avogadro's number, $N_A$, and divided by the molar mass, $m$, of the material.
$$ n = \frac{\rho N_A}{m} $$
We find
$$ n_{\text{U5O}_2} = \frac{\rho_{\text{U5O}_2} N_A}{m_{\text{U5O}_2}} $$
$$ n_{\text{U8O}_2} = \frac{\rho_{\text{U8O}_2} N_A}{m_{\text{U8O}_2}} $$
If we make the simplification that $n_{\text{U5}} = n_{\text{U5O}_2}$ and $n_{\text{U8}} = n_{\text{U8O}_2}$, and also substitute our equations for $\rho_{\text{U5O}_2}$ and $\rho_{\text{U8O}_2}$ in terms of $\rho_{\text{UO}_2}$ from above, then we find
\begin{align*}
n_{\text{U5}} &= \frac{w\rho_{\text{UO}_2} N_A}{m_{\text{U5O}_2}} \\
n_{\text{U8}} &= \frac{(1-w)\rho_{\text{UO}_2} N_A}{m_{\text{U8O}_2}} \\
\end{align*}
At this point, we can also separate our molar masses into molar masses of the components, for which we can find data.
$$ m_{\text{U5O}_2} = m_{\text{U5}} + 2m_{\text{O}} $$
$$ m_{\text{U8O}_2} = m_{\text{U8}} + 2m_{\text{O}} $$
We now have enough information to solve for both of these quantities, and so we plug in values. We also note that $n_O = 2(n_{\text{U8}} + n_{\text{U8}})$ since each uranium atom is bonded to 2 oxygen atoms.
\begin{align*}
n_{\text{U5}}	&= \frac{w\rho_{\text{UO}_2} N_A}{m_{\text{U5}} + 2m_{\text{O}}}		& \boxed{n_{\text{U5}}= 1.174 \times 10^{21} \text{ cm}^{-3} }\\
n_{\text{U8}}	&= \frac{(1-w)\rho_{\text{UO}_2} N_A}{m_{\text{U8}} + 2m_{\text{O}}} 	& \boxed{n_{\text{U8}}= 2.205 \times 10^{22} \text{ cm}^{-3} }\\
n_{\text{O}}	&= 2(n_{\text{U5}} + n_{\text{U8}})  									& \boxed{n_{\text{O}}= 4.645 \times 10^{22} \text{ cm}^{-3} }\\
\end{align*}


\documentclass[letterpaper,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath, amssymb, setspace, esint}
\usepackage{algorithm}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage{algpseudocode}
\usepackage{tikz}

\usepackage{fontspec}
\usepackage{minted}

\allowdisplaybreaks[1]

\makeatletter
\newcommand\blx@unitmark{23sp}
\makeatother

\newcommand{\LOne}[1]{ \lvert \lvert #1 \rvert \rvert_1 }
\newcommand{\LTwoSqr}[1]{ \lvert \lvert #1 \rvert \rvert_2^2 }
\newcommand{\LInf}[1]{ \lvert \lvert #1 \rvert \rvert_{\infty} }

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner sep=2pt] (#1) {};}
\newcommand\DrawBox[3][]{%
  \tikz[remember picture,overlay]\draw[#1] ([xshift=-3.5em,yshift=7pt]#2.north west) rectangle (#3.south east);}

\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\Input{\item[\algorithmicinput]}

\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Output{\item[\algorithmicoutput]}

\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

\begin{document}

\section*{Algorithm to compute  $\pi$}

\subsection*{Question 1:}

Construct an algorithm in C++ to approximate $\pi$. Assume that you are working on a project
 with a small development team and your algorithm will be used
 as a part of a larger code base.

\begin{itemize}
  \item Assume that the required precision of the calculation is not known until run-time.
  \item You do not need to determine the error $\lvert x - \pi \rvert$ associated with
   your calculation.
  \item Your algorithm should be able to work with single or double-precision floating point numbers
  \item Assume you have access to a compiler that supports any version of C and/or C++ that you would like
  numbers.
\end{itemize}

You can use any method, libraries etc. that you would
like with the exception of functions that approximate transcendental numbers ( e.g.
 std::asin, M\_PI or similar ).

It might be useful to know that $\pi = 4 \sum_{k=1}^{\infty} \frac{ (-1)^{k+1} }{ 2k - 1 }$
( known as the Gregory Series ). The first few terms in the series are $
4( 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \dots )$.

This is not the only way to compute $\pi$ and there is no special reason why this
 method should be preferred over any other method.

You do need to write a Make file, compiler/linker commands or
explicitly write out \#include statements, but you are responsible for appropriately
  using namespaces.

\subsection*{Possible Answer 1:}

\subsubsection*{Solution using Gregory Series:}
Start with the identity $\pi \approx 4 \sum_{k=0}^{N} \frac{ (-1)^{k+1} }{ 2k - 1 }$

\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{cpp}

    \* run_time_pi.h *\

    namespace wbp {

    template< typename T >
    T partial_sum( T (*f)(uint index), uint sum_start, uint sum_end ) {

       T sum_total = static_cast<T>( 0 );

       for( uint i = sum_start; i <= sum_end ; i++ ) {
           sum_total += f(i);
       }

       return sum_total;

    }

    template < typename T >
    T alt_sign( uint k ) {
       return ( ( k + 1 )%2 == 0 )?( static_cast<T>( 1 ) ):( static_cast<T>( -1 ) );
    }

    template < typename T >
    T gregory_term( uint k ) {

       return alt_sign<T>( k )*static_cast<T>(1) / static_cast<T>( 2*k - 1 );
    }

    template< typename T >
    T gregory_pi( uint num_iter ) {

      static_assert( std::is_floating_point<T>::value,
       "Template parameter must be a floating-point value.");
      return static_cast<T>( 4 )*partial_sum( &gregory_term<T>, 1, num_iter );

    }

    }
\end{minted}

\subsubsection*{Solution using Power Series:}
Start with the identity $\pi \approx \sqrt{ 6 \sum_{k=1}^{N} \frac{1}{k^2} }$

\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{cpp}

    \* run_time_pi.h *\

    namespace wbp {

    template< typename T >
    T partial_sum( T (*f)(uint index), uint sum_start, uint sum_end ) {

       T sum_total = static_cast<T>( 0 );

       for( uint i = sum_start; i <= sum_end ; i++ ) {
           sum_total += f(i);
       }

       return sum_total;

    }

    template < typename T >
    T pow_term( uint k ) {

        return static_cast<T>( 1 )/static_cast<T>( k * k );

    }

    template< typename T >
    T power_pi( uint num_iter ) {

        static_assert( std::is_floating_point<T>::value,
        "Template parameter must be a floating-point value.");
        return std::sqrt( static_cast<T>( 6 )*partial_sum( &pow_term<T>, 1, num_iter ) );

    }

    }
\end{minted}

\subsection*{Question 2:}

Suppose your team notices that your algorithm is a performance bottle-neck,
how would you speed it up?

\subsection*{Possible Answer 2:}

For any of these series methods the terms in the series are independent, as
 they only depend on index. The series terms can be computed in a canonical ``for''
  loop and easily parallelized using OpenMP for similar.

For a method like the Monte Carlo method each point can be assigned a position
independently of one another.

\subsection*{Question 3:}

Suppose someone else in your team has written an algorithm to compute $\pi$
 using the Gregory Series $ \left( \pi = 4 \sum_{k=0}^{\infty} \frac{ (-1)^{k+1} }{ 2k - 1 } \right)$.

Their algorithm, in pseudo-code, takes the following form:

 \FloatBarrier
 \begin{algorithm}[!htbp]
   \caption{ Estimate $\pi$ using Gregory Sum formula, using pairs of terms.}
   \begin{algorithmic}[1]
    \Input{$n \in \mathbb{N} $}\Comment{Specify the number of terms in the series -- must be an even number}
    \State $x \gets 0$\Comment{ Initialize the total sum to zero. }
     \For{ $k \in [ 1, 3, 5, \dots, n/2 - 1 ]$ }
      \State $x \gets x + \left( \frac{1}{2k-1} - \frac{1}{2( k + 1 ) -1} \right)$
     \EndFor
   \end{algorithmic}
   \Return $4 x$
 \end{algorithm}
 \FloatBarrier

The first few terms using this algorithm would read $
4( ( 1 - \frac{1}{3} ) + ( \frac{1}{5} - \frac{1}{7} ) + \dots )$.

You notice that after a large number of iterations this algorithm exhibits some strange
 behavior -- it does not seem to converge to $\pi$. You decide to investigate further
 and notice the error term $\lvert x - \pi \rvert$ does not go to zero. What
  might be causing this problem?

\subsection*{Possible Answer 3:}

As $k$ grows large we will find that $\frac{1}{2k-1} \approx \frac{1}{2( k + 1 ) -1}
\leftrightarrow \frac{1}{2k-1} - \frac{1}{2( k + 1 ) -1} \approx 0$.
We also expect that these terms will have similar numbers of significant digits --
 this is a situation that will almost certainly result in loss of significance /
 catastrophic cancellation. The result of this ( might ) be the error term
 converging to some non-zero constant, or diverging.

\subsection*{Question 4:}

Suppose your team decides that the number of iterations/recursions necessary
to guarantee required precision can be determined
 at compile time. Further it has been determined that
 double-precision floating-point numbers are the only type that needs to be
 supported.

 How would you re-write your algorithm to be
 computed at compile time instead of run time?

\subsection*{Possible Answer 4:}

Possible answer if starting with the Gregory Sum is stated below -- answer for
other series methods are similar.

\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{cpp}

    \* meta_pi.h *\
    namespace wbp {

    // General case
    template<unsigned int N, typename T = double>
    struct GregorySum {
        static constexpr T sum =
         ( (N % 2) ? 1.0 : -1.0 ) / ( 2*N - 1 ) +  GregorySum<N - 1, T>::sum;
    };

    // Specialized stop case
    template<typename T>
    struct GregorySum<1, T> {
        static constexpr T sum = 1;
    };

    template< unsigned int N >
    double compute_meta_pi() {
        return 4.0*GregorySum< N >::sum;
    }

    }
\end{minted}

Usage: \verb|wbp::compute_meta_pi<101>()|

\end{document}


\section{Definitions and Properties}
\begin{definition}
    Let $\Omega$ be a set, and $\mathscr F$ a set of subset of $\Omega$.
    We call $\mathscr F$ a $\sigma$-algebra on $\Omega$ if\\
    1. $\Omega\in\mathscr F$.\\
    2. $A\in\mathscr F\implies A^c\in\mathscr F$.\\
    3. For any countable sequence $(A_n)_{n\in\mathbb N}\in\mathscr F$, we have
    $$\bigcup_{n\in\mathbb N}A_n\in\mathscr F$$
    If $\mathscr F$ is a $\sigma$-algebra, then a function $\mathbb P:\mathscr F\to [0,1]$ is called a probablity measure if\\
    1. $\mathbb P(\Omega)=1$.\\
    2. For every sequence of disjoint sets $(A_n)_{n\in\mathbb N}\in\mathscr F$, we have
    $$\mathbb P\left( \bigcup_{n\in\mathbb N}A_n \right)=\sum_{n\in\mathbb N}\mathbb P(A_n)$$
    If $\mathbb P$ is a probablity measure, then we call $(\Omega,\mathscr F,\mathbb P)$ a probablity space.
\end{definition}
\begin{remark}
    When $\Omega$ is countable, we usually take $\mathscr F=2^\Omega$.
\end{remark}
\begin{definition}
    The elements of $\Omega$ are called outcomes and the elements of $\mathbb F$ are called events.
    If $A\in\mathscr F$, we interpret $\mathbb P(A)$ as the probablity that $A$ happens.
    Note that we only talk about probablity of events instead of outcomes.
\end{definition}
We will see later that if we take a random point from $[0,1]$, the probablity of a certain point being taken is $0$ (consequently any countable subset of $[0,1]$).
\begin{proposition}
    Let $A,B\in\mathscr F$.
    1. $\mathbb P(A^c)=1-\mathbb P(A)$.\\
    2. $\mathbb P(\varnothing)=0$.\\
    3. $A\subset B\implies\mathbb P(B)\ge \mathbb P(A)$.\\
    4. $\mathbb P(A\cup B)=\mathbb P(A)+\mathbb P(B)-\mathbb P(A\cap B)$.
\end{proposition}
\begin{proof}
    All follow from definition.
\end{proof}
\begin{example}
    1. Rolling a fair die.
    So $\Omega=\{1,2,3,4,5,6\}$ and $\mathscr F=2^\Omega$, and $\forall A\subset\Omega,\mathbb P(A)=|A|/6$.\\
    2. Equality likely outcomes.
    $\Omega$ is just a finite set of size $n>0$ and $\mathscr F=2^\Omega$, and $\forall A\subset\Omega,\mathbb P(A)=|A|/n$.
    This is the model of a randomly chosen point of $\Omega$.
    Taking $n=6$ gives the first example.\\
    3. Picking balls from a bag.
    Suppose we have a bag with $n$ labelled balls $1,2,\ldots n$ and indistinguishable by touch.
    We pick $k\le n$ balls at random (i.e. all outcomes are equally likely) without looking.
    Then $\Omega=\{A\subset \{1,2,\ldots,n\}:|A|=k\}$ and $\mathscr F=2^\Omega$, so $\forall A\subset\Omega,\mathbb P(A)=|A|/\binom{n}{k}$.\\
    4. A deck of cards.
    Suppose we have a well-shuffled (i.e. all possible ordering of the cards are equally likely) deck of $52$ cards (excluding jokers).
    So $\Omega=S_{52},\mathscr F=2^\Omega$ and $\forall A\subset\Omega,\mathbb P(A)=|A|/52!$.
    Hence $\mathbb P(\text{first two cards are aces})=(4\times 3\times 50!)/52!=1/221$.\\
    5. Largest digit.
    Consider a string of random digits (all outcomes equally possible) $0,1,\ldots,9$ of length $n$.
    Take $\Omega=\{0,1,\ldots,9\}^n$ and $\mathscr F,\mathbb P$ as in before.
    Let $A_k=\{\text{no digit exceeds $k$}\}$ and $B_k=\{\text{largest digit is $k$}\}$.
    Since $|A_k|=(k+1)^n$ we have $|B_k|=|A_k\setminus A_{k-1}|=(k+1)^n-k^n$, so $\mathbb P(B_k)=((k+1)^n-k^n)/10^n$.\\
    6. Birthday problem.
    Suppose there are $n$ people in the room.
    What is the probability of at least two people sharing the same birthday, given that nobody is born on 29 Feb and any other day in the year is equally probable.
    So $\Omega=\{1,2,\ldots,365\}^n$, and again $\mathscr F$ and $\mathbb P$ are taken as before (equally likely outcome).
    Let $A$ be the event that all birthdays are different, then
    $$\mathbb P(A)=\frac{|A|}{|\Omega|}=\frac{365\times 364\times\ldots (365-n+1)}{365^n}$$
    Then the probablity of two having the same birthday is $p=1-\mathbb P(A)$.
    If you take $n=22$, then you get $p\approx .476$, and when $n=23$, $p\approx .507$.
\end{example}


\subsection{Ideals}

An ideal is a subring where the multiplication of any element of the ideal with any element of the ring is also in the ideal.

\subsubsection{Examples}

Even numbers are an ideal of the integers.

Odd numbers are not an ideal. For example \(1\) is in the ideal, but multiplied by \(2\) gives \(2\), which is not in the ideal.


\subsection{Gödel numbering}

Gödel numbering assigns a unique number to each formula.

To contruct this we first assign a natural number to each symbol.

This gives us a sequence:

\(\{x_1, x_2, x_3,...,x_n\}\)

We can assign a unique number to this by using the first \(n\) prime numbers.

\(2^{x_1}3^{x_2}5^{x_3}...\)

This number can then be prime factored to recover the sequence, and therefore the formula.


%
% Chapter 5.2
%

\section*{5.2 Volumes}

\subsubsection*{Definition of Volume (Disk Method)}

Let \(S\) be a solid that lies between \(x=a\) and \(x=b\). If the cross-sectional area of \(S\) in the plane \(P_x\), through \(x\) and perpendicular to the \(x\)-axis, is \(A(s)\), where \(A\) is a continuous function, then the volume of \(a\) is

\[ V = \lim_{n \to \infty} \sum_{i=1}^n A(x_i^*) \Delta x = \int_a^b A(x) dx\]