% Created 2020-07-02 jue 10:37
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=1610]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{amssymb}
\DeclareMathOperator{\shift}{q}
\DeclareMathOperator{\diff}{p}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{2020-06-30}
\title{Computerized Control - difference equations, LSI, impulse response}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={Computerized Control - difference equations, LSI, impulse response},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.3.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Intro: Discrete-time signals and systems}
\label{sec:org0601f47}
\begin{frame}[label={sec:orga3fc324}]{Position control of a diskdrive arm}
\begin{columns}
\begin{column}{0.5\columnwidth}
\includegraphics[height=0.5\textheight]{../../figures/diskdrive.png}

\tiny "Laptop-hard-drive-exposed" by Evan-Amos - Own work. Licensed under CC BY-SA 3.0 via Commons
\end{column}
\begin{column}{0.5\columnwidth}
\[ J\ddot{\theta}(t) = u(t) + v(t) \]
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=10mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[block, right of=sum, node distance=18mm] (plant)  {$\frac{1}{Js^2}$};
    \node[coordinate, above of=sum, node distance=12mm] (disturbance) {};
    \node[coordinate, right of=plant, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$u(t)$} (sum);
    \draw[->] (sum) -- node[above] {} (plant);
    \draw[->] (plant) -- node[above, near end] {$\theta(t)$} (output);
    \draw[->] (disturbance) -- node[right, pos=0.2] {$v(t)$} (sum);
  \end{tikzpicture}
\end{center}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[label={sec:orgd6c9da9}]{Two approaches to designing a  discrete-time controller}
\begin{enumerate}
\item Do design the controller in the continuous-time domain (methods from control engineering class). Then discretize the continuous-time controller.
\includegraphics[width=0.7\linewidth]{../../figures/block1} \(F_d(z) \approx F(s)\)
\item Determine discrete-time model of the plant. Do design in discrete-time domain.
\begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

  \node[coordinate,] (refinput) {};
  \node[block, right of=refinput] (controller)  {Controller};
  \node[block, right of=controller, node distance=30mm] (zoh)  {Hold};
  \node[block, right of=zoh, node distance=26mm] (plant)  {Process};
  \node[block, right of=plant, node distance=26mm] (sampler)  {Sampler};
  \node[coordinate, right of=sampler, node distance=20mm] (output) {};

  \draw[->] (refinput) -- node[above, near start] {$y_{ref}(k)$} (controller);
  \draw[->] (controller) -- node[above, pos=0.4] {$u(k)$} (zoh);
  \draw[->] (zoh) -- node[above] {$u(t)$} (plant);
  \draw[->] (plant) -- node[above] {$y(t)$} (sampler);
  \draw[->] (sampler) -- node[pos=0.8, coordinate] (measure) {} node[above, near end] {$y(k)$} (output);
  \draw[->] (measure) -- ++(0,-20mm) -| (controller);
  \draw[red] (42mm, -13mm) rectangle (114mm, 8mm);
  \node[red] at (100mm, -10mm) {Discrete model};
\end{tikzpicture}
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:orgdfb76e8}]{Difference equations}
We saw two difference equations in the introductory lecture

\begin{enumerate}
\item Discretized version of continuous-time lead-compensator \(F(s)=K\frac{s+b}{s+a}\)
\begin{center}
\begin{tikzpicture}
\node[draw, inner sep=6pt] (block) {$K\frac{s+b}{s+a}$};
\draw[->] (block) ++ (2,0) -- node[above, near start] {$\theta(t)$} (block);
\draw[->] (block) -- node[above, near end] {$u_2(t)$}  ++(-2,0);
\end{tikzpicture}
\end{center}
\[ u_2(kh+h) - (1-ah)u_2(kh) = K\theta(kh+h) - K(1-bh)\theta(kh) \]
\item Discretized version of the double integrator \(G(s)=\frac{1}{Js^2}\)
\begin{center}
\begin{tikzpicture}
\node[draw, inner sep=6pt] (block) {$\frac{1}{Js^2}$};
\draw[<-] (block) ++ (2,0) -- node[above, near start] {$\theta(t)$} (block);
\draw[<-] (block) -- node[above, near end] {$u(t)$}  ++(-2,0);
\end{tikzpicture}
\end{center}
\[ \theta(k+2) - 2\theta(k+1) + \theta(k) = \frac{h^2}{J} u(k)\]
\end{enumerate}
\end{frame}

\begin{frame}[label={sec:orgf0f2966}]{Direct solution by iteration}
The difference equation can be used as a recipe to calculate the solution numerically

\alert{Example} Calculate the response of the lead-compensator \(u_2(kh+h) - (1-ah)u_2(kh) = K\theta(kh+h) - K(1-bh)\theta(kh)\) with \(a=8\), \(b=1\), \(h=0.1\), \(K=1\)
to the signal below
\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
      width=14cm,
      height=2.5cm,
      xlabel={$k$},
      ylabel={$\theta(k)$},
      xmin=-2.5,
      xmax=10.5,
      ]

      \addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { (k>=0)*(k<3) };

    \end{axis}
  \end{tikzpicture}
\end{center}
\begin{align*}
     u_2(k+1) &= 0.2u_2(k) + \theta(k+1) - 0.9\theta(k)\\
     u_2(0) &= 0.2u_2(-1) + \theta(0) - 0.9\theta(-1) = 0 + 1 -0 = 1\\
     u_2(1) &= 0.2u_2(0) + \theta(1) - 0.9\theta(0) = 0.2 + 1 - 0.9 = 0.3\\
     u_2(2) &= 0.2u_2(1) + \theta(2) - 0.9\theta(1) = 0.06 + 1 - 0.9 = 0.16\\
     u_2(3) &= 0.2u_2(2) + \theta(3) - 0.9\theta(2) = 0.032 + 0 - 0.9 = -0.868\\
     u_2(4) &= 0.2u_2(3) + \theta(4) - 0.9\theta(3) = -0.1736 + 0 - 0 = -0.1736\\
\end{align*}
\end{frame}

\begin{frame}[label={sec:orgabc8dff}]{Direct solution by iteration}
\begin{block}{Input}
\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
      width=14cm,
      height=2.5cm,
      xlabel={$k$},
      ylabel={$\theta(k)$},
      xmin=-2.5,
      xmax=10.5,
      ]

      \addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { (k>=0)*(k<3) };

    \end{axis}
  \end{tikzpicture}
\end{center}
\end{block}

\begin{block}{Output}
\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
      width=14cm,
      height=4.5cm,
      xlabel={$k$},
      ylabel={$\theta(k)$},
      ytick={-0.868, -0.1736, 0.16, 0.3, 1},
      xmin=-2.5,
      xmax=10.5,
      ]
      \addplot+[black, ycomb] coordinates {(-1,0) (0,1) (1, 0.3) (2,0.16) (3,-0.868)
	                                   (4,-0.1736) (5,-0.035) (6,-0.007) (7,-0.0014)};
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{block}
\end{frame}
\begin{frame}[label={sec:org6d480e0}]{Do on your own: Solution to the discrete double-integrator}
\alert{Activity} Calculate the response of the double-integrator
\[ \theta(k+2) - 2\theta(k+1) + \theta(k) = \frac{h^2}{J} u(k)\]
with \(h=1\), \(J=1\)

and initial values zero: \(\theta(k) = 0, \; k \le 0\)
to the signal below
\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
      width=14cm,
      height=3cm,
      xlabel={$k$},
      ylabel={$u(k)$},
      xmin=-2.5,
      xmax=10.5,
      ]

      \addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { (k>=0)*(k<2) - (k>=2)*(k<4) };
    \end{axis}
  \end{tikzpicture}
\end{center}

Sketch your solution as a graph on paper, photograph and send as PM on Remind.
\end{frame}

\begin{frame}[label={sec:org069f132}]{Do on your own: Solution to the discrete double-integrator}
\alert{Solution}
\footnotesize
\[ \theta(k+2) =  2\theta(k+1) - \theta(k) + u(k)\]
and initial values zero: \( \theta(k) = 0, \; k \le 0 \)
\begin{align*}
 \theta(0) &=  2\theta(-1) - \theta(-2) + u(-2) = 0\\
 \theta(1) &=  2\theta(0) - \theta(-1) + u(-1) = 0\\
 \theta(2) &=  2\theta(1) - \theta(0) + u(0) = 0 - 0 + 1 = 1\\
 \theta(3) &=  2\theta(2) - \theta(1) + u(1) = 2 - 0 + 1 = 3\\
 \theta(4) &=  2\theta(3) - \theta(2) + u(2) = 6 - 1 - 1 = 4\\
 \theta(5) &=  2\theta(4) - \theta(3) + u(3) = 8 - 3 - 1 = 4\\
 \theta(6) &=  2\theta(5) - \theta(4) + u(4) = 8 - 4 +0= 4\\
 \theta(7) &=  2\theta(6) - \theta(5) + u(5) = 8 - 4 +0= 4
 \end{align*}
   \begin{center}
     \begin{tikzpicture}
       \begin{axis}[
	 width=14cm,
	 height=3.5cm,
	 xlabel={$k$},
	 ylabel={$\theta(k)$},
	 xmin=-2.5,
	 xmax=10.5,
	 ]
	 \addplot+[black, ycomb] coordinates {(-2,0) (-1,0) (0,0) (1, 0) (2,1) (3,3)
	                                      (4, 4) (5,4) (6,4) (7,4) (8,4) (9,4)};
       \end{axis}
     \end{tikzpicture}
   \end{center}
\end{frame}

\begin{frame}[label={sec:org20ba333}]{A short intermezzo - the shift operator}
Recall the definition of the shift operator \(\shift\):
\[ \shift f(k) = f(k+1), \quad \text{for double-infinite sequence $f(k)$} \]

\alert{Activity} Show that the discretized lead-compensator
\[ u_2(k+1) = 0.2u_2(k) + \theta(k+1) - 0.9\theta(k)\]
can be written using the shift operator as
\[ u_2(k) = \frac{\shift -0.9}{\shift - 0.2} \theta(k).\]
\end{frame}

\begin{frame}[label={sec:orgfa278e9}]{Simple discrete-time controller for the disk drive arm}
\(J=1\) and \(h=1\) for simplicity.
\begin{center}
\begin{tikzpicture}
\tikzset{node distance=2cm,
    block/.style={rectangle, draw, minimum height=12mm, minimum width=14mm},
    sumnode/.style={circle, draw, inner sep=2pt}
}

  \node[coordinate] (input) {};
  \node[block, right of=input] (TR) {$0.2K$};
  \node[sumnode, right of=TR, node distance=30mm] (sum) {\tiny $\sum$};
  \node[block,right of=sum, node distance=30mm] (plant) {$\theta(k) = \frac{1}{(\shift-1)^2}u(k)$};
  %\node[sumnode, right of=plant, node distance=30mm] (sumdist) {$\sum$};
  %\node[coordinate, above of=sumdist, node distance=15mm] (dist) {};
  %\node[coordinate, right of=sumdist, node distance=15mm] (measure) {};
  \node[coordinate, right of=plant, node distance=30mm] (output) {};
  \node[coordinate, right of=plant, node distance=22mm] (measure) {};
  %\node[sumnode,below of=measure, node distance=25mm] (sumnoise) {$\sum$};
  %\node[coordinate, right of=sumnoise, node distance=15mm] (noise) {};
  \node[block,below of=plant, node distance=20mm] (SR) {$K\frac{\shift - 0.8}{\shift}$};

  \draw[->] (input) -- node[above, pos=0.2] {$\theta_{ref}(k)$} (TR);
  \draw[->] (TR) -- node[above] {$u_1(k)$} (sum);
  \draw[->] (sum) -- node[above] {$u(k)$} (plant);
  \draw[->] (plant) -- node[at end, above] {$\theta(k)$} (output);
  \draw[->] (measure) |- (SR);
  \draw[->] (SR) -| (sum) node[right, pos=0.8] {$u_2(k)$} node[left, pos=0.96] {$-$};
