\input{../../style/preamble}
\input{../../latex-math/basic-math}
\input{../../latex-math/basic-ml}

\newcommand{\titlefigure}{figure_man/bayes_reg.png}
\newcommand{\learninggoals}{
  \item Understand that regularization and parameter shrinkage can be applied to non-linear models
  \item Know structural risk minimization
  \item Know how regularization risk minimization is the same as MAP
      in a Bayesian perspective, where the penality corresponds to parameter prior.
}

\title{Introduction to Machine Learning}
\date{}

\begin{document}

\lecturechapter{Regularization in Non-Linear Models and Bayesian Priors}
\lecture{Introduction to Machine Learning}

%-------------------------------------------------------------------------------

\begin{vbframe}{Summary: Regularized Risk Minimization}

If we should define ML in only one line, this might be it:

$$
\riskrt= \min_{\thetab} \sumin \Lxyit + \lambda \cdot J(\thetab)
$$

We can choose for a task at hand:

\begin{itemize}
  \item the \textbf{hypothesis space} of $f$, which determines how features can
  influence the predicted $y$
  \item the \textbf{loss} function $L$, which measures how errors should be treated
  \item the \textbf{regularization} $J(\thetab)$, which encodes our inductive
  bias and preference for certain simpler models
\end{itemize}

\vfill

By varying these choices one can construct a huge number of different ML models.
Many ML models follow this construction principle or can be interpreted through
the lens of regularized risk minimization.

\end{vbframe}

%-------------------------------------------------------------------------------

\begin{vbframe}{Regularization in Nonlinear Models}

\begin{itemize}
  \item So far we have mainly considered regularization in LMs.
  \item Can also be applied to non-linear models (with numeric parameters), where it is
  often important to prevent overfitting.
  \item Here, we typically use $L2$ regularization, which
      still results in parameter shrinkage and weight decay.
  \item By adding regularization, prediction surfaces in regression and
  classification become smoother.
  \item Note: In the chapter on non-linear SVMs we will study the effects of
  regularization on a non-linear model in detail.
\end{itemize}

\end{vbframe}

%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
\begin{frame}{Regularization in Nonlinear Models}

\small
\textbf{Setting}: Classification for the \texttt{spirals} data.
Neural network with single hidden layer containing 10 neurons and logistic
output activation, regularized with $L2$ penalty term for $\lambda > 0$.
Varying $\lambda$ affects smoothness of the decision boundary and magnitude of
network weights:

\vfill

\only<1>{\begin{center}\includegraphics[width=\textwidth]{figure/fig-regu-nonlin-1.png}\end{center}}
\only<2>{\begin{center}\includegraphics[width=\textwidth]{figure/fig-regu-nonlin-2.png}\end{center}}
\only<3>{\begin{center}\includegraphics[width=\textwidth]{figure/fig-regu-nonlin-3.png}\end{center}}
\only<4>{\begin{center}\includegraphics[width=\textwidth]{figure/fig-regu-nonlin-4.png}\end{center}}

%\only<5>{\includegraphics[width=\textwidth]{figure/fig-regu-nonlin-5.png}}
%\only<6>{\includegraphics[width=\textwidth]{figure/fig-regu-nonlin-6.png}}
\end{frame}

\begin{frame}{Regularization in Nonlinear Models}

The prevention of overfitting can also be seen in CV.
Same settings as before, but each $\lambda$ is evaluated with
repeated CV (10 folds, 5 reps).

\begin{center}\includegraphics[width=0.7\textwidth]{figure/fig-regu-nonlin-srm-1.png}\end{center}

We see the typical U-shape with the sweet spot between overfitting (LHS, low $\lambda$) and
underfitting (RHS, high $\lambda$) in the middle.
\end{frame}

%-------------------------------------------------------------------------------
\begin{vbframe} {Structural Risk Minimization}

\begin{itemize}
  % \item Complex models generalize poorly (overfitting) if merely the empirical risk is optimized.
  \item Thus far, we only considered adding a complexity penalty to empirical risk minimization.
  \item Instead,  structural risk minimization (SRM) assumes that the hypothesis space $\Hspace$ can be decomposed into increasingly complex hypotheses (size or capacity): $\Hspace = \cup_{k \geq 1 }\Hspace_{k}$.
  \item Complexity parameters can be the, e.g. the degree of polynomials in linear models or the size of hidden layers in neural networks.
\end{itemize}

\begin{center}
\includegraphics[width=0.5\textwidth]{figure_man/fig-regu-srm-1}
% FIGURE SOURCE:https://docs.google.com/drawings/d/1qFoFSyuY4glsNvgYgIZ96yRcznOdA5q3oogI5fVBQ1A/edit?usp=sharing
\end{center}

\framebreak

\begin{itemize}

    \item SRM chooses the smallest $k$ such that the optimal model from $\Hspace_k$ found by ERM or RRM cannot significantly
        be outperformed by a model from a $\Hspace_m$ with $m > k$.
  \item By this, the simplest model can be chosen, which minimizes the generalization bound.
  \item One challenge might be choosing an adequate complexity measure, as for some models, multiple complexity measures exist.
\end{itemize}

\begin{center}
\includegraphics[width=0.6\textwidth]{figure_man/fig-regu-srm-2}
% FIGURE SOURCE: https://docs.google.com/drawings/d/1mk_qVUbfOYwwmuE0AgmnPiNSMoX--pE_nZsWYND0IhQ/edit?usp=sharing
\end{center}

\end{vbframe}

%-------------------------------------------------------------------------------
\begin{frame} {Structural Risk Minimization}

\small

\textbf{Setting}: Classification for the \texttt{spirals} data.
Neural network with single hidden layer containing $k$ neurons and logistic
output activation, L2 regularized with $\lambda = 0.001$.
So here SRM and RRM are both used.
Varying the size of the hidden layer affects smoothness of the decision boundary:

\vfill

\only<1>{\begin{center}\includegraphics[width=0.5\textwidth]{figure/fig-regu-nonlin-size-1.png}\end{center}}
\only<2>{\begin{center}\includegraphics[width=0.5\textwidth]{figure/fig-regu-nonlin-size-2.png}\end{center}}
\only<3>{\begin{center}\includegraphics[width=0.5\textwidth]{figure/fig-regu-nonlin-size-3.png}\end{center}}
\only<4>{\begin{center}\includegraphics[width=0.5\textwidth]{figure/fig-regu-nonlin-size-4.png}\end{center}}
\only<5>{\begin{center}\includegraphics[width=0.5\textwidth]{figure/fig-regu-nonlin-size-5.png}\end{center}}
\only<6>{\begin{center}\includegraphics[width=0.5\textwidth]{figure/fig-regu-nonlin-size-6.png}\end{center}}

\end{frame}

\begin{frame} {Structural Risk Minimization}
Again, complexity vs CV score.

\begin{center}\includegraphics[width=0.7\textwidth]{figure/fig-regu-nonlin-srm-2.png}\end{center}

A minimal model with good generalization seems to have ca. 6-8 hidden neurons.

\end{frame}

\begin{frame} {Structural Risk Minimization and RRM}

Note that normal RRM can also be interpreted through SRM, if we rewrite the penalized ERM as constrained ERM.

\begin{columns}
\begin{column}{0.5\textwidth}
\begin{eqnarray*}
\min_{\thetab} && \sumin \Lxyit  \\
  \text{s.t. } && \|\thetab\|_2^2  \leq t \\
\end{eqnarray*}
\end{column}
\begin{column}{0.5\textwidth}
\begin{figure}
\includegraphics[width=0.6\textwidth]{figure_man/ridge_hat.png}
\end{figure}
\end{column}
\end{columns}

\vspace{0.5cm}

We can interpret going through $\lambda$ from large to small as through $t$ from small to large.
This constructs a series of ERM problems with hypothesis spaces $\Hspace_\lambda$,
where we constrain the norm of $\thetab$ to unit balls of growing size.
\end{frame}

%-------------------------------------------------------------------------------

% \section{Regularization from a Bayesian Perspective}

\begin{vbframe} {RRM vs. Bayes}

We already created a link between max. likelihood estimation and ERM.

\lz

Now we will generalize this for RRM.

\lz

Assume we have a parameterized distribution $p(y | \thetab, \xv)$ for our data and
a prior $q(\thetab)$ over our parameter space, all in the Bayesian framework.

\lz

From the Bayes theorem we know:

$$
p(\thetab | \xv, y) = \frac{p(y | \thetab, \xv) q(\thetab) }{p(y | \xv)} \propto
p(y | \thetab, \xv) q(\thetab)
$$

\framebreak

The maximum a posteriori (MAP) estimator of $\thetab$ is now the minimizer of

$$
- \log p\left(y ~|~ \thetab, \xv\right) - \log q(\thetab).
$$

\begin{itemize}
  \item Again, we identify the loss $\Lxyt$ with $-\log(p(y | \thetab, \xv))$.
  \item If $q(\thetab)$ is constant (i.e., we used a uniform, non-informative
  prior), the second term is irrelevant and we arrive at ERM.
  \item If not, we can identify $J(\thetab) \propto -\log(q(\thetab))$, i.e.,
  the log-prior corresponds to the regularizer, and the additional $\lambda$, which controls the strength of our
  penalty, usually influences the peakedness / inverse variance / strength of our prior.
\end{itemize}

\framebreak

\begin{figure}
  \centering
  \scalebox{1}{\includegraphics[width=\textwidth]{figure/bayes_prior.png}}
\end{figure}

\begin{itemize}
  \item $L2$ regularization corresponds to a zero-mean Gaussian prior with
  constant variance on our parameters:
  $\theta_j \sim \mathcal{N}(0, \tau^2)$
  % ~ \forall j \in \{1, \dots , d\}$, $\tau > 0$.
  \item $L1$ corresponds to a zero-mean Laplace prior:
  $\theta_j \sim \mathit{Laplace}(0,b)$.
  $\mathit{Laplace}(\mu, b)$ has density $\frac{1}{2b}\exp(-\frac{|\mu-x|}{b})$,
  with scale parameter $b$, mean $\mu$ and variance $2b^2$.
  \item In both cases, regularization strength increases as the
  variance of the prior decreases: a prior probability mass more narrowly
  concentrated around 0 encourages shrinkage.
\end{itemize}

\end{vbframe}

% ------------------------------------------------------------------------------

\begin{vbframe}{Example: Bayesian L2 regularization}

\small We can easily see the equivalence of $L2$ regularization and a Gaussian
prior:

\begin{itemize}
  \small
  \item We define a Gaussian prior with uncorrelated components for $\thetab$:
  \begin{footnotesize}
    $$q(\thetab) = \mathcal{N}_d(\bm{0}, \mathit{diag}(\tau^2))
    = \prod_{j = 1}^d  \mathcal{N}(0, \tau^2)
    = (2\pi\tau^2)^{-\frac{d}{2}} \exp \left( - \frac{1}{2\tau^2} \sum_{j = 1}^d
    \theta_j^2 \right).$$
  \end{footnotesize}
  \item With this, the MAP estimator becomes
  \begin{footnotesize}
  \begin{eqnarray*}
    \thetah^{\text{MAP}} &=& \argmin_{\thetab} \left(
    - \log p\left(y ~|~ \thetab, \xv \right) - \log q(\thetab)
    \right) \\
    &=& \argmin_{\thetab} \left(
    - \log p\left(y ~|~ \thetab, \xv \right) + \tfrac{d}{2} \log(2\pi \tau^2) +
    \frac{1}{2\tau^2} \sum_{j = 1}^d \theta_j^2
    \right) \\
    % &=& \argmin_{\thetab} \left(
    % - \log p\left(\xv ~|~ \thetab\right) + \frac{1}{2\tau^2} {\thetab}^T\thetab
    % \right) \\
    &=& \argmin_{\thetab} \left(
    - \log p\left(y ~|~ \thetab, \xv \right) + \frac{1}{2\tau^2} \| \thetab \|_2^2
    \right).
  \end{eqnarray*}
  \end{footnotesize}
  % \item $\frac{1}{\tau^2}$ controls prior precision, i.e., inverse variance,
  % and thus the amount of shrinkage.
  \item We see how the inverse variance (precision) $1/\tau^2$ controls shrinkage.
  % (e.g., for linear Ridge regression with
  % $\epsilon \sim \mathcal{N}(0, \sigma^2)$ we set
  % $\lambda = \frac{\sigma^2}{\tau^2}$).
\end{itemize}

  % \item The conditional distribution of $\ydat$ in linear regression with
  % Gaussian errors $\epsilon^{(i)} \sim \mathcal{N}(0, \sigma^2) ~ \forall i \in
  % \setn$, $\sigma > 0$, is also Gaussian:
  % \begin{footnotesize}
  % $$- \log p\left(\ydat ~|~ \xv, \thetab\right) = \frac{n}{2} \log (2\pi
  % \sigma^2) + \frac{1}{2\sigma^2} \sumin \left(\yi - \fxit \right)^2.$$
  % \end{footnotesize}

% &=& \argmin_{\thetab} \left(
% \tfrac{n}{2} \log (2\pi \sigma^2) + \frac{1}{2\sigma^2} \sumin \left(\yi -
% \fxit \right)^2 + \tfrac{p}{2} \log(2\pi \tau^2) +
% \frac{1}{2\tau^2} \sumjp \theta_j^2
% \right) \\
% &=& \argmin_{\thetab} \left( \frac{1}{\sigma^2} \sumin \left(\yi -
% \fxit \right)^2 + \frac{1}{\tau^2} \sumjp \theta_j^2 \right) \\
% &=& \argmin_{\thetab} \left(
% \frac{1}{\sigma^2} \left(\ydat - \Xmat \thetab\right)^\top \left(\ydat - \Xmat
% \thetab\right) + \frac{1}{\tau^2} {\thetab}^T\thetab
% \right)

