%
% This is a borrowed LaTeX template file for lecture notes for CS267,
% Applications of Parallel Computing, UCBerkeley EECS Department.
% Now being used for CMU's 10725 Fall 2012 Optimization course
% taught by Geoff Gordon and Ryan Tibshirani.  When preparing
% LaTeX notes for this class, please use this template.
%
% To familiarize yourself with this template, the body contains
% some examples of its use.  Look them over.  Then you can
% run LaTeX on this file.  After you have LaTeXed this file then
% you can look over the result either by printing it out with
% dvips or using xdvi. "pdflatex template.tex" should also work.
%

\documentclass[twoside]{article}
\setlength{\oddsidemargin}{0.25 in}
\setlength{\evensidemargin}{-0.25 in}
\setlength{\topmargin}{-0.6 in}
\setlength{\textwidth}{6.5 in}
\setlength{\textheight}{8.5 in}
\setlength{\headsep}{0.75 in}
\setlength{\parindent}{0 in}
\setlength{\parskip}{0.1 in}

%
% ADD PACKAGES here:
%

\usepackage{amsmath,amsfonts,graphicx}

%
% The following commands set up the lecnum (lecture number)
% counter and make various numbering schemes work relative
% to the lecture number.
%
\newcounter{lecnum}
\renewcommand{\thepage}{\thelecnum-\arabic{page}}
\renewcommand{\thesection}{\thelecnum.\arabic{section}}
\renewcommand{\theequation}{\thelecnum.\arabic{equation}}
\renewcommand{\thefigure}{\thelecnum.\arabic{figure}}
\renewcommand{\thetable}{\thelecnum.\arabic{table}}

