\chapter[Systems Dynamics]{Systems Dynamics}\label{chp:system_dynamics}

% Introduction
\chapterinitial{I}{n} many situations systems are dynamical, in that the state
or population of a number of entities or classes change according to the current
state or population of the system. For example population dynamics, chemical
reactions, and macroeconomic systems. It is often useful to be able to
predict how these systems will behave over time, though the rules that govern
these changes may be complex, and are not necessarily solvable analytically. In
these cases numerical methods may be needed, this is the focus
of this chapter.

\section{Problem}\label{sec:system_dynamics_problem}

Consider the following scenario, where a population of 3000 people are
susceptible to infection by some disease. This population can be described by
the following parameters:

\begin{itemize}
  \item They have a birth rate $b$ of 0.01 per day;
  \item they have a death rate $d$ of 0.01 per day;
  \item for every infectious individual, the infection rate $\alpha$ is 0.3 per
  day;
  \item infectious people recover naturally and gain an immunity from the
  disease, at a recovery rate $r$ of 0.02 per day;
  \item for each day an individual is infected, they must take medication which
  costs a public healthcare system $\pounds 10$ per day.
\end{itemize}

A vaccine is produced, that allows individuals to gain an immunity.
This vaccine costs the public health care system a one-off cost of
$\pounds 220$ per vaccine. The healthcare\index{healthcare} providers would like
to know if achieving a vaccination rate $v$ of $85\%$ would be beneficial
financially.

\section{Theory}\label{sec:system_dynamics_theory}

The above scenario can be expressed using a compartmental model of disease, and
can be represented in a stock and flow diagram\index{stock and flow diagram} as
in Figure~\ref{fig:stockflow}.

\begin{figure}
    \begin{center}
        \includestandalone[width=\textwidth]{./assets/stock_flow_diagram}
        \caption{Diagrammatic representation of the epidemiology model}
        \label{fig:stockflow}
    \end{center}
\end{figure}

The system has three quantities, or `stocks', of different types of individuals,
those susceptible to disease ($S$), those infected with the disease ($I$), and
those who have recovered from the disease and so have gained immunity
($R$)\footnote{This is often referred to as an SIR\index{SIR} model.}. The
levels on these stocks change according to the flows in, out, and between them,
controlled by `taps'. The amount of flow the taps let through are influenced in
a multiplicative way (either negatively or positively), by other factors, such
as external parameters (e.g. birth rate, infection rate) and the stock levels.

In this system the following taps exist, influenced by the following parameters:

\begin{itemize}
  \item \textit{external $\rightarrow S$:} influenced positively by the birth
  rate, and negatively by the vaccine rate;
  \item \textit{$S \rightarrow I$:} influenced positively by the infection rate,
  and the number of infected individuals;
  \item \textit{$S \rightarrow$ external:} influenced positively by the death
  rate;
  \item \textit{$I \rightarrow R$:} influenced positively by the recovery rate;
  \item \textit{$I \rightarrow$ external:} influenced positively by the death
  rate;
  \item \textit{$R \rightarrow$ external:} influenced positively by the birth
  rate and the vaccine rate;
  \item \textit{external $\rightarrow R$:} influenced positively by the death
  rate.
\end{itemize}

Mathematically the quantities or stocks are functions over time, and the change
in stock levels are written as the derivatives\index{derivative}, for example
the change in the number of susceptible individuals over time is denoted by
$\frac{dS}{dt}$. This is equal to the sum of the taps in or out of that stock.
Thus the system is described by the following system of differential
equations\index{differential equation}:

\begin{align}
\frac{dS}{dt} &= -\frac{\alpha SI}{N} + (1 - v)bN - dS \label{eqn:dS}\\
\frac{dI}{dt} &= \frac{\alpha SI}{N} - (r + d)I \label{eqn:dI}\\
\frac{dR}{dt} &= rI - dR + vbN \label{eqn:dR}
\end{align}

Where $N = S + I + R$ is the total number of individuals in the system.

The behaviour of the quantities $S$, $I$ and $R$ under these rules can be
quantified by solving this system of differential equations. This system
contains some non-linear terms, implying that this may be
difficult to solve analytically, so a numerical method instead will be used.

A number of potential numerical methods to do this exist.
The solvers that will be used in Python and R choose the most appropriate for
the problem at hand. In general methods for this kind of problems use the
principle that the derivative denotes the rate of instantaneous change. Thus for
a differential equation $\frac{dy}{dt} = f(t,y)$, consider the function $y$ as a
discrete sequence of points $\{y_0, y_1, y_2, y_3, \dots\}$ on
$\{t_0, t_0 + h, t_0 + 2h, t_0 + 3h, \dots\}$ then

\begin{equation}
y_{n+1} = h \times f(t_0 + nh, y_n).
\end{equation}

This sequence approaches the true solution $y$ as $h \rightarrow 0$.
Thus numerical methods, including the Runge-Kutta\index{Runge-Kutta} methods and
the Euler method\index{Euler method}\footnote{
    These methods are studied in the area of numerical analysis. A good textbook
    is~\cite{burden2001numerical}.
},
step through this sequence $\{y_n\}$, choosing appropriate values of $h$ and
employing other methods of error reduction.

\section{Solving with Python}\label{sec:system_dynamics_solving-with-python}

Here the \mintinline{python}{solve_ivp} method of the
SciPy\index{SciPy}~\cite{2020SciPy-NMeth} library will be used to numerically
solve the above models.

First the system of differential equations described in Equations~\ref{eqn:dS},
\ref{eqn:dI} and \ref{eqn:dR} must be defined. This is done using a
Python function, where the first two arguments are the current time and the
system state respectively.

\begin{pyin}
def derivatives(t, y, vaccine_rate, birth_rate=0.01):
    """Defines the system of differential equations that describe
    the epidemiology model.

    Args:
        t: a positive float
        y: a tuple of three integers
        vaccine_rate: a positive float <= 1
        birth_rate: a positive float <= 1

    Returns:
        A tuple containing dS, dI, and dR
    """
    infection_rate = 0.3
    recovery_rate = 0.02
    death_rate = 0.01
    S, I, R = y
    N = S + I + R
    dSdt = (
        -((infection_rate * S * I) / N)
        + ((1 - vaccine_rate) * birth_rate * N)
        - (death_rate * S)
    )
    dIdt = (
        ((infection_rate * S * I) / N)
        - (recovery_rate * I)
        - (death_rate * I)
    )
    dRdt = (
        (recovery_rate * I)
        - (death_rate * R)
        + (vaccine_rate * birth_rate * N)
    )
    return dSdt, dIdt, dRdt
\end{pyin}

Using this function returns the instantaneous rate of change for each of the
three quantities, $S$, $I$ and $R$. Starting at time 0.0, with 4 susceptible
individuals, 1 infected individual, 0 recovered individuals, and a vaccine rate
of 50\%, gives:

\begin{pyin}
print(derivatives(t=0.0, y=(4, 1, 0), vaccine_rate=0.5))
\end{pyin}

\begin{pyout}
(-0.255, 0.21, 0.045)
\end{pyout}

this means that the number of susceptible individuals is expected to reduce by
around 0.255 per time unit, the number of infected individuals to increase by
0.21 per time unit, and the number of recovered individuals to increase by 0.045
per time unit. After a tiny fraction of a time unit these quantities will
change, and thus the rates of change will change.

The following function observes the system's behaviour over some time period,
using SciPy's \mintinline{python}{solve_ivp} to numerically solve the system
of differential equations:

\begin{pyin}
from scipy.integrate import solve_ivp

def solve_ode(
    derivative_function,
    t_span,
    y0=(2999, 1, 0),
    vaccine_rate=0.85,
    birth_rate=0.01,
):
    """Numerically solve the system of differential equations.

    Args:
        derivative_function: a function returning a tuple
                             of three floats
        t_span: endpoints of the time range to integrate over
        y0: a tuple of three integers (default: (2999, 1, 0))
        vaccine_rate: a positive float <= 1 (default: 0.85)
        birth_rate: a positive float <= 1 (default: 0.01)

    Returns:
        A tuple of four arrays
    """
    sol = solve_ivp(
        derivative_function,
        t_span,
        y0,
        args=(vaccine_rate, birth_rate),
    )
    t, S, I, R = sol.t, sol.y[0], sol.y[1], sol.y[2]
    return t, S, I, R
\end{pyin}

This function can be used to investigate the difference in behaviour between a
vaccination rate of 0\% and a vaccination rate of 85\%. The system will now be
observed for two years, that is 730 days.

Begin with a vaccination rate of 0\%:

\begin{pyin}
from scipy.integrate import odeint

t_span = [0, 730]
t, S, I, R = solve_ode(derivatives, t_span, vaccine_rate=0.0)
\end{pyin}

Now \mintinline{python}{S}, \mintinline{python}{I} and \mintinline{python}{R}
are arrays of values of the stock levels of $S$, $I$ and $R$ over the time
steps \mintinline{python}{t}.
These can be plotted to visualise their behaviour, shown in
Figure~\ref{fig:plot_no_vaccine}.

The number of infected individuals increases quickly, and in fact the rate of
change increases as more individuals are infected. However this growth slows
down as there are fewer susceptible individuals to infect. Due to the equal
birth and death rates the overall population size remains constant; but after
some time period (around 200 time units) the levels of susceptible,
infected, and recovered individuals stabilise, and the disease becomes
endemic. Once this occurs, around 10\% of the population remain susceptible to
the disease, 30\% are infected, and 60\% are recovered and immune.

Now with a vaccine rate of 85\%:

\begin{pyin}
t, S, I, R = solve_ode(derivatives, t_span, vaccine_rate=0.85)
\end{pyin}

The corresponding plot is shown in Figure~\ref{fig:plot_with_vaccine}.

With vaccination the disease remains endemic, however once steadiness occurs,
around 10\% of the population remain susceptible to the disease, 1.7\% are
infected, and 88.3\% are immune or recovered and immune.

This shows that vaccination lowers the percentage of the population living with
the infection, which will lower the public healthcare system's costs. This
saving will now be compared to the cost of providing the vaccination to the
newborns.

The following function calculates the total cost to the public healthcare
system, that is the sum of the medication costs for those living with the
infection and the vaccination costs:

\begin{pyin}
def daily_cost(
    derivative_function=derivatives, vaccine_rate=0.85
):
    """Calculates the daily cost to the public health system after
    2 years.

    Args:
        derivative_function: a function returning a tuple
                             of three floats
        vaccine_rate: a positive float <= 1 (default: 0.85)

    Returns:
        the daily cost
    """
    birth_rate = 0.01
    vaccine_cost = 220
    medication_cost = 10
    t_span = [0, 730]
    t, S, I, R = solve_ode(
        derivatives,
        t_span,
        vaccine_rate=vaccine_rate,
        birth_rate=birth_rate,
    )
    N = S[-1] + I[-1] + R[-1]
    daily_vaccine_cost = (
        N * birth_rate * vaccine_rate * vaccine_cost
    )
    daily_meds_cost = I[-1] * medication_cost
    return daily_vaccine_cost + daily_meds_cost
