%%
%% Automatically generated file from DocOnce source
%% (https://github.com/hplgit/doconce/)
%%
%%

%-------------------- begin preamble ----------------------

\documentclass[%
oneside,                 % oneside: electronic viewing, twoside: printing
final,                   % draft: marks overfull hboxes, figures with paths
10pt]{article}

\listfiles               %  print all files needed to compile this document

\usepackage{relsize,makeidx,color,setspace,amsmath,amsfonts,amssymb}
\usepackage[table]{xcolor}
\usepackage{bm,ltablex,microtype}

\usepackage[pdftex]{graphicx}

\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}

\usepackage{lmodern}         % Latin Modern fonts derived from Computer Modern

% Hyperlinks in PDF:
\definecolor{linkcolor}{rgb}{0,0,0.4}
\usepackage{hyperref}
\hypersetup{
    breaklinks=true,
    colorlinks=true,
    linkcolor=linkcolor,
    urlcolor=linkcolor,
    citecolor=black,
    filecolor=black,
    %filecolor=blue,
    pdfmenubar=true,
    pdftoolbar=true,
    bookmarksdepth=3   % Uncomment (and tweak) for PDF bookmarks with more levels than the TOC
    }
%\hyperbaseurl{}   % hyperlinks are relative to this root

\setcounter{tocdepth}{2}  % levels in table of contents

% prevent orhpans and widows
\clubpenalty = 10000
\widowpenalty = 10000

% --- end of standard preamble for documents ---

% insert custom LaTeX commands...

\raggedbottom
\makeindex
\usepackage[totoc]{idxlayout}   % for index in the toc
\usepackage[nottoc]{tocbibind}  % for references/bibliography in the toc

%-------------------- end preamble ----------------------

\begin{document}

% matching end for #ifdef PREAMBLE