%
% The following macro is used to generate the header.
%
\newcommand{\lecture}[4]{
   \pagestyle{myheadings}
   \thispagestyle{plain}
   \newpage
   \setcounter{lecnum}{#1}
   \setcounter{page}{1}
   \noindent
   \begin{center}
   \framebox{
      \vbox{\vspace{2mm}
    \hbox to 6.28in { {\bf EE302 - Feedback Systems
	\hfill Spring 2019} }
       \vspace{4mm}
       \hbox to 6.28in { {\Large \hfill Lecture #1 \hfill} }
       \vspace{2mm}
       \hbox to 6.28in { {\it Lecturer: #2 \hfill } }
      \vspace{2mm}}
   }
   \end{center}
   \markboth{Lecture #1}{Lecture #1}

   \vspace*{4mm}
}
%
% Convention for citations is authors' initials followed by the year.
% For example, to cite a paper by Leighton and Maggs you would type
% \cite{LM89}, and to cite a paper by Strassen you would type \cite{S69}.
% (To avoid bibliography problems, for now we redefine the \cite command.)
% Also commands that create a suitable format for the reference list.
\renewcommand{\cite}[1]{[#1]}
\def\beginrefs{\begin{list}%
        {[\arabic{equation}]}{\usecounter{equation}
         \setlength{\leftmargin}{2.0truecm}\setlength{\labelsep}{0.4truecm}%
         \setlength{\labelwidth}{1.6truecm}}}
\def\endrefs{\end{list}}
\def\bibentry#1{\item[\hbox{[#1]}]}

%Use this command for a figure; it puts a figure in wherever you want it.
%usage: \fig{NUMBER}{SPACE-IN-INCHES}{CAPTION}
\newcommand{\fig}[3]{
			\vspace{#2}
			\begin{center}
			Figure \thelecnum.#1:~#3
			\end{center}
	}
% Use these for theorems, lemmas, proofs, etc.
\newtheorem{theorem}{Theorem}[lecnum]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}
\newenvironment{proof}{{\bf Proof:}}{\hfill\rule{2mm}{2mm}}

% **** IF YOU WANT TO DEFINE ADDITIONAL MACROS FOR YOURSELF, PUT THEM HERE:

\begin{document}

% Lecture Details
\lecture{10}{Asst. Prof. M. Mert Ankarali}

\par

\section{PID Control Policy}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.6\textwidth]{PID}
    \end{center}
  \end{minipage}

A PID controller has the following forms in time and Laplace domain
%
  \begin{align*}
  u(t) &= K_P e(t) + K_D \frac{d}{dt} e(t) + K_I \int_{0}^{t} e(t) dt
    \\
   U(s) &= \left[ K_P + K_D s + K_I \frac{1}{s} \right] E(s)
  \end{align*}

In this lecture we will analyze the the effects of PID coefficients on
the transient and steady-state performance on a $2^{nd}$ order plant.
%

\subsection{Proportional (P) Controller}

Let's assume that $H(s) = 1$ and $G(s)$ is a second order
transfer function in general form
%
  \begin{align*}
  G(s) = \frac{\omega_n^2}{s^2 + 2 \zeta \omega_n + \omega_n^2}
  \end{align*}
%
SInce $C(s) = K_P$, let's first analyze the steady-state error performance
%
\begin{align*}
  &G_{OL}(s) = \frac{K_P \omega_n^2}{s^2 + 2 \zeta \omega_n + \omega_n^2}
\\
 Type \ 0 \quad K_p = K_P
\end{align*}
%
Thus steady-state error for unit-step and unit-ramp inputs can be find
as
\begin{itemize}
\item Unit step: $e_{ss} = \frac{1}{1 + K_P}$, i.e. $K_p \nearrow \
  \Rightarrow \ e_{ss} \searrow$
\\ Unit ramp: $e_{ss} = \infty$
\end{itemize}
%
To sum up, higher $K_P$ provides better steady-state performance.
Now let's analyze transient performance.
%
%
  \begin{align*}
  T(s) &= \frac{Y(s)}{R(s)} = \frac{K_P \omega_n^2}{s^2 + 2 \zeta
    \omega_n  + (1 + K_P) \omega_n^2}
\\
 \bar{\omega}_n &= \sqrt{1 + K_P} \omega_n
\\
 \bar{\zeta} &= \zeta \frac{1}{ \sqrt{ 1 + K_P} }
  \end{align*}
%
We can see that
%
  \begin{align*}
    K_p \nearrow \ &\Rightarrow \ \omega_n \nearrow \\
   K_p \nearrow \ &\Rightarrow \ \zeta \searrow
  \end{align*}
%
If the plant is an over-damped system (i.e. $\zeta > 1$), them increasing
$\omega_n$ and decreasing $\zeta$ should has a positive net
effect on the closed-loop performance.

On the other hand if the plant is an under-damped system,
we can observe the following relations
%
\begin{align*}
\bar{\omega}_d &= \bar{\omega}_n \sqrt{1 - \bar{\zeta}^2} = \omega_n
  \sqrt{1 + K_P}  \sqrt{1 - \frac{\zeta^2}{ 1 + K_P } }
  = \omega_n \sqrt{ 1 + K_P -\zeta^2 }
\\
\bar{\zeta} \bar{\omega}_n &= \frac{\zeta}{ \sqrt{ 1 + K_P } }
                             \omega_n \sqrt{1 + K_P} = \zeta \omega_n
\end{align*}
%
We can see that
%
  \begin{align*}
    K_p \nearrow \ &\Rightarrow \ \omega_d \nearrow \\
   K_p \nearrow \ &\Rightarrow \ \zeta \omega_n \mathrm{=}
  \end{align*}
%
In other words real part of the complex conjugate poles
are unchanged, yet imaginary part deviates from the real
axis. From previous lectures we kow that in this scenario
%
  \begin{align*}
    K_p \nearrow \ &\Rightarrow \ M_p \nearrow
  \end{align*}
%
In conclusion, If the plant is an under-damped system (i.e. $\zeta <
1$), them increasing the P gains has an negative effect on
the closed-loop performance (transient).

\textbf{Example 1:} Let $G(s) = \frac{1}{(s+0.5)(s+5)}$ (and over-damp
plant), then compute the unit-step steady state error for $K_p = 2$ and $K_P = 5$.
%
\begin{align*}
  e_{2} &= \frac{1}{1 + 2 / 2.5} \approx 0.55 \ , (\%55)
\\
  e_{5} &= \frac{1}{1 + 5 / 2.5} \approx 0.33 \ , (\%33)
\end{align*}
%
Obviously, stead-state performance is better with $K_p = 5$
compared to $K_p = 1$. Now compute the closed-loop poles
for $K_p = 1$ and $K_P = 5$, and estimate associated
settling times ($\%2$).
%
\begin{align*}
  T_2(s) = \frac{2}{s^2 + 5.5 s + 4.5} &\quad \rightarrow \
  p^{(2)}_{1} = -1 \ ,  p^{(2)}_{2} = -4.5  \quad t^{(2)}_s \approx 4
  s
\\
  T_5(s) = \frac{5}{s^2 + 5.5 s + 7.5} &\quad \rightarrow \
  p^{(2)}_{1} = -2.5 \ ,  p^{(2)}_{2} = -3  \quad t^{(2)}_s \approx
                                         1.6 s
\end{align*}
%
We can see that $K_P = 5$ provides a better transient performance
compared to $K_P = 2$. Now let's draw step-responses and
verify these observations

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.4\textwidth]{Pcont1}
    \end{center}
  \end{minipage}

\vspace{12 pt}

We verify that both steady-state and transient performance increases
with larger $K_P$. However, we can also see that seetling time
estimation for $K_P = 5$ has a larger error, which expected
since the poles are close to each other thus violates the
dominant pole assumption.

\textbf{Example 2:} Let $G(s) = \frac{1}{s^2 + 4 s + 5}$ (and under-damped
plant), then compute the unit-step steady state error for $K_p = 3$ and $K_P = 8$.
%
\begin{align*}
  e_{3} &= \frac{1}{1 + 3 / 5} \approx 0.625 \ , (\%62.5)
\\
  e_{8} &= \frac{1}{1 + 8 / 5} \approx 0.385 \ , (\%38.5)
\end{align*}
%
Obviously, stead-state performance is better with $K_p = 8$
compared to $K_p = 3$. Now compute the closed-loop poles
for $K_p = 3$ and $K_P = 8$, and estimate associated
maximum overshoots
%
\begin{align*}
  T_3(s) = \frac{3}{s^2 + 4 s + 8} &\quad \rightarrow \
  p^{(3)}_{1,2} = -2 \pm 2 j  \quad M_P =M_P = e^{-pi / \tan \phi_3} =
                                     e^{-pi} \approx 0.04 \ (\%4)
\\
  T_8(s) = \frac{5}{s^2 + 4 s + 13} &\quad \rightarrow \
  p^{(8)}_{1,1} = -2 \pm 3 j  \quad M_P = e^{-pi / \tan \phi_8} =
                                      e^{-pi 2 / 3} \approx 0.12  \ (\%12)
\end{align*}
%
We can see that $K_P = 3$ provides a better transient performance
compared to $K_P = 8$, since both frequency oscillates and overs-shoot
is increased with a higher P gain. Now let's draw step-responses and
verify these observations.

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.4\textwidth]{Pcont2}
    \end{center}
  \end{minipage}

\vspace{12 pt}

We verify that stead-state performance is better with a larger
P gain. However, transent performance is worse with a
larger P-gain. This implies that if the plant is an under-damped
plant, then P controller has some serious limitations.

\subsection{Proportional Derivative (PD) Controller}

In the classical form of PD controller, $C(s)$, takes the form
%
\begin{align*}
 C(s) = K_P + K_D s
\end{align*}
%
Let's first analyze the affect of $K_D$ term on steady-state
performance on the same case ($2^{nd}$ order plant in standard form)
%
\begin{align*}
  &G_{OL}(s) = \frac{K_P \omega_n^2 + K_D \omega_n^2 s}{s^2 + 2 \zeta
    \omega_n s + \omega_n^2}
\\
 Type \ 0 \quad K_p = K_P
\end{align*}
%
Thus steady-state error for unit-step and unit-ramp inputs can be find
as
\begin{itemize}
\item Unit step: $e_{ss} = \frac{1}{1 + K_P}$, i.e. $K_p \nearrow \
  \Rightarrow \ e_{ss} \searrow$
\\ Unit ramp: $e_{ss} = \infty$
\end{itemize}
  %
Obviously, $K_D$ has no effect on steady-state performance. Now
let's analyze the closed-loop transfer function
%
  \begin{align*}
  T(s) &= \frac{Y(s)}{R(s)} = \frac{K_P \omega_n^2 + K_D \omega_n^2 s}{s^2 + 2 \zeta
    \omega_n  s + \omega_n^2 + K_D \omega_n^2 s + K_P \omega_n^2}
\\
  &=   \frac{K_P \omega_n^2 + K_D \omega_n^2 s}{s^2 + ( 2 \zeta
    \omega_n + K_D \omega_n^2) s + (1 + K_P) \omega_n^2}
\\
,
\\
 \Bar{\omega}_n &= \sqrt{1 + K_P} \omega_n
\\
 \bar{\zeta} &= \frac{\zeta + K_D \omega_n / 2}{ \sqrt{ 1 + K_P} }
  \end{align*}
%
We can see that
%
  \begin{align*}
    K_P \nearrow \ &\Rightarrow \ \omega_n \nearrow \ \& \ \zeta \searrow
\\
   K_D \nearrow \ &\Rightarrow \ \omega_n = \ \& \ \zeta \nearrow
  \end{align*}
%
In other words, for a second order system, we have full control on
closed-loop pole locations with PD control policy. Since, a high $K_P$
is required/preferred for steady-state performance (which couses
the system to have overshoot and oscillatory behavior), $K_D$
term can be used to suppress oscillations and overshoot.
Note that in the closed-loop transfer function, numerator part
has a zero due to $K_D \omega_n^2 s$ term, which implies that
the closed-loop transfer function is not in standard $2^{nd}$ order
form. One should note the fact that, the existing of closed-loop
zero can affect the accuracy of our closed-loop transient performance
metric calculations (most probably deviations will be minor).
%

\textbf{Example 3:} Let $G(s) = \frac{1}{s^2 + 4 s + 5}$ (and under-damp
plant). Design a $PD$ controller such that steady-state error to
unit-step input is around $\% 20$ and the maximum percentage overshoot
is less than $\% 4$.

\textbf{Solution:} We first design $K_D$ based on steady-state
requirement then choose $K_D$ based on the over-shoot requirement.
%
  \begin{align*}
    e_{ss} &= \frac{1}{1 + K_P/5} = 0.2
    \\
   K_P &= 20
  \end{align*}
%
Now let's compte the closed-loop transfer function with $K_P = 20$.
  \begin{align*}
   T(s) &= \frac{20 + K_D s}{s^2 + (4 + K_D) s + 25}
  \end{align*}
%
Let $K_D = 4$, than the closed-loop transfer function and associated
poles are computed as
%
  \begin{align*}
   T(s) &= \frac{20 + 4s}{s^2 + 8 s + 25}
  \\
  p_{1,2} = -4 \pm 3 j
  \end{align*}
%
We can estimate the overshoot based on the pole locations
%
  \begin{align*}
    PM_P = \% 100 e^{-\pi / \tan \phi} = e^{-\pi 4 / 3} = \% 1.5 < \% 4
  \end{align*}
%
Let's plot the step-response of the resultant system and check if we
can meet the specifications.

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.4\textwidth]{PD1}
    \end{center}
  \end{minipage}

\vspace{12 pt}

We can observe that the computed over-shoot is $\% 5$, and indeed
higher than the requirement. Moreover, the gap between estimated
and numerically computed over-shoot is around $\%3.5$. Obviously,
the $K_D s = 4 s$ term in the numerator affects the output behavior and
its affect should be maximum when $0 < t < t_s$. In order see how
$K_D s = 4 s$, let's compre step responses of following transfer
functions
%
\begin{align*}
   T(s) &= \frac{20 + 4s}{s^2 + 8 s + 25}
  \\
   \hat{T}(s) &= \frac{20}{s^2 + 8 s + 25}
  \end{align*}
%
$T(s)$ and $\hat{T}(s)$ share the same poles and DC gain, but
$\hat{T}(s)$ is in standard form, thus has no zeros.

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.4\textwidth]{zero}
    \end{center}
  \end{minipage}

\vspace{12 pt}

We can see that there exist non-negligible differences
between two transfer functions, which clearly shows that
the numerator dynamics can substantially affect the response.
In this context, we should think as the transient performance
metrics and associated approximate estimation formulas
as heuristics which guides the design of controllers.

\subsection{Integral (I) Controller}

Practical use of Integral controller (alone) is extremely
rare, however we will analyze this case to better
understand the effect of Integral action on more useful
PI and PID topologies.

In the pure Integral controller, $C(s)$, takes the form
%
\begin{align*}
 C(s) = K_I \frac{1}{s}
\end{align*}
%
Let's first analyze the steady-state
performance where the plant is a $2^{nd}$ system in standard
%
\begin{align*}
  &G_{OL}(s) = \frac{K_I \omega_n^2}{s (s^2 + 2 \zeta
    \omega_n s + \omega_n^2)}
\\
 &Type \ 1 \quad K_v = K_I
\end{align*}
%
Thus steady-state error for unit-step and unit-ramp inputs can be find
as
\begin{itemize}
\item Unit step: $e_{ss} = 0$
\item Unit ramp: $e_{ss} = \frac{1}{K_I}$, i.e. $K_I \nearrow \ \Rightarrow \ e_{ss} \searrow$
\end{itemize}
  %
Basic idea is very clear, Integral action increases the type of the
system by introducing an extra pole at the origin (also increases the
total system order). Thus for a Type 0 plant, it completely eliminates
the steady-state error for step-like inputs, and provides a
constant steady-state error for ramp-like inputs.

Let's compte the closed-loop transfer function
%
\begin{align*}
T(s) = \frac{K_I \omega_n^2}{s^3 + 2 \zeta \omega_n s^2 + \omega_n^2 s
  + K_I \omega_n^2}
\end{align*}
  %
The closed loop system is now a third order system and
thus harder to analyze (we need new analysis tools). Moroever,
we have only one paremeter $K_I$ and the closed-loop system has
three poles. One may easily guess that it is very hard to obtain
a good performance with a pure Integral controller.
Sometimes it bay be even quite hard to obtain a stable behavior.

\paragraph{Example 4:} Let's analyze the influence of an $I$ controller on a
first order plant in order to better understand the positive and
negative effects of $I$ action. Let $G(s) = \frac{1}{s+2}$.

The steady-state value of $y(t)$ (for unit-step input)
for the uncontrolled plant is $y_{ss} = 1/2$, thus we
can say that steady-state error under unit-step input is
$e_{ss} = 0.5$, where as for unit ramp it is easy to
show that $e_{ss} = \infty$. On the other hand, we can estimate the
settling time ($\%2$) for the uncontrolled plant as $t_{s} \approx 2.$

Now let's first analyze the setady-state performance under
$C(s) = \frac{K_I}{s}$,
%
\begin{align*}
  &G_{OL}(s) = \frac{K_I }{s (s + 2)}
\\
 &Type \ 1 \quad K_v = K_I / 2
\end{align*}
%
\begin{itemize}
\item Unit step: $e_{ss} = 0$
\item Unit ramp: $e_{ss} = \frac{2}{K_I}$, i.e. $K_I \nearrow \ \Rightarrow \ e_{ss} \searrow$
\end{itemize}
 %
Obviously, steady-state performance improvement is
significant (structurally). Now, lets
compute closed-lopp transfer function
%
\begin{align*}
  T(s) = \frac{K_I}{s^2 + 2 s + K_I}
\end{align*}
%
Now let's analyze the transient performance (settling time and maximum
overshoot) for $K_I = 1$ and $K_I = 2$ and compre them
w.r.t. uncontrolled plant
%
\begin{align*}
  K_I &= 1 \quad \rightarrow \quad \zeta = 1 \ , \ p_{1,2} = -1 \quad
        \rightarrow \quad M_P =0 \ , \ t_{s} \approx 4 s
\\
 K_I &= 2 \quad \rightarrow \quad \zeta = 1/\sqrt{2} \ , \ p_{1,2} =
       -1 + \pm j  \quad
        \rightarrow \quad M_P = 0.04 \ , \ t_{s} \approx 4 s
\end{align*}
%
We can clearly see that integral action has a negative effect
on transient performance. $K_I = 1$ case has a worse
settling time value than the original plant<
Moroever we start to observe over-shoot at the output
when we increase integral gain to $K_I = 2$ case.

In order to illustrate these analytic observations,
we plotted the step responses of original plant,
closed-loop system with $K_I = 1$ and
closed-loop system with $K_I = 2$.

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.7\textwidth]{PIcase}
    \end{center}
  \end{minipage}

\vspace{12 pt}

It is clear that PI controlle eliminates the
steady-state error, but we can also observe that
transient performance substantially degraded. In both cases
settling time is worse than the original plant,
and for case $K_I = 2$, overs-shoot is clear from the
figure. One interesting result is that settling time performance
for $K_I = 1$ (critically damped) is worse than $K_I = 2$
(under-damped) even though the approximate formula
provides the same estimate. The reason is that for the critically
damped case, the approximation under-estimates
the settling time.

In this context, we can conclude that
a little bit over-shoot could be good for the closed-loop
system from the perspective of settling time.

\subsection{Proportional Integral (PI) Controller}

PI controller is commonly used in practical applications.
In general, if a single P controller is satisfactory for
transient requirements, but one seeks perfect steady-state
performance for unit-step like inputs, $PI$ is the first choice to
test (compared to PID).

In the PI controller, $C(s)$, takes the form
%
\begin{align*}
 C(s) = K_P + \frac{K_I}{s}
\end{align*}
%
Let's first analyze the steady-state
performance again with the plant is a $2^{nd}$ system in standard
%
\begin{align*}
  &G_{OL}(s) = \frac{K_P \omega_n^2 s +  K_I \omega_n^2}{s (s^2 + 2 \zeta
    \omega_n s + \omega_n^2)}
\\
 &Type \ 1 \quad K_v = K_I
\end{align*}
%
Thus steady-state error for unit-step and unit-ramp inputs can be find
as
\begin{itemize}
\item Unit step: $e_{ss} = 0$
\item Unit ramp: $e_{ss} = \frac{1}{K_I}$, i.e. $K_I \nearrow \ \Rightarrow \ e_{ss} \searrow$
\end{itemize}
  %
In other words $PI$ and $I$ controllers have same steady-state
performance characteristics. Now, let's compte the closed-loop transfer function
%
\begin{align*}
T(s) = \frac{K_P \omega_n^2 s + K_I \omega_n^2}{s^3 + 2 \zeta \omega_n
  s^2 + (1 + K_P) \omega_n^2 s
  + K_I \omega_n^2}
\end{align*}
  %
In this case, the closed-loop transfer function has three poles,
and we have two parameters for tuning. Even though it provides a
much better framework than an I controller, still we need different tools
to tune the $K_I$ and $K_P$ gains.

\paragraph{Example 5:} Let's compre PI and I controllers using the
same plant in previous example, $G(s) = \frac{1}{s+2}$. We know that
PI controller has the transfer function form $C(s) = \frac{K_I}{s}$
and steady-state error characteristics can be derived as
%
\begin{align*}
  &G_{OL}(s) = \frac{s K_P + K_I }{s (s + 2)}
\\
 &Type \ 1 \quad K_v = K_I / 2
\\
&\mathrm{Unit} \ \mathrm{step :} \ e_{ss} = 0
\\
&\mathrm{Unit} \ \mathrm{ramp :} \ e_{ss} = e_{ss} = \frac{2}{K_I}
\end{align*}
%
which are exactly same with the I controller. Now, lets
compute closed-lopp transfer function
%
\begin{align*}
  T(s) = \frac{K_D s + K_I}{s^2 + (2+K_D) s + K_I}
\end{align*}
%
Now let's choose $K_I = 8$ and $K_P = 4$, and estimate maximum
over-shoot and settling time for the closed-loop plant.
%
\begin{align*}
p_{1} = -2 \ , \ p_{2} = -4
\quad \rightarrow \quad M_P = 0 \ , \ t_{s} \approx 2 s
\end{align*}
%
These PI gains can match the settling time value
of the original plant without any over-shoot (since closed-loot
TF is over-damped).

Let's illustrate this analytic observations, by
plotting the step responses of the
closed-loop system with only integral controller with $K_I = 2$,
the closed-loop system with only integral controller with $K_I = 8$,
and the closed-loop system with PI controller with $K_P = 4 \ , K_I = 8$.

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.65\textwidth]{IvsPI}
    \end{center}
  \end{minipage}

