\section*{Exercise 1.1}
The number of all the conditions that selecting 200 people from 2000 people is
\[
    N_1=\binom{2000}{120}.
\]
The number of the conditions that my friend and I are selected is equal to the number of conditions that selecting 118 people from 1998 people. That is
\[
    N_2=\binom{1998}{118}.
\]
Hence, the probability that my friend and I are selected is
\[
    P=\frac{\binom{1998}{118}}{\binom{2000}{120}}=\frac{1998!}{118!1880!}\div\frac{2000!}{120!1880!}\approx0.00357.
\]

\section*{Exercise 1.2}
\enum{
\item
We denote that $C=B\backslash A$, which means $A\cap C=\varnothing$.

Since $P[\bigcup_k A_k]=\sum_k P[A_k]$, then $P[B]=P[A\cup C]=P[A]+P[C]$.

Since $P[C]\geq 0$, then $P[B]\geq P[A]$.

Hence $P[A]\leq P[B]$.

\item
Since $A$ and $B$ are independent, then $P[A\cap B]=P[A]P[B]>0$.

Hence $A\cap B \neq \varnothing$.

Thus $A$ and $B$ are not mutually exclusive.

\item
We denote that $C=A\cap B$, so that
\[
    (A\backslash C)\cap(C)\cap(B\backslash C)=\varnothing.
\]

Since $P[\bigcup_k A_k]=\sum_k P[A_k]$ for $\bigcap_k A_k=\varnothing$, then

\spl{
    P[A\cup B]&=P[(A\backslash C)\cup C\cup(B\backslash C)]\\
    &=P[A\backslash C]+P[C]+P[B\backslash C]\\
    &=P[A\backslash C]+P[C]+P[B\backslash C]\textbf{+P[C]-P[C]}\\
    &=(P[A\backslash C]+P[C])+(P[B\backslash C]+P[C])-P[C]\\
    &=P[A]+P[B]-P[C]\\
    &=P[A]+P[B]-P[A\cap B].
}
}