% \begin{eqnarray*}
% \thetah^{\text{MAP}} &=& argmin_{\thetab} \left(
% - \log p\left(\ydat ~|~ \xv, \thetab\right) - \log q(\thetab)
% \right) \\
%
% &=& \argmin_{\thetab} \left(
% \tfrac{n}{2} \log (2\pi \sigma^2) + \frac{1}{2\sigma^2} \sumin \left(\yi -
% \fxit \right)^2 + \tfrac{p}{2} \log(2\pi \tau^2) +
% \frac{1}{2\tau^2} \sumjp \theta_j^2
% \right) \\
% &=& \argmin_{\thetab} \left( \frac{1}{\sigma^2} \sumin \left(\yi -
% \fxit \right)^2 + \frac{1}{\tau^2} \sumjp \theta_j^2 \right) \\
% &=& \argmin_{\thetab} \left(
% \frac{1}{\sigma^2} \left(\ydat - \Xmat \thetab\right)^\top \left(\ydat - \Xmat
% \thetab\right) + \frac{1}{\tau^2} {\thetab}^T\thetab
% \right)
%
% &=& \argmin_{\thetab} \left(
% \tfrac{n}{2} \log (2\pi \sigma^2) + \frac{1}{2\sigma^2} \sumin \left(\yi -
% \fxit \right)^2 + \tfrac{p}{2} \log(2\pi \tau^2) +
% \frac{1}{2\tau^2} \sumjp \theta_j^2
% \right) \\
% &=& \argmin_{\thetab} \left( \frac{1}{\sigma^2} \sumin \left(\yi -
% \fxit \right)^2 + \frac{1}{\tau^2} \sumjp \theta_j^2 \right) \\
% &=& \argmin_{\thetab} \left(
% \frac{1}{\sigma^2} \left(\ydat - \Xmat \thetab\right)^\top \left(\ydat - \Xmat
% \thetab\right) + \frac{1}{\tau^2} {\thetab}^T\thetab
% \right)
% \end{eqnarray*}

% \footnotesize
% Now, define $\lambda := \frac{\sigma^2}{\tau^2}$ -- note how higher prior
% precision (i.e., lower variance) increases shrinkage! -- and set the derivative
% to 0:
%
% \begin{scriptsize}
% \begin{eqnarray*}
% 0 &=& \frac{1}{\lambda \tau^2} \left( - {\Xmat}^T \ydat + \thetab {\Xmat}^T \Xmat
% \right) + \frac{\lambda}{\sigma^2} \thetab
% \quad \Leftrightarrow \quad 0 = \frac{\sigma^2}{\tau^2} \left( - {\Xmat}^T \ydat
% + \thetab {\Xmat}^T \Xmat \right) + \lambda^2 \thetab \\
% 0 &=&  - {\Xmat}^T \ydat + \thetab {\Xmat}^T \Xmat + \lambda \thetab
% \quad \Leftrightarrow \quad
% \thetab(\Xmat^T \Xmat  + \lambda \id) = {\Xmat}^T \ydat
% \end{eqnarray*}
% \end{scriptsize}
%
% $\Rightarrow \thetah^{\text{MAP}} =
% (\Xmat^T \Xmat  + \lambda \id)^{-1} \Xmat^T\ydat  = \thetah^{\text{Ridge}}.$

\end{vbframe}

% ------------------------------------------------------------------------------

% \begin{vbframe} {Minimum Description Length}

% MDL principle

% \begin{itemize}
%   \item (Compress data using using fewer symbols than literal)
%   \item (In the MDL framework, learning is seen as data compression)
%   \item (More we compress, more we learn. Therefore, pick the hypothesis which results in the shortest code.)
%   \item (Occam's razor, principle of parsimony)
%   \item (All else being equal, a simpler explanation is better than a complex one.)
% \end{itemize}

% % \begin{equation}
% %     \begin{aligned}
% %     H_{\mathrm{mdl}} & :=\underset{H \in \mathcal{H}}{\arg \min }\left(L(H)+L_{H}(D)\right) \\
% %     L_{\mathrm{mdl}}(D) & :=\min _{H \in \mathcal{H}}\left(L(H)+L_{H}(D)\right)
% %   \end{aligned}
% % \end{equation}

% \framebreak

% \begin{itemize}
%   \item There is a correspondence between the length of a message L(x) and the distribution P(x)
%   $$P(x)=2^{-L(x)}, \quad L(x)=-\log _{2} P(x)$$
%   \item Two-part code : parameter block and data block
%   \item $L(H)$ is the length of a specific hypothesis in the set.
%   \item $L(D|H)$ is the length of the data encoded under H.
%   \item $L(D,H) = L(H) + L(D|H)$
%   \item (Represents a tradeoff between goodness-of-fit and complexity)
% \end{itemize}

% \framebreak

% Regression example : $Y_{i}=f\left(X_{i}\right)+\epsilon_{i} \text { for } i=1, \ldots, n \text { where } \epsilon_{i} \stackrel{i i d}{\sim} \mathcal{N}\left(0, \sigma^{2}\right)$

% For a given dataset, the length of the encoding is :

% $$\log 1 / p_{Y | X}\left(y^{n} | x^{n}\right) = \log \left(\sqrt{2 \pi \sigma^{2}} e^{\frac{1}{2 \sigma^{2}} \sum_{i=1}^{n}\left(y_{i}-f\left(x_{i}\right)\right)^{2}}\right) \propto \sum_{i=1}^{n}\left(y_{i}-f\left(x_{i}\right)\right)^{2}$$

% \lz

% The two-stage MDL procedure to pick the best hypothesis is :

% $$f_{\gamma}=\arg \min _{f \in F_{\gamma}}\left[L(f)+\frac{1}{n} \sum_{i=1}^{n}\left(Y_{i}-f\left(X_{i}\right)\right)^{2}\right]$$

% This is equivalent to regularized least squares.
% \end{vbframe}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%          REFERENCES          %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{vbframe}
% \frametitle{References}
% \footnotesize{
% \begin{thebibliography}{99}

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \bibitem[Ian Goodfellow et al., 2016]{5} Ian Goodfellow, Yoshua Bengio and Aaron Courville (2016)
% \newblock Deep Learning
% \newblock \emph{\url{http://www.deeplearningbook.org/}}

% \end{thebibliography}
% }
% \end{vbframe}

\endlecture
\end{document}


\documentclass[11pt]{article}
\usepackage{graphicx}
\graphicspath{ {C:/Users/yedkk/Desktop/CS465/HW1} }
\begin{document}
\section{Homework 3}
Name: Kangdong Yuan

\subsection{probelm1}
a).I did not work in a group.
\\b).I did not consult without anyone my group members
\\c).I did not consult any non-class materials.

\subsection{problem2}
Master’s theorem, we can split the recurrence into $T(n)=aT(\frac{n}{b})+(n^k*log^p(n))$ \\
a). $T(n)=11T(\frac{n}{5})+13n^{1.3}, \ a=11, \ b=5, \ k=1.3, \ p=0$\\
$log_{5}(11)=1.489>1.3=k$ Then the running time is $ T(n)=\Theta(n^{log_{5}(11)})$ \\
\\
b). $T(n)=6T(n/2)+n^{2.8}, \ a=6, \ b=2, \ k=2.8, \ p=0$\\
$log_{2}(6)=2.58498<2.8=k$ Then the running time is $ T(n)=\Theta(n^{log_{2}(6)})$ \\
\\
c). $T(n)=5T(n/3)+log^2(n), \ a=5, \ b=3, \ k=0, \ p=2$\\
$log_{3}(5)=1.464>0=k$ Then the running time is $ n^{log_{3}(5)}=\Theta(n^{1.464})$ \\
\\
d).$T(n)=T(n-2)+log(n)$, we can unfold it $T(n)=T(n-4)+log(n-2)+log(n)=T(n-6)+log(n-4)+log(n-2)+log(n)$ \\ Finally, if n is a even number $ T\left(n\right)=T\left(0\right)+\sum _{k=1}^{\frac{n}{2}}log\left(2k\right)$ \\
if n is a odd number, $T\left(n\right)=T\left(1\right)+\sum _{k=1}^{\frac{n}{2}}log\left(2k+1\right)$ \\
\\ The running time of \\ $Min (T\left(1\right)+\sum _{k=1}^{\frac{n}{2}}log\left(2k+1\right),T\left(0\right)+\sum _{k=1}^{\frac{n}{2}}log\left(2k\right) )  \leq T(n) \leq Max(T\left(1\right)+\sum _{k=1}^{\frac{n}{2}}log\left(2k+1\right),T\left(0\right)+\sum _{k=1}^{\frac{n}{2}}log\left(2k\right))$ \\
$T(n)=O(n*log(n))$ and $T(n)=\Omega(n*log(n))$,
So, for all n, the running time of $T(n)$ is $\Theta(n*log(n))$

\subsection{problem3}
We assume the n is the power of 2. Find the $A[i]=i$, first we know it is a sorted array, so we can use this feature to find when is the $A[i]=i$. \\
By using the divide and conquer, first divide this array by 2, and find the mid item. Because there are $n+1$ items in this array (the start index of this array is 0), the mid item in this array is $A[\frac{n}{2}]$ then check whether $A[\frac{n}{2}]=\frac{n}{2}$, if the conditions are true, we return the true. \\ If the conditions are not true, \\ if $A[\frac{n}{2}]>\frac{n}{2}$ we need do the same search in lower interval which from 0 to $\frac{n}{2}$. \\ But if $A[\frac{n}{2}]<\frac{n}{2}$ we need do the same search in upper interval which from $\frac{n}{2}$ to n. \\ We do this recurrence search process until we find the $A[i]=i$, or the search interval becomes 1.\\
The recurrence function is $T(n)=T(\frac{n}{2}+1)$, then we solve the running time by Master’s theorem. $a=1, \ b=2, \ k=0, \ p=0$, $log_2(1)=0=k, p>-1$ so we can know that running time is $O(n^k*log^{p+1}(n))=O(log(n))$

\subsection{problem4}
For each array, there are $n$ items in this array (the index of this array start from 1), and there are m different values in this array. \\
The sorted algorithm for this list is go through all the items in the array, we create m different new array to to store the values for each items. For example, the a new array store only value 5, then we go through all the items in array, we put all items = 5 into this array. Then, after put all items into new arrays, we compare the value in each sub-array by insertion sort. Finally, after sort each sub-array, we combine all sub-array with wanted order.  \\ We analysis the running time of this algorithm, when we go through all the items in array it take O(n), when we sort M sub-array by insertion sort, the average running time is O(M) (if the value in array is uniformly distributed). The total running time is O(n+M). \\
For small M, the running time is also O(n+m), the lower bound is cannot be the $\Omega(nlog(n))$

\end{document}


\documentclass[12]{scrartcl}
\usepackage{amssymb,amsmath,gensymb,dsfont,calc,multicol,fullpage}
\makeatletter
\newcommand\Aboxed[1]{
   \@Aboxed#1\ENDDNE}
\def\@Aboxed#1&#2\ENDDNE{%
   &
   \settowidth\@tempdima{$\displaystyle#1{}$}
   \setlength\@tempdima{\@tempdima+\fboxsep+\fboxrule}
   \kern-\@tempdima
   \boxed{#1#2}
}
\makeatother

\begin{document}

\title{Homework 11, Section 2.2; 7, 10, 12, 14, 15, 18}
\author{Alex Gordon}
\date{\today}
\maketitle
\section*{Homework}
\subsection*{7. A)}
$A^-1b_1 = \begin{bmatrix} -9 \\ 4   \end{bmatrix}$
$A^-1b_2 = \begin{bmatrix} 11 \\ -5   \end{bmatrix}$
$A^-1b_3 = \begin{bmatrix} 6 \\ -2   \end{bmatrix}$
$A^-1b_4 = \begin{bmatrix} 13 \\ -5   \end{bmatrix}$
\subsection*{7. B)}
The solutions are the same as in part A.
$A^-1b_1 = \begin{bmatrix} -9 \\ 4   \end{bmatrix}$
$A^-1b_2 = \begin{bmatrix} 11 \\ -5   \end{bmatrix}$
$A^-1b_3 = \begin{bmatrix} 6 \\ -2   \end{bmatrix}$
$A^-1b_4 = \begin{bmatrix} 13 \\ -5   \end{bmatrix}$

\subsection*{10. A)}
False. The product matrix is invertible, but the product of inverses should be in the reverse order.
\subsection*{10. B)}
True, theorem 6a.
\subsection*{10. C)}
True, by theorem 4.
\subsection*{10. D)}
True, by theorem 7.
\subsection*{10. E)}
False, The theorem is not correct.
\subsection*{12.}

\subsection*{14.}
$(B–C)DD^{–1} =0D^{–1}, (B–C)I=0, B–C=0, and B=C.$
\subsection*{15.}
$(ABC) C^{–1}B^{–1}A^{–1} = ABCC^{–1}B^{–1}A^{–1} = ABIB^{–1}A^{–1} = ABB^{–1}A^{–1} = AIA^{–1} = AA^{–1} = I$
\subsection*{18.}
$P^{–1}A = P^{–1}PBP^{–1}, \ P^{–1}A = IBP^{–1}, \ P^{–1}A = BP^{–1}$

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{CTA200 Assignment 2}
\author{Simon Smith}
\date{May 2020}

\usepackage{natbib}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{indentfirst}
\usepackage{float}
\usepackage{listings}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{url}
\usepackage{textcomp}
\usepackage{titling}
\usepackage{dirtytalk}
\usepackage{gensymb}

\geometry{margin=0.7in}
\setlength{\parskip}{1em}
\setlength{\parindent}{3em}

\begin{document}

\maketitle

\section{Q1. Behaviour of the Complex Plane}

\subsection{Methods}