\vspace{12 pt}

In this illustration, we can see that settling time for both
I controllers is around $4 s$, however settling time for the
PI controller is $1 s$ which is even better than our estimation.
The gap between settling time estimates come form the affect
of zero introduced by the PI controller. If one carefully, analyzes
the closed-loop transfer function he/she can see that a pole-zero
cancellation occurs (which may not be a good feature for practical
reasons). Technically, the closed-loop transfer function
is reduced to a first order system in this case (????).

We also see that when PI and I controllers has the same
$K_I$ gain, the over-shoot in I controller is $\% 30$ which is very
high.

\subsection{Proportional Integral Derivative (PID) Controller}

PID controller technically combines the advantages of
the PD and PI controllers, with the trade of increased
parameter and implementation complexity.
We know that PID controller has the following
transfer function form
%
\begin{align*}
 C(s) = K_P + K_D s + \frac{K_I}{s}
\end{align*}
%
Let's first analyze the steady-state
performance again where the plant is a $2^{nd}$ system in standard form
%
\begin{align*}
  &G_{OL}(s) = \frac{( K_D s^2 +  K_P s +  K_I ) \omega_n^2}{s (s^2 + 2 \zeta
    \omega_n s + \omega_n^2)}
\\
 &Type \ 1 \quad K_v = K_I
\end{align*}
%
Thus steady-state error for unit-step and unit-ramp inputs can be find
as
\begin{itemize}
\item Unit step: $e_{ss} = 0$
\item Unit ramp: $e_{ss} = \frac{1}{K_I}$, i.e. $K_I \nearrow \ \Rightarrow \ e_{ss} \searrow$
\end{itemize}
  %
In other words $PID$, $PI$, and $I$ controllers share the same steady-state
performance characteristics. Now, let's compte the closed-loop transfer function
%
\begin{align*}
  T(s) &= \frac{( K_D s^2 +  K_P s +  K_I ) \omega_n^2}{ s^3 + (2 \zeta
         \omega_n + K_D \omega_n^2) s^2 + (1 + K_P) \omega_n^2 s +  K_I}
\end{align*}
%
We can see that the closed-loop transfer function has three poles, and
we have three parameters to tune. If we have no limits on gains, we
can place the closed-loop poles to any desired location. However,
numerator has now two zeros, thus it is now harder to predict the
affect of closed-loop zeros on the output behavior.

\textbf{Example 4:} Let $G(s) = \frac{1}{s^2 + 4 s + 5}$, Design a
PID controller such that we observe zero unit-step steady state error,
maximum percent overs-shoot is less than $\% 5$, and settling time
is around $1 s$.

\textbf{Solution:} We already know that PID controller for a Type 0
plant completely eliminates the unit-step steady state error. Now
let's compte closed-loop transfer function
%
\begin{align*}
  T(s) &= \frac{K_D s^2 +  K_P s +  K_I}{ s^3 + (4 + K_D) s^2 + (5 +K_P) s +  K_I}
\end{align*}
%
One way of choosing appropriate pole locations for a third-order
closed-loop system is placing one of the poles (a real one) far away from the
other poles such that closed-loop system shows a second order
like behavior. We require that maximum over shoot of $\% 5$
and settling time is around $1 s$. Let the dominant poles of the
closed-loop system be
%
\begin{align*}
  p_{1,2} = -4 \pm 3 j
\end{align*}
%
Settling time and maximum overs-shoot associated with this
closed-loop poles can be estimated as
%
\begin{align*}
 t_s \approx 1 s
\\
M_P \approx e^{-pi 4/3} = 0.015
\end{align*}
%
These estimates satisfy the requirements. Let $p_3 = 3 (-4) = -12$, then
desired characteristic equation takes the form
%
\begin{align*}
d^*(s) = (s + 12)(s^2 + 8 s + 25) = s^2 + 20 s + 121 s + 300
\end{align*}
%
Then, we can compute the PID gains as
%
\begin{align*}
  K_P &=  116 \\
  K_I &= 300 \\
  K_D &= 16\\
\end{align*}
%
The first thing, we can observe is that the quantitive values of the gains
seem to be much larger than the gains that we played before.
In general, if we want to improve the performance of both
steady-state and transient characteristics by implementing PID
topology instead of P, PI, or PD the gain values would go up
which may cause practical problems and potentially
can be costly in terms of energetic performance.

Now let's plot the step response of the closed-loop
system and try to verify these analytic observations

\vspace{12 pt}

  \begin{minipage}[h]{1\linewidth}
    \begin{center}
      \includegraphics[width=0.5\textwidth]{PIDe}
    \end{center}
  \end{minipage}

\vspace{12 pt}

We can see that the settling time in numerical simulation
is much better than our estimate $0.6 s < 1 s$, however
numerical percentage over-shoot is higher than our estimate,
$\% 11 > \% 1.5$, and does not meet our specifications.
The core reason behind this is that it is basically harder to
tune a PID controller compared to P, PD, and PI controllers
due to increased parametric complexity and (may be more importantly)
$2^{nd}$ numerator dynamics introduced with the PID control
policy.

% **** This ENDS THE EXAMPLES. DON'T DELETE THE FOLLOWING LINE:
\end{document}


\chapter{Tabular Data}\label{Ch.1}

What does data look like? For most people, the first image that comes to mind is a spreadsheet, where each row represents something being measured and each column a type of measurement. This stereotype exists for a reason; many real-world data sets can indeed be organized this way. Data that can be represented using rows and columns is called \textit{tabular data}. The rows are also called \textit{observations} or \textit{records},  while the columns are called \textit{variables} or \textit{fields}. The different terms reflect the diverse communities within data science, and their origins are summarized in the table below.
\begin{tabular}{l l l}
 & Rows & Columns \\
\hline
Statisticians & "observations" & "variables" \\
Computer Scientists & "records" & "fields" \\
\end{tabular}

\section{Pandas DataFrames}\label{1.1}

The table below is an example of a
data set that can be represented in tabular form.
This is a sample of user profiles in the
San Francisco Bay Area from the online dating website
OKCupid. In this case, each observation is an OKCupid user, and the variables include age, body type, height, and
(relationship) status. Although a
\verb|DataFrame| can contain values of all types, the
values within a column are typically all of the same
type---the age and height columns store
numbers, while the body type and
status columns store strings. Some values may be missing, such as body type for the first user
and diet for the second.
\begin{tabular}{l l l l l l l}
age & body type & diet & ... & smokes & height & status \\
\hline
31 &  & mostly vegetarian & ... & no & 67 & single \\
31 & average &  & ... & no & 66 & single \\
43 & curvy &  & ... & trying to quit & 65 & single \\
... & ... & ... & ... & ... & ... & ... \\
60 & fit &  & ... & no & 57 & single \\
\end{tabular}

