\documentclass[amsmath,amssymb,aps,pra,reprint,groupedaddress,showpacs]{revtex4-1}

\usepackage{multirow}
\usepackage{verbatim}
\usepackage{color,graphicx, verbatim, float}

\input{../Common}

\begin{document}

%%%%%%%%%%%%%%% TITLE %%%%%%%%%%%%%%%%
\title{Fibonacci Sequence Prime Factors Properties}

%%%%%%%%%%%%%%% AUTHORS %%%%%%%%%%%%%%%%
\author{Lucchi Manuele}
\email[]{manuele.lucchi@studenti.unimi.it}
\affiliation{IT Department Students, Universita' degli Studi di Milano, Citta' degli Studi, Milano, Italia}

%%%%%%%%%%%%%%% DATE %%%%%%%%%%%%%%%%
\date{\today}

%%%%%%%%%%%%%%% ABSTRACT %%%%%%%%%%%%%%%%
\begin{abstract}
The purpose of this research is to find a common pattern in the scomposition at prime factors of the Fibonacci Numbers.
\end{abstract}

\maketitle

%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%
\section{Introduction}

The Fibonacci Sequence [1] is a recurrent equation introduced by Fibonacci in 1202. This sequence rapidly increases the size of its numbers and has a lot of properties.
The sequence is $F_n = F_{n-1} + F_{n-2}$.

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/fibonacci(100).png}
\caption{The Fibonacci Sequence}
\end{figure}

%%%%%%%%%%%%%%% POWER OF 2 BEHAVIOURS IN FIBONACCI NUMBERS %%%%%%%%%%%%%%
\section{Powers of 2 behaviours in Fibonacci Numbers}

We first tested with the simpler prime numbers. If we look at the power of 2 present in the first 100 Fibonacci Numbers we get something like this

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(100,2).png} % Power of 2 in 100
\caption{Power of 2 as factor of the first 100 Fibonacci Numbers}
\end{figure}

It's pretty easy to see some sort of regularity between the power of 2 included in each Fibonacci Number. We can now increase the range to 1000 to see it better

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(1000,2).png} % Power of 2 in 1000
\caption{Power of 2 as factor of the first 1000 Fibonacci Numbers}
\end{figure}

And again we can see a similar behaviour. First of all, we can notice how only numbers that are multiple of 3 are divisible by 2.

So we can suppose that every number divisible by 3 has its Fibonacci equivalent to be even

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(100,2)only(3).png} % Power of 2 in 100 divisible for 3
\caption{Power of 2 as factor of the first 100 Fibonacci Numbers but only for $n$ divisible by 3}
\end{figure}

Instead, if we look only at every $n$ not divisible by 3, we get something like this

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(100,2)except(3).png} % Power of 2 in 100 not divisible for 3
\caption{Power of 2 as factor of the first 100 Fibonacci Numbers but only for $n$ not divisible by 3}
\end{figure}

So we can draw a first conclusion:
$$\mathbf{\forall n \in \mathbb{N} : 3\mid n \implies 2\mid Fibonacci(n)}$$

Going forward, we can define the regularity for every exponent of 2,
for example, $F(n)$ is divisble by 2 starting from 3 (or $2^0 \cdot 3$) and $\forall n \in \mathbb{N}$ that is a multiple of 3.
If we search for Fibonacci Numbers divisible for $2^3$, we have to start from 6 (or $2^1 \cdot 3$)
and increase of 12 (or $2^2 \cdot 3$) at time. For $2^4$ the starting $n$ becomes 12 (or $2^2 * 4$)
and the increase between similar exponents is 24 (or $2^4 \cdot 3$).\\

So, after defining E as a certain exponent of 2 $\geq 3$ inside Fibonacci Numbers we can set the first $n$ which will appear as $2^{E-1}$ and the difference between the $n_s$ that have the same exponent of 2 in $F(n)$
as $2^E \cdot 3$.\\

However, the regularity in the exponents of 2 is not $\forall n$, since 0, 1, and 2 don't follow it,
otherwise, with 1 as exponent we would have a 3 as a starting point, but we got 6. Also, it seems like that 2 as an exponent will not appear,
so \AllBold{we will not find any $F(n)$ that can be divided by $2^2$ and not for $2^k$ with $k \in N \land k>2$ at the same time}

%%%%%%%%%%%%%%% A FIRST SPECIFIC FORMULA %%%%%%%%%%%%%%%%
\section{A first, specific Formula}

We have now all the tools for the development of a simple equation that gives us the power of in the $n_{th}$ number of Fibonacci. Let's define $S_n$ as the exponent of 2 of the $n_{th}$ Fibonacci Number,
we can say that $\mathbf{S_n = S_{n/2} + 1}$. But that is true for $F(n)$ only where $n$ is divisible by both 2 and 3, so we have to create special cases for
$n=1$ and divisible by 3 or 2 but not both. For $n = 1$ or $n \nmid 2 \land \mid 3 $ it's always 0, for $2 \mid n \land 3 \nmid n $ it's always one.

So the final recurrent formula will be:
$$S_n = \begin{cases} S_{n/2} + 1, & \mbox{if }2|n \land 3 \mid n \\ 1, & \mbox{if } 2 \nmid n \land 3|n \\ 0, & \mbox{if } \left( 2 \mid n \land 3 \nmid n \right) \lor n = 1 \lor n = 0
\end{cases} $$

%//DECISAMENTE MIGLIORAMENTO FORMULA

%%%%%%%%%%%%%%% A RECURRENT BEHAVIOR IN PRIME FACTORS SCOMPOSITION OF FIBONACCI NUMBERS %%%%%%%%%%%%%%%%
\section{A recurrent behavior in Prime Factors Scomposition of Fibonacci Numbers}

2 is not the only base to present a similar behavior. Every number (even non prime ones) have a similar regularity, but with some differences.
For example the 3

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(100,3).png} % Power of 3 in 100
\caption{Power of 3 as factor of the first 100 Fibonacci Numbers}
\end{figure}

Or the 5

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(100,5).png} % Power of 5 in 100
\caption{Power of 5 as factor of the first 100 Fibonacci Numbers}
\end{figure}

And so on even for bigger prime numbers like 23

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(10000,23).png} % Power of 23 in 10000
\caption{Power of 23 as factor of the first 10000 Fibonacci Numbers}
\end{figure}

%//INIZIO FOCUS STARTING POINT

\begin{comment}

But it's not exactly the same, it seems like the more we increase the base, the less the number appears as a prime factor in Fibonacci Numbers
(so with an increasing distance between the same exponents), but also this is not true, there are a lot of cases that don't follow the growing rule.

But before getting in a more generic formula, we have to name some things (since they are going to be different for every base).

\begin{enumerate}
\item $\mathbf{\Gamma}$ will be the base of the prime (or non-prime) number we are taking as example.
\item $\mathbf{\Omega}$ will be the exponent of base $\Gamma$ of $F(n)$
\item $\mathbf{\Delta \Omega}$ will be defined as the distance between the Fibonacci Number before the next equal exponent will appear
\item $\mathbf{F(n)}$ will be the $n_{th}$ Fibonacci Number
\item $\mathbf{\lambda(\Omega, \Gamma)}$ will identify the function that returns the first $n$ where the exponent of the $\Gamma$ base appears in $F(n)$
\item $\mathbf{\lambda_n(\Omega, \Gamma)}$ will, in the same way, be the $n_{th}$ Fibonacci Number where the given exponent for the given base appears
\end{enumerate}

If we look at FIG. 6 and 7, we have two examples to notice the differences between different bases.
For $\Gamma = 3$ and $\Gamma = 5$ we have $\lambda(1, 3) = 4$ and $\lambda(1, 5) = 6$.
It seems like $\lambda(1, \Gamma) = \Gamma + 1$, but that's wrong: for example,
for $\Gamma = 11$ we have $\lambda(1, 11) = 10$. For $\Gamma = 13$ it will be $n = 7$.
So let's have a look at the starting points of the first prime numbers:

\end{comment}

%//FINE FOCUS STARTING POINT

%%%%%%%%%%%%%%% FOCUS ON NON PRIME FACTORS %%%%%%%%%%%%%%%%
\section{Focus on non-prime factors}

Until now we tested only prime numbers, but we can expect a similar behavior for non-primes.
For example if we look at power of 4 $(2^2)$ we notice that it's exactly halved compared of 2.

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/factor(100,[2,4]).png}
\caption{Comparison of powers of 2 and 4 of the first 1000 Fibonacci Numbers}
\end{figure}

We can have better overview by comparing the distance between the same exponent of two different factors
as they first appear. For example, using 2 and 4 as factors and comparing the index in Fibonacci numbers
when they are on 1 by subtracting we get this:

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/distance(1000,2,4,1,minus).png}
\caption{Difference between indexes of the same recurrence of the same grade of 2 and 4}
\end{figure}

As expected it's a linear function. If we try to divide them, again we can see the ratio it's exactly $\frac{1}{2}$

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/distance(1000,2,4,1,div).png}
\caption{Division between indexes of the same recurrence of the same grade of 2 and 4}
\end{figure}

%%%%%%%%%%%%%%% COMPARISON ON DIFFERENT BASES %%%%%%%%%%%%%%%%
\section{Comparison on different bases}

But if we try to obtain a choerent behaviour for other couple of numbers we'll be disappointed. The examples below are between 9 and 3

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/distance(1000,3,9,1,minus).png}
\caption{Difference between indexes of the same recurrence of the same grade of 3 and 9}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/distance(1000,3,9,1,div).png}
\caption{Division between indexes of the same recurrence of the same grade of 3 and 9}
\end{figure}

While in both of them we can see some sort of logic, it's not $1/3$ nor the square root ($9 = 3^2$).
However, this sort of behavior is once again recurrent for every number, even between numbers that doesn't have anything in common.
For example, 2 and 3:

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/distance(500,2,3,1,minus).png}
\caption{Difference between indexes of the same recurrence of the same grade of 2 and 3}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=3in]{assets/distance(500,2,3,1,div).png}
\caption{Division between indexes of the same recurrence of the same grade of 2 and 3}
\end{figure}

We tested a lot of possibilities but couldn't be able to find what connect them all, except one thing:
In every test made by dividing the corrispondent indexes, going forward with the iterations shows the ratio to stabilize near a certain number
We can see that already in FIG. 13 where it goes somewhere between 0.44 and 0.45 or in FIG.15 where it tends to 1.
On the other hand, subtracting the indexes shows some different behaviours, from linear functions to regular curves that tries to mimic linear functions.
Or again, what we saw in FIG. 14, that' pretty regular but different from everything else seen until now.
Of course the same test can be done with different exponent grades with the same properties.

%%%%%%%%%%%%%%% CONCLUSION %%%%%%%%%%%%%%%%
\section{Conclusion}

As we know, the Fibonacci Numbers hold a great number of interesting properties and this one sums up to the list.

%%%%%%%%%%%%%%% REFERENCES %%%%%%%%%%%%%%%%
\begin{thebibliography}{24}

\bibitem{fibonaccigeneral}
{OEIS},
\textit{Sequence A000045 (Fibonacci Numbers)}

\end{thebibliography}

\end{document}

%%https://www.overleaf.com/learn/latex/Positioning_images_and_tables


% Created: Enze Chen, May 2017
% Last edited: Enze Chen, December 2017
%
% Appendix B of the MSE 142 coursereader. This chapter reviews the math and physics a student ought to know when starting this course.
% Math: Differentiation, integration, and differential equations.
% Physics: Forces, potential, simple harmonic motion.

% Uncomment the following three lines and last line to individually compile this chapter
%\documentclass[12pt, english]{book}
%\usepackage{142crstyle}
%\begin{document}