\end{tikzpicture}
\end{center}
Combine
\[ \theta(k+2) - 2\theta(k+1) + \theta(k) = u(k)\]
with the control-law
\begin{align*}
 u(k) &= u_1(k) - u_2(k)
      = 0.2K\theta_{ref}(k) - K(1 -0.8\shift^{-1})\theta(k)\\
      &=  0.2K\theta_{ref}(k) - K\big(\theta(k) -0.8\theta(k-1)\big)
\end{align*}
to get
\[ \theta(k+2) - 2\theta(k+1) + \theta(k) = 0.2K\theta_{ref}(k) - K\big(\theta(k) -0.8\theta(k-1)\big) \]
\end{frame}

\begin{frame}[label={sec:org8b2241d}]{Simple discrete-time controller for the disk drive arm, contd}
\[ \theta(k+2) - 2\theta(k+1) + \theta(k) = 0.2K\theta_{ref}(k) - K\big(\theta(k) -0.8\theta(k-1)\big) \]
\[ \theta(k+2)-2\theta(k+1) + (1+K)\theta(k) - 0.8K\theta(k-1) = 0.2K\theta_{ref}(k)\]

The solution consists of the sum of the \alert{homogenous solution} and the \alert{particular solution}
\[ \theta(k) = \theta_H(k) + \theta_P(k) \]

\begin{block}{Homogenous solution by \alert{ansatz}: \(\theta(k) = \alpha^k\)}
\[ \theta(k+2)-2\theta(k+1) + (1+K)\theta(k) - 0.8K\theta(k-1) = 0\]
\[ \alpha^{k+2} - 2\alpha^{k+1} + (1+K)\alpha^k - 0.8K\alpha^{k-1} = 0\]
\[ (\alpha^3 - 2\alpha^2 + (1+K)\alpha - 0.8K) \alpha^{k-1} = 0\]
A non-trivial solution \(\alpha \neq 0\) requires that the paranthesis is zero:
\[ \text{\bf Characteristic equation:} \quad \alpha^3 - 2\alpha^2 + (1+K)\alpha - 0.8K = 0\]
\end{block}
\end{frame}

\begin{frame}[label={sec:org2d13fd9}]{Simple discrete-time controller for the disk drive arm, contd}
The characteristic equation
   \[ \alpha^3 - 2\alpha^2 + (1+K)\alpha - 0.8K = 0\]

is of \alert{third} order and will  therefore have \alert{three} solutions \(\alpha_1\),  \(\alpha_2\) and \(\alpha_3\). These are the \alert{POLES} of  the closed-loop system!

The homogenous solution will consist of the linear combination
\[ \theta_H(k) = c_1 \alpha_1^k + c_2\alpha_2^k + c_3\alpha_3^k.\]
The solution will be stable if and only if
\[ |\alpha_j| < 1, \quad j=1,2,3\]
\end{frame}

\begin{frame}[label={sec:org70c3d4e}]{Simple discrete-time controller for the disk drive arm, contd}
Studying the characteristic equation using a root locus
\[ \alpha(\alpha -1)^2 + K(\alpha - 0.8) = 0\]
\alert{Group activity} Complete the root locus and choose a set of reasonable poles for the closed-loop system!

\begin{center}
  \begin{tikzpicture}[scale=2.5]
    \draw[->] (-1.2, 0) -- (1.2,0);
    \draw[->] (0, -1.2) -- (0,1.2);
    \node[red, pin=45:{2 plant poles}] at (1,0) {\large $\times$};
    \node[red, pin=135:{controller pole}] at (0,0) {\large $\times$};
    \node[green!70!black, pin=-145:{controller zero}] at (0.8,0) {\Large $\circ$};
    \node at (0.8, -0.2) {$0.8$};
    \node at (1, -0.2) {$1$};
    \draw[domain=0:360, samples=361, dashed] plot ({cos(\x)}, {sin(\x)});
    \node[coordinate, pin=60:{$|\alpha|=1$}] at (0.5, 0.87) {};
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org4424cd9}]{Simple discrete-time controller for the disk drive arm, solution}
\begin{center}
\includegraphics[width=0.6\linewidth]{rlocus-disk-arm.discrete}
\end{center}
\end{frame}

\section{Properties of discrete-time systems}
\label{sec:orgcbfc37f}

\begin{frame}[label={sec:org6768baf}]{Sampled systems are \alert{not} time invariant}
\includegraphics[height=0.6\linewidth]{../../figures/fig1-9.png}
\end{frame}

\begin{frame}[label={sec:orgcd9d813}]{The discrete-time causal linear shift-invariant system}
\begin{center}
\begin{tikzpicture}[node distance=20mm, anchor=north]
\node[coordinate] (input) {};
\node[rectangle, draw, right of=input, inner sep=3mm] (lti) {g};
\node[coordinate, right of=lti] (output) {};
\draw[->] (input) -- node[near start, above] {$u(k)$}  (lti);
\draw[->] (lti) -- node[near end, above] {$y(k)$} (output);
\end{tikzpicture}
\end{center}

\(g(k)\) is called the \alert{weighting sequence}.

\begin{block}{General (non-causal) LSI}
\[ y(k) = g \ast u = \sum_{n=-\infty}^\infty g(n) u(k-n) \]
\end{block}

\begin{block}{Causal LSI}
\[ y(k) = g \ast u = \sum_{n=0}^\infty g(n) u(k-n) \]
\end{block}
\end{frame}

\begin{frame}[label={sec:org005acb9}]{The discrete-time causal linear shift-invariant system}
\begin{block}{Impulse response}
If input signal is a unit pulse

\begin{center}
\begin{tikzpicture}
\begin{axis}[
  width=14cm,
  height=2.5cm,
  xlabel={$k$},
  ylabel={$u(k)$},
  xmin=-2.5,
  xmax=10.5,
]

\addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { (k==0)};

\end{axis}
\end{tikzpicture}
\end{center}

\vspace*{-5mm}

\[ y(k) = \sum_{n=0}^\infty g(n) \delta(k-n) = g(k) \]
\end{block}
\end{frame}

\begin{frame}[label={sec:org95ac3ec}]{The output of a causal, linear discrete-time system is a weighted sum of previous input}
\[ y(k) = g \ast u = \sum_{n=0}^\infty g(n) u(k-n) \]
The \alert{weighting sequence} \(g(k)\) is the \alert{impulse response} of the system.

What if the weighting sequence looks like this

\begin{center}
\begin{tikzpicture}
\small
\begin{axis}[
  width=14cm,
  height=3.5cm,
  xlabel={$k$},
  ylabel={$g(k)$},
  xmin=-0.5,
  xmax=10.5,
  ytick = {0, 1},
]

\addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { (k==4)};

\end{axis}
\end{tikzpicture}
\end{center}

\[y(k) = \]
\end{frame}

\begin{frame}[label={sec:org712e0e5}]{Linearity, shift invariance and the impulse response}
The input signal

\begin{center}
\begin{tikzpicture}
\small
\begin{axis}[
  width=14cm,
  height=3.5cm,
  xlabel={$k$},
  ylabel={$u(k)$},
  xmin=-0.5,
  xmax=10.5,
  ytick = {-1, 0, 0.6, 2},
]

\addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { 0.6*(k==0) + 2*(k==1) - 1*(k==2)};

\end{axis}
\end{tikzpicture}
\end{center}

Can be written
\[u(k) = 0.6\delta(k) + 2\delta(k-1) - \delta(k-2) \]
Since the system's response to a pulse is given by \(g(k)\), the output signal is
\[ y(k) = ?\]
\end{frame}

\begin{frame}[label={sec:orgb36aade}]{Linearity, shift invariance and the impulse response}
The input signal
\begin{center}
\begin{tikzpicture}
\small
\begin{axis}[
  width=14cm,
  height=3.5cm,
  xlabel={$k$},
  ylabel={$u(k)$},
  xmin=-0.5,
  xmax=10.5,
  ytick = {-1, 0, 0.6, 2},
]

\addplot+[black, ycomb, domain=-2:10, samples=13,variable=k] { 0.6*(k==0) + 2*(k==1) - 1*(k==2)};

\end{axis}
\end{tikzpicture}
\end{center}

Can be written
\[u(k) = 0.6\delta(k) + 2\delta(k-1) - \delta(k-2) \]
Since the system's response to a pulse is given by \(g(k)\), the output signal is
\[ y(k) = 0.6g(k) + 2g(k-1) - g(k-2) \]
\end{frame}

\section{First order system and pulse response}
\label{sec:orgbd0f28a}

\begin{frame}[label={sec:org757d292}]{First order system}
\begin{center}
\begin{tikzpicture}[node distance=20mm, anchor=north]
\node[coordinate] (input) {};
\node[rectangle, draw, right of=input, inner sep=3mm] (lti) {$G(q)=\frac{q}{q-a}$};
\node[coordinate, right of=lti] (output) {};
\draw[->] (input) -- node[near start, above] {$u(k)$}  (lti);
\draw[->] (lti) -- node[near end, above] {$y(k)$} (output);
\end{tikzpicture}
\end{center}

The system with pulse-transfer operator \(G(q)=\frac{q}{q-a}\) corresponds to the difference equation
\[ y(k) = G(q)u(k) \Leftrightarrow y(k) = \frac{q}{q-a} u(k) \]
\[ y(k+1) = ay(k) + u(k+1). \quad \text{If $a=1$, the system is a discrete-time integrator}\]
\end{frame}

\begin{frame}[label={sec:orgc876e7b}]{Impulse-response of a first order system}
\[ y(k+1) = ay(k) + u(k+1), \quad y(k)=0, \; k<0 \]
\[ u(k) = \delta(k) = \begin{cases} 1, & k=1\\ 0, & \text{otherwise}\end{cases} \]

\begin{block}{Solution}
\begin{align*}
y(0) &= ay(-1) + u(0) = 1\\
y(1) &= ay(0) + u(1) = a + 0 = a\\
y(2) &= ay(1) + u(2) = a*a + 0 = a^2\\
     &\vdots\\
y(n) &= a^n
\end{align*}
\end{block}
\end{frame}

\begin{frame}[label={sec:org8444f85}]{Impulse response of a first order system}
\[ y(k+1) = ay(k) + u(k+1) \]

\alert{Activity} Pair the impulse response to each of the values of \(a\)
\[ \text{I)}\; a=1 \qquad \text{II)}\; a=2 \qquad \text{III)}\; a = 0.5 \qquad \text{IV)}\; a=-0.9 \]