Within Python, tabular data is typically stored in
a special type of object called a \verb|DataFrame|. A \verb|DataFrame| is optimized for storing tabular data; for example, it uses the fact that the values within a column are all the same type to save memory and speed up computations. Unfortunately, the \verb|DataFrame| is not built into base Python, a reminder that Python is a general-purpose programming language. To be able to work with \verb|DataFrame|s, we have to import a data science package called \verb|pandas|, which essentially does one thing---define a data structure called a \verb|DataFrame| for storing tabular data. But this data structure is so fundamental to data science that importing \verb|pandas| is the very first line of many Jupyter notebooks and Python scripts:

\begin{lstlisting}[language=Python]
import pandas as pd
\end{lstlisting}

This command makes \verb|pandas| objects and utilities
available under the abbreviation \verb|pd|.

\subsection{Reading From CSV}\label{1.1.1}

How do we get data, which is ordinarily stored in a file on disk,
into a \verb|pandas| \verb|DataFrame|? \verb|pandas| provides
several utilities for reading data. For example,
the OKCupid data in
the table above is stored as a \textit{comma-separated values} (CSV) file on
the web, available at the URL https://dlsun.github.io/pods/data/okcupid.csv.

We can read in this file from the web using the \verb|read_csv| function in \verb|pandas|:

\begin{lstlisting}[language=Python]
data_dir = "https://dlsun.github.io/pods/data/"
df_okcupid = pd.read_csv(data_dir + "okcupid.csv")
df_okcupid.head()
\end{lstlisting}

The \verb|read_csv| function is also able
to read in a file from disk. It automatically infers
where to look based on the file path.
Unless the path is obviously a URL (e.g., it begins with \verb|http://|), it looks for the file
on the local machine.

Notice above how missing values are represented in a \verb|pandas| \verb|DataFrame|. Each missing value is represented by a \verb|NaN|, which is short for "not a number". As we will see, most \verb|pandas| operations simply ignore \verb|NaN| values.

\subsection{Exercises}\label{1.1.2}

\begin{enumerate}
\item
Download the OKCupid data set above to your workstation and use \verb|read_csv| to read in the file from your local machine.

\item
Read in the Framingham Heart Study data set,
which is available at the URL \verb|https://dlsun.github.io/pods/data/framingham_long.csv|. Be sure to give the \verb|DataFrame| an
informative variable name.

\end{enumerate}

\section{Rows and the Observational Unit}\label{1.2}

Recall that the rows of a tabular data set represent observations. Whenever you encounter a new (tabular) data
set, the first question you should ask yourself is:
\begin{displayquote}
"What is the observational unit?"
\end{displayquote}

In other words, what does each row of the
\verb|DataFrame| represent? In the case of
the OKCupid data set in the previous section, the observational unit was clearly an OKCupid user. But it is not always so obvious what the observational unit is.

For example, consider the Framingham Heart Study data set, which is available at \verb|https://dlsun.github.io/pods/data/framingham_long.csv|.
This data comes from a study of men and women in
the town of Framingham, Massachusetts, which has enrolled
thousands of patients since it began in 1948 and is still ongoing. The goal of the study is to identify risk factors for cardivascular disease (CVD) by following the subjects over time. The data set that we will analyze was collected on 4,434 subjects between 1956 and 1968. A description of the data set is available \verb|https://biolincc.nhlbi.nih.gov/media/teachingstudies/FHS_Teaching_Longitudinal_Data_Documentation.pdf|.

You might guess that the observational unit is a subject. Let's see if that guess is correct.

\begin{lstlisting}[language=Python]
import pandas as pd

data_dir = "https://dlsun.github.io/pods/data/"
df_framingham = pd.read_csv(data_dir + "framingham_long.csv")
df_framingham.head()
\end{lstlisting}

\small\begin{verbatim}
   RANDID  SEX  TOTCHOL  AGE  ...  TIMESTRK  TIMECVD  TIMEDTH  TIMEHYP
0    2448    1    195.0   39  ...      8766     6438     8766     8766
1    2448    1    209.0   52  ...      8766     6438     8766     8766
2    6238    2    250.0   46  ...      8766     8766     8766     8766
3    6238    2    260.0   52  ...      8766     8766     8766     8766
4    6238    2    237.0   58  ...      8766     8766     8766     8766

[5 rows x 39 columns]
\end{verbatim}

Each \verb|RANDID| corresponds to a unique subject in the study, but each subject appears multiple times in the data set. That is because this is a longitudinal study; each subject was measured at multiple points during their lifetime. So the observational unit in the Framingham Heart Study data set is a \textit{measurement} of a subject at a point in time.

If there is a variable or a set of variables in the data set that uniquely identifies the observational unit, then it is customary to make those variables the index the \verb|DataFrame|. In the Framingham data set, \verb|RANDID| and \verb|TIME| uniquely identify the observational unit, so we move these columns to the index. (Notice that we specify \verb|inplace=True| so that \verb|.set_index()| modifies the existing \verb|DataFrame| rather than returning a new one.)

\begin{lstlisting}[language=Python]
df_framingham.set_index(["RANDID", "TIME"], inplace=True)
df_framingham.head()
\end{lstlisting}

\small\begin{verbatim}
             SEX  TOTCHOL  AGE  SYSBP  ...  TIMESTRK  TIMECVD  TIMEDTH  TIMEHYP
RANDID TIME                            ...
2448   0       1    195.0   39  106.0  ...      8766     6438     8766     8766
       4628    1    209.0   52  121.0  ...      8766     6438     8766     8766
6238   0       2    250.0   46  121.0  ...      8766     8766     8766     8766
       2156    2    260.0   52  105.0  ...      8766     8766     8766     8766
       4344    2    237.0   58  108.0  ...      8766     8766     8766     8766

[5 rows x 37 columns]
\end{verbatim}

\subsection{Selecting Rows}\label{1.2.1}

We can select an individual row from a \verb|DataFrame| using its label in the index. For example, the fourth row in the Framingham data set above has label \verb|(6238, 2156)|. The \verb|.loc| attribute of the \verb|DataFrame| is used to select a row by its label.

\begin{lstlisting}[language=Python]
row = df_framingham.loc[(6238, 2156)]
row
\end{lstlisting}

We can also select a row by its position using the \verb|.iloc| attribute. Keeping in mind that the first row is actually row 0, the fourth row could also be extracted as:

\begin{lstlisting}[language=Python]
df_framingham.iloc[3]
\end{lstlisting}

Notice that a single row from a \verb|DataFrame| is no longer a \verb|DataFrame| but a different data structure, called a \verb|Series|.

\begin{lstlisting}[language=Python]
type(row)
\end{lstlisting}

We can also select multiple rows by passing a \textit{list} of labels or positions to \verb|.loc| and \verb|.iloc|, respectively.

\begin{lstlisting}[language=Python]
rows = df_framingham.loc[[(2448, 4628), (6238, 2156)]]
rows
\end{lstlisting}

\begin{lstlisting}[language=Python]
df_framingham.iloc[[1, 3]]
\end{lstlisting}

Notice that when we select multiple rows, we get a \verb|DataFrame| back.

\begin{lstlisting}[language=Python]
type(rows)
\end{lstlisting}

So a \verb|Series| is used to store a single observation (across multiple variables), while a \verb|DataFrame| is used to store multiple observations (across multiple variables).

If selecting consecutive rows, we can use Python's \verb|slice| notation. For example, the code below selects all rows from the fourth row, up to (but not including) the tenth row.

\begin{lstlisting}[language=Python]
df_framingham.iloc[3:9]
\end{lstlisting}

\subsection{Exercises}\label{1.2.2}

\begin{enumerate}
\item
Suppose you want to extract just the fourth row from the Framingham data set, but you want the result to be a \verb|DataFrame| instead of a \verb|Series|. In other words, you want a \verb|DataFrame| with a single row. Can you figure out how to accomplish this?

\item
Questions 2-4 deal with the Titanic data set \verb|https://dlsun.github.io/pods/data/titanic.csv|. Read in the Titanic data set. What is the observational unit?

\item
What column seems to be appropriate index for this data set? Do you see any problems with using this column as the index? (\textit{Hint:} Try looking up "Kelly, Mr. James" or "Green, Mr. George" in this \verb|DataFrame|.)

\item
Regardless of your reservations in the previous question, make "name" the index of the \verb|DataFrame|. Use this to extract a \verb|DataFrame| containing information about the three members of the Widener family (Widener, Mr. George Dunton, Widener, Mr. Harry Elkins, Widener, Mrs. Eleanor). What became of them? Using a search engine, what else can you learn about them?

\end{enumerate}

\begin{lstlisting}[language=Python]

\end{lstlisting}

\section{Columns and Variables}\label{1.3}

Recall that the columns of a tabular data set represent variables. They are the measurements that we make on each observation.

As an example, let's consider the variables in the OKCupid data set. This data set does not have a natural index, so we use the default index (0, 1, 2, ...).

\begin{lstlisting}[language=Python]
import pandas as pd

data_dir = "https://dlsun.github.io/pods/data/"
df_okcupid = pd.read_csv(data_dir + "okcupid.csv")
df_okcupid.head()
\end{lstlisting}

\subsection{Types of Variables}\label{1.3.1}

There is a fundamental difference between variables like \verb|age| and \verb|height|, which can be measured on a numeric scale, and variables like \verb|religion| and \verb|orientation|, which cannot be.

Variables that can be measured on a numeric scale are called \textbf{quantitative variables}. Just because a variable happens to contain numbers does not necessarily make it "quantitative". For example, in the Framingham data set, the \verb|SEX| column was coded as 1 for men and 2 for women. However, these numbers are not on any meaningful numerical scale; a woman is not "twice" a man.

Variables that are not quantitative but take on a limited set of values are called \textbf{categorical variables}. For example, the variable \verb|orientation| takes on one of three possible values (gay, straight, or bisexual), so it is a categorical variable. So is the variable \verb|religion|, which takes on a larger, but still limited, set of values. We call each possible value of a categorical variable a "level". Levels are usually non-numeric.

Some variables do not fit neatly into either classification. For example, the variable \verb|essay1| contains users' answers to the prompt "What I’m doing with my life". This variable is obviously not quantitative, but it is not categorical either because every user has a unique answer. In other words, this variable does not take on a limited set of values. We will group such variables into an "other" category.

Every variable can be classified into one of these three \textbf{types}:
\begin{itemize}
\item
quantitative,

\item
categorical, or

\item
other.

\end{itemize}

The type of the variable often dictates how we analyze that variable, as we will see in the next two chapters.

\subsection{Selecting Variables}\label{1.3.2}

Suppose we want to select the \verb|age| column from the \verb|DataFrame| above. There are three ways to do this.

1.  Use \verb|.loc|, specifying both the rows and columns. (The colon \verb|:| is Python shorthand for "all".)

\begin{lstlisting}[language=Python]
df_okcupid.loc[:, "age"]
\end{lstlisting}

2. Access the column as you would a key in a \verb|dict|.

\begin{lstlisting}[language=Python]
df_okcupid["age"]
\end{lstlisting}

3. Access the column as an attribute of the \verb|DataFrame|.

\begin{lstlisting}[language=Python]
df_okcupid.age
\end{lstlisting}

Method 3 (attribute access) is the most concise. However, it does not work if the variable name contains spaces or special characters, begins with a number, or matches an existing attribute of \verb|DataFrame|. For example, if \verb|df_okcupid| had a column called \verb|head|, \verb|df_okcupid.head| would not return the column because \verb|df_okcupid.head| is already reserved for something else.

Notice that a \verb|Series| is used here to store a single variable (across multiple observations). In the previous section, we saw that a \verb|Series| can also be used to store a single observation (across multiple columns). To summarize, the \verb|Series| data structure is used to store either a single row or a single column in a tabular data set. In other words, while a \verb|DataFrame| is two-dimensional (containing both rows and columns), a \verb|Series| is one-dimensional.

To select multiple columns, you would pass in a \textit{list} of variable names, instead of a single variable name. For example, to select both \verb|age| and \verb|religion|, either of the two methods below would work (and produce the same result):

\begin{lstlisting}[language=Python]
# METHOD 1
df_okcupid.loc[:, ["age", "religion"]].head()

# METHOD 2
df_okcupid[["age", "religion"]].head()
\end{lstlisting}

\subsection{Type Inference and Casting}\label{1.3.3}

\verb|pandas| tries to infer the type of each variable automatically. If every value in a column (except for missing values) is a number, then \verb|pandas| will treat that variable as quantitative. Otherwise, the variable is treated as categorical.

To determine the type that Pandas inferred, simply select that variable using the methods above and look for its \verb|dtype|. A \verb|dtype| of \verb|float64| or \verb|int64| indicates that the variable is quantitative.  For example, the \verb|age| variable has a \verb|dtype| of \verb|int64|, so it is quantitative.

\begin{lstlisting}[language=Python]
df_okcupid.age
\end{lstlisting}

On the other hand, the \verb|religion| variable has a \verb|dtype| of \verb|object|, so \verb|pandas| will treat it as categorical.

\begin{lstlisting}[language=Python]
df_okcupid.religion
\end{lstlisting}

Sometimes it is necessary to convert quantitative variables to categorical variables and vice versa. This can be achieved using the \verb|.astype()| method of a \verb|Series|. For example, to convert \verb|age| to a categorical variable, we simply cast its values to strings.

\begin{lstlisting}[language=Python]
df_okcupid.age.astype(str)
\end{lstlisting}

To save this as a column in the \verb|DataFrame|, we assign it to a column called \verb|age_cat|. (Note that this column does not exist yet! It will be created at the time of assignment.)

\begin{lstlisting}[language=Python]
df_okcupid["age_cat"] = df_okcupid.age.astype(str)

# Check that age_cat is a column in this DataFrame
df_okcupid.head()
\end{lstlisting}

\subsection{Exercises}\label{1.3.4}

Exercises 1-2 deal with the Titanic data set \verb|https://dlsun.github.io/pods/data/titanic.csv|

1. Read in the Titanic data set. Identify each variable in the Titanic data set as either quantitative, categorical, or other. Cast all variables to the right type and assign them back to the \verb|DataFrame|.

\begin{lstlisting}[language=Python]
# YOUR CODE HERE
\end{lstlisting}

2. Create a \verb|DataFrame| (not a \verb|Series|) consisting of just the \verb|class| column.

\begin{lstlisting}[language=Python]
# YOUR CODE HERE
\end{lstlisting}


% This is a model template for the solutions in computational science. You can find a very useful documentation for LaTeX in Finnish at ftp://ftp.funet.fi/pub/TeX/CTAN/info/lshort/finnish/ or in English at ftp://ftp.funet.fi/pub/TeX/CTAN/info/lshort/english/. The section List of mathematical symbols in Chapter 3 is especially useful for the typesetting of mathematical formulas.

% Compile the document to PDF by command 'pdflatex model.tex' in the terminal. The command must be run twice for the references in the text to be correct.

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
% This includes letters such as � and �
\usepackage[T1]{fontenc}
% Use here 'Finnish' for Finnish hyphenation. You may have to compile the code twice after the change.
\usepackage[english]{babel}
\usepackage{graphicx}
% Some math stuff
\usepackage{amsmath,amsfonts,amssymb,amsbsy,commath,booktabs,hyperref}
% This is just to include the urls
\usepackage{hyperref,subcaption}
\usepackage[margin=2cm]{geometry}

\setlength{\parindent}{0mm}
\setlength{\parskip}{1.0\baselineskip}
\usepackage{minted}
\usemintedstyle{tango}

\usepackage{listings}
\usepackage{color}
\usepackage{pdfpages}
\DeclareMathAlphabet{\pazocal}{OMS}{zplm}{m}{n}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
	language=Python,
	aboveskip=3mm,
	belowskip=3mm,
	showstringspaces=false,
	columns=flexible,
	basicstyle={\tiny\ttfamily},
	numbers=none,
	numberstyle=\tiny\color{gray},
	keywordstyle=\color{blue},
	commentstyle=\color{dkgreen},
	stringstyle=\color{mauve},
	breaklines=true,
	breakatwhitespace=true,
	tabsize=4
}

