% \documentclass[draft,11pt]{article}
\chapter{Random Matrix Concentration and Spectral Graph
  Sparsification}
\label{cha:randmat}
%\usepackage{cleveref}

%\allowdisplaybreaks

%%% for this lecture
%\newcommand\symset{S}
%\newcommand\psdset{S_+}
%\newcommand\pdset{S_{++}}

%\newcommand\symsetn{\symset^n}
%\newcommand\psdsetn{\psdset^n}
%\newcommand\pdsetn{\pdset^n}

%%% added by Hongjie
%\newcommand{\Hongjie}[1]{{\color{red} Hongjie: #1}}

%\begin{document}
\sloppy
%\lecture{8 --- Wednesday, April 8th}
%{Spring 2020}{Rasmus Kyng, Scribe: Hongjie Chen}{Random Matrix
%Concentration and Spectral Graph Sparsification}

\section{Matrix Sampling and Approximation}

We want to begin understanding how sums of random matrices behave, in
particular, whether they exhibit a tendency to concentrate in the same
way that sum of scalar random variables do under various conditions.

First, let's recall a scalar Chernoff bound, which shows that a sum of
bounded, non-negative random variables tend to concentrate around
their mean.
\begin{theorem}[A Chernoff Concentration Bound]
Suppose $X_1, \ldots, X_k \in \R$ are independent, non-negative,
random variables with $X_i \leq R$ always.
Let $X = \sum_i X_i$, and $\mu = \E{X}$,
then for $0 < \epsilon \leq 1$
\[
\Pr[ X \geq (1+\epsilon) \mu ] \leq \exp\left(\frac{-\epsilon^2
    \mu}{4R}\right)
\text{ and }
\Pr[ X \leq (1-\epsilon) \mu] \leq \exp\left(\frac{-\epsilon^2
    \mu}{4R}\right)
.
  \]
\end{theorem}
The Chernoff bound should be familiar to most of you, but you may not
have seen the following very similar bound.
The Bernstein bound, which we will state in terms of zero-mean
variables, is much like the Chernoff bound.
It also requires bounded variables.
But, when the variables have small variance, the Bernstein bound is
sometimes stronger.
\begin{theorem}[A Bernstein Concentration Bound]
  \label{thm:bernstein}
  Suppose $X_1, \ldots, X_k \in \R$ are independent, zero-mean,
random variables with $\abs{X_i} \leq R$ always.
Let $X = \sum_i X_i$, and $\sigma^2 = \Var{X} = \sum_i \E{X_i^2}$,
then for $\epsilon > 0$
\[
\Pr[ \abs{X} \geq t] \leq 2\exp\left(\frac{-t^2}{2Rt +
    4\sigma^2}\right)
.
  \]
\end{theorem}
We will now prove the Bernstein concentration bound for scalar random
variables, as a warm-up to the next section, where we will prove a version
of it for matrix-valued random variables.
To help us prove Bernstein's bound, first let's recall Markov's
inequality.
This is a very weak concentration inequality, but also very versatile,
because it requires few assumptions.
\begin{lemma}[Markov's Inequality]
  \label{lem:markov}
  Suppose $\XX \in \R$ is a non-negative random variable, with a finite
  expectation.
  Then for any $t > 0$,
  \[
    \Pr[ X \geq t] \leq \frac{\E{X}}{t}
    .
    \]
\end{lemma}
\begin{proof}
  \begin{align*}
    \E{X}
    &=
      \Pr[X \geq t]\E{X \mid X \geq t} + \Pr[X < t]\E{X \mid X < t}
   \\
    &\geq \Pr[X \geq t]\E{X \mid X \geq t}
    \\
    &\geq Pr[X \geq t] \cdot t
      .
\end{align*}
  We can rearrange this to get the desired statment.
\end{proof}
Now, we are ready to prove Bernstein's bound.
\begin{proof}[Proof of Theorem~\ref{thm:bernstein}]
  We will focus on bounding the probability that $\Pr[ X \geq t] $.
  The proof that $\Pr[ -X \geq t] $ is small proceeds in the same way.

  First we observe that
  \begin{align*}
    \Pr[ X \geq t ]
    &=
      \Pr [ \exp(\theta X) \geq \exp(\theta t) ]
      \tag*{for any $\theta >0$,
      because $x \to \exp(\theta x)$ is strictly increasing.}
    \\
    &\leq \exp(-\theta t) \E{\exp(\theta X)}
      \tag*{by Lemma~\ref{lem:markov} (Markov's Inequality).}
  \end{align*}
  Now, let's require that $\theta \leq 1/R$
  This will allow us to using the following bound: For all $\abs{z}
  \leq 1$,
  \begin{equation}
     \label{eq:expub}
  \exp(z) \leq 1+z+z^2.
\end{equation}
We omit a proof of this, but the plots in Figure~\ref{fig:expub}
suggest that this upper bound holds. The reader should consider how to
prove this.
\begin{figure}[h]
  \centering
  \includegraphics[width=0.4
  \textwidth]{fig/lecture7_exp-bounds}
\caption{Plotting $\exp(z)$ compared to $1+z+z^2$.}
\label{fig:expub}
\end{figure}
With this in mind, we see that
\begin{align*}
  \E{\exp(\theta X)}
  &= \E{\exp\left(\theta \sum_i X_i\right)}
  \\
  &=\E{\Pi_{i} \exp(\theta X_i)}
  \\
  &=\Pi_{i} \E{\exp{(\theta X_i)}}
    \tag*{because $\E{YZ} = \E{Y}\E{Z}$ for independent $Y$ and
    $Z$.} % See note\footnote{ hello }}%\todo{text}}.
  \\
  &\leq\Pi_{i} \E{1+\theta X_i+(\theta X_i)^2}
  \\
  &=\Pi_{i} (1+\theta^2\E{ X_i^2})
    \tag*{ because $X_i$ are zero-mean.}
  \\
  &\leq\Pi_{i} \exp( \theta^2\E{ X_i^2} )
    \tag*{ because $1+z \leq \exp(z)$ for all $z \in R$.}
  \\
  &=\exp\left( \sum_i \theta^2\E{ X_i^2} \right) = \exp(\theta^2 \sigma^2)
    .
\end{align*}
Thus
$\Pr[ X \geq t ] \leq \exp(-\theta t)\E{\exp(\theta X)} \leq
\exp(-\theta t + \theta^2 \sigma^2)$.
Now, to get the best possible bound, we'd like to minimize
$-\theta t + \theta^2 \sigma^2$ subject to the constraint
$0< \theta \leq 1/R$.
Setting
\[ \frac{\partial}{\partial \theta}\left(
    -\theta t + \theta^2
    \sigma^2
  \right)
  = - t + 2 \theta \sigma^2
  .
\]
Setting this derivative to zero gives
$\theta = \frac{t}{2\sigma^2}$,
and plugging that in gives
\[
    -\theta t + \theta^2\sigma^2
    =
    -\frac{t^2}{4\sigma^2}
  \]
This choice only satisfies our constraints on $\theta$ if
$\frac{t}{2\sigma^2} \leq 1/R$.
Otherwise, we let $\theta =  1/R$ and note that in this case
\[
    -\theta t + \theta^2\sigma^2
    =
    -\frac{t}{R} + \frac{\sigma^2}{R^2}
    \leq
    -\frac{t}{R} + \frac{t}{2R}
    =
    -\frac{t}{2R}
  \]
  where we got the inequality from $t > 2\sigma^2/R$.
  Altogether, we can conclude that there always is a choice of
  $\theta$ s.t.
  \[
    -\theta t + \theta^2\sigma^2
    \leq
    - \min\left( \frac{t}{2R} , \frac{t^2}{4\sigma^2} \right)
    \leq
    - \frac{t^2}{2Rt + 4\sigma^2}
    .
  \]
  In fact, with the benefit of hindsight, and a little algebra, we
  arrive at the same conclusion in another way:
  One
  can check that the
  following choice of $\theta$ is always valid and achives the same
  bound:
  $\theta = \frac{1}{2
    \sigma^2}\left( t - \frac{\sqrt{R} \cdot t^{3/2}}{\sqrt{2\sigma^2 + R
        t}} \right)$.
\end{proof}
We use $\norm{\cdot}$ to denote the spectral norm on matrices.
Let's take a look at a version of Bernstein's bound that applies to
sums of random matrices.
\begin{theorem}[A Bernstein Matrix Concentration Bound (Tropp 2011)]
  \label{thm:matbernstein}
  Suppose $\XX_1, \ldots, \XX_k \in \R^{n \times n}$ are independent,
  symmetric matrix-valued
  random variables.
Assume each $\XX_i$ is zero-mean, i.e. $\E{\XX_i} = \matzero_{n \times
  n}$, and that $\norm{\XX_i} \leq R$ always.
Let $\XX = \sum_i \XX_i$, and $\sigma^2 = \Var{\XX} = \sum_i \E{\XX_i^2}$,
then for $\epsilon > 0$
\[
\Pr[ \norm{\XX} \geq t] \leq 2n\exp\left(\frac{-t^2}{2Rt + 4\sigma^2}\right)
.
\]
\end{theorem}
This basically says that probability of $\XX$ being large in spectral
norm behaves like the scalar case, except the bound is larger by a factor
$n$, where the matrices are $n \times n$.
We can get a feeling for why this might be a reasonable bound by
considering the case of random diagonal matrices.
Now $\norm{\XX} = \max_j \abs{\XX (j,j)} = \max_j \abs{\sum_i
  \XX_i(j,j)}$.
In this case, we need to bound the largest of the $n$ diagonal
entries: We can do this by a union bound over $n$ instances of the
scalar problem -- and this also turns out to be essentially tight in some cases,
meaning we can't expect a better bound in general.

\section{Matrix Concentration}
In this section we will prove the Bernstein matrix concentration bound
(Tropp 2011) that we saw in the previous section.
\begin{theorem}\label{thm:matbernstein}
  Suppose $\XX_1, \ldots, \XX_k \in \R^{n \times n}$ are independent, symmetric matrix-valued random variables.
  Assume each $\XX_i$ is zero-mean, i.e. $\E{\XX_i} = \matzero_{n \times
  n}$, and that $\norm{\XX_i} \leq R$ always.
  Let $\XX = \sum_i \XX_i$, and $\sigma^2 = \|\Var{\XX}\| = \|\sum_i \E{\XX_i^2}\|$, then for $t > 0$
  \[
  \Pr[ \norm{\XX} \geq t] \leq 2n\exp\left(\frac{-t^2}{2Rt + 4\sigma^2}\right).
  \]
\end{theorem}
But let's collect some useful tools for the proof first.

\begin{definition}[trace]
  The trace of a square matrix $\AA$ is defined as
  \[ \trace{\AA} := \sum_{i} \AA(i,i) \]
\end{definition}

\begin{claim}[cyclic property of trace]
  $\trace{\AA\BB} = \trace{\BB\AA}$
\end{claim}

Let $S^n$ denote the set of all $n\times n$ real symmetric matrices,
$S^n_+$ the set of all $n\times n$ positive semidefinite matrices,
and $S^n_{++}$ the set of all $n\times n$ positive definite matrices.
Their relation is clear, $S^n_{++} \subset S^n_+ \subset S^n$.
For any $\AA\in S^n$ with eigenvalues $\lambda_1(\AA)\leq\cdots\leq\lambda_n(\AA)$, by spectral decomposition theorem, $\AA = \VV\LLambda\VV^\trp$ where $\LLambda=\diag_i\{\lambda_i(\AA)\}$ and $\VV^\trp\VV=\VV\VV^\trp=\II$,
we'll use this property without specifying in the sequel.

\begin{claim}\label{clm:tr_eq_sum_eigen}
  Given a symmetric and real matrix $\AA$, $\trace{\AA} = \sum_{i}\lambda_i$, where $\{\lambda_i\}$ are eigenvalues of $A$.
\end{claim}
\begin{proof}
  \[ \trace{\AA} = \trace{\VV\LLambda\VV^\trp} = \trace{\LLambda\underbrace{\VV^\trp\VV}_{\II}} = \trace{\LLambda} = \sum_i \lambda_i. \]
\end{proof}

\subsection{Matrix Functions}
\begin{definition}[Matrix function]
  Given a real-valued function $f:\R\to\R$, we extend it to a matrix function $f:S^n\to S^n$.
  For $\AA\in S^n$ with spectral decomposition $\AA=\VV\LLambda\VV^\trp$, let
  \[ f(\AA) = \VV \diag_i\left\{f(\lambda_i)\right\} \VV^\trp. \]
\end{definition}
\begin{example}
  Recall that every PSD matrix $\AA$ has a square root $\AA^{1/2}$.
  If $f(x) = x^{1/2}$ for $x\in\R_+$, then $f(\AA)=\AA^{1/2}$ for $\AA\in S^n_+$.
\end{example}
\begin{example}
  If $f(x) = \exp(x)$ for $x\in\R$, then $f(\AA) = \exp(\AA) = \VV\exp(\LLambda)\VV^\trp$ for $\AA\in S^n$.
  Note that $\exp(\AA)$ is positive definite for any $\AA\in S^n$.
\end{example}

\subsection{Monotonicity and Operator Monotonicity}
 Cosider a function $f:
 \calD \to \calC$.
 If we have a partial order $\leq_{\calD}$ defined on $\calD$ and a
 partial order $\leq_{\calC}$ defined on $\calC$, then we say that
 the function is monotone increasing (resp. decreasing) w.r.t. this pair of orderings if for all $d_1, d_2
 \in \calD$ s.t. $d_1 \leq_{\calD} d_2$ we have $f(d_1) \leq_{\calC}
 f(d_2) $ (resp. decreasing if $f(d_2) \leq_{\calC} f(d_1)$).

Let's  introduce some terminonology for important special cases of this idea.
We say that a function $f : \calS \to \R$, where $\calS \subseteq \symsetn$,
  is monotone increasing if $\AA \preceq \BB$ implies $f(\AA) \leq f(\BB)$.

  Meanwhile, a function $f : \calS \to \calT$ where $\calS, \calT \subseteq \symsetn$
  is said to be operator monotone increasing if $\AA \preceq \BB $ implies
  $f(\AA) \preceq f(\BB)$.
%
\begin{lemma}
  Let $T \subseteq \R$.
  If the scalar function $f : T \to \R$ is monotone increasing, the matrix
  function $\XX \mapsto \trace{f(\XX)}$ is monotone increasing.
\end{lemma}
\begin{proof}
  From previous chapters, we know if $\AA\preceq\BB$ then $\lambda_i(\AA)\leq\lambda_i(B)$ for all $i$.
  As $x \mapsto f(x)$ is monotone, then $\lambda_i(f(\AA))\leq\lambda_i(f(B))$ for all $i$.
  By Claim \ref{clm:tr_eq_sum_eigen}, $\trace{f(\AA)}\leq\trace{f(\BB)}$.
\end{proof}
From this, and the fact that $x \mapsto \exp(x)$ is a monotone function on the
reals, we get the following corollary.
\begin{corollary}\label{cor:trexpmono}
  If $\AA\preceq\BB$, then $\trace{\exp(\AA)}\leq\trace{\exp(\BB)}$,
  i.e. $\XX \mapsto \trace{\exp(\XX)}$ is monotone increasing.
\end{corollary}

\begin{lemma}\label{lem:invmono}
  If $\matzero \prec\AA\preceq\BB$, then $\BB^{-1}\preceq \AA^{-1}$, i.e. $\XX
  \mapsto \XX^{-1}$ is operator monotone decreasing on $\pdsetn$.
\end{lemma}
You will prove the above lemma in this week's exercises.

\begin{lemma}\label{lem:logmono}
  If $0\prec\AA\preceq\BB$, then $\log(\AA)\preceq\log(\BB)$.
\end{lemma}
To prove this lemma, we first recall an integral representation of the
logarithm.
\begin{lemma}
  \label{lem:logintegral}
\[
\log a = \int_0^{\infty} \left(\frac{1}{1+t} -\frac{1}{a+t}  \right)
\diff t
    \]
\end{lemma}
\begin{proof}
  \begin{align*}
\int_0^{\infty} \left(\frac{1}{1+t} -\frac{1}{a+t}  \right) \diff t
    &=
    \lim_{T \to \infty}
      \int_0^{T} \left(\frac{1}{1+t} -\frac{1}{a+t}  \right) \diff t
\\
    &=
    \lim_{T \to \infty}
      \left[ \log(1+t) - \log(a+t) \right]_0^{T}
\\
    &=
      \log(a)
      +
    \lim_{T \to \infty}
      \log\left( \frac{1+T}{a+T} \right)
\\
    &=
      \log(a)
  \end{align*}
\end{proof}
\begin{proof}[Proof sketch of Lemma~\ref{lem:logmono}]
Because all the matrices involved are diagonalized by the same
orthogonal transformation, we can conclude from
Lemma~\ref{lem:logintegral} that for a matrix $\AA \succ \matzero$,
\[
 \log(\AA)
   = \int_0^{\infty} \left(\frac{1}{1+t}\II -(t \II + \AA)^{-1} \right) \diff t
\]
This integration can be expressesd as the limit of a sum with positive
coefficients, and from this we
can show that is the integrand (the term inside the integration
symbol) is operator monotone increasing in $\AA$ by
Lemma~\ref{lem:invmono}, the result of the integral, i.e. $\log(\AA)$
must also be operator monotone increasing.
\end{proof}

The following is a more general version of Lemma 1.6.
\begin{lemma}
  Let $T \subset \R$.
  If the scalar function $f : T \to \R$ is monotone, the matrix
  function $\XX \mapsto \trace{f(\XX)}$ is monotone.
\end{lemma}

\begin{remark}
  It is not always true that when $f: \R \to \R$ is monotone, $f:
  \symsetn \to \symsetn$ is operator monotone.
  For example, $\XX \mapsto \XX^2$ and $\XX \mapsto \exp(\XX)$ are \emph{not}
  operator monotone.
  % while ${\XX \to (\XX)^{1/2}}$ is operator monotone
  % over $\psdsetn$.
\end{remark}

\subsection{Some Useful Facts}
\begin{lemma}\label{lem:ineq_exp}
  $\exp(\AA) \preceq \II + \AA + \AA^2$ for $\|\AA\|\leq1$.
\end{lemma}
\begin{proof}
  \begin{align*}
    \II+\AA+\AA^2-\exp(\AA)
    &= \VV\II\VV^\trp + \VV\LLambda\VV^\trp + \VV\LLambda^2\VV^\trp - \VV\exp(\LLambda)\VV^\trp \\
    &= \VV\left(\II+\LLambda+\LLambda^2-\exp(\LLambda)\right)\VV^\trp \\
    &= \VV \diag_i\{1+\lambda_i+\lambda_i^2-\exp(\lambda_i)\} \VV^\trp
  \end{align*}
  Recall $\exp(x)\leq1+x+x^2$ for all $|x|\leq1$.
  Since $\|A\|\leq1$ i.e. $|\lambda_i|\leq1$ for all $i$, thus $1+\lambda_i+\lambda_i^2-\exp(\lambda_i)\geq0$ for all $i$,
  meaning $\II+\AA+\AA^2-\exp(\AA) \succeq 0$.
\end{proof}

\begin{lemma}\label{lem:ineq_log}
  $\log(\II+\AA) \preceq \AA$ for $A\succ-\II$.
\end{lemma}
\begin{proof}
  \begin{align*}
    \AA-\log(\II+\AA)
    &= \VV\LLambda\VV^\trp - \VV\log(\LLambda+\II)\VV^\trp \\
    &= \VV\left(\LLambda-\log(\LLambda+\II)\right)\VV^\trp \\
    &= \VV \diag_i\{\lambda_i-\log(1+\lambda_i)\} \VV^\trp
  \end{align*}
  Recall $x\geq\log(1+x)$ for all $x>-1$.
  Since $\|A\|\succ-\II$ i.e. $\lambda_i>-1$ for all $i$, thus $\lambda_i-\log(1+\lambda_i)\geq0$ for all $i$,
  meaning $\AA-\log(\II+\AA) \succeq 0$.
\end{proof}

\begin{theorem}[Lieb]
  \label{thm:Lieb}
  Let $f:S^n_{++}\to\mathbb{R}$ be a matrix function given by
  \[ f(\AA) = \trace{\exp\left(\HH+\log(\AA)\right)} \]
  for some $\HH\in S^n$. Then $-f$ is convex (i.e. $f$ is concave).
\end{theorem}
The Lieb's theorem will be crucial in our proof of Theorem \ref{thm:matbernstein},
but it is also highly non-trivial and we will omit its proof here.
The interested reader can find a proof in Chapter 8 of \cite{tropp15}.

\begin{lemma}[Jensen's inequality]
  \label{lem:Jensen}
  $\E{f(X)} \geq f(\E{X})$ when $f$ is convex;
  $\E{f(X)} \leq f(\E{X})$ when $f$ is concave.
\end{lemma}

\subsection{Proof of Matrix Bernstein Concentration Bound}
Now, we are ready to prove the Bernstein matrix concentration bound.
\begin{proof}[Proof of Theorem \ref{thm:matbernstein}]
  For any $\AA\in S^n$, its spectral norm $\|\AA\| = \max\{|\lambda_n(\AA)|,|\lambda_1(\AA)|\} = \max\{\lambda_n(\AA),-\lambda_1(\AA)\}$.
  Let $\lambda_1\leq\cdots\leq\lambda_n$ be the eigenvalues of $\XX$. Then,
  \[\Pr[\|\XX\|\geq t] = \Pr\left[(\lambda_n \geq t) \bigvee (-\lambda_1 \geq t)\right] \leq \Pr[\lambda_n \geq t]+\Pr[-\lambda_1 \geq t]. \]
  Let $\YY:=\sum_i -\XX_i$. It's easy to see that $-\lambda_n\leq\cdots\leq-\lambda_1$ are eigenvalues of $\YY$, implying $\lambda_n(\YY)=-\lambda_1(\XX)$.
  Since $\E{-\XX_i} = \E{\XX_i} = 0$ and $\|-\XX_i\|=\|\XX_i\|\leq R$ for all $i$, if we can bound $\Pr[\lambda_n(\XX)\geq t]$, then applying to $\YY$, we can bound $\Pr[\lambda_n(\YY)\geq t]$.
  As
  \[ \Pr[-\lambda_1(\XX)\geq t] = \Pr[\lambda_n(\YY)\geq t], \]
  it suffices to bound $\Pr[\lambda_n \geq t]$.

  For any $\theta>0$, $\lambda_n\geq t \iff \exp(\theta\lambda_n) \geq \exp(\theta t)$ and
  $\trace{\exp(\theta\XX)} = \sum_i \exp(\theta\lambda_i)$
  by Claim \ref{clm:tr_eq_sum_eigen}, thus $\lambda_n\geq t \Rightarrow \trace{\exp(\theta\XX)}\geq\exp(\theta t)$.
  Then, using Markov's inequality,
  \begin{align*}
    \Pr[\lambda_n\geq t]
    &\leq \Pr[\trace{\exp(\theta\XX)}\geq\exp(\theta t)] \\
    &\leq \exp(-\theta t) \E{\trace{\exp(\theta\XX)}}
  \end{align*}
  For two independent random variables $\UU$ and $\VV$, we have
  \[ \Ex{\UU,\VV} f(\UU,\VV) = \Ex{\UU}\expct{\VV}{f(\UU,\VV)|\UU} = \Ex{\UU}\expct{\VV}{f(\UU,\VV)} . \]
  Define $\XX_{<i} = \sum_{j<i} \XX_j$. Let $0<\theta\leq1/R$,
  \begin{align*}
    \Ex{}\trace{\exp(\theta\XX)}
    &= \Ex{\XX_1,...,\XX_{k-1}} \Ex{\XX_k} \tr\exp\left( \underbrace{\theta\XX_{<k}}_{\HH} + \underbrace{\theta\XX_k}_{=\log\exp(\theta\XX_k)} \right), \quad\{\XX_i\}\text{ are independent}   \\
    &\leq \Ex{\XX_1,...,\XX_{k-1}} \tr\exp\left( \theta\XX_{<k} +
      \log\Ex{}\exp(\theta\XX_k) \right), \quad\text{by \ref{thm:Lieb}
      and \ref{lem:Jensen}} \\
    &\leq \Ex{\XX_1,...,\XX_{k-1}} \tr\exp\left( \theta\XX_{<k} +
      \log\E{\II+\theta\XX_k+\theta^2\XX_k^2} \right), \quad\text{by
      \ref{lem:ineq_exp}, \ref{cor:trexpmono}, and \ref{lem:logmono}} \\
    &\leq \Ex{\XX_1,...,\XX_{k-1}} \tr\exp\left( \theta\XX_{<k} + \theta^2\Ex{}\XX_k^2 \right), \quad\text{by \ref{lem:ineq_log} and \ref{cor:trexpmono}} \\
    &= \Ex{\XX_1,...,\XX_{k-2}} \Ex{\XX_{k-1}} \tr\exp\left( \underbrace{\theta^2\Ex{}\XX_k^2+\theta\XX_{<k-1}}_{\HH} + \theta\XX_{k-1} \right),  \\
    &\quad\vdots \\
    &\leq \tr\exp\left( \theta^2 \sum_i \E{\XX_i^2} \right),\\
    &\leq \tr\exp\left( \theta^2 \sigma^2 \II \right), \quad\text{by \ref{cor:trexpmono} and } \sum_i \E{\XX_i^2}\preceq\sigma^2\II \\
    &= n\cdot\exp(\theta^2\sigma^2).
  \end{align*}
  Then, \[ \Pr[\lambda_n\geq t] \leq n\cdot\exp(-\theta t+\theta^2\sigma^2), \]
  and
  \[ \Pr[\|\XX\|\geq t] \leq 2n\cdot\exp(-\theta t+\theta^2\sigma^2). \]
  Similar to the proof of Bernstein concentration bound for one-dimension random variable, minimize the RHS over $0<\theta\leq1/R$ yields
  \[ \Pr[\|\XX\|\geq t] \leq 2n\cdot\exp\left(\frac{-t^2}{2Rt + 4\sigma^2}\right). \]
\end{proof}

\section{Spectral Graph Sparsification}

\newcommand\Gtil{\tilde{G}}
\newcommand\Etil{\tilde{E}}

In this section, we will see that for any dense graph, we can find
another sparser graph whose graph Laplacian is approximately the same
as measured by their quadratic forms.
This turns out to be a very useful tool for designing algorithms.

\begin{definition}
  \label{def:psdapxeq}
  Given  $\AA,\BB \in \psdsetn$ and $\epsilon > 0$, we say
  \[
    \AA \approx_{\epsilon} \BB \text{ if and only if }
    \frac{1}{1+\epsilon} \AA \preceq \BB \preceq (1+\epsilon) \AA
    .
    \]
\end{definition}

Suppose we start with a connected graph $G = (V,E,\ww)$, where as usual we say that $\abs{V}
= n$ and $\abs{E} = m$.
We want to produce another graph $\Gtil = (V,\Etil,\wwtil)$ s.t
$\abs{\Etil} \muchl \abs{E}$ and at the same time $\LL_{G}
\approx_{\epsilon} \LL_{\Gtil}$.
We call $\Gtil$ a \emph{spectral sparsifier} of $G$.
Our construction will also ensure that $\Etil \subseteq E$, although
this is not important in most applications.
Figure~\ref{fig:gvsgtil} shows an example of a graph $G$ and
spectral sparsifier $\Gtil$.

\begin{figure}[h]
  \centering
  \includegraphics[width=0.6
  \textwidth]{fig/lecture8_gtil}
\caption{A graph $G$ and a spectral sparsifier $\Gtil$
,
  satisfisying
 $\LL_{G} \approx_{\epsilon} \LL_{\Gtil}$ for $\epsilon = 2.42$.
}
\label{fig:gvsgtil}
\end{figure}

We are going to construct $\Gtil$ by sampling some of the edges of $G$
according to a suitable probability distribution and scaling up their weight
to make up for the fact that we pick fewer of them.

To get a better understanding for the notion of approximation given in
\ref{def:psdapxeq} means, let's observe a simple consequence of it.

Given a vertex subset $T \subseteq V$, we say that $(T, V\setminus T)$
is a \emph{cut} in $G$ and that the value of the cut is
\[
  c_G(T) = \sum_{e \in E \intersect (T \times V\setminus T) } \ww(e)
.
\]

Figure~\ref{fig:cutexample} shows the $c_G(T)$ in a graph $G$.
\begin{figure}[h]
  \centering
  \includegraphics[width=0.4
  \textwidth]{fig/lecture8_cutvalue}
\caption{The cut $c_G(T)$ in $G$.}
\label{fig:cutexample}
\end{figure}

\begin{theorem}
  If $\LL_{G} \approx_{\epsilon} \LL_{\Gtil}$, then for all $T \subseteq V$,
  \[
    \frac{1}{1+\epsilon}   c_G(T) \leq   c_{\Gtil}(T) \leq (1+\epsilon)   c_G(T)
    .
    \]
\end{theorem}
\begin{proof}
  Let $\vecone_T \in \R^V$ be the indicator of the set $T$,
  i.e. $\vecone_T(u) = 1$ for $u \in V$ and $\vecone_T(u) = 0$
  otherwise.
  We can see that $\vecone_T^{\trp} \LL_{G} \vecone_T =  c_G(T)$, and
  hence the theorem follows by comparing the quadratic forms.
\end{proof}

But how well can we spectrally approximate a graph with a sparse graph? The next
theorem gives us a nearly optimal answer to this question.
\begin{theorem}[Spectral Graph Approximation by Sampling,
  (Spielman-Srivastava 2008)]
  \label{thm:spectralgraphapxbysamp}
Consider a connected graph $G = (V,E,\ww)$, with $n = \abs{V}$.
For any $0 < \epsilon < 1$
and $0 < \delta < 1$, there exist sampling probabilities $p_e$ for
each edge $e \in E$ s.t. if we include each edge $e$ in $\Etil$
independently with probabilty $p_e$ and set its weight $\wwtil(e) =
\frac{1}{p_e} \ww(e)$, then with probability at least $1-\delta$
the graph $\Gtil = (V,\Etil,\wwtil)$ satisfies
\[
  \LL_{G} \approx_{\epsilon} \LL_{\Gtil} \text{ and }  \abs{\Etil}
  \leq O( n \epsilon^{-2} \log(n/\delta) )
.
\]
\end{theorem}
The original proof can be found in \cite{spielman2011graph}.

\begin{remark}
  For convenience, we will abbreviate $\LL_{G}$ as $\LL$ and
  $\LL_{\Gtil}$ as $\LLtil$ in the rest of this section.
\end{remark}

We are going to analyze a sampling procedure by turning our goal into
a problem of matrix concentration.
Recall that
\begin{fact}
  $\AA \preceq \BB$ implies $\CC \AA \CC^\trp \preceq \CC \BB \CC^\trp
  $ for any $\CC \in \R^{n\times n}$.
\end{fact}
By letting $\CC = \LL^{\pinv/2}$, we can see that
\begin{equation}
  \label{eq:spectralreltoabs}
  \LL \approx_{\epsilon} \LLtil \text{ implies } \PPi_{\LL}
  \approx_{\epsilon} \LL^{\pinv/2}\LLtil\LL^{\pinv/2}
  ,
\end{equation}
where $\PPi_{\LL} = \LL^{\pinv/2}\LL\LL^{\pinv/2}$ is the orthogonal
projection to the complement of the kernel of $\LL$.
\begin{definition}
  Given a matrix $\AA$, we define $\PPi_{\AA}$ to be the orthogonal
  projection to the complement of the kernel of $\AA$,
  i.e. $\PPi_{\AA} \vv = \veczero$ for $\vv \in \ker(\AA)$ and
  $\PPi_{\AA} \vv = \vv$ for $\vv \in  \ker(\AA)^{\perp}$.
  Recall that $\ker(\AA)^{\perp} = \im(\AA^{\trp})$.
\end{definition}
\begin{claim}
  For a matrix $\AA \in \symsetn$ with spectral decomposition  $\AA =
  \VV\LLambda\VV^\trp = \sum_{i} \lambda_i \vv_i \vv_i^{\trp}$ s.t. $\VV^\trp \VV = \II$,
  we have $\PPi_{\AA} = \sum_{i : \lambda_i \neq 0} \vv_i \vv_i^{\trp}$, and
  $\PPi_{\AA} = \AA^{\pinv/2}\AA\AA^{\pinv/2} = \AA\AA^{\pinv} =  \AA^{\pinv}\AA $.
\end{claim}
From the definition, we can see that
$\PPi_{\LL}= \II - \frac{1}{n}\vecone\vecone^{\trp}$.

Now that we understand the projection $\PPi_{\LL}$, it is not hard to
show the following claim.
\begin{claim}
  \label{clm:spectralreltoabs}
  \noindent
\begin{enumerate}
\item
\label{enu:switchapxnormalizer}
  $\PPi_{\LL} \approx_{\epsilon} \LL^{\pinv/2}\LLtil\LL^{\pinv/2}$
  implies $\LL \approx_{\epsilon} \LLtil$.
\item
  \label{enu:normtoapx}
  For $\epsilon \leq 1$, we have that
  $\norm{\PPi_{\LL} - \LL^{\pinv/2}\LLtil\LL^{\pinv/2}} \leq
  \epsilon/2$ implies  $\PPi_{\LL} \approx_{\epsilon} \LL^{\pinv/2}\LLtil\LL^{\pinv/2}$.
\end{enumerate}
\end{claim}
Really, the only idea needed here is that when comparing quadratic
forms in matrices with
the same kernel, we necessarily can't have the quadratic forms
disagree on vectors in the kernel.
Simple! But we are going to write it out carefully, since we're still
getting used to these types of calculations.
\begin{proof}[Proof of Claim~\ref{clm:spectralreltoabs}]
  To prove Part~\ref{enu:switchapxnormalizer}, we
  assume $\PPi_{\LL} \approx_{\epsilon}
  \LL^{\pinv/2}\LLtil\LL^{\pinv/2}$.
  Recall that $G$ is a connected graph, so $\ker(\LL) =
  \Span\setof{\vecone}$, while $\LLtil$ is the Laplacian of a graph
which may or may not be connected, so $\ker(\LLtil) \supseteq
\ker(\LL)$, and equivalently $\im(\LLtil) \subseteq \im(\LL)$.
  Now, for any $\vv \in \ker(\LL)$ we have
  $\vv^{\trp} \LLtil \vv = 0 = \vv^{\trp} \LL \vv$.
  For any $\vv \in \ker(\LL)^{\perp}$ we have
  $\vv = \LL^{\pinv/2} \zz$ for some $\zz$, as $\ker(\LL)^{\perp} =
  \im(\LL) = \im(\LL^{\pinv/2})$.
  Hence
  \[
    \vv^{\trp} \LLtil \vv =
    \zz^{\trp} \LL^{\pinv/2}\LLtil\LL^{\pinv/2} \zz
    \geq
    \frac{1}{1+\epsilon}
     \zz^{\trp} \LL^{\pinv/2}\LL\LL^{\pinv/2} \zz
     =
     \frac{1}{1+\epsilon}
     \vv^{\trp} \LL \vv
   \]
and similarly
    \[
    \vv^{\trp} \LLtil \vv =
    \zz^{\trp} \LL^{\pinv/2}\LLtil\LL^{\pinv/2} \zz
    \leq
    (1+\epsilon)
     \zz^{\trp} \LL^{\pinv/2}\LL\LL^{\pinv/2} \zz
     =
    (1+\epsilon)
    \vv^{\trp} \LL \vv
    .
  \]
  Thus we have established  $\LL \approx_{\epsilon} \LLtil$.

  To prove Part~\ref{enu:normtoapx}, we assume $\norm{\PPi_{\LL} - \LL^{\pinv/2}\LLtil\LL^{\pinv/2}} \leq
  \epsilon/2$.
  This is equivalent to
  \[
   - \frac{\epsilon}{2} \II \preceq
    \LL^{\pinv/2}\LLtil\LL^{\pinv/2} - \PPi_{\LL} \preceq \frac{\epsilon}{2} \II
    \]
 But since
  \[
    \vecone^{\trp} (\LL^{\pinv/2}\LLtil\LL^{\pinv/2} - \PPi_{\LL})
    \vecone = 0,
  \]
  we can in fact sharpen this to
    \[
      - \frac{\epsilon}{2} \PPi_{\LL}
      \preceq
      \LL^{\pinv/2}\LLtil\LL^{\pinv/2} - \PPi_{\LL}
      \preceq
    \frac{\epsilon}{2} \PPi_{\LL}
    .
    \]
 Rearranging, we then conclude
 \[
   (1-\frac{\epsilon}{2}) \PPi_{\LL}
   \preceq
   \LL^{\pinv/2}\LLtil\LL^{\pinv/2}
    \preceq
    (1+\frac{\epsilon}{2}) \PPi_{\LL}
    .
  \]
Finally, we note that $1/(1+\epsilon) \leq (1-\frac{\epsilon}{2})$ to
reach our conclusion, $\PPi_{\LL} \approx_{\epsilon} \LL^{\pinv/2}\LLtil\LL^{\pinv/2}$.
\end{proof}

We now have most of the tools to prove
Theorem~\ref{thm:spectralgraphapxbysamp}, but to help us, we are going
to establish one small piece of helpful notation:
We define a matrix function $\Phi : \R^{n\times n} \to
\R^{n\times n} $
by
\[
  \Phi(\AA) = \LL^{\pinv/2}\AA\LL^{\pinv/2}.
  \]
We sometimes call this a ``normalizing map'', because it transforms a matrix to
the space where spectral norm bounds can be translated into relative
error guarantees compare to the $\LL$ quadratic form.
\begin{proof}[Proof of Theorem~\ref{thm:spectralgraphapxbysamp}]
  By Claim~\ref{clm:spectralreltoabs}, it suffices to show
  \begin{equation}
    \label{eq:spectralnormerrbound}
  \norm{\PPi_{\LL} - \LL^{\pinv/2}\LLtil\LL^{\pinv/2}} \leq
  \epsilon/2.
\end{equation}
  We introduce a set of independent random variables, one for each
  edge $e$, with a probability $p_e$ associated with the edge which we
  will fix later.
 We then let
  \[
    \YY_e =
    \begin{cases}
      \frac{\ww(e)}{p_e} \bb_e \bb_e^{\trp}
      & \text{ with probability } p_e
      \\
      \matzero
      & \text{ otherwise. }
    \end{cases}
  \]

This way, $\LLtil = \sum_e \YY_e$.
Note that $\E{\YY_e} = p_e \frac{\ww(e)}{p_e} \bb_e \bb_e^{\trp} =
\ww(e) \bb_e \bb_e^{\trp}$, and so
\[
  \E{\LLtil} =\sum_e \E{\YY_e} = \LL
  .
\]
By linearity of $\Phi$,
\[
\E{\Phi(\LLtil)} =  \Phi(\E{\LLtil}) = \PPi_{\LL}.
\]
Let us also define
\[
  \XX_e = \Phi(\YY_e) - \E {\Phi(\YY_e)} \text{ and } \XX = \sum_e \XX_e
\]
Note that this ensures $\E {\XX_e} = \matzero$.
We are now going to fix the edge sampling probabilities, in a way that
depends on some overall scaling parameter $\alpha > 0$.
We let
\[
  p_e = \min\left( \alpha \norm{\Phi\left(\ww(e)
      \bb_e\bb_e^{\trp} \right)}, 1 \right)
\]
then we see from the definition of $\YY_e$ that whenever $p_e < 1$
\[
  \norm{\Phi(\YY_e) } \leq \frac{1}{\alpha}
\]
from this, we can conclude, with a bit of work, that for all $e$
\begin{equation}
  \label{eq:bernsteinelementnormbound}
  \norm{\XX_e} \leq \frac{1}{\alpha}.
\end{equation}
We can also show that
\begin{equation}
  \label{eq:bernsteinvarnormbound}
  \norm{\sum_e \E{\XX_e^2}} \leq \frac{1}{\alpha}
  .
\end{equation}
In the exercises for this chapter, we will ask you to show that
Equations~\eqref{eq:bernsteinelementnormbound} and~\eqref{eq:bernsteinvarnormbound} holds.

This means that we can apply Theorem~\ref{thm:matbernstein} to our
$\XX = \sum_e \XX_e$, with $R =  \frac{1}{\alpha}$ and $\sigma^2 =
\frac{1}{\alpha}$, to get
\[
  \Pr\left[\norm{\PPi_{\LL} - \LL^{\pinv/2}\LLtil\LL^{\pinv/2}}
  \geq  \epsilon/2\right]
  \leq 2n\exp\left(\frac{-0.25 \epsilon^2}{(\epsilon +
      4)/\alpha}\right)
\]
Since $0 < \epsilon < 1$, this means that if
$\alpha = 40 \epsilon^{-2}  \log(n/\delta)$,
then
\[
  Pr\left[\norm{\PPi_{\LL} - \LL^{\pinv/2}\LLtil\LL^{\pinv/2}}
    \geq  \epsilon/2\right]
\leq \frac{2n\delta^2}{n^2} \leq \delta/2.
\]
In the last step, we assumed $n \geq 4$.

Lastly, we'd like to know that the graph $\Gtil$ is sparse.
The number of edges in $\Gtil$ is equal to the number of $\YY_e$ that
come out nonzero.
Thus, the expected value of $\abs{\Etil}$ is
\[
  \E{\abs{\Etil}}
  =
  \sum_e p_e \leq \alpha \sum_e \ww(e) \norm{\LL^{\pinv/2}\bb_e\bb_e^{\trp}\LL^{\pinv/2}}
\]
We can bound the sum of the norms with a neat trick relating it to the
trace of $\PPi_{\LL}$.
Note that in general for a vector $\aa \in \R^n$, we have
$\norm{\aa\aa^{\trp}} = \aa^{\trp}\aa = \trace{\aa\aa^{\trp}}$.
And hence
\begin{align*}
  \sum_e
  \ww(e)
  \norm{\LL^{\pinv/2}\bb_e\bb_e^{\trp}\LL^{\pinv/2}}
  &=
  \sum_e
  \ww(e)
  \trace{\LL^{\pinv/2}\bb_e\bb_e^{\trp}\LL^{\pinv/2}}
  \\
  &=
  \trace{ \LL^{\pinv/2}  \left( \sum_e\ww(e) \bb_e\bb_e^{\trp}
    \right)\LL^{\pinv/2}}
  \\
  &= \trace{ \PPi_{\LL} } = n-1.
\end{align*}
Thus with our choice of $\alpha$,
\[
  \E{\abs{\Etil}} \leq 40 \epsilon^{-2}  \log(n/\delta) n.
\]
With a scalar Chernoff bound, can show that $\abs{\Etil} \leq O( \epsilon^{-2}  \log(n/\delta) n)$
with probability at least $1-\delta/2$.
Thus by a union bound, the this condition and
Equation~\eqref{eq:spectralnormerrbound} are both satisfied with
probability at least $1-\delta$.
\end{proof}

\begin{remark}
  Note that
  \[
    \norm{\Phi\left(\ww(e) \bb_e\bb_e^{\trp} \right)}
    =
    \ww(e)
   \norm{\LL^{\pinv/2}\bb_e\bb_e^{\trp}\LL^{\pinv/2}}
  \leq
  \ww(e)\norm{ \LL^{\pinv/2}\bb_e}_2^2.
  \]
  Recall that in Chapter~\ref{cha:pinver}, we saw that the effective between vertex
  $v$ and vertex $u$ is given by
  $\norm{ \LL^{\pinv/2}(\ee_u - \ee_v)}_2^2$, and for an edge $e$
  connecting vertex $u$ and $v$, we have $\bb_{e}= \ee_u - \ee_v$.
 That means the norm of the ``baby Laplacian'' $\ww(e) \bb_e\bb_e^{\trp}$ of a single edge with weight
  $\ww(e)$ is exactly $\ww(e)$ times the effective resistance between
  the two endpoints of the edge.
\end{remark}

We haven't shown how to compute the sampling probabilities
efficiently, so right now, it isn't clear whether we can efficiently
find $\Gtil$.
It turns out that if we have access to a fast algorithm for solving
Laplacian linear equations, then we can find sufficiently good
approximations to the effective resistances quickly, and use these to
compute $\Gtil$.
An algorithm for this is described in \cite{spielman2011graph}.

\FloatBarrier

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "agao21_script"
%%% TeX-engine: luatex
%%% End:


\documentclass[letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}

\usepackage{parskip}
\usepackage{enumitem}
\usepackage[table]{xcolor}
\usepackage{siunitx}

\usepackage[T1]{fontenc}
\usepackage[scaled=1.0]{DejaVuSansMono}
\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.965,0.973,0.980}
\lstdefinestyle{Default-Custom-Style}{
	backgroundcolor=\color{backcolour},
	xleftmargin=0.5cm,
	frame=tlbr,
	framesep=8pt,
	framerule=0pt,
	%commentstyle=\color{codegreen},
	%keywordstyle=\color{magenta},
	%numberstyle=\tiny\color{codegray},
	%stringstyle=\color{codepurple},
	basicstyle=\footnotesize\ttfamily,
	breakatwhitespace=false,
	breaklines=true,
	captionpos=b,
	keepspaces=true,
	numbers=none,
	numbersep=5pt,
	showspaces=false,
	showstringspaces=false,
	showtabs=false,
	tabsize=2
}
\lstset{style=Default-Custom-Style}

\author{Jacob Huesman}
\title{Using difference equations to analyze a financial problem}
\begin{document}
\maketitle

\section{Introduction}
Like differential equations, difference equations can be used to model the behavior of a system. The main difference between the two being that differential equations are used to describe continuous time systems and difference equations are used to describe discrete time systems. Like the Laplace transform of a differential equation, the z-transform of a difference equation can be used to simplify solving a linear time-invariant system. This is very useful, as digital systems represent information in a discrete fashion, and there's also systems that are easier to model in discrete steps rather than as a continuous function.

This report will present an example of the application of the z-transform in modeling and solving a financial problem. The problem has been adapted from a problem posed by Dr. Green in his Signals and Systems class.

\section{Problem}
Suppose you would like to retire with 10 million dollars in savings. To keep the difference equations simple, let's say that you invest uniform monthly payments for a fixed number of years and that you receive a fixed annual percent yield. Let the interest be compounded monthly. Solve this problem with z-transform techniques for three different interest rates (low, medium, high), over three different time intervals (short, average, long). For each interest rate and investment duration, compute the required (fixed) monthly payments $C_{i,D}$ needed to realize the 10 million dollar retirement goal.

\section{Solution}
\subsection{Interest Rate and Duration}
We start by picking three realistic annual percentage rates reflective of different investments:
\begin{align*}
	i_{low}  &= \SI{0.5}{\%}  \\
	i_{med}  &= \SI{6.0}{\%}  \\
	i_{high} &= \SI{10.5}{\%}
\end{align*}

We define our investment time intervals as:
\begin{align*}
	D_{short} &= \SI{16}{years}  \\
	D_{med}   &= \SI{32}{years}  \\
	D_{long}  &= \SI{48}{years}
\end{align*}

\pagebreak
\subsection{Difference Equation}
Let $n=0$ correspond to the first investment contribution, let $n = 12D -1$ be the last investment contribution, and let $n = 12D$ be the time when the retirement account is to have the desired 10 million dollars in savings. Our difference equation is then
\begin{align}
	y[n] &= \left(1 + \frac{i}{100} \right)^{\frac{1}{12}} y[n-1] + C (u[n] - u[n-12D]), \label{eq1}
\end{align}
where $y[n]$ is the balance at month $n$, $\left(1 + \frac{i}{100} \right)^{\frac{1}{12}} y[n-1]$ is last month's balance plus the accrued interest, and $C(u[n] - u[n-12D])$ is the payments made each month over the specified duration. For the payments made each month, we had to include some unit step functions to handle the last month where no payment is made, but interest is still accrued.

\subsection{Z-Transform}

Now putting equation \ref{eq1} in standard form and simplifying we get,
\begin{align}
	y[n] - \left(1 + \frac{i}{100} \right)^{\frac{1}{12}} y[n-1] &= C (u[n] - u[n-12D]) \\
	\left(1 - \left(1 + \frac{i}{100} \right)^{\frac{1}{12}}E^{-1}\right)y[n] &= C (u[n] - u[n-12D]). \label{eq2}
\end{align}
Note that $E^{-1}$ is used to denote a delay operation. Having put equation \ref{eq1} in a more standard form we can take the z-transform of both sides,
\begin{align}
	\left(1 - \left(1 + \frac{i}{100} \right)^{\frac{1}{12}}z^{-1}\right)Y(z) &= C \left(\frac{z}{z-1} - \frac{z^{-12D+1}}{z-1} \right).
\end{align}

Which can be written as
\begin{align}
	Y(z) &=  C\left(\frac{\frac{z}{z-1} - \frac{z^{-12D+1}}{z-1}}
	{1 - \left(1 + \frac{i}{100} \right)^{\frac{1}{12}}z^{-1}}\right) \\
	     &=  C\left(\frac{z - z^{-12D+1}}{(z-1)(1 - p z^{-1})}\right), \label{eq3}
\end{align}
where
\begin{align}
	p = \left(1 + \frac{i}{100} \right)^{\frac{1}{12}}.
\end{align}
Note that a delay in the time domain corresponds to a negative power of z in the z-domain by the time shifting property:
\begin{align}
	\text{If }m>0: x[n-m]u[n-m] \stackrel{Z_u}{\Longleftrightarrow} z^{-m} X(z) \label{prop1}
\end{align}
We also used one z-transform pair:
\begin{align}
	&\gamma^nu[n] \stackrel{Z_u}{\Longleftrightarrow} \frac{z}{z-\gamma} \quad\text{ROC: } |z|>|\gamma| \label{tp1}
\end{align}

\subsection{Modified Partial Fraction Expansion}
Like solving differential equations with the Laplace transform, typically difference equations are solved by breaking the z-transformed equation into smaller pieces and then referencing a table of z-transform pairs to bring the equation back into the time domain. The process of breaking the equation into smaller pieces is known as the modified partial fraction expansion. It's the same as the partial fraction expansion used in Laplace transforms except since most z-transform tables require a $z$ in the numerator, we pull a $z$ out of the equation before performing the expansion, then return it after the expansion is complete.

We'll simplify (\ref{eq3}) and pull out the z before performing the expansion
\begin{align}
	\frac{Y(z)}{z} &= C \left( \frac{z - z^{-12D+1}}{z(z-1)(1 - pz^{-1})} \right) \\
	&= C \left( \frac{z - z^{-12D+1}}{(z-1)(z - p)} \right).
\end{align}
Let
\begin{align}
	\frac{Y_1(z)}{z} &= \frac{z}{(z-1)(z - p)}, \\
	\frac{Y_2(z)}{z} &= \frac{z^{-12D+1}}{(z-1)(z - p)},
\end{align}
then
\begin{align}
	\frac{Y(z)}{z} &= C \left( \frac{Y_1(z)}{z} - \frac{Y_2(z)}{z} \right).
\end{align}

We start by solving for $Y_1(z)$,
\begin{align}
	\frac{Y_1(z)}{z} &= \frac{z}{(z-1)(z - p)} \\
					 &= \frac{A}{z-1} + \frac{B}{z-p}.
\end{align}

Using the Heaviside cover-up method
\begin{align}
	A &= \frac{1}{1 - p}, \\
	B &= \frac{p}{p - 1},
\end{align}
so
\begin{align}
	\frac{Y_1(z)}{z} &= \left(\frac{1}{1 - p}\right)\left(\frac{1}{z-1}\right)
						+ \left(\frac{p}{p - 1}\right)\left(\frac{1}{z-p}\right).
\end{align}
Now in order to use (\ref{tp1}) we need a $z$ in the numerator of each component of the sum, so we move the $z$ on the LHS back over,
\begin{align}
	Y_1(z) &= \left(\frac{1}{1 - p}\right)\left(\frac{z}{z-1}\right)
	+ \left(\frac{p}{p - 1}\right)\left(\frac{z}{z-p}\right).
\end{align}

\pagebreak
\subsection{Inverse Z-Transform}
Using (\ref{tp1}) we can find the inverse z-transform of $Y_1(z)$,
\begin{align}
	Z^{-1}\left[Y_1(z)\right] &= \frac{1}{1 - p}u[n] + \frac{p}{p - 1}p^n u[n] \\
							  &= \left(\frac{1}{1 - p} + \frac{p}{p - 1}p^n \right)u[n] \\
							  &= \left(\frac{1}{p - 1}p^{n+1} - \frac{1}{p - 1} \right)u[n] \\
							  &= \frac{1}{p - 1}\left(p^{n+1} - 1 \right)u[n] \\
							  &= y_1[n].
\end{align}

Having found $y_1[n]$ we can use the time shift property (\ref{prop1}) to find $y_2[n]$. First note that we originally wrote $Y_2(z)$ as
\begin{align}
	\frac{Y_2(z)}{z} &= \frac{z^{-12D+1}}{(z-1)(z - p)}.
\end{align}

However, since we have already done the modified partial fraction decomposition on $Y_1(z)$ and will be using properties to find $Y_2(z)$, we no longer need $Y_1(z)$, $Y_2(z)$, or $Y(z)$ in that form, so we write
\begin{align}
	Y_1(z) &= \frac{z^2}{(z-1)(z - p)} \\
	Y_2(z) &= \frac{z^{-12D+2}}{(z-1)(z - p)} \\
	Y(z)   &= C \left(Y_1(z) - Y_2(z) \right).
\end{align}

Notice
\begin{align}
	Y_2(z) &= \frac{z^{-12D+2}}{(z-1)(z - p)} \\
	       &= \frac{z^{2}}{(z-1)(z - p)}z^{-12D} \\
	       &= Y_1(z) z^{-12D}.
\end{align}

We can then use the time shift property (\ref{prop1}) to find $y_2[n]$,
\begin{align}
	Z^{-1}\left[Y_2\right] &= Z^{-1}\left[Y_1(z) z^{-12D}\right] \\
						   &= \frac{1}{p - 1}\left(p^{n+1-12D} - 1 \right)u[n-12D] \\
						   &= y_2[n].
\end{align}

Having found both $y_1[n]$ and $y_2[n]$ we can take advantage of linearity to find $y[n]$. The z-transform is a linear transformation, so
\begin{align}
	ax[n] + by[n] \stackrel{Z_u}{\Longleftrightarrow} aX(z) + bY(z), \label{prop2}
\end{align}

which means that
\begin{align}
	C \left(Y_1(z) - Y_2(z) \right) &\stackrel{Z_u}{\Longleftrightarrow} C \left(y_1[n] - y_2[n] \right), \\
	Y(z) &\stackrel{Z_u}{\Longleftrightarrow} y[n].
\end{align}

So using (\ref{prop2})
\begin{align}
	y[n] &= C(y_1[n] - y_2[n]) \\
		 &= C \left(\frac{1}{p - 1}\left(p^{n+1} - 1 \right)u[n] - \frac{1}{p - 1}\left(p^{n+1-12D} - 1 \right)u[n-12D] \right) \\
		 &= \frac{C}{p - 1}\left(\left(p^{n+1} - 1 \right)u[n] - \left(p^{n+1-12D} - 1 \right)u[n-12D] \right). \label{eqny}
\end{align}

We now have a solution to our investment problem for an arbitrary monthly payment amount, interest, and investment duration. A pretty cool application of the z-transform.

\subsection{Monthly payments}
Equation (\ref{eqny}) is useful if we want to see what our projected balance would be on a given month. However we are interested in our monthly payments, given a goal, interest, and duration. So we'll rearrange (\ref{eqny}) as
\begin{align}
	C &= \frac{y[n](p - 1)}{\left(p^{n+1} - 1 \right)u[n] - \left(p^{n+1-12D} - 1 			\right)u[n-12D]} \\
	  &= \frac{y[12D](p - 1)}{\left(p^{12D+1} - 1 \right)u[12D] - \left(p^{12D+1-12D} - 1 \right)u[12D-12D]} \\
	  &= \frac{\SI{10}{M}(p - 1)}{p^{12D+1} - 1 - p^1 + 1} \\
	  &= \frac{\SI{10}{M}(p - 1)}{p\left(p^{12D} - 1\right)}
\end{align}

Having solved for $C$ we use MATLAB to find $C_{i,D}$ given our three interests and durations:
\begin{table}[h]
	\centering
	\begin{tabular}{|r|r|r|r|}
		\hline
		D, i & 0.5\% & 6\% & 10.5\% \\
		\hline
		16 years & \$50,022.43 & \$31,447.18 & \$21,026.04 \\
		\hline
		32 years & \$24,013.78 & \$8,882.5 & \$3,539.26 \\
		\hline
		48 years & \$15,362.61 & \$3,146.68 & \$692.73 \\
		\hline
	\end{tabular}
	\caption[Table 1:]{Monthly contribution for particular APYs and investment durations}
\end{table}

\pagebreak
\subsection{Investment Growth}
Looking at the investment growth with different APYs and investment durations shows pretty clearly that of the two factors we have some control over, duration is the more important one, it also happens to be the one we have the most control over.
\begin{center}
	\includegraphics[width=1\linewidth]{../matlab/investment_growth.png}
\end{center}

\section{Conclusion}
This  was a fairly simple example, but the techniques demonstrated here can be used to solve much more complex financial problems. You just need to be able to write it out as a difference equation and employ the techniques demonstrated above.

Handling finances intelligently is important for long term success. Although using the z-transform is not the only way, or really even an obvious way, it's a fairly simple tool that can be employed to quickly solve a financial problem.

\pagebreak
\section{Appendix}
\subsection{MATLAB Code}
\begin{lstlisting}[language=MATLAB]
clear;

i = [0.5, 6.0, 10.5];
D = [16, 32, 48];
Y_r = 10E6;

p = (1 + i./100).^(1/12);

C = (Y_r .* (p - 1)) ./ (p .* (p.^(12.*D') - 1));
P = 12.*D'.*C;

u = @(t) 1.0 .* (t >= 0);
y = @(n, C, p, D) C ./ (p - 1) .* ( (p.^(n + 1) - 1).*u(n) - (p.^(n + 1 - 12*D) - 1).*u(n - 12*D));
for j = 1:1:3
	n = 0:1:(D(j)*12);
	P_t = (n+1) .* C(j,:)';
	y_t = y(n, C(j,:)', p', D(j));
	for k = 1:1:3
		subplot(3,3,((j-1)*3+k-1)+1);
		plot(n, y_t(k,:), '-', n, P_t(k,:), '--', 'color', [0,0,0], 'linewidth', 1);
		title("APY of " + string(i(k))+"% over " + string(D(j)) + " years");
		legend('Value of Account', 'Principle Invested', 'Location','northwest');
		xlabel('Months');
		grid on;
	end
end
\end{lstlisting}

\end{document}


% ==============================================================================
%
%                          F I L T E R   D E S I G N
%
% ==============================================================================
\chapter{Filter Design} % ---------------------------------------------------- %
\label{ch:app:fdesign}
% ---------------------------------------------------------------------------- %

Some  additional information  about  the filter  design  process is  presented
here. This  comprises  a graphical  comparison  between  two variants  of  the
$R=625$  filter  chain,  measurements  for  resource  usage  on  the  FPGA  in
relation  to  filter  size,  and  the  frequency  responses  for  all  filters
and  filter  chains  as  designed  by Matlab  (for  measurement  results,  see
Chapter~\ref{ch:verification}). For studying  the filters  and chains  in more
detail, the reader is referred to the design files themselves.

% ==============================================================================
%
%                        D E C 6 2 5   V A R I A N T S
%
% ==============================================================================
\section{Decimation of 625: Variants} % <<< ---------------------------------- %
\label{sec:dec625_variants}
% ---------------------------------------------------------------------------- %

There are many possible  ways to implement a filter chain  for a given overall
downsampling  ratio $R$. Here,  two  possibilities are  compared for  $R=625$,
consisting  of  a  $125  \rightarrow  5$  cascade  and  a  $25  \rightarrow  5
\rightarrow 5$ cascade. In  both cascades, the initial stage is  a CIC filter,
followed by  a FIR compensator  and one or  more FIR filters. The  results are
shown in Figure~\ref{fig:dec625_variants}.

\begin{figure}
    \centering
    \input{images/fdesign/dec625Variants.tikz}
    \caption[Decimation Chain Variants for Rate of 625]{%
        Semilog  (top)  and   linear  plot  (middle)  for   two  variants  for
        implementing  a  decimation   chain  for  a  rate   change  factor  of
        $R=625$. Both choices  show almost the  same behavior with  regards to
        magnitude. The  bottom  plot  shows  the phase  response  of  the  two
        filters; here, too, the behavior  is very similar. \emph{Note:}  These
        plots show  the frequency responses  of the entire filter  cascade for
        the two respective variants.%
    }
    \label{fig:dec625_variants}
\end{figure}

%>>>
\clearpage
% ==============================================================================
%
%                         R E S O U R C E   U S A G E
%
% ==============================================================================
\section{Resource Usage for FIR Filters on the FPGA} % <<< ------------------- %
\label{sec:fir_filter_resouce_usage}
% ---------------------------------------------------------------------------- %

Measurements   on    resource   usage    for   various   FIR    filters   have
been   performed    and   are    presented   here. The   objective    was   to
determine   how   many   DSP   slices   a  given   FIR   filter   needs   when
implemented   with  Xilinx's   FIR  Compiler~\cite{xilinx:fir-compiler}. These
figures   form   the    basis   to   define   reasonable    bounds   for   the
FIR   filter   \code{5steep}  (see   Figure~   \ref{fig:fdesign:chain_concept}
on    page~\pageref{fig:fdesign:chain_concept}). Figure~\ref{fig:usage_report}
depicts      the       results      of      the       measurements,      while
Table~\ref{tab:usage_report:config}  contains   the  configuration  parameters
which were used for the FIR compiler core.

As can be  seen in the plot,  DSP slice usage rises roughly  linearly at these
high sampling  rates. When using  only a  single filter,  a filter  of roughly
\num{760} coefficients is  the maximum possible size. Because  the STEMlab has
two channels, and  because other filters are required as  well, an upper limit
for \code{5steep} of  \num{250} coefficients is set based  on these results. A
smaller  filter  is  also  acceptable  as long  as  it  fulfills  the  general
requirements.

\vfill
\begin{figure}[h]
    \centering
    \input{images/fdesign/usageReport.tikz}
    \caption[Usage Report FIR Compiler]{%
        Usage  report figures  for DSP  slices using  the Xilinx  FIR compiler
        block. The  configuration  of  the  filter in  terms  of  bit  widths
        is  identical   to  the  actual   configuration  used  in   the  final
        implementation. Unlike  the  implementation,  however, only  a  single
        channel was configured.%
    }
    \label{fig:usage_report}
\end{figure}

\vfill
\begin{table}[h]
    \centering
    \caption[FIR Compiler Parameters]{%
        The parameters used  to configure the FIR compiler core  for the usage
        measurements from Figure~\ref{fig:usage_report}%
    }
    \label{tab:usage_report:config}
    \begin{tabular}{lr}
        \toprule
        Parameter                   & Value          \\
        \midrule
        Clock Frequency             & \SI{125}{\MHz} \\
        Decimation Rate             & \num{5}        \\
        Input Data Width            & \SI{24}{\bit}  \\
        Input Fractional Bits       & \num{7}        \\
        Output Data Width           & \SI{32}{\bit}  \\
        Coefficient Fractional Bits & \num{17}       \\
        \bottomrule
    \end{tabular}
\end{table}
\vfill
%>>>
\clearpage
% ==============================================================================
%
%             F I L T E R   F R E Q U E N C Y   R E S P O N S E S
%
% ==============================================================================
\section{Filter Frequency Responses} % <<< ----------------------------------- %
\label{sec:filter_frequency_responses}
% ---------------------------------------------------------------------------- %

This  section   lists  the  frequency   responses  of  the  filters   and  the
cascades as specified  in Section~\ref{ch:filter_design}. For detailled filter
specifications, see the appropriate chapters  and/or the filter design code in
the repository.

% ==============================================================================
%
%                                 5 S T E E P
%
% ==============================================================================
\subsection{\code{5steep}} % <<< --------------------------------------------- %
\label{sec:filter_frequency_responses:5steep}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/5steep.tikz}
%>>>
% ==============================================================================
%
%                                  5 F L A T
%
% ==============================================================================
\subsection{\code{5flat}} % <<< ---------------------------------------------- %
\label{sec:filter_frequency_responses:5flat}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/5flat.tikz}
%>>>
% ==============================================================================
%
%                                 2 S T E E P
%
% ==============================================================================
\subsection{\code{2steep}} % <<< --------------------------------------------- %
\label{sec:filter_frequency_responses:2steep}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/2steep.tikz}
%>>>
% ==============================================================================
%
%                                  C I C 2 5
%
% ==============================================================================
\subsection{\code{CIC25}} % <<< ---------------------------------------------- %
\label{sec:filter_frequency_responses:cic25}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/cic25.tikz}
%>>>
% ==============================================================================
%
%                                 C F I R 2 5
%
% ==============================================================================
\subsection{\code{CFIR25}} % <<< --------------------------------------------- %
\label{sec:filter_frequency_responses:cfir25}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/cfir25.tikz}
%>>>
% ==============================================================================
%
%                                 C I C 1 2 5
%
% ==============================================================================
\subsection{\code{CIC125}} % <<< --------------------------------------------- %
\label{sec:filter_frequency_responses:cic125}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/cic125.tikz}
%>>>
% ==============================================================================
%
%                                C F I R 1 2 5
%
% ==============================================================================
\subsection{\code{CFIR125}} % <<< -------------------------------------------- %
\label{sec:filter_frequency_responses:cfir125}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/cfir125.tikz}
%>>>
% ==============================================================================
%
%                       C H A I N   F O R   R   =   2 5
%
% ==============================================================================
\subsection{Chain for R=25} % <<< -------------------------------------------- %
\label{sec:filter_frequency_responses:chain25}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/chain25.tikz}
%>>>
% ==============================================================================
%
%                      C H A I N   F O R   R   =  1 2 5
%
% ==============================================================================
\subsection{Chain for R=125} % <<< ------------------------------------------- %
\label{sec:filter_frequency_responses:chain125}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/chain125.tikz}
%>>>
% ==============================================================================
%
%                      C H A I N   F O R   R   =  6 2 5
%
% ==============================================================================
\subsection{Chain for R=625} % <<< ------------------------------------------- %
\label{sec:filter_frequency_responses:chain625}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/chain625.tikz}
%>>>
% ==============================================================================
%
%                     C H A I N   F O R   R   =  1 2 5 0
%
% ==============================================================================
\subsection{Chain for R=1250} % <<< ------------------------------------------ %
\label{sec:filter_frequency_responses:chain1250}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/chain1250.tikz}
%>>>
% ==============================================================================
%
%                     C H A I N   F O R   R   =  2 5 0 0
%
% ==============================================================================
\subsection{Chain for R=2500} % <<< ------------------------------------------ %
\label{sec:filter_frequency_responses:chain2500}
% ---------------------------------------------------------------------------- %
\input{images/fdesign/chain2500.tikz}
%>>>
%>>>

