%% INTRO %%
Consider the deformable model of a liver shown in the left of Figure~\ref{fig:liver-multimodel}.
It is surrounded by different anatomical structures (including the diaphragm, the ribs, the stomach, the intestines...) and is also in contact with a grasper (modeled as an articulated rigid chain).
In SOFA, this liver is simulated using three different representations: the first is used to model its internal mechanical behavior, which may be computed using Finite Element Method (FEM) or other models.
The geometry of the mechanical model is optimized for the internal force computations, e.g. one will try to use a reduced number of well-shaped tetrahedra for speed and stability.
However, we may want to use different geometrical models for visualization or contact computation.
The second representation is used for collision detection and response, while the third is dedicated to the visual rendering process.
This sections presents these representations and their connections.

\begin{figure}
 \centering
 \includegraphics[width=0.48\linewidth]{NewLiver.png}
 \includegraphics[width=0.5\linewidth]{NewLiverMap.png}
 \caption{A simulated Liver
Left: The simulation of the liver (dataset from IRCAD, France). Right: Three representations are used for the liver: one for the internal mechanics, one for the collisions, and one for the visualization.  These representations are linked using mappings (black arrows).}
 \label{fig:liver-multimodel}
\end{figure}

\section{Solid Mechanics} \label{sec:rigidAndDeformable}

Different models can be employed to discretize a deformable solid continuum as a dynamic or quasi-static system of particles (also called simulation nodes).
The node coordinates are the independent degrees of freedom (DOFs) of the object, and they are typically governed by equations of the following type:
% Some boundary conditions, composed of imposed motion at the node can be applied to the system.
\begin{equation}
 \label{eq:expliciteulerexample}
 \Va = \P \M^{-1} \sum_i \Vf_i (\Vx,\Vv)
\end{equation}
where \Vx and \Vv are the position and velocity vectors, the $\Vf_i$ are the different force functions (volume, surface and external forces in this example), \M is the mass matrix and \P is a projection matrix to  enforce boundary conditions on displacements. Note that the modeling of rigid body dynamics leads to the same type of equations.

The corresponding model in \sofa{} is a set of components connected to a common graph node, as shown in the right of Figure~\ref{fig:liver-mechanical}.
%
\begin{figure}
 \centering
 \includegraphics[width=0.95\linewidth]{liver-mechanical2.png}   % generated from ps using: dvipdf -dEPSCrop
 \caption{Mechanical model of a liver. In order to facilitate the combination of models and algorithms, the liver is described as a composition of specialized components.}
 \label{fig:liver-mechanical}
\end{figure}
%
Each component is responsible for a small number of tasks implemented using virtual functions in an object-oriented approach.
Each operator in Equation~\ref{eq:expliciteulerexample} corresponds to a component.
\textit{MeshLoader} is used to read the topology and the geometry.
The coordinate vector \Vx of the mesh nodes and all the other state vectors (velocity \Vv, net force $\sum \Vf$, etc.) are stored in \textit{MechanicalObject}, which is the core component of the mechanical model.
A tetrahedral connectivity is stored in \textit{TetrahedronSetTopologyContainer}, and made available to other components such as \textit{TetrahedralCorotationalFEMForceField}, which accumulates one of the terms of the force sum using the Finite Element method.
The two other terms come from \textit{SpringForceField}, which accumulates the forces generated by the membrane, and  \textit{ConstantForceField}, which accumulates external forces to a given subset of simulation nodes (for instance the pressure exerted by the diaphragm on the liver).
\textit{DiagonalMass} is used to implement the product with matrix $\M^{-1}$.
\textit{FixedConstraint} implements the product with matrix $\P$ to cancel the displacements of the squared particles.
\textit{EulerSolver} implements the logic of time integration.

This approach is highly modular because the components are completely independent of each other and are implemented using C++ classes with a reduced number of abstract functions.
For instance, in the example of figure \ref{fig:liver-mechanical}, if one want to use a FEM for the membrane force instead of the spring based computation, only \textit{SpringForceField} has to be changed for \textit{TriangleFEMForceField}. Similarly, the mass matrix, stored as diagonal matrix in this example, can be stored as a single scalar value (\textit{UniformMass}) if less accuracy but faster computation is sought, in combination with an iterative solver for instance.

For efficiency, each mechanical state vector contains the values of all the simulation nodes, to avoid multiple call of virtual function resolutions.
The vector size is basically the number of particles times the number of space dimensions.
We use C++ templates to avoid code redundancy between scalar types (float, double), the types of degrees of freedom (particles, frames, generalized coordinates), and the number of space dimensions.
All the particles in a vector have the same type known at compile time.
Degrees of freedom of different types must be grouped in different objects, possibly connected with interaction forces, as discussed in Section~\ref{sec:interactionforcefield}.
This greatly simplifies the design and allows aggressive compiler optimizations.

More than 30 classes of forces are implemented in SOFA, including springs, FEM for volumetric (tetrahedron or hexahedron) or surface (triangular shell and membrane) deformable objects using corotational or hyperelastic formulations, and for wire or tubular object (beam models meshed with segments), have been implemented.
Different types of springs allow for easy and fast modeling of the deformations (bending, compression/traction, volume, interactions between two bodies, joints...).
In rigid objects, the main components are the degrees of freedom (a single frame with 3 rotations and 3 translations) and the mass matrix that contains the inertia of the object.
Surfaces can be attached to objects using \textit{mappings}, as discussed in Section~\ref{sec:mappings}.

\section{Collision models} \label{sec:collisionModels}
When a lot of primitives comes into contact, collision detection and response can become the bottleneck of a simulation.
Several collision detection approaches have been implemented: distances between pairs of geometric primitives (triangles and spheres), points in distance fields, distances between colliding meshes using ray-tracing~\cite{HerFauRaf08}, and intersection volume using images~\cite{AFCFDK10}
The collision pipeline is described in section \ref{sec:collision} with more details.

In order to adapt the models to the data structure of the different collision algorithms, we have defined a \textit{collision model}.
This model is similar to a mechanical model, except that its topology and its geometry are of its own and can be  stored in a data structure dedicated to collision detection.
For instance, the component \textit{TriangleModel} is the interface for the computation of collision detection on a triangular mesh surfaces.

If the collision of a given simulation takes too much time, or to reduce the number of collision points, the meshes used for collision detection can be chosen less detailed than the mechanical ones.
In the opposite, if precise collision detection and response is needed with  smooth surfaces, it is sometimes suitable to use more detailed mesh for collision detection.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Visual models} \label{sec:visual}

In the context of surgical simulation for training, to reach the state of what is often called \textit{suspension of disbelief} i.e. when the user forgets that he or she is dealing with a simulator, there are other factors than the mechanical behavior.
Realistic rendering is one of them.
It involves visually recreating the operating field with as much detail as possible, as well as reproducing visual effects such as bleeding, smoke, lens deformation, etc.
The main feature of the visual model of SOFA is that the meshes used for the visualization can be disconnected from the models used for the simulation.
The mappings described in section \ref{sec:mappings} maintain the coherency between them.
Hence, SOFA simulation results can easily be displayed using models much more detailed than used for internal mechanics, and rendered using external libraries such as OGRE\footnote{www.ogre3d.org} and Open Scene Graph\footnote{www.openscenegraph.org}.

We have also implemented our own rendering library based on openGL.
This library allows for modeling and render the visual effects that occurs during an intervention or the images that the surgeon is watching during the procedure.
For instance, in the context of interventional radiology simulator, we have developed  a dedicated interactive rendering of X-ray and fluoroscopic images.


%\documentclass[prl,aps,twocolumn,floatfix]{revtex4}
%\usepackage{amsmath,graphics,epsfig,color,verbatim,ulem}

\documentclass[twocolumn,prb,preprintnumbers,amsmath,amssymb,floatfix]{revtex4}
\usepackage[linktocpage,bookmarksopen,bookmarksnumbered]{hyperref}
\usepackage{graphicx}
\usepackage{dcolumn}
\usepackage{braket}
\usepackage{bbold}

\usepackage{amsmath,graphics,epsfig,color,verbatim,ulem}
\newcommand{\eps}{\epsilon} \renewcommand{\a}{\alpha}
\renewcommand{\b}{\beta} \newcommand{\vR}{{\mathbf{R}}}
\renewcommand{\vr}{{\mathbf{r}}} \newcommand{\vk}{{\mathbf{k}}}
\newcommand{\vK}{{\mathbf{K}}} \newcommand{\vq}{{\mathbf{q}}}
\newcommand{\vQ}{{\mathbf{Q}}} \newcommand{\vPhi}{{\mathbf{\Phi}}}
\newcommand{\vS}{{\mathbf{S}}}
\newcommand{\cG}{{\cal G}}
\newcommand{\cF}{{\cal F}} \newcommand{\cD}{{\cal D}}
\newcommand{\Tr}{\mathrm{Tr}} \newcommand{\npsi}{\underline{\psi}}
\newcommand{\vA}{{\mathbf{A}}} \newcommand{\vE}{{\mathbf{E}}}
\newcommand{\vj}{{\mathbf{j}}} \newcommand{\vv}{{\mathbf{v}}}
\newcommand{\kb}{k_B} \newcommand{\cellvol}{}
\newcommand{\trace}{\mbox{Tr}} \newcommand{\ra}{\rangle }
\newcommand{\la}{\langle } \newcommand{\om}{\omega}
\newcommand{\up}{\uparrow}
\newcommand{\dn}{\downarrow}
\renewcommand{\Im}{\mathrm{Im}}
\renewcommand{\Re}{\mathrm{Re}}
\newcommand{\nphi}{\underline{\phi}}
\newcommand{\tIm}{\overline{\Im}}

\begin{document}
\special{papersize=8.5in,11in}
\setlength{\pdfpageheight}{\paperheight}
\setlength{\pdfpagewidth}{\paperwidth}

\title{How to compute the gap with $\Delta$ in orbital space}
\maketitle

\section{Refresh on Nambu formalism}

Nambu-Gorkov spinor is
\begin{eqnarray}
\Psi^\dagger_{\vk,i}=(c_{\vk i \up}^\dagger, c_{-\vk i \dn})
\end{eqnarray}
and the Nambu Green's function is
\begin{widetext}
\begin{eqnarray}
G_{\vk,ij}(\tau)=-\langle T_\tau
\left(
\begin{array}{c}
c_{\vk i \up}(\tau)\\
c^\dagger_{-\vk i \dn}(\tau)
\end{array}
\right)
\left(
\begin{array}{cc}
c^\dagger_{\vk j \up} & c_{-\vk j \dn}
\end{array}
\right)\rangle
=-
\left(
\begin{array}{cc}
\langle T_\tau c_{\vk i \up}(\tau) c^\dagger_{\vk j \up}\rangle  &\langle T_\tau c_{\vk i \up}(\tau) c_{-\vk j \dn}\rangle\\
\langle T_\tau c^\dagger_{-\vk i \dn}(\tau) c^\dagger_{\vk j \up}\rangle & \langle T_\tau c^\dagger_{-\vk i \dn}(\tau) c_{-\vk j \dn}\rangle
\end{array}
\right)
\end{eqnarray}
\end{widetext}

We define
\begin{eqnarray}
\cG_{\vk,ij}(\tau) = -\langle T_\tau c_{\vk i \up}(\tau) c^\dagger_{\vk j \up}\rangle\\
\cF_{\vk,ij}(\tau) = -\langle T_\tau c_{\vk i \up}(\tau) c_{-\vk j \dn}\rangle
\end{eqnarray}
and see that
\begin{eqnarray}
G_{22}&=&-\langle T_\tau c^\dagger_{-\vk i \dn}(\tau) c_{-\vk j \dn}\rangle=
\langle T_\tau c_{-\vk j \dn}(-\tau) c^\dagger_{-\vk i \dn} \rangle=
\nonumber\\
&=&-\cG_{-\vk,ji}(-\tau)\nonumber\\
G_{12}&=&-\langle T_\tau c^\dagger_{-\vk i \dn}(\tau) c^\dagger_{\vk j \up}\rangle=
\cF^*_{\vk,ji}(\tau)
\end{eqnarray}

