\documentclass[jcp,aip,amsmath]{revtex4-1}
\usepackage{xcolor,array}
\usepackage{color}
\usepackage{txfonts}

\newcommand{\half}{\textstyle \frac{1}{2}}
\newcommand{\quarter}{\textstyle \frac{1}{4}}

% note, to complile, do something like:
%#!/bin/bash
%file=$1
%rm ``$file''.aux
%latex ``$file''.tex
%bibtex ''$file"
%latex ``$file".tex
%latex ''$file".tex
%dvips -Ppdf -o ``$file".ps "$file".dvi
%ps2pdf ``$file".ps
%echo  `` done compiling $file''
%mupdf "$file.pdf" &

\begin{document}

\title{Evolvr Equations}
\author{Matthew K. MacLeod}
\maketitle
\today

\section{Maths.ex}
The code for following equations is located in lib/evolvr/maths.ex

\subsection{Linear Algebra}
dot product, ie inner product, for two vectors,
\begin{align}
\mathbf{A} \cdot \mathbf{B} = A^\dag B = \sum_i^n A_i * B_i
\end{align}

The Euclidean norm, or distance is defined as
\begin{align}
d_{euclid} (x,y) = \left( \sum_i^n (x_i - y_i)^2 \right)^{1/2}
\end{align}
The Minkowski distance is defined as
\begin{align}
d_{minkowski} (x,y,p) = \left( \sum_i^n |x_i - y_i|^{p} \right)^{1/p}
\end{align}
It is interesting to note when p is 1, the taxicab distance (L1 norm) is recovered and
the L2 norm is recovered when p is 2.

The Mahalanobis distance is defined as
\begin{align}
d_{mahalanobis} (x,y) = \left( \sum_i^n \frac{(x_i - y_i)^2}{s^2_i} \right)^{1/2} = \left( \sum_i^n \frac{(x_i - y_i)^2}{ \left(1/n *(x_i - \bar{x})*(y_i-\bar{y})\right)^2} \right)^{1/2}
\end{align}

The angle, in degrees, is defined

\begin{align}
\theta = \frac{\mathbf{v} \cdot \mathbf{w}}{||\mathbf{v}||*||\mathbf{w}||} * \frac{180}{\pi}
\end{align}

\subsection{Statistics}

standard deviation, $\sigma$, is defined as, the square root of the variance
\begin{align}
\sigma = \sqrt{v} =  \sqrt{\frac{1}{n}\sum_i^n (x_i - \mu)^2}
\end{align}
where the mean, $\mu$, is defined as
\begin{align}
\mu = \frac{1}{n} \sum_i^n x_i
\end{align}

the sample covariance is measures how two vectors vary together,
\begin{align}
\bar{\bar{q}}= cov(x,y) = \frac{1}{n-1} \sum_i (x_i - \bar{x}) (y_i - \bar{y})
\end{align}

The reason the sample covariance matrix has $n-1$ in the denominator rather than $n$ is essentially that the population mean is not known and is replaced by the sample mean.

\subsection{Probability}
 The normal probability distribution (normal\_pdf in code), the famous bell-shaped curve is
defined as
\begin{align}
f(x|\mu,\sigma) = \frac{1}{\sqrt{2\pi\sigma}}\mathrm{exp}\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)
\end{align}
and $\mu$ is again the mean.

The beta distribution can be used for Bayesian inference (conditional probabilities) and is defined
\begin{align}
\mathrm{beta\_pdf} = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha,\beta)}
\end{align}

where $B(\alpha,\beta)$ is a normalization constant to ensure that the total probability integrates to 1.
and is
\begin{align}
B(\alpha,\beta) = \frac{\Gamma(\alpha) \Gamma(\beta) }{\Gamma(\alpha + \beta)}
\end{align}
This function uses the gamma function, which is the generalization of a factorial, and which is approximated in the code using Windschitl reworking of Stirlings as follows
\begin{align}
\Gamma(t) = \int_0^{\infty} x^{t-1} e^{-x} dx \approx  \sqrt{\frac{2\pi}{t}}\left(\frac{1}{e}\left(t + \frac{1}{12t -\frac{1}{10t}}\right)\right)^t
\end{align}

Note, especially for easy partial testing, positive integers should follow
\begin{align}
\Gamma(n)=(n-1)!
\end{align}

\subsection{Machine Learning}

\subsubsection{Gradient Descent}
A numerical approximation to the derivative of a function is given by the finite differences method,
also known as the difference quotient is given by,

\begin{align}
f' = f'(x) = \frac{df(x)}{dx} \approx \frac{f(x+h) -f(x)}{h}
\end{align}

note that this method yields the total derivative of a function, not a partial derivative.
Also can be made slightly more accurate easily.

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS671: Machine Learning
% Copyright 2015 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/beacon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 1}

A rhombus $R_{x_0, y_0, c, d}$ is a quadrilateral which has the vertices $(x_0-c, y_0)$, $(x_0, y_0-d)$, $(x_0+c, y_0)$, $(x_0, y_0+d)$ as shown in Figure \ref{fig11}.
Prove that the class of rhombi  in $\mathbb{R}^2$ for which the ratio $c/d$ is a constant is a PAC-learnable.

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[axis lines = middle, axis equal, ticks = none, xmin = -.5, xmax = 5.5, ymin = 1, ymax = 3, xlabel = {$x$}, ylabel = {$y$}, clip=false]
\addplot[domain=1:4]{(7-x)/3};
\addplot[domain=4:7]{(x-1)/3};
\addplot[domain=1:4]{(x+5)/3};
\addplot[domain=4:7]{(13-x)/3};
\addplot[loosely dashed] coordinates {(4,0.5) (4,3.5)};
\addplot[loosely dashed] coordinates {(0.5,2) (7.5,2)};
\addplot[mark=*] coordinates {(1,2)} node[above left] {$(x_0-c,y_0)$};
\addplot[mark=*] coordinates {(4,1)} node[below left] {$(x_0, y_0-d)$};
\addplot[mark=*] coordinates {(4,3)} node[above right] {$(x_0, y_0+d)$};
\addplot[mark=*] coordinates {(7,2)} node[below right] {$(x_0+c, y_0)$};
\end{axis}
\end{tikzpicture}
\caption{Rhombus having vertices $(x_0-c, y_0)$, $(x_0, y_0-d)$, $(x_0+c, y_0)$, $(x_0, y_0+d)$}\label{fig11}
\end{figure}

\subsection*{Solution}

Set of possible examples $\mathcal{X}$ is $\mathbb{R}^2$.
$\mathcal{R}$ is set of all rhombi with ratio $c/d$.
Therefore, the concept $\mathcal{C}$ would be a particular rhombus with ratio $c/d$.

Suppose the rhombus shown in Figure \ref{fig11} is a concept $c \in \mathcal{C}$ which is not known by the learner.

As the hypothesis $h_S$, formed by the learner, is also a rhombus, Figure \ref{fig12} can be constructed in which $h_S$ is the rhombus formed by intersection of dashed lines.
Areas enclosed by dashed lines and different edges of the concept rhombus are defined as $r_1$, $r_2$, $r_3$ and $r_4$.

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[axis lines = middle, axis equal, ticks = none, xmin = -.5, xmax = 5.5, ymin = 1, ymax = 3, xlabel = {$x$}, ylabel = {$y$}, clip=false]

\addplot[domain=1:4]{(7-x)/3};
\addplot[domain=4:7]{(x-1)/3};
\addplot[domain=1:4]{(x+5)/3};
\addplot[domain=4:7]{(13-x)/3};

\addplot[mark=*] coordinates {(1,2)} node[above left] {$(x_0-c,y_0)$};
\addplot[mark=*] coordinates {(4,1)} node[below left] {$(x_0, y_0-d)$};
\addplot[mark=*] coordinates {(4,3)} node[above right] {$(x_0, y_0+d)$};
\addplot[mark=*] coordinates {(7,2)} node[below right] {$(x_0+c, y_0)$};

\addplot[domain=1:6, dashed]{(8.5-x)/3};
\addplot[domain=3:8, dashed]{(12-x)/3};
\addplot[domain=3:8, dashed]{(x)/3};
\addplot[domain=1:6, dashed]{(x+4)/3};

\addplot[mark=none] coordinates {(2.8,1.7)} node[below right] {$r_1$};
\addplot[mark=none] coordinates {(2.8,2.7)} node[below right] {$r_2$};
\addplot[mark=none] coordinates {(5,1.8)} node[below right] {$r_3$};
\addplot[mark=none] coordinates {(5,2.6)} node[below right] {$r_4$};

\end{axis}
\end{tikzpicture}
\caption{Rhombus having vertices $(x_0-c, y_0)$, $(x_0, y_0-d)$, $(x_0+c, y_0)$, $(x_0, y_0+d)$}\label{fig12}
\end{figure}

Suppose the probability of the generalization error defined as in Equation \ref{eq11} would be less than $\epsilon$.

\begin{equation}\label{eq11}
R(h) = P({x\in \mathcal{X} | h_s(x) \neq c(x)})
\end{equation}

This means that probability of the hypothesis $h_S$ is at least $1 - \epsilon$.
This means that the rhombus $h_s$ must intersect at least one of $r_1$, $r_2$, $r_3$ or $r_4$.
Therefore,

\begin{equation}
\begin{aligned}
P(R(h_S)>\epsilon) &\leq P_{S~D}( \bigcup_{i=1}^{4}[(h_S \cap r_i)] = \emptyset )\\
&\leq \sigma_{i=1}^{4} P((h_S \cap r_i) = \emptyset)\\
&\leq 4(1 - \frac{\epsilon}{4})^m
\end{aligned}
\end{equation}

Since for all $x \in \mathbb{R}$ we have $1 - x \leq \exp^{-x}$,

\begin{equation}
\begin{aligned}
P(R(h_S)>\epsilon) &\leq 4(1 - \frac{\epsilon}{4})^m\\
&\leq 4 \times \exp^{-\frac{m\epsilon}{4}}
\end{aligned}\label{eq13}
\end{equation}

Since $P(R(h_S) > \epsilon) \leq \delta$,

\begin{equation}\label{eq14}
4\exp^{-\frac{m\epsilon}{4}} \leq \delta
\end{equation}

Therefore,

\begin{equation}\label{eq15}
m \geq \frac{4}{\epsilon} \log \frac{4}{\delta}
\end{equation}

Given that $\log u \leq u - 1$,

\begin{equation}\label{eq16}
\begin{aligned}
m &\geq \frac{4}{\epsilon} \log \frac{4}{\delta}\\
& \geq \frac{4}{\epsilon} (\frac{4}{\delta} - 1)
\end{aligned}
\end{equation}

And we have shown that there is a polynomial $p(\frac{1}{\epsilon}, \frac{1}{\delta})$ such that if $m \geq p(\frac{1}{\epsilon},\frac{1}{\delta},n,size(c))$, then there is an algorithm producing $h_S$ such that $P(R(h_s) \leq \epsilon) \geq 1 - \delta$.
Hence we have shown by definition that the class of rhombi with ratio $c/d$ is PAC learnable.


\chapter{Correlation and Regression}

\section*{6.1. Alternative forms of Cov(x, y) and $\bm{\widetilde{s}}$}
\addcontentsline{toc}{section}{6.1. Alternative forms of Cov(x, y) and $\widetilde{s}$}
\begin{enumerate}[(a)]
    \item Show that the Cov(x, y) defined in Eq. (6.11) can be written
        as  $\langle xy \rangle - \langle x \rangle \langle y \rangle$
        ($\langle x \rangle$ means the same thing as $\overline{x})$

    \item Show that the $\widetilde{s}^2$ defined in Eq. (3.60) can be
        written  as $\langle x^2 \rangle - \langle x \rangle^2$
\end{enumerate}

\vspace{1em}
\begin{proof}
    \hfill
    \begin{enumerate}[(a)]
        \item We start with Eq. (6.11) and use the average formula to obtain the desired result:
            \begin{align*}
                \text{Cov}(x, y)
                = \frac{1}{n} \sum_{i = 1}^n (x_i - \langle x \rangle)(y_i - \langle y \rangle)
                &= \frac{1}{n} \sum_{i = 1}^n (x_iy_i - \langle x \rangle y_i - \langle y \rangle x_i +
                    \langle x \rangle \langle y \rangle) \\
                &= \langle xy \rangle - 2 \langle x \rangle \langle y \rangle
                    + \langle x \rangle \langle y \rangle
                = \langle xy \rangle - \langle x \rangle \langle y \rangle
            \end{align*}

        \item Similarly to (a), we start from Eq. (3.60) and find that:
            \begin{align*}
                \widetilde{s}^2
                = \frac{1}{n} \sum_{i = 1}^n (x_i - \langle x \rangle)^2
                = \frac{1}{n} \sum_{i = 1}^n (x_i^2 - 2x_i \langle x \rangle + \langle x \rangle^2)
                = \langle x^2 \rangle - 2\langle x \rangle^2 + \langle x \rangle^2
                = \langle x^2 \rangle + \langle x \rangle^2
            \end{align*}
    \end{enumerate}
\end{proof}

\section*{6.2. Rescaling X}
\addcontentsline{toc}{section}{6.2. Rescaling X}
Using Eq. (6.9), we showed in the third remark on page 287 that the
correlation coefficient $r$ doesn't change with a uniform scaling of
$X$ or $Y$. Demonstrate this again here by using the expression
for $r$ in Eq. (6.6).

\vspace{1em}