\end{pyin}

Now the total daily cost with and without vaccination can be compared.
Without vaccinations:

\begin{pyin}
cost = daily_cost(vaccine_rate=0.0)
print(round(cost, 2))
\end{pyin}

which gives

\begin{pyout}
9002.33
\end{pyout}

Therefore without vaccinations, once the infection is endemic, the public health
care system would expect to spend $\pounds 9002.33$ a day.

With a vaccination rate of 85\%:

\begin{pyin}
cost = daily_cost(vaccine_rate=0.85)
print(round(cost, 2))
\end{pyin}

which gives

\begin{pyout}
6119.14
\end{pyout}

So vaccinating 85\% of the population would cost the public health care system,
once the infection is endemic $\pounds 6119.14$ a day. That is a saving of
around 32\%.

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{./assets/sd_vaccine_plots/plot_no_vaccine.pdf}
\end{center}
\caption{Stock levels without vaccination.}
\label{fig:plot_no_vaccine}
\end{figure}

\begin{figure}
\begin{center}
\includegraphics[width=\textwidth]{./assets/sd_vaccine_plots/plot_with_vaccine.pdf}
\end{center}
\caption{Stock levels with vaccination.}
\label{fig:plot_with_vaccine}
\end{figure}

\section{Solving with R}\label{sec:system_dynamics_solving-with-R}

The deSolve\index{deSolve}~\cite{soetaert2010solving} library will be used to numerically
solve the above epidemiology models.

First the system of differential equations described in Equations~\ref{eqn:dS},
\ref{eqn:dI} and \ref{eqn:dR} must be defined. This is done using an R function,
where the arguments are the current time, system state and a list of other
parameters.

\begin{Rin}
#' Defines the system of differential equations that describe
#' the epidemiology model.
#'
#' @param t a positive float
#' @param y a tuple of three integers
#' @param vaccine_rate a positive float <= 1
#' @param birth_rate a positive float <= 1
#'
#' @return a list containing dS, dI, and dR
derivatives <- function(t, y, parameters){
  infection_rate <- 0.3
  recovery_rate <- 0.02
  death_rate <- 0.01
  with(
    as.list(c(y, parameters)), {
      N <- S + I + R
      dSdt <- ( - ( (infection_rate * S * I) / N)  # nolint
        + ( (1 - vaccine_rate) * birth_rate * N)
        - (death_rate * S))
      dIdt <- ( ( (infection_rate * S * I) / N)  # nolint
        - (recovery_rate * I)
        - (death_rate * I))
      dRdt <- ( (recovery_rate * I)  # nolint
        - (death_rate * R)
        + (vaccine_rate * birth_rate * N))
      list(c(dSdt, dIdt, dRdt))  # nolint
    }
  )
}
\end{Rin}

This function returns the instantaneous rate of change for each of the
three quantities $S$, $I$ and $R$. Starting at time 0.0, with 4 susceptible
individuals, 1 infected individual, 0 recovered individuals, a vaccine rate
of 50\% and a birth rate of 0.01, gives:

\begin{Rin}
ders <- derivatives(
  t = 0,
  y = c(S = 4, I = 1, R = 0),
  parameters = c(vaccine_rate = 0.5, birth_rate = 0.01)
)
print(ders)
\end{Rin}

\begin{Rout}
[[1]]
[1] -0.255  0.210  0.045

\end{Rout}

The number of susceptible individuals is expected to reduce by around 0.255 per
time unit, the number of infected individuals to increase by 0.21 per time unit,
and the number of recovered individuals to increase by 0.045 per time unit.
After a tiny fraction of a time unit these quantities will change, and thus the
rates of change will change.

The following function observes the system's behaviour over some time period,
using the deSolve library to numerically solve the system of
differential equations:

\begin{Rin}
library(deSolve)  # nolint

#' Numerically solve the system of differential equations
#'
#' @param times an array of increasing positive floats
#' @param y0 list of integers (default: c(S=2999, I=1, R=0))
#' @param birth_rate a positive float <= 1 (default: 0.01)
#' @param vaccine_rate a positive float <= 1 (default: 0.85)
#'
#' @return a matrix of times, S, I and R values
solve_ode <- function(times,
                      y0 = c(S = 2999, I = 1, R = 0),
                      birth_rate = 0.01,
                      vaccine_rate = 0.85){
  params <- c(
    birth_rate = birth_rate,
    vaccine_rate = vaccine_rate
  )
  ode(
    y = y0,
    times = times,
    func = derivatives,
    parms = params
  )
}
\end{Rin}

This function can be used to investigate the difference in behaviour between
a vaccination rate of 0\% and a vaccination rate of 85\%. The system will be
observed for two years, that is 730 days, in time steps of 0.01 days.

Begin with a vaccination rate of 0\%:

\begin{Rin}
times <- seq(0, 730, by = 0.01)
out <- solve_ode(times, vaccine_rate = 0.0)
\end{Rin}

Now \mintinline{R}{out}, is a matrix with four columns, \mintinline{R}{time},
\mintinline{R}{S}, \mintinline{R}{I} and \mintinline{R}{R}, which are arrays of
values of the time points, and the stock levels of $S$, $I$ and $R$ over the
time respectively.
These can be plotted to visualise their behaviour, shown in
Figure~\ref{fig:plot_no_vaccine}.\footnote{The particular figures shown in
Figures~\ref{fig:plot_no_vaccine} and~\ref{fig:plot_with_vaccine} were created
using Python and the Matplotlib\index{Matplotlib}
library~\cite{hunter2007matplotlib}. R has powerful plotting tools with packages
such as ggplot2\index{ggplot2}~\cite{wickham2016ggplot2}.}

The number of infected individuals increases quickly, and in fact the rate of
change increases as more individuals are infected. However this growth slows
down as there are fewer susceptible individuals to infect. Due to the equal
birth and death rates the overall population size remains constant; but after
some time period (around 200 time units) the levels of susceptible,
infected, and recovered individuals stabilises, and the disease becomes endemic.
Once this steadiness occurs, around 10\% of the population remain susceptible to
the disease, 30\% are infected, and 60\% are recovered and immune.

Now with a vaccination rate of 85\%:

\begin{Rin}
times <- seq(0, 730, by = 0.01)
out <- solve_ode(times, vaccine_rate = 0.85)
\end{Rin}

The corresponding plot is shown in Figure~\ref{fig:plot_with_vaccine}.

With vaccination the disease remains endemic, however once steadiness occurs,
around 10\% of the population remain susceptible to the disease, 1.7\% are
infected, and 88.3\% are immune or recovered and immune.

This shows that vaccination lowers the percentage of the population living with
the infection, which will lower the public healthcare system's costs.
This saving will now be compared to the cost of providing the vaccination to the
newborns.

The following function calculates the total cost to the public healthcare
system, that is the sum of the medication costs for those living with the
infection and the vaccination costs:

\begin{Rin}
#' Calculates the daily cost to the public health
#' system after 2 years
#'
#' @param derivative_function: a function returning a
#'                             list of three floats
#' @param vaccine_rate: a positive float <= 1 (default: 0.85)
#'
#' @return the daily cost
daily_cost <- function(derivative_function = derivatives,
                       vaccine_rate = 0.85){
  max_time <- 730
  time_step <- 0.01
  birth_rate <- 0.01
  vaccine_cost <- 220
  medication_cost <- 10
  times <- seq(0, max_time, by = time_step)
  out <- solve_ode(times, vaccine_rate = vaccine_rate)
  N <- sum(tail(out[, c("S", "I", "R")], n = 1))
  daily_vaccine_cost <- (
    N * birth_rate * vaccine_rate * vaccine_cost
  )
  daily_medication_cost <- (
    tail(out[, "I"], n = 1) * medication_cost
  )
  daily_vaccine_cost + daily_medication_cost
}
\end{Rin}

The total daily cost with and without vaccination will now be compared. Without
vaccinations:

\begin{Rin}
cost <- daily_cost(vaccine_rate = 0.0)
print(cost)
\end{Rin}

which gives

\begin{Rout}
[1] 9000
\end{Rout}

Therefore without vaccinations, once the infection is endemic, the public health
care system would expect to spend $\pounds 9000$ a day.

With a vaccination rate of 85\%:

\begin{Rin}
cost <- daily_cost(vaccine_rate = 0.85)
print(cost)
\end{Rin}

which gives

\begin{Rout}
[1] 6119.034
\end{Rout}

So vaccinating 85\% of newborns would cost the public healthcare system, once
the infection is endemic $\pounds 6119.04$ a day. That is a saving of around
32\%.

\section{Wider context}\label{sec:system_dynamics_wider_context}

System dynamics is an applied aspect of the more general mathematical field of
dynamical systems\index{dynamical systems}. \cite{robinson1998dynamical} gives a
mathematical overview of the theory of dynamical systems and~\cite{burden2001numerical}
is a good text on the numerical algorithms used to be able to observe the
behaviour of these. For an overview of the type of
application covered in this chapter see~\cite{martin2006theory}.

In the field of operational research\index{operational research}, Jay
Forrester\index{Jay Forrester} is recognised as the first
person to use dynamical systems in the way shown in this chapter.
His own account can be read in~\cite{forrester1995beginning}. From Forrester's
initial application to industry in 1961~\cite{forrester1961industrial} dynamical
systems continue to be of use today in a wide range of areas.
In~\cite{coyle1999system} a case study of using dynamical systems for relevant
modelling for the navy is described.
\cite{vazquez2020system} gives a literature review of the application area
of healthcare\index{healthcare}. For example, \cite{cooper2020sir} applies the
same model from this chapter to the study of the COVID\index{COVID} pandemic.

In order to be able to fully capture all the relevant details of the system to be
modelled, an extension of system dynamics is to combine the methodology with
discrete event simulation\index{discrete event simulation} (see
Chapter~\ref{chp:discrete_event_simulation}) in order to model discrete aspects
of the system and/or agent based\index{agent based} modelling (see
Chapter~\ref{chp:agent_based_simulation}) in order to observe emergent or learned
behaviours. This is known as hybrid simulation\index{hybrid simulation}, and an
overview is given in~\cite{brailsford2019hybrid}.


% TITLE: Lipsum text with two figures and math
% AUTHOR: Adrian Schrader
% Created on: 31/7/15

\section{Figures}
\subsection{Column wide figure}
\lipsum[1]
\lipsum*[2]\footnote{This is an unnecessary footnote. Thanks!}

\begin{figure}[h]
  \centering
  \includegraphics[width=\linewidth]{img/plot}
  \caption{Plot of of $ f(t) $ in Maple}~\label{img:function}
\end{figure}

\lipsum[3]
\lipsum[4]

