\documentclass[onecolumn]{article}
\usepackage{amsmath}
\usepackage{lscape}
\begin{document}
\title{01 - Conventions}
\author{}
\date{}
\maketitle
\section{Conventions} % (fold)
Following are applicable to calculate the chemical potential of a solution, and for expressing its composition.
\label{sec:conventions}
% section section_name (end)
\subsection{Composition}
\label{subsec:composition}
\begin{table}[h]
\begin{tabular}{|lll|l|}
\hline
		      & expression
					& [=] units
					& total\\
\hline
mole number   & $n_i$ & mol i
							& $n = \sum_i{n_i} = {n_0 + \sum_{j \neq 0}{n_j}}$ \\
mass 					& $w_i$	 & g i
							& $w = \sum_i{w_i} = {n_0 M_0 + \sum_{j \neq 0}{n_j M_j}}$ \\
molar mass		& $M_i = \frac{w_i}{n_i}$ & $\frac{g_i}{mol_i}$
 							& $M = \frac{\sum_i{w_i}}{\sum_i{w_i/M_i}} =
							\frac{1}{\sum_i{x_{w, i}/M_i}}$ \\
molal  conc.  & $m_i = \frac{n_i}{n_0 M_0} = \frac{n_i}{n_0 M_0 /
\left(1000 \frac{g}{kg} \right)}$ & $\frac{mol_i} {kg_{solvent}}$
							& $\sum_i{m_i} = \frac{\sum_i{n_i}}{n_0 M_0} =
							\frac{1}{x_0 M_0} $ \\
density 	    & $\rho_i = w_i/V_r = n_i M_i/V_r = c_i M_i$ &
							$\frac{g_i}{mL}$
							& $\rho = \sum_i{\rho_i} = \frac{\sum_i{w_i}}{V_r} = w/V_r$ \\
molar  conc.  & $c_i = n_i/V_r = n_i \rho_i /w_i = \rho_i / M_i$
							& $\frac{mol_i}{L}$
							& $c_T = \sum_i{c_i} = \frac{\sum_i{n_i}}{V_r} = n/V_r$ \\
mole fraction & $x_i = \frac{n_i}{\sum_j{n_j}}$ & [adim.]
							& $\sum_i{x_i}=1$ \\
mass fraction & $x_{w,i} = \frac{w_i}{\sum_j{w_j}}$ & [adim.]
			        & $\sum_i{x_{w, i}} = 1$\\
\hline
\end{tabular}
\caption{Conventional expressions of composition. Subscript 0 applied to the
solvent, other subscripts applied to solutes.}
\label{table:composition_expr}
\end{table}
Relationships are obtained for conversion, such as density of solution $\rho$ to density of
solvent $\rho_0 = c_0 M_0$, molal concentration to mole fraction and molar concentration to
mole fraction: \\
\[
\begin{aligned}
\frac{\rho}{\rho_0} & = \frac{\sum_{i}{\rho_i}}{c_0 M_0} = \frac{c_0 M_0 + \sum_{j \neq 0}{c_j M_j}}{c_0 M_0} = 1 + \frac{\sum_{j\neq0}{n_j M_j/V_r}}{n_0 M_0/V_r} = 1+\sum_{j\neq0}{m_j M_j}\\
\frac{m_i}{x_i} & = \frac{n_i/(n_0 M_0) \times (n_0 + \sum_{j\neq0}{n_j}) }{n_i} = \frac{1}{M_0} + \sum_{j\neq0}{m_j} = \frac{1}{M_0} \times (1 + \sum_{j\neq0}{m_jM_0}) \\
\frac{c_i}{x_i} & = \frac{n_i \rho \times (n_0 + \sum_{j\neq0}{n_j}) }{n_i \times (n_0 M_0 + \sum_{j\neq0}{n_j M_j})} = \frac{ n_0 + \sum_{j\neq0}{n_j} }{ n_0 M_0 + \sum_{j\neq0}{n_j M_j}} \rho = \frac{ \frac{1}{M_0} + \sum_{j\neq0}{m_j} }{ 1 + \sum_{j\neq0}{m_j M_j}} \rho \\
 & = \frac{\rho_0}{M_0} \times (1 + \sum_{j\neq0}{m_j M_0})
\end{aligned}
\]
These conversions are summarized below:
\begin{table}[h]
\begin{center}
\begin{tabular}{|l|cccc|}
\hline
 			& $x_i$ & $m_i$ & $c_i$ & $\rho_i$ \\
\hline
$x_i$ & 	1		& $\frac{x_i}{m_i} = x_0 M_0$
			&	$\frac{x_i}{c_i} = \frac{x_0 M_0}{\rho_0}$
			& $\frac{x_i}{\rho_i} = \frac{x_0 M_0}{\rho_0 M_i}$ \\
$m_i$ & $\frac{x_i}{m_i} = \frac{M_0}{1 + \sum_{j\neq0}{m_jM_0}}$	& 	1
			&	$\frac{m_i}{c_i} = \frac{1}{\rho_0}$
			& $\frac{m_i}{\rho_i} = \frac{1}{\rho_0 M_i} $\\
$c_i$ &	$\frac{x_i}{c_i} = \frac{M_0}{\rho} \times
			\frac{1+\sum_{j\neq0}{m_j M_j}}{1 + \sum_{j\neq0}{m_j M_0}}$
			& $\frac{m_i}{c_i} = \frac{1+\sum_{j\neq0}{m_j M_j}}{\rho}$
			&		1		& $\frac{c_i}{\rho_i} = \frac{1}{M_i}$ \\
$\rho_i$ & 	$\frac{x_i}{\rho_i} = \frac{M_0}{\rho M_i} \times
			\frac{1+\sum_{j\neq0}{m_j M_j}}{1 + \sum_{j\neq0}{m_j M_0}}$
			& $\frac{m_i}{\rho_i} = \frac{1 + \sum_{j\neq0}{m_j M_j}}{\rho M_i}$
			&	$\frac{c_i}{\rho_i} = \frac{1}{M_i}$	& 1 \\
\hline
\end{tabular}
\end{center}
\caption{Conversion between composition expressions in terms of solvent density
$\rho_0$ and solution density $\rho$}
\label{table:composition_expr_conv}
\end{table}
\subsection{Chemical potential \cite{Denbigh1968} }
\label{subsec:chemical_potential}
Suitable conventions are applied to different solutions: Convention I is useful for solutions of miscible liquids at T,P (e.g. ethanol/water). Conventions II, III, and IV are useful for solutions of components that are gases or solids at T,P (e.g. oxygen or sugar in water). \\
Convention I: Each component approaches ideality as its mole fraction approaches unity.
\begin{equation}
\textnormal{component (i): } (\gamma_i \rightarrow 1) as  (x_i \rightarrow 1)\\
\end{equation}
Convention II: Ideal solvent as it approaches unit mole fraction; ideal solutes at \textit{infinite dilution}, expressed in mole fraction.\\
\begin{equation}
\begin{aligned}
\textnormal{solvent (0): } & (\gamma_0 \rightarrow 1) as  (x_0 \rightarrow 1)\\
\textnormal{solute (i): } & (\gamma_i \rightarrow 1) as (x_i \rightarrow 0)
\end{aligned}
\end{equation}
Convention III: Ideal solvent as it approaches unit mole fraction; ideal solutes at \textit{infinite dilution}, expressed in molality. \\
\begin{equation}
\begin{aligned}
\textnormal{solvent (0): } & (\gamma_0 \rightarrow 1) as  (x_0 \rightarrow 1)\\
\textnormal{solute (i): } & (\gamma_i \rightarrow 1) as (m_i \rightarrow 0)
\end{aligned}
\end{equation}
Convention IV: Ideal solutes at \textit{infinite dilution}, expressed in molarity. \\
\begin{equation}
\begin{aligned}
\textnormal{solute (i): } & (\gamma_i \rightarrow 1) as (c_i \rightarrow 0)
\end{aligned}
\end{equation}
Chemical potential expressed under each convention, with $m_i^{\circ}$ and $c_i^{\circ}$ selected as reference solute states at unit molality, and molarity respectively: $m_i^{\circ}=1 \frac{mol}{kg_{solvent}}$ and $c_i^{\circ}=1 \frac{mol}{L}$.
\begin{equation}
\begin{aligned}
\mu_i = & \mu_i^{*} + RTln(\gamma_i^{I} x_i) \\
	= 	& \mu_i^{*} + RTln(\gamma_i^{II} x_i) \\
	=	& \mu_i^{\fbox{}} + RTln(\gamma_i^{III} m_i/m_i^{\circ}) \\
	= 	& \mu_i^{\diamond} + RTln(\gamma_i^{IV} c_i/c_i^{\circ})
\end{aligned}
\end{equation}
Notes on conversion between conventions:
\begin{itemize}
\item Values of activity coefficients $\gamma_i^{II}$ and $\gamma_i^{III}$ vary, but chemical potential does not vary due to convention selection.
\item Reference states chemical potentials under each convention $\mu_i^{*}$ and $\mu_i^{\fbox{}}$  are functions of temperature and pressure exclusively. Their difference calculated at infinite dilution does not change at other compositions:
\end{itemize}
\[
\mu_i^{*} - \mu_i^{\fbox{}}  =  func(T,P) =  \lim_{x_0 \to 1} \left( \mu_i^{*} - \mu_i^{\fbox{}}\right)
\]
\[
RTln\left[ \frac{\gamma_i^{III} m_i/m_i^{\circ}}{\gamma_i^{II} x_i}\right] = \lim_{x_0 \to 1} RTln\left[ \frac{\gamma_i^{III} m_i/m_i^{\circ}}{\gamma_i^{II} x_i}\right]
\]
Therefore, referring back to conventions II and III, $\gamma_i \rightarrow 1$ at \textit{infinite dilution}:
\[
\begin{aligned}
\frac{\gamma_i^{III} m_i/m_i^{\circ}}{\gamma_i^{II} x_i} & = \lim_{x_0 \to 1} \frac{\gamma_i^{III} m_i/m_i^{\circ}}{\gamma_i^{II} x_i} = \frac{1}{m_i^0} \times \lim_{x_0 \to 1} \frac{m_i}{x_i} \\
& = \frac{1}{m_i^0} \times \lim_{x_0 \to 1} \frac{n_0+\sum_{j \neq 0}n_j}{n_0 M_0} = \frac{1}{m_i^0 M_0} \times \lim_{x_0 \to 1} \frac{1}{x_0} \\
& = \frac{1}{m_i^\circ M_0}
\end{aligned}
\]
This leads to the following relationship between activity coefficients calculated by each convention:
\begin{equation}
\label{eq:act_coef_m_to_x}
\gamma_i^{II} = \gamma_i^{III} \times \frac{m_i M_0}{x_i}
\end{equation}
A standard form of equation \ref{eq:act_coef_m_to_x} is obtained by assuming complete dissociation of a solute $A$ with molality $m$ into $\nu$ components, so that for all solutes the sum of molalities can be expressed as $\sum_{i}m_i = \nu m $:
\[
A \rightleftharpoons A_1 + A_2 + A_2 + ... + A_{\nu}
\]
Combined with Table \ref{table:composition_expr_conv}, the expression as presented in \cite{Hamer1968} is obtained:
\[
\begin{aligned}
\gamma_i^{II} & =  \gamma_i^{III} \times M_0 \frac{m_i}{x_i} = \gamma_i^{III} \times (1+\sum_{j\neq0}{m_j M_0}) = \gamma_i^{III} \times \left(1+\frac{\nu m M_0}{1000 \frac{g}{kg}} \right)
\end{aligned}
\]
Equivalent expressions are obtained to convert activity coefficients expressed in terms of concentration:
\[
\begin{aligned}
\frac{\gamma_i^{IV} c_i/c_i^{\circ}}{\gamma_i^{II} x_i} & = \lim_{x_0 \to 1} \frac{\gamma_i^{IV} c_i/c_i^{\circ}}{\gamma_i^{II} x_i} = \frac{1}{c_i^0} \times \lim_{x_0 \to 1} \frac{c_i}{x_i} = \frac{\rho_0}{c_i^0} \times \lim_{x_0 \to 1} \frac{m_i}{x_i} \\ & = \frac{\rho_0}{c_i^0} \times \lim_{x_0 \to 1}\frac{n_0+\sum_{j \neq 0}n_j}{n_0 M_0} = \frac{\rho_0}{c_i^0 M_0}\\
\end{aligned}
\]
Similarly, the relationship between calculated activity coefficients is determined:
\begin{equation}
\label{eq:act_coef_c_to_x}
\gamma_i^{II} = \gamma_i^{IV} \times \frac{c_i M_0}{x_i \rho_0}
\end{equation}
Equation \ref{eq:act_coef_c_to_x} is represented accordingly with the solute dissociation convention:
\[
\gamma_i^{II} = \gamma_i^{IV} \times \frac{M_0}{\rho_0}\times \frac{c_i}{x_i} = \gamma_i^{IV} \times (1 + \sum_{j\neq0}{m_jM_0}) = \gamma_i^{IV} \times \left(1+\frac{\nu m M_0}{1000 \frac{g}{kg}} \right)
\]
For the solvent, conversion between conventions is obtained through equation
\ref{eq:act_coef_m_to_x}:
\[
\gamma_0^{II} = \gamma_0^{III} \times \frac{m_0 M_0}{x_0} =
\gamma_0^{III} \times \frac{1}{x_0}
\]
 \begin{thebibliography}{9}
 \bibitem{Denbigh1968} Denbigh, Kenneth G.; The principles of chemical equilibrium; 4th ed. Cambridge University Press, UK 1981.
 \bibitem{Hamer1968} NSRDS 24 Theoretical Mean Activity Coefficients of Strong Electrolytes in Aqueous Solutions from 0 to 100°C - Walter J. Hamer. NSRDS-NBS 24, 271p. (1968). www.nist.gov/data/nsrds/NSRDS-NBS-24.pdf [Apr-2016]
 \end{thebibliography}
\end{document}


\section{Electricity \& Magnetism}

This section covers material related to the E\&M portion of the exam.
Most of these subsections go into more depth than is required for the PGRE.
Gauss's law, Faraday's law, and radiation originating from charged particles are consistently tested on the practice exams.
Method of images is also something to brush up on.

\subsection{Electrostatics}