To prove the last identity, we check $\tau>0$ case, and write
\begin{eqnarray}
\cF^*_{\vk,ij}(\tau)=
-\frac{1}{Z}\Tr\left(
e^{-\beta H^*} e^{\tau H^*} c^*_{\vk i \up}e^{-\tau H^*} c^*_{-\vk j \dn}
\right)=\nonumber\\
-\frac{1}{Z}\Tr\left(
\left(e^{-\beta H+\tau H}\right)^T (c^\dagger_{\vk i \up})^T \left(e^{-\tau H}\right)^T (c^\dagger_{-\vk j \dn})^T
\right)=\nonumber\\
-\frac{1}{Z}\Tr\left(
\left(
c^\dagger_{-\vk j \dn}
e^{-\tau H}
c^\dagger_{\vk i \up}
e^{-\beta H+\tau H}
\right)^T
\right)=\nonumber\\
-\frac{1}{Z}\Tr\left(
e^{-\beta H+\tau H}
c^\dagger_{-\vk j \dn}
e^{-\tau H}
c^\dagger_{\vk i \up}
\right)=\nonumber\\
-\langle
T_\tau c^\dagger_{-\vk j \dn}(\tau)c^\dagger_{\vk i \up}
\rangle
\end{eqnarray}
Hence, Bogolubov Green's function is
\begin{eqnarray}
G_{\vk,ij}(\tau)=
\left(
\begin{array}{cc}
\cG_{\vk,ij}(\tau) & \cF_{\vk,ij}(\tau)\\
\cF^*_{\vk,ji}(\tau) & -\cG_{-\vk,ji}(-\tau)
\end{array}
\right)
\end{eqnarray}
or in matrix notation
\begin{eqnarray}
G_{\vk}(\tau)=
\left(
\begin{array}{cc}
\cG_{\vk}(\tau) & \cF_{\vk}(\tau)\\
\cF^\dagger_{\vk}(\tau) & -\cG^{T}_{-\vk}(-\tau)
\end{array}
\right)
\end{eqnarray}
and in frequency
\begin{eqnarray}
G_{\vk}(i\omega)=
\left(
\begin{array}{cc}
\cG_{\vk}(i\omega) & \cF_{\vk}(i\omega)\\
\cF^\dagger_{\vk}(-i\omega) & -\cG^{T}_{-\vk}(-i\omega)
\end{array}
\right)
\end{eqnarray}