\begin{tikzpicture}
\small
\begin{axis}[
width=7cm,
height=2.5cm,
xlabel={$k$},
ylabel={$g(k)$},
xmin=-3.5,
xmax=10.5,
ytick = {-1,0,1},
ymin = -1.2, ymax=1.2,
]
\addplot+[black, ycomb, domain=-3:10, samples=14,variable=k] { (k>=0)*pow(1,k)};
\end{axis}

\begin{axis}[
xshift=7cm,
width=7cm,
height=2.5cm,
xlabel={$k$},
ylabel={$g(k)$},
xmin=-3.5,
xmax=10.5,
ytick = {0},
ytick = {-1,0,1},
ymin = -1.2, ymax=1.2,
]
\addplot+[black, ycomb, domain=-3:10, samples=14,variable=k] { (k>=0)*pow(-0.9,k)};
\end{axis}

\begin{axis}[
xshift=0cm,
yshift=-2.5cm,
width=7cm,
height=2.5cm,
xlabel={$k$},
ylabel={$g(k)$},
xmin=-3.5,
xmax=10.5,
ytick = {0},
ytick = {-1,0,8},
ymin = -0.2, ymax=8.2,
]
\addplot+[black, ycomb, domain=-5:8, samples=14,variable=k] {  (k>=0)*pow(2,k) };
\end{axis}

\begin{axis}[
xshift=7cm,
yshift=-2.5cm,
width=7cm,
height=2.5cm,
xlabel={$k$},
ylabel={$g(k)$},
xmin=-3.5,
xmax=10.5,
ytick = {0},
ytick = {-1,0,1},
ymin = -1.2, ymax=1.2,
]
\addplot+[black, ycomb, domain=-5:8, samples=14,variable=k] {  (k>=0)*pow(0.5,k)};
\end{axis}

\end{tikzpicture}
\end{frame}
\end{document}


\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{pst-plot}
\usepackage{graphicx}
\usepackage{endnotes}
\usepackage{graphics}
\usepackage{floatflt}
\usepackage{wrapfig}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{pdflscape}
\usepackage{wrapfig}
\usepackage{gensymb}

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0 0}}

\pdfpagewidth 210mm
\pdfpageheight 297mm
\setlength\topmargin{0mm}
\setlength\headheight{0mm}
\setlength\headsep{0mm}
\setlength\textheight{250mm}
\setlength\textwidth{159.2mm}
\setlength\oddsidemargin{0mm}
\setlength\evensidemargin{0mm}
\setlength\parindent{7mm}
\setlength\parskip{0mm}