It is completely nonsensical to talk about negative numbers as outputs of a squaring function for the reals, but the complex numbers, as their name may suggest, behave in curious ways. We explore the tendencies of complex numbers when iterated squaring is applied.

\begin{equation}
    z_{i+1} = |z_i|^2 + c
\end{equation}

c is some complex number of the form $a + bi$ and we take z$_0$ to be zero. Starting here, we iterate over this expression until we can determine whether the series will run off to infinity. To do so, for a given c, we let this iteration run 10 times and then examine the value of the last term. If this term is greater than 100, we know it has run far away from the origin and will continue to do so. We now run this process for every point in the complex plane ranging from (-2, 2) and (-2i, 2i) and keep track of which points stay bounded to a finite number. We plot this subset to get a curious image which has come to be known as the Mandelbrot set.

We can rewrite the code to investigate this set of complex numbers further. Some points diverge faster than others and we can keep track of how quickly each point runs off to infinity. This produces the figure on the right of Figure \ref{fig:B0353+52}, showing finer details around the edges of the set.

\subsection{Analysis}

Any number that is positive real will diverge. It seems the region of complex numbers whose absolute value is less than unity will stay bounded. If you zoom in on regions of the image, it will resemble the structure of other parts of the image.

\begin{figure*}[ht]
    \centering
    \captionsetup{justification=centering}
    \begin{subfigure}[t]{0.4\linewidth}
        \includegraphics[width=\linewidth]{images/mandel.png}
    \end{subfigure}
    \begin{subfigure}[t]{0.535\linewidth}
        \includegraphics[width=\linewidth]{images/mandel2.jpg}
    \end{subfigure}
    \captionsetup{justification=centering,margin=1cm}
    \caption{}
    \label{fig:B0353+52}
\end{figure*}

\pagebreak

\section{Q2. Epidemics}

\subsection{Methods}

To model our epidemic, we initialize a population of 1000 residents with one preliminarily infected patient. We use a simple SIR model which keeps track of those who are Susceptible to illness (Healthy), Infected, and Recovered (or Removed from the Infected population). These three populations change with respect to he following differential equations:

\begin{align}
    \frac{dS}{dt} &= -\frac{\beta S I}{N} \\
    \frac{dI}{dt} &= \frac{\beta S I}{N} - \gamma I \\
    \frac{dR}{dt} &= \gamma I
\end{align}

$\gamma$ and $\beta$ are parameters that determine the progression of the disease. $\gamma$ represents how quickly the infected patients will recover, while $\beta$ describes the rate at which the disease spreads from the sick to the healthy. To implement this in python, a function was created which takes an array of times, initial values for S, I and R, as well as the parameters $\beta$ and $\gamma$. Using {\tt odeint} from the {\tt scipy} package, the equations were integrated over the time interval to show the following figures. In Figure \ref{fig:D}, the differential equations were modified to account for deaths related to the disease. Deceased patients are removed from the Infected population with respect to another parameter $\alpha$ as:

\begin{align}
    \frac{dS}{dt} &= -\frac{\beta S I}{N} \\
    \frac{dI}{dt} &= \frac{\beta S I}{N} - \gamma I  - \alpha D \\
    \frac{dR}{dt} &= \gamma I \\
    \frac{dD}{dt} &= \alpha D
\end{align}

In this SIRD model, we add in an initial value for D as well as the parameter $\alpha$, then integrate using the same method as above.

\subsection{Analysis}

\begin{figure}[H]
    \centering
    \captionsetup{justification=centering}
    \includegraphics[width=0.8\linewidth]{images/HF.png}
    \caption{Even though the Susceptible population is quickly infected, the very fast recovery rate stamps out the infection before it can reach the majority of the population.}
    \label{fig:HF}
\end{figure}
\begin{figure}[H]
    \centering
    \captionsetup{justification=centering}
    \includegraphics[width=0.8\linewidth]{images/HH.png}
    \caption{The disease quickly infects the vast majority of the population and then folks slowly recover due to the slow recovery rate. When all is said and done, this may come closer to modelling the spread of COVID-19 through countries such as the US and Italy.}
    \label{fig:HH}
\end{figure}
\begin{figure}[H]
    \centering
    \captionsetup{justification=centering}
    \includegraphics[width=0.8\linewidth]{images/Ns.png}
    \caption{The disease spreads slowly, physically this might reflect the effects of social distancing or just a weakly interacting virus, and not much of the population is infected. Then, with a similarly slow recovery rate, the population eventually heals without everyone incurring the disease.}
    \label{fig:Ns}
\end{figure}
\begin{figure}[H]
    \centering
    \captionsetup{justification=centering}
    \includegraphics[width=0.8\linewidth]{images/NF.png}
    \caption{This disease has a hard time spreading from person to person, but recoveries happen rapidly, allowing most of the population to avoid ever coming in contact with the disease.}
    \label{fig:NF}
\end{figure}
\begin{figure}[H]
    \centering
    \captionsetup{justification=centering}
    \includegraphics[width=0.8\linewidth]{images/withD.png}
    \caption{This disease will quickly infect the population while the recovery rate will have people healing fairly slowly. However, the death rate removes about 20\% of the population before they can recover.}
    \label{fig:D}
\end{figure}

\end{document}


%% State Space Modelling of Dynamic Systems
%% Lecture 2: Transforms and Time Responses of State-Space Models
\def\FileDate{99/10/06}
\def\FileVersion{1.2}
% ----------------------------------------------------------------
% Notes pages *********************************************************
% ----------------------------------------------------------------

\begin{slide}
   \heading{Transforms and Time Responses for SS Models}
   \begin{itemize}
   	\item Laplace Transform of State Space Models
   	\item Time Responses for State Space Models
    \item Detailed example (in class)
   	\item Problems (homework)
   \end{itemize}
\end{slide}

\section*{Transforms of State Space Models} % (fold)
\label{sec:transforms_of_state_space_models}

The Laplace transform can be used to convert a differential equation
into a transfer function. It can also be used to convert a state space
model into a transfer function. In this lecture we demonstrate how
this is done and we give an example.

\ifslidesonly
\begin{slide}
  \heading{Laplace transform of a vector of functions}
  \input{frag1}
\end{slide}
\fi
\input{frag1}

\ifslidesonly
\begin{slide}
  \heading{Laplace transform of a vector of functions (cont)}
  \input{frag1a}
\end{slide}
\fi
\input{frag1a}

\ifslidesonly
\begin{slide}
  \heading{Example}
  \input{frag2}
\end{slide}
\fi
\input{frag2}
\ifslidesonly
  \begin{slide}
    \heading{Transform of state equations}
    \input{frag3}
  \end{slide}
\fi
\input{frag3}

For the system in the example the state vector is defined as
$\mathbf{x}=[v_{31}, i_1]^{T}$, the input current is $u$, and the output
variables are all the currents and voltages in the circuit
$\mathbf{y}=[v_{31}, i_1, v_{32}, v_{21}, i_2]^{T}$. The transformed
state space model is therefore that shown in
\sref{slide:slide-l2eg1}.
\begin{slide}\label{slide:slide-l2eg1}
\heading{Transformed State-Equations for Example 1 from L12}
\begin{eqnarray*} s\left[\begin{array}{c}
  V_{31} \\
  I_1
\end{array}\right]-\left[\begin{array}{c}
  v_{31}(0) \\
  i_1(0)
\end{array}\right]&=&\left[\begin{array}{cc}
  0 & -1/C \\
  1/L & -R/L
\end{array}\right]\left[\begin{array}{c}
  V_{31} \\
  I_1
\end{array}\right]+\left[\begin{array}{c}
  1/C \\
  0
\end{array}\right]\left[U\right]\\
\left[\begin{array}{c}
  V_{31} \\
  I_1 \\
  V_{32} \\
  V_{21} \\
  I_{2}
\end{array}\right]&=&\left[\begin{array}{cc}
  1 & 0 \\
  0 & 1 \\
  1 & -R \\
  0 & R \\
  0 & -1
\end{array}\right]\left[\begin{array}{c}
  V_{31} \\
  I_1
\end{array}\right]+\left[\begin{array}{c}
  0 \\
  0 \\
  0 \\
  0 \\
  1
\end{array}\right]\left[U\right].
\end{eqnarray*}
\end{slide}

\ifslidesonly
\begin{slide}
\heading{Transfer function from state space models (1)}
\input{frag4}
\end{slide}\fi\input{frag4}
\ifslidesonly
\begin{slide}\heading{Transfer function from state space models (2)}
\input{frag5}
\end{slide}\fi\input{frag5}
\ifslidesonly
\begin{slide}\heading{Transfer function from state space models (3)}
\input{frag6}
\end{slide}\fi\input{frag6}
\ifslidesonly
\begin{slide}\heading{Transfer function from state space models (4)}
\input{frag7}\end{slide}\fi\input{frag7}
\ifslidesonly
\begin{slide}\heading{The Resolvant Matrix}
\input{frag8}\end{slide}\fi\input{frag8}

For the system in the example, the resolvent matrix is developed
in \sref{slide:l2eg2}.
\begin{slide}\label{slide:l2eg2}
\heading{Resolvent matrix for the example}
\begin{eqnarray*}
\mathbf{A}&=&\left[\begin{array}{cc}
  0 & -1/C \\
  1/L & -R/L
\end{array}\right] \\
s\mathbf{I}-\mathbf{A}&=&\left[
\begin{array}{cc}
  s & 0 \\
  0 & s
\end{array}
\right] - \left[
\begin{array}{cc}
  0 & -1/C \\
  1/L & -R/L
\end{array}
\right] = \left[
\begin{array}{cc}
  s & +1/C \\
  -1/L & s+R/L
\end{array}\right] \\
\left[s\mathbf{I}-\mathbf{A}\right]^{-1}&=&\frac{
\left[\begin{array}{cc}
  s+R/L & -1/C \\
  +1/L & s
\end{array} \right]
}{s(s+R/L)+1/(LC)}=\frac{\left[\begin{array}{cc}
  s+R/L & -1/C \\
  +1/L & s
\end{array} \right]}{s^2 + (R/L)s + 1/(LC) }
\end{eqnarray*}
\end{slide}

When $\left[s\mathbf{I}-\mathbf{A}\right]^{-1}$ has been obtained,
then the system transfer function is easily obtained through
$\mathbf{C}\left[s\mathbf{I}-\mathbf{A}\right]^{-1}\mathbf{B}+\mathbf{D}$.
For the system in the example, when all outputs are measured, the
system transfer matrix is that shown in \sref{slide:l2eg3}.

\begin{slide}\label{slide:l2eg3}
\heading{Transfer matrix for example}
\begin{eqnarray*}
\left[\begin{array}{cc}
  1 & 0 \\
  0 & 1 \\
  1 & -R \\
  0 & R \\
  0 & -1
\end{array}\right]
\left\{\frac{\left[
\begin{array}{cc}
  s+R/L & -1/C \\
 +1/L & s
\end{array}\right]}{s^2 + (R/L)s +1/(LC)}\right\}
\left[\begin{array}{c}
  1/C \\
  0
\end{array}\right]
 &+& \left[\begin{array}{c}
  0 \\
  0 \\
  0 \\
  0 \\
  1
\end{array} \right]\\
= \left[\begin{array}{cc}
  1 & 0 \\
  0 & 1 \\
  1 & -R \\
  0 & R \\
  0 & -1
\end{array}\right]
\left\{\frac{\left[
\begin{array}{c}
  (1/C)s+R/(LC)  \\
 +1/(LC)
\end{array}\right]}{s^2 + (R/L)s +1/(LC)}\right\}
 &+& \left[\begin{array}{c}
  0 \\
  0 \\
  0 \\
  0 \\
  1
\end{array} \right]\\
\end{eqnarray*}
\end{slide}
\begin{slide}
\heading{Transfer matrix for example (completed)}
\begin{eqnarray*} &=& \frac{1}{s^2 + (R/L)s
+1/(LC)}\left[\begin{array}{c}
  (1/C)s+R/(LC) \\
  1/(LC) \\
  (1/C)s \\
  R/(LC) \\
  -1/(LC)
\end{array}\right] + \left[\begin{array}{c}
  0 \\
  0 \\
  0 \\
  0 \\
  1
\end{array} \right]\\
&=& \left[\begin{array}{c}
  \frac{(1/C)s+R/(LC)}{s^2 + (R/L)s
+1/(LC)} \\
  \frac{1/(LC)}{s^2 + (R/L)s
+1/(LC)} \\
  \frac{(1/C)s}{s^2 + (R/L)s
+1/(LC)} \\
  \frac{R/(LC)}{s^2 + (R/L)s
+1/(LC)} \\
  -\frac{1/(LC)}{s^2 + (R/L)s
+1/(LC)}+1
\end{array}\right]
\end{eqnarray*}
\end{slide}

In matrix form, when combined with the input and output transforms we
have the situation illustrated in \sref{slide:l2final}.
Each transfer function relates the corresponding output transform
to the input transform. For example
\[V_{31}= \frac{(1/C)s+R/(LC)}{s^2 + (R/L)s
+1/(LC)}\ U.\]

\begin{slide}
  \label{slide:l2final}
  \heading{Transform Equations for Example}