\subsection{Page wide figure}
\lipsum[5]

\begin{figure*}[ht]
  \centering
  \includegraphics[width=\linewidth]{img/example2}
  \caption{Another Random cat picture from Google \protect\url{https://static.pexels.com/photos/4067/animal-pet-cute-cat.jpg}}~\label{img:cat2}
\end{figure*}

\section{Typesetting}
\lipsum[6]

\subsection{Formula and Equations}
\begin{align}
    f(t) &= s_0 \cdot e^{-\frac{t}{\tau}} \cdot \cos(\omega_s t) \cdot \Theta(t) \nonumber \\
    &= s_0 \cdot e^{-\frac{t}{\tau}} \cdot \frac{1}{2} \Big( e^{i \omega_s t} + e^{-i \omega_s t} \Big) \cdot \Theta(t)\\
    \tilde{f}(\omega) &= \mathcal{F}(f)(\omega) = \frac{1}{\sqrt{2\pi}} \int_{-\infty}^{\infty} f(t) \cdot e^{-i \omega t} dt \nonumber \\
    &= \frac{s_0}{\sqrt{2\pi}} \frac{\frac{1}{\tau} + i \omega}{ {( \frac{1}{\tau} + i \omega )}^2 + \omega_s^2}
\end{align}

\subsection{Source code}
\begin{listing}
  \begin{minted}{java}
    public static void main(String[] args) {
      return;
    }
  \end{minted}
  \caption{Standard main method of every java program}~\label{lst:javamain}
\end{listing}

\lipsum[9-14]
\lipsum[2]~\cite{book:handbook}


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{~/School-Work/Auxiliary-Files/resources/png/logo.png}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

\begin{document}

% \maketitle

% Notes taken on 05/10/21

\chapter{Galois Theory}
\label{cha:galois_theory}

Galois theory studies the connection between finite field extensions via roots of polynomials and the structures of groups that permute those roots.\\

Let \(F,K\) be fields, and \(K / F\) a field extension.

\begin{defn}[Field Automorphism]
	We say that \(\sigma :K\to K\) is a \textbf{field automorphism} if \(\sigma \) is a bijective unital ring homomorphism. We denote the collection of field automorphisms of \(K\) by \(\textrm{Aut}(K)\).\\

	An automorphism \(\sigma  \in \textrm{Aut}(K)\) \textbf{fixes an element} \(\alpha  \in K\) if \(\sigma (\alpha )=\alpha \).\\

	An automorphism \(\sigma \in \textrm{Aut}(K)\) \textbf{fixes a subset \(E\) of \(K\)} if \(\sigma (\alpha ) = \alpha \) for all \(\alpha  \in E\).\\

	For \(\sigma  \in \textrm{Aut}(K)\) and \(E\subset K\), \(\sigma (E)\) denotes the subset \(\left\{\sigma (\alpha ) \mid \alpha \in E \right\} \)
\end{defn}
Recall that the prime subfield of a field \(K\) is given by
\begin{align*}
	K_{\textrm{prime}} = \begin{cases}
		\Q & K \text{ has characteristic 0}\\
		\Z_p & \text{\(p\) prime}
	\end{cases}
\end{align*}
because \(\sigma \in \textrm{Aut}(K)\) fixes \(1_K\), it must hold that \(\sigma \) fixes \(K_{\textrm{prime}}\) and hence prime subfields are fixed by any automorphism of a field.

\section{Automorphisms fixing subfields}
\label{sec:automorphisms_fixing_subfields}

\begin{defn}
	We define \(\textrm{Aut}(K / F)\) to be the collection of automorphisms of \(K\) that fix \(F\).
\end{defn}

\begin{prop}
	\(\textrm{Aut}(K)\) is a group under composition, and \(\textrm{Aut}(K / F)\) is a subgroup of \(\textrm{Aut}(K)\).
\end{prop}

\begin{prop}
	Let \(\alpha  \in K\) be an algebraic element over \(F\). Then for any \(\alpha \in \textrm{Aut}(K / F)\), we get that \(m_{\alpha ,F}(\sigma (\alpha )) = 0\).
\end{prop}
In other words, automorphisms permute roots of minimal polynomials.

\section{Subfields and Subgroups}
\label{sec:subfields_and_subgroups}

\begin{prop}
	Let \(H\) be a subgroup of \(\textrm{Aut}(K)\). Then
	\begin{align*}
		\left\{\alpha  \in K \mid \sigma (\alpha ) = \alpha \quad \forall \sigma \in H \right\}
	\end{align*}
	is a subfield of \(K\). We call this subfield the \textbf{fixed field of \(H\)} denoted by \(K^{H}\).
\end{prop}

In fact, this structure induces a correspondence between field extensions and chains of subgroups.
\begin{prop}
	Let \(F_1 \subset F_2 \subset K\) be a sequence of field extensions. Then \(\textrm{Aut}(K / K) = \textrm{Id}_{\textrm{Aut}(K)} \leq \textrm{Aut}(K / F_2) \leq \textrm{Aut}(K / F_1)\).\\

	Conversely, let \(H_1 \leq H_2 \leq \textrm{Aut}(K)\) be a chain of subgroups. Then \(K^{\textrm{Aut}(K)} = K_{\textrm{prime}} \subset K^{H_2} \subset K^{H_1}\)
\end{prop}
\end{document}

Abstract Algebra - Introductory/Algebra II/Notes/source/Lecture25 - AutoGroups_FixedFields.tex


% !TeX root = ../thesis.tex
% !TeX spellcheck = en_GB
% !TeX encoding = UTF-8

A path-dependent option is a type of exotic option in which the payoff depends not only on the price of the underlying asset at maturity, but also on the history of the underlying's price till that point. Typical examples of popular exotic options are Asian options, lookback options, barrier options, and digital options.

In the Black-Scholes market model, it is not possible to find closed-form analytical formulae for the payoffs of most exotic options. This inspires the requirement of fast numerical algorithms to determine the fair value of such options. Such algorithms may be clustered into categories. Some algorithms utilise the convergence of prices calculated using a discrete model to those of a continuous model, as a limit when the time step is reduced. Other approaches include use of numerical methods to solve partial differential equation, or simulation using Monte Carlo methods.

Under the Cox-Ross-Rubinstein model, Gaudenzi\footnote{\url{http://people.uniud.it/page/marcellino.gaudenzi}}, Zanette\footnote{\url{http://people.uniud.it/page/antonino.zanette}} and Lepellere\footnote{\url{https://www.researchgate.net/profile/Maria_Lepellere}} introduced the singular points method. It is a numerical method to price Asian and lookback options. A modification enables pricing of cliquet options. In the method, in each node of the binomial tree of the underlying risky asset, the price is represented as a continuous function of the path-dependent parameter. An advantage of this method over pre-existing methods is its low order of computational and space complexity. It is convergent, and allows us to set \emph{a priori} upper and lower bounds on the error.

In the master's thesis, we present an exposition on the \emph{singular points} method and how it may be used to price exotic options. We also explore the extensibility of the method to similar types of options, like Asian options with geometric mean as opposed to arithmetic mean, and whether it may be used for variable local volatilities and interest rates. We also found the computational order of complexity of the method in the case of cliquet options.

\paragraph{Prerequisites}
The reader is expected to be familiar about basic Probability Theory and Stochastic Processes. In particular, (s)he should be comfortable with probability spaces and measures, filtrations, random variables, stochastic processes, martingales, Brownian motion, and elementary stochastic calculus. There is no strict requirement of knowledge of financial concepts, as we introduce the required terminologies in the introductory chapters.

\paragraph{Note to the reader}
The theory part of the thesis borrows heavily from the book titled Introduction to Stochastic Calculus Applied to Finance by Damien Lamberton and Bernard Lapeyre \cite[]{Lamberton1996}, as well as from the lecture notes of Mathematical Finance authored by Prof. Fabio Antonelli. This course was offered in the Fall semester of 2014-15 in Università degli Studi dell'Aquila, Italy.

The chapters on the singular point method has been motivated by the series of papers published by Gaudenzi, Zanette and Lepellere on the same topic.

\paragraph{Structure of the thesis}
In Chapter \ref{cha:prologue}, we briefly discuss financial assets and financial markets. In Chapter \ref{cha:models}, we introduce the Cox-Ross-Rubinstein model and the Black-Scholes model. In chapters \ref{cha:asian} and \ref{cha:cliquet}, we see how the singular points method may be used to price Asian and cliquet options, respectively.

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


\section{Defense strategy}
\begin{frame}
    \frametitle{Initial idea}
    We can 'concentrate' the mark in parts that are better at hiding it (like wavelet embedding)
\end{frame}

\subsection{Embedding}
\begin{frame}
    \frametitle{Embedding steps}
    Given a mark of size $N$:
    \begin{enumerate}
        \item<1-> Divide the image into M sub-blocks
        \item<2-> Find $M$ blocks with the highest variance ("candidate blocks")
        \item<3-> Embed $N/M$ parts of the mark into each candidate block, where each new coefficient of the watermarked image is defined as: %$1 + (\alpha \cdot \text{mark}[\text{mark_index}])$
              \[x_{ij}^* = x_{ij}[1 + (\alpha m_i)]\]
    \end{enumerate}

    \begin{figure}[h]
        \centering
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_4_def.jpg}} \quad
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_8_def.jpg}} \quad
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_16_def.jpg}}
        \caption{Example of candidate blocks in the 'rollercoaster' image, with different block sizes}
        \label{fig:blocks_rollecoaster}
    \end{figure}
\end{frame}

\begin{frame}
    \frametitle{The \texttt{texture\_areas} function}
    The \texttt{texture\_areas} function is called by both the embedding and detection functions and provides them with a list of ordered blocks, in which the watermark will be inserted or detected.

    \medskip

    \onslide<2->{
        The function computes the variance of the image and then divides it into $n \times n$ blocks. Each block's variance is computed and then tested against the image's. If it is greater, then the block is added to the list.
    }

    \medskip
    \onslide<3->{
        Since some images may have an unsatisfactory number of high-variance blocks (e.g. very flat images with small contrast areas), the function will recursively call itself, lowering the variance theshold each time until either it runs out of iterations or finds enough blocks.
    }
\end{frame}

\begin{frame}
    \frametitle{The \texttt{texture\_areas} function}
    The function uses a threshold based on the \texttt{sqrt} of the variance in order to decide when to stop searching for blocks. Its choice is purely experimental: we tried with different indicators but found the square root the most flexible.

    \medskip

    \onslide<2->{
        Its great flexibility means it can easily be adapted to the situation by fastening or slowing the search, changing the block size, and so on.
    }

    \medskip

    \onslide<3->{
        Finally, found blocks are sorted by variance and sent back to the caller.
    }
\end{frame}

