\section{AMR-Based Detonation Solver in OpenFOAM}

\begin{frame}{Tested Solvers}
Solvers tested for their capability to model shocks and detonations:
\begin{itemize}
\item \textbf{rhoReactingFoam}: included with OpenFOAM, a density-based combustion solver
\item \textbf{rhoCentralFoam}: included with OpenFOAM and developed by Greenshields \textit{et. al.} \cite{greenshields}, a density-based solver that uses the central-upwind schemes of Kurganov and Tadmor \cite{kurganov1}
\item \textbf{rhoReactingCentralFoam}: a solver combined by Caelan Lapointe with previous work done by Nakul \cite{nakul}, with AMR support
\end{itemize}
\end{frame}

\subsection{Governing Equations}

\begin{frame}[allowframebreaks]{Governing Equations}
Detonations were modeled using the reacting Navier-Stokes equations \cite{kuo,stokes}:
\begin{equation}
\frac{\partial \rho}{\partial t} + \nabla \cdot \left(\rho \bm{u}\right) = 0\,
\end{equation}
\begin{equation}
\frac{\partial \rho\bm{u}}{\partial t} + \nabla \cdot \left(\rho \bm{u}\otimes \bm{u}\right) + \nabla p -\mu\nabla^2\bm{u}= \bm{0}\,,
\end{equation}
\begin{equation}
\frac{\partial \rho E}{\partial t} + \nabla \cdot \left[\left(\rho E + p\right)\bm{u}\right] -\alpha\nabla^2 e = \dot{q}\,,
\end{equation}
\begin{equation}
\frac{\partial \rho Y_i}{\partial t} + \nabla \cdot \left(\rho Y_i \bm{u}\right) -\mu\nabla^2 Y_i= \dot{\omega}_i\,,
\end{equation}
where
\begin{equation}
\dot{q} = \sum_{i = 1}^N \dot{\omega_i} \Delta h_{f,i}^0\,,
\end{equation}
%is the heat flux, $\dot{\omega}_i$ is the species source reaction rate, $\rho$ is the density, $\bm{u}$ is the fluid velocity vector, $Y_i$ is the mass fraction of the $i$th species, $E$ is the total energy, $p$ is the pressure, $\mu$ is the dynamic viscosity, $e$ is the internal energy, $\alpha$ is the thermal diffusivity and $\Delta h_{f,i}^0$ is the species formation enthalpy.
\end{frame}

\begin{frame}{Notes About Governing Equations}
\begin{itemize}
\item Specific heat $C_{p,i}=C_{p,i}(T)$ from NIST JANAF \cite{janaf} lookup tables.
\item No explicit turbulence modeling, but turbulence can form
\begin{itemize}
    \item subgrid-scale turbulence structures averaged out numerically
    \item akin to implicit LES
\end{itemize}
\item Not modeling inviscid Euler equations; viscosity is accounted for with Sutherland \cite{sutherland} model:
\end{itemize}
\begin{equation}
\mu = \frac{A_s \sqrt{T_s}}{1 + \frac{T_s}{T}} \,,
\end{equation}
\end{frame}

\subsection{Chemistry Modeling}

\begin{frame}{Chemical Reactions}
Single-step stoichiometric hydrogen-air utilized for simplicity, which follows the following expression \cite{kuo}:
\begin{center}
\ch{2 H2 + 2 (O2 + 3.76 N2) -> 2 H2O + 7.52 N2}
\end{center}
with
\begin{table}[t!]
\centering
\begin{tabular}{cc}
Species & Mass Fraction \\ \hline
H\(_2\) & 0.02851 \\
H\(_2\)O & 0 \\
N\(_2\) & 0.745 \\
O\(_2\) & 0.226 \\ \hline
Total & 0.99951 \\
\end{tabular}
\end{table}
OpenFOAM allows for easy transitions between chemical models.
\end{frame}

\begin{frame}{Reaction Rate Modeling}
Arrhenius equation \cite{arrhenius} takes the form \cite{christ}
\begin{equation}
\dot{\omega}_i = AT^\beta \exp\left(\frac{ -E_a}{R T}\right)\,,
\end{equation}
where $Ta = Ea/R$. Simulation values were explored, but we settled on
\begin{equation}
   A = 1.4 \times 10^{13} ~ \text{m}^3\text{mol}^{-1}\text{s}^{-1},
   \qquad
   Ta = 12996 ~\text{K},
   \qquad
   \beta = 0\,.
\end{equation}
with \(R = 368.9\) J/Kg-K. As shown later these reasonably match Chapman-Jouguet detonation theory \cite{chapman} along with other published values \cite{towery1,hashemi}.
\end{frame}

\subsection{Domain Setup}

\begin{frame}{Simulation Domain Setup}
Besides ignition region, domain is at 1 atm and 300 K
\begin{figure}[t!]
\centering
\includegraphics[width=0.8\textwidth]{../figs/domainBC.png}
%\caption{Geometry and domain setup with boundary conditions}
%\label{fig:domainBC}
\end{figure}%
\end{frame}

\subsection{Parallel Computing}

\begin{frame}{Parallel Computing: Decomposition}
%Domain is decomposed into chunks which are independently processed in parallel, communicating with MPI \cite{walker}. Decomposition defined in \texttt{decomposeParDict}. Several methods for decomposition:
%\begin{itemize}
%\item \texttt{simple}: define splits in each direction
%\item \texttt{hierarchical}: \texttt{simple}, but with recursive ordering to splits
%\item \texttt{scotch}: minimizes boundaries between processors, can set weighting
%\item \texttt{manual}: manual cell allocation to each processor
%\end{itemize}
%The \texttt{simple} method was used here.
\begin{itemize}
\item Domain is decomposed into chunks which are independently processed in parallel, communicating with MPI \cite{walker}
\item Decomposition defined in \texttt{decomposeParDict}
\item Several methods for decomposing domain in OpenFOAM, but \texttt{simple} method was utilized which simply splits number of times in each direction
\end{itemize}
\end{frame}

\begin{frame}{Parallel Computing: Load Balancing}
\begin{figure}[p]
    \centering
    \begin{subfigure}[]{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{../figs/parallel_short.png}
        \caption{Domain decomposed into typical chunks, bad for detonation and AMR load balancing}
        %\label{sfig:shortdecomp}
    \end{subfigure}%
    \begin{subfigure}[]{0.45\textwidth}
        \centering
        \includegraphics[width=0.9\textwidth]{../figs/parallel_long.png}
        \caption{Domain decomposed into long chunks, better for detonation and AMR load balancing}
        %\label{sfig:longdecomp}
    \end{subfigure}
    %\caption{Example domain decomposition techniques for parallel computing}
    %\label{fig:decomp}
\end{figure}%
\end{frame}

\subsection{Adaptive Mesh Refinement}

\begin{frame}{Adaptive Mesh Refinement: Splitting}
Adaptive meshing splits the cells using an octree splitting method:
\begin{figure}[]
\centering
\includegraphics[width=0.4\textwidth]{../figs/amr_example.png}
%\caption{Adaptive mesh refinement octree splitting method}
%\label{fig:octree}
\end{figure}%
This makes the AMR inherently three-dimensional.
\end{frame}

\begin{frame}{Adaptive Mesh Refinement Parameters}
Allows for the mesh to refine and unrefine based on set parameters:
\begin{itemize}
\item \texttt{refineInterval}: frequency when active, based on time steps
\item \texttt{field}: which parameter to track
\item \texttt{lowerRefineLevel}: lower bound of active refinement
\item \texttt{upperRefineLevel}: upper bound of active refinement
\item \texttt{unrefineLevel}: upper bound of unrefinement
%\item \texttt{nBufferLayers}: number of buffer cell layers between levels of refinement
%\item \texttt{maxRefinement}: additional recursive refinement levels
\item \texttt{maxCells}: maximum cell count to trigger AMR update
\end{itemize}
\end{frame}

\begin{frame}{Adaptive Mesh Refinement: Refinement Levels}
\begin{columns}
\column{0.5\textwidth}
\texttt{maxRefinement}: Number of additional recursive refinement levels
\column{0.5\textwidth}
\begin{center}
\includegraphics[width=0.8\textwidth]{../figs/amr_refine.png}
\end{center}
\end{columns}
\end{frame}

\begin{frame}{Adaptive Mesh Refinement: Buffer Layers}
\begin{columns}
\column{0.5\textwidth}
\texttt{nBufferLayers}: Number of buffer cell layers between levels of refinement
\column{0.5\textwidth}
\begin{center}
\includegraphics[width=0.8\textwidth]{../figs/amr_buffer.png}
\end{center}
\end{columns}
\end{frame}

\begin{frame}{Adaptive Mesh Refinement: Detonation Wave}
\begin{figure}
\centering
\includegraphics[width=0.7\textwidth]{../figs/amr_cells.png}
\caption{Three-level adaptive mesh refinement over a pressure field surface contour, with the detonation wave traveling from the -x wall to +x exit}
%\label{fig:examr}
\end{figure}
\end{frame}

\begin{frame}{Initial Meshing}
\begin{figure}[]
    \centering
    \begin{subfigure}[]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{../figs/mesh/2Dmesh.png}
        \caption{Two-dimensional mesh}
    \end{subfigure}%
    \begin{subfigure}[]{0.5\textwidth}
        \centering
        \includegraphics[width=\textwidth]{../figs/mesh/3Dmesh.png}
        \caption{Three-dimensional mesh}
    \end{subfigure}
    %\caption{Static meshes used in OpenFOAM for detonation modeling, at an exaggerated unrefined resolution for example purposes}
    %\label{fig:meshcompare}
\end{figure}
\end{frame}

\subsection{Initial Work and Testing}

\begin{frame}{Initial Detonation Attempts}
First testing \texttt{rhoReactingFoam}, methane-oxygen without nitrogen was used, with corner ignition to test shock reflection and exit
\begin{figure}[]
\centering
\includegraphics[width=0.9\textwidth]{../figs/cornerdet.png}
%\caption{Initial methane and oxygen detonation boundary condition test with corner detonation. Velocity magnitude is plotted here without scale to just check the solver and boundary conditions for modeling potential. Detonation was initiated in the lower left corner.}
%\label{fig:cornerdet}
\end{figure}%
Testing the detonation tube setup performed by Towery \textit{et. al.} \cite{towery1} was next, to be used as comparison.
\end{frame}

\begin{frame}{\texttt{rhoReactingFoam} Problems}
\begin{columns}
\column{0.4\textwidth}
\begin{itemize}
\item Different static mesh resolutions were tested with \texttt{rhoReactingFoam}, but noise and instability in the solution was seen.
\item likely due to solver being more pressure-based than density-based
\end{itemize}
\column{0.6\textwidth}
\begin{figure}[]
\centering
\includegraphics[width=\textwidth]{../figs/rhoReactingFoam.png}
%\caption{Noise and instability in solution and shock capturing problems using the \texttt{rhoReactingFoam} solver}
%\label{fig:rrf}
\caption*{Pressure vs. Grid Location}
\end{figure}%
\end{columns}
\end{frame}

\begin{frame}{\texttt{rhoReactingCentralFoam} Selection and Validation}
\begin{columns}
\column{0.6\textwidth}
\begin{figure}[]
\centering
\includegraphics[width=\linewidth]{../figs/shocktube.png}
%\caption{Shock tube validated test case included with OpenFOAM compared to hybrid solver}
%\label{fig:sod}
\end{figure}%

\column{0.4\textwidth}
\begin{itemize}
\item turned towards the solvers utilizing central-upwind schemes of Kurganov and Tadmor \cite{kurganov1}
\item used the shock tube test to validate shock-capturing capability
\end{itemize}
\end{columns}
\end{frame}


\subsubsection*{Discrete Fourier Transforms\hspace*{\fill}\hyperlink{SignalProcessing}{(up)}\hypertarget{fftFunctions}{}} \addcontentsline{toc}{subsubsection}{Discrete Fourier Transforms}
Discrete Fourier transforms are done using a fast Fourier transform (FFT) algorithm.
\begin{table}[H]
\caption{Discrete Fourier Transform}
\label{tab:fftFunctions}
\begin{center}
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\hyperlink{fftFunc}{\rmfamily \bfseries FFT Function Set}} \\ \hline
fft & Execute FFT\\
fft\_create & Create FFT Object\\
fft\_destroy & Free FFT object\\
fft\_getattr & Get attributes of FFT object\\
\hline\end{tabular}
\end{center}
%\label{default}
\end{table}%