\newenvironment{exercise}[3]{\paragraph{Exercise #1: #2 (#3pt)}\ \\}{
\medskip}
\newcommand{\question}[2]{\setlength\parindent{0mm}\ \\$\mathbf{Q_#1:}$ #2\ \\}

\author{\large{Ilya Kuzovkin, Raul Vicente}}
\title{\huge{Introduction to Computational Neuroscience}\\\LARGE{Practice III: Data Analysis - Spiking Data}}

\begin{document}
\maketitle

The last time we had a look at the continuous brain data: strength of the EEG signal was changing over the time. Another very popular form of data, mostly associated with the single neuron recordings, is \emph{spiking} data. The concept is very simple: we attach a sensor to the neuron and when the neuron fires we write "1" into our data file, otherwise we write "0". The resulting data shows us for each time point whether the neuron has fired or not.\\
\ \\
\textbf{Request}: Please record the time you will spend of this homework and add it to the report. This is just for me to balance the amount and the difficulty level of the exercises.\\
\ \\
\textbf{Dataset}

\begin{wrapfigure}{r}{0.3\textwidth}
	\centering
	\vspace{-12pt}
	\includegraphics[width=0.15\textwidth]{orientationstimulus.jpg}
	\caption{Example of the stimulus. During the actual experiment the bars are moving.}
	\label{fig:stimulusexample}
	\vspace{-5pt}
\end{wrapfigure}
The dataset we will be working with has the spiking data recorded while the mouse was presented with a stimulus: moving bar on the screen, which can have different orientations. On Figure \ref{fig:stimulusexample} you can see an example: white and black bars are tilted (\emph{orientation}) and they are moving. The spiking activity is recorded from the 72 neurons and we want to find is whether different neurons react differently on the stimuli with different orientation. Have a look at Figure \ref{fig:orientationresponse} -- spiking pattern of this particular neuron seems to indicate that this neuron is more active when the bar is tilted $45\degree$ clockwise from the horizontal position. And its activity fades when the bar changes the orientation. We would like to rediscover from the data, than indeed different neurons respond differently to different orientations.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.8\textwidth]{orientation.jpg}
	\caption{Neuronal response to different orientations of the bar.}
	\label{fig:orientationresponse}
\end{figure}
\textbf{NB:} Note that in the dataset we have 13 stimuli (13 orientations), but the first one should be ignored (it has orientation $-1\degree$). So you should use only the other 12.

\begin{exercise}{1}{Raster plot}{0.5}
Let us start by plotting some spiking data. Under the \texttt{data/lgn} folder you have recordings from 72 neurons of a mouse. \texttt{Matlab/Octave} users can use files in the \texttt{matlab} folder, the same data is available in the plain text format in the \texttt{plain} folder.

\texttt{Matlab} files have names \texttt{mlgnori\_NN.mat} where \texttt{NN} is the number of the neuron. When you load it to your environment you will find that each file has a structure with two elements: \texttt{spktimes} and \texttt{stim}. The first one is $M\times N \times T$ matrix where $M$ is the stimulus number, $N$ is the trial number at $T$ is the time in milliseconds. So this is a 3-dimensional matrix with values 0 (no spike) and 1 (spike). The second file describes the stimulus. Open it and you will see what information is in there.

If you do not use \texttt{Matlab}, then it is easier for you to use the plain text files. They have names \texttt{neuron\_NN} \texttt{\_stimulus\_SS.csv} where \texttt{NN} is the number of the neuron and \texttt{SS} is the number of the stimulus. Inside each file one line represents one trial. For each millisecond it has value 0 (no spike) or 1 (spike). Files with names \texttt{stimulus\_SS.csv} describe the stimulus: they hold four values:
\begin{enumerate}
\itemsep 0em
	\item Time before the stimulus (in seconds).
	\item Duration of the stimulus (in seconds).
	\item Time after the stimulus (in seconds).
	\item Orientation of the stimulus (in degrees).
\end{enumerate}
Your task is to take any of the neurons and plot all trials as a raster plot (see Figure \ref{fig:raster_plot}). You will notice that neuronal response to the stimulus varies a lot, this is why you usually need several trials.
\begin{figure}[H]
   \centering
   \includegraphics[width=0.7\textwidth]{raster10tr.png}
   \caption{Raster plot of 10 trials on the same neuron.}
   \label{fig:raster_plot}
\end{figure}
On the $X$ axis we have time and on the $Y$ axis trials. A vertical bar indicates a spike. To draw a vertical line in \texttt{Matlab} you can use
\begin{verbatim}
	line([t t], [tr tr+0.5], 'Color', 'k');
\end{verbatim}
where \texttt{t} is the time and \texttt{tr} is the number of the trial.
\end{exercise}

\begin{exercise}{2}{More raster plots}{0.5}
The next step is to create two more raster plots. The first one will illustrate the behaviour of all the neurons under the same stimulus, while the other one will have responses of the same neuron but to the different stimuli.

First we need to average spiking data so that each neuron would be represented by one vector. Simplest way to do that is just to add trials together. Let us say that you have 10 trials, and each of them is a vector of 4500 time points. You just sum those vectors together to obtain one vector of length 4500. After that replace all values which are grater than 1 with 1.

Create the following two plots:
\begin{enumerate}
	\item Raster plot, where on $X$ axis we have time and on $Y$ axis we have all 72 neurons. Verticals bars are the responses to the same stimulus (choose any). Please note that in our dataset different recordings have different length, but this should not be a problem.
	\item Raster plot, where on $Y$ axis we have all 13 stimuli and bars are the responses from the same neuron (choose any) to those stimuli.
\end{enumerate}
\end{exercise}

\begin{exercise}{3}{Post-Stimulus Time Histogram (PSTH)}{0.5}
Another useful analysis tool is a histogram, where on $X$ axis we have time points (or time ranges) and on $Y$ axis the number of spikes that occurred during given time range. It is called \emph{Post-Stimulus Time Histogram (PSTH)}.
\begin{enumerate}
	\item Choose any neuron, any stimulus.
	\item Take an average over all trials as we did before.
	\item Choose time window, for example 20ms or 50ms.
	\item Plot a histogram, where on the $X$ axis we have time windows and on the $Y$ axis the number of spikes that occurred during that window.
\end{enumerate}
\end{exercise}

\begin{exercise}{4}{Tuning Curve}{0.5}
Yet another way of visualization is the \emph{tuning curve}. It is similar to the previous one, but now instead of the time we will plot a stimulus on the $X$ axis and number of spikes on the $Y$ axis.
\begin{enumerate}
	\item Choose any 3 neurons.
	\item For each neuron:
	\begin{enumerate}
		\item For each stimulus:
		\begin{enumerate}
			\item Calculate number of spikes during each trial into separate array. For example if we have 20 trials then this array will consist of 20 numbers.
			\item Calculate the average number of spikes per trial (use \texttt{mean} function).
			\item Calculate the standard deviation of the number of spikes (use \texttt{std} function).
		\end{enumerate}
		\item You will have two arrays, both of length 12 (because we have 12 stimuli):
		\begin{enumerate}
			\item Average number of spikes for each stimulus.
			\item Standard deviation of number of spikes for each stimulus.
		\end{enumerate}
		\item Use function \texttt{errorbar(average\_numbers, standard\_deviations)} to plot the curve and standard deviation bars.
	\end{enumerate}
	\item The result should look something like the one you see on Figure \ref{fig:tuningcurve}.
\end{enumerate}

\begin{figure}[H]
   \centering
   \includegraphics[width=0.8\textwidth]{tuningcurve.png}
   \caption{Tuning curve with standard deviation bars.}
   \label{fig:tuningcurve}
\end{figure}

\end{exercise}

\begin{exercise}{5*}{Rose diagram}{0.5}
There is a really neat way to visualize the tuning curve. In fits especially well with the experiment we have at our hands. It is called \emph{rose chart} or \emph{angle histogram}. You can see an example on Figure \ref{fig:rosediag}. The idea is that the values are placed on the circumference on the circle and the length of the sector is determined by the number of times the value appears in the list. It is like a histogram drawn in circle.

In the most cases this is just a fancy way to draw a histogram, but in our case it allows to represent our data in much better way by placing orientations (stimuli) on the circe. For example on Figure \ref{fig:rosediag} we can clearly see that neuron 8 is reacts more to the orientation of $0\degree$, the neuron 6 is most active in the range of $270\degree$ to $330\degree$ and so on.

\begin{figure}[H]
   \centering
   \includegraphics[width=0.7\textwidth]{rosediag.png}
   \caption{Rose diagram: the number of spikes for each of 12 orientations.}
   \label{fig:rosediag}
\end{figure}

Your task is to produce similar plot for any 9 neurons. To produce a plot for one neuron do the following:
\begin{enumerate}
	\item Create an array \texttt{A} where for each spike you will store the orientation when the spike occurred.
	\item For each orientation (stimulus) do:
	\begin{enumerate}
		\item Count the number of times the neuron fired.
		\item Add to \texttt{A} current orientation as many times as the neuron fired.
	\end{enumerate}
	\item Draw the plot. In \texttt{Matlab} you can do:
	\begin{enumerate}
		\item Array with the angles of bins (needed for the \texttt{rose} function):\\ \texttt{angles = degtorad([0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330]);}
		\item Call \texttt{rose(deg2rad(A), angles)}
	\end{enumerate}
\end{enumerate}
\end{exercise}

\begin{exercise}{6}{Receptive field}{1}
In general the \emph{receptive field} of a neuron is a region of space in which the presence of a stimulus will alter the firing of that neuron. By now you have a set of tools to analyse our spiking data and estimate the receptive fields of the neurons in the dataset we have. In our case the receptive field is the orientation of the bar on the screen (each of the neurons has a set of orientations it reacts to). Your task is to find 5 neurons with the most specific receptive fields. You can use any of the tools we have seen during this session or any other methods you find useful.

In your report please describe what you did, your findings and the list of 5 neurons with the most specific orientations.
\end{exercise}

\begin{exercise}{7*}{What else our neurons are tuned to?}{0.5}
In this session we have seen an example of how neurons are ``tuned" to respond to very specific stimuli. You task is to find  other interesting examples of stimuli our neurons are tuned to react to. Are there special neurons, which fire when you look at a human face? Neurons which react on the temperature? Hunger? Numbers?...
\end{exercise}

\ \\
\ \\
\ \\
\ \\
\ \\
Please submit a \texttt{pdf} report with answers to the questions and comments about your solutions. Also submit a code for the programming exercise(s). Pack those into \texttt{zip} archive and upload to the course web page.

\end{document}

2014/Practices/Practice 3 - Data Analysis - Spike Data/text/exercises-3.tex


\chapter{Datatype-Generic Programming}

\todo[inline]{Compare paper \citetitle{van2008lightweight}}

The implementation in Chapter \ref{chap-spec-impl} is an efficient implementation for incrementally computing the summation over a \texttt{Tree} datatype. However, when we want to implement this functionality for a different datatype, a lot of code needs to be copied while the process remains the same. This results in poor maintainability, is error-prone and is in general boring work.

An example of reducing manual implementations for datatypes is the \textit{deriving} mechanism in Haskell. The built-in classes of Haskell, such as \texttt{Show, Ord, Read}, can be derived for a large class of datatypes. However, deriving is not supported for custom classes. Therefore, we use \textit{Datatype-Generic Programming}\cite*{gibbons2006datatype} to define functionality for a large class of datatypes.

In this chapter, we introduce Datatype-Generic Programming, also known as \textit{generic programming} or \textit{generics} in Haskell, as a technique that exploits the structure of datatypes to define functions by induction over the type structure. This prevents the need to write the previously defined functionality for every datatype.

\section{Introduction}
There are multiple generic programming libraries, however to demonstrate the workings of generic programming we will be using a single library as inspiration, named \texttt{regular}\cite*{regular2022}. Here the generic representation of a datatype is called a \textit{pattern functor}. A pattern functor is a stripped-down version of a data type, by only containing the constructor but not the recursive structure. The recursive structure is done explicitly by using a fixed-point operator.

First, the pattern functors defined in \texttt{regular} are 5 core pattern functors and 2 meta information pattern functors. The core pattern functors describe the datatypes. The meta information pattern functors only contain information (e.g., constructor name) but not any structural information.

\todo[inline]{Use "representation types" instead of "pattern functor" for these datatypes}

\begin{minted}{haskell}
data U r         = U                 -- Empty constructors
data I r         = I r               -- Recursive call
data K a r       = K a               -- Constants
data (f :+: g) r = L (f r) | R (g r) -- Sums (Choice)
data (f :*: g) r = (f r) :*: (g r)   -- Products (Combine)
\end{minted}

The conversion from regular datatypes into pattern functors is done by the \texttt{Regular} type class. The \texttt{Regular} type class has two functions. The \texttt{from} function converts the datatype into a pattern functor and the \texttt{to} function converts the pattern functor back into a datatype. In \texttt{regular}, the pattern functor is represented by a type family. Then using the \texttt{Regular} conversion to a pattern functor, we can write the \texttt{Tree} datatype from Chapter \ref{chap-spec-impl} as:

\begin{minted}{haskell}
type family PF a :: * -> *

class Regular a where
  from :: a -> PF a a
  to   :: PF a a -> a

type instance PF (Tree a) = K a                -- Leaf
                         :+: (I :*: K a :*: I) -- Node

class Regular (Tree a) where
  from (Leaf x)     = L (K x)
  from (Node l x r) = R (I l :*: K x :*: I r)

  to (L (K x))                 = Leaf x
  to (R (I l :*: K x :*: I r)) = Node l x r
\end{minted}

To demonstrate the workings of generic programming, we are going to implement a simple generic function which determines the length of an arbitrary datatype. First, we define the length function within a type class. The type class is used, to define how to determine the length for every pattern functor \texttt{f}.

\begin{minted}{haskell}
class GLength f where
  glength :: (a -> Int) -> f a -> Int
\end{minted}

Writing instances for the empty constructor \texttt{U} and the constants \texttt{K} is simple because both pattern functors return zero. The \texttt{U} pattern functor returns zero, because it does not contain any children. The \texttt{K} pattern functor returns zero, because we do not count constants for the length.

\begin{minted}{haskell}
instance GLength U where
  glength _ _ = 0

instance GLength (K a) where
  glength _ _ = 0
\end{minted}

The instances for sums and products pattern functors are quite similar. The sums pattern functor recurses into the specified choice. The product pattern functor recurses in both constructors and combines them.

\begin{minted}{haskell}
instance (GLength f, GLength g) => GLength (f :+: g) where
  glength f (L x) = glength f x
  glength f (R x) = glength f x

instance (GLength f, GLength g) => GLength (f :*: g) where
  glength f (x :*: y) = glength f x + glength f y
\end{minted}

The instance for the recursive call \texttt{I} needs an additional argument. Because, we do not know the type of \texttt{x}, so an additional function (\inlinehaskell{f :: a -> Int}) needs to be given which converts \texttt{x} into the length for that type.

\begin{minted}{haskell}
instance GLength I where
  glength f (I x) = f x
\end{minted}

Then using the \texttt{GLength} instances for all pattern functors, a function can be defined using the generic length function. By first, converting the datatype into a generic representation, then calling \texttt{glength} given recursively itself, and for every recursive call increase the length by one.

\begin{minted}{haskell}
length :: (Regular a, GLength (PF a)) => a -> Int
length = 1 + glength length (from x)
\end{minted}

\begin{minted}{haskell}
> length [1, 2, 3]
    3
> length (Node (Leaf 1) 2 (Leaf 3))
    3
> length {"1": 1, "2": 2, "3": 3}
    3
\end{minted}

\input{sections/generic_programming/explicit_recursion.tex}

\input{sections/generic_programming/sums_of_products.tex}

\input{sections/generic_programming/mult_rec_datatypes.tex}

% \input{sections/generic_programming/comp_gen_lib.tex}


\documentclass[english]{../thermomemo/thermomemo}
\usepackage[utf8]{inputenc}

\title{Volume shift for generic EOS}
\author{Morten Hammer}

\usepackage[normalem]{ulem}

\usepackage{hyperref}
\usepackage{color}

\definecolor{midnightblue}{RGB}{35,35,132}
\definecolor{urlblue}{RGB}{70,130,180}

\definecolor{shadecolor}{gray}{0.9}

\hypersetup{
    colorlinks=true,
    linkcolor=midnightblue,
    urlcolor=urlblue,
    citecolor=midnightblue,
    linktoc=page
}

\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{framed}
\usepackage{siunitx,mhchem,todonotes}
\usepackage{xspace}
\newcommand{\pone}[3]{\frac{\partial #1}{\partial #2}\bigg|_{#3}}% partial
                                % derivative with information of
                                % constant variables
\newcommand*{\vektor}[1]{\boldsymbol{#1}}%
\newcommand{\dd}[1]{\mathrm{d}{#1}}
\newcommand{\eos}{\ensuremath{\text{eos}}\xspace}

\DeclareMathOperator*{\argmin}{arg\,min }

\usepackage[activate={true,nocompatibility},final,kerning=true,tracking=true,spacing=true,stretch=10,shrink=10]{microtype}
\microtypecontext{spacing=nonfrench}
\SetExtraKerning[unit=space]
    {encoding={*}, family={bch}, series={*}, size={footnotesize,small,normalsize}}
    {\textendash={400,400}, % en-dash, add more space around it
     "28={ ,150}, % left bracket, add space from right
     "29={150, }, % right bracket, add space from left
     \textquotedblleft={ ,150}, % left quotation mark, space from right
     \textquotedblright={150, }} % right quotation mark, space from left
\SetTracking{encoding={*}, shape=sc}{0}

\begin{document}
\frontmatter

%\tableofcontents

\section{Introduction}
The volume shift was introduced by P{\'e}neloux et al. \cite{Peneloux1982},
\begin{equation}
  c = \frac{1}{n}\underset{i}{\sum}c_i n_i,
\label{eq:volumeshift}
\end{equation}
where $c_i$ is a component constant representing the component volume
shift.

Different properties change when working with volume translations, see
Jaubert et al. \cite{Jaubert2016} for details.

The volume-shift have found application in many cubic based equations
of state (t-mPR\cite{Kordas1995}, PSRK\cite{Fischer1996},
VTPR\cite{Collinet2006}, tc-PR/tc-RK\cite{LeGuennec2016}, \dots), and
the component volume translations $c_i$, are often fixated to match the
liquid density at $T=0.7T_{\text{Crit}}$,
\section{Volume shifts for generic EOS}

The residual reduced Helmholtz function of a  generic EOS is found as follows,
\begin{align}
  F(T,V_{\eos},\vektor{n}) = \frac{A^\text{r}(T,V_{\eos},\vektor{n})}{RT}
  = \int^\infty_{V_{\eos}} \left[ \frac{P(T,V_{\eos}^\prime,\vektor{n})}{RT} - \frac{n}{V_{\eos}^\prime} \right]\dd{V_{\eos}^\prime}
  \label{eq:helmholtz_int_eos}
\end{align}
Introducing the volume shift,
\begin{equation}
V = V_{\eos}- \sum n_ic_i = V_{\eos}- C,
\label{eq:v_shift}
\end{equation}
The residual reduced helmholtz of the volume-shifted (vs) EOS can be
found, using $dV = dV_{\eos}$ at constant $n$ and $T$,
\begin{align}
  F^{\text{vs}}(T,V,\vektor{n})
  &= \int^\infty_V \left[ \frac{P(T,V^\prime+C,\vektor{n})}{RT} - \frac{n}{V^\prime} \right]\dd{V^\prime} \\ &= \int^\infty_V \left[ \frac{P(T,V^\prime+C,\vektor{n})}{RT} - \frac{n}{V^\prime + C} \right]\dd{V^\prime} + n\int^\infty_V \left[\frac{1}{V^\prime + C}  - \frac{1}{V^\prime} \right]\dd{V^\prime}\\ &= \int^\infty_{V_{\eos}} \left[ \frac{P(T,V_{\eos}^\prime,\vektor{n})}{RT} - \frac{n}{V_{\eos}^\prime} \right]\dd{V_{\eos}^\prime} + n\int^\infty_V \left[\frac{1}{V^\prime + C}  - \frac{1}{V^\prime} \right]\dd{V^\prime}\\ &= F(T,V_{\eos},\vektor{n})  + n \ln \left(\frac{V}{V_{\eos}} \right)
  \label{eq:helmholtz_int}
\end{align}
Here we need to treat $V_{\eos} = V_{\eos}(V,\vektor{n})$ with the
chain rule when differentiating $F^{\text{vs}}$.

If we introduce $F^C$ as the corrected residual reduced Helmholtz energy, due to
the difference in ideal volume,
\begin{align}
  F^C(V,\vektor{n}) &= n \ln \left(\frac{V}{V+C} \right),
  \label{eq:F_corr}
\end{align}
the differentials can be derived in a organized manner.
\begin{align}
  F^C_{V} &= n \left(\frac{1}{V} - \frac{1}{V+C} \right) = n \left(\frac{1}{V} - \frac{1}{V_{\eos}} \right), \\
  F^C_{VV} &= n \left(-\frac{1}{V^2} + \frac{1}{\left(V+C\right)^2} \right) = n \left(-\frac{1}{V^2} + \frac{1}{V_{\eos}^2} \right), \\
  F^C_{i} &= \ln \left(\frac{V}{V+C} \right) - \frac{nc_i}{V+C} = \ln \left(\frac{V}{V_{\eos}} \right) - \frac{nc_i}{V_{\eos}}, \\
  F^C_{ij} &= -\frac{\left(c_j + c_i \right)}{V+C} +  \frac{n c_ic_j}{\left(V+C\right)^2} = -\frac{\left(c_j + c_i \right)}{V_{\eos}} +  \frac{n c_ic_j}{V_{\eos}^2}, \\
  F^C_{Vi} &= \frac{1}{V} - \frac{1}{V+C} + \frac{nc_i}{\left(V+C\right)^2} =  \frac{1}{V} - \frac{1}{V_{\eos}} + \frac{nc_i}{V_{\eos}^2}
\end{align}
In addition the compositional differentials change since $V_{\eos} = V + C$,
\begin{align}
  F^{\eos}_{i} &= F^{\eos}_{i} + F^{\eos}_{V_{\eos}} c_i , \label{eq:Fi}\\
  F^{\eos}_{Ti} &= F^{\eos}_{Ti} + F^{\eos}_{TV_{\eos}} c_i , \\
  F^{\eos}_{ij} &= F^{\eos}_{ij} + F^{\eos}_{iV_{\eos}} c_j + F^{\eos}_{V_{\eos}j} c_i + F^{\eos}_{V_{\eos}V_{\eos}} c_ic_j . \label{eq:Fij}
\end{align}

\subsection{Test of the fugacity coefficient}
Let us test this for the fugacity coefficient. It is defined as
\begin{align}
  \ln \hat{\varphi}_i^{\text{vs}} = \biggl( \frac{\partial F^{\text{vs}}}{\partial n_i} \biggr)_{T,V,n_j} - \ln \left( Z \right) = F_{n_i}^{\text{vs}} - \ln \left( Z \right)
  \label{eq:fugacity}
\end{align}
Differentiating $F^{\text{vs}}$,
\begin{align}
  F_{n_i}^{\text{vs}} &= F_{n_i} + F_{V_{\eos}}c_i  + \ln \left(\frac{V}{V_{\eos}} \right) - \frac{n c_i}{V_{\eos}} = F_{n_i} + \ln \left(\frac{V}{V_{\eos}} \right) - \frac{P c_i}{RT}
  \label{eq:Fni}
\end{align}
Combining Equation \ref{eq:fugacity} and \ref{eq:Fni}, we get
\begin{align}
  \ln \hat{\varphi}_i^{\text{vs}} &= F_{n_i} + \ln \left(\frac{V}{V_{\eos}} \right) - \frac{P c_i}{RT} - \ln \left( \frac{PV}{n RT} \right) \\
                      &= F_{n_i} - \ln \left( \frac{PV_{\eos}}{n RT} \right) - \frac{P c_i}{RT} \\
  &= \ln \hat{\varphi}_i - \frac{P c_i}{RT}
  \label{eq:fugacity2}
\end{align}
which is the same result as reported by P{\'e}neloux et al.

\section{Correlations used for $c_i$}
The $c_i$ for the SRK EOS is calculated from the following equation:
\begin{equation}
  c_i = 0.40768\frac{R T_{c_i}}{P_{c_i}}\left(0.29441- Z_{\text{RA}}\right)
\label{eq:ci}
\end{equation}

$Z_{\text{RA}}$ are tabulated in TPlib. Reid et al. \cite{Reid1987}
also correlate $Z_{\text{RA}}$ as follows:
\begin{equation}
  Z_{\text{RA}} = 0.29056 - 0.08775 \omega
\label{eq:zra}
\end{equation}

Jhaveri and Youngren \cite{Jhaveri1988} have developed different paramaters for the PR EOS:
\begin{equation}
  c_i^{\text{PR}} = 0.50033\frac{R T_{c_i}}{P_{c_i}}\left(0.25969- Z_{\text{RA}}\right)
\label{eq:ci_PR}
\end{equation}

\section{Temperature dependent volume shift}
Temperature dependent volume translation are known to give supercritical
iso-therm crossings \cite{Pfohl1999} and possibly un-physical
behaviour \cite{Kalikhman2010} and must be executed with care. In
some cases it can be used as a simple remedy to improve liquid density
predictions.

In this case the $F^C$ function becomes,
\begin{align}
  F^C(V,\vektor{n},T) &= n \ln \left(\frac{V}{V+C\left(\vektor{n},T\right)} \right),
  \label{eq:F_corr_t}
\end{align}
and the temperature differentials become,
\begin{align}
  F^C_{T} &= -\frac{n C_T}{V+C} = -\frac{n C_T}{V_\eos}, \\
  F^C_{TT} &= -\frac{n C_{TT}}{V+C} + \frac{n C_T^2}{\left( V+C \right)^2} = -\frac{n C_{TT}}{V_\eos} + \frac{n C_T^2}{V_\eos^2}, \\
  F^C_{VT} &= \frac{n C_T}{\left( V+C \right)^2} = \frac{n C_T}{V_\eos^2}, \\
  F^C_{iT} &= -\frac{C_T}{V+C} - \frac{n c_{iT}}{V+C} + \frac{n C_T c_i}{\left( V+C \right)^2} = -\frac{\left(C_T + n c_{iT}\right)}{V_\eos}  + \frac{n C_T c_i}{V_\eos^2}.
\end{align}
In addition the compositional and temperature differentials change
since $V_{\eos} = V + C\left(\vektor{n},T\right)$,
\begin{align}
  F^{\eos}_{T} &= F^{\eos}_{T} + F^{\eos}_{V_{\eos}} C_T , \\
  F^{\eos}_{TT} &= F^{\eos}_{TT} + 2 F^{\eos}_{TV_{\eos}} C_T + F^{\eos}_{V_{\eos}V_{\eos}} C_T^2 + F^{\eos}_{V_{\eos}} C_{TT} , \\
  F^{\eos}_{VT} &= F^{\eos}_{VT} + F^{\eos}_{V_{\eos}V_{\eos}} C_T , \\
  F^{\eos}_{Ti} &= F^{\eos}_{Ti} + F^{\eos}_{TV_{\eos}} c_i + F^{\eos}_{V_{\eos}V_{\eos}} C_T c_i + F^{\eos}_{V_{\eos}i} C_T + F^{\eos}_{V_{\eos}} c_{T,i}.
\end{align}
While $F^{\eos}_{i}$ and $F^{\eos}_{ij}$ are unchanged from
\eqref{eq:Fi} and \eqref{eq:Fij} respectively.
\clearpage
\bibliographystyle{plain}
\bibliography{../thermopack}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\section{Link/activation functions: Regression}


\section{Experiments}
Since much of the project was spent on implementation there was little time
left for experimentation and testing. We were able to test a few different
options and test multiple games. There were many more runs than are included
in here but not all were recorded as we were testing that different parameters
worked and were were focusing on creating good videos for the presentation.

It is key to note that the default values of hyper parameters are:
discount=0.8, frame skips=4, learning rate=0.8, depth=1, explore=0,
random action=0.8. The discount is $\gamma$ in Equation~\ref{eq:Bell}, similarly
learning rate is $\alpha$ in the same equation. Frame skips are the number of
frames that the game skips when emulating, this helps reduce the number of
states and can help learning if adjacent states are extremely similar. Depth
is the number of lookaheads that we perform (amount of recursion in Bellman's
Equation). Explore is the k value in the exploration function, Equation
\ref{eq:explore}.

Figure~\ref{fig:AGLearn} shows the results for Airstriker-Genesis, the
default game for Retro. To perform this experiment a timer feature was
added to the program to denote the time when the new best reward was obtained.
These programs were run for the same amount of time.
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.9\textwidth]{AGLearners.png}
    \caption{Comparison of different hyper-parameters on Airstriker-Genesis}
    \label{fig:AGLearn}
\end{figure}
While this graph shows the brute forcing (greedy) algorithm greatly out
performing our Q-Learner this was not always observed in practice. With
limited time it is difficult to make accurate conclusions and properly tune
hyper-parameters. Experimentation found that performance on specific games
were extremely dependent upon the hyper-parameters. The Q-Learner has a tendency
to get trapped in local optima and unfortunately without good parameters
the learner does not perform as well. Seeing how many of the parameters
do not extend along the x-axis it appears that these learners got trapped in
local optima. Observing this we can see how important the exploration function
is to help the learner escape these.

\FloatBarrier
The next thing to look at is the harder game, Super Mario Bros (SMB). Figure
\ref{fig:SMBLearn} shows different hyper-parameters configured to play
SMB. All of these were again run for the same amount of time. I changed the
graph slightly because everything got stuck in a local optima except for the
parameter with $\gamma=0.1$ depth$=50$ and exploration$=1$ (depth$=100$ likely
would have updated again). These values would likely have continued updating
given more time but given the constraints I repeated their final scores with
a time of 5000 seconds so that it was easier to visually see what happened.
There was no manipulation of data as these were the values at 5000 seconds.
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.9\textwidth]{SMBLearners.png}
    \caption{Comparison of different hyper-parameters on Super Mario Bros}
    \label{fig:SMBLearn}
\end{figure}
Here we can see that every Q-Learner performed much better than the brute forcer
algorithm.

\FloatBarrier
We found that when playing a PSPACE-Hard game like DonkeyKong Country
that our Q-Learner more easily beat the brute forcing algorithm. This can
be seen in Figure~\ref{fig:DKCLearn}. DonkeyKong Country is specifically
a difficult game, as there are substantially more states and actions than
Airstriker-Genesis. There are also two characters that can be played and
swapped out, that each have different attributes. One of the sections that the
learner consistently got stuck on was when it was using the worse agent at
a particular section.
The learner that ran with a depth of 100 (depth of lookahead) was not run as
long as the other brute and default. There were constraints of how many
learners could be run at a single time on our system. Additionally
depth 100 explore 1 was run at a different time and showed more steady growth.
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.9\textwidth]{DKCLearners.png}
    \caption{Comparison of different hyper-parameters on DonkeyKong Country}
    \label{fig:DKCLearn}
\end{figure}

From these experiments we can make some conclusions about how Q-Learning works
in different games. It seems that when the game is sufficiently complex that
it performs much better than a greedy algorithm. It seems that we may have just
lucked out with the default values. Additionally I had run some examples over
the weekend but did not record the times associated with each step, so they
are not included in this analysis. In those examples I had found that a high
depth and exploration greatly helped the learners escape local optima.
Similarly there were experiments run with frame skipping. It was found that
games like DKC and SMB performed better with a slightly higher frame skip, moreso
DKC than SMB. We hypothesise that this is because in these games there is more
similarity between frames, or states. Specifically DKC performed better with
this because it was not reaching enemies, which is where reaction time matters
more. It is also believed that given more resources and time that these
algorithms would actually be able to do significantly better. For comparison
similar experiments by OpenAI, DeepMind, and others use hundreds of GPUs,
whereas currently these algorithms are single-threaded and run on CPUs.
Those systems also ran for hundreds or thousands of compute hours, where
our maximal value here is just under 3 hours.
There is clearly a lot of optimization that can be done that would greatly
help improve these programs.


\documentclass[american]{article}

\usepackage[margin=0.5in,paperwidth=5.8in,paperheight=8.8in]{geometry}

\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{mathtools,amsfonts}
\usepackage{pgfplots}
\usepackage{hyperref}
\usepackage[style=numeric]{biblatex}
\addbibresource{refs.bib}

\usepackage{bm}

\usepackage[draft,layout={inline,index}]{fixme}
\fxsetup{theme=colorsig}
\FXRegisterAuthor{jek}{ajek}{JEK}

% This must be the last package loaded
\usepackage[noabbrev,sort&compress]{cleveref}

\newcommand{\pnt}[1] {\ensuremath{\bm{#1}}}
\renewcommand{\vec}[1] {\ensuremath{\bm{#1}}}
\newcommand{\mat}[1] {\ensuremath{#1}}
\newcommand{\ten}[1] {\ensuremath{\bm{\mathcal{#1}}}}
\renewcommand{\Re}{{\mathbb{R}}}
\newcommand{\mnd}[1] {\ensuremath{\hat{#1}}}

\newcommand{\der}  [2]{\frac{d#1}{d#2}}
\newcommand{\pder} [2]{\frac{\partial#1}{\partial#2}}
\newcommand{\ppder}[2]{\frac{\partial^2#1}{{\partial#2}^2}}
\newcommand{\pdder}[3]{\frac{\partial^2#1}{\partial#2\partial#3}}
\newcommand{\relmiddle}[1]{\mathrel{}\middle#1\mathrel{}}

\author{Jeremy Kozdon}
\title{Spectrum notes}

\begin{document}

\maketitle

\section{Model}
Consider the model problem
\begin{subequations}
  \begin{alignat}{2}
    &\pder{u}{t} = \pder{h}{x}, &~~& 0 < x < 1, ~ 0 < t,\\
    &\pder{h}{t} = \pder{u}{x}, &~~& 0 < x < 1, ~ 0 < t,\\
    &u(0, t) = 0, &~~& 0 < t,\\
    &u(1, t) = 0, &~~& 0 < t,
  \end{alignat}
\end{subequations}

There is only one zero speed mode this problem,
\begin{equation}
  \begin{split}
    u(x, t) = 0,\\
    h(x, t) = c,\\
  \end{split}
\end{equation}
where $c$ is an arbitrary constant.

Throughout we assume that $\Omega = (0, 1)$ is partitioned into $K$ elements of
size $\Delta x = 1 / K$. The edges of the elements are $x_{i} = ih$ for $i = 0,
1, \dots, K$.

\section{CG-SEM with strong enforcement of boundary conditions}
We define the spaces
\begin{equation}
  \begin{split}
    V^{N} &= \{
      v: [0, 1] \rightarrow \mathbb{R}:
      v \in \mathcal{C}^{(0)}([0, 1]),
      v|_{x_{i}, x_{i+1}} \in \mathbb{P}^{N}
    \},\\
    \bar{V}^{N} &= \{
      v: [0, 1] \rightarrow \mathbb{R}:
      v \in V,
      v(0) = v(1) = 0
    \}.
    \end{split}
\end{equation}
For the CG solution with strong enforcement of boundary conditions, we seek a
$\bar{u} \in \bar{V}$ and $h \in V$ such that for all $\bar{\phi} \in \bar{V}$
and $\psi \in V$:
\begin{equation}
  \begin{split}
    \int_{0}^{1} \bar{\phi} \pder{\bar{u}}{t} = \int_{0}^{1} \bar{\phi} \pder{h}{x},\\
    \int_{0}^{1} \psi \pder{h}{t} = -\int_{0}^{1} \pder{\psi}{x} \bar{u}.
  \end{split}
\end{equation}
Letting $\mat{W}$ denote the diagonal matrix of LGL quadrature weights on the
reference element, $\mat{Q}$ the scatter matrix which includes the boundary
points, $\mat{\bar{Q}}$ the scatter matrix which excludes the boundary points,
$\mat{D}$ the reference element derivative matrix, and $J = \Delta x / 2$ the
Jacobian we have the discrete scheme:
\begin{equation}
  \begin{split}
    J \mat{\bar{Q}}^{T} (\mat{I} \otimes \mat{W}) \mat{\bar{Q}} \der{\vec{\bar{u}}}{t}
    &=
    \mat{\bar{Q}}^{T} (\mat{I} \otimes \mat{W} \mat{D}) \mat{Q} \vec{h},\\
    J \mat{Q}^{T} (\mat{I} \otimes \mat{W}) \mat{Q} \der{\vec{h}}{t}
    &=
    -\mat{Q}^{T} (\mat{I} \otimes \mat{D}^{T} \mat{W}) \mat{\bar{Q}} \vec{\bar{u}}.
  \end{split}
\end{equation}
This scheme has two zero speed modes.

\section{CG-SEM with weak enforcement of boundary conditions}
For the CG solution with weak enforcement of boundary conditions, we seek a
$u \in V$ and $h \in V$ such that for all $\phi \in V$ and $\psi \in V$:
\begin{equation}
  \begin{split}
    \int_{0}^{1} \phi \pder{u}{t} = \int_{0}^{1} \phi \pder{h}{x}
    - \alpha {\left.\left(\phi u\right)\right|}_{x = 0}
    - \alpha {\left.\left(\phi u\right)\right|}_{x = 1},\\
    \int_{0}^{1} \psi \pder{h}{t} = -\int_{0}^{1} \pder{\psi}{x},
  \end{split}
\end{equation}
where $\alpha \ge 0$.

Letting $\vec{l}_{0}^{T}$ and $\vec{l}_{1}^{T}$ be vectors that select the end
points we have the discrete form
\begin{equation}
  \begin{split}
    J \mat{Q}^{T} (\mat{I} \otimes \mat{W}) \mat{Q} \der{\vec{u}}{t}
    &=
    \mat{Q}^{T} (\mat{I} \otimes \mat{W} \mat{D}) \mat{Q} \vec{h}
    -\vec{l}_{0} \vec{l}_{0}^{T} \vec{u}
    -\vec{l}_{1} \vec{l}_{1}^{T} \vec{u},\\
    J \mat{Q}^{T} (\mat{I} \otimes \mat{W}) \mat{Q} \der{\vec{h}}{t}
    &=
    -\mat{Q}^{T} (\mat{I} \otimes \mat{D}^{T} \mat{W}) \mat{Q} \vec{u}.
  \end{split}
\end{equation}
for $\alpha = 0$ this has two zero speed modes, and for $\alpha > 0$ it has one
zero speed mode.

\printbibliography{}

\end{document}


\section{Generating learners with boosting}


\documentclass[12pt]{cdblatex}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% ============================================================================================
\section*{Example 9 The Gauss equation}

\CdbSetup{action=hide}

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices(position=independent).

   \nabla{#}::Derivative.

   K_{a b}::Symmetric.
   g^{a}_{b}::KroneckerDelta.

   def check (objA,objB):

       tmp := @(objA) - @(objB).

       distribute     (tmp)
       sort_product   (tmp)
       rename_dummies (tmp)
       canonicalise   (tmp)

       return tmp

   import cdblib

   # ---------------------------------------------
   check000A = cdblib.get ('check000','expected/example-09.json')
   check001A = cdblib.get ('check001','expected/example-09.json')

   # ---------------------------------------------
   check000B = cdblib.get ('check000','output/example-09.json')
   check001B = cdblib.get ('check001','output/example-09.json')

   # ---------------------------------------------
   diff000 = check (check000A,check000B)   # cdb (ex-09.diff000,diff000)
   diff001 = check (check001A,check001B)   # cdb (ex-09.diff001,diff001)

\end{cadabra}

% \clearpage

\begin{dgroup*}
   \Dmath*{\cdb*{ex-09.diff000}}
   \Dmath*{\cdb*{ex-09.diff001}}
\end{dgroup*}

\end{document}


\documentclass[dvisvgm,hypertex,aspectratio=169]{beamer}
\usefonttheme{serif}

\beamertemplatenavigationsymbolsempty

%\usepackage[utf8]{inputenc}
%\usepackage[T1]{fontenc}

%\usepackage[draft]{animate}
\usepackage[final]{animate}
\usepackage{ifthen}

%\usepackage{pythontex} % <--
\usepackage{graphicx}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usepackage{amsmath}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define footer
\usepackage{ccicons}

\makeatletter
\setbeamertemplate{footline}
{
  \leavevmode%
  \hbox{%
  %\begin{beamercolorbox}[wd=.333333\paperwidth,ht=2.25ex,dp=1ex,center]{title in head/foot}%
    %\usebeamerfont{title in head/foot}\insertsubsection
  %\end{beamercolorbox}%
  %\begin{beamercolorbox}[wd=.333333\paperwidth,ht=2.25ex,dp=1ex,right]{date in head/foot}%
  %  \usebeamerfont{date in head/foot}\insertshortdate{}\hspace*{2em}
  %  \insertframenumber{} / \inserttotalframenumber\hspace*{2ex}
  %\end{beamercolorbox}}%
  %\vskip0pt%
  \begin{beamercolorbox}[wd=.92\paperwidth,ht=2.25ex,dp=1ex,right]{author in head/foot}%
    \usebeamerfont{author in head/foot}\insertauthor
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.08\paperwidth,ht=2.25ex,dp=1ex,right]{date in head/foot}%
    \ccbysa
  \end{beamercolorbox}}%
  \vskip0pt%
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\author{\href{mailto:kjartan@tec.mx}{kjartan@tec.mx}}

\begin{document}

\section{Create animation}

\IfFileExists{./pendulum.dta}{%
\pgfplotstableread[col sep=comma]{pendulum.dta}{\qtable}
\begin{frame}[label=Q]{Pendulum phase plane}
  \begin{center}
    \begin{animateinline}[autoplay, loop,]{20}
      \multiframe{120}{n=0+1}{
        \pgfplotstablegetelem{\n}{1}\of\qtable
        \pgfmathsetmacro{\xx}{\pgfplotsretval}
        \pgfplotstablegetelem{\n}{2}\of\qtable
        \pgfmathsetmacro{\xdot}{\pgfplotsretval}
        \begin{tikzpicture}[scale=1.4, transform shape]
          %\clip (-5, -2) rectangle (6.5, 11);
          \begin{axis}[%
            width=5cm,
            height=5cm,
            axis lines=middle,
            xmin=-2, xmax=2,
            ymin=-4, ymax=4,
            xtick=\empty,
            ytick=\empty,
            xlabel={$\theta$},
            ylabel={$\dot{\theta}$},
            ]
          \addplot[blue!80!black, smooth] table[x index=1, y index=2] {\qtable};
          \node[circle, orange!80, fill, draw, inner sep=1pt] at (axis cs: \xx, \xdot) {};
        \end{axis}
      \end{tikzpicture}
    }
  \end{animateinline}
\end{center}
\end{frame}
}{}
\end{document}


This chapter verifies Moltres' ability to reproduce key neutronics parameters
using group constant data from Serpent 2, which is essential for accurate
neutronics calculations in the subsequent multiphysics simulations. The model
under study is a static model of the \gls{MSFR}, i.e. no salt flow, and
uniform temperature distribution to assess the accuracy of the
six-group neutron diffusion model in Moltres on a fast-spectrum reactor. Table
\ref{table:static} lists relevant details of the static \gls{MSFR} model
in Moltres. This
verification exercise builds on the previous study by Lindsay et al.
\cite{lindsay_introduction_2018} that verified Moltres' neutronics
capabilities with a two-group neutron diffusion model of the \gls{MSRE}.

\begin{table}[htbp!]
    \small
	\caption{Details of the static \gls{MSFR} model in Moltres.}
	\centering
	\begin{tabular}{ l c}
		\toprule
		Detail & Mathematical description \\
        \midrule
        No salt flow (static salt) & $v_{salt} = 0$ m$\cdot$s$^{-1}$ \\
        Uniform temperature of 973 K throughout the 2D core model & $T = 973$
        K \\
        Six neutron energy groups & $G = 6$ \\
        Eight delayed neutron precursor groups & $I = 8$ \\
        Vacuum boundary conditions for neutron flux &
        $\frac{d \phi}{dx}\big|_{\text{inflow}} = 0$ m$^{-2}\cdot$s$^{-1}$ \\
		\bottomrule
	\end{tabular}
	\label{table:static}
\end{table}

\section{Effective Multiplication Factor and Delayed Neutron Fraction}

Moltres solves the six-group neutron diffusion equations (Equation
\ref{eq:neut}) as a
steady-state eigenvalue problem to find the $k_{\text{eff}}$ for the static
\gls{MSFR} model. Table
\ref{table:keff} shows the $k_{\text{eff}}$ values from Serpent 2 and Moltres
at 973 K and the corresponding salt density, and Table \ref{table:ktemp} shows
the $k_{\text{eff}}$ values for other temperatures at 100 K intervals. Two
main factors contribute to the small discrepancies on the order of 100 pcm
between the two applications: the accuracy of the neutron diffusion
model, and the omission of the blanket tank structural material in Moltres.
The neutron diffusion model is not as accurate as the other S$_{\text{N}}$ or
SP$_{\text{N}}$ deterministic methods nor the Monte Carlo approach in Serpent.
Regarding the omission of the blanket tank material, this model
replaces the 2 cm-thick structural material with blanket salt. This
replacement is partly responsible for the higher $k_{\text{eff}}$ value
calculated by Moltres as fissions occur in the blanket salt. Nevertheless, the
discrepancy is smaller
than the 228.5 pcm and 256.7 pcm discrepancies reported by Cervi et al.
\cite{cervi_development_2019} for their six-group $SP_3$ and neutron
diffusion methods, respectively.

\begin{table}[htb!]
    \small
	\centering
	\caption{$k_{\text{eff}}$ values from Serpent 2 and Moltres at 973 K.}
	\begin{tabular}{l S c}
		\toprule
		{Code} & {$k_{\text{eff}}$} \\
		\midrule
		{Serpent 2} & 1.00662 \pm 0.00005 \\
		{Moltres with \glspl{DNP}} & 1.0079400 \pm 0.0000010 \\
		{Moltres without \glspl{DNP}} & 1.0049197 \pm 0.0000010 \\
		\bottomrule
	\end{tabular}
	\label{table:keff}
\end{table}
%
\begin{table}[htb!]
    \small
	\centering
	\caption{$k_{\text{eff}}$ values from Serpent 2 and Moltres at various
	temperatures from 800 K to 1400 K.}
	\begin{tabular}{S S S S}
		\toprule
		{Temperature [K]} & {$k_{\text{eff}}$ $\pm$ $\sigma$
		(Serpent 2)} & {$k_{\text{eff}}$ (Moltres)} & {Difference wrt Serpent
		2 [pcm]}
		\\
		\midrule
		800  & 1.01996 \pm 0.00005 & 1.02117 & 121 \\
		900  & 1.01172 \pm 0.00005 & 1.01322 & 150 \\
		1000 & 1.00428 \pm 0.00005 & 1.00544 & 116 \\
		1100 & 0.99735 \pm 0.00005 & 0.99859 & 124 \\
		1200 & 0.99006 \pm 0.00005 & 0.99119 & 113 \\
		1300 & 0.98356 \pm 0.00005 & 0.98439 &  83 \\
		1400 & 0.97702 \pm 0.00005 & 0.97820 & 118 \\
		\bottomrule
	\end{tabular}
	\label{table:ktemp}
\end{table}

The absolute value of $k_{\text{eff}}$ impacts the final steady-state
temperature of the reactor. We can raise or lower the average core temperature
at steady state to meet the design specifications for the inlet and outlet
temperatures by adjusting the fissile inventory. On the other hand, the
delayed neutron fraction, $\beta$, and reactivity coefficients, $\alpha$, are
clearer indicators of transient reactor behavior in an accident transient.
$\beta$ primarily affects the magnitude of the initial change in power and the
time delay towards the new equilibrium power, while $\alpha$ affects the
magnitude of the change in reactor power and temperature.

This work compares the $\beta$ value from Moltres to the
$\beta_{\text{eff}}$ value from Serpent because Moltres currently lacks an
adjoint calculation capability.
The difference between $\beta$ and $\beta_{\text{eff}}$ is
that $\beta$ is the unweighted delayed neutron fraction
while $\beta_{\text{eff}}$ is the delayed neutron fraction weighted by the
adjoint neutron flux. This work calculates $\beta$ by
taking the relative difference between the $k_{\text{eff}}$ values with and
without \glspl{DNP} in Table \ref{table:keff}. The $\beta$ and
$\beta_{\text{eff}}$ values at 973 K, shown in Table \ref{table:betaeff}, are
in good agreement with a 4.43 pcm discrepancy.

\begin{table}[htb!]
	\centering
	\caption{$\beta_{\text{eff}}$ and $\beta$ values from Serpent 2 and
	Moltres, respectively, at 973 K.}
	\begin{tabular}{l S S}
		\toprule
		{Code} & {$\beta_{\text{eff}}$ [pcm]} & {Difference wrt Serpent [pcm]}
		\\
		\midrule
		{Serpent} & 304.08 \pm .81 & {-}\\
		{Moltres} & 299.65 \pm .20 & 4.43\\
		\bottomrule
	\end{tabular}
	\label{table:betaeff}
\end{table}

\section{Reactivity Feedback Coefficients}

Temperature reactivity feedback arises mainly from Doppler broadening of
resonance absorption peaks and thermal expansion. The current work reports
the reactivity, $\rho$, values for temperatures from 800 K to 1400 K at 100 K
intervals (Figure \ref{fig:reactivity}). The slopes represent the total,
Doppler, density $\alpha$ values. The temperature range
extends below the melting point of the fuel salt (841 K) to ensure that
the data cover the relevant range between 841 K and 900 K. Table
\ref{table:alpha} shows the various $\alpha$ values calculated using the
linear least squares approach. The total temperature
coefficients from Serpent and Moltres show excellent agreement with a
discrepancy of 0.019 pcm K$^{-1}$.

\begin{figure}[htb!]
    \centering
    \includegraphics[width=.8\textwidth]{reactivity}
    \caption{Reactivity values from Serpent and Moltres. The Doppler
    reactivity values were calculated at a fixed density of 4.1249 g
    cm$^{-3}$. The thermal expansion reactivity values were calculated at a
    fixed temperature of 973 K.}
    \label{fig:reactivity}
\end{figure}
%
\begin{table}[htb!]
	\centering
	\caption{Doppler, density, and total temperature coefficients
	for the temperature range of 800 K to 1400 K.}
	\begin{tabular}{l S S S}
		\toprule
		{Software} & {$\alpha_D$ [pcm
		K$^{-1}$]} & {$\alpha_\rho$ [pcm K$^{-1}$]} & {$\alpha_T$ [pcm
		K$^{-1}$]} \\
		\midrule
		{Serpent} & -3.737 \pm 0.013 & -3.424 \pm 0.013 &
		-7.165 \pm 0.013 \\
		{Moltres} & {-} & {-} & -7.184\\
		\bottomrule
	\end{tabular}
	\label{table:alpha}
\end{table}

\section{Neutron Energy Spectrum}

\begin{figure}[htb!]
    \centering
    \includegraphics[width=.8\textwidth]{nt-spec}
    \caption{The fine-group and six-group neutron energy spectra from Serpent
    2 and Moltres normalized per unit lethargy.}
    \label{fig:ntspec}
\end{figure}

Moltres also closely replicated the six-group neutron spectrum from the
Serpent group constants. Figure \ref{fig:ntspec} compares the neutron energy
spectra from Serpent and Moltres in the central fuel salt region. More
generally, the
plot shows the distinctive fast spectrum observed in the \gls{MSFR} with dips
in the spectrum corresponding to elastic scattering resonances from lithium
and fluorine. We could obtain a more accurate representation of the neutronics
in the \gls{MSFR} by using more neutron energy groups with appropriate energy
boundaries but this would adversely impact simulation times in the
subsequent multiphysics finite element analyses.

In summary, Moltres replicated the relevant neutronics parameters
accurately using the group constant data from Serpent 2. Moltres agrees with
the high fidelity simulation in Serpent 2 for the $\beta_{\text{eff}}$ and
temperature reactivity coefficients, which are important parameters for
modeling transient reactor behavior. The $k_{\text{eff}}$ values have
discrepancies on the order of 100 pcm which are relatively small compared
to other \gls{MSR} multiphysics simulation tools (e.g. the neutron diffusion
and SP3 models in OpenFOAM \cite{cervi_development_2019}).


\citet{BesleyRQ2014} sketch the contours of mechanisms through which the broad relationship in their study - between pre-colonial conflict and modern day conflict - is realized. They argue that historical conflict affects modern outcomes through its consequences on subsequent economic, political and social outcomes. Throughout their analyses, the authors only demonstrate that this pre-colonial conflict correlates with some indicators of trust and certain measures of local economic development; however, it remains to assess which of these mechanisms matter when.

There are several feasible ways to parse the mechanisms through which pre-colonial conflict matters for modern development outcomes, both theoretical and empirical. In this section, we discuss an automated variable selection method using the Least Absolute Shrinkage and Selection Operator (LASSO) algorithm \citep{tibshirani1996regression}. The basic idea behind such variable selection approaches is simple: given a set of candidate independent variables, what combination of them best explains the observed variance in the outcome variable?

The intuition behind LASSO is that we run a series of models and penalize the absolute size of the regression coefficients. In so doing, we constrain the sum of regression coefficients to be below a certain threshold (which we specify using cross-validation, as explained below), and if this threshold is exceeded then we penalize particular parameters by setting them to zero. The basic process is to sequentially add parameters to the model in the order of their bivariate correlation with the outcome variable, and gradually increase the coefficients associated with these variables until they are no longer the most strongly correlated with the outcome. Then, introduce additional explanatory variables until no other variables can be introduced into the model which are better correlated with the outcome subject to the coefficient size constraint.

In all models, we use cross-validation to select the optimal number of coefficients to keep in the model. Cross-validation randomly selects a subset of the overall dataset as a training set, then fits a model and assesses its performance on the rest of the dataset (the validation data). If the model predicts out-of-sample observations with a relatively low mean-squared error then it is selected. This enables us to select the optimal LASSO-generated set of independent variables.

We argue that using the LASSO is useful in the context of \citet{BesleyRQ2014} for two distinct applications:
\begin{enumerate}
	\item When the order of candidate independent variables is a similar order to the number of observations in a dataset, and including all of them would badly overfit the model.
	\item When we believe that the way key explanatory variables interact with others is important for the underlying mechanism, but it is theoretically hard to predict which deserve focus.
\end{enumerate}

These two applications allow us first to select variables in models that are otherwise overdetermined, and secondly to detect effect heterogeneity that the authors do not explore.

\subsection{Assessing Overdetermination}

Regarding the first application, we apply the LASSO to the cross-country regressions the authors describe in Table 2 of their paper, which show that the number of years a given African country has been in civil war since independence is highly correlated with the number of wars in that territory between 1400-1700.

However, in their baseline specification they fit 9 independent variables to their 49 observations, and in their secondary specification they fit 29 independent variables to the same sample size. This represents a clear case of overadjustment \citep{SchistermanColePlatt2009} and the resultant models are highly overfitted to the data, with many regressors sharing high covariance. So, we use the LASSO to define the subset of these many variables which best explain variation in the dependent variable, then re-run the regression to assess how the coefficients of interest are affected.

Column 1 in Table 1 replicates the baseline cross-country specification of the authors, and column 2 uses the same set of regressors, applies the LASSO, then re-runs the regression with only the selected variables. Column 3 replicates their saturated specification, and column 4 applies the LASSO to the larger set of independent variables and run-runs the regression accordingly. In column 3, most of the regressors are excluded for display purposes.

\input{LASSO_1.tex}

The results of the LASSO process suggest that, even in spite of the overdetermined model, their key explanatory variable is explaining parts of variation in the outcome that the other variables - many of which are post-treatment - do not. This derives from the fact that the pre-colonial conflict variable is selected in both columns 2 and 4, and the coefficient size and significance is stable across specifications. The procedure suggests that the only other variables that meaningfully explain variation are whether the country is in the west or east of Africa, and whether it was a French or Portuguese colony. All the other controls are shown to contribute little to the explanatory power of the model.

\subsection{Heterogeneity in Social Attitudes}

Regarding the second application of the LASSO, in their more disaggregated analysis the authors use samples of a much larger size to demonstrate that historical conflict affects modern social attitudes but do not explore how the effect is moderated by any other factors. To try and get at the mechanisms through which pre-colonial conflict matters, we argue that looking at interaction effects between the key explanatory variable and other explanatory variables is an appropriate strategy.

For example, we may believe that pre-colonial conflict would affect the development of political institutions in a given country, which then affect the probability of civil wars \citep{AcemogluJohnsonRobinson2001}. So, pre-colonial conflict may be expected to especially impact social outcomes in areas which subsequently became autocratic, or which were later exploited by the slave trade. However, given the number of possible interaction effects in their models, it is hard to choose \textit{ex ante} which ones should be assessed.

The LASSO approach is highly applicable here: we generate models based on their most saturated specifications of their key results, and interact every independent variable with the explanatory variable of interest, pre-colonial conflict. Then, using the LASSO, we select the subset of interactions that together best explain variation in the outcome variable. While we cannot apply such an approach to the cross-country regressions due to power constraints, the social survey response data, with $n > 17,000$, is more amenable to such approach. The authors use this data to show that respondents who live in locations which suffered more pre-colonial civil wars display significantly lower levels of inter-group trust, significantly higher levels of ethnic identification, and significantly lower levels of national identification. In these regressions, they use 76 independent variables (including pre-colonial conflict) to control for omitted variables.

From these 76 variables, we exclude all occupation and country-level fixed effects and interact the remaining 18 independent variables with the variable for pre-colonial conflict.  Then, running the LASSO algorithm on the entire model, we assess which interaction terms are selected by the LASSO. The reason for excluding the fixed effects is that those interactions are hard to meaningfully interpret - we have no reason to believe that the effects of pre-colonial conflict should interact with the particular profession of the survey respondent, and looking at the interaction with country fixed effects just indicates which specific countries have had both pre-colonial conflicts and lower trust levels today.

Table \ref{lasso_1} presents the results, where `Positive' means that the interaction of the variable listed (when interacted with pre-colonial conflict) is selected by the LASSO and has a coefficient greater than zero, `Negative' means the coefficient is less than zero, and an empty cell means the interaction is not selected by the LASSO.

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Thu, May 05, 2016 - 13:58:53
\begin{table}[!htbp] \centering
  \caption{Interaction effects selected by lasso (Afrobarometer)}
  \label{lasso_1}
\footnotesize
\begin{tabular}{@{\extracolsep{2pt}} lccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Interacted variable & Inter-group trust & Ethnic identity & National identity \\
\hline \\[-1.8ex]
Age &   & Positive & Negative \\
Age squared & Negative &   & Negative \\
Male &   & Negative & Positive \\
Urban &   &   & Negative \\
Civil war incidence &   &   &   \\
Log slave exports & Negative & Positive & Negative \\
Log pop density (1400) &   &   & Negative \\
GDP per capita &   &   & Negative \\
Latitude &   &   & Negative \\
Longitude & Negative &   &   \\
Rainfall &   &   & Negative \\
Humidity &   &   & Negative \\
Temperature &   &   &   \\
Log coastline area & Positive &   &   \\
Islam &   & Positive &   \\
Log gold per capita &   &   &   \\
Log oil per capita & Positive &   & Positive \\
Log diamonds per capita &   &   &   \\
Ruggedness &   &   &   \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}

Even though this is a fully automated approach to variable selection, the algorithm produces a number of interesting results. First, the only interaction term selected across the three dependent variables measures the exposure of that region to the slave trade. So, places with long histories of conflict and high amounts of slaves exported demonstrate lower levels of inter-group trust, higher ethnic identification, and lower national identification. This aligns strongly with evidence on the persistent impact of the slave trade on modern attitudes, as argued by \citet{NunnWantchekon2011}.

Second, the interaction on oil reserves is selected in two of the models. This again makes sense: if we believe that pre-colonial conflict weakened institutional structures, then the presence of oil in that region ought to act as an exacerbator for conflict once the resource becomes valuable. A well-established literature on how institutions facilitate the resource curse in developing countries, which may impact social attitudes, leads credence to this heterogeneity \citep{mehlum2006institutions}.

Third, that the interaction with civil war incidence is not selected in any of the models. This suggests, perhaps surprisingly, that there is no meaningful interactive effect between pre-colonial conflict and post-colonial conflict on modern social attitudes. Other implications are less clear - that male respondents in areas with more history of conflict display stronger senses of ethnic identity and weaker senses of national identity may suggest something about the implications of conflict on gender norms, but it is hard to say much more.

\subsection{Heterogeneity in Grid-cell Results}

In their paper, the authors provide more granular evidence that the existence of pre-colonial conflict in a given 125 x 125 km grid cell is associated with more modern conflict in that grid cell, as well as significantly lower levels of night light intensity. As outlined in \citet{henderson_measuring_2012}, the use of night light density is argued to strongly proxy for subnational income indicators in developing countries - so, grid cells with more pre-colonial conflict have lower income today.

Here, we apply the same approach as to the survey data: we take the full set of 119 regressors from their from their most saturated models, then drop all the fixed effects for countries and economic activity in that grid cell. Then, with the remaining independent variables, we interact all of them with the central independent variable, apply the LASSO, and assess the interaction terms that are selected as explaining important heterogeneity. Table \ref{lasso_2} presents the results.

\begin{table}[!htbp] \centering
  \caption{Interaction effects selected by lasso (grid cell)}
  \label{lasso_2}
\footnotesize
\begin{tabular}{@{\extracolsep{2pt}} lcc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Interacted variable & Conflict & Light density \\
\hline \\[-1.8ex]
Distance to coast &   &   \\
Average elevation &   & Positive \\
Ruggedness &   &   \\
Average temperature & Positive &   \\
Average precipitation &   &   \\
Area &   &   \\
Log pop density (1990) &   & Negative \\
City in cell in 1400 &   &   \\
More than one ethnicity in cell &   & Negative \\
Log slave exports &   & Negative \\
Capital in cell &   & Positive \\
Distance to capital 0-10 percentile &   &   \\
Distance to capital 10-25 percentile &   & Negative \\
Distance to capital 25-50 percentile &   &   \\
Distance to capital 50-75 percentile & Positive & Positive \\
Distance to capital 75-90 percentile &   &   \\
Jurisdictional hierarchy &   &   \\
Log night lights (1992) &   &   \\
Mineral share &   &   \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}

While the selected interaction terms are less interpretable than those from the social attitudes data, it still provides some suggestive evidence on the mechanisms at work. For interactive effects on the presence of modern conflict, areas with pre-colonial conflict far from capital cities have more modern conflict, as well as those areas with higher average temperatures - which perhaps suggests a historical component to the large literature demonstrating a link between climate and violence \citep{hsiang2013quantifying}. Regarding local economic activity, more interactive effects appear important: again, the interaction with the slave trade is selected by the algorithm, as is the interaction with the extent of ethnic division in a given grid cell. Other interactions are harder to interpret.

\subsection{Summary}

The results of the variable selection analysis demonstrate two points. First, that in spite of the specification issues of their coarse cross-country regressions, the incidence of pre-colonial conflict appears to explain variation in modern conflict distinct from their set of plausibly overfitted set of independent variables. And second, that the process detects intuitive interactive effects in their data that the authors do not explore in their analysis - that the effect is especially magnified by subsequent exposure to the slave trade and the existence of oil, which supports the academic literature pointing to the central importance of historical institutional development in determining modern outcomes. We explore and evaluate the strength of these historical interactions in the next section.


\section{Weak Induction}
\input{Ch5/weak_ind}

\section{Strong Induction}
\input{Ch5/strong_ind}

\section{Proving the Induction Principle}
\subsection{The Well Ordering Principle}
\input{Ch5/wop}
\subsection{Equivalence of the Well Ordering Principle and the Principle of Mathematical Induction}
\input{Ch5/proving_wind}