\documentclass[a4paper,11pt]{article}

\title{Calorimeter moments analysis}
\author{Luca Baldini (luca.baldini@pi.infn.it),
  Johan Bregeon (johan.bregeon@pi.infn.it)}

\usepackage{ifthen}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{bbm}
\usepackage[margin=2cm,bottom=3cm,top=4cm,marginparsep=0pt,marginparwidth=0pt]%
	   {geometry}
\usepackage[margin=1cm, font=small, labelfont=bf, labelsep=endash]{caption}

\newcommand{\pder}[2]{\frac{\partial#1}{\partial#2}}
\newcommand{\pdersec}[3]%
           {\ifthenelse{\equal{#2}{#3}}%
             {\frac{\partial^2#1}{\partial#2^2}}%
             {\frac{\partial^2#1}{\partial#2\partial#3}}%
           }
\newcommand{\itm}{\mathbbm I}
\newcommand{\itc}[1]{\itm_{#1}}
\newcommand{\firstder}[2]{\frac{{\rm d}#1}{{\rm d}#2}}
\newcommand{\secder}[2]{\frac{{\rm d}^2#1}{{\rm d}#2^2}}
\newcommand{\tmax}{t_{\rm max}}
\newcommand{\diff}{{\rm d}}
\newcommand{\xdir}{\ensuremath{x_{\rm dir}}}
\newcommand{\ydir}{\ensuremath{y_{\rm dir}}}
\newcommand{\zdir}{\ensuremath{z_{\rm dir}}}

\begin{document}

\maketitle

\abstract{These are some (more or less) random notes about the calorimeter
  moments analysis. The first part is a brief description of the basic
  code that I put together while trying to understand it.
  There's also some stuff dealing with possible improvements of the
  moments analysis, namely the measurement of the shower skeweness and the
  the error analysis aimed at the projection of the calorimeter clusters
  into the ACD.
}

\section{Basics of the CAL moments analysis}

The code for the moments analysis basically calculates the moment of inertia
tensor (using energy as the weight instead of mass) and then diagonalizes this
to get the three principal axes. The basic definitions can be found in
\cite{goldstein,landau} and in our case they read:
\begin{align}
  \itc{xx}  = \sum_{i=1}^n w_i(r_i^2 - x_i^2),\quad
  \itc{yy} &= \sum_{i=1}^n w_i(r_i^2 - y_i^2),\quad
  \itc{zz}  = \sum_{i=1}^n w_i(r_i^2 - z_i^2)\\
  \itc{xy}  = -\sum_{i=1}^n w_ix_iy_i,\quad
  \itc{xz} &= -\sum_{i=1}^n w_ix_iz_i,\quad
  \itc{yz}  = -\sum_{i=1}^n w_iy_iz_i
\end{align}
where the index $i$ runs over the $n$ hits in the calorimeter and the $w_i$ are
the weights associated with the hits (essentially the energy release).
In addition to the moment of inertia, the sum of weights and the coordinates
of the energy centroids are also used:
\begin{align}
  W &= \sum_{i=1}^n w_i\\
  \mathbf{r}_c &= \frac{\sum_{i=1}^n w_i\mathbf{r}_i}{W}
\end{align}

In order to reduce the tensor of inertia to the principal axes we do have to
solve the secular equation:
\begin{equation}
  \det(\itm - \lambda{\mathbbm 1}) =
  \det\begin{pmatrix}
  \itc{xx} - \lambda & \itc{xy} & \itc{xz}\\
  \itc{xy} & \itc{yy} - \lambda & \itc{yz}\\
  \itc{xz} & \itc{yz} & \itc{zz} - \lambda
  \end{pmatrix} = 0
\end{equation}
which is a cubic equation  in $\lambda$ yielding the three eigenvalues.
By working out the tedious algebra we can write the equation as:
$$
\lambda^3 + c_2\lambda^2 + c_1\lambda + c_0 = 0
$$
where:
\begin{align}
  c_2 &= -(\itc{xx} + \itc{yy} + \itc{zz})\\
  c_1 &= \itc{xx}\itc{yy} + \itc{yy}\itc{zz} + \itc{xx}\itc{zz} -
  (\itc{xy}^2 + \itc{yz}^2 + \itc{xz}^2)\\
  c_0 &= -\itc{xx}\itc{yy}\itc{zz} - 2\itc{xy}\itc{yz}\itc{xz} +
  \itc{xx}\itc{yz}^2 + \itc{yy}\itc{xz}^2 + \itc{zz}\itc{xy}^2
\end{align}
If we now define a new variable $\lambda' = \lambda + c_2/3$, the previous
equation becomes:
$$
\lambda'^3 + a\lambda' + b = 0
$$
where:
\begin{align}
  a &= \left(\frac{3c_1 - c_2^2}{3}\right)\\
  b &= \left(\frac{27c_0 + 2c_2^2 - 9c_1c_2}{27}\right)
\end{align}
(the algebra is fully worked out in \cite{wolfram}).
We now set:
\begin{align}
  m &= 2\sqrt{\frac{-a}{3}}\\
  \psi &= \frac{1}{3}\arccos\left(\frac{3b}{am}\right)
\end{align}
and, finally we get the three real solutions (guranteed by the fact that the
tensor of inertia is symmetruc):
\begin{align}
  \lambda_0 &= m\cos(\psi) - c_2/3\\
  \lambda_1 &= m\cos(\psi + 2\pi/3) - c_2/3\\
  \lambda_2 &= m\cos(\psi + 4\pi/3) - c_2/3
\end{align}
Once we have the three eigenvalues we can work out the calcultaion of the
eigenvectors $\mathbf{e}^i$ ($i = 1\ldots3$) defined by:
$$
\itm\mathbf{e}^i = \lambda_i\mathbf{e}^i
$$
Following these conventions, $\lambda_1$ is the largest eigenvalue and,
as a consequence, $\mathbf{e}^1$ is the principal axis of the cluster.

Once the three principal axis of the cluster have been found, the
cluster $\chi^2$ (normalized to the number of \emph{degree of freedom}) is
calculated as:
\begin{equation}
  \chi^2 = \frac{\sum_{i=1}^n w_i d_i^2}{nW}
\end{equation}
where $d_i$ are the distances from each of the calorimeter hits to the
axis parallel to $\mathbf{e}^1$ and passing throught the cluster
centroid. Finally the some well know Merit quantities are calculated:
\begin{align}
  \texttt{CalTransRms} &= \sqrt{\frac{|\lambda_1|}{W}}\\
  \texttt{CalLongRms}  &= \sqrt{\frac{|\lambda_0| + |\lambda_2|}{2W\log L}}\\
  \texttt{CalLRmsAsym} &= \sqrt{\frac{|\lambda_0| - |\lambda_2|}
    {|\lambda_0| + |\lambda_2|}}
\end{align}
where $L$ is the number of radiation lengths transversed.

\subsection{Outline of the iterative moments analysis}

Put here some details about the iteration scheme, as they are
relevant for the calculation of the skewness (i.e. you get significantly
different answers at different steps).

\section{The (toy) problem in 2D}

I thought the problem of the diagonalization of the inertia tensor in 2D
could be useful for working out the error analysis, so here is a quick look at
it. The basic definitions read as:
\begin{align}
  \itc{xx}  = \sum_{i=1}^n w_i y_i^2,\quad
  \itc{yy}  = \sum_{i=1}^n w_i x_i^2,\quad
  \itc{xy}  = -\sum_{i=1}^n w_ix_iy_i
\end{align}
and the secular equation is:
\begin{equation}
  \det(\itm - \lambda{\mathbbm 1}) =
  \det\begin{pmatrix}
  \itc{xx} - \lambda & \itc{xy}\\
  \itc{xy} & \itc{yy} - \lambda
  \end{pmatrix} = 0
\end{equation}
The eigenvalues are readily found:
\begin{align}
  \lambda_0 &= \frac{(\itc{xx} + \itc{yy}) -
    \sqrt{(\itc{xx} - \itc{yy})^2 + 4\itc{xy}^2}}{2}\\
  \lambda_1 &= \frac{(\itc{xx} + \itc{yy}) +
    \sqrt{(\itc{xx} - \itc{yy})^2 + 4\itc{xy}^2}}{2}\\
\end{align}

At this point, assuming that $\lambda_0$ is the smallest eigenvalue, we're
left with the problem of calculating the corresponding eigenvector
$\mathbf{e}^0$, obeying the equation:
$$
\itm \mathbf{e}^0 = \lambda_0\mathbf{e}^0
$$
Being the problem bi-dimensional, the two eigenvectors can be parametrized in
terms of a single rotation angle $\phi$ (to be determined), representing the
angle of the principal axis with respect to the original reference frame i.e.:
\begin{equation}
  \begin{pmatrix}
    e^0_x\\
    e^0_y
  \end{pmatrix} =
  \begin{pmatrix}
    \cos\phi\\
    \sin\phi
  \end{pmatrix},\quad
  \begin{pmatrix}
    e^1_x\\
    e^1_y
  \end{pmatrix} =
  \begin{pmatrix}
    -\sin\phi\\
    \cos\phi
  \end{pmatrix}
\end{equation}
By putting everything together, from the definition of the principal axis
we get:
$$
\begin{pmatrix}
  \itc{xx} & \itc{xy}\\
  \itc{xy} & \itc{yy}
\end{pmatrix}
\begin{pmatrix}
  \cos\phi\\
  \sin\phi
\end{pmatrix} = \lambda_1
\begin{pmatrix}
  \cos\phi\\
  \sin\phi
\end{pmatrix}
$$
The first of the two equations in the system can be squared, yielding:
$$
\frac{(\itc{xx} - \itc{yy})}{\itc{xy}}\tan\phi = \tan^2\phi - 1
$$
and eventually, through the trigonometric equation
$$
\tan(2\phi) = \frac{2\tan\phi}{1 - \tan^2\phi}
$$
we get:
\begin{equation}
  \phi = -\frac{1}{2}
  \arctan \left( \frac{2\itc{xy}}{\itc{yy} - \itc{xx}} \right)
\end{equation}
The rotation matrix between the original system and the one defined by the
principal axis has the (transpose of) the two eigenvectors as its rows:
\begin{equation}
  S =
  \begin{pmatrix}
    e^0_x & e^0_y\\
    e^1_x & e^1_y
  \end{pmatrix} =
  \begin{pmatrix}
    \cos\phi & \sin\phi\\
    -\sin\phi & \cos\phi
  \end{pmatrix}
\end{equation}
and obviously we have:
\begin{equation}\label{eq:rotation}
  \lambda =
  \begin{pmatrix}
    \lambda_0 & 0\\
    0 & \lambda_1
  \end{pmatrix} = S\;\itm\;S^{-1} = S\;\itm\;S^{\rm T}
\end{equation}

\subsection{Error analysis in 2 dimensions}

As a first ingredient we'll need the derivatives of the components of the
tensor of inertia with respect to the coordinates and the weights:
\begin{align}
\pder{\itc{xx}}{x_i} &= 0,\quad
\pder{\itc{xx}}{y_i}  = 2w_iy_i,\quad
\pder{\itc{xx}}{w_i}  = y_i^2\\
\pder{\itc{yy}}{x_i} &= 2w_ix_i,\quad
\pder{\itc{yy}}{y_i}  = 0,\quad
\pder{\itc{yy}}{w_i}  = x_i^2\\
\pder{\itc{xy}}{x_i} &= -w_iy_i,\quad
\pder{\itc{xy}}{y_i}  = -w_ix_i,\quad
\pder{\itc{xy}}{w_i}  = -x_iy_i
\end{align}
We can then calculate the full covariance matrix of the errors using the
usual formula (see \cite{pdg}, section 32.1.4 for instance). Assuming that
the errors on the two positions and on the weights are mutually not correlated
(i.e. their covariance matrix is diagonal), we have:
\begin{equation}
  \Sigma_{k-l} = \sum_{i=1}^n
  \pder{\itc{k}}{x_i}\pder{\itc{l}}{x_i}(\Delta x_i)^2 +
  \pder{\itc{k}}{y_i}\pder{\itc{l}}{y_i}(\Delta y_i)^2 +
  \pder{\itc{k}}{w_i}\pder{\itc{l}}{w_i}(\Delta w_i)^2
\end{equation}
where $k$ and $l$ run over the three (double) indexes $xx$, $yy$ and $xy$.
We're ready to work out the details:
\begin{align}
  \Sigma_{xx-xx} &= (\Delta\itc{xx})^2 =
  \sum_{i=1}^n \left[4w^2_iy^2_i(\Delta y_i)^2 + y_i^4(\Delta w_i)^2\right]\\
  \Sigma_{yy-yy} &= (\Delta\itc{yy})^2 =
  \sum_{i=1}^n \left[4w^2_ix^2_i(\Delta x_i)^2 + x_i^4(\Delta w_i)^2\right]\\
  \Sigma_{xy-xy} &= (\Delta\itc{xy})^2 =
  \sum_{i=1}^n \left[w_i^2y_i^2(\Delta x_i)^2 + w_i^2x_i^2(\Delta y_i)^2 +
    x_i^2y_i^2(\Delta w_i)^2 \right]\\
  \Sigma_{xx-yy} &=
  \sum_{i=1}^n \left[ x_i^2y_i^2 (\Delta w_i)^2\right]\\
  \Sigma_{xx-xy} &=
  -\sum_{i=1}^n \left[ 2w_i^2x_iy_i(\Delta y_i)^2 +
    x_iy_i^3(\Delta w_i)^2 \right]\\
  \Sigma_{xy-yy} &=
  -\sum_{i=1}^n \left[ 2w_i^2x_iy_i(\Delta x_i)^2 +
    x_i^3y_i(\Delta w_i)^2 \right]
\end{align}

The rest of this section follows closely the prescription described in
\cite{errors} for the error propagation. We can slice the $2 \times 2$ tensor
of inertia and define a 4-component vector with the two columns one on top
of the other (this is what we call the $vec$ operator):
\begin{equation}
  vec(\itm) =
  \begin{pmatrix}
    \itc{xx}\\
    \itc{xy}\\
    \itc{xy}\\
    \itc{yy}
  \end{pmatrix}
\end{equation}
That said, we can rewite the equation (\ref{eq:rotation}) using the
Kronecker product of the rotation matrix
$$
T = S \otimes S
$$
as:
\begin{equation}\label{eq:tensor_transform}
  vec(\lambda) =
  \begin{pmatrix}
    \lambda_0\\
    0\\
    0\\
    \lambda_1
  \end{pmatrix} = T vec(\itm)
\end{equation}
It is useful to rearrange the the elements of the $vec$ operator in such a way
that the diagonal elements of the tensor come first, followed by the
non-diagonal ones, getting rid of the duplicated terms.
This is accomplished by introducing the matrix:
\begin{equation}
  D =
  \begin{pmatrix}
    1 & 0 & 0 & 0\\
    0 & 0 & 0 & 1\\
    0 & \frac{1}{2} & \frac{1}{2} & 0
  \end{pmatrix}
\end{equation}
which allows to define a new operator $v_d$:
\begin{equation}
  v_d(\itm) = D vec(\itm) =
  \begin{pmatrix}
    1 & 0 & 0 & 0\\
    0 & 0 & 0 & 1\\
    0 & \frac{1}{2} & \frac{1}{2} & 0
  \end{pmatrix}
  \begin{pmatrix}
    \itc{xx}\\
    \itc{xy}\\
    \itc{xy}\\
    \itc{yy}
  \end{pmatrix} =
  \begin{pmatrix}
    \itc{xx}\\
    \itc{yy}\\
    \itc{xy}
  \end{pmatrix}
\end{equation}
Talking about which, the covariance matrix of $v_d(\itm)$ reads:
\begin{equation}
  \Sigma_{v_d(\itm)} =
  \begin{pmatrix}
    \Sigma_{xx-xx} & \Sigma_{xx-yy} & \Sigma_{xx-xy}\\
    \Sigma_{xx-yy} & \Sigma_{yy-yy} & \Sigma_{xy-yy}\\
    \Sigma_{xx-xy} & \Sigma_{xy-yy} & \Sigma_{xy-xy}
  \end{pmatrix}
\end{equation}
in terms of the quantities we have calculated a few lines above.
In order to go back from the $v_d$ to the $vec$ representation we need
the so called pseudo-inverse of $D$:
\begin{equation}
  D^+ =
  \begin{pmatrix}
    1 & 0 & 0\\
    0 & 0 & 1\\
    0 & 0 & 1\\
    0 & 1 & 0
  \end{pmatrix}
\end{equation}
The paper \cite{errors} is wrong to this respect (see \cite{errors_corr}).
Using these definitions, equation (\ref{eq:tensor_transform}) can be rewritten
as:
\begin{equation}
  v_d(\lambda) =
  \begin{pmatrix}
    \lambda_0\\
    \lambda_1\\
    0
  \end{pmatrix} =
  D vec(\lambda) = DT vec(\itm) = DTD^+ v_d(\itm)
\end{equation}
If we define:
\begin{equation}
  V = DTD^+
\end{equation}
we can rewrite the previous equation as:
\begin{equation}
  v_d(\lambda) = V v_d(\itm)
\end{equation}

The infinitesimal change $\diff S$ in the rotation matrix $S$ when we change
the rotation angle $\phi$ by an infinitesimal amount $\diff\phi$ can be easily
calculated by differentiating $S$ itself:
$$
\diff S =
\begin{pmatrix}
  -\sin\phi\;\diff\phi & \cos\phi\;\diff\phi\\
  -\cos\phi\;\diff\phi & -\sin\phi\;\diff\phi\\
\end{pmatrix}
$$
If we introduce the antisimmetric tensor:
\begin{equation}
  \Omega =
  \begin{pmatrix}
    0 & -\diff\phi\\
    \diff\phi & 0
  \end{pmatrix}
\end{equation}
we can rewrite the previous equation as:
\begin{equation}
  \diff S = -S\Omega
\end{equation}
and, along the same lines, we have:
\begin{equation}
  \diff S^{\rm T} = \Omega S^{\rm T}
\end{equation}
as can be verified by direct matrix multiplication. It seems a bit odd, here,
to call $\Omega$ an infinitesimal quantity (I would have rather named it
$\diff\Omega$), but we'll bravely follow the conventions used in the paper to
avoid confusion.
We now define the quantity:
\begin{equation}
  \Omega^p = S\Omega S^{\rm T} =
\begin{pmatrix}
  0 & -\diff\phi\\
  \diff\phi & 0\\
\end{pmatrix} = \Omega
\end{equation}
By putting all together we have:
\begin{equation}
  S \diff \itm S^{\rm T} = \diff\mu =
  \Omega^p\lambda - \lambda\Omega^p + \diff\lambda =
  \begin{pmatrix}
    \diff\lambda_0 & (\lambda_0 - \lambda_1)\diff\phi\\
    (\lambda_0 - \lambda_1)\diff\phi & \diff\lambda_1
  \end{pmatrix}
\end{equation}
and we can write:
\begin{equation}
  vec(\diff\mu) = G\beta
\end{equation}
where:
\begin{equation}
  G =
  \begin{pmatrix}
    1 & 0 & 0\\
    0 & 0 & (\lambda_1 - \lambda_0)\\
    0 & 0 & (\lambda_1 - \lambda_0)\\
    0 & 1 & 0
  \end{pmatrix}
\end{equation}
and
\begin{equation}
  \beta =
  \begin{pmatrix}
    \diff\lambda_0\\
    \diff\lambda_1\\
    -\diff\phi
  \end{pmatrix}
\end{equation}
(again, $\beta$ is a differential quantities, so it is a bit odd to name
it without a $\diff$ in front). Further on through the paper:
\begin{equation}
  v_d(\diff\itm) =
  \begin{pmatrix}
    d\itc{xx}\\
    d\itc{yy}\\
    d\itc{xy}
  \end{pmatrix} =
  D (S^{\rm T} \otimes S^{\rm T}) vec(d\mu) =
  D (S^{\rm T} \otimes S^{\rm T}) G\beta = F\beta
\end{equation}
where we have defined:
\begin{equation}
  F = D (S^{\rm T} \otimes S^{\rm T}) G
\end{equation}
All we have to do is invert this equation, namely find $F^{-1}$. If we were
dealing with square matrices, all we had to do would be:
$$
F^{-1} = \left( D (S^{\rm T} \otimes S^{\rm T}) G \right)^{-1} =
G^{-1} \left( S^{\rm T} \otimes S^{\rm T} \right)^{-1} D^{-1} =
G^{-1} \left( S \otimes S \right) D^{-1} = G^{-1} T D^{-1}
$$
But in fact $G$ and $D$ are rectangular matrices, so we need again the pseudo
inverses. We have already the one for $D$, while for $G$, since
$G^TG$ is not singular, the solution is even easier:
\begin{equation}
  G^+ = \left( G^{\rm T}G \right)^{-1} G^{\rm T} =
  \begin{pmatrix}
    1 & 0 & 0 & 0\\
    0 & 0 & 0 & 1\\
    0 & \frac{1}{2(\lambda_1-\lambda_0)} & \frac{1}{2(\lambda_1-\lambda_0)} & 0
  \end{pmatrix}
\end{equation}
(this satisfies $G^+G = I_{3\times3}$, as can be easily verified by direct
multiplication). Summarizing:
\begin{equation}
  \beta = F^{-1}v_d(\diff\itm)
\end{equation}
and, more important, we get the full covariance matrix of the eigenvalues
and rotation angle:
\begin{equation}
  \Sigma_\beta = F^{-1} \Sigma_{v_d(\itm)} (F^{-1})^T
\end{equation}
Once we have the error on the rotation angle:
\begin{equation}
  (\Delta\phi)^2 = (\Sigma_\beta)_{33}
\end{equation}
the covariance matrix of the two components of the principal axis is:
\begin{equation}
  \Sigma_{\mathbf{e}^0} =
  \begin{pmatrix}
    \pder{e^0_x}{\phi}\pder{e^0_x}{\phi}(\Delta\phi)^2 &
    \pder{e^0_x}{\phi}\pder{e^0_y}{\phi}(\Delta\phi)^2\\
    \pder{e^0_x}{\phi}\pder{e^0_y}{\phi}(\Delta\phi)^2 &
    \pder{e^0_y}{\phi}\pder{e^0_y}{\phi}(\Delta\phi)^2
  \end{pmatrix} =
  \begin{pmatrix}
    \sin^2\phi & -\sin\phi\cos\phi\\
    -\sin\phi\cos\phi & \cos^2\phi
  \end{pmatrix}(\Delta\phi)^2
\end{equation}

\section{Error analysis in 3 dimensions}

Along the same lines of the previous subsection the derivatives of the
components of the inertia tensor are:
\begin{align}
\pder{\itc{xx}}{x_i} &= 0,\quad
\pder{\itc{xx}}{y_i}  = 2w_iy_i,\quad
\pder{\itc{xx}}{z_i}  = 2w_iz_i,\quad
\pder{\itc{xx}}{w_i}  = (y_i^2 + z_i^2)\\
\pder{\itc{yy}}{x_i} &= 2w_ix_i,\quad
\pder{\itc{yy}}{y_i}  = 0,\quad
\pder{\itc{yy}}{z_i}  = 2w_iz_i,\quad
\pder{\itc{yy}}{w_i}  = (x_i^2 + z_i^2)\\
\pder{\itc{zz}}{x_i} &= 2w_ix_i,\quad
\pder{\itc{zz}}{y_i}  = 2w_iy_i,\quad
\pder{\itc{zz}}{z_i}  = 0,\quad
\pder{\itc{zz}}{w_i}  = (x_i^2 + y_i^2)\\
\pder{\itc{xy}}{x_i} &= -w_iy_i,\quad
\pder{\itc{xy}}{y_i}  = -w_ix_i,\quad
\pder{\itc{xy}}{z_i}  = 0,\quad
\pder{\itc{xy}}{w_i}  = -x_iy_i\\
\pder{\itc{xz}}{x_i} &= -w_iz_i,\quad
\pder{\itc{xz}}{y_i}  = 0,\quad
\pder{\itc{xz}}{z_i}  = -w_ix_i,\quad
\pder{\itc{xz}}{w_i}  = -x_iz_i\\
\pder{\itc{yz}}{x_i} &= 0,\quad
\pder{\itc{yz}}{y_i}  = -w_iz_i,\quad
\pder{\itc{yz}}{z_i}  = -w_iy_i,\quad
\pder{\itc{yz}}{w_i}  = -y_iz_i
\end{align}
The elements of the covariance matrix are:
\begin{equation}
  \Sigma_{k-l} = \sum_{i=1}^n
  \pder{\itc{k}}{x_i}\pder{\itc{l}}{x_i}(\Delta x_i)^2 +
  \pder{\itc{k}}{y_i}\pder{\itc{l}}{y_i}(\Delta y_i)^2 +
  \pder{\itc{k}}{z_i}\pder{\itc{l}}{z_i}(\Delta z_i)^2 +
  \pder{\itc{k}}{w_i}\pder{\itc{l}}{w_i}(\Delta w_i)^2
\end{equation}
where now $k$ and $l$ run over the 6 independent (double) indexes of the
indertia tensor $xx$, $yy$, $zz$, $xy$, $xz$ and $yz$. Therefore the
$6\times6$ covariancewe will have $6(6+1)/2 = 21$ components:
\begin{align}
  \Sigma_{xx-xx} &= \sum_{i=1}^n \left\{
  4w_i^2 \left[ y_i^2(\Delta y_i)^2 + z_i^2(\Delta z_i)^2 \right] +
  \left(y_i^2 + z_i^2\right)^2(\Delta w_i)^2
  \right\}\\
  \Sigma_{xx-yy} &= \sum_{i=1}^n \left\{
  4w_i^2z_i^2 (\Delta z_i)^2 + (x_i^2 + z_i^2)(y_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{xx-zz} &= \sum_{i=1}^n \left\{
  4w_i^2y_i^2 (\Delta y_i)^2 + (x_i^2 + y_i^2)(y_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{xx-xy} &= -\sum_{i=1}^n x_iy_i  \left\{
  2w_i^2 (\Delta y_i)^2 + (y_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{xx-xz} &= -\sum_{i=1}^n x_iz_i \left\{
  2w_i^2 (\Delta z_i)^2 + (y_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{xx-yz} &= -\sum_{i=1}^n y_iz_i \left\{
  2w_i^2 \left[ (\Delta y_i)^2 + (\Delta z_i)^2 \right] +
  (y_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{yy-yy} &= \sum_{i=1}^n \left\{
  4w_i^2 \left[ x_i^2(\Delta x_i)^2 + z_i^2(\Delta z_i)^2 \right] +
  \left(x_i^2 + z_i^2\right)^2(\Delta w_i)^2
  \right\}\\
  \Sigma_{yy-zz} &= \sum_{i=1}^n \left\{
  4w_i^2x_i^2 (\Delta x_i)^2 + (x_i^2 + y_i^2)(x_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{yy-xy} &= -\sum_{i=1}^n x_iy_i \left\{
  2w_i^2 (\Delta x_i)^2 + (x_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{yy-xz} &= -\sum_{i=1}^n x_iz_i \left\{
  2w_i^2 \left[ (\Delta x_i)^2 + (\Delta z_i)^2 \right] +
  (x_i^2 + z_i^2)(\Delta w_i)^2
  \right\}
\end{align}
\begin{align}
  \Sigma_{yy-yz} &= -\sum_{i=1}^n y_iz_i \left\{
  2w_i^2 (\Delta z_i)^2 + (x_i^2 + z_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{zz-zz} &= \sum_{i=1}^n \left\{
  4w_i^2 \left[ x_i^2(\Delta x_i)^2 + y_i^2(\Delta y_i)^2 \right] +
  \left(x_i^2 + y_i^2\right)^2(\Delta w_i)^2
  \right\}\\
  \Sigma_{zz-xy} &= -\sum_{i=1}^n x_iy_i \left\{
  2w_i^2 \left[ (\Delta x_i)^2 + (\Delta y_i)^2 \right] +
  (x_i^2 + y_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{zz-xz} &= -\sum_{i=1}^n x_iz_i \left\{
  2w_i^2 (\Delta x_i)^2 + (x_i^2 + y_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{zz-yz} &= -\sum_{i=1}^n y_iz_i \left\{
  2w_i^2 (\Delta y_i)^2 + (x_i^2 + y_i^2)(\Delta w_i)^2
  \right\}\\
  \Sigma_{xy-xy} &= \sum_{i=1}^n \left\{
  w_i^2 \left[ y_i^2 (\Delta x_i)^2 + x_i^2 (\Delta y_i)^2 \right] +
  x_i^2y_i^2 (\Delta w_i)^2
  \right\}\\
  \Sigma_{xy-xz} &= \sum_{i=1}^n y_iz_i \left\{
  w_i^2 (\Delta x_i)^2 + x_i^2 (\Delta w_i)^2
  \right\}\\
  \Sigma_{xy-yz} &= \sum_{i=1}^n x_iz_i \left\{
  w_i^2 (\Delta y_i)^2 + y_i^2 (\Delta w_i)^2
  \right\}\\
  \Sigma_{yz-yz} &= \sum_{i=1}^n \left\{
  w_i^2 \left[ z_i^2 (\Delta y_i)^2 + y_i^2 (\Delta z_i)^2 \right] +
  y_i^2z_i^2 (\Delta w_i)^2
  \right\}\\
  \Sigma_{yz-xz} &= \sum_{i=1}^n x_iy_i \left\{
  w_i^2 (\Delta z_i)^2 + z_i^2 (\Delta w_i)^2
  \right\}\\
  \Sigma_{xz-xz} &= \sum_{i=1}^n \left\{
  w_i^2 \left[ z_i^2 (\Delta x_i)^2 + x_i^2 (\Delta z_i)^2 \right] +
  x_i^2z_i^2 (\Delta w_i)^2
  \right\}
\end{align}
In analogy with the two-dimensional case we define:
\begin{equation}
  D =
  \begin{pmatrix}
    1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
    0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
    0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
    0 & \frac{1}{2} & 0 & \frac{1}{2} & 0 & 0 & 0 & 0 & 0\\
    0 & 0 & \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 & 0\\
    0 & 0 & 0 & 0 & 0 & \frac{1}{2} & 0 & \frac{1}{2} & 0
  \end{pmatrix}
\end{equation}
so that
\begin{equation}
  v_d(\itm) = D vec(\itm) =
  \begin{pmatrix}
    \itc{xx}\\
    \itc{yy}\\
    \itc{zz}\\
    \itc{xy}\\
    \itc{xz}\\
    \itc{yz}
  \end{pmatrix}
\end{equation}
and the $6\times6$ covariance matrix of the components of the inertia tensor
is:
\begin{equation}
  \Sigma_{v_d(\itm)} =
  \begin{pmatrix}
    \Sigma_{xx-xx} & \Sigma_{xx-yy} & \Sigma_{xx-zz} &
    \Sigma_{xx-xy} & \Sigma_{xx-xz} & \Sigma_{xx-yz}\\
    \Sigma_{xx-yy} & \Sigma_{yy-yy} & \Sigma_{yy-zz} &
    \Sigma_{yy-xy} & \Sigma_{yy-xz} & \Sigma_{yy-yz}\\
    \Sigma_{xx-zz} & \Sigma_{yy-zz} & \Sigma_{zz-zz} &
    \Sigma_{zz-xy} & \Sigma_{zz-xz} & \Sigma_{zz-yz}\\
    \Sigma_{xx-xy} & \Sigma_{yy-xy} & \Sigma_{zz-xy} &
    \Sigma_{xy-xy} & \Sigma_{xy-xz} & \Sigma_{xy-yz}\\
    \Sigma_{xx-xz} & \Sigma_{yy-xz} & \Sigma_{zz-xz} &
    \Sigma_{xy-xz} & \Sigma_{xz-xz} & \Sigma_{xz-yz}\\
    \Sigma_{xx-yz} & \Sigma_{yy-yz} & \Sigma_{zz-yz} &
    \Sigma_{xy-yz} & \Sigma_{xz-yz} & \Sigma_{yz-yz}\\
  \end{pmatrix}
\end{equation}
In the three-dimensional case $D^+$ reads:
\begin{equation}
  D^+ =
  \begin{pmatrix}
    1 & 0 & 0 & 0 & 0 & 0\\
    0 & 0 & 0 & 1 & 0 & 0\\
    0 & 0 & 0 & 0 & 1 & 0\\
    0 & 0 & 0 & 1 & 0 & 0\\
    0 & 1 & 0 & 0 & 0 & 0\\
    0 & 0 & 0 & 0 & 0 & 1\\
    0 & 0 & 0 & 0 & 1 & 0\\
    0 & 0 & 0 & 0 & 0 & 1\\
    0 & 0 & 1 & 0 & 0 & 0
  \end{pmatrix}
\end{equation}
and the rotation matrix is obviously:
\begin{equation}
  S =
  \begin{pmatrix}
    e^0_x & e^0_y & e^0_z\\
    e^1_x & e^1_y & e^1_z\\
    e^2_x & e^2_y & e^2_z
  \end{pmatrix}
\end{equation}
The last pieces of information that we need are:
\begin{equation}
  S_p =
  \begin{pmatrix}
    0 & S_{20} & -S_{10}\\
    -S_{20} & 0 & S_{00}\\
    S_{10} & -S_{00} & 0\\
    0 & S_{21} & -S_{11}\\
    -S_{21} & 0 & S_{01}\\
    S_{11} & -S_{01} & 0\\
    0 & S_{22} & -S_{12}\\
    -S_{22} & 0 & S_{02}\\
    S_{12} & -S_{02} & 0
  \end{pmatrix}
\end{equation}
and:
\begin{equation}
  G^+ =
  \begin{pmatrix}
    1 & 0  & 0  & 0  & 0 & 0  & 0  & 0  & 0\\
    0 & 0  & 0  & 0  & 1 & 0  & 0  & 0  & 0\\
    0 & 0  & 0  & 0  & 0 & 0  & 0  & 0  & 1\\
    0 & 0  & 0  & 0  & 0 & \frac{1}{2(\lambda_2 - \lambda_1)} &
    0  & \frac{1}{2(\lambda_2 - \lambda_1)} & 0\\
    0 & 0  & \frac{1}{2(\lambda_0 - \lambda_2)} &
    0  & 0 & 0  & \frac{1}{2(\lambda_0 - \lambda_2)} & 0  & 0\\
    0 & \frac{1}{2(\lambda_1 - \lambda_0)} & 0 &
    \frac{1}{2(\lambda_1 - \lambda_0)} & 0 & 0  & 0  & 0  & 0
  \end{pmatrix}
\end{equation}
And at this point we just follow the paper (the new one, not the old one):
\begin{align}
  F^{-1} &= G^+ (S \otimes S) D^+\\
  K &=
  \begin{pmatrix}
    0_{9\times3} & S_p\\
    I_{3\times3} & 0_{3\times3}
  \end{pmatrix}
  F^{-1}
\end{align}
and eventually:
\begin{equation}
  \Sigma_{vec(S), \lambda} = K \Sigma_{v_d(\itm)} K^T
\end{equation}

\subsection{Simplified error analysis}

The main purpose in this section is to recover the expression for the
covariance matrix in the $(\xdir, \ydir, \zdir)$ representation when error
estimates on $\theta$ and $\phi$ are available---either on an event-by-event
basis or through some phenomenological parametrization. We will assume for
simplicity that the errors on $\theta$ and $\phi$ are uncorrelated.

The basic transformation equations are:
\begin{align}
  \xdir & = \sin\theta \cos\phi\\
  \ydir & = \sin\theta \sin\phi\\
  \zdir & = \cos\theta
\end{align}
and the elements of the covariance matrix, as a function of $\theta$
and $\phi$, read:
\begin{equation}
  \Sigma_{ij} = E[C_i C_j]
\end{equation}
where, denoting the central values with the subscript zero:
\begin{align}
  C_x & = (\sin\theta \cos\phi - \sin\theta_0 \cos\phi_0)\\
  C_y & = (\sin\theta \sin\phi - \sin\theta_0 \sin\phi_0)\\
  C_z & = (\cos\theta - \cos\theta_0)
\end{align}
We will take advantage of the fact that in the series expansion of the
expectation values:
\begin{align}
  E[f(\theta, \phi)] & \approx f(\theta_0, \phi_0) +
  \left[\pder{f(\theta, \phi)}{\theta}\right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!E[(\theta - \theta_0)] +
  \left[\pder{f(\theta, \phi)}{\phi}\right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!E[(\phi - \phi_0)] +\\\nonumber
  & \frac{1}{2}\left[\pdersec{f(\theta, \phi)}{\theta}{\theta}
  \right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!E[(\theta - \theta_0)^2] +
  \frac{1}{2}\left[\pdersec{f(\theta, \phi)}{\phi}{\phi}
  \right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!E[(\phi - \phi_0)^2] +\\\nonumber
  & \left[\pdersec{f(\theta, \phi)}{\theta}{\phi}\right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!E[(\theta - \theta_0)(\phi - \phi_0)]
\end{align}
the first three terms vanish for obvious reasons, and so does the last,
if the errors on $\theta$ and $\phi$ are not correlated. Therefore:
\begin{align}
  E[f(\theta, \phi)] \approx
  \frac{1}{2}\left[\pdersec{f(\theta, \phi)}{\theta}{\theta}
  \right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!\sigma^2_\theta +
  \frac{1}{2}\left[\pdersec{f(\theta, \phi)}{\phi}{\phi}
    \right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\!\!\!\sigma^2_\phi
\end{align}
In the following we will need:
\begin{align}
  \nonumber
  \left[\pdersec{C_i(\theta, \phi)C_j(\theta, \phi)}{\theta}{\theta}
    \right]_{\theta_0, \phi_0}
  \quad {\rm and}\quad
  \left[\pdersec{C_i(\theta, \phi)C_j(\theta, \phi)}{\phi}{\phi}
    \right]_{\theta_0, \phi_0}
\end{align}
The basic blocks are:
\begin{align}
  \nonumber
  \pder{C_x}{\theta} &= \cos\theta\cos\phi ,\quad
  \pder{C_x}{\phi} = -\sin\theta\sin\phi\\\nonumber
  \pder{C_y}{\theta} &= \cos\theta\sin\phi ,\quad
  \pder{C_y}{\phi} = \sin\theta\cos\phi\\\nonumber
  \pder{C_z}{\theta} &= -\sin\theta ,\quad
  \pder{C_z}{\phi} = 0\\\nonumber
  \pdersec{C_x}{\theta}{\theta} &= -\sin\theta\cos\phi ,\quad
  \pdersec{C_x}{\phi}{\phi} = -\sin\theta\cos\phi\\\nonumber
  \pdersec{C_y}{\theta}{\theta} &= -\sin\theta\sin\phi ,\quad
  \pdersec{C_y}{\phi}{\phi} = -\sin\theta\sin\phi\\\nonumber
  \pdersec{C_z}{\theta}{\theta} &= -\cos\theta ,\quad
  \pdersec{C_z}{\phi}{\phi} = 0\\\nonumber
\end{align}
And the actual ingredients for the covariance matrix can be easily
calculated remembering that all the terms containing a $C_i$ multiplicative
factor are zero when calculated in $(\theta_0, \phi_0)$:
\begin{align}
  \left[\pdersec{C^2_x}{\theta}{\theta}\right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\! =
  \left[\pder{}{\theta}\left(2C_x\pder{C_x}{\theta}\right)
    \right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\! =
  \left[2\left(\pder{C_x}{\theta}\right)^2 +
    2C_x\pdersec{C_x}{\theta}{\theta}\right]_{\theta_0, \phi_0}
  \!\!\!\!\!\!\!\! = 2\cos^2\theta_0\cos^2\phi_0
\end{align}
So, along the same lines (and defining for compactness
$s_{\theta_0} = \sin\theta_0$, $c_{\theta_0} = \cos\theta_0$,
$s_{\phi_0} = \sin\phi_0$, $c_{\phi_0} = \cos\phi_0$):
\begin{equation}
  %m[0, 0] = ct2*cp2*dt2 + st2*sp2*dp2
  %m[1, 1] = ct2*sp2*dt2 + st2*cp2*dp2
  %m[2, 2] = st2*dt2
  %m[0, 1] = ct2*cp*sp*dt2 - st2*cp*sp*dp2
  %m[0, 2] = -st*ct*cp*dt2
  %m[1, 2] = -st*ct*sp*dt2
  %m[1, 0] = m[0, 1]
  %m[2, 0] = m[0, 2]
  %m[2, 1] = m[1, 2]
  \Sigma =
  \begin{pmatrix}
    c_{\theta_0}^2c_{\phi_0}^2\sigma^2_\theta +
    s_{\theta_0}^2s_{\phi_0}^2\sigma^2_\phi
    & c_{\theta}^2 c_{\phi}s_{\phi}\sigma^2_\theta -
    s_{\theta}^2c_{\phi}s_{\phi}\sigma^2_\phi
    & -s_{\theta}c_{\theta}c_{\phi} \sigma^2_\theta\\
    c_{\theta}^2 c_{\phi}s_{\phi}\sigma^2_\theta -
    s_{\theta}^2c_{\phi}s_{\phi}\sigma^2_\phi
    & c_{\theta_0}^2s_{\phi_0}^2\sigma^2_\theta +
    s_{\theta_0}^2c_{\phi_0}^2\sigma^2_\phi
    & -s_{\theta}c_{\theta}s_{\phi} \sigma^2_\theta\\
    -s_{\theta}c_{\theta}c_{\phi} \sigma^2_\theta
    & -s_{\theta}c_{\theta}s_{\phi} \sigma^2_\theta
    & s_{\theta_0}^2\sigma^2_\theta\\
  \end{pmatrix}
\end{equation}

\section{Shower development: basic formul\ae}

The longitudinal profile of an electromagnetic shower is described by:
\begin{equation}
\firstder{E}{t} = E_0 p(t) = E_0 k t^\alpha e^{-bt}
\end{equation}
where
$$
k = \frac{b^{\alpha + 1}}{\Gamma(\alpha + 1)}
$$
(with this definition $p(t)$ is normalized to 1 and is therefore a probability
density) and the Euler $\Gamma$ function, defined by:
$$
\Gamma(\alpha) = \int_{0}^\infty t^{\alpha - 1} e^{-t} \diff t
$$
satisfies the well know relation:
$$
\Gamma(\alpha + 1) = \alpha \Gamma(\alpha)
$$
The position of the shower maximum is given by the condition:
$$
\left.\firstder{p}{t}\right|_{\tmax} =
k \tmax^{\alpha - 1} e^{-b\tmax} (\alpha  - b\tmax) = 0
$$
and therefore:
\begin{equation}
\tmax = \frac{\alpha}{b}
\end{equation}
The other two pieces of necessary information are the dependences of $\alpha$
and $b$ on the energy. These are given by the relations:
\begin{equation}
b \approx 0.5
\end{equation}
and:
\begin{equation}
\tmax = \frac{\alpha}{b} = \ln\left(\frac{E_0}{E_c}\right) + C
\end{equation}
where $C=0.5$ for photons and $C=-0.5$ for electrons and $E_c$ is the critical
energy for the material.

\section{Longitudinal moments}

Let's start from the calculation of the lowest order moments of the shower
longitudinal profile around $t=0$. The first one is the mean:
\begin{align}
\left< t \right> &= \mu = \int_{0}^\infty t p(t) ] \diff t =
k \int_{0}^\infty t^{\alpha + 1} e^{-bt} \diff t =\nonumber\\
&= \frac{b^{\alpha + 1}}{\Gamma(\alpha + 1)}
\frac{\Gamma(\alpha + 2)}{b^{\alpha + 2}} = \frac{(\alpha + 1)}{b}
\end{align}
(i.e. the mean of the profile is exactly $1/b$ radiation lengths to the
right of the shower maximum). Along the same lines:
\begin{align}
\left< t^2 \right> = \frac{b^{\alpha + 1}}{\Gamma(\alpha + 1)}
\frac{\Gamma(\alpha + 3)}{b^{\alpha + 3}} =
\frac{(\alpha + 2)(\alpha + 1)}{b^2}
\end{align}
and:
\begin{align}
\left< t^3 \right> = \frac{b^{\alpha + 1}}{\Gamma(\alpha + 1)}
\frac{\Gamma(\alpha + 4)}{b^{\alpha + 4}} =
\frac{(\alpha + 3)(\alpha + 2)(\alpha + 1)}{b^3}
\end{align}

We can apply the usual formul\ae\ for the moments $M_n$ centered around the
mean (as opposed to the ones centered around 0):
\begin{equation}
M_2 = \sigma^2 = \left< t^2 \right> - \mu^2 =
\frac{(\alpha + 1)}{b^2}
\end{equation}
and
\begin{equation}
M_3 = \left< t^3 \right> - 3\mu\sigma^2 - \mu^3 = \frac{2(\alpha + 1)}{b^3}
\end{equation}
The skewness $\gamma$ is given by:
\begin{equation}
\gamma = \frac{M_3}{\sigma^3} = \frac{2}{\sqrt{\alpha + 1}}
\end{equation}

Let's look at the problem from a different perspective, which will hopefully
turn out to be handy in the following. Integrating by parts, we get:
\begin{align*}
\left< t^n \right> & = k \int_{0}^\infty t^n \cdot t^\alpha e^{-bt} \diff t =
k \int_{0}^\infty t^\alpha e^{-bt} \diff\left(\frac{t^{n+1}}{n+1}\right) =\\
&= k\left.\frac{t^{n+1}}{n+1}  t^\alpha e^{-bt}\right|_0^\infty -
k \int_0^\infty \frac{t^{n+1}}{n+1} \left(
\alpha t^{\alpha - 1}e^{-bt} - bt^\alpha e^{-bt}
\right) \diff t =\\
&= \frac{kb}{n+1} \int_{0}^\infty  t^{\alpha + n + 1} e^{-bt} \diff t -
\frac{k\alpha}{n+1} \int_{0}^\infty  t^{\alpha + n} e^{-bt} \diff t =
\frac{b \left< t^{n+1} \right> - \alpha\left< t^n \right>}{n+1}
\end{align*}
from which it follows that:
\begin{equation}
\left< t^{n+1} \right> = \frac{(\alpha + n + 1)}{b}\left< t^n \right>
\end{equation}
For $n = 1$ we get:
$$
\left< t^2 \right> = \frac{(\alpha + 2)}{b}\left< t \right>
$$
or:
\begin{equation}
\sigma^2 = \frac{(\alpha + 2)}{b}\mu - \mu^2
\end{equation}
Whereas for $n = 2$:
$$
\left< t^3 \right> = \frac{(\alpha + 3)}{b}\left< t^2 \right>
$$
which translates into:
\begin{equation}
\gamma = \frac{\mu}{\sigma^3}\left[
  \frac{(\alpha + 3)(\alpha + 2)}{b^2} - 3\sigma^2 - \mu^2
  \right]
\end{equation}

All this equations can be directly verified by plugging in the expressions
for $\mu$, $\sigma$ and $\gamma$ explicitly obtained before, but the hope is
to generalize them to the case in which we don't sample the entire shower
(see the following section).

\section{Longitudinal moments over a finite interval}

We can generalize the previous relations to the case in which we only
sample a finite fraction of the longitudinal shower development,
say between $t_1$ and $t_2$. The formalism is essentially identical, except
for the fact that now we're dealing with a probability density function
over a finite interval:
$$
p_{\rm f}(t) = k_{\rm f} t^\alpha e^{-bt}
$$
with $k_{\rm f}$ being:
$$
k_{\rm f} = \frac{1}{\int_{t_1}^{t_2} t^\alpha e^{-bt} \diff t}
$$
(physically $k_{\rm f}$ is the ratio between the raw energy deposited in the
calorimeter and the true energy of the particle). So now we have:
\begin{equation}
\left< t^{n+1} \right> = \frac{(\alpha + n + 1)}{b}\left< t^n \right> -
\left.\frac{k_{\rm f}}{b} t^{(\alpha + n + 1)} e^{-bt}\right|_{t_1}^{t_2}
\end{equation}
and therefore:
\begin{equation}
\left< t^2 \right> = \frac{(\alpha + 2)}{b}\left< t \right> -
\frac{k_{\rm f}}{b}
\left[t_2^{(\alpha + 2)} e^{-bt_2} - t_1^{(\alpha + 2)} e^{-bt_1}\right]
\end{equation}
and:
\begin{equation}
\left< t^3 \right> = \frac{(\alpha + 3)}{b}\left< t^2 \right> -
\frac{k_{\rm f}}{b}
\left[t_2^{(\alpha + 3)} e^{-bt_2} - t_1^{(\alpha + 3)} e^{-bt_1}\right]
\end{equation}

Some more formula that might turn out to be useful for the normalization
of the skewness to the expected value for electronmagnetic showers.
The moments of the longitudinal distribution can be written as a function
of the incomplete gamma function, defined as:
\begin{equation}
\gamma(\alpha, t) = \frac{1}{\Gamma(\alpha)}
\int_{0}^{t} t^{\alpha - 1} e^{-t} \diff t
\end{equation}
from which it follows that:
\begin{equation}
\int_{t_1}^{t_2} t^{\alpha} e^{-bt} \diff t =
\frac{\Gamma(\alpha+1)}{b^{\alpha+1}}
\left( \gamma(\alpha+1, bt_2) - \gamma(\alpha+1, bt_1) \right)
\end{equation}
If we define:
$$
{\mathcal G}(\alpha, b, t_1, t_2) = \frac{\Gamma(\alpha)}{b^{\alpha}}
\left( \gamma(\alpha, bt_2) - \gamma(\alpha, bt_1) \right)
$$
we have:
\begin{align}
\left< t^n \right> =
\frac{{\mathcal G}(\alpha + n + 1, b, t_1, t_2)}
     {{\mathcal G}(\alpha + 1, b, t_1, t_2)}
\end{align}

\clearpage
\appendix

\emph{Caution: the stuff in the appendix is mostly crap, at this time.
I'll move it into appropriate sections as soon as it's in a reasonable shape
(and, of course, this does not mean that people should not take a look).}

Let's go back to the basic equation for the principal eigenvector:
$$
\itm\mathbf{e}^1 = \lambda_1\mathbf{e}^1
$$
Doing a full error propagation is not easy, since in this equation we do have
error on the six independent components of the inertia tensor, as well as on
the eigenvalue $\lambda_1$ we've just calculated. The errors on the
$\itc{ij}$ are reasonably easy to calculate, starting from the errors
associated with the finite dimensions of the crystals. On the other side
the propagation of the errors to $\lambda_1$ is not trivial, as the
expression is complicated. On top of that, these different error are not
indipendent from each other, as $\lambda_1$ is calculated starting from the
component of the inertia tensor.

The solution to this equation is:
\begin{align}
e^1_x &= \frac{1}{\sqrt{1 + \frac{A^2}{B^2} + \frac{A^2}{C^2}}}\\
e^1_y &= \frac{1}{\sqrt{1 + \frac{B^2}{A^2} + \frac{B^2}{C^2}}}\\
e^1_z &= \frac{1}{\sqrt{1 + \frac{C^2}{A^2} + \frac{C^2}{B^2}}}
\end{align}
where:
\begin{align}
A &= \itc{yz}(\itc{xx} - \lambda_1) - \itc{xy}\itc{xz}\\
B &= \itc{xz}(\itc{yy} - \lambda_1) - \itc{xy}\itc{yz}\\
C &= \itc{xy}(\itc{zz} - \lambda_1) - \itc{xz}\itc{yz}
\end{align}

\begin{thebibliography}{100}
\bibitem{goldstein}H.~Goldstein, \emph{Classical mechanics}.
\bibitem{landau}L.~D.~Landau, E.~M.~Lif\^sic, \emph{Mechanics}.
\bibitem{wolfram}\url{http://mathworld.wolfram.com/CubicFormula.html}
\bibitem{pdg}PDG Review of Particle Physics,
  \emph{Physics Letters B.} (2004) {\bf 592}
\bibitem{errors}T.~Soler and B.~H.~W.~van~Gelder,
  \emph{Geophys. J. Int.} (1991) {\bf 105}, 537--546.
\bibitem{errors_corr}T.~Soler and B.~H.~W.~van~Gelder,
  \emph{Geophys. J. Int.} (2006) {\bf 165}, 382.
\end{thebibliography}

\end{document}


\section{Adaptive gradient clipping in \tuner}
\label{sec:adapt_clip}
\begin{figure}
%	%\begin{minipage}{0.61\textwidth}
\centering
%% \vspace{-2.25em}
  \includegraphics[width=0.7\linewidth]{experiment_results/clipping_example.pdf}
% \vspace{-0.75em}
\caption{A variation of the LSTM architecture in \citep{zhu2016trained} exhibits exploding gradients.
The proposed adaptive gradient clipping threshold (blue) stabilizes the training loss.}
\label{fig:stability}
%\end{minipage}
\end{figure}

Gradient clipping has been established in literature as a standard---almost necessary---tool for training such objectives \citep{pascanu2013difficulty,Goodfellow-et-al-2016,gehring2017convolutional}.
However, the classic tradeoff between adaptivity and stability applies:
setting a clipping threshold that is too low can hurt performance;
setting it to be high, can compromise stability.
\tuner, keeps running estimates of extremal gradient magnitude squares, $h_{max}$ and $h_{min}$ in order to estimate a generalized condition number.
We posit that $\sqrt{h_{max}}$ is an ideal gradient norm threshold for adaptive clipping.
In order to ensure robustness to extreme gradient spikes, like the ones in Figure~\ref{fig:stability}, we also limit the growth rate of the envelope $h_{max}$ in Algorithm~\ref{alg:curv_func} as follows:
\begin{equation}
 h_{max}
 \leftarrow
 \beta \cdot h_{max}
 	+ (1-\beta) \cdot \textrm{min}\left\{
 		h_{max,t}, 100 \cdot h_{max}
 	\right\}
\end{equation}
Our heuristics follows along the lines of classic recipes like~\cite{pascanu2013difficulty}. However, instead of using the average gradient norm to clip, it uses a running estimate of the maximum norm $h_{\max}$.

In Section~\ref{sec:stability}, we saw that adaptive clipping stabilizes the training on objectives that exhibit exploding gradients. In Figure~\ref{fig:infl_clip}, we demonstrate that the adaptive clipping does not hurt performance on models that do not exhibit instabilities without clipping. Specifically, for both PTB LSTM and CIFAR10 ResNet, the difference between \tuner with and without adaptive clipping diminishes quickly.
\label{sec:infl_clip}
\begin{figure}
\centering
\begin{tabular}{c c}
	\includegraphics[width=0.35\linewidth]{experiment_results/ptb/clip_cmp.pdf} &
	\includegraphics[width=0.35\linewidth]{experiment_results/resnet/cifar10_clip_cmp.pdf}
\end{tabular}
\caption{Training losses on PTB LSTM (left) and CIFAR10 ResNet (right) for YellowFin with and without adaptive clipping.}
\label{fig:infl_clip}
\end{figure}


%! Author = tstreule

\section{MRI}

\subsection{Quantum basics}
\textbf{Spin} $S_z = \hbar m \quad m = -I, -I+1, ... I \quad |S| = \hbar \sqrt{I(I+1)}$\\
\textbf{Magnetic moment}: \fbox{$\vec{\mu} = \gamma \vec{S}$} ($\gamma$: gyromagnetic ratio)\\
\textbf{Energy content} in $\vec{B}$-Filed: \fbox{$E_m = -\vec{\mu} \cdot \vec{B} = \pm \frac{\hbar}{2} \gamma B_0$}\\
\textbf{Blotzmann stat.}: $\frac{n\ped{-1/2}}{n\ped{+1/2}} = \exp(-\frac{\Delta E_m}{k\ped{B} T})$ \quad $\frac{\Delta n}{n} \propto \frac{\gamma \hbar B_0}{2 k\ped{B} T}$\\
\textbf{Larmor frequency}: E gap $\leftrightarrow$ photons: \highlight{$\displaystyle \omega_L = \gamma B_0$}\\
\textbf{Macrosc. mag. dipolar moment}: \fbox{$\vec{M}_0 = \sum \vec{\mu} = \Delta n \mu_z$}

\textbf{Bloch equation} ($T_2 << T_1$)\\
\highlight{$\displaystyle \frac{d}{dt}\vec{M} = \left( \begin{matrix}
                                        -1/T_2 & -\gamma B_z & \gamma B_y \\
                                        \gamma B_z & -1/T_2 & -\gamma B_x \\
                                        -\gamma B_y & \gamma B_x & -1/T_1\end{matrix} \right) \vec{M} + \left( \begin{matrix}
                                                                                0 \\ 0 \\ M_0 / T_1 \end{matrix} \right)$}\\
{\small $M_z(t) = M_0\cos\alpha + (M_0-M_0\cos\alpha) (1-\exp(-t/T_1))$, \hfill $\alpha = \gamma B_1\tau\ped{B1}$: tip angle}

$\vec{B}$ with $|\vec{B}| = B_1 = const$ and $B_z = B_0$ spinning with $\omega_{RF}$ around the z-Axis. $\omega_{RF} = \omega_L \iff \vec{M}$ spins $\downarrow, \uparrow, \downarrow$ etc.

\textbf{Rot. frame of reference}: $\vec{B}$ stays at a slight angle and $\vec{M}$ rotates around it. Correction: $B_z = B_0 - \omega_{RF} / \gamma$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Basic setup}
\begin{minipage}{\linewidth}
    \begin{enumerate}
        \item Have $B_z$ + rot. field to push spins in x-y-plane.
        \item Coils to make gradients: Maxwell (z), and two Golay pairs
        \item Changing the gradient $\to$ travel k-space and sample it
        \item IFFT to get the image weighted with proton density
    \end{enumerate}
\end{minipage}

Signal from entire obj: \qquad $S(t) = \eu^{\iu \omega_0 t} \int\ped{obj} \rho(\vec{r})\eu^{\iu \vec{k} (t) \cdot \vec{r}} \diff^3 \vec{r}$\\
Fourier transform of $\rho(\vec{r})$: \quad $S(\vec{k}) = \eu^{-\iu \omega_0 t} \int\ped{obj} \rho(\vec{r})\eu^{-\iu \vec{k} \cdot \vec{r}} \diff^3 \vec{r}$

\textbf{Slice sel.}: Grad. in z-dir. and flip spins with a sinc$\times$gauss-pulse ($\approx \laplace$ rect, range of freq. where $\Delta B_z = 0$).

Grad. $\to$ \textbf{dephasing}. $\implies$ invert G to \textbf{rephase} spins.

\subsubsection{Measuring the spin}
\fbox{$\hat{U}\ped{ind} = \iu\omega I_0^{-1} \hat{\vec{\mu}} \cdot \hat{\vec{B}}^t(\vec{r}) = M_{xy} V s(\vec{r})$}\\
\highlight{$\displaystyle s(\vec{r}) = \iu\omega I_0^{-1} (\hat{B}^t_x(\vec{r}) - \iu\hat{B}^t_y(\vec{r}))$} \highlight{$\displaystyle B_1^{(-)} = s/(\iu\omega)$}\\
$I_0$: Transmit current, $B^t$: Field received at $\vec{r}$ when transmitting, $s$: \textbf{Coil sensitivity}, $\hat{\vec{\mu}} = V(M_{xy}, -\iu M_{xy}, 0)^T$,

s of smaller coil $\uparrow$. \quad Large distance: s of bigger coil $\uparrow$

Signal $\propto \frac{dM}{dt} = \iu\omega_0 M_0 \eu^{\iu\omega_0 t} \highlight{\propto \gamma^3}$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Measurement procedures (All Gradient Echo)}
\textbf{Echo-planar imaging (EPI)}: \includegraphics[width = 0.03\linewidth]{MRI_EPI}, spiral, radial

\begin{minipage}{.7\linewidth}
    $\circlearrowright \theta$, $T_E$, sample, $T_R-T_E$, again\\
    $\highlight{I \propto \rho \frac{(1-\eu^{-T_R/T_1})\sin \theta}{1 - \eu^{-T_R/T_1}\cos\theta} \eu^{-T_E/T_2}}$\\
    $\alpha\ped{Ernst} = \arccos (\eu^{-T_R/T_1})$

    \textbf{Sat. method}: \fbox{$1-\exp(-t/T_1)$} $T_E \approx 0$, $T_R \downarrow$ $\implies$ $T_1$ weighted. \textbf{Inv. recovery} also $T_1$\\
\end{minipage}%
\begin{minipage}{0.3\linewidth}
    \raggedleft
    \includegraphics[width = .9\linewidth]{MRI_GradientEcho}
\end{minipage}\\
\textbf{Spin-echo method}: \fbox{$\exp(-t/T_2)$} $T_2^*$ decay (\textbf{const. loc. inhomog.}). $\circlearrowright$ at $T_E/2$ by 180°. $T_E \uparrow$ $\implies$ $T_2$ weight.

Saturation Method \quad $|$ Spin Echo Method\\
\includegraphics[width = 0.2\linewidth]{MRI_SatMethodShort}
\includegraphics[width = 0.2\linewidth]{MRI_SatMethodLong}
\includegraphics[width = 0.25\linewidth]{MRI_SpinEchoShort}
\includegraphics[width = 0.24\linewidth]{MRI_SpinEchoLong}

\textbf{Inflow contrast}: $T_1$ weight. (Blood with $\uparrow M_z$ during $T_R$)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Noise, SNR and Resolution}
$\sigma^2\ped{noise} = 4 k_B \cdot T \cdot R \cdot BW$ \hfill $T$: Temp., $R=U/I$, $BW$: Bandwidth

If frequency indep. and uniform $T$ ($I_0$ is transmit c.):\\
$\highlight{\sigma^2_{noise} = 4 k_B \cdot T \cdot BW \int \sigma(\vec{r}) \frac{\abs{\vec{E}(\vec{r})}^2}{I_0^2}dV}$

Otherwise: $\sigma^2\ped{noise} = 4 k_B \iint T(\vec{r}) \sigma(\omega, \vec{r}) \frac{\abs{\vec{E}(\omega, \vec{r})}^2}{I_0^2} \frac{d\omega}{2\pi} dV$

\fbox{$\sigma^2\ped{noise} = 4 k\ped{B} \cdot BW (R\ap{eff}\ped{sample} T\ped{sample} + R\ap{eff}\ped{coil} T\ped{coil} + R\ap{eff}\ped{env} T\ped{env})$} (last Term usually negligable)

\fbox{$\textbf{SNR} = \frac{\omega B_1^{(-)} (\vec{r}) M_{xy}(\vec{r}) \Delta V}{\sqrt{4 k_B BW (T_{sample}R_{sample} + T_{coil}R_{coil})}} \sqrt{N_{avg}}$}

$BW \propto \text{Gradient strength} \quad \propto \text{Body size} \quad \propto t\ped{acq}^{-1}$\\
$\quad t_{scan} \propto N_{avg} \quad \implies \highlight{SNR \propto \Delta V \sqrt{t}}$

SNR$\uparrow$: magnetization$\uparrow$ by $B_0\uparrow$. $\omega\uparrow$. $T\ped{coil}\downarrow$, $R\ped{coil}\downarrow$

\textbf{Resolution limits}:\\
In k-space, the image is multiplied with\\
$H(k) = \textrm{rect}\left( \frac{k}{2 k\ped{max}}\right) \eu^{-t/T_2^*} \quad \implies \textbf{PSF}:\: \highlight{\Delta x \geq \frac{\pi}{\gamma G\ped{max} T_2^*}}$\\

Diffusion Area: $\left< \Delta x^2 \right> = 6 \cdot D \cdot T\ped{acq}$ \hfill $D = \unitfrac[10^{-3}]{mm^2}{s}$ for $\ce{H_2O}$.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Various}
\textbf{T/R Switch}: Block kW in transmit mode, nW in r mode.

\textbf{RF Body Coil - Gradients - $B_0$-Magnet (3-7T) - Shield Coils - Cryogenics} @4K.

\fbox{$\unit[1]{Gauss} = \unit[0.1]{mT}$} \textbf{Limits}: 5 Gauss: Pacemakers, Credit cards. --- 50 Gauss magnetic objects

\subsubsection{fMRI \textnormal{-- functional MRI (brain imaging)}}
Oxygenated hemoglobin is diamagnetic, deoxy-Hb is paramagnetic. Param. disturbs the B-field $\implies$ reduces $T_2^*$. $S_{task} > S_{idle}$. Use echo planar imaging because it is fast.\\
Calc \textbf{scalar product} of activation and \textbf{paradigm} ($\pm 1$)


% !TEX options=--shell-escape
\documentclass [12pt]{article}
\usepackage {amsmath}
\usepackage {amsthm}
\usepackage {amssymb}
\usepackage {graphicx}
\usepackage {float}
\usepackage {multirow}
\usepackage {xcolor}
\usepackage {algorithmic}
\usepackage [ruled,vlined,commentsnumbered,titlenotnumbered]{algorithm2e} \usepackage {array}
\usepackage {booktabs}
\usepackage {url}
\usepackage {parskip}
\usepackage [margin=1in]{geometry}
\usepackage [T1]{fontenc}
\usepackage {cmbright}
\usepackage [many]{tcolorbox}
\usepackage [colorlinks = true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage {enumitem}
\usepackage {xparse}
\usepackage {verbatim}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage[cache=false]{minted}
\usepackage{mdframed}
\usepackage{tikz}
\usetikzlibrary{shapes.symbols}
\newtheorem{theorem}{Theorem}

\DeclareTColorBox {Solution}{}{breakable, title={Solution}}
\DeclareTColorBox {Solution*}{}{breakable, title={Solution (provided)}}
\DeclareTColorBox {Instruction}{}{boxrule=0pt, boxsep=0pt, left=0.5em, right=0.5em, top=0.5em, bottom=0.5em, arc=0pt, toprule=1pt, bottomrule=1pt}
\DeclareDocumentCommand {\Expecting }{+m}{\textbf {[We are expecting:} #1\textbf {]}}
\DeclareDocumentCommand {\Points }{m}{\textbf {(#1 pt.)}}
\newcommand {\hint }[1]{\noindent {[\textbf {HINT:} \em #1 \em ]}} \newcommand {\pts }[1]{\textbf {(#1 pt.)}}

\begin{document}

{\LARGE \textbf {COMP 285 (NC A\&T, Spr `22)}\hfill \textbf {Weekly Quiz 3} }

\begin{Instruction}

\paragraph{Reporting Issues} If you find any issues with the solutions, reach out to Chi Wang (author) or Luis Perez (reviewer).

\end{Instruction}

\section{} Assume we modify the k-select algorithm that we saw in previous lectures; instead of picking the pivot cleverly, we just pick a uniformly random element as the pivot each time. We call the resulting algorithm QuickSelect. What is the worst case runtime of QuickSelect?

\begin{Solution}
$$\Theta(n^2)$$
If we get unlucky, it’s just as bad as the worst-case pivot when we do k-select.
\end{Solution}

\section{} Recall our median-of-medians implementation of the Select-K algorithm (where we deterministically select a value close to the median). What is the correct recurrence relation for this algorithm?

\begin{Solution}
$$T(n) = T\left(\frac{n}{5}\right) + T\left(\frac{7n}{10}\right) + O(n)$$

Recall Lecture 8. The $T\left(\frac{n}{5}\right)$ comes from recursively finding the median of the medians array. The $T\left(\frac{7n}{10}\right)$ comes from an upper bound, where we know that the median-of-medians will make the array smaller by at least $30\%$. Finally, the $O(n)$ is the extra work needed to partition the input into left/right.
\end{Solution}

\section{} In the median-of-medians algorithm, we split our input array into chunks of at most size 5, took the median of each chunk, and then took the median of the medians. We argued that 3 out of 5 of the values in the bottom half of the chunks had values smaller than the median of medians. So at least $(3/5)*(1/2) = 3/10$ of the values in our array were smaller. This gave us the recurrence relation below. $T(n) = T\left(\frac{n}{5}\right) + T\left(\frac{7n}{10}\right) + O(n)$
What is the recurrence relation if instead we split the input array into chunks of size 7.

\begin{Solution}
$$T(n) = T\left(\frac{n}{7}\right) + T\left(\frac{5n}{7}\right) + O(n)$$

If we split the input array into chunks of size 7, we have the following:

$$
L \leq \frac{5n}{7} + 7, R \leq \frac{5n}{7} + 7
$$
Similarly with the size 5 chunks solution, we got $T(n) = T\left(\frac{n}{7}\right) + T\left(\frac{5n}{7}\right) + O(n)$.

The argument is that 4 out of the 7 values in the bottom half of the chunks are smaller than the median of medians. So at least $(4/7) * (1/2) = 2/7$ of the values in the array were smaller.
\end{Solution}

\section{} In the median-of-medians algorithm, we split our input array into chunks of at most size 5, took the median of each chunk, and then took the median of the medians. We argued that 3 out of 5 of the values in the bottom half of the chunks had values smaller than the median of medians. So at least (3/5)*(1/2) = 3/10 of the values in our array were smaller. This gave us the recurrence relation below. $T(n) = T\left(\frac{n}{7}\right) + T\left(\frac{5n}{7}\right) + O(n)$
What is the recurrence relation if instead we split the input array into chunks of size 3.

\begin{Solution}
$$T(n) = T\left(\frac{n}{3}\right) + T\left(\frac{2n}{3}\right) + O(n)$$

If we split the input array into chunks of size 3, we have the following:

$$
L <= \frac{2n}{3} + 3, R <= \frac{2n}{3} + 3
$$
Similarly with the size 5 chunks solution, we got $T(n) = T\left(\frac{n}{3}\right) + T\left(\frac{2n}{3}\right) + O(n)$.
\end{Solution}

\section{} Recall from lecture that BogoSort is the algorithm where we randomly shuffle the array until its sorted. The worst-case running time and the expected running time of BogoSort are the same.

\begin{Solution}
No
\paragraph{}
The worst-case running time is infinite and the expected running time is $O(n*n!)$
\end{Solution}

\section{} What is the expected running time of QuickSort?

\begin{Solution}
$\Theta(n \log n)$ since in expectation, it is similar to MergeSort (divides the input into halfs).
\end{Solution}

\section{} What is the worst-case running time of QuickSort?
\begin{Solution}
$\Theta(n^2)$ since in the worst-case, our pivots are bad and our inputs are not split well.
\end{Solution}

\section{} There are algorithms that sort elements in time faster that $O(n \log n)$ by not relying on doing comparisons between pairs of elements.

\begin{Solution}
Yes
\paragraph{}
It is possible, for example Counting Sort and Radix Sort both of which use the fact that we're sorting integer/strings which can be ``hashed''.
\end{Solution}

\section{} In the implementation of QuickSort covered in lecture, all pairs of elements are always compared at least one.

\begin{Solution}
False
\paragraph{}
Each pair of elements gets compared either 0 or 1 times.
\end{Solution}

\section{} There is no comparison-based algorithm that has a deterministic or expected running time better than $O(n \log n).$

\begin{Solution}
Yes. Any algorithm that relies on comparting elements must ask at least $n \log n$ questions to correcting partition the space. This has been mathematically proven.
\end{Solution}

\end{document}


\section{Spherical geometry}
In this section, we look at spheres and how we can define distances, segments, intersections and areas on spheres. We then define the related concept of 3D angles and how we can compute a 3D version of the winding number based on spherical geometry primitives.

\input{3d/sph/sph-basics}
\input{3d/sph/sph-segs}
\input{3d/sph/sph-angles}


\documentclass{article}
\usepackage[margin=1.0in]{geometry}
\usepackage{amsmath,amsthm,amssymb, bbm, graphicx, hyperref, float}

\title{CTA200 Assignment 2}
\author{Michael Poon}
\date{Due: May 8 2020}

\begin{document}
\maketitle

\section{Mandelbrot Set}

In this question, we use \texttt{matplotlib, numpy} in Python. We use a double for-loop to iterate over every combination of 200 x and 200 y points, linearly spaced from -2 to 2. For each set of (x,y) points, we calculate $|z|^2$, iterate i 100 times, and if $z^2$ increases above 100, we deem it to diverge.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.6]{mandelbrot1.png}
    \caption{Mandelbrot set for $-2<x<2,-2<y<2$. Yellow indicates converging region, purple indicates diverging region.}
\end{figure}

Furthermore, we can color code when $|z|^2$ diverges over 100 which is seen in Figure 2.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.7]{mandelbrot2.png}
    \caption{Similar to Fig. 1, with colorbar representing iteration of divergence. }
\end{figure}

This question walks us through in developing a famous mathematical set called the Mandelbrot set, which entails highly complex fractal shapes. We can notice these fractal shapes begin to form along the border between the yellow and purple in Fig. 2. To further view the fractal shapes, we can zoom in on specific regions of interest which increases the local resolution to yield further complexity.

\section{SIR (Susceptible-Infected-Recovered) Model}

In this question, we use \texttt{matplotlib, scipy.integrate} in Python to solve the given system of differential equations. Specifically, we use the scipy.integrate subpackage \texttt{odeint} which solves the system of ODE's by integration to uncover how variables S (susceptible individuals), I (infected individuals) and R (recovered individuals) evolve over time. Using the SIR Model suggestions in the Compartmental Models in Epidemiology Wiki page, we choose 3 values of $\gamma$ and $\beta$ centred around $\gamma=0.035, \beta=0.4$.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.4]{SIR_grid.pdf}
    \caption{Grid of evolved equations for various $\beta,\gamma$ values.}
\end{figure}

We can introduce a new variable $\mu$ to represent the rate of mortality, subtracting $/mu I$ from the second ODE and equating it to $\frac{dD}{dt}$, as suggested in the SIRD section of the aforementioned Wiki page. This accounts for infected individuals who have died (since susceptible and recovered people cannot directly die from the modelled disease).

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.7]{SIRD.png}
    \caption{Similar to Fig 3., with $\mu=0.005$ and deaths starting at 0. This solves a set of 4 first order differential equations.}
\end{figure}

\end{document}


\chapter[$e^+ e^-$ annihilation]{$e^+ e^-$ annihilation}
\section{Cross section and Mean free path}
\subsection{Cross section per atom}
For the annihilation in fly of $e^+ e^-$ into two photons
the cross-section formula of Heitler is used \cite{heitler,egs4}:

\begin{eqnarray*}
\sigma(Z,E) & = & \frac{Z \pi r^2_0}{\gamma +1}
                  \left[\frac{\gamma^2 + 4 \gamma +1}{\gamma^2 -1}
                  \ln \left(\gamma +\sqrt{\gamma^2 -1} \right)  -\frac
                  {\gamma +3}{\sqrt{\gamma^2 -1}} \right]  \\
E      & = & \mbox{total energy of the incident positron} \\
\gamma & = & E/m_e c^2 \\
r_0    & = & \mbox{classical electron radius}
\end{eqnarray*}

\subsection{Mean free path}
\begin{itemize}
\item[*]
         In a simple material the number of atoms per volume is:
         \[n  = \frac{\mathcal{N}\rho}{A}\]
         where:
         \begin{eqnarray*}
          \mathcal{N} &  & \mbox{Avogadro's number} \\
          \rho        &  & \mbox{density of the medium} \\
          A           &  & \mbox{mass of mole}
         \end{eqnarray*}
\item[*]
         In a compound material the number of atoms of Element elm per volume is:
         \[n_{elm}  = \frac{\mathcal{N}\rho w_{elm}}{A_{elm}}\]
         where:
         \begin{eqnarray*}
          \mathcal{N} &  & \mbox{Avogadro's number} \\
          \rho        &  & \mbox{density of the medium} \\
          w_{elm}     &  & \mbox{proportion by mass of the Element elm}\\
          A_{elm}     &  & \mbox{mass of mole of the Element elm}
         \end{eqnarray*}
\item[*]
         The mean free path, $\lambda$, for a positron to be annihilated with
         an electron is given by
         \[
           \lambda(E_{\gamma}) \equiv \frac{1}{\Sigma (E_{\gamma})}
             = \frac{1}{\sum_{elm}{\lbrack n_{elm} \sigma(Z_{elm},E_{\gamma})\rbrack}}
         \]
         where $\sum_{elm}$ runs over all Elements the material is made of.
\end{itemize}

\section {Sampling the final state}
\subsection{kinematical limits}
\[e^+ \; e^- \to \gamma_a \; \gamma_b \]
\noindent
The incident $e^+$ has a kinetic energy $T$, a total energy: $E_e = T + mc^2$ \\
and a momentum: $Pc = \sqrt{T(T+2mc^2)}$ \\
The total available energy is: $E_{tot} = E_e + mc^2 = E_a + E_b$  \\
The momentum conservation is: $ \vec{P} = \vec{P}_{\gamma_a} + \vec{P}_{\gamma_b}$

\noindent
Lets define the ratio of energy transfered to one photon (says $\gamma_a$):
\[ \epsilon = \frac{E_a}{E_{tot}} \equiv \frac{E_a}{T+2mc^2} \]
\\
The energy transfered to $\gamma_a$ is maximum when $\gamma_a$ is emitted in the
direction of the incident $e^+$. Then $E_a \mathsf{max} = (E_{tot}+Pc)/2$
\\
The energy transfered to $\gamma_a$ is minimum when $\gamma_a$ is emitted in the opposite
direction of the incident $e^+$. Then $E_a \mathsf{min} = (E_{tot}-Pc)/2$

\noindent Therefore:
\begin {eqnarray*}
  \epsilon_{max} &=& \frac{E_a \mathsf{max}}{E_{tot}} =
  \frac{1}{2} \left\lbrack 1+ \sqrt{\frac{\gamma - 1}{\gamma+1}} \right\rbrack \\
  \epsilon_{min} &=& \frac{E_a \mathsf{min}}{E_{tot}} =
  \frac{1}{2} \left\lbrack 1- \sqrt{\frac{\gamma - 1}{\gamma+1}}
  \right\rbrack
\end {eqnarray*}
\\
where $\qquad \gamma = (T + mc^2)/mc^2$

\noindent
Therefore the range of $\epsilon$ is:
 $\quad \lbrack \epsilon_{min} \; ; \; \epsilon_{max} \rbrack $
 $\qquad ( \equiv \lbrack \epsilon_{min} \; ; \; 1-\epsilon_{min} \rbrack) $

\subsection{sample the gamma energy}
The differential cross-section of the two-photon
positron-electron annihilation can be written as
\cite{heitler,egs4}:
\[
   \frac{d \sigma (Z, \epsilon)} {d \epsilon} =
   \frac{Z \pi r_0^2}{\gamma - 1} \left( \frac{1}{\epsilon} \right)
   \left[
   1+\frac{2\gamma}{(\gamma+1)^2}-\epsilon-\frac{1}{(\gamma+1)^2}\frac{1}{\epsilon}
   \right]
\]
where
$Z$ is the atomic number of the material, $r_{0}$ the classical electron
radius, and $\epsilon \in [ \epsilon_{min} \; ; \; \epsilon_{max} ]$

\noindent
The differential
cross-section can be decomposed as:
\[
   \frac{d \sigma (Z, \epsilon)} {d \epsilon} =
   \frac{Z \pi r_0^2}{\gamma - 1}
   \alpha f(\epsilon) g(\epsilon)
\]
where:
\begin{eqnarray*}
\alpha      &=& \ln (\epsilon_{max}/\epsilon_{min}); \qquad
f(\epsilon)  = 1/(\alpha \epsilon) \\
g(\epsilon) &=& \left[
    1+\frac{2\gamma}{(\gamma+1)^2}-\epsilon-\frac{1}{(\gamma+1)^2}\frac{1}{\epsilon}
    \right]  \equiv
    1-\epsilon+\frac{2 \gamma \epsilon -1}{\epsilon (\gamma +1)^2}
\end{eqnarray*}
Given 2 random numbers $r, r' \in ]0,1[$,
the photon energies are sampled as follow:
\begin{enumerate}
\item
  sample $\epsilon$ from $f(\epsilon):
  \epsilon =\epsilon_{min} \left( \frac{\epsilon_{max}}{\epsilon_{min}} \right)^r$
\item
  test the rejection function: if $g(\epsilon) \geq r'$ accept
  $\epsilon$, otherwise return to step 1.
\end{enumerate}

\noindent
Then the photon energies are:
 $E_a = \epsilon E_{tot} \qquad E_b = (1-\epsilon) E_{tot}$

\subsection{compute the final kinematic}
Lets be $\theta$ the angle between the incident $e^+$ and $\gamma_a$. From the
energy-momentum conservation we have:
\[
\cos \theta = \frac{1}{Pc} \left[ T+mc^2 \frac{2\epsilon -1}{\epsilon} \right]
 = \frac{\epsilon(\gamma +1) - 1}{\epsilon \sqrt{\gamma^2 -1}}
\] \\
The azimuthal angle, $\phi$, is generated isotropically and
the momentum vector of the photons, $\vec{P_{\gamma_a}}$ and $\vec{P_{\gamma_b}}$
are computed from the energy-momentum conservation and
transformed into the {\tt World} coordinate system.

\subsection{annihilation at rest}
The method \textbf{AtRestDoIt} treates the special case when a positron comes
at rest before annihilating.
It generates two photons with energy $k=m$.
The angular distribution is isotropic.

\section{Restrictions}
\begin{enumerate}
\item
The annihilation processes producing one or three or more photons are
ignored, because these processes are negligible compared to the
annihilation into two photons\cite{egs4,messel};
\item
In calculating the process it is assumed that the atomic electron initially
is free and at rest. This is the usual assumption used in shower programs
\cite{egs4};
\end {enumerate}
\section{Status of this document}
 9.10.98  created by M.Maire.

\begin{thebibliography}{99}
\bibitem[Heit54]{heitler} W. Heitler.
   {\em The Quantum Theory of Radiation, Clarendon Press, Oxford} (1954)
\bibitem[Mess70]{messel} H. Messel and D. Crawford.
   {\em Electron-Photon shower distribution, Pergamon Press} (1970)
\bibitem[egs4]{egs4} R. Ford and W. Nelson.
   {\em SLAC-265, UC-32} (1985)
\end{thebibliography}


\section{Data Reduction Strategy}

\begin{slide}{Data Reduction: Strategy}

  Steps for reducing measured data to {\mue} and then to {\chik}:

\begin{cenpage}{125mm}
  \begin{enumerate}
  \item convert measured intensities to {\mue}.
  \item subtract a smooth pre-edge function, removing instrumental
    background and absorption from other edges.
  \item normalize {\mue} to go from 0 to 1.\\
    {\BlueEmph{ Analyze normalized {\mue}  as XANES.}}

  \item remove a smooth post-edge background function to approximate
    {\bkg} to isolate the XAFS {\chie}.
    \[ \chi(E) = \frac{\mu(E) -\mu_0(E)}{\Delta \mu_0(E_0)} \]

  \item identify the threshold energy ${E_0}$, and convert from
    $E$ to $k$ space: $k= \sqrt{{{2m(E-E_0)}\over{\hbar^2}}}$
  \item weight the XAFS {\chik} and Fourier transform from
    ${k}$ to ${R}$ space.
  \end{enumerate}

\end{cenpage}

\vmm\vmm
\onslide+<2->
  After we get the EXAFS $\chi(k)$, we can model this with {\feff}
  calculations.

\end{slide}

\section{XAS Viewer}

\begin{slide}{Data Processing with {\xasviewer}  }

  \begin{cenpage}{135mm}
  {\xasviewer} (part of Larch) provides a GUI for
  processing beamline data to $\chi(k)$.

  \vmm
  {\athena} and {\sixpack} too!

  \begin{columns}
    \begin{column}{45mm}

      \includegraphics[width=35mm]{figs/experiment/feo_mu}

      \vmm
      Collected Spectra $\mu(E)$

    \end{column}
    \begin{column}{chir45mm}
      \includegraphics[width=35mm]{figs/experiment/feo_chik}

      \vmm
      Isolated $\chi(k)$, $k$-weighted

    \end{column}
    \begin{column}{45mm}
      \includegraphics[width=35mm]{figs/experiment/feo_chir}

      \vmm
      $\chi(R)$, Fourier Transform of $\chi(k)$

    \end{column}
  \end{columns}

  \vmm \hrule

  \vmm\vmm

{\small{
  \begin{columns}[T]
    \begin{column}{65mm}
      \onslide+<2->

      Data Processing  features:
      \begin{itemize}
      \item  Data Visualization
      \item  Read in beamline data, merge data sets.
      \item  Pre-edge subtraction, Find $E_0$, Normalize Spectra
      \item  Background subtraction  to $\chi(k)$.
      \item  Fourier Transforms  $\chi(k) \rightarrow \chi(R)$.
      \item  Save/Read ``Projects'.
      \end{itemize}
    \vfill

  \end{column}

    \begin{column}{65mm}
   \onslide+<3->

   XANES Analysis features:
   \begin{itemize}
   \item  Pre-edge peak fitting.
   \item  Linear combination fits.
   \item Principal Component Analysis
   \end{itemize}

   Data Corrections:
   \begin{itemize}
   \item  Deglitching.
   \item  Energy Alignment.
   \item  Over-absorption Corrections.
   \item Smooth, Rebin, Deconvolutions.
   \end{itemize}

 \end{column}
\end{columns}

}}
\end{cenpage}

\end{slide}

\begin{slide}{ {\xasviewer} screenshots}

  \begin{cenpage}{145mm}

    \begin{columns}
    \begin{column}{75mm}
      \includegraphics[width=75mm]{figs/Screenshots/XASViewer_Main}

      Panels for Normalization, Pre-edge Peak Fitting, PCA, Linear
      Combination Fitting, EXAFS (Background Subtraction+Fourier
      Transform), and Feff Fitting.

      \vfill
    \end{column}
    \begin{column}{30mm}
      \includegraphics[width=30mm]{figs/Screenshots/XASViewer_MainPlot}

      \vmm
      \includegraphics[width=30mm]{figs/Screenshots/XASViewer_EXAFS_plot1}

      \vmm \vfill
    \end{column}

      \begin{column}{30mm}
        \includegraphics[width=30mm]{figs/Screenshots/XASViewer_plot_peakfit}

        \vmm
        \includegraphics[width=30mm]{figs/Screenshots/XASViewer_EXAFS_plot_chir}

        \vmm \vfill
    \end{column}
\end{columns}

% \begin{tabular}{ll}
%   \begin{minipage}{75mm}  \wpdf{72mm}{figs/Athena_Screenshot} \end{minipage} &
%   \begin{minipage}{40mm}
%     \vspace{-3mm}   \wpdf{37mm}{figs/athena_mue}

%     \vspace{-3mm}   \wpdf{37mm}{figs/athena_chik}

%     \vspace{-3mm} \wpdf{37mm}{figs/athena_chir}
%   \end{minipage} \\
% \end{tabular}

 \end{cenpage}
\end{slide}

\section{Data Reduction Steps}

\begin{slide}{Data Reduction: Converting Raw Data to  {\mue}  }

  \begin{cenpage}{145mm}
    Step 1:   Starting with measured intensities,  we construct {\mue}:

    \vmm\vmm

    {    \only<1> {
    {\Blue{Transmission XAFS:}}   $ I_t = I_0 e^{-\mu(E)t)}  $  \hspace{10mm}  $ \mu(E)t =  - \ln(I_t/I_o)  $

    \vmm
    \begin{columns}
      \begin{column}{40mm}
        \includegraphics[width=40mm]{figs/experiment/mu}

        \hspace{15mm}        $\mu = -ln(I_t/I_0)$
      \end{column}
      \begin{column}{40mm}
        \includegraphics[width=40mm]{figs/experiment/i1}

        \hspace{15mm}        $I_t$
      \end{column}
      \begin{column}{40mm}
        \includegraphics[width=40mm]{figs/experiment/i0}

        \hspace{15mm}        $I_0$
       \end{column}
     \end{columns}

     \vmm

  Transmission measurements are needed for high concentration elements, above
  ($\sim 5 {\rm wt \%}$).

   }}

 {\only<2> {

    {\Blue{Fluorescence XAFS:}}   $ \mu(E) \propto I_f / I_0 $

    \vmm
    \begin{columns}
      \begin{column}{40mm}
        \includegraphics[width=40mm]{figs/experiment/mu_fluor}

        \hspace{15mm}        $\mu = (I_f/I_0)$
      \end{column}
      \begin{column}{40mm}
        \includegraphics[width=40mm]{figs/experiment/if_multi}

        \hspace{15mm}        $I_f$
      \end{column}
      \begin{column}{40mm}
        \includegraphics[width=40mm]{figs/experiment/i0_fluor}

        \hspace{15mm}        $I_0$
       \end{column}
     \end{columns}

 \vmm\vmm

 here $I_f$ may be:

 \begin{itemize}
 \item  the intensity from a single fluorescence ion chamber (Lytle detector).
 \item  the {\RedEmph{sum}} of several signals from a multi-element solid-state detector.
 \item  other emission measurement (electron yield, visible light)
 \end{itemize}

 \vmm

 Fluorescence measurements are generally better for
 elements at concentrations below  ($\sim 5 {\rm wt  \%}$).

}}

  \vfill

\end{cenpage}
\end{slide}

\begin{slide}{Merging Data, Aligning Data, Deglitching }

Real Data is typically measured in several scans.  These scans can be
{\BlueEmph{merged}} together to give better statistics.

\vmm
Important issues when processing data

\begin{cenpage}{120mm}
\begin{itemize}
\item The monochromator energy calibration may drift with time.
  \begin{itemize}
    \item scans can be aligned so that $E_0$ is constant for all scans.
    \item a {\BlueEmph{Reference Channel}} -- often a metal foil -- can be
      measured {\RedEmph{during the scan}} and used for energy alignment.
    \item if the drift is bad enough (every scan, etc), consult the
      beamline scientist.
  \end{itemize}
\item At certain energies, $I_0$ jumps -- a {\BlueEmph{glitch}} -- that may
  not normalize out of {\mue}.  These points should be removed.
\item {\RedEmph{over-absorption}} for concentrated samples measured in
  fluorescence.
  \end{itemize}
\end{cenpage}

{\xasviewer}  can help with these. So can {\athena} and {\sixpack}.

\end{slide}

\begin{frame} \frametitle{Data Reduction: Pre-Edge Subtraction, Normalization}

  \begin{cenpage}{130mm}

    \begin{tabular}{ll}
      \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/mu_preedge}   \end{minipage}
      &
      \begin{minipage}{45mm}
        \setlength{\baselineskip}{10pt}
        \hspace{-3mm}{\Red{Pre-Edge Subtraction}}\vspace{0.5mm}

        We subtract away the background that fits the {\RedEmph{pre-edge}}
        region.  This gets rid of the absorption due to other absorption
        edges   (say, the  Fe ${\rm L_{\rm III}}$ edge).

     \end{minipage}\\
      \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/mu_norm}  \end{minipage}
      &
      \begin{minipage}{45mm} \setlength{\baselineskip}{10pt}
        \hspace{-3mm}{\Red{Normalization}}\vspace{0.5mm}

        We estimate the {\RedEmph{edge step}}, ${\Delta \mu_0(E_0)}$
        by extrapolating a simple curve the above {\mue} to the
        edge.
        \[ \mu_{\rm norm}(E) = \mu(E) /  {\Delta \mu_0(E_0)} \]

        \vmm

      \end{minipage}
    \end{tabular}
    \end{cenpage}
\end{frame}

\begin{frame}\frametitle{Data Reduction: Normalized XANES and ${E_0}$}

  \begin{cenpage}{130mm}

  \begin{tabular}{ll}
    \begin{minipage}{70mm}  \includegraphics[width=60mm]{figs/experiment/mu_norm_xanes}  \end{minipage}
    &
    \begin{minipage}{45mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{XANES}}\vspace{0.5mm}

      The XANES part of {\mue} shows a fairly rich spectral structure.
      We'll use $\mu_{\rm norm}(E)$  for XANES analysis.

    \end{minipage}\\
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/mu_xanes_deriv}  \end{minipage}
    &
    \begin{minipage}{45mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{Derivative}}\vspace{0.5mm}

      We can select ${E_0}$ roughly as the energy with the maximum
      derivative.  This is somewhat arbitrary, so we'll keep in mind that
      we may need to refine this value later on.
    \end{minipage}
  \end{tabular}
    \end{cenpage}
\end{frame}

\begin{frame}\frametitle{Data Reduction: Normalization with tabulated $\mu(E)$}

  \begin{cenpage}{130mm}

  \begin{tabular}{ll}
    \begin{minipage}{70mm}  \includegraphics[width=60mm]{figs/experiment/mu_mback}  \end{minipage}
    &
    \begin{minipage}{45mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{Using tabulated $\mu(E)$ }}\vspace{0.5mm}

      Tabulated values (Chanter, et al) can also be used to normalize the
      data.

      \vmm
      This can improve consistency, as the same functional fom is
      used for all spectra of an element and edge.

    \end{minipage}\\
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/mu_mback_poly}  \end{minipage}
    &
    \begin{minipage}{45mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{Polynomial vs tabulated values}}\vspace{0.5mm}

      The difference is typically at the margin, but consistency is good!

      \vmm
      This is based on the {\mback} procedure of T.C. Weng, et al.

    \end{minipage}
  \end{tabular}
    \end{cenpage}
\end{frame}

\begin{frame} \frametitle{Data Reduction: Post-Edge Background Subtraction}

    \begin{cenpage}{130mm}

  \begin{tabular}{ll}
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/bkg_full}  \end{minipage}
    &
    \begin{minipage}{50mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{Post-Edge Background}}\vspace{0.5mm}

      \[ \chi(E) = \frac{\mu(E) -\mu_0(E)}{\Delta \mu_0(E_0)} \]

      We don't have a measurement of {\bkg} (the absorption
      coefficient without neighboring atoms).

      \vspace{1mm}

    \end{minipage}\\
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/bkg_xanes}  \end{minipage}
    &
    \begin{minipage}{50mm} \setlength{\baselineskip}{10pt}

      We approximate {\bkg} by an adjustable, smooth
      function: a {\BlueEmph{spline}}.

      \vmm

      This can be dangerous -- a flexible enough
      spline could match {\mue} and remove all the EXAFS!
      We want a spline that will match the {\BlueEmph{low frequency}}
      components of {\mue}.

    \end{minipage}
  \end{tabular}
    \end{cenpage}

\end{frame}

\begin{frame} \frametitle{Data Reduction: {\chie} to {\chik}}

  \begin{cenpage}{130mm}

    \begin{tabular}{ll}
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/chie}  \end{minipage}
    &
    \begin{minipage}{45mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{$ \chi(E)$ }}\vmm

      \[ \chi(E) = \frac{\mu(E) -\mu_0(E)}{\Delta \mu_0(E_0)} \]

      where $ \Delta \mu_0(E_0) $ is the Edge Step found earlier.

      \vfill
    \end{minipage}\\
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/chik}  \end{minipage}
    &
    \begin{minipage}{45mm} \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{$\chi(k)$}}       \vmm

      Convert scale from Energy, $E$, to wavenumber $k$:

       \[ k= \sqrt{{{2m(E-E_0)}\over{\hbar^2}}} \]

      \vfill
    \end{minipage}
  \end{tabular}
\end{cenpage}

\end{frame}

\begin{frame} \frametitle{Data Reduction: {\chik}, ${k}$-weighting}

  \begin{cenpage}{130mm}
  \begin{tabular}{ll}
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/chikw}  \end{minipage}
    &
    \begin{minipage}{45mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{\chik}}\vmm

      The raw EXAFS {\chik} decays quickly with $k$.

      \vmm

      We emphasize the higher-$k$ portion of the spectra by
      multiplying by  ${k^2}$ or ${k^3}$.
      \vfill
    \end{minipage}\\
    \begin{minipage}{70mm}
      \includegraphics[width=60mm]{figs/experiment/chikw_win}  \end{minipage}
    &
    \begin{minipage}{45mm} \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{$k$-weighted $\chi(k)$:
          ${k^2\chi(k)}$}}\vspace{0.5mm}

      {\chik} is composed of sine waves, so we'll Fourier Transform from
      $k$ to $R$-space.

      \vmm To avoid ``ringing'', we'll multiply by a
      {\BlueEmph{window function}}.  \vfill
    \end{minipage}
  \end{tabular}
\end{cenpage}

\end{frame}

\begin{frame} \frametitle{EXAFS Fourier Transform: ${\chi(R)}$}

  \begin{cenpage}{130mm}
    \begin{tabular}{ll}
    \begin{minipage}{70mm} \includegraphics[width=60mm]{figs/experiment/chir}  \end{minipage}
    &
    \begin{minipage}{50mm}  \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{$\chi(R)$}}

      \vspace{0.5mm}

      Peaks in {\chir} often correspond to ``coordination shells''.
      The Fe-O distance in FeO is 2.14{\AA}  -- the  first
      peak is at 1.6{\AA}.

      \vmm
      This distance shift is due to the
      {\RedEmph{phase-shift}}: ${\sin[{2kR+ {\Red{\delta(k)}}}]}$.

    \end{minipage}\\
    \begin{minipage}{70mm}
      \includegraphics[width=60mm]{figs/experiment/chir_real_mag}  \end{minipage}
    &
    \begin{minipage}{50mm} \setlength{\baselineskip}{10pt}

      \vmm
      \hspace{-3mm}{\Red{$\chi(R)$ is complex:}}\vspace{0.5mm}

      Usually only the amplitude is shown, but there are
      oscillations in $\chi(R)$.

      \vmm

      Both real and imaginary parts are used in modeling.

      \vspace{3mm}
    \end{minipage}
  \end{tabular}
\end{cenpage}
\end{frame}

\begin{frame} \frametitle{Fourier Filtering}

  \begin{cenpage}{130mm}
    \begin{tabular}{ll}
      \begin{minipage}{70mm}
        \includegraphics[width=60mm]{figs/experiment/chir_win}
      \end{minipage}

    &
      \begin{minipage}{50mm}  \setlength{\baselineskip}{10pt}

        \hspace{-3mm}{\Red{$\chi(R)$}}

        often has well-separated peaks for different
        ``shells''. \vmm

      This shell can be isolated by a Filtered Back-Fourier Transform,
      using the window shown for the first shell of FeO.
       \vspace{1mm}

     \end{minipage}\\
      \begin{minipage}{70mm}
        \includegraphics[width=60mm]{figs/experiment/chiq}  \end{minipage}
    &
    \begin{minipage}{50mm} \setlength{\baselineskip}{10pt}
      \hspace{-3mm}{\Red{$\chi(q)$}}  (``$Q$-space'')

      \vspace{0.5mm}

      This results in the filtered $\chi(k)$ for the selected
      shell.

      \vmm

      Many analysis programs use such filtering to remove shells at higher
      $R$.

      \vmm
      Don't do it -- it's too hard.
      \vspace{3mm}
    \end{minipage}
  \end{tabular}
\end{cenpage}

\end{frame}

\begin{frame} \frametitle{Upcoming Topics on Data Reduction and Analysis}

Now that we've seen the basic procedures, a bit more detail on these topics
is needed:

\vmm \vmm \vmm

\begin{tabular}{ll}
%%  {\Red{XANES Analysis}} & What do we do with Normalized $\mu(E)$? \\
  {\Red{Background Subtraction}} & Details on getting from $\mu(E)$ to $\chi(k)$ \\
  {\Red{Fourier Transforms}} &  Details on getting from $\chi(k)$ to $\chi(R)$ \\
  \noalign{\bigskip}
  {\Red{EXAFS Analysis}}   & Modeling to fit experimental $\chi(k)$. \\
\end{tabular}

\vfill
\end{frame}


\input{../../UCLHeader.tex}
\input{../../UCLCommands.tex}
\begin{document}
\title{Error Correction Lecture 3}
\author{with Dan Browne}
\maketitle
\tableofcontents
\section{Correction of a General Error}

We can write the density matrix for a general error in terms of Kraus operators
,
\beq
\rho \rightarrow \sum_j E_j \rho E_j^\dagger = \Sigma(\rho)
\eeq
The limitation of this approach is we cannot write down every possible error. For example, an environment that just replaces $\rho$ by any random state cannot be written down in this formalism.

Here, we will assume that $\rho$ is pure, so that $\rho = \ket{\psi}\bra{\psi} $. This is a fair assumption, because the codewords should be pure states. Mixed states cannot be perfectly distinguished. thus, the channel has the following effect:
\beq
\Sigma \left( \ket{\psi}\bra{\psi}\right) = \sum_j E_j \ket{\psi}\bra{\psi} E_j^\dagger
\eeq
Therefore, we can understand the entire behaviour by studying $E_j \ket{\psi}$ only. If we can correct this, we are good.

Let's now assume that the system is a qubit. This is not necessary, but it will simplify our analysis. We then know that $I,X,Y,Z$  is a basis for linear operators that act on qubits. So we can write
\beq
E_j\ket{\psi} = \left( a I + b X + cY + dZ \right) \ket{\psi}
\eeq
This could perhaps be though of as a `superposition' of operators. We create a superposition of faulty states that contain Pauli errors. We assume that  we have a code that can correct for $X$ and $Z$ errors. We also assume tat $X,Y,Z$ all lead to distinct syndromes.

All errors are orthogonal, since they are also our basis operators. We can detect them and distinguish between them. However, a distinguishable syndrome leads to distinguishable states. Then, in a sense, we `collapse' to a single $X$, $Y$ or $Z$ error.

For a formal treatment of this situation, see Nielsen and Chuang. This is also called error quantisation, or error discretisation.

\section{The Knill-Laflamme condition}

Here, we will introduce a general view of quantum error correction. Here, we imagine a circuit with an error $\calE$ and an operator $R$ that allows for both measurement and detection. Let us start with a state $\ket{\psi}$ that is a codeword in the codespace. The error is a general Kraus operator error,
\beq
\calE (\rho) = \sum_j E_j \rho E_j^\dagger
\eeq
The final state of the circuit should ideally be some $\alpha \ket{\psi}$ where $\alpha$ is some proportionality constant.

Then, given a code, which we call $C$, and an error channel $\calE$, we can ask the following question: When does recovery exist?

Recovery can be thought of as a projector onto the code space. We can assume that we are encoding the logical bits, $\ket{00\ldots 0}$, $\ket{00\ldots 1}$ and so on all up to and including $\ket{11\ldots 1}$. We can choose to represent these bit strings as integers:
\beq
\ket{0}_L, \ket{1}_L , \ldots \ket{k-1}_L
\eeq
Then we can create a projector that projects onto the codespace:
\beq
P = \sum^{2k - 1}_{x = 0} \ket{x}\bra{x}
\eeq
This is a projector onto the space of all codewords.

Question: Why does the projector have $2k$ element? This could be a copying mistake.

Then, given a code $C$ with code space projector $P$ and error $\calE$, a recovery  $R$ exists if and only if
\beq
P E_j ^\dagger E_k P = \alpha_{jk} P
\eeq
So an error model can be corrected if this expression is proportional to $P$, where $\alpha_{jk}$ are the elements of some Hermitian matrix. We can rewrite the above to find
\beq
\sum_x \ket{x}\bra{x} E_j^\dagger E_k \sum_y \ket{y}\bra{y} = \alpha_{jk} \sum_z \ket{z}\bra{z}
\eeq
Here, $x,y,z$ are orthogonal codewords. Now, consider two additional codewords, $\ket{p}$ and $\ket{q}$. We apply these on each side of the expression above and find
\beq
\bra{p}E_j^\dagger E_k \ket{q} = \alpha_{jk} \delta_{pq}
\eeq
Let us look at the two different situations.

If $q \neq p$, then $\delta_{pq} = 0$ and so
\beq
\bra{p} E_j^\dagger E_k \ket{q} = 0
\eeq
What we are saying then is that the states $E_k \ket{q}$ and $E_j \ket{p}$ are orthogonal. This is telling us that no error can make us mix up the codewords because we could always distinguish between them .

Question: However, the error could still change one codeword into another. So we are simply saying that this error channel cannot create codewords that are mixed, or superpositions of other codewords.

The next situation is $p = q$ where $\delta_{pq} = 1$. So then
\beq
\bra{p} E_j^\dagger E_k \ket{q} = \alpha_{jk}
\eeq
What we conclude here is that the inner product of $E_k \ket{q}$ and $E_j \ket{p}$ is independent of the state $\ket{p}$. However, note also that $\alpha_{jk}$ could be zero.

Essentially, we want to prevent the environment from learning about the codeword. Let us look at an example.

\subsection{Example: The Repetition Code}
Recall that it cannot correct $Z$ errors. However, we can also have a value of a qubit by measuring just one single qubit.
Imagine that the environment measures one of our qubits by implementing a CNOT gate.

If we trace out the environment, we find that we have the operators
\beq
E_0 = \frac{I}{\sqrt{2}}
\eeq
\beq
E_1 = \frac{Z}{\sqrt{2}}
\eeq
We will show that this fails the condition that we said out before. Let $E$ act on the 1st qubit. We obtain
\beq
\bra{000} ( ZII)(III)\ket{000} = 1
\eeq
\beq
\bra{111} (ZII)(III) \ket{111} = -1
\eeq
Note that the two outcomes are different. Thus this code fails the Knill-Laflamme condition. Look at section 10.3 in Nielsen and Chuang for more information.

\section{The Steane Code}
This code has $(n = 7, k= 1, d = 3)$. It has logical states
\beq
\ket{0}_L = \ket{0000000} + \ket{1010101} + \ket{0110011} + \ket{1100110} + \ket{000111} + \ket{1011010} + \ket{0111100} + \ket{1101001}
\eeq
There is a large difference in the Hamming weight compared to what we had before. Here, the Hamming weight is 4. This code is actually using superpositions of codewords for a classical Hamming code. We obtain the other codeword by
\beq
\ket{1}_L = XXXXXXX \ket{0}_L
\eeq
To detect phase flips, we can perform the following measurements:
\begin{tabular}{c}
$IIIXXXX$ \\
$IXXIIXX$ \\
$XIXIXIX$
\end{tabular}

In order to detect phase flips, we can apply the measurement
\begin{tabular}{c}
$IIIZZZZ$ \\
$IZZIIZZ$ \\
$ZIZIZIZ$
\end{tabular}
Note the symmetry of the detection methods. Furthermore,  the logical operators are given by
\beq
\bar{X} = X^{\otimes 7}
\eeq
\beq
\bar{Z}= Z^{\otimes 7}
\eeq
Note that every time we write down logical operators, they must retain the original commutation and anti-commutation properties as the original operators. This is the case here as we have an odd number of operators.

\section{Example of a CSS Code}
CSS stands for Calderbank-Shor-Steane. A CSS code is a special type of stabiliser code (more on this later) which is fundamentally constructed based on classical codes.

Error detection operators are design to only detect one signle type of error. The Steane code is non-degenerate. This means that all detectable errors have a unique syndrome. The Steane code is also smaller than the Shor code that we looked at before, which is another advantage.

In classical code, we have the \textbf{Hamming bound}, which denotes a bound on the size of the code.

The Hamming bound is a limit on the parameters of an arbitrary block code.

\section{Quantum Hamming Bound}
For a non-degenerate code, every error has a unique syndrome. This can be used as a proof of a classical Hamming bound.

For a degenerate code, the above is not true. Recall that a non-degenerate code, for example the Steane code, can correct the following number of errors
\beq
t = \frac{d-1}{2}
\eeq
where $d$ is the Hamming distance. Then, we can show that a degenerate code has the following bound
\beq
\sum^t_{j = 0} \bpmat n \\ j \epmat 3^j 2^k \leq 2^n
\eeq
Every error here leads to a distinct syndrome.

Let us here outline the idea instead of looking at the actual proof. The proof can be found in Section 10.3.4 in Nielsen and Chuang.

First, note that we have a number of $2^k$ codewords. We can cound the maximum number of orthogonal states to see this.

The first thing we then do is count the number of different errors. We can have up to $t$ errors of the form $X,Y,Z$ on $t$ different qubits. Then, define $j \leq t$, where $j$ is the number of errors that occurred for this specific case.

Let us count how many error combinations there are. For a number of $n$ physical qubits and $j$ errors, we have a number of
\beq
\bpmat n \\j \epmat
\eeq
errors. On every location location we have a $X,Y,Z$ error. Then, we have
\beq
3^j \bpmat n \\j \epmat
\eeq
distinctive errors. Thus in total, if we consider all values of $j$, we have
\beq
\sum^t_{j = 0} 3^j \bpmat n \\j \epmat
\eeq
And given $2^k$ codewords, we need at least
\beq
\sum^t_{j = 0} 3^j \bpmat n \\j\epmat 2^k
\eeq
orthogonal codewords. These codewords need to be distinguishable, due to the Knill-Laflamme condition.

However, we have $2^n$ orthogonal states in the code space, and hence we obtain the inequality. Note that this only applies to non-degenerate codes, whereas for degenerate codes it is still an open question.

So, if we have $k = 1$ and $t = 1$, we find
\beq
2(1 + 3n) = 2^n
\eeq
Thus, we find
\beq
n >4
\eeq
which means that a 5-qubit code is the smallest code that we can have.

Question: I'm not entirely sure what this condition tells us.

\section{Ingredients for the Stabiliser Formalism}
Before we look closer at the stabiliser formalism, let us outline what we need. There are two main components that we will make use of: Pauli operators and Group Theory.

Recall that a group is an abstraction of symmetry operations. One example of this is a square. Symmetry transformations that leave the square invariant are rotations of 90, 180, 270 and 360 degrees, and reflections. What features are there that we need to take into account?

We can compose two symmetry operations to et another symmetry. For example, the identity is an example of this.

Recall that symmetries are invertible. In an abelian group, the group elements all commute.

We also have that operations are associative. That is,
\beq
A(BC) = (AB) C
\eeq

We can write these conditions succinctly as follows: A group  $G$ is a set of elements $\{g_i\}$ with a binary operation defined, $g_k \circ g_l$ for all elements that satisfies the following requirements.
\begin{itemize}
\item[1] Closure. For every element in the group, their combination using the binary group combination rule is
\beq
g_k \circ g_l \in G
\eeq

\item[2] An identity element $e$, such that
\beq
e \circ g_j = g_j
\eeq

\item[3] An inverse, such that
\beq
g_j (g_j)^{-1} = e
\eeq

\item[4] Composition rule is associative.
\end{itemize}

For us, the last three properties are automatically satisfied by using matrices as group elements. The only thing we need to really concern ourselves with is the requirement of closure.

Recall also that a subgroup is a subset of group elements that is itself a group. For example, it must include an identity.

Another important concept is that of \textbf{group generators}. These can be thought of as a sort-of group-theoretical basis.

generators are a subset of elements of the group $G$ such that every element of $G$ can be expressed as a product of generators. For the square, this would be the 90 rotation element -- from this we could generate all other rotations. However, we also need one more generator, a reflection. If we combine these, we can generate the full group. We sometimes write the group in terms of its generators:
\beq
G = \braket{a_1, a_2 \ldots a_n}
\eeq
Note that the set of generators is not unique. There can be many sets of generators that can create the full group.

The group that we shall study is the Pauli group, or more closely, the $N$-qubit Pauli group. We write
\beq
P_N = \braket{i, X_j, Z_j }
\eeq
for all $j = 1\ldots n$.

For the $n = 1$ case, we have already seen that
\beq
Y = i XZ
\eeq
So in order to create $Y$, we do indeed need to include $i$ as our generators. Therefore, we get the following group elements:
\begin{tabular}{cc}
$\pm I $ & $\pm i I$ \\
$\pm X$ & $\pm i X$ \\
$\pm Y$ & $\pm iY$ \\
$\pm Z$ & $\pm i Z$
\end{tabular}
The $N$-qubit group consists of all $N$-fold tensor products of $X,Y,Z,i$ with prefactors. Note that $N =1$ has 16 elements. In general, we get $4^{N+1}$ elements for the $N$-qubit group.

\end{document}

Error Correction Lecture 1 March 2016.tex