\section{Cycles and Permutations}
\begin{definition}
    Given a list $a_1,a_2,\ldots,a_k$ of distinct elements of $\{1,2,\ldots,n\}$, the $k$-cycle, written as $(a_1\ a_2\ \ldots\ a_k)$ is the permutation $\pi\in S_n$ defined by $\pi(a_i)=a_{i+1}$ for $1\le i\le k-1$, $\pi(a_k)=a_1$, and $\pi(j)=j$ if $j\notin\{a_i:1\le i\le k\}$.
\end{definition}
It is obvious that it is indeed a permutation, and we know that
$$(a_1\ a_2\ \ldots\ a_k)^{-1}=(a_k\ a_{k-1}\ \ldots\ a_1)$$
In particular, there is a set of special cycles.
\begin{definition}
    A $2$-cycle is called a transposition.
\end{definition}
Two different cycles $(a_1\ a_2\ \ldots\ a_k),(b_1\ b_2\ \ldots\ b_l)$ are called disjoint if $\{a_i:1\le i\le k\}\cap\{b_j:1\le j\le l\}=\varnothing$.\\
We can compose the permutations since they are in fact functions
\begin{example}
    $((1\ 2\ 3\ 4)\circ(3\ 2\ 4))(1)=2$, and $((1\ 2\ 3\ 4)\circ(3\ 2\ 4))(2)=(1\ 2\ 3\ 4)(4)=1$, similarly $((1\ 2\ 3\ 4)\circ(3\ 2\ 4))(3)=3,((1\ 2\ 3\ 4)\circ(3\ 2\ 4))(4)=4$, so it is a transposition $(1\ 2)$.\\
    By the same way, we have $(3\ 2\ 4)\circ (1\ 2\ 3\ 4)=(1\ 4)$, so $(3\ 2\ 4)$ and $(1\ 2\ 3\ 4)$ do not commute.\\
    So $S_4$ is not abelian.
    In fact, $S_n$ is not abelian for all $n\ge 3$ since $S_n$ can be embedded into $S_{n+1}$ by fixing the last element.
\end{example}
\begin{lemma}
    1. $(a_1\ a_2\ \ldots\ a_k)=(a_k\ a_1\ a_2\ \ldots\ a_{k-1})$.\\
    2. Disjoint cycles commute.
\end{lemma}
\begin{proof}
    Trivial.
\end{proof}
\begin{theorem}\label{disjoint_cycles}
    Every permutation is a product of disjoint cycles (including $1$-cycles for convenience) and it is unique to write it as such a product up to the ambiguity stated in the preceding lemma.
\end{theorem}
\begin{proof}
    Trivial but let us write this down.
    We do strong induction on $n$.
    The base case is trivial.
    Now we consider the sequence $1, \sigma(1),\sigma^2(1),\ldots$.
    Since there is only finitely many numbers, at some point the sequence goes back to $1$.
    Indeed, there must be some $p>q$ such that $\sigma^p(1)=\sigma^q(1)$, thus $\sigma^{p-q}(1)=1$.
    So we take the smallest $k\ge 1$ such that $\sigma^k(1)=1$, so $\sigma^i(1)\neq\sigma^j(1)$ for $i\neq j$ because if so then $\sigma^{i-j}(1)=1$ (WLOG $i>j$) which contradicts the minimality of $k$.\\
    Hence $\sigma$ maps $S=\{1,\sigma(1),\sigma^2(1),\ldots\}$ to itself, so it is a permutation on $T\{1,2,\ldots,n\}\setminus S$, thus $\sigma|_T$ is a product of disjoint cycles by induction hypothesis, thus we must have $\sigma=(1\ \sigma(1)\ \sigma^2(1)\ \ldots\ \sigma^{k-1}(1))\sigma|_T$, which proves the existence.\\
    The uniqueness is obvious since if $\sigma$ is written as two cycles, then we can pick an element and by enumerating it we can show that the cycles containing that element are the same.
    Hence the theorem is proved.
\end{proof}
\begin{lemma}
    For any permutation $\sigma$, write it as the product of disjoint cycles, that is,
    $$\sigma=(a_1^1\ a_2^1\ \ldots\ a_{k_1}^1)(a_1^2\ a_2^2\ \ldots\ a_{k_2}^2)\cdots (a_1^r\ a_2^r\ \ldots\ a_{k_r}^r)$$
    Then the order of $\sigma$ is the LCM of $k_1,k_2,\ldots,k_r$.
\end{lemma}
\begin{proof}
    Note that
    $$\sigma^j=(a_1^1\ a_2^1\ \ldots\ a_{k_1}^1)^j(a_1^2\ a_2^2\ \ldots\ a_{k_2}^2)^j\cdots (a_1^r\ a_2^r\ \ldots\ a_{k_r}^r)^j$$
    since disjoint cycles commute.
    Now the order of a cycle $(a_1^i\ a_2^i\ \ldots\ a_{k_i}^i)$ has order $k_i$.
    So if we let $\ell$ be the LCM of $k_1,k_2,\ldots,k_r$, we immediately have $\sigma^\ell=e$.
    Suppose $\sigma^m=e$ for some $m$, then we would have
    $$(a_1^1\ a_2^1\ \ldots\ a_{k_1}^1)^m=((a_1^2\ a_2^2\ \ldots\ a_{k_2}^2)^m\cdots (a_1^r\ a_2^r\ \ldots\ a_{k_r}^r)^m)^{-1}$$
    But the LHS fixes $a_s^1$ for any $1\le s\le k_1$ due to disjointness, thus the LHS must equal to the identity, i.e. $k_1|m$.
    Similarly $k_i|m$ for any $1\le i\le r$, thus $\ell|m\implies \ell\le m$, hence $\ell$ is the order of $\sigma$.
\end{proof}
\subsection{The Sign of Permutation}
\begin{proposition}
    Every permutation is a product of transpositions.
\end{proposition}
\begin{proof}
    By Theorem \ref{disjoint_cycles}, it suffices to show that every cycle is a product of transpositions, but
    $$(a_1\ a_2\ \ldots\ a_k)=(a_1\ a_k)(a_1\ a_{k-1})\cdots (a_1\ a_2)$$
    As desired.
\end{proof}
\begin{proof}[Alternative proof]
    We proceed by induction on $n$ such that the permutation is in $S_n$.
    In $n=0$, there is nothing to show.
    More generally, for $\sigma\in S_n$ for $n>1$.
    Choose an $a\in\{1,2,\ldots,n\}$, then $(a\ \sigma(a))\sigma$ fixes $a$, hence is a permutation on at most $n-1$ elements, so the proof is done by induction.
\end{proof}
\begin{definition}
    Define the sign of a permutation $\sigma$ by
    $$
    \operatorname{sgn}(\sigma)=
    \begin{cases}
        1\text{, if $\sigma$ has an even number of transpositions}\\
        -1\text{, otherwise}
    \end{cases}
    $$
\end{definition}
\begin{proposition}
    The sign of the permutation is well-defined.
\end{proposition}
\begin{proof}
    Let $\sigma$ be written as the product of disjoint cycles (including $1$-cycles).
    Suppose there is $\ell(\sigma)$ many such cycles.
    It is well-defined by Theorem \ref{disjoint_cycles}.\\
    Consider a transposition $(c\ d)$.
    $\ell(\sigma\circ (c\ d))=\ell(\sigma)+1$ if $c,d$ are in the same cycle in $\sigma$.
    Otherwise, it is $\ell(\sigma)-1$.
    So in either case, $\ell(\sigma\circ (c\ d))\equiv \ell(\sigma)+1\pmod{2}$.\\
    Note then that $\ell(\sigma)=\ell(t_1t_2\cdots t_k)$ where $t_i$ are transpositions.
    Thus $\ell(\sigma)\equiv\ell(e)+k\equiv n+k\pmod{2}$, so if a permutation has both signs $k,l$, then $k\equiv l\pmod{2}$.
    Therefore the sign is well-defined.
    Indeed, $\operatorname{sgn}(\sigma)=(-1)^{\ell(\sigma)-n}$.
\end{proof}
Immediately $\operatorname{sgn}((a_1\ a_2\ \ldots\ a_r))=(-1)^{r-1}$.
\begin{corollary}
    The function $\operatorname{sgn}$ is a homomorphism $S_n\to (\{1,-1\},\times,1)$.
\end{corollary}
\begin{proof}
    Trivial.
\end{proof}
\begin{definition}
    A permutation $\sigma$ is even if $\operatorname{sgn}(\sigma)=1$ and it is odd otherwise.
\end{definition}
\begin{definition}
    The alternating group $A_n\le S_n$ is defined as $A_n=\ker\operatorname{sgn}$.
    That is, $A_n$ consists of all even permutations.
\end{definition}
Note that any transposition has sign $-1$ and the identity has sign $1$, thus $\operatorname{sgn}$ is surjective, therefore the index of $A_n$ is $2$, hence it is normal.
\subsection{Conjugation in the Permutation Group}
\begin{proposition}
    If we have a permutation $\sigma$, then $\sigma (a_1\ a_2\ \ldots\ a_r)\sigma^{-1}=(\sigma(a_1)\ \sigma(a_2)\ \ldots\ \sigma(a_r))$.
\end{proposition}
\begin{proof}
    Trivial.
\end{proof}
\begin{corollary}
    $\tau,\tau'\in S_n$ are conjugates if and only if, when written as a composition of disjoint cycles (in which every number appears, i.e. counting $1$-cycles), they have the same number of cycles of each length.
\end{corollary}
\begin{proof}
    Follows directly from the formula in the preceding proposition and Theorem \ref{disjoint_cycles}.
\end{proof}
For a permutation, we can produce an (unique) string $1^{a_1}2^{a_2}\cdots n^{a_n}$ where $a_i$ is the number of cycles of length $i$.
We call such a string the ``cycles type'' of a permutation.
So the above corollary means that two permutations are in the same conjugacy class if and only if they have the same cycle type.
It is then curious to consider the size of each conjugacy class.
\begin{definition}
    The stabiliser of an element $g$ under the conjugacy action is the centraliser, written as $C_G(g)$.
\end{definition}
\begin{lemma}
    If $\tau\in S_n$ has cycle type $1^{a_1}2^{a_2}\cdots n^{a_n}$, then
    $$|C_G(\tau)|=1^{a_1}(a_1)!2^{a_2}(a_2)!\cdots n^{a_n}(a_n)!$$
\end{lemma}
\begin{proof}
    Obvious.
\end{proof}
\begin{corollary}
    The size of the conjugacy class containing $\tau$ is
    $$\frac{n!}{1^{a_1}(a_1)!2^{a_2}(a_2)!\cdots n^{a_n}(a_n)!}$$
    where $a_i$ are defined as before.
\end{corollary}
\begin{proof}
    Orbit-Stabiliser.
\end{proof}
\begin{example}
    Consider $S_4$, then the conjugacy classes are of sizes $1$ (consisting of $e$ only and have cycle type $1^4$), $6$ (of type $1^22^1$), $3$ (of type $2^2$), $8$ (of type $1^13^1$), and $6$ (of type $4^1$) by the formula.
    We do have $1+6+3+8+6=24=4!=|S_4|$.
    Note that given the number of conjugacy classes that we expect, it is trivial to work out what are the elements.
\end{example}
\begin{corollary}
    Any normal subgroup of a finite group $G$ is an union of conjugacy classes of $G$.
\end{corollary}
\begin{proof}
    If $h\in H$ is in one of the conjugacy class, then by normality $ghg^{-1}\in H$ for any $g$, so the entire conjugacy class is in $H$.
\end{proof}
\begin{example}
    We try to find the normal subgroups of $S_4$.
    Let $H\unlhd S_4$, then $H$ must contain the conjugacy class $\{e\}$.
    If $H$ contains the conjugacy class $1^22^1$, then since transpositions generates $S_4$, $H$ is the entire group $S_4$.\\
    If $H$ contains the conjugacy class $2^2$, then it contains the normal subgroup $K$ consisting of the conjugacy classes $1^4,2^2$ only.
    We the case $H>K$ means that $H$ contains more than $2$ conjugacy classes, so we can discuss this case later by considering other conjugacy classes.\\
    If $H$ contains the conjugacy class $3^1$, then it contains all $3$-cycles, which there are $8$ of them, so $|H|\ge 9$, so we must have $|H|=12$ or $24$.
    Note that $3$-cycles are even, so $H\cap A_4$ contains all $3$-cycles, which have at least $9$ elements, thus $H\cap A_4=A_4$, so $H=A_4$ or $H=S_4$.\\
    If $H$ contains the conjugacy class $4^1$, but then it contains at least one $3$-cycles (e.g. $(1\ 2\ 3\ 4)(1\ 4\ 2\ 3)=(2\ 4\ 3)$), but since $H$ is normal it in fact contains all $3$-cycles, then it is just the same as the previous case (where, since $(1\ 2\ 3\ 4)$ is odd, we have $H=S_4$).\\
    So $H$ is one of $K,A_4,S_4$.\\
    We have $S_4/S_4\cong \{e\},S_4/A_4\cong C_2, S_4/K\cong S_3$.