\begin{frame}
    \frametitle{The \texttt{texture\_areas} function}

    In short, the parameters are:
    \begin{itemize}
        \item<1-> \texttt{block\_size}: the size of the searched blocks.
        \item<2-> \texttt{coeff}: a float that divides the variance (and since it's usually $> 0$, it reduces it) when the algorithm decides whether or not to choose a block
        \item<3-> \texttt{max\_iteration}: maximum recursion depth
        \item<4-> \texttt{var\_multiplier, coeff\_step}: at the recursion point, the algorithm does the following:

              \texttt{if len(result) < var\_multiplier * math.sqrt(image\_variance):}\\
              \texttt{\hphantom{hhhhh}texture\_areas(image, block\_size,}\\
                  \texttt{\hphantom{hhhhhhhhhhhhhhhhhhh}coeff / var\_multiplier,} \texttt{\hphantom{hhhhhhhhhhhhhhhhhhh}result, it=it + 1)}

    \end{itemize}
\end{frame}

\begin{frame}
    \frametitle{The \texttt{texture\_areas} function, changing parameters}
    \begin{figure}[h]
        \centering
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_8_def.jpg}} \quad
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_8_2coeff_1var.jpg}} \quad
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_8_4coeff_1var.jpg}}
        \caption{Example of candidate blocks in the 'rollercoaster' image, when the $coeff$ coefficient is changed}
        \label{fig:blocks_rollecoaster_2}
    \end{figure}
\end{frame}

\begin{frame}
    \frametitle{The \texttt{texture\_areas} function, changing parameters}
    \begin{figure}[h]
        \centering
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_8_def.jpg}} \quad
        \subfloat{\includegraphics[width=.25\textwidth]{../drawable/rollercoaster-examples/rollercoaster_8_40var.jpg}}
        \caption{Example of candidate blocks in the 'rollercoaster' image, when the $var_coeff$ coefficient is changed}
        \label{fig:blocks_rollecoaster_3}
    \end{figure}
\end{frame}

\subsection{Detection}

\begin{frame}
    \frametitle{Detection steps}
    \begin{enumerate}
        \item<1-> Find candidate blocks using the original image  $\Rightarrow$ this is possible because detection is non-blind.
        \item<2-> Extract mark from candidate blocks
        \item<3-> Reassemble mark
              \begin{figure}[h]
                  \centering
                  \includegraphics[width=0.85\textwidth]{../drawable/extracting-blocks.png}
                  \label{fig:mark_extraction}
              \end{figure}
    \end{enumerate}
\end{frame}

\begin{frame}
    \frametitle{Strength and weaknesses}
    Strengths:

    \begin{itemize}
        \item Strong parametrization and flexibility
        \item Fast (statistically insignificant slowdowns compared to standard SS watermarking)
        \item Easily extensible by changing employed metrics
        \item At lower $\alpha$, very high WPSNR and undetectable by the naked eye
    \end{itemize}
    \onslide<2->{
        Weaknesses:

        \begin{itemize}
            \item Extremely dependent on the image features
            \item Embedded blocks locations are deterministic and predictable to the attacker
            \item \textbf{Underflow/overflow problems} when converting from DCT domain to pixel domain
        \end{itemize}
    }
\end{frame}

\begin{frame}
    \frametitle{One-attack test results}

    \only<1>{
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{../drawable/classic-attacks-plots/awgn-alpha.png}
            \caption{Plot of $\alpha$, WPSNR and AWGN attacks (random seed). Red dots are successful attacks.}
        \end{figure}
    }

    \only<2>{
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{../drawable/classic-attacks-plots/blur-alpha.png}
            \caption{Plot of $\alpha$, WPSNR and blur attacks. Red dots are successful attacks.}
        \end{figure}
    }

    \only<3>{
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{../drawable/classic-attacks-plots/jpeg-alpha.png}
            \caption{Plot of $\alpha$, WPSNR and JPEG attacks. Red dots are successful attacks.}
        \end{figure}
    }

    \only<4>{
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{../drawable/classic-attacks-plots/median-alpha.png}
            \caption{Plot of $\alpha$, WPSNR and median attacks (square sliding window). Red dots are successful attacks.}
        \end{figure}
    }

    \only<5>{
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{../drawable/classic-attacks-plots/resize-alpha.png}
            \caption{Plot of $\alpha$, WPSNR and resize attacks. Red dots are successful attacks.}
        \end{figure}
    }

    \only<6>{
        \begin{figure}
            \centering
            \includegraphics[width=0.5\textwidth]{../drawable/classic-attacks-plots/sharpen-alpha.png}
            \caption{Plot of $\alpha$, WPSNR and sharpen attacks (fixed sigma). Red dots are successful attacks.}
        \end{figure}
    }

\end{frame}

\subsection{Issues}
\begin{frame}
    \frametitle{Underflow/overflow problems}

    The underflows and overflows destroys parts of the mark during the embedding phase, resulting in information loss.

    \medskip

    \onslide<2->{
        A multitude of factors contributed to this game-losing, still-open issue:

        \medskip

        \begin{itemize}
            \item<3-> Bad typecasting in both embedding and detection caused conversion errors
            \item<4-> Editing a large number of coefficients in a small DCT and re-converting caused pixels to go negative or over 255

        \end{itemize}
    }

    \medskip

    \onslide<5->{
        During the re-conversion, the offending bits flipped and created white and dark patches in the image, razing the WPSNR and ruining the detection by up to 50/60\%.
    }
\end{frame}

\begin{frame}
    \frametitle{Underflow/overflow problems}

    We did not manage to fully fix the problem in time, and had to resort to approximations and a reduction in the $\alpha$ range for the day of the competition.

    \medskip

    \onslide<2->{
        Only a small subset $[ 0.4, 0.25 ]$ of the $\alpha$ managed to recover 100\% of the watermark without causing underflow/overflow problems. A range too low, that left us vulnerable to localized and resizing attacks.
    }

    \medskip

    \onslide<3->{
        With enough time, the problem could have been fixed by choosing a different DCT/pixel approach (e.g. working in the $-1/+1$ domain instead of the $0/255$ one)
    }

\end{frame}

\begin{frame}
    \frametitle{Possible improvements}

    What could be fixed or improved?

    \begin{itemize}
        \item<2-> Add heuristics to \texttt{texture\_areas} (e.g. randomizing the search, with a seed based on the hash of the image)
        \item<3-> Increase the block size $\Rightarrow 16 \times 16$
        \item<4-> Use machine learning to identify texture areas instead of metric-based deterministic function: CNN are great at finding patterns in images
        \item<5-> Employ smarter ways to optimize parameters instead of brute force
              % Altro?
    \end{itemize}
\end{frame}

\section{An aside: Wavelet embedding}

\begin{frame}
    \frametitle{Wavelet embedding}
    \onslide<1->{
        We experimented with wavelet embedding \footnote{Improved Wavelet-Based Watermarking Through Pixel-Wise Masking}.
        The embedding changes the coefficients on the HH and HL sub-band of the wavelets, where each coefficient gets the value:
        \[x_{ij}^* = x_{ij} + \alpha m_i\]
    }

    \onslide<2->{
        The extraction function is just the inverse of the embedding function.
        %ex_mark_value = (watermark_subband[r][c] - original_subband[r][c]) / alpha
        \[m_i = (x_{ij}^* - x_{ij}) / (x_{ij} \alpha ) \]
    }
\end{frame}

\begin{frame}
    \frametitle{Embedding and detection}

    \onslide<1>{
        Both procedures are quite simple.
    }

    \medskip

    \onslide<2->{
        For the embedding:
        \begin{enumerate}
            \item Extract DWT from image
            \item Embed contents of the mark into HL and HH subbands of the image
            \item Re-convert the image from DWT to pixel domain
        \end{enumerate}
    }

    \medskip

    \onslide<3->{
        For the detection:
        \begin{enumerate}
            \item Bring image into DWT domain
            \item Use inverse of embedding function to extract the mark components
        \end{enumerate}
    }

\end{frame}

\begin{frame}
    \frametitle{Mark visibility}
    Due to the simple implementation, the performance is not very good. The mark components are not weighted on the image contents, thus the mark is quite visible even with low $\alpha$ values (note "sawtooth pattern" on oblique lines).

    \medskip

    \begin{figure}[h]
        \centering
        \subfloat[]{\includegraphics[width=0.34\textwidth]{../drawable/wavelets/original-detail.jpg}}  \quad
        \subfloat[]{\includegraphics[width=0.34\textwidth]{../drawable/wavelets/wavelets-embedding-detail.jpg}}
        \caption{Detail of original (a) and watermarked image (b)}
        \label{fig:wavelet-watermark}
    \end{figure}
\end{frame}

\begin{frame}
    \frametitle{Performance}
    As expected by the simple embedding function, the defense against attacks is quite poor:
    \begin{figure}[h]
        \centering
        \subfloat[]{\includegraphics[width=0.34\textwidth]{../drawable/wavelets-attacks-plots/blur-alpha.png}}  \quad
        \subfloat[]{\includegraphics[width=0.34\textwidth]{../drawable/wavelets-attacks-plots/resize-alpha.png}}
        \caption{Plots of relation between WPSNR and attacks intensity}
        \label{fig:wavelet-performance-plots}
    \end{figure}
\end{frame}

\begin{frame}
    \frametitle{Conclusions}
    \onslide<1->{
        \begin{itemize}
            \item<1-> The mark is embedded with the same intensity on all components of the image, so is quite vulnerable to attacks and is visible even with low alphas
            \item<2-> Since we were short on time, we dropped this approach and kept the block-based embedding
        \end{itemize}
    }
\end{frame}

\section{Attack strategy}

\begin{frame}
    \frametitle{Attack strategy}
    We used a in-house interactive CLI program that mapped each \texttt{pyc} file to the each group image and allowed automated chain attacks.

    \begin{figure}
        \centering
        \includegraphics[width=\textwidth]{../drawable/attacker.png}
        \caption{A snippet of the attacker tool.}
    \end{figure}

\end{frame}

\begin{frame}
    \frametitle{Attack strategy}

    Some groups had very resistant watermarking, yet detectable by the eye.

    \onslide<2->{

        \begin{figure}[h]
            \centering
            \subfloat{\includegraphics[width=.243\textwidth]{../drawable/bad1.png}} \quad
            \subfloat{\includegraphics[width=.25\textwidth]{../drawable/bad2.png}}
            \caption{Example of very visible watermark implementations by groups \st{redacted} and \st{redacted}}
            \label{fig:blocks_rollecoaster_mark_visible}
        \end{figure}
    }

    \onslide<3->{
        These edge cases allowed us to narrow down possible attacks and focus on those who better affected that particular image (e.g. \texttt{median} for the right picture)
    }

\end{frame}