\begin{document}

\title{CS-E4830 Kernel Methods in Machine Learning \\ Assignment 1} % Replace the exercise round number
\author{Kunal Ghosh, 546247} % Replace with your name and student number
\maketitle
\section{Solution to Question 1}
\subsection{(a) True}
Its given that $K_1$ and $K_2$ are Positive semi-definite matrices. So, for any vector $v$ we have
\begin{equation}
    \begin{split}
        & v^{T}K_1v \ge 0 \text{ and } v^{T}K_2v \ge 0\\
        \text{from question } K & = aK_1 + bK_2 \text{ where } a,b \in \pazocal{R}^+ \\
        \text{then, } v^TKv &= v^T (aK_1 + bK_2) v \\
        & = v^T (aK_1) v + v^T (bK_2) v \\
        & = a (v^TK_1v) + b (v^TK_2)v \\
        & \text{Since, } a,b > 0 \text{ and } v^TK_1v, v^TK_2v \ge 0\\
        & \implies a (v^TK_1v) + b (v^TK_2)v \ge 0 \\
        & \implies  v^T (aK_1 + bK_2) v \ge 0 \\
        & \implies v^TKv \ge 0 \\
    \end{split}
\end{equation}
So, $K$ is Positive semi-definite. Hence a Kernel Matrix.
\subsection{(b) False}
If matrix $K$ is defined as ${K = K_1 - K_2}$, where ${K_1 , K_2}$ are positive semi-definite matrices, then
\begin{equation}
    \begin{split}
        v^TKv &= v^T(K_1 - K_2)v \\
        &= v^T(K_1)v - v^T(K_2)v \\
    \end{split}
\end{equation}
We know that, ${ v^TK_1v, v^TK_2v \ge 0 }$. However, the difference of two non-negative numbers is not always non-negative.
\begin{equation}
    \begin{split}
        & \implies v^T(K_1)v - v^T(K_2)v \ngeq 0 \\
        & \implies v^TKv \ngeq 0 \\
    \end{split}
\end{equation}

Hence ${K = K_1 - K_2}$ is not always positive semi-definite. Hence $K$ need not be a Kernel Matrix.
\subsection{(c) False}
If matrix $K$ is defined as ${K = K_1K_2}$ where ${K_1 , K_2}$ are positive semi-definite matrices, then\\
\textbf{[Counter Example]} : Product of two symmetric matrices is not always symmetric. \\
for, ${K_1 =
\begin{bmatrix}
       1 & -1 \\[0.3em]
       -1 & 1 \\[0.3em]
\end{bmatrix}
}$ and ${K_2 =
\begin{bmatrix}
       -2 & 1 \\[0.3em]
       1 & -3 \\[0.3em]
\end{bmatrix}
}$

where $\text{det}(K_1) > 0$ and $\text{det}(K_2) > 0$ so all the eigen values of $K_1$ and $K_2$ are positive.
Hence $K_1$ and $K_2$ are positive semi-definite matrices. But, \\${K = K_1K_2 =
\begin{bmatrix}
       -3 & 4 \\[0.3em]
       3 & -4 \\[0.3em]
\end{bmatrix}
}$\\
is not a symmetric matrix. Therefore ${K = K_1K_2}$ need not be a Kernel Matrix.
\subsection{(d) False}
\textbf{[Counter Example]} : Examples of $K_1$ and $K_2$ given above (previous section) are valid Kernel Matrices but don't have all positive entries.
\subsection{(e) True}
\includepdf[pages=-,scale=1]{1_5.pdf}
\includepdf[pages={1,2},scale=1]{2.pdf}
\section{Solution to Question 3}
\inputminted[baselinestretch=1, fontsize=\small, breaklines=true]{octave}{../gaussian_kernel.m}
\section{Solution to Question 4}
\inputminted[baselinestretch=1, fontsize=\small, breaklines=true]{octave}{../parzen_classify.m}
\section{Solution to Question 5}
In the caption in the figures below $\sigma$ is a parameter of the gaussian kernel which is used for the task of classification in this problem. Among the figures, fig-\ref{fig:1} shows the learning curves and fig-\ref{fig:2} shows the decision boundary for various values of $\sigma$. More description in the captions of the figures.
\begin{figure}[ht!]
    \centering
    \includegraphics[scale=0.7]{learning_curves.png}
    \caption{Learning curves: Initially, with low values of sigma there is overfitting (high training accuracy, low test accuracy) as the sigma values increase the model generalizes better until sigma reaches $\approx 0.7$ then the model starts to underfit.}
    \label{fig:1}