\newcommand{\exercisesection}[1]{\subsection*{#1}}

% ------------------- main content ----------------------

% ----------------- title -------------------------

\thispagestyle{empty}

\begin{center}
{\LARGE\bf
\begin{spacing}{1.25}
Project 5, deadline  December 14, 2018
\end{spacing}
}
\end{center}

% ----------------- author(s) -------------------------

\begin{center}
{\bf Molecular Dynamics project${}^{}$} \\ [0mm]
\end{center}

\begin{center}
% List of all institutions:
\end{center}

% ----------------- end author(s) -------------------------

% --- begin date ---
\begin{center}
Fall semester 2018
\end{center}
% --- end date ---

\vspace{1cm}

\subsection*{Theoretical background and description of the physical system}

In this project we will implement a model called molecular dynamics
(MD). Molecular dynamics allows us to study the dynamics of atoms and
explore the phase space. For those of you who are taking FYS2160 -
Thermodynamics, this project will be an excellent choice. The atoms
interact through a force given by the negative gradient of a potential
energy function. With this force, we can integrate Newton's
laws. While exploring the phase space, we will sample statistical
properties of the system like energy, temperature, pressure, diffusion
constant and so on.  The project can be seen also as an extension from project 3,
where you wrote an object-oriented code for the solar system.

Here we will use another potential  for modeling the interaction between atoms.
Molecular dynamics is based on the assumption that even atoms move
according to the laws of Newton, given the correct model for
interactions. The goal of this project is to model an argon gas, where
the atoms interact according to the famous Lennard-Jones potential,

\begin{equation}
    U(r) = 4\varepsilon\qty(\qty(\frac{\sigma}{r})^{12} - \qty(\frac{\sigma}{r})^6), \label{eq:lj}
\end{equation}

where $r$ is the distance between two atoms,
$r=\norm{\bm{r}_i-\bm{r}_j}$. The quantity $\sigma$ and $\varepsilon$
are parameters which determine which chemical compound is
modelled. This potential is a good approximation for noble
gases. Before you begin it is also useful to find the force on atom
$i$ at position $\bm{r}_i$ from atom $j$ at position $\bm{r}_j$.

We will also learn how to do write object oriented code. You can use your code
from project 3 or look at a
a code skeleton that contains the basic structure of an MD code.
You can find the \href{{https://github.com/andeplane/molecular-dynamics-fys3150}}{code skeleton
here}. You
can clone or fork this repository into your own.  The class structure
which you developed for project 3 can also be used again here, as well
as the Velocity Verlet algorithm of project 3.

After you have downloaded the repository, open the project in Qt
Creator and see if it runs. The program should create 100 argon atoms
and place them uniformly inside a cubic box with sides 10
Angstroms. Each atom is given a velocity according to the
Maxwell-Boltzmann distribution so that
\begin{align}
P(v_i)d\mathrm{v}_i = \left(\frac{m}{2\pi k_B
T}\right)^{1/2} \exp\left(-\frac{m v_i^2}{2k_B T}\right)d\mathrm{v}_i,
\end{align}
where $m$ is the mass of the atom, $k_B$ is
Boltzmann's constant and $T$ is the temperature. We recognize this as
a normal distribution with zero mean and standard deviation $\sigma =
\sqrt{k_B T/m}$. We recognize this also as the Boltzmann distribution $P(E) \propto \exp{(-\beta E)}$, with $E = 0.5mv^2$.
The program will evolve the system in time with no
forces so that all atoms move in a straight line. It will create a
file called \emph{movie.xyz} containing all the timesteps ready to
visualize with for example Ovito. You can download Ovito from this \href{{http://www.ovito.org/index.php/download}}{site}. You should start
with the fun part - to look at the simulation in Ovito. We then
strongly recommend that, before you start, you look at the code
structure and try to understand how the different classes are
connected to each other and how a typical timestep is computed. You
can skip understanding the contents of \emph{unitconverter.cpp} and \emph{vec3.cpp}.

\textbf{For this project you can collaborate with fellow students and you can  hand in a common report.}
This project (together with projects 3 and 4) counts 1/3 of the final mark.

\subsection*{Before starting}

If you wish to study the above program, while you develop your own code, it may be useful to
pay attention to the following two steps:
\begin{enumerate}
 \item First, spend some 30 minutes figuring out what molecular dynamics is and which problems it can be applied to. What areas of physics can it be used in? What about chemistry and biology? What you find here should be part of the  introductory section in your report.

 \item Then run the program, visualize the output with for example Ovito (as mentioned above). Now, spend some 30-60 minutes looking at the code and understand how the output is produced and try to understand every step in the code. The best way to do this is to start at the top of the \emph{main()} function and follow every line and every function call, step by step. \textbf{As a part of your report, you should explain the code structure and draw a flow chart (a diagram showing how the program is executed).} This part is extremely important and will save you a lot of time. And of course, please ask us teachers if you have any questions!
\end{enumerate}

\noindent
\paragraph{Project 5a): Getting started.}
When working with MD, the system size is usually limited by available computer resources. A typical large MD simulation (with a parallelized code) contains a few million atoms corresponding to a system much smaller than a cubic micron. In order to get rid of boundary effects, we usually apply periodic boundary conditions so that we simulate a system of infinite size. You need to implement the \emph{applyPeriodicBoundaryConditions} function in the \emph{System} class. After doing so, run the program again and notice how the atoms now are contained inside a box. Discuss the benefits of this strategy.

\paragraph{Project 5b): Velocity distribution.}
The atoms are usually given velocities according to the Maxwell-Boltzmann distribution (you should discuss why this is the case) which will result in a nonzero net momentum in the system. Implement the \emph{removeMomentum} function in the \emph{System} class so that the net momentum is zero.

\paragraph{Project 5c): FCC lattice.}
The atoms are now uniformly distributed in space. This is not very physical, so we should place the atoms in a crystal structure. When we later implement the Lennard-Jones potential, we will see that the face-centered cubic (FCC) lattice is a stable structure for the potential (it is actually the crystalline structure of argon). A lattice is built up by \emph{unit cells} - a group of atoms - so that a larger system can be created by repeating these cells in space. An FCC lattice unit cell of size $b$ in Angstroms (this is the lattice constant) consists of four atoms with local coordinates

\begin{align}
	\mathbf{r}_1 &= 0 \hat{\mathbf{i}} + 0 \hat{\mathbf{j}} + 0 \hat{\mathbf{k}},\\
	\mathbf{r}_2 &= \frac{b}{2} \hat{\mathbf{i}} + \frac{b}{2} \hat{\mathbf{j}} + 0 \hat{\mathbf{k}},\\
	\mathbf{r}_3 &= 0 \hat{\mathbf{i}} + \frac{b}{2} \hat{\mathbf{j}} + \frac{b}{2} \hat{\mathbf{k}},\\
	\mathbf{r}_4 &= \frac{b}{2} \hat{\mathbf{i}} + 0 \hat{\mathbf{j}} + \frac{b}{2} \hat{\mathbf{k}}.
\end{align}
You can now create $N_x \times N_y \times N_z$ such unit cells next to each other to form a larger system so that the origin of unit cell $(i,j,k)$ is

\begin{align}
	\mathbf{R}_{i,j,k} = i \hat{\mathbf{u}}_1 + j \hat{\mathbf{u}}_2 + k \hat{\mathbf{u}}_3,
\end{align}
where $i=0,1,..., N_x-1, j=0,1,..., N_y-1, k=0,1,..., N_z-1$. The unit vectors of the unit cells are scaled with the lattice constant $b$ so that

\begin{align}
	\hat{\mathbf{u}}_1 = b\hat{\mathbf{i}}, \quad \hat{\mathbf{u}}_2 = b\hat{\mathbf{j}}, \quad \hat{\mathbf{u}}_3 = b\hat{\mathbf{k}}.
\end{align}
Implement the function \emph{createFCCLattice(int numberOfUnitCellsEachDimension, double latticeConstant)} in the \emph{System} class. Remember to remove the 100 atoms that are created as the code is right now. Use lattice constant $b=5.26$ $\mathrm{Angstrom}$. Remember to update the system size so the \emph{applyPeriodicBoundaryConditions} function works properly. If you now visualize the result of the program, you should see the nice crystalline structure in the beginning of the simulation. What is the density $\rho$ in your system?

\paragraph{Project 5d): Calculation of forces.}
While this looks nice, we need to implement the computation of forces in order to see interesting physics. In this project, we will use the \href{{http://en.wikipedia.org/wiki/Lennard-Jones_potential}}{Lennard-Jones potential}  which calculates the energy between two atoms $i$ and $j$ as

\begin{align}
	U(r_{ij}) = 4\epsilon\left[\left(\frac{\sigma}{r_{ij}}\right)^{12} - \left(\frac{\sigma}{r_{ij}}\right)^6\right],
\end{align}
where $r_{ij} = \vert\mathbf{r}_i - \mathbf{r}_j\vert$ is the distance from atom $i$ to atom $j$, $\epsilon$ is the depth of the potential well (with dimension energy) and $\sigma$ is the distance at which the potential is zero. For argon, optimal values of the parameters are:

\begin{align}
	\frac{\epsilon}{k_B} = 119.8\mathrm{K}, \sigma=3.405 \mathrm{Angstrom}.
\end{align}
In our code, we use the so called MD units (see the units definition at the end). This potential reproduces thermodynamic equilibrium properties that are in good agreement with experimental values for argon. The equation of state for this system is the van der Waals equation of state. Phases such as solid, liquid and gas are reproduced from this simple model with phase transitions and other properties as well with a  remarkable agreement with data.

The total potential energy $V$ in the system is computed by summing over all pairs of atoms (counting each pair only once)

\begin{align}
	V = \sum_{i>j} U(r_{ij}).
\end{align}
The force is calculated by taking the negative gradient of the potential

\begin{align}
	\mathbf{F}(r_{ij}) = -\nabla U(r_{ij}),
\end{align}
giving the $x$-component (the other components are calculated the same way)

\begin{align}
	F_x(r_{ij}) = -\frac{\partial U}{\partial r_{ij}}\frac{\partial r_{ij}}{\partial x_{ij}},
\end{align}
where $x_{ij}$ is the $x$-component of $\mathbf{r}_{ij}$.

Calculate (analytically) the force and implement the \emph{calculateForces} function in the LennardJones class, using the Velocity Verlet algorithm that you developed for project 3. You will have to take care of the periodic boundary conditions. You could use the minimum image convention as described \href{{http://en.wikipedia.org/wiki/Periodic_boundary_conditions}}{here}. Now run the simulation with 5 unit cells in each dimension (500 atoms) for different initial temperatures. Can you tell if the system is in a solid state just by looking at the system in Ovito? At what initial temperature is the crystal melting?

\paragraph{Project 5e): Kinetic and potential energies.}
We now have a working Molecular dynamics code! The next step is to calculate some physical properties of the system. The easiest properties to measure are the kinetic and potential energy (calculated in the LennardJones class). The kinetic energy is defined as

\begin{align}
	E_k = \sum_{i=1}^{N_\text{atoms}} \frac{1}{2} m_i v_i^2,
\end{align}
where $m_i$ and $v_i$ is the mass and the speed of atom $i$. You can use the function \emph{atom->velocity.lengthSquared()} to calculate the dot product of the velocities.

You can also calculate an estimate of the temperature through the equipartition theorem, see for example D. Schroeder's \emph{An Introduction to Thermal Physics} for details,
\begin{align}
	\langle E_k \rangle = \frac{3}{2}N_\text{atoms} k_B T.
\end{align}
We can use this to define an \emph{instantaneous} temperature

\begin{align}
	T = \frac{2}{3}\frac{E_k}{N_\text{atoms} k_B}.
\end{align}

All of these quantities should be calculated in the \emph{StatisticsSampler} class. All the statistical quantities should be saved to a file which needs to be implemented in the \emph{saveToFile} function in the same class.

As you will see, since the temperature is proportional to the kinetic energy, its initial value will drop in the beginning when the system is initialized in the crystal structure. Why does this happen? What happens to the total energy? In order to have a system with final temperature $T$, what initial temperature $T_i$ is needed? What is the ratio $T/T_i$? Do multiple simulations with different random seeds and plot this ratio as a function of time. Again visualize with for example \emph{Ovito} and try to determine at what temperature the system actually melts at. After the temperature drops, it will reach a more or less stable value. The system is then said to be in an equilibrium state.

\paragraph{Project 5f): Diffusion constant.}
The last exercise is to compute the diffusion constant and use this to measure the melting temperature. We use the Einstein relation that relates the so called mean square displacement (MSD) to the diffusion constant

\begin{align}
	\langle r^2(t) \rangle = 6Dt,
\end{align}
where $D$ is the diffusion constant, $t$ is time. The mean square displacement for atom $i$ is calculated as

\begin{align}
	r_i^2(t) = |\mathbf{r}_i(t) - \mathbf{r}_i(0)|^2,
\end{align}
where $\mathbf{r}_i(t)$ is the position of atom $i$ at time $t$. When you implement this you need to add the initial position as a new property in the \emph{Atom} class. You also need to update the \emph{applyPeriodicBoundaryConditions} so that $r_i^2(t)$ is the actual displacement as if no periodic boundary condition has occured. Why is this important?

We can use the diffusion constant to find the melting temperature. Atoms in a solid will not diffuse much, meaning that a solid will have a diffusion constant close to zero. Now solve the Einstein relation for the diffusion constant $D$ and add a function in \emph{StatisticsSampler} thats measures this quantity. Plot the diffusion constant as a function of temperature $T$ and use this to find an estimate for the melting temperature. Use what you found earlier  to make sure you simulate temperatures both above and below the melting temperature. Also remember not to use the initial temperature, but the measured temperature after the system has reached equilibrium. You can use the builtin \emph{UnitConverter} to convert the temperature to SI units as illustrated in the top of the \emph{main} function.

\section*{Link with FYS2160 - what is really going on here?}
The idea of an MD simulation is to sample microstates from a statistical ensemble. In our simulation, we have a constant number of atoms $N$, a constant volume $V$ and a (more or less, depending on our integrator) constant energy $E$. This corresponds to the microcanonical ensemble (NVE).

Although we produce the dynamics of atoms, we should not see MD as a model that will give the true trajectories of the atoms, but rather a model that allows us to explore the phase space according to the probabilities we can calculate with statistical mechanics. A fundamental assumption in any MD simulation is the ergodic hypothesis. It states that \emph{over long periods of time, the time spent by a system in some region of the phase space of microstates with the same energy is proportional to the volume of this region, i.e., that all accessible microstates are equiprobable over a long period of time}. This means that by evolving the atoms through time will visit microstates of the ensemble according to the true probabilities given by the ensemble.

\section*{Units}

The program uses by default a set of units. We have chosen the following four units

\begin{align}
	\text{1 unit of mass } &= 1 \text{ a.m.u} = 1.661\times 10^{-27}\mathrm{kg},\\
	\text{1 unit of length } &= 1.0 \mathrm{angstrom} = 1.0\times 10^{-10}\mathrm{m},\\
	\text{1 unit of energy } &= 1.651\times 10^{-21}\mathrm{J},\\
	\text{1 unit of temperature} &= 119.735\mathrm{K}.
\end{align}
Boltzmann's constant is then equal to one (convince yourself about this), and other units can be derived by using known relations. We can for example find the unit of time by using $E=mc^2$. We have that

\begin{align}
	\text{Energy} = \text{Mass} \times \frac{\text{Length}^2}{\text{Time}^2},
\end{align}
which can be solved for time

\begin{align}
	\text{Time} = \text{Length} \times \sqrt{\frac{\text{Mass}}{\text{Energy}}}.
\end{align}
By inserting the values above, we get

\begin{align}
	\text{1 unit of time } &= 1.0 \times 10^{-10}\sqrt{\frac{1.661\times 10^{-27}}{1.651\times 10^{-21}}} \text{ s} = 1.00224\times 10^{-13}\mathrm{s}.
\end{align}
This way, we can continue working out the values of the other units. What are the units of force? And pressure? In the project, a class that calculates these values is given.

\subsection*{Introduction to numerical projects}

Here follows a brief recipe and recommendation on how to write a report for each
project.

\begin{itemize}
  \item Give a short description of the nature of the problem and the eventual  numerical methods you have used.

  \item Describe the algorithm you have used and/or developed. Here you may find it convenient to use pseudocoding. In many cases you can describe the algorithm in the program itself.

  \item Include the source code of your program. Comment your program properly.

  \item If possible, try to find analytic solutions, or known limits in order to test your program when developing the code.

  \item Include your results either in figure form or in a table. Remember to        label your results. All tables and figures should have relevant captions        and labels on the axes.

  \item Try to evaluate the reliabilty and numerical stability/precision of your results. If possible, include a qualitative and/or quantitative discussion of the numerical stability, eventual loss of precision etc.

  \item Try to give an interpretation of you results in your answers to  the problems.

  \item Critique: if possible include your comments and reflections about the  exercise, whether you felt you learnt something, ideas for improvements and  other thoughts you've made when solving the exercise. We wish to keep this course at the interactive level and your comments can help us improve it.

  \item Try to establish a practice where you log your work at the  computerlab. You may find such a logbook very handy at later stages in your work, especially when you don't properly remember  what a previous test version  of your program did. Here you could also record  the time spent on solving the exercise, various algorithms you may have tested or other topics which you feel worthy of mentioning.
\end{itemize}

\noindent
\subsection*{Format for electronic delivery of report and programs}

The preferred format for the report is a PDF file. You can also use DOC or postscript formats or as an ipython notebook file.  As programming language we prefer that you choose between C/C++, Fortran2008 or Python. The following prescription should be followed when preparing the report:

\begin{itemize}
  \item Use Devilry to hand in your projects, log in  at  \href{{http://devilry.ifi.uio.no}}{\nolinkurl{http://devilry.ifi.uio.no}} with your normal UiO username and password and choose either 'fys3150' or 'fys4150'. There you can load up the files within the deadline.

  \item Upload \textbf{only} the report file!  For the source code file(s) you have developed please provide us with your link to your github domain.  The report file should include all of your discussions and a list of the codes you have developed.  Do not include library files which are available at the course homepage, unless you have made specific changes to them.

  \item In your git repository, please include a folder which contains selected results. These can be in the form of output from your code for a selected set of runs and input parametxers.

  \item In this and all later projects, you should include tests (for example unit tests) of your code(s).

  \item Comments  from us on your projects, approval or not, corrections to be made  etc can be found under your Devilry domain and are only visible to you and the teachers of the course.
\end{itemize}

\noindent
Finally,
we encourage you to work two and two together. Optimal working groups consist of
2-3 students. For this specific report you can  hand in a common report.

% ------------------- end of main content ---------------

\end{document}


% !Mode:: "TeX:UTF-8"
% !TEX program  = xelatex
\section{Summary}
\subsection{Proof Expression}
Mathematical proof is relatively easy in mathematics writing, but it requires clear and concise expression as well. Therefore, you can express mathematical proof skillfully after the amount of training.

Some examples are often used in proof\cite{Nicholas1998Handbook},
\begin{itemize}
	\item At the beginning of the proof, ``you should strive to keep the reader informed of where you are in the proof and what remains to be done.'' Useful phrases include
		\begin{itemize}
			\item First, we establish that \ldots
			\item Our task is now to \ldots
			\item Our problem reduces to \ldots
			\item It remains to show that \ldots
			\item We are almost ready to invoke \ldots
		\end{itemize}
	\item At the end of the proof, it is often marked by the halmos symbol □. Sometimes the abbreviation QED is used instead.
	\item If you omit part of a proof, ``it is best to indicate the nature and length of the omission'', via phrases such as the following.
		\begin{itemize}
			\item It is easy/simple/straightforward to show that \ldots
			\item Some tedious manipulation yields \ldots
			\item An easy/obvious induction gives \ldots
			\item After two applications of \ldots\ we find \ldots
			\item An argument similar to the ones used in \ldots\ shows that \ldots
		\end{itemize}
\end{itemize}

\subsection{Important Conjunction}
``Join clauses good, like a conjunction should.'' This sentence points out the importance of conjunctions in English writing, thus, we should be cautious when using conjunctions. This subsection explains the different functions of conjunctions through a series of examples, including the following functions,
\begin{itemize}
	\item Combinations:
		\begin{itemize}
			\item and, both, also, as well as, not only \ldots\ but also, apart from, in addition to, moreover, furthermore;
		\end{itemize}
	\item Implications or explanations;
		\begin{itemize}
			\item as, because, since, due to, in view of, owing to, on account of, given, it follows that, consequently, therefore, thus;
		\end{itemize}
	\item Modifications and restrictions;
		\begin{itemize}
			\item alternatively, although, though, but, whereas, by contrast, except, however, on the other hand, nevertheless, despite, in spite of, instead of, rather that.
		\end{itemize}
\end{itemize}

\subsection{Comparison}
\subsubsection{Symbols In the Sentences}
Halmos once said, ``the best notation is no notation''. That is, symbols should be used as little as possible when writing sentences that include mathematical symbols unless you have to.

Please see Section~2.7.1 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} for more examples.

\subsubsection{Definite Article and Indefinite Article}
It is difficult for speakers, who speak the languages that do not have articles, to use articles correctly in English. Therefore, there are two important rules\cite{Nicholas1998Handbook},
\begin{itemize}
	\item Do not use \emph{the} (with plural or uncountable nouns) to talk about things in general;
	\item Do not use singular countable nouns without articles.
\end{itemize}

Please see Section~2.7.2 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作} for more examples.

\subsubsection{Common Misspellings or Confusions}
We often find that many words are spelling similar when learning English words, but we are not sure whether the words are used properly during the writing process. Thus we need software and accurate memory to reduce misspellings and confusion.

Please see Table~2.25 to Table~2.26 in \emph{Mathematical Writing in English}\cite{2013数学之英文写作}

\nocite{2013数学之英文写作,Nicholas1998Handbook}
\bibliography{ref}


\chapter{Preface}
The origin of the name ``Napkin''
comes from the following quote of mine.
\begin{quote}
\small
I'll be eating a quick lunch with some friends of mine
who are still in high school.
They'll ask me what I've been up to the last few weeks,
and I'll tell them that I've been learning category theory.
They'll ask me what category theory is about.
I tell them it's about abstracting things by looking at just the
structure-preserving morphisms between them, rather than the objects themselves.
I'll try to give them the standard example $\catname{Grp}$,
but then I'll realize that they don't know what a homomorphism is.
So then I'll start trying to explain what a homomorphism is,
but then I'll remember that they haven't learned what a group is.
So then I'll start trying to explain what a group is,
but by the time I finish writing the group axioms on my napkin,
they've already forgotten why I was talking about groups in the first place.
And then it's 1PM, people need to go places, and I can't help but think: \\[0.5ex]
\emph{``Man, if I had forty hours instead of forty minutes,
I bet I could actually have explained this all''.}
\end{quote}
This book was initially my attempt at those forty hours,
but has grown considerably since then.

\section*{About this book}
The \emph{Infinitely Large Napkin} is a light
but mostly self-contained introduction to a large
amount of higher math.

I should say at once that this book is not intended
as a replacement for dedicated books or courses;
the amount of depth is not comparable.
On the flip side, the benefit of this ``light'' approach
is that it becomes accessible to a larger audience,
since the goal is merely to give the reader a feeling for
any particular topic rather than to emulate a full semester of lectures.

I initially wrote this book with talented high-school students in mind,
particularly those with math-olympiad type backgrounds.
Some remnants of that cultural bias can still be felt throughout the book,
particularly in assorted challenge problems which are
taken from mathematical competitions.
However, in general I think this would be a good reference
for anyone with some amount of mathematical maturity and curiosity.
Examples include but certainly not limited to:
math undergraduate majors, physics/CS majors,
math PhD students who want to hear a little bit
about fields other than their own,
advanced high schoolers who like math but not math contests,
and unusually intelligent kittens fluent in English.

\section*{Source code}
The project is hosted on GitHub at
\url{https://github.com/vEnhance/napkin}.
Pull requests are quite welcome!
I am also happy to receive suggestions and corrections by email.

\section*{Philosophy behind the Napkin approach}
As far as I can tell, higher math for high-school students
comes in two flavors:
\begin{itemize}
	\ii Someone tells you about the hairy ball theorem in the form
	``you can't comb the hair on a spherical cat''
	then doesn't tell you anything about why it should be true,
	what it means to actually ``comb the hair'',
	or any of the underlying theory,
	leaving you with just some vague notion in your head.

	\ii You take a class and prove every result in full detail,
	and at some point you stop caring about what the professor is saying.
\end{itemize}
Presumably you already know how unsatisfying the first approach is.
So the second approach seems to be the default,
but I really think there should be some sort of middle ground here.

%I was talking to a friend of mine one day who described briefly
%what the Israel IMO training looked like.
%It turns out that rather than actually preparing for the IMO,
%the students would, say, get taught a semester's worth of
%undergraduate algebra in the couple weeks.
%This might seem absurd, but I think if your goal is to just show the students
%what this algebra thing they keeping hearing about is,
%and your students have substantially more mathematical maturity
%relative to their knowledge (e.g.\ are IMO medalists),
%it seems like you really could make a lot of headway.

%For example: often classes like to prove things for completeness.
%I personally find that many proofs don't really teach anything,
%and that it is often better to say
%``you could work this out if you wanted to,
%but it's not worth your time''.
Unlike university, it is \emph{not} the purpose of this book to
train you to solve exercises or write proofs\footnote{Which is
	not to say problem-solving isn't valuable;
	I myself am a math olympiad coach at heart.
	It's just not the point of this book.},
or prepare you for research in the field.
Instead I just want to show you some interesting math.
The things that are presented should be memorable and worth caring about.
For that reason, proofs that would be included for completeness
in any ordinary textbook are often omitted here,
unless there is some idea in the proof which I think is worth seeing.
In particular, I place a strong emphasis over explaining
why a theorem \emph{should} be true rather than writing down its proof.
This is a recurrent theme of this book:
\begin{moral}
	Natural explanations supersede proofs.
\end{moral}

My hope is that after reading any particular chapter in Napkin,
one might get the following out of it:
\begin{itemize}
	\ii Knowing what the precise definitions are of the main characters,

	\ii Being acquainted with the few really major examples,

	\ii Knowing the precise statements of famous theorems,
	and having a sense of why they \emph{should} be true.
\end{itemize}
Understanding ``why'' something is true can have many forms.
This is sometimes accomplished with a complete rigorous proof;
in other cases, it is given by the idea of the proof;
in still other cases, it is just a few key examples
with extensive cheerleading.

Obviously this is nowhere near enough if you want to e.g.\ do research in a field;
but if you are just a curious outsider,
I hope that it's more satisfying than the elevator pitch or Wikipedia articles.
Even if you do want to learn a topic with serious depth,
I hope that it can be a good zoomed-out overview before you really dive in,
because in many senses the choice of material is
``what I wish someone had told me before I started''.

%In terms of learning math, I suspect that
%there is some sense in which reading a textbook
%is like watching a movie.
%The first time you watch it, you spend most of your bandwidth
%getting to know the characters,
%keeping up with what's going on,
%trying to predict where it's headed next.
%But if you like the movie enough and watch it a second
%or third, or $n$th time, you'll pick up a lot of things
%that you didn't notice the first time through
%(e.g.\ all the hints that Z was the main villain all along).

\section*{More pedagogical comments and references}
The preface would become too long if I talked about
some of my pedagogical decisions chapter by chapter,
so \Cref{ch:refs} contains those comments instead.

In particular, I often name specific references,
and then end of that appendix has more references.
So this is a good place to look if you want further reading.

\section*{Historical and personal notes}
I began writing this book in December of 2014,
after having finished my first semester of undergraduate at Harvard.
It became my main focus for about 18 months after that,
as I became immersed in higher math.
I essentially took only math classes,
(gleefully ignoring all my other graduation requirements)
and merged as much of it as I could
(as well as lots of other math I learned on my own time)
into the Napkin.

Towards August of 2016, though, I finally lost steam.
The first public drafts went online then, and I decided to step back.
Having burnt out slightly,
I then took a break from higher math,
and spent the remaining two undergraduate years\footnote{Alternatively:
	`` \dots and spent the next two years forgetting everything
	I had painstakingly learned''.
	Which made me grateful for all the past notes in the Napkin!}
working extensively as a coach for the American math olympiad team,
and trying to spend as much time with my friends as I could
before they graduated and went their own ways.
% It was also at this time I started to get into Korean pop dance,
% which was the first (and only) extracurricular activity
% I did during my entire undergraduate stay.

During those two years, readers sent me many kind words of gratitude,
many reports of errors, and many suggestions for additions.
So in November of 2018,
some weeks into my first semester as a math PhD student,
I decided I should finish what I had started.
Some months later, here is what I have.

\section*{Acknowledgements}
\todo{add more acknowledgments}
I am indebted to countless people for this work.
Here is a partial (surely incomplete) list.

\begin{itemize}
\ii Thanks to all my teachers and professors for teaching me much of the
material covered in these notes,
as well as the authors of all the references I have cited here.
A special call-out to \cite{ref:55a}, \cite{ref:msci},
\cite{ref:manifolds}, \cite{ref:gathmann}, \cite{ref:18-435},
\cite{ref:etingof}, \cite{ref:145a}, \cite{ref:vakil},
\cite{ref:pugh}, \cite{ref:gorin},
which were especially influential.

\ii Thanks also to dozens of friends and strangers
who read through preview copies of my draft,
and pointed out errors and gave other suggestions.
Special mention to Andrej Vukovi\'c and Alexander Chua
for together catching over a thousand errors.
Thanks also to Brian Gu and Tom Tseng for many corrections.
(If you find mistakes or have suggestions yourself,
I would love to hear them!)

\ii I'd also like to express my gratitude for
many, many kind words I received
during the development of this project.
These generous comments led me to keep working on this,
and were largely responsible for my decision in November 2018
to begin updating the Napkin again.
\end{itemize}

Finally, a huge thanks to the math olympiad community,
from which the Napkin (and me) has its roots.
All the enthusiasm, encouragement, and thank-you notes I have received
over the years led me to begin writing this in the first place.
I otherwise would never have the arrogance to dream a project like this
was at all possible.
And of course I would be nowhere near where I am today were it not for the
life-changing journey I took in chasing my dreams to the IMO.
Forever TWN2!


\documentclass{article}

\title{\sc\LARGE CSCA67 Tutorial, Week 11\\
{\Large Nov. 23rd-Nov. 27th, 2015}}
\date{}
\author{\sc Compiled by {\em G. Singh Cadieux}\\[1ex]
\sc Adapted from\\
A. Bretscher, \href{http://www.utsc.utoronto.ca/~bretscher/a67/lectures/induction_ppt.pdf}{\em CSCA67 Week 10 Lecture Notes},\\
\href{http://youtu.be/NuGDkmwEObM?t=58m50s}{\em Lec 3 \textbar MIT 6.042J Mathematics for Computer Science, Fall 2010} \&\\
V. Hadzilacos, \textit{Course notes for {\em Introduction to the Theory of Computation}} (2007).}

\usepackage{fullpage}
\usepackage{amsmath,amssymb}
%\usepackage{color}
%\usepackage{multicol}
\usepackage{tikz}
\usepackage{hyperref}

\usetikzlibrary{patterns}
\usetikzlibrary{backgrounds}
\tikzstyle{background rectangle}=[draw=none]

\setlength{\parindent}{0pt}

\begin{document}
\maketitle

\section{\sc Review of last week's lecture}
\subsection*{\em Proof by induction}
Proof by induction is a proof technique that is used to prove that a predicate $P(n)$ is true for all natural numbers $n$ larger than $b$, where $b$ is a natural number.\\[1ex]
The natural numbers are the ``counting numbers": the nonnegative integers 0, 1, 2, \ldots (note that, depending upon the context, 0 may or may not be included in the natural numbers). The set of natural numbers is denoted $\mathbb{N}$.\\[1ex]
For example, suppose we have the predicate $P(n): n(n^2+5)$ is divisible by 6.\\
We can demonstrate that $P(n)$ is true or false for a specific value of $n$: for instance, if we choose $n=2$, $2(2^2+5)=18$ is divisible by 6, so $P(6)$ is true.\\
But we cannot demonstrate that $P(n)$ is true for all $n\in\mathbb{N}$ - that is, for all natural numbers $n$ - by successively choosing $n$ to be every natural number, since there are infinitely many natural numbers. We can instead use proof by induction to prove that $P(n)$ is true for all $n\in\mathbb{N}$.\\[1em]
\textsc{Proof Structure:}\\[1ex]
Establish a statement $S(n)$ to prove.\\
Generally, $S(n)$ will take the form ``$P(n)$ is true for all natural numbers $n\geq b$," where $P(n)$ is a predicate such as ``$n(n^2+5)$ is divisible by 6."
\begin{enumerate}
\item \textbf{Base case/basis:} Prove that $P(b)$ is true.
\item \textbf{Induction hypothesis:} Suppose that $P(k)$ is true for all natural numbers $k\geq b$.
\item \textbf{Induction step:} Prove that $P(k)$ implies $P(k+1)$ (i.e., prove that $P(k+1)$ is true if $P(k)$ is true).
\end{enumerate}
Conclude that $P(n)$ is true for all natural numbers $n\geq b$, which is to say that $S(n)$ is true.\\[1em]
\textsc{Intuitively}, this makes sense because, if $P(k)\to P(k+1)$, and $P(b)$ is true, then we reach each natural number with a series of implications starting from $b$:
\begin{equation*}
P(b)\to P(b+1)\to P((b+1)+1)\to P((b+2)+1)\to P((b+3)+1)\to\ldots
\end{equation*}

\textsc{Formally}, we can demonstrate that induction proofs are valid using the \textit{well-ordering principle}.\\[1ex]
\textbf{Well-Ordering Principle:} Any non-empty subset $C$ of $\mathbb{N}$ contains a smallest element.\\[1em]
Suppose that we have completed all 3 steps of a proof by induction in order to prove that $P(n)$ is true for all $n\in\mathbb{N}$ greater than $b$.\\[1ex]
Let $C=\{x\,|\,P(x)\text{ is false},\,x\geq b,\, x\in\mathbb{N}\}$ be the set of all values greater than $b$ for which $P(n)$ is false.\\[1ex]
Suppose that our proof is not valid. Then there must be at least one value for which $P(n)$ is false, so $C$ is non-empty. Then by the well-ordering principle, there exists a smallest element $a\in C$.\\[1ex]
Since our base case proves that $P(b)$ is true, $a\neq b$.\\[1ex]
Since $a>b$ and $b$ is a natural number, $a$ must be a natural number and $(a-1)$ must also be a natural number. But $(a-1)<a$ and $a$ is the smallest element in $C$, so $(a-1)$ must not be in $C$ - that is, $P(a-1)$ must be true.\\[1ex]
However, by the induction hypothesis, if $P(a-1)$ is true, then $P((a-1)+1)=P(a)$ must be true as well. Then $a$ is not in $C$.\\[1ex]
This is a contradiction, demonstrating that our assumption that $C$ is nonempty must be false. Since $C$ \textit{is} empty, there are no values for which $P(n)$ is false if the base case and induction hypothesis hold.\\[1em]
\textsc{Note} that a common misunderstanding of proof by induction is to think that, by using the induction hypothesis, our proof assumes that $P(n)$ is true for all natural numbers. This is \textit{not} the case.\\[1ex]
Rather, we use the induction hypothesis in the induction step to prove that, for any natural number $k$, \textit{if} $P(k)$ is true, then $P(k+1)$ is true as well. Recall that we can prove an implication $p\to q$ true if we prove that, when $p$ is true, $q$ is also true. We are not assuming that $P(n)$ is absolutely true; we are assuming that $P(n)$ is true only to prove that the implication $P(n)\to P(n+1)$ is true.\\[1em]
Q: \textsc{Prove that $P(n): 0+1+2+\ldots+(n-1)+n=\dfrac{n(n+1)}{2}$ is true for all $n\in\mathbb{N}$.}\\[1ex]
Let $S(n)$ be the statement that $P(n)$ holds for all $n\in\mathbb{N}$.\\[1ex]
\textbf{Basis:} $n=0$\\[1ex]
$\dfrac{(0)(0+1)}{2}=0$\\[1ex]
$\Rightarrow P(0)$ holds.\\[1ex]
\textbf{Induction hypothesis:} Suppose that $P(k)$ holds for $k\geq 0$.\\[1ex]
\textbf{Induction step:} $n=k+1$
\begin{align*}
0+1+2+\ldots+k+(k+1)& =(0+1+2+\ldots+k)+(k+1)\\
& =\dfrac{k(k+1)}{2}+(k+1)& \text{[by the induction hypothesis]}\\
& =\dfrac{k(k+1)}{2}+\dfrac{2(k+1)}{2}\\
& =\dfrac{(k+2)(k+1)}{2}\\
& =\dfrac{(k+1)((k+1)+1)}{2}
\end{align*}
$\Rightarrow P(k+1)$ holds if $P(k)$ holds, for $k\geq 0$.\\[1ex]
$\boxed{\Rightarrow S(n)\text{ is true.}}$

\section{\sc Proof by simple induction}

\subsection*{\href{http://www.youtube.com/watch?v=IFqna5F0kW8}{Q: {\em Prove that $4+9+14+19+\ldots+(5n-1)=\dfrac{n}{2}(3+5n)$ for all natural numbers $n$.}}}

Let $P(n)$ be the predicate ``$4+9+14+19+\ldots+(5n-1)=\tfrac{n}{2}(3+5n)$".\\
Let $S(n)$ be the statement that $P(n)$ holds for all $n\in\mathbb{N}$ (note that, here, $0\notin\mathbb{N}$).\\[1ex]
\textbf{Basis:} $n=1$\\[1ex]
$5(1)-1=4$\\[1ex]
$\dfrac{1}{2}(3+5(1))=\dfrac{1}{2}(8)=4$\\[1ex]
$5(1)-1=\dfrac{1}{2}(3+5(1))\Rightarrow P(1)$ holds.\\[1ex]
\textbf{Induction hypothesis:} Suppose that $P(k)$ holds for $k\geq 1$.\\[1ex]
\textbf{Induction step:} $n=k+1$
\begin{align*}
4+9+14+19+\ldots+(5(k)-1)+(5(k+1)-1)& =\dfrac{k}{2}(3+5(k))+(5(k+1)-1)& \text{[by I.H.]}\\
& =\dfrac{5}{2}k^2+\dfrac{13}{2}k+4& \text{[expanded and simplified]}\\
\dfrac{(k+1)}{2}(3+5(k+1))& =\left(\dfrac{k}{2}+\dfrac{1}{2}\right)(3+5k+5)\\
& =(\dfrac{5}{2}k^2+4k)+(\dfrac{5}{2}+4)& \text{[expanded and simplified]}\\
4+9+14+19+\ldots+(5(k)-1)+(5(k+1)-1)& =\dfrac{(k+1)}{2}(3+5(k+1))
\end{align*}
$\Rightarrow P(k+1)$ holds if $P(k)$ holds, for $k\geq 1$.\\[1ex]
$\boxed{\Rightarrow S(n)\text{ is true.}}$

\subsection*{\href{http://www.youtube.com/watch?v=wBvsIyQN4o8}{Q: {\em Prove that $-1+2+5+8+\ldots+(3n-4)=\dfrac{n}{2}(3-5n)$ for all natural numbers $n$.}}}

Let $P(n)$ be the predicate ``$-1+2+5+8+\ldots+(3n-4)=\tfrac{n}{2}(3-5n)$".\\
Let $S(n)$ be the statement that $P(n)$ holds for all $n\in\mathbb{N}$ (note that, here, $0\notin\mathbb{N}$).\\[1ex]
\textbf{Basis:} $n=1$\\[1ex]
$3(1)-4=-1$\\[1ex]
$\dfrac{1}{2}(3-5(1))=\dfrac{1}{2}(-2)=-1$\\[1ex]
$3(1)-4=\dfrac{1}{2}(3-5(1))\Rightarrow P(1)$ holds.\\[1ex]
\textbf{Induction hypothesis:} Suppose that $P(k)$ holds for $k\geq 1$.\\[1ex]
\textbf{Induction step:} $n=k+1$
\begin{align*}
-1+2+5+8+\ldots+(3(k)+4)+(3(k+1)-4)& =\dfrac{k}{2}(3-5(k))+(3(k+1)-4)& \text{[by I.H.]}\\
& =\dfrac{3}{2}k^2+\dfrac{1}{2}k-1& \text{[expanded and simplified]}\\
\dfrac{(k+1)}{2}(3+5(k+1))& =\left(\dfrac{k}{2}+\dfrac{1}{2}\right)(3+5k+5)\\
& =\dfrac{3}{2}k^2+\dfrac{1}{2}k-1& \text{[expanded and simplified]}\\
-1+2+5+8+\ldots+(3(k)+4)+(3(k+1)-4)& =\dfrac{(k+1)}{2}(3+5(k+1))
\end{align*}
$\Rightarrow P(k+1)$ holds if $P(k)$ holds, for $k\geq 1$.\\[1ex]
$\boxed{\Rightarrow S(n)\text{ is true.}}$

\subsection*{\href{http://www.youtube.com/watch?v=uHfwNKWyD20}{Q: {\em Prove that $\dfrac{1}{2}+\dfrac{1}{4}+\dfrac{1}{8}+\ldots+\dfrac{1}{2^n}=\dfrac{2^n-1}{2^n}$ for all natural numbers $n$.}}}

Let $P(n)$ be the predicate ``$\tfrac{1}{2}+\tfrac{1}{4}+\tfrac{1}{8}+\ldots+\tfrac{1}{2^n}=\tfrac{2^n-1}{2^n}$".\\
Let $S(n)$ be the statement that $P(n)$ holds for all $n\in\mathbb{N}$ (note that, here, $0\notin\mathbb{N}$).\\[1ex]
\textbf{Basis:} $n=1$\\[1ex]
$\dfrac{1}{2^{(1)}}=\dfrac{1}{2}$\\[1ex]
$\dfrac{2^{(1)}-1}{2^{(1)}}=\dfrac{1}{2}$\\[1ex]
$\dfrac{1}{2^{(1)}}=\dfrac{2^{(1)}-1}{2^{(1)}}\Rightarrow P(1)$ holds.\\[1ex]
\textbf{Induction hypothesis:} Suppose that $P(k)$ holds for $k\geq 1$.\\[1ex]
\textbf{Induction step:} $n=k+1$
\begin{align*}
\dfrac{1}{2}+\dfrac{1}{4}+\dfrac{1}{8}+\ldots+\dfrac{1}{2^{(k)}}+\dfrac{1}{2^{(k+1)}}& =\dfrac{2^k-1}{2^k}+\dfrac{1}{2^{(k+1)}}& \text{[by I.H.]}\\
& =\dfrac{2(2^k-1)}{2^{(k+1)}}+\dfrac{1}{2^{(k+1)}}\\
& =\dfrac{2^{(k+1)}-2}{2^{(k+1)}}+\dfrac{1}{2^{(k+1)}}\\
& =\dfrac{2^{(k+1)}-1}{2^{(k+1)}}
\end{align*}
$\Rightarrow P(k+1)$ holds if $P(k)$ holds, for $k\geq 1$.\\[1ex]
$\boxed{\Rightarrow S(n)\text{ is true.}}$

\section{\sc Proof by complete induction}
So far, we have been using a type of proof by induction known as ``simple" induction. Another type of proof by induction is \textit{complete} (or \textit{strong}) induction.\\[1ex]
Like simple induction, complete induction is used to prove that a predicate $P(n)$ is true for all natural numbers $n$ larger than $b$, where $b$ is a natural number.\\[1ex]
The difference betwen the two techniques is in the induction hypothesis: rather than assuming only that $P(k)$ is true, we assume that $P(b),\,P(b+1),\,P(b+2),\,\ldots,\,P(k)$ are all true (a ``stronger" induction hypothesis, hence the name of the technique).\\[1ex]
It is easier to use complete induction than simple induction for certain problems, since there are more assumptions that we can use.\\[1em]
\textsc{Proof Structure:}\\[1ex]
Establish a statement $S(n)$ to prove.\\
Generally, $S(n)$ will take the form ``$P(n)$ is true for all natural numbers $n\geq b$," where $P(n)$ is a predicate such as ``$n(n^2+5)$ is divisible by 6."
\begin{enumerate}
\item \textbf{Base case/basis:} Prove that $P(b)$ is true.
\item \textbf{Induction hypothesis:} Suppose that $P(b+1),\,P(b+2),\,\ldots,\,P(k)$ are true for all natural numbers $k\geq b$.
\item \textbf{Induction step:} Prove that $P(b)\wedge P(b+1)\wedge P(b+2)\wedge\ldots\wedge P(k)$ implies $P(k+1)$ (i.e., prove that $P(k+1)$ is true if $P(b),\,P(b+1),\,P(b+2),\,\ldots,\,P(k)$ are true).
\end{enumerate}
Conclude that $P(n)$ is true for all natural numbers $n\geq b$, which is to say that $S(n)$ is true.

\subsection*{\em The Unstacking Game}
Consider a game in which each player starts off with a stack of objects.

\begin{itemize}
\item At his/her turn, each player splits (one of) his/her stack(s) into 2 smaller stacks.
\item The number of points for each turn is the product of the sizes of the 2 stacks.
\item The players continue taking turns and splitting their stacks until all the stacks contain only 1 object each.
\item Each player's total score is the sum of his/her scores for all the turns. The player with the largest total score is the winner.
\end{itemize}
For example, starting with 5 objects, one possible strategy is:\\
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0,1) rectangle (0.75,1.5);
\draw (0,1.5) rectangle (0.75,2);
\draw (0,2) rectangle (0.75,2.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (0.85,0.5) rectangle (1.6,1);
\draw (0.85,1) rectangle (1.6,1.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (0.85,0.5) rectangle (1.6,1);
\draw (1.7,0) rectangle (2.45,0.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (-0.85,0) rectangle (-0.1,0.5);
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (0.85,0.5) rectangle (1.6,1);
\draw (1.7,0) rectangle (2.45,0.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (1.7,0) rectangle (2.45,0.5);
\draw (2.55,0) rectangle (3.3,0.5);
\draw (3.4,0) rectangle (4.15,0.5);
\end{tikzpicture}\\
This player will have $(2\times 3)+(2\times 1)+(1\times 1)+(1\times 1)=10$ points in total.\\[1em]
When we play the unstacking game, however, we notice that all strategies appear to result in the same score. For example, here is another strategy when starting with a stack of 5 objects:\\
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0,1) rectangle (0.75,1.5);
\draw (0,1.5) rectangle (0.75,2);
\draw (0,2) rectangle (0.75,2.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (0.85,0.5) rectangle (1.6,1);
\draw (0.85,1) rectangle (1.6,1.5);
\draw (0.85,1.5) rectangle (1.6,2);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (0.85,0.5) rectangle (1.6,1);
\draw (1.7,0) rectangle (2.45,0.5);
\draw (1.7,0.5) rectangle (2.45,1);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (-0.85,0) rectangle (-0.1,0.5);
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (0.85,0.5) rectangle (1.6,1);
\draw (1.7,0) rectangle (2.45,0.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (1.7,0) rectangle (2.45,0.5);
\draw (2.55,0) rectangle (3.3,0.5);
\draw (3.4,0) rectangle (4.15,0.5);
\end{tikzpicture}\\
This again produces a total score of $(1\times 4)+(1\times 3)+(2\times 1)+(1\times 1)=10$ points.\\[1em]
\textsc{We can theorize} that, for an inital stack of $n$ objects, any strategy will produce the same score $S(n)$. And since we believe that all strategies produce the same score, let us choose a strategy and calculate the resulting score $S(n)$, which should result from any other strategy as well.\\[1ex]
We know (and have demonstrated above) that the sum of all natural numbers up to $n$ is $\tfrac{n(n+1)}{2}$. If we choose the strategy of splitting each stack of blocks into a stack of 1 block and a stack of all the remaining blocks, then our total score will be $((n-1)\times 1)+(((n-1)-1)\times 1)+\ldots+(1\times 1)=(n-1)+(n-2)+\ldots+1$.\\
For example, when starting with 5 objects, our strategy proceeds as follows:\\
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0,1) rectangle (0.75,1.5);
\draw (0,1.5) rectangle (0.75,2);
\draw (0,2) rectangle (0.75,2.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0,1) rectangle (0.75,1.5);
\draw (0,1.5) rectangle (0.75,2);
\draw (0.85,0) rectangle (1.6,0.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0,1) rectangle (0.75,1.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (1.7,0) rectangle (2.45,0.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0,0.5) rectangle (0.75,1);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (1.7,0) rectangle (2.45,0.5);
\draw (2.55,0) rectangle (3.3,0.5);
\end{tikzpicture}
\begin{tikzpicture}[framed, inner frame sep=1ex]
\draw (0,0) rectangle (0.75,0.5);
\draw (0.85,0) rectangle (1.6,0.5);
\draw (1.7,0) rectangle (2.45,0.5);
\draw (2.55,0) rectangle (3.3,0.5);
\draw (3.4,0) rectangle (4.15,0.5);
\end{tikzpicture}\\
This gives us a score of $(4\times 1)+(3\times 1)+(2\times 1)+(1\times 1)=4+3+2+1$.\\[1ex]
In general, our total score from this strategy will be the sum of all natural numbers up to $(n-1)$, which we know to be $\tfrac{(n-1)((n-1)+1)}{2}=\tfrac{n(n-1)}{2}$.\\[1em]
\textsc{So now} let us use complete induction to prove\\
$Q(n):$ all strategies for the unstacking game produce the same score $S(n)=\dfrac{n(n-1)}{2}$ for all $n\in\mathbb{N}$, where $n$ is the number of objects in the initial stack.\\[1em]
\textbf{Basis:} $n=1$\\[1ex]
$S(1)=\dfrac{(1)((1)-1)}{2}=0$\\[1ex]
Since there are no moves that can be made with a stack of 1 object, all strategies will produce a score of 0. $\Rightarrow$ With $n=1$, all strategies produce the same score $S(1)$.\\[1ex]
\textbf{Induction hypothesis:} Suppose that, with $n=2$, all strategies produce the same score $S(2)$; with $n=3$, all strategies produce the same score $S(3)$; etc. Finally, suppose that, with $n=k\geq 1$, all strategies produce the same score $S(k)$.\\[1ex]
\textbf{Induction step:} $n=k+1$\\[1ex]
For our first turn, we divide the stack of $(k+1)$ objects into a stack of $m$ objects and a stack of $(k+1-m)$ objects (the value of $m$ depends upon our strategy).\\[1ex]
Based on our induction hypothesis, with an initial stack of $m\leq k$ objects, all strategies will produce a score of $S(m)=\tfrac{m(m-1)}{2}$; and with an initial stack of $(k+1-m)$ objects, all strategies will produce a score of $S(k+1-m)=\tfrac{(k+1-m)((k+1-m)-1)}{2}$.\\[1ex]
\textsc{Notice} that these assumptions cannot be used in simple induction.\\[1ex]
Then the total score for the game is the sum of the score for our first turn, and the total score for unstacking $m$ objects, and the total score for unstacking $(k+1-m)$ objects:
\begin{align*}
m(k+1-m)+S(m)+S(k+1-m)& =m(k+1-m)+\dfrac{m(m-1)}{2}+\dfrac{(k+1-m)((k+1-m)-1)}{2}\\
& =(km+m-m^2)+\dfrac{m^2-m}{2}+\dfrac{(k+1-m)(k+m)}{2}\\
& =\dfrac{1}{2}k^2+\dfrac{1}{2}k\qquad\text{[expanded and simplified]}
\end{align*}
Notice that this total score depends only on $k$ and not $m$ - that is, the total score depends only on the inital number of objects and not on our strategy, as we expected.
\begin{align*}
\dfrac{(k+1)((k+1)-1)}{2}& =\dfrac{k(k+1)}{2}\\
& =\dfrac{1}{2}k^2+\dfrac{1}{2}k\qquad\text{[expanded]}\\
m(k+1-m)+S(m)+S(k+1-m)& =\dfrac{(k+1)((k+1)-1)}{2}
\end{align*}
$\Rightarrow$ If all strategies produce the same score $S(2)$ with $n=2$, all strategies produce the same score $S(3)$ with $n=3$, etc., and all strategies produce the same score $S(k)$ with $n=k\geq 1$, then all strategies produce the same score $S(k+1)=\tfrac{(k+1)((k+1)-1)}{2}$ with $n=k+1$.\\[1ex]
$\boxed{\Rightarrow Q(n)\text{ is true.}}$

\section{\sc Additional practice problems}

{\bf Q: Using simple induction, prove that $1+2+4+\ldots+2^n=2^{n+1}-1$ for all natural numbers $n$.}\\[1em]
{\bf Q: Using simple induction, prove that $2^n\geq n^2$ for all natural numbers $n$.}\\[1em]
{\bf Q: Using simple induction, prove that $\sum\limits_{i=0}^n m^i=\dfrac{m^{n+1}-1}{m-1}$ for all integers $m\geq 2,\,n\geq 1$.}\\[1em]
Define the sequence $a_0,\,a_1,\,\ldots$ as\\
$a_i=\begin{cases}
2&\text{if }0\leq i\leq 2\\
a_{i-1}+a_{i-2}+a_{i-3}&\text{if }i>2
\end{cases}$\\[1ex]
{\bf Q: Using complete induction, prove that $a_n<2^n$ for every integer $n\geq 2$.}\\[1em]
{\bf Q: Find the flaw in the following proof by induction (a well-known proof by George P\'{o}lya).}\\[1ex]
$P(n)$: $n$ horses are of the same colour\\[1ex]
$S(n)$: $\forall n \in N$, $P(n)$ holds.\\[1ex]
\underline{Basis:} $n=1$\\[1ex]
$P(1)$ clearly holds as a single horse is the same colour as itself.\\[1ex]
\underline{Induction hypothesis:} Suppose that, within any set of $k$ horses, $k\geq 1$, all horses are of the same colour.\\[1ex]
\underline{Induction step:} $n=k+1$\\[1ex]
Consider a set of $k+1$ horses and number them $\{1,\,2,\,\ldots,\,k+1\}$ so that each horse is distinct. This set contains (among others) the overlapping subsets $\{1,\,2,\,\ldots,\,k\}$ and $\{2,\,3,\,\ldots,\,k+1\}$, each of which has $k$ horses.\\[1ex]
By the inductive hypothesis, the horses in the first subset are all of the same colour, and the horses in the second subset are all of the same colour. Both subsets contain the horses $\{2,\,3,\,\ldots,\,k\}$, meaning that these horses must all be of the same colour, and the horses not contained in both subsets (horse 1 in the first and horse $k+1$ in the second) must also be of the same colour as the rest. Thus, all $k+1$ horses are of the same colour.\\[1ex]
$\Rightarrow P(k+1)$ holds if $P(k)$ holds, for $k\geq 1$.\\[1ex]
$\boxed{\Rightarrow S(n)\text{ is true: all horses are of the same colour!}}$\\[1em]

{\bf Q: Find the flaw in the following proof by induction.}\\[1ex]
$P(n)$: $n=1$.\\[1ex]
$S(n)$: For all natural numbers $n\geq 1$, $P(n)$ holds.\\[1ex]
\underline{Basis:} $n=1$\\[1ex]
Trivial. $\Rightarrow P(1)$ holds.\\[1ex]
\underline{Induction hypothesis:} Suppose that $P(k)$ holds for $k\geq 1$ - that is, suppose that, for any $k\geq 1$, $k=1$.\\[1ex]
\underline{Induction step:} $n=k+1$\\[1ex]
Consider the (as yet unproven) statement $k+1=1$.
\begin{align*}
k+1& =1\\
(k+1)(k-1)& =1(k-1)\\
k^2-1& =k-1\\
k^2& =k\\
1^2& =1\qquad\text{[by I.H.]}
\end{align*}
Since this final statement is true, and is equivalent to the initial statement $k+1=1$, $k+1=1$ must be true as well.\\[1ex]
$\Rightarrow P(k+1)$ holds if $P(k)$ holds, for $k\geq 1$.\\[1ex]
$\boxed{\Rightarrow S(n)\text{ is true.}}$

\end{document}


\lab{F2PY}{F2PY}
\label{lab:f2py}
\objective{Learn how to use F2PY, especially for wrapping Fortran code involving arrays.}

Here we will explore another simple way to access external code from Python.
Various utilities exist to do this, and many are specialized for specific tasks.
F2PY is a utility that can be used to automatically wrap Fortran subroutines so that they are callable from Python.
It is included with NumPy and in most cases does not usually require heavy configuration for use.
The primary requirement for using F2PY is a working Fortran compiler.
If you have access to gfortran, the Fortran equivalent of gcc, it should work well.

\section*{Examples}

F2PY is an interface generator designed to be used primarily to generate Python bindings for array based functions and subroutines from Fortran.
Many simple Fortran files can be compiled \emph{automatically} without any hand-written interfaces between languages.
Windows 64 bit users, if you encounter errors, see the Windows 64 bit install guide.
Here we will wrap a Fortran function that performs a basic tridiagonal solve.
This function is essentially the same as the one wrapped in Lab \ref{lab:cythonwrap}.
In Lab \ref{lab:cythonwrap} the Fortran function had been modified so that it was already callable from C.
F2PY takes care of necessary name and type changes so that pure Fotran functions and subroutines can be wrapped without modification.
We will wrapp the following unmodified version of the algorithm for a tridiagonal solve.
\lstinputlisting[style=fromfile, language=Fortran, name=ftridiag.f90]{./ftridiag/ftridiag.f90}
This subroutine can be wrapped by running the following in the terminal:
\begin{lstlisting}[style=ShellInput]
f2py -c ftridiag.f90 -m ftridiag
\end{lstlisting}
Depending on the way your system is configured, the following command may be needed
\begin{lstlisting}[style=ShellInput]
python f2py.py -c ftridiag.f90 -m ftridiag
\end{lstlisting}
Additional optimization flags can be passed to the compiler using the \li{--opt} flag, as in:
\begin{lstlisting}[style=ShellInput]
f2py -c ftridiag.f90 -m ftridiag --opt="-O3 -march=native"
\end{lstlisting}

Running these commands will create a file called \li{ftridiag.pyd} on a Windows computer or \li{ftridiag.so} on a Linux or Mac based computer.
This file is a Python module that is linked against the compiled Fortran code.
All the details about array type, shape, size, etc. are infered by F2PY.
This wrapper can be tested using a test similar to the one used in Lab \ref{lab:cythonwrap}
\lstinputlisting[style=fromfile, name=ftridiag_test.py]{./ftridiag/test.py}
\begin{info}
This example shows how to compile a single subroutine.
If the subroutine were a part of a Fortran module, F2PY would have created a submodule for the compile module.
For example, if the subroutine were contained in the module \li{tridiag}, after compilation, it would be available in the Python submodule \li{ftridiag.tridiag} as \li{ftridiag.tridiag.ftridiag}.
\end{info}

\begin{info}
In many cases F2PY can automatically infer the correct values for arguments that are used to convey information about array shape.
These arguments are usually included as optional arguments in the calling signature for the Python function.
F2PY also verifies that the shapes of the arrays passed to each subroutine match the shapes of arrays shown in the declaration of the interface for the subroutine.
\end{info}

\begin{warn}
When working with arrays that have more than one dimension, F2PY expects arrays that are Fortran ordered, i.e. arrays that have their columns stored in contiguous blocks.
By default it will raise an error if it receives a C-contiguous array, though it can be configured to silently make a copy instead.
A Fortran ordered array can be made by taking the transpose of a C contiguous array.
This becomes very important when working with arrays with two or more dimensions.
\end{warn}

Again, similar to working with Cython, we can also create a \li{setup.py} file to create our Python extension module.
The following \li{setup.py} file will do the same thing as the terminal command used above.
\lstinputlisting[style=fromfile, name=ftridiag_setup.py]{./ftridiag/setup.py}

\begin{problem}
Included with this lab is a pure-fortran version of the \li{fssor} subroutine that you wrapped in Lab \ref{lab:cythonwrap}.
Wrap it again using f2py.
Write a \li{setup.py} file as shown above.
When wrapping automatically in this way, F2PY was able to infer that \li{m} and \li{n} are the dimensions of the array.
On the other hand, we have not defined default vaues for the tolerance and the maximum number of iterations, so those must still be passed in as arguments.
\end{problem}

\section*{Detailed Compilation Steps}
Though F2PY can be used automatically as shown, it can be used to generate more carefully customized wrappers for Fortran functions and subroutines.
When compiling an extension, F2PY performs two basic steps that are accessible to users:
\begin{itemize}
\item Create a \li{.pyf} signature file defining the interface for the Fortran subroutines
\item Compile the Fortran code and use Python's C API to make an extension module that calls it in the way defined in the signature file.
\end{itemize}
If we want a more specific interface for the Fortran code we are wrapping we can generate the signature file, then modify it so that F2PY will make a Python function with the desired calling signature.

Consider the \li{ftridiag} subroutine used above.
F2PY can be used to generate a signature file based on the contents of the file \li{ftridiag.f90} via the command
\begin{lstlisting}[style=ShellInput]
f2py ftridiag.f90 -m ftridiag -h ftridiag.pyf
\end{lstlisting}
The signature file created by F2PY is
\lstinputlisting[style=fromfile, language=Fortran, name=ftridiag.pyf]{./ftridiag/ftridiag.pyf}
This looks almost like a function signature in Fortran, except that F2PY has accounted for the fact that \li{n} is one more than the dimension of the first array.

Notice that F2PY has automatically recognized that the last argument to the Fortran subroutine can be inferred directly from the length of the arrays given as arguments.
When it detects that an argument can be inferred from array shapes, it includes it as an optional argument in the Python binding.
F2PY also automatically checks that the dimensions of the arrays passed are consistent with the dimensions listed in the subroutine declaration.

A F2PY signature file can, in turn, be used to create a Python extension module that wraps the Fortran subroutines as described in the signature file.
The following command will use the signature file and Fortran source file above to create a Python extension module.
\begin{lstlisting}[style=ShellInput]
f2py -c ftridiag.pyf ftridiag.f90
\end{lstlisting}

Signature files can also be used in \li{setup.py} scripts like this:
\lstinputlisting[style=fromfile, language=Python, name=setup.py]{./ftridiag/setup_with_sig.py}

F2PY's signature files are more useful when working with arrays with more than one dimension.
Consider the following Fortran subroutine that uses a simple iterative method to find an approximate solution to Laplace's equation on a square.
\lstinputlisting[style=fromfile, language=Fortran, name=flaplace.f90]{./flaplace/flaplace.f90}

% Wrap a C function using f2py to teach about signature files.

% Wrap a LAPACK function (dgtsv?), have them do it too.

\begin{warn}
The LAPACK routines available in \li{scipy.linalg.lapack} do not raise errors when passed a C contiguous array.
Instead, the array is copied into Fortran contiguous form before it is passed to the Fortran subroutine.
This means that code is less likely to raise errors, but the implicit copying of arrays may slow a program down unnecessarily.
\end{warn}

% Discuss how memory layout affects performance.

% Discuss programmer time value.


\documentclass[12pt]{article}

\include{preamble}

\title{Math 368 / 621 Fall 2019 \\ Midterm Examination Two}
\author{Professor Adam Kapelner}

\date{November 13, 2019}

\begin{document}
\maketitle

\noindent Full Name \line(1,0){220} Circle Section and Class: A~B~C ~~ 368~621

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using an unauthorized cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent I acknowledge and agree to uphold this Code of Academic Integrity. \\

\begin{center}
\line(1,0){250} ~~~ \line(1,0){100}\\
~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
\end{center}

\normalsize

\vspace{-1cm}
\section*{Instructions}

This exam is seventy five minutes and closed-book. You are allowed one page (front and back) of a \qu{cheat sheet.} You may use a graphing calculator of your choice. Please read the questions carefully. If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in choose, permutation, exponent, factorial or any other notation which could be resolved to a number with a computer. Questions marked \qu{[MA]} are required for those enrolled in 621 and extra credit for those enrolled in 368. If you are enrolled in 368, I advise you to finish the other questions on the exam and only then attempt the extra credit. Questions marked \qu{[Extra Credit]} are extra credit for both 368 and 621 students. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\problem Below are some theoretical exercises.

\benum

\subquestionwithpoints{6} Let $X \sim \text{Weibull}(k, \lambda)$ and $c$ is a positive constant. Circle the statement(s) that are true.

\begin{enumerate}
\item Let $k = 0.53$. $\prob{X > c} < \cprob{X > 2c}{X > c}$
\item Let $k = 0.53$. $\prob{X > c} > \cprob{X > 2c}{X > c}$
\item Let $k = 1$. $\prob{X > c} < \cprob{X > 2c}{X > c}$
\item Let $k = 1$. $\prob{X > c} > \cprob{X > 2c}{X > c}$
\end{enumerate}

\subquestionwithpoints{6} Let $X \sim \text{ParetoI}(1, \lambda)$ Let $f_X(x)$ denote its density and $F_X(x)$ denote its CDF, both valid for all $x \in \reals$. Set $\lambda$ so that the 80-20 Pareto Principle holds. Circle the statement(s) that are true.

\begin{enumerate}
\item $Q[X, 0.8] = Q[X, 0.2]$
\item $Q[X, 0.8] = f_X(0.8)$
\item $Q[X, 0.8] = F_X(0.8)$
\item $ \frac{\displaystyle\int_{-\infty}^{F^{-1}_X(0.8)} x f_X(x)}{\displaystyle\expe{X}} = 0.2$
\item $\lambda = 0.8$
\end{enumerate}

\subquestionwithpoints{10} Let $X_1, X_2, \ldots \iid \exponential{\lambda}$ and $N \sim \poisson{\lambda}$. Each statement below is either true or false. Circle the statement(s) that are true for all $k \in \naturals$.

\begin{enumerate}
\item $N = X_1 + X_2 + \ldots + X_k$
\item $X_1 + X_2 + \ldots + X_k \sim \text{Gamma}(k, \lambda)$
\item $\prob{X_1 + X_2 + \ldots + X_k < 1} = Q(k, \lambda)$
\item $\prob{X_1 + X_2 + \ldots + X_k < 1} = P(k, \lambda)$
\item $\prob{N < 1} = Q(k, \lambda)$
\item $\prob{N < 1} = P(k, \lambda)$
\item $\prob{N < k} = Q(k, \lambda)$
\item $\prob{N < k} = P(k, \lambda)$
\item $\prob{X_1 + X_2 + \ldots + X_k > 1} = \prob{N < k}$
\end{enumerate}

\subquestionwithpoints{6} Consider a continuous r.v. $X$ with known CDF $F_X$ Let $Y = -X$ and find the CDF of $Y$ in terms of $F_X$.  \spc{1}

\subquestionwithpoints{8} Let $X_1, X_2 \iid \text{Logistic}\parens{0, 1}$. Provide an expression for the $\prob{X_1 + X_2 > a}$ where $a \in \reals$ is a constant. Your answer must consist of just numbers, symbols and no brand name notation e.g. $\int_\reals \oneover{1 + x^2} dx$ but not  $\int_{\support{X_1}} \text{Logistic}\parens{0, 1} dx$. Do NOT simplify! \spc{5}

%\subquestionwithpoints{6} Assume $\Gamma(\half) = \sqrt{\pi}$ and show that $\Gamma(\frac{5}{2}) = \frac{3}{4}\sqrt{\pi}$. \spc{1}

\subquestionwithpoints{6} Let $X \sim \poisson{\lambda}$ and let $Y = g(X) = \sqrt{X}$. Find its PMF $p_Y(y)$ and make sure $p_Y(y)$ is valid $\forall y \in \reals$. Do NOT simplify. \spc{4}

\subquestionwithpoints{4} Let $X \sim \text{BetaPrime}(\alpha, \beta) := \oneover{B(\alpha, \beta)} \frac{x^{\alpha - 1}}{(x + 1)^{\alpha + \beta}} \indic{x \geq 0}$. Decompose the PDF of $X$ into $c \cdot k(x)$ where $k(x)$ is the kernel and $c$ is the normalization constant. \spc{1}
%
%\beqn
%c &=& \\
%&& \\
%&& \\
%k(x) &=& \\
%&&\\
%\eeqn

\subquestionwithpoints{6} Let $X \sim \text{BetaPrime}(\alpha, \beta)$. Prove that $X^{-1} \sim \text{BetaPrime}(\beta, \alpha)$. \spc{10}

%\subquestionwithpoints{6} Let $X \sim \text{Gamma}\parens{\alpha, \beta}$ and let $Y = g(X) = X^{-1}$. Find its PDF $f_Y(y)$ and make sure $f_Y(y)$ is valid $\forall y \in \reals$. \spc{10}

\subquestionwithpoints{8} Consider dependent continuous r.v.'s $X_1, X_2$ with known JDF $f_{X_1, X_2}(x_1, x_2)$ valid for all $x_1, x_2 \in \reals$. Consider the transformation of these r.v.'s of $H = \frac{X_1}{\natlog{X_2}}$. Find a formula that will allow you to compute its PDF $f_H(h)$ in terms of the JDF $f_{X_1, X_2}(x_1, x_2)$. \spc{7}

\subquestionwithpoints{8} Let $X_1, X_2 \iid \text{Laplace}(0,1)$ and let $R = \frac{X_1}{X_2}$. Write a mathematical expression for $f_R(r)$. Do NOT simplify! \spc{4}

\subquestionwithpoints{8} Let $\Xoneton \iid \exponential{\lambda}$. Find the distribution of their minimum. If it is a brand name r.v. we studied, indicate it with the value(s) of its parameters. \spc{5}

\subquestionwithpoints{8} Let $X_1, \ldots, X_{37} \iid \stduniform$. Find $\prob{X_{(17)} \leq \half}$. Express your answer in terms of the regularized incomplete beta function. \spc{8}

\subquestionwithpoints{8} If $X_1, X_2 \iid \text{ExtNegBin}(k, p) := \frac{\Gamma(k + x)}{\Gamma(k)x!} (1 - p)^x p^k \indic{x \in \naturals_0}$ who share the ch.f. $\phi_X(t) = \tothepow{\frac{1-p}{1-pe^{it}}}{k}$, show that $T = X_1 + X_2$ is also distributed as a ExtNegBin and find its parameters. If you use the following properties of ch.f.'s make sure to reference them in your proof by saying \qu{(P1)}, \qu{(P2)}, etc.

\footnotesize
\begin{itemize}
\item[(P0)] $\phi_X(0) = 1$
\item[(P1)] $\phi_X(t) = \phi_Y(t)$ is equivalent to $X \equalsindist Y$
\item[(P2)] If $Y = aX + b$ then $\phi_Y(t) = e^{ibt}\phi_X(at)$
\item[(P3)] If $X_1, X_2 \inddist$ and $T = X_1 + X_2$ then $\phi_T(t) = \phi_{X_1}(t) \phi_{X_2}(t)$ and if $X_1, X_2 \iid$ then $\phi_T(t) = \phi_{X}(t)^2$
\item[(P4)] For any moment that exists, $\expe{X^n} = \phi^{(n)}_X(0) / i^n$
\end{itemize}
\normalsize~\spc{6}

\subquestionwithpoints{8} [MA] If $Y~|~X = x \sim \text{ExtNegBin}(k, x)$ and $X \sim \betanot{\alpha}{\beta}$. Find the PMF of $Y$, a r.v. model you have not seen yet. Simplify \textit{as much as possible}. For those curious, $Y \sim \text{BetaNegBin}(k, \alpha, \beta)$, appropriately named the \qu{beta negative binomial}. \spc{8}

\subquestionwithpoints{6} [Extra Credit] If $Y~|~X = x \sim \binomial{10}{x}$ and $X \sim \betanot{\alpha}{\beta}$ where $Y$ models the number of male births for women who have 10 children and $x$ is the probability of having a male child (assuming each child's gender is an independent and identically distributed process). If Jane had 2 girls, what is the distribution of her $X$? \spc{1}

\eenum

\end{document}


\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{tikz-cd}
\usepackage{url}

\title{Mathematical software - homework 2}
\author{Sebastiano Tronto}

\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}

\theoremstyle{definition}
\newtheorem{ex}{Exercise}

\theoremstyle{definition}
\newtheorem*{remark}{Remark}

\newcommand{\bs}{\textbackslash}

\begin{document}

\noindent\hrulefill

\begin{center}
\Huge{\textbf{Mathematical Software - Homework 2}}
\end{center}

\noindent\hrulefill
\begin{center}
\begin{tabular}{lcr}
\texttt{sebastiano.tronto@uni.lu} & \qquad \qquad \qquad \qquad &
\textbf{Deadline: Sunday, April 18th}
\end{tabular}
\end{center}

\vspace{1cm}

\begin{center}
  \emph{\large
    For each of the following exercises submit a .tex and a .pdf file.
  }
\end{center}

\vspace{1cm}

\begin{ex}
  Create a Latex document containing the following pictures:
  \begin{enumerate}[label=(\alph*)]
    \item The regular polygon with $N$-sides centered at the origin of the
          plane (see below).
          The number $N$ of sides must be easy to change at will: you should
          use the \texttt{\textbackslash pgfmathsetmacro} command to set a
          value for $N$ at the beginning, so that changing only that number
          makes the whole picture change accordingly.
      \begin{center}
        \begin{tikzpicture}[scale=1]
        	\pgfmathsetmacro{\N}{6}
        	\pgfmathsetmacro{\an}{360/\N}
        	\pgfmathsetmacro{\r}{4}

        	\draw[lightgray!30,thin] (-6,-6) grid (6,6);
        	\draw[->] (-5,0) -- (5,0);
        	\draw[->] (0,-5) -- (0,5);

        	\filldraw[draw=green,fill=green!30,thick] (0.3*\r,0)
        	  arc[radius=0.3*\r,start angle=0, end angle=\an]
        	  -- node[right] {$\alpha=\an$}(0,0) --  cycle;
        	\draw[thin] (\an:\r) -- (0,0) circle[radius=\r] -- (\r,0);
        	\draw[line width=1.1pt]
        	  (\r,0)  \foreach \x in {1,2,...,\N} { -- (\x*\an:\r) };
        	\foreach \x in {1,...,\N} {
        		\filldraw[red] (\x*\an:\r) circle[radius=0.1];
        	};
        \end{tikzpicture}
      \end{center}
    \item The following commutative diagram:
      \begin{tikzcd}
        0 \ar[r] & A' \ar[r,hook] \ar[d] & A \ar[r,two heads] \ar[d,"\sim"] &
          A'' \ar[r] \ar[d] \ar[l,dashed,"s"',bend right] & 0 \\
        0 \ar[r] & A' \ar[r,hook,"i_{A'}"'] & A'\oplus A''
          \ar[r,two heads,"\pi_{A''}"'] & A'' \ar[r] & 0
      \end{tikzcd}
  \end{enumerate}
\end{ex}

\vspace{0.8cm}

\begin{ex}
  Suppose you have to give a short presentation (10 minutes) on a topic of
  your choice related to your study programme (you will not be asked to
  actually perform this presentation). You can choose to talk about a theorem
  you find important, a result you have seen in class or something else (see
  below for a list of possible topics). For example, if you talk about an
  important theorem you can give the theorem statement, explain why this
  theorem is important and/or possible applications of this result, and
  optionally an idea of the proof; but you can also deviate from this and talk
  for example about the historical background that lead to the development of
  this theorem.

  Your task is to prepare slides for such a presentation using Beamer (Latex).
  Since the time for the (imaginary) presentation is very short, you should
  write 5-8 slides (you can have more if some contain very few or no
  words).

  If you feel like certain slides do not make sense without your explanation
  (for example if you have one slide with just one picture and you plan to
  talk with the picture in background), you can write some comments in the
  .tex file.

  If you can't think of a topic that you like, you can pick one of the
  following:
  \begin{itemize}
    \item The fundamental theorem of arithmetic (about prime numbers)
    \item The central limit theorem (probability theory)
    \item Differential equations (what they are, applications,
          methods to solve them...)
  \end{itemize}
\end{ex}

\newpage
\section*{Grading}

This homework assignment is worth 25\% of your final grade.

\vspace{0.3cm}
\noindent\textbf{Exercise 1 (10 points).}
Part (a) is worth 5 points, divided as follows:
\begin{itemize}
  \item 3 points for obtaining a regular polygon whose number of sides
        can be changed by setting a variable with \texttt{\bs pgfmathsetmacro}
        (or in a similarly easy way).
  \item 1.5 points for other features of the picture (verteces, angle) that
        also change accordingly to the same variable.
  \item 0.5 points for the style of the other elements of the picture. This is
        a matter of personal preference and it does not need to be exactly the
        same as the picture, but some key features should remain (e.g.
        the grid lines should be less visible than the rest of the picture,
        the circle line style should be different from the polygon).
\end{itemize}
Part (b) is worth 5 points, divided as follows:
\begin{itemize}
  \item 3 points if the nodes and arrows of the diagram are correct from a
        mathematical point of view (that is, the arrows point to the correct
        object).
  \item 1 point if the labels of the arrows are placed as shown in the picture
        above.
  \item 1 point for the correct style of the arrows (dashed, curved).
\end{itemize}

\vspace{0.2cm}
\noindent\textbf{Exercise 2 (10 points).}
\begin{itemize}
  \item Producing a presentation that contains at least 4 slides is worth
        5 points (but within reason: for example, the slides must not be empty).
  \item Up to 3 more points are given if the presentation is of a suitable
        length (watch out: both a presentation too short and one too long can
        loose points!).
        Comments in the .tex file can help me understand how long you plan to
        spend on each slide. If you are not sure how long your presentation is
        going to take, try it and write down how long each slide took.
  \item Up to 2 more points will be given if the slides ``look nice'' from
        the audience's perspective (e.g. not too many words on the same slides,
        are there nice pictures, etc).
\end{itemize}

\end{document}