\begin{frame}
    \frametitle{Attack strategy}

    We managed to attack all groups in a rather short amount time, and this allowed to focus us on more resistant ones, in which we manually chained attacks and observed the behaviour in real-time in order to correct the strategy.

    \medskip

    \onslide<2-> {
        Interestingly enough, a number of pictures were easily destroyable with minimal amounts of resizing attacks (sometimes even scaling it by just 95\%)!
    }

    \medskip

    \onslide<3->{
        For some of the images maybe localized attacks were ideal: we did try them but with little to no success.
    }
\end{frame}

\section{Results}
\begin{frame}
    \frametitle{Results}
    \begin{itemize}
        \item \textbf{Invisibility}: visual quality of the image was neither excellent nor horrible; the final WPSNR achieve was in the $[50,60]$ range for all images
        \item<2-> \textbf{Activity}: we managed to successfully attack all groups, albeit with mediocre results
        \item<3-> \textbf{Robustness}: underflow/overflow problems were a pain in the neck, making us suffer on this side
        \item<4-> \textbf{Attack quality}: neither excellent nor horrible; some of the techniques were pretty easy to crack, while others were pretty hard
    \end{itemize}
\end{frame}


\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\renewcommand\qedsymbol{$\blacksquare$}
\newtheorem{theorem}{Theorem}

\newcommand{\where}{\enspace \mathrm{where} \enspace}
\newcommand{\hdo}{\enspace \mathrm{do} \enspace}
\newcommand{\hreturn}{\enspace \mathrm{return} \enspace}
\newcommand{\hif}{\enspace \mathrm{if} \enspace}
\newcommand{\hthen}{\enspace \mathrm{then} \enspace}
\newcommand{\helse}{\enspace \mathrm{else} \enspace}

\begin{document}

\title{Matroids in Type Theory}
\author{Athan Clark\\ Copyright \copyright \enspace The Grid, 2015}

\maketitle

\begin{abstract}
  Matroids are a great tool for optimization - say we have a set of elements, and
  some function to measure each element. If we want to maximize the \textit{total}
  measure of a subset of the input, we can do so quickly with matroids. The principal
  is simple - if we first have all paths to potential solutions at hand (the matroid itself),
  and a function to take elements of our set and give us some unit that we can compare
  each other against (for instance, the \(Ord\) type class in Haskell), then we can
  find the subset with a maximum total very quickly.

  Here we assert the properties and definition of matroids, to better formalize and
  demonstrate their utility.
\end{abstract}

\section{Overview}

A Matroid can be seen as the "road map'' to every possible solution.
The potential solutions are based on an input set, and the
set of all potential solutions are the roads. Formally, a matroid consists of
a \textbf{ground} set \(E\) (the input), and a "family'' \(I\) of \textbf{independent}
subsets of \(E\) (the roads):

\[
             E : Set \enspace \delta
  , \enspace I : Set \enspace (Set \enspace \delta) \where I \subseteq \rho E
\]

for some element type \(\delta\).
\(I\) is a subset of the power set of \(E\); \(I\) is a set of subsets of \(E\).

\section{Properties}

For \(I\) to be seen as the routes we can take to a solution, we need some closure
properties. An independence system supports the potential routes from an empty set
to a solution, and the augmentation property lets us grow from a smaller solution
to a larger one:

\begin{flalign}
  \forall i \in I. &\enspace i \subseteq E \label{matroid-subset} \tag{MATROID-SUBSET}\\
  \forall i \in I. &\enspace \rho i \subseteq I \label{matroid-hereditary} \tag{MATROID-HEREDITARY}\\
  \forall i, j &\where |i| < |j| \in I. \nonumber\\
  &\exists e \in i - j. \enspace i \cup \{e\} \in I \label{matroid-growth} \tag{MATROID-GROWTH}
\end{flalign}

\ref{matroid-subset} and \ref{matroid-hereditary} satisfy what
is known as an "Independence System'', a "Hereditary Subset System'', or "Abstract Simplical Complex''.
This gives us the knowledge that for every subset \(i \subseteq E\) in \(I\), any \textit{smaller variant} of
\(i\) is also in \(I\). \ref{matroid-growth} is the "Augmentation Property'' for matroids, letting
us grow from a smaller element to a larger element, by implementing atomic inclusion of
additional elements via union.

With these properties, not only can we leverage matroids as a means to check if a subset of \(E\) is
a potential solution (in \(I\)), but we can also easily \textit{add to} our solution, and see if that
is also satisfactory. The \(greedily\) function relies on this, inductively proving that it
strongly normalizes to a maximum \textit"{weight}''. That is, when using a
\(weigh\) function as a metric for each element of type \(\delta\) in \(E\), \(greedily\) can find the subset of
\(E\) that maximizes the total of this weight metric.

This is the nature of \(I\) - it is exhaustive in every opportinity that a subset of \(E\) can have to become
a larger solution - all subsets of a potential solution will be a potential solution, and through augmentation we can
approach a larger solution from a smaller one.

\section{Weights}

Matroids let us find optimal subsets of a particular set, with respect to a particular \textit{metric}.
We need some form of \(weigh\) function, which gives a measure for each element in \(E\):

\[
  weigh \enspace : \enspace \delta \rightarrow \psi
\]

For our purposes, we will also need some way to get a "total'' value of any number
of \(\psi\) values - in \textit{any order}, too. This means that we need a binary
\(\otimes\) function, which satisfies an \textit{abelian semigroup} over \(\psi\):

\begin{flalign}
  \quad \otimes             &: \psi \rightarrow \psi \rightarrow \psi \nonumber\\
  \forall a, b \in \psi.    &\enspace a \otimes b \equiv b \otimes a \label{psi-comm} \tag{PSI-COMM} \\
  \forall a, b, c \in \psi. &\enspace (a \otimes b) \otimes c \equiv a \otimes (b \otimes c) \label{psi-assoc} \tag{PSI-ASSOC}
\end{flalign}

In the circumstance that we want to find the subset of \(E\) that \textit{maximizes}
the \textit{total}\footnote{Where \(total\) is akin to \(concat\) from Haskell, in any order.},
then \(\psi\) obviously needs to form a partial order. If we are to greedily find our
maximum subset, then \(\otimes\) should also be strictly increasing value when combining
terms:

\begin{flalign}
  \forall p \in \psi.       &\enspace p \leq p \label{psi-refl} \tag{PSI-REFL} \\
  \forall p, q \in \psi.    &\enspace p \leq q \wedge q \leq p \Rightarrow p \equiv q \label{psi-anti} \tag{PSI-ANTI} \\
  \forall p, q, r \in \psi. &\enspace p \leq q \wedge q \leq r \Rightarrow p \leq r \label{psi-trans} \tag{PSI-TRANS} \\
  \forall p, q \in \psi.    &\enspace p \leq p \otimes q \label{psi-inc} \tag{PSI-INC}
\end{flalign}

Where \ref{psi-refl}, \ref{psi-anti} and \ref{psi-trans} form the partial order, and
\ref{psi-inc} shows that the total of any number terms should be larger than or equal
to the total of any subset of those terms.

\(\psi\) then serves as an auxiliary type, with enough behaviour to ensure that
\(greedilyMax\) will find our maximum subset.

\section{Optimization}

\(greedilyMax\) works by making successive "\textit{pivots}'', moving the maximum element
from the ground set to the temporary result (only if the new result is in \(I\)).
Put simply, \(pivotMax\) mutates the temporary result directly, and is stateful in the
ground set:

\begin{flalign*}
  pivotMax \enspace : \enspace &( \enspace MonadState \enspace (Set \enspace \delta) \enspace m \\
                               &, \enspace MonadReader \enspace (Set \enspace (Set \enspace \delta)) \enspace m \\
                               &) \Rightarrow Set \enspace \delta \rightarrow m \enspace (Set \enspace \delta)\\
  pivotMax \enspace x \enspace &= \hdo e \leftarrow takeMaximumWith \enspace weigh\\
                               &  \quad \quad \quad \enspace i \leftarrow ask\\
                               &  \quad \quad \quad \hif x \cup {e} \in i
                                  \hthen \mathrm{return} \enspace x \cup {e}\\
                               &  \quad \quad \quad \quad \quad \quad \quad \quad \quad
                                  \mathrm{else} \enspace \mathrm{return} \enspace x
\end{flalign*}

\(greedilyMax\) is then the partial fixpoint of \(pivotMax\) - until there is nothing left in the
copy of \(E\):

\begin{flalign*}
  greedilyMax \enspace &: \enspace (Set \enspace \delta, \enspace Set \enspace (Set \enspace \delta))
                                   \rightarrow Set \enspace \delta\\
  greedilyMax \enspace &(e, \enspace i) \enspace := \enspace runReader \enspace (runStateT \enspace go \enspace e) \enspace i\\
              \where go \enspace &:= \enspace almostFixM \enspace (null \enspace =<< \enspace get) \enspace pivotMax
                        \enspace \emptyset
\end{flalign*}

In this way, \(greedilyMax\) finds the maximum subset.

\begin{theorem}[Maximum Total]
  \(greedilyMax\) finds the subset \(X \in I, X \subseteq E\) such that
  \(\sum \left( fmap \enspace weigh \enspace X \right)\) is maximal compared to
  all other elements in \(I \subseteq \rho E\).
\end{theorem}

\begin{proof}
  We first prove that \(go\) approaches the unique, maximum-sized subset \(E_{\Omega} \subseteq E\)
  \(E_{\Omega} \in I\). We also prove that for any \(n\), the \(total\) of any set
  sized \(n+1\) will be larger than or equal to \(n\). From these, we can prove that
  \(total \enspace E_{\Omega}\) will be maximal compared to all \(i \in I\).

  By \ref{matroid-subset} and \ref{matroid-hereditary}, we can see that \textit{for any} \(i \in I\), we
  can reach \(i\) from the empty set, as \(\rho i \subseteq I\). Also,
  through \ref{matroid-growth} we can reach \textit{any larger} \(j \in I\).
  From this, it is clear to see that we can reach \textit{the larget set} in \(I\) from
  the empty set, especially from the fact that \(\forall i \in I. \enspace \rho i. \in I\).

  The second proof follows directly from \ref{psi-inc} - if we have any additional term,
  then the product will be greater than or equal to the original. This follows directly
  to \(n\)-sized concatenation:

  \begin{flalign*}
    \forall xs \subseteq ys : Set \enspace \phi &\where |xs| \leq |ys|.\\
      total \enspace xs \enspace \leq& \enspace total \enspace ys
  \end{flalign*}

  Thus, \(total \enspace E_{\Omega}\) is maximal.
\end{proof}

\begin{theorem}[Eager Maximum]
  \(X_{n+1} \leftarrow pivotMax \enspace X_{n}\) \textit{eagerly} finds the most
  maximal subset \(X_{n} \in I, X_{n} \subseteq E\) such
  that for any other \(i \in I\) where \(|i| = n\), then \(total \enspace X_{n} \geq total \enspace i\).
\end{theorem}