\end{figure}
\begin{figure}[ht!]
    \centering
    \includegraphics[scale=0.4]{sigma_figs.png}
    \caption{Decision Boundaries: For low values of sigma overfitting is clearly visible (small "circles" tightly encircling certain datapoints). As the sigma increases the model generalizes better, for $\sigma \approx 0.7$ the test accuracy is the best (least number of red-circles inside the decision boundary). Finally, as the sigma keeps on increasing the decision boundary tends to a linear decision boundary which isn't the most ideal for this dataset as can be seen in the learning curves Fig-\ref{fig:1} above where both the training and test accuracy drops as the sigma is increased beyond $\approx$ 0.7.}
    \label{fig:2}
\end{figure}
\end{document}


\documentclass[]{beamer}
%\documentclass[handout]{beamer}
%\usepackage[dvips]{color}
\usepackage{graphicx}
\usepackage{amsmath,amssymb,array,comment,eucal}
\input{../macros}
\usepackage{verbatim}

\usetheme{Warsaw}
\title{Distribution Assumptions}
\subtitle{Merlise Clyde}
\author{STA721 Linear Models}
\institute{Duke University}
\date{\today}
\logo{duke.eps}

\begin{document}
\maketitle

\begin{frame}\frametitle{Outline}
Topics
  \begin{itemize}
  \item Normality \& Transformations
  \item Box-Cox
  \item Nonlinear Regression
  \end{itemize}

Readings: Christensen  Chapter 13  \& Wakefield Chapter 6
\end{frame}
%\section{Models}
\begin{frame} \frametitle{ Linear Model}
Linear Model again:
 $$ \Y = \mub + \eps $$ \pause
Assumptions: \pause
\begin{eqnarray*}
   \mub \in C(\X) & \Leftrightarrow & \mub = \X \b \pause \\
   \eps  & \sim &  \N(\zero_n, \sigma^2 \I_n) \pause
\end{eqnarray*}
\begin{itemize}
\item Normal Distribution for $\eps$ with constant variance \\
\item Outlier Models
\item Robustify with heavy tailed error distributions
\item Computational Advantages of Normal Models
\end{itemize}
\end{frame}

\begin{frame}
    \frametitle{Normality}
    Recall
    \begin{eqnarray*}
 \e & = &(\I - \P_\X) \Y \pause \\
 & = & (\I - \P_\X)(\X \bhat + \eps)  \pause\\
 & = &  (\I - \P_\X)\eps  \pause
    \end{eqnarray*}
$$e_i = \epsilon_i - \sum_{j=1}^n h_{ij} \epsilon_j$$  \pause
Lyapunov CLT\footnote{independent but not identically distributed} implies that  residuals will be approximately normal
(even for modest $n$),
if the errors are not normal  \pause

\vfill

``Supernormality of residuals''
  \end{frame}
  \begin{frame}
    \frametitle{Q-Q Plots}
\begin{itemize}
\item Order $e_i$: $e_{(1)} \le e_{(2)} \ldots \le e_{(n)}$  sample
    order statistics or sample quantiles  \pause
\item Let $z_{(1)} \le z_{(2)} \ldots z_{(n)}$ denote the expected
  order statistics of a sample of size $n$ from a standard normal
  distribution ``theoretical quantiles''  \pause
\item If the $e_i$ are normal then $\E[e_{(i)}] = \sigma z_{(i)}$   \pause
\item Expect that points in a scatter plot of $e_{(i)}$ and $z_{(i)}$
  should be on a straight line.  \pause
\item Judgment call - use simulations to gain experience!
\end{itemize}

  \end{frame}

  \begin{frame}
    \frametitle{Animal Example}

\centerline{\includegraphics[height=3in]{brain}}
  \end{frame}

  \begin{frame}
    \frametitle{Residual Plots}

\centerline{\includegraphics[height=3in]{brains-resid}}
  \end{frame}

  \begin{frame}
    \frametitle{Box-Cox Transformation}
    Box and Cox (1964) suggested a family of power transformations for
    $Y > 0$  \pause
$$
U(\Y, \lambda) =  Y^{(\lambda)} = \left\{
   \begin{array}{ll}
     \frac{(Y^\lambda -1)}{\lambda} & \lambda \neq 0 \\
 \log(Y) & \lambda = 0
   \end{array} \right.
$$  \pause

\begin{itemize}
\item Estimate $\lambda$ by maximum Likelihood  \pause
$$\cL(\lambda, \b, \sigma^2) \propto \prod f(y_i \mid \lambda, \b,
\sigma^2)$$

\item  $U(\Y, \lambda) = Y^{(\lambda)} \sim \N(\X\b, \sigma^2)$
  \pause
\item Jacobian term is $\prod_i y_i^{\lambda - 1}$ for all $\lambda$  \pause
\item Profile Likelihood based on substituting MLE $\b$ and $\sigma^2$
  for each value of $\lambda$ is
$$\log(\cL(\lambda) \propto (\lambda -1)
\sum_i \log(Y_i) - \frac{n}{2} \log(\SSE (\lambda))$$
\end{itemize}

  \end{frame}
  \begin{frame}
    \frametitle{ Profile Likelihood}

\centerline{\includegraphics[height=3in]{brains-BC}}
  \end{frame}

  \begin{frame}
    \frametitle{ Residuals After Transformation of Response}

\centerline{\includegraphics[height=3in]{brain-resid-logY}}

  \end{frame}
  \begin{frame}
    \frametitle{ Residuals After Transformation of Both}

\centerline{\includegraphics[height=3in]{brains-tran}}

  \end{frame}

 \begin{frame}
    \frametitle{ Transformed Data}

\centerline{\includegraphics[height=3in]{brain-log}}

  \end{frame}

  \begin{frame}
    \frametitle{Test that Dinos are Outliers}
    \begin{table}[ht]
\begin{center}
\begin{tabular}{lrrrrrr}
  \hline
 & Res.Df & RSS & Df & Sum of Sq & F & Pr($>$F) \\
  \hline
1 & 23 & 12.12 &  &  &  &  \\
  2 & 26 & 60.99 & -3 & -48.87 & 30.92 & 0.0000 \\
   \hline
\end{tabular}
\end{center}
\pause
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
  \hline
 & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
  \hline
(Intercept) & 2.1504 & 0.2006 & 10.72 & 0.0000 \\
  log(body) & 0.7523 & 0.0457 & 16.45 & 0.0000 \\
  Triceratops & -4.7839 & 0.7913 & -6.05 & 0.0000 \\
  Brachiosaurus & -5.6662 & 0.8328 & -6.80 & 0.0000 \\
  Dipliodocus & -5.2851 & 0.7949 & -6.65 & 0.0000 \\
   \hline
\end{tabular}
\end{center}
\end{table}
\pause
Dinosaurs come from a different population from mammals
\end{table}
  \end{frame}
\begin{frame}[fragile]  \frametitle{Model Selection Priors}
\begin{verbatim}
brains.bas = bas.lm(log(brain) ~ log(body) + diag(28),
   data=Animals,  prior="hyper-g-n", a=3,
   modelprior=beta.binomial(1,28),
   method="MCMC", n.models=2^17, MCMC.it=2^18)
# check for convergence
plot(brains.bas$probne0, brains.bas$probs.MCMC)
\end{verbatim}
\includegraphics[height=2in]{bas-animals-conv}
\end{frame}

\begin{frame}  \frametitle{image(brains.bas)}
\includegraphics[height=2.75in]{brains-image}

{\tt rownames(Animals)[c(6, 14, 16, 26)]  }

{\tt "Dipliodocus" "Human" "Triceratops" "Brachiosaurus"}
\end{frame}

  \begin{frame}\frametitle{Variance Stabilizing Transformations}
    \begin{itemize}
    \item If $Y - \mu$ (approximately) $N(0, h(\mu))$ \pause
    \item Delta Method implies that
$$g(Y) \stackrel{\cdot}{\sim}\N( g(\mu),  g'(\mu)^2 h(\mu)$$ \pause

\item Find function $g$ such that $g'(\mu)^2/h(\mu)$ is constant
$$g(Y) \sim N(g(\mu), c)$$\pause
    \end{itemize}

\begin{itemize}
\item Poisson Counts ($Y > 3$): $g$ is square root transformation \pause
\item Binomial: arcsin($ \sqrt{Y}$)
\end{itemize}
Note: transformation for normality may not be the same as the variance
stabilizing transformation; boxcox assumes mean function is correct \pause

\vspace{11pt}
Generalized Linear Models preferable

\end{frame}

\begin{frame}{Nonlinear Models}
Drug concentration of caldralazine  at time $X_i$ in a cardiac
failure patient given a single 30mg dose  $(D = 30)$ given by \pause

$$
\mu(\b) = \left[\frac{D}{V} \exp(-\kappa_e x_i) \right]
$$
with $\b = (V, \kappa_e)$  $V = volume$ and $\kappa_e$ is the
elimination rate \pause

If $\log(Y_i)  = \log(\mu(\b)) + \epsilon_i$ with $\epsilon_i \simiid
N(0, \sigma^2)$ then the model is intrinisically linear (can transform
to linear model)
\begin{eqnarray*}
\log(\mu(\b)) & = & \log\left[\frac{D}{V} \exp(-\kappa_e x_i) \right]  \\
  & = & \log[D] - \log(V) -\kappa_e x_i \\
log(Y_i) - \log[30] & = &\beta_0 + \beta_1 x_i + \epsilon_i
\end{eqnarray*}
\pause

\end{frame}

\begin{frame}[fragile]\frametitle{Nonlinear Least Squares}
\begin{verbatim}
> conc.nlm = nls( log(y) ~ log((30/V)*exp(-k*x)),
             data=df, start=list(V=vhat, k=khat))
> summary(conc.nlm)
Formula: log(y) ~ log((30/V) * exp(-k * x))
Parameters:
              Estimate Std. Error t value Pr(>|t|)
V.(Intercept) 16.66331    7.11923   2.341 0.057796 .
k.x            0.15211    0.02368   6.423 0.000673 ***

Residual standard error: 0.7411 on 6 degrees of freedom
Number of iterations to convergence: 0
Achieved convergence tolerance: 3.978e-09
\end{verbatim}
\end{frame}

\begin{frame} {Additive Errors}
  \begin{itemize}
  \item  under  multiplicative log normal errors model is equivalent
    to linear model \pause
  \item with additive Gaussian errors (or other distributions) model
    is intrinsically nonlinear - nonlinear least squares (or posterior
    sampling) \pause
  \end{itemize}
$$ Y_i = (30/V) * exp(-k * x_i) + \epsilon_i$$ \pause
$$\epsilon_i \simiid \N(0, \sigma^2)$$
\end{frame}
\begin{frame}[fragile]\frametitle{Intrinsically Nonlinear Model}
\begin{verbatim}
> summary(conc.nlm)
Formula: y ~ (30/V) * exp(-k * x)
Parameters:
  Estimate Std. Error t value Pr(>|t|)
V 13.06506    0.60899   21.45 6.69e-07 ***
k  0.18572    0.01124   16.52 3.14e-06 ***
---
Residual standard error: 0.05126 on 6 degrees of freedom
Number of iterations to convergence: 4
Achieved convergence tolerance: 7.698e-06
\end{verbatim}
\end{frame}
\begin{frame} {Fitted Values \& Residuals}
  \includegraphics[height=3in]{nonlinear}
\end{frame}

\begin{frame} \frametitle{Functions of Interest}
Interest is in
\begin{itemize}
\item clearance: $V \kappa_e$ \pause
\item elimination half-life $x_{1/2} = \log 2/\kappa_e$ \pause
\end{itemize}

  \begin{itemize}
  \item   Use properties of MLEs: asymptotically  $\hat{\b} \sim N\left(\b,
    I(\hat{\b})^{-1}\right)$ \pause
  \item (Multivariate) Delta Method for transformations  \pause
\item Asymptotic Distributions
  \end{itemize}

Bayes obtain the posterior directly for parameters and functions of parameters!    Priors?  Constraints on Distributions?
\end{frame}

\begin{frame}\frametitle{Summary}
  \begin{itemize}
  \item Optimal transformation for normality (MLE) depends on choice
    of mean function \pause
 \item May not be the same as the variance stabilizing transformation \pause
 \item Nonlinear Models as suggested by Theory or Generalized Linear
   Models are alternatives \pause
 \item ``normal'' estimates may be useful approximations for large $p$
   or for starting values for more complex models  (where convergence
   may be sensitive to starting values)
  \end{itemize}
\end{frame}
\end{document}


\documentclass[a4wide,12pt]{article}

\usepackage{verbatim}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{a4wide}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage{cite} % [2,3,4] --> [2--4]
\usepackage{shadow}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage[toc,page]{appendix}
\usepackage[squaren]{SIunits}

\renewcommand{\vec}{\mathbf}
\newcommand{\dm}[1]{\mathrm{d}#1}
\newcommand{\ihat}{\hat{\textbf{\i}}}
\newcommand{\jhat}{\hat{\textbf{\j}}}
\newcommand{\khat}{\hat{\textbf{k}}}
\providecommand{\e}[1]{\ensuremath{\times 10^{#1}}}

\begin{document}
\section*{Introduction to numerical projects}

Here follows a brief recipe and recommendation on how to write a report for each
project.
\begin{itemize}
\item Give a short description of the nature of the problem and the eventual
numerical methods you have used.
\item Describe the algorithm you have used and/or developed. Here you may find it convenient
to use pseudocoding. In many cases you can describe the algorithm
in the program itself.

\item Include the source code of your program. Comment your program properly.
\item If possible, try to find analytic solutions, or known limits
in order to test your program when developing the code.
\item Include your results either in figure form or in a table. Remember to
       label your results. All tables and figures should have relevant captions
       and labels on the axes.
\item Try to evaluate the reliabilty and numerical stability/precision
of your results. If possible, include a qualitative and/or quantitative
discussion of the numerical stability, eventual loss of precision etc.

\item Try to give an interpretation of you results in your answers to
the problems.
\item Critique: if possible include your comments and reflections about the
exercise, whether you felt you learnt something, ideas for improvements and
other thoughts you've made when solving the exercise.
We wish to keep this course at the interactive level and your comments can help
us improve it.
\item Try to establish a practice where you log your work at the
computerlab. You may find such a logbook very handy at later stages
in your work, especially when you don't properly remember
what a previous test version
of your program did. Here you could also record
the time spent on solving the exercise, various algorithms you may have tested
or other topics which you feel worthy of mentioning.
\end{itemize}

\section*{Format for electronic delivery of report and programs}
%
The preferred format for the report is a PDF file. You can also
use DOC or postscript formats or an ipython notebook file ipynb.
As programming language we prefer that you choose between C/C++, Fortran2008 or Python.
The following prescription should be followed when preparing the report:
\begin{itemize}
\item Use Devilry to hand in your projects, log in  at
\url{ http://devilry.ifi.uio.no} with your normal UiO username and password
and choose either 'fys3150' or 'fys4150'.
There you can load up the files within the deadline.
\item Upload {\bf only} the report file!  For the source code file(s) you have developed please provide us with your link to your github domain.
\item Comments  from us on your projects, approval or not, corrections to be made
etc can be found under
your Devilry domain and are only visible to you and the teachers of the course.

\end{itemize}

Finally,
we encourage you to work two and two together. Optimal working groups consist of
2-3 students. You can then hand in a common report.

\section*{Project 3 - Molecular dynamics: atomic modeling of argon, deadline Friday December 4th (noon)}
In this project we will implement a model called molecular dynamics (MD). MD allows us to study the dynamics of atoms and explore the phase space. For those of you who are taking FYS2160 - Thermodynamics, this project will be an excellent choice. The atoms interact through a force given as the negative gradient of a potential energy function. With this force, we can integrate Newton's laws. While exploring the phase space, we will sample statistical properties of the system like energy, temperature, pressure, diffusion constant and so on.

We will also learn how to do write object oriented code. You are given a code skeleton that contains the basic structure of an MD code, but you will have to implement many of the functions yourself. We think that \textit{learn by example} is a great way to learn how to proper object orient your code, but you can of course start from scratch if you want. You can find the code here: \url{https://github.com/andeplane/molecular-dynamics-fys3150}, but you should fork\footnote{A fork is a copy of a repository that will be added to your GitHub account so that you can continue working on the project. After forking a repository, you can commit, push and pull to your version.} the repository. Go to \url{https://github.com/andeplane/molecular-dynamics-fys3150} (remember to be logged in) and fork the project. After pressing the Fork button, you should have a fork that you can clone with \textit{git clone git@github.com:<username>/molecular-dynamics-fys3150.git}.

After you have downloaded the repository, open the project in Qt Creator and see if it runs. The program should create 100 argon atoms and place them uniformly inside a cubic box with sides 10 Angstroms. Each atom is given a velocity according to the Maxwell-Boltzmann distribution so that
\begin{align}
	\label{eq:maxwell_boltzmann}
	P(v)\dm{v} = \left(\frac{m}{2\pi k_B T}\right)^{1/2} \exp\left(-\frac{m v^2}{2k_B T}\right) \dm{v},
\end{align}
where $m$ is the mass of the atom, $k_B$ is Boltzmann's constant and $T$ is the temperature. We recognize this as a normal distribution with zero mean and standard deviation $\sigma = \sqrt{k_B T/m}$. Fun fact: we also recognize this as the Boltzmann distribution $P(E) \propto \exp{(-\beta E)}$, with $E = 0.5mv^2$. The program will evolve the system in time with no forces so that all atoms move in a straight line. It will create a file called \textit{movie.xyz} containing all the timesteps ready to visualize with for example Ovito\footnote{Download Ovito from \url{http://www.ovito.org/index.php/download}}. You should start with the fun part - to look at the simulation in Ovito. We then strongly recommend you to, before you start, look at the code structure and try to understand how the different classes are connected to each other and how a typical timestep is computed. You can skip understanding the contents of \textit{unitconverter.cpp} and \textit{vec3.cpp}. If you haven't written classes in C{}\verb!++! before, you might want to read the lecture notes or find a tutorial on the internet.

\begin{enumerate}
\item[I)] These are two very important steps that come even before part a. First, spend 30 minutes figuring out what molecular dynamics is and what problems it can be used to understand. What areas of physics can it be used in? What about chemistry and biology? What you find here should be part of your introduction section in your report.
\item[II)] Run the program, visualize the output with Ovito. Now, spend 30-60 minutes looking at the code and understand how the output is produced and try to understand every step in the code. The best way to do this is to start at the top of the \textit{main()} function and follow every line and every function call, step by step. \textbf{As a part if your report, you should explain the code structure and draw a flow chart (a diagram showing how the program is executed).} This part is extremely important and will save you a lot of time. And of course, please ask us teachers if you have any questions.

\item[a)]
When working with MD, the system size is usually limited by available computer resources. A typical large MD simulation (with a parallelized code) contains a few million atoms corresponding to a system much smaller than a cubic micron. In order to get rid of boundary effects, we usually apply periodic boundary conditions so that we simulate a system of infinite size. You need to implement the \textit{applyPeriodicBoundaryConditions} function in the \textit{System} class. After doing so, run the program again and notice how the atoms now are contained inside a box. Discuss the benefits of this strategy.

\item[b)]
The atoms are usually given velocities according to the Maxwell-Boltzmann distribution (you should discuss why this is the case) which will result in a nonzero net momentum in the system. Implement the \textit{removeMomentum} function in the \textit{System} class so that the net momentum is zero.

\item[c)]
The atoms are now uniformly distributed in space. This is not very physical, so we should place the atoms in a crystal structure. When we later implement the Lennard-Jones potential, we will see that the face-centered cubic (FCC) lattice is a stable structure for the potential (it is actually the crystalline structure of argon). A lattice is built up by \textit{unit cells} - a group of atoms - so that a larger system can be created by repeating these cells in space. An FCC lattice unit cell of size $\unit{\textit{b}}{\angstrom}$ (this is the lattice constant) consists of four atoms with local coordinates
\begin{align}
	\vec r_1 &= 0 \ihat + 0 \jhat + 0 \khat,\\
	\vec r_2 &= \frac{b}{2} \ihat + \frac{b}{2} \jhat + 0 \khat,\\
	\vec r_3 &= 0 \ihat + \frac{b}{2} \jhat + \frac{b}{2} \khat,\\
	\vec r_4 &= \frac{b}{2} \ihat + 0 \jhat + \frac{b}{2} \khat.
\end{align}
You can now create $N_x \times N_y \times N_z$ such unit cells next to each other to form a larger system so that the origin of unit cell $(i,j,k)$ is
\begin{align}
	\vec R_{i,j,k} = i \hat{\textbf{u}}_1 + j \hat{\textbf{u}}_2 + k \hat{\textbf{u}}_3,
\end{align}
where $i=0,1,..., N_x-1, j=0,1,..., N_y-1, k=0,1,..., N_z-1$. The unit vectors of the unit cells are scaled with the lattice constant $b$ so that
\begin{align}
	\hat{\textbf{u}}_1 = b\ihat, \quad \hat{\textbf{u}}_2 = b\jhat, \quad \hat{\textbf{u}}_3 = b\khat.
\end{align}
Implement the function \textit{createFCCLattice(int numberOfUnitCellsEachDimension, double latticeConstant)} in the \textit{System} class. Remember to remove the 100 atoms that are created as the code is right now. Use lattice constant $b=\unit{5.26}{\angstrom}$. Remember to update the system size so the \textit{applyPeriodicBoundaryConditions} function works properly. If you now visualize the result of the program, you should see the nice crystallic structure in the beginning of the simulation. What is the density $\rho$ in your system?

\item[d)]
While this looks nice, we need to implement the computation of forces in order to see interesting physics. In this project, we will use the Lennard-Jones potential\footnote{See \url{http://en.wikipedia.org/wiki/Lennard-Jones_potential} for more details.} which calculates the energy between two atoms $i$ and $j$ as
\begin{align}
	U(r_{ij}) = 4\epsilon\left[\left(\frac{\sigma}{r_{ij}}\right)^{12} - \left(\frac{\sigma}{r_{ij}}\right)^6\right],
\end{align}
where $r_{ij} = |\vec r_i - \vec r_j|$ is the distance from atom $i$ to atom $j$, $\epsilon$ is the depth of the potential well (units energy) and $\sigma$ is the distance at which the potential is zero. For argon, optimal values of the parameters are:
\begin{align}
	\frac{\epsilon}{k_B} = \unit{119.8}{\kelvin}, \sigma=\unit{3.405}{\angstrom}.
\end{align}
In our code, we use the so called MD units (see appendix \ref{sec:units}). This potential actually reproduces equilibrium thermodynamic properties that are in good agreement with experimental values for argon. Different phases such as solid, liquid and gas are reproduced from this simple model with phase transitions and everything which is rather remarkable.

The total potential energy $V$ in the system is computed by summing over all pairs of atoms (counting each pair only once)
\begin{align}
	V = \sum_{i>j} U(r_{ij}).
\end{align}
The force is calculated by taking the negative gradient of the potential
\begin{align}
	\vec F(r_{ij}) = -\nabla U(r_{ij}),
\end{align}
giving the $x$-component (the other components are calculated the same way)
\begin{align}
	F_x(r_{ij}) = -\frac{\partial U}{\partial r_{ij}}\frac{\partial r_{ij}}{\partial x_{ij}},
\end{align}
where $x_{ij}$ is the $x$-component of $\vec r_{ij}$.

Calculate (analytically) the force and implement the \textit{calculateForces} function in the LennardJones class. You will have to take care of the periodic boundary conditions\footnote{You could use the minimum image convention as described here \url{http://en.wikipedia.org/wiki/Periodic_boundary_conditions}.}. Now run the simulation with 5 unit cells in each dimension (500 atoms) for different initial temperatures. Can you tell if the system is in a solid state just by looking at the system in Ovito? At what initial temperature is the crystal melting?

\item[e)]
We now have a working Molecular dynamics code! Congratulations, well done. The next step is to calculate some physical properties of the system. The easiest properties to measure are the kinetic and potential energy (calculated in the LennardJones class). The kinetic energy is of course defined as
\begin{align}
	E_k = \sum_{i=1}^{N_\text{atoms}} \frac{1}{2} m_i v_i^2,
\end{align}
where $m_i$ and $v_i$ is the mass and the speed of atom $i$. You can use the function \textit{atom->velocity.lengthSquared()} to calculate the dot product of the velocities.

You can also calculate an estimate of the temperature through the equipartition theorem\footnote{See for example D. Schroeder's \textit{An Introduction to Thermal Physics} for details.}
\begin{align}
	\langle E_k \rangle = \frac{3}{2}N_\text{atoms} k_B T.
\end{align}
We can use this to define an \textit{instantaneous} temperature
\begin{align}
	\label{eq:temperature}
	T = \frac{2}{3}\frac{E_k}{N_\text{atoms} k_B}.
\end{align}

All of these quantities should be calculated in the \textit{StatisticsSampler} class. All the statistical quantities should be saved to a file which needs to be implemented in the \textit{saveToFile} function in the same class.

As you will see, since the temperature is proportional to the kinetic energy, its initial value will drop in the beginning when the system is initialized in the crystal structure. Why does this happen? What happens to the total energy? In order to have a system with final temperature $T$, what initial temperature $T_i$ is needed? What is the ratio $T/T_i$? Do multiple simulations with different random seeds and plot this ratio as a function of time. Again visualize with \textit{Ovito} and use your eyes to determine at what temperature the system actually melts. After the temperature drops, it will reach a more or less stable value after some time. The system is then said to be in an equilibrium state.

\item[f)]
The last exercise is to compute the diffusion coefficient and use this to measure the melting temperature. We use the Einstein relation that relates the so called mean square displacement (MSD) to the diffusion coefficient
\begin{align}
	\langle r^2(t) \rangle = 6Dt,
\end{align}
where $D$ is the diffusion coefficient, $t$ is time. The mean square displacement for atom $i$ is calculated as
\begin{align}
	r_i^2(t) = |\vec r_i(t) - \vec r_i(0)|^2,
\end{align}
where $\vec r_i(t)$ is the position of atom $i$ at time $t$. When you implement this you need to add the initial position as a new property in the \textit{Atom} class. You also need to update the \textit{applyPeriodicBoundaryConditions} so that $r_i^2(t)$ is the actual displacement as if no periodic boundary condition has occured. Why is this important?

We can use the diffusion coefficient to find the melting temperature. Atoms in a solid will not diffuse much, so a solid will have a diffusion coefficient close to zero. Now solve the Einstein relation for the diffusion coefficient $D$ and add a function in \textit{StatisticsSampler} measuring this quantity. Plot the diffusion coefficient as a function for different temperatures $T$ and use this to find an estimate for the melting temperature. Use the approximate melting temperature you found in e) to make sure you simulate temperatures both above and below the melting temperature. Also remember not to use the initial temperature, but the measured temperature after the system has reached equilibrium. You can use the builtin \textit{UnitConverter} to convert the temperature to SI units as illustrated in the top of the \textit{main} function.

\end{enumerate}

\begin{appendices}
\section{FYS2160 - what is really going on here?}
The idea of an MD simulation is to sample microstates from a statistical ensemble. In our simulation, we have a constant number of atoms $N$, a constant volume $V$ and a (more or less, depending on our integrator) constant energy $E$. This corresponds to the microcanonical ensemble (NVE).

Although we produce the dynamics of atoms, we should not see MD as a model that will give the true trajectories of the atoms, but rather a model that allows us to explore the phase space according to the probabilities we can calculate with statistical mechanics. A fundamental assumption in any MD simulation is the ergodic hypothesis. It states that\footnote{\url{http://en.wikipedia.org/wiki/Ergodic_hypothesis}.} \textit{"over long periods of time, the time spent by a system in some region of the phase space of microstates with the same energy is proportional to the volume of this region, i.e., that all accessible microstates are equiprobable over a long period of time"}. This means that by evolving the atoms through time will visit microstates of the ensemble according to the true probabilities given by the ensemble.

\section{Units}
\label{sec:units}
The program uses by default a set of units where we have chosen the following four units
\begin{align}
	\text{1 unit of mass } &= 1 \text{ a.m.u} = \unit{1.661\e{-27}}{\kilogram},\\
	\text{1 unit of length } &= \unit{1.0}{\angstrom} = \unit{1.0\e{-10}}{\meter},\\
	\text{1 unit of energy } &= \unit{1.651\e{-21}}{\joule},\\
	\text{1 unit of temperature} &= \unit{119.735}{\kelvin}.
\end{align}
Boltzmann's constant is then equal to one (convince yourself about this), and other units can be derived by using known relations. We can for example find the unit of time by using $E=mc^2$. We have that
\begin{align}
	\text{Energy} = \text{Mass} \times \frac{\text{Length}^2}{\text{Time}^2},
\end{align}
which can be solved for time
\begin{align}
	\text{Time} = \text{Length} \times \sqrt{\frac{\text{Mass}}{\text{Energy}}}.
\end{align}
By inserting the values above, we get
\begin{align}
	\text{1 unit of time } &= 1.0 \e{-10}\sqrt{\frac{1.661\e{-27}}{1.651\e{-21}}} \text{ s} = \unit{1.00224\e{-13}}{\second}.
\end{align}
This way, we can continue working out the values of the other units. What are the units of force? And pressure? In the project, a class that calculates these values is given.

\section{Visualizations in VMD}
\subsection{Installation}
\subsubsection{OS X}
The installation on Mac OS X should be fairly simple. Just visit \url{http://www.ks.uiuc.edu/Development/Download/download.cgi?PackageName=VMD} and download the version (the 1.9.1 version works, but feel free to try the 1.9.2 beta) called \textit{MacOS X OpenGL, CUDA (32-bit Intel x86)}. You will need to create an account, but don't worry. They won't send you any spam. Once you have downloaded the file, open the dmg-file and copy \textit{VMD 1.9.1} into your Applications folder and start the program.
\subsubsection{Linux (computer lab)}
The installation on Linux requires that we compile the source code. Since we don't have sudo access on the computer lab machines we need to configure the installation path. To simplify this process, we have written an installation script in Python that you can find here: \url{http://folk.uio.no/anderhaf/fys3150/install_vmd_linux.py}. Just download the script and run it with \textit{python install\_vmd\_linux.py}. When you run the script, \textit{VMD} will be installed in a folder called \textit{VMD} in the directory you run the script from. The same script should work on Ubuntu. The compiled executable is then put in \textit{./VMD/bin/} so that you can run vmd with the command \textit{./VMD/bin/vmd}.

\subsection{Usage}
VMD is a great tool to look at MD simulations. When you open VMD, you can load the file \textit{movie.xyz} by clicking \textit{File, New Molecule} and browsing to your file. Click \textit{Load} and the file is loaded into VMD. The default representation of atoms isn't very beautiful, so go to \textit{Graphics, Representations} and choose for example \textit{VDW} from the \textit{Drawing Method} list. You can make the spheres smaller by choosing a smaller \textit{Sphere Scale}.

\end{appendices}

\end{document}