\begin{eqnarray*}
\mathbf{Y}(s)&=&\left[\mathbf{C}\left[s\mathbf{I}-\mathbf{A}\right]^{-1}\mathbf{B}+\mathbf{D}\right]\mathbf{U}(s)\\
\mathbf{Y}(s) &=& \mathbf{G}(s)\mathbf{U}(s)\\
\left[\begin{array}{c}
  V_{31}(s) \\
  I_1(s) \\
  V_{32}(s) \\
  V_{21}(s) \\
  I_{2}(s)
\end{array}\right] &=& \left[\begin{array}{c}
  \frac{(1/C)s+R/(LC)}{s^2 + (R/L)s
+1/(LC)} \\
  \frac{1/(LC)}{s^2 + (R/L)s
+1/(LC)} \\
  \frac{(1/C)s}{s^2 + (R/L)s
+1/(LC)} \\
  \frac{R/(LC)}{s^2 + (R/L)s
+1/(LC)} \\
  -\frac{1/(LC)}{s^2 + (R/L)s
+1/(LC)}+1
\end{array}\right]\mathbf{U}(s).
\end{eqnarray*}
\end{slide}
Note that the denominator is the same for each transfer function,
and that the order of the numerator is less than the denominator
except for one case, for which
\begin{eqnarray*}
I_2 & = & \left(-\frac{1/(LC)}{s^2 + (R/L)s +1/(LC)}+1\right)U\\
    & = & \frac{-1/{LC}+s^2 + (R/L)s +1/(LC)}{s^2 + (R/L)s
    +1/(LC)}\ U
    \\
    & = &\frac{s^2 + (R/L)s}{s^2 + (R/L)s
    +1/(LC)}\ U
\end{eqnarray*}
Replacing $s$ by $\frac{d}{dt}$ gives the corresponding
differential equations relating the dependant variable to the
input.

\begin{slide}
	\heading{Converting SS to TF in Matlab}
	Continuing example from previous lecture
	\begin{verbatim}
		circ_tf = tf(circ_ss)
	\end{verbatim}
	Returns a vector of transfer functions.
\end{slide}

\subsection*{Some Important Properties}

\subsubsection*{System poles}

Clearly the denominator of the transfer function is generated by the matrix inverse which produces the term:
$$\det[s\mathbf{I}-\mathbf{A}]$$

This evaluates to the denominator polynomial and the poles of the system are the roots of the system's characteristic equation:
$$\det[s\mathbf{I}-\mathbf{A}] = 0.$$
\ifslidesonly
\begin{slide}
	\heading{System Poles}
	The system poles are solutions to the system's characteristic equation
	$$\det[s\mathbf{I}-\mathbf{A}] = 0.$$
\end{slide}
\fi

\subsubsection*{System zeros}

What is the corresponding numerator polynomial of the transfer function, whose roots give the zeros of the system?
The zeros are those values of $s$ for which the output is zero when the input and states are not zero. Thus:
\begin{eqnarray*}
(s\mathbf{I}-\mathbf{A})\mathbf{X}-\mathbf{B}U & = & \mathbf{0} \\
\mathbf{CX}+dU = Y & = & 0
\end{eqnarray*}
\input{frag9}
\ifslidesonly
\begin{slide}
	\heading{System Zeros (1)}
	Zeros are those values of $s$ for which the system output is zero when the input and states are not zero
	\begin{eqnarray*}
		(s\mathbf{I}-\mathbf{A})\mathbf{X}-\mathbf{B}U & = & \mathbf{0} \\
		\mathbf{CX}+dU = Y & = & 0
	\end{eqnarray*}
	\input{frag9}
\end{slide}
\fi

\input{frag10}
\ifslidesonly
\begin{slide}
	\heading{System Zeros (2)}
	\input{frag10}
\end{slide}
\fi

\input{frag11}
\ifslidesonly
\begin{slide}
	\heading{System Zeros (3)}
	\input{frag11}
\end{slide}
\fi

\begin{slide}
	\heading{Determine Poles and Zeros in Matlab}
	\begin{verbatim}
		circ_zpk = zpk(circ_ss)
	\end{verbatim}
\end{slide}

%% State Space Modelling of Dynamic Systems
%% Time Response of a State-Space Model

\section*{Time Response for State Space Models} % (fold)
\label{sec:time_response_for_state_space_models}

In this lecture we shall determine the time response of a system
represented by a state-space model. We shall take as a starting
point the transformed state space model developed in the last
lecture. It is possible to derive the time response directly in
the time domain from the state-equations. I believe however, that
the following development will be easier to understand. I advise
you to consult any text book for the time domain development.

In the last lecture, we showed that the state space model was
\ssmodeltd{} and that the transformed equation was  \ssmodelfd{}
In this lecture we will show how the time response of the state
space model may be derived from the transformed model.

\subsection*{Zero Input Response}
\label{ssec:zero_input_response} We first consider the response of
the system to its initial conditions and zero input. This is the
\emph{zero input response} or homogeneous response of the system.
With the input transform $U(s)=0$ the state equation becomes
\[\mathbf{X}(s)=\resolvent{}\mathbf{x}(0).\] We define
\[\Phis{} = \resolvent{}\] so that
\begin{equation}\label{eq:l4eq1}
\mathbf{X}(s)=\Phis{}\mathbf{x}(0)
\end{equation} is the
transform of the zero input state response. In the time domain,
the corresponding state response is given by the inverse Laplace
transform of equation~(\ref{eq:l4eq1})
\begin{equation}
\label{eq:l4eq2} \mathbf{x}(t) = \mathcal{L}^{-1}
\left\{\Phis{}\mathbf{x}(0)\right\},\end{equation} which, since
the initial condition vector $\mathbf{x}(0)$ is a vector of
constants becomes:
\begin{eqnarray}
\mathbf{x}(t) & = & \mathcal{L}^{-1}
\left\{\Phis{}\right\}\mathbf{x}(0)\nonumber \\ & = &
\phit{}\mathbf{x}(0).\label{eq:l4eq3}
\end{eqnarray}
\subsection*{State Transition
Matrix}\label{ssec:state_transition_matrix} The vector of time
functions $\phit{}$ is known as the \emph{state transition
matrix}. It defines the motion (or \emph{trajectory}) of the state
variables through the $n$-dimensional \emph{state space} from a
given set of initial states.
\begin{slide}\label{slide:l4slide1}
\heading{State Transition Matrix} The \emph{resolvent matrix} is
defined as \[\Phis{} = \resolvent =
\frac{\adjsIminusA}{\detsIminusA}.\] The resolvent matrix is a
matrix of rational polynomials in the Laplace transform variable
$s$. The state transition matrix is
\[\phit{}
=
\mathcal{L}^{-1}\left\{\frac{\adjsIminusA}{\detsIminusA}\right\}.\]
What is the form of this function?
\end{slide}
\begin{slide}\label{slide:l4slide2}
\heading{Characteristic Polynomial} The adjoint matrix
$\adjsIminusA$ is a matrix of polynomials each of which is of
order $n-1$ or less. The determinant
\[\detsIminusA{} = \left|\begin{array}{cccc}
  s-a_{11} & -a_{12} & \cdots & -a_{1n} \\
  -a_{21} & s-a_{22} & \cdots & -a_{2n} \\
  \vdots & \vdots & \ddots & \vdots \\
  -a_{n1} & -a_{2n} & \cdots & s-a_{nn}
\end{array}\right|\] is called the \emph{characteristic polynomial}. It
is always a polynomial of degree $n$. Therefore each element of
the resolvent matrix $\Phis{}$ is of the form
\[\frac{b_{n-1}s^{n-1}+ \cdots + b_0}{s^n + a_{n-1}s^{n-1}+\cdots+a_0}.\]
\end{slide}
\begin{slide}\label{slide:l4slide3}
\heading{System Characteristic (or Eigen) Values} The coefficients
$b_i$ of each element of the resolvent matrix $\Phis{}$ depend on
the form of the system. The characteristic polynomial
$\detsIminusA$ is the common denominator of all elements of
$\Phis{}$. It may be factorized:
\begin{eqnarray*}
\detsIminusA{}&=&s^n + a_{n-1}s^{n-1} + \cdots + a_1s + a_0
\\
&=&(s - p_1)(s-p_2)\cdots(s-p_n)
\end{eqnarray*}
The values $p_i$ are known as the \emph{characteristic} (or
\emph{eigen}) values of the state matrix $\mathbf{A}$. The
characteristic values are the \emph{poles} of the
system!
\end{slide}
\begin{slide}\label{slide:l4slide4}
\heading{The Zero Input Response} If we make a partial fraction
expansion of the characteristic equation, then for the $i$-$j$th
element of the resolvent matrix we have
\[\Phi_{ij}(s)=\frac{r_1}{s-p_1}+\frac{r_2}{s-p_2} + \cdots +
\frac{r_n}{s-p_n}\](where the coefficient $r_i$ is the so called
\emph{residue} of the partial fraction expansion determined at the
value $s=p_i$). Inverse Laplace transforming this equation we
finally obtain the \emph{state transition} function:
\[\phi_{ij}(t) = r_1 e^{p_1 t} + r_2 e^{p_2 t} + \cdots + r_n
e^{p_n t}.\]
\end{slide}
\begin{slide}\label{slide:l4slide5}
\heading{System ``Modes''} The state transition matrix $\phit{}$
is an $n\times n$ matrix, each element of which is a linear
combination of the $n$ ``mode functions'' (or simply \emph{modes})
\[e^{p_1 t},\ e^{p_2 t},\ \ldots,\ e^{p_n t}.\] Each mode is constructed from the
eigen values ($p_1,\ p_2,\ \ldots,\ p_n$) of the state matrix
$\mathbf{A}$. The eigen values are also known as the \emph{poles}
of the system.
\end{slide}
\subsection*{Example}
If \[\mathbf{A} = \left[\begin{array}{cc}
  0 & 1 \\
  -6 & -5
\end{array}\right]\] \[\sIminusA = \left[\begin{array}{cc}
  s & -1 \\
  6 & s+5
\end{array}\right]\] so \[\detsIminusA = s^2 + 5s + 6 = (s+2)(s+3).\]
The characteristic values of the system are $p_1=-2$ and $p_2=-3$
so the modes are $e^{-2t}$ and $e^{-3t}$.

Now \[\adjsIminusA = \left[\begin{array}{cc}
  s+5 & 1 \\
  -6 & s
\end{array}\right]\] so the resolvent matrix is \[\Phis{} = \left[\begin{array}{cc}
  \frac{s+5}{(s+2)(s+3)} & \frac{1}{(s+2)(s+3)} \\
  \frac{-6}{(s+2)(s+3)} & \frac{s}{(s+2)(s+3)}
\end{array}\right].\] Expanding each element of $\Phis$ we get
\[\Phis{} = \left[\begin{array}{cc}
  \frac{3}{s+2}-\frac{2}{s+3} & \frac{1}{s+2}-\frac{1}{s+3} \\
  \frac{-6}{s+2}+\frac{6}{s+3} & -\frac{2}{s+2}+\frac{3}{s+3}
\end{array}\right].\] Inverse Laplace transforming this we get the final state
transition matrix, representing the zero-input response of the
system: \[\phit{}=\left[\begin{array}{cc}
  3e^{-2t}-2e^{-3t} & e^{-2t}-e^{-3t} \\
 -6e^{-2t}+6e^{-3t} & -2e^{-2t}+3e^{-3t}
\end{array}\right].\]

Note that, by an alternative derivation\footnote{%
For a first order differential equation \[\frac{dx}{dt}=ax\] so
the Laplace transform is \begin{eqnarray*}sX(s) - x(0) & = &
aX(s)\\ X(s)(s - a)& = & x(0)\\ X(s)
&=&\frac{x(0)}{s-a}.\end{eqnarray*} The inverse laplace transform
of the final equation gives \[x(t) = e^{at}.\] For the vector case
\[s\mathbf{X}(s)-\mathbf{x}(0)=\mathbf{A}\mathbf{X(s)}\] so by comparison
\[\mathbf{x}(t)=e^{\mathbf{A} t}.\] The proper proof is of course
a little more involved!}, we can show that
\[\phit{}=e^{\mathbf{A}t}\] where $e^{\mathbf{A}t}$ is called the matrix exponential.

\subsection*{Unforced system response}
To determine the response of the system to the initial state we recall that
\[\mathbf{x} = \mathbf{B}\mathbf{\Phis} \mathbf{x}(0)\]
 and putting this together with the definition of $\mathbf{Y(s)}$ we can
 determine the unforced system response of a system.

\begin{slide} \label{slide:l4s11a}
\heading{Zero Input System Response}
For non-zero initial conditions and zero input we have
\begin{eqnarray}\label{eq:l4eq6a}
  \mathbf{X}(s)&=&\resolvent{}\mathbf{x}(0) \\
&=& \Phis{}\mathbf{x}(0) \label{eq:l4eq7a}
\end{eqnarray}
The zero-input system output transform is therefore given by
\begin{equation}\label{eq:l4eq8a}
 \mathbf{Y}_\mathrm{zi}(s)=\mathbf{C}\Phis{}\mathbf{x}(0).
\end{equation}
\end{slide}
Because we have already shown that $\phi(t)$ will be a linear combination of modes,
the unforced system response $y(t)$ will be another linear combination of the same modes.

\subsection*{The Zero State Response}
We now consider the case where the initial condition vector
$\mathbf{x}(0)=\mathbf{0}$ and we will obtain the response of the
system to an input. This is called the \emph{zero state response}
of the system.

\begin{slide} \label{slide:l4s11}
\heading{Zero State Response}
For zero initial conditions we have
\begin{eqnarray}\label{eq:l4eq6}
  \mathbf{X}(s)&=&\resolvent{}\mathbf{B}\mathbf{U}(s) \\
&=& \Phis{}\mathbf{B}\mathbf{U}(s) \label{eq:l4eq7}
\end{eqnarray}
The system output transform (forced response) is given by
\begin{equation}\label{eq:l4eq8}
 \mathbf{Y}_{\mathrm{zs}}(s)=\left[\mathbf{C}\Phis{}\mathbf{B}+\mathbf{D}\right]\mathbf{U}(s)
\end{equation} (where $\mathbf{C}\Phis{}\mathbf{B}+\mathbf{D}$ is the system transfer
matrix).
\end{slide}