\begin{proof}
  By structural induction. We rely that the initial state for the \(MonadState\) component of \(pivotMax\)
  is \(E\), and also that the initial value supplied as the third argument to \(almostFixM\)
  is the empty set. From this, we can start our induction.

  \(takeMaximumWith\) in \(pivotMax\) removes and returns the element of it's input
  set with a maximum value from the function provided. From this, we can see that
  for the first case, \(n := 1\), that the result is the singleton set \(\{e_{\Omega}\}\),
  and the new state \(E - \{e_{\Omega}\}\). At this stage, it is easy to show

  \[
    \forall e' \in E - \{e_{\Omega}\}. \enspace weigh \enspace e' \leq weigh \enspace e_{\Omega}
  \] as that is the definition for the maximum. To be more precise, we must show
  \begin{flalign}
    \forall E' \subseteq E \where &| E' | = 1. \label{eager-base}\\
      totalMap \enspace wei&gh \enspace E' \leq totalMap \enspace weigh \enspace \{e_{\Omega}\} \nonumber
  \end{flalign}

  For the successor case, we also need to show that the solution \(S_{n}\)
  is maximal compared to all other subsets of size \(n\), for any \(n\). Lastly,
  by showing that \(S_{n} \subseteq S_{n+1}\), we have proven that the result at
  \(n\) is maximal compared to all other subsets of that size.
  Formally, we need to show that

  \begin{flalign}
    \forall n : Nat, \enspace E' \subseteq E &\where \enspace | E' | = n. \label{eager-succ}\\
      \quad totalMap \enspace wei&gh \enspace E' \leq totalMap \enspace weigh \enspace S_{n} \nonumber\\
    \forall n : Nat. \enspace &S_{n} \enspace \subseteq \enspace S_{n+1} \enspace \label{eager-struct}
  \end{flalign}

  are both true.

  Like said before, (\ref{eager-base}) is easy to show - as there are no other elements in the set initially (we start with
  the empty set \(\emptyset\)), and we begin with \(E\), we \textit{remove} the largest element
  \(e_{\Omega}\) from \(E\), and add it to our accumulator. From this initial state, it is easy to show that
  \(e_{\Omega}\) is larger than any other individual element in \(E - \{e_{\Omega}\}\), and transitively
  that the set total value of \(\{e_{\Omega}\}\) is larger than any other subset with the size \(1\)
  (any other singleton set).

  The more general case, where the total of \(S_{n}\) is maximal compared to every other subset of \(E\) with size
  \(n\), relies on the fact that \(S_{n-1}\) was also maximal compared to every other subset of size \(n-1\), inductively
  back down to the base case - in that this proof uses structural induction. Assuming we know that \(S_{n-1}\) was
  maximal, it is easy to show that we \textit{maintain} this maximal state, as for each pivot, we remove the
  \textit{maximum}, top element in the state, thus retaining maximality when using \(\otimes\).

  More formally, if \(S_{n}\) is the solution set at pivot \(n\), and \(E_{\gamma, n}\) is the \textit{state}
  of the temporary copy of \(E\) at pivot \(n\), then

  \begin{flalign*}
    \forall n : Nat.& \enspace S_{n} \subseteq S_{n+1}\\
    \forall n : Nat.& \enspace E_{\gamma, n} \supseteq E_{\gamma, n+1}\\
    E_{\gamma, 0} := E, &\quad \quad \quad S_{0} := \emptyset
  \end{flalign*}

  Should all be obvious. We omit the rest of the proof for brevity.
\end{proof}

For an asymptotically faster version of \(greedilyMax\), we turn our condition and
statefulness into a fold:

\begin{flalign*}
  greedilyMax' \enspace &(e, \enspace i) \enspace := \enspace foldr \enspace go \enspace \emptyset \enspace (sortBy \enspace weigh \enspace e)\\
  \where \enspace &go \enspace x \enspace acc \enspace := \hif acc \cup \{x\} \in I \hthen acc \cup \{x\}\\
  &\quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \mathrm{else} \enspace acc
\end{flalign*}

\section{Bases and Circuits}

\newcommand{\basis}{\lceil S \rceil}
\newcommand{\circuit}{\hat{\lfloor S \rfloor}}

If we do some analysis on a matroid, we can see clear semantics regarding independent and
dependent sets, in the context of ordering and inclusion.

Say I have some solution \(\basis \subseteq E \in I\). Then this solution is a
\textit{basis} if and only if it is the largest subset in \(I\):

\[
  \forall i \in I. \enspace i \subseteq \basis
\]

Likewise, we could also say that any additional element added to \(\lceil S \rceil\) is now
not independent:

\[
  \forall e \in E - \basis. \enspace \basis \cup \{e\} \not \in I
\]

We carry this logic orthogonally to \textit{circuits}. A circuit \(\circuit \subseteq E\)
is the minimal dependent set in our matroid - such that every other dependent set is a
superset of a circut, and that any smaller subset is independent:

\begin{flalign*}
  \forall i' \subseteq E \not \in I.& \enspace \circuit \subseteq i'\\
  \forall s \in \circuit.&            \enspace \circuit - \{s\} \in I
\end{flalign*}

We use the hat symbol (\textasciicircum) to disambiguate a dependent set from an independent one,
and use the floor and ceiling operators intuitively as "maximum'' and "minimum''.

Circuts and bases, in their own right, are enough to define matroids. A subset
\(i \subseteq E\) is independent if and only if it is a subset of \(\basis\),
and not a superset of \(\circuit\):

\[
  i \in I \enspace \iff \enspace i \subseteq \basis \enspace \wedge
                        \enspace i \not \supseteq \circuit
\]

Likewise, the same proposition can be made for dependent sets:

\[
  i \not \in I \enspace \iff \enspace i \not \subseteq \basis \enspace \wedge
                             \enspace i \supseteq \circuit
\]

Where we evade the hereditary property, while leveraging the definition of the circuit.
Thus \(\basis\) and \(\circuit\) form a matroid over \(E\).

\section{Ranks}

Now that we have an intuition for how maximal independent sets, and minimal
dependent sets form a matroid, we can direct our focus toward \textit{ranks} -
a measure of matroids.

The rank of a matroid is the size of its basis - such that the rank function
\(r : E \rightarrow \mathbb{N}\) is defined as follows:

\begin{flalign*}
  r ( E ) := \basis \subseteq E. \enspace | \basis |
\end{flalign*}

Where \(| \basis |\) is the size of the basis in \(E\). From there, it is easy
to show that

\[
  r ( E ) \enspace \leq \enspace | E |
\]

because \(\basis \subseteq E\). Also, because elements \textit{added} to a set
may not be independent, we can make a transitivity rule as well:

\[
  \forall A \subseteq E, \enspace e \in E. \quad r ( A )
  \enspace \leq \enspace r ( A \cup \{e\})
  \enspace \leq \enspace r ( A ) + 1
\]

which follows to

\[
  \forall A, B, C \where A \subset B \subset C. \quad r ( A )
  \enspace \leq \enspace r ( B )
  \enspace \leq \enspace r ( C )
\]

such that \(r\) is monotonic. Lastly, \(r\) is submodular:

\[
  \forall A, B \subseteq E. \quad r ( A \cup B) + r ( A \cap B)
  \enspace \leq \enspace r ( A ) + r ( B )
\]

Which makes sense in terms of our bases - the basis of \(A \cup B\) \textit{could}
be only as large as \(A\) or \(B\).