\end{example}
As $A_n\unlhd S_n$, for $\sigma\in A_n$, $\operatorname{ccl}_{A_n}(\sigma)\subseteq\operatorname{ccl}_{S_n}(\sigma)$, but the equality may not hold.
For example, $(1\ 2\ 3)$ and $(1\ 3\ 2)$ are even and conjugates of each other in $S_3$, but they are not in $A_3$ which is abelian.
On the other hand, in $S_5$, we have, however, $[(2\ 3)(4\ 5)](1\ 2\ 3)[(2\ 3)(4\ 5)]^{-1}=(1\ 3\ 2)$, and $(2\ 3)(4\ 5)$ is even, so they are conjugate in $A_5$ in this case.\\
For $\sigma\in A_n$, we have
$$|A_n|/|\operatorname{ccl}_{A_n}(\sigma)|=|C_{A_n}(\sigma)|,|S_n|/|\operatorname{ccl}_{S_n}(\sigma)|=|C_{S_n}(\sigma)|$$
But we also have $|S_n|=2|A_n|$, so either the conjugacy classes are the same, which implies $|C_{A_n}(\sigma)|=|C_{S_n}(\sigma)|/2$.
Otherwise, $|\operatorname{ccl}_{A_n}(\sigma)|=|\operatorname{ccl}_{S_n}(\sigma)|/2$ and $C_{A_n}(\sigma)=C_{S_n}(\sigma)$.\\
Thus either the centraliser of $\sigma$ contains an odd element and the conjugacy classes are the same or the centralisers of $\sigma$ is contained in $A_n$ and the conjugacy class in $A_n$ is half the size of that in $S_n$.
\begin{example}
    Consider $S_4$ and $A_4$.
    We look at the conjugacy classes in $S_4$ and study whether they split in $A_4$.
    $\{e\}$ itself constitutes a conjugacy class, so there is nothing to show.
    The conjugacy class $1^22^1$ of transpositions is all odd, thus does not lie in $A_4$.
    $2^2$ in $S_4$ are double transpositions, which are even, so it do lie in $A_4$, but there are only $3$ elements, so it cannot split.
    On the other hand, $(1\ 2)$ centralises $(1\ 2)(3\ 4)$, so the centraliser does contain an odd element.\\
    The conjugacy class $1^13^1$ are even so do lie in $A_4$.
    And the centraliser of it is contained in $A_4$, i.e. all even elements, so the conjugacy class splits into two.
    Indeed, they splits to give $\{(1\ 2\ 3),(1\ 4\ 2),(1\ 3\ 4),(2\ 4\ 3)\}$ and $\{(1\ 3\ 2),(1\ 2\ 4), (1\ 4\ 3),(2\ 3\ 4)\}$.\\
    The $4$-cycles in $S_4$ are odd, so do not lie in $A_4$, hence again there is nothing to show.
\end{example}
We can also use it to search for normal subgroups of $A_4$.
\begin{example}
    By definition a normal subgroup of $A_4$ must be the union of conjugacy classes.
    Then we could either have $\{e\}$, or $K$, constituting of $\{e\}$ with all the double transpositions.
    Note that if the normal subgroup contains one of the $1^13^1$ conjugacy classes, it must contain the other one which constitutes the inverses of it.
    Hence it must contain at least $9$ elements, but $|A_4|=12$, so it can only be $A_4$.
    Therefore the normal subgroups are $\{e\},K,A_4$.
\end{example}
In particular, $A_4$ is not simple.
\begin{theorem}
    $A_5$ is simple.
\end{theorem}
In fact, $A_n$ is simple for any $n\neq 4$.
\begin{proof}
    $S_5$ has $120$ elements, and its conjugacy classes can be summarized as $1^5,1^32^1,1^12^2,1^23^1,2^13^1,1^14^1,5^1$, we can have the following table
    \begin{center}
        \begin{tabular}{c|ccccccc}
            Cycle type&$1^5$&$1^32^1$&$1^12^2$&$1^23^1$&$2^13^1$&$1^14^1$&$5^1$\\
            Size&$1$&$10$&$15$&$20$&$20$&$30$&$24$\\
            Sign&+&-&+&+&-&-&+
        \end{tabular}
    \end{center}
    By looking at whether the conjugacy class split for a typical even permutation of each cycle type, we conclude the following sizes of conjugacy classes in $A_5$.
    \begin{center}
        \begin{tabular}{c|ccccc}
            Cycle type&$1^5$&$2^2$&$3^1$&$5^1$&$5^1$\\
            Size&$1$&$15$&$20$&$12$&$12$
        \end{tabular}
    \end{center}
    Thus there is no way to sum them up (where we must of course add the first cycle type that is the identity) to produce a factor of $|A_5|=60$.
    Therefore $A_5$ is simple.
\end{proof}


\documentclass[letterpaper, twoside, 12pt]{book}
\usepackage{packet}

\begin{document}

\setcounter{chapter}{1}

\chapter{Part 2.4: Sections 14.7-14.8}

\setcounter{chapter}{14}
\setcounter{section}{6}

\section{Maximum and Minimum Values} %14.7

\begin{definition}
  Let $f$ be a function of many variables defined near the point $P_0$.
  Then $f$ has a \textbf{local maximum} $f(P_0)$ at $P_0$ if $f(P_0)$ is the
  largest value of $f$ near $P_0$, and
  $f$ has a \textbf{local minimum} $f(P_0)$ at $P_0$ if $f(P_0)$ is the smallest
  value of $f$ near $P_0$.
  (Local maxima and minimal are also known as local extreme values or
  local extrema.)
\end{definition}

\begin{definition}
  If $P_0$ is a point in the domain of $f$ and
    \[
      \nabla f(P_0) = \vect{0} \text{ or } \nabla f(P_0) \text{ DNE}
    \]
  then $P_0$ is called a \textbf{critical point}.
\end{definition}

\begin{theorem}
  Critical points of a two-variable function occur when the tangent plane
  is horizontal (because $\<0,0,-1\>$ is a normal vector)
  or the tangent plane does not exist.
\end{theorem}

\begin{theorem}
  The local maximum and minimum values of a function always
  occur at critical points.
\end{theorem}

          \begin{problem}
            Prove that $f(x,y)=x^2+16y^2$ has exactly one local extreme value
            value by showing that $(0,0)$ is the only critical point
            for $f$, and then showing that $f(0,0)$ is the minimum value
            of the function.
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{remark}
  By plotting the graph of $f$ in the previous problem, you can see
  that $(0,0)$ yields the lowest point on the surface.
\end{remark}

\begin{definition}
  The \textbf{saddle points} of $f$ are the critical points which don't yield local extreme values.
\end{definition}

          \begin{problem}
            Prove that $(0,0)$ is a saddle point of the function
            $f(x,y)=4x^2-9y^2$ by first showing that it is a critical point,
            and then considering the function $f$ restricted to the curves $y=0$
            and $x=0$ in the $xy$ plane.
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{remark}
  The term ``saddle point'' comes from the fact that the graph near a
  saddle point often looks like a saddle (such as in the previous problem).
\end{remark}

\begin{definition}
  The \textbf{discriminant} of a differentiable two variable function
  $f$ is the function
  \[
    f_D
      =
    \begin{array}{|cc|}f_{xx}&f_{xy}\\f_{yx}&f_{yy}\end{array}
      =
    f_{xx}f_{yy} - f_{xy}^2
  \]
\end{definition}

          \begin{problem}
            Compute the discriminant of the function
            $f(x,y)=3x^2y-2y^3+4x$.
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{theorem}
  The \textbf{Second Derivative Test} for two-variable functions gives
  a way to (sometimes) determine if a critical point either yields a
  local maximum, a local minimum, or a saddle point.
  Let $(a,b)$ be a critical point of of $f$ where $\nabla f$ is defined.
    \begin{itemize}
      \item If $f_D(a,b)>0$ and $f_{xx}(a,b)<0$,
            then $f(a,b)$ is a local maximum.
      \item If $f_D(a,b)>0$ and $f_{xx}(a,b)>0$,
            then $f(a,b)$ is a local minimum.
      \item If $f_D(a,b)<0$,
            then $f$ has a saddle point at $(a,b)$.
      \item If $f_D(a,b)=0$,
            then the test is inconclusive.
    \end{itemize}
\end{theorem}

          \begin{problem}
            Prove that $f_{xx}$ could be replaced with $f_{yy}$ in the
            Second Derivative Test by showing that if $f_D(a,b)>0$,
            then $f_{xx}(a,b)$ and $f_{yy}(a,b)$ are either both positive
            or both negative.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            In an earlier problem we found that $f(x,y)=x^2+16y^2$ has exactly
            one critical point $(0,0)$. Use the Second Derivative Test
            to show that $f(0,0)$ is the minimum value of the function.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Identify all the critical points for
            $f(x,y)=x^3-6xy+\frac{3}{2}y^2-1$, then use the Second Derivative
            Test to label each critical point as yielding a local minimum,
            a local maximum, or a saddle point.
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{definition}
  Let $f$ be a function of many variables.
  Then $f$ has an \textbf{absolute maximum} $f(P_0)$ at $P_0$ if $f(P_0)$ is
  the largest value in the range of $f$, and
  $f$ has an \textbf{absolute minimum} $f(P_0)$ at $P_0$ if $f(P_0)$ is
  the smallest value in the range of $f$.
  (Absolute maxima and minimal are also known as the absolute extreme values or
  absolute extrema.)
\end{definition}

\begin{theorem}
  A continuous function $f$ restricted to a closed and bounded domain $D$
  always has an absolute minimum and absolute maximum value.
\end{theorem}

\begin{theorem}
  The only possible points which can yield the absolute value of a function
  $f$ of two variables $x,y$ on a restricted domain $D\subseteq \mathbb R^2$
  are:
  \begin{itemize}
    \item Critical points of $f$ inside $D$
    \item Critical points of $f$ restricted to the boundary of $D$
    \item Corners on the boundary of $D$
  \end{itemize}
  The absolute maximum and absolute minimum values may be computed by
  plugging in all of these candidates into $f$.
\end{theorem}

\begin{remark}
  The previous theorem works because it checks all the local extreme values
  against each other to find the absolute largest and smallest value.