\subsection*{Full System Response}
\ifslidesonly
\begin{slide}
\heading{Full System Response}
\input{frag12}
\end{slide}
\fi
\input{frag12}
\ifslidesonly
\begin{slide}
\heading{End of Pre-Class Presentation}
In the class we will reinforce these concepts by working through an example in detail.
\end{slide}
\fi
\begin{slide}\label{slide:l4s12}
\heading{Example} Let
\begin{eqnarray*}\dot{\mathbf{x}}&=&\left[\begin{array}{cc}
  0 & 1 \\
  -6 & -5
\end{array}\right]\mathbf{x}+\left[\begin{array}{cc}
  2 & 1 \\
 -1 & 0
\end{array}\right]\mathbf{u}\\
\mathbf{y}&=&\left[\begin{array}{cc}
  2 & -1 \\
  0 & 1
\end{array}\right]\mathbf{x}\\ \mathbf{u}&=&\left[\begin{array}{c}
  e^{-t} \\
  \epsilon(t)
\end{array}\right].
\end{eqnarray*}
Calculate the full system response for this system given that the initial conditions
are $\mathbf{x}(0)=[-1, 1]^T$.
\end{slide}

\subsection*{Solution}
The transfer
function is \begin{eqnarray*}
\mathbf{G}(s)&=&\mathbf{C}\Phis{}\mathbf{B} =
\left[\begin{array}{cc}
  2 & -1 \\
  0 & 1
\end{array}\right]
\left[\begin{array}{cc}
  \frac{s+5}{(s+2)(s+3)} & \frac{1}{(s+2)(s+3)} \\
  \frac{-6}{(s+2)(s+3)} & \frac{s}{(s+2)(s+3)}
\end{array}\right]\left[\begin{array}{cc}
  2 & 1 \\
 -1 & 0
\end{array}\right]\\
&=&\left[\begin{array}{cc}
  2 & -1 \\
  0 & 1
\end{array}\right]
\left[\begin{array}{cc}
  \frac{2s+9}{(s+2)(s+3)} & \frac{s+5}{(s+2)(s+3)} \\
  \frac{-s-12}{(s+2)(s+3)} & \frac{-6}{(s+2)(s+3)}
\end{array}\right]\\
&=& \left[\begin{array}{cc}
  \frac{5s+30}{(s+2)(s+3)} & \frac{2s+16}{(s+2)(s+3)} \\
   \frac{-s-12}{(s+2)(s+3)} & \frac{-6}{(s+2)(s+3)}
\end{array}\right]
\end{eqnarray*}

The transform of the input vector is
\[\mathbf{U}(s) = \mathcal{L}\left[\begin{array}{c}
  e^{-t} \\
  \epsilon(t)
\end{array}\right] = \left[\begin{array}{c}
  \frac{1}{s+1} \\
  \frac{1}{s}
\end{array}\right].\] Hence
\begin{eqnarray*}\mathbf{Y}(s) &=& \left[\begin{array}{cc}
  \frac{5s+30}{(s+2)(s+3)} & \frac{2s+16}{(s+2)(s+3)} \\
   \frac{-s-12}{(s+2)(s+3)} & \frac{-6}{(s+2)(s+3)}
\end{array}\right]\left[\begin{array}{c}
  \frac{1}{s+1} \\
  \frac{1}{s}
\end{array}\right]\\
&=&\left[\begin{array}{c}
  \frac{7s^2+48s+16}{s(s+1)(s+2)(s+3)} \\
  \frac{-(s^2+18s+6)}{s(s+1)(s+2)(s+3)}
\end{array}\right] = \left[\begin{array}{c}
  \frac{8/3}{s}+\frac{25/2}{s+1}-\frac{26}{s+2}+\frac{65/6}{s+3} \\
   -\frac{1}{s}-\frac{11/2}{s+1}+\frac{13}{s+2}-\frac{13/2}{s+3}
\end{array}\right]
\end{eqnarray*}

Inverse Laplace transforming the
previous result gives the zero-state output response of the
system: \[\left[\begin{array}{c}
  \frac{8}{3}\epsilon(t)+\frac{25}{2}e^{-t}-26e^{-2t}+\frac{65}{6}e^{-3t} \\
   -\epsilon(t)-\frac{11}{2}e^{-t}+13e^{-2t}-\frac{13}{2}e^{-3t}
\end{array}\right].\]
Note that response is now a linear combination of the system modes
($e^{-2t}$ and $e^{-3t}$) and the input modes ($\epsilon(t)$ [the
unit step function] and $e^{-t}$).

If the initial condition vector $\mathbf{x}(0)=[-1, 1]^T$ what is
the total response of the system?

\begin{eqnarray*} \mathbf{Y}(s)&=&
\left[\begin{array}{cc}
  2 & -1 \\
  0 & 1
\end{array}\right]
\left[\begin{array}{cc}
  \frac{s+5}{(s+2)(s+3)} & \frac{1}{(s+2)(s+3)} \\
  \frac{-6}{(s+2)(s+3)} & \frac{s}{(s+2)(s+3)}
\end{array}\right]\\
&\times& \left\{\left[\begin{array}{c}
  -1 \\
  1
\end{array}\right]+\left[\begin{array}{cc}
  2 & 1 \\
  -1 & 0
\end{array}\right]\left[\begin{array}{c}
  \frac{1}{s+1} \\
  \frac{1}{s}
\end{array}\right]\right\}\\
&=& \left[\begin{array}{cc}
  \frac{2s+16}{(s+2)(s+3)} & \frac{-s+2}{(s+2)(s+3)} \\
  \frac{-6}{(s+2)(s+3)} & \frac{s}{(s+2)(s+3)}
\end{array}\right]\left[\begin{array}{c}
  \frac{-s^2+2s+1}{s(s+1)} \\
  \frac{s^2}{s(s+1)}
\end{array}\right]
\end{eqnarray*}

\begin{eqnarray*}
\mathbf{Y}(s)&=& \left[\begin{array}{c}
  \frac{-3s^2-10s^2+34s+16}{s(s+1)(s+2)(s+3)} \\
  \frac{s^3+6s^2-12s-6}{s(s+1)(s+2)(s+3)}
\end{array}\right]\\
\mathbf{y}(t)&=& \left[\begin{array}{c}
  \frac{8}{3}\epsilon(t)+\frac{25}{2}e^{-t}-34e^{-2t}+\frac{59}{6}e^{-3t} \\
   -\epsilon(t)-\frac{11}{2}e^{-t}+17e^{-2t}-\frac{19}{2}e^{-3t}
\end{array}\right].
\end{eqnarray*}

\begin{slide}
\heading{Solving this problem in Matlab}
We can use the symbolic toolbox!
\begin{verbatim}
A = [0, 1; -6, -5]; B = [2, 1; -1, 0];
C = [2, -1; 0, 1]; D = [0, 0; 0, 0];
x0 = [-1, 1]';
syms t s
u = [exp(-t),heaviside(t)];
U = laplace(u)
PhiS = inv(s*eye(2) - A)
Yzs = (C*PhiS*B + D)*Us
Yzi = C*PhiS*x0
Ytotal = Yzs + Yzi
y = ilaplace(Ytotal)
\end{verbatim}
\end{slide}

\begin{slide}
	\heading{Simulation of State Space Models in Matlab}
\begin{itemize}
	\item The functions \emph{step} and \emph{impulse} will produce step impulse responses of a system with zero initial conditions.
	\item The function \emph{lsim} can be used to determine the state space response to arbitrary inputs.
	\item If you need to simulate a system with arbitrary inputs and predefined initial conditions, you should use the \emph{Linear Simulation Tool} which is opened when you execute \texttt{lsim(model)}.
	Visit the Matlab Control Systems Toolbox help pages to find out more.
\end{itemize}
\end{slide}

\section*{Problems}
\begin{enumerate}
\item Prove that the impulse response of a state-space model is
\begin{eqnarray*}
\mathbf{g}(t)&=&\mathcal{L}^{-1}\left\{\mathbf{C}\Phis{}\mathbf{B}+\mathbf{D}\right\}\\
&=&\mathcal{L}^{-1}\left\{\mathbf{G}(s)\right\}
\end{eqnarray*}
\item \label{l4q1}
For the circuit example introduced in Lecture 13, if $L=100$~mH,
$C=1000$~$\mu$F and $R=20$~$\Omega$, and the output is taken to be
the voltage $v_{21}(t)$ across the resistor, determine the
following:
\begin{enumerate}
\item The state transition matrix
\item The zero input response given an initial voltage across the
capacitor $v_{32}(0)=10$ V and assuming the current in the
inductor is initially $0$ A.
\item The zero state output and state variable responses given an input current of
$i=0.5e^{-t}$ \item The total output response
\end{enumerate}
\item Use state-space methods to find the total solution to the
differential equation
$$\frac{d^2(t)}{dt^2}+20\frac{dy(t)}{dt}+10y(t) = 16u(t)$$ given
the initial conditions $\frac{dy(0)}{dt}=1$ and $y(0)=0$ and
$u(t)=\epsilon(t)$.
\item Show that the state space models
\begin{eqnarray*}\dot{\mathbf{x}}&=&\left[\begin{array}{cc}
  0 & 1 \\
  -100 & -200
\end{array}\right]+\left[\begin{array}{c}
  0 \\
  200000
\end{array}\right]u\\ y&=&[1, 0]\mathbf{x}
\end{eqnarray*} and \begin{eqnarray*}\dot{\mathbf{x}}&=&\left[\begin{array}{cc}
  -0.50126 & 0 \\
  0 & -199.5
\end{array}\right]+\left[\begin{array}{c}
  1124.2 \\
  2.0051\times 10^{5}
\end{array}\right]u\\ y&=&[0.89398   -0.0050125]\mathbf{x}
\end{eqnarray*} have the same
input-output relationship (transfer function). Construct the state
transition matrix $\phit$ and the zero state and zero input output
response equations for both systems and observe how the modes of
the system are represented in the system output equations. Compare
the result with that of question~\ref{l4q1} which has the same
transfer function! What does this tell you about the transfer
function as a representation of a system?
\end{enumerate}

% section time_response_for_state_space_models (end)

%----------------------------------------------------------------
% The end of slides
% ----------------------------------------------------------------
\endinput

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "#"
%%% End:


\chapter{Mediciones y Resultados}
This chapter will summarize the measurements made and the results obtained after the development of the biosensors. It will be divided into three procedures differentiated by the type of characterization: electrical, dimensional and electrochemical.

\section{Caracterización eléctrica}
Four-point resistance measurements were made obtaining the following results:

$\bullet$ Between two carbon pads: \textbf{2,2 k$\Omega$}.

$\bullet$ Same carbon pads with a layer of cured gold nanoparticles ink: \textbf{1,15 k$\Omega$}.

$\bullet$ Same carbon pads with two layers of cured gold nanoparticles ink: \textbf{500 $\Omega$}.

Given the deposition of the gold ink layer on the carbon, a parallel is formed between the two elements. Using the equation of resistors in parallel (\ref{ecuacion1}), the resistance of the one and two cured layers of gold nanoparticles can be calculated.