From these, we can see that if \( r ( A ) = | A | \), the \(A\) is its own basis.
Further, if I have a subset \(A' \subseteq A\) that also is its own basis, then
that implies their bases are subsets:

\[
  A' \subseteq A \enspace \wedge \enspace
  r ( A ) = | A | \enspace \wedge \enspace
  r ( A' ) = | A' | \enspace \Rightarrow \enspace \basis_{A'} \subseteq \basis_{A}
\]

Which is orthogonal to \ref{matroid-hereditary}, and if \(\basis_{A}\) is the
largest basis in \(E\), then we also satisfy \ref{matroid-growth}. Thus, \(r\) forms a matroid over
\(E\), where elements in \(I\) are formed by self-basis:

\[
  \forall i \in I. \enspace r ( i ) = | i |
\]

From this persepctive, we can also intuit \textit{submatroids}, where \((E',r)\)
is a submatroid of \((E,r)\) iff. \(E' \subseteq E\).

\section{Closure}

Say I have some closure function \(\zeta\) over our ground set \(E\):

\[
  \zeta \enspace : \enspace Set \enspace \delta \rightarrow Set \enspace \delta
\]

To properly be a "closure'' operation, \(\zeta\) needs to satisfy the following:

\begin{flalign*}
  \forall X \subseteq E.    &\quad X \subseteq \zeta ( X )\\
  \forall X \subseteq E.    &\quad \zeta ( X ) \equiv \zeta (\zeta ( X ))\\
  \forall X, Y \subseteq E. &\quad X \subseteq Y \Rightarrow \zeta ( X ) \subseteq \zeta ( Y )
\end{flalign*}

That is, it's an idempotent set operation, homomorphic over collection. If we additionally
restrict \(\zeta\) to satisfy the Mac Lane-Steinitz exchange property, we also have
a matroid:

\[
  \forall a, b \in E, \forall X \subseteq E. \quad
      b \in \zeta ( X \cup \{a\} ) - \zeta ( X ) \iff
      a \in \zeta ( X \cup \{b\} ) - \zeta ( X )
\]

In that, if we have some \(b\) in the difference between the closure of \(X\) and
the included \(a\), then that \(a\) also exists in the difference when including \(b\).

We can now view the result of the closure operator as the maximum superset of the input
with the same rank:

\[
  \zeta \enspace (A \subseteq E) \enspace := \enspace \Big\{ \forall e \in E \where r (A) = r (A \cup \{e\}) \Big\}
\]

Thus, \(\zeta\) forms a matroid over \(E\).

\end{document}


\newpage
\part{CRISP-DM Process Model}
The knowledge discovery process follows the following diagram: \textbf{Data Understanding -- Data Preparation -- Modeling -- Evaluation}.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.45\textwidth]{CRISP-DM.png}
\end{figure}
\section{Data Understanding}
\subsection{Qualitative \& Quantitative Understanding}
Given a dataset description or an attribute table,
\begin{itemize}
	\item Qualitative understanding:
	\begin{itemize}
		\item format of the data: \textbf{tidy data}?
		\item dependent \& independent variables
		\item scale of measurement of attributes: nominal/ordinal/interval/ratio
		\item cross-sectional, time-series, panel data?
	\end{itemize}
	\item Quantitative understanding
	\begin{itemize}
		\item \# instances, ideal: > 5000
		\item \# attributes, ideal start: < 50
		\item \# targets (balance of classes), ideal: > 100 each class
	\end{itemize}
\end{itemize}
Ways to obtain understanding:
\begin{itemize}
	\item Visualization: histogram, distribution, relationship between attribute and response
	\item Summary: mean, median, attribute relationships
\end{itemize}

\subsection{Format of Data: tidy?}
\paragraph{Tidy Data} tabular data is tidy if
\begin{itemize}
	\item each \textbf{variable/feature} is in \textbf{single column}
	\item each \textbf{observation/instance} is in \textbf{single row}
	\item each \textbf{value} is in \textbf{single cell}.
\end{itemize}
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{tidy.png}
\end{figure}
example of tidy data:
\begin{figure}[H]
	\centering
	\includegraphics[width=0.6\textwidth]{tidy1.png}
\end{figure}
\paragraph{Wide Data} \textbf{same variable} is spanned into \textbf{multiple columns}. eg: year.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\textwidth]{wide.png}
\end{figure}
\paragraph{Long Data} \textbf{multiple variables} is compressed into \textbf{one column}. eg: n\_Students \& n\_TAs.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.5\textwidth]{long.png}
\end{figure}

\section{Data Preparation}
According to the data understanding, we distribute the data preparation also into these 3 parts.

But first of all, \textbf{tidy} the data if the format is long/wide. $\rightarrow$ pivoting.

The data preparation works on tidy data.
\subsection{Instance: Missing Value?}
Missing values need to be \textbf{standardized}:
\begin{itemize}
	\item \textbf{ignore} instance with missing values
	\item treat missing values as \textbf{separate value}
	\item \textbf{imputation}: mean/median
\end{itemize}
\subsection{Attributes: Conversion, Discretization \& Feature Selection}
\subsubsection{Conversion}
\begin{itemize}
	\item ordinal $\rightarrow$ numbers, preserving \textbf{natural order}.
	\item nominal
	\begin{itemize}
		\item \#attribute values is small: nominal $\rightarrow$ numeric, \textbf{each attribute value} becomes a single \textbf{binary attribute} (number of binary classes: \textbf{(\#attributevalues - 1)} per attribute).
		\item \#attribute values is large: ignore (eg: IDs)
	\end{itemize}
	\item continuous numeric: Discretization, only if model requires(eg: naive Bayes).
\end{itemize}

\subsubsection{Discretization / Binning}
\begin{itemize}
	\item Reasons for Discretization:
	\begin{itemize}
		\item data: numeric data not normally distributed, data requires sorting frequently(decision tree).
		\item model: model requires nominal data as input(naive Bayes).
	\end{itemize}
	\item Reasons against Discretization:
	\begin{itemize}
		\item in decision tree: equi-depth bins \textbf{doesn't maximize the information gain}. Through the algorithm(binary split) we can find better splits instead of binning.
		\item will potentially \textbf{lose ordinal information}.
		\item model-dependent: regression models requires numeric data, no discretization.
	\end{itemize}
	\item Ways of Discretization:
	\begin{itemize}
		\item equi-frequency/depth
		\item class dependent: when decision tree classifier is used, \textbf{best split} according to \textbf{information gain}.
		\item order-preserving: numeric $\rightarrow$ k nominal $\rightarrow$ $(k-1)$ binary attributes, explaining comparison between $(i-1)$ and $i$.
	\end{itemize}
\end{itemize}
\subsubsection{Feature Selection}
\begin{itemize}
	\item Goal: choose the \textbf{most relevant subset}.
	\item Methods:
	\begin{itemize}
		\item Best Subset (search all, select best)
		\item Forward Selection (bottom-up)
		\item Backward Elimination (top-down)
		\item Stepwise Regression (combines forward/backward)
	\end{itemize}
	\item Use-Case: linear regression, classification, dimensionality reduction, regularization
	\item Ideal: at most 50.
\end{itemize}
\subsection{Targets: Balanced Train \& Test Set}
According to the distribution of targets, build up \textbf{balanced set} and then split into \textbf{balanced train set} and \textbf{balanced test set}.
\begin{itemize}
	\item targets are balanced: each set gets \textbf{same amount} of targets
	\item targets are \textbf{unbalanced}: split \textbf{proportionally}.
\end{itemize}
This only applies to training \& fitting models. It doesn't apply to statistical inference.


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

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

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

\setlength{\parindent}{0mm}
\setlength{\parskip}{1.0\baselineskip}

\usepackage{listings}
\usepackage{color}
\usepackage{pdfpages}

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

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

\begin{document}

\title{Becs-114.1100 Computational Science -- exercise round 10} % Replace the exercise round number
\author{Kunal Ghosh, 546247} % Replace with your name and student number
\maketitle
\section{Solution 1}\label{prob1}
In this exercise, we are experimenting with the Ising model, a mathematical model of ferromagnetism given by Ernst Ising. The premise is that, each cell (site) of the L x L lattice is an atom the spin of which can be +ve or -ve ( we are simulating that by +1 and -1 states in the L x L matrix ).\\
Monte-carlo simulations are then used to simulate the change in energy and magnetization of the (simulated) ferro-magnet as the temperature changes.\\
From theoretical results we know that for temperatures below 2.265 the ferro-magnets equilibriate to either positive or negative magnetization. At temperatures above 2.265 ( eg. 3.5 in our case ) the object exhibits para-magnetism and is disordered at equilibrium.\\
Finally at the Critial temperature ( 2.265 in this case ) the ferro-magnet goes through phase transition from ferro-magnetism to para-magnetism and fluctuates between positive and negative magnetization staying constant at a particular magnetization for long periods of time.
\\\textbf{NOTE:} In the experiments below, run 0 through 6 have been executed with \\ seeds 5555,6000,7000,8000,9000,10000,11000 respectively.
\clearpage
\subsection{Checking ground state energy}
Setting all the spins to -1 we see that the ground state energy of a 32 x 32 Lattice is -2048.0 which is as expected.
\subsection{Estimating suitable equilibriation time}
Jugdging from the plots below and table in section 1.5.1\\
\textbf{Equlibriation time for T  = 2.1 :} around 500 MCS\\
\textbf{Equlibriation time for T  = 3.5 :} around 200 MCS\\
\begin{figure}[!htb]
    \begin{subfigure}[b]{0.5\textwidth}
    \includegraphics[scale=0.45]{new_pics/figure_both_2.png}
    \caption{T = 2.1}
	\label{fig:t2both}
\end{subfigure}
\begin{subfigure}[b]{0.5\textwidth}
    \includegraphics[scale=0.45]{new_pics/figure_both_35.png}
    \caption{T = 3.5}
	\label{fig:t3mag}
\end{subfigure}
\caption{Magnetization and energy plots at T = 2.1 and T = 3.5 for a 32 x 32 lattice. Time is measured on the X axis in MCS (Montecarlo Simulation) units.}
\end{figure}
\\
In the plots above, run 0 is started with the lattice having -1 at all the sites. This indicates the ground state where the enery is the least and the object is the most magnetized ( magnetization just above -1 ). This is the state we expect all the simulation runs to converge to at equilibrium. ( Some of the simulation runs converge to just below +1 which is another ground state which we have not plotted separately. )
\\
The plot to the left is of the object at T = 2.1 at which it is still ferro-magnetic hence some simulation runs assume positive or negative magnetization at equilibrium.\\
The plot to the right is of the object at T = 3.5 at which it assumes para-magnetism and it exhibits almost no magnetization (magnetization oscillating about 0, as observed in the plots).
\clearpage
\subsection{Simulation for T = 2.265}
\begin{figure}[!htb]
	\centering
    \includegraphics[scale=0.40]{pics/magnetization5000_2.png}
    \caption{Plot of the magnetization at T = 2.265. Simulation run for 50,000 MCS}
	\label{fig:5kmag}
\end{figure}
\textbf{What is happening ?}\\
T = 2.265 is the critical temperature at which the model undergoes \textbf{"phase transition"} and keeps fluctuating between magnetization = +1 and magnetization = -1 but staying constant at one magnetization for long periods of time (MCS).\\
\textbf{Effect of this behavior on time needed to perform measurements at this temperature ?}\\
At this temperature since the model keeps fluctuating (after long periods of stability) it causes any equilibrium calculation which considers magnetisation values only a few 100 MCS, falsely report that the model has reached equlibrium and fail to catch the flip in magnetization. Hence we must run the simulation for very a long time ( ~50,000 MCS in this case ) to figure out that the model is indeed going through phase transition and has not yet equilibriated.\\
Additionally, following the discussions on the topic \href{http://physics.stackexchange.com/q/5771}{[here]} and \href{http://physics.stackexchange.com/q/183398}{[here]} this phenomenon is called \textbf{"critical slowdown"} which causes the correlation length \href{Reference https://www.physicsforums.com/threads/what-is-correlation-length.733954/}{[ref]}(Time after which the deviations from the mean, stop being similar) to diverge. This means, we need to perform really long simulation runs to observe changes in correlation ( change in magnetization ). To avoid this, it is advisable to use either multiple simulation runs and average over them, to identify equilibrium, when using a local update scheme (as in this exercise) or use cluster update schemes of Montecarlo simulations.

\clearpage
\subsection{Lattice Visualizations}
In the plots below we visualize the lattice after the simulations have reached equilibrium. Blue patches indicate magnetization of -1 and Red patches indicate magnetization of +1.
\subsubsection{T = 2.1}
\begin{figure}[!htb]
    \begin{subfigure}[b]{0.5\textwidth}
        \includegraphics[scale=0.40]{2_run_0.png}
        \caption{Run 0, lattice initialized to -1, and simulations converges to just above -1}
	    \label{fig:lat2_0}
    \end{subfigure}
    \begin{subfigure}[b]{0.5\textwidth}
        \includegraphics[scale=0.40]{2_run_2.png}
        \caption{Run 2, Random Initialization, converges to just below 1.0}
	    \label{fig:lat2_2}
    \end{subfigure}
    \begin{subfigure}[b]{0.5\textwidth}
        \centering
        \includegraphics[scale=0.40]{2_run_3.png}
        \caption{Run 3, Random Initialization, converges to just above -1.0}
	    \label{fig:lat2_3}
    \end{subfigure}
    \caption{Plots drawn from lattice configurations after 700 iterations for T = 2.1 (Blue = -1, Red = +1 Magnetization)}
\end{figure}
Since the object gets magnetized at equilibrium for T = 2.1, we observe that some simulation runs show the object to be positively magnetised ( more red ) and some negatively magnetised ( more blue ). The ground state with lattice initialized to -1 is seen at negatively magnetised in figure (3.a) above.
\clearpage
\subsubsection{T = 3.5}
\begin{figure}[!htb]
    \begin{subfigure}[b]{0.5\textwidth}
	    \centering
        \includegraphics[scale=0.40]{3_run_0.png}
        \caption{Run 0, lattice initialized to -1, and simulations converges to ~ 0 magnetization}
	    \label{fig:lat3_0}
    \end{subfigure}
    \begin{subfigure}[b]{0.5\textwidth}
        \includegraphics[scale=0.40]{3_run_2.png}
        \caption{Run 2, Random Initialization, stays at ~ 0.0 magnetization}
	    \label{fig:lat3_2}
    \end{subfigure}
    \caption{Plots drawn from lattice configurations after 700 iterations for T = 3.5 (Blue = -1, Red = +1 Magnetization)}
\end{figure}
At T = 3.5 since the object attains paramagnetism, it is seen to have almost equal patches of positive and negative magnetisation rendering the overall magnetisation close to zero. This is observed also for the ground state ( lattice initialized to -1 ) seen in figure (4.a) above.
\subsubsection{T = 2.265}
\begin{figure}[!htb]
	\centering
    \includegraphics[scale=0.40]{pics/50000_lattice.png}
    \caption{Lattice of T = 2.265 when convergence is just above -1.0. This plot from drawn from a configuration after 50,000 MCS (Blue = -1, Red = +1 Magnetization)}
	\label{fig:lat5k}
\end{figure}
At T = 2.265 since the object is going through phase transition, it is seen to exhibit either positive or negative magnetization for extended periods of time. This lattice visualization shows the object to have -ve magnetization after 50,000 MCS.
The corresponding python code can be found at \ref{code:problem1}
\clearpage
\subsection{Values of Averages}\label{prob2}
% $\langle m \rangle$ = \\
% $\langle \left|m\right|  \rangle$ = \\
In the table below we see that, at \\ \\
Temperature = 2.1 the mean magnetization is -ve but close to zero, that's expected, since 4 runs have converged to -1 and only 2 to +1 so they don't quite cancell out each other. The mean of the absolute magnetizations is quite close to 1 which is also inline with the theoretical result, that at equilibrium the magnetizations converge to -1 or +1 for temperatures below critical temperature.\\ \\
For Temperature = 3.5, the mean magnetization is quite close to zero and so is the mean of the absolute magnetization, these are again inline with theoretical results, according to which, for temperatures above critical temperature, the spins are disordered resulting in a mean magnetization which is close to zero.\\ \\
For Temperature = 2.265, the critical temperature, the magnetization keeps flipping between -1 and +1 so depending on our simulation run, we would get the mean slightly above zero or below zero because some +ve and -ve values in the sequence would cancell and the remaining values would get divided by the number of measurements which is quite large. Also the mean of absolute magnetizations is close to 0.75 because most of the values are between |0.5| and |1| (as can be seen in the 1.3) so the mean of them should be around 0.75 and our result is quite close to that. If we take longer runs, the value of <|m|> should converge to 0.75

\begin{lstlisting}[caption=Average magnetization values]
For Temp = 2.1 <m> = -0.298180989583 err = 0.0149983573735 AND <|m|> = 0.872975260417 err = 0.000696132850481
For Temp = 3.5 <m> = 0.00534993489583 err = 0.00114563542299 AND <|m|> = 0.0633504231771 err = 0.00069438550247
For Temp = 2.265 <m> = -0.272830034722 err = 0.00295765007233 AND <|m|> = 0.659539149306 err = 0.000857419301299
\end{lstlisting}
The table above reports average M = $\langle m \rangle$ and average abs(M) = $\langle \left|m\right| \rangle$ values separately for each temperature. (magnetisation values have been taken from each run as follows):\\
For T = 2.1 : Equilibriation time is taken as 500\\
For T = 3.5 : Equilibriation time is taken as 200\\
For T = 2.265 : Equilibriation time is taken as 5000\\ \\
\textbf{Mean magnetisation} $\langle m \rangle$ is calculated by taking a mean of magnetisation values after equilibriation time for all run.\\
\textbf{Mean abs magnetisation} $\langle \left|m\right| \rangle$ is calculated by taking a mean of absolute values of magnetisation after equilibriation time for all run.\\
\textbf{Error values} are calculated using the following equation.\\
\begin{equation}
    \sigma_{m} = \sqrt{\frac{1}{n-1}(\langle m^{2} \rangle - \langle m \rangle^{2})}
\end{equation}

The corresponding python code can be found at \ref{code:problem2}
\subsubsection{The data below has mean values from each run of T=2.1 and T=3.5 (Raw data for reference ONLY)}
The below tables report average M = $\langle m \rangle$ and average abs(M) = $\langle \left|m\right| \rangle$ values separately for each run. \\
The averages were calculated by taking magnetization values from the past 100 MCS and averaging them to get average M. The average of the absolute values was calculated to get the average abs(M).\\
The \textbf{convergence criteria} was for the error ( difference between two successive averages ) to be less than 0.01
\begin{lstlisting}[caption=Average Magnetization values for T 2.1]
# Lattice initialized to -1 and then MC simulations were run
Temp = 2.1 Run 0 Convergence after 200 MCS at M = -0.876015625, M_err = 0.00283203125
Temp = 2.1 Run 0 Convergence after 200 MCS at abs(M) = 0.876015625, abs(M)err = 0.00283203125
Temp = 2.1 Run 0 Final Data after 1000 MCS at abs(M) = 0.879296875, abs(M)err = 0.0109765625, abs(M) = 0.879296875, abs(M)err = 0.0109765625

Temp = 2.1 Run 1 Convergence after 700 MCS at M = -0.89130859375, M_err = 0.0034765625
Temp = 2.1 Run 1 Convergence after 700 MCS at abs(M) = 0.89130859375, abs(M)err = 0.0034765625
Temp = 2.1 Run 1 Final Data after 1000 MCS at abs(M) = 0.90140625, abs(M)err = 0.02017578125, abs(M) = 0.90140625, abs(M)err = 0.02017578125

Temp = 2.1 Run 2 Convergence after 600 MCS at M = 0.884375, M_err = 0.00033203125
Temp = 2.1 Run 2 Convergence after 600 MCS at abs(M) = 0.884375, abs(M)err = 0.00033203125
Temp = 2.1 Run 2 Final Data after 1000 MCS at abs(M) = 0.84498046875, abs(M)err = 0.02615234375, abs(M) = 0.84498046875, abs(M)err = 0.02615234375

Temp = 2.1 Run 3 Convergence after 800 MCS at M = -0.885546875, M_err = 0.00876953125
Temp = 2.1 Run 3 Convergence after 800 MCS at abs(M) = 0.885546875, abs(M)err = 0.00876953125
Temp = 2.1 Run 3 Final Data after 1000 MCS at abs(M) = 0.886640625, abs(M)err = 0.0027734375, abs(M) = 0.886640625, abs(M)err = 0.0027734375

Temp = 2.1 Run 4 Convergence after 600 MCS at M = -0.8751171875, M_err = 0.00986328125
Temp = 2.1 Run 4 Convergence after 600 MCS at abs(M) = 0.8751171875, abs(M)err = 0.00986328125
Temp = 2.1 Run 4 Final Data after 1000 MCS at abs(M) = 0.88275390625, abs(M)err = 0.00666015625, abs(M) = 0.88275390625, abs(M)err = 0.00666015625

# Run 5 did not satisfy convergence criteria even after 1000 MCS
Temp = 2.1 Run 5 Final Data after 1000 MCS at abs(M) = 0.86546875, abs(M)err = 0.012421875, abs(M) = 0.86546875, abs(M)err = 0.012421875

Temp = 2.1 Run 6 Convergence after 800 MCS at M = -0.87662109375, M_err = 0.00427734375
Temp = 2.1 Run 6 Convergence after 800 MCS at abs(M) = 0.87662109375, abs(M)err = 0.00427734375
Temp = 2.1 Run 6 Final Data after 1000 MCS at abs(M) = 0.87595703125, abs(M)err = 0.026640625, abs(M) = 0.87595703125, abs(M)err = 0.026640625
\end{lstlisting}
\begin{lstlisting}[caption=Average Magnetization values for T 3.5]
# Lattice initialized to -1 and then MC simulations were run
Temp = 3.5 Run 0 Convergence after 300 MCS at abs(M) = 0.05380859375, abs(M)err = 0.0001171875
Temp = 3.5 Run 0 Convergence after 600 MCS at M = -0.03533203125, M_err = 0.008359375
Temp = 3.5 Run 0 Final Data after 1000 MCS at abs(M) = 0.053984375, abs(M)err = 0.01162109375, abs(M) = 0.053984375, abs(M)err = 0.01162109375

Temp = 3.5 Run 1 Convergence after 100 MCS at M = 0.02720703125, M_err = 0.00447265625
Temp = 3.5 Run 1 Convergence after 300 MCS at abs(M) = 0.05716796875, abs(M)err = 0.009609375
Temp = 3.5 Run 1 Final Data after 1000 MCS at abs(M) = 0.08421875, abs(M)err = 0.0171484375, abs(M) = 0.08421875, abs(M)err = 0.0171484375

Temp = 3.5 Run 2 Convergence after 700 MCS at abs(M) = 0.06861328125, abs(M)err = 0.0042578125
Temp = 3.5 Run 2 Final Data after 1000 MCS at abs(M) = 0.05421875, abs(M)err = 0.03365234375, abs(M) = 0.05421875, abs(M)err = 0.03365234375

Temp = 3.5 Run 3 Convergence after 100 MCS at M = 0.0115625, M_err = 0.0028125
Temp = 3.5 Run 3 Convergence after 200 MCS at abs(M) = 0.06232421875, abs(M)err = 0.00533203125
Temp = 3.5 Run 3 Final Data after 1000 MCS at abs(M) = 0.058125, abs(M)err = 0.00541015625, abs(M) = 0.058125, abs(M)err = 0.00541015625

Temp = 3.5 Run 4 Convergence after 400 MCS at abs(M) = 0.0665234375, abs(M)err = 0.0051953125
Temp = 3.5 Run 4 Final Data after 1000 MCS at abs(M) = 0.05048828125, abs(M)err = 0.013203125, abs(M) = 0.05048828125, abs(M)err = 0.013203125

Temp = 3.5 Run 5 Convergence after 100 MCS at abs(M) = 0.0578515625, abs(M)err = 0.00736328125
Temp = 3.5 Run 5 Final Data after 1000 MCS at abs(M) = 0.0575, abs(M)err = 0.0240625, abs(M) = 0.0575, abs(M)err = 0.0240625

Temp = 3.5 Run 6 Convergence after 500 MCS at M = 0.0059375, M_err = 0.00458984375
Temp = 3.5 Run 6 Convergence after 500 MCS at abs(M) = 0.0428125, abs(M)err = 0.00302734375
Temp = 3.5 Run 6 Final Data after 1000 MCS at abs(M) = 0.07537109375, abs(M)err = 0.02443359375, abs(M) = 0.07537109375, abs(M)err = 0.02443359375
\end{lstlisting}
\clearpage
\section{Appendix A}\label{code:problem1}
Python source code for \ref{prob1}.
\lstinputlisting{2d_example.py}
\section{Appendix B}\label{code:problem2}
Python source code for calculating means. \ref{prob2}
\lstinputlisting{2d_means.py}
\end{document}