% !TEX root =main.tex

%\section{Further Remarks on  C-TLP Protocol}\label{discussion-C-TLP}

%In this section, we provide some remarks on  C-TLP scheme presented in Section \ref{SO-PoR-Protocol}.

\begin{remark} To make each  puzzle instance, a \emph{distinct} random generator: $r_{\scriptscriptstyle j}$, is used. This is the reason, in  C-TLP protocol, before a puzzle  is  generated   in step \ref{call-RTLP-GenPuz},  a new public key is set in step \ref{set-pk-in-loop}.  Also, at the beginning of the protocol only $r_{\scriptscriptstyle 1}$ is public and the rest of the generators are kept secret. They are found and used sequentially after their related puzzle is solved.
\end{remark}

\begin{remark} The commitments opening, including the commitment random values, are not known to other verifiers (than the puzzle generator) at the beginning of the protocol. At this point,  only the committed values are public. Once a solver solves each puzzle,  it  extracts one of the commitments' opening, and sends it to a public verifier who can check if the opening matches the commitment.
\end{remark}

\begin{remark}
 In  C-TLP, we use the folklore hash-based commitment scheme, in the random oracle model, only to achieve more computation improvement than that can be achieved in the standard model. But C-TLP can utilise any efficient non-interactive commitment scheme in the \emph{standard model} as well, e.g. Pedersen Commitment.
\end{remark}

\begin{remark}
The efficiency of  C-TLP scheme stems from three crucial factors: (a) removing computation overlaps when solving different puzzles: even though solving $j\text{\small{-th}}$ puzzle, where $j>1$, requires $jT$ squaring, $(j-1) T$ of the squaring is used to solve previous puzzles that leads to $\frac{z+1}{2}$ times computation cost reduction at the server-side,  (b)  supporting reusable single  public parameter: $a=2^{\scriptscriptstyle T}$, generated only once that costs $O(1)$, as opposed to the RSA-based TLP whose cost is linear: $O(z)$, and (c) supporting efficient verification: due to the way each message is encoded (i.e. embedding the opening in a solution).
\end{remark}

\begin{remark}
C-TLP also can efficiently  be used in a multi-server setting,  where there are $z$ servers: $\{S_{\scriptscriptstyle 1},...,S_{\scriptscriptstyle z}\}$,  each $S_{\scriptscriptstyle j}$ needs to solve puzzle $\ddot{o}_{\scriptscriptstyle j}$ at time $f_{\scriptscriptstyle j}$ and passes on the solution to the next server $S_{\scriptscriptstyle j+1}$ to solve the next puzzle by time $f_{\scriptscriptstyle j+1}>f_{\scriptscriptstyle j}$. In this setting,  due to the scalability property of C-TLP (and unlike using the existing time-lock puzzles naively), other servers do not need to start solving the puzzle   as soon as the client releases puzzles' public parameters. Instead, they can wait until the previous solution is issued that saves them a significant cost. Furthermore, a  server can first  verify the correctness  of the solution found by the previous server (due to the public verifiability of C-TLP),  if accepted then   it starts finding the next solution.
\end{remark}

\begin{remark}
In the following, we outline an  approach that looks an option to construct an efficient C-TLP; however, as we will show it would not be secure. In particular,  one uses the TLP to generate $z$ public and secret key pairs. Then, it uses the TLP to compute $z\text{\small{-th}}$ puzzle as $\mathtt{TLP.GenPuZ}(m_{\scriptscriptstyle z},pk_{\scriptscriptstyle z},sk_{\scriptscriptstyle z})\rightarrow \ddot{o}_{\scriptscriptstyle z}$.  Then, it embeds $\ddot{o}_{\scriptscriptstyle z}$ into $(z-1)\text{\small{-th}}$ one, i.e. $\mathtt{TLP.GenPuZ}(m_{\scriptscriptstyle z-1}||\ddot{o}_{\scriptscriptstyle z},pk_{\scriptscriptstyle z-1},sk_{\scriptscriptstyle z-1})\rightarrow \ddot{o}_{\scriptscriptstyle z-1}$. This process goes on until $\ddot{o}_{\scriptscriptstyle 1}$  is created. It sends the combined puzzles and public key (including all random generators) to the server; with the hope that puzzles can be solved sequentially and the time gap between finding two solutions will be $\Delta$.  This approach is not secure, because as soon as the server accesses $\ddot{o}_{\scriptscriptstyle 1}$ and public parameters, it can in parallel perform $T$ squaring on every generator, i.e. $r^{\scriptscriptstyle 2^{\scriptscriptstyle T}}_{\scriptscriptstyle i}$, for all $i, 1\leq i\leq z$. In this case, as soon as $\ddot{o}_{\scriptscriptstyle 1}$ is solved and  $\ddot{o}_{\scriptscriptstyle 2}$  is extracted, it has enough information to  immediately solve $\ddot{o}_{\scriptscriptstyle 2}$ and accordingly the rest of the puzzles without doing any further exponentiation.
\end{remark}