\section{ab-initio DMFT and SC gap}
We first write LDA+DMFT solution in eigenbasis, which is frequency
dependent
\begin{eqnarray}
\cG_{\vk}(i\omega,\vr\vr') = \psi^R_{\vk l}(i\omega,\vr)\frac{1}{i\omega+\mu-\varepsilon_{\vk l, i\omega}} \psi^L_{\vk l}(i\omega,\vr')
\end{eqnarray}
or
\begin{eqnarray}
\int d\vr d\vr' \psi^L_{\vk l}(i\omega,\vr) \cG^{-1}_{\vk}(i\omega,\vr\vr') \psi^R_{\vk l}(i\omega,\vr')= {i\omega+\mu-\varepsilon_{\vk l, i\omega}}
\nonumber
\end{eqnarray}
The DMFT projector $U_{\vr\alpha}$, which is used to embed the
self-energy, can embed gap and give its real space representation as
\begin{eqnarray}
\Delta^\vk(\vr\vr')=U_{\vr\alpha}\Delta^\vk_{\alpha\beta}U^\dagger_{\beta\vr'}
\end{eqnarray}
being non-local in band index or real space.
We can transform it to DMFT eigenbasis by
\begin{eqnarray}
\bar{\Delta}^\vk_{ll'}(i\omega)=\int d\vr d\vr' \psi^L_{\vk l}(i\omega,\vr) U_{\vr\alpha}\Delta^\vk_{\alpha\beta}U^\dagger_{\beta\vr'}   \psi^R_{\vk l}(i\omega,\vr')
\end{eqnarray}
These projectors are computed by "dmftgk" in "e" mode, and they are
printed to file "UL.dat " as $UAl=\psi^L_{\vk l}(i\omega,\vr)
U_{\vr\alpha}$ and "UR.dat" as $UAr=U^\dagger_{\beta\vr'}   \psi^R_{\vk l}(i\omega,\vr')$.

Finally, we need to solve for Bogolubov quasiparticles by
diagonalizing the following Hamiltonian
\begin{eqnarray}
H_{BG}=
\left(
\begin{array}{cc}
\varepsilon_{\vk,i\omega}-\mu & \bar{\Delta}^\vk(i\omega)\\
\bar{\Delta}^{\vk \dagger}(-i\omega) & -\varepsilon_{-\vk,-i\omega}^T+\mu
\end{array}
\right)
\end{eqnarray}
It turns out that $\varepsilon_{\vk,i\omega}$ and
$\bar{\Delta}(i\omega)$ do not have a branch-cut at $i\omega=0$ hence
we can safely take the zero frequency limit. We will also assume that
the lattice has inversion symmetry, hence
$\varepsilon_{-\vk}=\varepsilon_{\vk}$. We hence diagonalize
\begin{eqnarray}
H_{BG}=
\left(
\begin{array}{cc}
\varepsilon_{\vk,0}-\mu & \bar{\Delta}_\vk\\
\bar{\Delta}_{\vk}^{\dagger} & -\varepsilon_{\vk,0}^T+\mu
\end{array}
\right)
\end{eqnarray}
The difference between the smallest positive eigenvalue $\lambda^-$ and the
largest negative eigenvalue $\lambda^+$ is equal twice the gap $2\Delta=\lambda^+-\lambda^-$.

\end{document}


\section{Analysis of the Proposed Approach}

\subsection{General  Overview}
\par Generally, our approach relies on enabling the collaborative learning among the distributed predictors. Each predictor node receives a stream of events related to a distinct domain object, and the central coordinator is responsible for synchronizing their prediction models using the \textit{synchronization operation} that we defined earlier. Moreover, the predictors only need to share the parameters of their models without aggregating all input event streams.

\par In addition, our approach relies on enabling the collaborative learning between the prediction models of the input event streams. By doing so, we assume that the underlying event streams belong to the same  distribution and share the same behavior (e.g., mobility patterns). We claim this assumption is reasonable in many application domains: for example, in the context of maritime surveillance, vessels travel through standard routes, defined by the International Maritime Organization (IMO). Additionally, vessels have similar mobility patterns in specific areas such as moving with low speed and multiple turns near the ports \cite{pallotta2013vessel,liu2014knowledge}. That allows our system to construct a coherent global prediction model dynamically for all input event streams based on merging their local prediction models.

\par The underlying learning process is based on estimating the transition probability matrix of the \pmcmr models, where the maximum-likelihood estimator  \cite{anderson1957statistical} is used to learn the transition probability matrix for each \pmcmr. In our approach, we propose a synchronization operation (Equation ~\ref{eq:dis_pi_estim}) to build a global estimation of the transition probability matrix among the \emph{$K$} predictors. In what follows, we present the maximum-likelihood estimator, and we  provide a theoretical analysis of the proposed operation, where we further present a probabilistic guarantee of the efficiency of our distributed learning estimation against the isolated estimations.
% may add it to conclusion
%\par By enabling collaborative learning our approach is imposing an acceleration of learning of the underlying prediction models with less training data, in addition, it provides an improvement of the predictive performance compared to the no-distributed  version of event forecasting with Pattern Markov Chains system.

\subsection{Theoretical Analysis}
\label{sec:theoretical}

 In this section, we present preliminaries of the Markov chain models and the maximum-likelihood estimator of the transition probabilities, and we describe the theoretical properties of our proposed synchronization operator and its relation to the maximum-likelihood estimator. Also, we derive a probabilistic guarantee of our method estimations for the transition probabilities.

 \subsubsection*{Preliminaries}
 We first give a brief introduction to the Markov chain theory, where the theoretical definitions and notations presented in this section are based  on the work described in \cite{bertsekas2002introduction,Billingsley1961,anderson1957statistical,howard2012dynamic}.

\begin{definition}
	Let $\{q_0, q_1, \ldots q_n\}$ be a sequence of random variables as \textbf{Markov chain}, where the variable $q_i$ belongs to a finite state space $\mathbf{S =\{1,\ldots m\}}$ and represents the observed state of the chain at time $i$. Let the transition probabilities of the Markov chain be $p_{ij}(t+1)$ such that $i,j \in S$ and $t=0,\ldots, n$, where  $p_{ij}(t+1)$ is the probability of the state $j$ at time $t+1$, given  state $i$ at time $t$, the sequence $\{q_0, q_1, \ldots q_n\}$ satisfies the \textbf{Markov property}

	\begin{equation}
	\begin{aligned}
	P(q_{t+1}=j|q_{t}=i,q_{t-1}=i_{t-1},\ldots ,q_{0}=i_{0})=P(q_{t+1}=j|q_{t}=i)\\
	\forall i,j,i_{t-1},i_{0} \in S
	\end{aligned}
	\end{equation}

\end{definition}

\par That is, the probability of moving to a future state only depends on the current state (Markov chain of order $1$). For higher order $m$ Markov chains the conditional probabilities are modeled to be dependent on the last $m$ states.

\par When the conditional probabilities $P(q_{t+1}=j|q_{t}=i)$ are independent of the time $t$ (stationary), the Markov chain is called \textbf{homogeneous} such that $p_{ij}:=P(q_{t+1}=j|q_{t}=i)$.

The transition probabilities of the homogeneous Markov chain are represented by a $m \times m$ matrix that is called \textbf{transition probability matrix} $\boldsymbol{\Pi}$ with $p_{ij}$ elements

\begin{equation}
\label{eq:matrix_example}
\boldsymbol{\Pi} =
\begin{bmatrix}
p_{1,1}	   &p_{1,2}  &. 		&. 		& . &  	p_{1,m} \\
p_{2,1}		   &.  & .		& .	    & .	& . \\
. 		   &.  & .		& .	    & .	& . \\
.		   &.  & .		& .		& .	& . \\
.		   &.  & .		& .		& .	& .\\
p_{m,1}	   & p_{m,1}	&.		& .	& .	&p_{m,m}
\end{bmatrix}
\end{equation}

where $0 \leq p_{i,j}\leq 1 $ and the rows sum up to one
\begin{equation}
\sum_{j=1}^{m} p_{i,j}= 1\ \ \ \ \ \ \ \ \ i=1,2 \ldots m
\end{equation}

\subsection{Learning the Transition Probability Matrix for a Single Markov Chain}
 \
\par As mentioned in Section~\ref{sec:pmc_prediction}, we rely on the transition probability matrix of \pmcmr to build the prediction intervals, given that the sequence of the states $\{q_0, q_1, \ldots q_n\}$ that DFA visits after consuming the input event stream is a 1-order homogeneous Markov chain.

\par However, in practice, the underlying transition probability matrix is unknown. Thus, it is required to estimate or learn it form the observed sequence $\{q_0, q_1, \ldots q_n\}$. The maximum-likelihood estimator is a common method to estimate the transition probability matrix \cite{anderson1957statistical}.

\begin{definition}
	Let $\boldsymbol{\Pi}$ be the transition probability matrix of a single homogeneous Markov chain with a set of states $S$,
	$\pi_{i,j}$ the transition probability from state $i$ to state $j$,
	$n_{i,j}$ the number of observed transitions from state $i$ to state $j$,
	then the maximum-likelihood finds $\boldsymbol{\hat{\Pi}}$ as an estimate for $\boldsymbol{\Pi}$, where its elements $\hat{p}_{i,j}$ are
	\begin{equation}
	\label{eq:pi_estim}
	\hat{p}_{i,j}=\frac{n_{i,j}}{\sum_{l \in S} n_{i,l}}=\frac{n_{i,j}}{n_{i}}
	\end{equation}

\end{definition}

Where this estimator is used separately to learn each transition probability matrix of all \pmcmr models.

\textbf{Proprieties of the maximum-likelihood estimates}.
	The maximum likelihood estimates of transition probabilities of a single sequence $\{q_0, q_1, \ldots q_n\}$  are obtained based on the observed transitions between the states of the chain. That is, the maximum likelihood estimates are basically the count of transitions from $i$ to $j$ divided by the total count of the chain being in state $i$.

	\par ~\citet{anderson1957statistical} have shown that

%	\begin{equation}
%	\label{eq:lim_dist}
%	\lim_{n\to\infty} \sqrt{n}\ (\hat{p}_{i,j} - {p}_{i,j}) \sim \mathcal{N}(\mu,\,\sigma^{2}_{mle})\,.
%	\end{equation}

	\begin{equation}
	\begin{aligned}
	\label{eq:lim_dist}
	 \sqrt{n}\ (\hat{p}_{i,j} - {p}_{i,j}) \xrightarrow{d} \mathcal{N}(0,\,\sigma^{2})\\
	 as\ n \xrightarrow{} \infty
	 \end{aligned}
	\end{equation}
Thus, the random variable $\sqrt{n}\ (\hat{p}_{i,j} - {p}_{i,j})$ has asymptotically normal distribution with mean $\mu=0$, and the variance $\sigma^{2}$ is given by

\begin{equation}
\begin{aligned}
\sigma^{2}=\mathrm{Var}(\sqrt{n}\ (\hat{p}_{i,j} - {p}_{i,j})) = \frac {{p}_{i,j}\ (1- {p}_{i,j})} {\phi_{i}} \\
\text{s.t.}\ \phi_{i} = \sum_{l=1}^{m} \sum_{t=1}^{T} \eta_{l} \ p_{l,j}^{t-1}
\end{aligned}
\end{equation}

Where ($\eta_{l} > 0,\ \sum_{} \eta_{l}=1$), and $p_{l,j}^{t-1}$ is the probability of state $j$ at time $t-1$ given that the state $l$ at time $0$ ~\cite{anderson1957statistical}. We are interested in the variances of $(\hat{p}_{i,j} - {p}_{i,j})$ that represents the error in the estimation of ${p}_{i,j}$ by the maximum-likelihood estimator, which is given by:

\begin{equation}
\begin{aligned}
\mathrm{Var} (\hat{p}_{i,j} - {p}_{i,j})=\widehat{\sigma}^{2}\\
\end{aligned}
\end{equation}

We assume that for some finite $n$ the random variable $(\hat{p}_{i,j} - {p}_{i,j})$ has a normal distribution with mean $\mu=0$, and the variance is roughly similar to the asymptotic variance  as $\ n \xrightarrow{} \infty$. Thus
\begin{equation}
\begin{aligned}
\label{eq:var_isol}
	\widehat{\sigma}^{2} = \mathrm{Var} (\frac {\sqrt{n}}{\sqrt{n}}\  (\hat{p}_{i,j} - {p}_{i,j})) = \frac {\mathrm{Var}(\sqrt{n}\ (\hat{p}_{i,j} - {p}_{i,j}))}{n} \approx \frac {\sigma^{2}}{n}
\end{aligned}
\end{equation}

It can be easily observed that variances drop as the sample size $n$ grows large.  Next, we will show that our proposed approach of synchronizing the maximum likelihood estimators over $k$ chains preserves a similar asymptotic behavior and gives efficient estimates of the transition probabilities.

\subsection{Probabilistic Learning Guarantee}
\label{sec:guarantee}
 %in Equation ~\ref{eq:dis_pi_estim}%
\par The proposed synchronization operator aggregates the maximum-likelihood estimates over $k$ observed sequences (i.e., sequences of the DFA states based on the consumed event streams), the operator estimates a global transition probabilities matrix for a set of $k$ sequences, which are arranged as a serial sequence composed of all $k$ sequences with length $N=k \cdot n$, assuming that all $k$ sequences have $n$ observations. For the sake of simplicity, we assume that the synchronization phase happens on batch size equals to $n$ (i.e., $b=n$) given that the divergence threshold is zero ($\Delta=0$), then the global transition probabilities $\hat{\pi}_{i,j}$ are given
\begin{equation}
\label{eq:dis_pi_estim2}
	\begin{aligned}
\hat{\pi}_{i,j}=\frac{\sum_{k \in K} n_{k,i,j}}{\sum_{k \in K} \sum_{l \in L} n_{k,i,l}} = \hat{p}_{i,j}(N)\\\\
 where\ N = k \cdot n.
 \end{aligned}
\end{equation}
%This is equivalent to

\par Thus, this operation allows to observe more samples, which is naturally producing better estimates of the transition probabilities. In addition, our proposed synchronization operation of the $k$ transition matrices has the same properties as the maximum likelihood estimator over a serial sequence of all $k$ sequences, while skipping $k-1$ transitions between every two consecutive sequences, which is in practice a small number that can be neglected comparing to the total transitions count $k \cdot n$. As result, the global transition probabilities of our approach  have the same properties as maximum likelihood estimates, in particular, the random variable $\sqrt{N}\ (\hat{\pi}_{i,j} - {p}_{i,j})$ has asymptotically normal distribution with mean $\mu=0$, and the variance is

\begin{equation}
\begin{aligned}
\label{eq:lim_dist2}
\sqrt{N}\ (\hat{\pi}_{i,j} - {p}_{i,j}) \xrightarrow{d} \mathcal{N}(0,\,\sigma^{2})\\
as\ N \xrightarrow{} \infty\\
where\ N = n \cdot k .\\
\end{aligned}
\end{equation}

 Following Equation ~\ref{eq:var_isol} we have the variance for finite $N$
\begin{equation}
\begin{aligned}
\label{eq:var_sync}
 \mathrm{Var} (\hat{\pi}_{i,j} - {p}_{i,j}) \approx \frac {\sigma^{2}}{N} =  \frac {\sigma^{2}}{k.n}
\end{aligned}
\end{equation}

That is, since $N > n$ combining $k$ sequences, the variances of estimations of our method  $\mathrm{Var} (\hat{\pi}_{i,j} - {p}_{i,j})$ are smaller than the estimates of maximum-likelihood over a single sequence $\mathrm{Var} (\hat{p}_{i,j} - {p}_{i,j})$.
\par Thus, it follows from Chebyshev's inequality \cite{feller1968introduction} that we have for the random variable $\hat{p}_{i,j} - {p}_{i,j}$, for any constant $c > 0$

\[ \Pr\left( |(\hat{p}_{i,j} - {p}_{i,j}) - \mu| \geq c \right) \leq
\frac{\mathrm{Var} (\hat{p}_{i,j} - {p}_{i,j})}{c^2} \]

 The mean $\mu$ is zero and the $\mathrm{Var} (\hat{p}_{i,j} - {p}_{i,j})$ equals  $\frac {\sigma^{2}}{n}$, and therefore

 \[ \Pr\left( |\hat{p}_{i,j} - {p}_{i,j}| \geq c \right) \leq
 \frac{\sigma^{2}}{c^2 \cdot n} \]

$\hat{p}_{i,j} - {p}_{i,j}$  represents the deviation/error between the estimates of maximum-likelihood over a single (i.e., isolated) sequence and the true probabilities. On the other hand, we can obtain, in the same way, the probability bound of deviations for our synchronization operator estimates as follows:

\[ \Pr\left( |\hat{\pi}_{i,j} - {p}_{i,j}| \geq c \right) \leq
\frac{\sigma^{2}}{c^2 \cdot n \cdot k} \]

\par Using Equation~\ref{eq:var_sync} we obtain the value $widehat\mathrm{Var} (\hat{\pi}_{i,j} - {p}_{i,j})$, and Equation~\ref{eq:var_sync} for $\mathrm{Var} (\hat{p}_{i,j} - {p}_{i,j})$. Given that $k \ge 1$, then  the variance of $\hat{\pi}_{i,j} - {p}_{i,j}$  is less than or equal to the variance of $\hat{p}_{i,j} - {p}_{i,j}$

\[
\frac{\sigma^{2}}{c^2 \cdot n \cdot k} \leq
\frac{\sigma^{2}}{c^2 \cdot n}
 \]

So, we have for any constant $c > 0$ and $k \ge 1$, a probabilistic learning guarantee of the transition probabilities:
\begin{equation}
\begin{aligned}
\label{eq:guarantee}
 \Pr\left( |\hat{\pi}_{i,j} - {p}_{i,j}| \geq c \right) \leq
 \frac{1}{k}\ \Pr\left( |\hat{p}_{i,j} - {p}_{i,j}| \geq c \right)
 \end{aligned}
 \end{equation}
 where $\hat{p}_{i,j}$ a transition probability, which is learned on a single \pmcmr predictor, and the $\hat{\pi}_{i,j}$ is the global transition probability computed by the synchronization operation (Equation ~\ref{eq:dis_pi_estim}).
 \par To summarize, our approach is based on aggregating the maximum-likelihood estimates over $k$ sequences, which speeds up the convergence to reach the true transition probabilities as a result of the smaller variances of the estimates.

%The weak law of large
%numbers states that, if $X_1, X_2, X_3, \ldots$ are independent and identically
%distributed random variables with mean $\mu$ and standard deviation $\sigma$,
%then for any constant $\epsilon > 0$ we have
%%
%\[ \lim_{n \rightarrow \infty} \Pr \left( \left| \frac{X_1 + X_2 + \cdots +
%	X_n}{n} - \mu \right| > \epsilon \right) = 0. \]
%Use Chebychev's inequality to prove the weak law of large numbers.

\subsection{Transition Matrix of the Underlying Markov Chain}
\label{sec:underlaying_mc}
\par In order to empirically study the learning efficiency of our proposed approach, we need to map the transition probabilities of the underlying Markov chain with the transition probability matrix ($\hat{\Pi}$) of the \pmcmr model. \par ~\citet{nuel_pattern_2008} showed in \textbf{Theorem 3} the relation between the elements  of
$\Pi$ and the conditional probabilities of the $m-$order Markov chain $X=\{X_1, X_2, \ldots X_n\}$ described by

\[ \Pi(p, q) =
\begin{cases}
P(X_{m+1}=b|X_1\ldots X_m=\delta^{-m}(p))     & \quad \text{if } \delta(p,q)=b \\
0  & \quad \text{if } p \notin  \delta(p,X)
\end{cases}
\]
\par In the case of the underlying transition probability matrix is known (e.g., synthetic event streams), we then can compare the estimation efficiency of the different approaches for the transition probabilities of the underlying Markov chain $X$.  For example, the transition probability matrix of the \pmcmr model for the pattern $\mathcal{P}=a ; d ; c$ over  $\Sigma=\{a,b,c,d\}$ is represented by:

\begin{equation}
\label{eq:matrix}
\boldsymbol{\Pi} =
\begin{Bmatrix}
 1          \\ 2        \\3             \\4         \\5         \\ 6
\end{Bmatrix}
\begin{bmatrix}
P(a|a) 	& P(b|a) 		& P(c|a) 		& 0			  & p(d|a) 	& 0  \\
P(a|b) 	& P(b|b) 		& P(c|b) 		& P(d|b)	  & 0 	    & 0  \\
P(a|c) 	& P(b|c) 		& P(c|c) 		& P(d|c) 	  & 0 	    & 0  \\
P(a|d) 	& P(b|d) 		& P(c|d) 		& P(d|d)	  & 0 	    & 0  \\
P(a|d) 	& P(b|d) 		&  0	 	    & P(d|d)	  & 0 	    & P(c|d)  \\
0			& 0			& 0		        & 0    		  & 0		& 1.0     \\
\end{bmatrix}
\end{equation}

\par We use this theorem In Section~\ref{sec:results_synthetic} to calculate the estimation error for the transition probability matrix of the source Markov chain, which is used to generate the synthetic event streams. We compare the learning efficiency of the isolated \pmcmr predictors and our approach that enables the \pmcmr predictors to synchronize the transition probabilities matrices to create a global one.


\documentclass[main.tex]{subfiles}
\begin{document}

\marginpar{Monday\\ 2020-3-16, \\ compiled \\ \today}

\section{Vallone's part}

We will discuss some protocols with which to generate entanglement, how it is measured and how it is used.

Then, we would have another lab activity in which we are shown how to make a Quantum Key Distribution protocol.

Last time (?) we discussed the Ambu-Mandel effect: this is about the fact that two photons impacting on a beam splitter can either go both up or both down.

What happens if the two impacting photons are entangled?
Let us denote \(a\) and \(b\) as the incoming photons, while \(c\) and \(d\) are the ones coming out.

Let us assume these two photons start out as a singlet in their polarization:
%
\begin{align}
\ket{\psi^{-}} &= \frac{1}{\sqrt{2}}
\qty(\ket{H}_{A} \ket{V}_{B}  - \ket{V}_{A} \ket{H}_{B} )  \\
&= \frac{1}{\sqrt{2}} \qty(a ^\dag_{H} b ^\dag_{B} - a ^\dag_{V} b ^\dag_{H}) \ket{0}
\,,
\end{align}
%
where the second expression is the same as the first, written in the second quantization formalism.
The operators can be written as
%
\begin{align}
a ^\dag &= \frac{1}{\sqrt{2}} \qty(c ^\dag + i d ^\dag) \\
b ^\dag &= \frac{1}{\sqrt{2}} \qty(d ^\dag + i c ^\dag)
\,,
\end{align}
%
which means that when the photon changes direction it picks up a phase delay of \(i = e^{i \pi /2}\).

Substituting this in, we find
%
\begin{align}
\ket{\psi^{-}} &= \frac{1}{\sqrt{2}^3}
\qty[\qty(c_{H} ^\dag + i d_{H} ^\dag)\qty(d_{V} ^\dag + i c_{V} ^\dag)   - \qty(c_{V} ^\dag + i d_{V} ^\dag)\qty(d_{H} ^\dag + i c_{V}^\dag) ] \ket{0}  \\
&= \frac{1}{\sqrt{2}} \qty[c_{H} ^\dag d_{V} ^\dag - c_{V} ^\dag d_{H} ^\dag] \ket{0}
\,,
\end{align}
%
since these operators commute if they act on different spaces.
So, the photons must always come out in different directions. We can compute this for different Bell states:
%
\begin{align}
\ket{\psi^{\pm}} &= \frac{1}{\sqrt{2}} \qty[a_{H} ^\dag b_{V} ^\dag \pm a_{V} ^\dag b_{H} ^\dag] \ket{0} \\
\ket{\phi^{\pm}} &= \frac{1}{\sqrt{2}} \qty[a_{H} ^\dag b_{H} ^\dag \pm a_{V} ^\dag b_{V} ^\dag] \ket{0}
\,,
\end{align}
%
and with similar steps as before we get that for the \(\psi^{\pm }\) states (singlet) the photons come out in different states, while for the \(\phi^{\pm }\) (triplet) states they come out the same side.

This is idealized, as if the photons had an infinite wavelength. In the lab, we can plot the number of coincidences as the delay in time of arrival changes: the coincidences have a bump or a hump for \(\Delta t = 0\); for the singlets the coincidences go to zero, while the triplets the coincidences double from the regular (non-entangled) case.

We can have a projective measurement in our space to be one which distinguishes these 4 states: this is a Bell State Measurement.

We can check whether we see \(\psi \) or \(\phi \) by looking at coincidence or no coincidence, and by looking at whether the polarizations are the same or different we can see whether we have the \(+\) or \(-\) in  \(\psi^{\pm }\) (or \(\phi^{\pm}\) respectively).

\subsection{Quantum Teleportation}

This means transferring the wavefunction from a particle to another.
Particles are indistinguishable, the only thing which is different from one to the other is the wavefunction.
So, if we can transfer the wavefunction we have transferred the particles for any purpose.

``Classical FAX becomes quantum teleportation''.

Let us discuss the teleportation protocol. For another reference, see the notes for the course on Quantum Information \cite[]{tissinoQuantumInformationNotes2019}. The photon starts out with a wavefunction
%
\begin{align}
\ket{\varphi }_{A} = \alpha \ket{0} + \beta \ket{1}
\,,
\end{align}
%
and we have an EPR state, with two particles entangled, let us say, in the singlet state \(\ket{\psi^{-}}\) on particles \(B\) and \(C\).

What we should do is a Bell State Measure on particles \(A\) and \(B\). Our result has 4 possible outcomes, we codify it into 2 classical bits.

Then, we will apply a unitary transformation depending on these 2 bits on particle \(C\). Then, the state of particle \(A\) will be replicated on particle \(C\).

This destroys the state of particle \(A\).
This also does not give us any information on the state, or on the parameters \(\alpha \) and \(\beta \).
We cannot teleport faster than light, since we are bound to transmitting classical bits.

The state will be
%
\begin{align}
\ket{\chi } =
  \ket{\phi}_{A} \ket{\psi^{-}}_{BC}
 &= \qty(\alpha \ket{0}_{A} + \beta \ket{1}_{A} )
 \otimes
 \frac{1}{\sqrt{2}} \qty(\ket{01}_{BC} - \ket{10}_{BC}) \\
 &= \frac{1}{\sqrt{2}} \qty(\alpha \ket{001} + \beta \ket{101} - \alpha \ket{010} - \beta \ket{110})
\,,
\end{align}
%
so if we compute
%
\begin{align}
_{BC}\braket{\psi^{\pm}}{\chi }_{ABC}
 &= \frac{1}{2} \qty( \pm \beta \ket{1}_{C} - \alpha \ket{0}_{C}) \\
&= -\frac{1}{2} \qty(\alpha \ket{0}_{C} \mp \beta \ket{1}_{C})
\,,
\end{align}
%
so if we measure \(\psi^{-}_{AB}\) we have \(\frac{1}{2} \ket{\varphi }_{C}\), while if we measure \(\psi^{+}_{AB}\) we will have \(\frac{1}{2} \sigma_{z} \ket{\varphi}_{C}\).

On the other hand, if we measure \(\phi^{\pm}_{AB}\) we will have either \(\frac{1}{2} \sigma_{x} \ket{\varphi}_{C}\) or \(\frac{1}{2} \sigma_{y} \ket{\varphi}_{C}\).

The \(\frac{1}{2}\) factor accounts for the normalization of the probabilities of obtaining the various states: regardless of \(\ket{\varphi }\), we have probability \(\frac{1}{4} = \frac{1}{2^2}\) for each of the 4 states. The two classical bits contain no information about the state.

If we do not transmit the two classical bits, the state on particle \(C\) becomes completely mixed:
%
\begin{align}
\frac{1}{4} \dyad{\varphi } +
\frac{1}{4} \sigma_{x} \dyad{\varphi }\sigma_{x}  +
\frac{1}{4} \sigma_{y} \dyad{\varphi }\sigma_{y}  +
\frac{1}{4} \sigma_{z} \dyad{\varphi }\sigma_{z} =
\frac{1}{2} \mathbb{1}
\,.
\end{align}

If we start with two entangled photons \(A\) and \(B\), and clone photon \(B\) into photon \(D\), then photons \(A\) and \(D\) will be entangled.
This is \emph{entanglement swapping}. It works because \(A's\) wavefunction factors out of everything.

This means that we can have entangled particles even if they have never directly interacted.

This is especially useful if we want to entangle slow, massive particles.
In the future, it might be the basis for the \emph{quantum internet}.

This cannot be done if we do not transmit classical information.

\subsection{Dense Coding}

If we just have one qubit, we can use it to encode only one bit of information.

However, if we use additional entangled qubits we can do better, still by sending just one qubit physically.

Alice and Bob start out with a singlet state \(\propto \qty(\ket{01} - \ket{10})\).
Alice applies one of \(\mathbb{1}\), \(\sigma_{x}\), \(\sigma_{y}\) or \(\sigma_{z}\) based on the values of her two classical bits.

The state becomes
%
\begin{align}
\mathbb{1} \rightarrow \frac{1}{\sqrt{2}} \qty(\ket{01} - \ket{10 }) &= \ket{\psi^{-}} \\
\sigma_{x} \rightarrow \frac{1}{\sqrt{2}} \qty(\ket{11} - \ket{00 }) &= \ket{\phi^{-}} \\
\sigma_{y} \rightarrow \frac{1}{\sqrt{2}} \qty(\ket{11} - i\ket{00 }) &= \ket{\phi^{+}} \\
\sigma_{z} \rightarrow \frac{1}{\sqrt{2}} \qty(\ket{01} + \ket{10 }) &= \ket{\psi^{+}}
\,,
\end{align}
%
so in the end Alice can measure qubit \(A\), which is sent to her, and qubit \(B\), which she already had.
Based on the results of her measurements, she can determine what the two classical bits were.

\subsection{Tomography}

How do we measure \(\alpha \) and \(\beta \) for
%
\begin{align}
\ket{\psi } = \alpha \ket{0} + \beta \ket{1}
\,?
\end{align}
%

We can measure along the regular basis to find \(\abs{\alpha }^2\) and \(\abs{\beta }^2\).
In order to get information about their phases, we apply a Hadamard gate: if we measure along
%
\begin{align}
\frac{1}{\sqrt{2}} \qty(\ket{0} \pm \ket{1})
\,
\end{align}
%
then we find information about \(\abs{\alpha + \beta }^2 / 2\) and \(\abs{\alpha - \beta }^2/2\).

We can decompose a density matrix \(\rho \) as \(\rho = r_{\mu } \Gamma^{\mu }\), where the \(\Gamma^{\mu }\) are a basis of Hermitian matrices.

Then, if we have a basis \(\ket{\psi_{\alpha }}\) we can do
%
\begin{align}
\mathbb{P}_{\alpha } = \ev{\psi_{\alpha }}{\rho }
= \sum_{\mu } r_{\mu } \ev{\psi_{\alpha }}{\Gamma^{\mu }}
= \sum_{\mu } r_{\mu } B_{\mu \alpha }
\,,
\end{align}
%
and this system is solvable as long as the projectors \(\dyad{\psi_{\alpha }}\) are linearly independent (which implies that the matrix \(B_{\mu \alpha }\) is invertible).

It is a fact that for a \(d\)-dimensional Hilbert space we need \(d^2-1\) of these.

For a qubit, we can write its density matrix as
%
\begin{align}
\rho = \frac{1}{2} \qty(\mathbb{1} + \vec{r} \cdot \vec{\sigma})
\,;
\end{align}
%
in this case the four states needed are \(P_0 = \dyad{0}\), \(P_1 = \dyad{1}\), \(P_{+} = \dyad{+}\), \(P_{-} = \dyad{-}\).
These are not independent as states, however they are independent as projectors.

Then, we can recover the components of the Bloch vector representation as
%
\begin{align}
r_{z} &= P_0 - P_1  \\
r_{x} &= 2 P_{+} -1  \\
r_{y} &= 2 P_1 - 1
\,,
\end{align}
%
so in general the only way to recover all the information about a quantum state is to project it along linearly independent projectors (or, really, the necessity is that the projectors span the whole Hilbert space).

\end{document}


\section{Final Remarks}
Some images will appear to have a slightly different color-mix when transformed back
to real space. This is not so much of a feature of the Fourier transform, rather, it
has to do with the normalization we apply to the pixels after the inverse transform.
If one takes a little more attention to color-normalization, one can retain
the color profile.


\documentclass[12pt]{article}

\usepackage{lipsum}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage[T1]{fontenc}
\usepackage{multirow}
\usepackage[body={16cm,24cm}, top=3cm]{geometry}
\geometry{papersize={21.59cm,27.94cm}}

\parindent=0in
\pagestyle{headings}

\begin{document}

%\title{\textbf{Macro II Problem Set 6}}
\title{
 \textbf{Macro II Problem Set 6 }\\
  \large Welfare Analysis of Practical Monetary Policy Rules\\
   within the Basic New Keyesian Model}

\author{
  QK\\
  \texttt{277201411527XX}
  \and
 CSS\\
  \texttt{277201411527XX}
}
\maketitle

\section{Part I. A Taylor-type Interest Rate Rule.}  %% PART I
\subsection{\dots Write out the entries in the new vector $\tilde{B}_{t}$} %problem 1
\normalsize{\textbf{A.N.S:}}
Assuming that variations in the technology parameter $a_t$ represent the only driving force in the economy, and are described by a stationary AR(1) process with autoregressive coefficient $\rho_a$, the following equality holds:
\begin{equation}
\begin{aligned}
\hat{r}_{t}^{n}-v_{t} &= -\sigma\psi^{n}_{ya}(1-\rho_{a})a_{t}-\phi_{y}\psi^{n}_{ya}a_{t}\\
&=-\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}]a_{t}
\end{aligned}
\end{equation}
Then, we have:
\begin{equation}
\begin{aligned}
B_{T}(\hat{r_{t}}^{n}-v_{t})&=-B_{T}(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}])a_{t}\\
& = \tilde{B}_{t}a_{t}
\end{aligned}
\end{equation}
\[ \Longrightarrow   \tilde{B}_{t}= -B_{T}(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}]) \]