\begin{equation}\label{ecuacion1}
R_{1//2}=\frac{R_{1} \times R_{2}}{R_{1}+R_{2}}
\end{equation}

De esta forma se obtienen los siguientes valores:

$\bullet$ A layer of cured gold nanoparticles: \textbf{2,4 k$\Omega$}.

$\bullet$ Two layers of cured gold nanoparticles: \textbf{0,65 $\Omega$}.

Assuming that the gold layers are thin film prints, the equation (\ref{ecuacion2}) is used to obtain the resistivity of the material,

\begin{equation}\label{ecuacion2}
\rho_{Bulk}=\frac{R \times w \times t}{L}
\end{equation}

where $\rho$\textsubscript{Bulk} is the resistivity, $R$ the resistance, $w$ the width, $t$ the thickness and $L$ the length of the material. In this case, the gold nanoparticles path. The width of the design is 0.04 cm, the average thickness for two layers 0.00055 cm (obtained by profilometer) and the length 1.05 cm giving the following results:

$\bullet$ A layer of cured gold nanoparticles ink (assuming half the thickness): \textbf{0,025 $\Omega$ $\times$ cm}

$\bullet$ Two layers of cured gold nanoparticles ink: \textbf{0,0000136 $\Omega$ $\times$ cm o 13,6 $\mu\Omega$ $\times$ cm}

According to the materials resistivity table, pure gold has a resistivity of \textbf{2,35 $\mu\Omega$ $\times$ cm} \cite{Resistividad}.

\section{Dimensional characterization}
After optically measuring the eight electrodes in a cartridge, the diameter was averaged at \textbf{993 $\mu$m} and the separations between elements of the electrochemical cell at \textbf{390 $\mu$m}.

The table of the figure~\ref{fig:Figura_tabla_rugosidades} shows the results of the roughness obtained.

\begin{figure}[H]
  \centering
    \includegraphics[width=0.6\textwidth]{Figures/Figura_tabla_rugosidades}
  \caption{Roughness values obtained for two \emph{WE} from two biosensors (nPoc3 and nPoc5).}
  \label{fig:Figura_tabla_rugosidades}
\end{figure}

The thicknesses of the same \emph{WE} were measured on the same biosensors. In order to obtain the estimated thickness of the 2 layers printed with the gold nanoparticles ink, the thickness was measured on a pure carbon electrode.

The table of the figure~\ref{fig:Figura_tabla_espesores} shows the values obtained from the thicknesses.

\begin{figure}[H]
  \centering
    \includegraphics[width=0.6\textwidth]{Figures/Figura_tabla_espesores}
  \caption{Thickness values obtained for two \emph{WE} of two biosensors (nPoc3 and nPoc5).}
  \label{fig:Figura_tabla_espesores}
\end{figure}

The tables were extracted from the report made at the Center for Physics and Metrology of the National Institute of Industrial Technology \cite{caracdimen}.

\section{Caracterización electroquímica}
The first cyclic voltammetry was performed with a pure gold electrode deposited on silicon using a \textit{Sputtering} process. The response for the ferro and ferricyanide electrochemical probes on a \textit{sputtering} deposited gold electrode was obtained (Figure ~\ref{fig:Figura_EQ_Oro_Sputtering_1mm}), which will be used as a reference to compare with electrodes printed with gold nanoparticles ink using the \textit{Inkjet} printing process.

\begin{figure}[H]
  \centering
    \includegraphics[width=0.8\textwidth]{Figures/Figura_EQ_Oro_Sputtering_1mm}
  \caption{Cyclic voltammetry with gold electrode obtained by Sputtering.}
  \label{fig:Figura_EQ_Oro_Sputtering_1mm}
\end{figure}

As a second measurement, it was decided to observe the behavior of the electrochemical probes on a carbon electrode (Figure ~\ref{fig:Figura_EQ_Carbono}), thus having the two curves that would overlap when testing a carbon electrode covered by the ink of gold nanoparticles.

\begin{figure}[H]
  \centering
    \includegraphics[width=0.8\textwidth]{Figures/Figura_EQ_Carbono}
  \caption{Cyclic voltammetry with carbon electrode.}
  \label{fig:Figura_EQ_Carbono}
\end{figure}
The next electrochemical measurement was performed on a carbon electrode coated with two layers of gold nanoparticle ink with 1 mm (Figure ~\ref{fig:Figura_EQ_Oro_Inkjet_Ambos} (a)) and 1.1 mm (Figure ~\ref{fig:Figura_EQ_Oro_Inkjet_Ambos} (b)) in diameter.

Comparing the result of the cyclical voltammetric curve of the electrochemical cell with gold \emph{WE} deposited by \textit{Sputtering} with the \emph{WE} with gold nanoparticles ink (Figure ~\ref{fig:Figura_EQ_Sputt_2-Inkjet}), the similarity is observed maintaining the current peaks corresponding to the gold.

\begin{figure}[H]
  \centering
    \includegraphics[width=1\textwidth]{Figures/Figura_EQ_Oro_Inkjet_Ambos}
  \caption{Cyclic voltammetry with carbon \emph{WE} coated with gold nanoparticles ink of different diameters: (a) 1 mm; (b) 1.1 mm.}
  \label{fig:Figura_EQ_Oro_Inkjet_Ambos}
\end{figure}

\begin{figure}[H]
  \centering
    \includegraphics[width=0.8\textwidth]{Figures/Figura_EQ_Sputt_2-Inkjet}
  \caption{Comparison of cyclic voltammetries of electrochemical cells with gold \emph{WE} deposited by \textit{Sputtering} and with carbon \emph{WE} with gold nanoparticles ink.}
  \label{fig:Figura_EQ_Sputt_2-Inkjet}
\end{figure}

The increase of intensity in the electrodes with gold \textit{inkjet} printing is due to the presence of carbon underneath it, as can be verified by overlapping the previous curves with that of pure carbon (Figure ~\ref{fig:Figura_EQ_Oro_Inkjet_Ambos}) and also, the increase in the effective area of the \emph{WE} due to the difference in roughness between the gold deposited by \textit{sputtering}  and the ink of gold nanoparticles printed by \textit{inkjet}.

\begin{figure}[H]
  \centering
    \includegraphics[width=0.8\textwidth]{Figures/Figura_EQ_Sptt_AuInkjet_Carbono_Valox}
  \caption{Overlapping \emph{WE} with gold deposited by \textit{Sputtering}, carbon \emph{WE} and carbon \emph{WE} with gold nanoparticles ink.}
  \label{fig:Figura_EQ_Sptt_AuInkjet_Carbono_Valox}
\end{figure}

To have comparisons of the nanoparticles ink on different substrates, measurements of the \textit{Inkjet} printed ink were made on Valox, PET and cellulose pulp sheet (Figure ~\ref{fig:Figura_EQ_Inkjet_Valox_PET_Papel}).

\begin{figure}[H]
  \centering
    \includegraphics[width=1\textwidth]{Figures/Figura_EQ_Inkjet_Valox_PET_Papel}
  \caption{Results of cyclic voltammetry on \textit{inkjet} printing of ink with gold nanoparticles on: (a) Valox; (b) PET and (c) Paper.}
  \label{fig:Figura_EQ_Inkjet_Valox_PET_Papel}
\end{figure}

The comparable results obtained between the gold deposited by \textit{Sputtering} and the \textit{Inkjet} printing on smooth substrate (PET) stands out (Figure ~\ref{fig:Figura_EQ_Sputt_Inkjet_PET}).

\begin{figure}[H]
  \centering
    \includegraphics[width=0.8\textwidth]{Figures/Figura_EQ_Sputt_Inkjet_PET}
  \caption{Comparison of cyclic voltammetry on \emph{WE} of gold deposited by \textit{Sputtering} and \textit{inkjet} printing of ink with gold nanoparticles on PET.}
  \label{fig:Figura_EQ_Sputt_Inkjet_PET}
\end{figure}


\subsection*{b)}

