%\documentclass{scrartcl}
%\usepackage{beamerarticle}
%%\usepackage{dtsc-beamer}
%\usepackage{fullpage}

\documentclass[9pt]{beamer}
\usetheme{boxes}
\usetheme{Boadilla}
\usecolortheme{beaver}
%\usecolortheme{sidebartab}
% \usefonttheme{structurebold}
\usefonttheme{serif}

%gets rid of bottom navigation bars
\setbeamertemplate{footline}[page number]{}

%gets rid of navigation symbols
\setbeamertemplate{navigation symbols}{}

% \usepackage{helvet}
\usepackage{amsmath, amssymb}
\usepackage{color}
%\usepackage{asymptote}
\usepackage{mathrsfs}
\usepackage{dsfont}
\usepackage{url}
\usepackage{cancel}
\usepackage{tikz}
\usetikzlibrary{fit,positioning}
\usetikzlibrary{shapes,matrix,decorations.markings,arrows}
\usetikzlibrary{graphs}
\usepackage{bbm}
\def\ind{\mathbbm{1}} %Indicator function
%
\definecolor{darkblue}{rgb}{0.0, 0.0, 0.55}
\definecolor{notsodarkblue}{rgb}{0.0, 0.0, 0.7}
\setbeamercolor{title}{fg=darkblue}
\setbeamercolor{frametitle}{fg=darkblue}
\newcommand{\myitem}{\item[$\bullet$]}
\definecolor{darkgreen}{rgb}{0, 0.55, 0}
\definecolor{lgray}{rgb}{0.9,0.9,0.9}