\subsection{Show how to compute $var(a_t)$?, And what is the value of $var(a_t)$? }%problem 2
\textbf{A.N.S:} For $a_t=\rho_{a}a_{t-1}+\varepsilon_{t}^{a}$,
\begin{equation*}
Var(a_t) = \rho_a^2 Var(a_{t-1})+Var(\varepsilon_{t}^{a})
\end{equation*}
Assume $Var(a_t) = Var(a_{t-1})$, we can find:
\begin{equation}
Var(a_{t}) = \frac{Var(\varepsilon_{t}^{a})}{1- \rho_a^2 } = \frac{\sigma_{a}^2}{1- \rho_a^2 } \label{eq3}
\end{equation}
Substitute $\sigma_{a}^2 = 0.00712^2$ and $\rho_a = 0.9$ into equation (\ref{eq3}), we can compute
\[Var(a_{t})= 2.6681\times 10^{-4} \]

\subsection{Please show how to compute $Var(\tilde{y}_{t})$ and $Var(\pi_{t})$ as a function of $var(a_t)$ } %problem 3
\textbf{A.N.S:} Consider the DIS and NKPC system:
\begin{equation}
\left[
\begin{array}{c}
 \tilde{y}_{t}\\
 \pi_{t}
\end{array}
\right] = A_T
\left[
\begin{array}{c}
E_{t}\{\tilde{y}_{t+1}\}\\
E_{t}\{ \pi_{t+1}\}
\end{array}
\right]+ \tilde{B}_{T}a_t\label{eq4}
\end{equation}
where ,
\begin{gather*}
 A_T =