%{{{1
\documentclass[11pt]{article} % 12-point font

\usepackage[margin=1in]{geometry} % set page to 1-inch margins
\usepackage{amsmath} % for math
\usepackage{amssymb} % like \Rightarrow
\setlength\parindent{0pt} % Suppresses the indentation of new paragraphs.
\pagenumbering{gobble} % suppress page numbers

% Big display
\newcommand{\ds}{ \displaystyle }
% Parenthesis
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\p}[1]{\left(#1\right)}
\newcommand{\bk}[1]{\left[#1\right]}
\newcommand{\bc}[1]{ \left\{#1\right\} }
\newcommand{\abs}[1]{ \left|#1\right| }
% Derivatives
\newcommand{\df}[2]{ \frac{d#1}{d#2} }
\newcommand{\ddf}[2]{ \frac{d^2#1}{d{#2}^2} }
\newcommand{\pd}[2]{ \frac{\partial#1}{\partial#2} }
\newcommand{\pdd}[2]{\frac{\partial^2#1}{\partial{#2}^2} }
% Distributions
\newcommand{\Normal}{ \text{Normal} }
\newcommand{\Beta}{ \text{Beta} }
\newcommand{\Gam}{ \text{Gamma} }
\newcommand{\InvGamma}{ \text{Inv-Gamma} }
\newcommand{\Uniform}{ \text{Uniform} }
\def\Dir{\text{Dirichlet}}
\def\TN{\text{TN}}
% Statistics
\newcommand{\E}{\text{E}}
\newcommand{\iid}{\overset{iid}{\sim}}
\newcommand{\ind}{\overset{ind}{\sim}}

% Graphics
\usepackage{graphicx}  % for figures
\usepackage{float} % Put figure exactly where I want [H]

% Uncomment if using bibliography
% Bibliography
\usepackage{natbib}
\bibliographystyle{plainnat}

% Adds settings for hyperlinks. (Mainly for table of contents.)
\usepackage{hyperref}
\hypersetup{
  pdfborder={0 0 0} % removes red box from links
}
%}}}1

% Macros for this project
\def\true{\text{TRUE}}
\usepackage[dvipsnames,usenames]{color}
\newcommand{\bbh}{\color{blue}\textbf}  % blue bold highlight
\newcommand{\bch}{\color{blue}\it}  % blue italics
\newcommand{\ech}{\color{black}\rm}
\def\imgdir{results}
\input{imgtemplate.tex}
\def\imscale{.5}

% Title Settings
\title{Patients Data Analysis with r-FAM and $K=20$}
\author{Arthur Lui}
\date{\today} % \date{} to set date to empty

% MAIN %
\begin{document}

\maketitle

% \tableofcontents \newpage % Comment to remove table of contents

% \section{Objective}\label{sec:objective}
% TODO

\section{Data}\label{sec:data}
The data for this study consists of transformed marker expression levels
obtained from the blood samples of three subjects. Each of the three samples
belongs to a different subject. The number of cells in the samples are (4677,
1367, 12843). Thirty-two markers were studied in all samples and are listed
in Table~\ref{tab:markers}. To remove overly-extreme values in the analysis,
the data were further processed so that cells where at least one transformed
marker expression level was greater than 4 or less than -7 were excluded. The
final cell-counts in each sample was (4556, 1308, 12256).

% THE 32 MARKERS:
% 2B4 2DL1 2DL3 2DS4 3DL1 CCR7 CD158B CD16 CD25 CD27 CD57 CD62L CD8 CD94 CKIT
% DNAM1 EOMES GRA GRB KLRG1 LFA1 NKG2A NKG2C NKG2D NKP30 PERFORIN SIGLEC7 SYK
% TBET TIGIT TRAIL ZAP70

\begin{table}[H]
  \begin{center}
    \begin{tabular}{|l|l|l|l|}
      \hline
      1: 2B4    &  9: CD25   &  17: EOMES & 25: NKP30    \\
      2: 2DL1   &  10: CD27  &  18: GRA   & 26: PERFORIN \\
      3: 2DL3   &  11: CD57  &  19: GRB   & 27: SIGLEC7  \\
      4: 2DS4   &  12: CD62L &  20: KLRG1 & 28: SYK      \\
      5: 3DL1   &  13: CD8   &  21: LFA1  & 29: TBET     \\
      6: CCR7   &  14: CD94  &  22: NKG2A & 30: TIGIT    \\
      7: CD158B &  15: CKIT  &  23: NKG2C & 31: TRAIL    \\
      8: CD16   &  16: DNAM1 &  24: NKG2D & 32: ZAP70    \\
      \hline
    \end{tabular}
  \end{center}
  \caption{Index for markers referenced in this study.}
  \label{tab:markers}
\end{table}

\subsection{MCMC setup}
\begin{itemize}
  \item 10000 burn-in. 5000 samples were collected (no thinning).
  \item We also did weight-preserving parallel tempering (WPPT) with temperatures
        $\bc{1, 1.003, 1.006, 1.01}$. Swaps between every pair of chains were
        proposed at each MCMC iteration.
  \item We fit 4 models, each with a different value for $\phi$. The values
        for $\phi$ were (0, 1, 10, 100).
  \item The computation time averaged across the 4 models was 159 hours.
  \item 5\% of data in each sample are used to sample from trained prior, and
    $M=5$ was used.
  \item \textbf{Some Priors:}
  \begin{itemize}
    \item $w^\star_{i,k} \sim \Gam(\text{shape}=10, \text{rate}=1)$
    \item $p_c \sim \text{Beta}(1, 99)$
    \item $L_0=6$, $L_1=3$
    \item $\alpha \sim \Gam(1, 1)$
    \item $\delta_0 \sim \TN^-(1, 0.1)$
    \item $\delta_1 \sim \TN^+(1, 0.1)$
    \item $\eta_{z, i,j,\ell} \sim \Dir_{L_z}(1)$
    \item $\sigma^2_i \sim \InvGamma(3, 1)$
  \end{itemize}
\end{itemize}

\newpage
\section{Log likelihood (post-burn)}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{loglike_postburn.pdf}{\imscale}
  \end{center}
  \caption{Log-likelihood for 5000 iterations after burn-in of 10000 for each
  simulation setup.}
  \label{fig:ll}
\end{figure}

\newpage
\section{Swap proportions}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{swapprops.pdf}{\imscale}
  \end{center}
  \caption{These figures summarize proportion of instances that swaps were
  made when proposed between the various chains for each setup. A noticeable amount of Swapping occurred between the chains. The colorbars on the right of each
  heatmap show that the highest proportion of swaps between any two chains is
  between 40\% and 60\%.}
  \label{fig:swapproprs}
\end{figure}

\newpage
\section{Posterior mean of $Z$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{Zmean.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{Posterior means of $Z$ for each run. This figure is included only
  to show that it should not be used, due to label switching which happens as
  a result of states-swapping between chains.}
  \label{fig:zmean}
\end{figure}

\newpage
\section{Posterior estimate of $Z_1$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{Z1.pdf}{\imscale}
  \end{center}
  \caption{Posterior estimate of $Z_1$.
  Note that the labels are likely different for the different samples due
  to label switching.}
  \label{fig:z1est}
\end{figure}

\newpage
\section{Posterior estimate of $Z_2$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{Z2.pdf}{\imscale}
  \end{center}
  \caption{Posterior estimate of $Z_2$.
  Note that the labels are likely different for the different samples due
  to label switching.}
  \label{fig:z2est}
\end{figure}

\newpage
\section{Posterior estimate of $Z_3$}
\begin{figure}[H]
  \begin{center}  % 2 x 2
    \imgtemplate{Z3.pdf}{\imscale}
  \end{center}
  \caption{Posterior estimate of $Z_3$.
  Note that the labels are likely different for the different samples due
  to label switching.}
  \label{fig:z3est}
\end{figure}

\newpage
\section{Posterior estimate of $y_1$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{y1.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:y1est}
\end{figure}

\newpage
\section{Posterior estimate of $y_2$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{y2.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:y2est}
\end{figure}

\newpage
\section{Posterior estimate of $y_3$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{y3.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:y3est}
\end{figure}

\newpage
\section{Box plots for $p_i$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{p.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ppost}
\end{figure}

\newpage
\section{Trace plots for $p_1$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{p1_trace.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:p1trace}
\end{figure}

\newpage
\section{Trace plots for $p_2$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{p2_trace.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:p2trace}
\end{figure}

\newpage
\section{Trace plots for $p_3$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{p3_trace.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:p3trace}
\end{figure}

\newpage
\section{Posterior distribution for $R_i$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{Rcounts.pdf}{.4}
  \end{center}
  \caption{Posterior distribution for $R_i$. When $\phi=0$, $R_i$ appears
  to be the smallest. This could be due to the MCMC being stuck in a certain
  mode more easily. For $\phi > 0$, $R_i$ seems to decrease as $\phi$ increases.}
  \label{fig:W-post}
\end{figure}

\newpage
\section{Box plots for $W$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{W.pdf}{\imscale}
  \end{center}
  \caption{Posterior distribution for $W$. This figure is not very useful
  to look at, due to label-switching.}
  \label{fig:W-post}
\end{figure}

\newpage
\section{Trace plots for $\mu^\star$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{mus_trace.pdf}{\imscale}
  \end{center}
  \caption{Trace plots for $\mu^\star_z$.}
  \label{fig:mus-trace}
\end{figure}

\newpage
\section{Posterior distributions for $\mu^\star$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{mus.pdf}{\imscale}
  \end{center}
  \caption{Box plots of $\mu^\star$.}
  \label{fig:mus}
\end{figure}

\newpage
\section{Trace plots for $\sigma^2$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{sig2_trace.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{Trace plots for $\sigma^2$.}
  \label{fig:sig2-trace}
\end{figure}

\newpage
\section{Posterior distributions for $\sigma^2$}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{sig2.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:sig2}
\end{figure}

\newpage
\section{Posterior distributions for sample 1 marker 1}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{dden/dden_i1_j1.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ddi1j1}
\end{figure}

\newpage
\section{Posterior distributions for sample 1 marker 2}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{dden/dden_i1_j2.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ddi1j2}
\end{figure}

\newpage
\section{Posterior distributions for sample 2 marker 1}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{dden/dden_i2_j1.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ddi2j1}
\end{figure}

\newpage
\section{Posterior distributions for sample 2 marker 2}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{dden/dden_i2_j2.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ddi2j2}
\end{figure}

\newpage
\section{Posterior distributions for sample 3 marker 2}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{dden/dden_i3_j1.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ddi1j6}
\end{figure}

\newpage
\section{Posterior distributions for sample 3 marker 2}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \imgtemplate{dden/dden_i3_j2.pdf}{\imscale}
  \end{center}
  % TODO: Write more?
  \caption{}
  \label{fig:ddi2j6}
\end{figure}

\newpage
\section{Missing Mechanisms}
\begin{figure}[H]
  \begin{center}  % 6 x 5
    \begin{tabular}{ccc}
      \includegraphics[scale=0.35]{\imgdir/phi0/img/missmech_1.pdf} &
      \includegraphics[scale=0.35]{\imgdir/phi0/img/missmech_2.pdf} &
      \includegraphics[scale=0.35]{\imgdir/phi0/img/missmech_3.pdf} \\
    \end{tabular}
  \end{center}
  \caption{}
\label{fig:missmech}
\end{figure}

% Uncomment if using bibliography:
% \bibliography{sim}

\end{document}


\documentclass[aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{amsmath,amsfonts,amsthm,bm}
\usepackage{color}
\usepackage{minted}
\usepackage{graphicx} % Allows including images
\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage{tikz}
\usepackage[version=3]{mhchem}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\setminted{fontsize=\scriptsize}

\hypersetup{
    colorlinks=true,
    linkcolor=red,
    filecolor=magenta,
    urlcolor=red,
}

\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\argmin}{argmin}
\let \vec \mathbf

\mode<presentation> {
    \usetheme{CambridgeUS}
    %\setbeamertemplate{footline} % To remove the footer line in all slides uncomment this line
    \setbeamertemplate{footline}[page number] % To replace the footer line in all slides with a simple slide count uncomment this line
    \setbeamertemplate{navigation symbols}{} % To remove the navigation symbols from the bottom of all slides uncomment this line
}

\title[Linear Classification]{Linear Classification}

\author{Shyue Ping Ong}
\institute[UCSD]{University of California, San Diego\\
\medskip
}
\date{NANO281} % Date, can be changed to a custom date

\begin{document}

\begin{frame}
    \titlepage % Print the title page as the first slide
\end{frame}

\begin{frame}{Overview}
    \tableofcontents
\end{frame}

\section{Preliminaries}

\begin{frame}{Preliminaries}
    \begin{itemize}
        \item Linear methods can also be used for classification, i.e., decision boundaries are linear.
        \item These methods are surprisingly effective across a large spectrum of datasets, even compared to more complex ML models.
    \end{itemize}
\end{frame}

\begin{frame}{Metal vs Insulator Dataset}
    \begin{itemize}
        \item To demonstrate the use of these methods, we will first discuss the ``toy'' dataset.
        \item 2000+ binary (\ce{A_xB_y}) compounds with experimental band gaps.
        \item Class 0: metals; Class 1: insulators.
        \item Using pymatgen, we can generate some simple features. Here, we will create simply features based on the mean and absolute difference in electronegativity between A and B (why?).
    \end{itemize}
    \begin{figure}
        \centering
        \includegraphics[width=0.35\textwidth]{figures/electronnegativity_bandgap.png}
    \end{figure}
\end{frame}

\begin{frame}[fragile]{Creating the features and classes}
\inputminted{python}{example_element_features.py}
\end{frame}

\section{Basic concepts}

\begin{frame}{Basic concepts}
    \begin{itemize}
        \item If there are $K$ classes, we have a $N \times K$ indicator response matrix. Each row is a vector $Y = (Y_1, Y_2, ..., Y_K)$ where $Y_k = 1$ if the instance belongs to the $k$th class and all other $Y$s are 0.
        \begin{equation*}
            \vec{Y} = \begin{pmatrix}
            0 & 0 & ... & 1\\
            1 & 0 & ... & 0\\
            ...& & &\\
            0 & 1 & ... & 0\\
            \end{pmatrix}
        \end{equation*}
        \item For the $k$th response variable, the fitted $\hat{f_k}(x) = \hat{\beta_{k0}} + \hat{\beta_{k}^T}x$.
        \item Decision boundary between $k$ and $l$ class is given by $\hat{f_k}(x) = \hat{f_l}(x)$.
        \item Input is divided into regions.
        \item Similar to linear regression, we can augment the input space with polynomial (e.g., $X_1^2, X_2^s, X_1X_2$) and other basis functions, leading to boundaries that are non-linear.
    \end{itemize}
\end{frame}

\begin{frame}
\frametitle{Linear regression of indicator matrix}
\begin{columns}
\column{0.5\textwidth}
    \begin{itemize}
        \item Treat each column of $\vec{Y}$ as a target. Least squares solution:
        \begin{equation*}
            \hat{\vec{Y}} = \vec{X}(\vec{X}^T\vec{X})^{-1}\vec{X}^T\vec{Y}
        \end{equation*}
        \item For each new observation $x$, we compute $\hat{f_k}(x) = (1, x^T)(\vec{X}^T\vec{X})^{-1}\vec{X}^T\vec{Y}$.
        \item Find the largest component, and that will result in the classification k, $G(x) = \argmax_{k \in G} \hat{f_k}(x)$.
        \item Major issue: some categories may be masked for $K \geq 3$.
    \end{itemize}
\column{0.5\textwidth}
    \begin{figure}
        \centering
        \includegraphics[width=0.8\textwidth]{figures/linearclassifier.pdf}
    \end{figure}
\end{columns}
\end{frame}

\section{Discriminant Analysis}

\begin{frame}{Discriminant Analysis}
    \begin{itemize}
        \item From Bayes rule, we have:
        \begin{equation*}
            P(G = k|X = x) = \frac{f_k(x)\pi_k}{\sum_{l=1}^K f_l(x)\pi_l}
        \end{equation*}
        \item where $f_k(x)$ are the class conditional probability densities ($P(X = x | G=k)$) and $\pi_k$ are the prior probabilities of being in class $k$.
        \item Most common approach - assume Gaussian class densities.
        \begin{equation*}
            f_k(x) = \frac{1}{(2\pi)^{p/2}|\Sigma_k|^{1/2}}\exp{-\frac{1}{2}(x-\mu_k)^T\Sigma_k^{-1}(x-\mu_k)}
        \end{equation*}
    \end{itemize}
\end{frame}

\begin{frame}{Linear Discriminant Analysis}
    \begin{itemize}
        \item Assume all classes have a common covariance matrix, i.e., $\Sigma_k = \Sigma$.
        \item To compare two classes $k$ and $l$, we can compare the log ratios.
        \begin{eqnarray*}
            \log{\frac{P(G = k|X = x)}{P(G = l|X = x)}} & = & \log{\frac{f_k(x)}{f_l(x)}} + \log{\frac{\pi_k}{\pi_l}}\\
            & = & \log{\frac{\pi_k}{\pi_l}} -\frac{1}{2}(\mu_k+\mu_l)^T\Sigma^{-1}(\mu_k-\mu_l) \\
            & & + x^T \Sigma^{-1} (\mu_k - \mu_l)
        \end{eqnarray*}
        \item At the decision boundary, $P(G=k|X=x) = P(G=l|X=x)$, which leads to a linear equation in $x$.
        \item Equivalently, we have
        \begin{equation*}
            G(x) = \argmax_k \left \{ \log{\pi_k} -\frac{1}{2}\mu_k^T\Sigma^{-1}\mu_k + x^T \Sigma^{-1} \mu_k \right \}
        \end{equation*}
    \end{itemize}
\end{frame}

\begin{frame}{Linear Discriminant Analysis, contd.}
    \begin{itemize}
        \item In general, we do not know the prior distributions and covariance matrix. These are estimated from the data.
        \begin{itemize}
            \item $\hat{\pi_k} = N_k/N$
            \item $\hat{\mu_k} = \sum_{g_i=k} x_i / N$
            \item $\hat{\Sigma} = \sum_{k=1}^K \sum_{g_i=k} (x_i - \hat{\mu_k})^T(x_i - \hat{\mu_k}) / (N - K)$
        \end{itemize}
        \item Avoids masking problem of linear regression classification.
        \item For the example data,
        \begin{figure}
            \centering
            \includegraphics[width=0.35\textwidth]{figures/lda_metal_insulator.png}
        \end{figure}
    \end{itemize}
\end{frame}

\begin{frame}{Quadratic Discriminant Analysis}
    \begin{itemize}
        \item Covariances are not assumed equal.
        \begin{equation*}
            G(x) = \argmax_k \left \{ \log{\pi_k} -\frac{1}{2}(x-\mu_k)^T\Sigma_k^{-1}(x-\mu_k) -\frac{1}{2} \log{|\Sigma_k|} \right \}
        \end{equation*}
        \item No cancellation of terms and decision boundaries are quadratic.
        \item Covariances must be estimated for each category.
        \item For the same metal-insulator example,
        \begin{figure}
            \centering
            \includegraphics[width=0.35\textwidth]{figures/qda_metal_insulator.png}
        \end{figure}
    \end{itemize}
\end{frame}

\begin{frame}[fragile]{Discriminant analysis in scikit-learn}
\inputminted{python}{example_sklearn_discriminant_analysis.py}
\end{frame}

\begin{frame}{Logistic regression}
    \begin{itemize}
        \item Model posterior probabilities with linear function.
        \begin{eqnarray*}
            \log{\frac{P(G=1|X=x)}{P(G=K|X=x)}} & = & \beta_{10} + \beta_1^T x\\
            \log{\frac{P(G=2|X=x)}{P(G=K|X=x)}} & = & \beta_{20} + \beta_2^T x\\
            & ... &\\
            \log{\frac{P(G=K-1|X=x)}{P(G=K|X=x)}} & = & \beta_{(k-1)0} + \beta_{k-1}^T x
        \end{eqnarray*}
        \item Results in the following posterior probabilities:
        \begin{eqnarray*}
            P(G=1|X=x) & = & \frac{\exp{(\beta_{10} + \beta_1^T x)}}{1 + \sum_{l=1}^{K-1}\exp{(\beta_{l0} + \beta_l^T x})}\\
            P(G=K|X=x) & = & \frac{1}{1 + \sum_{l=1}^{K-1}\exp{(\beta_{l0} + \beta_l^T x})}
        \end{eqnarray*}
    \end{itemize}
\end{frame}

\begin{frame}{Solving for the Logistic Regression Coefficients}
    \begin{itemize}
        \item Typically fitted using \textit{maximum likelihood}.
        \begin{equation*}
            l(\beta) = \sum_{i=1}^N \log {P(G = k | X = x i; \beta)}
        \end{equation*}
        \item Differentiation and setting $\frac{\partial{l}}{\partial{\beta}} = 0$ leads to equations that are non-linear in $\beta$.
        \item These equations are solved using some optimization algorithm (e.g., Newton-Raphson, BFGS, etc.).
    \end{itemize}
\end{frame}

\begin{frame}[fragile]{Logistic regression on metal/insulator dataset}
\inputminted{python}{example_sklearn_logistic__regression.py}
\begin{figure}
    \centering
    \includegraphics[width=0.5\textwidth]{figures/logistic_metal_insulator.png}
\end{figure}
\end{frame}

\begin{frame}
    \Huge{\centerline{The End}}
\end{frame}

\end{document}


\documentclass[a4paper, leqno]{article}

\usepackage{amsmath, amsthm, amssymb, amsfonts, subfiles}
\usepackage{enumerate}
\usepackage{times, mathptmx}
\usepackage{fancyhdr}

\newtheorem{theorem}{Theorem}
\newtheorem{proposition}{Proposition}
\newtheorem{identity}{Identity}
\newtheorem{corollary}{Corollary}

\theoremstyle{definition}
\newtheorem{definition}{Definition}
\newtheorem{problem}{Problem}
\newtheorem*{solution}{Solution}

\theoremstyle{remark}
\newtheorem*{example}{Example}

\newcommand{\lcm}{\text{lcm}}

\author{\itshape Masum Billal}
\title{\scshape Nurani Number Theory}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{Nurani Number Theory}
\fancyhead[RE,LO]{Masum Billal}
\fancyfoot[CE,CO]{\thepage}

\begin{document}
	\maketitle
	\titlepage
	\section*{Preface ($2016$ Edition)}
		This is an edited version of a note I wrote in $2010-2011$ (approximately). The primary objective was not to write a note though. I used to write something everyday as a habit of practicing \LaTeX, specially when I learned about a new feature. The end result was this note. For that reason, you can see that the original document structure (I lost the source file of that one, so I re-wrote it) is quite clumsy. Also, some topics seem random and not really connected to the main topic. I am keeping them here anyway since they may be useful after all.

		The note was meant for BdMO math campers. However, it may be useful for any newcomer looking for interesting problems and ideas to solve them. This is not a textbook. And you should not use it as a reference for something rigorous such as definitions. The reason is that, I have focused more on making the sense rather than stating something rigorous that makes less sense. Moreover, I could not write too much at that time and even though I am writing it now, I hardly have the time to improve it or add more to it. You can see the references section for further reading.

		I want to take this chance to clear something up. From my personal experience, I have seen most of the beginners try to learn lots of theorems in order to be able to solve problems. They do this mostly as a mean of shortcut. I have tried a lot to change that thinking among the students. But it has become a tradition to follow that one must know thousands of theorems so everyone calls you master of number theory. There are two things to mention here. One is that you can never become a master of number theory. Second is that, even though the word \texttt{theory} is juxtaposed with \textbf{Number Theory}, by no means it implies that you must or should know a lot of theorems. \textbf{Stop memorizing theorems just because you think it's cool to tell others you know this theorem or that theorem}.

		Here is a real life example. Probably the first book I ever read for problem solving was \textit{Problem Solving Strategies} by \textsc{Arthur Engel}. I could not understand any of the first five chapters, so I skipped to chapter $6$, Number Theory. I could understand the facts discussed in the early section of that chapter. Then I skipped to the exercises. There were some problems I could not solve.
			\begin{enumerate}
				\item If $a+b+c$ is divisible by $6$, then so is $a^3+b^3+c^3$.
				\item If an integer can be expressed as a sum of squares, so can be the square of that integer.
			\end{enumerate}
		Now, this turned into something embarrassing when I saw the solutions. One can find brute force solutions for them. For example, I posed this problem to someone. He mentioned parity and considering cases of remainders upon division by $3$ etc. See this ingenious solution now. $a^3-a=a(a-1)(a+1)$, these are three consecutive integers. So, one is divisible by $2$, another by $3$. Consequently, the product is divisible by $6$. Therefore, $a^3-a+b^3-b+c^3-c=a^3+b^3+c^3-(a+b+c)$ is divisible by $6$, no matter what. This does not require any theory or any knowledge except for pure thinking. And that is the case for number theory, not the other way around. When I told \textit{Po-Shen Loh} this solution, he was like, "It's really nice!".\footnote{Po-Shen Loh is the coach of USAIMO team.}

		With that said, after you are done with this book, you may think that I am being two faced here. Therefore, I will explain it more. At first I have tried to show why you should solve problems as if you know nothing. That means, you will solve everything right from the start without any heads up. Once you think that you are at the point where you can find theorems or lemmas associated with a problem to solve, you will know what I mean. After that, you are ready to read about any book in number theory (though it depends on the pre-requisite of that book). The point is that, you should realize, we use theorems just to speed up our thinking process and save the time of doing the same thing twice all over again. We should not use theorems to actually solve problems. That is the reason why there is no choice but to discuss theorems when we talking about number theory. But this does not imply in any way that theorem is the core of number theory. It only means that we will study how the numbers dance and develop interesting properties. Although this is my personal opinion, I have found it to be true practically that, if you use theorems as means of solving problems rather than spending the time to gain your intuitive maturity, you can not solve problems beyond a certain level (roughly saying, I do not intend to argue about level of problems or anything). I hope you get my point. Even if you don't, it will not matter to me. But it will matter to me a lot if someone tries not to use theorems to make sense of something.

		Since this note was not reviewed or edited by anyone else, it may contain errors. One may find the definitions or some proofs too informal, but that is precisely the reason of creating this document. So, please do not brag about formality in this regard. Making better sense of something is more important to me than stating something that does not make a whole lot sense.

		Finally, I would like to mention that you can use or distribute it however you like as long as you don't use it for financial gain. Also, please don't share it after changing it yourself. If you want to change something, tell me first\footnote{I have decided not to let anyone change it manually every time someone wants to include/edit something. Rather you should do it in \textbf{Git} and create a pull request.}. Feel free to email me for making this better or if you want to be a contributor.
			\begin{flushright}
				\slshape Masum Billal\\
				\date{October $10$, $2016$}
			\end{flushright}
		\newpage
	\section{Divisibility}
		Note the following division of $97$ by $24$.
			\begin{align*}
				97 & = 4\cdot24+1
			\end{align*}
		In this division, we call $4$ the \textit{quotient} (the result of the division) and $1$ the \textit{remainder} (the part which was left) of this division. For the division $96 = 24 · 4 + 0$ we have the remainder $0$. In this case, we say that $96$ is divisible by $24$ (so by $4$ as well).
			\begin{definition}
				Let $a$ and $b$ be two natural numbers such that $b$ leaves remainder $0$ upon division by $a$. Then $b$ is said to be divisible by $a$. We denote it by $a|b$. Sometimes, the notation $b\vdots a$ is also used. But in this note, we shall make use of the notation of $a|b$ mostly.

				Here, $a$ is called a \textit{divisor} or \textit{factor} of $b$ and $b$ is called a \textit{multiple} of $a$. If $b$ leaves $a$ remainder other than $0$, then $b$ is not divisible by $a$ and is denoted by $a\nmid b$. Moreover, unless stated explicitly, we usually assume the associated integers are positive integers.
			\end{definition}

			\begin{example}
				$7|343$, $565655$ is a multiple of $5$, $29$ is a divisor of $841$ and so on.
			\end{example}
		Try some more examples and make sure with the notations and definitions of divisibility. Because your further reading of this note requires this excellency.
			\begin{definition}[Prime and Composite]
				A natural number $n$ is \textit{prime} if it has exactly $2$ positive divisors. Any positive integer that has more than $2$ positive divisors is a \textit{composite number}.
			\end{definition}
		You may notice that this definition is a bit different from what you know. But this definition clears up the ambiguity that keeps going around regarding $1$: is $1$ a prime or not?
			\begin{example}
				$2$ is the only even prime. If an even number is greater than $2$, then it must be divisible by $2$. Thus, it can not be a prime. First $3$ odd primes are $3, 5, 7$.
			\end{example}
	\subsection{Parity}
		\begin{definition}
			If a number leaves remainder $0$ upon division by $2$, then it is \textit{even}. If it leaves the remainder $1$, then it is  \textit{odd}. The property of a number being even or odd is called \textit{parity}. Two numbers are of the same parity if they both are odd or both are even. Otherwise they are of opposite parity. In other words, if two numbers give same remainder upon division by $2$, they are of the same parity, otherwise they are of opposite parity.
		\end{definition}

		\begin{example}
			$5$ and $7$ are of the same parity, whereas $4$ and $3$ are not.
		\end{example}

		\begin{proposition}
			The following statements are true.
				\begin{enumerate}[i.]
					\item The sum and difference of two numbers of the same parity is even.
					\item The sum and difference of two numbers of different parity is odd.
					\item Increasing or decreasing a number by a multiple of 2 does not change
					the parity.
					\item Any odd multiple of a number has the same parity of the number, and
					for even multiple has a parity even.
					\item The parity remains unchanged after raising to a power.
				\end{enumerate}
		\end{proposition}

		\begin{problem}
			The difference of two odd numbers is divisible by $2$ but not by $4$. Prove
			that their sum is divisible by $4$.
		\end{problem}
	How do we proceed to solve this? Since this involves divisibility by $2$, we should at least give parity a try. Here are two solutions.
		\begin{solution}[$1$]
			We have to take two odd number. So let us do the most obvious thing and assume that $2a+1$ and $2b+1$ are two odd numbers. From the condition, $2a+1-(2b+1)=2(a-b)$ is not divisible by $4$. This tells us that $a-b$ is odd. In that case, $a-b=2x+1$ for some integer $x$. We are required to show that $a+b$ must be divisible by $4$.
				\begin{align*}
					a+b & =2a+1+2b+1\\
						& = 2(b+2x+1)+1+2b+1\\
						& = 4b+4x+4\\
						& = 4(b+x+1)
				\end{align*}
			This is certainly divisible by $4$.
		\end{solution}

		\begin{solution}[$2$]
			What would be a good alternative approach to prove this claim? Since we need to prove some divisibility regarding $4$, we should consider what happens when we divide odd numbers by $4$. And not very surprisingly we find that an odd number is either of the form $4k+1$ or of the form $4l+3$. Therefore, we have three cases.
				\begin{enumerate}[(a)]
					\item Both odd numbers are of the form $4k+1$. However, this can not hold. The reason is that this would imply their difference is divisible by $4$ since $4k+1-(4l+1)=4(k-l)$.
					\item Both odd numbers are of the form $4k+3$. Same argument shows that this can not be true as well.
					\item We are only left with the option where one is of the form $4k+1$ and the other is of the form $4l+3$. This indeed complies with the condition of the statement since $4k+1-(4l+3)=2(2k-2l+1)$ and $2k-2l+1$ is odd. And if we sum them now, $4k+1+4l+3=4(k+l+1)$ is found to be divisible by $4$.
				\end{enumerate}
		\end{solution}
	A clever reader would ask themselves, how do we jump to the third case without checking the first two manually? This kind of thinking can lead you to direct and better solutions, whereas others may find some tedious solutions.
		\begin{proposition}
			Let $a$ and $b$ be two positive integers.
				\begin{enumerate}[i.]
					\item If $a|b$, then $\dfrac{b}{a}$ is an integer. So, there is an integer $k$ such that $\dfrac{b}{a}=k$ or $b=ak$. Moreover, we can say that $k|b$.
					\item For any integer $a$, $a|a$ and $a|0$.
					\item If $0|a$ then $a$ must be $0$.
					\item If we assume that $a|b$ then $|b|\geq |a|$ where $|a|$ denotes the absolute value of integer $b$.\footnote{This claim has a flaw in it. Find it!}
					\item The claim above is not entirely true. The only exception is that $b=0$.
					\item Let $c$ be an integer such that $a|c$. If $a|b$ holds true as well, $a|b\pm c$.
					\item This proposition can be generalized. If $a|b$ and $a|c$ then for any two integers $x,y$ we have $a|bx+cy$.
					\item[Euclid's Lemma] If $p$ is a prime and $a,b$ are positive integers such that $p$ divides $ab$, then at least one of $p|a$ or $p|b$ must be true.
					\item The least positive remainder in a division is unique.

					{\normalfont If $a|b$, then it must leave a remainder other than $0$. Say, it is $r$. Then, $b-r$ would be divisible by $a$. Let
						\begin{align*}
							b-r=aq & \iff b=aq+r
						\end{align*}
					When we mention such a remainder $r$, we usually mean the least positive remainder. To explain this, take the example $23=5\cdot4+3=5\cdot3+8$. So technically both $3$ and $8$ are remainders. But $3$ is the least positive remainder when $23$ is divided by $5$. Moreover, notice that the least positive remainder is less than the dividing number.

					What the proposition says is that, for positive integer $a,b$ there are unique integer $q$ and a unique positive integer $r$ such that $b=aq+r$ and $0\leq r<a$. We can prove this easily as well. And the uniqueness of $r$ can prove the uniqueness of $q$ as well (and vice versa). For the sake of contradiction, suppose that,
						\begin{align*}
							b = aq_1+r_1 & = aq_2+r_2
						\end{align*}
					where both $0\leq r_1,r_2<a$. From the latter, we get
						\begin{align*}
							a(q_1-q_2) & = r_2-r_1
						\end{align*}
					This equation says that $a$ divides $r_2-r_1$. Unless $|r_2-r_1|=0$, this can not be true (why?). The conclusion follows.}
					\item For all composite $n > 1$, $n$ has a prime divisor $p$ such that
						\begin{align*}
							p & \leq \sqrt{n}
						\end{align*}
					{\normalfont First you should think for yourself why this has to be true. Actually, no. First you should think if this is even true at all or I am playing with you. After you play around with some examples and convince yourself that this might actually be true, only then you can work on proving it. And this goes for all problems in general. Take $n=12$ and $n=35$. They have prime divisors $2$ and $5$. Take some more and you should realize why this must be true.

					Assume that the smallest prime factor of $n$ is $p$. Then $n = pk$ for some
					$k\geq p$. If $k < p$, then $k$ would have at least one prime factor less than
					$p$, but that is not possible. Therefore, $k \geq p$. Then
						\begin{align*}
							n & = kp\\
							  & \geq p^2\\
							\iff p& \leq \sqrt{n}
						\end{align*}
					Using this property, we can determine whether a number is a prime or
					not. Though this is not an efficient approach at all, it is very useful for
					small numbers.}
				\end{enumerate}
		\end{proposition}

	\section{GCD-LCM}
		Take the numbers $18$ and $12$ and consider their divisors. The list of their divisors is
			\begin{align*}
				\{1,2,3,6,9,18\} & , \{1,2,3,4,6,12\}
			\end{align*}
		Since $1$ belongs to both list, we will have at least one element common. But is there any other common element? In this case we have $1,2,3,6$. The greatest one among these common divisors is $6$. We call $6$ the \textit{greatest common divisor} of $12$ and $18$. We denote the greatest common divisor of $a$ and $b$ by $\gcd(a,b)$ or shortly $(a,b)$. In this note, we shall use this notation for brevity. When $(a,b) = 1$ that is two numbers do not have a common divisor other than $1$, then $a$ is called to be co-prime or relatively
		prime with $b$ and is denoted by $a \bot b$.
			\begin{example}
				$(6,28) = 2$, because $2$ is the most common part among them. $56\bot 243$, since $56=2^3\cdot7$ and $243=3^5$ do not share any common factor other than $1$.
			\end{example}
		Can you prove that it must be unique for any two positive integer? \textit{Least common multiple} follows from the idea of greatest common divisor. Both $a$ and $b$ have infinite multiples namely
			\begin{align*}
				a\cdot1,a\cdot2,\cdots\\
				b\cdot1,b\cdot2,\cdots
			\end{align*}
		For example, the multiples of $12$ are $12,24,36,\ldots$ The multiples of $18$ are $18,36,\ldots$. Now, an analogous question to greatest common divisor would be, is there a multiple of $12$ that is a multiple of $18$ as well? Well, the answer is simple. Yes, $12\cdot18$. Ok, but this makes us ask something not so obvious. What is the smallest possible positive integer that is a multiple of both $12$ and $18$? We know that such a multiple exists but the product of those two positive integers may not be the one we are looking for. In this case, $12\cdot18=216$ but as we can see, $36$ is the number with the desired property. We call $36$ the least common multiple of $12$ and $18$. It is denoted by $\lcm(a,b)$ or shortly $[a,b]$ sometimes.
			\begin{definition}
				$\gcd(a,b)$ is the greatest positive integer that divides both $a$ and $b$. $\lcm(a,b)$ is the least positive integer that is divisible by both $a$ and $b$.
			\end{definition}

			\begin{proposition}
				Assume that $g$ and $l$ are the greatest common divisor and least common multiple of two positive integers $a$ and $b$ respectively.
					\begin{enumerate}
						\item By definition, $g|a,b$ and $a,b|l$.
						\item $a$ divides $b$ if and only if $(a,b)=a$.
						\item Consequently, $g|l$, therefore, $l\geq a,b\geq g$ holds.
						\item If $p$ is a prime divisor of $a$ or $b$, then $p|l$. Similarly, if $q$ is a prime divisor of $g$, $q|a,b$.
						\item For any integers $x,y$ $g|ax+by$.
						\item $ab=gl$.
						{\normalfont This is easy to make sense. Since $g$ is the greatest common divisor, we can divide both $a$ and $b$ by $g$. Assume that, $a=gm$ and $b=gn$. From the definition, $m$ and $n$ can not have any common factor. This also means that $l=gmn$ (why?). The rest follows.}
						\item $(a,a)=a$ and $(a,0)=a$. Similarly, $[a,a]=a$ and $[a,0]=0$.
						\item[Euclidean Algorithm] If $b=aq+r$, then $(a,b)=(a,r)$.
						{\normalfont Probably you have already used it in $4$th or $5$th grade. But have you ever wondered why this works? Why does a same method not work for least common multiple? You can easily understand the reason if you think a bit. $g|a$ and $g|b=aq+r$. Then, $g|b-aq=r$ as well. This says that the greatest common divisor would be in the remainder too.}
						\item For positive integers $m,n$ $a^m\bot b^n$ if and only if $a\bot b$.
					\end{enumerate}
			\end{proposition}
		Prove the last proposition yourself.
		\section{Some Identities}
			\begin{identity}[Sophie Germain Identity]
				Let $a,b$ be positive integers.
					\begin{align*}
						a^4+4b^4 & = (a^2+2ab+2b^2)(a^2-2ab+2b^2)
					\end{align*}
				You may have already seen this while learning how to factorize\footnote{In Bangladesh, students are taught some factoring techniques such as \textit{middle term factor, completing the square}.}.
			\end{identity}

			\begin{corollary}
				If $a,b>1$ then $a^4+4b^4$ is composite.
			\end{corollary}
		Prove this. Also prove the opposite.
			\begin{problem}
				If $a^4+4b^4$ is a prime for natural number $a,b$, then $a=b=1$.
			\end{problem}
		The next identity is also taught in our high school. However, not many students of grade $9/10$ realize or know this.
			\begin{identity}\label{id:geodiv}
				Let $n$ be a positive integer.
					\begin{align*}
						a^n-b^n & = (a-b)(a^{n-1}+a^{n-2}b+\cdots+ab^{n-2}+b^{n-1})
					\end{align*}
			\end{identity}
		How many ways can you prove this? Induction is certainly a way. How about trying to find the sum $a^{n-1}+a^{n-2}b+\cdots+ab^{n-2}+b^{n-1}$? Moreover, can you find any similarity between this sum and a geometric series? Does that enable you to find a solution that is similar to finding the sum of a geometric series?
			\begin{corollary}
				For two positive integers $a,b$,
					\begin{align*}
						a-b & |a^n-b^n
					\end{align*}
				where $n$ is a positive integer.
			\end{corollary}

			\begin{corollary}
				If $n$ is an odd positive integer, then
					\begin{align*}
						a+b & |a^n+b^n
					\end{align*}
			\end{corollary}
		Prove this using \eqref{id:geodiv}.
			\begin{identity}[Fibonacci-Brahmagupta Identity]\label{id:fib}
				The product of two sum of squares is a sum of square as well.
					\begin{align*}
						(a^2+b^2)(c^2+d^2) & = (ad+bc)^2+(ac-bd)^2\\
											&= (ac+bd)^2+(ad-bc)^2
					\end{align*}
			\end{identity}
		Note that this product has two representations. Remember the second problem I mentioned in preface? Just notice this. If $n=x^2+y^2$,
			\begin{align*}
				n^2 & = (x^2+y^2)^2\\
					& = (x^2-y^2)^2+4x^2y^2\\
					& = (x^2-y^2)^2+(2xy)^2
			\end{align*}
		Here we used the fact $(a+b)^2=(a-b)^2+4ab$. This is something students learn at $6/7$th grade in Bangladesh. Yet, none of the people I asked to prove it has been able to find this simple solution. So, if you still think that the amount of theory you know somehow defines your level\footnote{If you believe in level theory, that is}, then feel free to contact me. Maybe I can show you a thing or two.

		Anyway, we can generalize identity \eqref{id:fib}.
			\begin{align*}
				(a^2+nb^2)(c^2+nd^2) & = (ac-bd)^2+n(ad+bc)^2
			\end{align*}
		Find the other representation yourself.
			\begin{identity}[Bhaskara's Lemma]
				Let $n$ be a square-free integer\footnote{It does not have a divisor that is a perfect square other than $1$. However, we could take $n$ to be any integer and we would only end up with something like $x^2-y^2=l$.} and $k$ be an arbitrary integer. If $y^2-nx^2 = k$ then for any integer $m$,
					\begin{align*}
						\dfrac{(mx+ny)^2}{k}-n\left(\dfrac{mx+y}{k}\right)^2 & = \dfrac{m^2-n}{k}
					\end{align*}
			\end{identity}
		This identity allows us to solve \textit{Pell-Fermat equation} which is a type of \textit{Diophantine equation}.
		\subfile{mod.tex}
	\begin{thebibliography}{99}
		\bibitem{engel} \textit{Arthur Engel}, Problem-Solving Strategies (Chapter $6$), 1998 Springer-Verlag New York, Inc.

		\bibitem{paul} \textit{Paul Zeitz}, The Art and Craft of Problem Solving (Chapter $7$), John Wiley \& Sons, Inc.
		\bibitem{titu} \textit{Titu Andreescu, Dorin Andrica}, Number Theory: Structures, Examples, and Problems, Springer, $2009$.

	\end{thebibliography}
\end{document}


\documentclass[simplex.tex]{subfiles}
% DO NOT INCLUDE PREAMBLES/PACKAGES HERE!!
% packages are inherited from preamble.tex; you can compile this on its own
\begin{document}
\subsection{Graph-testing}

In neuroimaging connectomics studies, it is often desired to determine
whether the observed network properties are statistically significant or
not. In order to correctly achieve this, we need to define a null
distribution. In order to generate the null distribution, the common
technique is to  generate an average of 1000 samples of graphs with the
same degree sequence of the observed graphs. However, this technique
does not yield a uniform sample from the null distribution, resulting in
ill-conditioned tests. Here, we investigate statistically accurate
methods in graph testing. \\

One strategy that we have explored is generating the null distribution
by sampling from graphs with the same degree-sequence of the observed
graph. While the samples are not generated uniformly, we know how to
rescale the samples such that we can estimate the mean of the uniform
distribution of the graphs. We are currently working on to extend this
algorithm to validly estimate the 95 percentile as well. \\

Another technique that we have explored is using parametric bootstrap to
obtain the critical region for any significance level. Namely, we fit a
stochastic block model to the data and use Generalized Likelihood Ratio
Test to determine the number of blocks that best fit the data. In the
initial experiments, as expected, we see that the power of the test
increases as the graph size increase. As the next step, after finding
the model that best first the data, we can sample from that distribution
many times, compute the test statistic, and get the critical region for
any significance level. In this strategy we are not conditioning the
graphs on their degree sequence, which is an advantage as the graphs
tend to include noise so we cannot use the observed degree sequence as
the ground truth.

%%%   EXAMPLE FIGURE BLOCK
%\begin{figure}[!h]
%\begin{cframed}
%\centering
%\includegraphics[width=0.15\textwidth]{../../figs/neurodata_small.png}
%\caption{Please provide a detailed caption for your figure.}
%\label{fig:name}
%\end{cframed}
%\end{figure}

\clearpage
\end{document}


\subsection{Complex logarithms}


\documentclass[]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{color}
\usepackage{fancybox}
\usepackage{graphicx}
\usepackage[pdftex,colorlinks]{hyperref}
\usepackage{pdfpages}
\renewcommand*\rmdefault{cmdh}
%\usepackage[T1]{fontenc}
\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}
\newcommand{\subscript}[1]{\ensuremath{_{\textrm{#1}}}}
\renewcommand{\thesection}{}

\def\thesection{\arabic{section}}
\def\thesubsection{\arabic{section}.\arabic{subsection}}
\def\thesubsubsection{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}}

\begin{document}
\rmfamily
\setcounter{secnumdepth}{3}

	% Cover Page
	\begin{titlepage}
		\includepdf[pages={1}]{title_page.pdf}
	\end{titlepage}

	% Introduction

	\section{Introduction}
		As the demand for mixed mode integrated circuits increases, the design of analog circuits such as operational amplifiers in CMOS technology becomes more critical. This technology has become an integral part of many integrated circuit chips fabricated today in a number of application areas. These amplifiers offer a number of advantages in terms of power dissipation, die area, and compatibility with digital circuits when compared to their bipolar counterparts.

		In this paper we design and simulate a specific, widely used operational amplifier architecture, showing in detail how the formulation of the design variables takes place through a number of equations derived and calculated by hand. We then simulate the amplifier using HSPICE, presenting performance measures such as unity-gain bandwidth, open-loop gain, and settling time. We show how the proposed amplifier meets the specifications set in place by the project guidelines. We conclude with a discussion of the characteristics of the amplifier and the overall experience of the design project.

		% Circuit Schematic
		\begin{figure}
			\subsection{Circuit Schematic}
			\includegraphics[width=1.2\textwidth]{CMOS_complete.pdf}
			\caption{Two stage op-amp considered in this paper.}
		\end{figure}
		\newpage

	% Hand Analysis Begins Here...
	\section{Part I: Hand Analysis}
		For the purposes of designing this amplifier, the design approach is as follows. First, we lay the groundwork for all important equations that must be considered in order to meet the specifications. We note dependencies and realize variables that enable design control. We then begin with the most rigorous specifications and choose our variables to be within margins that exceed the requirements, to provide a safety cushion against non-idealities. We keep all constraints in mind and finalize a design. We confirm that the requirements are met.

		\subsection{Equations}

		In this section, all of the relevant equations required to meet the specifications are listed for the purposes of demonstrating the interdependencies between variables and to illustrate the design process of the amplifier.
		$$$$
		Specifications to be met:

		\begin{itemize}
			\item Gain $\ge 1500 $
			\item Output Swing : 1.2V
			\item Settling Time : 8ns with 0.4V step
			\item Unity Gain Frequency $\ge 600 MHz$
			\item Common Mode Input Range: 0.8V
			\item CMRR $\ge 75dB$
			\item PSRR $\ge 60dB |_{DC} \ \ 50dB$ at 1MHz
			\item Power Dissipation: 1.5mW
		\end{itemize}

		\pagebreak

			\subsubsection{Settling Time}
				$$$$
				To meet this specification, we will design our amplifier to have a slew rate that is one tenth of the settling time. Although this is not a one to one correspondence with the specification, settling time and slew rate are closely related, and achieving the above margin is a good indication that the spec has been met.\footnote{\tiny C. T. Nguyen, Lecture 24, April 25, 2013}

				$$ SR = \frac{10V_{0}}{T_{s|0.1\%}}$$
				$$T_{s|0.1\%} = 8ns \ \ with\ \  0.4V Step$$
				$$ SR = \frac{10(0.4)}{8ns}$$
				$$SR = 500 \frac{V}{\mu S} $$
				$$SR = \frac{I_{xm}}{C} $$

				\begin{center}\framebox{$\frac{I_{xm}}{C_{}} \ge 5\times10^{8} V/s $}\end{center}
				where C is the largest capacitance in the circuit, and Ixm is the limiting current.
				$$$$

			\subsubsection{Unity Gain Frequency}
				$$$$
				$$ugf= \frac{gm_{I}}{C_{}} $$
				$$ugf = 2\pi 600MHz$$
				\begin{center}\framebox{$\frac{gm_{I}}{C_{}} \ge 2\pi 600 MHz $}\end{center}

			\subsubsection{Phase Margin}
				$$$$
				Phase margin, settling time, and unity gain frequency are all closely related and impact the stability of the circuit in feedback. Aiming for a target phase margin that is conducive to meeting the above two specs,
				$$PM = 65^{\circ}$$
				$$\omega_{p2} \ge tan(65)\times 2\pi 600Mhz$$
				\begin{center}\framebox{$\frac{gm_{II}}{C_{I}+C_{II}} \ge tan(65)\times 2\pi 600MHz$}\end{center}
				\pagebreak

			\subsubsection{Gain}
				$$$$
				Total Gain: $A_{v} = A_{v_{1}}A_{v_{2}}$

				$$ A_{v_{1}} = gm_{3}\left(\frac{\frac{V_{A4}}{I_{D3}}\frac{V_{A5}}{I_{D3}}}{\frac{V_{A4}}{I_{D3}}+\frac{V_{A5}}{I_{D3}}}\right) \ \ \ \ \ \ \ \ \ \ A_{v_{2}} = -gm_{6}\left(\frac{\frac{V_{A6}}{I_{D6}}\frac{V_{A9}}{I_{D6}}}{\frac{V_{A6}}{I_{D6}}+\frac{V_{A9}}{I_{D6}}}\right)$$
				\newline
				$$ A_{v} = \frac{-gm_{3}gm_{6}}{I_{D3}I_{D6}}\left(\frac{V_{A4}V_{A5}}{V_{A4}+V_{A5}}\right)\left(\frac{V_{A6}V_{A9}}{V_{A6}+V_{A9}}\right) \footnote{\tiny Paul Gray, Robert Meyer, "Analysis and Design of Analog Integrated Circuits" (Wiley and Sons, 2010), 423}$$
				\newline
				Using the relation $ \frac{gm}{I_{D}} = \frac{2}{r_{0}} $ gives us
				\newline
				$$ A_{v} = \frac{-4}{V_{ov3}V_{ov6}}\left(\frac{\frac{1}{\lambda_{n}}\frac{1}{\lambda_{p}}}{\frac{1}{\lambda_{n}}+\frac{1}{\lambda_{p}}}\right)^2$$
				$$A_{v}= \frac{32.65}{V_{ov3}V_{ov6}}$$
				\begin{center}\framebox{$\frac{32.65}{V_{ov3}V_{ov6}} \ge 1500$}\end{center}

			\subsubsection{Output Swing}
				$$$$
				In order to satisfy an output swing of 1.2V we must satisfy the following:
				$$ V_{out, _{max}}  = V_{DD} - V_{ov_{9}} = 1.35 V $$
				$$ V_{out, _{min}}  =  V_{ov_{6}} = 0.15 V$$
				\begin{center}\framebox{$V_{ov9} \le 0.15V$}\end{center}
				\begin{center}\framebox{$V_{ov6} \le 0.15V$}\end{center}
				\pagebreak

			\subsubsection{Common Mode Rejection Ratio}
				$$$$
				\begin{center}
				CMRR = $|\frac{A_{dm}}{A{cm}}|$
				\end{center}

				The common mode rejection ratio is only dependent upon the first stage of the amplifier, because the second stage is a single ended input and output.
				\newline
				\begin{center}
				CMRR = $2gm_{3}r_{07}gm_{2}(r_{03} || r_{02})$

				= $\frac{4}{V_{ov3}V_{ov2}}\left(\frac{\frac{1}{\lambda_{n}}\frac{1}{\lambda_{p}}}{\frac{1}{\lambda_{n}}+\frac{1}{\lambda_{p}}}\right) \footnote{\tiny Paul Gray, Robert Meyer, "Analysis and Design of Analog Integrated Circuits" (Wiley and Sons, 2010), 427} $

				\end{center}
				\begin{center}\framebox{$\frac{11.43}{V_{ov3}V_{ov2}} \ge 75dB$}\end{center}

			\subsubsection{Power Supply Rejection Ratio}

				In calculating the PSRR, we will neglect the variations due to the positive supply, $V_{DD}$. This is because $PSRR_{+}\rightarrow \infty $ for low frequencies with perfect matching \footnote{\tiny Paul Gray, Robert Meyer, "Analysis and Design of Analog Integrated Circuits" (Wiley and Sons, 2010), 431}. This analysis is shown in G$\&$M page 431.

				Instead, we will calculate the PSRR that emerges due to variations from the $V_{SS}$ supply, which in this case is ground.
				$$A_{-} = \frac{v_{o}}{vss} = \frac{r_{09}}{r_{06}+r_{09}} = \frac{\frac{1}{\lambda_{p}}}{\frac{1}{\lambda_{n}}+\frac{1}{\lambda_{p}}} \footnote{\tiny Paul Gray, Robert Meyer, "Analysis and Design of Analog Integrated Circuits" (Wiley and Sons, 2010), 431}$$
				$$PSRR = \frac{A_{dm}}{A_{-}}$$
				\begin{center}\framebox{$\frac{A_{dm}}{0.5714} \ge 60 dB$}\end{center}

				\pagebreak

			\subsubsection{Common Mode Input Range}

				When $V_{IC}$ is reduced to the point where $ V_{GD_{3}} = V_{GD_{5}}  = V_{t_{3}} = V_{t_{5}}, M3 $ and $ M5 $ operate at the edge of saturation.
				Thus, we define the lower end of the common mode input range to be :

				$$ V_{IC} \ge V_{t_{3}} + V_{t_{2}}+V_{ov_{2}}  $$
				$$ V_{IC} \ge V_{ov_{2}}  $$

				The specification calls for a common mode input range that is $ 0.8V $ inside the output swing range. This gives us a constraint on $  V_{ov_{2}} $

				$$ V_{IC_{min}} = 0.125 V $$
				\begin{center}\framebox{$ V_{ov_{2}}  \le 0.125V$}\end{center}

				If $ V_{IC} $ is too high, we have $M7$ falling into the triode region. Examining the drain voltage of $ M7,$

				$$ V_{DS_{7}} = V_{IC} - V_{GS_{3}} -V_{DD} = V_{IC} - V_{t3} - V_{ov_{3}} - V_{DD} $$

				Therefore, $ V_{IC} $ must satisfy the following:

				$$ V_{IC} < V_{DD_{}}  -	| V_{t_{3}}| - |V_{ov_{3}}| - |V_{ov_{7}}| $$
				$$ V_{IC} < 1.5  -0.3 - |V_{ov_{3}}| - 0.15 $$
				$$ V_{IC} < 1.05 - |V_{ov_{3}}|  $$
				From the specification,
				$$V_{IC_{max}} = 0.925 V$$
				$$ 0.925 < 1.05 - |V_{ov_{3}}|  $$
				\begin{center}\framebox{$ |V_{ov_{3}}| < 0.125$}\end{center}

				\pagebreak

		\subsection{Design Equations and Sizing}
			In this section, we illustrate the design process of the amplifier. With the knowledge of the equations and constraints, we are able to make design decisions. These design decisions are based partly on intelligent speculations and in a large part on the equations and constraints. We complete the design with dimensions for each transistor and the value of the compensating capacitor, $C_{c}$.
			$$$$
			For consistency with the load capacitance, we choose $C_{c}$ to be 2pF. This gives us the following:
			\subsubsection{Slew Rate}
				$$\frac{I_{xm}}{2pF} \ge 500 000 000 $$
				$$I_{xm} \ge 1mA$$

			\subsubsection{Unity Gain Frequency}
				$$\frac{gm_{I}}{2pF} \ge 2\pi 600 MHz $$
				$$gm_{I} \ge 7.53mS$$

			\subsubsection{Output Swing}
			$$V_{ov9} \le 0.15V$$
			$$V_{ov6} \le 0.15V$$

			To have a comfortable margin, we set $V_{ov6,9} = 0.09 V$

			\subsubsection{Gain}
				$$\frac{32.65}{V_{ov3}V_{ov6}} \ge 1500$$
				With $V_{ov6} = 0.09$,
				$$v_{ov3} \le 0.24 V $$
				Set $V_{ov3} = 0.1V$ to satisfy the common mode input range spec.
				$$$$

				\pagebreak

			\subsubsection{Sizing of M3, M5}
				To reduce capacitances and minimize power and current consumption, set all channel lengths to the minimum value, $L_{min} = 130 nm$.
				$$ gm_{3} = k_{p}'W/L(V_{ov}) $$
				$$ 0.00753 = k_{p}'W/L(0.1) $$
				$$ W/L_{3,5} = 73712/130 $$

			\subsubsection{Sizing of M7, M9}
				$$$$
				As we can see from the settling time constraint, the circuit requires the maximum current we have available given the power restriction. With this in mind, we choose a reasonable value from experience and set the current through M9 to be 375 microamps and bias the output at half of the supply voltage. In addition, we match transistors 7 and 9 to ensure that the settling time specification is met, since both transistors supply current through large capacitors.
				$$0.5k_{p}'(W/L)_{}(V_{ov})^2(1+0.15(V_{SD}) = 375 \mu A $$
				$$0.5k_{p}'(W/L)_{}(0.09)^2(1+0.15(0.75) = 375 \mu A $$
				$$W/L_{7,9} = 81474/130$$

			\subsubsection{Sizing of M1}
				$$$$
				In order to meet the power dissipation requirement, the current through M1 must be no more than 250 microamps.
				$$0.5k_{p}'(W/L)_{}(V_{ov})^2(1+0.15(V_{ov}+V_{t}) = 250 \mu A $$
				$$0.5k_{p}'(W/L)_{}(0.09)^2(1+0.15(0.39) = 250 \mu A $$
				$$W/L_{1} = 57087/130$$

			\subsubsection{Sizing of M6}
				$$0.5k_{n}'(W/L)_{}(V_{ov})^2(1+0.2(0.75) = 375 \mu A $$
				$$0.5k_{n}'(W/L)_{}(0.09)^2(1+0.2(0.75) = 375 \mu A $$
				$$ W/L_{6} = 31527/130 $$

			\subsubsection{Sizing of M2, M4}
				$$$$
				From common mode input range,
				$$V_{ov2,4} \le 0.15V $$
				At equality we have,
				$$0.5k_{n}'(W/L)_{}(V_{ov})^2(1+0.2(V_{DS}) = 188 \mu A $$
				The drain-source voltage of M4 is the Vgs of M6. With this in mind,
				$$0.5k_{n}'(W/L)_{}(0.15)^2(1+0.2(0.39) = 188 \mu A $$
				$$W/L_{2,4} = 6053/130 $$

			\subsubsection{RHP Zero}
				$$$$
				We want to set the resistor, $R_{z}$, to a value such that the zero introduced by the circuit is pushed out to infinity. To do this we set,
				$$ R_{z} = 1/gm_{II} $$
				$$ gm_{II} = k_{n}'W/L(V_{ov}) = 7.24 mS $$
				$$R_{z} = 138 \Omega$$
				$$R_{z} = L/(Wk_{n}'(V_{GS}-V_{t})) $$
				$$ W/L_{r} = 2012/130 $$

			\subsubsection{Frequency Response}
				$$$$
				We would like to impart a phase margin of 60 degrees or better to offer a circuit that is stable under unity gain feedback and yields a quick settling time for sharp rising signals. To this end, we must push the second pole out past the value shown in the equations section above. In order to satisfy this, we must meet the following constraint,
				$$C_{I}+C_{II} \le 8.96\times 10^{-13}$$
				The calculations involving the derivation of the capacitances is shown in a\\ MATLAB script below and we can see that the requirement is met. We have included the estimated areas and relevant perimeters of the drain and source of the transistors as per the project guidelines.

				\pagebreak
				\includepdf[pages={1}]{matlab_script.pdf}
				\includepdf[pages={2}]{matlab_script.pdf}

	\section{Part II: Simulation}
		We go from the design parameters we have just derived and simulate the op-amp in hspice. We make adjustments to certain dimensions as necessary for biasing. For instance, the width of M6 had to be reduced in order to allow for saturation conditions for all transistors. Other transistors were adjusted to reduce power consumption. We make adjustments to certain dimensions as necessary for the fulfillment of specifications as well. Slight tweaking of all transistors is required in order to comply with the 65nm technology used for this design. Transistors were also sized to reduce or increase overdrive voltages. Through many iterations, the devices are sized in such a way that the project parameters are all met. Dimensions used in the simulation of the circuit are within 10 percent of the estimated values except where noted. Wherever variances lie, the hand calculated values rectify these disparities when the values in the equations used in the above calculations are substituted with values from the simulation.\\ \\
		We have met all of the specifications called for in the design and have done so adequately. Graphs, tables and simulated values are now shown to substantiate the performance of this op-amp.

		\begin{figure}
			\subsection{Transistor and Bias Summary}
				$$$$
				\subsubsection{Biasing and small signal parameters}
				As we can see, all bias currents are within $10\%$ of hand calculated values. The overdrive voltage of $M1,7,9$ were further reduced to increase efficiency.

				\includegraphics[width=1.25\textwidth]{operating_point.pdf}
		\end{figure}

		\begin{figure}
			\includegraphics[width=1.3\textwidth]{operating_point_page2.pdf}
			\caption{Summary of biasing and small signal parameters simulated in hspice}
		\end{figure}
		\newpage
		\begin{center}
			\begin{figure}
				\subsubsection{Transistor Dimensions}
				All transistor sizes are within $10\%$ of the expected values from the hand calculations with the exception of $M2,4,6$ and $MR$. $M6$ was manually resized to secure saturation conditions across all transistors. The simulation shows that this transistor is in the linear region when the size is in the range of the hand calculated value. The size of $M6$ was reduced until the transistor was saturated and the output voltage was biased at a value close to $0.75V$. A similar procedure was initially taken with $M2,4$. $MR$ was manually resized to provide a good phase margin. We are able to manually resize this device by pulling the newly simulated value of $gm_{II}$ and adjusting the width until the resistance is approximately equal to $1/gm_{II}$.The result is a circuit that matches all other expected values to within $10\%$ or better.
				$$$$

  				\begin{tabular}{ | l | l | l | p{5cm} |}
   			 		\hline
   			 		Transistor & Width & Length &------------\\ \hline
   			 		M1 & 61,100nm & 130nm & \\ \hline
    					M2 & 2,535nm & 130nm & \\ \hline
    					M4 & 2,535nm & 130nm & \\ \hline
					M3 & 74,230nm & 130nm & \\ \hline
					M5 & 74,230nm & 130nm & \\ \hline
					M6 & 5,460nm & 130nm & \\ \hline
					M7 & 88,400nm & 130nm & \\ \hline
					M9 & 88,400nm & 130nm & \\ \hline
					MR & 845nm & 130nm & \\ \hline
	  			  	\end{tabular}
			\caption{Table of device dimensions.}
			\end{figure}
			\end{center}
			\newpage

			\begin{center}
			\begin{figure}
				\subsection{Performance Summary}
  				\begin{tabular}{ | l | l | l | p{2cm} |}
   			 		\hline
   			 		Parameter & Specification & Circuit Performance & Spec Met? \\ \hline
   			 		DC Gain & 1500 & 5121 & Yes\\ \hline
    					Common Mode Input Range & 0.8V & 1.35V & Yes\\ \hline
    					Output Swing & 1.2V & 1.275V & Yes\\ \hline
					Power Dissipation & 1.5mW & 1.48mW & Yes\\ \hline
					Unity Gain Frequency & 600 MHz & 725 MHz & Yes\\ \hline
					Settling Time & 8ns & 7.8ns rising, 7.78ns falling & Yes\\ \hline
					CMRR & 75dB & 83.2dB & Yes\\ \hline
					PSRR at DC & 60dB & 92dB & Yes\\ \hline
					PSRR at 1MHz & 50dB & 94.7dB & Yes\\ \hline
					Lmin & 130nm & 130nm & Yes\\ \hline
					Wmin & 195nm & 845nm & Yes\\ \hline
					Temperature & 25C & 25C & Yes\\ \hline
  			  	\end{tabular}
			\caption{Table of simulated specifications}
			\end{figure}
			\end{center}

		\newpage

		\begin{figure}
			\subsection{DC Gain, Phase Margin, and Unity Gain Frequency}
			The graphs below show the simulated results of the amplifier's open loop gain, frequency response, and phase margin at unity gain.
				\subsubsection{DC Gain}
				\includegraphics[width=1.1\textwidth]{diff_gain_DC.pdf}
				\caption{The differential gain of the amplifier is $74.18dB$}
		\end{figure}

		\begin{figure}
			\subsubsection{ Unity Gain Frequency}
			\includegraphics[width=0.9\textwidth]{diff_gain_UNITY.pdf}
			\caption{The unity gain frequency of the amplifier is $725.98MHz$}
			\subsubsection{Phase}
			\includegraphics[width=0.9\textwidth]{diff_gain_PHASE.pdf}
			\caption{The phase at unity gain is $-113^{o}$. The phase margin at unity is $67^{o}$}
		\end{figure}

		\begin{figure}
			\subsection{CMRR and PSRR}
			The graph below shows the amplifier's common mode rejection ratio and power supply rejection ratio. By taking the ratio of the common mode gain to the differential mode gain, we compute the common mode rejection ratio. By taking the ratio of the power supply gain to the differential mode gain, we compute the power supply rejection ratio. These values can be found in figure 3 above.\\
			\\
			Note that the power supply gain from the negative source can be computed by the equation offered in section 2.1.7. With the amplifier providing a gain on the order of $74$dB at low frequencies and roughly $60$dB at $1MHz$, it is clear that $PSRR_{-} > 60dB$.
				\subsubsection{Common mode gain}
				\includegraphics[width=1.1\textwidth]{cmrr_DC.pdf}
				\caption{The common mode gain of the amplifier is $-9.03dB$}
		\end{figure}

		\begin{figure}
			\subsubsection{ Power supply gain at DC}
			\includegraphics[width=0.89\textwidth]{psrr_DC.pdf}
			\caption{The power supply gain at $DC$ is $-17.9dB$}
			\subsubsection{Power supply gain at 1MHz}
			\includegraphics[width=0.89\textwidth]{psrr_1MHz.pdf}
			\caption{The power supply gain at $1MHz$ is $-20.5dB$}
		\end{figure}

		\begin{figure}
			\subsection{Common Mode Input Range}
			This is defined as the range of voltages at which $dVout/dVin$ becomes $1/2$ in unity gain feedback. To show this, we put the op amp into unity gain configuration and sweep the $DC$ voltage at the input from 0 to $V_{DD}$. The graphs below show the maximum and minimum of this range. This amplifier exceeds the specification by a relatively large margin. This is partly due to tying the bulk to $V_{DD}$ in $M3,5$. This was done because the body effect on the input transistors can be used to increase the range\footnote{\tiny Paul Gray, Robert Meyer, "Analysis and Design of Analog Integrated Circuits" (Wiley and Sons, 2010), 428}. The total common mode input range of the amplifier is listed in figure 3.
				\subsubsection{Common Mode Input Range, minimum}
				\includegraphics[width=0.6\textwidth]{common_mode_input_range_1.pdf}
				\caption{The minimum value corresponds to an output voltage of $0.115V$}
				\subsubsection{Common Mode Input Range, maximum}
				\includegraphics[width=0.6\textwidth]{common_mode_input_range_2.pdf}
				\caption{The maximum value corresponds to an output voltage of $1.47V$}

		\end{figure}

		\begin{figure}
			\subsection{Output Swing}
			This is defined as the range of voltages at which $dVout/dVin$ becomes $1/10th$ the nominal differential gain. To show this, we sweep the $DC$ voltage at one of the inputs around its bias points and find the points on the curve where the derivative is approximately equal to $1/10th$ of the nominal gain. The graphs below show the minimum and maximum of this range. The total output swing of the amplifier is listed in figure 3.
				\subsubsection{Output Swing, minimum}
				\includegraphics[width=0.65\textwidth]{output_swing.pdf}
				\caption{The minimum value corresponds to an output voltage of $0.168V$}
				\subsubsection{Output Swing, maximum}
				\includegraphics[width=0.65\textwidth]{output_swing_2.pdf}
				\caption{The maximum value corresponds to an output voltage of $1.44V$}

		\end{figure}

		\begin{figure}
			\subsection{Settling Time}
			This is the most stringent specification to meet, as it requires relatively large amounts of current to be supplied from the current sources. We measure settling time by putting the op amp into unity gain feedback and applying a $0.4V$ step to the input. We then run a transient analysis in hspice to determine the time it takes for the amplifier's output to converge to within $0.1\%$. The time it takes to converge must be under $8$ns.\\ \\
			In order to meet this specification, some changes to device dimensions were made in order to increase the current to a value high enough to just meet the spec. Increasing the sizes of the current sources allowed for a better settling time; the drawback however, is that power consumption became excessive. To ameliorate this, the size of the biasing transistor was reduced and the bias resistor increased in order to preserve roughly the same overdrive voltage. The result is an amplifier that has a settling time of just under $8$ns and consumes less than $1.5$mW of power.
				\subsubsection{Settling Time}
				\includegraphics[width=1.1\textwidth]{settling_time_zoomed_out.pdf}
				\caption{$0.4V$ step input and amplifier response}
		\end{figure}

		\begin{figure}
				\subsubsection{Settling Time, rising input}
				\includegraphics[width=0.8\textwidth]{settling_time_t1.pdf}
				\caption{The time it takes to converge on a rising input is $7.88ns$}
		\end{figure}

		\begin{figure}
				\subsubsection{Settling Time, falling input}
				\includegraphics[width=0.8\textwidth]{settling_time_t2.pdf}
				\caption{The time it takes to converge on a falling input is $7.78ns$}
		\end{figure}
		\pagebreak
		...
		\newpage
		\section{Conclusion}
			We have shown the design process of an analog integrated circuit CMOS operational amplifier, and we have shown how equations we have derived throughout the semester participate in this process. We have seen how tradeoffs must be made between certain parameters in order to provide an amplifier that is balanced and meets a wide range of specifications. For the most part, the amplifier considered in this paper is ideal for use in digital circuits or mixed signal circuits where the characteristics of the stages connected to the op-amp are known in advance. This amplifier provides a good gain for digital signals (digital signals typically do not require as much gain as their analog counterparts), a wide bandwidth, stellar phase margin for gains down to and below unity, a very quick settling time, and consumes very little power. Furthermore, the amplifier has a very good power supply rejection ratio which offers outstanding performance in digital circuits with high levels of switching noise. Overall, the amplifier we have designed is a stable, low power op-amp that is the quintessential CMOS operational amplifier utilized by engineers throughout the world.
			$$$$
			The design of this op amp was a scrupulous and testing process, and a tremendous learning experience as well. To see how all of the many variables depended on each other required me to think in a way that I have never before and forced me to stretch and expand my mind to such great heights. Arguably, a better way to design a circuit like this is to write a geometric program and have a computer automatically compute a robust design given the parameters and specifications. This implies that the circuit designer can spend more time doing real design, $i.e.,$ carefully analyzing the optimal trade-offs between competing objectives and less time doing parameter tuning or wondering whether a certain set of specifications can be achieved. However, for the purposes of this course and the learning experience required to become a competent circuit designer, it is necessary to do this type of analysis by hand and think about the circuit ourselves, rather than have a computer do the thinking for us. At the end of the day, I can say that I have a solid grasp and comprehension of the way a relatively large MOS circuit like this operates. I realize the interdependencies between variables like dimension, transconductance, and overdrive voltage, and I acknowledge the tradeoffs required to satisfactorily meet an extensive range of specifications. Overall, the wisdom and erudition bestowed upon me as a result of successfully completing this project is immense, and it was undeniably worth the effort.

\end{document}


    \filetitle{length}{Number or priors in system priors object}{systempriors/length}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
N = length(S)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{S} {[} systempriors {]} - System priors,
  \href{systempriors/Contents}{\texttt{systempriors}} object.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{N} {[} numeric {]} - Number of priors imposed in the system
  priors object, \texttt{S}.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}