\newcommand{\LABFIG}[1]{\label{fig:#1}}%{\tt [fig:$\text{$#1$}$]}}

\newcommand{\ve}[1]{\boldsymbol{#1}}
\newcommand{\set}[1]{\mathcal{#1}}

\def\X{\ve{X}}
\def\x{\ve{x}}
\def\y{\ve{y}}
\def\Y{\ve{Y}}
\def\S{\ve{S}}
\def\s{\ve{s}}
\def\sg{\tilde{{s}}}
\def\n{n}
\def\C{\mathcal{C}}
\def\H{\ve{H}}
\newcommand{\st}[1]{{e}_{#1}}		%state
\newcommand{\str}[2]{{e}_{#2,(#1)}}
\newcommand{\St}[1]{{E}_{#1}}		%state
\def\seqst{\ve{\st{}}}
\def\seqSt{\ve{\St{}}}
\def\S{\ve{S}}		%Symbol sequence
\def\AQAM{\mathcal{A}}
\def\Aest{\mathcal{E}}
\def\chl{\mathtt{h}} %Channel length
\def\s{\ve{s}} %transmitted word of symbols
\def\sk{{s}_k} %transmitted word of symbols
\def\hsk{\hat{\s}_k}
\def\t{\mathtt{L}}   %Word length (symbols)

\def\eqtwo{\stackrel{\text{\tiny (mod 2)}}{=}}

\newcommand{\p}[1]{p_{_{#1}}} %pdf or pmf
\newcommand{\q}[1]{q_{_{#1}}} %pdf or pmf
\newcommand{\Iset}[1]{\mathtt{#1}} %Index set
\def\d{\text{d}}	%Differential (for integrals)

\newcommand{\mc}[1]{\mathcal{#1}}
\def\sX{\mc{X}}

\newcommand\Def[1]{{\textbf{Definition:}\\\emph{#1}\\\begin{center} ------------------------ \end{center}}}
\newcommand\Prop[1]{{\textbf{\textcolor{red}{Property:}}\\\emph{#1}\\\begin{center} \textcolor{red}{------------------------} \end{center}}}

\newcommand{\noteB}[1]{\textbf{\textcolor{notsodarkblue}{#1}}}

\newcommand{\noteR}[1]{\textbf{\textcolor{darkred}{#1}}}

\newcommand{\noteG}[1]{\textbf{\textcolor{darkgreen}{#1}}}

\newcommand{\snoteB}[1]{{\textcolor{darkblue}{#1}}}

\newcommand{\snoteR}[1]{{\textcolor{darkred}{#1}}}

\newcommand{\snoteG}[1]{{\textcolor{darkgreen}{#1}}}

\newcommand{\fs}[2]{#2}

\title[]{Introduction to Graphical Models and Inference for Communications}
\author[\textcolor{white}{Advanced Digital Communications}]{{\textcolor{black}{Pablo Martinez Olmos \\ olmos@tsc.uc3m.es}}
}
\date[]{{}}
\institute{\textcolor{white}{}}

% \pgfdeclareimage[height=0.5cm]{ITW}{pics/Logo.png}
%  \logo{\pgfuseimage{ITW}}

\AtBeginSection[]
{
  \begin{frame}<beamer>{Index}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}

\begin{document}

\frame{
\titlepage
\thispagestyle{empty}
\begin{center}
\includegraphics[scale=0.05]{Figuras/uc3m-logo.pdf}
\end{center}
}

\section{Course overview}

\frame{
\frametitle{\noteB{Graphical Models and approximate inference}
}

\begin{itemize}
\item \snoteB{Exact inference over discrete trees: the Belief Propagation algorithm}.
%\item \snoteG{The forward/backward  algorithm is nothing but the BP algorithm}.
%\item \snoteR{BP for channel equalization.}
\item \snoteB{Approximate Inference over graphs with cycles using BP. }.
%\item \snoteG{Codes on graphs and iterative algorithms: achieve channel capacity at low-cost}.
%\item \snoteR{BP for binary and non-binary LDPC codes.}
\item \snoteB{Inference over Gaussian distributions using BP}.
%\item \snoteG{The Kalman Filter is nothing but the BP algorithm.}
%\item \snoteR{Gaussian message-passing on linear models. }
%\item \snoteG{Approximate message passing for Compressed Sensing.}
\item \snoteB{Beyond BP:  Mean field approximations, Expectation propagation and Monte Carlo approximations.}
%\item \snoteG{Approximate inference in digital-communication receivers.}
%\item \snoteR{Review of recent literature on the topic and expositions!}
\end{itemize}

}

\frame{
\frametitle{\noteB{Graphical Models and approximate inference} \noteG{ for  communications}
}

\begin{itemize}
\item \snoteB{Exact inference over discrete trees: the Belief Propagation algorithm}.
\begin{itemize}
\item \snoteG{The forward/backward BCJR  algorithm is nothing but the BP algorithm}.
\end{itemize}
%\item \snoteR{BP for channel equalization.}
\item \snoteB{Approximate Inference over graphs with cycles using BP. The Bethe approximation and beyond}.
\begin{itemize}
\item \snoteG{Codes on graphs and iterative algorithms: achieving channel capacity at low-cost}.
\end{itemize}
%\item \snoteR{BP for binary and non-binary LDPC codes.}
\item \snoteB{Inference over Gaussian distributions using BP}.
\begin{itemize}
\item \snoteG{The Kalman Filter is nothing but the BP algorithm.}
%\item \snoteG{Approximate message passing for Compressed Sensing.}
\end{itemize}
%\item \snoteR{Gaussian message-passing on linear models. }
\item \snoteB{Beyond BP:  Mean field approximations, Expectation propagation and Monte Carlo approximations.}
\begin{itemize}
\item \snoteG{Approximate inference in digital-communication receivers.}
\end{itemize}
%\item \snoteR{Review of recent literature on the topic and expositions!}
\end{itemize}

}

\frame{
\frametitle{\noteB{Graphical Models and approximate inference} \noteG{for  communications}
}

\noteR{Learning by programming and simulating algorithms ...}

\begin{itemize}
\item \snoteB{Exact inference over discrete trees: the Belief Propagation algorithm}.
\begin{itemize}
\item \snoteG{The forward/backward  algorithm is nothing but the BP algorithm}.
\item \snoteR{Bit-MAP decoding of convolutional codes using BP.}
\end{itemize}
\item \snoteB{Approximate Inference over graphs with cycles using BP. The Bethe approximation and beyond}.
\begin{itemize}
\item \snoteG{Codes on graphs and iterative algorithms: achieving channel capacity at low-cost}.
\item \snoteR{BP decoding of Turbo codes.}
\item \snoteR{BP decoding of binary  LDPC codes.}
\end{itemize}
\item \snoteB{Inference over Gaussian distributions using BP}.
\begin{itemize}
\item \snoteG{The Kalman Filter is nothing but the BP algorithm.}
\item \snoteG{Approximate message passing for Compressed Sensing.}
\item \snoteR{Gaussian message-passing on linear models.}
\end{itemize}
\item \snoteB{Beyond BP:  Mean field approximations, Expectation propagation and Monte Carlo approximations.}
\begin{itemize}
\item \snoteG{Approximate inference in digital-communication receivers.}
\item \snoteR{Iterative receivers for digital communications.}
\end{itemize}
\end{itemize}

}

%\frame{
%\frametitle{\noteB{Graphical Models and approximate inference} \noteG{for  communications}
%}
%
%\noteR{... and by reading papers together.}
%
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.35]{Figuras/papers.pdf}
%\end{tabular}
%\end{figure}
%
%}

\frame{
\frametitle{Graphical Models}

\begin{itemize}
\item Graphical Models (GMs) bringht together graph theory and probability theory in a powerful formalism for multivariate statistical modeling.
\item \noteB{In a nutshell:} GMs use graphs to represent and manipulate joint probability distributions.
\end{itemize}

\begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.6]{Figuras/Graph_1.pdf}
\end{tabular}
\end{figure}

}

\frame{
\frametitle{You already know something about inference over GMs}

\begin{itemize}
\item Statistical models have long been formulated in terms of graphs in applied fields including bioinformatics, speech processing, image processing, coding theory in communications.
\end{itemize}

\begin{itemize}
\item Many widely known algorithms are expressed in terms of recursions operating on these graphs:
\begin{itemize}
\item Kalman filtering for state-space models.
\item Forward-backward algorithm for hidden Markov models.
\item Viterbi algorithm.
\item Iterative message passing decoders for LDPC codes.
\item ...
\end{itemize}
\end{itemize}

\begin{block}{}
These ideas can be understood, unified and generalized within the formalism of graphical models.
\end{block}

}
%
%\frame{
%\frametitle{High-dimensional probability distributions}
%
%%\begin{itemize}
%%\item In fields that involve the study of large number of interacting variables, graphical models are increasing in evidence.
%%\end{itemize}
%\begin{itemize}
%\item Mathematically, the interactions in real-world systems are described by high-dimensional probability distributions.
%\end{itemize}
%\begin{itemize}
%\item \noteR{High-dimensional probability distributions are  intractable} to learn and manipulate.
%\end{itemize}
%
%
%
%
%
%%
%
%
%
%
%
%}
%
%\frame{
%\frametitle{High-dimensional probability distributions}
%
%Consider a set of $d$ random binary variables $\X=(X_1,X_2,X_3,\ldots,X_n)$
%
%\begin{itemize}
%\item  \noteB{Storing} in a table all possible entries of the joint probability distribution $\p{\X}(\x)$ requires $2^{n}$ entries, unfeasible for $n\rightarrow\infty$.
%\item Even if we could store all values of $\p{\X}(\x)$, can we \noteB{learn} such distribution from samples? ($2^{n}$ probabilities to learn).
%\item Even if we could learn $\p{\X}(\x)$ and store it, can we make \noteB{inference}? Marginalizing cost $2^{n}$ binary sums!
%\end{itemize}
%
%\vspace{1cm}
%
%\noteR{Even on the most optimistically fast supercomputer this would take far too long,
%even for a $n = 100$ variable system.}
%
%}

\frame{
\frametitle{GMs to represent  probability distributions}

\begin{alertblock}{}
 Probabilistic graphical models are used to approximate such distributions by specifying a set of \noteB{conditional independence} or causality relationships, reducing the number of parameters needed to specify the distribution.
 \end{alertblock}

\vspace{1cm}

\begin{block}{}
Graph theory  also plays a fundamental role in assessing the \noteB{computational complexity of inference} algorithms over a GM.
\end{block}

\begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.6]{Figuras/Graph_1.pdf}
\end{tabular}
\end{figure}

}

\frame{
\frametitle{GMs: learn the model/learn from the model}

There are two main problems associated to graphical models:

\begin{enumerate}
\item Graphical Model Selection (Learn a graphical model from samples).
\item Efficient inference in Graphical Models.
\end{enumerate}

%FIRST PLOT: LEARNING GRAPHICAL MODEL STRUCTURE---> Given Data (set of channel outputs, noisy image or measurements, data from stock market) and a set of random variables that we estimate generated this data (transmitted symbols, pixels, traders) ---> Learn the graphical model! Everything could be connected to everything--> complexity reduction blah blah storage blah bla

%SECOND PLOT: INFERENCE. Given a graphical model --> we want to compute marginals or conditional probabilities. The structure of the graph is fundamental to asses the complexity/accuracity of inference methods. (General theme of shewoon slides)

}

\frame{
\frametitle{Graphical Model Selection}

\begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.35]{Figuras/Graph_3.pdf}
\end{tabular}
\end{figure}

}

\frame{
\frametitle{Exact Inference: the discrete case}

Let $\X$ be a discrete random vector and $\Y$ a noisy observation, where $\x\in\set{X}^n$, $\y\in\set{Y}^m$ and
 the joint pmf is $\p{\X,\Y}(\x,\y)$. For a given observation $\Y=\y$, we are interested in drawing conclusions about $\X$.

\vspace{0.5 cm}

Two basic kind of computations:

\begin{block}{}
Given $\Y=\y$, find the most probable realization of the posterior probability distribution:
\begin{align*}
\hat{\x}&=\arg\max_{\x\in\set{X}^n} \p{\X|\y}(\x)=\arg\max_{\x\in\set{X}^n} \frac{\p{\Y|\x}(\y)\p{\X}(\x)}{\p{\Y}(\y)}=\arg\max_{\x\in\set{X}^n} \p{\Y|\x}(\y)\p{\X}(\x)
\end{align*}
\noteR{Complexity $\rightarrow$ $\mathcal{O}(|\set{X}|^n)$}.
\end{block}

}

\frame{
\frametitle{Exact Inference: the discrete case (II)}

\begin{block}{}
Given $\Y=\y$, compute the \emph{marginal} probability distribution $\p{X_i|\y}{(x_i)}$:
\begin{align*}
\p{X_i|\y}{(x_i)}&=\sum_{\x_{\sim i}\in\set{X}^{n-1}} \p{\X|\y}(\x)\\\nonumber
&=\frac{1}{\p{\Y}(\y)}\sum_{\x_{\sim i}\in\set{X}^{n-1}}\p{\Y|\x}(\y)\p{\X}(\x),
\end{align*}
\noteR{Complexity $\rightarrow$ $\mathcal{O}(|\set{X}|^n)$}.
\end{block}
}

%
%\frame{
%\frametitle{Inference over discrete probability distributions.}
%Given a probability distribution over $\X=(X_1,X_2,\ldots,X_d)$
%\begin{align*}
%\mu(\ve{x})=\mathbb{P}_{X_1,X_2,\ldots,X_d}(x_1,x_2,\ldots,x_d)
%\end{align*}
%where $x_i\in\sX$.
%\begin{itemize}
%\item Finding the most probable realization (mode):
%\begin{align*}
%\hat{\ve{x}}=\arg \max_{\ve{x}\in\sX^n}\mu(\ve{x})
%\end{align*}
%\item Compute marginals:
%\begin{align*}
%\mu(x_i)&=\sum_{\ve{x}\sim x_i}\mu(\ve{x})\qquad \text{Discrete case}\\
%\mu(x_i)&=\int_{\ve{x}\sim x_i}\mu(\ve{x})\qquad \text{Continuous case}
%\end{align*}
%\item Sampling (generate new samples from $\mu(\ve{x})$).
%\end{itemize}
%\begin{exampleblock}{}
%\centering Key challenge $n>>1$
%\end{exampleblock}
%}

\frame{
\frametitle{Structure helps!}
\begin{align*}
\p{\X}(\x)=\p{X_1}(x_1)\p{X_2|x_1}(x_2)\p{X_3|x_2}(x_3)\p{X_4|x_3}(x_4)\p{X_5|x_4}(x_5)
\end{align*}

\vspace{0.5cm}
How many operations we need to compute $\hat{\x}=\arg\max_{\x\in\set{X}^n}  \p{\X}(\x)$?

\begin{itemize}
\item \noteR{Brute force}: 5 variables $\Rightarrow |\set{X}|^5$ evaluations of $\p{\X}(\x)$
\item \noteB{Efficient Inference}: It is easy to check that it only costs $5 |\set{X}|^2$ function evaluations
\end{itemize}

\begin{exampleblock}{}
 When the probability distribution factorizes,
we can achieve huge computational gains.
\end{exampleblock}

In other words...

\begin{block}{}
When the probability distribution is represented by a GM,
we can achieve huge computational gains.
\end{block}

}

\frame{
\frametitle{Inference and GMs}

\begin{itemize}
\item  It is critical to understand for which graphical structures efficient inference (polynomial complexity with the dimension $n$) is possible.
\item We will use such knowledge to propose feasible and accurate \noteB{approximate inference methods} for those GMs for which  \noteB{exact inference} is prohibitively complex.
\end{itemize}

}

\frame{
\frametitle{Exact Inference: the continuous case}
Assume now that both $\X$ and $\Y$ are real-valued random vectors with joint pdf $\p{\X,\Y}(\x,\y)$ where $\x\in\set{\mathbb{R}}^n$ and $\y\in\set{\mathbb{R}}^m$ and that we observe $\Y=\y$.

\begin{block}{}
\begin{align*}
\hat{\x}=\arg\max_{\x\in\mathbb{R}^n} \p{\X|\y}(\x)=\arg\max_{\x\in\mathbb{R}^n} \frac{\p{\Y|\x}(\y)\p{\X}(\x)}{\p{\Y}(\y)}=\arg\max_{\x\in\mathbb{R}^n} \p{\Y|\x}(\y)\p{\X}(\x)\
\end{align*}
\noteR{Typically $ \p{\X|\y}(\x)$ is not convex and multimodal}
\end{block}

\begin{block}{}
\begin{align*}
\p{X_i|\y}{(x_i)} &=\int \p{\X|\y}(\x) \d\x_{\sim \Iset{i}}=\frac{1}{\p{\Y}(\y)}\int \p{\Y|\x}(\y)\p{\X}(\x) d\x_{\sim \Iset{i}}
\end{align*}
\noteR{This integral lacks in general of analytical solution}
\end{block}

\noteB{Special case: Gaussian distribution}.

}

\frame{
\frametitle{Structure helps!}

\begin{itemize}
\item Efficient \emph{approximate inferece} over $ \p{\X|\y}(\x)$ for real-valued distributions will be attained by constructing tractable approximation $\q{\X}(\x)$ where inference is possible.
\item Let $\q{\X}(\x|\ve{\theta})$ a family of distributions parameterized by a given vector $\ve{\theta}$. Assuming $\p{\X|\y}(\x)$ and $\q{\X}(\x|\ve{\theta})$ have the same support, two common criterions:
\begin{align*}
\q{\X}(\x|\hat{\ve{\theta}})&=\max_{\ve{\theta}} KL(\q{\X}||\p{\X})\qquad \text{Variational Inference}\\
\q{\X}(\x|\hat{\ve{\theta}})&=\max_{\ve{\theta}} KL(\p{\X}||\q{\X})\qquad \text{Expectation Propagation}
\end{align*}
\item Feasible methods exploit the properties of the GM that represents $\p{\X|\y}(\x)$!
\end{itemize}
}

\section{Representing probability distributions via Factor graphs}

\frame{
\frametitle{Graphical models}
\begin{itemize}
\item There are three main graph-based languages for describing probability distributions: \noteG{Bayessian Networks} (directed graphs), \noteB{Markov Random Fields} (undirected graphs) and \noteB{Factor graphs}.
\end{itemize}
\begin{itemize}
\item  They differ in the set of conditional independences they can encode and in the factorization of the distribution that they induce.
\end{itemize}
\begin{itemize}
\item  For the purpose of solving inference problems, \noteB{it is often convenient to convert both directed and undirected graphs into factor graphs}.
\end{itemize}

}

\subsection{Directed graphical models}

\frame{
\frametitle{Bayesian networks (directed graphical models)}
\begin{itemize}
\item They efficiently represent large joint distributions by making a set of \noteB{conditional independence} (CI) assumptions.
\begin{align*}
X\perp Y| Z \Leftrightarrow \p{X,Y|z}(x,y)=\p{X|z}(x)\p{Y|z}(y)
\end{align*}
\end{itemize}

\begin{itemize}
\item A Bayesian network is a probability distribution of the form
\begin{align*}
\p{\X}(\x)=\prod_{i=1}^{n}\p{X_i|\ve{x}^{\pi}_i}(x_i)
\end{align*}
where $\ve{x}^{\pi}_i$ is the set of parents of $X_i$.
\item Represented as a \textbf{directed graph}.
\end{itemize}

\begin{figure}
\includegraphics[scale=0.5]{Figuras/Graph_4.pdf}
\end{figure}
$\p{\X}(\x)=\p{X_1}(x_1)\p{X_2}(x_2)\p{X_3|x_1,x_2}(x_3)\p{X_5}(x_5)\p{X_4|x_3,x_5}(x_4)\p{X_6|x_1}(x_6)$

}
%
%\frame{
%\begin{itemize}
%\item BNs are well-suited to use known causal relationships and to express conditional dependences between variables in the graph.
%\end{itemize}
%
%\begin{block}{Medical diagnosis with the t quick medical reference (QMR) network}
%Both diseases $\ve{D}$ and symptoms $\ve{S}$ are binary random variables. The conditional probabilities  are represented by the \emph{noisy-OR model}:%\footnote{Pmf for discrete variables are denoted by $P(x)$}:
%\begin{align*}
%\p{S_j|\ve{d}}(s_j=0)=(1-\theta_{0,i})\prod_{i\in\pi_{s_j}}(1-\theta_{i,j})^{d_j}
%\end{align*}
%\end{block}
%
%
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.2]{Figuras/Graph_6.pdf}
%\end{tabular}
%\end{figure}
%
%\begin{alertblock}{}
%Given real data, we try to learn the $\theta_{i,j}$ parameters.
%\end{alertblock}
%
%}
%
%\frame{
%\frametitle{Inference over the QMR network}
%\begin{itemize}
%\item If we observe a certain set $\mc{S}$ of symptoms, what is the probability that the patient suffers from disease $d_j$?
%\begin{align*}
%P(s_j,|\ve{d}_{\mc{S}})=\sum_{\ve{s}\sim s_j}\sum_{\ve{d}\sim \ve{d}_{\mc{S}}} P(\ve{s},\ve{d})
%\end{align*}
%\end{itemize}
%
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.2]{Figuras/Graph_7.pdf}
%\end{tabular}
%\end{figure}
%
%\begin{block}{}
%\begin{itemize}
%\item Exact calculation needs $2^{569+4075-|S|}$ sums!!
%\item We will study methods for approximate inference.
%\end{itemize}
%\end{block}
%
%}
%
%\frame{
%\frametitle{Evaluating CI is hard in BNs}
%
%Given a BN
%\begin{align*}
%\mu(\ve{x})=\prod_{i=1}^{d}\mu(x_i|\ve{x}_{\pi(x_i)})
%\end{align*}
%if we consider a partition of the variable in the directed graph into three sets $A$, $B$ and $C$, evaluating whether
%\begin{align*}
%\X_A\perp \X_B| \ve{x}_E
%\end{align*}
%is not trivial (see $d$-separation theorem and the Bayes Ball algorithm).
%
%\begin{block}{Why are we interested in CI?}
%Structure helps to efficiently solve inference problems!
%\end{block}
%
%}
%
%\frame{
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.2]{Figuras/Graph_8.pdf}
%\end{tabular}
%\end{figure}
%
%\begin{exampleblock}{With no CI assumptions (no BNs)}
%$|\sX|^{|A|+|B|+|C|}$ sums!
%\end{exampleblock}
%
%
%}
%
%
%\frame{
%
%\begin{block}{If we exploit $\X_B\perp\X_C|\X_A$}
%\begin{align*}
%P(x_j)&=\sum_{\ve{x}_{A}}\sum_{\ve{x}_{B}}\sum_{\ve{x}_{C}\sim x_{j}}P(\ve{x}_{A})P(\ve{x}_{B}|\ve{x}_{A})P(\ve{x}_{C}|\ve{x}_{A})\\
%&=\sum_{\ve{x}_{A}}P(\ve{x}_{A}) \underbrace{\left(\sum_{\ve{x}_{B}} P(\ve{x}_{B}|\ve{x}_{A}) \right)}_{=1 ~ \forall \ve{x}_{A}}\left(\sum_{\ve{x}_{C}\sim x_{j}} P(\ve{x}_{C}|\ve{x}_{A}) \right)
%\end{align*}
%The  cost to compute the marginal by brute-force is reduced to $|\sX|^{|A|+|B|}$.
%\end{block}
%
%
%}
%
%\frame{
%
%\Def{ the Markov Blanket of a given set of random variables $\X_S$ is the set of variables $\X_{M(S)}$ such that
%\begin{align*}
%\X_S \perp \X_{\text{rest of the graph}}|\X_{M(S)}
%\end{align*}
%}
%
%Evaluating the Markov Blanket is BNs is not intuitive.
%
%}

\frame{
\frametitle{Hidden Markov Models}

\begin{itemize}
\item BNs are well-suited to use known causal relationships and to express conditional dependences between variables in the graph.
\end{itemize}

\begin{itemize}
\item HMMs are naturally represented by BNs.
\end{itemize}

\begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.5]{Figuras/Graph_17_2.pdf}
\end{tabular}
\end{figure}

\begin{align*}
\p{\X,\Y}(\x,\y)&=\p{\X_1}(\x_1)\p{\Y_1|\x_1}(\y_1)\p{\X_2|\x_1}(\x_2)\p{\Y_2|\x_2}(\y_2)\ldots\p{\X_t|\x_{t-1}}(\x_t)\p{\Y_t|\x_t}(\y_t)
\end{align*}

}

\subsection{Undirected graphical models}

\frame{
\frametitle{Markov Random Fields (MRFs)}
\begin{itemize}
\item Also called undirected graphical models.
\item For some domains, being force to choose a direction for the edges, as required by BNs, is unnatural.
\item MRFs do not require to specify orientations.
\item They are more natural for domains such as spatial or relational data. Widely used in image analysis and spatial statistics.
\item Less interpretable as BNs.
\item \noteB{More flexible}. Factors are \noteG{arbitrary positive functions} (potentials).
\end{itemize}

}

\frame{

For a random vector $\x$, a MRF is defined as follows
\begin{align*}
\p{\X}(\x)=\frac{1}{Z}\prod_{c=1}^{C}\phi_{c}(\x_c),
\end{align*}
where
\begin{enumerate}
\item $\X_c$ $c=1,\ldots,C$ are non-disjoint groups of variables.
\item All groups are maximal (there is no $(c,d)$ such that $\X_c\subseteq\X_d$).
\item $\phi_{c}(\x_c): \sX^{|C|}\rightarrow \mathbb{R}^{+}$ (a.k.a. potential functions)
\item $Z$ is a normalization constant:
\begin{align*}
Z&=\int_{\sX^n} \prod_{c=1}^{C}\phi_{c}(\x_c) \text{d}\x, \qquad Z=\sum_{\sX^n} \prod_{c=1}^{C}\phi_{c}(\x_c)
\end{align*}
\end{enumerate}

}

\frame{
\begin{itemize}
\item Graphically, we represent a MRF by an undirected graph where all variables in $\X_c$ are connected to each other (clique).
\end{itemize}

\begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.5]{Figuras/Graph_9.pdf}
\end{tabular}
\end{figure}

}
%
%\frame{
%\begin{itemize}
%\item Cliques correspond to maximal groups (maximal cliques).
%\item There is no clique (fully connected subgraph) contained in another clique.
%\end{itemize}
%
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.7]{Figuras/Graph_10.pdf}
%\end{tabular}
%\end{figure}
%
%}
%
%\frame{
%
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.45]{Figuras/Graph_11.pdf}
%\end{tabular}
%\end{figure}
%
%
%
%}
%
\frame{
\frametitle{Evaluating conditional independence in MRF}
\Prop{In a MRF, if a group of conditioned (observed) variables $\X_o$ is such that all paths from $\X_A$ to $\X_B$ go through $\X_o$, then
\begin{align*}
\X_A\perp \X_B| \X_o
\end{align*}
  }

  \begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.45]{Figuras/Graph_12.pdf}
\end{tabular}
\end{figure}

}

\frame{
\frametitle{Markov Blanket in MRFs}

  \begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.45]{Figuras/Graph_13.pdf}
\end{tabular}
\end{figure}

}

\frame{
\frametitle{Markov Blanket in MRFs}

  \begin{figure}
\begin{tabular}{c}
\includegraphics[scale=0.45]{Figuras/Graph_14.pdf}
\end{tabular}
\end{figure}

}

%\frame{
%\frametitle{From BNs to MRFs}
%\begin{itemize}
%\item Converting BNs into MRFs graphs is relatively easy.
%\item However, it typically results into more dense graphs.
%\item Complexity/performance of basic inference methods is related with the sparseness of the graph.
%\item \noteR{Conversion might not be a good idea}.
%\item \noteG{There are some algorithms that are only defined for MRFs}.
%\end{itemize}
%
%}

%
%\frame{
%
%\begin{block}{}
%What is the Markov Blanket of $X_3$?
%\end{block}
%
%  \begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.6]{Figuras/Graph_15_2.pdf}
%\end{tabular}
%\end{figure}
%
%$$\p{\X}(\x)=\p{X_1}(x_1)\p{X_2}(x_2)\p{X_3|x_1,x_2,x_5}(x_3)\p{X_5}(x_5)\p{X_4|x_3,x_5,x_6}(x_4)\p{X_6|x_1}(x_6)$$
%
%}
%
%\frame{
%
%
%  \begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.6]{Figuras/Graph_16_2.pdf}
%\end{tabular}
%\end{figure}
%
%$$\p{\X}(\x)=\color{darkblue}{\overbrace{\p{X_1}(x_1)\p{X_2}(x_2)\p{X_3|x_1,x_2,x_5}(x_3)}^{{\phi_1(\x_1)}}}\color{darkred}{\underbrace{\p{X_5}(x_5)\p{X_4|x_3,x_5,x_6}(x_4)}_{\phi_2(\x_2)}}\color{darkgreen}{\overbrace{\p{X_6|x_1}(x_6)}^{\phi_3(\x_3)}}$$
%
%\begin{exampleblock}{}
%The graph is denser :( ... but we can easily determine the Markov Blanket of $X_3$ :).
%\end{exampleblock}
%
%}
%
%\frame{
%\frametitle{Image Modeling using MRFs}
%
%  \begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.6]{Figuras/Graph_19.pdf}
%\end{tabular}
%\end{figure}
%
%}
%
%\frame{
%\frametitle{Image Modeling using MRFs}
%
%  \begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.6]{Figuras/Graph_20.pdf}
%\end{tabular}
%\end{figure}
%
%}

\subsection{Factor graphs}

\frame{
\frametitle{Factor Graphs}

\begin{itemize}
\item As  MRFs, they are convenient to model soft constraints between random variables that are not naturally given as a conditional probability.
\item  Factor graphs (FGs) make the factorization of a distribution explicit in the graph by introducing additional nodes for the factor themselves.
\item They can preserve more details about the factorization.
\item For the purpose of solving \noteB{inference} problems, it is often \noteR{convenient to convert both directed and undirected graphs into factor graphs.}
\end{itemize}

\begin{exampleblock}{}
In many probabilistic models in communications, the joint distribution is expressed as large product of functions that overlap in their dependencies. In these scenarios, FGs are highly recommended since an equivalent MRF would produce an almost fully connected graph.
\end{exampleblock}

}

\frame{
\frametitle{Factor Graphs (FGs)}
\Def{Given a probability distribution
\begin{align*}
\p{\X}(\x)=\frac{1}{Z}\prod_{j\in\Iset{J}}t_j(\x_{j})
\end{align*}
The FG has a node (represented by a square) for each factor $t_j$, $j\in\Iset{J}$ and a variable node (represented by a circle) for each variable $x_i$, $i=1,\ldots,n$.  For each $x_i\in\x_j$, we place an undirected link between factor $t_j$ and variable $x_i$.
}

\begin{block}{}
FGs simply describe the factorization of functions.
\end{block}

\begin{block}{}
They are bipartite graphs.
\end{block}

}

\frame{

\begin{align*}
\p{\X}(\x)=\frac{1}{Z} t_1(x_1,x_2) t_2(x_2,x_3,x_4) t_3(x_3,x_5)t_4(x_4)t_5(x_4,x_5), \qquad \x\in\set{X}^5
\end{align*}

\begin{center}
\begin{tikzpicture}[scale=1]
\tikzstyle{factor}=[rectangle,minimum size = 5mm, thick, draw =black,fill=black]
\tikzstyle{var}=[circle,minimum size = 5mm, thick, draw =black,fill=black]
\tikzstyle{second}=[circle, minimum size = 10mm, thick]
\tikzstyle{box}=[rectangle, draw=black!100]
\tikzstyle{connect}=[-latex, thick]
\draw[step=5cm];
	\node[var] (x_1) at (-1,0) [label=above:{$X_1$}]{};
	\node[factor] (t_1) at (0,0) [label=above:{$t_1$}]{};
	\node[var] (x_2) at (1,0) [label=above:{$X_2$}]{};
	\node[factor] (t_2) at (2,0) [label=above:{$t_2$}]{};
	\node[var] (x_3) at (3,1) [label=above:{$X_3$}]{};
	\node[factor] (t_3) at (4,1) [label=above:{$t_3$}]{};
	\node[var] (x_4) at (3,-1) [label=left:{$X_4$}]{};
	\node[factor] (t_4) at (3,-2) [label=below:{$t_4$}]{};
	\node[factor] (t_5) at (4,-1) [label=below:{$t_5$}]{};
	\node[var] (x_5) at (5,-1) [label=right:{$X_5$}]{};

	\path
		(x_1) edge []  (t_2)
		(t_2) edge [] (x_3)
		(x_3) edge [] (t_3)
		(t_2) edge [] (x_4)
		(t_4) edge [] (x_4)
		(x_4) edge [] (x_5)
		(t_3) edge [] (x_5);

\end{tikzpicture}
\end{center}

}

\frame{

By defining $\S=[\begin{array}{ccc} X_3 & X_4 & X_5 \end{array}]^T$, the same distribution factorizes as follows

\begin{align*}
\p{\X}(\x)&=\frac{1}{Z} t_1(x_1,x_2) t_2(x_2,x_3,x_4) t_3(x_3,x_5)t_4(x_4)t_5(x_4,x_5)\\\\
\p{\X}(\x)&=\frac{1}{Z} t_1(x_1,x_2) t_2(x_2,\s) t_s(\s),
\end{align*}

\begin{center}
\begin{tikzpicture}[scale=1]
\tikzstyle{factor}=[rectangle,minimum size = 5mm, thick, draw =black,fill=black]
\tikzstyle{var}=[circle,minimum size = 5mm, thick, draw =black,fill=black]
\tikzstyle{second}=[circle, minimum size = 10mm, thick]
\tikzstyle{box}=[rectangle, draw=black!100]
\tikzstyle{connect}=[-latex, thick]
\draw[step=5cm];
	\node[var] (x_1) at (-1,0) [label=above:{$X_1$}]{};
	\node[factor] (t_1) at (0,0) [label=above:{$t_1$}]{};
	\node[var] (x_2) at (1,0) [label=above:{$X_2$}]{};
	\node[factor] (t_2) at (2,0) [label=above:{$t_2$}]{};
	\node[var] (s) at (3,0) [label=above:{$\S$}]{};
	\node[factor] (t_s) at (4,0) [label=above:{$t_s$}]{};

	\path
		(x_1) edge []  (t_2)
		(t_2) edge [] (s)
		 (s) edge [] (t_s);

\end{tikzpicture}
\end{center}

\begin{block}{}
Factors can be merged/split arbitrarily, allowing further algorithmic development for efficient inference.
\end{block}

\begin{alertblock}{Clustering of variable/factor nodes}
It does not changes the global probability distribution but defines a new graph where, at the expense of more complex potential functions and losing representation of conditional independency constraints,  approximate inference methods provide more accurate estimates.
\end{alertblock}

}

%
%\frame{
%However, assessing CI can be difficult in FGs
%\begin{figure}
%\begin{tabular}{c}
%\includegraphics[scale=0.6]{Figuras/Graph_24.pdf}
%\end{tabular}
%\end{figure}
%
%
%}

%\frame{
%\frametitle{Summarizing...}
%
%\begin{itemize}
%\item For most the  applications in communications of efficient inference over GMs, we will use FGs.
%\item In other fields, BNs and MRFs are widely used.
%\item Algorithms may require specific GMs representations.
%\item In any case, recall that the graph properties (density, cycles, minimum distance paths etc...) and CI between regions of the graph are extremely linked with how efficient can inference be made.
%\end{itemize}
%
%
%}

%\section{Examples in Communications}

%\frame{

%}

\section{Factor graphs in digital communications scenarios}

\subsection{Transmission of linear block codes over a memoryless channel}
\frame{
\frametitle{Transmission of linear block codes over a memoryless channel}
\begin{itemize}
\item A binary codeword $\x=(x_1,x_2,\ldots,x_\n)$ is randomly selected from a linear block code $\C$ of length $\n$.
\item Assume a Hamming (7,4) linear block code with $\n=7$ and parity check matrix:
\begin{align*}
\H=\left[ \begin{array}{ccccccc} 1 & 0 & 1 & 0 & 1 & 0 & 1\\ 0 & 1 & 1 & 0 & 0 & 1 & 1\\ 0 & 0 & 0 & 1 & 1 & 1 & 1\end{array}\right],
\end{align*}
\item Hence, every codeword $\x=(x_1,x_2,\ldots,x_7)$ have to fulfill the following parity-check equations:
\begin{align*}
x_1\oplus x_3\oplus x_5\oplus x_7=0\\
x_2\oplus x_3\oplus x_6\oplus x_7=0\\
x_4\oplus x_5\oplus x_6\oplus x_7=0
\end{align*}
\end{itemize}
}

\frame{
\begin{itemize}
\item  $\x\in\C$ is transmitted over a memoryless channel so that every output $y_i$ depends just on $x_i$.
\item For a given channel observation $\y$, the joint posterior probability of the transmitted codeword is expressed as
\begin{align*}
\p{\X|\y}(\x)=\frac{\p{\Y|\x}(\y)\;\p{\X}(\x)}{\p{\Y}(\y)}\propto\p{\X}(\x)\prod_{i=1}^{n}\p{Y_i|x_i}(y_i),
\end{align*}
where
\begin{align*}
\p{\X}(\x)=\frac{\ind[\x\H^T\eqtwo\ve{0}]}{|\C|}.
\end{align*}
\item This last term can be further factorized. Let  $\ve{d}(\x)\doteq\x\H$ (mod 2) be the \emph{syndrome} vector, of length $n-k$ (number of rows of $\H$)
\begin{align*}
\p{\X}(\x)=\frac{\displaystyle\prod_{j=1}^{n-k}\ind[d_j(\x)\eqtwo0]}{|\C|}
\end{align*}
\end{itemize}

}

\frame{
Finally ...
\begin{align*}
\p{\X|\y}(\x)=\frac{1}{|\C|\p{\Y}(\y)}\prod_{j=1}^{n-k}\ind\left[d_j(\x)\eqtwo0 \right] \prod_{i=1}^{\n}\p{Y_i|x_i}(y_i).
\end{align*}

\begin{figure}[h]
\centering\includegraphics[scale=0.6]{Figuras/Fig1_3.pdf}
 \end{figure}

}

\frame{
\begin{block}{Block-wise maximum a posteriori decoder (block-MAP decoding)}
\begin{align*}
\hat{\x}=\arg\max_{\x\in\C}\p{\X|\y}(\x)=\arg\max_{\x\in\C}\prod_{j=1}^{n-k}\ind[d_j(\x)\eqtwo0]\prod_{i=1}^{n}\p{Y_i|x_i}(y_i),
\end{align*}
\end{block}
\begin{exampleblock}{Symbol-wise maximum a posteriori decoder (symbol-MAP)}
\begin{align*}
\hat{x}_i&=\arg\max_{x_i\in\{0,1\}}\p{X_i|\y}(x_i)=\arg\max_{x_i\in\{0,1\}} \sum_{\x_{\sim i}\in\{0,1\}^{n-1}}\p{\X|\y}(\x)\\
&=\arg\max_{x_i\in\{0,1\}} \sum_{\x_{\sim i}\in\{0,1\}^{n-1}} \prod_{j=1}^{n-k}\ind[d_j(\x)\eqtwo0]\prod_{i=1}^{n}\p{Y_i|x_i}(y_i)
\end{align*}
\end{exampleblock}
\begin{itemize}
\item \noteR{Both have $\mathcal{O}(2^n)$ complexity.}
\end{itemize}
}

\subsection{Symbol transmission and detection over ISI channels}
\frame{
\frametitle{Symbol transmission and detection over ISI channels}
\begin{itemize}
\item Consider a sequence of $\t$ i.i.d. $M$-ary  complex-valued  symbols, $\sk$, obtained by the encoding of a sequence of information bits.
\item $s_k\in\AQAM$ and $|\AQAM|=M$.
\item  ISI channel that also introduces additive white Gaussian noise (AWGN):
\begin{align*}
y_k=\sum_{i=0}^{\chl}h_{i}s_{k-i}+w_k, \qquad w_k\sim\mathcal{CN}(\ve{0},\sigma_w^2\mathbf{I})
\end{align*}
\item The sequence of symbols $\s$ is preceded and terminated by a sequence of $\chl$ deterministic known symbols: $s_k=s^*$ for $k\in\{-\chl+1,\ldots,0\}$ and for $k\in\{\t+1,\ldots,\sk+\chl\}$.
\item $\y=(y_1,y_2,\ldots,y_{\t+\chl})$ is the observation vector.
\end{itemize}

%
%\begin{center}
%\begin{tikzpicture}[scale=1]
%\tikzstyle{factor}=[rectangle,minimum size = 5mm, thick, draw =black,fill=black]
%\tikzstyle{var}=[circle,minimum size = 5mm, thick, draw =black,fill=black]
%\tikzstyle{second}=[circle, minimum size = 10mm, thick]
%\tikzstyle{box}=[rectangle, draw=black!100]
%\tikzstyle{connect}=[-latex, thick]
%\draw[step=5cm];
%	\node[var] (x_1) at (-1,0) [label=above:{$X_1$}]{};
%	\node[factor] (t_1) at (0,0) [label=above:{$t_1$}]{};
%	\node[var] (x_2) at (1,0) [label=above:{$X_2$}]{};
%	\node[factor] (t_2) at (2,0) [label=above:{$t_2$}]{};
%	\node[var] (x_3) at (3,1) [label=above:{$X_3$}]{};
%	\node[factor] (t_3) at (4,1) [label=above:{$t_3$}]{};
%	\node[var] (x_4) at (3,-1) [label=left:{$X_4$}]{};
%	\node[factor] (t_4) at (3,-2) [label=below:{$t_4$}]{};
%	\node[factor] (t_5) at (4,-1) [label=below:{$t_5$}]{};
%	\node[var] (x_5) at (5,-1) [label=right:{$X_5$}]{};
%
%
%	\path
%		(x_1) edge []  (t_2)
%		(t_2) edge [] (x_3)
%		(x_3) edge [] (t_3)
%		(t_2) edge [] (x_4)
%		(t_4) edge [] (x_4)
%		(x_4) edge [] (x_5)
%		(t_3) edge [] (x_5);
%
%\end{tikzpicture}
%\end{center}

}

\frame{
\begin{align*}
\p{\S|\y}(\s)=\frac{\color{darkred}{}\p{\Y|\s}(\y)\color{black}\p{\S}(\s)}{\p{\Y}(\y)}%\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
\end{align*}
%where
%\begin{itemize}
%\item $\p{S_k}(s_k)=1/M$ for $s_k\in\AQAM$ for $k=1,\ldots,s$.
%\item $\p{S_k}(s_k)=\ind[S_k=s^*]$ for $k=\t+1,\ldots,\t+\chl$.
%\end{itemize}
}

\frame{
\begin{align*}
&\p{\S|\y}(\s)=\frac{\color{darkred}{}\p{\Y|\s}(\y)\color{black}\p{\S}(\s)}{\p{\Y}(\y)}\\\\%\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
&\p{\Y|\s}(\y)=\prod_{k=1}^{\t+\chl}\p{Y_k|\s}(y_k)%=\prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k)
\end{align*}
%where
%\begin{itemize}
%\item $\p{S_k}(s_k)=1/M$ for $s_k\in\AQAM$ for $k=1,\ldots,s$.
%\item $\p{S_k}(s_k)=\ind[S_k=s^*]$ for $k=\t+1,\ldots,\t+\chl$.
%\end{itemize}
}

\frame{
\begin{align*}
&\p{\S|\y}(\s)=\frac{\color{darkred}{}\p{\Y|\s}(\y)\color{black}\p{\S}(\s)}{\p{\Y}(\y)}\\\\%\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
&\p{\Y|\s}(\y)=\prod_{k=1}^{\t+\chl}\p{Y_k|\s}(y_k)=\color{darkred}{}\prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{k-\chl}^{k}}(y_k)
\end{align*}
%where
%\begin{itemize}
%\item $\p{S_k}(s_k)=1/M$ for $s_k\in\AQAM$ for $k=1,\ldots,s$.
%\item $\p{S_k}(s_k)=\ind[S_k=s^*]$ for $k=\t+1,\ldots,\t+\chl$.
%\end{itemize}
}

\frame{
\begin{align*}
&\p{\S|\y}(\s)=\frac{\color{darkred}{}\p{\Y|\s}(\y)\color{darkgreen}\p{\S}(\s)}{\p{\Y}(\y)}\\\\%\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
&\p{\Y|\s}(\y)=\prod_{k=1}^{\t+\chl}\p{Y_k|\s}(y_k)=\color{darkred}{}\prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{k-\chl}^{k}}(y_k)\\\\
&\color{darkgreen}\p{\S}(\s)=\prod_{u=-\chl+1}^{\t+\chl}\p{S_u}(s_u)
%\\\\&\color{darkgreen}{}\p{S_u}(s_u)=1/M \text{ for } s_u\in\AQAM \text{ and } u=1,\ldots,s\\\\
\end{align*}
}

\frame{
\begin{align*}
&\p{\S|\y}(\s)=\frac{\color{darkred}{}\p{\Y|\s}(\y)\color{darkgreen}\p{\S}(\s)}{\p{\Y}(\y)}\\\\%\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
&\p{\Y|\s}(\y)=\prod_{k=1}^{\t+\chl}\p{Y_k|\s}(y_k)=\color{darkred}{}\prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{k-\chl}^{k}}(y_k)\\\\
&\color{darkgreen}\p{\S}(\s)=\prod_{u=-\chl+1}^{\t+\chl}\p{S_u}(s_u)
\\\\&\color{darkgreen}{}\p{S_u}(s_u)=1/M \text{ for } s_u\in\AQAM \text{ and } u=1,\ldots,s\\\\
\end{align*}
}

\frame{
\begin{align*}
&\p{\S|\y}(\s)=\frac{\color{darkred}{}\p{\Y|\s}(\y)\color{darkgreen}\p{\S}(\s)}{\p{\Y}(\y)}\\\\%\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
&\p{\Y|\s}(\y)=\prod_{k=1}^{\t+\chl}\p{Y_k|\s}(y_k)=\color{darkred}{}\prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{k-\chl}^{k}}(y_k)\\\\
&\color{darkgreen}\p{\S}(\s)=\prod_{u=-\chl+1}^{\t+\chl}\p{S_u}(s_u)
\\\\&\color{darkgreen}{}\p{S_u}(s_u)=1/M \text{ for } s_u\in\AQAM \text{ and } u=1,\ldots,\t\\\\
&\color{darkgreen}{}\p{S_u}(s_u)= \ind[s_u=s^*] \text{ for }  u\in\{-\chl+1,\ldots,0\} \text{ and } u\in\{\t+1,\ldots,\chl+\chl\}
\end{align*}
}

%\frame{
%\begin{align*}
%\p{\S|\y}(\s)=\frac{\p{\Y|\s}(\y)\p{\S}(\s)}{\p{\Y}(\y)}\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{j=k-\chl}^{k}}(y_k) \p{S_k}(s_k)
%% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
%\end{align*}
%where
%\begin{itemize}
%\item $\p{S_k}(s_k)=1/M$ for $s_k\in\AQAM$ for $k=1,\ldots,s$.
%\item $\p{S_k}(s_k)=\ind[S_k=s^*]$ for $k=\t+1,\ldots,\t+\chl$.
%\end{itemize}
%
%
%}

\frame{

\begin{align*}
\p{\S|\y}(\s)=\frac{\p{\Y|\s}(\y)\p{\S}(\s)}{\p{\Y}(\y)}\propto \prod_{k=1}^{\t+\chl}\p{Y_k|\{s_j\}_{k-\chl}^{k}}(y_k) \prod_{u=-\chl+1}^{\t+\chl}\p{S_u}(s_u)
% \cdot \prod_{k=t+1}^{\t+\chl}\p{Y_k|\{S_j=s_j\}_{j=k-\chl}^{t}}(y_k)
\end{align*}

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.7]
\tikzstyle{factor}=[rectangle,minimum size = 4mm, thick, draw =black,fill=black]
\tikzstyle{factorg}=[rectangle,minimum size = 4mm, thick, draw =lgray,fill=lgray]
\tikzstyle{var}=[circle,minimum size = 4mm, thick, draw =black,fill=black]
\tikzstyle{varg}=[circle,minimum size = 4mm, thick, draw =lgray,fill=lgray]
\tikzstyle{connect}=[-latex, thick]

	\node[var] (s0) at (0,2) [label=left:{$S_1$}]{};
	\node[factor] (fs0) at (0,4) [label=above:{$\p{S_1}$}]{};
	\node[factor] (fy0) at (0,0) [label=below:{$\p{Y_1|s_{-2},\ldots,s_1}$}]{};

	\node[var] (s1) at (2,2) [label=left:{$S_2$}]{};
	\node[factor] (fs1) at (2,4) [label=above:{$\p{S_2}$}]{};
	\node[factor] (fy1) at (2,0) [label=below:{}]{};

	\node[var] (s2) at (4,2) [label=left:{$S_3$}]{};
	\node[factor] (fs2) at (4,4) [label=above:{$\p{S_3}$}]{};
	\node[factor] (fy2) at (4,0) [label=below:{}]{};

	\node[var] (s3) at (6,2) [label=left:{$S_4$}]{};
	\node[factor] (fs3) at (6,4) [label=above:{$\p{S_4}$}]{};
	\node[factor] (fy3) at (6,0) [label=below:{$...$}]{};

	\node[varg] (s4) at (8,2) [label=left:{\color{gray}$S_5$}]{};
	\node[factorg] (fs4) at (8,4) [label=above:{}]{};
	\node[factor] (fy4) at (8,0) [label=below:{}]{};

	\node[varg] (s5) at (10,2) [label=left:{\color{gray}$S_6$}]{};
	\node[factorg] (fs5) at (10,4) [label=above:{}]{};
	\node[factor] (fy5) at (10,0) [label=below:{}]{};

	\node[varg] (s6) at (12,2) [label=left:{\color{gray}$S_7$}]{};
	\node[factorg] (fs6) at (12,4) [label=above:{}]{};
	\node[factor] (fy6) at (12,0) [label=below:{$\p{Y_7|s_4,\ldots,s_7}$}]{};

%	\node[var] (s7) at (14,2) [label=left:{$S_7$}]{};
%	\node[factor] (fs7) at (14,4) [label=above:{$\ind[s_7=s^*]$}]{};
%	\node[factor] (fy7) at (14,0) [label=below:{$\p{Y_7|s_4,\ldots,s_7}$}]{};

	\path

		(fs0) edge [] (fy0)
		(fs1) edge [] (fy1)
		(fs2) edge [] (fy2)
		(fs3) edge [] (fy3);

		%(fs7) edge [] (fy7);
                 ;
	\path
		(s0) edge [] (fy1)
		(s0) edge [] (fy2)
		(s0) edge [] (fy3);

	\path
		(s1) edge [] (fy2)
		(s1) edge [] (fy3)
		(s1) edge [] (fy4);

	\path
		(s2) edge [] (fy3)
		(s2) edge [] (fy4)
		(s2) edge [] (fy5);

	\path
		(s3) edge [] (fy4)
		(s3) edge [] (fy5)
		(s3) edge [] (fy6);

\path[font=\scriptsize,gray,line width=0.05mm]
		(s6) edge [] (fy6)
		(s5) edge [] (fy6)
		(s5) edge [] (fy5)
		(s4) edge [] (fy5)
		(s4) edge [] (fy6)
		(s4) edge [] (fy4)
		(fs4) edge [] (s4)
		(fs5) edge [] (s5)
		(fs6) edge [] (s6);

\end{tikzpicture}
%\caption{Factor graph associated to $\p{\S,\ve{\Phi}|\Y=\y}(\s,\ve{\ve{\phi}})$ in \EQ{2.16} }\LABFIG{2.2}
\caption{Factor graph representation of $\p{\S|\y}(\s)$  for the case $\chl=3$ and $\t=4$.}\LABFIG{1.4}
\end{figure}

\begin{exampleblock}{}
This FG has cycles. By clustering nodes we can obtain a cycle-free representation.
\end{exampleblock}

}

\frame{
\begin{itemize}
\item \snoteR{\emph{State} variable at time $k$, $E_k$}. Given a realization of the $\chl$ symbols transmitted immediately before time $k$, i.e., $\sg_{k}=\{s_{k-\chl},s_{k-\chl+1},\ldots,s_{k-2},s_{k-1}\}$, then
\begin{align*}
e_k=f(\sg_{k}): \AQAM^{\chl} \rightarrow \{1,2,\ldots, M^\chl\}
\end{align*}
\item \snoteG{$E_k$ is a R.V. that takes $M^\chl$ possible values. }
\end{itemize}
\begin{itemize}
\item However, there is a deterministic (one-to-one) relationship between $\sg_{k}$ and $e_k$.
\end{itemize}
}

\frame{
\begin{itemize}
%\item \snoteR{\emph{State} variable at time $k$}. Let $\s_{k}=\{s_{k-\chl},s_{k-\chl+1},\ldots,s_{k-2},s_{k-1}\}$, then
%\begin{align*}
%e_k=f(\s_{k}): \AQAM^{\chl} \rightarrow \{1,2,\ldots, M^\chl\}
%\end{align*}
%\item \snoteG{$e_k$ is a R.V. that takes $M^\chl$ possible values.}
\item The joint posterior probability of the symbols  $\S$ and states $ \seqSt$ can be expressed as
\begin{align*}
\only<1-4>{\p{\S,\seqSt|\y}(\s,\seqst)=\frac{\snoteR{\p{\Y|\seqst}(\y)}~ \snoteB{\p{\seqSt|\s}(\seqst)}~ \snoteG{\p{\S}(\s)}}{\p{\Y}(\y)}}
\end{align*}
where
\begin{align*}
\only<1-1>{}
\end{align*}
\begin{align*}
\only<2-4>{\snoteR{\p{\Y|\seqst}(\y)}&=\prod_{k=1}^{\t+\chl}\p{Y_k|\st{k},\st{k+1}}(y_k)}
\\
\only<3-4>{\snoteB{\p{\seqSt|\s}(\seqst)}&=\ind[\st{1}=\st{}^*]\ind[\st{\t+\chl+1}=\st{}^*]\prod_{k=1}^{\t+\chl}
\ind[e_k\cup s_k\rightarrow e_{k+1}]}\\
\only<4-4>{\snoteG{\p{\S}(\s)}&=\prod_{k=1}^{\t+\chl}\p{S_k}(s_k)}
\end{align*}
\end{itemize}

}

\frame{
 If we finally define
\begin{align*}
t_k(\st{k},\st{k+1},s_k)=\p{Y_k|\st{k},\st{k+1}}(y_k)~ \ind[\st{k}\cup s_k\rightarrow \st{k+1}]~
\end{align*}
\only<2-2>{
Then...
\begin{exampleblock}{}
\begin{align*}
\p{\S,\seqSt|\y}(\s,\seqst)\propto \ind[\st{1}=\st{}^*] \ind[\st{\t+\chl+1}=\st{}^*] \prod_{k=1}^{\t+\chl} \p{S_k}(s_k)t_k(\st{k},\st{k+1},s_k)
\end{align*}
\end{exampleblock}
}

}

\frame{
\begin{align*}
\p{\S,\seqSt|\y}(\s,\seqst)\propto \ind[\st{1}=\st{}^*] \ind[\st{\t+\chl+1}=\st{}^*] \prod_{k=1}^{\t+\chl} \p{S_k}(s_k)t_k(\st{k},\st{k+1},s_k)
\end{align*}

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.3]
\tikzstyle{factor}=[rectangle,minimum size = 3mm, thick, draw =black,fill=black]
\tikzstyle{var}=[circle,minimum size = 3mm, thick, draw =black,fill=black]
\tikzstyle{connect}=[-latex, thick]

	\node[factor] (tphi0) at (0,0) [label=below:{$\ind[\st{1}=\st{}^*]$}]{};
	\node[var] (phi0) at (2,0) [label=above:{$\st{1}$}]{};
	\node[factor] (tphi1) at (4,0) [label=below:{$t_1$}]{};
	\node[var] (s0) at (4,3) [label=left:{$S_1$}]{};
	\node[factor] (fs0) at (4,6) [label=above:{$\p{S_1}$}]{};

	\node[var] (phi1) at (6,0) [label=above:{$\st{2}$}]{};
	\node[factor] (tphi2) at (8,0) [label=below:{$t_2$}]{};
	\node[var] (s1) at (8,3) [label=left:{$S_2$}]{};
	\node[factor] (fs1) at (8,6) [label=above:{$\p{S_2}$}]{};

	\node[var] (phi2) at (10,0) [label=above:{$\st{3}$}]{};
	\node[factor] (tphi3) at (12,0) [label=below:{$t_3$}]{};
		\node[var] (s2) at (12,3) [label=left:{$S_3$}]{};
	\node[factor] (fs2) at (12,6) [label=above:{$\p{S_3}$}]{};

	\node[var] (phi3) at (14,0) [label=above:{$\st{4}$}]{};
	\node[factor] (tphi4) at (16,0) [label=below:{$t_4$}]{};
	\node[var] (s3) at (16,3) [label=left:{$S_4$}]{};
	\node[factor] (fs3) at (16,6) [label=above:{$\p{S_4}$}]{};

	\node[var] (phi4) at (18,0) [label=above:{$\st{5}$}]{};
	\node[factor] (tphi5) at (20,0) [label=below:{$t_5$}]{};
	%\node[var] (s4) at (20,3) [label=left:{$S_5$}]{};
	%\node[factor] (fs4) at (20,6) [label=above:{$\p{S_5}$}]{};

	%\node[factor] (fs5) at (24,6) [label=above:{$\p{S_6}$}]{};
	%\node[var] (s5) at (24,3) [label=left:{$S_6$}]{};
	\node[var] (phi5) at (22,0) [label=above:{$\st{6}$}]{};
	\node[factor] (tphi6) at (24,0) [label=below:{$t_6$}]{};

	%\node[factor] (fs6) at (28,6) [label=above:{$\p{S_7}$}]{};
	%\node[var] (s6) at (28,3) [label=left:{$S_7$}]{};
	\node[var] (phi6) at (26,0) [label=above:{$\st{7}$}]{};
	\node[factor] (tphi7) at (28,0) [label=below:{$t_7$}]{};
	\node[var] (phi7) at (30,0) [label=above:{$\st{8}$}]{};
	\node[factor] (tphi8) at (32,0) [label=right:{$\ind[\st{8}=\st{}^*]$}]{};

	\path
		(tphi0) edge [] (tphi8)
		(fs0) edge [] (tphi1)
		(fs1) edge [] (tphi2)
		(fs2) edge [] (tphi3)
		(fs3) edge [] (tphi4);

\end{tikzpicture}
\caption{Factor graph associated to $\p{\S,\seqSt|\y}$ for the case $\chl=3$ and $\t=4$.}\LABFIG{1.5}
\end{figure}

}

\frame{

\begin{block}{}
Beyond visualization, the fact that the FG in the latter case is cycle-free has important practical implications to construct efficient inference methods.
\end{block}
\vspace{0.5cm}

Indeed, because of the FG structure the complexity of
\begin{align*}
\hat{s}_k &=\arg\max_{s_k\in\AQAM} \p{s_k|\y}(\s)=\arg\max_{s_k\in\AQAM} \sum_{\s_{\sim k}\in\AQAM^{\t-1}} \sum_{\seqst} \p{\S,\seqSt|\y}(\s,\seqst)\\
\hat{\s}&=\arg\max_{\s\in\AQAM^\t} \p{\S,\seqSt|\y}(\s,\seqst)
\end{align*}
can be reduced to \noteR{$\mathcal{O}(M^\t)$} to  \noteB{$\mathcal{O}(\t M^\chl)$}.
}

\end{document}


\documentclass[11pt]{amsart}
\usepackage{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   % ... or a4paper or a5paper or ...
%\geometry{landscape}                % Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}

\title{Brief Article}
\author{The Author}
%\date{}                                           % Activate to display a given date or no date

\begin{document}
%\maketitle
%\section{}
%\subsection{}

Consider ODE
$$|u'| -1 = 0, \hbox{ on } (-1, 1), \quad u(\pm 1) = 0.$$
\begin{itemize}
\item
Prove that $u(x) = |x|-1$ is a viscosity solution.
\item Can you prove comparison principle?
\end{itemize}

Consider
$$\inf_{a\in (-1, 1)} \{(1+a^2) u''\}  = 0, \hbox{ on } (-1, 1).$$
Is it uniformly elliptic?
\end{document}


		\documentclass[10pt,a4paper]{book}
		\usepackage[latin1]{inputenc}
		\usepackage{amsmath}
		\usepackage{amsfonts}
		\usepackage{amssymb}
		\usepackage{graphicx}
		\usepackage{float}
		\usepackage{hyperref}
		\hypersetup{
			colorlinks,
			citecolor=black,
			filecolor=black,
			linkcolor=black,
			urlcolor=black
		}
	\DeclareMathOperator{\E}{\mathbb{E}}
		\begin{document}
			\tableofcontents
			\newpage
			\chapter{Base concepts}
			\section{Density}
			$\dfrac{\text{edges of graph g}}{\text{num edges complete graph with n nodes}}$
			\section{BFS}
			\begin{itemize}
				\item Each node has three states: unvisited, visited, explored
				\item When a node is visited all non-visited neighbours are added into a FIFO queque, then the node is set as explored
				\item First, the starting node is explored
				\item While the queque is not empty:
				\begin{enumerate}
					\item Pick the first node in the queque
					\item Visit the node
				\end{enumerate}
			\item Each time a node is inserted into the queue, we also associate to this node its parent
			\item Property of BFS in unweighted graphs: the nodes at level i in the BFS tree are also the nodes at distance i in the graph
			\item When the graph is undirected it also allow us to know if the graph is connected (check the bfs tree contains all the nodes)
			\item You can know also the connected components of the graph doing a BFS from the non visited nodes while all are not explored
			\item Time complexity: each edge is considered two times: one when an extreme is visited and another when the other extreme is visited

			\end{itemize}
			\section{DFS}
			\begin{itemize}
				\item Same of BFS but instead of a FIFO queque use a LIFO queque or recursion
				\item Property of DFS: the dfs not include only the edges that link a node \textit{x} to its ancestor
				\item With the latter property we can use the DFS to check if a graph g contains a cycle
			\end{itemize}
			\section{Terms}
			\subsection{Subgraph}
			Subset of nodes of a graph g and all edges that links these nodes
			\subsection{Connected components}
			Maximal subgraph contained in the graph g where cannot be inserted more nodes
			\subsection{Graph connected}
		 A graph is connected if exists only one connected component
		 \subsection{Strongly connected components (direct graph)}
		 A direct graph is strongly connected contains only one maximal subgraph where exists a path from x to y \textit{and} from y to x
			\chapter{Algorithm for SCC in a direct graph}
			The problem is that when we perform the DFSs we can visit different connected components.
			\section{DAG}
			A DAG is a Directed Acyclic Graph. To check if a graph is a DAG with a DFS (see above).\\
			\section{Topologicals ordering}
			With a DAG you can sort the nodes in a \textit{topological ordering} with some alters to the DFS algorithm:
			\begin{itemize}
				\item 	We denote as s(x) the time when a node x is visited and as f(x) the time when the invocation of dfs on the node ends
				\item When a node is visited the time indicator is increased and the the invocation ends the \textit{time} counter is increased then assigned as finished time
				\item At the end we can sort the nodes by finishing time
				\item If all nodes are not reached, remove all the reached nodes from the graph and then start a new DFS while all nodes are not removed
			\end{itemize}
			We demonstrate the following property: given a DAG, if exists an edge between a pair of nodes x and y $ (x \rightarrow y)  $ then $  f(x) > f(y) $:\\
			There are two possible option when the DFS is invoked with argument x:
			\begin{enumerate}
				\item y is explored or y is not visited, otherwise is an ancestor edge but this not respect the hypotheses that g is a DAG. In the first case y is yet explored so it have assigned the  finished time while x not so is surely more than f(y).
				\item In the second case x is visited but the finished time is assigned after all the neighbours are explored (included y) so we return to the latter case
			\end{enumerate}
			\section{Component Graph}
			A component graph is a graph where the nodes are the SCC (strongly connected components) of g and exists an edge between $ c_1 $ and $ c_2 $ if exists one of the two options:
			\begin{itemize}
				\item exists an edge from a node in $ c_1 $ to $ c_2 $
				\item exists an edge from a node in $ c_2 $ to $ c_1 $
			\end{itemize}
			Any component graph is a DAG: for absurd if exists a bidirectional edge between $ c_1 $ and $ c_2 $ so the two nodes are not maximal so $ c_1 $ and $ c_2 $ should be an unique node
			\section{Graph transpose}
			The transpose $ G^T $ of a graph G is a graph where the edge directions are inverted. E.G. $ (a,b) \in G \rightarrow (b,a) \in G^T $
			\section{Kosaraju-Sharir algorithm}
			Base intuition: if we start the DFS in the last SCC of component graph (the SCC with only entering edge) then the DFS tree will include only the nodes of the SCC. Then remove the explored nodes from the graph and restart the DFS until all nodes are removed.\\
			Problem: We don't know the SCCs, that is what we want to find!\\
			Let's give some definitions:
			\begin{itemize}
				\item \textit{$ f(c) $}: largest finished time of a node in a connected component \textit{c}
				\item \textit{$ s(c) $}: smallest finished time of a node in a connected component \textit{c}
			\end{itemize}
			Let's prove the following assert: given $ c_1 $ and $ c_2 $ two connected components such as exists the edge $ (u,v) $ where $ u \in c_1 $ and $ v \in c_2 $, then $ f(c_1) > f(c_2) $.
			\subsection{Demonstration}
			We distinguish two cases:
			\begin{itemize}
				\item $ s(c_1) < s(c_2) $: Given \textit{x} the first node explored in $ c_1  $ then the DFS will find the edge $ (u,v) $ where $ u \in c_1 $ and $ v \in c_2 $ that continue the DFS in $ c_2 $. So it's obvious that for, how works the DFS and how are assigned the finishing times, the finishing time are first assigned to $ c_2 $ because the recursive calls through nodes in $ c_2 $ ends until reaches the node $ v $ that continue the assignment of the finishing time in $ c_1 $.\\
				In conclusion we can infer the following dis-equation:
				\begin{center}
				$ f(x) = f(c_1) > f(u) > f(v)  = f(c_2) $
				\end{center}
				\item $ s(c_1) > s(c_2) $: In this case we start in $ c_2 $ but we can infer that not exists a path  from $c_2$ to $ c_1 $ because the component graph is a DAG and exists for hypothesis the edge $ (u,v) $ where $ u \in c_1 $ and $ v \in c_2 $. So we can assert that the current DFS will not explore $ c_1 $ but a successive DFS will explore $ c_1 $ so $ f(c_1) > f(c_2) $
			\end{itemize}
			So we have demonstrate the latter assert.\\
		 From this proposition we can infer that if we take the node with the largest finished time we are surely in a SCC with only outgoing edges in the component graph! So if we do a DFS in the transpose graph we explore only the nodes in its connected component.\\
		 In conclusion the Kosaraju-Sharir algorithm is the following:
		 \begin{enumerate}
			 	\item Do DFS until all nodes have finishing time
			 	\item Transpose the graph
			 	\item Do DFS starting from the node with largest finishing. This is a connected component
			 	\item Remove all node explored
			 	\item Go to 3 until the graph have at least one node
		 \end{enumerate}
		 	Complexity: $ O(m) $
		 	\section{CC in social networks}
		 	\subsection{Giant component}
		 	A giant  component is a (strongly) connected component that contains a constant fraction of the nodes
		 	\subsection{Giant components in SN}
		 	The giant components is SN are very commons. \\
		 	In the web directed graph a common phenomenon is the \textit{bowtie phenomenon}: it says that we can classify the nodes in the following groups:
		 	\begin{itemize}
		 		\item \textit{SCC}: Nodes contained in a big giant strongly connected component
		 		\item \textit{IN}: the set of nodes that can only reach the \textit{SCC}
		 		\item \textit{OUT}: the set of nodes that can be reached from the \textit{SCC}
		 		\item \textit{tendrils}: nodes that can be reached only from \textit{IN} but are not part of \textit{SCC} and nodes that can reach \textit{OUT} but they are not part of \textit{SCC}
		 		\item \textit{disconnected}: nodes that can't reach the \textit{SCC} also if the graph is undirected
		 	\end{itemize}
		 	\section{Shortest path}
		 	In the case of undirected edges, the shortest path between two nodes x and y  is the minimum numbers of edges needed to reach y from x. It can be computed performing a BFS starting from x.\\
		 	There are two variants of this problem:
		 	\begin{itemize}
		 		\item SSSP (Single Source Shortest Path) that consists to find all the shortest path from a specific source
		 		\item APSP (All Pairs Shortest Path) that consists in find all the shortest path between all the pairs of nodes
		 	\end{itemize}
	 \chapter{Dijkstra's algorithm}
	 The Dijkstra's algorithm solves the SSSP problem in the case of weighted edges when the weights are positive. It is a \textit{greedy} algorithm, so it take the best decision only considering the current state and not the overall procedure.\\
	 At the beginning choose a starting node \textit{s} and then initialize a distance array $ dist $ with $ dist[s] = 0 $ and $ dist[x]  = \infty \,\, \forall x \neq s $ \\
	 The greedy step computed by Dijkstra is the following:
	 \begin{enumerate}
		 	\item choose a node \textit{x} not yet picked and that has the minimum $ dist[x] $
		 	\item for each neighbour $ y \in N(x) $ set $ dist[y] = \min(dist[y], \,\,dist[x] + d(x,y)) $
	 \end{enumerate}
		\section{Demonstration of the correctness of  Dijkstra's algorithm}
		To demonstrate the Dijkstra's algorithm we have to prove that given \textit{x} the node chosen at the step \textit{i} then $ dist[x] $ must be the minimum from \textit{s} to \textit{x}.\\
			Let's introduce some terms: $ A_i $ is the set of nodes picked at the step \textit{i} and $ d_i(x) $ the shortest path only through the nodes in $ A_i $.\\
			First, we prove by induction that $ dist[x] $ at the step i is equal to $ d_i(x) $:
			\begin{itemize}
				\item \textit{base case (i = 1)}  this case is simple: the shortest path from s to the neighbours $ y $ is simply $ d(s, y) = d_1(y) $
				\item \textit{inductive step} $ (k \rightarrow k+1) $ Let's suppose that the proof is valid until k so we have that $ dist[x] = d_k(n) \,\, \forall n \in A_k $. Given \textit{x} the node picked at the step k+1 and $ \pi_{k+1}(y) $ the shortest path from s to y through the nodes in $ A_{k+1} = A_k  \cup \{x\}$, for each edge $ (x, y) $ we have the two following cases about y:
				\begin{itemize}
					\item $ x \notin\pi_{k+1}(y)  $ it means that x is not in the shortest path from s to y not pass through x so we have that $ \pi_{k}(y)  = \pi_{k+1}(y)  $
					\item $ x \in\pi_{k+1}(y)  $  it means that the shortest path to y pass through x so we have that $ d_{k+1}(y) = d_k(x) + w(x,y) $
				\end{itemize}
			\end{itemize}
		Now we show that \textit{if x is the node chosen at the step i, a shortest path from s to x pass only through $ A_i $}.
		We demonstrate for absurd, so given \textit{y} the first node not in $ A_i $ that is in the shortest path of $ x  $ from $ s $ we can infer the following propositions:
		\begin{itemize}
			\item Since Dijkstra choose the node with minimum distance, $ d_i(x) =  dist[x]  \leq dist[y] = d_i(y) $
			\item $ d(s,y) < d(s,x) $ because all weights are positive and y is in the shortest path of x
			\item $ d(s,x) < d_i(x) $ since we are assuming that a shortest path from s to x not pass through nodes in $ A_i $
		 	\item  $ d(s,y) = d_i(y) $	since y is the first node outside of $ A_i $ (see above demonstration)
	\end{itemize}
	With the latter propositions we have an absurd because:\\ $ d(s,y) < d(s,x) < d_i(x) \leq d_i(y) = d(s,y) $\\
	\section{Complexity of Dijkstra's algorithm}
	The complexity depends on the data structure used to represent $ dist $.\\
	There are three types of time which complexity of Dijkstra's algorithm depends on:
	\begin{itemize}
		\item $ t_i $: The time to initialize $ dist $
		\item $ t_m $ Time to find the minimum in $ dist $
		\item $ t_u $ Time to update a value in $ dist $
	\end{itemize}
	Suppose that $ dist $ is an array, we have that:
	\begin{itemize}
		\item $ t_i = O(n) $ (Initialize $ n $ values)
		\item $ t_m  = O(n)$ (Sequential search)
		\item $ t_u  = O(1)$ (Set a value in an array is immediate)
	\end{itemize}
		so the time complexity of Dijkstra's algorithm is
		\begin{center}
			$ O(t_i + n t_m + m t_u)  = O(n + n\cdot n + m) = O(n^2) $
		\end{center}
		But if $ dist $ is an heap we have:
		\begin{itemize}
		\item $ t_i = O(n \log(n)) $ (Initialize $ n $ values and sort by heap property)
		\item $ t_m  = O(\log(n))$ (Thanks to heap property)
		\item $ t_u  = O(\log(n))$ (Check heap property)
		\end{itemize}
		in this case the time complexity of Dijkstra's algorithm is
		\begin{center}
			$ O(t_i + n t_m + m t_u)  = O(n \log(n)+ n \log(n) + m \log(n)) = O((n+m) \log(n)) $
		\end{center}
		which is good if the graph is sparse
		\chapter{Floyd-Warshall algorithm}
		This algorithm solve the APSP problem with  edges having negative edges, but that not contains a negative cycle because if it exists the shortest path should pass through the cycle infinitely.
		\section{Dynamic programming technique}
		The dynamic programming technique is used in the Floyd-Warshall algorithm and consists of saving in a data structure the results of sub-problems of a generic algorithm to use them when a call with the same input happens.
		\begin{figure}
			\centering
			\includegraphics[width=0.5\linewidth]{img/DeepinScreenshot_20180222212118}
			\caption{Problem tree representation when it's useful the dynamic programming technique}
			\label{fig:deepinscreenshot20180222212118}
		\end{figure}
		\section{The algorithm}
		Let's define the following terms:
		\begin{itemize}
			\item $ A_0 = \emptyset $ and $ A_i = \{1 \dots i \} $
			\item $ \pi_i(x,y) $ the shortest path from x to y through the nodes contained in $ A_i $ (if x cannot reach y through $ A_i $ then $  \pi_i(x,y) = \infty $ )
			\item  $ d_i(x,y) $ the length of $ \pi_i(x,y) $
			\item Note that for $ i = 0 $ we have $ d_0(x,y) = w(x,y) $ if exists an edge between x and y else $ \infty $
		\end{itemize}
		For $ 1 \leq i \leq n $ we can calculate $ d_i(x,y) $ by distinguish between the two following cases:
		\begin{itemize}
			\item $ d_i(x,y)  $ pass through $ A_i $ but not by \textit{i}: in this case $ d_i(x,y) = d_{i-1}(x,y) $
			\item $ d_i(x,y)  $ pass through $ A_i $ and by \textit{i}: in this case $ d_i(x,y) = d_{i-1}(x,i) + d_{i-1}(i, y) $
		\end{itemize}
	\subsection{Complexity}
	\subsubsection{Space}
	This algorithm need a tri-dimensional array $ dist $ where the first dimension is used to memorize each step and the other two as matrix of the distance so the space complexity is $ O(n^3) $ but it can save some space memorizing only two matrix: the matrix of distances at step $ i $ and $ i-1 $, then the space complexity is $ O(n^2) $
	\subsubsection{Time}
	The time complexity is $ O(n^3) $ because for \textit{n} times the algorithm must update the distance matrix ($ O(n^2) $)
	\chapter{Six degree of separation}
	\section{The experiment}
	An experiment conducted by \textit{Stanley Milgram} says that, in 1990, the average distance between two persons in 6. He give to 296 people a letter that must be sent to the recipient and they can do two things:
	\begin{itemize}
		\item if it know the recipient: send directly the letter
		\item if it don't know the recipient: send the letter to the most probable person that maybe know the recipient
	\end{itemize}
	64 of 296 letters are delivered to the recipient and the average distance between the sender and the recipient is measured to be 6
	\section{Small world hypothesis}
	The experiment of \textit{Stanley Milgram} aims to verify that is called in SNA the \textit{small world hypothesis}, that means calculate the average shortest path across a network. In a social network, the average shortest path means calculate APSP (All Pair Shortest Path) then computationally is very often prohibitive (at least $ O(n^2) $ and in the worst case $ O(n^3) $).\\
	To avoid the high cost, we calculate an approximation of average shortest path using a \textit{sample} of nodes, but we need to measure the error of this approximation in order to use it rightfully.\\
	\section{Approximation of average shortest path}
	\subsection{Markov's inequality}
	The Markov's inequality is:
	\begin{center}
	$ Pr \left[ X \geq t \right] \leq \dfrac{\E\left[ X \right]}{t}$
	\end{center}
	The demonstration is simple:
	\begin{align*}
		 \E[X] &= \sum_u u \cdot P(X = u) \\
		 & \geq \sum_{u \geq t} u \cdot P(X = u)  \\
		  &\geq t \cdot \sum_{u \geq t}  P(X = u) = t\cdot P(X \geq t)
	\end{align*}
	\subsection{The Chernoff's bounding method}
	 The Chernoff?s bounding method says that if X is a random variable and $ \forall t > 0 $ we can assert:
		\begin{center}
			$ P(X \geq t) \leq \min_{s > 0} e^{-st} \E\left[ e^{sX} \right]$
		\end{center}
		Using the Markov's inequality we can demonstrate the Chernoff?s bounding method:
		\begin{align*}
			P(X \geq t) &= P(sX \geq st)  \\
			&= P(e^{sX}\geq e^{st}) \leq e^{-st} \E \left[ e^{sX} \right]
		\end{align*}
		\subsection{Assert to demonstrate}
		If X is a random variable such that $ \E \left[ X \right] = 0 $ and $ a \leq X  \leq b $, for any $ s > 0 $ we have that:\\
		\begin{align*}
		\E \left[ e^{sX} \right] \leq e^{\dfrac{s^2(b-a)^2}{8}}
		\end{align*}
		Since $ e^{sx} $ is convex we have that:
		\begin{align*}
			e^{sX} \leq \dfrac{X -a }{b-a} e^{sb} + \dfrac{b-X}{b-a} e^{sa}
 		\end{align*}
 		For hypothesis $ \E \left[X\right]  = 0$ so we have that:\\
 		\begin{align*}
 		  \E \left[e^{sX} \right] &\leq  \E \left[ \dfrac{X -a }{b-a} e^{sb} + \dfrac{b-X}{b-a} e^{sa}  \right] \\
 		  &= -\dfrac{a }{b-a} e^{sb} + \dfrac{b}{b-a} e^{sa}
 		\end{align*}
 		Now we define $ p = \dfrac{b}{b-a} $ and $ u = s(b-a) $, then we rewrite the latter formula in a log  scale with the new terms:
 		\begin{align*}
 		 		\log \left(   \dfrac{b}{b-a} e^{sa}  -\dfrac{a }{b-a} e^{sb} \right)
 		 				&= sa + \log \left(p + (1 - p)e^{s(b-a)}\right) \\
 		 		&= (p-1) u + \log(p+(1-p) e^u) = \varphi(u)
 		\end{align*}

		We apply the Taylor theorem to $ \varphi(u) $:
\begin{align*}
\varphi(u) = \varphi(0) + \varphi'(0) u + \dfrac{1}{2} \varphi''(\xi)u^2
\end{align*}
Since $ \varphi(0) $ and $ \varphi'(0) $ are 0 and $ \varphi''(\xi)  \leq \dfrac{1}{4}$ we have that:\\
\begin{align*}
\varphi(u) &= \dfrac{1}{2} \varphi''(\xi)u^2 \\
&\leq \dfrac{u^2}{8} = \dfrac{s^2(b-a)^2}{8}
\end{align*}
		\subsection{The Hoeffding's inequality}
		Given k independent random variables $ X_1 \dots X_k $ such that $ a_i \leq X_i \leq b_i $ and $ X = \sum_i X_i $ for any $ t > 0 $ we have that:
		\begin{align*}
			P(X - \E\left[X\right] \geq T) \leq e^{\dfrac{-2t^2}{\sum_{i=1}^{k} (b_i - a_i)^2}}
		\end{align*}

		If we apply the \textit{Chernoff's bounding method} to $ X - \E \left[X\right] $ we have that \\
		\begin{align*}
			P(X - \E \left[ X\right]  \geq t)  &\leq \min_{s > 0} e^{-st} \E \left[ e^{s(X - \E \left[X\right])} \right] \\
			& =\min_{s > 0} e^{-st} \prod_{i=0}^{k} \E \left[ e^{s(X_i - \E \left[X_i\right])} \right] \\
			& = \min_{s > 0} e^{-st} \prod_{i=0}^{k} e^{\dfrac{s^2(b_i - a_i)^2}{8}} \\
			&=  \min_{s > 0}  e^{-st}  e^{\dfrac{s^2\sum_i (b_i - a_i)^2}{8}} \\
			&= \min_{s > 0} e^{-st + \dfrac{s^2\sum_i (b_i - a_i)^2}{8}}
		\end{align*}
		In conclusion we have that $ 	P(X - \E \left[ X\right]  \geq t)  \leq  \min_{s > 0} e^{-st + \dfrac{s^2\sum_i (b_i - a_i)^2}{8}} $.
		Now we want to minimize the exponent of e for the Hoeffding's inequality, so we derive $ -st + \dfrac{s^2\sum_i (b_i - a_i)^2}{8} $ by s and we obtain a minimum for $ s = \dfrac{4t}{\sum_{i=1}^{k}(b_i - a_i)^2} $ .
		If we substitute we have:
		\begin{align*}
		P(X - \E \left[ X\right]  \geq t) & \leq  \min_{s > 0} e^{-st + \dfrac{s^2\sum_i (b_i - a_i)^2}{8}} \\
		&= e^{-\dfrac{4t}{\sum_{i=1}^{k}(b_i - a_i)^2} \cdot t + \dfrac{\left(\dfrac{4t}{\sum_{i=1}^{k}(b_i - a_i)^2} \right)^2\sum_i (b_i - a_i)^2}{8}} \\
		&=e^{-\dfrac{4t^2}{\sum_{i=1}^{k}(b_i - a_i)^2} + \dfrac{\dfrac{\left(4t\right)^2}{\sum_{i=1}^{k}(b_i - a_i)^2} }{8}} \\
		&= e^{\dfrac{-4t^2 \cdot 8 + \dfrac{\left(4t\right)^2}{\sum_{i=1}^{k}(b_i - a_i)^2} \cdot \sum_{i=1}^{k}(b_i - a_i)^2 }{\sum_{i=1}^{k}(b_i - a_i)^2 \cdot 8}} \\
		&= e^{ \dfrac{(4t)^2(-2+1)}{ \sum_{i=1}^{k}(b_i - a_i)^2 \cdot 8} } \\
		&= e^{ - \dfrac{16t^2}{ \sum_{i=1}^{k}(b_i - a_i)^2 \cdot 8} } = e^{ - \dfrac{2t^2}{ \sum_{i=1}^{k}(b_i - a_i)^2} }
		\end{align*}

		The Hoeffding's inequality works also when random variables are in the form $ -X_1 \ldots -X_k $ because
		\begin{align*}
			P(X - \E\left[X\right] \leq -t) \leq e^{ - \dfrac{2t^2}{ \sum_{i=1}^{k}(b_i - a_i)^2} } \\
			P(|X - \E\left[X\right ]| \geq  t) \leq 2e^{ - \dfrac{2t^2}{ \sum_{i=1}^{k}(b_i - a_i)^2} } \\
		\end{align*}
		\section{Finally the approximation}
		We can define grthe average shortest path at distance h as
		\begin{align*}
			N_h = \dfrac{|\{ (u,v) \in V \times V : d(u,v) = h \}|}{n(n-1)}
		\end{align*}
		instead of V if we sample from V a subset  $ U = \{u_1 \ldots u_k \} \subseteq V $ we have
		\begin{align*}
			N_h(U) = \dfrac{|\{ (u,v) \in U \times V : d(u,v) = h \}|}{|U|(n-1)} = \dfrac{\sum_{i=0}^{k} N_h(\{u_i\})}{k}
		\end{align*}
		The expected value of $  N_h(\{u_i\}) $ is
		\begin{align*}
			\E \left[ N_h(\{u_i\})  \right]	&= \dfrac{1}{n} \sum_{v \in V} N_h(\{v\})   \\
			& = N_h(V) = N_h
		\end{align*}
		then the expected value of set $  U  \subseteq V$ is
		\begin{align*}
			\E \left[N_h(U)\right] &= \E \left[\dfrac{\sum_{i=1}^{k} N_h(\{u_i\})}{k}\right] \\
			&= \dfrac{\sum_{i=1}^{k} \E \left[ N_h(\{u_i\} \right])}{k} \\
			&= \dfrac{\sum_{i=1}^{k} N_h}{k} \\
			&= \dfrac{k N_h}{k} = N_h
		\end{align*}
		Because $ N_h $ is a ratio, we can assert that $ 0 \leq N_h \leq 1 $ so $ \forall \,\, i \,\, N_h(\{u_i\}) $ we can define $ a_i = 0 $ and $ b_i = 1 $. So we can apply the Hoeffding's inequality to $ N_h $ to obtain this approximation:
		\begin{align*}
		P\left( \lvert  \dfrac{\sum_{i=1}^{k}  N_h(\{u_i\} )}{k} - N_h \rvert \geq t \right) \leq   2e^{-2t/k}
		\end{align*}
		If we choose the sample size $ k = \frac{2}{\alpha}t^{2}  \dfrac{1}{\ln(n) }$ we obtain the following bound
		\begin{align*}
			2 e^{\dfrac{-2t^2 \ln(n)}{\frac{2}{\alpha}t^{2} }} &= 2 e^{ - \alpha \cdot \ln(n)}\\
			&= 2 e^{\ln(n^{ - \alpha})} \\
			&= 2 n^{ - \alpha}
		\end{align*}
		Then the upper bound is dependent from $ \alpha $ (often $ \alpha = 2 $)

		\include{chapters/diameter}
		\include{chapters/power_law}
		\include{chapters/centrality}

	\end{document}


\section{Molecular dynamics and simulating Hamiltonian Systems}

Much of the point of statistical mechanics is to \emph{avoid} dealing with the dynamics of microscopic systems by studying their aggregate (or extensive) properties in a statistical fashion. Given this, and the computational challenges of dealing with the dynamics of very large numbers of (probably interacting) particles, you might think that direct simulations don't have much or a role to play in statistical mechanics. But:
\begin{itemize}
	\item It isn't always tractable (or it's just plain difficult) to find analytical expressions or sufficiently accurate approximations to all stat mech systems.
	\item Computing power is increasingly cheap and the size/complexity of the systems that we can study via simulation are always improving.
	\item Simulations let us directly test assumptions about what effects are important to a system by, for example, simulating a different interaction term or different geometries.
	\item There are many simulation approaches where clever heuristics allow us to deal with much larger systems than might be expected, while still getting the physics correct. (E.g. Monte Carlo methods --- see Shaun's content.) In some cases physical laws, such as energy-conservation, or preservation of phase space volume can be ``baked in'' to the simulation, such that the simulation automatically preserves the appropriate behaviour.
\end{itemize}

We're going to look in more detail at this last point --- how we might ensure that we can run a simulation that preserves physical laws. The context that we are going to look at is molecular dynamics.

As you might expect from the previous section, such systems can be described by specifying a Hamiltonian where the kinetic energy component takes the familiar form
$$ T = \sum_i \frac{{\bf p}_i^2}{2m_i},$$
where the $p_i$ are the momenta of the molecules and $m_i$ are their masses.

Interactions between particles are described by $U$, the potential term of the Hamiltonian. About the simplest model would be to treat each particle as a hard sphere, with no interactions between them, except during collisions.
An interaction term that is only slightly more complicated, but is much more realistic, is given by the Lennard-Jones potential:
$$
	U({\bf q}) = 4\epsilon_{ij}\left(\left(\frac{\sigma_{ij}}{q_{ij}}\right)^{12} - 2\left(\frac{\sigma_{ij}}{q_{ij}}\right)^{6}\right),
$$
where $|q_{ij}| = |q_i-q_j|$ are the inter-atomic separations. It's not hard to see that the potential has an absolute minimum at $q = \sigma$ --- this corresponds to the equilibrium separation between atoms. (Omitting the factor of 2 in the potential means the location of the minimum picks up a factor of $2^{-6}$.) The $\epsilon_{ij}$ terms gives the depth of the potential well --- the strength of the inter-atomic attraction.

As a brief historical aside, the $q^{-6}$ term in the potential can be derived mathematically as the attraction due to induced-dipole -- induced dipole interaction. This was known before Lennard-Jones, whose contribution was to include a term for the repulsion when the inter-atomic distance gets very small (Pauli repulsion due to overlapping wave-functions for electrons that are closer than the preferred bonding distance --- but that is getting into condensed matter physics). The repulsion term is actually exponential, rather than $q^{-12}$, but Lennard-Jones decided that $q^{-12}$ is pretty close to an exponential, and is much easier to calculate. Once you already know $q^{-6}$ you can just square it and save yourself a (relatively) expensive calculation of an exponential.

It is worth noting that the pairwise interactions in the Lennard-Jones potential mean that for $N$ particles we must calculate $N^2$ interaction terms. Clearly, this can cause issues once N gets large. One common approach is to use a \emph{long-range cut-off} and only calculate the interaction terms for atoms that are less than some distance $L$ away, or perhaps, only for those inside a unit cell, with sides of length $L$.

Regardless of the exact nature of the interaction term, we end up dealing with a Hamiltonian of the form
$$
	H({\bf q},{\bf p}) = T({\bf p}) + U({\bf q}).
$$

\subsection{Numerical integration}
Given the equations of motion (or the Hamiltonian) that describes a system, and some compatible initial conditions ${\bf q}(0)$, we want to find the state of the system at some time $T$ in the future (or the solution along some interval $[0,T]$). We do this by breaking the interval into small enough time steps of size $h$ and discretizing the solution ${\bf q}(t)$ as ${\bf q}_n = {\bf q}(t_n) $ where $t_n = nh$.

The simplest approach to approximating the discrete solution is the \emph{explicit Euler method}. It approximates the solution at ${\bf q}(t_{n+1})\simeq {\bf q}_{n+1}$ with
$$
	{\bf q}_{n+1} = {\bf q}_n + h \dot{\bf q}_n.
$$
That is, it evaluates the differential equation at the current time step and approximates the solution with a straight line of slope $\dot{\bf q}_n$.

[You can add a diagram to illustrate this - even a hand drawn example.]

The error in the approximation of the solution by the explicit Euler method grows in proportion to the step size $h$; that is, $\|{\bf q}_N-{\bf q}(Nh)\|=\mathcal{O}(h)$. We therefore say that the method is fist order accurate.

While the explicit Euler method is very easy to implement, it has a number of issues (you'll see one of them in the assignment). These include the fact that it becomes unstable (the error in its approximation starts to grow exponentially) when the step size is too large. The \emph{implicit Euler} (like the explicit Euler method, but the derivative is evaluated at the end of the time step i.e.$f(q_{n+1})$) method avoids this issue, but at the cost of needing to iteratively solve an implicit equation $q_{n+1}=q_n+hf(q_{n+1})$. It is still only first order.

Since $M\dot{\bf q} = {\bf p}$ and $M\ddot{\bf q} = \dot{\bf p}$ the equations of motion for the system can be written as
\begin{equation}
	M\ddot{\bf q} = f({\bf q}) = -\nabla U({\bf q}).
	\label{eq:2vf}
\end{equation}
That is, we can turn the set of $2d$ first-order differential equations in ${\bf q}$ and ${\bf p}$ into $d$ second order differential equation in ${\bf q}$.
In much of what follows, I'm going to omit the mass matrix and write $\ddot{\bf q} = f({\bf q})$. This is purely for convenience. Everything still holds when $M$ takes other values for the masses --- just make sure to include a factor of $M^{-1}$ in the $f({\bf q})$ term.

A better approach to numerical integration is to use the Verlet (AKA St\"{o}rmer, AKA leap-frog) method. This is most easily understood in its \emph{two-step formulation} (i.e. the method requires information from two previously known time steps ($t_{n-1}$ and $t_n$) in order to compute the approximation at the future time step $t_{n+1}$). The approximation is given by
\begin{equation}
	\frac{{\bf q}_{n+1} - 2{\bf q}_n + {\bf q}_{n-1}}{h^2} = f({\bf q}_n),
	\label{eq:2lf}
\end{equation}
where $f({\bf q})$ is the second order differential equation in equation \ref{eq:2vf}. It's not hard to see (you should verify it yourself) that the LHS of \ref{eq:2lf} is an approximation of the second derivative at the point ${\bf q}_n$.

[add a picture to illustrate this]

The two-step formulation of the leapfrog method can therefore be seen as determining a parabola that interpolates the points ${\bf q}_{n-1},~{\bf q}_{n},$ and ${\bf q}_{n+1}$ and matches the second derivative at the midpoint.

It turns out that, for a number of reasons, it is better to work with the Verlet method in its one-step formulation. This can be derived by introducing the velocity $p=\dot{\bf q}$ and turning equation \ref{eq:2vf} back into a first-order system of twice the dimension
$$
	\dot{\bf q} = {\bf p},\qquad \dot{\bf p} = f({\bf q}).
$$

We then introduce discrete approximations of $q$ and $p$ as follows:
$$
p_n =\frac{q_{n+1}-q_{n-1}}{2h},\qquad p_{n-\frac12} = \frac{q_n-q_{n-1}}{h}, \qquad q_{n-\frac12} = \frac{q_n + q_{n-1}}{2},
$$
where some of the values have been calculated on a staggered grid. We can then use these to formulate a one-step version of the Verlet method.
\begin{eqnarray*}
	{\bf p}_{n+\frac12} &=& {\bf p}_n+\frac{h}{2}f({\bf q}_n),\\
	{\bf q}_{n+1} &=& {\bf q}_n +h{\bf p}_{n+\frac12},\\
	{\bf p}_{n+1} &=& {\bf p}_{n+\frac12}+\frac{h}{2}f({\bf q}_{n+1}).
\end{eqnarray*}

[Add a picture to illustrate this]

There is also a dual formulation of the method that gives the values of ${\bf q}$ and ${\bf p}$ on the staggered grid points $t_{n-\frac12},t_{n+\frac12},$ etc.
(It makes sense to think of the one-step method as a discrete map $\Phi_h$, similar to the flow map $\varphi_{f,t}$ that maps from the known solution, to its approximation one time step forward.)

If the velocity values are not needed at the end of each step, then the last line of one step can be combined with the first line of the next step to avoid one evaluation of $f$ per step by only calculating ${\bf p}$ on the staggered grid:
\begin{eqnarray*}
	{\bf p}_{n+\frac12} &=& {\bf p}_{n-\frac12}+hf({\bf q}_n),\\
	{\bf p}_{n+1} &=& {\bf q}_n +h{\bf p}_{n+\frac12}.
\end{eqnarray*}
This is the most computationally efficient form of the Verlet method. It is also numerically more stable than the two-step formulation.

It is not too hard to show (you should prove it for yourself) that the Verlet method is symplectic. That is, the discrete map that it defines preserves the symplectic form that we saw earlier. This is almost, but not quite the same as preserving the value of the associated Hamiltonian along solutions of the vector field. In fact, it is possible to show (but trickier than proving symplecticity) that the difference between the value of the Hamiltonian evaluated along the discrete approximation from the Verlet method differs only exponentially slowly from the exact value of the Hamiltonian for the true solution. This makes the Verlet method a popular choice for molecular dynamics. In addition to having good stability and being easy to implement, it is automatically almost energy-preserving.

The Verlet method is \emph{symmetric} (replacing $h$ with $-h$ and swapping the subscripts $n$ and $n+1$ gives the same method again) and \emph{reversible} (changing the sign on the velocity for the initial condition is the same as running the method in reverse).It is also second order accurate; that is the error in the solution $\|{\bf x}_{Nh}-{\bf x}(Nh)\|=\mathcal{O}(h^2)$.

Now that we have a suitable method for calculating the micro-state of a system at some point in the future, we can look for ways to measure macroscopic quantities from the solution. It is worth bearing in mind that for any simulations, the initial conditions used are unlikely to correspond to a representative configuration for the system. It is therefore good practise to let the simulation run for a period of time (perhaps a few thousand time steps) before attempting to measure any properties. This process is known as equilibration or thermalization.

Using simulated particle trajectories usually means we implicitly invoke an assumption of \emph{ergodicity}. This essentially says that averaging an observable along a solution, for a long enough time period for a system, is equivalent to averaging that observable over a large number of initial conditions for the same system, where the initial conditions correspond to the same macro-state of the system. While the hypothesis of ergodicity is useful, there are many real world cases where it doesn't hold --- for example any system that exhibits \emph{hysteresis} in which properties of the system are path dependent. Magnet systems, in particular, tend to display such properties. For more discussion of this, see section 4.0 of \emph{Statistical Mechanics in a Nutshell}.

We have seen earlier that we identify the temperature of a thermodynamic system with the average square velocities of the particles in the system with some suitable normalisation. The temperature of our simulated system, with $N$ particles is therefore given by
$$
	T({\bf p}) = \frac{1}{Nk_B}\sum_{i=1}^N\frac{1}{2m_i}\|p_i\|^2.
$$

Another easily computable quantity is the virial function (we haven't mentioned this yet, but it is used to relate the total potential energy of a system to the total kinetic energy and can be used to calculate the pressure of a system --- see pp 255--256 of \emph{Statistical Mechanics in a Nutshell}):
$$
	C({\bf r}) = \sum_{i=2}^N\sum_{j=1}^{i-1}\nabla U(r_{ij})r_{ij},
$$
Both of these quantities give instantaneous values of the observables. Other quantities can be calculated as time averages of a solution of the system. For example, the constant volume specific heat:
$$
	C_v = k_B\left[\frac{2}{3N}-\frac{4}{9}\frac{\langle(T-\langle T\rangle)^2\rangle}{\langle T\rangle^2}\right]^{-1}
$$

\subsection{Recommended reading}
\begin{itemize}
	\item Sections 8.1 and 8.2 of \emph{Statistical Mechanics in a Nutshell}
	\item \emph{Geometric numerical integration illustrated by the St\"{o}rmer–Verlet method}, E. Hairer, Ch. Lubich, and G. Wanner, Acta Numerica (2003)
	\item Chapter 11 of \emph{Simulating Hamiltonian Dynamics}, B. Leimkuhler and S. Reich, Cambridge Univeristy Press (2004)
\end{itemize}