Here we construct our hybrid MCMC algorithm to sample from the posterior $f(\theta,\boldsymbol{\lambda},\boldsymbol{t}|\boldsymbol{\tau})$. All components except the breakpoints $\boldsymbol{t}$ have been updated using Gibbs sampling, which is explained in further down in this section. The Metropolis--Hastings algorithm is explained below and has been used for the posterior of $\boldsymbol{t}$. Where we, as already mentioned, used a symmetric proposal for the proposal kernel. We will now briefly discuss the two different algorithms used for sampling.
\begin{itemize}
\item{The \textbf{Metropolis--Hastings} algorithm is an algorithm that is used for sampling from high--dimensional and/or complicated distributions that are for example only known up to a normalizing constant. The idea behind the MH algorithm is to choose a transition density $q$ almost arbitrarily. However, this density will not give the desired asymptotic distribution $f$, i.e. the distribution from which you wish to sample. We correct this by introducing a new transition density $\hat{q}$ using $q$ and a probability $\alpha(x,x^*)$ where $x$ is the current level and $x^*$ is a proposal. The proposal $x^*$ is rejected with probability $1 - \alpha(x,x^*)$. In order for the global balance equation to be satisfied, we must have that $\alpha(\cdot,\cdot)$ is defined as
\[\alpha(x,x^*) = \min \left ( 1, \frac{f(x^*)q(x|x^*)}{f(x)q(x^*|x )} \right ) .\]
Since we now have a quotient, we can easily sample from distributions known only up to normalizing constants.
In our case we have, as already mentioned, a symmetric proposal. Which practically means that $q(x^*|x ) = q(x|x^* )$ and the two right terms of the quotient cancel out. The MH algorithm is explained briefly in figure \ref{fig:metrohast}.
\begin{figure}[H]
  \centering
    \includegraphics[scale=0.34]{./Figures/metrohast.png}
  \caption[An Electron]{Overview of how the Metropolis--Hastings algorithm works. The figure is taken from the lecture notes by Martin Sk\"old.}
  \label{fig:metrohast}
\end{figure}
}

\item{The \textbf{Gibbs sampler} algorithm is a Markov Chain Monte Carlo (MCMC) algorithm for obtaining a sequence of observations, which in turn can be approximated from a multivariate probability distribution. Typically the Gibbs sampler is used to approximate joint distributions, marginal distributions of one variable or computing an integral, when some of the variables are known observations, and is not in need of sampling. As this a MCMC algorithm, Gibbs sampling generates a Markov Chain of samples, hence the beginning of the chain may not accurately describe the desired distribution and one may want to remove the burn-in of the chain. Below a brief description of the algorithm is presented

\begin{enumerate}
	\item{Assuming we want to sample from multivariate distribution f on X. Assuming that X can be divided into blocks.}
	\item{We denote $x^k$ by the \textit{k}th component and $x^{-k}$ by the remaining components. Assuming that it is easy to simulate from $f_k(x^k|x^{-k}),\forall k$, i.e. the conditional distribution of $X^k$ given the other components.}
	\item{Now, simulating a sequence $X_k$, forming a Markov Chain on X, with the following properties: Given $X_k$, draw $X_{k+1}^n \sim f_n(x^n|x^{-n})$.}
\end{enumerate}
}
\end{itemize}

%\textit{Random walk proposal:} update one breakpoint at a time. For each breakpoint $t_i$ we generate a candidate $t_i^*$ according to

%\begin{equation*}
%t_i^*=t_i+\epsilon, \quad \text{with} \quad \epsilon \sim U(-R,R), \quad R=\rho(t_{i+1}-t_{i-1}
%\end{equation*}
The symmetric proposal yields a correct MCMC algorithm, as long as the proposal kernel specifies an irreducible aperiodic Markov Chain. The algorithm will generate a reversible Markov Chain with stationary distribution $\pi$, assuming that $\chi$ is a subset of $\mathbb{R}^k$ and the transition kernel $r(z|x)$ and $\pi(z)$ are densities on $\chi$. \\

The proposed kernel yields a correct MCMC as we have introduced a symmetric proposal, thus
\begin{equation}
r(z|x)=r(x|z), \quad \forall (x,z) \in t
\end{equation}
%http://dept.stat.lsa.umich.edu/~yvesa/afmp.pdf
implying that we can reach each state from any state and is non-periodic, i.e. an irreducible aperiodic Markov Chain.


\documentclass[letterpaper, 12pt]{article}

\usepackage{preamble}

\begin{document}
\pagestyle{fancy}
\lhead{MAT1002, Problems}

\section{Ivrii Problems}
\begin{enumerate}
    \item
    Show the identities
    \begin{enumerate}
        \item \[\sum_{n = -\infty}^\infty \frac{(-1)^n}{(z-n)^2} = \frac{\pi^2}{(\sin{\pi z})(\tan{\pi z})}\]
        \item \[\frac{1}{z} + \sum_{n=1}^\infty (-1)^n \frac{2z}{z^2 - n^2} = \frac{\pi}{\sin{\pi z}}\]
        \item \[\sum_{n=-\infty}^\infty \frac{1}{(z+n)^2 + a^2} = \frac{\pi}{a}\cdot \frac{\sinh{2\pi a}}{\cosh{2 \pi a} - \cos{2 \pi z}}.\]
    \end{enumerate}

    \begin{solution}
        First, note that each of the series in question are absolutely convergent (at least pointwise) by comparision with the series \(\sum \frac{1}{n^2}\) wherever the terms in the series do not have a singularity. Thus we may rearrange these series and still be certain that they converge pointwise away from singularities of their terms.
        \begin{enumerate}
            \item Separating even and odd terms we get,
            \begin{align*}
                \sum_{n=-\infty}^\infty \frac{(-1)^n}{(z-n)^2} &= \sum_{k=-\infty}^\infty \frac{1}{(z-2k)^2} - \sum_{k=-\infty}^\infty \frac{1}{(z-2k+1)^2} \\
                &= \frac{1}{4}\left( \frac{\pi^2}{\sin^2 \frac{\pi z}{2}} - \frac{\pi^2}{\sin^2 \frac{\pi (z-1)}{2}}\right) \\
                &=\frac{\pi^2 (\cos^2 \frac{\pi z}{2} - \sin^2 \frac{\pi z}{2})}{4 \sin^2 \frac{\pi z}{2} \cos^2 \frac{\pi z}{2} }\\
                &= \frac{\pi^2 \cos{\pi z}}{\sin^2 \pi z}
            \end{align*}
        \end{enumerate}

    \end{solution}

    \item
    Prove the identity by taking the logarithmic derivative of both sides
    \[ \pi x \prod_{n=1}^\infty \left(1 + \frac{x^2}{n^2}\right) = \sinh{\pi x}.\]

    \item
        Let \(\mathcal{F}\) be the family of holomorphic functions in the unit disk satisfying \(|f^{(n)} \leq n! \) for all \(n \geq 0\). Show that \(\mathcal{F}\) is a normal family.

    \item
    Suppose \(f\) is an entire function. Show that \(f\) has an \(n-th\) root if and only if all zeros of \(f\) have multiplicity divisible by \(n\).

    \item
    Suppose \(f(z)\) is a holomorphic function defined on the unit disk with \(|f(z)| \leq M\). Let the zeros of \(f\) be \(a_1, a_2, \dots, a_n\) counted with their multiplicities. Show that
    \[ |f(z)| \leq M \left| \prod_{k=1}^n \frac{a_k - z}{1 - \overline{a_k}z}\right|.\]
    In particular, \(|f(0)| \leq M \prod_{k=1}^n |a_k|\) and if \(f(0)=0\) then \(|f(z)| \leq M|z|\).

    \item
    (Blashke Product) Suppose \(f(z)\) is a holomorphic function defined on the unit disk with a zero at \(0\) of order \(s\) and the other zeros \(\{a_k\}\) satisfying \(\sum_k(1-|a_k|) < \infty\) (or equivalently \(\sum_k \log|a_k| > -\infty\)). It then admits a nice factoriztion \(f = BG\) where \(B\) is a product of
    \[ B(z) = z^s \prod_{k=1}^\infty \frac{|a_k|}{a_k} \cdot \frac{a_k - z}{1- \overline{a_k}z}\]
    and \(G(z)\) is a holomorphic function without zeros. Show that \(B(z)\) is holomorphic.

    \item
    Show that a bounded holomorphic function admits a Blashke product, ie that the sum \(\sum_k \log|a_k|\) converges (Hint: use the first corollary of Problem 5).

    \item
    (Jensen's formula) Suppose \(f(z)\) is a holomorphic function in the unit disk without zeros. As \(\log |f(z)|\) is harmonic, we have \(\log|f(0)| = \frac{1}{2\pi} \int_0^{2\pi} \log|f(re^{i \theta})| d\theta \) for \(0 < r < 1\). But what if \(f(z)\) has zeros in the unit disk? Suppose that \(f(0) \neq 0\) and denote its zeros by \(\{a_k\}\). In this case,
        \[ \log|f(0)| = \frac{1}{2\pi} \int_0^{2\pi} \log|f(r e^{i\theta})| d \theta - \sum_{|a_k| < r} \log(\frac{r}{|a_k|}).\]
    Here \(\log|f(0)|\) is not equal to, but is actually less than the mean value. Such functions are called subharmonic. Prove the above formula (Hint: first consider \(r\) for which no \(\{a_k\}\) lie on \(|z| = r\), also show that RHS is continuous).

    \item
    (Canonical Product) Look back at the proof of Weierstrass' theorem and observe the following: Suppose \(f(z)\) is an entire function with zeros \(\{a_k\}\) satisfying \(\sum_k \frac{1}{|a_k|^{m+1}} \leq \infty\) for some integer \(m\). Then it is possible to choose all \(m_k = m\).

    \item
    Prove that
    \[\frac{\sin{\pi z}}{\pi z} = \prod_{n=1}^\infty \left(1 - \frac{z^2}{n^2}\right).\]
    Hint: The Weierstrass theorem implies that
    \[ \sin{\pi z} = z e^{g(z)} \prod_{n \neq 0} \left(1 - \frac{z}{n}\right) e^{z/n}.\]
    To find \(g(z)\), take the logarithmic derivative and use Example 2 from Section 1.2.

    \item
    (Wedderburn's lemma) Suppose \(f\) and \(g\) are entire functions without common zeros. Show that there exists entire functions \(a\), \(b\) such that \(a f + b g = 1\).

    Find the residues at the poles of the \(\Gamma\) function (Hint: use the functional equation).

    \item
    (Bohr-Mollerup theorem) Suppose \(f: \R^+ \to \R^+\) is a logarithmically convex function satisfying \(f(x+1) = f(x)\) and \(f(1) = 1\). Then necessarily, \(f(x) = \Gamma(x)\) for all \(x > 0\).
    Hint: Show that for all natural \(n \geq 2\) and positive \(x\)
    \[(n-1)^x(n-1)! \leq f(x+ n) \leq n^x(n-1)!\]
    and
    \[\frac{n^x n!}{x(x+1) \cdots (x+n)} \leq f(x) \leq \frac{n^x n!}{x(x+1) \cdots (x+n)} \cdot \frac{x+n}{n}.\]

    \item
    Show that the automorphisms of the upper half-plane which preserve \(i\) are given by
    \[w(z) = \frac{z+ \tan{\theta/2}}{1 - z \tan{\theta/2}}.\]
    Write the formula for \(\theta = \pi\).

    \item
    Find the automorphism group of \(\C \setminus \{0\}\).

    \item
    Show that two annuli are biholomorphic if and only if the ration of their radii are the same (complete the proofs below).
    \begin{enumerate}
        \item Use the Previous problem.
        \item Use the uniqueness of solution to the Dirichlet problem:
    \end{enumerate}

    \item
    Show that two tori \(\C / \Gamma_1\) (with \(\Gamma_1\) generated by \(e_1, e_2\)) and \(\C/\Gamma_2\) (with \(\Gamma_2\) generated by \(f_1, f_2\)) are biholomorphic if and only if there is a fractional linear transformation with integer coefficients and determinant \(1\) which takes \((e_1, e_2)\) to \((f_1, f_2)\).

    \item
    (Schwarz-Christoffel formula) Show that the mapping \(F: \mathbb{H}^+ \to \Omega\) (where \(\Omega\) is a polygon) given by the formula
    \[ F(w) = C \int_0^w \prod_{k=1}^{n-1} (w - w_k)^{-\beta_k} dw + C\]
        is conformal for some distinct real numers \(w_k\) and \(\sum \beta_k = 2\).

    \item
    Show that \(F(w) = \int_0^w (1-w^n)^{-2/n} dw\) maps the unit disk onto the interior of a regular polygon with \(n\) sides.

    \item
    Find the image of the unit disk under the mapping \(F(z) = \frac{1}{z} \prod_{k=1}^n (z-a_k)^{\lambda_k} \) where \(\lambda_k\) are positive with \(\sum_{k=1}^n \lambda_k = 2\).

    \item
    Prove the addition theorem:
    \[\p(z_1+z_2) = - \p(z_1) - \p(z_2) + \frac{1}{4} \left( \frac{\p'(z_1) - \p'(z_2)}{\p(z_1) - \p(z_2)} \right)^2\]

    \item
    Another form of the addition theorem: if \(u + v + w = 0\) in \(\C/\Gamma\) then
    \[
    \begin{vmatrix}
    1 & 1 & 1 \\
    \p(u) & \p(v) & \p(w) \\
    \p'(u) & \p'(v) & \p'(w)
    \end{vmatrix}
    = 0
    \]

    \item
    Suppose that \(f(z)\) is an even doubly periodic function (let the group of periods be \(\Gamma\)). There exists points \(a_1, a_2, \dots, a_n; b_1, b_2, \dots, b_n \in \C/\Gamma\) such that
    \[ f(z) = c \prod_{k=1}^n \frac{\p(z) - \p(a_k)}{\p(z) - \p(b_k)}\]

    \item
    Show that a doubly periodic function is a rational function of the \(\p\) and \(\p'\) (Hint: use the previous problem).

    \item
    Show that while the function \(f(z) = \sum_n z^{2^n}\) is holomorphic in the unit disk, it does not extend holomorphically to any larger open set (Hint: \(f(z^2) = f(z) - z)\).

    \item
    Find the radius of convergence of \(f(z) = \sum_{k\geq 1} \frac{z^{2k}}{k+1}\). Find a maximal domain of existence (a maximal open set in \(\C\) to which \(f(z)\) may be analytically continued).

    \item
    The set of solutions \((z,w)\) of \(w^2 - 2wz + 1 = 0\) can be completed to a compact Riemann surface over \(\C P^1\). Find the residues of the differential form \(\frac{dz}{\sqrt{z^2-1}}\) points at infinity.

    \item
    Prove
    \[ \int_0^1 \frac{dx}{\sqrt[3]{1-x^3}} = \frac{2\pi}{3 \sqrt{3}}.\]

    \item
    Prove Picard's theorem for meromorphic functions: if a meromorphic function defined on the entire complex plane omits three values, it is necessarily constant.

    \begin{solution}
        If \(f\) is meromorphic and omits \(a\), \(b\), and \(c\) then \(\frac{1}{f(z)-a}\) is a holomorphic function which omits \(\frac{1}{b-a}\) and \(\frac{1}{c-a}\). Thus by Picard's Little theorem \(f\) is constant.
    \end{solution}

    \item
    Show that a non-constant holomorphic function defined on \(\C\setminus\{0\}\) omits at most \(1\) value.
    \begin{solution}
        If \(f\) has an essential singularity at \(0\) or \(\infty\) then an application of Picard's Great theorem completes the proof. If not then \(f\) is a meromorphic function on the Riemann sphere and is thus rational (see Section~\ref{otherprob}). Then the fundamental theorem of algebra completes the proof.
    \end{solution}

    \item
    Show that the function \(f(z) = ze^z\) attains every value from \(\C\).
    \begin{solution}
        Note first that \(f\) has an essential singularity at \(\infty\). That is, \(f(1/z) = \frac{e^1/z}{z}\) has an essential singularity at \(0\). Thus by Picard's theorem on the neighbourhood of infinity \(|z| > 1\), \(f\) omits at most one value. This value must be \(0\) and since \(f\) acheives \(0\) at \(z=0\), \(f\) attains every value.
    \end{solution}

    \item
    Suppose \(f\), \(g\) are meromorphic functions such that \(f^3 + g^3 = 1\). Show that actually \(f\) and \(g\) are constant functions. Is result still true if 3 is replaced by a larger positive integer?
    \begin{solution}
        Note that the function \(\frac{f^3}{g^3} + 1 =  \frac{1}{g^3}\) is nonvanishing. Thus \(\frac{f}{g}\) omits the \(3\) distinct roots of \(-1\). Thus by Picard's theorem for meromorphic functions \(\frac{f}{g} = c\). But then \((c^3 +1)g^3 = 1\) so \(g\) and therefore \(f\) is constant.
    \end{solution}

    \item
    Suppose \(f\), \(g\) are entire functions satisfying \(e^f + e^g = 1\). Show that \(f\), \(g\) are actually constant functions.
    \begin{solution}
        If \(e^f + e^g = 1\) then the entire function \(e^f = 1 - e^g\) omits \(0\) and \(1\), therefore by Picard's Little theorem it must be constant. Then by differentiating we find that \(f'\) must be zero so \(f\) is constant.
    \end{solution}
\end{enumerate}

\section{Other Problems}\label{otherprob}
\begin{enumerate}
    \item
    If \(f\) is an entire function without an  essential singularity at \(\infty\) then \(f\) is a polynomial.
    \begin{solution}
        Since \(f\) either has a pole or a removeable singularity at \(\infty\), \(\lim_{z \to \infty} \frac{f(z)}{z^k} = 0\) for some integer \(k\). But then by Cauchy's estimate
        \[
            |f^{(n)}(0)| \leq \frac{n!Cr^k }{r^n}
        \]
        for large enough radius \(r\). But then if \(n > k\), \(f^{(n)} (0) = 0\) so \(f\) is a polynomial.
    \end{solution}

    \item
    If \(f\) is a meromorphic function with a nonessential singularity at \(\infty\) then \(f\) is a rational function.
    \begin{solution}
        Since \(f\) has a nonessential singularity at \(\infty\), this singularity must be isolated so there is a neighbourhood of \(\infty\) which has no poles. This imples that \(f\) only has finitely many poles say \(z_1, \dots, z_n\) counted with multiplicity. Then \((z-z_1) \cdots (z-z_n) f(z)\) is a holomorphic function with no essential singularity at \(\infty\) so applying the previous problem completes the proof.
    \end{solution}

\end{enumerate}

\end{document}


\subsection{Cubes}

\frame{
  \frametitle{Extensions to Cubes}

  \begin{itemize}
    \item Assign $x$, $y$, and $z$ as the opposite pairs of faces of a cube.
    \item Characterize sequences of $x$, $y$, and $z$ collisions.
  \end{itemize}
}

\frame{
  \frametitle{Intuition}

  \begin{itemize}
    \item Examine collisions in $xy$, $yz$, and $xz$ planes.
    \item Movement in each plane is independent.
    \item Combine $xy$, $yz$, and $xz$ collision sequences to get final sequence.
  \end{itemize}
}

\frame{
  \frametitle{Example}

  \begin{example}
    $xy$ sequence: $xxyyxx$ \\
    $yz$ sequence: $zzyzyz$ \\
    $xz$ sequence: $xxzzzx$
  \end{example}
}


\documentclass[a4paper,11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\title{Neural network notes}
\author{Marco Marini}

\begin{document}

\maketitle
\tableofcontents

\begin{abstract}
This document contains notes about neural network regarding the Temporal Differential reinforcement alghorithms.
\end{abstract}

\section{General}

Let us have a neural network composed by $ N $ layers. Each layer $ i = 1 \dots N $ is composed by $ s_i $ neurons.

The first layer
\[ H_{1i} = x_i \]
rappresents the input values of the network.
We identify the input bias signals with
\[ H_{i0} = 1 \]

Each neuron processes the input signals from its layer and produces the output signal to the next layer.

Let us exam a NLR (Non Linear regression) network.

The transfer function of each hidden neuron $ i < N $ is

\begin{equation}
\label{hidden}
  \begin{array}l
    H_{(i+1)j} = \frac{1}{1 + e^{-Z_{ij}}}
    \\
    Z_{ij} = \sum_{k=0}^{s_i} w_{ijk} H_{ik}
  \end{array}
\end{equation}

the transfer function of each output neuron $ N $ is

\begin{equation}
\label{nlr}
    H_{Nj} = \sum_{k=0}^{s_{N-1}} w_{(N-1)jk} H_{(N-1)k}
\end{equation}

\section{Cost Function}

Let $ y_j $ be the expected output for the neuron $ j $, the error of neuron is

\begin{equation}
\label{delta}
  \delta_{(N-1)j} = y_j - H_{Nj}
\end{equation}

The cost function of the network is
\[
  J=\left|
    \begin{array}l
      \frac{1 - \alpha}{2} \sum_{i=1}^{s_N} \delta_{(N-1)i}^2 + \frac{\alpha}{2}\sum_{i,j,k} w_{ijk}^2, \; k \ge 1
      \\
      \frac{1 - \alpha}{2} \sum_{i=1}^{s_N} \delta_{(N-1)i}^2,\; k=0
    \end{array}
  \right.
\]
where $ \alpha $ is the regularization factor.

Let us extract $ \nabla J $

\[
	\frac{\partial}{\partial w_{ijk}} J = \left|
    \begin{array}l
		(1 - \alpha) \sum_{r=1}^{s_N} \delta_{(N-1)r} \frac{\partial}{\partial w_{ijk}}  \delta_{(N-1)r} + \alpha w_{ijk}, \; k \ge 1
	      \\
		(1 - \alpha)\sum_{r=1}^{s_N} \delta_{(N-1)r} \frac{\partial }{\partial w_{ijk}}\delta_{(N-1)r}, \; k =0
    \end{array}
  \right.
\]

By (\ref{delta}) we have
\[
	\frac{\partial}{\partial w_{ijk}} \delta_{(N-1)r} =
	-\frac{\partial}{\partial w_{ijk}} H_{Nr}
\]
therefore
\[
	\frac{\partial}{\partial w_{ijk}} J =\left|
    \begin{array}l
		-(1 - \alpha) \sum_{r=1}^{s_N} \delta_{(N-1)r} \frac{\partial}{\partial w_{ijk}} H_{Nr} + \alpha w_{ijk}, \; k \ge 1
	      \\
		-(1 - \alpha)\sum_{r=1}^{s_N} \delta_{(N-1)r} \frac{\partial}{\partial w_{ijk}} H_{Nr}, \; k =0
    \end{array}
  \right.
\]

By now we disregard the regularization effects.

By (\ref{nlr}) we have
\[
	\frac{\partial}{\partial w_{(N-1)jk}}  H_{Nj} = H_{(N-1)k}
\]
\[
	\frac{\partial}{\partial w_{(N-1)jk}}  H_{Nr} = 0, \; r \ne j
\]
therefore
\[
	\frac{\partial}{\partial w_{(N-1)jk}} J =
		- \delta_{(N-1)j} H_{(N-1)k}
\]

By (\ref{nlr}) and $ i \le N - 2  $ we have
\[
	\frac{\partial J}{\partial w_{ijk}} =
	 - \sum_r \delta_{(N-1)r} \frac{\partial}{\partial w_{ijk}} \left[ \sum_s w_{(N-1)rs} H_{(N-1)s} \right] =
\]
\[
	= - \sum_{r,s} \delta_{(N-1)r} w_{(N-1)rs} \frac{\partial}{\partial w_{ijk}} H_{(N-1)s}
\]

By (\ref{hidden}) we have
\[
	\frac{\partial}{\partial w_{ijk}} H_{(i+1)j} =
	\frac{\partial}{\partial Z_{ij}} H_{(i+1)j}
	\frac{\partial}{\partial w_{ijk}} Z_{ij} =
	H_{(i+1)j} (1 - H_{(i+1)j}) Hik
\]
therefore
\[
	\frac{\partial}{\partial w_{(N-2)jk}} J =
	- \sum_r \delta_{(N-1)r} w_{(N-1)rj}
	H_{(N-1)j} (1 - H_{(N-1)j}) H_{(N-2)k}
\]

\section{Back-propagation}

Let us define
\[
	\delta_{(N-2)j} =
	\sum_r \delta_{(N-1)r} w_{(N-1)rj} H_{(N-1)j} (1 - H_{(N-1)j})
\]

Then we have
\[
	\frac{\partial}{\partial w_{(N-2)jk}} J =
	- \delta_{(N-2)j}  H_{(N-2)k}
\]

We can induce just for $ i \le N - 2 $
\[
	\delta_{ij} =
	\sum_k \delta_{(i+1)k} w_{(i+1)kj} H_{(i+1)j} (1 - H_{(i+1)j})
\]
while in general
\[
	\frac{\partial}{\partial w_{ijk}} J =
	- \delta_{ij} H_{ik}
\]

Let us now define the back-propagation tensor as
\[
  B_{ijk} =  w_{(i+1)kj} H_{(i+1)j} (1 - H_{(i+1)j}), \; i \le N-2
\]

The errors on intermediate layers are
\[
  \delta_{ij} = \sum_{k=1}^{s_i} \delta_{(i+1)k} B_{ijk},\; i = 1 \dots N-2
\]

Now reintroducing the regularization effetcs we have
\[
	\frac{\partial}{\partial w_{ijk}} J =\left|
    \begin{array}l
		-(1 - \alpha) \delta_{ij} H_{ik} + \alpha \, w_{ijk}, \; k \ge 1
	      \\
		-(1 - \alpha) \delta_{ij} H_{ik}, \; k =0
    \end{array}
  \right.
\]

The weight changes to reduce the error (gradient descent) are

\[
  \Delta w_{ijk}  = -\eta \frac{\partial}{\partial w_{ijk}} J = \left|
  \begin{array}l
    \eta \left[ (1 - \alpha) \delta_{ij} H_{ik} - \alpha w_{ijk} \right], \; k \ge 1
    \\
    \eta (1 - \alpha) \delta_{ij} H_{ik}, \; k = 0
  \end{array}
  \right.
\]
where $ \eta $ is the learning rate.

\section{Returns}

Let us have a process where the best policy generates a never end episode (infinite length) and let the positive reward be $ r_+ > 0 $ every $ n $ steps, while a wrong choice ad step $ n $ generates the end of episode with a negative reward of $ r_- < 0 $.

The return at step $ n $ is then

\[
	R_+ = \frac{r_+}{1 - \lambda ^ {2 (n -1)}}
 \]
if the best strategy is applied otherwise
\[
R_- = r_-
\]

Suppose $ R_+ \gg -R_- $ we have
\[
	\frac{r_+}{1 - \lambda ^ {2 (n -1)}} \gg -r_-
\]
\[
	r_+ \gg -(1 - \lambda ^ {2 (n -1)}) r_-
\]

For example if $ \lambda ^ {2 (n -1)} = \frac{1}{2}, r_- = -1 $ we have
\[
r_+ \gg \frac{1}{2}
\]
such as $ r_+ = 5 $

\end{document}


    \filetitle{sort}{Sort SVAR parameterisations by squared distance of shock reponses to median}{SVAR/sort}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
[B,~,Inx,Crit] = sort(A,[],SortBy,...)
[B,Data,Inx,Crit] = sort(A,Data,SortBy,...)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{A} {[} SVAR {]} - SVAR object with multiple parameterisations
  that will be sorted.
\item
  \texttt{Data} {[} struct \textbar{} empty {]} - SVAR database; if
  non-empty, the structural shocks will be re-ordered according to the
  SVAR parameterisations.
\item
  \texttt{SortBy} {[} char {]} - Text string that will be evaluated to
  compute the criterion by which the parameterisations will be sorted;
  see Description for how to write \texttt{SortBy}.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\item
  \texttt{B} {[} SVAR {]} - SVAR object with parameterisations sorted by
  the specified criterion.
\item
  \texttt{Data} {[} tseries \textbar{} struct \textbar{} empty {]} -
  SVAR data with the structural shocks re-ordered to correspond to the
  order of parameterisations.
\item
  \texttt{Inx} {[} numeric {]} - Vector of indices so that
  \texttt{B = A(Inx)}.
\item
  \texttt{Crit} {[} numeric {]} - The value of the criterion based on
  the string \texttt{SortBy} for each parameterisation.
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{'progress='} {[} \texttt{true} \textbar{}
  \emph{\texttt{false}} {]} - Display progress bar in the command
  window.
\end{itemize}

\paragraph{Description}\label{description}

The individual parameterisations within the SVAR object \texttt{A} are
sorted by the sum of squared distances of selected shock responses to
the respective median reponses. Formally, the following criterion is
evaluated for each parameterisation

\[ \sum_{i\in I,j\in J,k\in K} \left[ S_{i,j}(k) - M_{i,j}(k) \right]^2 \]

where $S_{i,j}(k)$ denotes the response of the i-th variable to the j-th
shock in period k, and $M_{i,j}(k)$ is the median responses. The sets of
variables, shocks and periods, i.e. \texttt{I}, \texttt{J}, \texttt{K},
respectively, over which the summation runs are determined by the user
in the \texttt{SortBy} string.

How do you select the shock responses that enter the criterion in
\texttt{SortBy}? The input argument \texttt{SortBy} is a text string
that refers to array \texttt{S}, whose element \texttt{S(i,j,k)} is the
response of the i-th variable to the j-th shock in period k.

Note that when you pass in SVAR data and request them to be sorted the
same way as the SVAR parameterisations (the second line in Syntax), the
number of parameterisations in \texttt{A} must match the number of data
sets in \texttt{Data}.

\paragraph{Example}\label{example}

Sort the parameterisations by squared distance to median of shock
responses of all variables to the first shock in the first four periods.
The parameterisation that is closest to the median responses

\begin{verbatim}
S2 = sort(S1,[],'S(:,1,1:4)')
\end{verbatim}


\documentclass[12pt]{article}
\usepackage{bm}
\newcommand{\x}{\bm{x}}
\usepackage{color}
\usepackage{amsmath}
\usepackage{authblk}
\usepackage{graphicx,psfrag,epsf}
\usepackage{hyperref}
\usepackage{enumerate}
\usepackage{natbib}
\usepackage{subcaption}
\let\cite\citep
\usepackage{colortbl}
\usepackage{soul}
\usepackage{url}
\usepackage{xcolor}
\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

\usepackage{bm}
\newcommand{\bx}{\bm{x}}
\newcommand{\bs}{\bm{s}}

\usepackage[toc,page]{appendix}

\usepackage{amsthm}

\newtheorem{theorem}{Theorem}
\makeatother

\title{Bayesian Quadrature algorithm proposal}
\date{\today}
\author{Seth Flaxman}
\usepackage[parfill]{parskip}

\begin{document}
\maketitle

\section{BQ with Bayesian CART (not BART, yet!)}
{\bf This section has been updated as it covers CART, not BART.}

We assume we have access to any easy to evaluate distribution $p(x)$, a costly to evaluate function $f(x)$,
and we wish to approximate the integral:
\begin{equation}
	I_f = \int_{x \in \mathcal{X}} f(x) p(x) dx
\label{eq:integral}
\end{equation}

Using Bayesian CART, we will learn a function $f_{\mbox{BART}}$ and approximate:
\begin{equation}
	I_f \approx \int_{x \in \mathcal{X}} f_{\mbox{BART}}(x) p(x) dx
	\label{eq:approx1}
\end{equation}

We augment our tree data structure with the following information:
for a given split, there is some probability
based on $p(x)$ of following the left path, and some probability $1-p(x)$ of following the right path. We store
these probabilities, so that for every terminal node $\rho_i$, we can calculate $p(\rho_i)$, the probability of the
event $\rho_i$, by multiplying the probabilities along the edges of the path by which we reached node $\rho_i$.

[INSERT FIGURE ILLUSTRATING THIS HERE]

We denote the value of terminal node $\rho_i$ by $f_{\mbox{CART}}(\rho_i)$.
We further augment our tree data structure by calculating Eq.~\eqref{eq:integral} for the set of events that reach
each terminal node:
\begin{align}
	& \int_{x \in \rho_i} f_{\mbox{CART}}(x) p(x) dx \\
	&=  f_{\mbox{CART}}(\rho_i) p(\rho_i)
	\label{eq:approx2}
\end{align}
Assuming we have $m$ terminal nodes, we use Eq.~\eqref{eq:approx2} to calculate Eq.~\eqref{eq:approx1}:
\begin{align}
%	& \int_{x \in \mathcal{X}} f(x) p(x) dx \\
	& \int_{x \in \mathcal{X}} f_{\mbox{CART}}(x) p(x) dx \\
	= & \sum_{i=1}^m \int_{x \in \rho_i} f_{\mbox{CART}}(x) p(x) dx \\
	= &  \sum_{i=1}^m f_{\mbox{CART}}(\rho_i) p(\rho_i)
\end{align}

Now given a set of trees $t_1, \ldots, t_T$ drawn from the Bayesian CART posterior, we have
functions $f_{\mbox{CART}}^{(1)}, \ldots, f_{\mbox{CART}}^{(T)}$, each with its own set of terminal
nodes $\rho_i^{j}$, $i=1, \ldots, m_j$. We use these trees to approximate
Eq.~\eqref{eq:integral}:
\begin{align}
	I_f = \int f(x)p(x) dx \approx \frac{1}{T} \sum_{j=1}^T \sum_{i=1}^{m_j} f_{\mbox{CART}}^j(\rho_i^j) p(\rho_i^j)
\end{align}

After fitting CART, we calculate $I_f$ for each tree drawn from the posterior.
If we draw $P$ samples, we obtain $I_f^{(1)}, \ldots, I_f^{(P)}$, allowing us to summarize
the mean and variance as:
\begin{equation}
	\mbox{E}[I_f] = \frac{1}{n} \sum_p I_f^{(p)}
\end{equation}
\begin{equation}
	\mbox{Var}[I_f] = \frac{1}{n^2} \sum_p (I_f^{(p)} - \mbox{E}[I_f])^2
\end{equation}

\section{From Bayesian CART to BART}
[TO BE FILLED IN]

\section{Choosing the next query location (Sequential BQ with BART)}
Since it's not at all obvious how to take a Sequential Bayesian Quadrature with GPs approach to the BART setting,
a simple alternative is to look where the posterior variance in $f(x)$ as weighted by $p(x)$ is largest.
Thus, we choose as the next query location:
\begin{equation}
	x^* = \mbox{argmax}_{x} \mbox{Var}[f(x)p(x)]
	\label{eq:maxvar}
\end{equation}
To implement this in practice, we follow the BO with BART approach. Choose a set of test locations $x_1, \ldots, x_n$
using, e.g.~LHS and find the posterior predictve distribution of BART at each $x_i$:
\begin{equation}
	p(f(x_i) | \mathcal{D})
\end{equation}
Since we've used MCMC, what this means in practice is that we obtain a set of $p$ samples for each $x_i$: $f^{(1)}(x_i), \ldots, f^{(p)}(x_i)$ from the $p$ separate sum-of-trees.
Now we can calculate the variance using the empirical estimator for mean and variance:
\begin{align}
	\mbox{E}[f(x_i)p(x_i)] &= \frac{1}{p} \sum_j f^{(j)}(x_i)p(x_i) \\
	\mbox{Var}[f(x_i)p(x_i)] &= \frac{1}{p} \sum_j \left[f^{(j)}(x_i)p(x_i) - \mbox{E}[f(x_i)p(x_i)]\right]^2
\end{align}
Finally, we find $x^*$ following Eq.~\eqref{eq:maxvar}.

\section{Calculating $p(x)$}
If we assume a uniform prior $p(x)$ this will allow us to conveniently reuse the calculations that are already
used for sampling. In section 3.2 of Chipman, George, and McCulloch [Bayesian CART Model Search] they describe
$p_{\mbox{RULE}}()\rho | \eta, T)$ and propose what they call the ``uniform specification'' where
at each ndoe, all predictors are equally likely to be effective. This means that calculating $p(x)$ should
be simple: just reuse the calculation that is built into BART for the uniform specification of $p_{\mbox{RULE}}$.

\end{document}