\left(
\begin{array}{cc}
 \sigma & 1-\beta\phi_{\pi}      \\
  \kappa\sigma & \kappa+\beta(\sigma+\phi_y)
\end{array}
\right)\Omega\\
 \tilde{B}_{t}= -B_{T}(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}])\\
B_{T} =\left[
\begin{array}{c}
1\\
\kappa
\end{array}
\right]\Omega\\
\kappa = \lambda(\sigma+\frac{\varphi+\alpha}{1-\alpha})\\
\lambda = \frac{(1-\theta)(1-\beta\theta)}{\theta}\Theta\\
\Theta = \frac{1-\alpha}{1-\alpha-\alpha\varepsilon}
\end{gather*}
Rewrite Equation(\ref{eq4}) in a recursive form:
\begin{equation}
\begin{aligned}
\left[
\begin{array}{c}
 \tilde{y}_{t}\\
 \pi_{t}
\end{array}
 \right] &= A_T
 \{
 A_T
\left[
\begin{array}{c}
E_{t}\{\tilde{y}_{t+2}\}\\
E_{t}\{ \pi_{t+2}\}
\end{array}
\right]+ \tilde{B}_{T}a_{t+1}
 \}+ \tilde{B}_{T}a_t\\
 & = (A_T)^2\left[
\begin{array}{c}
E_{t}\{\tilde{y}_{t+2}\}\\
E_{t}\{ \pi_{t+2}\}
\end{array}
\right]+A_T\rho_a\tilde{B}_{T}a_{t}+\tilde{B}_{T}a_t\\
&\qquad\vdots\qquad\vdots\qquad\vdots\\
&=(A_T)^{\infty}\left[
\begin{array}{c}
E_{t}\{\tilde{y}_{\infty}\}\\
E_{t}\{ \pi_{\infty}\}
\end{array}
\right]+\sum_{i=0}^{\infty}(A_T\rho_a)^{i}\tilde{B}_{T}a_{t}\\
& = (I_2 - \rho_a A_T)^{-1} \tilde{B}_{T}a_{t}
\end{aligned}
\end{equation}
Denote $2\times 1$ vector $ C = (I_2 - \rho_a A_T)^{-1} \tilde{B}_{T} $, thus,
\begin{gather}
  var(\tilde{y}_{t}) = [C(1)]^2 var(a_t)\\
  var(\pi_{t}) = [C(2)]^2 var(a_t)
\end{gather}
where $C(i)$ is the $i-th$ element of $C$

%We can rewrite equation (\ref{eq4}) as :
%\begin{gather}
%\tilde{y}_{t} =  \sigma\Omega E_{t}\{\tilde{y}_{t+1}\}+ (1-\beta\phi_{\pi} )\Omega E_{t}\{ \pi_{t+1}\}-\Omega(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}])a_{t}\\
%\pi_{t} =  \kappa\sigma\Omega E_{t}\{\tilde{y}_{t+1}\} + (\kappa+\beta(\sigma+\phi_y))\Omega E_{t}\{ \pi_{t+1}\}-\Omega\kappa(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}])a_{t}
%\end{gather}
%Assume $Var(\tilde{y}_{t})=Var(E_{t}\{\tilde{y}_{t+1}\})$ and $Var( \pi_{t}) = Var( \pi_{t+1})$, the variance of $\tilde{y}_{t}$ and $\pi_{t}$ satisfy:
%\begin{gather}
%Var(\tilde{y}_{t}) =  (\sigma\Omega)^2 Var(\tilde{y}_{t})+ [(1-\beta\phi_{\pi} )\Omega]^2 Var( \pi_{t})+[\Omega(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}])]^2Var(a_{t})\\
%Var( \pi_{t})= ( \kappa\sigma\Omega)^2 Var(\tilde{y}_{t})  + [(\kappa+\beta(\sigma+\phi_y))\Omega]^2 Var( \pi_{t})+[\Omega\kappa(\psi^{n}_{ya}[\sigma(1-\rho_{a})+\phi_{y}])]^2Var(a_{t})
%\end{gather}
Use Matlab , we can easily solve the system:
\begin{gather*}
 var(\tilde{y}_{t}) =  1.5645\times 10^{-5} \\
 var( \pi_{t}) =1.5324\times 10^{-4}\\
\left[
\begin{array}{c}
Var(\tilde{y}_{t}) \\
 Var( \pi_{t})
\end{array}
\right] = \left[
\begin{array}{c}
 1.5645\times 10^{-5} \\
1.5324\times 10^{-4}
\end{array}
\right]
  \end{gather*}
\newpage
\subsection{Compute the average welfare loss per period.} %% Problem 4
\textbf{A.N.S:}The average welfare loss per period in terms of variations of output gap and inflation is:
\begin{equation}
L = \frac{1}{2}\big[ (\sigma+\frac{\varphi+\alpha}{1-\alpha})Var(\tilde{y}_{t})+\frac{\varepsilon}{\lambda} Var( \pi_{t})\big]\label{eq9}
\end{equation}
Substituting $Var(\tilde{y}_{t})$ and $Var( \pi_{t})$ into  equation (\ref{eq9}), we can solve the welfare loss:
\begin{equation*}
L = 0.0041
\end{equation*}

\subsection{Vary the values of policy reaction parameters and generate a table as follows, and fill the blank entries:}
\textbf{A.N.S:} Repeated the problem 1.3,1.4 procedure with respect to the given $\phi_{\pi}$ and  $\phi_{y}$.
{\centering
\begin{table}[h]
\centering
\begin{tabular}{lllll}
\hline
 & \multicolumn{4}{l}{Taylor Rule} \\ \hline
$\phi_{\pi}$ &    \textbf{1.5}    &  \textbf{1.5}      & \textbf{1.5}      &  \textbf{5}     \\
 $\phi_{y}$&    \textbf{1 }   &   \textbf{0.125}     &   \textbf{0}    & \textbf{ 0}     \\
 $\sigma(\tilde{y})$&   $3.6408 \times 10^{-5}$   &  $3.0547\times 10^{-6}$      &  $6.8208\times 10^{-7}$     & $1.5955\times 10^{-8}$      \\
 $\sigma(\pi)$&   $3.5662 \times 10^{-4}$    &  $2.9921  \times 10^{-5}$      &  $6.6810\times 10^{-6} $  & $1.5628\times 10^{-7}$       \\
  $\sigma(L)$&   $0.0095$  &     $7.9396 \times 10^{-4}$    &   $1.7728\times 10^{-4}$     &  $4.1469\times 10^{-6}$    \\   \hline
\end{tabular}
\end{table}}
\newpage
\section{Part II.  A Money Growth Rule.}  %% PART 2
\subsection{Compute $\hat{r}_t^n$ and  $\hat{y}_t^n$ }
From Chapter 3 Gali(2008) \footnote{Gali, Jordi. Monetary Policy, inflation, and the Business Cycle: An introduction to the new Keynesian Framework. Princeton University Press, 2008.}, we know the following relations:
\begin{gather}
 \hat{y}_t^n = \psi_{ya}^n a_t\\
 \hat{r}_t^n = -\sigma\psi_{ya}^n(1-\rho_{a})a_t
\end{gather}
where $\psi_{ya}^n =\frac{(1+\varphi)}{\varphi+\alpha+\sigma(1-\alpha)} $.
Hence, we have:
\begin{gather}
  var(\hat{y}_t^n) =  (\psi_{ya}^n)^2 var(a_t) =  2.6681\times 10^{-4}\\
  var(\hat{r}_t^n) = [\sigma\psi_{ya}^n(1-\rho_{a})]^2  var(a_t) = 2.6681\times 10^{-6}