%^^A vim: foldenable foldcolumn=4 foldmethod=marker foldmarker=<<<,>>>


% Modified based on Xiaoming Sun's template
\documentclass{article}
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\usepackage{setspace}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{extramarks}
\usepackage{chngpage}
\usepackage{soul,color}
\usepackage{graphicx,float,wrapfig}

\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
}

\newcommand{\Class}{Mathematics for Computer Science}

% Homework Specific Information. Change it to your own
\newcommand{\Title}{Homework 3}

% In case you need to adjust margins:
\topmargin=-0.45in      %
\evensidemargin=0in     %
\oddsidemargin=0in      %
\textwidth=6.5in        %
\textheight=9.0in       %
\headsep=0.25in         %

% Setup the header and footer
\pagestyle{fancy}                                                       %
\chead{\Title}  %
\rhead{\firstxmark}                                                     %
\lfoot{\lastxmark}                                                      %
\cfoot{}                                                                %
\rfoot{Page\ \thepage\ of\ \protect\pageref{LastPage}}                          %
\renewcommand\headrulewidth{0.4pt}                                      %
\renewcommand\footrulewidth{0.4pt}                                      %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Some tools
\newcommand{\enterProblemHeader}[1]{\nobreak\extramarks{#1}{#1 continued on next page\ldots}\nobreak%
                                    \nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak}%
\newcommand{\exitProblemHeader}[1]{\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak%
                                   \nobreak\extramarks{#1}{}\nobreak}%

\newcommand{\homeworkProblemName}{}%
\newcounter{homeworkProblemCounter}%
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter}]%
  {\stepcounter{homeworkProblemCounter}%
   \renewcommand{\homeworkProblemName}{#1}%
   \section*{\homeworkProblemName}%
   \enterProblemHeader{\homeworkProblemName}}%
  {\exitProblemHeader{\homeworkProblemName}}%

\newcommand{\homeworkSectionName}{}%
\newlength{\homeworkSectionLabelLength}{}%
\newenvironment{homeworkSection}[1]%
  {% We put this space here to make sure we're not connected to the above.

   \renewcommand{\homeworkSectionName}{#1}%
   \settowidth{\homeworkSectionLabelLength}{\homeworkSectionName}%
   \addtolength{\homeworkSectionLabelLength}{0.25in}%
   \changetext{}{-\homeworkSectionLabelLength}{}{}{}%
   \subsection*{\homeworkSectionName}%
   \enterProblemHeader{\homeworkProblemName\ [\homeworkSectionName]}}%
  {\enterProblemHeader{\homeworkProblemName}%

   % We put the blank space above in order to make sure this margin
   % change doesn't happen too soon.
   \changetext{}{+\homeworkSectionLabelLength}{}{}{}}%

\newcommand{\Answer}{\ \\\textbf{Answer:} }
\newcommand{\Acknowledgement}[1]{\ \\{\bf Acknowledgement:} #1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make title
\title{\textmd{\bf \Class: \Title}}
\date{March 18, 2019}
\author{Xingyu Su 2015010697}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{spacing}{1.1}
\maketitle \thispagestyle{empty}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Begin edit from here

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[LPV 5.4.3]

Let $A$ and $B$ be independent events. Express the probability $P(A \cup B)$ in terms of the probabilities of $A$ and $B$.

\Answer

Because $A$ and $B$ are independent events, so $P(A \cap B) = 0$.

Then $P(A\cup B) = P(A)+P(B)-P(A\cap B)=P(A)+P(B)$

\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[LPV 5.4.4]

We select a subset $X$ of the set $S = \{1, 2, \dots , 100\}$ randomly and uniformly (so that every subset has the same probability of being selected). What is the probability that

(a) X has an even number of elements;

(b) both 1 and 100 belong to X;

(c) the largest element of X is 50;

(d) X has at most 2 elements.

\Answer

(a)
\begin{equation*}
P(a) = \frac{1}{2^{100}}\left(\binom{0}{100}+\binom{2}{100}+\cdots+\binom{98}{100}+\binom{100}{100}\right) = \frac{1}{2}
\end{equation*}

(b)
\begin{equation*}
P(b) = \frac{2^{98}}{2^{100}}=\frac{1}{4}
\end{equation*}

(c)
\begin{equation*}
P(c) = \frac{2^{49}}{2^{100}}=\frac{1}{2^{51}}\approx 4.44\times 10^{-16}
\end{equation*}

(d)
\begin{equation*}
P(d) = \frac{\binom{0}{100}+\binom{1}{100}+\binom{2}{100}}{2^{100}} \approx 4\times 10^{-27}
\end{equation*}
\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[LPV 5.4.5]

We flip a coin $n$ times $(n \geq 1)$. For which values of n are the following pairs of events independent?

(a) The first coin flip was heads; the number of all heads was even.

(b) The first coin flip was head; the number of all heads was more than the number of tails.

(c) The number of heads was even; the number of heads was more than the number of tails.

\Answer

(a) To get same probability for even and odd number of all heads for left $2\sim n$ coins, $n$ should be an odd number with $n>1$. Thus, $n=3,5,7,\dots,2k+1,\dots$

(b) Since the symmetry will guarantee the probability of more heads and more tails are always the same. So any $n\geq 1$ is reasonable.

(c) To keep the symmetry, $n$ should be an even number. Thus, $n=2,4,6,\dots,2k,\dots$
\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[LPV 6.3.5]

Let $p$ be a prime and $1 \leq a \leq p-1$. Consider the numbers $a, 2a, 3a, \dots , (p - 1)a$. Divide each of them by $p$, to get residues $r_1 , r_2 , \dots , r_{p-1}$ . Prove that every integer from $1$ to $p-1$ occurs exactly once among these residues.

[Hint: First prove that no residue can occur twice.]

\Answer

First prove that no residue can occur twice. Assume there are two different coefficients $k,s \ (k > s)$  that have same residues $r_k=r_s=r$. Denote the division as:
\begin{align*}
ka &= k' p+r \\
sa &= s' p+r
\end{align*}

Then
\begin{equation*}
(k-s)a=(k'-s')p
\end{equation*}

Where $(k-s)$ and $(k'-s')$ are positive integers. But $p$ is a prime number and $0<k-s<p-1$ $1\leq a\leq p-1$ can not have factorization $p$. So there comes conflict and the Assumption is wrong.

Since there is no residue occur twice and all residues have $1\leq r_1, r_2, \dots, r_{p-1} \leq p-1$. So every integer from $1$ to $p-1$ occurs exactly once among these residues.
\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[LPV 6.5.2]

Consider a regular $p$-gon, and for a fixed $k (1 \leq k \leq p - 1)$, consider all $k$-subsets of the set of its vertices. Put all these $k$-subsets into a number of boxes: We put two $k$-subsets into the same box if they can be rotated into each other. For example, all $k$-subsets consisting of $k$ consecutive vertices will belong to one and the same box.

(a) Prove that if $p$ is a prime, then each box will contain exactly $p$ of these rotated copies.

(b) Show by an example that (a) does not remain true if we drop the assumption that $p$ is a prime.

(c) Use (a) to give a new proof of Lemma 6.5.2.

\Answer

(a) Since each set can be rotated $p$ times, we just need to prove that each of the p rotated copies of a set are different. Suppose that there is a copy occurs $k$ times during the rotation. Then every other copy \textbf{must} occur $k$ times. Now we get $k|p$, and obviously $k!=p$ so $k=1$. In result, the identity is proved.

(b) Consider a case shown in Figure 1.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.3\linewidth]{figures/hw3_hexagon}
  \caption{A hexagon shows (a) is wrong when p is not a prime number}
  \label{fig:hw3_hexagon}
\end{figure}

(c) Since each box contains $p$ subsets of size $k$, the total number of subsets $\binom{p}{k}$ must satisfy $p|\binom{p}{k}$

\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[LPV 6.5.4]

Give a third proof of Fermat’s “Little” Theorem based on Exercise 6.3.5.

[Hint: Consider the product $a(2a)(3a) \cdots ((p - 1)a)$]

\Answer

From Exercise 6.3.5, we know that if $k a$ can be denote as $m_k p+r_k$ for $k=1,2,\cdots,p-1$, then every integer from $1$ to $p-1$ occurs exactly once among $r_1,r_2,\cdots,r_{p-1}$. Consider the product $a(2a)(3a) \cdots ((p - 1)a)$, we have:

\begin{equation*}
a(2a)(3a) \cdots ((p - 1)a)=(p-1)!a^{p-1}
\end{equation*}

Then:
\begin{align*}
(p-1)!(a^{p-1}-1)
  &=a(2a)(3a) \cdots ((p - 1)a)-(p-1)! \\
  &=(m_1p+r_1)(m_2p+r_2)\cdots(m_{p-1}p+r_{p-1})-r_1r_2\cdots r_{p-1} \\
  &=c_1 p^{p-1}+ c_2 p^{p-2}+\cdots+c_{p-1} p
\end{align*}
Which obviously satisfies $p|a^{p-1}-1$.
\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{homeworkProblem}[Special Problem 1]
Alice and Bob each independently tosses an unbiased coin $n$ times. Let $X$ and $Y$ be the random variables corresponding to the number of HEADs in Alice’ and Bob’s results. Your solutions must be closed-form formulas in $n$.

(a) Determine the expected value of the random variable $X - Y$.

(b) Determine the variance of the random variable $X - Y$.

(c) Let $S$ denote the event that $X = Y$, and let $s(n) = Pr\{S\}$. Determine $s(n)$.

(d) Let $T$ denote the event that $X = Y + 1$, and let $t(n) = Pr\{T\}$. Determine $t(n)$.

\Answer

Easy to know that $X$ and $Y$ are independent, and:
\begin{equation*}
P(X=k) = P(Y=k) = \frac{1}{2^n}\binom{n}{k}
\end{equation*}
\begin{equation*}
E(X)=E(Y)=\frac{1}{2^n}\sum_{k=0}^n k\binom{n}{k}=\frac{n}{2}
\end{equation*}
\begin{equation*}
D(X)=D(Y)=\frac{1}{2^n}\sum_{k=0}^n (k-\frac{n}{2})^2\binom{n}{k}=\frac{n}{4}
\end{equation*}

(a)
\begin{equation*}
E(X-Y) = E(X)-E(Y) = \sum_{k=0}^nk{P(X=k)-kP(Y=k)}=0
\end{equation*}

(b)
\begin{equation*}
D(X-Y) = D(X)+D(Y) = \frac{n}{2}
\end{equation*}

(c)
\begin{equation*}
s(n) = P(X=Y)
  = \sum_{k=0}^n P(X=k)P(Y=k)
  = \frac{1}{2^{2n}}\sum_{k=0}^n \binom{n}{k}^2
\end{equation*}

\hspace{2em}
Consider equation $(1+x)^n(1+x)^n=(1+x)^{2n}$, we have:
\begin{equation*}
\left(\binom{n}{0}+x\binom{n}{1}+\cdots x^n\binom{n}{n}\right)^2=\binom{2n}{0}+x\binom{2n}{1}+\cdots x^{2n}\binom{2n}{2n}
\end{equation*}

\hspace{2em}
Compare the coefficients of $x^{n}$, we get:
\begin{align*}
\binom{2n}{n}
  &= \binom{n}{0}\binom{n}{n}+\binom{n}{1}\binom{n}{n-1}+\cdots+\binom{n}{n}+\binom{n}{0} \\
  &= \binom{n}{0}^2+\binom{n}{1}^2+\cdots+\binom{n}{n}^2
\end{align*}

\hspace{2em}
So
\begin{equation*}
s(n) = \frac{1}{2^{2n}}\sum_{k=0}^n \binom{n}{k}^2 = \frac{1}{2^{2n}}\binom{2n}{n}=\frac{(2n)!}{(n!)^2 2^{2n}}
\end{equation*}

(d)
\begin{equation*}
t(n) = P(X=Y+1)
  = \sum_{k=0}^{n-1} P(X=k+1)P(Y=k)
  = \frac{1}{2^{2n}}\sum_{k=0}^{n-1} \binom{n}{k+1}\binom{n}{k}
\end{equation*}

\hspace{2em}
Similar to (c), compare the coefficients of $x^{n+1}$, we get:
\begin{align*}
\binom{2n}{n+1}
  &= \binom{n}{n}\binom{n}{1}+\binom{n}{n-1}\binom{n}{2}+\cdots+\binom{n}{1}\binom{n}{n} \\
  &= \binom{n}{0}\binom{n}{1}+\binom{n}{1}\binom{n}{2}+\cdots+\binom{n}{n-1}\binom{n}{n}
\end{align*}

\hspace{2em}
So
\begin{equation*}
t(n) = \frac{1}{2^{2n}}\sum_{k=0}^{n-1} \binom{n}{k}\binom{n}{k+1} = \frac{1}{2^{2n}}\binom{2n}{n+1}=\frac{(2n)!}{(n+1)!(n-1)!2^{2n}}
\end{equation*}

\end{homeworkProblem}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% ACKNOWLEGEMENT
\Acknowledgement

None

% End edit to here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{spacing}
\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% ***********************************************************************************
% Pure LaTeX part to be inserted in a document (be careful of depencies of packages & commands)
% Prepared by XXX and YYY under the supervision of Arnaud de La Fortelle
% Fall 2017
% 1D LWR traffic model of the simulation part
% ***********************************************************************************

\subgroup{3}{Yue Hu and Carlin Yao}

\paragraph{Model presentation}
What is the model we want to simulate? What do we want to observe? Which is the state space and the dynamics?

\paragraph{Implementation}
Explain the structure of the code. Do not put necessarily all the code (not more than 100 lines) since some routines (functions) can hide efficiently some unnecessary complexity. Provide a code that run (and explicit librairies and dependencies). Ensure your file name is aligned with this part.

 \paragraph{Results}
 Explain the quantities you are studying (i.e. metrics and statistics). Provide good visualization.

\paragraph{Interpretation}
Relate these quantities to the model and to theoretical knowledge of the course.

 \paragraph{Conclusion}
 What have we learned? Is everything aligned (theory and practice)? What was difficult? Provide perspectives.


\documentclass[handout]{beamer}
\usepackage{tikz}
\def\checkmark{\tikz\fill[scale=0.4](0,.35) -- (.25,0) -- (1,.7) -- (.25,.15) -- cycle;}
\usepackage{pifont}
\newcommand{\xmark}{\ding{55}}
\usepackage{animate}
\usepackage{xcolor,cancel}

\usetheme{PaloAlto}
\title{Practical ODE modelling}
\author[Ben]{Ben Lambert$^1$}
\usepackage{datetime}
\newdate{date}{8}{2}{2021}
\date{\displaydate{date}}
\institute[University of Oxford]{
	\inst{1}University of Oxford}
\beamertemplatenavigationsymbolsempty
\setbeamertemplate{sidebar left}{}

\makeatletter
\newcommand\mathcircled[1]{%
	\mathpalette\@mathcircled{#1}%
}
\newcommand\@mathcircled[2]{%
	\tikz[baseline=(math.base)] \node[draw,circle,inner sep=1pt] (math) {$\m@th#1#2$};%
}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\begin{frame}
	\frametitle{Lecture outcomes}

	\begin{enumerate}
		\item Explain difficulty with inference for ODEs;
		\item Inference with PINTS and other libraries;
		\item PINTS demo.
	\end{enumerate}

\end{frame}

\section{ODE inference refresh}
\frame{\tableofcontents[currentsection]}

\begin{frame}
	\frametitle{Setting up the inference problem: forward model}

	\begin{equation}
	\frac{d y(t)}{dt} = f(y, t; \theta).
	\end{equation}

	\begin{figure}
		\centerline{\includegraphics[width=0.7\textwidth]{./Figures/lec7_odeSingleBulding1.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Setting up the inference problem: noise model}

	\begin{equation}
	y^*(t) \stackrel{i.i.d.}{\sim} \mathcal{N}(y(t;\theta), \sigma).
	\end{equation}

	\begin{figure}
		\centerline{\includegraphics[width=0.7\textwidth]{./Figures/lec7_odeSingleBulding2.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Setting up the inference problem: data}

	\begin{equation}
	\mathcal{L} = \prod_{i=1}^{S} \mathcal{N}(y^*(t_i)| y(t_i;\theta), \sigma).
	\end{equation}

	\begin{figure}
		\centerline{\includegraphics[width=0.7\textwidth]{./Figures/lec7_odeSingleBulding2_data.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Setting up the inference problem: posterior}

	\begin{equation}
	p(\theta|X) \propto p(\theta,\sigma) \prod_{i=1}^{S} \mathcal{N}(y^*(t_i)| y(t_i;\theta), \sigma).
	\end{equation}

	\begin{figure}
		\centerline{\includegraphics[width=0.7\textwidth]{./Figures/lec7_odeSingleBulding2_data.pdf}}
	\end{figure}

\end{frame}

\section{Issues with ODE inference}
\frame{\tableofcontents[currentsection]}

\begin{frame}
	\frametitle{Why is inference for ODEs hard?}

	\begin{itemize}
		\item ODEs (typically) require numerical solutions $\implies$ expensive;
		\item Non-linearity $\implies$ posterior distributions can be complex / unidentified.
	\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{Why is inference for ODEs hard?}

	\begin{figure}
		\centerline{\includegraphics[width=1\textwidth]{./Figures/girolami.png}}
	\end{figure}

	from ``Bayesian inference for differential equations'', Girolami (2008)

\end{frame}

\begin{frame}
	\frametitle{How to make your life easier}

	Before you start inference:

	\begin{itemize}
		\item Fake data simulation followed by inference: make simulated data as similar to real data characteristics as possible;
		\item Profile likelihood method to assess identifiability;
		\item General mathematical analysis: assess sensitivity of outputs to parameters.
	\end{itemize}
\end{frame}

\section{Inference cycle}
\frame{\tableofcontents[currentsection]}

\begin{frame}
	\frametitle{How to fit model to data?}

	\begin{figure}
		\centerline{\includegraphics[width=1\textwidth]{./Figures/student_cycle.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Why does this cycle of misery persist?}

	\begin{itemize}
		\item Statistical literature is written by methods experts for other experts;
		\item Statistics papers do not often contain high quality pseudocode;
		\item Software accompanying papers is typically not professionally developed;
		\item Software is typically idiosyncratic;
		\item Different problems require different solutions but solutions currently require reinventing the wheel;
		\item Not enough knowledge sharing between applied researchers about ``Which method works best?''.
	\end{itemize}

\end{frame}

\begin{frame}
\frametitle{What is PINTS?}

PINTS: \textbf{P}robabilistic \textbf{I}nference for \textbf{N}oisy \textbf{T}ime \textbf{S}eries, which covers two broad categories of inference methods:

\begin{itemize}
	\item Optimisation: single set of parameters returned;
	\item Sampling: many sets returned.
\end{itemize}

It's an open-source Python library available on Github.

\end{frame}

\begin{frame}
\frametitle{What does existing literature do?}

\begin{itemize}
	\item Most other inference software pin their hope on a single sampler:
	\begin{itemize}
		\item BUGS and JAGS: Gibbs sampler;
		\item PyMC3 and Stan: No U-Turn Sampler (NUTS);
	\end{itemize}
	\item Other libraries prepackage forward model solution methods with sampling method.
\end{itemize}

\end{frame}

\begin{frame}
	\frametitle{How is PINTS different?}
	\begin{itemize}
		\item PINTS not aligned to a single algorithm;
		\item PINTS is designed to interface with (other) probabilistic programming languages (for example, Stan);
		\item PINTS aimed at harder forward models: ODEs and PDEs;
		\item PINTS allows users freedom to use own forward model solution method.
	\end{itemize}
\end{frame}

\begin{frame}
	\frametitle{PINTS niche}

	\begin{figure}
		\centerline{\includegraphics[width=1\textwidth]{./Figures/niche-samplers.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Deciding on an approach}

	\begin{itemize}
		\item For cheap forward model $\implies$ use Stan (you can always use PINTS' Stan interface if this doesn't work);
		\item For expensive forward models, potentially requiring bespoke solvers $\implies$ use PINTS.
	\end{itemize}

\end{frame}

\begin{frame}
\frametitle{PINTS in detail}

\begin{itemize}
	\item Literature emphasis on developing new algorithms;
	\item Literature dense with hard-to-decipher algorithm details;
	\item PINTS aims to make this research \textbf{useful}:
	\begin{itemize}
		\item Common, easy-to-use, interface for lots of methods;
		\item Rigorous software development practices;
		\item Many levels of testing: unit, functional, and so on;
		\item Collaboration with statisticians working on various methods;
		\item Benchmark problems;
		\item Pseudocode and tutorial papers which explain algorithms and their ecosystem.
	\end{itemize}
\end{itemize}

\end{frame}

\begin{frame}
\frametitle{PINTS roadmap: samplers}

\begin{figure}
	\centerline{\includegraphics[width=0.9\textwidth]{./Figures/pints-roadmap-triangle-more-samplers.pdf}}
\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Families of samplers: overall}

	\begin{figure}
		\centerline{\includegraphics[width=1\textwidth]{./Figures/hierarchy-samplers-top.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{Families of samplers: within MCMC}

	\begin{figure}
		\centerline{\includegraphics[width=1\textwidth]{./Figures/hierarchy-samplers-mcmc.pdf}}
	\end{figure}

\end{frame}

\begin{frame}
	\frametitle{PINTS: optimisers}

	Optimisation more ``solved'' than sampling and have two families in PINTS:

	\begin{itemize}
		\item Gradient-free: CMAES, XNES, SNES, Nelder-mead, PSO, SHGO (planned);
		\item 1st order sensitivities: gradient descent, L-BFGS (planned).
	\end{itemize}

\end{frame}

\begin{frame}
\frametitle{Conclusions}

\begin{itemize}
	\item Inference for ODE models is generally hard;
	\item Stan and PINTS are your current best bet for inference.
\end{itemize}

\end{frame}

\bibliographystyle{authordate1}
\bibliography{Malaria}

\end{document}


% Created 2015-09-14 Mon 02:39
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage[margin=18mm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{parskip}
\usepackage{standalone}
\usepackage{tikz,pgf,pgfplots}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks,decorations.markings,datavisualization,datavisualization.formats.functions,intersections,external}
\pgfplotsset{compat=1.9}
\newcommand*{\mexp}[1]{\ensuremath{\mathrm{e}^{#1}}}
\newcommand*{\laplace}[1]{\ensuremath{\mathcal{L} \{#1\}}}
\newcommand*{\laplaceinv}[1]{\ensuremath{\mathcal{L}^{-1} \{#1\}}}
\newcommand*{\realpart}[1]{\ensuremath{\operatorname{Re}(#1)}}
\newcommand*{\impart}[1]{\ensuremath{\operatorname{Im}(#1)}}
\newcommand*{\vsp}[1]{\rule{0pt}{#1}}
\newcommand*{\tderiv}[1]{\ensuremath{\frac{d^{#1}}{dt^{n}}}}
\newcommand*{\bbm}{\begin{bmatrix}}
\newcommand*{\ebm}{\end{bmatrix}}
\newcommand*{\obsmatrix}{\mathcal{O}}
\newcommand*{\contrmatrix}{\mathcal{C}}
\newcommand*{\cwh}{\ensuremath{\cos \omega h}}
\newcommand*{\swh}{\ensuremath{\sin \omega h}}
\newcommand*{\zethree}{\big(z - \mexp{-3h}\big)}
\providecommand{\alert}[1]{\textbf{#1}}

\title{Computerized control - preparation for partial exam 1}
\author{Kjartan Halvorsen}
\date{\today}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs Org-mode version 7.9.3f}}

\begin{document}

\maketitle

\section*{The Nyquist criterion}
\label{sec-1}

The figure below shows the Nyquist curve for the open-loop pulse-transfer function
\[ H(z) = \frac{0.25K}{(z-1)(z-0.5)} \]
for the case when $K=1$.
\begin{center}
\includestandalone[width=0.4\linewidth]{nyquist}
\end{center}

(a) Indicate the phase marginal and the amplitude marginal in the figure.

(b) If the loop is closed as in the figure below, then for which values of $K$ is the closed-loop system stable?
\begin{center}
\includestandalone[width=0.4\linewidth]{feedback}
\end{center}
\section*{The Nyquist criterion and choice of sampling interval}
\label{sec-2}

The figure below shows the Nyquist curve for the continous-time system (dashed line)
\[ G(s) = \frac{1}{s(s+1)} \]
and for the corresponding discrete-time system sampled with different sampling periods: $h \in \{0.1, 0.4, 1 \}$.
\begin{center}
\includestandalone[width=0.3\linewidth]{nyquist2}
\end{center}

(a) Describe briefly in own words how the Nyquist curve changes with increasing sampling period, and what this means for stability of the closed-loop system.

(b) State the rule-of-thumb for choosing sampling period based on the Nyquist diagram.
\section*{Å\&W problem 2.10}
\label{sec-3}

The figure below shows a system of two tanks, where the input is the flow to the first tank and the output is the level in the second tank.
\begin{center}
\includegraphics[width=0.2\linewidth]{tanks.png}
\end{center}
Use of the levels as state variables gives the system
\begin{align*}
\frac{dx}{dt} &= \bbm -0.0197 & 0\\0.0178 & -0.0129 \ebm x + \bbm 0.0263\\0 \ebm u\\
y &= \bbm 0 & 1\ebm x.
\end{align*}

(a) Sample the system with sampling period $h=12$.

(b) Verify that the pulse-transfer operator for the system is
\[ H(q) = \frac{0.030q + 0.026}{q^2 -1.65q + 0.68}. \]
\section*{Å\&W problem 3.2}
\label{sec-4}

Consider the closed-loop system below with
\[ H(z) = \frac{K}{z(z-0.2)(z-0.4)}. \]
\begin{center}
\includestandalone[width=0.4\linewidth]{feedback}
\end{center}

Determine the valuse of $K$ for which the closed-loop system is stable.
\section*{Å\&W problem 3.20}
\label{sec-5}

Given the system
\[ (q^2 + 0.4q)y(k) = u(k) \]

(a) For which values $K$ in the proportional controller
\[ u(k) = K\big(u_c(k) - y(k)\big) \]
is the closed-loop system stable?

(b) Determine the stationary error $e = u_c-y$ when $u_c(k)$ is a step and $K=0.5$ in the controller in (a).
\section*{Solutions}
\label{sec-6}
\subsection*{The Nyquist criterion}
\label{sec-6-1}

   (a) The amplitude marginal is denoted $A_m$ and the phase marginal $\varphi_m$ in the figure below.
   \begin{center}
   \includestandalone[width=0.4\linewidth]{nyquist-solution}
   \end{center}

   (b) Since the open loop system $H(z)$ has no poles outside the unit disc (the pole in $1$ is excluded in the domain studied), the closed loop system \[H_c(z) = \frac{H(z)}{1+H(z)} \] will be stable as long as the nyquist curve does not encircle the point $-1$ on the negative real axis. The plot shows the nyquist curve for the system \[H_1(z) = \frac{1}{(z-1)(z-0.5)}.\] Since multiplication by a positive real value $K$ will only change the magnitude of each point on the curve, not the phase, the point of interest is the intersction with the negative real axis, and the corresponding amplitude marginal $A_m$. Wee see that the closed loop system will be stable as long as
   \[\frac{K}{A_m} < 1, \] hence,
   \[K < A_m = 2, \]
   where the numerical value is obtained by visual inspection of the graph. The result also exemplifies the point of the amplitude marginal: It is the maximum amplification of the open loop system that is possible before the closed loop system becomes unstable.
\subsection*{Nyquist criterion and sampling}
\label{sec-6-2}

   (a) The important changes in the nyquist curve when sampling the system are
\begin{enumerate}
\item The curve intersects the real axis, so instability will occur for proportional control with high gain.
\item The phase marginal decreases with increasing sampling period $h$.
\end{enumerate}

   (b) The sampling period should be chosen so that the phase marginal is decreased by 5-15 degrees.
\subsection*{For the problems from Å\&W, see the solution manual}
\label{sec-6-3}

\end{document}