\subsubsection{Coulomb's Law}
Force Law: \(\displaystyle\mathbf{F}_E=k_e\frac{q_1q_2}{\scriptr^2}\hat{\scriptr}\)\\\\*
Electric Field:\\*
\(\displaystyle\mathbf{F}_E=q\mathbf{E}\)\\*
\(\displaystyle\mathbf{E}=k_e\sum_i\frac{q_i}{\scriptr_i^2}\hat{\scriptr}_i\to k_e\int\frac{\hat{\scriptr}}{\scriptr^2}\mathrm{d}q\)\\\\*
Electric Field Example: E-field along \(z\)-axis due to a ring of charge \(Q\) centered at the origin and in the \(x\)-\(y\) plane: \(\displaystyle\mathbf{E}=\frac{k_eQd}{(R^2+z^2)^{3/2}}\hat{z}\)

\subsubsection{Gauss's Law}
Electric Flux: \(\Phi_E=\int_S\mathbf{E}\cdot\,\mathrm{d}\mathbf{a}\)\\*
Through a Closed Surface: \(\displaystyle\Phi_E=\oint_{\delta V}\mathbf{E}\cdot\,\mathrm{d}\mathbf{a}\)\\*
Differential Form: \(\nabla\cdot\mathbf{E}=\displaystyle\frac{\rho}{\epsilon_0}\)\\*
Integral Form: \(\displaystyle\oint_{\delta V}\mathbf{E}\cdot\,\mathrm{d}\mathbf{a}=\frac{Q_{enc}}{\epsilon_0}\)\\\\*
Common Uses of Gauss's Law:
\begin{itemize}
\item Spherical Symmetry: Inside uniformly charged sphere of radius \(a \to\) \(\displaystyle E=\frac{k_eQ}{a^3}r=\frac{\rho}{3\epsilon_0}r\)
\item Cylindrical Symmetry: Outside cylinder of radius \(b\) with uniform surface charge \(\to\) \(\displaystyle E=\frac{\sigma b}{\epsilon_0}\frac{1}{r}\)
\item Planar Surface: Near plate with uniform surface charge \(\to\) \(\displaystyle E=\frac{\sigma}{2\epsilon_0}\)
\end{itemize}
Gauss's law is also useful in showing that all the net charge on a conductor must reside on its surface, as well as that there is no E-field inside a conductor and, therefore, no net force on a particle placed in a conductor.\\*
Gauss's law is typically the easiest way to calculate E-fields when enough spatial symmetry is present.

\newpage
\subsubsection{Electric Potential}
\(\nabla\times\mathbf{E}=\vec{0}\to\mathbf{E}=-\nabla V \to -\nabla^2 V=\displaystyle\frac{\rho}{\epsilon_0}\)\\\\*
Electric Potential: \(\displaystyle V(\mathbf{b})-V(\mathbf{a})= -\int_\mathbf{a}^\mathbf{b}\mathbf{E}\cdot\,\mathrm{d}\mathbf{s}\)\\\\*
If \(\mathbf{a}\) is taken to be a reference where \(V(\mathbf{a})=0\), then \(\displaystyle V(\mathbf{r})= -\int_\mathbf{O}^\mathbf{r}\mathbf{E}\cdot\,\mathrm{d}\mathbf{s}\)\\\\*
\(\displaystyle V=k_e\sum_i\frac{q_i}{\scriptr_i} \to k_e\int\frac{\mathrm{d}q}{\scriptr}\)\\*
A conductor is an equipotential and the \(\mathbf{E}\) field is \(\bot\) to the surface just above a conductor (else the surface charge would move).\\*
Electric Potential Example: Potential along \(z\)-axis due to a ring of charge \(Q\) centered at the origin and in the \(x\)-\(y\) plane \(\to\) \(\displaystyle\ V=\frac{k_eQ}{\sqrt{R^2+z^2}}\)

\subsubsection{Electrostatic Force on a Conductor}
Force per unit area: \(\mathbf{f}=\sigma\mathbf{E}_{ave}=\frac{1}{2}\sigma(\mathbf{E}_{above}-\mathbf{E}_{below})\)\\*
(This actually applies to any surface charge.)\\\\*
For a conductor: \(\mathbf{f}=\frac{1}{2\epsilon_0}\sigma^2\hat{\mathbf{n}}\)

\subsubsection{Electric Dipole}
Electric Dipole Moment: \(\mathbf{p}=q\mathbf{d}\) where \(\mathbf{d}\) is the displacement vector pointing from the negative charge (\(-q\)) to the positive charge (\(q\))\\*
\(\displaystyle\mathbf{p}=\int{\mathbf{r'}\rho(\mathbf{r}')\mathrm{d}v'}\)\\\\*
The electric potential from a dipole:\\*
\(\displaystyle V(\mathbf{r})=\frac{1}{4\pi\epsilon_0}=\frac{\mathbf{p}\cdot\hat{\mathbf{r}}}{r^2}\)\\\\*
The field from a dipole:\\*
\(\displaystyle E_{dipole}\propto \frac{p}{r^3}\)\\*
Specifically, \(\displaystyle \mathbf{E}_{dipole}=\frac{1}{4\pi\epsilon_0}\frac{1}{r^3}\left[3\left(\mathbf{p}\cdot\hat{\mathbf{r}}\right)\hat{\mathbf{r}}-\mathbf{p}\right]\)\\\\*
Effect of an external \(\mathbf{E}\) field on a dipole:\\*
Force: \(\mathbf{F}=\left(\mathbf{p}\cdot\nabla\right)\mathbf{E}\)\\*
Torque: \(\vec{\tau}=\mathbf{p}\times\mathbf{E}\)\\*
Potential Energy: \(U=-\mathbf{p}\cdot\mathbf{E}\)

\subsubsection{Dielectrics}
The dipole moment per unit volume is called the polarization \(\mathbf{P}\).\\*
\(\mathbf{P}\cdot\hat{\mathbf{n}}=\sigma_{bound}\)\\*
\(-\nabla\cdot\mathbf{P}=\rho_{bound}\)\\\\*
\(\displaystyle V(\mathbf{r})=\frac{1}{4\pi\epsilon_0}\int{\frac{\hat{\mathbf{\scriptr}}\cdot\mathbf{P}(\mathbf{r}')}{\scriptr^2}\mathrm{d}v'}\)\\\\*
The electric displacement is \(\mathbf{D}=\epsilon_0\mathbf{E}+\mathbf{P}\).\\*
\(\nabla\cdot\mathbf{D}=\rho_{free}\to \displaystyle\oint_{\delta V}\mathbf{D}\cdot\,\mathrm{d}\mathbf{a}=Q_{free_{enc}}\)\\\\*

Linear Dielectrics:\\*
Dielectric Constant: \(\displaystyle\kappa=\frac{\epsilon}{\epsilon_0}\) (note: \(\kappa\geq 1\) typically)\\*
\(\mathbf{P}=(\epsilon-\epsilon_0)\mathbf{E}\)\\*
\(\mathbf{D}=\epsilon\mathbf{E}=\kappa\epsilon_0\mathbf{E}\)\\\\*
A convenient way to calculate \(\displaystyle\sigma_{bound}\) is to use \(\mathbf{D}=\epsilon\mathbf{E}\) with \(\displaystyle\oint_{\delta V}\mathbf{D}\cdot\,\mathrm{d}\mathbf{a}=Q_{free_{enc}}\) to get \(\mathbf{P}\) and finally use the relation \(\mathbf{P}\cdot\hat{\mathbf{n}}=\sigma_{bound}\).

\subsubsection{Energy in an Electrostatic Field}
\(\displaystyle U=\frac{k_e}{2}\sum_j\sum_{i\neq j}\frac{q_iq_j}{r_{ij}} \)\\*
\(\displaystyle U=\frac{\epsilon_0}{2}\int{E^2\mathrm{d}v}\)\\*
\(\displaystyle U=\frac{1}{2}\int{\mathbf{D}\cdot\mathbf{E}\hspace{2pt}\mathrm{d}v}\)

\subsubsection{Method of Images}
Force behaves as if there was an actual image charge present.\\*
Energy, however, needs to be calculated using only regions ``outside'' the conductor. (\(\mathbf{E}=\mathbf{0}\) ``inside'')\\*
For planar conductor, replace the conductor with a mirror image of the charge distribution with opposite charge.\\*
For spherical conductor of radius \(R\), replace the conductor with a charge \(q'\) a distance \(b\) from the origin.
(\(r\) is the distance \(q\) is from the center of the sphere)
\begin{eqnarray}
\displaystyle q'&=&\frac{-R}{r}q \nonumber \\
\displaystyle b&=&\frac{R^2}{r} \nonumber
\end{eqnarray}

\subsubsection{Separation of Variables}
Used to solve Laplace's equation: \(\nabla^2 V(\mathbf{r})=0\)\\\\*
Decompose \(V(\mathbf{r})\) into separate, independent functions of the coordinates.\\\\*
Use boundary conditions to find relationship between the series coefficients and exploit the orthogonality (or orthonormality) of the trigonometric functions, \(P_l\), or \(Y_l^m\).\\\\*
For Cartesian coordinates you must set up and solve each scenario from scratch.\\*
In spherical polar coordinates with azimuthal symmetry, use:\\*
\(\displaystyle V(r,\theta)=\sum_{l=0}^\infty\left(A_lr^l+\frac{B_l}{r^{l+1}}\right)P_l(\cos{\theta})\)

\subsection{Magnetostatics}

\subsubsection{Current}
\(\displaystyle I=\frac{\mathrm{d}q}{\mathrm{d}t}\)\\\\*
Drift Velocity of Charge Carriers: \(I=nqv_DA\)\\*
Current Density: \(J=\frac{I}{A}=nqv_D\to \mathbf{J}=nq\mathbf{v}_D\)\\\\*
Linear Current: \(\mathbf{I}=\lambda \mathbf{v}\)\\*
Surface Current: \(\mathbf{K}=\sigma \mathbf{v}\)\\*
Volume Current: \(\mathbf{J}=\rho \mathbf{v}\)

\subsubsection{Biot-Savart's Law}
\(\displaystyle\mathbf{B}=\frac{\mu_0}{4\pi}\int\frac{\mathbf{I}\times\hat{\scriptr}}{\scriptr^2}\mathrm{d}s'\)\\\\*
\(\displaystyle\mathbf{B}=\frac{\mu_0}{4\pi}\int\frac{\mathbf{K}\times\hat{\scriptr}}{\scriptr^2}\mathrm{d}a'\)\\\\*
\(\displaystyle\mathbf{B}=\frac{\mu_0}{4\pi}\int\frac{\mathbf{J}\times\hat{\scriptr}}{\scriptr^2}\mathrm{d}v'\)\\\\*
Common Uses of Biot-Savart's Law:
\begin{itemize}
\item Circular wire arc at center of curvature: \(\displaystyle B=\frac{\mu_0I\theta}{4\pi R}\)
\item Circular current loop along axis: \(\displaystyle B=\frac{\mu_0I}{2}\frac{r^2}{\left(R^2+z^2\right)^{3/2}}\)
\end{itemize}
Both of these give \(\displaystyle B=\frac{\mu_0I}{2R}\) at the center of a current loop.
Far away from the loop (along the axis) the B-field behaves like \(\displaystyle B\propto\frac{1}{z^3}\).

\subsubsection{Amp\`ere's Law}
DifferentialForm: \(\nabla\times\mathbf{B}=\mu_0\mathbf{J}\)\\*
Integral Form: \(\displaystyle\oint_{\delta S}\mathbf{B}\cdot\,\mathrm{d}\mathbf{l}=\mu_0 I_{enc}\)\\\\*
Common Uses of Amp\`ere's Law:
\begin{itemize}
\item Long wire: \(\displaystyle B=\frac{\mu_0I}{2\pi r}\)
\item Solenoid/Toroid: \(\displaystyle B=\mu_0 n I\) where \(\displaystyle n=\frac{N_{turns}}{l}\) for a solenoid and \(\displaystyle\frac{N_{turns}}{2\pi R}\) for a toroid
\end{itemize}

\subsubsection{Magnetic Forces on Objects}
Particle: \(\mathbf{F}_B=q\mathbf{v}\times\mathbf{B}\)\\*
Wire:  \(\mathbf{F}_B=I\mathbf{L}\times\mathbf{B}\) (\(\mathbf{L}\) connects \emph{endpoints} of wire)\\*
The net magnetic force acting on any closed current loop in \emph{uniform} magnetic field is zero (\(\mathbf{L}=\mathbf{0}\)), but the torque isn't necessarily zero.\\*
\(\vec{\tau}=I\mathbf{A}\times\mathbf{B}\) where \(\mathbf{A}\) is the area enclosed by the loop\\\\*
\(\mathbf{F}=\int I(\mathrm{d}\mathbf{s}\times\mathbf{B})\)\\*
\(\mathbf{F}=\int (\mathbf{K}\times\mathbf{B})\mathrm{d}a\)\\*
\(\mathbf{F}=\int (\mathbf{J}\times\mathbf{B})\mathrm{d}v\)\\\\*
Cyclotron (angular) frequency:
\begin{eqnarray}
ma&=&qvB \nonumber\\
m\omega^2R&=&q\omega RB \nonumber\\
\omega_{cyclotron}&=&\frac{qB}{m} \nonumber
\end{eqnarray}
Force per length between two current carrying wires separated by a distance \(a\): \(\displaystyle\frac{F_B}{l}=\frac{\mu_0I_1I_2}{2\pi a}\) (currents in the same direction attract while opposite currents repel)

\subsubsection{Magnetic Flux}
\(\Phi_B=\int_S\mathbf{B}\cdot\,\mathrm{d}\mathbf{a}\)\\\\*
\(\displaystyle\Phi_B=\oint_{\delta V}\mathbf{B}\cdot\,\mathrm{d}\mathbf{a}=0\to\nabla\cdot\mathbf{B}=\mathbf{0}\)\\*
This last statement just means there are no magnetic monopoles, or ``magnetic charges.''

\subsubsection{Magnetic Vector Pontential}
\(\nabla\cdot\mathbf{B}=\mathbf{0}\to\mathbf{B}=\nabla\times\mathbf{A}\)\\*
\(\nabla^2\mathbf{A}=-\mu_0\mathbf{J}\)\\\\*
\(\displaystyle\mathbf{A}=\frac{\mu_0}{4\pi}\int\frac{I}{\scriptr}\mathrm{d}\mathbf{s}'\)\\\\*
\(\displaystyle\mathbf{A}=\frac{\mu_0}{4\pi}\int\frac{\mathbf{K}(\mathbf{r}')}{\scriptr}\mathrm{d}a'\)\\\\*
\(\displaystyle\mathbf{A}=\frac{\mu_0}{4\pi}\int\frac{\mathbf{J}(\mathbf{r}')}{\scriptr}\mathrm{d}v'\)

\subsubsection{Magnetic Dipole}
\(\mathbf{m} =\int I\mathrm{d}\mathbf{a}\)\\\\*
The magnetic vector potential from a dipole:\\*
\(\displaystyle\mathbf{A}_{dipole}=\frac{\mu_0}{4\pi}\frac{\mathbf{m}\times\hat{\mathbf{r}}}{r^2}\)\\\\*
The field from a dipole:\\*
\(\displaystyle \mathbf{B}_{dipole}=\frac{\mu_0}{4\pi}\frac{1}{r^3}\left[3\left(\mathbf{m}\cdot\hat{\mathbf{r}}\right)\hat{\mathbf{r}}-\mathbf{m}\right]\)\\\\*
Effect of an external \(\mathbf{B}\) field on a dipole:\\*
Force: \(\mathbf{F}=\nabla(\mathbf{m}\cdot\mathbf{B})\)\\*
Torque: \(\vec{\tau}=\mathbf{m}\times\mathbf{B}\)\\*
Potential Energy: \(U=-\mathbf{m}\cdot\mathbf{B}\)

\subsubsection{Dia-Para-Ferromagnetic Materials}
The magnetic dipole moment per unit volume is called the magnetization \(\mathbf{M}\).\\*
\(\nabla\times\mathbf{M}=\mathbf{J}_{bound}\)\\*
\(\mathbf{M}\times\hat{\mathbf{n}}=\mathbf{K}_{bound}\)\\\\*
\(\displaystyle\mathbf{A}(\mathbf{r})=\frac{\mu_0}{4\pi}\int\frac{\mathbf{M}\times\hat{\scriptr}}{\scriptr^2}\mathrm{d}v'\)\\\\*
\(\displaystyle\mathbf{H}=\frac{1}{\mu_0}\mathbf{B}-\mathbf{M}\)\\\\*
\(\nabla\times\mathbf{H}=\mathbf{J}_{free}\)\\*
\(\displaystyle\oint\mathbf{H}\cdot\mathrm{d}\mathbf{l}=I_{free_{enc}}\)\\\\*
For Linear Materials: \(\mathbf{H}=\mu\mathbf{B}\)
\begin{itemize}
\item Diamagnets: \(\mu < \mu_0\to\) no unpaired electrons and field is reduced by Lenz's law acting on electron orbits
\item Paramagnets: \(\mu > \mu_0\to\) has some unpaired electrons that align with applied field
\item Ferromagnets: \(\mu \gg \mu_0\to\) has many unpaired electrons and forms magnetic domains within the material
\end{itemize}

\subsection{Electrodynamics}

\subsubsection{Displacement Current and the Amp\`ere-Maxwell Law}
\(\displaystyle I_d=\epsilon_0\frac{\mathrm{d}\Phi_E}{\mathrm{d}t}=\epsilon_0\frac{\mathrm{d}}{\mathrm{d}t}\int_S\mathbf{E}\cdot\,\mathrm{d}\mathbf{a}\)\\*
\(\displaystyle\oint_{\delta S}\mathbf{B}\cdot\,\mathrm{d}\mathbf{l}=\mu_0 (I+I_d)=\mu_0I +\mu_0\epsilon_0\frac{\mathrm{d}\Phi_E}{\mathrm{d}t}\)\\*
\(\displaystyle\nabla\times\mathbf{B}=\mu_0\mathbf{J}+\mu_0\epsilon_0\frac{\partial\mathbf{E}}{\partial t}\)

\subsubsection{Lorentz Force}
\(\mathbf{F}_L=q(\mathbf{E}+\mathbf{v}\times\mathbf{B})\)

\subsubsection{Faraday's Law}
\(\displaystyle{\cal{E}}_{induced}=-\frac{\partial\Phi_B}{\partial t}\)\\\\*
\(\displaystyle\nabla\times\mathbf{E}=-\frac{\partial\mathbf{B}}{\partial t}\)\\\\*
\(\displaystyle\oint\mathbf{E}\cdot\mathrm{d}\mathbf{l}=\frac{-\mathrm{d}\Phi_B}{\mathrm{d}t}\)\\\\*
Example: Induced voltage in a rotating conducting bar with \(\vec{\omega}\) parallel to \(\mathbf{B}\)
\begin{eqnarray}
\mathrm{d}{\cal E}&=&Bv\mathrm{d}r \nonumber\\
{\cal E}&=&B\int v\,\mathrm{d}r=\omega B\int_0^l r\,\mathrm{d}r \nonumber\\
{\cal E}&=&\frac{1}{2}\omega Bl^2 \nonumber
\end{eqnarray}

\subsubsection{Lenz's Law}
The induced current in a loop is in the direction that creates a magnetic field that \emph{opposes} the change in magnetic flux through the area enclosed by the loop (the negative sign in Faraday's Law).

\subsection{Maxwell's Equations}

\subsubsection{Without Matter}
\begin{itemize}
\item \(\displaystyle\nabla\cdot\mathbf{E}=\frac{\rho}{\epsilon_0}\)
\item \(\displaystyle\nabla\cdot\mathbf{B}=\vec{0}\)
\item \(\displaystyle\nabla\times\mathbf{E}=-\frac{\partial\mathbf{B}}{\partial t}\)
\item \(\displaystyle\nabla\times\mathbf{B}=\mu_0\mathbf{J}+\mu_0\epsilon_0\frac{\partial\mathbf{E}}{\partial t}\)
\end{itemize}

\subsubsection{With Matter}
\begin{itemize}
\item \(\displaystyle\nabla\cdot\mathbf{D}=\rho_{free}\)
\item \(\displaystyle\nabla\cdot\mathbf{B}=\vec{0}\)
\item \(\displaystyle\nabla\times\mathbf{E}=-\frac{\partial\mathbf{B}}{\partial t}\)
\item \(\displaystyle\nabla\times\mathbf{H}=\mathbf{J}+\frac{\partial\mathbf{D}}{\partial t}\)
\end{itemize}

\subsubsection{Conservation of Charge}
If you take the divergence of \(\displaystyle\nabla\times\mathbf{B}=\mu_0\mathbf{J}+\mu_0\epsilon_0\frac{\partial\mathbf{E}}{\partial t}\) you'll get:\\*
\(\displaystyle 0=\mu_0\nabla\cdot\mathbf{J}+\mu_0\epsilon_0\frac{\partial}{\partial t}\nabla\cdot\mathbf{E}\)\\*
After substituting in \(\displaystyle\nabla\cdot\mathbf{E}=\frac{\rho}{\epsilon_0}\), you get the differential expression for the conservation of charge:\\\\*
\(\displaystyle \nabla\cdot\mathbf{J}=-\frac{\partial\rho}{\partial t}\)

\subsubsection{Boundary Conditions}
These are the boundary conditions for the fields in medium 1 and 2\\\\*
General Boundary Conditions:
\begin{itemize}
\item \(D_{2}^{\bot}-D_{1}^{\bot}=\sigma_{free}\)
\item \(B_{2}^{\bot}-B_{1}^{\bot}=0\)
\item \(\mathbf{E}_{2}^{\parallel}-\mathbf{E}_{1}^{\parallel}=\mathbf{0}\)
\item \(\mathbf{H}_{2}^{\parallel}-\mathbf{H}_{1}^{\parallel}=\mathbf{K}_{free}\times\hat{\mathbf{n}}\)
\end{itemize}
Boundary Conditions for Linear Media:
\begin{itemize}
\item \(\epsilon_2E_{2}^{\bot}-\epsilon_1E_{1}^{\bot}=\sigma_{free}\)
\item \(B_{2}^{\bot}-B_{1}^{\bot}=0\)
\item \(\mathbf{E}_{2}^{\parallel}-\mathbf{E}_{1}^{\parallel}=\mathbf{0}\)
\item \(\displaystyle\frac{1}{\mu_2}\mathbf{B}_{2}^{\parallel}-\frac{1}{\mu_1}\mathbf{B}_{1}^{\parallel}=\mathbf{K}_{free}\times\hat{\mathbf{n}}\)
\end{itemize}

These allow you to calculate the induced surface charge when using separation of variables: \(\displaystyle \epsilon_{above}\left(\frac{\partial V}{\partial n}\right)_{above}-\epsilon_{below}\left(\frac{\partial V}{\partial n}\right)_{below}=\sigma_{free}\)

\subsubsection{Field Energy}
Energy Density in Electric Field: \(\displaystyle u_E=\frac{1}{2}\epsilon_0E^2\)\\*
Energy Density in Magnetic Field: \(\displaystyle u_B=\frac{B^2}{2\mu_0}\)\\\\*
Total Energy Density in the Fields: \(\displaystyle u_{em}=\frac{1}{2}\left(\epsilon_0E^2+\frac{B^2}{\mu_0}\right)\)\\\\*
Total Energy  in the Fields: \(\displaystyle U_{em}=\frac{1}{2}\int\left(\epsilon_0E^2+\frac{B^2}{\mu_0}\right)\mathrm{d}v\)

\subsection{Circuits}

\subsubsection{Kirchhoff's Rules}
\begin{itemize}
\item Junction Rule: \(\displaystyle\sum I_{in}=\sum I_{out}\)
\item Loop Rule: \(\displaystyle\sum_{closed loop}\Delta V=0\)
\end{itemize}

\subsubsection{Resistors}
Resistance: \(\displaystyle R=\rho\frac{l}{A}\)\\\\*
Conductivity: \(\displaystyle\sigma = \frac{1}{\rho}\)\\\\*
Ohm's Law: \(\mathbf{J}=\sigma\mathbf{E}\to V=IR\)\\\\*
\(\displaystyle R_{series}=\sum_i R_i\)\\*
\(\displaystyle\frac{1}{R_{parallel}}=\sum_i \frac{1}{R_i}\)

\subsubsection{Capacitors}
Capacitance: \(\displaystyle C=\frac{Q}{V}\)\\*
Parallel Plate Capacitor: \(V=E_{both plates}d=\displaystyle\frac{Qd}{\epsilon_0 A} \to C=\frac{\epsilon_0 A}{d}\) where \(A\) is the area of one plate and \(d\) is the distance between the plates\\*
Energy in a Capacitor: \(U_C=\frac{1}{2}CV^2=\frac{1}{2}\frac{Q^2}{C}=\frac{1}{2}QV\)\\*
\(\displaystyle C_{parallel}=\sum_i C_i\)\\*
\(\displaystyle\frac{1}{C_{series}}=\sum_i \frac{1}{C_i}\)\\*
Capacitor with Dielectric: \(C=\kappa C_0\)\\*
E-Field Inside Capacitor with Dielectric: \(\displaystyle\mathbf{E}=\frac{\mathbf{E}_0}{\kappa}\to\) \(\mathbf{E}<\mathbf{E}_0\)

\subsubsection{Inductors}
Self-Inductance: \(\displaystyle{\cal E}_L=-\frac{\partial\Phi_B}{\partial t}=-L\frac{\mathrm{d} I}{\mathrm{d} t}\)\\*
Inductance: \(\displaystyle L=-\frac{{\cal E}_L}{{\mathrm{d} I}{/\mathrm{d} t}}\)\\\\*
Solenoid: \(\displaystyle L=\frac{N\Phi_B}{I}=\frac{\mu_0 N^2 A}{l}\)\\\\*
Energy in an Inductor: \(U=\frac{1}{2}LI^2\)\\\\*
Shortcut to Calculating \(L\): \(\displaystyle \frac{1}{2}LI^2=\frac{1}{2\mu_0}\int B^2\mathrm{d}v'\)\\\\*
Mutual Inductance: \(\displaystyle M=M_{12}=\frac{N_2\Phi_{12}}{I_1}=M_{21}=\frac{N_1\Phi_{21}}{I_2}\)\\*
\(\displaystyle{\cal E}_1=-M\frac{\mathrm{d}I_2}{\mathrm{d}t}\) and \(\displaystyle{\cal E}_2=-M\frac{\mathrm{d}I_1}{\mathrm{d}t}\)

\subsubsection{Power}
Power \emph{Delivered to} a Capacitor/Inductor: \(P=IV\)\\*
Power \emph{Dissipated by} a Resistor: \(P=I^2R\)

\subsubsection{DC Circuits}

\paragraph{RC Circuits:}
Time Constant: \(\tau=RC\)\\\\*
Charging: \(\displaystyle{\cal E} - \frac{q}{C}-R\frac{\mathrm{d}q}{\mathrm{d}t}=0\)
\begin{itemize}
\item \(\displaystyle q(t)={\cal E}C\left(1-e^{-t/\tau}\right)\)
\item \(\displaystyle I(t)=\frac{{\cal E}}{R}e^{-t/\tau}\)
\end{itemize}
Discharging: \(\displaystyle \frac{q}{C}+R\frac{\mathrm{d}q}{\mathrm{d}t}=0\)
\begin{itemize}
\item \(\displaystyle q(t)=q_0e^{-t/\tau}\)
\item \(\displaystyle I(t)=-\frac{q_0}{RC}e^{-t/\tau}\)
\end{itemize}

\paragraph{RL Circuits:}
Time Constant: \(\displaystyle\tau=\frac{L}{R}\)\\\\*
With Driving Voltage: \(\displaystyle{\cal E} - IR-L\frac{\mathrm{d}I}{\mathrm{d}t}=0\)
\begin{itemize}
\item \(\displaystyle I(t)=\frac{{\cal E}}{R}\left(1-e^{-t/\tau}\right)\)
\end{itemize}
Without Driving Voltage: \(\displaystyle IR+L\frac{\mathrm{d}I}{\mathrm{d}t}=0\)
\begin{itemize}
\item \(\displaystyle I(t)=I_0e^{-t/\tau}\)
\end{itemize}

\paragraph{LC Circuits:}
Angular Frequency: \(\displaystyle\omega_{0}=\frac{1}{\sqrt{LC}}\)\\\\*
With Charged Capacitor: \(\displaystyle \frac{q}{C}+L\frac{\mathrm{d}^2q}{\mathrm{d}t^2}=0\)
\begin{itemize}
\item \(\displaystyle q(t)=q_{max}\cos(\omega_{0}t+\phi)\)
\end{itemize}
This is just a SHO.

\paragraph{LRC Circuits:}
Angular Frequency: \(\displaystyle\omega_{d}=\sqrt{\omega_0^2-\left(\frac{R}{2L}\right)^2}\)\\\\*
Without Driving Voltage: \(\displaystyle \frac{q}{C}+R\frac{\mathrm{d}q}{\mathrm{d}t}+L\frac{\mathrm{d}^2q}{\mathrm{d}t^2}=0\)
\begin{itemize}
\item When \(R\) is small: \(\displaystyle q(t)=q_{max}e^{-Rt/2L}\cos(\omega_{d}t)\)
\end{itemize}
Critically damped at \(R_c=\frac{\sqrt{4L}}{C}\)\\*
This is just a damped harmonic oscillator.

\subsubsection{AC Circuits}
Driving Voltage: \(V(t)=V_{max}\sin(\omega t)\)\\\\*
Current and voltage across a resistor are in phase.\\*
Current lags behind voltage by \(90^{\circ}\) in an inductor.\\*
Current leads voltage by \(90^{\circ}\) in a capacitor.\\\\*
Transformer: \(\displaystyle V_2=\frac{N_2}{N_1}V_1\)\\*
\(\displaystyle P_1=P_2\to R_{eq}=\left(\frac{N_1}{N_2}\right)^2R_L\)\\\\*
Reactance:
\begin{itemize}
\item Inductive reactance: \(X_L=\omega L\)
\item Capacitive reactance: \(\displaystyle X_C=\frac{1}{\omega C}\)
\end{itemize}
\paragraph{LRC Circuits:}
Resonant Angular Frequency: \(\displaystyle\omega_{0}=\frac{1}{\sqrt{LC}}\)\\\\*
Driving Current: \(I(t)=I_{max}\sin(\omega t-\phi)\)\\*
\(\phi =\mathrm{tan}^{-1}\left(\frac{X_L-X_C}{R}\right)\)\\\\*
Voltage Across
\begin{itemize}
\item R: \(v_R=I_{max}R\sin(\omega t)\)
\item L: \(v_L=I_{max}X_L\sin\left(\omega t-\frac{\pi}{2}\right)\)
\item C: \(v_C=I_{max}X_C\sin\left(\omega t+\frac{\pi}{2}\right)\)
\end{itemize}
Impedance: \(Z=\sqrt{R^2+(X_L-X_C)^2}\)\\*
\(V_{max}=I_{max}Z\)\\\\*
Impedance Matching: \(Z_{source}=Z_{load}^*\) for maximum power transfer\\\\*
This is just a forced-damped harmonic oscillator.

\subsection{Electromagnetic Waves}

\subsubsection{Wave Equations}
\(\displaystyle\frac{\partial^2E}{\partial x^2}=\frac{1}{c^2}\frac{\partial^2E}{\partial t^2}\)\\\\*
\(\displaystyle\frac{\partial^2B}{\partial x^2}=\frac{1}{c^2}\frac{\partial^2B}{\partial t^2}\)\\*
\(\displaystyle c=\frac{1}{\sqrt{\mu_0\epsilon_0}}\)

\subsubsection{Poynting Theorem}
Poynting Vector: \(\mathbf{S}=\frac{1}{\mu_0}\mathbf{E}\times\mathbf{B}\)\\*
Units are is \(W/m^2\) (same as intensity)\\*
Points in the direction of wave propagation (for transverse waves).\\\\*
Poynting Theorem (\(W\) is work): \(\displaystyle \frac{\mathrm{d}W}{\mathrm{d}t}=-\frac{\mathrm{d}U_{em}}{\mathrm{d}t}-\oint_{\delta V}\mathbf{S}\cdot\mathrm{d}\mathbf{a}\)\\\\*
For plane waves: \(\displaystyle I=S_{av}=\frac{1}{2}\epsilon_0E_{max}^2=\frac{B_{max}^2}{2\mu_0}\)

\subsubsection{Radiation Pressure}
Perfect Absorber: \(\displaystyle P_A=\frac{S\cos^2(\theta)}{c}\)\\*
Perfect Reflector: \(\displaystyle P_R=2P_A=\frac{2S\cos^2(\theta)}{c}\)\\*
\(\theta\) is measured from the normal of the surface.

\subsubsection{Power Radiated from an Accelerating Charge}
Larmor Formula: \(\displaystyle P=\frac{q^2a^2}{6\pi\epsilon_0c^3}=\frac{\mu_0q^2a^2}{6\pi c}\)

\subsubsection{Radiation Reaction Force}
\(\displaystyle\mathbf{F}_{rad}=\frac{\mu_0q^2}{6\pi c}\dot{\mathbf{a}}\)

\subsubsection{Power Radiated from an Oscillating Charge}
\begin{itemize}
\item Never radiates in the direction of its oscillation axis
\item Polarization is parallel to the oscillation axis
\end{itemize}
Intensity: \(\displaystyle I\propto\frac{\sin^2(\theta)}{r^2}\)\\*
\(\theta\) is measured from the axis of oscillation.

\subsubsection{Dipole Radiation}
\(\displaystyle\mathbf{B}=\frac{-\mu_0}{4\pi cr}[\hat{\mathbf{r}}\times\ddot{\mathbf{p}}]\)\\\\*
\(\displaystyle\mathbf{E}=-c\hat{\mathbf{r}}\times\mathbf{B}\)\\\\*
Power radiated from an electric dipole: \(\displaystyle P_p=\frac{\mu_0\ddot{p}^2}{6\pi c}\)\\\\*
Power radiated from a magnetic dipole: \(\displaystyle P_m=\frac{\mu_0\ddot{m}^2}{6\pi c^3}\)

\subsubsection{Cherenkov Radiation}
Radiation emitted when a charged particle passes through an insulator at a speed greater than the speed of light in that material\\*
It is due to the charged particles polarizing the molecules of the material, which then fall back rapidly to their ground state, emitting radiation in the process.
The spectrum is continuous, and its intensity is proportional to the frequency of the photon.
There is also a high frequency cutoff.

\newpage
\section{Optics \& Wave Phenomena}
This section on optics and waves covers material that is taught in a freshman level physics course on electricity and magnetism.
The PGRE requires very little advanced knowledge on this topic.
However, this is an important section to study thoroughly as there are many optics questions on the test that are easily solvable in less than sixty seconds.
Know how to rapidly draw ray diagrams and find the focal point and image for mirrors and lenses.
There is typically as least one question over telescopes as well.
For wave phenomena, always keep in mind a wave on a string as it is conceptually similar to reflection and refraction.

\subsection{General Information}

\subsubsection{Group and Phase Velocity}
\(\displaystyle v_{phase}=\frac{\omega}{k}\)\\\\*
\(\displaystyle v_{group}=\frac{\mathrm{d}\omega}{\mathrm{d}k}\)

\subsubsection{Huygen's Principle}
All points on a given wave front are taken as point sources for the production of spherical secondary waves, called wavelets, which propagate outward through a medium with speeds characteristic of waves in that medium.
After some time interval has passed, the new position of the wave front is the surface tangent to the wavelets.

\subsubsection{Fermat's Principle}
When a light ray travels between any two points, its path is the one that requires the smallest time interval.

\subsubsection{Images}
A real image is formed when light rays pass through and diverge from the image point.\\*
A virtual image is formed when the light rays \emph{do not} pass through the image point but only appear to diverge from that point.\\\\*
This equation is useful for both \emph{thin} lenses and mirrors.\\\\*
\(\displaystyle \frac{1}{s_o}+\frac{1}{s_i}=\frac{1}{f}\) where  \(s_i\) is the distance from the image to the reflecting/refracting surface, \(s_o\) is the distance from the object to the reflecting/refracting surface, and \(f\) is the focal length of the lenses/mirror.

\subsubsection{Magnification}
Lateral Magnification: \(\displaystyle M=\frac{h_{i}}{h_{o}}=-\frac{s_{i}}{s_{o}}\) where \(h_i\) is the image height and \(h_o\) is the object height.\\\\*
Angular Magnification: \(\displaystyle m=\frac{\theta}{\theta_0}\) where \(\theta_0\) is defined by \(\displaystyle\tan(\theta_0)=\frac{h_o}{.25m}\) and \(\theta\) is defined by \(\displaystyle\tan(\theta)=\frac{h_i}{s_i}\)

\subsubsection{Telescope}
A refracting telescope is an array of two converging lenses placed far enough apart so their focal points are at the same location.
The first lens is a weak ``objective'' lens while the second lens is a powerful ``eyepiece'' lens.
The total magnification of this array is \(\displaystyle m=-\frac{f_o}{f_e}\) where \(f_o\) is the focal length of the objective lens and \(f_e\) is the focal length of the eyepiece.

\subsubsection{Aberrations}
Spherical: results from focal point not being the same for rays incident at different positions (affects both mirrors and lenses)\\*
Chromatic: results from the dispersion of light within lenses causing different focal points for different wavelengths of light (only affects lenses)

\subsection{Reflection}
Specular reflection is due to a relatively smooth surface (compared to the wavelength of light).
This is the type of reflection from an ideal mirror (typically this is the type of reflection is simply called reflection).\\*
Diffuse reflection is due to a relatively rough surface and causes material to scatter light in all directions.\\\\*
Retroreflector: A reflector that ``always'' reflects light back to its source. Examples shapes are tiny refractive spheres and the ``inside corner'' of a reflective cube.\\\\*
In general for mirrors: \(\displaystyle f=\frac{R}{2}\)

\subsubsection{Flat Mirrors}
\begin{center}
\includegraphics[scale=0.6]{images/PGRE_Figures_3p2p1_Flat_Mirror.png}
\end{center}
\(R=\infty\)
\begin{center}
  \begin{tabular}{ c | c  }
    Object Placement & Image \\ \hline%\cline{2-2}
    \multirow{3}{*}{ anywhere} & virtual \\
    & \(M=1\) \\
    & \(s_o=-s_i\) \\
    \hline
  \end{tabular}
\end{center}
Two \(\perp\) flat mirrors produce three virtual images (practice drawing the ray diagram).

\subsubsection{Concave Mirrors}
\begin{center}
\includegraphics[scale=0.6]{images/PGRE_Figures_3p2p2_Concave_Mirror.png}
\end{center}
Using the convention of this document: \(R>0\)
\begin{center}
  \begin{tabular}{ c | c  }
    Object Placement & Image \\ \hline%\cline{2-2}
    \multirow{3}{*}{ \(s_o<f\)} & virtual \\
    & upright \\
    & \(M>1\) \\ \hline
    \(s_o=f\) & no image \\ \hline
    \multirow{2}{*}{ \(f<s_o<R\)} & real \\
    & \(M<-1\) \\ \hline
    \multirow{2}{*}{  \(s_o=R\)} & real \\
    & \(M=-1\) \\ \hline
     \multirow{2}{*}{  \(s_o>R\)} & real \\
    & \(-1<M<0\) \\
    \hline
  \end{tabular}
\end{center}

\subsubsection{Convex Mirrors}
\begin{center}
\includegraphics[scale=0.6]{images/PGRE_Figures_3p2p3_Convex_Mirror.png}
\end{center}
Using the convention of this document: \(R<0\)
\begin{center}
  \begin{tabular}{ c | c  }
    Object Placement & Image \\ \hline%\cline{2-2}
    \multirow{2}{*}{ anywhere} & virtual \\
    & \(0<M<1\) \\
    \hline
  \end{tabular}
\end{center}

\subsection{Refraction}
Index of Refraction: \(\displaystyle n=\frac{c}{v}\) where \(v\) is the speed of light in the material (\(n>1\) always)\\*
When light travels from one medium to another, the frequency and energy stay constant (but speed and wavelength change).\\*
Dispersion: \(n=n(\lambda)\) (the index of refraction depends on the wavelength of light)\\\\*
Images from Refraction: \(\displaystyle \frac{n_1}{s_o}+\frac{n_2}{s_i}=\frac{n_2-n_1}{R}\) (single surface)\\\\*
Thin Lens Equation: \(\displaystyle \frac{1}{f}=(n-1)\left(\frac{1}{R_1}-\frac{1}{R_2}\right)\)\\\\*
Lensmaker's Equation: \(\displaystyle \frac{1}{f}=(n-1)\left(\frac{1}{R_1}-\frac{1}{R_2}+\frac{(n-1)d}{nR_1R_2}\right)\)\\\\*
Sign convention: \(R\) and \(s_i\) are negative if measured from the same side as the object and are positive if measured from the opposite side as the object (opposite of mirrors).\\*
The \(d\) in the Lensmaker's equation is the thickness of the lens. For converging lenses it is the thickest width and for diverging lenses it is the smallest width.\\\\*
For a combination of lenses use the image of the first at the object of the second.\\*
For thin lenses in contact: \(\displaystyle\frac{1}{f}=\frac{1}{f_1}+\frac{1}{f_2}\)

\subsubsection{Snell's Law}
\(n_1\sin(\theta_1)=n_2\sin(\theta_2)\)\\\\*
Critical Angle: \(\displaystyle\theta_2=90^{\circ}\to \sin(\theta_c)=\frac{n_2}{n_1}\)

\subsubsection{Flat Refracting Surface}
\(\displaystyle R=\infty\to s_i=-\frac{n_2}{n_1}s_o\)

\subsubsection{Converging Lenses}
\(f>0\)\\*
A converging lens is thicker in the middle and thin at the ends.

\subsubsection{Diverging Lenses}
\(f<0\)\\*
A diverging lens is thinner in the middle and thick at the ends.

\subsection{Interference \& Diffraction}

\subsubsection{ Double-Slit Interference}
Sources must be coherent and monochromatic\\*
Screen must be far from the slits\\*
Bright Fringes: \(d\sin(\theta_{bright})=m\lambda\), \(m=0,\pm1,\pm2,\ldots\)\\*
Dark Fringes: \(d\sin(\theta_{dark})=\left(m+\frac{1}{2}\right)\lambda\)\\\\*
To get the position on the screen use \(\displaystyle\sin(\theta)\approx\frac{y}{L}\)

\subsubsection{Thin Films}
An electromagnetic wave undergoes a phase change of \(\pi\) upon reflection from a medium that has a higher index of refraction than the one in which the wave is traveling (wave on a string attached to a wall).\\\\*
This is for near normal incidence. In what follows \(m=0,1,2,\ldots\)\\*
Case: \(n_1<n_{film}<n_3\)\\*
Constructive Interference when: \(2n_{film}t=m\lambda\)\\*
Destructive Interference when: \(2n_{film}t=\left(m+\frac{1}{2}\right)\lambda\)\\*
Case: \(n_1<n_{film}>n_3\)\\*
Constructive Interference when: \(2n_{film}t=\left(m+\frac{1}{2}\right)\lambda\)\\*
Destructive Interference when: \(2n_{film}t=m\lambda\)\\*

\subsubsection{Single-Slit Diffraction}
Screen must be far from the slit\\*
Dark Fringes: \(a\sin(\theta_{dark})=m\lambda\), \(m=\pm1,\pm2,\pm3,\ldots\)

\subsubsection{Rayleigh Criterion}
This comes from the first order diffraction minimum.\\*
For Slit: \(\displaystyle\sin(\theta)=\frac{\lambda}{d}\)\\*
For Circular Aperture:  \(\displaystyle\sin(\theta)=1.22\frac{\lambda}{d}\)

\subsubsection{Diffraction (Interference) Grating}
Same as interference.\\*
Bright Fringes: \(d\sin(\theta_{bright})=m\lambda\), \(m=0,\pm1,\pm2,\ldots\)

\subsubsection{Bragg's Law}
This works for massive particles and photons (both have wave-like properties).\\*
Bright Fringes: \(2d\sin(\theta_{bright})=m\lambda\), \(m=1,2,3,\ldots\)

\subsection{Polarization}

\subsubsection{Polarizers}
If unpolarized light passes through a polarizer, the beam's intensity is halved.
If (plane) polarized light passes through a polarizer at a relative angle \(\phi-\theta\), the intensity is given by \(I_2=I_1\cos^2(\phi-\theta)\)

\subsubsection{Brewster's Law}
\begin{center}
\includegraphics[scale=0.6]{images/PGRE_Figures_3p5p2_Brewsters_Law.png}
\end{center}
A reflected beam can be completely polarized if \(\tan(\theta_p)=n\)

\subsection{Rayleigh Scattering}
This type of scattering occurs when light elastically scatters off particles much smaller than the wavelength of the light.
Quantitatively, the intensity of the light that is scatter is related the wavelength of light by \(\displaystyle I\propto\frac{1}{\lambda^4}\).
This is the reason the sky looks blue and the sun appears yellow through Earth's atmosphere.


%% Dominic's material
%\chapter{Bootstrap}
%WORK fuse with earlier material
%\comment{

\section{Empirical distribution function}
\work

%\subsection{\define}
Let $x_1,\ldots,x_n$ be a random sample of size $n$. The {\it empirical distribution function} (EDF) of $x_1,\ldots,x_n$ is, for any real number $t$:
\begin{equation}
\hat{F}_n(t)=\frac{|\{x_i:x_i\leq t\}|}{n}=\frac{1}{n}\sum^n_{i=1}I_{(-\infty,t]}(x_i),
\end{equation}

i.e. the proportion of sample points that are less than or equal to $t$.
%\begin{flushright}   $\boxbox$ \end{flushright}

Note that the EDF takes values between 0 and 1. Note also that if the sample comes from a continuous distribution, then the EDF takes a step of $1/n$ at each sample value; if the sample comes from a discrete distribution, the EDF may take steps that are multiples of $1/n$ at distinct sample values.

%\subsection{\define}
Let $x_1^*,\ldots,x_n^*$ be the distinct points in $x_1,\ldots,x_n$, so that $m \leq n$, and let:
\begin{equation}
c_=|\{x_i:x_i=x_j^* \}|=\sum^n_{i=1}I_{(x_j^*)}(x_i),
\end{equation}
i.e. $c_j$ is the number of sample values that are equal to $x^*_j$. The EDF can also be regarded as a discrete distribution that assigns a probability mass of $1/n$ to each of the observations, $x_1,\ldots,x_n$, i.e. with an {\it empirical mass function} (EMF):
\begin{equation}
\hat{f}_n(x)=\frac{1}{n}\sum^m_{j=1}c_jI_{(x^*_j)}(x)=\begin{array}{l}
c_j/n,\textrm{    if }x=x^*_j,\\0,\textrm{    otherwise.}\\
\end{array}
\end{equation}

If the sample values are continuous, then $m = n$ and $c_1=\ldots=c_n=1$.
%\begin{flushright}   $\boxbox$ \end{flushright}

\begin{example}
{\it Continuous data.}
Suppose the observed values of 10 continuous random variables, arranged in increasing order, are:
$$\begin{array}{ccccc}
 1.5937     &   1.4410 &       1.3362&        0.6918 &      0.2944\\
0.5711	   &    0.7143&        0.8580&        1.2540  &      1.6236\\
\end{array}$$

%\includegraphics
%\includegraphics
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{example}

\begin{example}
{\it Discrete data.}
Suppose the observed values of 10 discrete random variables, arranged in increasing order, are
$$\begin{array}{cccccccccc}
1 &    2&     2&     2&     2  &   2&     3&     4  &   5 &    5
\end{array}$$
%\includegraphics
%\includegraphics
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{example}

The EDF is an estimator for the distribution function and can therefore be used as a model for the distribution function. The EDF is a {\it nonparametric} model because its parameters are the sample points, $x_1,\ldots,x_n$, and so the number of parameters increases as the sample size increases. The following two results show why the EDF is a good estimator for the distribution function.

\begin{prop}
For any real number $t$:
\begin{equation}
E[\hat{F}_n(t)]=F(t),
\end{equation}
and:
\begin{equation}
Var[\hat{F}_n(t)]=\frac{F(t)[1-F(t)]}{n},
\end{equation}

and therefore $\hat{F}_n(t)$ is an unbiased and consistent estimator of $F(t)$.

\begin{proof}
Consider a fixed real number $t$. By definition:
$$\hat{F}_n(t)=\frac{1}{n}\sum^n_{i=1}I_{(-\infty,t]}(x_i).$$

The result follows by noting that $I_{(-\infty,t]}(x_i)$ is a Bernoulli random variable with parameter (\textquotedblleft success" probability):
$$P[I_{(\infty,t]}(x_i)=1]=P(x_i\leq t)=F(t),$$
since $x_i$ has distribution function $F$.
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{proof}
\end{prop}

\begin{prop}[Glivenko-Cantelli theorem]
For any real number $t$, $\hat{F}_n(t)$ converges almost certainly (i.e. with a probability of 1) and uniformly to $F(t)$, i.e.:
\begin{equation}
P(\lim_{n\rightarrow\infty}\sup_t|\hat{F}_n(t)-F(t)|=0)=1.
\end{equation}
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{prop}

The Glivenko-Cantelli theorem says that the largest absolute difference between $\hat{F}_x$ and $F$ converges to 0 as $n$ goes to infinity, with a probability of 1.

The next result allows us to construct a confidence band for the EDF.

\begin{prop}[Dvoretzky-Kiefer-Wolfowitz inequality]
For any  $\epsilon > 0$:
\begin{equation}
P(\sup_t|\hat{F}_n(t)-F(t)|>\epsilon)\leq 2 \exp(-2n\epsilon^2).
\end{equation}
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{prop}

For $\alpha\in (0, 1), a (1-\alpha)$ confidence band for $F$ should contain $F$ with probability of at least $(1-\alpha)$. In other words, the probability of $F$ being outside the band is at most $\alpha$. Hence, the bound in the Dvoretzky-Kiefer-Wolfowitz inequality should be $\alpha$:
\begin{equation}
2\exp(-2n\epsilon^2)=\alpha\Leftrightarrow \epsilon=\sqrt{\frac{1}{2n}\ln(\frac{2}{\alpha})},
\end{equation}
i.e.:
$$P(\sup_t|\hat{F}_n(t)-F(t)|>\sqrt{\frac{1}{2n}\ln(\frac{2}{\alpha})})\leq\alpha,$$
or, for all $t$:
$$P(\max\{\hat{F}_n(t)-\sqrt{\frac{1}{2n}\ln(\frac{2}{\alpha})},0\}\leq F(t)\leq\min\{\hat{F}_n(t)+\sqrt{\frac{1}{2n}\ln(\frac{2}{\alpha})},1\}).$$
Therefore, a $(1-\alpha )$ confidence band for $F$ is:
\begin{equation}
[\max\{\hat{F}_n(t)-\sqrt{\frac{1}{2n}\ln(\frac{2}{\alpha})},0\},\min\{ \hat{F}_n(t)+\sqrt{\frac{1}{2n}\ln(\frac{2}{\alpha})} \}].
\end{equation}

\begin{example}
Referring to Example 4.1.3 where $n = 10$, a 0.95 confidence band for $F$ is given by:
\begin{displaymath}\begin{split}
&[\max\{\hat{F}_10(t)-\sqrt{\frac{\ln 40}{20}},0\},\min\{\hat{F}_10(t)+\sqrt{\frac{\ln 40}{20}}\}]\\
=&[\max\{\hat{F}_10(t)-0.4295,0\},\min\{\hat{F}_10(t)+0.4295\}]\\
\end{split}\end{displaymath}
The function edfplot, which is available from the course web-page, produced the figure below:
%\includegraphics
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{example}

\section{Nonparametric bootstrap}
\work
Let $x_1,\ldots,x_n$ be a random sample and suppose that we wish to estimate an unknown quantity, $\theta$, using an estimator $\hat{\theta}$ that is based on $x_1,\ldots,x_n$. The performance of $\hat{\theta}$ as an estimator for $\theta$ can be assessed by its {\it bias, variance} and {\it mean squared error}.

%\subsection{{\it Definitions}}
\begin{asparaenum}[(a)]
\item The {\it bias} of $\hat{\theta}$ is:
\begin{equation}
Bias(\hat{\theta})=E(\hat{\theta})-\theta.
\end{equation}

\item The {\it variance} of $\hat{\theta}$ is:
\begin{equation}
Var(\hat{\theta})=E[(\hat{\theta}-E(\hat{\theta}))^2]=E(\hat{\theta}^2)-E(\hat{\theta})^2.
\end{equation}

\item The {\it mean squared error (MSE)} of $\hat{\theta}$ is:
\begin{equation}
MSE(\hat{\theta})=E[(\hat{\theta}-\theta)^2]=Var(\hat{\theta})+Bias(\hat{\theta})^2.
\end{equation}
\end{asparaenum}
%\begin{flushright}   $\boxbox$ \end{flushright}
When the distribution of $x_1,\ldots,x_n$ is known, one way to estimate the MSE of $\hat{\theta}$ is to use Monte Carlo simulation to generate $N$ new random samples, each of size $n$, from which $N$ new estimates of $\theta$ can be obtained:
\begin{displaymath}
\begin{split}
\textrm{original sample :}\{x_1,\ldots,x_n\}&\rightarrow\hat{\theta}\\
\textrm{generated samples :}\{x_{1,1},\ldots&,x_{1,n}\}\rightarrow\hat{\theta}_1\\
M&\\
\{x_{N,1},\ldots&,x_{N,n}\}\rightarrow\hat{\theta}_N.\\
\end{split}
\end{displaymath}
Here, $x_{j,i}$ denotes the $i^{\textrm{th}}$ value in the $j^{\textrm{th}}$ sample. An estimate of $MSE(\hat{\theta})$ is given by:
\begin{equation}
MES(\hat{\theta})\approx\frac{1}{N}\sum^N_{j=1}(\hat{\theta}_j-\hat{\theta})^2.
\end{equation}

Furthermore, if $N$ is large, an approximate $(1-\alpha)$ confidence interval for $\theta$ is given by $(\hat{\theta}_{(\lceil N\alpha/2\rceil)},\hat{\theta}_{(\lceil N(1-\alpha/2)\rceil)})$. For example, if $N = 1000$, then an approximate 0.95 confidence interval is $(\hat{\theta}_{(25)},\hat{\theta}_{(975)})$.

If the distribution of $x_1,\ldots,x_n$ is unknown, the idea behind the {\it nonparametric bootstrap} is to use the EDF as an estimate of $F$, and then perform Monte Carlo simulation with $\hat{F}_n$ to estimate the MSE and to get approximate confidence intervals.

\subsection{Bootstrap estimates of bias, variance and mean squared error}
\work
Recall that the EDF can be regarded as a discrete distribution that assigns a probability mass of $1/n$ to each of the observations, $x_1,\ldots,x_n$. Thus, using $\hat{F}_n$ as an estimate of $F$, a random sample can be generated from $\hat{F}_n$ by {\it randomly sampling with replacement} from $x_1,\ldots,x_n$. A random sample of size n obtained in this way is called a {\it bootstrap sample}. The MSE of an estimator $\hat{\theta}$ can be obtained as follows:
\begin{asparaenum}[(a)]
\item Compute $\hat{\theta}$ using $x_1,\ldots,x_n$.

\item Obtain $N$ bootstrap samples, each of size $n$, by randomly sampling with replacement from $x_1,\ldots,x_n$: $\{x_{1,1},\ldots,x_{1,n}\},\{x_{2,1},\ldots,x_{2,n}\},\ldots,\{x_{N,1},\ldots,x_{N,n}\}$.

\item	For each bootstrap sample, compute the {\it bootstrap estimate} of $\theta$:
\begin{displaymath}
\begin{split}
\{x_{1,1},\ldots&,x_{1,n}\}\rightarrow\hat{\theta}_1\\
M&\\
\{x_{N,1},\ldots&,x_{N,n}\}\rightarrow\hat{\theta}_N.\\
\end{split}
\end{displaymath}

\item	Compute the mean of the bootstrap estimates:
\begin{equation}
\bar{\hat{\theta}}=\frac{1}{N}\sum^N_{j=1}\hat{\theta}_j.
\end{equation}
\item	Estimate the bias, variance and MSE by:

\begin{equation}
Bias(\hat{\theta})\approx \bar{\hat{\theta}}-\hat{\theta},
\end{equation}
\begin{equation}
Var(\hat{\theta})\approx \frac{1}{N}\sum^N_{j=1}(\hat{\theta}_j-\bar{\hat{\theta}})^2,
MSE(\hat{\theta})\approx \frac{1}{N}\sum^N_{j=1}(\hat{\theta}_j-\bar{\hat{\theta}})^2.
\end{equation}

\end{asparaenum}
%\begin{flushright}   $\boxbox$ \end{flushright}

\begin{labwork}
The table below contains the number, rounded to the nearest thousand, of open-close cycles of 20 door latches before they fail. Find the bias, variance and MSE of the sample mean.

\begin{table}[h]
%\caption{Number of open-close cycles until latch failure (thousands).}
$$\begin{array}{|cccccccccc|}
\multicolumn{10}{|c|}{\textrm{Sample mean} = 38.65}\\\hline
7	&11&	15&	16&	20&	22&	24&	25&	29&	33\\
34	&37	&41	&42	&49	&57	&66	&71	&84	&90\\\hline
\end{array}$$
\end{table}

\begin{table}[h]
%\caption{Bootstrap samples and sample means.}
$$\begin{array}{|c|cccccccccc|c|}
\multicolumn{1}{|c|}{j}&\multicolumn{10}{|c|}{j^{\textrm{th}} \textrm{bootstrap sample}} &\multicolumn{1}{|c|}{ \hat{\theta}_j}\\ \hline
1	&22 & 57 & 42&  16 & 24 & 11 & 20&   7 & 41&  90&\\
&90  &16 & 66 & 25 & 90 & 66 & 25&  24 & 84 & 66	&44.1\\
&&&&&&&&&&&\\
2	&90 & 37 & 15 & 84 & 29 & 57  &57 & 57&  11 & 49&\\
&41&  57 & 84  &71 & 37 & 20 & 29&  84&   7 & 15	&46.6\\
		&&&&&&&&&&&\\
3	&49 & 84&  29 & 41 & 57 & 11 & 49 & 42  &90 & 34&\\
&71&  33&  41 & 84 & 49 & 66 & 20 & 20 & 29 & 15&	45.7\\
&&&&&&&&&&&\\
M&&&&&&M&&&&&M\\
&&&&&&&&&&&\\\hline
\end{array}$$
\end{table}

%\Matlab code:
\begin{VrbM}
x = load('latch.txt') % load data from text file and store in x
n = length(x); % determine number of data values
N = 100000; % number of bootstrap samples
nN = n * N;
xmean = mean(x) % mean of original data
xboot = randsample(x,nN,true); % sample with replacement nN values from x
xboot = reshape(xboot,n,N); % organise resampled values into N columns of n
                                              % values each so that each column is a bootstrap
                                              % sample of size n
xbootmean = mean(xboot); % means of bootstrap samples
bmean = mean(xbootmean); % mean of bootstrap means
bias = bmean - xmean
variance = mean((xbootmean - bmean).^2)
mse = variance + bias * bias
\end{VrbM}
Results:
\begin{VrbM}
xmean = 38.6500
bias = 0.0308
variance = 27.3295
mse = 27.3304
\end{VrbM}
 %\begin{flushright}   $\boxbox$ \end{flushright}
\end{labwork}

\subsection{Percentile interval}
\work
Let $\hat{\theta}_1,\ldots,\hat{\theta}_N$ be the bootstrap estimates of $\theta$ from $N$ bootstrap samples. An approximate $1-\alpha$ confidence interval for $\theta$, known as a $1-\alpha$ percentile interval, is given by $(\hat{\theta}_{(\lceil N\alpha/2\rceil)},\hat{\theta}_{(\lceil N(1-\alpha/2)\rceil)})$.
%\begin{flushright}   $\boxbox$ \end{flushright}

\begin{labwork}
 The sodium contents of single servings from 40 packages of a food product are measured and given in the table below. Find a 0.95 percentile interval for the median amount of sodium in a single serving of this food product.
\begin{table}
%\caption
$$\begin{array}{|c|c|c|c|c|c|c|c|c|c|c|}
\multicolumn{11}{|c|}{\textrm{ \small{Sodium contents (mg) of single servings from 40 packages of a food product.}}}\\
\hline
i	&1&	2	&3&	4	&5&	6&	7&	8&	9&	10\\
x_{(i)}	&72.1&	72.8	&72.9	&73.3&	73.3	&73.3&	73.9	&74.0	&74.2&74.2\\
\hline
\multicolumn{11}{|c|}{ \textrm{ }}\\\hline
i	&11	&12	&13	&14	&15	&16	&17	&18	&19	&20\\
x_{(i)}	&	74.3	&74.6	&74.7	&75.0	&75.1	&75.1	&75.2	&75.3	&75.3	&75.3\\\hline
\multicolumn{11}{|c|}{ \textrm{ }}\\\hline
i	&21	&22	&23	&24	&25	&26	&27&28	&29	&30\\
x_{(i)}		&75.4&	76.1	&76.5	&76.5	&76.6	&76.9&	77.1	&77.2&	77.4&77.4\\\hline
\multicolumn{11}{|c|}{\textrm{ } }\\\hline
i	&31	&32	&33	&34	&35	&36	&37	&38	&39	&40\\
x_{(i)}		&77.7&	78.0	&78.3&	78.6	&78.8	&78.9	&79.7	&80.3&	80.5	&81.0\\\hline
\end{array}$$
\end{table}

\Matlab code:
\begin{VrbM}
x = load('sodium.txt'); % load data from text file and store in x
n = length(x); % determine number of data values
N = 100000; % number of bootstrap samples
nN = n * N;
alpha = 0.05;
alpha2 = alpha / 2;
alpha21 = 1 - alpha2;
xmed = median(x) % median of original data
xboot = randsample(x,nN,true); % sample with replacement nN values from x
xboot = reshape(xboot,n,N); % organise resampled values into N columns of n
                                              % values each so that each column is a bootstrap
                                              % sample of size n
xbootmed = median(xboot); % medians of bootstrap samples
xbootmedsort = sort(xbootmed); % sort medians in increasing order

% (1-alpha) percentile interval:
[xbootmedsort(ceil(N*alpha2)) xbootmedsort(ceil(N*alpha21))]
\end{VrbM}
Results:
\begin{VrbM}
xmed = 75.3500
ans =  75.0500   77.0000
\end{VrbM}
Therefore, a 0.95 percentile interval for the median is $(\hat{\theta}_{(2500)},\hat{\theta}_{(97500)})= (75.05, 77)$.
%\begin{flushright}   $\boxbox$ \end{flushright}
\subsection{{\it Properties of the percentile interval.}}
\work
\begin{asparaenum}[(a)]
\item {\it Transformation-invariant.} Let $g$ be a one-to-one transformation and let $\psi=g(\theta)$. Let $(\hat{\theta}_{(\lceil N\alpha/2\rceil)},\hat{\theta}_{(\lceil N(1-\alpha/2)\rceil)})$ be a $1-\alpha$ percentile interval for $\theta$. If $g$ is increasing, then $(g(\hat{\theta}_{(\lceil N\alpha/2\rceil)}),g(\hat{\theta}_{(\lceil N(1-\alpha/2)\rceil)}))$ is a $1-\alpha$ percentile interval for $\psi$. If $g$ is decreasing, then $(g(\hat{\theta}_{(\lceil N(1-\alpha/2)\rceil)}),g(\hat{\theta}_{(\lceil N\alpha/2\rceil)}))$ is a $1-\alpha$ percentile interval for $\psi$.

\item {\it Range-preserving.} A percentile interval for $\theta$ lies within the range of possible values for $\theta$.

\item {\it First-order accurate.} The error in the coverage probability of a percentile interval goes to zero at rate $1/\sqrt{n}$.

\end{asparaenum}
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{labwork}

\subsection{Bias-corrected and accelerated (BCA) interval}
\work
The BCA interval is an improvement of the percentile interval that corrects for median bias (the difference between $\hat{\theta}$ and the median of $\hat{\theta}_1,\ldots,\hat{\theta}_N$) and has a coverage probability that is closer to $1-\alpha$. Like the percentile interval, the BCA interval's end-points are chosen from the ordered values of $\hat{\theta}_1,\ldots,\hat{\theta}_N$, and so the interval has the form $(\hat{\theta}_{(r)},\hat{\theta}_{(s)})$, where $1   r < s   N$. The variables $r$ and $s$ are chosen to correct for median bias and improve coverage probability.

Let $\Phi$ denote the standard normal distribution function and let $z_p$ be the $p$-quantile of the standard normal distribution. Then:
\begin{equation}
r=round[N\Phi(z_{\hat{b}}+\frac{z_{\hat{b}}+z_{\hat{\alpha/2}}}{1-\hat{\alpha}(z_{\hat{b}}+z_{\hat{\alpha/2}})})],
\end{equation}
and:
\begin{equation}
s=round[N\Phi(z_{\hat{b}}+\frac{z_{\hat{b}}+z_{\hat{\alpha/2}}}{1-\hat{\alpha}(z_{\hat{b}}+z_{\hat{\alpha/2}})})],
\end{equation}

where $\hat{a}$ and $\hat{b}$ are yet to be defined. Before defining them, observe that if $\hat{a}=z_{\hat{b}}=0$, then the BCA interval reduces to the percentile interval.

Now $z_{\hat{b}}$ is a measure of the median bias of the bootstrap estimates, $\hat{\theta}_1,\ldots,\hat{\theta}_N$, and so:
\begin{equation}
\hat{b}=\frac{|\{\hat{\theta}_j:\hat{\theta}_j<\hat{\theta}\}|}{N}=\frac{1}{N}\sum^N_{j=1}I_{(-\infty,\hat{\theta})(\hat{\theta}_j)}.
\end{equation}

If the median of $\hat{\theta}_1,\ldots,\hat{\theta}_N$ coincides with $\hat{\theta}$, then $\hat{b}=0.5$ and $z_{0.5}=0$, and so there is no median bias.
The symbol $\hat{a}$ signifies the acceleration because it measures the rate of change of the standard deviation of $\hat{\theta}$  with respect to $\theta$. If the standard deviation of $\hat{\theta}$ is assumed to be the same for all $\theta$, then $\hat{a}$ is 0; this is often unrealistic, so $\hat{a}$ corrects for this. One way to compute $\hat{a}$ is:
\begin{equation}
\hat{a}=\frac{\sum^N_{j=1}(\bar{\hat{\theta}}-\hat{\theta}_j)^3}{6[\sum^N_{j=1}(\bar{\hat{\theta}}-\hat{\theta}_j)^2]^{3/2}}.
\end{equation}
%\begin{flushright}   $\boxbox$ \end{flushright}

\begin{labwork}
Continuing with the previous example, find a 0.95 BCA interval for the median.
%\Matlab code: (continued)
\begin{VrbM}
% (1-alpha) BCA interval:
b = sum(xbootmed < xmed) / N;
xbootmedmean = mean(xbootmed);
a = sum((xbootmedmean - xbootmed).^3) / (6 * (sum((xbootmedmean -
xbootmed).^2)^1.5));
zb = norminv(b,0,1);
zalpha2 = norminv(alpha2,0,1);
zalpha21 = norminv(alpha21,0,1);
r = round(N * normcdf(zb + ((zb + zalpha2) / (1 - a * (zb + zalpha2))),0,1));
s = round(N * normcdf(zb + ((zb + zalpha21) / (1 - a * (zb + zalpha21))),0,1));
[xbootmedsort(r) xbootmedsort(s)]
\end{VrbM}
Results: (continued)
\begin{VrbM}
r = 1188
s = 95149
ans = 74.8500   76.7500
\end{VrbM}
Therefore, the 0.95 BCA interval for the median is $ (\hat{\theta}_{1188},\hat{\theta}_{95149})= (74.85, 76.75)$.
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{labwork}

\subsection{Properties of the BCA interval}
\work
\begin{asparaenum}[(a)]
\item The BCA interval is transformation-invariant and range-preserving.

\item {\it Second-order accurate.} The error in the coverage probability of a BCA interval tends to zero at rate $1/n$.

\end{asparaenum}
%\begin{flushright}   $\boxbox$ \end{flushright}

\section{Extension to multivariate data and linear regression}
\work
The extension of the nonparametric bootstrap to multivariate data is straightforward. Bootstrap samples are obtained by randomly sampling with replacement from the multivariate data points.

\begin{labwork}
This is an example involving bivariate data. The data in {\tt shoe.txt} are the shoe sizes (column 1) and heights (column 2, in inches) of 24 college-age men.
%\includegraphics
Represent each bivariate data point by $(x_i,y_i)$, where $x_i$ is the shoe size and $y_i$ is the height of the $i^{\textrm{th}}$ man. The correlation between $x$ and $y$ is:
$$\rho=\frac{E\{[x-E(x)][y-E(y)]\}}{\sqrt{E\{[x-E(x)]^2\}E\{[y-E(y)]^2\}}},$$

which can be estimated by the sample correlation:

$$\hat{\rho}=\frac{\sum_{i=1}^{24}(x_i-\bar{x})(y_i-\bar{y})}{\sqrt{[\sum^{24}_{i=1}(x_i-\bar{x})^2][\sum^{24}_{i=1}(y_i-\bar{y})^2]}}$$

where $\bar{x}$ and $\bar{y}$ are the sample means of x and y respectively. Find a 0.95 BCA interval for the correlation.

A bootstrap sample is obtained by randomly sampling with replacement from $(x_1,y_1),\ldots,(x_{24},y_{24})$. Denoting the $j^{\textrm{th}}$ bootstrap sample by $(x_{j,1},y_{j,1}),\ldots,(x_{j,24},y_{j,24})$, the $j^{\textrm{th}}$ bootstrap estimate of the correlation coefficient is:
$$\hat{\rho}_j=\frac{\sum_{i=1}^{24}(x_{j,i}-\bar{x}_j)(y_{j,i}-\bar{y}_j)}{\sqrt{[\sum^{24}_{i=1}(x_{j,i}-\bar{x}_j)^2][\sum^{24}_{i=1}(y_{j,i}-\bar{y}_j)^2]}}.$$

%\Matlab code:
\begin{VrbM}
data = load('shoe.txt'); % load data from text file
x = data(:,1); % store shoe size in x
y = data(:,2); % store height in y
n = length(x); % determine number of data values
N = 100000; % number of bootstrap samples
nN = n * N;
alpha = 0.05;
alpha2 = alpha / 2;
alpha21 = 1 - alpha2;
bootcxy = zeros(1,N); % storage for correlations of bootstrap samples
cxy = corr(x,y) % sample correlation between x and y
iboot = randsample(n,nN,true); % sample with replacement nN values from the
                                                   % indices 1,...,n
iboot = reshape(iboot,n,N); % organise resampled values into N columns of n
                                            % values each so that each column cotains the
                                            % indices for a bootstrap sample of size n
for i = 1:N
    bootcxy(i) = corr(x(iboot(:,i)),y(iboot(:,i))); % correlation of bootstrap sample i
end
bootcxysort = sort(bootcxy); % sort correlations in increasing order

% (1-alpha) BCA interval:
b = sum(bootcxy < cxy) / N;
bootcxymean = mean(bootcxy);
a = sum((bootcxymean - bootcxy).^3) / (6 * (sum((bootcxymean -
bootcxy).^2)^1.5));
zb = norminv(b,0,1);
zalpha2 = norminv(alpha2,0,1);
zalpha21 = norminv(alpha21,0,1);
r = round(N * normcdf(zb + ((zb + zalpha2) / (1 - a * (zb + zalpha2))),0,1))
s = round(N * normcdf(zb + ((zb + zalpha21) / (1 - a * (zb + zalpha21))),0,1))
[bootcxysort(r) bootcxysort(s)]
\end{VrbM}
Results:
\begin{VrbM}
cxy = 0.7176
r = 1596
s = 96198
ans = 0.3647    0.8737
\end{VrbM}
Therefore, the 0.95 BCA interval for the correlation is $ (\hat{\rho}_{(1596)},\hat{\rho}_{(96198)})= (0.3647, 0.8737)$. For comparison, the usual asymptotic 0.95 confidence interval is $(0.4422, 0.8693)$.
%\includegraphics
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{labwork}

\subsection{Confidence intervals for regression coefficients}
\work
For the shoe data, consider a simple linear regression of height on shoe size:
$$y_i=\beta_0+\beta_1x_i+\epsilon_i$$

where the $\epsilon_1,\epsilon_2,\ldots$ are assumed to be IID with mean 0 and variance $\sigma^2$. Let:
$$B=\left( \begin{array}{c}\beta_0\\\beta_1\\\end{array} \right), Y=\left( \begin{array}{c}y_1\\M\\y_{24}\end{array} \right) \textrm{ and } X=\left( \begin{array}{cc}1&x_1\\ M&M\\ 1&x_{24}\\\end{array}\right).$$

The least-squares estimates of the regression coefficients are given by:
$$\hat{B}=(X^TX)^{-1}xTY,$$
which is equivalent to:
$$\hat{\beta}_1=\frac{\sum^n_{i=1}(x_i-\bar{x})(y_i-\bar{y})}{\sum^n_{i=1}(x_i-\bar{x})^2}\textrm{ ,    }\hat{\beta}_0=\bar{y}-\hat{\beta}_1\bar{x}.$$
%\Matlab code:
\begin{VrbM}
data = load('shoe.txt'); % load data from text file
x = data(:,1); % store shoe size in x
y = data(:,2); % store height in y

n = length(x); % determine number of data values
X = [ones(n,1) x]; % predictor matrix
be = regress(y,X) % linear regression of y on x
\end{VrbM}
Results:
\begin{VrbM}
be = 	59.2285
		1.1988
\end{VrbM}
Therefore, the least-squares line is $y = 59.23 + 1.2x $.

To get 0.95 BCA intervals for the regression coefficients, we obtain bootstrap samples of size 24 and recompute the least-squares estimates for each bootstrap sample. As in the previous example, a bootstrap sample is obtained by randomly sampling with replacement from $(x_1,y_1),\ldots,(x_{24},y_{24})$. Denote the $j^{\textrm{th}}$ bootstrap sample by $(x_{j,1},y_{j,1}),\ldots,(x_{j,24},y_{j,24})$ and let:
$$Y_j=\left(\begin{array}{c}y_{j,1}\\M\\y_{j,24}\end{array}\right)\textrm{ and }X_j=\left(\begin{array}{cc}
1&x_{j,1}\\ M&M\\1&x_{j,24}\end{array}\right).$$

Then the $j^{\textrm{th}}$ bootstrap estimates of the regression coefficients are given by:
$$\hat{B}_j=(X^T_jX_j)^{-1}X^T_jY_j.$$
\Matlab code: (continued)
\begin{VrbM}
N = 100000; % number of bootstrap samples
nN = n * N;
alpha = 0.05;
alpha2 = alpha / 2;
alpha21 = 1 - alpha2;
bootbe = zeros(2,N);
iboot = randsample(n,nN,true); % sample with replacement nN values from the
                                                  % indices 1,...,n
iboot = reshape(iboot,n,N); % organise resampled values into N columns of n
                                            % values each so that each column cotains the
                                            % indices for a bootstrap sample of size n
for i = 1:N
    % regression for bootstrap sample i:
    bootbe(:,i) = regress(y(iboot(:,i)),[ones(n,1) x(iboot(:,i))]);
end
bootbesort = sort(bootbe,2); % sort regression coefficients in increasing order

% (1-alpha) BCA interval for be0:
b = sum(bootbe(1,:) < be(1)) / N;
bootbe0mean = mean(bootbe(1,:));
a = sum((bootbe0mean - bootbe(1,:)).^3) / (6 * (sum((bootbe0mean -
bootbe(1,:)).^2)^1.5));
zb = norminv(b,0,1);
zalpha2 = norminv(alpha2,0,1);
zalpha21 = norminv(alpha21,0,1);
r = round(N * normcdf(zb + ((zb + zalpha2) / (1 - a * (zb + zalpha2))),0,1))
s = round(N * normcdf(zb + ((zb + zalpha21) / (1 - a * (zb + zalpha21))),0,1))
[bootbesort(1,r) bootbesort(1,s)]

% (1-alpha) BCA interval for be1:
b = sum(bootbe(2,:) < be(2)) / N;
bootbe1mean = mean(bootbe(2,:));
a = sum((bootbe1mean - bootbe(2,:)).^3) / (6 * (sum((bootbe1mean -
bootbe(2,:)).^2)^1.5));
zb = norminv(b,0,1);
zalpha2 = norminv(alpha2,0,1);
zalpha21 = norminv(alpha21,0,1);
r = round(N * normcdf(zb + ((zb + zalpha2) / (1 - a * (zb + zalpha2))),0,1))
s = round(N * normcdf(zb + ((zb + zalpha21) / (1 - a * (zb + zalpha21))),0,1))
[bootbesort(2,r) bootbesort(2,s)]
\end{VrbM}
Results: (continued)
\begin{VrbM}
r = 2489
s = 97489
ans = 55.3841   63.9200

r = 2407
s = 97404
ans = 0.6913    1.5887
\end{VrbM}
Therefore, the 0.95 BCA interval for $\beta_0$ is $(\hat{\beta}_{0,(2489)},\hat{\beta}_{0,(97489)}) = (55.38, 63.92)$ and for $\beta_1$ is $(\hat{\beta}_{1,(2407)},\hat{\beta}_{1,(97404)}) = (0.6913, 1.5887)$.
%\includegraphics

\subsection{Alternative bootstrap method for regression}
\work
 Consider simple linear regression of response $y$ on predictor $x$ with $(x_1,y_1),\ldots,(x_n,y_n)$, where the predictors $x_1,\ldots,x_n$ are fixed and deterministic. As in the previous example:
$$y_i=\beta_0+\beta_1x_i+\epsilon_i,$$

where the $\epsilon_1,\epsilon_2,\ldots$ are assumed to be IID with mean 0 and variance $\sigma^2$. Using the same notations as before, the least-squares estimates of the regression coefficients are given by:
$$\left(\begin{array}{c}\hat{\beta}_0\\\hat{\beta}_1\end{array}\right)=(X^TX)^{-1}X^TY.$$

For $i = 1,\ldots,n$, compute the residuals:
$$\hat{\epsilon}_i=y_i-\hat{\beta}_0+\hat{\beta}_1x_i,$$

and then the centred residuals:
$$\hat{\epsilon}^*_i=\hat{\epsilon}_i-\bar{\hat{\epsilon}},$$
where $\bar{\hat{\epsilon}}$ is the sample mean of $\hat{\epsilon}_1,\ldots,\hat{\epsilon}_n$.

The $j^{\textrm{th}}$ bootstrap sample and bootstrap regression coefficients are obtained as follows:
\begin{asparaenum}[(a)]
\item Randomly sample with replacement from $\hat{\epsilon}_1^*,\ldots,\hat{\epsilon}_n^*$ to get $\hat{\epsilon}_{j,1},\ldots,\hat{\epsilon}_{j,n}$.

\item	For $i = 1,\ldots,n$, obtain bootstrap responses by:
$$y_{j,i}=\hat{\beta}_0+\hat{\beta}_1x_i+\hat{\epsilon}_{j,i}.$$

The bootstrap sample is $(x_1,y_{j,1}),\ldots,(x_n,y_{j,n})$.

\item	The bootstrap regression coefficients are given by:
$$\left(\begin{array}{c}\hat{\beta}_{j,0}\\\hat{\beta}_{j,1}\end{array}\right)=(X^TX)^{-1}X^TY_j.$$
\end{asparaenum}

\begin{labwork}
Referring to Example 4.3.2 for the shoe data, obtain 0.95 BCA intervals for the regression coefficients by bootstrapping residuals.

%\Matlab code:
\begin{VrbM}
data = load('shoe.txt'); % load data from text file
x = data(:,1); % store shoe size in x
y = data(:,2); % store height in y
n = length(x); % determine number of data values
X = [ones(n,1) x]; % predictor matrix

% linear regression of y on x:
[be,beint,res] = regress(y,X); % store residuals in res
res = res - mean(res); % centred residuals

N = 100000; % number of bootstrap samples
nN = n * N;
alpha = 0.05;
alpha2 = alpha / 2;
alpha21 = 1 - alpha2;
bootbe = zeros(2,N);

rboot = randsample(res,nN,true); % sample with replacement nN values from res
rboot = reshape(rboot,n,N); % organise resampled values into N columns of n
                                             % values each so that each column contains n
                                             % bootstrapped residuals
for i = 1:N
    yboot = X * be + rboot(:,i);
    bootbe(:,i) = regress(yboot,X); % regression for bootstrap sample i
end
bootbesort = sort(bootbe,2); % sort regression coefficients in increasing order

% (1-alpha) BCA interval for be0:
b = sum(bootbe(1,:) < be(1)) / N;
bootbe0mean = mean(bootbe(1,:));
a = sum((bootbe0mean - bootbe(1,:)).^3) / (6 * (sum((bootbe0mean - bootbe(1,:)).^2)^1.5));
zb = norminv(b,0,1);
zalpha2 = norminv(alpha2,0,1);
zalpha21 = norminv(alpha21,0,1);
r = round(N * normcdf(zb + ((zb + zalpha2) / (1 - a * (zb + zalpha2))),0,1))
s = round(N * normcdf(zb + ((zb + zalpha21) / (1 - a * (zb + zalpha21))),0,1))
[bootbesort(1,r) bootbesort(1,s)]

% (1-alpha) BCA interval for be1:
b = sum(bootbe(2,:) < be(2)) / N;
bootbe1mean = mean(bootbe(2,:));
a = sum((bootbe1mean - bootbe(2,:)).^3) / (6 * (sum((bootbe1mean - bootbe(2,:)).^2)^1.5));
zb = norminv(b,0,1);
zalpha2 = norminv(alpha2,0,1);
zalpha21 = norminv(alpha21,0,1);
r = round(N * normcdf(zb + ((zb + zalpha2) / (1 - a * (zb + zalpha2))),0,1))
s = round(N * normcdf(zb + ((zb + zalpha21) / (1 - a * (zb + zalpha21))),0,1))
[bootbesort(2,r) bootbesort(2,s)]
\end{VrbM}
Results:
\begin{VrbM}
r = 2551
s = 97550
ans = 54.6419   63.9479

r = 2372
s = 97366
ans = 0.7148    1.6548
\end{VrbM}
Therefore, the 0.95 BCA interval for $\beta_0$ is $(\hat{\beta}_{0,(2551)},\hat{\beta}_{0,(97550)})  = (54.64, 63.95)$; for $\beta_1$, it is $(\hat{\beta}_{1,(2372)},\hat{\beta}_{1,(97366)})= (0.7148, 1.6548)$.
%\includegraphics
%\begin{flushright}   $\boxbox$ \end{flushright}
\end{labwork}

\section{Extension to dependent data}
\work
Recall that the nonparametric bootstrap requires the data values to be IID. We briefly describe how the nonparametric bootstrap can be applied when the data values are not independent, particularly in the context of time series data. The key idea is to divide the data into blocks that are \textquotedblleft approximately independent" and then perform random sampling with replacement on these blocks rather than on the individual data values. Hence, this extension of the nonparametric bootstrap to dependent data is sometimes referred to as the {\it block bootstrap}.

\subsection{Block bootstrap}
\work
Let $x_1,\ldots,x_n$ be a sequence of time series measurements, with the indices denoting the times at which the measurements are taken, i.e. $x_1$ is obtained before $x_2$ and so on. The measurements are not independent but have some form of dependence over time. One of the simplest forms of the block bootstrap is as follows:
\begin{asparaenum}[(a)]
\item Specify a {\it block length} $b$ ($b$ must be smaller than $n$). Let $m=round(n/b)$.

\item	 Divide $x_1,\ldots,x_n$ into blocks as follows:
$$\begin{array}{l}
B_1=\{x_1,\ldots,x_b\}\\
B_2=\{x_2,\ldots,x_b+1\}\\
\textrm{        M      }\\
B_{n-b+1}=\{x_{n-b+1},\ldots,x_n\}\\
\end{array} $$

\item Randomly pick $m$ blocks with replacement, calling them $B_1^*,\ldots,B_m^*$.

\item Concatenate $B_1^*,\ldots,B_m^*$ to get a bootstrap sample of the time series.

\item 	Repeat Steps (c) and (d) to get another bootstrap sample.
\end{asparaenum}
%\begin{flushright}   $\boxbox$ \end{flushright}
The block bootstrap procedure looks simple but the difficulty lies in the choice of the block length. A good block length depends on at least three things: the time series, the statistic of interest and the purpose for bootstrapping the statistic. Unfortunately, further discussion on block length choice involves concepts that are beyond the level of this course and so we shall have to stop here.

\section{Exercises}
\work
\begin{exercise}
Download the \Matlab function, {\tt edfplot.m}, from the course webpage. Use it to obtain the EDF and 0.95 confidence band for:
\begin{asparaenum}[(a)]
\item the continuous data in Example 4.1.3;

\item the discrete data in Example 4.1.4.
\end{asparaenum}
\end{exercise}

\begin{exercise}
The sodium data in Example 4.2.5 are given in {\tt sodium.txt}. Use the nonparametric bootstrap with 100,000 bootstrap samples to find the bias, variance and MSE of the sample median.
\end{exercise}

\begin{exercise}
The latch data in Example 4.2.3 are given in {\tt latch.txt}. Use the nonparametric bootstrap with 100,000 bootstrap samples to find a 0.95 percentile interval and the 0.95 BCA for the standard deviation. (\Matlab function for standard deviation is {\tt std}.)
\end{exercise}

\begin{exercise}
The data in {\tt hemoglobin.txt} are the haemoglobin levels (in g/dl) of 20 Canadian Olympic ice hockey players.
\begin{asparaenum}[(A)]
\item	Use the nonparametric bootstrap with 100000 bootstrap samples to find:
\begin{asparaenum}[(i)]
\item the bias, variance and MSE of the sample 0.1-quantile;

\item	a 0.95 percentile interval and the 0.95 BCA for the 0.1-quantile.
\end{asparaenum}
(\Matlab function for quantile is {\tt quantile}.)

\item	Using the BCA interval that you have already found in Part (a), find the 0.95 BCA interval for the square root of the 0.1-quantile.

\item	Plot a density histogram for the bootstrap estimates of the 0.1-quantile.
\end{asparaenum}
\end{exercise}

\begin{exercise}
The data in {\tt tar.txt} are the tar contents in a sample of 30 cigars of a particular brand.
\begin{asparaenum}[(a)]
\item Use the nonparametric bootstrap with 100,000 bootstrap samples to find:
\begin{asparaenum}[(i)]
\item the bias, variance and MSE of the sample interquartile range;

\item	a 0.95 percentile interval and the 0.95 BCA for the interquartile range.
\end{asparaenum}
(\Matlab function for interquartile range is {\tt iqr}.)

\item	Plot a density histogram for the bootstrap estimates of the interquartile range.
\end{asparaenum}
\end{exercise}

\begin{exercise}
Work through Examples 4.3.1, 4.3.2 and 4.3.4 for the shoe data.
\end{exercise}

\begin{exercise}
The data in {\tt stream.txt} contain chloride concentrations (in mg/l) found at the surface of streams (column 1), and road densities (in \%) in the vicinities of the streams (column 2).
\begin{asparaenum}[(a)]
\item Obtain a scatter plot of chloride concentration against road density and find a 0.95 BCA interval (using 100,000 nonparametric bootstrap samples) for the correlation coefficient between road density and chloride concentration. What can you conclude from the scatter plot and the confidence interval?

\item	Consider a simple linear regression of chloride concentration ($y$ or response) on road density ($x$ or predictor). Obtain 0.95 BCA intervals for the regression coefficients, using 100,000 nonparametric bootstrap samples and by:
\begin{asparaenum}[(i)]
\item bootstrapping sample points;

\item	bootstrapping residuals.
\end{asparaenum}\end{asparaenum}
\end{exercise}

\begin{exercise}
The data in salmon.txt contain the number of recruits (column 1) and the number of spawners (column 2) in 40 salmon farms. The units are thousands of fish. Recruits are fish that are big enough to be sold. Spawners are fish that are kept for laying eggs, after which they die.

The Beverton-Holt model for the relationship between recruits and spawners is:
$$R=\frac{1}{\beta_0+(\beta_1/S)},$$
where $R$ and $S$ are the numbers of recruits and spawners, and $\beta_0,\beta_1\geq 0$.
\begin{asparaenum}[(a)]
\item	Use the data to estimate $\beta_0$ and $\beta_1$ for the Beverton-Holt model by using linear regression with the transformed variables $1/R$ and $1/S$.

\item	Consider the problem of maintaining a sustainable farm. The salmon population stabilises when $R = S$. Show that the stable population size is given by:
$$R=S=\frac{1-\beta_1}{\beta_0}.$$

Using the estimates from Part (a), estimate the stable population size.

\item	Use the nonparametric bootstrap with 100,000 bootstrap samples to find 0.95 BCA intervals for the stable population by:
\begin{asparaenum}[(i)]
\item	bootstrapping sample points;

\item	bootstrapping residuals.
\end{asparaenum}\end{asparaenum}
\end{exercise}
%}% end of comment for Dominic's material


The interpretation of $\lambda ^? _{\rightarrow}$ involves the following
six stages:
\begin{gather*}
    \texttt{standard I/O} \xrightarrow{\text{chars}}
    \texttt{lexing} \xrightarrow{\text{tokens}} \texttt{parsing} \\
    \lhook\joinrel\xrightarrow{\text{terms}} \texttt{typechecking}
    \xrightarrow{\text{cast terms}} \texttt{evaluation}
    \xrightarrow{\text{value}/\text{error}} \texttt{pretty-printing}
\end{gather*}
where a program as a sequence of characters is first read from the
standard input, tokenized by a lexical analyzer, and parsed into
an abstract syntax tree. The program is then typechecked and
modified with casts before being evaluated. The final
result is then printed in a readable format to the standard output.  We start by
defining the abstract syntax for the language.

\begin{lstlisting}
    data Term = Zero
               | Tru
               | Fls
               | Var Int Type String
               | If Term Term Term
               | Succ Term
               | Pred Term
               | IsZero Term
               | Lambda Type Term [String]
               | App Term Term
               | Ref Term
               | Deref Term
               | Loc Int
               | Assign Term Term
               | Cast Coercion Term
\end{lstlisting}

Variables (\lstinline{Var Int Type String}) are represented as nameless terms using de Bruijn indices
in order to avert the problem of capturing free variables. Accordingly, each variable
is assigned a number to indicate the position of its binder. For example,
$\lambda x. \: \lambda y. \: x \: y$ is rewritten as $\lambda. \: \lambda.
\: 1 \: 0$. That is, $y$ is bound to the first binder, and $x$ to the second.
Variables whose de Bruijn indices are greater than the total number of binders
are considered free. Also, instead of maintaining a global type environment
$\Gamma$, we have each variable remember its own bound type. We also
include the original name of a variable for later printing.

Note that this syntax extends upon the one we have formalized in Section 2
with cast terms (\lstinline{Cast Coercion Term}) and
store locations (\lstinline{Loc Int}). These terms only arise as intermediate
results from type checking and evaluation, and hence are not made available
to programmers. Likewise, the data types for types and coercions are transcribed
directly from their formal definitions, with one addition of type \lstinline{TUnit}
used for free variables.

\begin{lstlisting}
    data Type = TUnit
               | Dyn
               | Boolean
               | Nat
               | Arr Type Type
               | TRef Type
\end{lstlisting}

\begin{lstlisting}
    data Coercion = Iden Type
                  | Project Type Label
                  | Inject Type
                  | CRef Coercion Coercion
                  | Func Coercion Coercion
                  | Seq Coercion Coercion
                  | Fail Type Type Label
\end{lstlisting}

Given the syntax definition, the rest of this section will mainly focus on
implementing the typechecker and
evaluator for $\lambda ^? _{\rightarrow}$. The details on the
lexer, parser, and pretty printer can be consulted in
Appendix A, B, and C.

\subsection{Intermediate Language and Cast Insertion}
The static analysis of $\lambda ^? _{\rightarrow}$ both
typechecks annotated terms and inserts coercions for
dynamically-typed terms to ensure type soundness. The cast insertion
rules, shown in Figure 6 [8], resemble the typing rules in
Figure 2.
The rules for variables, constants, and
abstractions do not require any cast insertion. Rules {\scriptsize{[C-SUCC2]}},
{\scriptsize{[C-PRED2]}}, and {\scriptsize{[C-ISZERO2]}} handle the
case when the argument type is dynamic by coercing it to \texttt{Nat}.
The rules for \texttt{if} expressions make sure that the condition
type is coerced to \texttt{Bool} if dynamic, and the two branch
types are consistent by coercing one to another. Rule
{\scriptsize{[C-APP1]}} coerces the argument type to the parameter
type given that the operator is a function. Otherwise, we need to
insert a function check $\langle \texttt{Fun}? ^l \rangle$ to verify
that the operator resolves to a function at runtime. The rules for
assignment follow the same pattern, casting the right-hand side type
to the left-hand side type and inserting a reference check
$\langle \texttt{Ref}? ^l \rangle$ when the left-hand side type is
unknown. A reference check is also important when dereferencing a
dynamic term, as in rule {\scriptsize{[C-DEREF2]}}.
\input{figures/CastInsertion}

The cast insertion process is implemented as part of the typechecker.
We also need to incorporate blame tracking whenever an unsafe cast
is introduced to the original program. To keep it simple, we
represent a blame label as an integer. A new label is obtained
by simply incrementing the value of the last assigned label by 1.
Additionally, the typechecker should be able to report errors on
ill-typed expressions. In order to achieve both blame tracking
and exception handling, a new monad \lstinline{TCheckState} is defined
as follows:
\begin{lstlisting}
    type TCheckState a = ExceptT TypeError (State Label) a
\end{lstlisting}
By combining the \lstinline{State} monad and the \lstinline{Except} monad, \lstinline{TCheckState}
helps maintain a global counter for labeling unsafe coercions
as well as return errors during type checking. The data type
for type errors captures eight possible causes: (1) out-of-bound variables,
(2) type difference between the two conditional branches, (3) non-boolean condition,
(4) unexpected argument type for an arithmetic operator or (5) a function,
(6) assigning to a non-reference term,
(7) type mismatch between the two sides of an assignment, and (8) derefencing a
non-reference term.

\begin{lstlisting}
    data TypeError = NotBound Term
                   | Difference Type Type
                   | NotBool Type
                   | NotNat Type
                   | FunMismatch Type Type Term
                   | NotFunction Term
                   | IllegalAssign Term
                   | AssignMismatch Type Type Term
                   | IllegalDeref Term
\end{lstlisting}

We transliterate the cast insertion rules in Figure 6
into the \lstinline{typeCheck'} function that takes in an expression as input
and returns either a tuple of a modified expression and its type,
or a type error.

\input{code/Typecheck.tex}

There are few slight changes to the formal type system. First is the
omission of the global type environment for bound variables. Instead, each
variable carries its own type, and those with \lstinline{TUnit} type are
out of bound. Secondly, the function always checks for type
consistency before performing coercions. \input{figures/Semantics}This sanity check allows the
typechecker to handle some errors directly instead of delegating them
to the evaluator through failure coercions.
Additionally, we add another rule for application when the
argument type is identical to the parameter type. As a result, no casts
are needed. In this way, we eliminate unnecessary coercions that make
no changes to the semantics. The same pattern is applied for conditional
expressions and assignments by including a check for type equivalence before
type consistency. Finally, we call the subroutine \lstinline{GlobalS.newLabel}
to generate a new blame label for every unsafe cast inserted.

\subsection{Operational Semantics}
Figure 7 formalizes the operational semantics for a typechecked
program with coercions. In addition to the standard rules for
the $\lambda$-calculus with references, the semantics also
defines rules for cast expressions. Rule
{\scriptsize{[E-ID]}} removes the identity coercion and
returns the inner value.
Rule {\scriptsize{[E-COMP]}} composes two
adjacent coercions given that the first coercion is normalized.
This restriction makes sure that coercions do not get accumulated
without being reduced.
The {\scriptsize{[E-GROUND]}} rule unwraps a cast value
when its coercion is normalized. Otherwise, we take a small
step of evaluation inside the coercion using the reduction rules
in Figure 5. The combination of {\scriptsize{[E-CSTEP]}} and
{\scriptsize{[E-COMP]}} helps maintain a bounded size on coercions.
Rule {\scriptsize{[E-CDEREF]}} casts the value read
from a cell, whereas {\scriptsize{[E-CASSIGN]}} also casts the value
written to it. Rule
{\scriptsize{[E-CAPP]}} splits the function coercion
into two separate coercions for the argument and the result.
Finally, we terminate the evaluation and throw a cast error
immediately when the coercion $\texttt{Fail}^l$ is encountered,
as described in the {\scriptsize{[E-FAIL]}} rule. The blame
label consequently identifies the responsible coercion.

Similar to the \lstinline{typeCheck'} procedure, we define a
monad \lstinline{SEvalState} to provide a global state for a set $\sigma$ of
store locations and also to signal runtime errors.
\begin{lstlisting}
    type SEvalState a = ExceptT RuntimeError (State StoreEnv) a
\end{lstlisting}
The following implementation describes a small-step evaluator
based on the established reduction rules in Figure 7.

\input{code/Evaluator}

For the evaluation of a cast expression (\lstinline{Cast c t'}),
we first check if the inner expression \lstinline{t'} is a value. If not, then
we take a small-step of evaluation for \lstinline{t'}. Otherwise,
we consider the next case where \lstinline{t'} is a cast value
\lstinline{Cast d u}, where \lstinline{d} is normalized. In this
case, we apply rule {\scriptsize{[E-COMP]}} to combine coercions \lstinline{c}
and \lstinline{d}. The subsequent cases handle cast expressions where
\lstinline{t'} is a simple value, and the coercion \lstinline{c} takes
on different patterns. The
\lstinline{unbox} function unwraps a cast expression whose coercion is normalized
provided that the target type matches the runtime type.
The subroutines \lstinline{GlobalS.allocate} and \lstinline{GlobalS.update}
modify the global store $\sigma$ by allocating a new cell and
updating an existing cell, respectively, while \lstinline{GlobalS.peek}
reads from a cell. Function \lstinline{subsFromTop}
performs a substitution
operation $e[x \mapsto v]$ on nameless terms by renumbering
free variables. Appendix D discusses this process in more detail.
Runtime errors include cast errors and stuckness. Cast expressions
with a failure coercion give a cast error.
A stuck evaluation results when a term has no transition states
because it is either a simple value or meaningless. Hence, a \lstinline{Stuck}
exception is simply an indication that an evaluation has
terminated. Otherwise,
we keep applying the small-step \lstinline{evaluate'} on non-stuck terms
to reach the final state. The following
function performs the big-step semantics.

\begin{lstlisting}
    evaluate :: Term -> Either RuntimeError Term
    evaluate t =
        let (res, t') = GlobalS.runBEval $ evaluateToValue t StoreEnv.empty
        in case res of
               Right t''
                   | isUncoercedVal t'' -> Right t''
                   | otherwise          -> Left Stuck
               Left (Blame s1 s2 l)     -> let cause = fromJust $ blame l t
                                               bres = BlameRes cause t'
                                           in Left $ CastError s1 s2 bres
               Left err                 -> Left err
\end{lstlisting}

The subroutine \lstinline{GlobalS.runBEval} initializes a global state
for evaluation with an empty store. \lstinline{evaluateToValue} repeatedly
applies the small-step evaluator until a stuck term or an error is encountered.
A stuck term is only valid if it is an uncoerced value. Else, an actual error
of a stuck evaluation is returned. In the case of a cast error, we call the
\lstinline{blame} function with a label to search for the erroneous
coercion in the input expression. The data type \lstinline{BlameRes}
wraps this coercion along with the last evaluated term for the error message.

\subsection{Overview}
Now that we have defined the type checking and evaluation procedures,
our interpreter for $\lambda ^? _{\rightarrow}$ works as follows:
\begin{lstlisting}
    interpret :: String -> IO ()
    interpret line = case parseExpr line of
        Right validExpr -> case typeCheck validExpr of
                               Right t  -> case evaluate t of
                                               Right res -> printMsg res
                                               Left err  -> printMsg err
                               Left err -> printMsg err
        Left err        -> print err
\end{lstlisting}
The interpreter first parses the input stream into an abstract syntax tree.
If parsing succeeds, then the parsed expression is typechecked;
otherwise, we output a parse error. A well-typed expression is subsequently
evaluated to a value, or a runtime error is thrown. Function \lstinline{printMsg}
prints the result in a readable format (see Appendix C).

\subsection{Examples}
\input{Example}


\problemname{Get Yer Armor Here}

\noindent Torbjörn, a small dwarf who excels in creating armor packs from Scrap Metal, is surveying a map representing a
battlefield. He has been airdropped into the middle of this battlefield, which is represented by a rectangular N x M 2D
matrix. He wishes to collect as much scrap metal as he can before requesting an extraction.\\

\noindent The values of the matrix will represent the number of number of scrap metal available to Torbjörn at each location in the
battlefield. If the field doesn’t have an exact center, assume that Torbjörn will start in the square closest to the center
with the highest scrap metal count. \\

\noindent On each turn, Torbjörn will collect the scrap metal available on the square that he is currently on, and then move
either one square up, down, left, or right, choosing the square that has the most available scrap metal. If there is no
more scrap metal available in any adjacent squares, Torbjörn will request an extraction and deem the mission complete.
You may assume that Torbjörn will never be forced to choose between adjacent squares with the same number of scrap
metal. \\

\noindent Write a program that given input representing the map, returns the maximum number of scrap metal that the Torbjörn
collect from the battlefield. You may assume that the matrix is rectangular with at least 1 row and 1 column, and it is
populated with non-negative integers. \\

\section*{Input}
You will be given $1 \leq N,M \leq 5000$, followed by $N$ lines, each containing $M$ non-negative integers representing the
available scrap metal at each point on the map.
\section*{Output}
Output the maximum amount of scrap metal Torbjörn will collect from this map before being extracted.\\


\section {Family of Transfer Functions}
\setlength{\parindent}{0pt}
(Prepared by Sanket Gandhi)

\vspace{0.3cm}
\begin{itemize}
    \item Recall that a DFA is defined by ($F$, $V$, \^{}) where ($V$, \^{}) forms the semilattice and $F$ is family of transfer functions.
    \item Family of Transfer Functions $F$ is set of functions. For particular DFA each transfer function for an instruction or a basic block is drawn from the set $F$.
    \item If transfer function does not have certain properties then DFA may not terminate. $F$ should have certain properties in order to reason about optimality and convergence of DFA.
\end{itemize}

\subsection{Properties of Family Transfer Functions F}
Family of transfer functions $F$ must have following properties.
\begin{itemize}
    \item Every function is $F$ takes a value from DFA value set $V$ and returns a value from same set $V$, that is every function $f$ in $F$ have form $f:V \rightarrow V $
    \item F contains the identity function that is, $\exists \ f \in F \ $ such that $f(x) = x $, $ \forall \ x \in V$. \\ This property is needed because in case of NOP instructions the output is equal to input. So identity function should be in $F$.
    \item F is closed under composition, if $f_{1},f_{2} \in F$ then $f_{1}\circ f_{2} \in F$.\\ This property will be helpful for defining transfer functions for basic blocks.
\end{itemize}

\subsection{Example}
Many of the DFAs(Reaching definitions, Constant Propagation etc.) have following structure in transfer function.
\[f(x) = (x-kill_{s})\bigcup gen_{s}\] for some instruction $s$. $kill_{s}$ and $gen_{s}$ only depends on $s$ and are independent of $x$.
\\$F$ is the set of function which is populated by changing $kill$ and $gen$ set in above structure.
In case of reaching definitions if we have $n$ definitions in program then size of $F$ will be $4^{n}$.  Lets check whether $F$ is family of transfer functions or not.
\begin{itemize}
    \item Every function in $F$ take a set of some kind $x$ input and perform set operations. The output is also set of same kind. $F$ have first property of family of transfer function.
    \item In case of $gen = \emptyset$ and $kill = \emptyset$, the transfer function will become $f(x) = x$ and $f\in F$.
    \item Consider two functions $f_{1}(x) = (x-kill_{1})\bigcup gen_{1}$ and $f_{2}(x) = (x-kill_{2})\bigcup gen_{2}$. Both $f_{1}, f_{2}$ belongs to $F$.
    \[f_{2}(f_{1}(x)) = ((x-kill_{1})\bigcup gen_{1}-kill_{2})\bigcup gen_{2}\]
    By using property $A\bigcup B - C = (A-C)\bigcup (B-C)$ the above equation becomes
    \[f_{2}(f_{1}(x)) = (x-(kill_{1} \bigcup kill_{2}))\bigcup ((gen_{1}-kill_{2})\bigcup gen_{2})\]
    With $kill_{1\circ 2} = kill_{1} \bigcup kill_{2}$ and $gen_{1\circ 2} = (gen_{1}-kill_{2})\bigcup gen_{2}$ the composite function becomes $f_{2}(f_{1}(x)) = (x - kill_{1 \circ 2})\bigcup gen_{1\circ 2}$. Therefore $f_{1} \circ f_{2} \in F$ as $F$ contain transfer functions with all possible $gen$ and $kill$ set.
\end{itemize}