\end{gather}
\subsection{ Compute $var(\Delta\zeta_t)$}
Since the  exogenous money demand shock$\zeta_t$ is an AR(1) process,
\begin{equation}
var(\Delta\zeta_t) = \frac{\sigma(\zeta)}{1-\rho_{\zeta}^2} = 6.2016\times 10^{-5}
\end{equation}

\subsection{Show how to compute $\big[ var(\tilde{y}_t)\quad var(\pi_t) \quad var(\Delta\zeta_t) \big]'$}
Following page 56 of Gali(2008),  we know:
\begin{equation*}
A_{M,0}\equiv\left[
\begin{array}{ccc}
 1+\sigma\eta & 0  & 0  \\
  -\kappa& 1  & 0   \\
  0 & -1  &  1
\end{array}
\right], \quad
A_{M,1}\equiv\left[
\begin{array}{ccc}
 \sigma\eta & \eta  & 1  \\
  0& \beta  & 0   \\
  0 & 0  &  1
\end{array}
\right], \quad
B_{M}\equiv\left[
\begin{array}{ccc}
 \eta & -1 & 0  \\
 0& 0  & 0   \\
  0 & 0 &  -1
\end{array}
\right]
\end{equation*}
Split the linear matrix equation into a three equations system, we have:
\begin{gather}
 (1+\sigma\eta)\tilde{y}_{t} = \sigma\eta E_{t}(\tilde{y}_{t+1})+\hat{l}_{t}^{+}+\eta\hat{r_{t}}^{n} - \hat{y_{t}}^{n}\\
 -\kappa\tilde{y}_{t} + \pi_{t} = \beta E_{t}(\pi_{t+1})\\
 - \pi_{t} +\hat{l}_{t-1}^{+} = \hat{l_{t}}^{+} - \Delta\zeta_{t} \label{eq16}
\end{gather}
Taking expectation w.r.t equation (\ref{eq16}) and rearranging the equation we have,
\begin{equation}
 \hat{l_{t}}^{+} = E_{t}(\pi_{t+1})+E_{t}( \hat{l}_{t+1}^{+})-\rho_{\zeta} \Delta\zeta_{t}
\end{equation}
Equations (14), (15) and (17)  can be summarized compactly by the system:

\begin{equation}
\left[
\begin{array}{ccc}
 1+\sigma\eta & 0  & 0  \\
  -\kappa& 1  & 0   \\
  0 & 0  &  1
\end{array}
\right]
\left[
\begin{array}{c}
\tilde{y}_{t}\\
\pi_{t}\\
\hat{l_{t}}^{+}
\end{array}
\right]=
\left[
\begin{array}{ccc}
 \sigma\eta & \eta  & 1  \\
  0& \beta  & 0   \\
  0 & 1  &  1
\end{array}
\right]
\left[
\begin{array}{c}
E_{t}(\tilde{y}_{t+1})\\
E_{t}(\pi_{t+2})\\
E_{t}(\hat{l}_{t+1}^{+})
\end{array}
\right]
+
\left[
\begin{array}{cc}
 -1.4 & -0.6 \\
  0 & 0   \\
  0 & -0.6
\end{array}
\right]
\left[
\begin{array}{ccc}
 a_t    \\
   \Delta\zeta_{t} \\
\end{array}
\right]
\end{equation}

Denote
 \begin{equation}
A \left[
\begin{array}{c}
\tilde{y}_{t}\\
\pi_{t}\\
\hat{l_{t}}^{+}
\end{array}
\right]=B\left[
\begin{array}{c}
E_{t}(\tilde{y}_{t+1})\\
E_{t}(\pi_{t+2})\\
E_{t}(\hat{l}_{t+1}^{+})
\end{array}
\right]+C\left[
\begin{array}{c}
 a_t    \\
   \Delta\zeta_{t} \\
\end{array}
\right]
\end{equation}
Solve the system, we get
\begin{equation}
\left[
\begin{array}{c}
\tilde{y}_{t}\\
\pi_{t}\\
\hat{l_{t}}^{+}
\end{array}
\right]=D \left[
\begin{array}{c}
 a_t    \\
   \Delta\zeta_{t} \\
\end{array}
\right]
\end{equation}
Substituting the known parameters we can solve,
 \begin{equation}
D =
\left[
\begin{array}{cc}
  0.10496 & 0.28902   \\
 0.12277 &    0.5773 \\
  1.105&   −0.63406
\end{array}
\right]
\end{equation}
We have
 \begin{gather}
 var(\tilde{y}_{t}) = D(1,1)^2 var(a_t) + D(1,2)^2 var( \Delta\zeta_{t})\\
 var(\pi_{t}) = D(2,1)^2 var(a_t) + D(2,2)^2 var( \Delta\zeta_{t})
\end{gather}

\subsection{Compute the values of $\big[ var(\tilde{y}_t)\quad var(\pi_t) \quad var(\Delta\zeta_t) \big]'$ }
Substituting the known parameters into equation (22) an (23), we find:
\begin{equation*}
\begin{aligned}
var(\tilde{y}_{t}) = 8.1197 \times 10^{-6}\\
var(\pi_{t}) = 2.469 \times 10^{-5}\\
\end{aligned}
\end{equation*}
\subsection{Compute the average welfare loss per period.}
Frome equation (8), we find
L = 0.0075
\newpage

\section{Figure}

\begin{figure}
\centering
   \includegraphics[height=5cm, width=10cm]{img/doge.jpg}
  \caption{Doge}
  \label{fig:stock}
\end{figure}

\section{Codes}

\subsection{Codes for Part I}
\begin{verbatim}
%% ps6I_par.m This file stores the given parameter value.
beta = .99;
sigma = 1;
phi = 1;
alpha = 1/3;
epsilon = 6;
theta = 2/3;
phi_pi = 1.5;
phi_y = 0.5; % change this value to obtain various outcome
rho_v = 0.5;
rho_a = 0.9;

var_a = 0.00712^2/(1-0.9^2)
ps6_go

%% ps6I_go.m  This file undergo the procedure to calculate the value required
%Caculating parameters
phi_ya_n=(1+phi)/(phi+alpha+sigma*(1-alpha));
kappa=(sigma+(phi+alpha)/(1-alpha))*(1-theta)*(1-beta*theta)/theta*(1-alpha)/(1-alpha+alpha*epsilon);
kappa = lambda*(sigma+(phi+alpha)/(1-alpha));
Omega = 1/(sigma+phi_y+kappa*phi_pi)
AT = [sigma*Omega, (1-beta*phi_pi)*Omega; (kappa*sigma)*Omega, (kappa+beta*(sigma+phi_y))*Omega]
BT = [1*Omega; kappa*Omega]
residual_par = -psi_nya*(sigma*(1-rho_a)+phi_y);
BT_tilde = [-psi_nya*(sigma*(1-rho_a)+phi_y)*Omega; -kappa*psi_nya*(sigma*(1-rho_a)+phi_y)*Omega]
I = eye(2);
C = (I - rho_a*AT)^(-1)*BT_tilde
var_y = C(1)^2*var_a
var_pi = C(2)^2*var_a
WL = 0.5*((sigma+((phi+alpha)/(1-alpha)))*var_y + (epsilon/lambda)*var_pi)
\end{verbatim}

\subsection{Codes for Part II}
\begin{verbatim}
%% ps6II_par.m This file stores the given parameter value.
beta = .99;
sigma = 1;
phi = 1;
alpha = 1/3;
epsilon = 6;
theta = 2/3;
phi_pi = 1.5;
phi_y = 0.5; % change this value to obtain various outcome
rho_v = 0.5;
rho_a = 0.9;

var_zeta = var_zetaepsilon^2/(1-rho_zeta^2)
var_a = 0.00712^2/(1-0.9^2)
ps6_go

%% ps6II_go.m  This file undergo the procedure to calculate the value required
%Caculating parameters

var_ny = (psi_nya)^2*var_a
var_nr = (sigma*psi_nya*(1-rho_a))^2*var_a

phi_ya_n=(1+phi)/(phi+alpha+sigma*(1-alpha));
kappa=(sigma+(phi+alpha)/(1-alpha))*(1-theta)*(1-beta*theta)/theta*(1-alpha)/(1-alpha+alpha*epsilon);
kappa = lambda*(sigma+(phi+alpha)/(1-alpha));
Omega = 1/(sigma+phi_y+kappa*phi_pi)
AM0=[1+sigma*eta  0  0
  kappa 1  0
  0  0   1  ]
  AM1 =[sigma*eta  eta   1
  0 beta  0
  0  0   1  ]
 BM = [ eta  -1  0
 0 0   0
  0 0  -1  ]

  A = 1+sigma*eta  0  0
 - kappa 1  0
  0  -1  1  ]
  B= [sigma*eta  eta   1
  0 beta  0
  0  1   1  ]
  C= [-1.4  -0.6
          0  0
         0  -0.6]
 D = [  0.10496 0.28902
 0.12277 0.5773
1.105 0.63406]
var_y = D(1,1)^2*var_a+ D(1,1)^2*var_zeta
var_pi = D(2,1)^2*var_a + D(2,2)^2*var_zeta

WL = 0.5*((sigma+((phi+alpha)/(1-alpha)))*var_y + (epsilon/lambda)*var_pi)

\end{verbatim}

\end{document}


% -------------------------------------------------------------------------
%  This is a good place to collect feedback from first-time users.
% -------------------------------------------------------------------------

\section{From first-time users of Amanzi}
%
The purpose of this section is to describe first-hand experience in solving square
systems using Amanzi.
%
Square systems are those that can be written as
%
\begin{equation}
  \mbox{Find }u\in V:\;\forall v\in V\quad
  a(u,v) = f(v).
\end{equation}
%
The types of problems these systems include well-known differential systems like Poisson, advection-diffusion,
magneto- and electro-statics.
%
In summary, the process of solving these types of systems is as follows, begin by creating a mesh using the
{\tt MeshFactory}, then create a class for your PDE type as a derived class from {\tt PDE\_HelperDiscretization},
this will give access to a container for local matrices and a global operator that assembles these matrices as
well as routines for applying Dirichlet-type boundary conditions.
%
Thus, the next step is to populate the entries in the container for mass matrices, then assemble the global
system and the right-hand side and apply a linear solver.
%
\subsection{Defining your PDE class}\label{Sec:PDEClass}
%
The PDE class defined must be a derived class of PDE\_HelperDiscretization, this immediately gives
new class access to two important variables, a global operator and a container for local matrices and a series of
useful routines to apply boundary conditions, assemble global systems, etc.
%
This all comes with a caveat: you must define a member function called {it UpdateMatrices()} which is usually used to populate the local matrices, failure to do so will result in an abstract class with no possibility for instantiation.
%
The header for a class to solve the Poisson equation in second order form will look like
%
\begin{lstlisting}
class PDE_SecondOrderPoisson: public PDE_HelperDiscretization {
 public:
  PDE_SecondOrderPoisson(const Teuchos::RCP<const AmanziMesh::Mesh>& mesh);
  ~PDE_SecondOrderPoisson() {};

  // populate container of local matrices
  virtual
  void UpdateMatrices(const Teuchos::Ptr<const CompositeVector>& u,
                      const Teuchos::Ptr<const CompositeVector>& p);

  // postprocessing: calculate flux u from potential p
  virtual
  void UpdateFlux(const Teuchos::Ptr<const CompositeVector>& p,
                  const Teuchos::Ptr<CompositeVector>& u);

  // accessors
  Teuchos::RCP<CompositeVectorSpace> GetCVS() { return cvs_; }

 public:
  Teuchos::RCP<CompositeVectorSpace> cvs_;
};
\end{lstlisting}
%
Notice that in this class we have, additionally, defined a composite vector space as a class variable.
%
Composite vector spaces are factories for composite vectors which is an enhanced EPetra MultiVector.
%
This factory can help us create vectors for our trial or test spaces which in the case of a square system are the same or at least have the same set of degrees of freedom.
%
In what follows we will explain how to define the global operator, we note that this is all done in the constructor of the PDE class.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Constructing the local stiffness matrix}\label{Sec:NumericalScheme}
We will approximate, for a cell $P$, the bilinear form $a$ as
%
\begin{equation}
	a_P(p,q):=\int_P\nabla p\cdot\nabla q \approx p^I\cdot M_P q^I