\chapter{Prerequisites} \label{ch:prereq}
%{ \doublespacing
Ideally, you should find most of the material in this section to be review. More advanced concepts will be developed as we go along, but just to make sure we are all starting on the same page, I have listed the major concepts that I expect my students to be familiar with coming into this course. It would be wise to skim this section and take some more time with concepts you might not be as familiar with---nailing those will allow you to get a lot more out of this course! If any of the following topics are confusing, please stop by office hours and I'd be happy to go over it. \par

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

\section{Math review} \label{sec:math}
\subsection{Complex numbers}
Quantum mechanics lives in complex space, and you will find yourself manipulating complex numbers quite frequently. A complex number is a number that can be expressed in the form
\begin{tcolorbox}[title=Complex numbers]
$a + bi$, where $i = \sqrt{-1}$ and $a,b \in \mathbb{R}$
\end{tcolorbox}

We begin by looking at some powers of $i$. Namely,
\begin{align*}
i^1 &= \sqrt{-1} = i \\
i^2 &= i \cdot i = -1 \\
i^3 &= (i^2)i = -i \\
i^4 &= (i^3)i =  1 \\
i^5 &= (i^4)i = i \tag{same as $i^1$}\\
&\vdots
\end{align*}
and so on, where we begin to see it repeating in the exponent modulo 4. When we look at negative exponents, the same pattern holds.
\begin{align*}
i^1 &= \sqrt{-1} = i \\
i^0 &= 1 \\
i^{-1} &= \dfrac{1}{i} = \dfrac{i}{i\cdot i} = -i \tag{same as $i^{3}$} \\
i^{-2} &= \dfrac{i^{-1}}{i} = \dfrac{-i}{i} = -1 \tag{same as $i^2$} \\
&\vdots
\end{align*}

Whenever we want to eliminate complex numbers and work with real numbers instead, we multiply the complex number $a+bi$ by its \textbf{complex conjugate} $a-bi$, which has the opposite imaginary part. When we do so, we obtain
\begin{equation*}
(a+bi)(a-bi) = a(a) + a(-bi) + bi(a) + bi(-bi) = a^2 - \cancel{abi} + \cancel{abi} + b^2(-i^2) = a^2+b^2
\end{equation*}

Notation wise, if we have a complex number $\zeta = a + bi$, we denote its complex conjugate using an asterisk, such that $\zeta^* = a - bi$. \par

One of the most amazing results in complex analysis is \textbf{Euler's formula},~\footnote{See \href{https://en.wikipedia.org/wiki/Euler\%27s\_formula}{Wikipedia} for details and a proof.} which states that
\begin{tcolorbox}[title=Euler's formula]
	$e^{ix} = \cos(x) + i\sin(x), \quad \forall x \in \mathbb{R}$
\end{tcolorbox}

When this formula is evaluated at $x=\pi$, we arrive at the identity $e^{i\pi} = \cos(\pi) + i\sin(\pi) = -1$. Euler's formula will be extremely useful in our analysis of waves, because the sinusoidal behavior of cosine and sine are all encompassed neatly in the complex exponential term. I'll give two final comments about Euler's formula here:
\begin{enumerate}[1.]
	\item Though it's written in a slightly different form, $e^{ix}$ is still a complex number, with $a = \cos(x)$ and $b = \sin(x)$. Therefore, it has a complex conjugate equal to $e^{-ix}=\cos(x) -i\sin(x)$. Let's apply what we did above and multiply $e^{ix}$ and $e^{-ix}$.
	\begin{align*}
	e^{ix} \cdot e^{-ix} &= (\cos(x) + i\sin(x))(\cos(x)-i\sin(x)) \\
	e^{ix} \cdot e^{-ix} &= \cos^2(x) -i\cos(x)\sin(x) + i\sin(x)\cos(x) -i^2\sin^2(x) \\
	e^{ix} \cdot e^{-ix} &= \cos^2(x) + \sin^2(x) \\
	\Aboxed{e^{ix} \cdot e^{-ix} &= 1}
	\end{align*}

	We could have also arrived at this result by using the identity $e^a \cdot e^b = e^{a+b}$. Proceeding, $e^{ix} \cdot e^{-ix} = e^{ix - ix} = e^0 = 1$.

	\item With the complex conjugate on hand, we can now easily extract the individual cosine and sine terms. Notice what happens if we add $e^{ix}$ to $e^{-ix}$:
	\begin{align*}
	e^{ix} + e^{-ix} &= (\cos(x) + \cancel{i\sin(x)}) + (\cos(x) - \cancel{i\sin(x)}) \\
	e^{ix} + e^{-ix} &= 2\cos(x) \\
	\Aboxed{ \dfrac{e^{ix} + e^{-ix}}{2}&= \cos(x) }
	\end{align*}

	In a similar vein, we can show that $\boxed{\dfrac{e^{ix} - e^{-ix}}{2i} = \sin(x)}$ (note the $i$ in the denominator).

\end{enumerate}

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

\subsection{Differentiation} \label{sec:diff}
No matter which form it takes, the \Sch\ equation is inherently a differential equation, so we should be prepared to work with derivatives in this course. Before jumping into the math, it's important to understand at a high level what a derivative represents. \par
\begin{enumerate}[1.]
	\item Namely, the \textbf{first derivative} represents the slope of a tangent line to the function, or the instantaneous rate of change.
	\item The \textbf{second derivative} tells us the concavity of the function, where a positive value symbolizes concave up and a negative value for concave down.
\end{enumerate}

\textbf{Note}: I will be using both $\dv{x} f$ and $f'$ notation for derivatives, so I kindly ask for your patience and flexibility. This might also be the first time you see the notation $\pdv{x} f$, which represents a \emph{partial} derivative of $f$ with respect to the variable $x$. We won't be working with them too much, but know that it behaves exactly the same way as a normal derivative would. It's written this way to clarify that the function $f$ depends on more variables than just $x$, and we would only like to know how it changes with respect to changes in $x$. \par

The three main types of functions you will need to differentiate are:
\begin{enumerate}[1.]
	\item Polynomial: We apply the power rule, $\dv{x} x^n = nx^{n-1}$.
	\item Exponential: The derivative $ \dv{x} e^{f(x)} = f'(x)e^{f(x)}$.
	\item Trigonometric: $\dv{x} \cos(x) = -\sin(x)$. $\dv{x} \sin(x) = \cos(x)$.
\end{enumerate}

In every case, watch out for
\begin{itemize}
	\item Product rule: $\dv{x} [f(x)\cdot g(x)] = f(x)\cdot g'(x) + f'(x)\cdot g(x)$.
	\item Chain rule: $\dv{x} [f(g(x))] = f'(g(x)) \cdot g'(x)$.
\end{itemize}

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

\subsection{Taylor series}

Derivatives involve infinitesimals, and one technique that we will apply (and you will master!) is taking \textbf{Taylor series}~\footnote{See \href{http://tutorial.math.lamar.edu/Classes/CalcII/TaylorSeries.aspx}{Paul Dawkins' page} for more information.} expansions of functions. We will rely on Taylor series heavily to make approximations of functions whenever the result approaches 0. The Taylor series of a function $f(x)$ at a point $x=a$ is given by the infinite power series:
\begin{tcolorbox}[title=Taylor series formula]
$f(x)|_{x=a} = f(a) + f'(a)(x-a) + \dfrac{f''(a)}{2!}(x-a)^2 + \dots = \displaystyle\sum_{n=0}^{\infty} \dfrac{f^{(n)}(a)}{n!}(x-a)^n$
\end{tcolorbox}

The \emph{order} of a Taylor series is the largest degree polynomial in the series expansion that we write out explicitly. Everything above the highest-order term is typically discarded or abbreviated using $\text{big O}$ notation. As an example, the Taylor series for cosine about $x=0$ can be expressed as \[ \cos(x)|_{x=0} = 1 - \dfrac{x^2}{2!} + \order{x^4} \] where we lumped together everything after the second order term because those values are small enough that they won't affect the final answer and can be safely discarded. There's no requirement to memorize any specific Taylor series expansions for this class; you should just look them up in a table or derive the first few terms (we generally don't go beyond second order terms).

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

\subsection{Integration}
This course will feature plenty of integrals, not all of which are intuitive, but we will walk through all of them in lecture. As long as you have solid fundamentals, and are able to keep track of signs, integration limits, etc., then they shouldn't present a problem. \par

One thing to keep in mind for integration, differentiation, and this course in general is the idea of \emph{linear} operations/operators. What this essentially means is that we can distribute the operation across a sum, with integration being an example:
\begin{tcolorbox}[title=Linear operations---integration example]
	$\displaystyle\int \left[ f(x) + g(x) + h(x) \right]\dd{x} = \displaystyle\int f(x)\dd{x} + \displaystyle\int g(x)\dd{x} + \displaystyle\int h(x)\dd{x}$
\end{tcolorbox}

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

\subsection{Differential equations}
Knowledge of basic differential equations can be helpful for this course, but we will be sure to cover all the bases. Just know that a differential equation such as the \Sch\ equation combines all the fun of differentiation with the fun of integration! A differential equation always features some combination of derivatives, variables, and constants, an example of which is given by \[ \dv{y}{x} + f(x)y = 0 \]
where $f(x)$ is some arbitrary function that only depends on $x$. To cover some basic terminology, the above equation is a \textbf{linear}, \textbf{first-order} differential equation because the derivatives of $y$ (our dependent variable) are not multiplied together (only added) and the highest-order derivative of $y$ is the first derivative. This equation is also \textbf{separable} because we are able to rearrange the equation to have all variables of the same type on each side of the equation.
\begin{align*}
\dv{y}{x} + f(x)y &= 0 \\
\dv{y}{x} &= -f(x)y \\
\frac{1}{y}\dd{y} &= -f(x)\dd{x}
\end{align*}

Separable differential equations are nice for many reasons, one of which is that they can be solved by straightforward integration. We proceed to get
\begin{align*}
\int\frac{1}{y}\dd{y} &= \int-f(x)\dd{x} \\
\ln y &= -\int f(x)\dd{x} + C \\
y &= y_0\exp\left(-\int f(x)\dd{x}\right)
\end{align*}

where $y_0$ appears as the integration constant depending on initial conditions. We won't be focusing too much on solving differential equations in this class, at least not in this way, and this example was just meant to give you a general idea for the procedure.

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

\section{Physics review}
Often times, it helps to reframe the abstract nature of quantum mechanics in a classical mechanics context, so this section will highlight some key points in classical mechanics and electromagnetism. \par

\subsection{Forces and potentials}

Newton's laws of motion are fundamentally important, and you should be familiar in particular with the Second law, which says that force equals mass times acceleration.
\begin{tcolorbox}[title=Newton's Second law] \vspace{-2ex}
\[ F = ma = m \dv{v}{t} = m\dv[2]{x}{t} \]
\end{tcolorbox}

This expression can be rearranged to obtain
\begin{equation*}
	F = m\dv{v}{t} \implies \int F\dd{t} = \int m\dd{v} \implies \boxed{p = mv}
\end{equation*}
where $p$ is the symbol for \textbf{momentum}. \par

We can also find the energy/work done by multiplying together force and distance, such that $\Delta U = -W = -F \Delta x$. In differential terms, this is commonly expressed as
\begin{equation}
\boxed{F = -\dv{U}{x}} \label{eq:FdU}
\end{equation}
where the direction of the force is opposite the energy gradient (e.g. a hill slopes upwards, but pushes a ball downwards). \par

You should already know the expressions for the different forms of energy, shown in the table below. \par
\begin{table}[!h]
	\centering
	\begin{tabular}{ll}
		\textbf{Form} & \textbf{Expression} \\ \toprule
		Gravitational potential energy & $U_g = mgh$ \\ \rule{0ex}{3ex}
		Elastic potential energy & $U_e = \frac{1}{2}kx^2$ \\ \rule{0ex}{3ex}
		Kinetic energy & $K = \frac{1}{2}mv^2$
	\end{tabular}
\end{table}

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

\subsection{Simple harmonic motion} \label{sec:shm}
The behavior of atoms and subatomic particles can often be modeled using harmonic oscillators, so it's important to start with a solid understanding of simple harmonic motion (SHM) and the simple harmonic oscillator (SHO).

\begin{figure}[!h]
	\centering
	\begin{tikzpicture}
	\node[draw, rectangle, minimum size=8mm] (m) at (2.9,0) {$m$};
	\draw[decoration={aspect=0.45, segment length=3mm, amplitude=3mm,coil},decorate] (0,0)--(m);
	\fill[pattern = north east lines] (0,-0.42) rectangle (-0.4,1);
	\draw[thick] (0,1)--(0,-0.42)--(3.5,-0.42);
	\end{tikzpicture}
	\caption{Model of a simple harmonic oscillator as a mass on a spring.}
	\label{fig:SHO-model}
\end{figure}

We can think of a SHO as a mass on a spring (Figure~\ref{fig:SHO-model}). In equilibrium, everything is stationary; but when the mass is perturbed slightly, it feels a restoring force from the spring given by \textbf{Hooke's Law}: $F=-kx$, where $k$ is the spring constant and $x$ is the amount of displacement from equilibrium. The elastic potential energy stored in the spring is given by $U_e=\frac{1}{2}kx^2$, which can be derived by integrating Hooke's Law according to Equation~\ref{eq:FdU}. \par

With SHM, we typically like to think of cycles in terms of angle traversed, which makes it easier to model using trigonometric functions. This leads to \textbf{angular frequency}, $\omega$, which has units of radians per second (rad/s). In terms of the cycle frequency $f$, we have $\omega = 2\pi f$. Since period is the inverse of frequency, this also leads to $\omega = \frac{2\pi}{T}$. Perhaps most importantly, we can relate the angular frequency to the spring constant and mass by
\begin{tcolorbox}[title=Key Relationship] \vspace*{-2ex}
\begin{equation}
\omega=\sqrt{\frac{k}{m}} \label{eq:wkm}
\end{equation}
\end{tcolorbox}

We will not derive Equation~\ref{eq:wkm} here, but we will apply the result often when analyzing the quantum harmonic oscillator.

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

\subsection{Electromagnetism}
Then we have the electromagnetic analog to mechanics. As we analyze the behavior of electrons, we will treat them as point charges $q$ that experience a force given by
\begin{equation*}
	F = qE = -q\dv{V}{x}
\end{equation*}

where $E$ is the electric field and $V$ is the electric potential. Similar to the gravitational context, the force is related to the electric potential energy by $F = -\dv{U}{x}$, and an electron with mass $m_e$ experiences an acceleration according to Newton's Second law, $F = m_ea$. When dealing with two point charges, the force is also given by \textbf{Coulomb's law}, an inverse-square law which states that
\begin{equation*}
	F = \frac{1}{4\pi \varepsilon_0} \frac{q_1q_2}{r^2}
\end{equation*}

where $\varepsilon_0$ is the permittivity of free space, $q_1$ and $q_2$ are the charges of the particles, and $r$ is the distance between them. \par

It may also be helpful to know that light is one form of electromagnetic radiation, and the force carrier for the electromagnetic force is called the \textbf{photon}. Traveling electromagnetic radiation has both an electric field and magnetic field that are orthogonal (perpendicular) to each other (Figure~\ref{fig:EB-field}). The \emph{frequency} of light is directly proportional to its energy and the range of frequencies of electromagnetic radiation make up the \textbf{electromagnetic spectrum} (Figure~\ref{fig:EM-spec}).

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.7\linewidth]{EB-field}
	\caption{Electromagnetic radiation contains orthogonal electric and magnetic fields. Image courtesy of \href{http://www.schoolphysics.co.uk/age16-19/Wave\%20properties/Polarisation/text/Polarisation_/images/2.png}{schoolphysics}.}
	\label{fig:EB-field}
\end{figure}

\begin{figure}[!h]
	\centering
	\includegraphics[width=0.9\linewidth]{EM-spectrum}
	\caption{The electromagnetic spectrum. Image courtesy of \href{https://sites.google.com/site/chempendix/em-spectrum}{Sapling Learning}.}
	\label{fig:EM-spec}
\end{figure}

%}
%\end{document}


\section{Part 2. Gaussian Random Field}

The file of the functions used for this exercise is:

\lstinputlisting{part_two.py}

My script produces the following results, showing the Gaussian random fields for n = -1, -2, and -3, in Figures \ref{fig:gauss1}, \ref{fig:gauss2}, and \ref{fig:gauss3}, respecitvely.
As I chose the size to be in Mpc, the minimum size that these plots show is 1 Mpc. This affects the
maximum size in conjunction with $n$, as $n$ influences the structure formation on different scales. So, for example,
in Fig. \ref{fig:gauss3}, there are over and underdensities on the scale of a few hundred Mpc, while for
$n = -1$, the over and underdensities are on the order of one to a couple of Mpc in size. If I had chosen a physical size
of 1 pixel is 1 kpc instead, the same would hold true, with the largest over and underdensities for $n = -3$ being a few hundred kpc
in size, while for a larger $n$, $n = -1$, they would be at much smaller scales.

Additionally, as $n$ increases from -3 to -1, $k$'s absolute value goes up, suggesting that the minimum and maximum $k$
values get larger. For $n = -3$, the maximum over or underdensity has a $k$ value of around 0.003, while for $n = -1$, that value
goes up by nearly an order of magnitude to 0.025. The minimum $k$ value between those two $n$ values similar changes, going from 0.0005
to 0.005.

The larger $k$ values for larger $n$ valules also corresponds to smaller, more detailed structures in the density field, while
lower $k$ values correspond to much larger in space fluctuations.

\begin{figure}[h!]
  \centering
  \includegraphics[width=0.9\linewidth]{./plots/GaussianField-1.png}
  \caption{Gauss Random Field for n = -1}
  \label{fig:gauss1}
\end{figure}

\begin{figure}[h!]
  \centering
  \includegraphics[width=0.9\linewidth]{./plots/GaussianField-2.png}
  \caption{Gauss Random Field for n = -2}
  \label{fig:gauss2}
\end{figure}

\begin{figure}[h!]
  \centering
  \includegraphics[width=0.9\linewidth]{./plots/GaussianField-3.png}
  \caption{Gauss Random Field for n = -3}
  \label{fig:gauss3}
\end{figure}


\chapter{The arith Library}

This document describes the facilities provided by the \ml{arith} library
for the HOL system~\cite{description}. The main function is a partial decision
procedure for a subset of linear natural number
arithmetic\index{linear arithmetic}. There are also some associated functions,
and some more general tools for normalisation and for increasing the power of
proof procedures.

The main function, \ml{ARITH\_CONV}, is a partial decision procedure for
Presburger natural arithmetic\index{Presburger arithmetic}. Presburger natural
arithmetic is the subset of arithmetic formulae made up from natural number
constants, numeric variables, addition, multiplication by a constant, the
relations on the natural numbers ($<$, $\leq$, $=$, $\geq$, $>$) and the
logical connectives ($\neg$, $\wedge$, $\vee$, $\Rightarrow$,
$\Leftrightarrow$, $\forall$, $\exists$).
Products\index{products}\index{multiplication} of two expressions which both
contain variables are not included in the subset, but the function
{\small\verb%SUC%} which is not normally included in a specification of
Presburger arithmetic are allowed in this \HOL\ implementation.

\ml{ARITH\_CONV} further restricts the subset as follows: when the formula has
been put in prenex normal form\index{prenex normal form} it must contain only
one kind of quantifier, that is the quantifiers must either all be
universal\index{quantifiers!universal} (`forall') or all
existential\index{quantifiers!existential}. Variables may appear
free\index{free variables} (unquantified) provided any quantifiers that do
appear in the prenex normal form are universal; free variables are taken as
being implicitly universally quantified so mixing them with existential
quantifiers would violate the above restriction.

\index{normalisation}
The function makes use of a number of preprocessors\index{preprocessors} which
extend the coverage beyond the subset specified above and which are also
available as separate functions. Natural number subtraction\index{subtraction},
the predecessor function {\small\verb%PRE%}, and conditional
statements\index{conditional statements} can be eliminated using the function
\ml{SUB\_AND\_COND\_ELIM\_CONV}. Another preprocessor, \ml{INSTANCE\_T\_CONV},
permits substitution instances\index{instances}\index{substitution instances}
of universally quantified formulae to be accepted. There is also a function
for putting a formula into prenex normal form\index{prenex normal form}:
\ml{PRENEX\_CONV}.

\section{The method of proof}

The main function, \ml{ARITH\_CONV}, is constructed from two separate
procedures, one for universally quantified formulae (\ml{FORALL\_ARITH\_CONV})
and one for existentially quantified formulae (\ml{EXISTS\_ARITH\_CONV}).
\ml{FORALL\_ARITH\_CONV}\index{quantifiers!universal} uses a variable
elimination\index{variable elimination} technique similar to the one described
by Hodes~\cite{VarElimHodes}\index{Hodes}. This procedure is
incomplete\index{completeness} for the natural numbers\index{natural numbers}.
In particular, it does not prove formulae which depend on the integral
properties\index{integral properties} of natural numbers.

\ml{EXISTS\_ARITH\_CONV}\index{quantifiers!existential} uses a technique of
Shostak's~\cite{SUPINFShostak}\index{Shostak} based on the
{\small SUP-INF}\index{SUP-INF method} method due to
Bledsoe~\cite{SUPINFBledsoe}\index{Bledsoe}. This technique is able to find
witnesses\index{witnesses} for the existentially quantified variables, that is
values which satisfy\index{satisfaction of formulae} the formula.
Unfortunately this method too is incomplete\index{completeness} for the
natural numbers. The implementation used by \ml{EXISTS\_ARITH\_CONV} does not
have a very good coverage. It could be improved but only at the expense of
increased computation times. Despite the incompleteness, the procedure should
be of some use.

Both of the proof methods are described more fully in a
report~\cite{EfficFullyExpTP} which also discusses techniques for writing
efficient\index{efficient proof procedures} proof procedures in the \HOL\
system.

\section{Using the library}

The \ml{arith} library can be loaded into a user's \HOL\ session using the
function \ml{load\_library}\index{load\_library@{\ptt load\_library}} (see the
\HOL\ manual for a general description of library loading). The first action
in the load sequence initiated by \ml{load\_library} is to update the \HOL\
help\index{help!updating search path} search path. The help search path is
updated with a pathname to online help files for the \ML\ functions in the
library. After updating the help search path, the \ml{reduce}
library~\cite{reduce} is loaded. The \ml{arith} library makes use of some of
the functions in the \ml{reduce} library. Finally, the \ML\ functions in the
\ml{arith} library are loaded into \HOL.

The following session shows how the \ml{arith} library may be loaded using
\ml{load\_library}:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#load_library `arith`;;
Loading library `arith` ...
Updating help search path
.Loading library `reduce` ...
Extending help search path.
Loading boolean conversions........
Loading arithmetic conversions..................
Loading general conversions, rule and tactic.....
Library `reduce` loaded.
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
................................
Library `arith` loaded.
() : void

#
\end{verbatim}\end{session}

\noindent
The arithmetic proof procedure can be called by applying the conversion
\ml{ARITH\_CONV}:

\begin{session}\begin{verbatim}
#ARITH_CONV "!m n p. m < n /\ n < p ==> m < p";;
|- (!m n p. m < n /\ n < p ==> m < p) = T
\end{verbatim}\end{session}

\noindent
It is easy to define a function which returns a theorem directly corresponding
to the arithmetic formula:

\begin{session}\begin{verbatim}
#let ARITH_PROVE = EQT_ELIM o ARITH_CONV;;
ARITH_PROVE = - : conv

#ARITH_PROVE "!m n p. m < n /\ n < p ==> m < p";;
|- !m n p. m < n /\ n < p ==> m < p
\end{verbatim}\end{session}

\section{Using the procedure to prove a formula false}

Continuing the session from the previous section, here is an example of how
to use the proof procedure to prove that a formula is
false\index{false!proof of}\index{proving false}:

\begin{session}\begin{verbatim}
#NEGATE_CONV ARITH_CONV "!m n. m < n";;
|- (!m n. m < n) = F

#NEGATE_CONV ARITH_CONV "?m n. (m + n) < 0";;
|- (?m n. (m + n) < 0) = F
\end{verbatim}\end{session}

\noindent
\ml{NEGATE\_CONV} works by applying the proof procedure to the
negation\index{negation} of the formula. This means that when proving a
universally quantified formula false, the main procedure is actually trying to
prove that an existentially quantified formula is true. One could define a
function that proves formulae either true or false, but this would be
inefficient as for certain examples it would apply \ml{ARITH\_CONV} twice. The
author anticipates that in most circumstances the user will know whether the
formula is true or false and can therefore apply either \ml{ARITH\_CONV} or
\ml{NEGATE\_CONV ARITH\_CONV} as appropriate. A combined function is
illustrated below:

\begin{session}\begin{verbatim}
#let TF_ARITH_CONV = ARITH_CONV ORELSEC NEGATE_CONV ARITH_CONV;;
TF_ARITH_CONV = - : conv

#TF_ARITH_CONV "!m n p. m < n /\ n < p ==> m < p";;
|- (!m n p. m < n /\ n < p ==> m < p) = T

#TF_ARITH_CONV "!m n. m < n";;
|- (!m n. m < n) = F
\end{verbatim}\end{session}

\section{Constructing a tactic}

Consider the following goal\index{goals}:

\begin{session}\begin{verbatim}
#g "!x y t. x /\ ((~(t + 3 = 0)) => F | y) = y /\ x";;
"!x y t. x /\ ((~(t + 3 = 0)) => F | y) = y /\ x"

() : void
\end{verbatim}\end{session}

\noindent
\ml{ARITH\_CONV} can be used as a tactic\index{tactics} as illustrated below:

\begin{session}\begin{verbatim}
#expand (CONV_TAC ARITH_CONV);;
OK..
evaluation failed     ARITH_CONV -- formula not in the allowed subset
\end{verbatim}\end{session}

\noindent
This fails because the arithmetic conversion is only being applied to the
whole goal. To simplify the goal it is necessary to apply the conversion
deep\index{depth conversion} within the goal:

\begin{session}\begin{verbatim}
#let DEPTH_ARITH_TAC = CONV_TAC (ONCE_DEPTH_CONV ARITH_CONV);;
DEPTH_ARITH_TAC = - : tactic

#expand DEPTH_ARITH_TAC;;
OK..
"!x y t. x /\ (T => F | y) = y /\ x"

() : void
\end{verbatim}\end{session}


\chapter{What are Linear models?}

Chapter aims:

\begin{compactitem}
	\item developing an intuitive understanding of the theory of linear
	statistical models
	\item Introduce concepts to be developed using practical examples in
	subsequent chapters
\end{compactitem}

\section{Linear models}

Let's start with an example: What predicts the weights ($w$) of
lecturers (like yours truly!)? WE may hypothesize that the following
{\it variables} we have collected contribute to predicting weights:

\begin{compactitem}
	\item Height ($h$) in metres
	\item Exercise per week ($e$) in hours
	\item Gender ($g$)
	\item Distance from home to nearest Greggs bakery ($d$) in metres
	\item Ownership of a games console ($c$)
\end{compactitem}

From this, we can build a statistical model:
$w = \beta_1 + \beta_2 h + \beta_3 e + \beta_4 g_m  + \beta_5 d +
\beta_6 c_s + \beta_7 c_a + \varepsilon\$

The model has a combination of four components:

\begin{center}

\begin{tikzpicture}

\useasboundingbox (0 mm, 0mm) rectangle (118 mm, 30mm); % width of slide less margins - fixed (128 - 2 x 5)

% style fix minimum height on nodes to align labels above components,
% a chain to align the equation along and some colour styles for overlays
\begin{scope}[every node/.style={text height=3mm,  text depth=1mm, on chain, inner sep=0.5mm},
              gr/.style={teal},
              rd/.style={red},
              bl/.style={blue},
              gy/.style={gray},
              start chain, node distance=0mm]

\node [gr] (w) at (12mm, 19mm) {$w$}; % fixed position within bbox
\node      (eq) {$=$};
\node [rd] (b1) {$\beta_1$};
\node      (p)  {$+$};
\node [rd] (b2) {$\beta_2$};
\node [bl] (h)  {$h$};
\node      (p)  {$+$};
\node [rd] (b3) {$\beta_3$};
\node [bl] (e)  {$e$};
\node      (p)  {$+$};
\node [rd] (b4) {$\beta_4$};
\node [bl] (g)  {$g_m$};
\node      (p)  {$+$};
\node [rd] (b5) {$\beta_5$};
\node [bl] (d)  {$d$};
\node      (p)  {$+$};
\node [rd] (b6) {$\beta_6$};
\node [bl] (cS)  {$c_s$};
\node      (p)  {$+$};
\node [rd] (b7) {$\beta_7$};
\node [bl] (cA)  {$c_a$};
\node      (p)  {$+$};
\node [gy] (eps)  {$\varepsilon$};

\end{scope}

\begin{scope}[every node/.style={rounded corners, draw, minimum height=6mm},
              every path/.style={latex-, draw=red, thick}]

    \node (exp)  [inner sep=0pt, yshift=1cm, fit={(e) (cS)}, label=center:Explanatory variables, fill=blue!10, draw=blue] {};
    \node (coef) [inner sep=0pt, yshift=-1cm, fit={(b2) (b6)}, label=center:Coefficients, fill=red!10, draw=red] {};
	\node (resp)  [above = 4mm of w, fill=teal!10, draw=teal] {Response variable};
	\node (resid)  [below = 4.5mm of eps, fill=gray!10, draw=gray] {Residuals};

	\foreach \i in {e, d, g, cS}  \draw [blue] (\i.north) -- (\i.north |- exp.south);
	\draw [blue] (cA.north) |- (exp.east);
	\draw [blue] (h.north) |- (exp.west);
	\foreach \i in {b2, b3, b4, b5, b6}  \draw [red] (\i.south) -- (\i.south |- coef.north);
	\draw [red] (b7.south) |- (coef.east);
	\draw [red] (b1.south) |- (coef.west);
    \draw [teal] (w.north) --  (resp.south);
    \draw [gray] (eps.south) -- (resid.north);

\end{scope}

\end{tikzpicture}

\begin{compactitem}
\item A \textcolor{teal}{response variable} ($w$)
\item A set of \textcolor{blue}{explanatory variables} ($h,e,g,d,c$)
\item A set of \textcolor{red}{coefficients} ($\beta_1$ -- $\beta_7$)
\item A set of \textcolor{gray}{residuals} ($\varepsilon$)
\end{compactitem}

\end{center}

% \section{Different types of variables}
%
% \begin{center}
% \begin{tikzpicture}
%
% \useasboundingbox (0 mm, 0mm) rectangle (118 mm, 30mm);
%
% \begin{scope}[every node/.style={text height=3mm,  text depth=1mm, on chain, inner sep=0.5mm},
              % gr/.style={teal},
              % rd/.style={red},
              % bl/.style={blue},
              % gy/.style={gray},
              % start chain, node distance=0mm]
%
% \node [bl] (w) at (12mm, 19mm) {$w$}; % fixed position within bbox
% \node      (eq) {$=$};
% \node [gy] (b1) {$\beta_1$};
% \node      (p)  {$+$};
% \node [gy] (b2) {$\beta_2$};
% \node [bl] (h)  {$h$};
% \node      (p)  {$+$};
% \node [gy] (b3) {$\beta_3$};
% \node [bl] (e)  {$e$};
% \node      (p)  {$+$};
% \node [gy] (b4) {$\beta_4$};
% \node [rd] (g)  {$g_m$};
% \node      (p)  {$+$};
% \node [gy] (b5) {$\beta_5$};
% \node [bl] (d)  {$d$};
% \node      (p)  {$+$};
% \node [gy] (b6) {$\beta_6$};
% \node [rd] (cS)  {$c_s$};
% \node      (p)  {$+$};
% \node [gy] (b7) {$\beta_7$};
% \node [rd] (cA)  {$c_a$};
% \node [gy] (p)  {$+$};
% \node [gy] (eps)  {$\varepsilon$};
%
%
% \end{scope}
%
% \begin{scope}[every node/.style={rounded corners, draw, minimum height=6mm},
              % every path/.style={latex-, draw=red, thick}]
              %
    % \node (cont)  [inner sep=0pt, yshift=1cm, fit={(w) (d)}, label=center:Continuous variables, fill=blue!10, draw=blue] {};
    % \node (cat) [inner sep=0pt, yshift=-1cm, fit={(g) (cA)}, label=center:Categorical variables, fill=red!10, draw=red] {};
%
	% \foreach \i in {w, h, e, d}  \draw [blue] (\i.north) -- (\i.north |- exp.south);
	% \foreach \i in {g, cA, cS}  \draw [red] (\i.south) -- (\i.south |- coef.north);
    %
% \end{scope}
%
% \end{tikzpicture}
%
%
% \begin{compactitem}
% \item The response variable is always \textcolor{blue}{continuous}.
% \item The explanatory variables can be a mix of:
% \begin{compactitem}
% \item \textcolor{blue}{Continuous} variables: height, exercise and distance.
% \item \textcolor{red}{Categorical} variables: gender and console ownership.
% \end{compactitem}
% \item \textcolor{red}{Categorical} variables or {\it factors} have a number of {\it levels}:
% \begin{compactitem}
% \item Gender has two levels (Male / Female)
% \item Console has three levels (None / Sofa-based / Active)
% \end{compactitem}
% \end{compactitem}
% \end{center}
%
%
%
% \section{Terms and coefficients}
%
% \begin{center}
% \begin{tikzpicture}
%
% \useasboundingbox (0 mm, 0mm) rectangle (118 mm, 30mm); % width of slide less margins - fixed (128 - 2 x 5)
%
% \begin{scope}[every node/.style={text height=3mm,  text depth=1mm, on chain, inner sep=0.5mm},
              % gr/.style={teal},
              % rd/.style={red},
              % bl/.style={blue},
              % gy/.style={gray},
              % start chain, node distance=0mm]
%
% \node [bl] (w) at (12mm, 19mm) {$w$}; % fixed position within bbox
% \node      (eq) {$=$};
% \node [gy] (b1) {$\beta_1$};
% \node      (p)  {$+$};
% \node [gy] (b2) {$\beta_2$};
% \node [bl] (h)  {$h$};
% \node      (p)  {$+$};
% \node [gy] (b3) {$\beta_3$};
% \node [bl] (e)  {$e$};
% \node      (p)  {$+$};
% \node [gy] (b4) {$\beta_4$};
% \node [rd] (g)  {$g_m$};
% \node      (p)  {$+$};
% \node [gy] (b5) {$\beta_5$};
% \node [bl] (d)  {$d$};
% \node      (p)  {$+$};
% \node [gy] (b6) {$\beta_6$};
% \node [rd] (cS)  {$c_s$};
% \node      (p)  {$+$};
% \node [gy] (b7) {$\beta_7$};
% \node [rd] (cA)  {$c_a$};
% \node [gy] (p)  {$+$};
% \node [gy] (eps)  {$\varepsilon$};
%
%
% \end{scope}
%
% \begin{scope}[every node/.style={rounded corners, draw, minimum height=6mm},
              % every path/.style={latex-, draw=red, thick}]
              %
    % \node (height)    [below=0.5cm of h, fill=blue!10, draw=blue, xshift=-3mm] {Height};
    % \draw [blue] (h.south) -- (h.south |- height.north);
%
    % \node (exercise)  [below=0.5cm of e, fill=blue!10, draw=blue, xshift=4mm] {Exercise};
    % \draw [blue] (e.south) -- (e.south |- exercise.north);
%
    % \node (distance)  [below=0.5cm of d, fill=blue!10, draw=blue] {Distance};
    % \draw [blue] (d.south) -- (d.south |- distance.north);
%
    % \node (gender)  [above=0.5cm of g, fill=red!10, draw=red] {Gender};
    % \draw [red] (g.north) -- (g.north |- gender.south);
%
    % \node (console) [inner sep=0pt, yshift=1.1cm, fit={(cA) (cS)}, label=center:Console, fill=red!10, draw=red] {};
    % \foreach \i in {cA, cS}  \draw [red] (\i.north) -- (\i.north |- console.south);
    %
% \end{scope}
%
% \end{tikzpicture}
%
%
% \begin{compactitem}
% \item Each explanatory variable is a {\it term} in the model
% \item Each term has at least one coefficient
% \item \textcolor{blue}{Continuous} terms always have one coefficient
% \item \textcolor{red}{Factors} have $N - 1$ coefficients, where $N$ is the number of levels
%
% \end{compactitem}
% \end{center}
%
%
% \section{Wait! Why $N-1$? What is $\beta_1$?}
%
% \begin{center}
% \begin{tikzpicture}[every node/.style={text height=3mm,  text depth=1mm, on chain, inner sep=0.5mm},
              % gr/.style={teal},
              % rd/.style={red},
              % bl/.style={blue},
              % gy/.style={gray},
              % pr/.style={draw, rounded corners=2mm, gray},
              % start chain, node distance=0mm]
%
% \useasboundingbox (0 mm, 0mm) rectangle (118 mm, 21mm); % width of slide less margins - fixed (128 - 2 x 5)
%
% \node  (w) at (12mm, 10mm) {$w$}; % fixed position within bbox
% \node  (eq) {$=$};
% \node  (b1) {$\beta_1$};
% \node  (p)  {$+$};
% \node  (b2) {$\beta_2$};
% \node  (h)  {$h$};
% \node  (p)  {$+$};
% \node  (b3) {$\beta_3$};
% \node  (e)  {$e$};
% \node  (p)  {$+$};
% \node  (b4) {$\beta_4$};
% \node  (g)  {$g_m$};
% \node  (p)  {$+$};
% \node  (b5) {$\beta_5$};
% \node  (d)  {$d$};
% \node  (p)  {$+$};
% \node  (b6) {$\beta_6$};
% \node  (cS)  {$c_s$};
% \node  (p)  {$+$};
% \node  (b7) {$\beta_7$};
% \node  (cA)  {$c_a$};
% \node  (p)  {$+$};
% \node  (eps)  {$\varepsilon$};
%
% \node [pr, red, fit= (b1), line width=1pt] {};
% \node [pr, fit= (b2) (h)] {};
% \node [pr, fit= (b3) (e)] {};
% \node [pr, fit= (b4) (g)] {};
% \node [pr, fit= (b5) (d)] {};
% \node [pr, fit= (b6) (cS)] {};
% \node [pr, fit= (b7) (cA)] {};
%
% \end{tikzpicture}
% %
%
% \begin{compactitem}
% \item Two ways of thinking about $\beta_1$:
% \begin{compactitem}
% \item Continuous variables: the $y$ {\it intercept}
% \item Factors: the baseline or {\it reference} value
% \end{compactitem}
% \item This baseline is the value for the {\it first levels} of each factor
% \item All response values start at this baseline
% \item All the other coefficients measure {\it differences} from $\beta_1$:
% \begin{compactitem}
% \item along a continuous slope
% \item as an offset to a different level
% \end{compactitem}
% \end{compactitem}
%
% \end{center}
%
%
% \section{Linear models are just a sum}
%
% \begin{center}
% \begin{tikzpicture}[every node/.style={text height=3mm,  text depth=1mm, on chain, inner sep=0.5mm},
              % cross/.style={path picture={
                   % \draw[red, ultra thick] (path picture bounding box.center) +(0, 1.4mm) -- +(0, -1.4mm) (path picture bounding box.center) +(1.4mm, 0mm) -- +(-1.4mm,0) ;}},
              % gr/.style={teal},
              % rd/.style={cross, red},
              % bl/.style={blue},
              % gy/.style={gray},
              % pr/.style={draw, rounded corners=2mm, gray},
              % start chain, node distance=0mm]
%
% \useasboundingbox (0 mm, 0mm) rectangle (118 mm, 21mm); % width of slide less margins - fixed (128 - 2 x 5)
%
% \node      (w) at (12mm, 10mm) {$w$}; % fixed position within bbox
% \node      (eq) {$=$};
% \node      (b1) {$\beta_1$};
% \node [rd] (p)  {$+$};
% \node      (b2) {$\beta_2$};
% \node      (h)  {$h$};
% \node [rd] (p)  {$+$};
% \node      (b3) {$\beta_3$};
% \node      (e)  {$e$};
% \node [rd] (p)  {$+$};
% \node      (b4) {$\beta_4$};
% \node      (g)  {$g_m$};
% \node [rd] (p)  {$+$};
% \node      (b5) {$\beta_5$};
% \node      (d)  {$d$};
% \node [rd] (p)  {$+$};
% \node      (b6) {$\beta_6$};
% \node      (cS)  {$c_s$};
% \node [rd] (p)  {$+$};
% \node      (b7) {$\beta_7$};
% \node      (cA)  {$c_a$};
% \node [rd] (p)  {$+$};
% \node      (eps)  {$\varepsilon$};
%
% \end{tikzpicture}
% %
% \end{center}
%
% \begin{compactitem}
% \item Find the baseline value for women with no games console ($\beta_1$)
% \item The model tells us how much to add to this\ldots
% \begin{compactitem}
% \item  for a height of 1.82 metres?
% \item  for doing 150 minutes of exercise a week?
% \item  for being male?
% \item  for living 2416 metres from a Greggs?
% \item  for owning an Xbox?
% \end{compactitem}
% \end{compactitem}
%
%
%
%
%
%
% {\section{Examples - one continuous variable}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{Origin.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &=  \beta_1 x \\
		  % \\
		  % 4  &=  4 \times 1 \\
		  % 8  &=  4 \times 2 \\
		  % 12 &=  4 \times 3 \\
		  % 16 &=  4 \times 4
		% \end{align*}
		% \[\beta_1 =4\]
% \end{columns}
% }
%
%
%
% {\section{Examples - one continuous variable}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{Intercept.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1 + \beta_2 x \\
		  % \\
		  % 9  &= 5 + 4 \times 1 \\
		  % 13  &= 5 + 4 \times 2 \\
		  % 21 &= 5 + 4 \times 3 \\
		  % 29 &= 5 + 4 \times 4
		% \end{align*}
        % \[\beta_1 = 5; \beta_2=4\]
% \end{columns}
% }
%
%
% {\section{Examples - one factor}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{Factor.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1 + \beta_2 g_m  \\
		  % \\
		  % 2  &= 2 + 3 \times 0 \\
		  % 2  &= 2 + 3 \times 0 \\
		  % 2  &= 2 + 3 \times 0 \\
		  % 5  &= 2 + 3 \times 1 \\
		  % 5  &= 2 + 3 \times 1 \\
		  % 5  &= 2 + 3 \times 1
		% \end{align*}
        % \[\beta_1 = 2; \beta_2=3\]
% \end{columns}
% }
%
%
%
% {\section{Examples - one continuous variable and one factor}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{TwoVars.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1  + \beta_2 x + \beta_3 g_m\\
		  % \\
		  % 3   &= 1 + 2 \times 1 + 3 \times 0\\
		  % 5   &= 1 + 2 \times 2 + 3 \times 0\\
		  % 7   &= 1 + 2 \times 3 + 3 \times 0\\
		  % 6   &= 1 + 2 \times 1 + 3 \times 1\\
		  % 8   &= 1 + 2 \times 2 + 3 \times 1\\
		  % 10  &= 1 + 2 \times 3 + 3 \times 1
		% \end{align*}
        % \[\beta_1 = 1; \beta_2=2; \beta_3=3\]
% \end{columns}
% }
%
%
%
%
% {\section{Examples - one continuous variable and one factor}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{TwoVarsHighlight.pdf}
		%
		% \column{0.5\textwidth}
			% \begin{align*}
			  % y  &= \beta_1  + \beta_2 x + \beta_3 g_m\\
			  % \\
			  % 3   & = 1 + 2 \times 1 + 3 \times 0\\
			  % 5   & = 1 + 2 \times 2 + 3 \times 0\\
			  % 7   & = 1 + 2 \times 3 + 3 \times 0\\
			  % 6   & = 1 + 2 \times 1 + 3 \times 1\\
			  % {\color{red}8} & {\color{red}\;= 1 + 2 \times 2 + 3 \times 1}\\ % loses a space for some reason
			  % 10  & = 1 + 2 \times 3 + 3 \times 1
			% \end{align*}
	        % \[\beta_1 = 1; \beta_2=2; \beta_3=3\]
% \end{columns}
% }
%
% \section{How do we deal with variation?}
%
%
%
% {\section{Residuals - variation is everywhere}
%
% \begin{center}
		% \includegraphics[width=0.6\textwidth]{VariationOrme2002.pdf}\\
		% {\tiny ln(volume)}
% \end{center}
		%
% \begin{compactitem}
% \item Data always shows variation from a perfect model
% \begin{compactitem}
% \item Missing variables (age, lab vs. field biology, time of day)
% \item Measurement error
% \item Stochastic variation
% \end{compactitem}
% \end{compactitem}
% }
%
%
%
% {\section{Residuals - variation is everywhere}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{Error.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1 + \beta_2 x \\
		  % \\
		  % 9.50  &= \;?\; + \;?\; \times 1 \\
		  % 11.00 &= \;?\; + \;?\; \times 2 \\
		  % 19.58 &= \;?\; + \;?\; \times 3 \\
		  % 20.00 &= \;?\; + \;?\; \times 4
		% \end{align*}
		%
		% \begin{center}
		 % {\it No unique line \\ through the points}
		% \end{center}
		 %
% \end{columns}
% }
%
%
%
%
% {\section{Residuals - Guess 1}
 % \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{TooFlat.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1 + \beta_2 x  + {\color{red} \varepsilon }\\
		  % \\
		  % 9.50  &= 12.52 + 1 \times 1 - {\color{red} 4.02}\\
		  % 11.00 &= 12.52 + 1 \times 2 - {\color{red} 3.52}\\
		  % 19.58 &= 12.52 + 1 \times 3 + {\color{red} 4.06}\\
		  % 20.00 &= 12.52 + 1 \times 4 + {\color{red} 3.48}
		% \end{align*}
		% \[\beta_1 = 12.52; \beta_2=1\]
%
% \end{columns}
% }
%
%
%
% {\section{Residuals  - Guess 2}
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{Toosteep.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1 + \beta_2 x + {\color{red} \varepsilon }\\
		  % \\
		  % 9.50  &= -2.48 + 7 \times 1 + {\color{red} 4.98}\\
		  % 11.00 &= -2.48 + 7 \times 2 - {\color{red} 0.52}\\
		  % 19.58 &= -2.48 + 7 \times 3 + {\color{red} 1.06}\\
		  % 20.00 &= -2.48 + 7 \times 4 - {\color{red} 5.52}
		% \end{align*}
		% \[\beta_1 = -2.48; \beta_2=7\]
%
% \end{columns}
% }
%
%
%
% {\section{Residuals - least squares solution}
%
% Minimize the {\it sum} of the {\it squared} residuals
%

% \begin{columns}
% \column{\textwidth}
% \animategraphics[width=\textwidth,
                 % controls, %autoplay, %palindrome,
                 % timeline=Varying_timeline.txt,
                 % buttonsize=1em]{12}{Varying_timeline}{}{}
%
% \end{columns}
% }
%
%
%
% {\section{Residuals - least squares solution}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{JustRight.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % y  &= \beta_1 + \beta_2 x  + {\color{red} \varepsilon }\\
		  % \\
		  % 9.50  &= 5 + 4 \times 1 + {\color{red} 0.50}\\
		  % 11.00 &= 5 + 4 \times 2 - {\color{red} 2.00}\\
		  % 19.58 &= 5 + 4 \times 3 + {\color{red} 2.58}\\
		  % 20.00 &= 5 + 4 \times 4 - {\color{red} 1.00}
		% \end{align*}
		%
		% \[\beta_1 = 5; \beta_2=4\]
				%
% \end{columns}
% }
%
%
%
%
% {\section{Model as a matrix - terminology}
%
% \begin{center}
%
% {\LARGE $\mathbf{Y} = \mathbf{X \beta  +  \varepsilon}$}
%
% \vspace{5mm}
%
% \begin{tikzpicture}
%%minimum height on nodes to align labels above components
% \begin{scope}[every node/.style={minimum height=22mm}]
% \node                    (Y)  {$\left[ \begin{array}{r@{}l}
 								   % \alert<2>  {9} & \alert<2>{.50} \\
								   % \alert<3> {11} & \alert<3> {.00} \\
                                    % \alert<4> {19} & \alert<4> {.58} \\
                                    % \alert<5> {20} & \alert<5> {.00}  \end{array}\right]$};
% \node [right= 2mm of Y]  (eq) {$=$};
% \node [right= 2mm of eq] (X)  {$\left[ \begin{array}{cc} \alert<2>{1} & \alert<2>{1} \\
                                     % \alert<3>{1} & \alert<3>{2} \\
                                     % \alert<4>{1} & \alert<4>{3} \\
                                     % \alert<5>{1} & \alert<5>{4} \\ \end{array}\right]$};
% \node [right= 0mm of X]  (b)  {$\left[ \begin{array}{c} \alert<2-5>{5} \\
                                    % \alert<2-5>{4} \end{array}\right]$};
% \node [right= 2mm of b]  (pl) {$+$};
% \node [right= 2mm of pl] (e)  {$\left[ \begin{array}{r@{}l}
								   % \alert<2>  {0} & \alert<2>{.50} \\
								   % \alert<3> {-2} & \alert<3> {.00} \\
                                    % \alert<4>  {2} &\alert<4> {.58} \\
                                    % \alert<5> {-1} & \alert<5> {.00}  \end{array}\right]$};
% \end{scope}
%
% \begin{scope}[every node/.style={rounded corners, draw, fill=red!10},
              % every path/.style={latex-, draw=red, thick, fill=red}]
%
% \onslide<1-5>{
	% \node [above= 5mm of Y] (obs) {Observed values};
	% \node [below= 5mm of X] (mat) {Model matrix};
	% \node [above= 5mm of b] (coe) {Coefficients};
	% \node [below= 5mm of e] (res) {Residuals};
	% \draw (Y) -- (obs);
	% \draw (X) -- (mat);
	% \draw (b) -- (coe);
	% \draw (e) -- (res);
% }
%
% \onslide<6>{
	% \node [above= 5mm of eq, minimum width=38mm] (given) {Given these \ldots};
	% \node [above= 5mm of b, xshift=18mm] (find) {\ldots find the set of these\ldots};
	% \node [below= 5mm of b] (min)  {\ldots that minimize the sum of the squares of these.};
%
	% \draw (Y.north) -- (Y.north |- given.south);
	% \draw (X.north) -- (X.north |- given.south);
	% \draw (b.north) -- (b.north |- find.south);
	% \draw (e.south) -- (e.south |- min.north);
% }
%
% \end{scope}
%
% \end{tikzpicture}
% \end{center}
% }
%
%
% {\section{Model as a matrix - predictions}
%
% \begin{center}
%
% {\LARGE $\mathbf{\hat{Y}} = \mathbf{X \beta }$}
%
% \vspace{5mm}
%
% \begin{tikzpicture}
%% minimum height on nodes to align labels above components
% \begin{scope}[every node/.style={minimum height=22mm}]
% \node                    (Y)  {$\left[ \begin{array}{c} {9} \\
                                     % {13} \\
                                     % {17} \\
                                     % {21} \\ \end{array}\right]$};
% \node [right= 2mm of Y]  (eq) {$=$};
% \node [right= 2mm of eq] (X)  {$\left[ \begin{array}{cc} {1} & {1} \\
                                     % {1} & {2} \\
                                     % {1} & {3} \\
                                     % {1} & {4} \\ \end{array}\right]$};
% \node [right= 0mm of X]  (b)  {$\left[ \begin{array}{c} {5} \\
                                    % {4} \end{array}\right]$};
% \end{scope}
%
% \begin{scope}[every node/.style={rounded corners, draw, fill=red!10},
              % every path/.style={latex-, draw=red, thick, fill=red}]
%
	% \node [above= 5mm of Y] (obs) {Predicted or fitted values};
	% \node [below= 5mm of X] (mat) {Model matrix};
	% \node [above= 5mm of b] (coe) {Coefficients};
	% \draw (Y) -- (obs);
	% \draw (X) -- (mat);
	% \draw (b) -- (coe);
	%
% \end{scope}
%
% \end{tikzpicture}
% \end{center}
% }
%
%
% {\section{Predicted values}
%
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[width=\textwidth]{Predicted.pdf}
		%
	% \column{0.5\textwidth}
		% \begin{align*}
		  % \hat{y}  &= \beta_1 + \beta_2 x  \\
		  % \\
		  % 9  &= 5 + 4 \times 1 \\
		  % 13 &= 5 + 4 \times 2 \\
		  % 17 &= 5 + 4 \times 3 \\
		  % 21 &= 5 + 4 \times 4
		% \end{align*}
% \end{columns}
% }
%
% \section{Is a linear model appropriate?}
%
%
% {\section{Assumptions}
%
% \begin{compactitem}
% \item Linear models have the following assumptions:
% \begin{compactitem}
% \item No measurement error in explanatory variables
% \item The explanatory variables are not very highly correlated
% \item \color<2>{red} The model is linear
% \item \color<2>{red} The model has constant normal variance
% \end{compactitem}
% \item If these assumptions are not met, the model can be very wrong
% \item<2> The last two need some further explanation
% \end{compactitem}
%
% }
%
%
% \section{`The model is linear'}
%
% \includegraphics[width=\textwidth]{Linear.pdf}
%
% \begin{compactitem}
% \item These are {\it all} good linear models.
% \item Linear models can include curved relationships (e.g. polynomials)
% \item The data can be modelled as a {\it sum} of components
% \item A {\it linear combination} of variables and coefficients
% \end{compactitem}
%
%
% \section{The meaning of 'constant normal variance'}
		% \includegraphics[height=72mm]{ResidDemo.pdf}
		%
		% \begin{compactitem}
		% \item The data has a similar spread around any predicted point in the model
		% \vspace{2cm}
		% \item The residuals are normal
		% \item Points {\it should} be spaced equally in the area under the curve
		% \item Expect mostly small but a few larger residuals
		% \end{compactitem}
		%
% \includegraphics[width=\textwidth]{ConstantVarianceMods.pdf}
%
% \begin{compactitem}
% \item Three good models
% \begin{compactitem}
% \item Is the spread the same for all fitted values?
% \item Do the residuals match the normal expectation?
% \end{compactitem}
% \end{compactitem}
%
%
%
% \includegraphics[width=0.95\textwidth]{FitResid.pdf}
%
% \includegraphics[width=0.95\textwidth]{QQNorm.pdf}
%
% \includegraphics[width=\textwidth]{NonConstantVarianceMods.pdf}
%
% \begin{compactitem}
% \item Three bad models
% \begin{compactitem}
% \item Is the spread the same for all fitted values?
% \item Do the residuals match the normal expectation?
% \end{compactitem}
% \end{compactitem}
%
%
%
% \includegraphics[width=0.95\textwidth]{BadFitResid.pdf}
%
% \includegraphics[width=0.95\textwidth]{BadQQNorm.pdf}
%
%
% {\section{When is a linear model appropriate?}
% {\centering \Huge
%
% Plot the data!\\
% Plot the residuals!
%
% }}
%
%
% \section{Is a linear model explanatory?}
%
%
% {\section{How explanatory is the model?}
%
% \begin{center}
% \begin{compactitem}
% \item Finally! Some statistics! (Woohoo!)
% \item {\it Terms}: analysis of variance
% \begin{compactitem}
% \item Does the model explain enough variation?
% \item Does each term explain enough variation?
% \end{compactitem}
% \item {\it Coefficients}: $t$ tests
% \begin{compactitem}
% \item Are the coefficients different from zero?
% \end{compactitem}
% \end{compactitem}
%
% \end{center}
% }
%
%
% \section{Null and saturated models - two endpoints}
% \begin{columns}
%
	% \column{0.5\textwidth}
		% \includegraphics[height=72mm]{NullSaturated.pdf}
%
	% \column{0.5\textwidth}
		% \begin{compactitem}
		% \item The null model ($H_0$)
		% \item Nothing is going on
		% \item Biggest possible residuals
		% \item Residual sum of squares (RSS) is as big as it can be
		% \vspace{1cm}
		% \item The saturated model
		% \item One coefficient per data point
		% \item RSS is zero - all the sums of squares are now explained (ESS)
		% \end{compactitem}
		%
% \end{columns}
%
%
%
%
% \section{More interesting models}
%
%
	%
		% \centerline{\includegraphics[height=0.5\textheight]{Intermediate.pdf}}
%
		% \begin{compactitem}
		% \item Added a term with three levels
		% \item Some but not all of the residual sums of squares are explained
		% \item Is this enough to be interesting?
		% \end{compactitem}
%
%
%
%
%
% \section{The F statistic}
%
% \begin{center}
%
	% \begin{tikzpicture}[every node/.style={ text depth=1mm, inner sep=2mm,font=\Large}, node distance=0mm]
	%
	% \node (stat) {F};
	% \node (eq1) [right= of stat] {=};
%
	% \matrix (ratio1) [right=of eq1,ampersand replacement=\&] {
		% \node (ESS) {ESS}; \node(div1) [right=of ESS] {/}; \node (Nc) [right= of div1]{$N_c$} ; \\
		% \node (RSS) 	{RSS}; \node(div2) [right=of RSS]{/}; \node (Nr) [right= of div2]{$N_r$} ;\\
	% };
%
	% \draw (ESS.south west) -- (Nc.south east);
	% \node (eq2) [right= of ratio1] {=};
%
	% \matrix (ratio2)[right=of eq2,ampersand replacement=\&] {
		% \node (ESSn) {9.92}; \node(div3) [right=of ESSn] {/}; \node (Ncn) [right= of div3]{$2$} ; \\
		% \node (RSSn) {6.59}; \node(div4) [right=of RSSn]{/}; \node (Nrn) [right= of div4]{$6$} ;\\
	% };
%
	% \draw (ESSn.south west) -- (Ncn.south east);
	%
	% \node (eq3) [right= of ratio2] {=};
	% \node (res) [right= of eq3] {4.52};
	%
	% \begin{scope} [every node/.style={fill=blue!10, draw=blue, font=\large, rounded corners, align=center},
                   % every path/.style={latex-,  thick, draw=blue}]
	% \node (ESSGood)  [above=of ESS, yshift=8mm, xshift=-4mm] {Large ESS \\ is good};
	% \node (RSSGood)  [below=of RSS, yshift=-8mm, xshift=-4mm] {Small RSS \\ is good};
	% \node (NcGood)  [above=of Nc, yshift=8mm, xshift=8mm, anchor=south west] {Fewer coefficients \\ is better};
	% \node (NrGood)  [below=of Nr, yshift=-8mm, xshift=8mm, anchor=north west] {Residual degrees of freedom};
	%
	% \draw [blue] (ESS.north) -- (ESS.north |- ESSGood.south);
	% \draw  [blue](RSS.south) -- (RSS.south |- RSSGood.north);
	% \draw [blue] (Nc.north) |- (NcGood.west);
	% \draw [blue] (Nr.south) |- (NrGood.west);
%
	%
	% \end{scope}
	%
	% \end{tikzpicture}
%
% \end{center}
%
%
%
%
%
% \section{$F$ values by chance}
%
% \includegraphics[width=\textwidth]{F_extremes.pdf}
%
% \begin{compactitem}
% \item What is the distribution of $F$ if nothing is going on?
% \item Simulate 10,000 datasets where nothing is going on ($H_0$ is true)
% \item Calculate $F$ for each random dataset under $H_1$
% \item Mostly $H_1$ has a low $F$ - but sometimes it is high by chance
% \end{compactitem}
%
%
%
%
%
%
%
% \section{Distribution of $F$}
%
%
% \centerline{\animategraphics[height=0.5\textheight,
                 % %autoplay, %controls, %palindrome,
                 % timeline=FAnimate.txt,
                 % buttonsize=1em]{12}{FAnimate}{}{}}
%
% \begin{compactitem}
% \item In our possibly interesting model, $F=4.52$
% \item<2> 95\% of the random data sets have $F\le 5.5$
% \item<2> A model this good is found by chance 1 in 16 times ($p=0.063$)
% \item<2> Not quite interesting enough!
% \end{compactitem}
%
%
%
%
%
% \section{Are coefficients different from zero?}
%
%
% \begin{center}
%
	% \begin{tikzpicture}[every node/.style={ text depth=1mm, inner sep=2mm,font=\Large}, node distance=0mm]
	%
	% \node (stat) {t};
	% \node (eq1) [right= of stat] {=};
%
	% \matrix (ratio1) [right=of eq1,ampersand replacement=\&] {
		% \node (diff) {Effect size}; \\
		% \node (prec) 	{Precision}; \\
	% };
	% \draw (diff.south west) -- (diff.south east);
	%
	% \node (eq2) [right= of ratio1] {=};
%
	% \matrix (ratio2) [right=of eq2,ampersand replacement=\&] {
		% \node (val) {Coefficient value}; \\
		% \node (se) 	{Standard error}; \\
	% };
	% \draw (val.south west) -- (val.south east);
	%
	% \begin{scope} [every node/.style={fill=blue!10, draw=blue, font=\large, rounded corners, align=center},
                   % every path/.style={latex-,  thick, draw=blue}]
	% \node (esgood)  [above=of eq2, yshift=10mm ] {Large is good - bigger changes};
	% \node (precgood)  [below=of eq2, yshift=-10mm] {Small is good - known more precisely};
	%
	% \draw [blue] (diff.north) -- (diff.north |- esgood.south);
	% \draw [blue] (val.north) -- (val.north |- esgood.south);
	% \draw [blue] (prec.south) -- (prec.south |- precgood.north);
	% \draw [blue] (se.south) -- (se.south |- precgood.north);
		%
	% \end{scope}
	%
	% \end{tikzpicture}
% \end{center}
%
%
% \begin{compactitem}
% \item The value of a coefficient in a model is an {\it effect size}
% \item How much does changing this variable change the response?
% \item A {\it standard error} estimates how precisely we know the value
% \end{compactitem}
%
%
%
%
%
% \section{Variation in effect size and precision}
%
%
	%
		% \centerline{\includegraphics[height=75mm]{T_examples.pdf}}
%
%
%
%
%
%
% \section{$t$ values by chance}
%
% \includegraphics[width=\textwidth]{t_extremes.pdf}
%
% \begin{compactitem}
% \item What is the distribution of $t$ if nothing is going on?
% \item Simulate 10,000 datasets where nothing is going on ($H_0$ is true)
% \item Calculate $t$ for each random dataset under $H_1$
% \item Mostly $H_1$ has a $t$ near zero but can be positive or negative
% \end{compactitem}
%
%
%
%
%
%
% \section{Distribution of $t$}
%
%
% \centerline{\animategraphics[height=0.5\textheight,
                 % %autoplay, %controls, %palindrome,
                 % timeline=FAnimate.txt,
                 % buttonsize=1em]{12}{tAnimate}{}{}}
%
% \begin{compactitem}
% \item 95\% of the random data sets have $t\le \pm 2.09$
% \item Only the two higher precision models are expected to occur less than 1 time in 20 by chance.
% \end{compactitem}
%
%
%
%
% \section{Summary}
%
% \begin{compactitem}
% \item Linear models predict a continuous response variable
% \item A sum based on the effect size of explanatory variables
% \item Estimate the model using least squares residuals
% \item Need to check if the model is appropriate
% \item Then check if the model is explanatory
% \end{compactitem}

% ##################################################
% ################## Extra stuff ###################
% ##################################################

%
%
%\section{Examples: the null model}
%
%\includegraphics[width=\textwidth]{Anova_null.pdf}
%
%\begin{compactitem}
%\item The null hypothesis ($H_0$): Nothing is going on
%\item The residuals have to get smaller as we include terms.
%\item How much shorter?
%\end{compactitem}
%
%
%
%
%
%
%\section{Examples: one continuous term}
%
%\includegraphics[width=\textwidth]{Anova_mod.pdf}
%
%\begin{compactitem}
%\item An alternative model ($H_1$) using $x$
%\item Added one term ($x$) to the model to give ($H_1$)
%\item Do we reject $H_0$ and accept this new model?
%\end{compactitem}
%
%
%
%
%
%\section{Examples: adding a factor}
%
%\includegraphics[width=\textwidth]{Anova_mod2.pdf}
%
%\begin{compactitem}
%\item Another model ($H_2$) using $x$ and a factor $f$ with three levels
%\item The sum of squares gets smaller again
%\item We've added one term ($f$) but two coefficients ($f_b$ and $f_c$)
%\item Is this even better than $H_1$?
%\end{compactitem}
%
%
%
%
%
%\section{Change in variance}
%
%\begin{table}[htdp]
%\begin{center}
%\begin{tabular}{ccccc}
%	  & Model A & Model B & Model C \\
%\hline
%$H_0 $& Unexplained SS& 241.97 & 185.02 & 259.80 \\
%      & Explained SS  & 0      & 0      &    0   \\
%$H_1 $& Unexplained SS& 241.97 & 173.21 & 62.95 \\
%      & Explained SS  & 0.005  & 11.803 & 196.84 \\
%%$H_2$& 238.07 & 123.75 & 25.05 \\
%%								\\
%\end{tabular}
%\end{center}
%\end{table}%
%
%
%
%

%
%
%	{\section{How to find $\beta$ \dots}
%
%	\begin{center}
%	\begin{tikzpicture}
%
%	\node                     (eq1)  {=};
%	\node [left=  2mm of eq1] (lhs1) {$\mathbf{Y}$};
%	\node [right= 2mm of eq1] (rhs1) {$\mathbf{X \beta  +  \varepsilon}$};
%	\node [below= 7mm of eq1] (eq2)  {=};
%	\node [left=  2mm of eq2] (lhs2) {$\mathbf{X^T Y}$};
%	\node [right= 2mm of eq2] (rhs2) {$\mathbf{X^T X \beta}$};
%	\node [below= 7mm of eq2] (eq3)  {=};
%	\node [left=  2mm of eq3] (lhs3) {$\mathbf{(X^T X )^{-1} X^T Y}$};
%	\node [right= 2mm of eq3] (rhs3) {$\mathbf{\beta}$};
%
%
%	\end{tikzpicture}
%	\end{center}
%
%	}
%
%
%
%
%
%{\section{Categorical variables}
%\begin{columns}
%
%\column{0.5\textwidth}
%	\includegraphics[width=\textwidth]{Anova.pdf}
%
%\column{0.5\textwidth}
%	Features of categorical variables\\
%	\begin{compactitem}
%		\item Also called {\it factors}
%		\item Have a set of discrete {\it levels}
%		\item We want to model {\it differences} between levels
%		\item The least squares estimate for a level is simply the {\it mean} within the level
%		\item How do we include this in the linear model?
%	\end{compactitem}
%\end{columns}
%}
%
%
%
%
%{\section{Contrasts}
%
%\begin{compactitem}
%\item Many possible differences between levels ({\it contrasts})
%\item For 4 levels, the possible contrasts between:
%\begin{compactitem}
%\item two levels
%\item a pair of levels and a third level
%\item two pairs of levels
%\item one level and the remaining three levels
%\end{compactitem}
%\item Each {\it contrast} represents a possible hypothesis.
%\end{compactitem}
%\vspace{3mm}
%
%	\begin{tikzpicture}
%
%	\newcommand{\bb}{\node [circle,  fill=blue, inner sep=0.5mm]{} ;}
%	\newcommand{\rr}{\node [circle,  fill=red, inner sep=0.5mm]{} ;}
%
%	\matrix (contrasts) [matrix of nodes, ampersand replacement=\&, % BEAMER hijacks straight &
%	                     row sep=1mm, column sep=2.5mm,
%	                     nodes={circle, fill=black!35, inner sep=0.5mm}, nodes in empty cells]
%	{
%		    \&     \& \bb \&     \& \rr \& \rr \& [4mm] % {a}{b}
%		    \& \rr \&     \& \rr \& \bb \& \bb \&     \& \rr \& \bb \& \bb \& [4mm] % {ab}{c}
%	    \rr \& \rr \&  \bb \& [4mm] %% {ab}{cd}
%	    \rr \& \bb \& \bb \& \bb \\[1.5mm] %% {abc}{d}
%		    \& \bb \&     \& \rr \&     \& \bb \&
%		\rr \&     \& \bb \& \bb \&     \& \rr \& \bb \& \bb \& \bb \& \bb \&
%		\rr \& \bb \& \rr \&
%		\bb \& \rr \& \bb \& \bb \\[12mm]
%		\bb \&     \&     \& \bb \& \bb \&     \&
%		\bb \& \bb \& \rr \&     \& \rr \&     \& \bb \& \bb \&     \& \rr \&
%		\bb \& \rr \& \rr \&
%		\bb \& \bb \& \rr \& \bb \\[1.5mm]
%		\rr \& \rr \& \rr \&     \&     \&     \&
%	    \bb \& \bb \& \bb \& \bb \& \bb \& \bb \& \rr \&     \& \rr \&     \&
%	    \bb \& \bb \&  \bb \&
%	    \bb \& \bb \& \bb \& \rr \\
%	};
%
%	%% Draw in background bars to clarify contrast sets
%	\begin{scope}[on background layer, every node/.style=
%	                  {rounded corners, inner sep=0.7mm,
%	                    fill=black!20}]
%
%		 \def\x{contrasts-1-\i}
%		 \def\y{contrasts-4-\i}
%		 \def\r{col-\i}
%	     \foreach \i in {1, 2, ..., 23} \node (\r) [fit=(\x) (\y)]{};
%	\end{scope}
%
%	\node[left=4mm of contrasts-1-1]{Treat 2};
%	\node[left=4mm of contrasts-2-1]{Treat 1};
%	\node[left=4mm of contrasts-3-1]{Manip};
%     	\node[left=4mm of contrasts-4-1]{Control};
%
%	%		\draw ([yshift=5mm] col-1.north west)   -- node[above, font=\footnotesize] {\{a\} v. \{b\}}   ([yshift=5mm] col-6.north east);
%	%		\draw ([yshift=-5mm] col-7.south west)  -- node[below, font=\footnotesize] {\{a,b\} v. \{c\}}   ([yshift=-5mm] col-16.south east);
%	%		\draw ([yshift=5mm] col-17.north west)  -- node[above, font=\footnotesize] {\{a,b\} v. \{c,d\}}   ([yshift=5mm] col-19.north east);
%	%		\draw ([yshift=-5mm] col-20.south west) -- node[below, font=\footnotesize] {\{a,b,c\} v. \{d\}}   ([yshift=-5mm] col-23.south east);
%
%	\end{tikzpicture}
%}
%
%
%
%
%
%{\section{Model as a matrix - terminology}
%
%\begin{center}
%
%{\LARGE $\mathbf{Y} = \mathbf{X \beta  +  \varepsilon}$}
%
%\vspace{5mm}
%
%\begin{tikzpicture}
%% minimum height on nodes to align labels above components
%\begin{scope}[every node/.style={minimum height=22mm}]
%\node                    (Y)  {$\left[ \begin{array}{c}
%									1.58 \\ 6.79 \\
%									4.16 \\ 6.73 \\
%									4.26 \\ 6.33 \\
%									3.18 \\ 8.73
%                                 \end{array}\right]$};
%\node [right= 2mm of Y]  (eq) {$=$};
%\node [right= 2mm of eq] (X)  {$\left[ \begin{array}{cccc}
%									1 & 0 & 0 & 0 \\  1 & 0 & 0 & 0 \\
%									1 & 1 & 0 & 0 \\  1 & 1 & 0 & 0 \\
%									1 & 0 & 1 & 0 \\  1 & 0 & 1 & 0 \\
%									1 & 0 & 0 & 1 \\  1 & 0 & 0 & 1 \\
%                                \end{array}\right]$};
%\node [right= 0mm of X]  (b)  {$\left[ \begin{array}{c}
%                                         \beta_1 \\ \beta_2 \\
%                                         \beta_3 \\ \beta_4 \\
%                                 \end{array}\right]$};
%
%\node [right= 2mm of b]  (pl) {$+$};
%\node [right= 2mm of pl] (e)  {$\left[ \begin{array}{c}
%                                         \varepsilon_1 \\ \varepsilon_2 \\
%                                         \varepsilon_3 \\ \varepsilon_4 \\
%                                         \varepsilon_5 \\ \varepsilon_6 \\
%                                         \varepsilon_7 \\ \varepsilon_8 \\
%                                 \end{array}\right]$};
%\end{scope}
%
%\end{tikzpicture}
%\end{center}
%}
%
%
%
%{\section{Categorical variables}
%\begin{columns}
%
%\column{0.5\textwidth}
%	\includegraphics[width=\textwidth]{AnovaCoef.pdf}
%
%\column{0.5\textwidth}
%\end{columns}
%}
%
%
%
%
%
%{\section{Model as a matrix - terminology}
%
%\begin{center}
%
%{\LARGE $\mathbf{Y} = \mathbf{X \beta  +  \varepsilon}$}
%
%\vspace{5mm}
%
%\begin{tikzpicture}
%% minimum height on nodes to align labels above components
%\begin{scope}[every node/.style={minimum height=22mm}]
%\node                    (Y)  {$\left[ \begin{array}{c}
%									1.58 \\ 6.79 \\
%									4.16 \\ 6.73 \\
%									4.26 \\ 6.33 \\
%									3.18 \\ 8.73
%                                 \end{array}\right]$};
%\node [right= 2mm of Y]  (eq) {$=$};
%\node [right= 2mm of eq] (X)  {$\left[ \begin{array}{cccc}
%									1 &  0.5 & -0.5 &    0 \\  1 &  0.5 & -0.5 &    0 \\
%									1 &  0.5 &  0.5 &    0 \\  1 &  0.5 &  0.5 &    0 \\
%									1 & -0.5 &    0 & -0.5 \\  1 & -0.5 &    0 & -0.5 \\
%									1 & -0.5 &    0 &  0.5 \\  1 & -0.5 &    0 &  0.5 \\
%                                \end{array}\right]$};
%\node [right= 0mm of X]  (b)  {$\left[ \begin{array}{c}
%                                         \beta_1 \\ \beta_2 \\
%                                         \beta_3 \\ \beta_4 \\
%                                 \end{array}\right]$};
%
%\node [right= 2mm of b]  (pl) {$+$};
%\node [right= 2mm of pl] (e)  {$\left[ \begin{array}{c}
%                                         \varepsilon_1 \\ \varepsilon_2 \\
%                                         \varepsilon_3 \\ \varepsilon_4 \\
%                                         \varepsilon_5 \\ \varepsilon_6 \\
%                                         \varepsilon_7 \\ \varepsilon_8 \\
%                                 \end{array}\right]$};
%\end{scope}
%
%\end{tikzpicture}
%\end{center}
%}
%
%
%
%
%{\section{Concepts}
%\begin{centering}
%
%
%\includegraphics[width=\textwidth]{ScatterplotGroup.pdf}
%
%\end{centering}
%}
%
%
%{\section{Concepts}
%
%\begin{centering}
%
%\includegraphics[width=\textwidth]{CoefMarkup.pdf}
%
%\end{centering}
%
%}
%\section{Is a linear model appropriate?}
%
%{\section{Concepts}
%
%\begin{centering}
%
%\includegraphics[width=\textwidth]{CoefMarkup.pdf}
%
%\end{centering}
%
%}
%
%
%{\section{Concepts}
%
%\begin{centering}
%
%\includegraphics[width=\textwidth]{CoefMarkup.pdf}
%
%\end{centering}
%
%}


%%%%%%%%%% Start TeXmacs macros
\newcommand{\nocomma}{}
\newcommand{\noplus}{}
\newcommand{\tmop}[1]{\ensuremath{\operatorname{#1}}}
\newcommand{\tmtextbf}[1]{{\bfseries{#1}}}
\newcommand{\tmtextit}[1]{{#1}}
\newcommand{\tmtexttt}[1]{{\ttfamily{#1}}}
\newenvironment{descriptioncompact}{\begin{description} }{\end{description}}
%%%%%%%%%% End TeXmacs macros

\section{Restricted Boltzmann Machines}

Hopfield networks are deterministic: training a network with the same patterns
will yield the same weights, and matching a pattern against the network will
always give the same result. This has the advantage of simplicity and ease of
testing. However, as mentioned before, there are spurious attractors in the
Hopfield network (linear combination of an odd number of training patterns).
Using stochastic updating rules will decrease the probability of being 'stuck'
in such a state. This resembles simulated annealing, but ensuring we are not
caught in a local minima (in the energy landscape).

Restricted Boltzmann Machines have been used for various purposes in recent
years
\cite{louradour2011classification} \cite{teh2001rate} \cite{nair2010rectified}
, most of it
conducted by Geoffrey E. Hinton at university of Toronto, Canada. They have a
simple structure: one layer of visible units and one layer of hidden units,
which form a bipartite graph, as in Figure. The patterns used to train the
network correspond to the visible (exposed units), while the hidden units
correspond to the attributes of the features we would like to learn. It is
worth mentioning that in the most common and simple form, both the visible and
hidden units take binary values. This is the approach we also adopted for our
implementation.

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \begin{figure}[h]
  \centering
  \resizebox{300px}{300px}{\includegraphics{RBM-1.png}}
  \caption{Restricted Boltzmann Machine}
\end{figure}

The connections between the neurons in the two layers is symmetrical, so it
can be represented as a weight matrix which keeps the connection between hidden
and visible layers. A state of a Restricted Boltzmann machine is given by the
values of both the visible and given state. The corresponding Hopfield energy
formula for a state is given by :
\[ E (v, h) = - \sum_{i \in V} a_i v_i - \sum_{j \in H} b_j h_i - \sum_{i \in
   V, \nocomma j \in H \nocomma} v_i h_j w_{\tmtextit{\tmtextit{\tmop{ij}}}}
\]

where, $w_{\tmtextit{\tmtextit{}}}$is the weight matrix, $a$ and $b$are the
vectors of biases corresponding to the visible, respectively hidden layers. As
expected, $v$is vector of visible units and \tmtextit{h} is the vector of
hidden units.

We denote by \tmtextit{V} = \{1, .. \tmtextit{length} \tmtextit{v}\} , the
indices of a visible vector and by \tmtextit{H} = \{1, .. \tmtextit{length}
\tmtextit{h}\} the indices of a hidden vector.

\subsection{Probability of a state}

After training, the network assigns a probability to each possible state, as
follows:
\[ p \left( v, h \right) = \frac{1}{Z} e^{- E \left( v, h \right)} \]
where Z is used to normalise the probabilities
\[ Z = \sum_{v, h} e^{- E \left( v, h \right)} \]
Thus, the probability the network assigns for a visible vector \tmtextit{v}:
\begin{equation}
  p \left( v \right) = \sum_h \frac{1}{Z} e^{- E \left( v, h \right)}
\end{equation}

\subsection{Training the network}

The network can be trained such that one maximises the probability of a training
pattern. The derivative of the log probability of a training vector (given by
(1)), is simple, as follows:
\[ \frac{\delta \log p \left( v \right)}{\delta w_{\tmop{ij}}} = < v_i h_i
   >_{\tmop{data}} - < v_i h_i >_{\tmop{model}} \]
Due to the fact that the Restricted Boltzmann Machine can be represented as a bipartite graph, it is easy
to attain an \tmtextit{unbiased} sample of the hidden units, given the
visible units.
\begin{equation}
  p \left( h_j = 1 \left|  \right. v \right) = \sigma \left( b_j + \sum_{i \in
  V} v_i w_{\tmop{ij}} \right)
\end{equation}
For the visible units, the same formula gives a \tmtextit{biased} sample of
the visible units.
\begin{equation}
  p \left( v_i = 1 \left|  \right. h \right) = \sigma \left( a_i + \sum_{j \in
  H} h_j w_{\tmop{ij}} \right)
\end{equation}

where $\sigma = \frac{1}{1 \noplus + e^{- x}}^{}$, is the logistic sigmoid
function.

There are ways of getting an unbiased sample of the visible units, but they
are very time consuming. In practice, the contrastive divergence algorithm is
used as a faster substitute. The visible units are set to a training vector
and the binary states of the hidden vector are computed using (2). These
binary units are used to \tmtextit{reconstruct} the states of the visible
vector using (3).

The training rule then becomes:
\[ \Delta w_{\tmop{ij}} = \lambda \left( < v_i h_i >_{\tmop{data}} - < v_i h_i
   >_{\tmop{reconstruction}} \right) \]
We note that the above training rule has the desired properties for modelling
the biological brain: it is both \tmtextit{local} and \tmtextit{incremental}.
Angle brackets \ denote the expected value under the given distribution by the
following subscript.

\subsection{Using Restricted Boltzmann Machines for classification}

As suggested by Hinton in section 16 \cite{hinton2010practical}, there are various ways of using Restricted
Boltzmann Machines for classification. We have employed two methods, one which
we developed ourselves and another one described in the paper. The latter is adding
another set of visible units, called the \emph{classification units}.

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \begin{figure}[h]
  \ \ \ \ \ \ \ \ \ \ \centering
  \resizebox{350px}{350px}{\includegraphics{RBM-2.png}}
  \caption{Classification Boltzmann machine}
\end{figure}

The vector of hidden units is used to model the joint distribution between a
vector of inputs v and a target (classification) vector y. The classification
vector y corresponds to a class. It's length is given by the number of
classes. $y = e_c$, where c is the class the vector is modelling ($e_c$ is a
vector with all zeros, expect from the position c, where it is 1).

As seen from Figure 2, one now needs to weight matrices, which we will denote
by \tmtextit{W}(the weight matrix between the visible units and hidden ones) \
and U (the weight matrix and between the classification units and hidden
ones). Vectors \tmtextit{b}, \tmtextit{c}, \tmtextit{d} correspond to the
vector of biases for the visible units (training patterns), hidden units and
classification units, respectively.

The energy function for this model is:
\[ E \left( v \nocomma, y, h \right) = - d^T y - c^T h - b^T v - h^T W v - h^T
   U y \]
Which gives rise to the following formula for the probability of a configuration
\tmtextit{v}, \tmtextit{y}.
\[ p \left( v, y, h \right) = \frac{e^{- E \left( v \nocomma, y, h
   \right)}}{Z} \]
where \tmtextit{Z} is the normalising constant. Thus,
\[ p \left( v, y \right) = \sum_h p \left( v, y, h \right) = \sum_h \frac{e^{-
   E \left( v \nocomma, y, h \right)}}{Z} \]
The distribution used for the Classification Boltzmann Machine are the
following:
\begin{equation}
  p \left( h_i = 1 \left| v, y \right. \right) = \sigma \left( c_j + \noplus
  \noplus W_j v \noplus + U_j y \right)
\end{equation}
\begin{equation}
  p \left( v_i = 1 \left| h \right. \right) = \sigma \left( b_j + \noplus
  \noplus h^T W^i  \right)
\end{equation}
\begin{equation}
  p \left( y = e_c \left| h \right. \right) = \frac{e^{d^T e_c \noplus + h^T
  Ue_c}}{N}
\end{equation}
where N is the normalising constant $\sum_c$ $e^{d^T e_c \noplus + h^T Ue_c}$.

Note that we denote by $W_i$ the row \tmtextit{i} of matrix \tmtextit{W}, and
by $W^i_{}$ the column \tmtextit{i} of matrix \tmtextit{W}.

Valuable reference for this was given to us from
\cite{louradour2011classification} and
\cite{larochelle2008classification}.

\subsection{Learning}

Contrastive divergence can be used to train the network, giving rise to the
following update rules:

\begin{eqnarray*}
  b'  & = & b + \lambda \left( v - v_{\tmop{sampled}} \right)\\
  c'  & = & c + \lambda \left( h_{\sigma} - h_{\sigma \tmop{sampled}}
  \right)\\
  d'  & = & d + \lambda \left( y - y_{\tmop{sampled}} \right)\\
  W'  & = & W + \lambda \left( h_{\sigma} v^T - h_{\sigma \tmop{sampled}}
  v_{\tmop{sampled}}^T \right)\\
  U' & = & U + \lambda \left( \left. h_{\sigma} y^T - h_{\sigma
  \tmop{sampled}} y_{\tmop{sampled}}^T \right) \right.
\end{eqnarray*}
where we denote by $x'$ the new value of parameter x. $v_{\tmop{sampled}} $and
$y_{\tmop{sampled}} $are obtained by sampling the distributions in (5) and
(6).
\begin{eqnarray*}
  h_{\sigma} & = & \sigma \left( c_j + \noplus \noplus W_j v \noplus + U_j y
  \right)\\
  h_{\sigma \tmop{sampled}} & = & \sigma \left( c_j + \noplus \noplus W_j
  v_{\tmop{sampled}} \noplus + U_j y_{\tmop{sampled}}  \right)
\end{eqnarray*}
\subsection{Classification}
\[ p \left( y = e_c \left| v \right. \right) = \frac{e^{- F \left( v \nocomma,
   e_c \right)}}{\sum_d e^{- F \left( v \nocomma, e_d \right)}} \]
where $F \left( v \nocomma, e_c \right) $is the \tmtextit{free energy
function}
\[ F \left( v \nocomma, e_c \right) = - d^T y - \sum^H_{j = 1}
   \tmtextit{\tmtextit{s}} \left( c_j + \noplus \noplus W_j v \noplus + U_j y
   \right) \nocomma \]
where $s \left( x \right) = \log \left( 1 + e^x \right) $

The implementation of the Classification Boltzmann Machine can be found in

\tmtexttt{ClassficationBoltzmannMachine.hs. }

\subsection{New method}

Another method we have employed using Boltzmann Machines was created by us. We
have never seen this approach used somewhere else. Instead of creating 2 types
of visible units, we use the simple Restricted Boltzmann Machine, with one
type of visible units (and hence a single matrix). For each training vector
we append the classification at the end. The classification is represented as
a binary vector, either as above, by using $e_c$, where c is the
classification of the current pattern, or even in a more compressed manner, by
creating the binary vector using the representation in base 2 of class c.

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \begin{figure}[h]
  \ \ \ \ \ \ \ \ \ \ \ \ \
  \centering
  \resizebox{350px}{350px}{\includegraphics{RBM-3.png}}
  \caption{Boltzmann machines according to our new method}
\end{figure} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \

The training is done in the usual way, but with the complete vectors (actual
pattern and classification). In our case, as different patterns ought to have
different classifications, we use as class the index of the pattern in the list
(with removed duplicates) of training patterns.

When a pattern needs to be matched to one of the training patterns for
recognition, one uses the log probability to compute the
probability of each of the classifications, and chooses the one with maximum
probability.

\begin{figure}[h]
  \centering
  \resizebox{498px}{146px}{\includegraphics{RBM-4.png}}
  \caption{The recognition process in the new Boltzmann machine.}
\end{figure}

As given in \cite{hinton2010practical}, the log
probability is  given by:
\[ \tmtextit{\log} \left( \tmop{class} = c \left| v \right. \right) =
   \frac{e^{- F_c \left( v \right)}}{\sum_{c'} e^{- F_{c'} \left( v
   \right)}} \]
\[ F \left( v \right) = - \sum_{i \in V} v_i a_i - \sum_{j \in H} \log \left(
   1 \noplus + e^{x_j} \right) \]

where $x_j = b_j + \sum_i w_{ij_{}} v_j$

The implementation of this procedure can be found in
\tmtexttt{RestrictedBoltzmannMachine.hs.}


\documentclass{article}
\usepackage{tocloft}
\include{common_symbols_and_format}
\renewcommand{\cfttoctitlefont}{\Large\bfseries}
\begin{document}
\logo
\rulename{Exponential Trading Rule - One factor} %Argument is name of rule
\tblofcontents
\ruledescription{These representations have the position dependent on an exponentially weighted average of the prior values of research and price. The lookback length, $\lookbacklength$, determines how far back the rule looks. The decay rate, $\decaycoefficient$, and amplitude, $\amplitudecoefficient$, coefficients determine the contribution from each exponential. The expression is normalised to be dimensionless by division by the weighting factors and current price.}

\ruleparameters
{Window size}{10}{This is the number of time steps over which exponential contributions are sourced.}{$\lookbacklength$}
{Exponential decay rate for price}{0.0}{This is the decay factor for contributions from the price series.}{$\decaycoefficient^{\price}$}
{Exponential decay rate for research}{0.0}{This is the decay factor for contributions from the research series.}{$\decaycoefficient^{\research}$}
{Amplitude of price contribution}{-0.1}{This factor scales the overall contribution from past values of price.}{$\amplitudecoefficient^{\price}$}
{Amplitude of research contribution}{0.1}{This factor scales the overall contribution from past values of research.}{$\amplitudecoefficient^{\research}$}
\stoptable

\section{Equation}

\begin{equation}
\contribution_{\dummyiterator}^{\price} = e^{-\frac{\dummyiterator}{e^{-\decaycoefficient^\price}}} \\
\end{equation}
\begin{equation}
\contribution_{\dummyiterator}^{\research} = e^{-\frac{\dummyiterator}{e^{-\decaycoefficient^\research}}} \\
\end{equation}
\begin{equation}
\bigcontribution_\price = \amplitudecoefficient^\price \frac{\sum_{\dummyiterator}^{\lookbacklength} \price_{\currenttime - \dummyiterator} \lambda_{\dummyiterator}^{\price}}{\sum_{\dummyiterator = 0}^{\lookbacklength} \lambda_{\dummyiterator}^{\price}} \\
\end{equation}
\begin{equation}
\bigcontribution_\research = \amplitudecoefficient^\research \frac{\sum_{\dummyiterator}^{\lookbacklength} \research_{\currenttime - \dummyiterator} \lambda_{\dummyiterator}^{\research}}{\sum_{\dummyiterator = 0}^{\lookbacklength} \lambda_{\dummyiterator}^{\research}} \\
\end{equation}
\begin{equation}
\position_\currenttime = (\bigcontribution_\price+ \bigcontribution_\research)/\price_\currenttime \\
\end{equation}

\hspace{200mm}

\noindent where $\price_\currenttime$ is the price at time $\currenttime$, $\research$ is the value of the research series, $\amplitudecoefficient$ are the amplitude coefficients, $\decaycoefficient$ are the decay rate coefficients and $\position$ is the resultant fractional portfolio investment.

\hspace{200mm}
\hspace{200mm}

\keyterms
\furtherlinks

\end{document}