\section*{Exercise 1.3}
\enum{
\item
No.

Assume that the probability of "heads" for one toss is $p$, then
\spl{
    &P[\text{two heads}]=p^2=\frac{1}{3}\\
    &P[\text{no head}]=(1-p)^2=\frac{1}{3}\\
    &P[\text{one head}]=1-(1-p)^2-p^2=\frac{1}{3}\\
}
There's no such $p$ satisfying the equations.

\item
No.

Assume that the probabilities of "heads" are respectively $p_1$ and $p_2$, then
\spl{
    &P[\text{two heads}]=p_1p_2=\frac{1}{3}\\
    &P[\text{no head}]=(1-p1)(1-p_2)=\frac{1}{3}\\
    &P[\text{one head}]=p_1(1-p_2)+p_2(1-p_1)=\frac{1}{3}\\
}
We obtain $
\left\{
\begin{aligned}
    p_1&=\frac{3+\sqrt{3}i}{6}\\
    p_2&=\frac{3-\sqrt{3}i}{6}\\
\end{aligned}
\right.
$

Hence there's no real root for $p_1$ and $p_2$.
}

\section*{Exercise 1.4}
\enum{
\item
The probability that the selected participant has been asked the first question is $\frac{1}{2}$; the probability that the selected participant has \textbf{not} been asked the first question is $\frac{1}{2}$.

We denote "claim to see the barn" as "B", "has been asked the first question" as "A".

\[
    P=P[B|A]P[A]+P[B|\urcorner A]P[\urcorner A]=0.17\times0.5+0.03\times0.5=0.1
\]

\item
Since
\spl{
    P[A]&=0.5\neq0\\
    P[B|A]&=0.17,\\
}
then
\[
    P[B]=0.1\neq P[B|A].
\]
Hence seeing the barn is not independent of being asked the first question.
}

\section*{Exercise 1.5}
We suppose that the chips we can get are only from the market. Also, we suppose that the stolen chips are all sold on the market.

Denote that "being defective" as "D" and "being stolen" as "S", so that
\[
    P[S|D]=\frac{P[D|S]P[S]}{P[D]}.
\]
Since the stolen chips are stolen before inspection, then
\begin{equation}\label{1}
    P[D|S]=0.5.
\end{equation}

For the probability that the chip we get was stolen,
\begin{equation}\label{2}
    P[S]=0.01.
\end{equation}

Finally,
\begin{equation}\label{3}
    P[D]=P[D|\urcorner S]P[\urcorner S]+P[D|S]P[S]=0.05\times0.99+0.5\times0.01=0.0545.
\end{equation}

According to Results \ref{1}, \ref{2} and \ref{3}, we obtain
\[
    P[S|D]=\frac{0.5\times0.01}{0.0545}=\frac{10}{109}\approx0.0917.
\]

\section*{Exercise 1.6}
The prisoner is right.

Denote that "A", "B", "C" means A, B, C is going to die, respectively; "B*" means B is told not to die by the warden.

For example, B is told not to die. Then we obtain
\spl{
    P[A|B*]&=\frac{P[B*|A]P[A]}{P[B*|A]P[A]+P[B*|B]P[B]+P[B*|C]P[C]}\\
    &=\frac{\frac{1}{2}\times\frac{1}{3}}{\frac{1}{2}\times\frac{1}{3}+0\times\frac{1}{3}+1\times\frac{1}{3}}=\frac{1}{3}.
}
That is $P[C|B*]=1-P[A|B*]-P[B|B*]=1-\frac{1}{3}-0=\frac{2}{3}$.

In this case, the chance that A is going to die does not change, but the chance that C is going to die doubles.

\section*{Exercise 1.7}
\enum{
\item
\[
m_X(t)=E[e^{tX}]=\sum_{k=1}^n e^{tx_k}\frac{1}{n}=\frac{1}{n}\sum_{k=1}{n}e^{tx_k},\quad t\in\mathbb{R}.
\]
\item
\spl{
    E[X]=\frac{\dd m_X(t)}{\dd t}\bigg|_{t=0}=\frac{1}{n}\sum_{k=1}^n x_ke^{tx_k}\bigg|_{t=0}=\frac{1}{n}\sum_{k=1}^n x_k.
}
\spl{
    Var[X]&=E[X^2]-E[X]^2=\frac{\dd^2 m_X(t)}{\dd t^2}\bigg|_{t=0}-(\frac{\dd m_X(t)}{\dd t}\bigg|_{t=0})^2\\
    &=\frac{1}{n}\sum_{k=1}^n x_k^2 e^{tx_k}\bigg|_{t=0}-(\frac{1}{n}\sum_{k=1}^n x_k)^2\\
    &=\frac{1}{n}\sum_{k=1}^nx_k^2-(\frac{1}{n}\sum_{k=1}^n x_k)^2.
}
}

\section*{Exercise 1.8}
Since there exists some $\varepsilon$ such that $m_X(t)=m_Y(t)$ for all $t\in (-\varepsilon,\varepsilon)$, then their high order of derivatives exist and are the same. In other words,
\[
    \left\{
    \begin{aligned}
        &E[X]=\frac{\dd m_X(t)}{\dd t}\bigg|_{t=0}=\frac{\dd m_Y(t)}{\dd t}\bigg|_{t=0}=E[Y]\\
        &E[X^2]=\frac{\dd^2 m_X(t)}{\dd t^2}\bigg|_{t=0}=\frac{\dd^2 m_Y(t)}{\dd t^2}\bigg|_{t=0}=E[Y^2]\\
        &\vdots\\
        &E[X^{n}]=\frac{\dd^n m_X(t)}{\dd t^n}\bigg|_{t=0}=\frac{\dd^n m_Y(t)}{\dd t^n}\bigg|_{t=0}=E[Y^{n}]\\
    \end{aligned}
    \right.
\]

We also know that
\begin{equation}\label{eq1}
    \left\{
    \begin{aligned}
        &E[X]=\sum_{x=0}^nxf_X(x)\\
        &E[X^2]=\sum_{x=0}^nx^2f_X(x)\\
        &\vdots\\
        &E[X^n]=\sum_{x=0}^nx^nf_X(x)\\
    \end{aligned}
    \right.
\end{equation}

Similarly, for $(Y,f_Y)$,
\begin{equation}\label{eq2}
    \left\{
    \begin{aligned}
        &E[Y]=\sum_{x=0}^nxf_Y(x)\\
        &E[Y^2]=\sum_{x=0}^nx^2f_Y(x)\\
        &\vdots\\
        &E[Y^n]=\sum_{x=0}^nx^nf_Y(x)\\
    \end{aligned}
    \right.
\end{equation}

We also know that
\begin{equation}\label{eq3}
\begin{split}
    &f_X(0)+f_X(1)+\hdots+f_X(n)=1,\\
    &f_Y(0)+f_Y(1)+\hdots+f_Y(n)=1.
\end{split}
\end{equation}

To simplify the calculations, we denote that
\[
    \textbf{E}_{(n+1)\times1}=\left(
    \begin{aligned}
        &E[Y]\\
        &E[Y^2]\\
        &\vdots\\
        &E[Y^n]\\
        &1\\
    \end{aligned}
    \right)
    =
    \left(
    \begin{aligned}
        &E[X]\\
        &E[X^2]\\
        &\vdots\\
        &E[X^n]\\
        &1\\
    \end{aligned}
    \right),
\]
\[
    \textbf{A}_{(n+1)\times(n+1)}=\left(
    \begin{tabular}{cccc}
        0 & 1 & $\hdots$ & n\\
        $0^2$ & $1^2$ & $\hdots$ & $n^2$\\
        &$\vdots$&&\\
        $0^n$ & $1^n$ & $\hdots$ & $n^n$\\
        1 & 1 & $\hdots$ & 1\\
    \end{tabular}
    \right),
\]

where $\textbf{det(A)}\neq0$.

\[
    \textbf{X}_{(n+1)\times1}=\left(
    \begin{aligned}
        &f_X(0)\\
        &f_X(1)\\
        &\vdots\\
        &f_X(n-1)\\
        &f_X(n)\\
    \end{aligned}
    \right),\quad
    \textbf{Y}_{(n+1)\times1}=
    \left(
    \begin{aligned}
        &f_Y(0)\\
        &f_Y(1)\\
        &\vdots\\
        &f_Y(n-1)\\
        &f_Y(n)\\
    \end{aligned}
    \right)
\]

Hence, equations \ref{eq1}, \ref{eq2}, \ref{eq3} can be presented as
\begin{equation}\label{eq4}
\begin{split}
    &\textbf{A}\textbf{X}=\textbf{E}\\
    &\textbf{A}\textbf{Y}=\textbf{E}
\end{split}
\end{equation}

We can tell that Eqs. \ref{eq4} are the same equation system. Since $\textbf{det(A)}\neq0$, we find that the equation system obtains only one solution.

Thus $\textbf{X}=\textbf{Y}$; that is
\begin{equation*}
    \left\{
    \begin{aligned}
        &f_X(0)=f_Y(0)\\
        &f_X(1)=f_Y(1)\\
        &\vdots\\
        &f_X(n)=f_Y(n)\\
    \end{aligned}
    \right.
\end{equation*}

In other words, $f_X(x)=f_Y(x)$ for $x=0,1,\hdots,n$.

\section*{Exercise 1.9}
\enum{
\item
Applying the total probability formula, we obtain
\[
    P[Z=z]=\sum_{y=1}^zP[Z=z|Y=y]P[Y=y].
\]
Since X and Y are independent, then
\[
    P[Z=z|Y=y]=P[X=z-y|Y=y]=P[X=z-y].
\]

Thus
\[
    P[Z=z]=\sum_{y=1}^{z}P[X=z-y]P[Y=y]=\sum_{x+y=z}P[X=x]P[Y=y].
\]

\item
For a geometric random variable, $P_g[X=x]=(1-p)^{x-1}p$.

For a pascal distribution with $r=2$, $$P_p[X=x]=\binom{x-1}{1}p^2(1-p)^{x-2}=(x-1)p^2(1-p)^{x-2}$$.

The sum of two independent geometric random variables is
\spl{
    P[Z=X+Y]&=P[Z=z]\\
    &=\sum_{x+y=z}P_g[X=x]P_g[Y=y]\\
    &=\sum_{x+y=z}(1-p)^{x-1}p(1-p)^{y-1}p\\
    &=\sum_{x+y=z}(1-p)^{x+y-2}p^2\\
    &=\sum_{x+y=z}(1-p)^{z-2}p^2
}
Since $x=1,\hdots,z-1$ and $y=1,\hdots,z-1$, then there are $z-1$ terms of $(1-p)^{z-2}p^2$.
\[
    P[Z=z]=(z-1)(1-p)^{z-2}p^2=P_p[X=x].
\]

Hence, the sum of two independent geometric random variables follows a Pascal distribution with $r = 2$.
}


% !TeX root = ./corona_contact_tracing.tex
% chktex-file 46
% !TeX spellcheck = en-GB
% !TeX encoding = utf8

\subsection{SIR Model}
Our basic stochastic SIR model relies on the assumptions that every person in an environment can be modeled as a point value, which has a location (i.e.\ GPS coordinates) and an infection state. These states can be either \textit{susceptible} (S), \textit{infected} (I), \textit{recovered} (R) or in advanced models also \textit{under quarantine} (Q) or \textit{dead} (D). All individuals, here called agents, have a probability (here called diffusion rate $d$ to make a step per time step on a predefined grid. In the case, that some agents meet at the same location, disease spreading can occur. An infected agent spreads the disease with probability $\beta$ to all the agents in its close vicinity (same location on the grid). Furthermore recovery is covered by taking a recovery rate into account, i.e.\ a probability $\gamma$ to recover from the disease per time step. If an infected agent recovers from the disease, the state of the agent changes from \textit{infected} to \textit{recovered}, which is definite (no double infections). The process ends, when no infected agents are left.
\begin{center}
    Susceptibles $\overset{\beta}{\longrightarrow}$ Infected $\overset{\gamma}{\longrightarrow}$ Recovered.
\end{center}

An example of a early model state is shown in Figure~\ref{fig:1}
\begin{figure}[H]
    \centering
    \includegraphics[width=0.4\linewidth]{initial_setup.png}
    \caption{Early state of SIR model, blue dots = susceptibles, red dots = infected agents, green dots = Recovered agents}%
    \label{fig:1}
\end{figure}

In this case, 1000 agents were initialized on a 100 by 100 grid, where a certain amount of infected agents were introduced as a seed. Letting the agents perform random walks on the grid (maximally one step each time step with probability $d$), and letting the model converge, the results in figure~\ref{fig:2} can be observed.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.9\linewidth]{1_1000_agents}
    \caption{Plot of the proportions of susceptible (blue), infected (red) and recovered (green) individuals in each state over time.}%
    \label{fig:2}
\end{figure}

With the above stated parameters ($d=0.8$, $\beta=0.6$, $\gamma=0.01$), the disease does not spread over the whole population. However over 80\% were infected over time, which could be a very likely scenario of the corona out brake.


\section{Overview}\label{sec:abstractrefinements:overview}

We start with a high level overview of abstract refinements,
by illustrating how they can be used to uniformly specify and
automatically verify various kinds of invariants.

\subsection{Parametric Invariants}\label{sec:overview:parametric}

\mypara{Parametric Invariants via Type Polymorphism}
Suppose we had a generic comparison @(<=) :: a -> a -> Bool@ as in
\ocaml.
We could use it to write:
\begin{code}
  max     :: a -> a -> a
  max x y = if x <= y then y else x

  maximum :: [a] -> a
  maximum (x:xs) = foldr max x xs
\end{code}
In essence, the type given for @maximum@ states that
\emph{for any} @a@, if a list of @a@ values is passed
into @maximum@, then the returned result is also an @a@
value.
%
Hence, for example, if a list of \emph{prime} numbers
is passed in, the result is prime, and if a list of
\emph{even} numbers is passed in, the result is even.
Thus, we can use refinement types \cite{LiquidPLDI08}
to verify
%
\begin{code}
  type Even = {v:Int | v % 2 = 0 }

  maxEvens :: [Int] -> Even
  maxEvens xs = maximum (0 : xs')
    where xs' = [ x | x <- xs, x `mod` 2 == 0]
\end{code}
%
Here the @%@ represents the modulus operator in the refinement logic
\cite{z3} and we type the primitive {@mod :: x:Int -> y:Int -> {v: Int | v = x % y}@}.
Verification proceeds as follows.
Given that {@xs :: [Int]@}, the system has to verify that
{@maximum (0 : xs') :: Even@}.
%
To this end, the type parameter of @maximum@ is instantiated with the
\emph{refined} type @Even@, yielding the instance:
%
\begin{code}
  maximum :: [Even] -> Even
\end{code}
%
Then, @maximum@'s argument should be proved to have type
{@[Even]@}.
So, the type parameter of @(:)@
is instantiated with {@Even@}, yielding the instance:
%
\begin{code}
  (:) :: Even -> [Even] -> [Even]
\end{code}
%
Finally, the system infers that {@0 :: Even@} and {@xs' :: [Even]@},
\ie the arguments of {@(:)@} have the expected types, thereby verifying
the program.
%
The refinement type instantiations can be inferred,
from an appropriate set of logical qualifiers,
using the abstract interpretation framework of Liquid
Types~\cite{LiquidPLDI08}.
Here, once
@ v%2 = 0 @
is added to the set of qualifiers, either manually or (as done by
our implementation) by automatically scraping predicates from
refinements appearing in specification signatures, the
refinement type instantiations and hence verification, proceed
automatically.
%
Thus, parametric polymorphism offers an easy means of encoding
second-order invariants, \ie of quantifying over or parametrizing
the invariants of inputs and outputs of functions.

\mypara{Parametric Invariants via Abstract Refinements}
Instead, suppose that the comparison operator was monomorphic and only
worked for @Int@ values. The resulting (monomorphic) signatures
\begin{code}
  max     :: Int -> Int -> Int
  maximum :: [Int] -> Int
\end{code}
preclude the verification of @maxEvens@ (\ie typechecking against the
signature shown earlier). This is because the new type of @maximum@
merely states that \emph{some} @Int@  is returned as output and not
necessarily one that enjoys the properties of the values in the input
list. This is a shame, since the property clearly still holds.
We could type
%@max :: forall t <: Int. t -> t -> t@
\begin{code}
  max :: forall t <: Int. t -> t -> t
\end{code}
but this route would introduce the complications
that surround bounded quantification which could render checking
undecidable~\cite{piercebook}.

To solve this problem, we introduce \emph{abstract refinements}
which let us
quantify or parameterize a type over its constituent refinements.
For example, we can type @max@ as
\begin{code}
  max :: forall
 Bool>. Int
 -> Int
 -> Int

\end{code}
where @Int
@ is an abbreviation for the refinement type {@{v:Int | p v}@}.
Intuitively, an abstract refinement @p@ is encoded in the refinement logic
as an \emph{uninterpreted function symbol}, which satisfies the
\emph{congruence} axiom~\cite{Nelson81}
%
$$\forall \overline{X}, \overline{Y}: (\overline{X} = \overline{Y})
\Rightarrow P(\overline{X}) = P(\overline{Y})$$
%
Thus, it is trivial to verify, with an SMT solver, that @max@
enjoys the above type: the input types ensure that both @p x@ and @p y@
hold and hence the returned value in either branch satisfies
the refinement  @{v:Int | p v}@, thereby ensuring the output
type. By the same reasoning, we can generalize the type of @maximum@
to
\begin{code}
  maximum :: forall
 Bool>. [Int
] -> Int

\end{code}
Consequently, we can recover the verification of @maxEvens@.
Now, instead of instantiating a \emph{type} parameter, we simply instantiate
the \emph{refinement} parameter of @maximum@ with the concrete
refinement
@{\v -> v % 2 = 0}@,
after which type checking proceeds as usual \cite{LiquidPLDI08}.
%
Later, we show how to retain automatic verification by inferring
refinement parameter instantiations via liquid typing
(\S~\ref{sec:abstractrefinements:infer}).

\mypara{Parametric Invariants and Type Classes}
The example above regularly arises in practice, due to type classes.
In Haskell, the functions above are typed
%
\begin{code}
  (<=)    :: (Ord a) => a -> a -> Bool
  max     :: (Ord a) => a -> a -> a
  maximum :: (Ord a) => [a] -> a
\end{code}
%
We might be tempted to ignore the typeclass constraint
and treat \ttcode{maximum} as @[a] -> a@.
This would be quite unsound, as typeclass predicates preclude
universal quantification over refinement types.
Consider the function @sum :: (Num a) => [a] -> a@ which adds the elements
of a list.
%Clearly,
The @Num@ class constraint implies that numeric operations occur
in the function, so
if we pass @sum@ a list of odd numbers,
we are \emph{not} guaranteed to get back an odd number.

%Thus, given that we cannot instantiate class-predicated type
%parameters with arbitrary refinement types,

Thus, how do we soundly verify the desired type of @maxEvens@
without instantiating class predicated type parameters with
arbitrary refinement types? First, via the same analysis as
the monomorphic @Int@ case, we establish that
%
\begin{code}
  max    :: forall
 Bool>. (Ord a) => a
 -> a
 -> a

  maximum:: forall
 Bool>. (Ord a) => [a
] -> a

\end{code}
%
Next, at the call-site for @maximum@ in @maxEvens@ we
instantiate the type variable @a@ with @Int@ and
the abstract refinement @p@ with @{\v -> v % 2 = 0}@
after which, the verification proceeds as described
earlier (for the @Int@ case).
Thus, abstract refinements allow us to quantify over
invariants without relying on parametric polymorphism,
even in the presence of type classes.

\subsection{Index-Dependent Invariants}\label{sec:overview:index}

Next, we illustrate how abstract invariants allow us to
specify and verify index-dependent invariants of key-value maps.
To this end, we develop a small library of \emph{extensible vectors}
encoded, for purposes of illustration, as functions from @Int@ to
some generic range @a@. Formally, we specify vectors as
%
\begin{code}
  data Vec a <dom :: Int -> Bool, rng :: Int -> a -> Bool>
    = V (i:Int<dom> -> a <rng i>)
\end{code}
%
Here, we are parameterizing the definition of the type @Vec@
with \emph{two} abstract refinements, @dom@ and @rng@, which
respectively describe the \emph{domain} and \emph{range} of the vector.
That is, @dom@ describes the set of \emph{valid} indices
and @r@ specifies an invariant relating each @Int@ index
with the value stored at that index.

\mypara{Creating Vectors}
We can use the following basic functions to create vectors:
%
\begin{code}
  empty :: forall
 a -> Bool>.Vec<{\_ -> False}, p> a
  empty = V (\_ -> error "Empty Vec")

  create :: x:a -> Vec <{\_ -> True}, {\_ v -> v = x}> a
  create x = V (\_ -> x)
\end{code}
%
The signature for @empty@ states that its domain is empty (\ie is
the set of indices satisfying the predicate @False@) and that the
range satisfies @any@ invariant. The signature for @create@,
instead, defines a \emph{constant} vector that maps every index to the
constant @x@.

\mypara{Accessing Vectors}
We can write the following @get@ function for reading the contents
of a vector at a given index:
%
\begin{code}
  get :: forall <d :: Int -> Bool, r :: Int -> a -> Bool>
         i:Int<d> -> Vec<d, r> a -> a<r i>
  get i (V f) = f i
\end{code}
%
The signature states that for any domain @d@ and range @r@,
if the index @i@ is a valid index, \ie is of type, \verb+Int<d>+
then the returned value is an \verb+a+ that additionally satisfies the
range refinement at the index @i@.
%
The type for @set@, which \emph{updates} the vector at
a given index, is even more interesting, as it allows us to
\emph{extend} the domain of the vector:
%
\begin{code}
  set :: forall <d :: Int -> Bool, r :: Int -> a -> Bool>
         i:Int<d>
      -> a<r i>
      -> Vec<d && {\k -> k != i}, r> a
      -> Vec<d, r> a
  set i v (V f) = V (\k -> if k == i then v else f k)
\end{code}
%
The signature for @set@ requires that
(a)~the input vector is defined everywhere at @d@ \emph{except}
the index @i@ and
(b)~the value supplied must be of type @a<r i>@, \ie satisfy the range
relation at the index @i@ at which the vector is being updated.
The signature ensures that the output vector is defined at
@d@ and each value satisfies the index-dependent range refinement @r@.
%
Note that it is legal to call @get@ with a vector that is \emph{also}
defined at the index @i@ since, by contravariance, such a vector is a
subtype of that required by (a).

\mypara{Initializing Vectors} Next, we can write the following function,
@init@, that ``loops" over a vector, to @set@ each index to
a value given by some function.
%
\begin{code}
  initialize :: forall <r :: Int -> a -> Bool>.
                (z: Int -> a<r z>)
             -> i: {v: Int | v >= 0}
             -> n: Int
             -> Vec <{\v -> 0 <= v && v  a
             -> Vec <{\v -> 0 <= v && v < n}, r> a

  initialize f i n a
    | i >= n    = a
    | otherwise = initialize f (i+1) n (set i (f i) a)
\end{code}
%
The signature requires that
(a)~the higher-order function @f@ produces values that satisfy
the range refinement @r@ and
(b)~the vector is initialized from @0@ to @i@.
%
The function ensures that the output vector is initialized from @0@
through @n@.
%
We can thus verify that
%
\begin{code}
  idVec   :: Vec <{\v -> 0<=v && v<n}, {\i v -> v=i}> Int
  idVec n = initialize (\i -> i) 0 n empty
\end{code}
%
\ie @idVec@ returns a vector of size @n@ where each
key is mapped to itself. Thus, abstract refinement types allow us
to verify low-level idioms such as the incremental initialization
of vectors, which have previously required
special analyses~\cite{Gopan05,JhalaMcMillanCAV07,CousotsPOPL11}.

\mypara{Null-Terminated Strings}
%
We can also use abstract refinements to verify code which
manipulates C-style null-terminated strings,
represented as @Char@ vectors for ease of exposition.
Formally, a null-terminated string of size @n@ has the type
%
\begin{code}
  type NullTerm n
    = Vec <{\v -> 0<=v<n}, {\i v -> i=n-1 => v='\0'}> Char
\end{code}
%%Vec <{\v -> 0 <= v && v < n},
%%     {\i v -> i = n - 1 => v = '\0'}>
%%    Char
%
The above type describes a length-@n@ vector of characters whose
last element must be a null character, signalling the end of
the string.
%
We can use this type in the specification of a function,
@upperCase@, which iterates through the characters of a string,
uppercasing each one until it encounters the null terminator:
%
%  Vec <{\v -> 0 <= v && v < n},
%       {\i v -> i = n - 1 => v = '\0'}>
%      Char ->
%  Vec <{\v -> 0 <= v && v < n},
%       {\i v -> i = n - 1 => v = '\0'}>
%      Char
% upperCase n s = ucs 0 s where
%   ucs i s = let c = get i s in
%             if c == '\0'
%               then s
%               else ucs (i + 1) (set i (toUpper c) s)
\begin{code}
  upperCase :: n:{v: Int| v>0} -> NullTerm n -> NullTerm n
  upperCase n s = ucs 0 s
    where
      ucs i s = case get i s of
                  '\0' -> s
                  c    -> ucs (i + 1) (set i (toUpper c) s)
\end{code}
%
Note that the length parameter @n@ is provided solely as a ``witness''
for the length of the string @s@, which allows us to use the length of
@s@ in the type of @upperCase@; @n@ is not used in the
computation.
%
In order to establish that each call to @get@ accesses string @s@
within its bounds, our type system must establish that, at each call
to the inner function @ucs@, @i@ satisfies the type
@{v: Int | 0 <= v && v < n}@.
%
This invariant is established as follows.
%
First, the invariant trivially holds on the first call to @ucs@, as
@n@ is positive and @i@ is $0$.
%
Second, we assume that @i@ satisfies the type
%
@{v: Int | 0 <= v && v < n}@,
%
and, further, we know from the types of @s@ and @get@ that @c@ has the
type @{v: Char | i = n - 1 => v = '\0'}@.
%
Thus, if @c@ is non-null, then @i@ cannot be equal to @n - 1@.
%
This allows us to strengthen our type for @i@ in the else branch to
@{v: Int | 0 <= v && v < n - 1}@ and thus to conclude that the value
@i + 1@ recursively passed as the @i@ parameter to @ucs@ satisfies the
type @{v: Int | 0 <= v && v < n}@, establishing the inductive
invariant and thus the safety of the @upperCase@ function.

\mypara{Memoization}
Next, let us illustrate how the same expressive signatures allow us to
verify memoizing functions. We can specify to the SMT solver the
definition of the Fibonacci function via an uninterpreted function
@fib@ and an axiom:
%
\begin{code}
  measure fib :: Int -> Int
  axiom: forall i. (fib i) = if i <= 1 then 1 else fib (i-1) + fib (i-2)
\end{code}
%axiom_fib :: i:Int -> {v: Bool | (? v) <=> (fib(i) = i <= 1 ? 1 : fib(i-1) + fib(i-2))}
%
Next, we define a type alias @FibV@ for the vector
whose values are either @0@ (\ie undefined) or
equal to the Fibonacci number of the corresponding index.
%(We use @Int@ instead of @Maybe Int@ as the domain for brevity.)
%
\begin{code}
  type FibV = Vec<{\_->True},{\i v-> v != 0 => v = fib i}> Int
\end{code}
%
Finally, we can use the above alias to verify @fastFib@,
an implementation of the Fibonacci function, which uses
a vector memoize intermediate results
%
\begin{code}
  fastFib   :: n:Int -> {v:Int | v = fib(n)}
  fastFib n = snd $ fibMemo (create 0) n

  fibMemo :: FibV -> i:Int -> (FibV, {v: Int | v = fib(i)})
  fibMemo t i
    | i <= 1    = (t, 1)
    | otherwise = case get i t of
                    0 -> let (t1, n1) = fibMemo t  (i-1)
                             (t2, n2) = fibMemo t1 (i-2)
                             n        = n1 + n2
                         in  (set i n t2,  n)
                    n -> (t, n)
\end{code} %$
%
%% axiom_fib :: i:Int -> {v: Bool | (? v) <=> (fib(i) = i <= 1 ? 1 : fib(i-1) + fib(i-2))}
%%
%% fibMemo t i
%%   | i <= 1
%%   = (t, assume (axiom_fib i) $ 1)
%%
%%   | otherwise
%%   = case get i t of
%%       0 -> let (t1, n1) = fibMemo t  (i-1)
%%                (t2, n2) = fibMemo t1 (i-2)
%%                n        = assume (axiom_fib i) $ n1 + n2
%%            in  (set i n t2,  n)
%%       n -> (t, n)
%
Thus, abstract refinements allow us to define key-value maps with
index-dependent refinements for the domain and range.
Quantification over the domain and range refinements allows us
to define generic access operations (\eg @get@, @set@,
@create@, @empty@) whose types enable us establish
a variety of precise invariants.

\subsection{Recursive Invariants}\label{sec:overview:rec}

Next, we turn our attention to recursively defined datatypes and show
how abstract refinements allow us to specify and verify high-level
invariants that relate the elements of a recursive structure.
Consider the following refined definition for lists:
%
\begin{code}
  data [a]
 a -> Bool> where
    []  :: [a]

    (:) :: h:a -> [a
]
 -> [a]

\end{code}
%data List
 a -> Bool> a where
%  Nil  :: List
 a
%  Cons :: h:a -> List
 (a
) -> List
 a
%
%% data List a
 a -> Bool>
%%   = Cons { head :: a
%%          , tail :: List
 (a
) }
%%   | Nil
%% \end{code}
%
The definition states that a value of type @[a]
@
is either empty (@[]@) or constructed from a pair of
a \emph{head} @h::a@ and a \emph{tail} of a list of
@a@ values \emph{each} of which satisfies the refinement @(p h)@.
Furthermore, the abstract refinement @p@ holds recursively
within the tail, ensuring that the relationship @p@
holds between \emph{all} pairs of list elements.

Thus, by plugging in appropriate concrete refinements,
we can define the following aliases, which correspond
to the informal notions implied by their names:
\begin{code}
  type IncrList a = [a]<{\h v -> h <= v}>
  type DecrList a = [a]<{\h v -> h >= v}>
  type UniqList a = [a]<{\h v -> h != v}>
\end{code}
%%
%\begin{code}
%type IncList a    = List <{\h v -> h <= v}> a
%type DecList a    = List <{\h v -> h >= v}> a
%type UniqueList a = List <{\h v -> h != v}> a
%\end{code}
%
That is, @IncrList a@ (resp. @DecrList a@) describes a list sorted
in increasing (resp. decreasing) order and @UniqList a@ describes
a list of \emph{distinct} elements, \ie not containing any duplicates.
We can use the above definitions to verify
%
\begin{code}
  [1, 2, 3, 4] :: IncrList Int
  [4, 3, 2, 1] :: DecrList Int
  [4, 1, 3, 2] :: UniqList Int
\end{code}
%
%
%\begin{code}
%xs :: IncList Int
%xs = 1 `Cons` 2 `Cons` 3 `Cons` 4 `Cons` Nil
%
%ys :: IncList Int
%ys = 4 `Cons` 3 `Cons` 2 `Cons` 1 `Cons` Nil
%
%zs :: UniqueList Int
%zs = 4 `Cons` 1 `Cons` 3 `Cons` 2 `Cons` Nil
%\end{code}
%
More interestingly, we can verify that the usual algorithms
produce sorted lists:
%
\begin{code}
  insertSort :: (Ord a) => [a] -> IncrList a
  insertSort []     = []
  insertSort (x:xs) = insert x (insertSort xs)

  insert :: (Ord a) => a -> IncrList a -> IncrList a
  insert y []       = [y]
  insert y (x:xs)
    | y <= x        = y : x : xs
    | otherwise     = x : insert y xs
\end{code}
%
%insertSort        :: (Ord a) => List a -> IncList a
%insertSort Nil           = Nil
%insertSort (x `Cons` xs) = insert x (insertSort xs)
%
%insert y Nil
%  = y `Cons` Nil
%insert y (Cons x xs)
%  | y <= x    = y `Cons` (x `Cons` xs)
%  | otherwise = x `Cons` (insert y xs)
%
%
Thus, abstract refinements allow us to \emph{decouple} the definition
of the list from the actual invariants that hold.
This, in turn, allows us to conveniently reuse the same
underlying (non-refined) type to implement various algorithms
unlike, say, singleton-type based implementations which require
up to three different types of lists (with three different ``nil" and ``cons"
constructors~\cite{Sheard06}). This, makes abstract refinements
convenient for verifying complex sorting implementations like that of
@Data.List.sort@ which, for efficiency, use lists with different
properties (\eg increasing and decreasing).

\mypara{Multiple Recursive Refinements}
We can define recursive types with multiple parameters.
For example, consider the following refined version of a type used
to encode functional maps (\ttcode{Data.Map}):
%
\begin{code}
  data Tree k v <l :: k -> k -> Bool, r :: k -> k -> Bool>
    = Bin { key   :: k
          , value :: v
          , left  :: Tree <l, r> (k <l key>) v
          , right :: Tree <l, r> (k <r key>) v }
    | Tip
\end{code}
%
The abstract refinements \ttcode{l} and \ttcode{r} relate each \ttcode{key}
of the tree with \emph{all} the keys in the \emph{left} and \emph{right}
subtrees of \ttcode{key}, as those keys are respectively of type
\ttcode{k <l key>} and \ttcode{k <r key>}.
%
Thus, if we instantiate the refinements with the following predicates
\begin{code}
  type BST k v     = Tree<{\x y -> x> y},{\x y-> x< y}> k v
  type MinHeap k v = Tree<{\x y -> x<=y},{\x y-> x<=y}> k v
  type MaxHeap k v = Tree<{\x y -> x>=y},{\x y-> x>=y}> k v
\end{code}
then @BST k v@, @MinHeap k v@ and @MaxHeap k v@
denote exactly binary-search-ordered, min-heap-ordered, and
max-heap-ordered trees (with keys and values of types @k@ and
@v@).
%
We demonstrate in (\S~\ref{sec:experiments}) how we use the above types to
automatically verify ordering properties of complex, full-fledged libraries.

\subsection{Inductive Invariants}\label{sec:overview:induction}

Finally, we explain how abstract refinements allow us to formalize
some kinds of structural induction within the type system.

\mypara{Measures} First, let us formalize a notion of \emph{length} for
lists within the refinement logic. To do so, we define a special
\ttcode{len} measure by structural induction
%
\begin{code}
  measure len :: [a] -> Int
    len []      = 0
    len (x:xs)  = 1 + len(xs)
\end{code}
%
We use the measures to automatically strengthen the
types of the data constructors~\ref{sec:measures}:
%
\begin{code}
  data [a] where
    []  :: {v:[a] | len v = 0}
    (:) :: a -> xs:[a] -> {v:[a]| len v = 1 + len xs}
\end{code}
%
Note that the symbol \ttcode{len} is encoded as an \emph{uninterpreted}
function in the refinement logic, and is, except for the congruence axiom,
opaque to the SMT solver. The measures are guaranteed, by construction,
to terminate and so we can soundly use them as uninterpreted
functions in the refinement logic. Notice also, that we can define
\emph{multiple} measures for a type; in this case we simply conjoin
the refinements from each measure when refining each data constructor.

With these strengthened constructor types, we can verify, for example,
that @append@ produces a list whose length is the sum of the input lists'
lengths:
%
\begin{code}
  append :: l:[a] -> m:[a] -> {v:[a]| len v = len l + len m}
  append []     zs = zs
  append (y:ys) zs = y : append ys zs
\end{code}
%
However, consider an alternate definition of @append@ that uses @foldr@
%
\begin{code}
  append ys zs = foldr (:) zs ys
\end{code}
%
where @foldr :: (a -> b -> b) -> b -> [a] -> b@.
It is unclear how to give @foldr@ a (first-order) refinement type
that captures the rather complex fact that the fold-function
is ``applied" all over the list argument, or, that it is a catamorphism.
Hence, hitherto, it has not been possible to verify the second definition
of @append@.

\mypara{Typing Folds} Abstract refinements allow us to
solve this problem with a very expressive type for \ttcode{foldr}
whilst remaining firmly within the boundaries of SMT-based
decidability. We write a slightly modified fold:
%
\begin{code}
  foldr :: forall
 b -> Bool>.
           (xs:[a] -> x:a -> b
 ->
)
        -> b

        -> ys:[a]
        -> b

  foldr op b []     = b
  foldr op b (x:xs) = op xs x (foldr op b xs)
\end{code}
%
The trick is simply to quantify over the relationship @p@
that @foldr@ establishes between the input list @xs@ and
the output @b@ value. This is formalized by the type signature,
which encodes an induction principle for lists:
the base value @b@ must
(1)~satisfy the relation with the empty list,
and the function @op@ must take
(2)~a value that satisfies the relationship with the tail
    @xs@ (we have added the @xs@ as an extra ``ghost"
    parameter to @op@),
(3)~a head value @x@, and return
(4)~a new folded value that satisfies the relationship with \ttcode{x:xs}.
If all the above are met, then the value returned by @foldr@
satisfies the relation with the input list @ys@.
%
This scheme is not novel in itself~\cite{coq-book}
--- what is new is the encoding, via uninterpreted predicate symbols,
in an SMT-decidable refinement type system.

\mypara{Using Folds} Finally, we can use the expressive type
for the above @foldr@ to verify various inductive properties
of client functions:
%
\begin{code}
  length :: zs:[a] -> {v: Int | v = len zs}
  length = foldr (\_ _ n -> n + 1) 0

  append :: l:[a] -> m:[a] -> {v:[a]| len v = len l + len m}
  append ys zs = foldr (\_ -> (:)) zs ys
\end{code}
%
The verification proceeds by just (automatically) instantiating the
refinement parameter \ttcode{p} of \ttcode{foldr} with the concrete
refinements, via Liquid typing:
%
\begin{code}
  {\xs v -> v = len xs}                 -- for length
  {\xs v -> len v = len xs + len zs}    -- for append
\end{code}

%%This concludes a tour of the many kinds of expressive specifications
%%that abstract refinements enable, while preserving fully automatic
%%verification.


\section{Derivative Rules for Trigonometric Functions}\label{sec:DerivativeRulesTrig}
We next look at the derivative of the sine function.
In order to prove the derivative formula for sine, we recall two limit computations from earlier:
$$\lim_{x\to 0}\frac{\sin x}{x}=1\qquad\mbox{~and~}\qquad\lim_{x\to 0}\frac{\cos x -1}{x}=0,$$
and the double angle formula
$$\sin\left(A+B\right)=\sin A\cos B+\sin B\cos A.$$

\begin{theorem}{Derivative of Sine Function}{Derivative of Sine Function}
$\ds{\left(\sin x\right)'=\cos x}$
\end{theorem}

\begin{proof}
Let $f(x)=\sin x$.
Using the definition of derivative we have:
$$\begin{array}{ccl}
f'(x) & = &  \ds{\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}}\\
\\
& = & \ds{\lim_{h\to 0}\frac{\sin(x+h)-\sin x}{h}}\\
\\
& = & \ds{\lim_{h\to 0}\frac{\sin x\cos h+\cos x\sin h-\sin x}{h}}\\
\\
& = & \ds{\lim_{h\to 0}\sin x\cdot\lim_{h\to 0}\frac{\cos h-1}{h}+\lim_{h\to 0}\cos x\cdot\lim_{h\to 0}\frac{\sin h}{h}}\\
\\
& = & \ds{\sin x\cdot 0+\cos x\cdot 1}\\
\\
& = & \ds{\cos x}\\
\end{array}$$
since
$$\lim_{x\to 0}\frac{\sin x}{x}=1\mbox{~~and~~}\lim_{x\to 0}\frac{\cos x -1}{x}=0.$$
\end{proof}

A formula for the derivative of the \ifont{cosine function} can be found in a similar fashion:
$$\frac{d}{dx}(\cos x)=-\sin x.$$

Using the quotient rule we get formulas for the remaining trigonometric ratios. To summarize, here are the derivatives of the six trigonometric functions:
$$\begin{array}{|rcl|rcl|rcl|}
\hline
~&~&~&~&~&~&~&~&~\\
\ds{\frac{d}{dx}\left(\sin(x)\right)} &= & \ds{\cos(x)} &
\ds{\frac{d}{dx}\left(\tan(x)\right)}&= & \ds{\sec^2(x)} &
\ds{\frac{d}{dx}\left(\csc(x)\right)} &= & \ds{-\csc(x)\cot(x)} \\
~&~&~&~&~&~&~&~&~\\
\hline
~&~&~&~&~&~&~&~&~\\
\ds{\frac{d}{dx}\left(\cos(x)\right)} &= & \ds{-\sin(x)} &
\ds{\frac{d}{dx}\left(\cot(x)\right)} &= & \ds{-\csc^2(x)} &
\ds{\frac{d}{dx}\left(\sec(x)\right)} &= & \ds{\sec(x)\tan(x)} \\
~&~&~&~&~&~&~&~&~\\
\hline
\end{array}$$

\begin{example}{Derivative of Product of Trigonometric Functions}{Derivative of Product of Trigonometric Functions}
Find the derivative of $f(x)=\sin x\tan x$.
\end{example}

\begin{solution}
Using the Product Rule we obtain
$$f'(x)=\cos x\tan x+\sin x\sec^2x.$$
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for Section \ref{sec:DerivativeRulesTrig}}

\begin{enumialphparenastyle}
%%%%%%%%%%
\begin{ex}
Find the derivatives of the following functions.
\begin{multicols}{3}
\begin{enumerate}
	\item	$\ds \sin x\cos x$
	\item	$\ds \cot x$
	\item	$\ds\csc x-x\tan x$
\end{enumerate}
\end{multicols}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the points on the curve $\ds y= x+ 2\cos x$ that have a
horizontal tangent line.
\begin{sol}
$\ds \pi/6+2n\pi$, $5\pi/6+2n\pi$, any integer $n$
\end{sol}
\end{ex}

\end{enumialphparenastyle}


%---------------------------Scaled Jacobian-----------------------------
\section{Scaled Jacobian}

Let $J$ be the Jacobian as defined in \S\ref{s:tet-jacobian}

\[
\lambda_1 = \normvec{ L_0 }
            \normvec{ L_2 }
            \normvec{ L_3 }
\]

\[
\lambda_2 = \normvec{ L_0 }
            \normvec{ L_1 }
            \normvec{ L_4 }
\]

\[
\lambda_3 = \normvec{ L_1 }
            \normvec{ L_2 }
            \normvec{ L_5 }
\]

\[
\lambda_4 = \normvec{ L_3 }
            \normvec{ L_4 }
            \normvec{ L_5 }
\]

\[
\lambda_{\max} = \max\left\{\lambda_1, \lambda_2, \lambda_3, \lambda_4, J\right\}
\]

\begin{equation*}
q = \frac{J\sqrt{2}}{\lambda_{\max}}
\end{equation*}

Note that if $\lambda_{\max} < DBL\_MIN$, we set $q = DBL\_MAX$.

\tetmetrictable{scaled Jacobian}%
{$1$}%                                        Dimension
{$[\frac{1}{2},\frac{\sqrt{2}}{2}]$}%         Acceptable range
{$[-\frac{\sqrt{2}}{2},\frac{\sqrt{2}}{2}]$}% Normal range
{$[-DBL\_MAX,DBL\_MAX]$}%                     Full range
{1}%                                          Equilateral tet
{\cite{knu:00}}%                              Citation
{v\_tet\_scaled\_jacobian}%                            Verdict function name


\lab{Solitons}{Solitons}
\label{lab:solitons}

\objective{
We study traveling wave solutions of the Korteweg-de Vries (KdV) equation, using a pseudospectral discretization in space and a Runge-Kutta integration scheme in time.  }

Here we consider soliton solutions of the Korteweg-de Vries (KdV) equation. This equation is given by
\[  \frac{\partial u }{\partial t} + u \frac{\partial u}{\partial x} + \frac{\partial^3 u}{\partial x^3} = 0.
\]
The KdV equation is a canonical equation that describes shallow water waves.

The KdV equation possesses traveling wave solutions called solitons.
These traveling waves have the form
\[ u(x,t) = 3s \sech^2\left(\frac{\sqrt{s}}{2}(x - st - a)\right),
\]
where $s$ is the speed of the wave.
Solitons were first studied by John Scott Russell in 1834, in the Union Canal in Scotland.
When a canal boat suddenly stopped, the water piled up in front of the boat continued moving down the canal in the shape of a pulse.

Note that there is a soliton solution for each wave speed $s$, and that the amplitude of the soliton depends on the speed of the wave.
Solitons are traveling waves in the shape of a pulse, they are nonlinearly stable (bumped waves return to their previous shape), and they maintain their energy as they travel.
They also enjoy an additional stability property: They play well with others.
Two interacting solitons will maintain their shape after crossing paths.

\section*{Numerical solution}
Consider the KdV equation on $[-\pi,\pi]$, together with an appropriate initial condition:
\begin{align*}
	 &{ }u_t = -\left(\frac{u^2}{2} \right)_x - u_{xxx},\\
     &{ }u(x,0) = u_0(x).
\end{align*}
We will use initial data that is zero at the endpoints. This will allow us to use a pseudospectral method for periodic initial data to find a numerical approximation for the solution $u(x,t)$.

If we use $N$ subintervals in space, we then obtain the spatial step $h = 2\pi/N$ and the grid points $\{x_j\}_{j=1} = \{-\pi,-\pi + h,\ldots,\pi-h\}$.
Let $\mathcal{F}(u)(t) = \hat{u}(t)$ denote the Fourier transform of $u(x,t)$ (in space), so that
\[
\mathcal{F}(u) = \hat{u}(k,t), \quad k=-N/2+1, \ldots, N/2.
\]
Similarly we let $\mathcal{F}^{-1}$ represent the discrete inverse Fourier transform.
Recall that $k$ represents the wave numbers in Fourier space; our code defines it by
\begin{lstlisting}
# Dependencies for this lab's code:
from __future__ import division
from math import sqrt, pi
import numpy as np
from scipy.fftpack import fft, ifft
from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm

# Array of wave numbers.  This array is reordered in Python to
# accomodate the ordering inside the fft function in scipy.
k = np.concatenate(( np.arange(0,N/2) ,
					 np.array([0])	,
					 np.arange(-N/2+1,0,1)	)).reshape(N,)
\end{lstlisting}

We now apply the Fourier transform to the KdV equation. In Fourier space, we obtain
\begin{align*}
	\mathcal{F}(u)_t &= -\frac{ik}{2}\mathcal{F}(u^2)- (ik)^3\mathcal{F}(u).
\end{align*}
Let $U(t)$ be the vector valued function given by $U(t) = (u(x_j,t))_{j=1}^N$.
Let $\mathcal{F}(U)(t)$ denote the discrete Fourier transform of $u(x,t)$ (in space), so that
\[
\mathcal{F}(U)(t) = (\mathcal{F}(u)(k,t))_{k=-N/2+1}^{N/2}.
\]
Similarly we let $\mathcal{F}^{-1}$ represent the discrete inverse Fourier transform.
Using the pseudospectral approximation in space leads to the system of ODEs
\begin{align}
	\mathcal{F}(U)_t =  -\frac{i}{2} \vec{k}\mathcal{F}\left( \mathcal{F}^{-1}(\mathcal{F}(U))^2\right) + i\vec{k}^3\mathcal{F}(U)
\end{align}
where $\vec{k}$ is a vector, and multiplication is done element-wise. In terms of $Y = \mathcal{F}(U)$, this simplifies to
\begin{align}
	Y_t =  -\frac{i}{2} \vec{k}\mathcal{F}\left( \mathcal{F}^{-1}(Y)^2\right) + i\vec{k}^3Y
	\label{lab:solitons:pseudospectral}
\end{align}
and is implemented below.
\begin{lstlisting}
# Defines the left hand side of the ODE y' = G(t,y)
# defined above.
ik3 = 1j*k**3.
def G_unscaled(t,y):
	out = -.5*1j*k*fft(ifft(y,axis=0)**2.,axis=0)  + ik3*y
	return out
\end{lstlisting}

Equation \eqref{lab:solitons:pseudospectral} is solved below, using a soliton as initial data for the KdV equation.
Note that the Fourier transform must be applied to the soliton before solving, and that the final numerical solution must be transformed back from Fourier space before plotting.
\begin{lstlisting}
N = 256
x = (2.*np.pi/N)*np.arange(-N/2,N/2).reshape(N,1)   # Space discretization
s, shift = 25.**2., 2.  							# Initial data is a soliton
y0 = (3.*s*np.cosh(.5*(sqrt(s)*(x+shift)))**(-2.)).reshape(N,)

# Solves the ODE.
max_t =
dt = # constant*N**(-2.)
max_tsteps = int(round(max_t/dt))
y0 = fft(y0,axis=0)
T,Y = RK4(G_unscaled, y0, t0=0, t1=max_t, n=max_tsteps)

# Using the variable stride, we step through the data,
# applying the inverse fourier transform to obtain u.
# These values will be plotted.
stride = int(np.floor((max_t/25.)/dt))
uvalues, tvalues = np.real(ifft(y0,axis=0)).reshape(N,1), np.array(0.).reshape(1,1)
for n in range(1,max_tsteps+1):
	if np.mod(n,stride) == 0:
		t = n*dt
		u = np.real( ifft(Y[n], axis=0) ).reshape(N,1)
		uvalues = np.concatenate((uvalues,np.nan_to_num(u)),axis=1)
		tvalues = np.concatenate((tvalues,np.array(t).reshape(1,1)),axis=1)

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.view_init(elev=45., azim=150)
tv, xv = np.meshgrid(tvalues,x,indexing='ij')
surf = ax.plot_surface(tv,xv, uvalues.T, rstride=1, cstride=1, cmap=cm.coolwarm,
						linewidth=0, antialiased=False)
tvalues = tvalues[0]; ax.set_xlim(tvalues[0], tvalues[-1])
ax.set_ylim(-pi, pi); ax.invert_yaxis()
ax.set_zlim(0., 4000.)
ax.set_xlabel('T'); ax.set_ylabel('X'); ax.set_zlabel('Z')
plt.show()
\end{lstlisting}

The method we have used requires the use of an algorithm for (ODE) initial value problems, such as the RK4 algorithm. The RK4 method is implemented below.
\begin{lstlisting}
def initialize_all(y0, t0, t1, n):
	""" An initialization routine for the different ODE solving
	methods in the lab. This initializes Y, T, and h. """

	if isinstance(y0, np.ndarray):
		Y = np.empty((n, y0.size),dtype=complex).squeeze()
	else:
		Y = np.empty(n,dtype=complex)
	Y[0] = y0
	T = np.linspace(t0, t1, n)
	h = float(t1 - t0) / (n - 1)
	return Y, T, h

def RK4(f, y0, t0, t1, n):
	""" Use the RK4 method to compute an approximate solution
	to the ODE y' = f(t, y) at n equispaced parameter values from t0 to t
	with initial conditions y(t0) = y0.

	'y0' is assumed to be either a constant or a one-dimensional numpy array.
	't0' and 't1' are assumed to be constants.
	'f' is assumed to accept two arguments.
	The first is a constant giving the current value of t.
	The second is a one-dimensional numpy array of the same size as y.

	This function returns an array Y of shape (n,) if
	y is a constant or an array of size 1.
	It returns an array of shape (n, y.size) otherwise.
	In either case, Y[i] is the approximate value of y at
	the i'th value of np.linspace(t0, t, n).
	"""
	Y, T, h = initialize_all(y0, t0, t1, n)
	for i in xrange(1, n):
		K1 = f(T[i-1], Y[i-1])
		tplus = (T[i] + T[i-1]) * .5
		K2 = f(tplus, Y[i-1] + .5 * h * K1)
		K3 = f(tplus, Y[i-1] + .5 * h * K2)
		K4 = f(T[i], Y[i-1] + h * K3)
		Y[i] = Y[i-1] + (h / 6.) * (K1 + 2 * K2 + 2 * K3 + K4)
	return T, Y
\end{lstlisting}

%
% \begin{lstlisting}
%
% \end{lstlisting}
% \begin{lstlisting}
%
% \end{lstlisting}
% \begin{lstlisting}
%
% \end{lstlisting}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{soliton.png}
\caption{The solution to Problem \ref{problem:solitons:single}.}
\label{fig:solitons:single}
\end{figure}

\begin{problem}
Run the code above to numerically solve the KdV equation on $[-\pi,\pi]$ with initial conditions
\[
u(x,t=0) = 3s\sech^2\left(\frac{\sqrt{s}}{2}(x+a)\right),
\]
where $s = 25^2,$ $a = 2$. Solve on the time domain $[0,.0075]$. Define the stepsize variable {\tt dt} in the code above so that the method is numerically stable.  How small must {\tt dt} be?

The solution is shown in Figure \ref{fig:solitons:single}.
\label{problem:solitons:single}
\end{problem}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{interacting_solitons.png}
\caption{The solution to Problem \ref{problem:solitons:interacting}.}
\label{fig:solitons:interacting}
\end{figure}

\begin{problem}
Numerically solve the KdV equation on $[-\pi,\pi]$. This time we define the initial condition
to be the superposition of two solitons,
\[
u(x,t=0) = 3s_1\sech^2\left(\frac{\sqrt{s_1}}{2}(x+a_1)\right) + 3s_2\sech^2\left(\frac{\sqrt{s_2}}{2}(x+a_2)\right),
\]
where $s_1 = 25^2,$ $a_1 = 2$, and $s_2 = 16^2,$ $a_1 = 1$.\footnote{This problem is solved in \textit{Spectral Methods in MATLAB}, by Trefethen.} Solve on the time domain $[0,.0075]$.  How small must {\tt dt} be so that the method is numerically stable?  The solution is shown in Figure \ref{fig:solitons:interacting}.
\label{problem:solitons:interacting}
\end{problem}

\begin{problem}
	Consider again equation \eqref{lab:solitons:pseudospectral}. The linear term in this equation is
	$i\vec{k}^3Y$. This term contributes much of the exponential growth in the ODE, and responsible for
	how short the time step must be to ensure numerical stability. Make the substitution $Z = e^{-ik^3t}Y$ and find a similar ODE for $Z$. This essentially allows the exponential growth to be scaled out (it's solved for analytically). Use the resulting equation to solve the previous problem. How short can the time step be made?
\end{problem}

%
% The discrete Fourier transform (DFT) of $f$, denoted by $\hat{f}$ or $\mathcal{F}(f)$, is given by
% \[
% \hat{f}(k) = h \sum_{j=1}^N e^{-ikx_j}f(x_j) \quad \text{ where } k = -N/2+1, \ldots,0,1,\ldots, N/2.
% \]
% The inverse DFT is then given by
% \begin{align}
% \begin{split}
% f(x_j) &= \frac{1}{2\pi}\sum_{k=-N/2}^{N/2}\frac{e^{ikx_j}}{c_k}\hat{f}(k), \quad j = 1,\ldots, N,
% \end{split}\label{inverse_dft}
% \end{align}
% where
% \begin{align}
% 	c_k = \begin{cases} 2 & \text{if }k = -N/2 \text{ or }k = N/2, \\ 1 &  \text{otherwise.}
% \end{cases}
% \end{align}
% The inverse DFT can then be used to define a natural interpolant (sometimes called a band-limited interpolant) by evaluating (\ref{inverse_dft}) at any $x$ rather than $x_j$:
% \begin{align}
% p(x) = \frac{1}{2\pi}\sum_{k=-N/2}^{N/2} e^{ikx}\hat{f}(k). \label{interpolant}
% \end{align}
% The interpolant for $f'$ is then given by
% \begin{align}
% p'(x) = ik \frac{1}{2\pi}\sum_{k=-N/2+1}^{N/2-1} e^{ikx}\hat{f}(k). \label{spectral2:deriv}
% \end{align}

% Consider the function $u(x) = \sin^2 (x) \cos(x) +e^{2\sin(x+1)}$.
% Using \eqref{spectral2:deriv}, the derivative $u'$ may be approximated with the following code.  \footnote{See \textit{Spectral Methods in MATLAB} by Lloyd N. Trefethen.  Another good reference is \textit{Chebyshev and Fourier Spectral Methods} by John P. Boyd.}
% We note that although we only approximate $u'$ at the Fourier grid points, \eqref{spectral2:deriv} provides an analytic approximation of $u'$ in the form of a trigonometric polynomial.

% \begin{lstlisting}
% import numpy as np
% from scipy.fftpack import fft, ifft
% import matplotlib.pyplot as plt
%
% N=24
% x1 = (2.*np.pi/N)*np.arange(1,N+1)
% f = np.sin(x1)**2.*np.cos(x1) + np.exp(2.*np.sin(x1+1))
%
%
% k = np.concatenate(( np.arange(0,N/2) ,
% 					 np.array([0])	, # Because hat{f}'(k) at k = N/2 is zero.
% 					 np.arange(-N/2+1,0,1)	))
%
% # Approximates the derivative using the pseudospectral method
% f_hat = fft(f)
% fp_hat = ((1j*k)*f_hat)
% fp = np.real(ifft(fp_hat))
%
% # Calculates the derivative analytically
% x2 = np.linspace(0,2*np.pi,200)
% derivative = (2.*np.sin(x2)*np.cos(x2)**2. -
% 				np.sin(x2)**3. +
% 				2*np.cos(x2+1)*np.exp(2*np.sin(x2+1))
% 				)
%
% plt.plot(x2,derivative,'-k',linewidth=2.)
% plt.plot(x1,fp,'*b')
% plt.savefig('spectral2_derivative.pdf')
% plt.show()
%
% \end{lstlisting}

% \begin{figure}
% \centering
% \includegraphics[width=\textwidth]{spectral2_derivative.pdf}
% \caption{The derivative of $u(x) = \sin^2 (x) \cos(x) +e^{2\sin(x+1)}$.}
% \label{fig:spectral:spectral2_derivative}
% \end{figure}
%
% \begin{problem}
% Consider again the function $u(x) = \sin^2 (x) \cos(x) +e^{2\sin(x+1)}$.
% Create a function that approximates $\frac{1}{2}u''-u'$ on the Fourier grid points for a given $N$.
% \end{problem}

% \begin{lstlisting}
% import numpy as np
% import matplotlib.pyplot as plt
%
% # Solve the ODE $u_{xx} = e^u,$ with boundary conditions
% #  $u(0) = u(2\pi) = 0$
%
%
% \end{lstlisting}

% \section*{The advection equation}
% Recall that the advection equation is given by
% \begin{align}
% &{ }u_t + cu_x = 0
% \end{align}
% where $c$ is the speed of the wave (the wave travels to the right for $c > 0$).
% We will consider the solution of the advection equation on the circle; this essentially amounts to solving the advection equation on $[0,2\pi]$ and assuming periodic boundary conditions.

% A common method for solving time-dependent PDEs is called the \textit{method of lines}. To apply the method of lines to our problem, we use our Fourier grid points in $[0,\pi]$: given an even $N$, let $h = 2\pi/N$, so that $\{x_1,\ldots,x_N\} = \{h,2h,\ldots,2\pi-h,2\pi\}$.  By using these grid points we obtain the collection of equations
% \begin{align}
% &{ }u_t(x_j,t) + cu_x(x_j,t) = 0, \quad t >0, \quad j = 1, \ldots N. \label{spectral2:method_oflines}
% \end{align}

% traveling wave solutions of a partial differential equation are solutions of the form $u(x,t) = u(x-st),$ where $s$ is the speed of the traveling wave.
% Thus a traveling wave solution is a solution that is a function of one variable, $\xi= x-st$.
% This new frame of reference corresponds to an observer moving along with the wave, so that the wave appears stationary as the observer studies it.

% \subsection*{Burgers' equation}
% We will examine the process of studying traveling wave solutions using Burgers' equation, a nonlinear PDE from gas dynamics.
% It is given by
% \begin{align}
% 	u_t + \left( \frac{u^2}{2} \right)_x = \nu u_{xx}, \label{eqn:Burgers_pde}
% \end{align}
% where $u$ and $\nu$ represent the velocity and viscosity of the gas, respectively.
% It models both the process of transport with the nonlinear advection term $(u^2/2)_x = u u_x$, as well as diffusion due to the viscosity of the gas ($\nu u_{xx}$).
%
% Let us look for a traveling wave solution $u(x,t) = \hat{u}(x-st)$ for Burgers equation.
% We transform \eqref{eqn:Burgers_pde} into the moving frame $(x,t) \to (\bar{x},\bar{t}) = (x-st, t)$. In this frame \eqref{eqn:Burgers_pde} becomes
% \begin{align}
% 	u_{\bar{t}} - s u_{\bar{x}}+ \left(\frac{u^2}{2} \right)_{\bar{x}} = \nu u_{\bar{x}\bar{x}}
% 	\label{eqn:Burgers_pde_moving_frame}
% \end{align}
% % The coordinate system $(\bar{x},\bar{t})$ is called the moving frame because the traveling wave is stationary in this coordinate system.
% This new frame of reference corresponds to an observer moving along with the wave, so that the wave appears stationary as the observer studies it.
% Thus, $\hat{u}_{\bar{t}} = 0$, so that the wave profile $\hat{u}$ satisfies the ordinary differential equation
% \begin{align}
% 	 -s u_{\bar{x}}+ \left(\frac{u^2}{2} \right)_{\bar{x}} = \nu u_{\bar{x}\bar{x}}.
% 	\label{eqn:Burgers_ode}
% \end{align}
%
% From here on we will drop the bar notation for simplicity.
% We seek a traveling wave solution with asymptotically constant boundary conditions; that is,  $\lim_{x \to \pm \infty}\hat{u}(x) = u_{\pm}$
%
% both exist, and  $\lim_{x \to \pm \infty} \hat{u}'(x) = 0$.
% We will suppose that $u_- > u_+ > 0$.
%
% % A traveling wave solution $u(\xi)$ of Burgers' equation will satisfy the ordinary differential equation
% % \[ -s u' + u u' = \nu u''.\]
%
% Note that to this point we still don't know the speed of the traveling wave.
% Integrating both sides of this differential equation, and then taking the limit as $x \to +\infty$, we obtain
% \begin{align*}
% -s\int_{-\infty}^x u' + \int_{-\infty}^x \left(\frac{u^2}{2}\right)' &= \nu \int_{-\infty}^x u'',\\
% -s(u(x) - u_-) + \frac{u^2(x)}{2} - \frac{u_-^2}{2} &= \nu (u'(x) - u'(-\infty)), \\
% -s(u_+ - u_-) + \frac{u_+^2}{2} - \frac{u_-^2}{2} &= 0.
% \end{align*}
% Thus given boundary conditions $u_{\pm}$ at $\pm \infty$, the speed of the traveling wave must be $s = \frac{u_- + u_+}{2}$.
%
% % Usually at this point, the traveling wave must be numerically solved using the profile ODE (\eqref{eqn:Burgers_ode} for Burgers equation).
% However, the profile ODE for Burgers is simple enough that it is possible to obtain an analytic solution.
% The traveling wave is  given by
% % If we continue by solving the first order equation
% % \[-\frac{u_- + u_+}{2}(u(x) - u_-) + \frac{u^2(x)}{2} - \frac{u_-^2}{2} = \nu u'(x)\]
% % we obtain a one-parameter family of solutions
% \[\hat{u}(x) = s - a \tanh \left(\frac{ax }{2\nu} + \delta\right)\]
% where $a = (u_- - u_+)/2$ and $\delta$ is fixed real number.
% We get a family of solutions because any translation of a traveling wave solution is also a traveling wave solution.
%
% \subsection*{Stability of traveling waves}
% Suppose that an evolutionary PDE
% \begin{align}
% u_t = G(u,u_x, u_{xx}, \ldots).
% \label{eqn:evol_pde_repeat}
% \end{align}
% has a traveling wave solution $u(x,t) = \hat{u}(x-st)$.
% An interesting question to consider is whether the mathematical solution, $\hat{u}$, has a physical analogue.
% In other words, does the traveling wave show up in real life?
% This question is the start of the mathematical study of stability of traveling waves.
%
% We begin by translating \eqref{eqn:evol_pde_repeat} into the moving frame $(x,t) \to (\bar{x},\bar{t}) = (x-st, t)$.
% In this frame the PDE becomes
% \begin{align*}
% u_t - su_x = G(u,u_x, u_{xx}, \ldots).
% \end{align*}
% In these coordinates the traveling wave is stationary.
% Thus, the solution of
% \begin{align*}
% \begin{split}
% u_t - su_x &= G(u,u_x, u_{xx}, \ldots), \\
% u(x,t = 0) &= \hat{u}(x),
% \end{split}
% \end{align*}
% is given by $u(x,t) = \hat{u}(x)$.
% We say that the traveling wave $\hat{u}$ is asymptotically orbitally stable if whenever $v(x)$ is a small perturbation of $\hat{u}(x)$, the general solution of
% \begin{align*}
% \begin{split}
% u_t - su_x &= G(u,u_x, u_{xx}, \ldots), \\
% u(x,t = 0) &= v(x),
% \end{split}
% \end{align*}
% converges to some translation of $\hat{u}$ as $t \to \infty$.
% Using this definition to prove stability of a traveling wave is a nontrivial task.
%
% \subsection*{Visualizing stability of the traveling wave solution of Burgers' equation}
% The traveling wave solution of Burgers' equation is a stable wave.
% To view this numerically, we discretize the PDE
% \[u_t -su_x + uu_x = u_{xx}\]
% using the second order centered approximations
% % \begin{align*}
% % &{ } u_t(x_j,t_{n+1/2}) \approx \frac{u_j^{n+1}-u_j^n}{\triangle t}, \quad
% % u_x(x_j,t_{n+1/2}) \approx \frac{1}{2} \left( \frac{u_{j+1}^{n+1}-u_{j-1}^{n+1}}{2 \triangle x} +  \frac{u_{j+1}^{n}-u_{j-1}^{n}}{2 \triangle x}\right) ,\\
% % &{ } u_{xx}(x_j,t_{n+1/2}) \approx \frac{1}{2} \left( \frac{u_{j+1}^{n+1}- u_{j}^{n+1}+u_{j-1}^{n+1}}{(\triangle x)^2} + \frac{u_{j+1}^{n}- u_{j}^{n}+u_{j}^{n+1}}{(\triangle x)^2}\right).
% % \end{align*}
% \begin{align*}
% &{ } D_t U_j^{n+1/2} = \frac{U_j^{n+1}-U_j^n}{\triangle t}, \quad
% D_{xx}U_j^{n+1/2} = \frac{1}{2} \left( \frac{U_{j+1}^{n+1}-U_{j-1}^{n+1}}{2 \triangle x} +  \frac{U_{j+1}^{n}-U_{j-1}^{n}}{2 \triangle x}\right),\\
% &{ } D_{xx}U_j^{n+1/2} = \frac{1}{2} \left( \frac{U_{j+1}^{n+1}- U_{j}^{n+1}+U_{j-1}^{n+1}}{(\triangle x)^2} + \frac{U_{j+1}^{n}- U_{j}^{n}+U_{j-1}^{n}}{(\triangle x)^2}\right)
% \end{align*}
%
% Substituting these expressions into the PDE we obtain a second-order, implicit Crank-Nicolson method
% % \begin{align*}
% % &{ }u_j^{n+1} + k_1 (u_j^{n+1}-s)(u_{j+1}^{n+1} - u_{j-1}^{n+1})
% % - k_2(u_{j+1}^{n+1} - 2u_j^{n+1}+ u_{j-1}^{n+1}) = \\
% % &{ } u_j^n - k_1(u_j^n-s) (u_{j+1}^n - u_{j-1}^n)
% % + k_2 (u_{j+1}^n -2u_j^n + u_{j-1}^n),
% % \end{align*}
% \begin{align*}
% U_j^{n+1} - U_j^n &= K_1 \big[(s - U_j^{n+1})(U_{j+1}^{n+1} - U_{j-1}^{n+1})
% + (s - U_j^n) (U_{j+1}^n - U_{j-1}^n) \big] \\
% &{ }  \quad
% + K_2 \big[(U_{j+1}^{n+1} - 2U_j^{n+1}+ U_{j-1}^{n+1}) + (U_{j+1}^n -2U_j^n + U_{j-1}^n) \big],\\
% &{ }  \quad
% \end{align*}
% where $K_1 = \frac{ \triangle t }{4 \triangle x}$ and $K_2 = \frac{ \triangle t}{2(\triangle x)^2}$.
%
% \begin{problem}
% Numerically solve the initial value problem
% \begin{align*}
% 	&{ } u_t -su_x + uu_x = u_{xx}, \quad x \in (-\infty,\infty),\\
% 	&{ } u(x,0) = v(x),
% \end{align*}
% for $t \in [0,1]$.
% Let the perturbation $v(x)$ be given by
% \[v(x) = 3.5(\sin{(3x)} + 1)\frac{1}{\sqrt{2\pi}} \exp{(-x^2/2)}\]
% And let the initial condition be $u(x, 0) = \hat{u}(x) + v(x)$
% Approximate the $x$ domain,$(-\infty, \infty)$, numerically by the finite interval $[-20,20]$, and fix $u(-20) = u_-$, $u(20) = u_+$. Let $u_- = 5$, $u_+ = 1$.
% Use 150 intervals in space and 350 steps in time.
% Animate your results.
% You should see the solution converge to a translate of the traveling wave $\hat{u}$.
%
% Hint: This difference scheme is no longer a linear equation.
% We have a nonlinear equation in $U^{n+1}$.
% We can still solve this function using Newton's method or some other similar solver.
% In this case, use \li{scipy.optimize.fsolve}.
% \end{problem}

% \section*{Traveling wave solutions of an evolution equation}
% Consider the transport equation together with initial conditions on the real line:
% \begin{align*}
% 	&{ }u_t + su_x  = 0, \quad -\infty < x < \infty, \\
% 	&{ }u(x,0) = f(x).
% \end{align*}
% This initial value problem has as its general solution the function $u(x,t) = f(x -st)$. Thus the function $f$ represents a 'signal' that is transported to the right with speed $s$.
%
% In a similar fashion we may consider a general evolutionary PDE
% \begin{align}
% u_t = G(u,u_x, u_{xx}, \ldots),
% \label{lab:solitons:evol_pde}
% \end{align}
% and ask whether Equation \eqref{lab:solitons:evol_pde} has traveling wave solutions.
% In other words, is there a signal or wave profile $f(x)$, so that $u(x,t) = f(x-st)$ is a solution of \eqref{evol_pde} that carries the signal at a constant speed $s$?
%
% These traveling waves are often significant physically.  In a previous lab we looked at traveling
% wave solutions of Burgers equation, an canonical equation in gas dynamics.
%
%
% For example, in a PDE modelling insect population dynamics a traveling wave could represent a swarm of locusts; in a PDE describing a combustion process a traveling wave could represent an explosion or detonation.


\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{arcs}
\usepackage{wasysym}
\usepackage{tikz}
\usepackage{maria}

\newtheorem{lemma}{Lemma}
\theoremstyle{definition}
\newtheorem*{example}{Example}
\newtheorem*{exercise}{Exercise}

\newtheorem*{definition}{Definition}
\topmargin -0.5in
\oddsidemargin 0in
\textwidth 6in
\textheight 9.25in

\title{Generating Functions}

\author{Maria Monks -- Red Group}
\date{June 19, 2012}

\pagestyle{empty}

\begin{document}

\maketitle{}

\begin{quote}
  A generating function is a clothesline on which we hang a sequence of numbers up for display.
\end{quote}
\hfill --Herbert Wilf, \textit{Generatingfunctionology}

\section*{Generating function basics}

\indent  Generating functions are a useful tool for solving combinatorial problems.

A classic example of a generating function identity is the geometric series formula $$\frac{1}{1-x}=1+x+x^2+x^3+\cdots.$$  (The term \textit{function} is misleading - here $x$ is just a formal symbol, and the coefficients of the series are the important part!)

\begin{definition}
The \textit{(ordinary) generating function} of the sequence $c_0,c_1,c_2,\ldots$ with variable $x$ is the expression $$c_0+c_1x+c_2x^2+\cdots.$$  We abbreviate this series as $$\sum_{i=0}^\infty c_ix^i.$$
\end{definition}

Generating functions can be added and multiplied together.  They can also be differentiated, and sometimes composed!

The following are \textit{definitions} of addition, multiplication, differentiation, and composition of generating functions (we're starting from the beginning here - no calculus allowed.)

\begin{itemize}
 \item \textbf{Addition:} $\sum_{i=0}^\infty a_i x^i+\sum_{i=0}^\infty b_i x^i=\sum_{i=0}^\infty (a_i+b_i) x^i$
 \item \textbf{Multiplication:} $\left(\sum_{i=0}^\infty a_i x^i\right)\cdot \left(\sum_{i=0}^\infty a_i x^i\right)=\sum_{n=0}^\infty \left(\sum_{i=0}^n a_ib_{n-i}\right)x^n$
 \item \textbf{Differentiation:} $\frac{d}{dx} \left(\sum_{n=0}^\infty a_n x^n\right)=\sum_{i=1}^\infty na_nx^{n-1}$
 \item \textbf{Composition:} If $F(x)=f_0+f_1x+f_2x^2+\cdots$ and $G(x)=g_1x+g_2x^2+\cdots$, then $$F\circ G(x)=\sum_{n\ge 0} f_nG(x)^n=\sum_{N=0}^\infty h_N x^N$$ where $$h_N=\sum_{s_1+\cdots+s_k=N} f_kg_{s_1}g_{s_2}\cdots g_{s_n}.$$
\end{itemize}

\begin{exercise}
  Is there a generating function that behaves like an additive identity?  A multiplicative identity?  Can subtraction and division of generating functions be defined?  Why did we only define composition above in the case that $G$ has no constant term?
\end{exercise}

\begin{exercise}
  Use the definitions above to prove the generating function identity $$\frac{1}{1-x}=1+x+x^2+x^3+\cdots.$$  Notice that $1-x=1-x+0\cdot x^2+0\cdot x^3+\cdots$ is a generating function as well.
\end{exercise}

\section*{Tricks for manipulating generating functions}

Let $F(x)=\sum_{n=0}^\infty a_n x^n$ and $G(x)=\sum_{n=0}^\infty b_n x^n$ be generating functions over $x$.  Try your hand at proving the following identities, using only the definitions above.

\begin{itemize}
  \item $x F(x)=\displaystyle\sum_{n=1}^\infty a_{n-1}x^n$
  \item $\displaystyle{\frac{F(x)-a_0}{x} = \sum_{n=0}^\infty a_{n+1}x^n}$
  \item $\displaystyle{\frac{d}{dx} (F(x)+G(x))=\frac{d}{dx}F(x)+\frac{d}{dx}G(x)}$
  \item $\displaystyle{\frac{d}{dx} (F(x)G(x))=G(x)\cdot\frac{d}{dx}F(x)+F(x)\cdot \frac{d}{dx}G(x)}$
  \item If $b_0\neq 0$, $G(x)$ has a multiplicative inverse: $$G(x)^{-1}=b_0^{-1}-b_0^{-1}b_1x+ (b_0^{-3}b_1^2-b_0^{-2}b_2)x^2+\cdots.$$
  \item If $b_0\neq 0$, $\displaystyle{\frac{d}{dx} \left(\frac{F(x)}{G(x)}\right)=\frac{G(x)\frac{d}{dx}F(x)-F(x)\frac{d}{dx}G(x)}{G(x)^2}}$.
\end{itemize}

\begin{exercise}  Show that
 $$\frac{1}{(1-x)^2}=1+2x+3x^2+4x^3+5x^4+\cdots.$$
\end{exercise}

\section*{Using generating functions to solve recurrences}

Suppose we wish to find an explicit formula for the $n$th Fibonnacci number $F_n$, where $F_0=0$, $F_1=1$, and $F_{n}=F_{n-1}+F_{n-2}$ for all $n\ge 2$.  Consider the generating function $$G(x)=\sum_{n=0}^\infty F_n x^n.$$  Let's manipulate this to take advantage of the recursion:
$$ G(x)-xG(x)-x^2G(x) = F_0+F_1x-F_0x+\sum_{n=2}^\infty (F_n-F_{n-1}-F_{n-2}) x^n =x.$$
Thus $G(x)=x/(1-x-x^2)$.  Using partial fractions and expanding each term as a geometric series, we find that $$G(x)=\sum_{n=0}^\infty \frac{1}{\sqrt{5}}\left(\left(\frac{1+\sqrt{5}}{2}\right)^n-\left(\frac{1-\sqrt{5}}{2}\right)^n\right)x^n,$$ and so $F_n=\frac{1}{\sqrt{5}}\left(\left(\frac{1+\sqrt{5}}{2}\right)^n-\left(\frac{1-\sqrt{5}}{2}\right)^n\right)$.

\begin{exercise}
  Use generating functions to find an explicit formula for the $n$th term in the sequence $a_n$ where $a_0 = 1$, $a_1 = 5$, $a_{n+2} = 4a_{n+1} -3a_n$.
\end{exercise}

\section*{Exponential generating functions}

We now have a good handle on ordinary generating functions.  But there are other useful generating functions associated to a given sequence of numbers!

\begin{definition} The \textit{exponential generating function} for the sequence $\{a_i\}_{i=0}^\infty$ is the series $\sum_{n=0}^\infty \frac{a_n}{n!}x^n$.  \end{definition}

\textbf{When to use them:}  Exponential generating functions are often useful for dealing with sequences that count \textit{labeled} objects, whereas ordinary generating functions are better for \textit{unlabeled} objects.

For instance, if we let $p(n)$ be the number of ways of sorting $n$ indistinguishable balls into various groups, and let $B(n)$ be the number of ways of sorting $n$ balls labeled $1,2,\ldots,n$ into various groups, it is easy to work with the ordinary generating function for $p(n)$ and the exponential generating function for $B(n)$, but not the other way around. (See Problems.)

\textbf{Behaviour:}  The product of exponential generating functions behaves somewhat differently from that of ordinary generating functions:
$$\left(\sum_{n=0}^\infty \frac{a_n}{n!}x^n\right)\cdot \left(\sum_{n=0}^\infty \frac{b_n}{n!}x^n\right)=\sum_{n=0}^\infty \frac{1}{n!}\left(\sum_{k=0}^n\binom{n}{k}a_k b_{n-k}\right)x^n$$

\begin{exercise}
What do addition and differentiation do to exponential generating functions?
\end{exercise}

We can define $e^{x}$, $\sin(x)$, and $\cos(x)$ to be the exponential generating functions shown below.
\begin{itemize}
  \item $e^{x}=\sum_{n=0}^\infty \frac{1}{n!}x^n$
  \item $\sin(x)=\sum_{n=0}^\infty (-1)^n\frac{1}{(2n+1)!}x^{2n+1}$
  \item $\cos(x)=\sum_{n=0}^\infty (-1)^n\frac{1}{(2n)!}x^{2n}$
\end{itemize}

\begin{exercise}
  Using the definition of $e^x$ as a generating function, show that $e^{x}e^{y}=e^{x+y}$ and that $\frac{d}{dx}e^{x}=e^x$.
\end{exercise}

And now for some...

\section*{Problems!}

\begin{enumerate}
\item Prove the following generating function identities:
   \begin{enumerate}
     \item $(1+x)^{n}=\sum_{k=0}^n \binom{n}{k}x^k$
     \item $(1-x)^{-n}=\sum_{k=0}^\infty \binom{-n}{k}(-x)^k=\sum_{k=0}^\infty \binom{n+k-1}{k} x^k$
     \item $\frac{1}{1-y(1+x)}=\sum_{k,n}\binom{n}{k}x^ky^n$
   \end{enumerate}

\item (Andy Niedermaier.)  Find a closed form for the generating function for each of the following sequences, and use it to find an explicit formula for $a_n$:
\begin{enumerate}
  \item $a_0 = 1$, $a_1 = 6$, $a_{n+2} = 4a_{n+1} - 4a_n$
  \item $a_0 = 0$, $a_1 = 5$, $a_2 = 47$, $a_{n+3} = 31a_{n+1} + 30a_n$
  \item $a_0 = a_1 = 1$, $a_{n+2} = a_{n+1} + 6a_n + n$
\end{enumerate}

\item Simplify $\sum_{n=0}^\infty n^2 x^n$.  (There are at least three nice ways of doing this!)

\item Prove the following combinatorial identities using generating functions:
\begin{enumerate}
   \item $\sum_{k=0}^n\binom{n}{k}^2=\binom{2n}{n}$
   \item $\sum_{k=0}^n k \binom{n}{k}=n\cdot 2^{n-1}$
   \item $\binom{n}{0}+\binom{n}{2}+\binom{n}{4}+\cdots=\binom{n}{1}+\binom{n}{3}+\binom{n}{5}+\cdots=2^{n-1}$
\end{enumerate}

\item Find an explicit formula for $$\binom{n}{0}+\binom{n}{3}+\binom{n}{6}+\binom{n}{9}+\cdots$$ in terms of $n$.

\item  (360 Problems for Mathematical Contests.) Find a closed form expression for the sum $$S_n=\binom{n}{1}-3\binom{n}{3}+5\binom{n}{5}-7\binom{n}{7}+\cdots.$$

\item In each of the following, find the ordinary generating function for the sequence whose $n$th term is described below.
   \begin{enumerate}
     \item The number of subsets of $\{1,2,\ldots,n\}$.
     \item $p(n)$, the number of \textit{partitions} of $n$.  That is, the number of ways of writing $n$ as a sum of positive integers where order doesn't matter.
     \item The number of partitions of $n$ into distinct parts.
     \item The number of partitions of $n$ into odd parts.  How does this relate to the generating function for partitions into distinct parts?
     \item The number of \textit{$k$-compositions} of $n$, that is, ways of writing $n$ as a sum of $k$ \textit{ordered} nonnegative integers $s_1,\ldots,s_k$.
     \item The number of integer lattice paths from $(0,0)$ to $(n,n)$, going either one unit right or one unit up at each step, such that every point $(x,y)$ on the path satisfies $y\le x$.
   \end{enumerate}

\item (HMMT 2007.)  Let $S$ denote the set of all triples $(i,j,k)$ of positive integers satisfying $i+j+k=17$.  Compute $$\sum_{(i,j,k)\in S} ijk.$$

\item \textbf{Signless Stirling Numbers of the First Kind:} Let $c(n,k)$ be the number of permutations of $\{1,2,\ldots,n\}$ which are a product of $k$ cycles.  Prove that $$\sum_{k}c(n,k)y^k=y(y+1)\cdots(y+n-1).$$

\item \textbf{Stirling Numbers of the Second Kind:} Let $S(n,k)$ be the number of ways of partitioning a set of $n$ labeled elements into $k$ nonempty unordered blocks. Show that $$\sum_{n} S(n,k)x^n=\frac{x^k}{(1-x)(1-2x)\cdots (1-kx)}.$$

\item \textbf{Derangements:} Let $D_n$ be the number of \textit{derangements} of $n$, that is, the number of permutations $\phi$ of $\{1,2,\ldots,n\}$ such that $\phi(i)\neq i$ for any $1\le i \le n$.  Find a closed form expression for the exponential generating function of $D_n$.  (Hint: Multiply $D(x)=\sum_{n\ge 0} \frac{D_n}{n!} x^n$ by $e^x$.)

\end{enumerate}

\end{document}


\subsection{Sample Variance}

\begin{frame}{Sample Variance}
    \begin{itemize}
        \item A basic time series measure

        \item Linear complexity

        \item Following calculation based on \cite{chan1983algorithms}
    \end{itemize}
\end{frame}

\begin{frame}{Sample Variance}{Calculation}
    \begin{block}{Given}
        \begin{itemize}
            \item A domain set $\mathbb{U}$

            \item A distance measure function $d$ with $d: \mathbb{U} \times \mathbb{U} \to \mathbb{R}$

        \end{itemize}
    \end{block}
    \begin{block}{Input}
        \begin{itemize}
            \item A time series $Q = (q_1, q_2, \dots, q_i, \dots, q_l)$ with length $l$ over the domain set
                $\mathbb{U}$
        \end{itemize}
    \end{block}
\end{frame}

\begin{frame}{Sample Variance}{Calculation}
    \begin{block}{Calculation}
        \begin{itemize}
            \item $VAR(Q) = \frac{1}{l}\sum \limits_{i=1}^{l} d(q_i, \bar{q})^2$

        \end{itemize}
    \end{block}
    \begin{block}{Mean of $Q$}
        \begin{itemize}
            \item $\bar{q} = \frac{1}{l} \sum \limits_{i=1}^{l} q_i$
        \end{itemize}
    \end{block}
\end{frame}


\chapter{Notation}
\begin{tabular}{@{}lll@{}}
\renewcommand{\baselinestretch}{1.3}\small\normalsize
$a$ & italic Latin letter & scalar\\
$\theta$ & italic Greek letter & angle\\
\ve{a} & boldface Greek/Latin letter & column vector\\
\ve{0} & boldface figure 0 & null vector (of appropriate dimension)\\
\m{M} & sans serif upper-case letter & matrix\\
\m{1} & sans serif figure 1 & identity matrix (of appropriate dim.)\\
\q{q} & sans serif lower-case letter & quaternion\\
$\dot{c}$ & dot & first derivative with respect to time\\
$\ddot{c}$ & double dot & second derivative with respect to time\\
$\m{J}^T$ & superscript $T$ & matrix transpose\\
$\m{M}^{-1}$ & superscript $-1$ & matrix or quaternion inverse (eq.~\ref{quatInverse})\\
$\overline{\q{q}}$ & bar & quaternion conjugate (eq.~\ref{quatConjugate})\\
$\norm{\ve{a}}$ & norm & vector or quaternion magnitude (eq.~\ref{quatMagnitude})\\
$\tilde{\ve{a}}$ & tilde & corresponding quaternion (eq.~\ref{vectorToQuat})\\
$\dual{\ve{a}}$ & asterisk & dual tensor (eq.~\ref{dualTensor})\\
$\ve{a}\times\ve{b}$ & cross & vector cross product\\
$\ve{a}\cdot\ve{b}$ & dot & inner product\\
$\Re(\q{q})$ & real & real part of a quaternion\\
\end{tabular}
\vspace{10pt}

Given any vector $\ve{a} = (a_1, a_2, a_3)^T$, we define its dual
tensor, written as a $3\times3$ matrix, to be
\begin{equation}\label{dualTensor}
\dual{\ve{a}} = \left[\begin{array}{ccc}
    0 & -a_3 & a_2 \\ a_3 & 0 & -a_1 \\ -a_2 & a_1 & 0
    \end{array}\right]
\end{equation}
(see \cite{RHB:02,BaraffWitkin:97} and also Kalra~\cite{Kalra:95}, who defines it to be
the transpose of the expression above).
The dual allows us to rewrite a vector cross product as a matrix multiplication:
\begin{equation}
\ve{a}\times\ve{b} = \dual{\ve{a}}\,\ve{b}
\end{equation}
Note that $(\dual{\ve{a}})^T = -\dual{\ve{a}}$.

Let us also recall some basic identities of vector and matrix algebra~\cite{RHB:02}:
\begin{eqnarray*}
\ve{a}\times\ve{a} & = & \ve{0}\quad\mathrm{(the~null~vector)} \\
\ve{a}\times\ve{b} & = & -\ve{b}\times\ve{a} \\
\ve{a}\times(\ve{b} + \ve{c}) & = & \ve{a}\times\ve{b} + \ve{a}\times\ve{c} \\
\ve{a}\cdot\ve{b} & = & \ve{b}\cdot\ve{a} \\
\ve{a}\cdot\ve{b} & = & \ve{a}^T\,\ve{b} \\
\ve{a}\cdot(\ve{b} + \ve{c}) & = & \ve{a}\cdot\ve{b} + \ve{a}\cdot\ve{c} \\
\ve{a}\cdot(\ve{b}\times\ve{c}) & = & \ve{b}\cdot(\ve{c}\times\ve{a}) \\
    & = & \ve{c}\cdot(\ve{a}\times\ve{b}) \\
\ve{a}\times(\ve{b}\times\ve{c}) & = &
    \ve{b}(\ve{a}\cdot\ve{c}) - \ve{c}(\ve{a}\cdot\ve{b}) \\
(\m{A}\m{B})\m{C} & = & \m{A}(\m{B}\m{C}) \\
\m{A}(\m{B} + \m{C}) & = & \m{A}\m{B} + \m{A}\m{C} \\
(\m{A}\m{B})^T & = & \m{B}^T\m{A}^T \\
\m{A}\m{A}^{-1} = \m{A}^{-1}\m{A} & = & \m{1}\quad\mathrm{(the~identity~matrix)}
\end{eqnarray*}


\documentclass[12pt]{article}

\usepackage[left=1cm,right=1cm,
    top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage[T2A]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\parindent=0.5cm

\usepackage{hyperref}
\usepackage{indentfirst}

\numberwithin{equation}{section}
%\usepackage{showkeys}

\begin{document}
\section*{MSAI Statistics \& Probability – Week 5 Seminar \& HW}\\

\textbf{Problem 1:} Consider an Erd\H{o}s-R\'{e}nyi random graph $G(n,p)$ on $n$ vertices (that is, for any two vertices, there is an edge between them with probability $p$ and no edge with probability $1-p$; all edges are independent of each other). Find the expected number of triangles in $G(n,p)$ (triples of vertices all pairwise connected with an edge). Provide an answer for $n=7,~p=\frac{1}{7}.$
\\

\textbf{Problem 2:} Let $\xi\sim\mathcal{N}(0,\sigma^2)$ (a random variable distributed normally with zero mean and dispersion $\sigma^2$). Find $\mathbb{E}\xi^k$ (expectation of $k$-th power of $\xi$). Provide an answer for $\sigma^2 = 2,~k=4.$
\\

\textbf{Problem 3:} Let $\xi\sim\textrm{Pois}(\lambda)$ (a random variable with Poisson distribution with rate $\lambda$). For some natural $k\in\mathbb{N}$, find $\mathbb{E}{\xi\choose k}$ (expected ${\xi\choose k}$ - binomial coefficient). Provide an answer for $\lambda=3,~k=4.$
\\

\textbf{Problem 4:} Find $\mathbb{E}\xi$ if $\xi$ have the following distribution function:
$$F(x)=
\begin{cases}
0, ~~~ x<-2,\\
1/5, ~~~-2\leq x<1 \\
x^2/4, ~~~1\leq x<2 \\
1, ~~~ x\geq 2
\end{cases}$$

\textbf{Problem 5:} (Conditional expectation). You have two fair dice (six-sided cubes, $1,2,...,6$ on the faces, each face equally probable). You throw them and read the sum of their values, $z=x+y$. Find the expected $\mathbb{E}(x|z).$

To do this, consider the following functional
$$J(\alpha)=\sum_{x,z}(x-\alpha z)^2p(x,z)=\sum_{x,y}(x-\alpha (x+y))^2p(x,y)$$
- it only depends on $\alpha$. You know all possible $x$ outcomes and all possible $z=x+y$ outcomes and their probabilities! Use any mathematical software to compute the expression of $J(\alpha)$ as a function of $\alpha$, and then minimize it with respect to $\alpha.$

\end{document}