\end{equation}
%
where the superscript $I$ refers to the vector of degrees of freedom of $p$ and $q$. The matrix $M$ is given by
%
\begin{equation}\label{eq:LocStiffnessMat}
	M_P = R(R^TN)^{\dagger}R^T+\lambda (I-N(N^TN)^{-1}N^T),\quad\lambda = \frac{2}{\# nodes}\mbox{tr}\left( R(R^TN)^{\dagger}R^T\right),
\end{equation}
%
for
%
\begin{equation}\label{eq:NandR}
	N = \begin{pmatrix}
		1 & x_{v_1}-x_P & y_{v_1}-y_P\\
		1 & x_{v_2}-x_P & y_{v_2}-y_P\\
		1 & x_{v_3}-x_P & y_{v_3}-y_P\\
		1 & x_{v_4}-x_P & y_{v_4}-y_P
	\end{pmatrix}\quad
	R = \frac{1}{2}\begin{pmatrix}
		0 & |e_4| n_x^{(4)}+|e_1|n_x^{(1)} & |e_4| n_y^{(4)}+|e_1|n_y^{(1)}\\
		0 & |e_1| n_x^{(1)}+|e_2|n_x^{(2)} & |e_1| n_y^{(1)}+|e_2|n_y^{(2)}\\
		0 & |e_2| n_x^{(2)}+|e_3|n_x^{(3)} & |e_2| n_y^{(2)}+|e_3|n_y^{(3)}\\
		0 & |e_3| n_x^{(3)}+|e_4|n_x^{(4)} & |e_3| n_y^{(3)}+|e_4|n_y^{(4)}
	\end{pmatrix}
\end{equation}
%
where $\left\{\left(x_{v_i},y_{v_i}\right):1\le i\le 4\right\}$ is the set of vertices of the rectangle $P$, $\left\{\left(n_x^{(i)},n_y^{(i)}\right):1\le i\le 4\right\}$ makes a set of outward normal vectors to the edges of $P$.
%
This derivation is an example of a mimetic method presented in chapter 4 of \cite{da2014mimetic}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Populating the local matrices and defining the global operator}\label{Sec:LocalMatAndGlobalOp}
%
The first step in populating the local matrices is to define a schema.
%
There should be one schema for the test space and one for the trial space, in the case of square systems the same can be used for both.
%
Schemas define the different aspects of a variable and its discretization.
%
Schemas require two inputs: the base and and an item.
%
The base describes what type of assembly is required for this variable the choices include cells, faces, edges and nodes all part of the AmanziMesh namespace.
%
Selecting, for example, faces as the base will imply that the local matrices are associated with the faces of the mesh.
%
Moreover, item defines the type of degrees of freedom that are used to discretize the variable in question.
%
Items require three inputs: a part of the topology of the mesh like a node or an edge which defines where the degrees of freedom are placed, the type of quantity the degree of freedom, whether it is scalar of vector valued and the number of degrees of freedom of this type.
%
For a classic finite element method to solve the Poisson equation the definition of its schema will look something like
%
\begin{lstlisting}
Schema p_schema;
// the assembly should run over the cells thus its base are the cells
p_schema.SetBase(AmanziMesh::CELL);

// the pressure dofs are cell-based and scalars
p_schema.AddItem(AmanziMesh::NODE,WhetStone::DOF_Type::SCALAR,1);
p_schema.Finalize(mesh);  // computes the starting position of the dof ids
\end{lstlisting}
%
Feeding the mesh to the schema, as shown in the last step, will create important variables used in the eventual assembly.
%
Once the necessary schemas are defined the local operator can be initialized and populated in a fairly straight-forward way.
%
It is a matter of feeding the schemas to the local operator and defining the necessary matrices. For example:
%
\begin{lstlisting}
local_op_ = Teuchos::rcp(new Op_Cell_Schema(p_schema,p_schema,mesh));
// populate the local matrices
for (int c = 0; c < ncells_owned ; c++) {
  Mcell(0,0) =  3, Mcell(0,1) = -1, Mcell(0,2) = -1, Mcell(0,3) = -1;
  Mcell(1,0) = -1, Mcell(1,1) =  3, Mcell(1,2) = -1, Mcell(1,3) = -1;
  Mcell(2,0) = -1, Mcell(2,1) = -1, Mcell(2,2) =  3, Mcell(2,3) = -1;
  Mcell(3,0) = -1, Mcell(3,1) = -1, Mcell(3,2) = -1, Mcell(3,3) =  3;
  local_op_->matrices[c] = Mcell;
}
\end{lstlisting}
%
The above demostrates how one can take an existing matrix and update the local operator.
%
In practice one must also build the required matrix.
%
Initally, say to define the matrix in \eqref{eq:LocStiffnessMat} one requires to find geometric features of the mesh like the coordinates of the nodes, the length of edges or vectors that are orthogonal to the boundary of a cell.
%
All of these can be attained from the mesh class from the commands displayed below
%
\begin{lstlisting}
AmanziMesh::Entity_ID_List nodeids, edgeids;
AmanziMesh::Entity_ID node0, node1, node2, node3;
AmanziGeometry::Point v0, v1, v2, v3;
AmanziGeometry::Point n0, n1, n2, n3, vP;
double l0, l1, l2, l3;

vP = mesh_->cell_centroid(c);
mesh->cell_get_edges(c, &edgeids);

mesh_->edge_get_nodes(edgeids[0], &node0, &node1);
mesh_->edge_get_nodes(edgeids[1], &node1, &node2);
mesh_->edge_get_nodes(edgeids[2], &node2, &node3);

mesh_->node_get_coordinates(node0, &v0);
mesh_->node_get_coordinates(node1, &v1);
mesh_->node_get_coordinates(node2, &v2);
mesh_->node_get_coordinates(node3, &v3);

l0 = mesh_->edge_length(edgeids[0]);
l1 = mesh_->edge_length(edgeids[1]);
l2 = mesh_->edge_length(edgeids[2]);
l3 = mesh_->edge_length(edgeids[3]);

n0 = mesh_->face_normal(edgeids[0], false, c);
n1 = mesh_->face_normal(edgeids[1], false, c);
n2 = mesh_->face_normal(edgeids[2], false, c);
n3 = mesh_->face_normal(edgeids[3], false, c);
\end{lstlisting}
%
Notice that to attain normal vectors we use the command {\tt face\_normal} this is because in amanzi faces and edges are the same entity in two dimensions.
%
Next, we will construct the matrices $N$ and $R$ as defined in \eqref{eq:NandR}.
%
They will be instances of the class DenseMatrix in the WhetStone namespace.
%
This will give us access to important member functions that perform standard operations in linear algebra like inverting or multiplying matrices.
%
Their construction is:
%
\begin{lstlisting}
WhetStone::DenseMatrix N(4,3);
WhetStone::DenseMatrix R(4,3);

N(0,0) = 1, N(0,1) = v0[0] - vP[0], N(0,2) = v0[1] - vP[1];
N(1,0) = 1, N(1,1) = v1[0] - vP[0], N(1,2) = v1[1] - vP[1];
N(2,0) = 1, N(2,1) = v2[0] - vP[0], N(2,2) = v2[1] - vP[1];
N(3,0) = 1, N(3,1) = v3[0] - vP[0], N(3,2) = v3[1] - vP[1];

R(0,0) = 0, R(0,1) = l3*n3[0]+l0*n0[0], R(0,2) = l3*n3[1]+l0*n0[1];
R(1,0) = 0, R(1,1) = l0*n0[0]+l1*n1[0], R(1,2) = l0*n0[1]+l1*n1[1];
R(2,0) = 0, R(2,1) = l1*n1[0]+l2*n2[0], R(2,2) = l1*n1[1]+l2*n2[1];
R(3,0) = 0, R(3,1) = l2*n2[0]+l3*n3[0], R(3,2) = l2*n2[1]+l3*n3[1];
\end{lstlisting}
%
Finally, all that we have left in order to construct the local stiffness matrices is to perform the operators in \eqref{eq:LocStiffnessMat}.
%
To do this we will define a series of temporary variables that store the variables in between.
%
\begin{lstlisting}
WhetStone::DenseMatrix TR(3,4);
WhetStone::DenseMatrix Mcell(4,4), Scell(4,4);
WhetStone::DenseMatrix temp1(3,3), temp2(4,3);

// formula: R(R^TN)^t R^T+lambda*(Id-N(N^TN)^{-1}N^T)/2
// lambda = tr(R(R^TN)^t R^T)

// the first summand.
temp1.Multiply(R, N, true);
temp1.InverseMoorePenrose();
temp2.Multiply(R, temp1, false);
TR.Transpose(R);
Mcell.Multiply(temp2, TR, false);

// the second summand.
double lambda = Mcell.Trace();
temp1.Multiply(N, N, true);
temp1.InverseSPD();
temp2.Multiply(N, temp1, false);
TR.Transpose(N);
Scell.Multiply(temp2, TR, false);

Scell *= -lambda / 2;
for(int i = 0; i < 4; i++) Scell(i, i) += lambda / 2;
local_op_->matrices[c] = Mcell + Scell;
\end{lstlisting}
%
The nested for loops that are performed towards the end of the above code are intended to substract the identity while simultaneously multiplying by lambda.
\\
To finalize we need to define the global operator which requires us to fist specify a composite vector space that is consistent with the schema that we defined in the local systems.
%
Thankfully schema has a routine that does this for us.
%
Thus, initializing the global operator can be done in four lines as follows:
%
\begin{lstlisting}
cvs_ = Teuchos::rcp(new CompositeVectorSpace(
    cvsFromSchema(p_schema, mesh, false)));

// constructor for a global operator requires a parameter list
Teuchos::ParameterList plist = Teuchos::ParameterList();

// create a global operator for the mass matrix
global_op_ = Teuchos::rcp(new Operator_Schema(cvs_, plist, p_schema));

// assign the corresponding container of local matrices
std::string my_name = "Diffusion: Poisson";
local_op_ = Teuchos::rcp(new Op_Cell_Node(my_name, mesh_));
global_op_->OpPushBack(local_op_);
\end{lstlisting}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Creating a mesh}\label{CreatingAMesh}
The class {\tt MeshFactory} gives the necessary tools to create a mesh.
Mesh factory requires some inputs: the MPI communicator and the preferences which provide the specific capability that is used.
A default communicator is already defined in Amanzi namespace and the preference is usually set
to the MSTK framework.