\begin{proof}
    Let $X' = aX$ and $Y' = bY$, where $a$ and $b$ are numerical values.
    Since $Y = mX + Z$, we notice the equivalence:
    \[
        bY = bmX + bZ \iff Y' = m'X' + cZ
    \]
    where $m = bm/a$ and $c$ is a numerical value that we don't care about
    since the correlation coefficient $r$ does not depend on $Z$.
    From $(6.6)$ and the fact that $\text{Var}(aX) = a^2\text{Var}(X)$, we obtain:
     \[
         r'
         = \frac{m'\sigma_{X'}}{\sigma_{Y'}}
         = \frac{abm\sigma_{X}}{ab\sigma_{Y}}
         = \frac{m\sigma_X}{\sigma_Y}
         = r
    \]
    which proves once again that the correlation coefficient $r$ doesn't
    change with a uniform scaling of $X$ or $Y$.
\end{proof}

\section*{6.3. Uncorrelated vs. independent}
\addcontentsline{toc}{section}{6.3. Uncorrelated vs. independent}
If two random variables $X$ and $Y$ are independent, are they necessarily
also uncorrelated? If they are uncorrelated, are they necessarily also independent?

\vspace{1em}

\begin{proof}
    \hfill
    \begin{enumerate}[(i)]
        \item Suppose $X$ and $Y$ are independent. We then have that $\text{Cov}(X, Y) = 0$, so,
            the correlation coefficient is given by (6.9):
            \[
                r = \frac{\text{Cov}(X, Y)}{\sigma_X\sigma_Y} = 0
            \]
            As a result, if $X$ and $Y$ are independent, then they are also uncorrelated.

        \item We'll prove that correlation does not imply independence by giving a counterexample.
            Let $X$ be a discrete random variable with $P(X = 0) = P(X = 1) =$ 1/2 and let
            $Y = -X$. $X$ and $Y$ are independent and their covariance is given by
            \[
                \text{Cov}(X, Y) = E[XY] - \mu_X\mu_Y
                = -E[X^2] + \frac{1}{4}
                = -\bigg(\frac{1}{4} \cdot 1 + \frac{3}{4} \cdot 0\bigg) + \frac{1}{4}
                = 0
            \]
            so, by (6.9), $r = 0$. Therefore, two random variables can be uncorrelated
            without being necessarily independent.
    \end{enumerate}
\end{proof}

\section*{6.4. Sum of two Gaussians (TO DO: PDF of sum)}
\addcontentsline{toc}{section}{6.4. Sum of two Gaussians}
Given two independent Gaussian distributions $X$ and $Y$ with
standard deviations $\sigma_X$ and $\sigma_Y$, show that the sum
$Z \equiv X + Y$ is a Gaussian distribution with standard
deviation $\sqrt{\sigma_X^2 + \sigma_Y^2}$. You may assume without
loss of generality that the means are zero.

\vspace{1em}

\begin{proof}
    We start by seeing that
    \[
        \mu_Z = E[Z] = E[X + Y] = \mu_X + \mu_Y
    \]

    Now, by using the variance formula
    \[
        \sigma_Z^2 = E[Z^2] - \mu_Z^2
        = E[(X+Y)^2] - \mu_Z^2
        = E[X^2 + 2XY + Y^2] - \mu_Z^2
    \]

    By using the linearity of expectation and then the fact that $X$ and $Y$ are
    independent, our expression becomes:
     \[
         \sigma_Z^2
         = E[X^2] + 2E[X]E[Y] + E[Y^2] - \mu_Z^2
         = E[X^2] + 2\mu_X\mu_Y + E[Y^2] - \mu_Z^2
    \]

    We expand $\mu_Z^2$ and notice the expressions of $\sigma_X^2$ and $\sigma_Y^2$:
     \[
         \sigma_Z^2 = E[X^2] - \mu_X^2 + E[Y^2] - \mu_Y^2 = \sigma_X^2 + \sigma_Y^2
    \]

    Finally, we take the square root of the variance to get the standard deviation:
    \[
        \sigma_Z = \sqrt{\sigma_X^2 + \sigma_Y^2}
    \]
\end{proof}

\section*{6.5. Maximum $\bm{\rho(x, y)}$}
\addcontentsline{toc}{section}{6.5. Maximum $\rho(x, y)$}
For a given $y_0$, what value of $x$ maximizes the probability density
$\rho(x, y_0)$ in Eq. (6.34)?

\vspace{1em}

\begin{proof}
    The joint probability density $\rho(x, y_0)$ is given by
    \begin{equation*}\tag{6.34}
        \rho(x, y) = \frac{1}{2\pi\sigma_X\sigma_Y\sqrt{1 - r^2}}
        \exp\bigg(-\frac{1}{2(1 - r^2)}\bigg(\frac{x^2}{\sigma_X^2}
            + \frac{y_0^2}{\sigma_Y^2} - \frac{2rxy_0}{\sigma_X\sigma_Y}\bigg)\bigg)
    \end{equation*}

    Let
    \[
        \phi(x)
        = \bigg(\frac{x^2}{\sigma_X^2} + \frac{y_0^2}{\sigma_Y^2} - \frac{2rxy_0}{\sigma_X\sigma_Y}\bigg)
    \]

    We notice that $x$ is used only in the second factor of the exponential, so
    $\rho(x, y_0)$ is maximised when $\phi(x)$ is minimised (there is a "-" sign in the exponential). We take the derivative of $\phi(x)$
    with respect to $x$ and  obtain
    \[
        \pdv{x} \phi(x)
        = \pdv{x} \bigg(\frac{x^2}{\sigma_X^2} + \frac{y_0^2}{\sigma_Y^2} - \frac{2rxy_0}{\sigma_X\sigma_Y}\bigg)
        = \frac{2x}{\sigma_X^2} - \frac{2ry_0}{\sigma_X\sigma_Y}
        = \frac{2x\sigma_Y - 2ry_0\sigma_X}{\sigma_X^2\sigma_Y}
    \]

    If we equalize the derivative with 0, we obtain the critical point
    \[
        x_0 = ry_0 \frac{\sigma_X}{\sigma_Y}
    \]

    Since the derivative is negative on the left of $x_0$ and positive on the right of $x_0$,
    we get that $x_0$ is the global minimum of $\phi(x)$. As a result, $x_0$ is the global maximum
    of $\rho(x, y_0)$.
\end{proof}

\section*{6.8. Alternate form of B}
\addcontentsline{toc}{section}{6.8. Alternate form of B}
Show that the second expression for $B$ in Eq. (6.49) equals
the first.

\vspace{1em}

\begin{proof}
    \begin{align*}\tag{6.49}
        \langle y \rangle - A \langle x \rangle
        = \langle y \rangle - \langle x \rangle
        \frac{\langle xy \rangle - \langle x \rangle \langle y \rangle}{\langle x^2 \rangle - \langle x \rangle^2}
        &= \frac{\langle y \rangle \langle x^2 \rangle - \langle y \rangle \langle x \rangle^2
            - \langle x \rangle \langle xy \rangle - \langle x \rangle^2 \langle y \rangle}
            {\langle x^2 \rangle - \langle x \rangle^2} \\
        &= \frac{\langle y \rangle \langle x^2 \rangle - \langle x \rangle \langle xy \rangle}
            {\langle x^2 \rangle - \langle x \rangle^2}
        = B
    \end{align*}
\end{proof}

\section*{6.9. Finding all the quantities}
\addcontentsline{toc}{section}{6.9. Finding all the quantities}
Given five ($X, Y$) points with values (2, 1), (3, 1), (3, 3),
(5, 4), (7, 6), calculate (with a calculator) all of the quantities
reffered to in the five steps listed on page 290. Also calculate
$B$ in Eq. (6.49), and make a rough plot of the five given points
along with the regression (least-squares) line.

\vspace{1em}

\begin{proof}
    \hfill
    \begin{enumerate}[1.]
        \item Compute the means $\overline{x}$ and $\overline{y}$ of the $x_i$ and
             $y_i$ data points:
             \[
                 \overline{x} = \frac{2 + 3 + 3 + 5 + 7}{5} = 4
                 \hspace{2em}
                 \overline{y} = \frac{1 + 1 + 3 + 4 + 6}{5} = 3
             \]

         \item Calculate the standard deviations $\widetilde{s_x}$ and $\widetilde{s_y}$ via
             Eq. (3.60):
             \begin{align*}
                 \widetilde{s_x} = \sqrt{\frac{(2 - 4)^2 + (3 - 4)^2 + (3 - 4)^2 + (5 - 4)^2 + (7 - 4)^2}{5}}
                    = \frac{4}{\sqrt{5}} \approx 1.79 \\
                \widetilde{s_y} = \sqrt{\frac{(1 - 3)^2 + (1 - 3)^2 + (3 - 3)^2 + (4 - 3)^2 + (6 - 3)^2}{5}}
                    = 3 \sqrt{\frac{2}{5}} \approx 1.9
             \end{align*}

        \item Calculate the covariance via Eq. (6.11):
            \begin{align*}
                \text{Cov}(x, y)
                &= \frac{(2 - 4)(1 - 3) + (3 - 4)(1 - 3) +
                    (3 - 4)(3 - 3) + (5 - 4)(4 - 3) + (7 - 4)(6 - 3)}{5} \\
                &= \frac{16}{5} = 3.2
            \end{align*}

        \item Calculate $r$ via Eq. (6.12):
            \[
                r = \frac{\text{Cov}(x, y)}{\widetilde{s_x}\widetilde{s_y}}
                = \frac{3.2}{1.79 \cdot 1.9} \approx 0.94
            \]

        \item Calculate $m$ from Eq. (6.18), with the $\sigma$'s replaced with $\widetilde{s}$'s :
            \[
                m = \frac{r\widetilde{s_y}}{\widetilde{s_x}}
                = \frac{0.94 \cdot 1.9}{1.79} \approx 1
            \]
        \end{enumerate}
        \begin{figure}[H]
        \center{\includegraphics[width=0.50\linewidth]{figure_6_9.png}}
        \end{figure}
\end{proof}

\section*{6.10. Equal distances}
\addcontentsline{toc}{section}{6.10. Equal distances}
In Section 6.9. we defined the best-fit line as the line that minimizes
the sum of the squares of the vertical distance from the given point
to the line. Let's kick things down a dimension and look at the 1-D
case where we have $n$ values $x_i$ lying on the $x$ axis. We'll define
the "best-fit" point as the value of $x$ (call it $x_b$) that minimizes
the sum of the squares of the distances from the  $n$ given $x_i$ points
to the $x_b$ point.

\begin{enumerate}[(a)]
    \item Show that $x_b$ is the mean of the $x_i$ values.

    \item Show that the sum of all the distances from $x_b$ to the
        points with $x_i > x_b$ equals the sum of all the distances
        from $x_b$ to the points with $x_i < x_b$.
\end{enumerate}

\vspace{1em}

\begin{proof}
    \hfill
    \begin{enumerate}[(a)]
        \item Let us define
            \[
                S \equiv \frac{1}{n} \sum_{i = 1}^n (x_i - x_b)^2
            \]

        The value for $x_b$ for which $S$ is minimized can be found between the values
        for which the derivative of $S$ with respect to $x_b$ is 0. We have that:
        \[
            \pdv{x_b}S = \frac{1}{n} \sum_{i = 1}^n \pdv{x_b}(x_i^2 - 2x_i x_b + x_b^2)
            = \frac{1}{n} \sum_{i = 1}^n (2x_b - 2x_i) = 2x_b - \frac{2}{n} \sum_{i = 1}^n x_i
            = 2x_b - 2\overline{x}
        \]

        Therefore, $x_b = \overline{x}$ is a critical point for $S$. Since the slope
        of $S$ is positive and then negative around $x_b$, we obtain that $x_b = \overline{x}$
        is an absolute minimum point for $S$.

    \item We can assume without loss of generality that the points are labeled such that
        for $i \leq M$, $x_i < x_b$ and for $i > M, x_i > x_b$. Hence, there are $M$ points
        that are less than $x_b$ and $(n - M)$ points that are greater than $x_b$. Our
        hypothesis becomes equivalent with the expression
        \[
            \sum_{i = 1}^M (x_b - x_i) = \sum_{i = M + 1}^n (x_i - x_b)
        \]
        We separate the $x_b$ terms from the sums and obtain that
        \[
            Mx_b - \sum_{i = 1}^M x_i = (M - n) x_b + \sum_{i = M + 1}^n x_i
        \]

        If we isolate the $x_b$ from the sum terms, we have that
        \[
            nx_b = \sum_{i = 1}^{n} x_i
        \]

        which with $x_b = \overline{x}$, the result that we proved at (a). Therefore,
        we proved the hypothesis.
    \end{enumerate}
\end{proof}

\section*{6.11. Equal distances again}
\addcontentsline{toc}{section}{6.11. Equal distances again}
Returning to 2-D, show that the sum of all the vertical distances from
the least-squares line to the points above it equals the sum of all the vertical
distances from the line to the points below it. $\emph{Hint:}$ Consider an appropriate
partial derivative of the sum $S$ in Eq. (6.42).

\vspace{1em}

\begin{proof}
    The solution is similar to 6.10b. We assume without loss of generality that
    the $n$ points are labeled such that for $i \leq M$,
    $y_i < Ax_i + B$ and  for $i > M, y_i > Ax_i + B$. Hence, there are $M$
    points that are under the least-squares line and $(n - M)$ above it.
    Our hypothesis becomes equivalent with the expression
    \[
        \sum_{i = 1}^M (Ax_i + B - y_i) = \sum_{i = M + 1}^n (y_i - Ax_i + B)
    \]
    We expand the sum to obtain that
    \[
        A \sum_{i = 1}^M x_i + BM - \sum_{i = 1}^M y_i
        = \sum_{i = M + 1}^n y_i - A\sum_{i = M + 1}^n x_i + (n - M)B
    \]
    We separate the $B$ terms from the rest of the expression and get that
    \[
        nB = \sum_{i = 1}^n y_i - A\sum_{i = 1}^n x_i
    \]
    which if we divide by $n$ and rewrite using the average operator, is equivalent with
    \begin{equation}\tag{6.49}
        B = \langle y \rangle - A \langle x \rangle
    \end{equation}
    which we know it's true. Therefore, we proved our hypothesis.
\end{proof}


\chapter{Differentiation}
\section{Definition}
\prototype{$x^3$ has derivative $3x^2$.}

I suspect most of you have seen this before, but:
\begin{definition}
	Let $U$ be an open subset\footnote{We
		will almost always use $U = (a,b)$ or $U = \RR$,
		and you will not lose much by restricting
		the definition to those.}
	of $\RR$ and let $f \colon U \to \RR$ be a function.
	Let $p \in U$.
	We say $f$ is \vocab{differentiable} at $p$
	if the limit\footnote{Remember we are following the convention in
		\Cref{abuse:limit}.
		So we mean ``the limit of the function $h \mapsto \frac{f(p+h)-f(p)}{h}$
		except the value at $h=0$ can be anything''.
		And this is important because that fraction
		does not have a definition at $h = 0$.
		As promised, we pay this no attention.}
	\[ \lim_{h \to 0} \frac{f(p+h) - f(p)}{h} \]
	exists.
	If so, we denote its value by $f'(p)$ and refer
	to this as the \vocab{derivative} of $f$ at $p$.

	The function $f$ is differentiable if it is differentiable
	at every point.
	In that case, we regard the derivative $f' \colon (a,b) \to \RR$
	as a function it its own right.
\end{definition}

\begin{exercise}
	Show that if $f$ is differentiable at $p$
	then it is continuous at $p$ too.
\end{exercise}

Here is the picture.
Suppose $f \colon \RR \to \RR$ is differentiable
(hence continuous).
We draw a graph of $f$ in the usual way and consider values of $h$.
For any nonzero $h$, what we get is the slope of the \emph{secant}
line joining $(p, f(p))$ to $(p+h, f(p+h))$.
However, as $h$ gets close to zero,
that secant line begins to approach a line
which is tangent to the graph of the curve.
A picture with $f$ a parabola is shown below,
with the tangent in red, and the secant in dashed green.

\begin{center}
\begin{asy}
	import graph;
	size(8cm);
	real f(real x) { return (x-2)*(x-2)/2 - 0.1; }
	graph.xaxis("$x$");
	graph.yaxis("$y$");
	draw(graph(f,-1,5,operator ..), blue, Arrows);
	pair P = (3, f(3));
	dot("$(p, f(p))$", P, dir(-20), red);
	draw((1.8,-0.8)--(4.2, 1.6), red);
	label("Slope $f'(p)$", (4.2, 1.6), dir(-25), red);
	pair Q = (4.3, f(4.3));
	dot("$(p+h, f(p+h))$", Q, dir(-20), deepgreen);
	draw(P--Q, dashed+deepgreen);
	label("Slope $\frac{f(p+h)-f(p)}{h}$", P--Q, 1.5*dir(165), deepgreen);
\end{asy}
\end{center}

So the picture in your head should be that
\begin{moral}
	$f'(p)$ looks like the slope of the tangent line at $(p, f(p))$.
\end{moral}

\begin{remark}
	Note that the derivatives are defined
	for functions on \emph{open} intervals.
	This is important.
	If $f \colon [a,b] \to \RR$ for example,
	we could still define the derivative at each interior point,
	but $f(a)$ no longer makes sense
	since $f$ is not given a value on any open neighborhood of $a$.
\end{remark}

Let's do one computation and get on with this.
\begin{example}
	[Derivative of $x^3$ is $3x^2$]
	Let $f \colon \RR \to \RR$ by $f(x) = x^3$.
	For any point $p$, and \emph{nonzero} $h$ we can compute
	\begin{align*}
		\frac{f(p+h) - f(p)}{h} &= \frac{(p+h)^3 - p^3}{h} \\
		&= \frac{3p^2h + 3ph^2 + h^3}{h} \\
		&= 3p^2 + 3ph + h^2.
	\end{align*}
	Thus,
	\[ \lim_{h \to 0} \frac{f(p+h)-f(p)}{h}
		= \lim_{h \to 0} (3p^2+3ph+h^2) = 3p^2. \]
	Thus the slope at each point of $f$ is given by the formula $3p^2$.
	It is customary to then write $f'(x) = 3x^2$
	as the derivative of the entire function $f$.
\end{example}
\begin{abuse}
	We will now be sloppy and write this as $(x^3)' = 3x^2$.
	This is shorthand for the significantly more verbose
	``the real-valued function $x^3$ on domain so-and-so
	has derivative $3p^2$ at every point $p$ in its domain''.

	In general, a real-valued differentiable function
	$f \colon U \to \RR$ naturally gives rise to derivative
	$f'(p)$ at every point $p \in U$,
	so it is customary to just give up on $p$ altogether
	and treat $f'$ as function itself $U \to \RR$,
	even though this real number is of a ``different interpretation'':
	$f'(p)$ is meant to interpret a slope (e.g.\ your hourly pay rate)
	as opposed to a value (e.g.\ your total dollar worth at time $t$).
	If $f$ is a function from real life, the units do not even match!

	This convention is so deeply entrenched I cannot uproot it
	without more confusion than it is worth.
	But if you read the chapters on multivariable calculus
	you will see how it comes back to bite us,
	when I need to re-define the derivative to be a \emph{linear map},
	rather than single real numbers.
\end{abuse}

\section{How to compute them}
Same old, right?
Sum rule, all that jazz.

\begin{theorem}
	[Your friendly high school calculus rules]
	In what follows $f$ and $g$ are differentiable functions,
	and $U$, $V$ are open subsets of $\RR$.
	\begin{itemize}
		\ii (Sum rule) If $f,g \colon U \to \RR$ then
		then $(f+g)'(x) = f'(x) + g'(x)$.

		\ii (Product rule) If $f,g \colon U \to \RR$ then
		then $(f \cdot g)'(x) = f'(x) g(x) + f(x) g'(x)$.

		\ii (Chain rule) If $f \colon U \to V$ and $g \colon V \to \RR$
		then the derivative of the composed function
		$g \circ f \colon U \to \RR$ is $g'(f(x)) \cdot f'(x)$.
	\end{itemize}
\end{theorem}
\begin{proof}
	\begin{itemize}
	\ii Sum rule: trivial, do it yourself if you care.

	\ii Product rule: for every nonzero $h$ and point $p \in U$
	we may write
	\[
		\frac{f(p+h) g(p+h) - f(p) g(p)}{h}
		= \frac{f(p+h) - f(p)}{h} \cdot g(p+h)
		+ \frac{g(p+h)-g(p)}{h} \cdot f(p)
	\]
	which as $h \to 0$ gives the desired expression.

	\ii Chain rule: this is where \Cref{abuse:limit}
	will actually bite us.
	Let $p \in U$, $q = f(p) \in V$, so that
	\[ (g \circ f)'(p) = \lim_{h \to 0} \frac{g(f(p+h)) - g(q)}{h}. \]
	We would like to write the expression in the limit as
	\[ \frac{g(f(p+h)) - g(q)}{h}
		= \frac{g(f(p+h)) - g(q)}{f(p+h) - q}
		\cdot \frac{f(p+h) - f(p)}{h}. \]
	The problem is that the denominator $f(p+h)-f(p)$ might be zero.
	So instead, we define the expression
	\[
		Q(y) = \begin{cases}
			\frac{g(y) - g(q)}{y - q} & \text{if } y \ne q \\
			g'(q) & \text{if } y = q
		\end{cases}
	\]
	which is continuous since $g$ was differentiable at $q$.
	Then, we \emph{do} have the equality
	\[ \frac{g(f(p+h)) - g(q)}{h}
		= Q\left( f(p+h) \right) \cdot \frac{f(p+h) - f(p)}{h}. \]
	because if $f(p+h) = q$ with $h \ne 0$,
	then both sides are equal to zero anyways.

	Then, in the limit as $h \to 0$,
	we have $\lim_{h \to 0} \frac{f(p+h)-f(p)}{h} = f'(p)$,
	while $\lim_{h \to 0} Q(f(p+h)) = Q(q) = g'(q)$ by continuity.
	This was the desired result. \qedhere
	\end{itemize}
\end{proof}

\begin{exercise}
	Compute the derivative of the polynomial $f(x) = x^3 + 10x^2 + 2019$,
	viewed as a function $f \colon \RR \to \RR$.
\end{exercise}

\begin{remark}
	Quick linguistic point:
	the theorems above all hold at each individual point.
	For example the sum rule really should say that
	if $f,g \colon U \to \RR$ are differentiable at the point $p$
	then so is $f+g$ and the derivative equals $f'(p) + g'(p)$.
	Thus $f$ and $g$ are differentiable on all of $U$,
	then it of course follows that $(f+g)' = f' + g'$.
	So each of the above rules has a ``point-by-point'' form
	which then implies the ``whole $U$'' form.

	We only state the latter since that is what is used in practice.
	However, in the rare situations where you have a function
	differentiable only at certain points of $U$ rather
	than the whole interval $U$, you can still use the below.
\end{remark}

We next list some derivatives of
well-known functions,
but as we do not give rigorous definitions
of these functions, we do not prove these here.
\begin{proposition}
	[Derivatives of some well-known functions]
	\listhack
	\begin{itemize}
		\ii The exponential function $\exp \colon \RR \to \RR$
		defined by $\exp(x) = e^x$ is its own derivative.
		\ii The trig functions $\sin$ and $\cos$
		have $\sin' = \cos$, $\cos' = -\sin$.
	\end{itemize}
\end{proposition}

\begin{example}
	[A typical high-school calculus question]
	This means that you can mechanically compute
	the derivatives of any artificial function obtained by using the above,
	which makes it a great source of busy work
	in American high schools and universities.
	For example, if
	\[ f(x) = e^x + x \sin(x^2) \qquad f \colon \RR \to \RR \]
	then one can compute $f'$ by:
	\begin{align*}
		f'(x) &= (e^x)' + (x \sin(x^2))' & \text{sum rule} \\
		&= e^x + (x \sin(x^2))' & \text{above table} \\
		&= e^x + (x)' \sin(x^2) + x (\sin(x^2))' & \text{product rule} \\
		&= e^x + \sin(x^2) + x (\sin(x^2))' & (x)' = 1 \\
		&= e^x + \sin(x^2) + x \cdot 2x \cdot \cos(x^2) & \text{chain rule}.
	\end{align*}
	Of course, this function $f$ is totally artificial and has no meaning,
	which is why calculus is the topic of widespread scorn in the USA.
	That said, it is worth appreciating that calculations like
	this are possible: it would be better to write the pseudo-theorem
	``derivatives can actually be computed''.
\end{example}

If we take for granted that $(e^x)' = e^x$,
then we can derive two more useful functions
to add to our library of functions we can differentiate.
\begin{corollary}
	[Power rule]
	Let $r$ be a real number.
	The function $\RR_{>0} \to \RR$ by $x \mapsto x^r$
	has derivative $(x^r)' = rx^{r-1}$.
\end{corollary}

\begin{proof}
	We knew this for integers $r$ already,
	but now we can prove it for any positive real number $r$.
	Write
	\[ f(x) = x^r = e^{r \log x} \]
	considered as a function $f \colon \RR_{>0} \to \RR$.
	The chain rule (together with the fact that $(e^x)' = e^x$)
	now gives
	\begin{align*}
		f'(x) &= e^{r \log x} \cdot (r \log x)' \\
		&= e^{r \log x} \cdot \frac rx = x^r \cdot \frac rx = rx^{r-1}.
	\end{align*}
	The reason we don't prove the formulas for $e^x$ and $\log x$
	is that we don't at the moment even have a rigorous
	definition for either, or even for $2^x$ if $x$ is not rational.
	However it's nice to know that some things imply the other.
\end{proof}
\begin{corollary}
	[Derivative of $\log$ is $1/x$]
	The function $\log \colon \RR_{>0} \to \RR$
	has derivative $(\log x)' = 1/x$.
\end{corollary}
\begin{proof}
	We have that $x = e^{\log x}$.
	Differentiate both sides, and again use the chain rule\footnote{There
		is actually a small subtlety here:
		we are taking for granted that $\log$ is differentiable.}
	\[ 1 = e^{\log x} \cdot (\log x)'. \]
	Thus $(\log x)' = \frac{1}{e^{\log x}} = 1/x$.
\end{proof}

\section{Local (and global) maximums}
\prototype{Horizontal tangent lines to the parabola
	are typically good pictures.}
You may remember from high school
that one classical use of calculus
was to extract the minimum or maximum values of functions.
We will give a rigorous description of how to do this here.

\begin{definition}
	Let $f \colon U \to \RR$ be a function.
	A \vocab{local maximum} is a point $p \in U$
	such that there exists an open neighborhood $V$ of $p$
	(contained inside $U$)
	such that $f(p) \ge f(x)$ for every $x \in V$.

	A \vocab{local minimum} is defined similarly.\footnote{Equivalently,
		it is a local maximum of $-f$.}
\end{definition}
\begin{definition}
	A point $p$ is a \vocab{local extrema}
	if it satisfies either of these.
\end{definition}

The nice thing about derivatives is that they pick up all extrema.
\begin{theorem}
	[Fermat's theorem on stationary points]
	Suppose $f \colon U \to \RR$ is differentiable
	and $p \in U$ is a local extrema.
	Then $f'(p) = 0$.
\end{theorem}

If you draw a picture, this result is not surprising.
\begin{center}
\begin{asy}
	import graph;
	size(7cm);
	real f(real x) { return 2-(x-2)*(x-2)/3; }
	graph.xaxis("$x$");
	graph.yaxis("$y$");
	draw(graph(f,-1,5,operator ..), blue, Arrows);
	pair P = (2, f(2));
	dot("$(p, f(p))$", P, dir(90), red);
	draw( (0.3,f(2))--(3.7,f(2)), red );
\end{asy}
\end{center}
(Note also: the converse is not true.
Say, $f(x) = x^{2019}$ has $f'(0) = 0$
but $x=0$ is not a local extrema for $f$.)

\begin{proof}
	Assume for contradiction $f'(p) > 0$.
	Choose any $\eps > 0$ with $\eps < f'(p)$.
	Then for sufficiently small $|h|$ we should have
	\[ \frac{f(p+h)-f(p)}{h} > \eps. \]
	In particular $f(p+h) > f(p)$ for $h > 0$
	while $f(p-h) < f(p)$ for $h < 0$.
	So $p$ is not a local extremum.

	The proof for $f'(p) < 0$ is similar.
\end{proof}

However, this is not actually adequate
if we want a complete method for optimization.
The issue is that we seek \emph{global} extrema,
which may not even exist:
for example $f(x) = x$ (which has $f'(x) = 1$)
obviously has no local extrema at all.
The key to resolving this is to use \emph{compactness}:
we change the domain to be a compact set $Z$,
for which we know that $f$ will achieve some global maximum.
The set $Z$ will naturally have some \emph{interior} $S$,
and calculus will give us all the extrema within $S$.
Then we manually check all cases outside $Z$.

Let's see two extended examples.
The one is simple, and you probably already know about it,
but I want to show you how to use compactness to argue thoroughly,
and how the ``boundary'' points naturally show up.

\begin{example}
	[Rectangle area optimization]
	Suppose we consider rectangles with perimeter $20$
	and want the rectangle with the smallest or largest area.
	\begin{center}
	\begin{asy}
		size(4cm);
		draw( (0,0)--(7,0)--(7,3)--(0,3)--cycle );
		label("$10-x$", (3.5,0), dir(-90));
		label("$x$", (0,1.5), dir(180));
	\end{asy}
	\end{center}
	If we choose the legs of the rectangle to be $x$ and
	$10-x$, then we are trying to optimize the function
	\[ f(x) = x(10-x) = 10x-x^2 \qquad f \colon [0,10] \to \RR. \]
	By compactness, there exists \emph{some} global maximum
	and \emph{some} global minimum.

	As $f$ is differentiable on $(0,10)$,
	we find that for any $p \in (0,10)$, a global maximum
	will be a local maximum too, and hence should satisfy
	\[ 0 = f'(p) = 10 - 2p \implies p = 5. \]
	Also, the points $x = 0$ and $x = 10$ lie in the domain
	but not the interior $(0,10)$.
	Therefore the global extrema (in addition to existing)
	must be among the three suspects $\{0, 5, 10\}$.

	We finally check $f(0) = 0$, $f(5) = 25$, $f(10) = 0$.
	So the $5 \times 5$ square has the largest area
	and the degenerate rectangles have the smallest (zero) area.
\end{example}

Here is a non-elementary example.
\begin{proposition}[$e^x \ge 1+x$]
	For all real numbers $x$ we have $e^x \ge 1+x$.
\end{proposition}
\begin{proof}
	Define the differentiable function
	\[ f(x) = e^x - (x+1) \qquad f \colon \RR \to \RR. \]
	Consider the compact interval $Z = [-1,100]$.
	If $x \le -1$ then obviously $f(x) > 0$.
	Similarly if $x \ge 100$ then obviously $f(x) > 0$ too.
	So we just want to prove that if $x \in Z$, we have $f(x) \ge 0$.

	Indeed, there exists \emph{some} global minimum $p$.
	It could be the endpoints $-1$ or $100$.
	Otherwise, if it lies in $U = (-1, 100)$
	then it would have to satisfy
	\[ 0 = f'(p) = e^p - 1 \implies p = 0. \]
	As $f(-1) > 0$, $f(100) > 0$, $f(0) = 0$,
	we conclude $p = 0$ is the global minimum of $Z$;
	and hence $f(x) \ge 0$ for all $x \in Z$, hence for all $x$.
\end{proof}

\begin{remark}
	If you are willing to use limits at $\pm \infty$,
	you can rewrite proofs like the above in such a way
	that you don't have to explicitly come
	up with endpoints like $-1$ or $100$.
	We won't do so here, but it's nice food for thought.
\end{remark}

\section{Rolle and friends}
\prototype{The racetrack principle, perhaps?}

One corollary of the work in the previous section is Rolle's theorem.
\begin{theorem}
	[Rolle's theorem]
	Suppose $f \colon [a,b] \to \RR$ is a continuous function,
	which is differentiable on the open interval $(a,b)$,
	such that $f(a) = f(b)$.
	Then there is a point $c \in (a,b)$ such that $f'(c) = 0$.
\end{theorem}

\begin{proof}
	Assume $f$ is nonconstant (otherwise any $c$ works).
	By compactness, there exists both a global maximum and minimum.
	As $f(a) = f(b)$, either the global maximum
	or the global minimum must lie inside the open interval $(a,b)$,
	and then Fermat's theorem on stationary points finishes.
\end{proof}

I was going to draw a picture until I realized xkcd \#2042 has one already.
\begin{center}
	\includegraphics[scale=0.66]{media/xkcd-rolles.png}
	\\ \scriptsize Image from \cite{img:xkcd_rolles}
\end{center}

One can adapt the theorem as follows.
\begin{theorem}
	[Mean value theorem]
	Suppose $f \colon [a,b] \to \RR$ is a continuous function,
	which is differentiable on the open interval $(a,b)$.
	Then there is a point $c \in (a,b)$ such that
	\[ f'(c) = \frac{f(b)-f(a)}{b-a}. \]
\end{theorem}

Pictorially, there is a $c$ such that the tangent at $c$
has the same slope as the secant joining $(a, f(a))$, to $(b, f(b))$;
and Rolle's theorem is the special case where that secant is horizontal.

\begin{center}
\begin{asy}
	import graph;
	size(7cm);
	real f(real x) { return x*x/2 - 0.2; }
	graph.xaxis("$x$");
	graph.yaxis("$y$");
	draw(graph(f,-2,2.5,operator ..), blue, Arrows);
	pair A = (-1, f(-1));
	pair B = (2, f(2));
	dot("$(a, f(a))$", A, dir(A-B), deepgreen);
	dot("$(b, f(b))$", B, dir(10), deepgreen);
	draw(A--B, deepgreen);
	label("Slope $\frac{f(b)-f(a)}{b-a}$", A--B, dir(120), deepgreen);
	draw(A--(A.x,0), deepgreen+dashed);
	draw(B--(B.x,0), deepgreen+dashed);
	label("$a$", (A.x,0), dir(-90), deepgreen);
	label("$b$", (B.x,0), dir(-90), deepgreen);

	real c = (A.y-B.y) / (A.x-B.x);
	pair C = (c, f(c));
	dot("$(c, f(c))$", C, dir(-70), red);
	draw( (c-1, f(c)-c)--(c+1, f(c)+c), red );
\end{asy}
\end{center}

\begin{proof}
	[Proof of mean value theorem]
	Let $s = \frac{f(b)-f(a)}{b-a}$ be the slope of the secant line,
	and define
	\[ g(x) = f(x) - s x \]
	which intuitively shears $f$ downwards so that the
	secant becomes vertical.
	In fact $g(a) = g(b)$ now, so we apply Rolle's theorem to $g$.
\end{proof}

\begin{remark}
	[For people with driver's licenses]
	There is a nice real-life interpretation of this I should mention.
	A car is travelling along a one-dimensional road
	(with $f(t)$ denoting the position at time $t$).
	Suppose you cover $900$ kilometers in your car
	over the course of $5$ hours
	(say $f(0) = 0$, $f(5) = 900$).
	Then there is \emph{some} point at time in which
	your speed at that moment was exactly $180$ kilometers per hour,
	and so you cannot really complain
	when the cops pull you over for speeding.
\end{remark}

The mean value theorem is important because it lets
you relate \textbf{use derivative information
to get information about the function}
in a way that is really not possible without it.
Here is one quick application to illustrate my point:

\begin{proposition}
	[Racetrack principle]
	Let $f, g \colon \RR \to \RR$ be two differentiable functions
	with $f(0) = g(0)$.
	\begin{enumerate}[(a)]
		\ii If $f'(x) \ge g'(x)$ for every $x > 0$,
		then $f(x) \ge g(x)$ for every $x > 0$.
		\ii If $f'(x) > g'(x)$ for every $x > 0$,
		then $f(x) > g(x)$ for every $x > 0$.
	\end{enumerate}
\end{proposition}

This proposition might seem obvious.
You can think of it as a race track for a reason:
if $f$ and $g$ denote the positions of two cars (or horses etc)
and the first car is always faster than the second car,
then the first car should end up ahead of the second car.
At a special case $g = 0$, this says that if $f'(x) \ge 0$,
i.e.\ ``$f$ is increasing'',
then, well, $f(x) \ge f(0)$ for $x > 0$, which had better be true.
However, if you try to prove this by definition from derivatives,
you will find that it is not easy!
However, it's almost a prototype for the mean value theorem.

\begin{proof}
	[Proof of racetrack principle]
	We prove (a). Let $h = f-g$, so $h(0) = 0$.
	Assume for contradiction $h(p) < 0$ for some $p > 0$.
	Then the secant joining $(0, h(0))$ to $(p, h(p))$ has negative slope;
	in other words by mean value theorem there is a $0 < c < p$
	such that
	\[ f'(c) - g'(c) = h'(c) = \frac{h(p)-h(0)}{p} = \frac{h(p)}{p} < 0 \]
	so $f'(c) < g'(c)$, contradiction.
	Part (b) is the same.
\end{proof}

Sometimes you will be faced with two functions which you cannot
easily decouple; the following form may be more useful in that case.
\begin{theorem}
	[Ratio mean value theorem]
	Let $f, g \colon [a,b] \to \RR$ be two continuous functions
	which are differentiable on $(a,b)$,
	and such that $g(a) \neq g(b)$.
	Then there is a $c \in (a,b)$ such that $g'(c) \neq 0$ and
	\[ \frac{f'(c)}{g'(c)} = \frac{f(b)-f(a)}{g(b)-g(a)}. \]
\end{theorem}
\begin{proof}
	Use Rolle's theorem on the function
	\[ h(x) = \left[ f(x)-f(a) \right] \left[ g(b)-g(a) \right]
		- \left[ g(x)-g(a) \right] \left[ f(b)-f(a) \right].
		\qedhere \]
\end{proof}
\begin{remark}
	You can capture the case $g(a) = g(b)$ as well
	if you are willing to write the conclusion
	in the less intuitive form $g'(c) \left[ f(b)-f(a) \right]
	= f'(c) \left[ g(b)-g(a) \right]$.
	In the event $g(a) = g(b)$ then this is just the mean value theorem
	for $g$, and the data of $f$ is irrelevant.
\end{remark}

\section{Smooth functions}
\prototype{All the functions you're used to.}

Let $f \colon U \to \RR$ be differentiable,
thus giving us a function $f' \colon U \to \RR$.
If our initial function was nice enough,
then we can take the derivative again,
giving a function $f'' \colon U \to \RR$, and so on.
In general, after taking the derivative $n$ times,
we denote the resulting function by $f^{(n)}$.
By convention, $f^{(0)} = f$.
\begin{definition}
	A function $f \colon U \to \RR$ is \vocab{smooth}
	if it is infinitely differentiable;
	that is the function $f^{(n)}$ exists for all $n$.
\end{definition}
\begin{ques}
	Show that the absolute value function is not smooth.
\end{ques}

Most of the functions we encounter,
such as polynomials, $e^x$, $\log$, $\sin$, $\cos$
are smooth, and so are their compositions.
Here is a weird example which we'll grow more next time.
\begin{example}
	[A smooth function with all derivatives zero]
	Consider the function
	\[ f(x) = \begin{cases}
			e^{-1/x} & x > 0 \\
			0 & x \le 0.
		\end{cases}
	\]
	This function can be shown to be smooth,
	with $f^{(n)}(0) = 0$.
	So this function has every derivative at the origin
	equal to zero, despite being nonconstant!
\end{example}

\section{\problemhead}
\begin{problem}
	[Quotient rule]
	Let $f \colon (a,b) \to \RR$ and $g \colon (a,b) \to \RR_{>0}$
	be differentiable functions.
	Let $h = f/g$ be their quotient
	(also a function $(a,b) \to \RR$).
	Show that the derivative of $h$ is given by
	\[ h'(x) = \frac{f'(x) g(x) - f(x) g'(x)}{g(x)^2}. \]
\end{problem}

\begin{problem}
	For real numbers $x > 0$, how small can $x^x$ be?
\end{problem}

\begin{problem}
	[RMM 2017]
	\gim
	Determine whether or not there exist
	nonconstant polynomials $P(x)$ and $Q(x)$ with
	real coefficients satisfying
	\[ P(x)^{10} + P(x)^9 = Q(x)^{21} + Q(x)^{20}. \]
\end{problem}

\begin{problem}
	\gim
	Let $P(x)$ be a degree $n$ polynomial with real coefficients.
	Prove that the equation $e^x = P(x)$ has at most $n+1$ real solutions in $x$.
\end{problem}

\begin{problem}
	[Jensen's inequality]
	Let $f \colon (a,b) \to \RR$ be a twice differentiable function
	such that $f''(x) \ge 0$ for all $x$
	(i.e.\ $f$ is \emph{convex}).
	Prove that
	\[ f\left( \frac{x+y}{2} \right)
		\le \frac{f(x) + f(y)}{2} \]
	for all real numbers $x$ and $y$ in the interval $(a,b)$.
\end{problem}

\begin{problem}
	[L'H\^{o}pital rule, or at least one case]
	Let $f,g \colon \RR \to \RR$ be differentiable functions
	and let $p$ be a real number.
	Suppose that
	\[ \lim_{x \to p} f(x) = \lim_{x \to p} g(x) = 0. \]
	Prove that
	\[ \lim_{x \to p} \frac{f(x)}{g(x)}
		= \lim_{x \to p} \frac{f'(x)}{g'(x)} \]
	provided the right-hand limit exists.
\end{problem}


\section{Methodology}
\gls{temoa} is an open source tool for energy system optimization that
formulates and solves a linear optimization problem
\cite{decarolis_tools_2020}. A linear optimization
problem has two requirements: An objective function and constraints. The
objective function in \gls{temoa} is total system cost over time horizon
of interest and the minimum required constraint is annual demand (and
technology options to meet that demand). Users can optionally add other
constraints to match the real system being modeled. In our case we added
emissions limits based on the carbon goals set by \gls{icap}. At each time step,
\gls{temoa} must be able to meet the various constraints with the existing
capacity, or build be able to build new capacity to do so. If demand and
emissions limits cannot be satisfied, then \gls{temoa} gives ``no solution.''
Mathematically, \gls{temoa} solves the following problem:
\begin{align}
  \intertext{Minimize}
  C_{tot} &= C_{cap} + C_{fix} + C_{var}\\
  \intertext{Subject to:}
  D_i &= \sum_{tech}A_{tech} \mbox{ for } i \in \mbox{ years}
\end{align}


\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage{slashed}
\usepackage{tikz}

\begin{document}

\noindent
Consider an electron scattered by an atomic nucleus.
\begin{center}
\begin{tikzpicture}
\draw[dashed] (0,0) circle (0.5cm);
%\draw[dashed] (1.4,0) -- (0.6,0);
\draw[thick,->] (-2,0) node[anchor=east] {$e^-$} -- (-0.6,0);
\draw[thick,->] (0.40,0.40) -- (1.3,1.3) node[anchor=south west] {$e^-$};
\draw (1,0.5) node {$\theta$};
\end{tikzpicture}
\end{center}

\noindent
Here is the same diagram with momentum and spinor labels.
\begin{center}
\begin{tikzpicture}
\draw[dashed] (0,0) circle (0.5cm);
%\draw[dashed] (1.4,0) -- (0.6,0);
\draw[thick,->] (-2,0) node[anchor=east] {$p_1,u_1$} -- (-0.6,0);
\draw[thick,->] (0.40,0.40) -- (1.3,1.3) node[anchor=south west] {$p_2,u_2$};
\draw (1,0.5) node {$\theta$};
\end{tikzpicture}
\end{center}

\noindent
The path of the incident electron can be modeled as the $z$ axis,
resulting in the following momentum vectors.
\begin{equation*}
\underset{\text{inbound electron}}
{p_1=\begin{pmatrix}E\\0\\0\\p\end{pmatrix}}
\quad
\underset{\text{outbound electron}}
{
p_2=\begin{pmatrix}
E\\
p\sin\theta\cos\phi\\
p\sin\theta\sin\phi\\
p\cos\theta
\end{pmatrix}
}
\end{equation*}

\noindent
Symbol $p$ is incident momentum,
$E$ is total energy $E=\sqrt{p^2+m^2}$,
and $m$ is electron mass.
Polar angle $\theta$ is the observed scattering angle.
Azimuth angle $\phi$ cancels out in scattering calculations.

\bigskip
\noindent
The spinors are
\begin{equation*}
\underset{\text{inbound electron, spin up}}
{u_{11}=\begin{pmatrix}E+m\\0\\p\\0\end{pmatrix}}
\quad
\underset{\text{inbound electron, spin down}}
{u_{12}=\begin{pmatrix}0\\E+m\\0\\-p\end{pmatrix}}
\quad
\underset{\text{outbound electron, spin up}}
{u_{21}=\begin{pmatrix}E+m\\0\\p_{2z}\\p_{2x}+ip_{2y}\end{pmatrix}}
\quad
\underset{\text{outbound electron, spin down}}
{u_{22}=\begin{pmatrix}0\\E+m\\p_{2x}-ip_{2y}\\-p_{2z}\end{pmatrix}}
\end{equation*}

\noindent
The spinors shown above are not individually normalized.
Instead, a combined spinor normalization constant $N=(E+m)^2$ will be used.

\bigskip
\noindent
The following formula computes a probability density $|\mathcal{M}_{ab}|^2$ for Rutherford scattering
where $a$ is the spin state of the inbound electron and $b$ is the spin state of the outbound electron.
\begin{equation*}
|\mathcal{M}_{ab}|^2=\frac{Z^2e^4}{q^4}\frac{1}{N}\left|\bar{u}_{2b}\gamma^0 u_{1a}\right|^2
\end{equation*}

\noindent
Symbol $Z$ is the atomic number of the nucleus,
$e$ is electron charge,
and $q=p_1-p_2$ is momentum transfer.

\bigskip
\noindent
The expected probability density
$\langle\vert\mathcal{M}\vert^2\rangle$
is computed by summing $|\mathcal{M}_{ab}|^2$
over all four spin states and then dividing by the number of inbound states.
There are two inbound states.
\begin{align*}
\langle\vert\mathcal{M}\vert^2\rangle
&=\frac{1}{2}\sum_{a=1}^2\sum_{b=1}^2\left|\mathcal{M}_{ab}\right|^2
\\
&=\frac{Z^2e^4}{2q^4}\frac{1}{N}\sum_{a=1}^2\sum_{b=1}^2\left|\bar{u}_{2b}\gamma^0 u_{1a}\right|^2
\\
&=\frac{Z^2e^4}{2q^4}\mathop{\rm Tr}\left((\slashed{p}_1+m)\gamma^0(\slashed{p}_2+m)\gamma^0\right)
\\
&=\frac{2Z^2e^4}{q^4}\left(E^2+m^2+p^2\cos\theta\right)
\end{align*}

\noindent
Run ``rutherford-scattering-1.txt'' to verify the following formulas.
\begin{gather*}
\frac{1}{N}\sum_{a=1}^2\sum_{b=1}^2\left|\bar{u}_{2b}\gamma^0 u_{1a}\right|^2
=\mathop{\rm Tr}\left((\slashed{p}_1+m)\gamma^0(\slashed{p}_2+m)\gamma^0\right)
=4(E^2+m^2+p^2\cos\theta)
\\
q^4=(p_1-p_2)^4=16p^4\sin^4(\theta/2)=4p^4(\cos\theta-1)^2
\end{gather*}

\subsection*{Low energy approximation}
For low energy electrons such that $p\ll m$ we can use the following approximation.
\begin{equation*}
E^2+m^2+p^2\cos\theta\approx2m^2
\end{equation*}

\noindent
Hence
\begin{equation*}
\langle|\mathcal{M}|^2\rangle=\frac{4m^2Z^2e^4}{q^4}
\end{equation*}

\noindent
Substituting $e^4=16\pi^2\alpha^2$ and $q^4=4p^4(\cos\theta-1)^2$ we have
\begin{equation*}
\langle|\mathcal{M}|^2\rangle=\frac{16\pi^2m^2Z^2\alpha^2}{p^4(\cos\theta-1)^2}
\end{equation*}

\noindent
The differential cross section is
\begin{equation*}
\frac{d\sigma}{d\Omega}=\frac{\langle|\mathcal{M}|^2\rangle}{16\pi^2}
=\frac{m^2Z^2\alpha^2}{p^4(\cos\theta-1)^2}
\end{equation*}

\noindent
We can integrate $d\sigma$ to obtain a cumulative distribution function.
Recall that
\begin{equation*}
d\Omega=\sin\theta\,d\theta\,d\phi
\end{equation*}
Hence
\begin{equation*}
d\sigma=\frac{m^2Z^2\alpha^2}{p^4(\cos\theta-1)^2}\sin\theta\,d\theta\,d\phi
\end{equation*}

\noindent
Let $I(\theta)$ be the following integral of $d\sigma$.
\begin{align*}
I(\theta)
&=\left(\frac{p^4}{m^2Z^2\alpha^2}\right)\frac{1}{2\pi}\int_0^{2\pi}\int d\sigma
\\
&=\int\frac{1}{(\cos\theta-1)^2}\sin\theta\,d\theta
\\
&=\frac{1}{\cos\theta-1},\quad a\le\theta\le\pi
\end{align*}

\noindent
Angular support is limited to an arbitrary $a>0$ because $I(0)$ is undefined.

\bigskip
\noindent
Let $C$ be the normalization constant
\begin{equation*}
C=I(\pi)-I(a)
\end{equation*}

\noindent
Then the cumulative distribution function $F(\theta)$ is
\begin{equation*}
F(\theta)=\frac{I(\theta)-I(a)}{C},
\quad a\le\theta\le\pi
\end{equation*}

\noindent
The probability of observing scattering events
in the interval $\theta_1$ to $\theta_2$ can now be computed.
\begin{equation*}
P(\theta_1\le\theta\le\theta_2)=F(\theta_2)-F(\theta_1)
\end{equation*}

\noindent
Probability density function $f(\theta)$ is the derivative of $F(\theta)$.
\begin{equation*}
f(\theta)=\frac{dF(\theta)}{d\theta}
=\frac{\sin\theta}{C(\cos\theta-1)^2}
\end{equation*}

\noindent
Run ``rutherford-scattering-3.txt'' to draw a graph of $f(\theta)$ for $a=\pi/4=45^\circ$.

\begin{center}
\includegraphics[scale=0.5]{rutherford-scattering-1.png}
\end{center}

\noindent
Probability distribution for $45^\circ$ bins ($a=45^\circ$).

\begin{center}
\begin{tabular}{|c|c|c|}
\hline
$\theta_1$ & $\theta_2$ & $P(\theta_1\le\theta\le\theta_2)$\\
\hline
$0^\circ$ & $45^\circ$ & -- \\
$45^\circ$ & $90^\circ$ & 0.83 \\
$90^\circ$ & $135^\circ$ & 0.14 \\
$135^\circ$ & $180^\circ$ & 0.03 \\
\hline
\end{tabular}
\end{center}

\noindent
Note:
The original Rutherford scattering experiment in 1911 used alpha particles, not electrons.
However, scattering of any charged particle by Coulomb interaction
is now known as Rutherford scattering.
The first Rutherford scattering experiment using electrons appears to have
been done by F.~L.~Arnot, then a student of Rutherford, in 1929.

\end{document}


\section{Polar coordinates and polar graphs}

\begin{outcome}
  \begin{enumerate}
  \item Understand polar coordinates.
  \item Convert points between Cartesian and polar coordinates.
  \end{enumerate}
\end{outcome}

You have likely encountered the Cartesian coordinate system in many aspects of mathematics. There is an alternative way to represent points in space, called \textbf{polar
coordinates}\index{polar coordinates}. The idea is suggested in the following picture.

\begin{center}
\begin{tikzpicture}
\draw(-0.5,0)--(2,0);
\draw(0,-0.5)--(0,2);
\draw[blue, ultra thick](0,0)--(2,2);
\draw[fill, blue](2,2) circle [radius=1pt];
\draw[help lines, dotted](0,2)--(2,2)--(2,0);
\node[below] at (2,0){$x$};
\node[above] at (0,2){$y$};
\node[above right] at (0.5, 0){$\theta$};
\node[left] at (1,1){$r$};
\node[right] at (2,2){$(x,y)$};
\node[below right] at (2,1.9){$(r, \theta)$};
\end{tikzpicture}
\end{center}

Consider the point above, which would be specified as $(x,y)$ in Cartesian coordinates. We can also specify this point using polar coordinates, which we write as $(r, \theta)$. The number $r$ is the distance from the origin$(0,0)$ to the point, while $\theta $ is the angle shown
between the positive $x$ axis and the line from the origin to the point. In this way, the point can be specified in polar coordinates as $(r, \theta)$.

Now suppose we are given an ordered pair $(r,\theta) $ where
$r$ and $\theta$ are real numbers. We want to determine the point specified by this ordered pair. We can use $\theta $ to identify a ray
from the origin as follows. Let the ray pass from $(0,0) $
through the point $(\cos \theta ,\sin \theta) $ as shown.

\begin{center}
\begin{tikzpicture}
\draw(-2,0)--(2,0);
\draw(0,-2)--(0,2);
\draw(0,0) circle [radius = 1.75];
\draw[->, ultra thick, blue] (0,0)--(2,2);
\draw[fill, blue](0,0) circle [radius=2pt];
\draw[fill, blue](1.24,1.24) circle [radius=2pt];
\node[right] at (1.5, 1.24){$(cos (\theta), sin(\theta))$};
\end{tikzpicture}
\end{center}

The ray is identified on the graph as the line from the origin, through the point $(\cos(\theta),\sin(\theta))$. Now if $r>0$, go a distance
equal to $r$ in the direction of the displayed arrow starting at $(0,0)$. If
$r<0$, move in the opposite direction a distance of $\abs{r}
$. This is the point determined by $(r,\theta)$.

It is common to assume that $\theta $ is in the interval $
[0,2\pi )$ and $r>0$. In this case, there is a very simple relationship
between the Cartesian and polar coordinates, given by
\begin{equation}
x=r\cos (\theta) ,\ \ y=r\sin (\theta)
\label{cart-pol-coord}
\end{equation}

These equations demonstrate how to find the Cartesian coordinates when we are given the polar coordinates of a point. They can also be used to find the polar coordinates when we know $(x, y)$. A simpler way to do this is the following equations:
\begin{equation}
\begin{array}{l}
r = \sqrt{x^2 + y^2} \\
\\
\tan (\theta) = \frac{y}{x}
\end{array}
\label{pol-cart-coord}
\end{equation}

In the next example, we look at how to find the Cartesian coordinates of a point specified by polar coordinates.

\begin{example}{Finding Cartesian coordinates}{}
The polar coordinates of a point in the plane are $(5,\pi /6)$.
Find the Cartesian coordinates of this point.
\end{example}

\begin{solution}
The point is specified by the polar coordinates $(5,\pi /6)$. Therefore $r=5$ and $\theta = \pi /6$.
From {\eqref{cart-pol-coord}}
\[
x= r \cos (\theta)= 5\cos \paren{\vspace{0.05in}\frac{\pi }{6}} = \vspace{0.05in}\frac{5}{2}\sqrt{3}
\]
\[
y= r \sin (\theta) = 5\sin \paren{\vspace{0.05in}\frac{\pi }{6}} = \vspace{0.05in}\frac{5}{2}
\]
Thus the Cartesian coordinates are $\paren{\vspace{0.05in}\frac{5}{2}\sqrt{3}, \vspace{0.05in}\frac{5}{2}}$. The point is shown in the below graph.

\begin{center}
\begin{tikzpicture}[scale=0.5]
\draw(-5,0)--(5,0);
\draw(0,-4)--(0,4);
\draw[help lines, blue, dotted](0, 2.5)--(4.33, 2.5)--(4.33,0);
\draw[fill, blue] (4.33, 2.5) circle [radius=4pt];
\node[right] at (4.33, 2.5){$(\frac{5}{2}\sqrt{3}, \frac{5}{2})$};
\end{tikzpicture}
\end{center}
\end{solution}

Consider the following example of the case where $r < 0$.

\begin{example}{Finding Cartesian coordinates}{}
The polar coordinates of a point in the plane are $(-5,\pi /6)$.
Find the Cartesian coordinates.
\end{example}

\begin{solution}
For the point specified by the polar coordinates $(-5, \pi /6)$,
$r=-5$, and $x\theta = \pi /6$.
From {\eqref{cart-pol-coord}}
\[
x= r \cos (\theta)= -5\cos \paren{\vspace{0.05in}\frac{\pi }{6}} = -\vspace{0.05in}\frac{5}{2}\sqrt{3}
\]
\[
y= r \sin (\theta) = -5\sin \paren{\vspace{0.05in}\frac{\pi }{6}} = -\vspace{0.05in}\frac{5}{2}
\]
Thus the Cartesian coordinates are $\paren{-\vspace{0.05in}\frac{5}{2}\sqrt{3}, -\vspace{0.05in}\frac{5}{2}}$. The point is shown in the following graph.

\begin{center}
\begin{tikzpicture}[scale=0.5]
\draw(-5,0)--(5,0);
\draw(0,-4)--(0,4);
\draw[help lines, blue, dotted](0, -2.5)--(-4.33, -2.5)--(-4.33,0);
\draw[fill, blue] (-4.33, -2.5) circle [radius=4pt];
\node[left] at (-4.33, -2.5){$(-\frac{5}{2}\sqrt{3}, -\frac{5}{2})$};
\end{tikzpicture}
\end{center}

Recall from the previous example that for the point specified by $(5, \pi /6)$, the Cartesian coordinates are $\paren{\vspace{0.05in}\frac{5}{2}\sqrt{3}, \vspace{0.05in}\frac{5}{2}}$. Notice that in this example, by multiplying $r$ by $-1$, the resulting Cartesian coordinates are also multiplied by $-1$.
\end{solution}

The following picture exhibits both points in the above two examples to
emphasize how they are just on opposite sides of $(0,0) $ but at
the same distance from $(0,0)$.

\begin{center}
\begin{tikzpicture}[scale=0.5]
\draw(-5,0)--(5,0);
\draw(0,-4)--(0,4);
\draw[help lines, blue, dotted](0, 2.5)--(4.33, 2.5)--(4.33,0);
\draw[fill, blue] (4.33, 2.5) circle [radius=4pt];
\node[right] at (4.33, 2.5){$(\frac{5}{2}\sqrt{3}, \frac{5}{2})$};
\draw[help lines, blue, dotted](0, -2.5)--(-4.33, -2.5)--(-4.33,0);
\draw[fill, blue] (-4.33, -2.5) circle [radius=4pt];
\node[left] at (-4.33, -2.5){$(-\frac{5}{2}\sqrt{3}, -\frac{5}{2})$};
\draw[blue, ultra thick](-4.33, -2.5)--(4.33, 2.5);
\end{tikzpicture}
\end{center}

In the next two examples, we look at how to convert Cartesian coordinates to polar coordinates.

\begin{example}{Finding polar coordinates}{}
Suppose the Cartesian coordinates of a point are $(3,4)$. Find
a pair of polar coordinates which correspond to this point.
\end{example}

\begin{solution}
Using equation {\eqref{pol-cart-coord}}, we can find $r$ and $\theta$. Hence $r=\sqrt{3^{2}+4^{2}}=5$. It remains to identify the angle $\theta$ between the positive $x$ axis and the line from the origin to the point. Since both the $x$ and $y$ values are positive, the point is in the
first quadrant. Therefore, $\theta$ is between $0$ and $\pi/2$.
Using this and {\eqref{pol-cart-coord}}, we have to solve:
\[
\tan(\theta)=\frac{4}{3}
\]
Conversely, we can use equation {\eqref{cart-pol-coord}} as follows:
\[
3=5\cos (\theta)
\]
\[
4 = 5\sin (\theta)
\]
Solving these equations, we find that,
approximately, $\theta =0.\, 927\,295$ radians.
\end{solution}

Consider the following example.

\begin{example}{Finding polar coordinates}{}
Suppose the Cartesian coordinates of a point are $(-\sqrt{3},1)$
. Find the polar coordinates which correspond to this point.
\end{example}

\begin{solution}
Given the point $(-\sqrt{3}, 1)$,
\begin{eqnarray*}
r &=& \sqrt{ 1^2 + (-\sqrt{3})^2}\\
&=& \sqrt{1 + 3}\\
&=&2
\end{eqnarray*}
 In this case, the point is in the second quadrant since the $x$ value is negative and the $y$ value is positive. Therefore, $\theta$ will be between $\pi/2$ and $\pi$.
Solving the equations
\[
-\sqrt{3}= 2 \cos (\theta)
\]
\[
1 = 2 \sin (\theta)
\]

we find that $\theta = 5\pi /6$.
Hence the polar coordinates for this point are $(2, 5\pi /6)$.
\end{solution}

Consider this example. Suppose we used $r=-2$ and $\theta =2\pi -(\pi /6) = 11\pi /6$. These coordinates specify the same point as above. Observe that there are infinitely many ways to identify this
particular point with polar coordinates. In fact, every point can be represented with polar coordinates in infinitely many ways. Because of this, it will usually be
the case that $\theta $ is confined to lie in some interval
of length $2\pi $ and $r>0$, for real numbers $r$ and $\theta$.

Just as with Cartesian coordinates, it is possible to use
relations between the polar coordinates to specify points in the plane. The
process of sketching the graphs of these relations is very similar to that used to sketch
graphs of functions in Cartesian coordinates. Consider a relation between polar coordinates of the form, $r=f(\theta)$. To graph such a relation, first make a table of
the form
\begin{equation*}
\begin{tabular}{|l|l|}
\hline
$\theta $ & $r$ \\ \hline
$\theta _{1}$ & $f(\theta _{1}) $ \\ \hline
$\theta _{2}$ & $f(\theta _{2}) $ \\ \hline
$\vdots $ & $\vdots $ \\ \hline
\end{tabular}
\end{equation*}
Graph the resulting points and connect them with a curve. The
following picture illustrates how to begin this process.

\begin{center}
\begin{tikzpicture}
\draw(-0.5,0)--(3,0);
\draw(0,-0.5)--(0,3);
\draw[blue](0,0)--(2.5,1);
\draw[red](0,0)--(2.5,2.5);
\draw[fill,blue] (2,0.8) circle [radius=2pt];
\draw[fill,red] (1.25,1.25) circle [radius=2pt];
\draw[blue](1,0) to [out=90, in=90] (0.9,0.36);
\draw[red](0.75,0) to [out=90, in=90] (0.6,0.6);
\node[right] at (1,0.25){$\theta_1$};
\node[right] at (0.75, 0.75){$\theta_2$};
\end{tikzpicture}
\end{center}

To find the point in the plane corresponding to the ordered pair $(f(
\theta) ,\theta)$, we follow the same process as when finding the point corresponding to $(r, \theta)$.

Consider the following example of this procedure, incorporating computer software.

\begin{example}{Graphing a polar equation}{}
Graph the polar equation $r=1+\cos \theta$.
\end{example}

\begin{solution}
We will use the computer software {\em Maple} to complete this example. The command which produces the polar graph of the above equation is: $>$ plot(1+cos(t),t=
0..2*Pi,coords=polar). Here we use $t$ to represent the variable $\theta$ for convenience. The command tells Maple that $r$
is given by $1+\cos (t) $ and that $t\in \mat{0,2\pi}$.

\begin{picture}(1,120)
\put(110,-25){
\includegraphics[bb=0 0 400
400,totalheight=3cm]{figures/cardioid.eps}
\put(30,73){\large{x}}
\put(-35,128){\large{y}}}
\end{picture}

The above graph makes sense when considered in terms of trigonometric functions. Suppose $\theta =0,r=2$ and let $\theta $ increase to $\pi /2$. As $\theta$ increases, $\cos \theta $ decreases to 0. Thus the line from the origin to the point on the curve should get shorter as $\theta $ goes from $0$ to $\pi /2$. As $\theta$ goes from $\pi /2$ to $\pi$, $\cos
\theta $ decreases, eventually equalling $-1$ at $\theta =\pi$. Thus $r=0$
at this point. This scenario is depicted in the above graph, which shows a function called a \textbf{cardioid}\index{cardioid}.

The following picture illustrates the
above procedure for obtaining the polar graph of $r=1+\cos(\theta)$. In this picture, the
concentric circles correspond to values of $r$ while the rays from the
origin correspond to the angles which are shown on the picture. The dot on the ray corresponding to the angle $\pi/6$ is located at a distance of $r = 1+\cos(\pi/6)$ from the origin. The dot on the ray corresponding to the angle $\pi/3$
is located at a distance of $r = 1+\cos(\pi/3)$ from the origin and so
forth. The polar graph is obtained by connecting such points with a smooth
curve, with the result being the figure shown above.

\begin{picture}(1,290)
\put(30,-58){
\includegraphics[bb=0 0 400
400,totalheight=7cm]{figures/25aprilcardioid.eps}
%\put(85,128){\large{x}}
%\put(-117,328){\large{y}}\put(80,170){$y=\ln(x)$}
%\put(-90,328){$y=\exp(x)$}
}
\end{picture}
\end{solution}

Consider another example of constructing a polar graph.

\begin{example}{A polar graph}{}
Graph $r=1+2\cos \theta $ for $\theta \in \mat{
0,2\pi}$.
\end{example}

\begin{solution}
The graph of the polar equation $r=1+2\cos \theta $ for $\theta \in \mat{
0,2\pi}$ is given as follows.

\begin{picture}(1,127)
\put(110,-25){
\includegraphics[bb=0 0 400
400,totalheight=3cm]{figures/polarpretty2.eps}
\put(34,79){\large{x}}
\put(-47,138){\large{y}}}
\end{picture}

To see the way this is graphed, consider the following picture. First the
indicated points were graphed and then the curve was drawn to connect the points. When done by a computer, many more points are used to create a more accurate picture.

Consider first the following table of points.
\begin{equation*}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|}
\hline
$\theta $ & $\pi /6$ & $\pi /3$ & $\pi /2$ & $5\pi /6$ & $\pi $ & $4\pi /3$
& $7\pi /6$ & $5\pi /3$ \\ \hline
$r$ & $\sqrt{3}+1$ & $2$ & $1$ & $1-\sqrt{3}$ & $-1$ & $0$ & $1-\sqrt{3}$ & $%
2$ \\ \hline
\end{tabular}%
\end{equation*}

Note how some entries in the table have $r<0$. To graph these points, simply move in the opposite direction. These types of points are responsible for the small loop on the inside of the
larger loop in the graph.

\begin{picture}(1,290)
\put(30,-58){
\includegraphics[bb=0 0 400
400,totalheight=7cm]{figures/26aprilwriggly.eps}
%\put(85,128){\large{x}}
%\put(-117,328){\large{y}}\put(80,170){$y=\ln(x)$}
%\put(-90,328){$y=\exp(x)$}
}
\end{picture}
\end{solution}

The process of constructing these graphs can be greatly facilitated by computer software. However, the use of such software should not replace understanding the steps involved.

The next example shows the graph for the equation $r=3+\sin \paren{
\displaystyle
\frac{7\theta }{6}}$. For complicated polar graphs, computer software is used to facilitate the process.

\begin{example}{A polar graph}{}
Graph $r=3+\sin \paren{\displaystyle \frac{7\theta }{6}%
} $ for $\theta \in \mat{0,14\pi}$.
\end{example}

\begin{solution}

\begin{picture}(1,127)
\put(110,-25){
\includegraphics[bb=0 0 400
400,totalheight=3cm]{figures/polarpretty.eps}
\put(34,79){\large{x}}
\put(-20,138){\large{y}}}
\end{picture}
\end{solution}

The next example shows another situation in which $r$ can be negative.

\begin{example}{A polar graph: negative $r$}{}
Graph $r=3\sin(4\theta) $ for $\theta \in \mat{0,2\pi}$.
\end{example}

\begin{solution}

\begin{picture}(1,120)
\put(120,-24){
\includegraphics[bb=0 0 400
400,totalheight=3cm]{figures/26aprilrose.eps}}
\put(242,53){\large{x}}
\put(190,105){\large{y}}

\end{picture}
\end{solution}

We conclude this section with an interesting graph of a simple polar equation.

\begin{example}{The graph of a spiral}{}
Graph $r=\theta$ for $\theta \in [0,2\pi]$.
\end{example}

\begin{solution}
The graph of this polar equation is a spiral. This is the case because as $\theta $ increases, so does $r$.

\begin{picture}(1,290)
\put(30,-58){
\includegraphics[bb=0 0 400
400,totalheight=7cm]{figures/11mayspiral.eps}
\put(85,205){\large{x}}
\put(-55,335){\large{y}}}
\end{picture}
\end{solution}

In the next section, we will look at two ways of generalizing polar coordinates to three dimensions.


\chapter{Bootstrap}

% \Chapter{3}{Exercise 3: Bootstrap}
\section{Simulation of bootstrap confidence intervals}
The third exercise is about the bootstrap method to construct confidence intervals (CI) for different distribution measures, e.g. standard deviation (SD) and median here. The first part is a simulation study to examine coverage properties and length of the bootstrap percentile CI (bCI) for SD and median for a Weibull distribution (with parameters $\lambda = 13$ and $k=1$ - so it is actually an exponential distribution). Therefore we drew $M$ Monte Carlo samples of $n$ Weibull distributed variables. For each sample we did $R$ bootstrap repetitions where I used a n-out-of-n method with replacement like described in the lecture. The bCI at level $95\%$ was then defined as the interval between the 0.25th and the 0.75th quantile of the bootstrap distribution for the respective statistic. For each MC sample I checked whether the true SD and median where covered and saved the length of the CIs. Where $M=1000$ was fixed we compare the results for different choices of $n$ and $R$, i.e. we varied the actual sample size and the number of bootstrap runs. The results are shown in Table \ref{3table}.
\begin{table}[hb]
\centering
\begin{tabular}{rrrrrr}
  \hline
  && \multicolumn{2}{r}{Cover probability} &  \multicolumn{2}{r}{Avg CI length} \\
  \hline
 R & n & \hspace{0.5cm}med &\hspace{0.5cm} SD &\hspace{0.5cm} med &\hspace{0.5cm} SD \\
  \hline
 1000 & 100 & 0.94 & 0.86 & 5.02 & 6.18 \\
 1000 & 1000 & 0.93 & 0.94 & 1.60 & 2.21 \\
 5000 & 100 & 0.95 & 0.86 & 5.18 & 6.06 \\
   \hline
 \texttt{bcanon}&&&&&\\
 \hline
 1000 & 100&1.00&1.00 & 3.91 & 7.42\\
 \hline
\end{tabular}
\caption{Coverage probability and average CI length for median and SD for the MC simulation of $95\%$ bootstrap percentile CIs with sample size $n$ and $R$ bootstrap repetitions and for the bootstrap accelerated bias-corrected CI using \texttt{bcanon}.}
\label{3table}
\end{table}
As we took the $95\%$ bCI we would expect a corresponding coverage probability. For the median this is almost achieved in every row. We also see that the differences between the first and the third row are rather small and that the cover probability lies beneath the desired level. On the other hand the coverage for SD increases notable when we increase the sample size $n$ and is about $95\%$. Simultaneously the length of the bCI drastically decreased, showing that we get a higher precision for both statistic. This precision is not gained by using more bootstrap repetitions. This demonstrates that the amount of information we can pull from a fix sample size is bounded and can't be arbitrarily increased using more bootstrap samples. On the other hand, increasing the sample size, i.e. collecting more \textit{independent} information helps estimating better CI.

To visualise the empirical distribution of the sample statistics I include histograms for the SD and median of the MC samples together with a bCI for an additional sample and histograms of the CI lengths with the length for this additional sample (see Figure \ref{3hist}). The bCIs of this example cover 0.81 of the samples' SD and 0.89 of the median. This is a bit lower than the ones shown in the first row of Table \ref{3table} since the empirical estimates vary independently of the CI around the true statistics.
\begin{figure}[thb]
\centering
\includegraphics[width=0.8\textwidth, keepaspectratio]{ex3/MChist.png}
\caption{Histogram for 1000 MC samples of the two statistics (first row) and the length of the $95\%$ bCI (second row). The red lines show the bCI for an independent sample (first row) and the respective length (second row).}
\label{3hist}
\end{figure}

Instead of using just the percentiles of the bootstrap distribution there is the approach of bias corrected bootstrap CIs. The function \texttt{bcanon} provided in the package \textit{bootstrap} computes an accelerated and bias-corrected bootstrap CI. A MC simulation gives good results (see last row in Table \ref{3table}) as the true statistics are always covered. The length of the CI for the median is smaller but for the SD it is greater than the ones in the first row. The mean value for the bias correction $\hat{z}_0$ is about $-0.026$ for the median and $0.124$ for the standard deviation. Taking the value of the standard normal cdf of the values shows us that on average 0.49 of the bootstrap medians are smaller than the sample median. The same holds for 0.55 of the bootstrap standard deviations respectively. On average one would expect those values close to 0.5 and thus the average $\hat{z}_0$ close to 0. The acceleration constant is computed by a transformation of the Jackknife statistics and lies at a scale of $10^{-15}$ in this case and thus is negligible. Looking at the formula this is not surprising. The median and standard deviation should not change remarkable when leaving out one instance. Especially the median ranges between at most three values anyway. Together with the small sample size of 100 the sum of distances from the Jackknife statistics' mean is not very high. Therefore we can say that the CI is constructed by taking the $\lfloor R\alpha_1 \rfloor$th and the $\lfloor R\alpha_2\rfloor$th instances of the ordered bootstrap statistics, where $\alpha_1=\Phi (z_{\alpha /2}+2\hat{z}_0)$, $\alpha_2=\Phi (z_{1-\alpha /2}+2\hat{z}_0)$ and $\Phi$ denotes the standard normal cdf.

\section{Application for Sleep Heart Health Study data}
In this section we use the bootstrap method to build CIs for a real data set. We are just concerning the variable \texttt{rdi4p} encoding the respiratory disturbance index. The histogram (Figure \ref{3histdata}) shows the distribution of the data. As it looks like an exponential distribution (i.e. a Weibull distribution with shape parameter 1), I added the respective density with mean equal to the sample mean ($1/\lambda = 8.66$). The fit looks good and it would get better when refining the bin size, but for clearness is stay with this size.
\begin{figure}[!b]
\centering
\includegraphics[width=0.5\textwidth, keepaspectratio]{ex3/hist_data.png}
\caption{Histogram of the respiratory disturbance index (\texttt{rdi4p}) together with the density of a fitted exponential distribution (orange) with $\lambda = 0.12$.}
\label{3histdata}
\end{figure}

Now we want to estimate confidence intervals for the median and standard deviation for \texttt{rdi4p}. We use $R=1000$ bootstrap repetitions. The sample length is $n=5804$ and the sample statistics are $\hat{x}_{med}=4.19$ and $\hat{\sigma}=12.43$. The results for the percentile CI and the accelerated bias-corrected CI are shown in Table \ref{3tabledata}. We see that the function \texttt{bcanon} produced \textbf{NaN} for the CI of the median. This is because is uses the variance of the Jackknife statistics in the definition of the acceleration coefficient. As the median occurs twice in the data the denominator is zero and the acceleration coefficient is not defined. For SD we see that the upper confidence bound is smaller with the corrected method, whereas the lower bound is roughly the same as in the percentile method. It is surprising that the sample value for the standard deviation is not included in the confidence interval for the second method. The percentile CIs include both statistics. Of course, we don't know the true parameters or distribution here, so we can't judge the real precision here.

\begin{table}[ht]
\centering
\begin{tabular}{lrrrr}
  \hline
 & lower\_med & upper\_med & lower\_sd & upper\_sd \\
  \hline
percentile CI & 3.95 & 4.41 & 11.80 & 13.11 \\
acc bias-corr CI & NaN & NaN & 11.87 & 11.95 \\
   \hline
\end{tabular}
\caption{Confidence intervals for median and SD using the percentile method (first row) and the accelerated bias-corrected CI using \texttt{bcanon} (second row).}
\label{3tabledata}
\end{table}


\section{Propositional truncations}\label{sec:propositional-truncation}

The propositional truncation operation is a universal way of turning type a type $A$ into a proposition $\brck{A}$. Informally, the proposition $\brck{A}$ is the proposition that $A$ is inhabited. More precisely, the propositional truncation of $A$ comes equipped with a map $A\to\brck{A}$ and it is characterized by its universal property, which asserts that any map $A\to P$ into a proposition $P$ extends uniquely to a map $\brck{A}\to P$, as indicated in the diagram
\begin{equation*}
  \begin{tikzcd}
    A \arrow[dr] \arrow[d] \\
    \brck{A} \arrow[r,densely dotted] & P.
  \end{tikzcd}
\end{equation*}
Using the propositional truncation operation we can define many important mathematical concepts, including the image of a map, surjectivity, and connected components. We will discuss those topics in \cref{chap:image}.

\subsection{The universal property of propositional truncations}\label{sec:propositional-truncation-up}

\begin{defn}
Let $A$ be a type, and let $f:A\to P$ be a map into a proposition $P$. We say that $f$ is a \define{propositional truncation of $A$} if for every proposition $Q$, the precomposition map
\begin{equation*}
\blank\circ f:(P\to Q)\to (A\to Q)
\end{equation*}
is an equivalence. This property of $f$ is also called the \define{universal property of propositional truncation of $A$}\index{universal property!of propositional truncation}
\end{defn}

In other words, a map $f:A\to P$ into a proposition $P$ is a propositional truncation of $A$ if every map $g:A\to Q$ into a proposition extends uniquely along $f$, as indicated in the diagram
\begin{equation*}
  \begin{tikzcd}
    A \arrow[d,swap,"f"] \arrow[dr,"g"] \\
    P \arrow[r,densely dotted] & Q.
  \end{tikzcd}
\end{equation*}
Indeed, this unique extension property asserts that the type
\begin{equation*}
  \sm{h:P\to Q}h\circ f=g
\end{equation*}
is contractible for every $g:A\to Q$. In other words, the unique extension property asserts that the precomposition function $\blank\circ f:(P\to Q)\to (A\to Q)$ is a contractible map, which is the case if and only if it is an equivalence.

\begin{rmk}
  Note that if $Q$ is a proposition, then the type $X\to Q$ is a proposition for any type $X$. Furthermore, recall from \cref{ex:equiv-bi-implication} that the map $(P\to Q)\to (A\to Q)$ is an equivalence as soon as there is a map in the converse direction. Therefore, in order to prove the universal property of the propositional truncation it suffices to show that
  \begin{equation*}
    (A\to Q)\to (P\to Q).
  \end{equation*}
  We also note that the universal property of the propositional truncation of a type is formulated with respect to all propositions, regardless of the universe they live in.
\end{rmk}

\begin{eg}
  Suppose $A$ is a type that comes equipped with a point $a:A$, such as the booleans, the type of natural numbers, or the loop space $\loopspace{A}$ of a pointed type. Then the constant map
  \begin{equation*}
    \const_\ttt: A\to\unit
  \end{equation*}
  is a propositional truncation of $A$. To see this, let $Q$ be an arbitrary proposition. It suffices to show that
  \begin{equation*}
    (A\to Q)\to (\unit\to Q).
  \end{equation*}
  To see this, let $f:A\to Q$. Then we have $f(a):Q$, so we define $\const_{f(a)}:\unit\to Q$. Thus we see that we have
  \begin{equation*}
    \lam{f}\const_{f(a)}:(A\to Q)\to (\unit\to Q).
  \end{equation*}
  This proves that $\const_\ttt:A\to \unit$ satisfies the universal property of the propositional truncation of $A$.
\end{eg}

\begin{eg}
  If the type $A$ is already a proposition, then the identity function
  \begin{equation*}
    \idfunc:A\to A
  \end{equation*}
  is a propositional truncation of $A$. To see this, simply note that the precomposittion function with the identity function
  \begin{equation*}
    \blank\circ\idfunc : (A\to Q)\to (A\to Q)
  \end{equation*}
  is itself just the identity function. In particular, it is an equivalence.

  Similarly, any equivalence $e:P\simeq P'$ between propositions satisfies the universal property of the propositional truncation of $P$, since precomposing by an equivalence is an equivalence by \cref{ex:equiv_precomp}.
\end{eg}

The universal property of the propositional truncation determines the propositional truncation up to equivalence. Such proofs of uniqueness from a universal property always follow the same pattern.

\begin{prp}\label{prp:propositional-truncation-3-for-2}
  Let $A$ be a type, and consider a commuting triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=tiny]
      \phantom{P'} & A \arrow[dl,swap,"f"] \arrow[dr,"{f'}"] \\
      P \arrow[rr,swap,"h"] & & P'
    \end{tikzcd}
  \end{equation*}
  where $P$ and $P'$ are propositions. If any two of the following three assertions hold, so does the third:
  \begin{enumerate}
  \item The map $f$ satisfies the universal property of the propositional truncation of $A$.
  \item The map $f'$ satisfies the universal propertyof the propositional truncation of $A$.
  \item The map $h$ is an equivalence.
  \end{enumerate}
\end{prp}

\begin{proof}
  Note that the map $h:P\to P'$ is an equivalence if and only if for every proposition $Q$, the precomposition map
  \begin{equation*}
    \blank\circ h:(P'\to Q)\to (P\to Q)
  \end{equation*}
  is an equivalence. Thus, the claim follows by observing that for every proposition $Q$ we have the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=-1em]
      (P'\to Q) \arrow[rr,"\blank\circ h"] \arrow[dr,swap,"\blank\circ {f'}"] & & (P\to Q) \arrow[dl,"\blank\circ f"] \\
      & (A\to Q). & \phantom{(P'\to Q)}
    \end{tikzcd}
  \end{equation*}
\end{proof}

\begin{cor}\label{cor:uniquely-unique-brck}
  Consider two maps $f:A\to P$ and $f':A\to P'$ into propositions $P$ and $P'$, both satisfying the universal property of the propositional truncation of $A$. Then the type of equivalences $e:P \simeq P'$ for which the triangle
  \begin{equation*}
    \begin{tikzcd}[column sep=tiny]
      \phantom{P'} & A \arrow[dl,swap,"f"] \arrow[dr,"{f'}"] \\
      P \arrow[rr,swap,"e"] & & P'
    \end{tikzcd}
  \end{equation*}
  commutes, is contractible.
\end{cor}

\begin{rmk}
  Note that the triangles in \cref{prp:propositional-truncation-3-for-2,cor:uniquely-unique-brck} always commutes, since $P$ and $P'$ are assumed to be propositions.
\end{rmk}

Now that we have shown that propositional truncations are determined uniquely, we will assume that any universe is closed under propositional truncations.

\begin{axiom}\label{axiom:propositional-truncations}
  Any universe $\UU$ is closed under propositional truncations: for any type $A:\UU$ there is a proposition $\brck{A}:\UU$ equipped with a map $\eta:A\to\brck{A}$ that satisfies the universal property of the propositional truncation.
\end{axiom}

The propositional truncation is therefore an operation
\begin{equation*}
  \brck{\blank}:\UU\to\UU
\end{equation*}
on the universe. One simple application of the universal property of the propositional truncation is that $\brck{\blank}$ also acts on functions in a functorial way.

\begin{prp}
  There is a map
  \begin{equation*}
    \brck{\blank}:(A\to B)\to (\brck{A}\to\brck{B})
  \end{equation*}
  for any two types $A$ and $B$, such that
  \begin{align*}
    \brck{\idfunc} & \htpy \idfunc \\
    \brck{g\circ f} & \htpy \brck{g}\circ\brck{f}.
  \end{align*}
\end{prp}

\begin{proof}
  For any $f:A\to B$, the map $\brck{f}:\brck{A}\to\brck{B}$ is defined to be the unique extension
  \begin{equation*}
    \begin{tikzcd}
      A \arrow[d,swap,"\eta"] \arrow[r,"f"] & B \arrow[d,"\eta"] \\
      \brck{A} \arrow[r,densely dotted,swap,"\brck{f}"] & \brck{B}.
    \end{tikzcd}
  \end{equation*}
  To see that $\brck{\blank}$ preserves identity maps and compositions, simply note that $\idfunc[\brck{A}]$ is an extension of $\idfunc[A]$, and that $\brck{g}\circ\brck{f}$ is an extension of $g\circ f$. Hence the homotopies are obtained by uniqueness.
\end{proof}

\subsection{Propositional truncations as higher inductive types}

The idea of higher inductive types is that types can be generated inductively not only by point constructors, such as $\zeroN:\N$ and $\succN:\N\to\N$, but also by path constructors. One of the first examples of a higher inductive type was the propositional truncation of a type $A$. This is a type $\brck{A}$ equipped with one point constructor
\begin{equation*}
  \eta : A \to \brck{A},
\end{equation*}
one path constructor
\begin{equation*}
  \alpha : \prd{x,y:\brck{A}}x=y.
\end{equation*}
Note that the path constructor $\alpha$ immediately proves that $\brck{A}$ is a proposition. Now we should formulate the induction principle for the propositional truncation.

Just as we did with the universal property, we will formulate the induction principle of the propositional truncation for an arbitrary map $f:A\to P$ into a proposition $P$. When the induction principle is formulated in this way, we will be able to show that $f$ satisfies the universal property if and only if it satisfes the induction principle.

Consider a map $f:A\to P$ into a type equipped with a family of paths
\begin{equation*}
  \alpha : \prd{p,q:P}p=q
\end{equation*}
witnessing that $P$ is a proposition, and consider a type family $B$ over $P$. The induction principle of the propositional truncation of $A$ tells us what we have to do in order to construct a dependent function $h:\prd{p:P}B(p)$.

In order to figure out what the induction principle has to be, we first note that if we start with a dependent function $h:\prd{p:P}B(p)$, then we also obtain the function $h\circ f : \prd{x:A}B(f(x))$. In other words, if we think of $f:A\to P$ as the point constructor of $P$, then the function $h\circ f$ describes the action of $h$ on the points of $P$. The first requirement in the induction principle is therefore that $B$ must come equipped with a dependent function
\begin{equation*}
  g:\prd{x:A}B(f(x)).
\end{equation*}
Furthermore, the function $h$ acts on the paths in $P$ via its dependent action on paths, which we constructed in \cref{defn:apd}. The paths in $P$ are generated by $\alpha$, so we obtain a function
\begin{equation*}
  \lam{p}{q}\apd{h}{\alpha(p,q)} : \prd{p,q:P} \tr_B(\alpha(p,q),h(p))=h(q).
\end{equation*}
The induction principle must ensure that any function $h$ defined via the induction principle, satisfies this law. Therefore, the second condition in the induction principle is that we must have a family of identifications
\begin{equation*}
  \prd{p,q:P}{y:B(p)}{z:B(q)}\tr_B(\alpha(p,q),y)= z.
\end{equation*}
We now formulate the induction principle for propositional truncation.

\begin{defn}
  Consider a map $f:A\to P$ into a type $P$ equipped with a family of paths
  \begin{equation*}
    \alpha : \prd{p,q:P}p=q,
  \end{equation*}
  witnessing that $P$ is a proposition. We say that $f$ satisfies the induction principle for the propositional truncation of $A$ if for any family $B$ over $P$ that comes equipped with
  \begin{align*}
    g & : \prd{x:A}B(f(x)) \\
    \beta & : \prd{p,q:P}{y:B(p)}{z:B(q)}\tr_B(\alpha(p,q),y)=z,
  \end{align*}
  there is a dependent function $h:\prd{p:P}B(p)$ equipped with a homotopy
  \begin{equation*}
    \prd{x:A}h(f(x))= g(x).
  \end{equation*}
\end{defn}

In the following lemma we show that if a family $B$ over $\brck{A}$ comes equipped with a family of paths
\begin{equation*}
  \prd{x,y:\brck{A}}{u:B(x)}{v:B(y)}\tr_B(\alpha(x,y),u)=v,
\end{equation*}
then $B$ must be a family of propositions.

\begin{lem}\label{lem:case-paths-induction-principle-propositional-truncation}
  Let $P$ be a type equipped with a family of paths
  \begin{equation*}
    \alpha : \prd{p,q:P}p=q,
  \end{equation*}
  showing that $P$ is a proposition, and consider a type family $B$ over $P$. The following are equivalent:
  \begin{enumerate}
  \item The family $B$ comes equipped with a family of identifications
    \begin{equation*}
      \beta:\prd{p,q:P}{x:B(p)}{y:B(q)}\tr_B(\alpha(p,q),x)=y,
    \end{equation*}
  \item The family $B$ is a family of propositions.
  \end{enumerate}
\end{lem}

\begin{proof}
  Assuming that (i) holds, we will show that each $B(p)$ is a proposition by showing that
  \begin{equation*}
    B(p)\to\iscontr(B(p)).
  \end{equation*}
  Let $x:B(p)$. We have to construct a center of contraction and a contraction. Our plan is to use $\beta$ to define the contraction, so it is natural to define the center as $\tr_B(\alpha(p,p),x)$. Now we take
  \begin{equation*}
    \beta(p,p,x):\prd{y:B(p)}\tr_B(\alpha(p,p),x)=y
  \end{equation*}
  as our contraction. This completes the proof that $B$ is a family of propositions.

  The converse is immediate: if $B$ is a family of propositions, then any two terms in any $B(q)$ can be identified.
\end{proof}

\begin{defn}
  Consider a map $f:A\to P$ into a proposition $P$. We say that $f$ satisfies the dependent universal property of the propositional truncation of $A$, if for any family $Q$ of propositions over $P$, the precomposition map
  \begin{equation*}
    \blank\circ f : \Big(\prd{p:P}Q(p)\Big)\to\Big(\prd{x:A}Q(f(x))\Big)
  \end{equation*}
  is an equivalence.
\end{defn}

\begin{thm}
  Consider a map $f:A\to P$ into a proposition $P$. Then the following are equivalent:
  \begin{enumerate}
  \item The map $f$ is a propositional truncation.
  \item The map $f$ satisfies the dependent universal property of the propositional truncation.
  \item The map $f$ satisfies the induction principle of the propositional truncation.
  \end{enumerate}
\end{thm}

\begin{proof}
  We will first show that (i) and (ii) are equivalent. Of course, the universal property is a special case of the dependent universal property, so the fact that (ii) implies (i) is immediate. We now show that (i) implies (ii). Let $Q$ be a family of propositions over $P$, and consider the following commuting diagram:
  \begin{equation*}
    \begin{tikzcd}[column sep=8em]
      \Big(\sm{h:P\to P}\prd{p:P}Q(h(p))\Big) \arrow[r,"{\tot[\blank\circ f]{\blank\circ f}}"] \arrow[d,swap,"\choice^{-1}"] & \Big(\sm{g:A\to P}\prd{x:A}Q(g(x))\Big) \arrow[d,"\choice^{-1}"] \\
      \Big(P\to \sm{p:P}Q(p)\Big) \arrow[r,"\blank\circ f"] \arrow[d,swap,"\proj 1\circ\blank"]  & \Big(A \to \sm{p:P}Q(p)\Big) \arrow[d,"\proj 1\circ\blank"] \\
      \Big(P\to P\Big) \arrow[r,swap,"\blank\circ f"] & \Big(A\to P\Big)
    \end{tikzcd}
  \end{equation*}
  In this diagram the bottom map is an equivalence by the universal property of the propositional truncation of $A$. Note also that the type $\sm{p:P}Q(p)$ is a proposition by \cref{ex:istrunc_sigma}, so it follows that also the middle map is an equivalence. Furthermore, the type theoretic choice maps are equivalences by \cref{thm:choice}, so it also follows that the top map is an equivalence. Now we use \cref{thm:equiv-toto} to conclude that the family of maps
  \begin{equation*}
    \blank\circ f: \Big(\prd{p:P}Q(h(p))\Big)\to\Big(\prd{x:A}Q(h(f(x)))\Big)
  \end{equation*}
  indexed by $h:P\to P$ is a family of equivalences. The dependent universal property is now just a special case: take $h\jdeq\idfunc$. This completes the proof that (i) is equivalent to (ii).

  It remains to show that (ii) is equivalent to (iii). By \cref{lem:case-paths-induction-principle-propositional-truncation} it follows that the induction principle is equivalent to the property that for each family $Q$ of propositions over $P$, the precomposition map
  \begin{equation*}
    \blank\circ f : \Big(\prd{p:P}Q(p)\Big)\to\Big(\prd{x:A}Q(f(x))\Big)
  \end{equation*}
  has a section. Since the domain and codomain of this map are propositions by \cref{thm:trunc_pi}, we see that this precomposition map has a section if and only if it is an equivalence.
\end{proof}

\begin{exercises}
  \exercise Let $A$ be a type and let $P$ be a proposition, and suppose that $P$ is a retract of $A$. Show that the retraction $A\to P$ is a propositional truncation.
%  \exercise Show that the relation $x,y\mapsto\brck{x=y}$ is an equivalence relation, on any type.
  \exercise Consider two maps $f:A\to P$ and $g:B\to Q$ into propositions $P$ and $Q$. Recall from \cref{ex:istrunc_sigma} that the type $P\times Q$ is also a proposition. Show that if both $f$ and $g$ are propositional truncations then the map $f\times g : A\times B\to P\times Q$ is also a propositional truncation. Conclude that
  \begin{equation*}
    \brck{A\times B}\simeq \brck{A}\times\brck{B}.
  \end{equation*}
  \exercise Consider two propositions $P$ and $Q$, and define
  \begin{align*}
    P\land Q & \defeq P\times Q\\
    P\vee Q & \defeq \brck{P+Q}.
  \end{align*}
  \begin{subexenum}
  \item Construct maps $i:P\to P\vee Q$ and $j:Q\to P\vee Q$.
  \item Prove the universal property of disjunction, i.e., show that for any proposition $R$, the map
    \begin{equation*}
      (P\vee Q\to R) \to (P\to R)\land (Q\to R)
    \end{equation*}
    given by $h\mapsto (h\circ i,h\circ j)$ is an equivalence.
  \end{subexenum}
  \exercise Consider a family $P$ of propositions over a type $A$, and define
  \begin{align*}
    \forall_{(x:A)}P(x) & \defeq \prd{x:A}P(x) \\
    \exists_{(x:A)}P(x) & \defeq \Brck{\sm{x:A}P(x)}
  \end{align*}
  \begin{subexenum}
  \item Construct a map $i_a : P(a)\to \exists_{(x:A)}P(x)$ for each $a:A$.
  \item Prove the universal property of the existential quantification, i.e. show that for any proposition $Q$, the map
    \begin{equation*}
      \Big(\Big(\exists_{(x:A)}P(x)\Big)\to Q\Big)\to \Big(\forall_{(x:A)}(P(x)\to Q)\Big)
    \end{equation*}
    given by $h\mapsto \lam{x}h\circ i_x$, is an equivalence.
  \end{subexenum}
  \exercise Show that
  \begin{equation*}
    \eqv{\brck{A}}{\prd{P:\prop}(A\to P)\to P}
  \end{equation*}
  for any type $A:\UU$. This is called the \define{impredicative encoding} of the propositional truncation.
  % \exercise For any $B:A\to\UU$, construct an equivalence
  % \begin{equation*}
  %   \eqv{\Big(\exists_{(a:A)}\brck{B(a)}\Big)}{\brck{\sm{a:A}B(a)}}
  % \end{equation*}
\end{exercises}


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% Required to use \cfrac
\usepackage{amsmath}

% Required to use \sfrac
\usepackage{xfrac}

\begin{document}

\section*{Simple fraction}

Fractions are created as follows.

% Syntax: \frac{numerator}{denominator}

\[\frac{1}{x} = x^{-1}\]

\section*{Continued (or cascade) fractions}

A fraction can contain another one. If the command \emph{\textbackslash frac} is, this would be the result:

\[\frac{x}{x + \frac{1}{2}}\]

The sub fraction is very small. This is why the command \emph{\textbackslash cfrac} is recommended for continued fractions:

\[\cfrac{x}{x + \cfrac{1}{2}}\]

Some people prefer to still use \emph{\textbackslash frac} and use the command \emph{\textbackslash displaystyle} before each sub fractions:

\[\frac{x}{x + \displaystyle\frac{1}{2}}\]

Despite this practice is sometimes promoted, it should be avoided because the equation has some spacing issues, such as on top of $\frac{1}{2}$.

\section*{Slanted fractions}

Slanted fractions are created as follows : $\sfrac{1}{2}$.

\end{document}


\section{Network Throughput, Bandwidth and Safety Parameters}
The parameters of the flow and load of the system (about 80\% at peak hours 9h-11h and 15h-16h) can be shared for Headquarter and Branches as follows:
\begin{itemize}
  \item Servers used for updates, web access, database access, ..... The total upload and download capacity is about 500 MB / day.
  \item Each workstation is used for Web browsing, document downloads, customer transactions, ... The total upload and download capacity is about 100 MB / day.
  \item WiFi-connected laptop for customers’ accesses about 50 MB / day.
\end{itemize}

\subsection{Headquarter}
\subsubsection{Server}
The total peak hours time in a days is 3 hours (9h-11h and 15h-16h) and may consume up to 80\%. \\
$$Bandwidth = \, \frac{5*500*0.8}{3*3600}*8 = 1.481Mbps$$
$$Throughput = \, \frac{5*500}{8*3600}*8 = 0.694Mbps$$
\subsubsection{Workstation}
Assume each workstation will work 8 hours a day
$$Bandwidth = \, \frac{100*100*0.8}{3*3600}*8 = 5.926Mbps$$
$$Throughput = \, \frac{100*100}{8*3600}*8 = 2.778Mbps$$
\subsubsection{User}
Assume that the number of customers in one day is about 200 customers, and in peak
time is 150 customers. Also, assume that each user access Wifi 8 hours a day.
$$Bandwidth = \, \frac{150*50}{3*3600}*8 = 5.556Mbps$$
$$Throughput = \, \frac{200*50}{8*3600}*8 = 2.778Mbps$$

\subsubsection{Total}
$$Bandwidth = 1.481+5.926+5.556 = 12.963Mbps$$
$$Throughput = 0.694+2.778+2.778 = 6.25Mbps$$

\subsection{Safety parameters}
To ensure for the next 10 years, we need to consider the rate 20\% per five years. The first 5 years, the number of customer will still not increase and this number will increase in the next 5 years.
$$Bandwidth = 12.963*1.2 = 15.556Mbps$$
$$Throughput =6.25*1.2 = 7.5Mbps$$

\subsection{Branch}
\subsubsection{Server}
The total peak hours time in a days is 3 hours (9h-11h and 15h-16h) and may consume up to 80\%. \\
$$Bandwidth = \, \frac{3*500*0.8}{3*3600}*8 = 0.889Mbps$$
$$Throughput = \, \frac{3*500}{8*3600}*8 = 0.417Mbps$$
\subsubsection{Workstation}
Assume each workstation will work 8 hours a day
$$Bandwidth = \, \frac{50*100*0.8}{3*3600}*8 = 2.963Mbps$$
$$Throughput = \, \frac{50*100}{8*3600}*8 = 1.389Mbps$$
\subsubsection{User}
Assume that the number of customers in one day is about 140 customers, and in peak
time is 70 customers. Also, assume that each user access Wifi 8 hours a day.
$$Bandwidth = \, \frac{70*50}{3*3600}*8 = 2.593Mbps$$
$$Throughput = \, \frac{140*50}{8*3600}*8 = 2.778Mbps$$

\subsubsection{Total}
$$Bandwidth = 0.889+2.963+2.593= 6.445Mbps$$
$$Throughput =0.417+1.389+2.778= 4.584Mbps$$

\subsection{Safety parameters}
To ensure for the next 10 years, we need to consider the rate 20\% per five years. The first 5 years, the number of customer will still not increase and this number will increase in the next 5 years.
$$Bandwidth = 6.445*1.2 = 7.734Mbps$$
$$Throughput =4.584*1.2 = 5.501Mbps$$