\end{remark}

          \begin{problem}
            Find the absolute maximum and minimum value of
            $f(x,y)=x^2+y^2-2x-2y$ restricted to the region bounded by
            the triangle with vertices
            $(0,0)$, $(2,4)$, and $(2,0)$. (Hint: this
            triangle is given by the curves $y=0$, $y=2x$, and
            $x=2$.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Find the absolute maximum and minimum value of
            $f(x,y)=2xy$ restricted to the region bounded by
            the circle $x^2+y^2=4$. (Hint: find a vector equation $\vect{r}(t)$
            for this circle and find the critical points of $f(\vect{r}(t))$.)
          \end{problem}

          \begin{solution}

          \end{solution}

\section{Lagrange Multipliers} %14.8

          \begin{problem}
            A rancher wants to enclose a rectangular area using the
            straight edge of a cliff on one side, and barbed wire on
            the other three sides. If the rancher wants to maximize
            the area of this rectangle, what are the dimensions of the
            fence and the maximized area? In other words, find $x,y$ which
            maximize $A(x,y)=xy$ given the constraint $2x+y=100$, and
            the value of $A$ for those values.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Plot the constraint $2x+y=100$ from the previous problem in
            the $xy$-plane, along with the level curves of $A(x,y)=xy$
            for $k=750,1000,1250,1500$. (Make sure the image includes
            $x\in[-100,100]$ and $y\in[-100,100]$.)
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{remark}
  Notice that the point $(x,y)$ which maximizes the value of $A(x,y)$
  is where the constraint $2x+y=100$ and the level curve $xy=1250$
  share the same tangent line (and therefore normal vectors).
\end{remark}

\begin{theorem}
  A function
  $f(P)$ of many variables, constrained by the requirement
  $g(P)=k$ for some function $g$ and constant $k$, is maximized or
  minimized at a point $P_0$ where the normal vector to the
  level curve/surface $f(P)=f(P_0)$ is parallel to the normal vector
  to the curve/surface $g(P)=k$. Put another way:
  \[
    \nabla f = \lambda(\nabla g)
  \]
\end{theorem}

\begin{theorem}
  The \textbf{Method of Lagrange Multiplers} for two-variable functions
  states that to maximize/minimize $f(x,y)$ on the constraint $g(x,y)=k$,
  you should solve the system of equations
    \[
      f_x(x,y)=\lambda g_x(x,y)
    \]
    \[
      f_y(x,y)=\lambda g_y(x,y)
    \]
    \[
      g(x,y)=k
    \]
  where $\lambda$ is an unknown real number,
  and testing all solutions of $x,y$ to find the maximum
  and minimum values of $f$.
\end{theorem}

          \begin{problem}
            Use the Method of Lagrange Multipliers to solve the first
            problem of this section. (Tip: to start, use the first
            two equations and eliminate the variable $\lambda$ since
            it's not needed for the solution.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Find the minimum surface area of a right circular cylinder
            with volume equal to $432\pi$ cubic units.
            (Hint: $V=\pi r^2h$ and $SA=2\pi r(r+h)$.)
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            OPTIONAL.
            A river of constant width $10\sqrt{3}\approx 17.32$ meters
            flows $40$ meters per second from north to south. A swimmer
            on the west side of the river can swim at a constant
            $20$ meters per second through still waters, but since the flow
            of the river is faster than her top speed, this swimmer will
            unavoidably be pushed downstream if she tries to swim across.

            Use the Method of Lagrange Mutlipliers to prove that if
            the swimmer sets an angle of $\frac{\pi}{6}=30^\circ$ north of
            east, then she will minimize the distance she is pushed downstream
            as she swims from the west riverbank to the east riverbank.
            (Hint: define $x(\theta,t)$ to be the distance she travels east
            after $t$ seconds if she sets the angle $\theta$, and define
            $y(\theta,t)$ to be the distance she travels south
            after $t$ seconds if she sets the angle $\theta$. Then your
            constrant is that $x(\theta,t)$ should be the width of the river,
            although that's actually not needed to solve the puzzle...)
          \end{problem}

          \begin{solution}

          \end{solution}

\begin{theorem}
  The \textbf{Method of Lagrange Multiplers} for three-variable functions
  states that to maximize/minimize $f(x,y,z)$ on the constraint $g(x,y,z)=k$,
  you should solve the system of equations
    \[
      f_x(x,y,z)=\lambda g_x(x,y,z)
    \]
    \[
      f_y(x,y,z)=\lambda g_y(x,y,z)
    \]
    \[
      f_z(x,y,z)=\lambda g_z(x,y,z)
    \]
    \[
      g(x,y,z)=k
    \]
  where $\lambda$ is an unknown real number,
  and testing all solutions of $x,y,z$ to find the maximum
  and minimum values of $f$.
\end{theorem}

          \begin{problem}
            Find the maximum volume of a rectangular box without a lid
            which uses $48$ square units of material.
          \end{problem}

          \begin{solution}

          \end{solution}

          \begin{problem}
            Find the highest and lowest points which lay on the curve of intersection for the cylinder $x^2+y^2=8$ and the plane
            $2x+2y+z=16$.
          \end{problem}

          \begin{solution}

          \end{solution}

\end{document}


\section{Results}\label{sec:analysis}

\begin{table}[t]
\centering
\begin{small}
\begin{tabular}{| c | p{2.44in} |}\hline
\textbf{Parameter} & \textbf{Definition}	\\\hline
Traffic Demand per Subscriber (\S~\ref{subsec:behavior})	& \(\dfrac{\text{total bytes transferred in
measurement int.}}{\text{number of contributing subscribers}}\)	\\
Peak Demand (\S~\ref{subsec:behavior})			& Daily 95th percentile of bytes transferred in any
15-minute interval \\
Prime-Time Ratio (\S~\ref{subsec:primetime}) 	& \( \dfrac{ \text{avg usage in peak (prime-time)
hour}}{ \text{avg usage in off-peak hour}}\) 		\\
Peak-to-Average Ratio (\S~\ref{subsec:peakratio}) 	& \(\dfrac{\text{95\%-ile of daily traffic
demand}}{\text{mean of daily traffic demand}}\)	\\\hline
\end{tabular}
\end{small}
\caption{\green{Evaluation Metrics.}}
\label{tab:eval-criteria}
\end{table}

\paragraph{Metrics}
Table~\ref{tab:eval-criteria} shows the metrics that we use to evaluate
how user demand responds to service-tier upgrades. The \emph{traffic
  demand} for a subscriber is defined as the total bytes transferred, in
upstream or downstream, during a single sample measurement (15 minutes).
We use traffic demand to calculate the total demand per hour, and the
average and 95th percentile peak demand over a day. To compare the total
traffic of the control and treatment groups, we scale to a thousand
subscribers wherever applicable (Table~\ref{tab:data-stats}).  We
define \emph{prime time} as 8:00 p.m. to 12:00 a.m., when Internet usage
tends to be highest.  Indeed, we
observed that the total daily traffic consistently falls within 90th percentile
during this four-hour period. We define the \emph{prime-time ratio} as
the ratio of traffic during an average prime-time hour, to the average
hourly traffic outside the prime-time hour.  This ratio conveys the
disparity between demand during the prime-time and the rest of the day.
The rest of this section explores the effects of a service-tier upgrade
on user traffic demand in the context of these metrics.

\input{behavior}

\input{primetime}

\input{peakratio}

% PUT RESULTS TABLE HERE

%\input{prevalence}


\section{Type theoretic universes}

To complete our specification of dependent type theory, we introduce type theoretic \emph{universes}. Universes are types that consist of types. In other words, a universe is a type $\UU$ that comes equipped with a type family $\Ty$ over $\UU$, and for any $X:\UU$ we think of $X$ as an \emph{encoding} of the type $\Ty(X)$. We call this type family the \emph{universal type family}.

There are several reasons to equip type theory with universes. One reason is that it enables us to define new type families over inductive types, using their induction principle. For example, since the universe is itself a type, we can use the induction principle of $\bool$ to obtain a map $P:\bool\to\UU$ from any two terms $X_0,X_1:\UU$. Then we obtain a type family over $\bool$ by substituting $P$ into the universal type family:
\begin{equation*}
  x:\bool\vdash \Ty(P(x))~\mathrm{type}
\end{equation*}
satisfying $\Ty(P(0_\bool))\jdeq \Ty(X_0)$ and $\Ty(P(1_\bool))\jdeq \Ty(X_1)$.

We use this way of defining type families to define many familiar relations over $\N$, such as $\leq$ and $<$. We also introduce a relation called \emph{observational equality} $\mathsf{Eq}_\N$ on $\N$, which we can think of as equality of $\N$. This relation is reflexive, symmetric, and transitive, and moreover it is the least reflexive relation. Furthermore, one of the most important aspects of observational equality $\mathsf{Eq}_\N$ on $\N$ is that $\mathsf{Eq}_\N(m,n)$ is a type for every $m,n:\N$, unlike judgmental equality. Therefore we can use type theory to reason about observational equality on $\N$. Indeed, in the exercises we show that some very elementary mathematics can already be done at this early stage in our development of type theory.

A second reason to introduce universes is that it allows us to define many types of types equipped with structure. One of the most important examples is the type of groups, which is the type of types equipped with the group operations satisfying the group laws, and for which the underlying type is a set. We won't discuss the condition for a type to be a set until \cref{chap:hierarchy}, so the definition of groups in type theory will be given much later. Therefore we illustrate this use of the universe by giving simpler examples: pointed types, graphs, and reflexive graphs.

One of the aspects that make universes useful is that they are postulated to be closed under all the type constructors. For example, if we are given $X:\UU$ and $P:\Ty(X)\to \UU$, then the universe is equipped with a term
\begin{equation*}
  \check{\Sigma}(X,P):\UU
\end{equation*}
satisfying the judgmental equality $\Ty(\check{\Sigma}(X,P)\jdeq\sm{x:\Ty(X)}\Ty(P(x))$. We will similarly assume that any universe is closed under $\Pi$-types and the other ways of forming types. However, there is an important restriction: it would be inconsistent to assume that the universe is contained in itself. One way of thinking about this is that universes are types of \emph{small} types, and it cannot be the case that the universe is small with respect to itself. We address this problem by assuming that there are many universes: enough universes so that any type family can be obtained by substituting into the universal type family of some universe.

\subsection{Specification of type theoretic universes}

In the following definition we already state that universes are closed under identity types. Identity types will be introduced in \cref{chap:identity}.

\begin{defn}
  A \define{universe}\index{universe|textbf} in type theory is a closed type $\UU$\index{U@{$\UU$}|textbf} equipped with a type family $\Ty$\index{Ty@{$\Ty$}} over $\UU$ called the \define{universal family}\index{universal family}\index{family!universal family|textbf}, equipped with the following structure:
  \begin{enumerate}
  \item $\UU$ is closed under $\Pi$, in the sense that it comes equipped with a function
    \begin{equation*}
      \check{\Pi} :\prd{X:\UU}(\Ty(X)\to\UU)\to\UU
    \end{equation*}
    for which the judgmental equality
    \begin{equation*}
      \Ty\big(\check{\Pi}(X,P)\big)\jdeq \prd{x:\Ty(X)}\Ty(P(x)).
    \end{equation*}
    holds, for every $X:\UU$ and $P:\Ty(X)\to\UU$.
  \item $\UU$ is closed under $\Sigma$ in the sense that it comes equipped with a function
    \begin{equation*}
      \check{\Sigma} :\prd{X:\UU}(\Ty(X)\to\UU)\to\UU
    \end{equation*}
    for which the judgmental equality
    \begin{equation*}
      \Ty\big(\check{\Sigma}(X,P)\big) \jdeq \sm{x:\Ty(X)}\Ty(P(x))
    \end{equation*}
    holds, for every $X:\UU$ and $P:\Ty(X)\to\UU$.
  \item $\UU$ is closed under identity types, in the sense that it comes equipped with a function
    \begin{equation*}
      \check{\mathrm{I}} : \prd{X:\UU}\Ty(X)\to(\Ty(X)\to\UU)
    \end{equation*}
    for which the judgmental equality
    \begin{equation*}
      \Ty\big(\check{\mathrm{I}}(X,x,y)\big)\jdeq (\id{x}{y})
    \end{equation*}
    holds, for every $X:\UU$ and $x,y:\Ty(X)$.
  \item $\UU$ is closed under coproducts, in the sense that it comes equipped with a function
    \begin{equation*}
      \check{+}:\UU\to (\UU\to\UU)
    \end{equation*}
    that satisfies $\Ty\big(X\check{+}Y\big)\jdeq \Ty(X)+\Ty(Y)$.
  \item $\UU$ contains terms $\check{\emptyt},\check{\unit},\check{\N}:\UU$
    that satisfy the judgmental equalities
    \begin{align*}
      \Ty\big(\check{\emptyt}\big) & \jdeq \emptyt \\
      \Ty\big(\check{\unit}\big) & \jdeq \unit \\
      \Ty(\check{\N}) & \jdeq \N.
    \end{align*}
  \end{enumerate}
  Given a universe $\UU$, we say that a type $A$ in context $\Gamma$ is \define{small}\index{small type|textbf} with respect to $\UU$ if it occurs in the universe, i.e., if it comes equipped with a term $\check{A}:\UU$ in context $\Gamma$, for which the judgment
  \begin{equation*}
    \Gamma\vdash\Ty\big(\check{A}\big)\jdeq A~\mathrm{type}
  \end{equation*}
  holds. If $A$ is small with respect to $\UU$, we usually write simply $A$ for $\check{A}$ and also $A$ for $\Ty(\check{A})$. In other words, by $A:\UU$ we mean that $A$ is a small type.
\end{defn}

\begin{rmk}
  Since ordinar function types are defined as a special case of dependent function types, we don't have to assume that universes are closed under ordinary function types. Similarly, it follows from the assumption that universes are closed under dependent pair types that universes are closed under cartesian product types.
\end{rmk}

\subsection{Assuming enough universes}
  Most of the time we will get by with assuming one universe $\UU$, and indeed we recommend on a first reading of this text to simply assume that there is one universe $\UU$. However, sometimes we might need a second universe $\mathcal{V}$ that contains $\UU$ as well as all the types in $\UU$. In such situations we cannot get by with a single universe, because the assumption that $\UU$ is a term of itself would lead to inconsistencies like the Russel's paradox.

  Russel's paradox is the famous argument that there cannot be a set of all sets. If there were such a set $S$, then we could consider Russel's subset
  \begin{equation*}
    R:=\{x\in S\mid x\notin x\}.
  \end{equation*}
  Russell then observed that $R\in R$ if and only if $R\notin R$, so we reach a contradiction. A variant of this argument reaches a similar contradiction when we assume that $\UU$ is a universe that contains a term $\check{\UU}:\UU$ such that $\mathcal{T}\big(\check{\UU}\big)\jdeq \UU$. In order to avoid such paradoxes, Russell and Whitehead formulated the \emph{ramified theory of types} in their book \emph{Principia Mathematica}. The ramified theory of types is a precursor of Martin L\"of's type theory that we are studying in this course.

  Even though the universe is not a term of itself, it is still convenient if every type, including any universe, is small with respect to \emph{some} universe. Therefore we will assume that there are sufficiently many universes: we will assume that for every finite list of types
\begin{align*}
  \Gamma_1 & \vdash A_1~\mathrm{type} \\
  & ~\vdots \\
  \Gamma_n & \vdash A_n~\mathrm{type},
\end{align*}
there is a universe $\UU$ that contains each $A_i$ in the sense that $\UU$ comes equipped with a term
\begin{align*}
  \Gamma_i\vdash \check{A}_i:\UU
\end{align*}
for which the judgment
\begin{equation*}
  \Gamma_i\vdash \Ty\big(\check{A}_i\big)\jdeq A_i~\mathrm{type}
\end{equation*}
holds. With this assumption it will rarely be necessary to work with more than one universe at the same time.

\begin{rmk}
  Using the assumption that for any finite list of types in context there is a universe that contains those types, we obtain many specific universes:
  \begin{enumerate}
  \item There is a \emph{base universe} $\UU_0$ that we obtain using the empty list of types in context. This is a universe, but it isn't specified to contain any further types.
  \item Given a finite list
    \begin{align*}
      \Gamma_1 & \vdash A_1~\mathrm{type} \\
      & ~\vdots \\
      \Gamma_n & \vdash A_n~\mathrm{type},
    \end{align*}
    of types in context, and a universe $\UU$ that contains them, there is a universe $\UU^+$ that contains all the types in $\UU$ as well as $\UU$. More precisely, it is specified by the finite list
    \begin{align*}
      & \vdash \UU~\mathrm{type} \\
      X:\UU & \vdash \mathcal{T}(X)~\mathrm{type}.
    \end{align*}
    Note that since the universe $\UU^+$ contains all the types in $\UU$, it also contains the types $A_1,\ldots,A_n$. To see this, we derive that there is a code for $A_i$ in $\UU^+$.
    \begin{prooftree}
      \AxiomC{$\Gamma_i\vdash\check{A}_i:\UU$}
      \AxiomC{$X:\UU\vdash\check{\mathcal{T}}(X):\UU^+$}
      \UnaryInfC{$\Gamma_i,X:\UU\vdash\check{\mathcal{T}}(X):\UU^+$}
      \BinaryInfC{$\Gamma_i\vdash\check{\mathcal{T}}(\check{A}_i):\UU^+$}
    \end{prooftree}
    We leave it as an exercise to derive the judgmental equality
    \begin{equation*}
      \mathcal{T}^+(\check{\mathcal{T}}(\check{A}_i))\jdeq A_i.
    \end{equation*}
  \item Given two finite lists
    \begin{align*}
      \Gamma_1 & \vdash A_1~\mathrm{type} & \Delta_1 & \vdash B_1~\mathrm{type} \\
      & ~\vdots & & ~\vdots \\
      \Gamma_n & \vdash A_n~\mathrm{type} & \Delta_m & \vdash B_m~\mathrm{type}
    \end{align*}
    of types in context, and two universes $\mathcal{U}$ and $\mathcal{V}$ that contain $A_1,\ldots,A_n$ and $B_1,\ldots,B_m$ respectively, there is a universe $\UU\sqcup\mathcal{V}$ that contains the types of both $\UU$ and $\mathcal{V}$. The universe $\UU\sqcup\mathcal{V}$ is specified by the finite list
    \begin{align*}
      X:\UU & \vdash \mathcal{T}_{\mathcal{U}}(X)~\mathrm{type} \\
      Y:\mathcal{V} & \vdash \mathcal{T}_{\mathcal{V}}(Y) ~\mathrm{type}.
    \end{align*}
    With an argument similar to the previous construction of a universe, we see that the universe $\UU\sqcup\mathcal{V}$ contains the types $A_1,\ldots,A_n$ as well as the types $B_1,\ldots,B_m$.

    Note that we could also directly obtain a universe $\mathcal{W}$ that contains the types $A_1,\ldots,A_n$ and $B_1,\ldots,B_m$. However, this universe might not contain all the types in $\UU$ or all the types in $\mathcal{V}$.
  \end{enumerate}
  Since we don't postulate any relations between the universes, there are indeed very few of them. For example, the base universe $\UU_0$ might contain many more types than it is postulated to contain. Nevertheless, there are some relations between the universes. For instance, there is a function $\UU\to\UU^+$, since we can simply derive
  \begin{prooftree}
    \AxiomC{$X:\UU\vdash \check{\mathcal{T}}(X):\UU^+$}
    \UnaryInfC{$\vdash \lam{X}\check{\mathcal{T}}(X) : \UU\to\UU^+$}
  \end{prooftree}
  Similarly, there are functions $\UU\to \UU\sqcup\mathcal{V}$ and $\mathcal{V}\to \UU\sqcup\mathcal{V}$ for any two universes $\UU$ and $\mathcal{V}$.
\end{rmk}

\subsection{Pointed types}

\begin{defn}
  A \define{pointed type} is a pair $(A,a)$ consisting of a type $A$ and a term $a:A$. The type of all pointed types in a universe $\UU$ is defined to be
  \begin{equation*}
    \UU_\ast \defeq \sm{X:\UU}X.
  \end{equation*}
\end{defn}

\begin{defn}
  Consider two pointed types $(A,a)$ and $(B,b)$. A \define{pointed map} from $(A,a)$ to $(B,b)$ is a pair $(f,p)$ consisting of a function $f:A\to B$ and an identification $p:f(a)=b$. We write
  \begin{equation*}
    A\to_\ast B \defeq \sm{f:A\to B}f(a)=b
  \end{equation*}
  for the type of all pointed maps from $(A,a)$ to $(B,b)$, leaving the base point implicit.
\end{defn}

Since we have a type $\UU_\ast$ of \emph{all} pointed types in a universe $\UU$, we can start defining operations on $\UU_\ast$. An important example of such an operation is to take the loop space of a pointed type.

\begin{defn}
  We define the \define{loop space} operation $\Omega : \UU_\ast \to \UU_\ast$
  \begin{equation*}
    \Omega(A,a)\defeq \big((a=a),\refl{a}\big).
  \end{equation*}
\end{defn}

We can even go further and define the \emph{iterated loop space} of a pointed type. Note that this definition could not be given in type theory if we didn't have universes.

\begin{defn}
  Given a pointed type $(A,a)$ and a natural number $n$, we define the $n$-th loop space $\Omega^n(A,a)$ by induction on $n:\N$, taking
  \begin{align*}
    \Omega^0(A,a) & \defeq (A,a) \\
    \Omega^{n+1}(A,a) & \defeq \Omega(\Omega^n(A,a)).
  \end{align*}
\end{defn}

\subsection{Relations on the natural numbers}

\begin{defn}\label{defn:obs_nat}
We define the \define{observational equality}\index{observational equality!on N@{on $\N$}} on $\N$ as binary relation $\mathsf{Eq}_\N:\N\to(\N\to\UU)$\index{Eq_N@{$\mathsf{Eq}_\N$}|textbf} satisfying
\begin{align*}
\mathsf{Eq}_\N(\zeroN,\zeroN) & \jdeq \unit & \mathsf{Eq}_\N(\succN(n),\zeroN) & \jdeq \emptyt \\
\mathsf{Eq}_\N(\zeroN,\succN(n)) & \jdeq \emptyt & \mathsf{Eq}_\N(\succN(n),\succN(m)) & \jdeq \mathsf{Eq}_\N(n,m).
\end{align*}
\end{defn}

\begin{constr}
We define $\mathsf{Eq}_\N$ by double induction on $\N$. By the first application of induction it suffices to provide
\begin{align*}
E_0 & : \N\to\UU \\
E_S & : \N\to (\N\to\UU)\to(\N\to\UU)
\end{align*}
We define $E_0$ by induction, taking $E_{00}\defeq \unit$ and $E_{0S}(n,X,m)\defeq \emptyt$. The resulting family $E_0$ satisfies
\begin{align*}
E_0(\zeroN) & \jdeq \unit \\
E_0(\succN(n)) & \jdeq \emptyt.
\end{align*}
We define $E_S$ by induction, taking $E_{S0}\defeq \emptyt$ and $E_{S0}(n,X,m)\defeq X(m)$. The resulting family $E_S$ satisfies
\begin{align*}
E_S(n,X,\zeroN) & \jdeq \emptyt \\
E_S(n,X,\succN(m)) & \jdeq X(m)
\end{align*}
Therefore we have by the computation rule for the first induction that the judgmental equality
\begin{align*}
\mathsf{Eq}_\N(\zeroN,m) & \jdeq E_0(m) \\
\mathsf{Eq}_\N(\succN(n),m) & \jdeq E_S(n,\mathsf{Eq}_\N(n),m)
\end{align*}
holds, from which the judgmental equalities in the statement of the definition follow.
\end{constr}

\begin{lem}
  Suppose $R:\N\to(\N\to\UU)$ is a reflexive relation on $\N$, i.e., $R$ comes equipped with
  \begin{equation*}
    \rho : \prd{n:\N}R(n,n).
  \end{equation*}
  Then there is a family of maps
  \begin{equation*}
    \prd{m,n:\N}\EqN(m,n)\to R(m,n).
  \end{equation*}
\end{lem}

\begin{proof}
  We will prove by induction on $m,n:\N$ that there is a term of type
  \begin{equation*}
    f_{m,n}:\prd{e:\EqN(m,n)}{R:\N\to(\N\to\UU)}\Big(\prd{x:\N}R(x,x)\Big)\to R(m,n)
  \end{equation*}
  The dependent function $f_{m,n}$ is defined by
  \begin{align*}
    f_{\zeroN,\zeroN} & \defeq \lam{\ttt}{r}{\rho}\rho(\zeroN) \\
    f_{\zeroN,\succN(n)} & \defeq \indempty \\
    f_{\succN(m),\zeroN} & \defeq \indempty \\
    f_{\succN(m),\succN(n)} & \defeq \lam{e}{R}{\rho}f_{m,n}(e,R',\rho'),
  \end{align*}
  where $R'$ and $\rho'$ are given by
  \begin{align*}
    R'(m,n) & \defeq R(\succN(m),\succN(n)) \\
    \rho'(n) & \defeq \rho(\succN(n)).\qedhere
  \end{align*}
\end{proof}

We can also define observational equality for many other kinds of types, such as $\bool$ or $\Z$. In each of these cases, what sets the observational equality apart from other relations is that it is the \emph{least} reflexive relation.

\subsection{The finite types}

\begin{defn}\label{defn:fin}
We define the type family $\mathsf{Fin}:\N\to\UU$ of finite types\index{Fin@{$\mathsf{Fin}$}|textbf}\index{finite types|textbf} by induction on $\N$\index{family!of finite types}, taking
\begin{align*}
\mathsf{Fin}(\zeroN) & \defeq \emptyt \\
\mathsf{Fin}(\succN(n)) & \defeq \mathsf{Fin}(n)+\unit
\end{align*}
\end{defn}

\begin{defn}
  For each $n:\N$, we define a map
  \begin{equation*}
    \prd{m:\N} \big((m<n)\to \mathsf{Fin}(n)\big)
  \end{equation*}
\end{defn}

\begin{proof}
  We construct this map by induction on $n,m:\N$. In the base case for $n$, the map is constructed by induction on the empty type, since the relation $m<\zeroN$ never holds. For the inductive step
\end{proof}

\begin{exercises}
\item Let $A$ be a type.
  \begin{subexenum}
  \item Show that $(A+\neg A)\to(\neg\neg A\to A)$.
  \item Show that $\neg\neg\neg A \to \neg A$.
  \end{subexenum}
\item Construct a function
  \begin{equation*}
    \check{\Pi}:\prd{A:\UU} (\Ty(A)\to\UU)\to \UU
  \end{equation*}
  such that
  \begin{equation*}
    \Ty(\check{\Pi}(A,B))\jdeq \prd{x:\Ty(A)}\Ty(B(x))
  \end{equation*}
  holds for every $A:\UU$ and $B:\Ty(A)\to\UU$.

  \emph{A similar exercise can be posed for $\Sigma$ and $+$ (and for $\to$ and $\times$ as special cases of $\Pi$ and $\Sigma$).}
\item \label{ex:obs_nat_eqrel}Show that observational equality on $\N$\index{observational equality!on N@{on $\N$}!is an equivalence relation} is an equivalence relation\index{equivalence relation!observational equality on N@{observational equality on $\N$}}, i.e., construct terms of the following types:
  \begin{align*}
    & \prd{n:\N} \EqN(n,n) \\
    & \prd{n,m:\N} \EqN(n,m)\to \EqN(m,n) \\
    & \prd{n,m,l:\N} \EqN(n,m)\to (\EqN(m,l)\to \EqN(n,l)).
  \end{align*}
\item \label{ex:obs_nat_least}\index{observational equality!on N@{on $\N$}!is least reflexive relation}Let $R$ be a reflexive binary relation\index{reflexive relation}\index{relation!reflexive} on $\N$, i.e., $R$ is of type $\N\to (\N\to\UU)$ and comes equipped with a term $\rho:\prd{n:\N}R(n,n)$. Show that
  \begin{equation*}
    \prd{n,m:\N} \EqN(n,m)\to R(n,m).
  \end{equation*}
\item \index{observational equality!on N@{on $\N$}!is preserved by functions}Show that every function $f:\N\to \N$ preserves observational equality in the sense that
  \begin{equation*}
    \prd{n,m:\N} \EqN(n,m)\to \EqN(f(n),f(m)).
  \end{equation*}
  \emph{Hint: to get the inductive step going the induction hypothesis has to be strong enough. Construct by double induction a term of type}
  \begin{equation*}
    \prd{n,m:\N}{f:\N\to\N} \EqN(n,m)\to \EqN(f(n),f(m)),
  \end{equation*}
  \emph{and pull out the universal quantification over $f:\N\to\N$ by \cref{ex:swap}.}
\item
  \begin{subexenum}
  \item Define the \define{order relations}\index{relation!order}\index{order relation} $\leq$ and $<$ on $\N$.
  \item Show that $\leq$ is reflexive and that $<$ is \define{anti-reflexive}\index{anti-reflexive}\index{relation!anti-reflexive}, i.e., that $\neg(n<n)$.
  \item Show that both $\leq$ and $<$ are transitive, and that $n<S(n)$.
  \item Show that $k\leq \min(m,n)$ holds if and only if both $k\leq m$ and $k\leq n$ hold, and show that $\max(m,n)\leq k$ holds if and only if both $m\leq k$ and $n\leq k$ hold.
  \end{subexenum}
\item \label{ex:obs_bool}\index{observational equality!on 2@{on $\bool$}}
  \begin{subexenum}
  \item Define observational equality $\mathsf{Eq}_\bool$\index{Eq_bool@{$\mathsf{Eq}_\bool$}|textbf} on the booleans.
  \item Show that $\mathsf{Eq}_\bool$ is reflexive.\index{observational equality!on 2@{on $\bool$}!is reflexive}
  \item Show that for any reflexive relation $R:\bool\to(\bool\to \UU)$ one has\index{observational equality!on 2@{on $\bool$}!is least reflexive relation}
    \begin{equation*}
      \prd{x,y:\bool} \mathsf{Eq}_\bool(x,y)\to R(x,y).
    \end{equation*}
  \end{subexenum}
\item \label{ex:int_order}
  \begin{subexenum}
  \item Define the order relations\index{relation!order}\index{order relation} $\leq$ and $<$ on and $\Z$.
  \item For $k:\Z$, consider the type $\Z_{\geq k}\defeq \sm{n:\Z}n\geq k$. Construct
    \begin{align*}
      b_k & : \Z_{\geq k} \\
      s_k & : \Z_{\geq k}\to\Z_{\geq k},
    \end{align*}
    and show that $\Z_{\geq k}$ satisfies the induction principle of the natural numbers\index{induction principle!of N@{of $\N$}}:
    \begin{equation*}
      \ind{\Z_{\geq k}} : P(b_k)\to \Big(\prd{n:\Z_{\geq k}} P(n)\to P(s_k(n))\Big)\to \Big(\prd{n:\Z_{\geq k}} P(n)\Big)
    \end{equation*}
  \end{subexenum}
\item
  \begin{subexenum}
  \item Show that $\N$ satisfies \define{strong induction}, i.e., construct for any type family $P$ over $\N$ a function of type
    \begin{equation*}
      P(\zeroN) \to \Big(\prd{k:\N}\Big(\prd{m:\N} (m\leq k) \to P(m)\Big)\to P(\succN(k))\Big) \to \prd{n:\N}P(n).
    \end{equation*}
  \item Show that $\N$ satisfies \define{ordinal induction}, i.e., construct for any type family $P$ over $\N$ a function of type
    \begin{equation*}
      \Big(\prd{k:\N} \Big(\prd{m:\N} (m< k) \to P(m)\Big)\to P(k)\Big) \to \prd{n:\N}P(n).
    \end{equation*}
  \end{subexenum}
\item
  \begin{subexenum}
  \item For each $i:\mathsf{Fin}(\succN(n))$, define a function
    \begin{equation*}
      \mathsf{skip}_i : \mathsf{Fin}(n)\to\mathsf{Fin}(\succN(n))
    \end{equation*}
    that includes $\mathsf{Fin}(n)$ in $\mathsf{Fin}(\succN(n))$ by skipping $i$.
  \item For each $i:\mathsf{Fin}(n)$, define a function
    \begin{equation*}
      \mathsf{double}_i : \mathsf{Fin}(\succN(n))\to\mathsf{Fin}(n)
    \end{equation*}
    that projects $\mathsf{Fin}(\succN(n))$ onto $\mathsf{Fin}(n)$ by doubling at $i$.
  \end{subexenum}
\end{exercises}


\documentclass{tufte-handout}

%\geometry{showframe}% for debugging purposes -- displays the margins

\usepackage{amsmath}

% Set up the images/graphics package
\usepackage{asymptote} % for asymptote graphics
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio}
\graphicspath{{graphics/}}

\title{Summing Approximations}
\author[Warren MacEvoy]{Warren MacEvoy}
% \date{24 January 2009}  % if the \date{} command is left out, the current date will be used

% The following package makes prettier tables.  We're all about the bling!
\usepackage{booktabs}

\hypersetup{colorlinks} % Comment this line if you don't wish to have colored links
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{listings}
\usepackage{siunitx} % align decimals in tables
\usepackage{nth}     % 1st, 2nd, ...
\usepackage{microtype} % Improves character and word spacing
\usepackage{asymptote} % for asymptote graphics
\usepackage{ccicons}
\usepackage{lipsum} % Inserts dummy text
\usepackage{booktabs} % Better horizontal rules in tables
\usepackage{graphicx} % Needed to insert images into the document

% The units package provides nice, non-stacked fractions and better spacing
% for units.
\usepackage{units}

% The fancyvrb package lets us customize the formatting of verbatim
% environments.  We use a slightly smaller font.
\usepackage{fancyvrb}
\fvset{fontsize=\normalsize}

% Small sections of multiple columns
\usepackage{multicol}

% Provides paragraphs of dummy text
\usepackage{lipsum}

% These commands are used to pretty-print LaTeX commands
\newcommand{\doccmd}[1]{\texttt{\textbackslash#1}}% command name -- adds backslash automatically
\newcommand{\docopt}[1]{\ensuremath{\langle}\textrm{\textit{#1}}\ensuremath{\rangle}}% optional command argument
\newcommand{\docarg}[1]{\textrm{\textit{#1}}}% (required) command argument
\newenvironment{docspec}{\begin{quote}\noindent}{\end{quote}}% command specification environment
\newcommand{\docenv}[1]{\textsf{#1}}% environment name
\newcommand{\docpkg}[1]{\texttt{#1}}% package name
\newcommand{\doccls}[1]{\texttt{#1}}% document class name
\newcommand{\docclsopt}[1]{\texttt{#1}}% document class option name

\theoremstyle{definition}
\newtheorem{definition}{Definition}
\theoremstyle{example}
\newtheorem{example}{Example}
\theoremstyle{theorem}
\newtheorem{theorem}{Theorem}
\begin{document}

\maketitle% this prints the handout title, author, and date

\begin{abstract}
\noindent Understanding asymptotic behavior of a series or function is useful.
\end{abstract}

%\printclassoptions

\section{Estimating Sums}

\begin{figure}
\begin{center}
\includegraphics[width=1.00\linewidth]{graphics/intsum.pdf}
\end{center}
\caption{\label{fig:intmid}Comparing sum (black rectangles) with integrals of increasing $f$ (red), $f$ shifted -1/2 (green), and $f$ shifted -1 (blue).}
\label{fig:intmid}
\end{figure}

Summing is a linear operation, so

\begin{equation}
  \sum_{k=0}^{n-1} \left[ \alpha f(k) + \beta g(k) \right] = \alpha \sum_{k=0}^{n-1} f(k) + \beta \sum_{k=0}^{n-1} g(k) \,.
\end{equation}

Summing powers of $k$:

\begin{equation}
  \sum_{k=0}^{n-1} 1 = n \,.
\end{equation}

\begin{equation}
  \sum_{k=0}^{n-1} k = \frac{1}{2} n(n-1) = \frac{1}{2} n^2 + O(n)\,.
\end{equation}

\begin{equation}
  \sum_{k=0}^{n-1} k^2 = \frac{1}{3}n(n-1/2)(n-1) = \frac{1}{3} n^3 + O(n^2) \,.
\end{equation}

\begin{equation}
  \sum_{k=0}^{n-1} k^3 = \frac{1}{4}n^2(n-1)^2 = \frac{1}{4} n^4 + O(n^3) \,.
\end{equation}

Generally, for $p>0$,

\begin{equation}
  \sum_{k=0}^{n-1} k^p \approx \frac{1}{p+1} (n-1/2)^{p+1} = \frac{1}{p+1} n^{p+1} +O(n^p) \,.
\end{equation}

Estimating sums with integrals.

If $f(x)$ is increasing, then
\begin{equation}
\int_{a-1}^{b} f(x) \, dx \leq \sum_{k=a}^{b} f(k) \approx \int_{a-1/2}^{b+1/2} f(x) dx \leq \int_{a}^{b+1} f(x) \, dx
\end{equation}

\end{document}


\chapter{An introduction to arbitrage pricing theory}
\label{chap:arbitrage}

We shall now review the basic setting and results of arbitrage pricing theory.

\section{Discrete one period model}

In order to gain an insight into the basic concepts in mathematical finance, an overview of discrete one period model is given in this chapter. This treatment is based on \textcite[pp. 5--34]{bjork2004arbitrage} and \textcite[pp. 3--12]{duffie2010dynamic}. \added[comment={Added contribution}]{While the context follows these sources closely, the presentation should be unique. For example, we do not assume the ecistance of a risk-free rate.}. A more complete overview of discrete model with multiple time periods can be found in \textcite[pp. 33--85]{musielarutkowski2005martingale}.

Let $(\Omega, \Pf)$ be a discrete probability space with $\Omega = \{ \omega_1, \omega_2, \ldots , \omega_m \}$ and we assume that $\Pf (\omega ) > 0$ for all $\omega \in \Omega$. The discrete one period market model is the sample space $\Omega$ coupled with the price process $S$. We assume that at the time $t=0$ the price vector $S_0 \in \R^n$ is a known constant and at the time $t=1$ it is a random vector $S_1 : \Omega \rightarrow \R^n$. Thus our model consists of one time step with $n$ stochastic assets.

A portfolio $w$ is a vector in $\R^n$. Since we do not put any restrictions on $w$, we assume that there are no restriction on buying or short selling of assets in this market. We denote the starting value of portfolio $w$ by $V_0^w = w^{\top} S_0$ and the final value of it is a random variable $V_1^w = w^{\top} S_1$.

We say that the portfolio $w$ is an arbitrage portfolio if either
  \begin{enumerate}[labelindent=\parindent, leftmargin=*]
    \item $V_0^w < 0$ and $V_1^w \geq 0$ or
    \item $V_0^w \leq 0$ and $V_1^w > 0$
  \end{enumerate}
$\Pf$-surely. We say that the market with starting prices is arbitrage free if there are no arbitrage portfolios. We denote
  \begin{align}
    \label{marketmatrixindiscretesetting}
    M = \begin{bmatrix} S_1 ( \omega_1 ) & S_1 ( \omega_2 ) & \ldots & S_1  ( \omega_m ) \end{bmatrix}_{n \times m} = \begin{bmatrix} M_1^{\top} \\ M_2^{\top} \\ \vdots \\ M_n^{\top} \end{bmatrix}_{n \times m}
  \end{align}
where $M_i^{\top} \in \R_m$ is the vector for terminal prices of $i$th asset. Now the absence of arbitrage is equivalent to that neither
  \begin{enumerate}[labelindent=\parindent, leftmargin=*]
    \item $w^{\top} S_0 = V_0^w < 0$ and $w^{\top} M \geq 0$ nor
    \item $w^{\top} S_0 = V_0^w \leq 0$ and $w^{\top} M > 0$
  \end{enumerate}
does not hold for any portfolio $w$. We recall that for vectors, we use $x > 0$ to denote that every component of $x$ is positive. Likewise $x \geq 0$ denotes component-wise non-negativity.

\subsection{The first fundamental theorem of asset pricing in discrete one period model}

A state-price vector is a vector $x \in \R^m$ satisfying $S_0 = Mx$ and $x > 0$. This definition can be understood in the following context. If $x \in \R^m$ is a state-price vector and $x = \beta y$, where $\left| y \right| = 1 $ and $\beta = \left| x \right|$, then $S_0 = Mx = \beta M y$. where $My$ is the expected value of $S_1$ under the probability measure where the state probabilities are given by the vector $y$. The probability vector of the original probability space $\Pf$ does not have to be a state-price vector.

It may happen that the state-price vector does not exists, but if it does, then $V_0^{w} = w^{\top} S_0 = w^{\top} Mx$ for all portfolios $w$ and this implies that arbitrage does not exist. Suppose that $w \in \R^n$ and $x$ is a state-price vector. Now $x > 0$ and $w^{\top} S_0 = w^{\top} Mx < 0$ implies that $w^{\top} M$ has a negative component. Likewise $x > 0$ and $w^{\top} S_0 = w^{\top} Mx = 0$ implies that not all elements of $w^{\top} M$ are positive.

We shall now prove the reverse implication using a variant of hyperplane separation theorem, which can be found in many standard textbooks for convex optimization.

\begin{thm}[Hyperplane separation theorem]
Let $A$ and $B$ be closed convex subsets of $R^s$. If either of them is compact, then there exists $0 \not = x \in R^s$ such that
  \begin{align}
    a^{\top} x < b^{\top} x
  \end{align}
for all $a \in A$ and $b \in B$.
\end{thm}

\begin{lemma}
\label{discreteonetimefirstfundamentallemma}
The market $(\Omega, S)$ is arbitrage free if and only if a state-price vector exists.
\end{lemma}

\begin{proof}
\added[comment={Added reference}]{The argument presented here is essentially the same as the one found in} \textcite[p. 4]{duffie2010dynamic}. We denote
  \begin{align}
    A = \{ \ (-w^{\top} S_0, w^{\top} M ) \in \R^{m+1} \ | \ w \in \R^n \ \}
  \end{align}
and $C = \R_+^{m+1}$. Now the absence of arbitrage is equivalent to $A \cap C = \{ 0 \}$. We need to only prove that the absence of arbitrage implies that the state-price vector does exist.

It is clear that $A$ is a closed and convex linear subspace of $\R^{m+1}$. We consider the function $f$ defined by $z \mapsto z / \left| z \right|$ for all $0 \not = z \in \R^{m+1}$. Since $C$ is a convex subset, it is easy to to see that the convex closure of $B = f(C \setminus \{ 0 \})$ is a convex and compact subset of $C$.

By the hyperplane separation theorem, there exists $0 \not = y \in R^{m+1}$ such that $a^{\top} y < b^{\top} y$ for all $a \in A$ and $b \in B$. Since $0 \in A$, $0 < b^{\top} y$ for all $b \in B$, which implies that $0 < c^{\top} y$ for all $0 \not = c \in C$. Coordinate vectors are in $C$ and this means that $y > 0$. Since $A$ is a subspace, $a \in A$ implies that $-a \in A$ and this means that $0 \leq a^{\top} y < \left| c^{\top} y \right|$ for all $a \in A$ and $0 \not = c \in C$. Thus $a^{\top} y = 0 $ for all $a \in A$.

If $y = (y_1, y_2, \ldots , y_{m+1})^{\top} \in \R^{m+1}$ and $y^* = (y_2, \ldots , y_{m+1})^{\top} \in \R^{m}$, then
  \begin{align}
    y_1 w^{\top} S_0 = w^{\top} M y^*
  \end{align}
for all $w \in \R^n$, which implies $x = y^* / y_1 > 0$ is a state-price vector.
\end{proof}

If the market is arbitrage free and $x = (x_1, \ldots x_m)^{\top}$ is a state-price vector, then by denoting
  \begin{align}
    \beta = \sum_{i=1}^m x_i
  \end{align}
and $\Pm (\omega_i) = q_i = x_i / \beta > 0$ for all $i = 1,2, \ldots ,m$, we have a new probability measure $\Pm$ on $\Omega$. This measure has the property
  \begin{align}
    \label{martingalemeasureequationdiscrete}
    S_0 = M x = \beta M \frac{x}{\beta} = \beta \E_{\Pm} \left( S_1 \right) .
  \end{align}
Since we assumed that $\Pf (\omega) > 0$ for all $\omega \in \Omega$, it is easy to see that $\Pm (\omega) > 0$ for all $\omega \in \Omega$.

A deflator $d$ is a strictly positive process, so that $d_0 > 0$ and $d_1(\omega) > 0$ for all $\omega \in \Omega$. Now we may define relative price processes
\begin{align}
S^d_0 &= \frac{S_0}{d_0}, \\
S^d_1 &= \frac{S_1}{d_1}
\end{align}
with regards to the deflator $d$.

\begin{lemma}
	\label{discreteonetimemartingalemeasurelemma}
	The market has a state-space vector if and only if the market with relative prices has a state-price vector.
\end{lemma}

\begin{proof}
	Let
	\begin{align}
	M^d = \left[ \ \frac{S_1 ( \omega_1 )}{ d_1 (\omega_1) } \ \frac{S_1 ( \omega_2 )}{ d_1 (\omega_2) } \ \ldots \ \frac{S_1  ( \omega_m )}{ d_1 (\omega_m) } \ \right]_{n \times m} .
	\end{align}
	If $x = (x_1, x_2, \ldots , x_m)^{\top} > 0$ and
	\begin{align}
	x^d = d_0^{-1} ( x_1 d_1 (\omega_1), x_2 d_1 (\omega_2), \ldots , x_m d_1 (\omega_m) )^{\top},
	\end{align}
	then $d_0 M^d* x^d = Mx$. Therefore $S_0 = Mx$ if and only if $S^d_0 = M^d x^d$, where $x^d \in \R^m$ and $x^d > 0$.
\end{proof}

Inspired by these observations, we define that a measure $\Pm$ that satisfies
  \begin{enumerate}[labelindent=\parindent, leftmargin=*]
    \item $\Pf( \omega ) = 0$ if and only if $\Pm ( \omega ) = 0$ (meaning that the measures share null sets) and
    \item there exists a deflator $d$ such that
    \begin{align}
    		S^d_0 = \E_{\Pm} \left( S^d_1 \right)
   	 \end{align}
  \end{enumerate}
is an equivalent martingale measure induced by deflator $d$. By Lemma \ref{discreteonetimemartingalemeasurelemma}, the original market is arbitrage-free if and only if the deflated market is arbitrage-free, which is equivalent to
    \begin{align}
		S^d_0 = \beta \E_{\Pm} \left( S^d_1 \right)
	\end{align}
for some $\beta \in \R_+$ and measure $\Pm$. We can define a new deflator $e$ by $e_0 = d_0$ and $e_1 = \beta d_1$. Now
    \begin{align}
		S^e_0 = \beta \E_{\Pm} \left( S^e_1 \right)
	\end{align}
meaning that $\Pm$ is an EMM induced by $e$. Thus we see that the market is arbitrage-free if and only if it has an EMM.

The curious aspect of the probabilities of the EMM is that they are not influenced by the original probability measure $\Pf$ beyond the fact that they share the same sets of non-zero probabilities. In fact, the probabilities of the EMM is defined by the original price vector $S_0$ and the state space $M$.

Assume that $\Pm$ is an equivalent measure. Since $\Pf$ is probability measure with no null sets, we may define a new random variable called Radon-Nikod\'{y}m derivative as
\begin{align}
\Lambda ( \omega ) = \frac{ \Pm ( \omega ) }{ \Pf ( \omega ) }
\end{align}
for all $\omega \in \Omega$. If $X$ is a random variable, then
\begin{align}
\E_{\Pf} (\Lambda X) = \sum_{i=1}^m \Pf (\omega_i) \frac{ \Pm ( \omega_i ) }{ \Pf ( \omega_i ) } X( \omega_i ) = \E_{\Pm} (X)
\end{align}
so we see that the expectations under different measures are linked by the Radon-Nikod\'{y}m derivative. By this we see that $X$ is a martingale under the measure $\Pm$ if and only if $\Lambda X$ is a martingale under the measure $\Pf$.

A num\'{e}raire is any asset with only positive prices. If one of the assets is a num\'{e}raire with initial price $s_0$ and terminal price $s_1( \omega )$, then we use it as a deflator and define relative price process
  \begin{align}
    S_0^* &= \frac{S_0}{s_0}, \\
    S_1^* &= \frac{S_1}{s_1}.
  \end{align}
If the market is arbitrage-free, then by Lemma \ref{discreteonetimemartingalemeasurelemma} there exists such $\beta \in \R_+$ and measure $\Pm$ that
\begin{align}
S^*_0 = \beta \E_{\Pm} \left( S^*_1 \right) .
\end{align}
But since $s$ is one of the assets, it must hold that $\beta = 1$ meaning that the measure $\Pm$ is an equivalent martingale measure.

We can now state the first fundamental theorem of asset pricing that weaves these different concepts together.

\begin{thm}[First fundamental theorem of asset pricing for discrete one period model]
The following are equivalent in a discrete one period market model:
  \begin{enumerate}[labelindent=\parindent, leftmargin=*]
    \item the market is arbitrage free,
    \item a state-price vector exists and
    \item an equivalent martingale measure $\Pm$ exists.
  \end{enumerate}
If $\Pm_d$ is an EMM induced by a deflator $d$ and $\Pm_e$ is an EMM induced by a deflator $e$, then
  	\begin{align}
		S_0 = d_0 \E_{\Pm_d} \left( S^d_1 \right) = e_0 \E_{\Pm_d} \left( S^e_1 \right) .
	\end{align}
In particularly, if the market has a num\'{e}raire $s$ and there is no arbitrage, then a martingale measure induced by $s$ satisfies
  	\begin{align}
		S_0 = s_0 \E_{\Pm} \left( \frac{S_1}{s_1} \right).
	\end{align}
\end{thm}

A risk-free asset is a num\'{e}raire with a constant terminal price $z$. We assume that the risk-free asset has initial price of $1$ and terminal price of $1+r$, where $r > -1$. Thus if a risk-free asset exists and there is no arbitrage, an EMM $\Pm$ induced by a risk-free asset satisfies
	\begin{align}
		S_0 = \E_{\Pm} \left( \frac{S_1}{1+r} \right) .
	\end{align}

\subsection{The second fundamental theorem of asset pricing in discrete one period model}

\added[comment={Added reference}]{The arguments presented in this section is} essentially the same as the ones in \textcite[pp. 31--34]{bjork2004arbitrage}

How should we then price derivatives in this model? A contingent claim $X$ is a random variable $X : \Omega \rightarrow \R$. If the the original market is arbitrage-free, then the first fundamental theorem of asset pricing implies that an equivalent martingale measure $\Pm$ induced by a deflator $d$ satisfies
  \begin{align}
    S_0 & = d_0 \E_{\Pm} \left( S^d_1 \right) .
  \end{align}
It would be natural to define
  	\begin{align}
		X_0 & = d_0 \E_{\Pm} \left( X^d_1 \right)
	\end{align}
to be the initial price of the claim $X$. But since there may be different EMMs, $X_0$ may not be well-defined. Thus it is vital to pose the question how many measures there are. If the market is arbitrage free, then the equation $S_0 = M x$ has a solution by Lemma \ref{discreteonetimefirstfundamentallemma}. By basic linear algebra, this solution is unique if and only if $\Kernel{ ( M ) } = 0$. We also know that null space is the orthogonal compliment of the row space meaning that
  \begin{align}
    \label{linearalgebraduality}
    \Kernel{ ( M ) } = \Image{ (M^{\top}) }^{\perp}
  \end{align}
and this suggests that we have a closer look at the image set $\{ x^{\top} M \ | \ x \in \R^n \} $. But the image set is just the set of all possible portfolios of the original market.

If there is such portfolio $w$ that $w^{\top} S_1 = X$ $\Pf$-surely, then we say that $X$ is replicated by the portfolio $w$. This is equivalent to
	\begin{align}
		X \in \{ x^{\top} M \ | \ x \in \R^n \} .
	\end{align}
By the first fundamental theorem of asset pricing, we see that if the contingent claim $X$ can be replicated and the market has no arbitrage, then for any pair of an EMM $\Pm$ and a replicating portfolio $w$, we have that
  \begin{align}
    V_0^w = d_0 \E_{\Pm} \left( w^{\top} S^d_1 \right) = d_0 \E_{\Pm} \left( \frac{X}{d} \right),
  \end{align}
where $d$ is a deflator inducing $\Pm$. The left hand side depends only on the replicating portfolio $w$ and the right hand size depends only on the measure $\Pm$ and the deflator. This implies that $V_0^{w_1} = V_0^{w_2}$ for any portfolios $w_1, w_2$ which replicates $X$.

We define $Z_0 = (w^{\top} S_0, S_0^{\top})^{\top}$, $Z_1 = (w^{\top} S_1, S_1^{\top})^{\top}$. If $\Pm$ is an EMM with a deflator $d$, then
  	\begin{align}
		Z_0 = d_0 \E_{\Pm} \left( Z^d_1 \right)
	\end{align}
if and only if
  	\begin{align}
		w^{\top} S_0 &= d_0 \E_{\Pm} \left( w^{\top} S^d_1 \right) ,\\
		S_0 &= d_0 \E_{\Pm} \left( S^d_1 \right) .
	\end{align}
Hence, if the market is arbitrage-free, then the arbitrage-free price of a replicated contingent claim $X$ is
$w^{\top} S_0$, where $w$ is any replicating portfolio.

Now we define that the market is complete if every contingent claim can be replicated. This means that if $M$ is defined as in \ref{marketmatrixindiscretesetting}, then the marker is complete if and only if
  \begin{align}
    \Image{ (M^{\top}) } = \{ M^{\top} w \ | \ w \in \R^n \} = \{ (w^{\top} M)^{\top} \ | \ w \in \R^n \} = \R^m
  \end{align}
meaning that the matrix $M$ has a rank of $m$. By duality in \ref{linearalgebraduality}, we see that this is equivalent to $\Kernel{ ( M ) } = 0$. Therefore the market is complete if and only if the solution to the equation in Lemma \ref{discreteonetimefirstfundamentallemma} has a unique solution. Thus we have proved the second fundamental theorem of asset pricing.

\begin{thm}[Second fundamental theorem of asset pricing]
Assume that the market is arbitrage free. The market is complete if and only if there is a deflator that induces a unique equivalent martingale measure. Then every EMM induced by a given deflator is unique. If the market has a num\'{e}raire, then the market is complete if and only if EMM induced by the num\'{e}raire is unique.
\end{thm}

Completeness also implies that the market has $m$ linearly independent asset price processes since the matrix $M$ has a rank of $m$. This means, in a sense, that for every risk dimension is covered be tradable assets and therefore every possible contingent claim can be replicated.

So we have identified three distinct scenarios which are from best to worst:
  \begin{enumerate}[labelindent=\parindent, leftmargin=*]
    \item The market model is arbitrage free and complete which is equivalent to the fact the there exists a deflator with a unique martingale measure. Every contingent claim can be given a unique price which is the cost of replicating portfolio. Or, if $d$ is deflator with induced EMM $\Pm$, then the initial arbitrage-free price of a contingent claim $X$ is
    	\begin{align}
    		\label{deflatorinducedpricingformula}
	    	X_0 = d_0 \E_{\Pm} \left( \frac{X_1}{d_1} \right) .
    	\end{align}
    This price does not depend on the choice of the deflator $d$ as long as the deflator induces an EMM.
    \item The market is arbitrage free but not complete. Then every deflator which induces an equivalent martingale measure have several EMMs. Every replicated contingent claim can be given a unique arbitrage-free price which is the cost of replicating portfolio. This price is given by Equation \ref{deflatorinducedpricingformula}. Contingent claims that could not be replicated may not be priced in the sense of the Equation \ref{deflatorinducedpricingformula}.
    \item The market has arbitrage which makes pricing rather meaningless.
  \end{enumerate}

Thus, if the market does not allow arbitrage, then we may price every replicated contingent claim can be given an arbitrage-free price. This price is the cost of the cost of replicating portfolio $w$ which is equal to
    \begin{align}
		V^w_0 = d_0 \E_{\Pm} \left( \frac{V^w_1}{d_1} \right) ,
	\end{align}
where $d$ is any deflator that induces an EMM $\Pm$. If the market has a num\'{e}raire, then we may use this as the deflator.


% !TEX root = CSL 2021.tex

To illustrate our construction, we start from a relatively concrete example: we consider a simply-typed lambda calculus with a base type $\mathsf{Real}$ and primitives for real numbers, and we follow the plan outlined in the introduction, which yields for each simple type a notion of approximate value, approximate function, diameter and distance between programs. Most definitions are straightforward and intuitive: the interesting, not immediately obvious point is that our construction does yield a \emph{partial metric} on each type.

\emph{Simple types} are defined as follows: $\mathsf{Real}$ is a simple type; if $A$ and $B$ are simple types, then $A \to B$ and $A \times B$ are simple types.
For all $n>0$, we fix a set $\mathcal{F}_n$ of functions from $\mathbb{R}^n$ to $\mathbb{R}$. We consider the usual Curry-style simply-typed $\lambda$-calculus over the types defined above (the left and right projection are denoted by $\pi_L:A\times B \to A$ and  $\pi_R:A\times B \to B$ respectively, and the constructor for pairs by $\left\langle-,-\right\rangle$), enriched with the following constants: for all $r \in \mathbb{R}$, a constant $r:\mathsf{Real}$; for all $n>0$ and all $f\in\mathcal{F}_n$, a constant $f:\mathsf{Real}\to\ldots\to\mathsf{Real}\to\mathsf{Real}$. We call this calculus $\STLC$, and its terms are simply called \emph{terms}. We write $t[x_1 := u_1, \ldots, x_n := u_n]$ to denote the \emph{simultaneous} substitution of $u_1, \ldots, u_n$ for $x_1, \ldots, x_n$ in $t$. For all types $A$, we denote by $\Lambda_A$ the set of closed terms of type $A$. The relation of $\beta$-reduction is enriched with the following rule, extended to all contexts: for all $n>0$, $f\in\mathcal{F}_n$, and $r_1,\ldots,r_n\in\mathbb{R}$, $f r_1 \ldots r_n \to_\beta s$, where $s = f(r_1, \ldots, r_n)$. By standard arguments \cite{abramsky:handbook-2}, this calculus has the properties of subject reduction, confluence and strong normalisation.

\begin{remark}\label{rem:continuous}
The class of real-valued functions which can be computed in $\STLC$ depends on the choice we make for $\mathcal F_{n}$. With suitable choices (see for instance \cite{TaylorReal, Di-Gianantonio:2013aa,Edalat:2000aa}) one can obtain that all programs of type $\mathsf{Real}\to \mathsf{Real}$ compute \emph{continuous} functions\footnote{Note that for this to be possible, $\mathcal F_{n}$ cannot contain the identity function over $\mathsf{Real}$.}, that all such programs are \emph{integrable} over closed intervals, or that all such programs are \emph{continuously differentiable}.

\end{remark}

In addition to the usual notion of $\beta$-equivalence between terms of $\STLC$, we will exploit also a stronger equivalence: given two closed terms $t,u$ of type $A$,  we say that $t$ and $u$ are \emph{observationally equivalent} and write $t \oeq_A u$ if for all terms $C$ such that $x:A \vdash C : \mathsf{Real}$ is derivable, $C[x:=t]$ is $\beta$-equivalent to $C[x:=u]$ (which amounts to saying that they both $\beta$-reduce to the same real number).  It is clear that observational equivalence is a congruence and that two $\beta$-equivalent terms are always observationally equivalent. %The notion of distance we define in the next paragraph will be a generalization of this notion of equivalence.

\subsection{Approximate Values and Approximate Programs}

The first step of our construction for $\STLC$ is to associate to each simple type $A$ a set $\intervals{A}$ whose elements are certain sets of programs of type $A$ that we call \emph{approximate values of type $A$}.
A closed term $t\in \Lambda_{A}$ represents a program with return type $A$ and no parameters, so an approximate value can be thought of as a specification of a program with return type $A$ and no parameters \emph{up to} a certain degree of error or approximation.

For each simple type $A$, the set of approximate values $\intervals{A}\subseteq \mathcal P(\Lambda_A)$ is defined inductively as follows:
\begin{itemize}
\item $\intervals{\mathsf{Real}} = \{ \{ t \in \Lambda_\mathsf{Real} \mid \exists r \in I, t \to_\beta^* r \} \mid I \subseteq \mathbb{R} \text{ is a compact interval or $\emptyset$ or } \mathbb{R} \}$,
\item $\intervals{A \times B} = \{ a \times b \mid a  \in \intervals{A}, b \in \intervals{B} \}$, where $a \times b = \{ t \in \Lambda_{A \times B} \mid \pi_L t \in a \text{ and } \pi_R t \in b \}$,
\item $\intervals{A \to B} = \{ \{ t \in \Lambda_{A \to B} \mid \forall u \in \Lambda_{A},~ tu \in I(u) \} \mid I : \Lambda_A \to \intervals{B} \}$.
\end{itemize}

The approximate values of type $\mathsf{Real}$ are sets of closed programs of type $\mathsf{Real}$ which essentially coincide with the compact intervals of $\mathbb R$, plus the empty set and $\mathbb{R}$ itself. An approximate value in $\intervals{A\times B}$ is a ``rectangle'' $a \times b$, with $a\in \intervals{A}$ and $b \in \intervals{B}$, while an approximate value in $\intervals{A\to B}$ is uniquely determined by a function
$I$ from closed terms $u\in \Lambda_{ A}$ to approximate values $I(u)\in \intervals{B}$.

For example, any two terms $t,u\in \Lambda_{\mathsf{Real}}$ with normal forms $q, r\in \mathbb{R}$ induce an approximate value $[t,u]_{\mathsf{Real}}= \{v\in \Lambda_{\mathsf{Real}}\mid  v\to_{\beta}^{*} s \land (q\leq s\leq r \vee q\geq s\geq r)\}$ of type $\mathsf{Real}$. Similarly, any two terms $t,u\in \Lambda_{\mathsf{Real}\to \mathsf{Real}}$ induce an approximate value
$[t,u]_{\mathsf{Real}\to \mathsf{Real}}= \{v\in \Lambda_{\mathsf{Real}\to\mathsf{Real}}\mid
\forall r \in \Lambda_{\mathsf{Real}} \ vr\in [ tr, ur]_{\mathsf{Real}}
\}$. For instance, if $t= \lambda x.\sin(x)+1$ and $u=\lambda x.\cos(x)-1$, then
$[t,u]_{\mathsf{Real}\to \mathsf{Real}}$ contains all closed terms corresponding to maps oscillating between $\sin (x)+1$ and $\cos(x)+1$ (\textit{e.g.} the program $\lambda x. \sin(x+1)$, as illustrated in Fig. \ref{fig:interval1}).

\begin{figure}
\begin{subfigure}{0.42\textwidth}
\parbox[h][3cm][c]{\textwidth}{
\adjustbox{center}{$
\begin{tikzpicture}[domain=-5:5, scale=0.5]
%\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[<->]   (-5,0) -- (5,0);
\draw[<->] (0,-3) -- (0,3); % node[above] {$f(x)$};

   % \node at (0,0)[circle,fill,inner sep=1pt]{};
%
\draw[color=blue, samples=40] plot (\x,{sin(\x r)+1}) node[above] {\tiny$\sin(x)+1$};
\draw[color=orange, samples=40] plot (\x,{cos(\x r)-1}) node[below] {\tiny$\cos(x)-1$};

\draw[color=gray, dotted, thick, samples=40] plot (\x,{sin(deg(\x+1) ))}) node[below] {\tiny$\sin(x+1)$};

\end{tikzpicture}$}
}
\caption{$\lambda x.\sin(x+1)$ is in $[\lambda x.\sin(x)+1, \lambda x.\cos(x)+1]_{\mathsf{Real}\to\mathsf{Real}}$.}
\label{fig:interval1}
\end{subfigure} \ \ \
%\end{figure}
%\begin{figure}
\begin{subfigure}{0.52\textwidth}
\parbox[h][3cm][c]{\textwidth}{
\adjustbox{center}{$
\begin{tikzpicture}[domain=-4:4, scale=0.65]

%\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[<->]   (-4,0) -- (4,0);
\draw[<->] (0,-2) -- (0,2); % node[above] {$f(x)$};

\draw[dashed, |-|] (-1,0) -- (1,0);
\node(a) at (-1,-0.3) {\tiny$-1$};
\node(a) at (1,-0.3) {\tiny$1$};

\node(a) at (0.3,-1.2) {\tiny$-1$};
\node(a) at (-0.3,0.8) {\tiny$1$};
   % \node at (0,0)[circle,fill,inner sep=1pt]{};

\draw[|-|] (4.8,0.4) -- node[right] {\tiny$\varepsilon$} (4.8,1.3);

\draw[|-|] (5.4,0.35) -- node[right] {\tiny$\delta$} (5.4,0.45);

\draw[color=red, domain=-4:4, samples=40] plot (\x, {(1/2*sqrt(2*pi))*exp(-((\x)^2)) } ) node[above] {\tiny$u[x]$};
%\draw[color=violet, domain=-1.2:1.2] plot (\x, { (\x)^3 } );

\draw[color=blue, domain=-4:0] plot (\x,-1);
\draw[color=blue, domain=0:4] plot (\x,1) node[above] {\tiny$t[x]$};

\draw[dotted] (-4,0.4) -- (4.8,0.4);
\draw[dotted] (-4,1.3) -- (4.8,1.3);

\node(z) at (-1,0.4) {\tiny$\bullet$};
\node(zz) at (1,0.4) {\tiny$\bullet$};

\node(z) at (0,-1) {\tiny$\bullet$};
\node(zz) at (0,1) {\tiny$\bullet$};

\node(r) at (0,0.4) {\tiny$\bullet$};
\node(rr) at (0.3,0.5) {\tiny$r$};

\end{tikzpicture}$}
}
\caption{$\varepsilon=(\partial(u)\circ \partial(t))([-1,1])$ is bigger than $\delta=\partial(u\circ t)([-1,1])=[r,r]$.}
\label{fig:interval2}
\end{subfigure}
\caption{Examples of functional approximate values and of approximate programs. }
\end{figure}

For all $A$, the set $\intervals{A}$ is a a subset of $\mathcal{P}(\Lambda_A)$ closed under arbitrary intersections. We deduce that $\intervals{A}$ has arbitrary meets (given by intersections) and arbitrary joins
$\bigvee_{i\in I}a_{i}= \bigcap \{ a\in \intervals{A}\mid \forall i\in I \ a_{i}\subseteq a\}$, and thus $\intervals{A}$ is a complete lattice. In particular, for all $t\in\Lambda_A$, there is a least element of $\intervals{A}$ that contains $t$, which will be denoted by $\tointerval{t}$.
One can check that $\tointerval{t} = \tointerval{u}$ if and only if $t\oeq_{A} u$.

Monotone functions from approximate values to approximate values represent \emph{approximate programs}. They behave like a model of the simply-typed $\lambda$-calculus in a weak sense, namely: \begin{itemize}
\item for all monotone functions $\vec{\alpha} \mapsto c[\vec{\alpha}] : \intervals{A_1} \times \ldots \times \intervals {A_n} \to \intervals {B \to C}$ and $\vec{\alpha} \mapsto b[\vec{\alpha}] : \intervals{A_1} \times \ldots \times \intervals {A_n} \to \intervals {B}$, we can define a monotone function $\vec{\alpha} \mapsto (c[\vec{\alpha}]~ b[\vec{\alpha}]) = \sup\{\tointerval{vu} \mid v \in c[\vec{\alpha}], u \in b[\vec{\alpha}]\} : \intervals{A_1} \times \ldots \times \intervals {A_n} \to \intervals {C}$,
\item for all monotone functions $\vec{\alpha} \mapsto c[\vec{\alpha}] : \intervals{A_1} \times \ldots \times \intervals {A_n} \to \intervals {C}$ and all $i \leq n$, we can define a monotone function $(\alpha_j)_{j \neq i} \mapsto (\lambda \alpha_i.~ c[\vec{\alpha}]) = \{ v \in \Lambda_{A_i \to C} \mid \forall t_i \in \Lambda_{A_i},~ v t_i \in c[\alpha_1, \ldots, \tointerval{t_i}, \ldots, \alpha_n] \} : \prod_{j \neq i} \intervals{A_j} \to \intervals{A_i \to C}$,
\end{itemize}
and these two constructions are weakly compatible with $\beta$-reduction and $\eta$-expansion:

\begin{proposition} \label{prop:intervals-weak-model-lambda} For all monotone functions $(\vec{\alpha}, \beta) \mapsto c[\vec{\alpha}, \beta] : \intervals{A_1} \times \ldots \times \intervals {A_n} \times  \intervals {B} \to \intervals {C}$ and $\vec{\alpha} \mapsto b[\vec{\alpha}] : \intervals{A_1} \times \ldots \times \intervals {A_n} \to \intervals {B}$, $\left(\vec{\alpha} \mapsto (\lambda \beta.~  c[\vec{\alpha}, \beta])~ b[\vec{\alpha}]\right) \leq \left(\vec{\alpha} \mapsto c[\vec{\alpha}, b[\vec{\alpha}]]\right)$, %$$\begin{array}{ll} & \vec{\alpha} \mapsto (\lambda \beta.~  c[\vec{\alpha}, \beta])~ b[\vec{\alpha}] \\ \leq & \vec{\alpha} \mapsto c[\vec{\alpha}, b[\vec{\alpha}]]\text{,}\end{array}$$
and for all monotone functions $\vec{\alpha} \mapsto d[\vec{\alpha}] : \intervals{A_1} \times \ldots \times \intervals {A_n} \to  \intervals {B \to C}$,
$\left(\vec{\alpha} \mapsto \lambda \beta.~  d[\vec{\alpha}]~ \beta\right) \geq \left(\vec{\alpha} \mapsto d[\vec{\alpha}]\right)$,
%$$\begin{array}{ll} & \vec{\alpha} \mapsto \lambda \beta.~  d[\vec{\alpha}]~ \beta \\ \geq & \vec{\alpha} \mapsto d[\vec{\alpha}]\text{,}\end{array}$$
where functions are ordered by pointwise inclusion.  In other words, on approximate programs, $\beta$-reduction and $\eta$-expansion \emph{discard} information, and conversely $\beta$-expansion and $\eta$-reduction \emph{recover} some information.
\end{proposition}

\begin{proof} Without loss of generality, we can assume $n=0$.
Let $v \in \lambda \beta.~ c[\beta]$ and $u \in b$. By definition, $tu \in c[\tointerval{u}]$, so $\tointerval{tu} \subseteq c[\tointerval{u}] \subseteq c[b]$. Therefore, $(\lambda \beta.~ c[\beta])~ b \subseteq b$.
Let $v \in d$. For all $u \in \Lambda_B$, by definition, $vu \in d\tointerval{u}$. Therefore, $v \in \lambda \beta.~ d~ \beta$.
\end{proof}

Beyond theoretical aspects (which will be made clearer in Section 5)
Proposition \ref{prop:intervals-weak-model-lambda} is also important in practice because it implies that if we compute an approximation of a program from approximations of its parts and then simplify the resulting approximate program using $\beta$-reduction and $\eta$-expansion, what we obtain is still a valid approximation of the original program.

We can define a weak embedding from terms into approximate programs, by mapping each term to its tightest approximation: for all terms $t$ such that $\alpha_{1}:A_{1},\dots,\alpha_{n}:A_{n}\vdash t:B$, we define a monotone function $\partial(t):\intervals{A_{1}}\times \dots \times \intervals{A_{n}} \to \intervals{B}$ by $\partial(t)(a_1, \ldots, a_n) = \sup \{ \tointerval{t u_1 \ldots u_n} \mid u_1 \in a_1, \ldots, u_n \in a_n \}$.

\begin{remark} \label{remark:push-exp-stlc}
The map $\partial$ is constant on classes of observational equivalence, and one can check that it is is weakly compatible with the constructions of the $\lambda$-calculus, in particular:
\begin{itemize}
\item $\partial (\alpha_{i})(a_{1},\dots, a_{n})=a_{i}$,
\item $\partial (tu)(a_{1},\dots, a_{n}) \subseteq \partial (t)(a_{1},\dots, a_{n}) ~ \partial (u)(a_{1},\dots, a_{n})$,
\item $\partial (\lambda \beta. t)(a_{1},\dots, a_{n}) \subseteq \lambda \beta.~ \partial (t)(\beta, a_{1},\dots, a_{n})$.
\end{itemize}
\end{remark}

This map $\partial(t)$ can be taken as a measure of the \emph{sensitivity} of $t$, as it maps an interval $a$, that is a quantifiably uncertain input, to a quantifiably uncertain output $\partial(t)(a)$.
For instance, if we take the term $t[x]= \sin(x)+1$ above, then $\partial(t): \intervals{\mathsf{Real}}\to \intervals{\mathsf{Real}}$ sends the interval $[-\pi,\pi]_{\mathsf{Real}}$ into $[0,2]_{\mathsf{Real}}$.

\begin{remark} \label{remark:oplax-functor-stlc}
When composing two maps $\partial(t)$ and $\partial(u)$, we might obtain a worse approximation than by computing $\partial(t[u/x])$ directly.
For instance, let $t[x]$ and
$u[x]$ be, respectively, the discontinuous and Gaussian functions illustrated in Fig. \ref{fig:interval2}.
If $a$ is the interval $[-1,+1]$, then $\partial(t)(a)=[-1,1]$, and since $u[x:=-1]=u[x:=1]\simeq_{\beta} r$ for some $0<r<1$, we deduce that $\partial(u)(\partial(t)(a))=[-1,1] \supsetneq [r,r  ]= \partial (u[t/x])(a)$.

\end{remark}

\subsection{A Partial Metric on Each Type}
\label{subsection:type-gpms}

So far, we have associated each type $A$ of $\STLC$ with a complete lattice $\intervals{A}\subseteq \mathcal P(\Lambda_{A})$ of approximate values of type $A$, and each typed program $t:A\to B$ with an approximate program $\partial(t)$ (in fact, a monotone function) from approximate values of type $A$ to approximate values of type $B$.
We will now exploit this structure to define, for each type $A$ of $\STLC$, a generalized partial metric on the closed (exact) programs of type $A$. % space $(\Lambda_{A}/\oeq_{A}, \distances{A}, d_{A})$.

% The definition of this space will exploit in an essential way the sets of approximate values $\intervals{A}$.

The first step is to define, for every simple type $A$, a commutative integral quantale $(\distances{A}, \quantalegeq_A, \quantaleop_A)$ of \emph{distances of type $A$}:
\begin{itemize}
\item $(\distances{\mathsf{Real}}, \quantalegeq_\mathsf{Real}, \quantaleop_\mathsf{Real}) = ([0,\infty], \leq, +)$,
\item $\distances{A \times B} = \distances{A} \times \distances{B}$,
\item $\distances{A \to B} =\operatorname{Poset}(\intervals{A}, \distances{B})$.
% \{ \text{monotonically decreasing functions from } \intervals{A} \text{ to } \distances{B} \}$.
\end{itemize}
where, for two posets $Q,R$, $\operatorname{Poset}(Q,R)$ denotes the set of monotone functions from $Q$ to $R$.
Observe that the quantale $\distances{A\to B}$ is a set of functions over the approximate values of $A$.

For all simple types $A$, we now define a \emph{distance function} $d_A : \Lambda_A \times \Lambda_A \to \distances{A}$:
\begin{itemize}
\item $d_\mathsf{Real}(t,u) = \left\vert r-s \right\vert$, where $r,s$ are the unique elements of $\mathbb{R}$ such that $t \to_\beta^* r$ and $u \to_\beta^* s$,
\item $d_{A \times B}(t,u) = (d_A(\pi_L t, \pi_L u), d_B(\pi_R t, \pi_R u))$,
\item $d_{A \to B}(t,u) = a \mapsto \sup \left\{ d_B(rv, sw) \mid r,s \in \{t,u\}, v,w \in a \right\}$.
\end{itemize}

It would be tempting to define $d_{A \to B}(t,u)(a)$ simply as $\sup \left\{ d_B(tv, uw) \mid v,w \in a \right\}$, but then the axiom ``$d_{A \to B}(t,t) \leq d_{A \to B}(t,u)$'' of partial metric spaces would fail.

The maps $d_{A}$ are clearly compatible with observational equivalence (\textit{i.e.} if $a \oeq_A a'$ and $b \oeq_A b'$, then $d_A(a,b) = d_A(a',b')$).

\begin{figure}
\adjustbox{center}{
\begin{tikzpicture}

\draw[|<->|, orange] (-2,0) to node[above] {$a$} (2,0);
\draw[|<->|, blue] (1,0) to node[above]  {$b$} (4,0);
\draw[violet] (1,0) to (2,0);

\draw[|<->|, dashed] (-2,0.8) to node[above] {\tiny$\delta(a\cup b)$} (4,0.8);
\draw[|<->|, dashed] (1,0.5) to node[below] {\tiny$\delta(a\cap b)$} (2,0.5);
\draw[|<->|, dashed] (-2,-0.5) to node[below] {\tiny$\delta(a)$} (2,-0.5);
\draw[|<->|, dashed] (1,-0.8) to node[below] {\tiny$\delta(b)$} (4,-0.8);

\end{tikzpicture}}
\caption{\small The diameter function is \emph{modular} over intersecting real intervals:
$\mathsf{diam}(a\cup  b)+\mathsf{diam}(a\cap b)=\mathsf{diam}(a)+\mathsf{diam}(b)$ for all $a,b\in [\R]$ such that $a\cap b\neq \emptyset$.
This property is at the heart of our generalization of diameters. Observe that this property fails when $a\cap b$ is empty.}
\label{fig:modular}
\end{figure}

Our objective is now to prove that $\left(\Lambda_A/\oeq_A, \distances{A}, d_A\right)$ is a generalized partial metric space.
To this end, we define for all simple types $A$ a monotone \emph{diameter function} $\diam_A : \intervals{A} \to \distances{A}$ by $\diam_A(a) = \sup \{ d_A(t,u) \mid t,u \in a \}$. The key to our objective will be to prove that $\diam_{A}$ is \emph{sub-modular} on intersecting approximate values (henceforth, \emph{quasi-sub-modular} -- see Proposition \ref{prop:submodular}): this generalizes the fact that, on the (real-valued) metric space $\mathbb{R}$, the diameter is \emph{modular} over intersecting closed intervals (see Fig. \ref{fig:modular}).
 %-- or rather, using the quantale ordering convention, supermodular.

 First, one can check that for all $t,u \in \Lambda_A$, $\diam_A\left(\tointerval{t} \vee \tointerval{u}\right) = d_A(t,u)$, and that:
 \begin{itemize}
\item
$\diam_\mathsf{Real}(a)   = \sup\{s-r \mid s,r \in \mathbb{R} \text{ such that } s,r \in a\}$,
\item
$\diam_\mathsf{A \times B}(p)  = \left(\diam_A\left(\sup\left\{\tointerval{\pi_L t} \mid t \in p\right\}\right)\diam_B\left(\sup\left\{\tointerval{\pi_R t} \mid t \in p\right\}\right)\right)$,
\item$ \diam_\mathsf{A \to B}(b)  = a \mapsto \diam_B\left(\sup\left\{\tointerval{vt} \mid t\in a, v \in b\right\}\right)$.
\end{itemize}
This leads then to the following:
%With that, we can prove that the diameter function $\diam_{A}$ is super-modular over intersecting approximate values:
%``quasi-supermodularity'':
\begin{proposition}[$\diam_{A}$ is quasi-sub-modular]\label{prop:submodular} For all simple types $A$ and all $a,b \in \intervals{A}$ such that $a \wedge b \neq \emptyset$, $\diam(a \wedge b) \quantaleop \diam(a \vee b) \quantalegeq \diam(a) \quantaleop \diam(b)$.
\end{proposition}
\begin{proof}
We proceed by induction on types.

Let $a,b \in \intervals{\mathsf{Real}}$ such that $a\wedge b \neq \emptyset$. Let $I = \{r \in \mathbb{R} \mid r \in a\}$ and $J = \{s \in \mathbb{R} \mid s \in b\}$: then $I$ (respectively, $J$, $I \cap J$, $I \cup J$) is either $\mathbb{R}$ or a non-empty compact interval of $\mathbb{R}$, and its length in the usual sense is equal to $\diam_\mathsf{Real}(a)$ (respectively, $\diam_\mathsf{Real}(b)$, $\diam_\mathsf{Real}(a \wedge b)$, $\diam_\mathsf{Real}(a \vee b)$). Note that the only reason we know that $I \cup J$ is an interval is because $a\wedge b \neq \emptyset$ implies $I \cap J \neq \emptyset$. The length of an interval of $\mathbb{R}$ is equal to its Lebesgue measure, therefore $\operatorname{length}(I \cap J) + \operatorname{length}(I \cup J) = \operatorname{length}(I) + \operatorname{length}(J)$, so $\diam_\mathsf{Real}(a \wedge b) \quantaleop \diam_\mathsf{Real}(a \vee b) = \diam_\mathsf{Real}(a) \quantaleop \diam_\mathsf{Real}(b)$.

Let $a,b \in \intervals{A_L \times A_R}$ such that $a\wedge b \neq \emptyset$. For all $c \in \intervals{A_L \times A_R}$, let $c_L = \sup\{\tointerval{\pi_L t} \mid t \in c\}$ and $c_R = \sup\{\tointerval{\pi_R t} \mid t \in c\}$.
One can check that $(a \wedge b)_L = a_L \wedge b_L$, $(a \wedge b)_R = a_R \wedge b_R$, $(a \vee b)_L = a_L \vee b_L$ and $(a \vee b)_R = a_R \vee b_R$, so $\diam(a \wedge b) \quantaleop \diam(a \vee b) = (\diam(a_L \wedge b_L) \quantaleop \diam(a_L \vee b_L),  \diam(a_R \wedge b_R) \quantaleop \diam(a_R \vee b_R)) \quantalegeq (\diam(a_L) \quantaleop \diam(b_L), \diam(a_R) \quantaleop \diam(b_R)) = \diam(a) \quantaleop \diam(b)$.

Let $f,g \in \intervals{A \to B}$ and $a \in \intervals{A}$. For all $h \in \intervals{A \to B}$, let $ha = \sup\{\tointerval{vt} \mid v \in h, t \in a\}$. One can check that $(f \wedge g)a \subseteq (f a) \wedge (g a)$ and $(f \vee g)a = (f a) \vee (g a)$. As a result, $(\diam(f \wedge g)\quantaleop \diam(f \vee g))(a) \quantalegeq \diam((fa) \wedge (ga)) \quantaleop \diam((fa) \vee (ga)) \quantalegeq \diam(fa) \quantaleop \diam(ga) = (\diam(f)\quantaleop\diam(g))(a)$.
\end{proof}

It is well-known  \cite{6845021} that any
function $\delta: L\to \mathbb [0,\infty]$ on a lattice $L$ that is monotone and \emph{sub-modular} induces a pseudo-metric $d: L\times L \to \mathbb [0,\infty]$ by letting $d^*(a,b)=2\delta(a\vee b)-\delta(a)-\delta(b)$. In fact, one can decompose this construction: first, one defines a partial pseudometric $d$ on $L$ by $d(a,b) = \diam(a \vee b)$, and then $d^*$ is just the distance given by equation \eqref{eq:pmettomet}: $d^*(a,b) = 2d(a,b)-d(a,a)-d(b,b)$. We can use this way of reasoning to establish that the maps $d_{A}$ are indeed partial metrics:

\begin{corollary} \label{corollary:stlc-metric} For all simple types $A$, $\left(\Lambda_A/\oeq_A, \distances{A}, d_A\right)$ is a generalized partial metric space, that is to say:
\begin{enumerate}
\item for all $t,u \in \Lambda_A$, $d_A(t,t) \quantalegeq d_A(t,u)$,
\item for all $t,u \in \Lambda_A$, if $d_A(t,t) = d_A(t,u) = d_A(u,u)$, then $t \oeq_A u$,
\item for all $t,u \in \Lambda_A$, $d_A(t,u) = d_A(u,t)$,
\item for all $t,u,v \in \Lambda_A$, $d_A(t,v) \quantaleop d_A(u,u) \quantalegeq d_A(t,u) \quantaleop d_A(u,v)$.
\end{enumerate}
\end{corollary}
\begin{proof}
As mentioned above, for all $t,u\in\Lambda_A$, $d_A(t,u) = \diam_A(\tointerval{t} \vee \tointerval{u})$, which immediately gives point 3. Since $\delta_A$ is monotone and $\tointerval{t} \vee \tointerval{t} \leq \tointerval{t} \vee \tointerval{u}$, we also get point 1.

One can check (by induction on types) that the restriction of $\diam_A$ to the ideal generated by the $\tointerval{t}$ (for $t \in \Lambda_A$) is \emph{strictly} monotone. Therefore, if  $d_A(t,t) = d_A(t,u) = d_A(u,u)$, \textit{i.e.} $\diam_A(\tointerval{t}) = \diam_A(\tointerval{t} \vee \tointerval{u}) = \diam_A(\tointerval{u})$,  then $\tointerval{t} = \tointerval{t} \vee \tointerval{u} = \tointerval{u}$, so $t \oeq_A u$.

The triangular inequality is an immediate consequence of the quasi-sub-modularity of $\diam_A$: $d(t,v) \quantaleop d(u,u) = \diam(\tointerval{t} \vee \tointerval{v}) \quantaleop \diam(\tointerval{u}) \quantalegeq \diam((\tointerval{t} \vee \tointerval{u}) \vee (\tointerval{u} \vee \tointerval{v})) \quantaleop \diam((\tointerval{t} \vee \tointerval{u}) \wedge (\tointerval{u} \vee \tointerval{v})) \quantalegeq \diam(\tointerval{t} \vee  \tointerval{u}) \quantaleop \diam(\tointerval{u} \vee  \tointerval{v}) = d(t,u) \quantaleop d(u,v)$.
\end{proof}

%
%\begin{remark}
%Corollary \ref{corollary:stlc-metric} is a slight refinement of the well-known fact \cite{6845021} that any
%function $\delta: L\to \mathbb [0,\infty]$ on a lattice $L$ that is monotone and \emph{submodular}  (\emph{i.e.}~ satisfying $\delta(a\cup b)+ \delta(a\cap b) \leq \delta(a)+\delta(b)$ - recall that the order on $\mathbb [0,\infty]$, taken as a quantale, is opposite to the usual order) induces a pseudo-metric $d: L\times L \to \mathbb [0,\infty]$ by letting $d(a,b)=2\delta(a\vee b)-\delta(a)-\delta(b)$. In fact, one can decompose this construction: first, one defines a partial pseudometric $p$ on $L$ by $p(a,b) = \diam(a \vee b)$, and then $d$ is just the distance given by equation \eqref{eq:pmettomet}: $d(a,b) = p^*(a,b) = 2p(a,b)-p(a,a)-p(b,b)$.
%
%
%
%\end{remark}
%
%