The mesh factory object can create meshes in several ways depending on the dimensionality (2D or 3D) and the types of cells.
A complete routine to build a simple quadrilateral mesh in 2D will look like this:
%
\begin{lstlisting}
auto comm = Amanzi::getDefaultComm();
MeshFactory factory(comm);
factory.set_preference(Preference({Framework::MSTK}));
// generate a square mesh covering [-1,1] x [-1,1] with 9 cells.
Teuchos::RCP<const Mesh> mesh = factory.create(-1.0,-1.0,1.0,1.0, 3,3);
\end{lstlisting}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Adding boundary conditions}\label{Sec:AddingBoundaryCond}
The class {\tt PDE\_HelperDiscretization} has some built-in features to impose boundary conditions
but in order to access them we need to define the object of class {\tt BCs} which takes as
creation arguments the mesh, where the degrees of freedom will be places and the type of degree
of freedom.
%
Moreover, we must also populate the two class variables, {\tt bc\_model} which defines what
type of boundary condition we want to prescribe and {\tt bc\_value} which precise value of such
boundary condition.
%
For example,
%
\begin{lstlisting}
// the BCs are placed on the nodes and are scalars
Teuchos::RCP<BCs> bcv = Teuchos::rcp(new BCs(
    mesh, AmanziMesh::NODE, WhetStone::DOF_Type::SCALAR));

std::vector<int>& bcv_model = bcv->bc_model();
std::vector<double>& bcv_value = bcv->bc_value();

Point xv(2);  // a point with two entries
// nnode_wghost is the number of nodes in the mesh including ghosts
for (int v = 0; v < nnodes_wghost; ++v) {
  mesh->node_get_coordinates(v, &xv);
  // This will identify which points lie in the boundary
  if (fabs(xv[0] + 1.0) < 1e-6 || fabs(xv[0] - 1.0) < 1e-6 ||
      fabs(xv[1] + 1.0) < 1e-6 || fabs(xv[1] - 1.0) < 1e-6) {
    bcv_model[v] = Operators::OPERATOR_BC_DIRICHLET;
    bcv_value[v] = 1.0;
  }
}
\end{lstlisting}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Assembly and imposing the boundary conditions}\label{Sec:AssemblyAndBoundaryCond}
%
Amanzi imposes boundary conditions by placing $1$ in the correct place in the global matrix and adding the value to the right hand side yielding the correct values in the final solution after the linear solve is performed. Thus, before imposing these conditions we must feed the right hand side to the object created by our PDE class. This is fairly simple since we already have created a composite vector space for the functions defined in our schema we can make use of this factory to initialize the right hand side and manually populate its entries as follows
%
\begin{lstlisting}
CompositeVector source(cvs);
Epetra_MultiVector& src = *source.ViewComponent("node");
for (int v = 0; v < nnodes_owned; v++) {
  mesh->node_get_coordinates(v, &xv);
  src[0][v] = 1.0;
}
\end{lstlisting}
%
Next we instantiate our PDE class, feed the right hand side, apply the boundary conditions and assemble the system
%
\begin{lstlisting}
auto op_poisson = Teuchos::rcp(new PDE_SecondOrderPoisson(mesh));
op_poisson->SetBCs(bcv, bcv);
op_poisson->UpdateMatrices(Teuchos::null, Teuchos::null);
op_poisson->ApplyBCs(true, true, true);

// global assembly
Teuchos::RCP<Operator> global_op = op_poisson->global_operator();
global_op->UpdateRHS(source, true);
global_op->SymbolicAssembleMatrix();
global_op->AssembleMatrix();
\end{lstlisting}
%
\subsection{The linear solve}\label{Sec:Linear Solve}
%
In order to apply a linear solver we must initialize a vector for the solution and initialize the preconditioner.
%
The linear solve is templated to fit the different types of scenarios where linear solves are necessary.
%
\begin{lstlisting}
const CompositeVectorSpace& cvs = *op_poisson->GetCVS();
CompositeVector solution(cvs);
solution.PutScalar(0.0);  // solution initialized with the value zero

// This file contains the specifications for the preconditioner
std::string xmlFileName = "test/operator_SecondOrderPoisson.xml";
Teuchos::ParameterXMLFileReader xmlreader(xmlFileName);
Teuchos::ParameterList plist = xmlreader.getParameters();
auto slist = plist.sublist("preconditioners").sublist("Hypre AMG");
global_op->InitializePreconditioner(slist);
global_op->UpdatePreconditioner();

auto olist = plist.sublist("solvers").sublist("PCG").sublist("pcg parameters");
AmanziSolvers::LinearOperatorPCG<Operator, CompositeVector,
    CompositeVectorSpace> pcg(global_op, global_op);

pcg.Init(olist);
CompositeVector& rhs = *global_op->rhs();
int ierr = pcg.ApplyInverse(rhs, solution);
\end{lstlisting}
%
The xml file used above contains the following information
%
\begin{lstlisting}[language=xml]
<ParameterList name="solvers">
 <ParameterList name="PCG">
  <Parameter name="iterative method" type="string" value="pcg"/>
  <ParameterList name="pcg parameters">
   <Parameter name="maximum number of iterations" type="int" value="20"/>
   <Parameter name="error tolerance" type="double" value="1e-12"/>
  </ParameterList>
 </ParameterList>
</ParameterList>

<ParameterList name="preconditioners">
 <ParameterList name="Hypre AMG">
  <Parameter name="discretization method" type="string" value="generic mfd"/>
  <Parameter name="preconditioner type" type="string" value="boomer amg"/>
  <ParameterList name="boomer amg parameters">
   <Parameter name="cycle applications" type="int" value="2"/>
   <Parameter name="smoother sweeps" type="int" value="3"/>
   <Parameter name="strong threshold" type="double" value="0.5"/>
   <Parameter name="tolerance" type="double" value="0.0"/>
   <Parameter name="relaxation type" type="int" value="6"/>
   <Parameter name="verbosity" type="int" value="0"/>
  </ParameterList>
 </ParameterList>
</ParameterList>
\end{lstlisting}
%


\documentclass{article}
\usepackage{amsmath}
\bibliographystyle{plain}

\begin{document}
\title{Live Path Refinement through Probabilistic Rules}
\author{Taylor Lloyd}

\maketitle

\section{Motivation}
    GPS sensor inaccuracies are well-known in existing literature, and multiple techniques exist for refining GPS data, and interpolating between GPS readings.
    However, existing work tends either to focus on efficiently modelling probabilities as gaussian distributions~\cite{kobayashiITIE98} or deterministically map positions onto a network~\cite{brakatsoulasVLDB05}.
    However, little work focuses on the properties of the objects being tracked by GPS. By constructing rules as continuous probabilities over a (position, velocity) state space, and propagating (position, velocity) forward through time, one can construct a more accurate path than by using GPS sensors alone.

\section{Background}

    There are four competing standards for global positioning today, GPS~(USA), D-GPS~(USA), GLONASS~(Russia), and Galileo~(EU). As these systems depend on satellite transmissions being observed on earth, the accuracy of these systems depends not only on the system used, but also on the number of satellites currently visible. With sufficient satellite coverage, none of these systems vary by more than 10 meters~\cite{liJG15}. However, as coverage changes over time, GPS signals are typically modelled with gaussian uncertainty.

    The Edmonton Transit System~(ETS) makes near-realtime position information available for all active ETS busses, providing a GPS position every 30 seconds, but at up to a minute's delay.
    Because this information is sparse and out-of-date when it arrives, interpolating between positions and projecting forward to the present can produce much more useful path information.

  GPS positions inherently include measurement error, as mentioned above, but naive attempts to interpolate between positions can cause dramatically more error if done poorly. Lonergan \textit{et al.} demonstrated that point-connecting strategies such as linear or quadratic interpolation have error that scales linearly with the time between GPS positions when tracking wild animals~\cite{lonergan09}.

\section{Related Work}

  Kalman Filters~\cite{kalmanJBE60} allow for the efficient combination of sensor data and historical state by modelling all inputs as gaussian uncertainties. In addition, it can accommodate for unknown or random inputs by reducing certainty as forward propagation continues. However, Kalman filters can only incorporate present sensor data, which makes them inappropriate for this use-case.

  Map-Matching~\cite{brakatsoulasVLDB05} is the process of mapping arbitrary locations onto a network. As a typical example, GPS positions are mapped onto the road network, to allow for routing and turn-by-turn directions.
Most map-matching algorithms attempt to map current positions to networks, though some are designed to match an entire path. When an entire path must be matched to the network, greedy strategies are typically employed, occasionally with lookahead to reduce error.

\section{Proposal}

This research seeks to apply the theory behind Kalman Filters to non-gaussian probabilities, in the context of moving objects with infrequent and inaccurate sensor updates. Specifically, this work will model a four-dimensional probability space $(p_x, p_y, v_x, v_y)$ over a series of timestamps, where $p_x$ and $p_y$ correspond to position and $v_x$ and $v_y$ correspond to velocity.

\subsection{Interpolation}
To produce probabilities for subsequent timestamps, probable velocities are applied to probable positions.
\begin{equation}
  P(p_{x'}, p_{y'}, v_x, v_y, t') = \sum_{p_x, p_y}
  \begin{cases}
    P(p_x, p_y, v_x, v_y, t), & \text{if } p_x + v_x = p_{x'} \text { and } p_y + v_y = p_{y'} \\
    0, & \text{otherwise}
  \end{cases}
\end{equation}

Then, uncertainty is added to the velocity to account for unknown acceleration or deceleration.
Finally, object-aware rules are applied to refine the estimates.

\subsection{Object-Aware Rules}

In this project, all object-aware rules must be independent of the current timestamp, and must be phrased as probabilities over the same 4D space as the objects themselves. These constraints allow the rules to be applied multiplicatively.

\begin{equation}
  P_{refined}(p_x, p_y, v_x, v_y, t) = P(p_x, p_y, v_x, v_y, t) \prod_{R \in \text{Rules}} R(p_x, p_y, v_x, v_y)
\end{equation}

Initially, the only rules explored will be the following:
\begin{enumerate}
    \item Busses drive along their routes, rarely making wrong turns
    \item Busses drive the speed limit in the correct direction, along roads
\end{enumerate}

The exact probability with which these rules hold true will be a tunable parameter in the final product.
Near-realtime bus locations are provided by Edmonton Open Data, as is route information. Information about road speed limits and directions is provided by Open Street Map.

\subsection{State Approximation}
In order to make the problem efficiently computable, states will be modelled as a grid, with linear interpolation between the nearest data points to provide a continuous space.
Probabilities will be limited in fidelity to the grid in which they are held. The exact fidelity of the grid is a parameter to be explored, but will likely be held constant in the final product.

\section{Evaluation}
As this work assumes that GPS locations are themselves unreliable, there is no \textit{true} path against which to compare the output of this work. Therefore, the focus of this evaluation will be in showing that the computed path is insensitive to error in the underlying GPS signal.

To provide a reproducible dataset, a quantity of historical bus coordinates will be captured.
Then the dataset will be permuted to produce a number of similar datasets by applying a gaussian error to each GPS position.

In order to produce a path for comparison, the $(p_x, p_y, t)$ tuple will be extracted from the most-probable state for each timestamp.
The paths produced for each permuted dataset will then be compared to the path produced for the original dataset. Error will then be calculated using the average of Euclidean distances over time.

\section{Expectations}

The outcome of this work will be a visual presentation operating on live input from the busses in Edmonton Transit, with the ability to toggle the object-aware rules to observe their effect on the predicted path.

In addition, this work will produce an evaluation of the reproducibility of paths using the method presented here, and how the reproducibility is effected by the rules designed.

\section{Detailed Timeline}
  This timeline has an optimistic end date, to allow for slip-time.

    \begin{itemize}
        \item Feb 25, 2017

        Prototype capable of capturing and consuming bus data, and applying naive forward estimation

        \item Mar 7, 2017

        Prototype including route and speed limit rules

        \item Mar 14, 2017

        Data capture and initial analysis of effectiveness

        \item Mar 21, 2017

        Final report and presentation ready
    \end{itemize}

\bibliography{references}
\end{document}