\documentclass[12pt]{article}%{amsart}
\usepackage[top = 1.0in, bottom = 1.0in, left = 1.0in, right = 1.0in]{geometry}
% See geometry.pdf to learn the layout options. There are lots.
%\geometry{letterpaper}               % ... or a4paper or a5paper or ...
%\geometry{landscape}             % Activate for for rotated page geometry
\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{amssymb}
%\usepackage{epstopdf}
% \usepackage{caption}
\usepackage{amsmath}
% \usepackage{longtable}
% \usepackage{tabu}
% \usepackage{accents} %to get undertildes for vec & mat
\usepackage{color} % for colored text
% \usepackage[normalem]{ulem} % for editing: striking out text using 'sout' command

% \newcommand\mytablefigwidth{0.35\textwidth}

% allows to use .ai files directly w/o resaving as pdf
\DeclareGraphicsRule{.ai}{pdf}{.ai}{}

% Handy math macros!
\newcommand{\vect}[1]{\vec{#1}}
\newcommand{\matr}[1]{\mathbf{#1}}
\newcommand{\rate}[3]{{#1}_{#2}^{#3}}
\newcommand{\mmnote}[1]{\textcolor{cyan}{(MM:~#1)}}

% derivative macros. these are sneaky. the [{}] is an empty default 1st arg
% usage: provide 1 arg: \deriv{x} gives d/dx
% provide 2 args like so: \deriv[f]{x} gives df/dx
\newcommand{\deriv}[2][{}]{\frac{d #1}{d #2}}
\newcommand{\pderiv}[2][{}]{\frac{\partial #1}{\partial #2}}

%%%%% Referencing macros %%%%%
\newcommand{\fref}[1]{Figure~\ref{#1}}
\newcommand{\tref}[1]{Table~\ref{#1}}
\newcommand{\eref}[1]{Eq.~(\ref{#1})}
\newcommand{\erngref}[2]{Eq.~(\ref{#1}-\ref{#2})} % Equations
% \newcommand{\test}[1][]{%
% \ifthenelse{\isempty{#1}{}}{omitted}{#1}%
% }

%%%%%
\begin{document}

\title{Generating function solution for the bursty unregulated promoter}

\maketitle

\section{One-state promoter with bursts, mRNA only}

\subsection{From master equation to generating function}
Before tackling mRNA and protein together as in~\cite{Shahrezaei2008}, let's
just do mRNA (replicating Charlotte's notes; she says it's worked out
in~\cite{Paulsson2000}, but I don't see it, at least not in notation I can
comprehend). The master equation of interest is
\begin{align}
\deriv{t}p(m,t) = (m+1)\gamma p(m+1,t) - m\gamma p(m,t) - r p(m,t)
        + r \sum_{m^\prime=0}^m G_{m-m^\prime}(\theta) p(m^\prime,t),
\label{eq:1state_unreg_003}
\end{align}
where $G_{k}(\theta)$ is the geometric distribution defined as
\begin{align}
G_{k}(\theta) = \theta(1 - \theta)^k, \, k\in\{0,1,2,\dots\}.
\end{align}
With this convention, the mean burst size $\beta = (1-\theta)/\theta$.
$\gamma$ and $r$ are mRNA degradation rates and transcription burst
initiation rates, resp. The last term represents all ways the system could
end up with $m$ mRNAs, having started the burst with $m^\prime$. Define
$\lambda = r/\gamma$ and nondimensionalize time by $\gamma$, giving
\begin{align}
\deriv{t}p(m,t) = (m+1)p(m+1,t) - m p(m,t) - \lambda p(m,t)
        + \lambda \sum_{m^\prime=0}^m G_{m-m^\prime}(\theta) p(m^\prime,t),
\end{align}
The probability generating function is defined as
\begin{align}
F(z,t) = \sum_m z^m p(m,t).
\end{align}
Multiply both sides of the CME by $z^m$ and sum over all $m$ to get
\begin{align}
\pderiv[F]{t} = (1 - z) \pderiv[F]{z}
        + \left(\frac{\theta}{1-z(1-\theta)}-1\right)\lambda F.
\end{align}
(This is just like in Charlotte's notes; the tricky bit is to
recognize the need to reverse the double sum in the last term from
$\sum_{m=0}^\infty\sum_{m^\prime=0}^m$ to
$\sum_{m^\prime=0}^\infty\sum_{m=m^\prime}^\infty$.
The sum on $m$ is then an easy geometric series. The rest of the procedure
is just the standard reindexing tricks for sums over master equations.)
Changing variables to $\xi=1-\theta$ and simplifying gives
\begin{align}
\pderiv[F]{t} + (z - 1) \pderiv[F]{z} = \frac{(z-1)\xi}{1-z\xi}\lambda F.
\label{eq:1state_unreg_015}
\end{align}
\subsection{Stead-state}
At steady-state, the PDE reduces to the ODE
\begin{align}
\deriv[F]{z} = \frac{\xi}{1-z\xi}\lambda F,
\end{align}
which we can integrate as
\begin{align}
\int \frac{dF}{F} = \int \frac{\lambda\xi dz}{1-\xi z}.
\end{align}
The initial conditions for generating functions always confuse me,
especially since most authors play fast and loose and assume ``it's
trivial.'' The key fact: from the definition
$F(z,t) = \sum_m z^m p(m,t)$,
normalization requires that
$F(z=1^-,t) = \sum_m p(m,t) = 1$.\footnote{
Sometimes the generating function may be undefined \textit{at} $z=1$ but the
limit still holds. Also people tend to change variables from $z$ to other
things, so don't lose track of how this condition transforms.
}
Doing the integrals (and producing constant $c$) gives
\begin{align}
\ln F &= -\lambda \ln(1-\xi z) + c
\\
F &= \frac{c}{(1-\xi z)^\lambda}.
\end{align}
Only one choice for $c$ can satisfy initial conditions, producing
\begin{align}
F(z) = \left(\frac{1-\xi}{1-\xi z}\right)^\lambda
        = \left(\frac{\theta}{1 - z(1-\theta)}\right)^\lambda,
\end{align}
which is exactly the negative binomial's generating function, as expected.

\subsection{Time-dependent case}
Return to~\eref{eq:1state_unreg_015} and for convenience,
change variables to $v=z-1$, producing
\begin{align}
\frac{1}{v}\pderiv[F]{t} + \pderiv[F]{v} = \frac{\lambda\xi}{1-(1+v)\xi} F.
\end{align}
This can be solved with the method of characteristics. Parametrize the
characteristics by the new variable $s$. Initial conditions at $t=0$
correspond to $s_0$, $v_0$, and $F(v_0,t=0)$, where $s_0$ and $v_0$ are
constants to be found later. The characteristic equations are
\begin{align}
\deriv[t]{s} &= \frac{1}{v}
\\
\deriv[v]{s} &= 1
\\
\deriv[F]{s} &= \frac{\lambda\xi}{1-(1+v)\xi} F.
\end{align}
We could immediately write $v=s+c_v$ for some constant $c_v$ yet to be
found. Most authors drop the constant without justification. This works
because our set of ODEs only contains $ds$, not $s$ itself. So, in fact,
we need not even solve for $v$ in terms of $s$, since the second ODE
tells us $ds=dv$, we can immediately remove $s$ from the problem and we
are left with the ODEs
\begin{align}
\deriv[t]{v} &= \frac{1}{v}
\\
\deriv[F]{v} &= \frac{\lambda\xi}{1-(1+v)\xi} F.
\end{align}
The first is trivial. Using our initial conditions it gives
\begin{align}
v = v_0 e^t.
\end{align}
Set this aside and tackle the second ODE.
It still permits separation of variables which gives
\begin{align}
\int \frac{dF}{F} &= \int \frac{\lambda \xi dv}{1 - (1+v)\xi}
\\
\ln F &= -\lambda \ln(1-(1+v)\xi) + \text{constant}.
\end{align}
Initial conditions determine the constant, but this is subtle. It may be
tempting to transform back to $z$ and $t$, for which initial conditions
are more obvious, but $v$ is a more convenient variable since it is
parametrizes the characteristics. In other words, we can view $F$ as a
one variable function $F(v)$ or as a two variable function $F(z,t)$. It
is much easier to deal with before changing variables back, rather than
after. But, in terms of $v$, what are our initial conditions? We know
$t=0$ corresponds to some $v_0$ we have yet to determine, and this
corresponds to some $F(v_0)$ yet to be found. Any parametrization of the
constant that enforces this is acceptable. A clever idea is to break up
and reparametrize the constant as $c_1$ and $c_2$ and bury these in the
logs as
\begin{align}
\ln \frac{F(v)}{c_1} &= -\lambda \ln\frac{1-(1+v)\xi}{c_2},
\end{align}
where we are now explicitly showing the $v$ dependence of $F$ for clarity.
For this equality to remain true when $v=v_0$,
the only choice of $c_1$ and $c_2$ that works is
\begin{align}
\ln \frac{F(v)}{F(v_0)} &= -\lambda \ln\frac{1-(1+v)\xi}{1-(1+v_0)\xi}
\end{align}
which leads immediately to
\begin{align}
F(v) &= F(v_0) \left(\frac{1-(1+v_0)\xi}{1-(1+v)\xi}\right)^{\lambda}
\end{align}
Now we may transform variables back to $z$ and $t$.
Recall from earlier that $1+v=z$ and $v=v_0 e^t$, so $v_0 = (z-1)e^{-t}$.
These substitutions handle the term in parentheses. $F(v_0)$ is more subtle.
We must avoid the trap of thinking there exists some $z_0$ corresponding to $v_0$;
$v_0$ corresponds only to $t=0$. This is the magic of characteristics.
$F(v_0)$ corresponds to the initial condition on $F$, i.e., $F(z,t=0)$
\textit{not} $F(z_0,t=0)$ since $z_0$ does not exist.
But $F(z,t=0)$, and therefore $F(v_0)$, is by definition given by
\begin{align}
F(v_0) = F(z,t=0) = \sum_m z^m p(m, t=0),
\end{align}
where $p(m, t=0)$ is whatever initial probability distribution
we specify for the problem. Finally then
\begin{align}
F(z, t) &=  \left(\frac{1-(1+(z-1)e^{-t})\xi}{1-z\xi}\right)^{\lambda}
                \left(\sum_m z^m p(m, t=0)\right)
\\
F(z, t) &=  \left(\frac{1-(1-\theta)(1+(z-1)e^{-t})}
                        {1-(1-\theta)z}\right)^{\lambda}
                \left(\sum_m z^m p(m, t=0)\right).
\end{align}
If the initial condition is $p(0,0)=1$, then the second term in
parentheses disappears and this clearly reduces to the usual negative
binomial generating function as $t\rightarrow\infty$. For any other
initial condition, this has to remain true but it's not obvious to me
how the math will work out, even for the next-simplest initial condition
like $p(m,0) = \delta_{mk}$.
\mmnote{Still need to work out some algebra details here to see what happens.}
\mmnote{Our t-dependence is already different than theirs, even though
the steady state is the same. For trivial $k=0$ IC, everything will work
out similarly, I think. For anything else, I think the direct approach
of differentiating will produce an impossible mess. But we might be able
to use their idea of a ``propagator'' probability and do a convolution
over that; in principle, getting to the same place, but that might be
tidier??}

\section{Adding translation}
If we include transcription and translation, the master equation becomes
very similar to Eq.~(1) in~\cite{Shahrezaei2008}, except with mRNA
production terms borrowed from our~\eref{eq:1state_unreg_003}.
As in~\cite{Shahrezaei2008}, it is more convenient to nondimensionalize
time by the protein degradation rate $\gamma_p$ rather than the mRNA
degradation rate $\gamma_m$.
Then $\gamma\equiv\gamma_m/\gamma_p$ is the dimensionless mRNA lifetime,
and $\lambda\equiv r_m/\gamma_p$ is the dimensionless rate at which
transcription bursts initiate.
The mean burst size $\beta = (1-\theta)/\theta$ as before.
The new parameter we need is the translation rate $r_p$,
which we immediately nondimensionalize as $r\equiv r_m/\gamma_p$.
The authors of~\cite{Shahrezaei2008} have three dimensionless parameters
$a$, $b$, and $\gamma$, which have a simple correspondence with ours:
$a=\lambda$, $b=r/\gamma$, and $\gamma=\gamma$.

With all this, the master equation takes the form
\begin{align}
\deriv{t}p(m,n,t) =
        & \lambda \left[\sum_{m^\prime=0}^m G_{m-m^\prime}(\theta) p(m^\prime,n,t)
        - p(m,n,t) \right]
        \\
        & + r m [ p(m,n-1,t) - p(m,n,t) ]
        \\
        & + \gamma [ (m+1) p(m+1,n,t) - m p(m,n,t) ]
        \\
        & + (n+1) p(m,n+1,t) - n p(m,n,t).
\end{align}
The first line covers transcription, the second translation, the third
mRNA degradation, and the fourth protein degradation. Conceptually, this
is identical to Eq.~(1) in~\cite{Shahrezaei2008} except for the very
first term.\footnote{The equations also differ by nondimensionalization
and relabeling of parameters, but these are ``bookkeeping'' operations,
not physical/conceptual ones.}
The probability generating function is now defined as
\begin{align}
F(z,w,t) = \sum_{m,n=0}^\infty z^m w^n p(m,n,t),
\end{align}
and similarly to before, we transform the master equation by multiplying
both sides by $z^m w^n$ and summing over $m$ and $n$.
The first term is unaffected by summing over $n$ and proceeds exactly as
in the mRNA-only case. All other terms proceed exactly as
in~\cite{Shahrezaei2008}, so the PDE for the generating function is
\begin{align}
\pderiv[F]{t} = \left(\frac{\theta}{1-z(1-\theta)}-1\right)\lambda F
        + rz(w - 1) \pderiv[F]{z} + \gamma(1 - z) \pderiv[F]{z}
        + (1 - w) \pderiv[F]{w}.
\end{align}
For convenience, and analogously to the mRNA-only case, changing variables to
$u=z-1$, $v=w-1$, and $\xi=1-\theta$ and simplifying gives
\begin{align}
\pderiv[F]{t} = \frac{\lambda\xi u}{1-\xi(u+1)} F
        + \left[ rv(1+u) - \gamma u \right] \pderiv[F]{u}
        - v \pderiv[F]{v},
\end{align}
or, rearranging terms to emphasize the analogy with
Eq.~(2) in~\cite{Shahrezaei2008},
\begin{align}
\pderiv[F]{v} - \left[ r(1+u) - \gamma \frac{u}{v} \right] \pderiv[F]{u}
        + \frac{1}{v}\pderiv[F]{t} = \frac{\lambda\xi}{1-\xi(u+1)} \frac{u}{v} F.
\end{align}
Again, the left-hand side is functionally identical to Eq.~(2)
in~\cite{Shahrezaei2008}, and only the right-hand side has new content.
Despite the more complicated right-hand side, this is still solvable
with the method of characteristics; parametrizing the characteristics by
$s$ leads to a set of four equations, namely
\begin{align}
\deriv[v]{s}=1
\\
\deriv[t]{s}=\frac{1}{v}
\\
\deriv[u]{s} = \gamma \frac{u}{v} - r(1+u)
\\
\deriv[F]{s} = \frac{\lambda\xi}{1-\xi(u+1)} \frac{u}{v} F.
\end{align}
The equations for $t$ and $v$ are identical to the mRNA-only case,
allowing us to immediately eliminate $s$ from the problem and replace it
with $v$. We also have
\begin{align}
v = v_0 e^t
\end{align}
as before, for some initial $v_0$ yet to be found corresponding to $t=0$.
The remaining two ODEs to be solved are
\begin{align}
\deriv[u]{v} = \gamma \frac{u}{v} - r(1+u)
\\
\deriv[F]{v} = \frac{\lambda\xi}{1-\xi(u+1)} \frac{u}{v} F.
\end{align}
Shahrezaei and Swain approximately solve the first for $\gamma \gg 1$.
\mmnote{Now I see why they defined $b$ as they did, so they could get
$\gamma$ alone on one side. That's a bit cleaner than the way I've
parametrized, but it shouldn't change the conclusion.} In fact, this
approximation should serve us even better than it did them: their yeast
genes of interest feature $\gamma\sim 2-6$, whereas for \textit{E. coli}
with protein ``degradation'' dominated by dilution we expect $\gamma\sim
5-15$. They find $u(v) \approx bv/(1-bv)$, which, translated to our
variables, reads
\begin{align}
u(v) \approx \frac{rv}{\gamma-rv}.
\end{align}
Plugging this into the ODE for $F$ gives
\begin{align}
\deriv[F]{v} \approx
        \frac{\lambda\xi}{1-\xi\frac{\gamma}{\gamma-rv}} \frac{r}{\gamma-rv} F
        = \frac{\lambda r \xi}{(1-\xi)\gamma - rv} F
        = \frac{\lambda r (1-\theta)}{\theta\gamma - rv} F,
\end{align}
where in the last step we changed variables back to $\theta=1-\xi$.
This is still solvable with separation of variables, as
\begin{align}
\int\frac{dF}{F} &\approx \int \frac{\lambda r (1-\theta) dv}{\theta\gamma - rv} F
\\
\ln \frac{F(v)}{F(v_0)} &\approx -\lambda (1-\theta)
                \ln\left(\frac{\theta\gamma - rv}{\theta\gamma - rv_0}\right),
\end{align}
where we handled initial conditions in close analogy to the mRNA-only case.
With some algebra, and recalling that $v=w-1$ and $v_0=(w-1)e^{-t}$, we have
\begin{align}
F(w,t) \approx \left(\frac{1-\frac{r}{\theta\gamma}(w-1)e^{-t}}
                {1-\frac{r}{\theta\gamma}(w-1)}\right)^{\lambda(1-\theta)}
                \left(\sum_n w^n p(n,t=0)\right).
\label{eq:1state_unreg_030}
\end{align}
Note that $z$, which corresponded to mRNA copy number, dropped out when
we substituted our approximate solution for $u$ as a function of $v$.
This gave us a generating function for the protein distribution alone,
with all stochasticity and burstiness of the mRNA (approximately)
incorporated. The second term is simply the initial condition set by our
initially specified probability distribution on protein.
The first term is exactly, up to relabeling parameters, Eq.~(7)
in~\cite{Shahrezaei2008}, which leads to a probability distribution that
resembles a negative binomial, but with time dependence, multiplied by a
hypergeometric funtion.

Is this result of use to us? As with Eq.~(8) in~\cite{Shahrezaei2008},
it is valid only for $\gamma \gg 1$ and $t\gg \gamma^{-1}$.
The former is easily satisfied for our system. The latter means the result
is only valid for times longer than the mRNA lifetime,
which is also satisfied in our problem.

But the time scale to approach steady state is set by the protein
lifetime itself, which, if dominated by dilution, means $F(w,t)$ will
never reach steady-state, not even to a crude approximation.
So~\eref{eq:1state_unreg_030} applies, but we \textit{cannot} take its
steady-state limit. For very special initial conditions, Shahrezaei and
Swain work out the full time-dependent probability distribution, Eq.~(8)
in~\cite{Shahrezaei2008}. But it is not clear if we can do this for the
binomially partitioned initial distribution that we would like to use.
\mmnote{Needs more thought.}

%%%%%%%%%%%%%%%%%%%%% APPENDICES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\appendix

% \bibliographystyle{nature}
\bibliographystyle{abbrv}
\bibliography{../library}

\end{document}


%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\chapter{Units of Measure}
\label{sec:units-of-measure}

\minitoc

\newpage

\section{Units of Measure}

\syntax\begin{lstlisting}
Type_Representation
    ::= ...
      | '=' 'abstract' 'measure'
      | '=' 'measure' ['(' Long_Id ')'] [semi] measure_def

measure_def
    ::= '_' -- anonymous measure, inferrable by compiler
      | measure_simple
measure_simple
    ::= measure_sequence
      | measure_simple '*' measure_simple  -- product, e.g. "'U * 'V"
      | measure_simple '/' measure_simple  -- quotient, e.g. "'U / 'V"
      | '/' measure_simple  -- reciprocal, e.g. "/'U"
      | '1'  -- dimensionless
measure_sequence
    ::= measure_power [measure_sequence]
measure_power
    ::= measure_atom
      | measure_atom '^' integer_literal  -- power of measure, e.g. "m^2"
measure_atom
    ::= Type_Var  -- variable measure, e.g. "'U"
      | Long_Id  -- named measure, e.g. "ft"
      | '(' measure_simple ')'  -- parenthesized measure, e.g. "(N m)"

measure_literal
    ::= '_'  -- anonymous measure, inferrable by compiler
      | measure_literal_simple
measure_literal_simple
    ::= measure_literal_sequence  -- implicit product, e.g. "m s^-3"
      | measure_literal_simple
        '*' measure_literal_simple  -- product, e.g. "m * s^4"
      | measure_literal_simple
        '/' measure_literal_simple  -- quotient, e.g. "m/s^3"
      | '/' measure_literal_simple  -- reciprocal, e.g. "/s"
      | '1'  -- dimensionless
measure_literal_sequence
    ::= measure_literal_power [measure_literal_sequence]
measure_literal_power
    ::= measure_literal_atom
      | measure_literal_atom '^' integer_literal  -- power of measure, e.g. "m^2"
measure_literal_atom
    ::= Long_Id  -- named measure, e.g. "ft"
      | '(' measure_literal_simple ')'  -- parenthesized measure, e.g. "(N m)"
\end{lstlisting}

Numbers in Aml can have associated units of measure, which are typically used to indicate length, volume, mass, distance and so on. By using quantities with units, the runtime is allowed to verify that arithmetic relationships have the correct units, which helps prevent programming errors.


\chapter{Canonical Correlation}
\label{cancor}

\section{Canonical variates}
\label{canvar}

\section{Interpretation}
\label{canint}
%%%%%\chapter{Canonical Correlation}

In canonical correlation, we are interested in the relationship between two sets of variables.   We do this by creating linear combinations $\boldsymbol{U} = \boldsymbol{a_{1} x_{1}} + \boldsymbol{a_{2} x_{2}} + \cdots + \boldsymbol{a_{p} x_{p}}$ and  $\boldsymbol{V} = \boldsymbol{b_{1} y_{1}} + \boldsymbol{b_{2} y_{2}} + \cdots + \boldsymbol{b_{q} y_{q}}$ such that the correlation between $\boldsymbol{U}$ and $\boldsymbol{V}$ is as high as possible.

To do this, we need to work out the correlation matrix, and partition it:

\begin{displaymath}
\begin{array}{ccccccc} & x_{1} & \ldots & \_{p} & y_{1} & \ldots & y_{q} \end{array}
\end{displaymath}
\begin{displaymath}
\begin{array}{c} x_{1} \\ \vdots \\ x_{p} \\y_{1} \\ \vdots \\y_{3}\end{array}
\left( \begin{array}{ccc|ccc} &&&&&\\&A_{p \times p}& &C_{p \times q}&\\&&&&&\\
\hline
&&&&&\\&C_{q \times p}& &B_{q \times q}&\\&&&&&\\ \end{array} \right)
\end{displaymath}

Having done this, we calculate the matrix:

\begin{displaymath}
\boldsymbol{B^{-1}C^{T}A^{-1}C}
\end{displaymath}

and find the associated eigenvalues (in descending order)  $\lambda_{1} > \lambda_{2} > \ldots > \lambda_{r}$.   The corresponding eigenvectors $\boldsymbol{b_{1}}, \boldsymbol{b_{2}}, \ldots, \boldsymbol{b_{r}}$ give the coefficients of the Y variables.

So:

\begin{displaymath}
\boldsymbol{v_{i}} = \boldsymbol{b_{i}^{T}} \boldsymbol{Y}
\end{displaymath}

where $\boldsymbol{b_{i}} = \left(\begin{array}{c} b_{i1} \\ \vdots \\ b_{iq} \end{array} \right)$ and $\boldsymbol{Y} = \left(\begin{array}{c} \boldsymbol{y_{1}} \\ \vdots \\ \boldsymbol{y_{q}} \end{array} \right)$, or in longhand:

\begin{displaymath}
\boldsymbol{v_{i}} = b_{i1} \boldsymbol{y_{1}} + \cdots + b_{iq} \boldsymbol{y_{q}}
\end{displaymath}

Having calculated these, it is possible to solve the coefficients for the X variables:

$a_{1} = \boldsymbol{A^{-1} C b_{1}}, a_{2} = \boldsymbol{A^{-1} C b_{2}}, \ldots,  a_{r} = \boldsymbol{A^{-1} C b_{r}}$,

f
\begin{displaymath}
\boldsymbol{u_{i}} = \boldsymbol{a_{i}^{T}} \boldsymbol{X}
\end{displaymath}

where $\boldsymbol{a_{i}} = \left(\begin{array}{c} a_{i1} \\ \vdots \\ a_{iq} \end{array} \right)$ and $\boldsymbol{X} = \left(\begin{array}{c} \boldsymbol{x_{1}} \\ \vdots \\ \boldsymbol{x_{r}} \end{array} \right)$, or in longhand:

\begin{displaymath}
\boldsymbol{u_{i}} = a_{i1} \boldsymbol{x_{1}} + \ldots + a_{ir} \boldsymbol{x_{r}}
\end{displaymath}

And one really cute result is that $\left[corr(\boldsymbol{u_{i}}, \boldsymbol{v_{i}})\right]^{2} = \lambda_{i}$.

\section{Computer example}

Franco Modigliani proposed a life cycle savings model, the savings ratio (aggregate personal saving divided by disposable income) is explained by per-capita disposable income, the percentage rate of change in per-capita disposable income, and two demographic variables: the percentage of population less than 15 years old and the percentage of the population over 75 years old.

However, we are interested here in the relationship between the two demographic variables (percent of population under 15, percent of population over 75) and the three financial variables (personal savings, per-capita disposal income, growth rate of dpi).   The first stage of any such analysis would be a visual inspection.

\begin{verbatim}
pairs(LifeCycleSavings, pch = 16)
\end{verbatim}

\begin{figure}
\begin{center}
\includegraphics[width = 0.6\textwidth]{images/cancor}
\caption{Pairwise scatterplots of Life Cycle Savings data}
\end{center}
\end{figure}

And it is worth examining the correlation matrix:

\singlespacing
\begin{verbatim}
> cor(LifeCycleSavings)
              sr       pop15       pop75        dpi        ddpi
sr     1.0000000 -0.45553809  0.31652112  0.2203589  0.30478716
pop15 -0.4555381  1.00000000 -0.90847871 -0.7561881 -0.04782569
pop75  0.3165211 -0.90847871  1.00000000  0.7869995  0.02532138
dpi    0.2203589 -0.75618810  0.78699951  1.0000000 -0.12948552
ddpi   0.3047872 -0.04782569  0.02532138 -0.1294855  1.00000000
\end{verbatim}
\onehalfspacing

It appears that the \textbf{X} variables are correlated.   This is less so for \textbf{Y} variables, and even less so for \textbf{X,Y} inter-correlations.

You need to be sure that the variables are \emph{scaled} before carrying out a canonical correlation analysis.

\singlespacing
\begin{verbatim}
LifeCycleSavingsS <- scale(LifeCycleSavings)
pop <- LifeCycleSavingsS[, 2:3] ## The X matrix
oec <- LifeCycleSavingsS[, -(2:3)] ## the Y matrix
\end{verbatim}
\onehalfspacing

Having created an \textbf{X} matrix and a \textbf{Y} matrix, we now want to find linear combinations of \textbf{X} which have maximum correlation with \textbf{Y}.

\singlespacing
\begin{verbatim}
> cancor(pop, oec)
$cor
[1] 0.8247966 0.3652762

$xcoef
             [,1]       [,2]
pop15 -0.08338007 -0.3314944
pop75  0.06279282 -0.3360027

$ycoef
           [,1]        [,2]         [,3]
sr   0.03795363  0.14955310 -0.023106040
dpi  0.12954600 -0.07518943  0.004502216
ddpi 0.01196908 -0.03520728  0.148898175

$xcenter
        pop15         pop75
-4.662937e-16  2.753353e-16

$ycenter
          sr          dpi         ddpi
1.421085e-16 6.661338e-17 4.440892e-16
\end{verbatim}
\onehalfspacing

This indicates one canonical correlate with a correlation of 0.8247966 between $z_{\boldsymbol{X}1}$ and $z_{\boldsymbol{Y}1}$

\begin{eqnarray}
z_{\boldsymbol{X}1} =  -0.08338007  x_{pop15} + 0.06279282 x_{pop75}\\
z_{\boldsymbol{Y}1} = 0.03795363 y_{sr} + 0.12954600 y_{dpi} +  0.01196908 y_{ddpi}
\end{eqnarray}

If we extract the coefficients as vectors (this time we have created \texttt{LCS.cancor} as an object; also we have used \texttt{as.numeric(\ldots)} to extract the coefficients in a form suitable for matrix multiplication).

\singlespacing
\begin{verbatim}
> LCS.cancor <- cancor(pop, oec)
> ycoef <- as.numeric(LCS.cancor$ycoef[,1])
> xcoef <- as.numeric(LCS.cancor$xcoef[,1])
> v1 <-  oec %*% ycoef ## remember oec and pop are scaled
> u1 <-  pop %*% xcoef
> plot(v1, u1)
> identify(v1, u1, row.names(LifeCycleSavings))
\end{verbatim}
\onehalfspacing

\subsection{Interpreting the canonical variables}

There is some ``controversy'' about the best way of interpreting the canonical variables.   You have two possibilities:

\begin{itemize}
\item Interpret the coefficients in a similar way to that used in principal components (problems with collinear variables)
\item Calculate the correlation between the canonical and the original variables (doesn't tell you anything about joint contributions)
\end{itemize}

%Consider:

%$\rho_{\hat{U},\boldsymbol{x}}$ = matrix of correlations between $\hat{U}$ and $\boldsymbol{x}$\\
%$\rho_{\hat{V},\boldsymbol{y}}$ = matrix of correlations between $\hat{V}$ and $\boldsymbol{y}$ \\
%$\rho_{\hat{U},\boldsymbol{y}}$ = matrix of correlations between $\hat{U}$ and $\boldsymbol{y}$ \\
%$\rho_{\hat{V},\boldsymbol{x}}$ = matrix of correlations between $\hat{V}$ and $\boldsymbol{x}$\\

%Which can be obtained fairly simply as:

%\begin{eqnarray*}
%\rho_{\hat{U},\boldsymbol{x}} = a \Sigma_{11}

\subsection{Hypothesis testing}

As with Principal Components, a certain amount of hypothesis testing is possible.   The distributional properties of canonical variables is far wilder than principal components - none of the recommended books discuss it.   However, the tests can be described.   For example, if we wanted to test whether there was any relationship between our two sets of variables:

\begin{displaymath}
H_{0}; \boldsymbol{\Sigma}_{12} = \boldsymbol{0}
\end{displaymath}

The Likelihood ratio test leads us to:

\begin{displaymath}
\Lambda^{\frac{2}{n}} = |\boldsymbol{I} - \boldsymbol{S_{22}^{-1}}\boldsymbol{S_{21}}\boldsymbol{S_{11}^{-1}}\boldsymbol{S_{12}}| = \prod_{i=1}^{k}(1-r_{i}^{2}) \sim \Lambda_{Wilks}(p, n-1-q,q)
\end{displaymath}

Using Bartlett's approximation this can yield a $chi^{2}$ test:

\begin{displaymath}
-\left(n-\frac{1}{2}(p+q+3)\right) \log \prod_{i=1}^{k}(1-r_{i}^{2}) \sim \chi^{2}_{pq}
\end{displaymath}

As we've seen before, perhaps we are more interested in finding out how many canonical correlations we need to keep in our analysis.   Bartlett also proposed a statistic only $s$ canonical correlations are non-zero:

\begin{displaymath}
-\left(n-\frac{1}{2}(p+q+3)\right) \log \prod_{i=s+1}^{k}(1-r_{i}^{2}) \sim \chi^{2}_{(p-s)(q-s)}
\end{displaymath}

%%% Local Variables: ***
%%% mode:latex ***
%%% TeX-master: "book.tex"  ***
%%% End: ***


\section{Non-uniform Nyquist}
A selection of quotations showing approaches to a non-uniform Nyquist limit:

\begin{description}

\item[Average Sampling Rate]

\citet{Scargle82} (citing the following: Beutler 1966, 1970; Masry \& Lui 1975; Higgins 1976; Wiley 1978; Gaster \& Roberts 1975, 1977; Kar, Hornkohl \& Farmer 1981; Ludeman 1981 \todo{read and summarize these!}):
\begin{quote}
Error-free recovery of a band-limited signal [i.e.~reproduction of the entire function $X(t)$ from the samples $X(t_i)$] can be achieved with irregular sampling as long as the mean sampling rate exceeds the Nyquist rate (i.e. the average number of samples per unit time must exceed twice the highest frequency component in the signal).
\end{quote}

\citet{NumRec}:
\begin{quote}
One guide to choosing $f_{hi}$ is to compare it with the Nyquist frequency $f_c$ which would obtain if the $N$ data points were evenly spaced over the same span $T$, that is $f_c = N/(2T)$. The accompanying program includes an input parameter {\tt hifac}, defined as $f_{hi}/f_{c}$.
\end{quote}

\citet{Horne86}:
\begin{quote}
The largest frequency we calculated was $\pi N/T$ which
is the traditional Nyquist frequency for evenly-spaced data. The Nyquist
frequency is not well defined for unevenly spaced signals, but it can serve as
a reasonable upper limit for the calculation.
\end{quote}

\item[Harmonic Average of Sampling Rate]

\citet{Debosscher07}:
\begin{quote}
For the highest frequency, we used the average of the inverse time intervals
between the measurements: $f_N = 0.5(1/\Delta T)$ as a pseudo
Nyquist frequency. Note that $f_N$ is equal to the Nyquist frequency
in the case of equidistant sampling. For particular cases,
an even higher upper limit can be used \citep[see][]{Eyer99}.
Our upper limit should be seen as a compromise between
the required resolution to allow a good fitting, and computation
time.
\end{quote}

\item[Minimum Sample Spacing]

\citet{Percy86}:
\begin{quote}
... the Nyquist frequency is not well defined for input data obtained at unequally spaced time intervals, the usual case in astronomy \citep{Scargle82}.
Theoretically such a data set contains informatin on the periodicities down to
$\Delta t = \min(t_i - t{i-1})$. In practice, however, a pseudo-Nyquist frequency may be defined by averaging $\Delta t = (t_i - t_{i-1})$, where large, uncharacteristic temporal gaps are avoided. Alternatively, the harmonic mean of all $\Delta t$ may be used. The result is that a useful pseudo-Nyquist frequency may be defined by $\nu_{Ny} = (2 \langle\Delta t \rangle)^{-1}$.
\end{quote}

\citet{Press89}:
\begin{quote}
It is often meaningful to examine frequencies significantly higher than the
Nyquist frequency that would obtain if the same number of data points were
evenly spaced in the same total length of time. Some spectral information is
obtainable for frequencies all the way up to something like half the inverse
spacing of the {\it closest} spaced points.
\end{quote}

\citet{Roberts87}:
\begin{quote}
...for arbitrary $\{t_r\}$, the sampling theorem tells us nothing.
If the data samples are otherwise equally spaced but with missing
points, the theorem says that the data completely determine
a function whose FT is zero for $|v| > 1/(2\Delta_{max})$, where $\Delta_{max}$
is the {\it largest completely sampled} data spacing. However,
there are smaller spacings, and these certainly carry information
about frequencies greater than $1/(2\Delta_{max} )$; some information
is available about frequencies as high as
$1/(2\Delta_{min} )$, where $\Delta_{min}$ is the smallest spacing between data
points. Furthermore, if the $\{t_r\}$ are more or less randomly
distributed, so that a wide range ofspacings are present and
there is little redundancy in the spacing between various
points, tests have shown (Paper II) that significant information
is available on frequencies greater than $l/(2\Delta_{min})$.

Nonetheless, in the present paper we will restrict ourselves
to frequencies obeying
$\nu < \nu_{max} = 1/(2\Delta_{min})$.
\end{quote}

\citet{Hilditch01}:
\begin{quote}
The highest frequency, for equally spaced data, is formally given by the {\it Nyquist frequency}, $f_N = 1/(2\Delta t)$, where $\Delta t$ is the {\it sampling interval} in the data string.
For unequally spaced data, it is common practice to estimate a pseudo-Nyquist frequency from the minimum value of $\Delta t$.
\end{quote}

\item[Getting it Right]

\citet{ICVG2014}:
\begin{quote}
As a good choice for the maximum search frequency, a pseudo-Nyquist frequency $\omega_{max} = \pi/\Delta t$, where $1/\Delta t$ is the median of the inverse time interval between data points, was proposed by \citet{Debosscher07} (in the case of even sampling, $\omega_{max}$ is equal to the Nyquist frequency). In practice, this choice may be a gross underestimate because unevenly sampled data can detect periodicity with frequencies even higher than $2\pi / \Delta t_{min}$ \citep[see][]{Eyer99}. An appropriate choice of $\omega_{max}$ thus depends on sampling (the phase coverage at a given frequency is the relevant quantity) and needs to be carefully chosen: a hard limit on maximum detectable frequency is of course given by the time interval over which individual measurements are performed, such as imaging exposure time.
\end{quote}

\end{description}


In metro systems where passengers ``tap in'' and ``tap out'' using
fare cards, tokens or similar means, explicit travel time data may be
available for every passenger. In a given system, more or less may
indeed be measured; for the purpose of an example calibration as
shown here, it is assumed in particular that passenger travel-time
data are available.

In order to exhibit a concrete calibration procedure, The Madrid Metro
model prepared in the last chapter is used. The scope is restricted to
calibration of the parameters of the walking-speed distribution in the system.
For simplicity, a single log-normal walking-time distribution is applied
to the system as a whole.

In calibrating only the walking-speed distribution, it is implicitly assumed
that other aspects of the system are specified correctly; in particular,
corridor lengths, train schedules, train speeds and track lengths are assumed
to have been finalised.

In order to calibrate the walking-speed distribution, a reference set
of data must be supplied which consists of travel times. Since in the
present context no such real-world set is available for Madrid, a
synthetic set of travel times is generated and used.

\section{Travel-time tool}
The travel-time tool can be run with no parameters\\
\comline{java -jar TravelTimeTool.jar }\\
in order to see documentation and options.

In \Figref{travtime} is shown a distribution of the travel times
appearing in the simulated system for trips beginning between 05:00
and 09:00. The variable plotted is the mean travel time for
\emph{comparable trips}, which are defined as trips with the same
origin and destination, occuring within the same time interval (in
this case, the interval size is 24 minutes).
\begin{figure}[ht]
  \centering
  \includegraphics[angle=0,width=10cm]{80_figs/_TTD_0500-0900.eps}
  \caption{Distribution of travel time; $\mu=2404$ s, $\sigma=1270$ s.}
  \label{travtime}
\end{figure}
To generate the distribution of travel times shown in \Figref{travtime}, \\
\comline{java -jar TravelTimeTool.jar TTD 05:00,09:00,16 0,3600,30 madrid\_01.zo }\\
In addition to the `TTD' mode, there are modes which generate synthetic
reference data (`SYN'), compare against reference data (`TTC'),
and evaluate an objective function for calibration (`OBJ').

These modes are described below; they can be demonstrated in sequence by running\\
\comline{./01\_Run\_ZIM\_SYN\_TTD\_TTC\_OBJ.sh}

\section{Synthetic reference data}
To exhibit how calibration can be performed, a reference set of travel
times can be generated, based on a simulation run, with a command like:\\
\comline{java -jar TravelTimeTool.jar SYN 100 out/madrid\_01.zo > \_SYN\_TravelTimeData.csv} \\
The file {\tt \_SYN\_TravelTimeData.csv} produced will be the reference data,
used in here in place of actual ground-truth measurements.

\section{Comparison}
To see the discrepancy between the simulation output and the reference data:
{\tt java -jar TravelTimeTool.jar TTC 05:00,09:00,16 -600,600,20 \_SYN\_TravelTimeData.csv out/madrid\_01.zo }\\
For this example, the time interval between 05:00 and 09:00 is considered.
The result is shown in \Figref{TTdiff}.
\begin{figure}[ht!]
  \centering
  \includegraphics[angle=0,width=10cm]{80_figs/_TTC_0500-0900.eps}
  \caption{Distribution of travel-time error}
  \label{TTdiff}
\end{figure}
This shows that the travel times (compared within `bins' as described before) are 100 seconds
faster than the reference data. (Fluctuations average out, making this distribution very narrow.)
The mean simulation travel-time error is $-100$ s with a standard deviation of $7.37$ s.

\section{Objective}
\label{DefObj}
The parameters of the walking-speed distribution are to be adjusted so that the simulated
travel times correspond as closely as possible to the reference data.
The objective used for this purpose is
\[
\Phi(\mu,\sigma) := \sum_{o,d,\tau} (\tilde{T}_{o,d,\tau} - T_{o,d,\tau})^2
\]
where $\mu$ and $\sigma$ are the mean and standard deviation of the
log-normal distribution\footnote{It is noted that they are not the
 mean and standard deviation of the associated normal distribution; this was described in \Secref{sec:zsource}}
used for walking speeds and specified in the \zobj{zsource} in {\tt 00\_StaticTypes.zim}.
These quantities are each measured in m/s.

$\tau$ labels discrete time intervals during the day; these are statistical bins enabling a definition of \emph{comparable} trips.
Two trips are comparable when they share the same origin $o$, the same destination $d$ and begin
during the same interval $\tau$. The specification {\tt 05:00,09:00,16} in the above commands indicates
that there will be $16$ intervals of $15$ minutes each; in this case $\tau=0\dots15$.

$\tilde{T}_{o,d,\tau}$ is the reference travel-time mean for trips from origin $o$ to
destination $d$ during time interval $\tau$. $T_{o,d,\tau}$ is the mean generated by simulation.

\section{Calibration}
The calibrated values for $\mu$ and $\nu$ are those which minimise $\Phi$.
The method used here to approximate this minimum is a discrete search algorithm which is
analogous to gradient descent;
\[
(\mu,\sigma) = {\textup{LocalMin}(\Phi;\mu_0,\sigma_0)}
\]
with parameters $E=1.075$, $R=0.7$, $s_0=1.0e-8$, $\delta=0.01$, $i_{max}=100$ and initial
estimate $\mu_0=2.0$, $\sigma_0=0.7$.
$\hat{C}$ is set to constrain the parameters within sane
ranges; $1 < \mu < 10$ and $0.05 < \sigma < 1.0$ which are not expected to be saturated.

Each evaluation of $\Phi(\mu,\sigma)$ corresponds to a simulation run;
$\Phi$ depends on the simulation output and the reference data.  The
LocalMin algorithm is as follows.

\begin{itemize}
\item[] {\bf algorithm} $\textup{LocalMin}(f;{\bf x}_0)$ to find ${\bf x}$ near ${\bf x}_0$ which sufficiently\\
  minimises $f(\bf x)$ subject to constraints $\hat{C}$.
  \begin{itemize}
  \item[] {\bf Choose} values for constant parameters:
    \begin{itemize}
    \item[] $E$ --- enthusiasm
    \item[] $R$ --- reluctance
    \item[] $\delta$ --- perturbation
    \item[] $i_{max}$ --- maximum iterations
    \end{itemize}
  \item[] {\bf Set} initial values
    \begin{itemize}
    \item[] ${\bf x} \leftarrow {\bf x}_0$ --- initial guess
    \item[] $s \leftarrow s_0$ --- descent factor
    \item[] $i \leftarrow 0$ --- iteration counter
    \end{itemize}
  \item[] {\bf Loop}:
  \item[] {Evaluate} $f$ to numerically define gradient of $f$ at ${\bf x}$:
    \begin{itemize}
    \item[] Evaluate $f_{base} \leftarrow f({\bf x})$ (or use $f_{new}$ if $i>0$ and last step was enthusiastic)
    \item[] for $j$ from $1$ to $d$ do
      \begin{itemize}
      \item[] Evaluate $f_j \leftarrow f({\bf x} + \delta \hat {\bf j})$
      \end{itemize}
    \item[] ${\bf g}$ defined by $g_j = (f_j - f_{base})/{\delta}$ is a numerical gradient of $f$ at ${\bf x}$.
    \end{itemize}
  \item[] Use ${\bf g}$ to search for a better ${\bf x}$:
    \begin{itemize}
    \item[] $\Delta{\bf x} \leftarrow s {\bf g}$
    \item[] Tentative new estimate: ${\bf x}' \leftarrow {\bf x} + \Delta{\bf x}$
    \item[] Apply constraints: ${\bf x}' \leftarrow \hat{C} {\bf x}'$
    \item[] if ${\bf x}' = {\bf x}$ then
      \begin{itemize}
      \item[] Quit with result ${\bf x}$
      \end{itemize}
    \item[] Evaluate $f_{new} \leftarrow f({\bf x}')$
    \item[] if $f_{new} < f_0$ then
      \begin{itemize}
      \item[] Update estimate: ${\bf x} \leftarrow {\bf x}'$
      \item[] Exhibit enthusiasm: $ s \leftarrow sE$
      \end{itemize}
    \item[] else if $f_{new} = f_0$ then
      \begin{itemize}
      \item[] Quit with result ${\bf x}$
      \end{itemize}
    \item[] else
      \begin{itemize}
      \item[] Exhibit reluctance: $ s \leftarrow sR$
      \end{itemize}
    \end{itemize}
  \item[] $i \leftarrow i+1$
  \item[] Check:
    \begin{itemize}
    \item[] if $i>i_{max}$ then quit with result ${\bf x}$
    \item[] if $|\Delta{\bf x}| < dx$ then quit with result ${\bf x}$
    \end{itemize}
  \end{itemize}
\end{itemize}
The notation is that ${\bf x}$ is of dimension $d$, and $\hat {\bf j}$ is a unit vector in the $j$ direction, with $j=1\dots d$.

\section{Running calibration}

A script performing the above calibration procedure can be run; the synthetic data generated
with the script described above is used.\\
\comline{./02\_Run\_Zim\_Minimise\_Objective.awk} \\
After some computation, the result can be found in the file {\tt \_00\_StaticTypes.zim}:
\[ % 1.32948   0.162204
  v_\mu = 1.329,  \quad v_\sigma = 0.162
.\]

\section{Calibrated model}

Running again the Travel-Time Tool, \\
\comline{./03\_Run\_TTC\_post\_calib.sh} \\
a comparison bewteen the simulator output and the synthetic
reference data now appears as shown in \Figref{TTdiff2}.
The calibration procedure has reduced the mean travel-time error from $-100$ s to $-19$ s

\begin{figure}[ht]
  \centering
  \includegraphics[angle=0,width=10cm]{80_figs/_PostCalib_TTC_0500-0900.eps}
  \caption{Distribution of travel-time error after calibration; $\mu=-15$ s, $\sigma=90$ s.}
  \label{TTdiff2}
\end{figure}

The new global distribution of travel times is shown in the following
chapter, in \Figref{_TTDist2}; it now has a mean of $2333$ s and a
standard deviation of $998$ s.
%\begin{figure}[ht]
%  \centering
%  \includegraphics[angle=0,width=10cm]{_TTD2_0500-2500.eps}
%  \caption{Distribution of travel times after calibration; $\mu=...$ s, $\sigma=...$ s.}  \label{TTglobal2}
%  \label{TTglobal2}
%\end{figure}

In \Chapref{Chap:MadResu} various results will be extracted from the
calibrated simulation.


\documentclass[8pt]{article}

\usepackage{fullpage}
\usepackage[margin=.5in]{geometry}
\usepackage{epic}
\usepackage{eepic}
\usepackage{graphicx}
\usepackage{mathtools}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{ragged2e}
\usepackage[parfill]{parskip}
\usepackage{amssymb}

\newcommand{\proof}[1]{
{\noindent {\it Proof.} {#1} \rule{2mm}{2mm} \vskip \belowdisplayskip}
}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\newtheorem{lemma}{Lemma}[section]
\newtheorem{theorem}[lemma]{Theorem}
\newtheorem{claim}[lemma]{Claim}
\newtheorem{definition}[lemma]{Definition}
\newtheorem{corollary}[lemma]{Corollary}

\begin{document}
\hfill \small{\today} \\
\setlength{\fboxrule}{.5mm}\setlength{\fboxsep}{1.2mm}
\newlength{\boxlength}\setlength{\boxlength}{\textwidth}
\addtolength{\boxlength}{-4mm}
\begin{center}\framebox{\parbox{\boxlength}{\bf
\center{CS 577 - Homework 11}
\center{Sejal Chauhan, Vinothkumar Siddharth, Mihir Shete}
}}\end{center}
\vspace{5mm}

\section{Graded written problem}

\textbf{Part a:} Two Travelling Turkey Problem (TTTP) as mentioned in the written problem has the following contraints:
\begin{itemize}
\item Each city is visited at least once
\item Number of distinct routes has to be as small as possible
\item Each city can be visited any number of times
\item Path starts and ends at the same city
\item Maximum of the total effort of the two turkeys is minimized
\end{itemize}

To formally describe the problem we will consider a complete graph $G = (V, E)$ with $2$ edge-weight functions $w_a: E \rightarrow \mathbb{R}$ and $w_h: E \rightarrow \mathbb{R}$. Where $w_a(e)$ represents the effort taken by \textit{Abe} to travel along the edge $e$ from a city on one end of the edge to the other, while $w_h(e)$ represents the effort required by \textit{Honest}

With the above definition for the graph $G$ we can describe the decision version of the problem as:

For the graph G and $k \in \mathbb{R}$, report \textit{yes} if there exists a spanning tree $S$ such that $MAX(\sum_{\forall e \in S}{w_a(e)}, \sum_{\forall e \in S}{w_h(e)})$ is at-most $k$, else report \textit{no}.

\textbf{Part b:} Decision problem is NP-complete

To prove that TTTP decision problem is NP-complete, we will show that Partition Set(PS) that is a known NP-complete problem, can be reduced to TTTP problem.
Given a set $S$ of Real numbers and the sum of all elements in $S$ as $2*w$, we compute a graph $G$, such that $G$ has a spanning tree $ST$ with $MAX(\sum_{\forall e \in ST}{w_a(e)}, \sum_{\forall e \in ST}{w_h(e)}) \leq w$ only if we can Partition the set using PS algorithm.

To transform the elements of $S$ to the graph $G$ we create a root node, and for every element $i$ in $S$ and add two vertices with edge weight as ($w_a = S[i]$, $w_h = 0$) and ($w_a = 0$, $w_h = S[i]$) to the root note. The edge weight between these two vertices is (0,0). Construct this gadget with all the elements in $S$ and make it a complete graph by connecting all the unconnected vertices with edge weights ($w_a = \infty$,$w_h = \infty$).

Now, if the TTTP-decision problem returns that \textit{yes} we claim that we can partition the set $S$ in $2$ subsets such that each subset has sum $w$. This is possible when the TTTP-decision problem can find a spanning tree in the graph $G$ such that $\sum_{\forall e \in ST}{w_a(e)} = \sum_{\forall e \in ST}{w_h(e)} = w$. All the weights returned by $w_a(e)$ can be considered elements of one subset and the weights returned by $w_h(e)$ the elements of the other subset for the \textit{Partition} problem on set $S$, and we can see that these subsets contain all the elements of set $S$ and their sums are equal and so the PS-decision problem on set will also return \textit{yes}.

If the TTTP-decision problem returns \textit{no}, then consider a spanning tree made of edges whose weights are not $\infty$. Such a spanning tree will have minimum total edge weight for $w_e$ and $w_h$ summed together of $2*w$ when all the elements in the set $S$ either as the edge weights for one turkey or the other. Now since we cannot find a spanning tree, wherein $\sum_{\forall e \in ST}{w_a(e)} \leq w$ and $\sum_{\forall e \in ST}{w_h(e)} \leq w$, because if one of them is less than $w$ then the other one will be greater than $w$. And hence these set of edge weights cannot be used to create a solution for the PS problem, so PS-decision problem will also return \textit{no}.

Thus, PS-decision can be reduced to TTTP-decision problem and so TTTP-decision problem is NP-complete.

\end{document}


\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usepackage{listings}
\usepackage{mathtools}
\title{ELEC4320 Homework 3}
\author{Dhesant Nakka | 20146587}

\lstset {
  language=C++,
  basicstyle=\footnotesize
}

\newcommand{\textunderscript}[1]{$_{\text{#1}}$}

\begin{document}
\maketitle

\section*{Question 1}
a)
\begin{center}
  \includegraphics[width=0.7\linewidth]{q1_mux.png}
\end{center}
The design takes advantage of the structure of multiplexer, where one 2-input multiplexer can be mapped to a single 3-input LUT, shown in the following truth table. By using the 2 3-LUT's as 2-input multiplexers, a two-tier 4-input multiplexer can be made using only one block.
\begin{center}
\begin{tabular}{ | c c c | c | }
  \hline
  S & A\textunderscript{0} & A\textunderscript{1} & O \\
  \hline
  0 & 0 & 0 & 0 \\
  0 & 0 & 1 & 1 \\
  0 & 1 & 0 & 0 \\
  0 & 1 & 1 & 1 \\
  1 & 0 & 0 & 0 \\
  1 & 0 & 1 & 0 \\
  1 & 1 & 0 & 1 \\
  1 & 1 & 1 & 1 \\
  \hline
\end{tabular}\\
\end{center}

b) Using the same 2-input multiplexer from a 3-input LUT as part a).\\*
\includegraphics[width=\linewidth]{q1_lut.png}\\\\

\section*{Question 2}
System Generator schematic diagram:\\*
\includegraphics[width=\linewidth]{q2/model.png}\\
\clearpage
Simulink simulation results:\\*
\includegraphics[width=\linewidth]{q2/data_plot.png}

\section*{Question 3}
a)
i)
The longest operation is the two compound adders A1 and A2 in C-step1, requiring a total time of 6 ns. Therefore, the required clock period should be 6ns.\\*

Moving the A2 adder block from C-step1 to C-step2 would satisify the new clock period of 4ns, since the output of A2 is not needed until C-step3 (see Figure \ref{fig:q3_design});

\begin{figure}[h!]
  \centering
  \includegraphics[width=0.8\linewidth]{q3/q3_design.png}
  \caption{The new design schematic to meet the 4ns clock period requirement}
  \label{fig:q3_design}
\end{figure}

ii)
According to the new design, in addition the 2 multipliers and 1 adder needed to implement the design, the design will also need 4 multiplexers to control which input goes to the multiplier and adder blocks, and 3 registers (as seen in Figure \ref{fig:q3_hw}), to store the output from each of the multiplier and adder blocks between each cycle. The assignment can be seen in Figure \ref{fig:q3_assign}. The final schedule can be seen in Table \ref{tab:q3_schedule}. The final hardware diagram can be seen in Figure \ref{fig:q3_hw}.

\begin{figure}[h!]
  \centering
  \includegraphics[width=0.8\linewidth]{q3/q3_design2.png}
  \caption{Assignment of the operations to different hardware block}
  \label{fig:q3_assign}
\end{figure}

\begin{table}[h!]
  \centering
  \begin{tabular}{| r | c c c |}
    \hline
    Step & Mult 1 & Mult 2 & Add 1 \\
    \hline
    C-step1 & A $\times$ B & C $\times$ D & E + F \\
    C-step2 & Mult 1 $\times$ Mult 2 & & G + Add 1 \\
    C-step3 & & & Mult 1 + Add 1 \\
    \hline
  \end{tabular}
  \caption{Scheduling of block operations}
  \label{tab:q3_schedule}
\end{table}

\begin{figure}[h!]
  \centering
  \includegraphics[width=\linewidth]{q3/q3_diagram.png}
  \caption{Block overview of the final implementation}
  \label{fig:q3_hw}
\end{figure}

\clearpage

b)
\begin{lstlisting}[showstringspaces=false]
  #include "ap_cin.h"

  typedef uint4 t_data4;
  typedef uint5 t_data5;
  typedef uint8 t_data8;

  t_data5 A[10];
  t_data8 B[10], C[10];
  t_data4 i;

  for (i = 0; i < 10; ++i) {
    C[i] = A[i] + B[i];
  }
\end{lstlisting}

The new code uses less area because of the reduced width of all the integers. Instead of having to create a 32-bit adder on the FPGA, which will require a lot of LUT's, all that is needed now is at 8-bit LUT adder, since the output value will never exceeed the 8-bit limit of 255. Also, the comparator needed to check the flow of the loop can be made smaller, since it is now a 4-bit integer instead of a 32-bit integer. In addition, since the arrays are also a lot smaller, they use less memory space than before, allowing them to be reshaped to fit into less memory.

\end{document}


%#!pdflatex Naruse_Esurf_2020.tex

\section{Inverse modeling by deep learning NN}
In this study, numerical simulation of a turbidity current is repeated under various random initial conditions to produce a data set of the characteristic features of turbidites. Then, this artificial dataset of turbidites is used for supervised training of a deep learning NN. The values of the turbidites characteristics, i.e., distribution of volume-per-unit-area of all grain size classes, in the training data set are input to the NN, and the estimated initial conditions (e.g., initial flow height and concentration) of the turbidity current is obtained from the output nodes of NN. The output values of the NN are compared with the true conditions. The optimization of weight coefficients of NN is then conducted to reduce the mean square of the difference between the true conditions and the output values of the NN. If the number of training datasets is sufficiently large, the trained NN should be able to estimate the paleo-hydraulic conditions from the data of the ancient turbidites (Fig. \ref{fig:schematic_diagram_procedures}). In other words, an empirical relationship with numerical results and the model input parameters are explored in this method, and the discovered relationship is used for inverse modeling of turbidity currents.

The local conditions of a turbidity current (velocity, concentration, etc.) at any locations and time can be estimated from the reconstructed initial conditions. The flow parameters are obtained by calculating the time evolution of the forward model from the initial conditions. In this way, we can obtain the behavior of the flow with a relatively small number of parameters. This approach has already been tried successfully by Lesshafft et al. (2011), and Falcini et al. (2009) also reconstructed flow conditions of turbidity currents by obtaining boundary conditions of the model.

The details of these procedures are described below.

\subsection{Production and preprocessing of training and test data sets for supervised machine learning}

We conducted iterative calculations using the forward model and accumulated data to train and validate the inverse model. To investigate the appropriate amounts of data for training the inverse model, we conducted 500--3500 iteration of the forward model calculations. To verify the performance of the trained model, 300 test data sets were also generated numerically, independent of the training data.

Model input parameters that are subject to inversion are required to produce the training and test data by the forward model calculation (Fig. \ref{fig:model_input_parameters}). In this study, the model inputs are the initial flow height $H_0$, the initial flow length $l_0$, the initial sediment concentration for the $i$th grain size class $C_i$, and the basin slope $S$. These model parameters are generated as uniform random numbers within a certain range, and their range is changed according to the target of the inverse analysis. Since this study is aimed at field-scale analysis, the following ranges are chosen. Both initial depth and length of suspended cloud range from 50 to 600 m. The sediment concentration for each grain size class ranges from 0.01\% to 1.0\%. The number of grain size classes $N$ is four, and the representative grain diameters are 1.5, 2.5, 3.5 and 4.5 phi. The inclination of the basin plain where the turbidites are expected to form ranges from 0 to 1.0\%.

Each run of the forward model calculation is initiated with the given model input parameters, and is terminated when the flow head reaches the downstream end or sufficiently long time period ($1.2 \times 10^5$ s.) has elapsed. As a result of the calculation, the forward model outputs the volume-per-unit-area of sediment for all grain size classes over the 100 km-long calculation domain. The inverse model estimates the model input parameters from the resultant spatial distribution of the granulometric characteristics of the deposits. However, in natural outcrops, it is unlikely that the entire distribution of the turbidite beds would be exposed. Therefore, we limit the length of the sampling window in the calculation domain, and only the sediment data contained in this window is extracted for both training and testing. The upstream end of the sampling window was set at the transition point between the steep slope and the basin plain (5 km from the upstream end), and the length of the window varies from 1 to 30 km to evaluate the data interval required for the inverse analysis.

Before the model input parameters are input to NN, all values are normalized between 0 and 1 using the following equation:

\begin{equation}
I_{i}^{*} = \frac{I_i - I_{min}}{I_{max} - I_{min}}
\label{eq:normalization}
\end{equation}

\noindent where $I_i^*$ and $I_i$ denote the $i$th normalized and original input parameters, respectively. $I_{\mathrm{max}i}$ and $I_{\mathrm{min}i}$ are the maximum and minimum values used for generating the $i$th input parameter, respectively. This min-max normalization is applied to consider all parameters at equal weights because the range of the initial flow conditions is significantly different between them.

\subsection{Structure of NN}
The artificial NN is used as the inverse model to reconstruct flow conditions from the depositional architecture. We input the spatial distribution of volume-per-unit-area of multiple grain size classes of a turbidite in the NN, which outputs the values of the flow initial conditions and the basin slope. In this study, we use a fully connected NN that has four hidden layers. The volume-per-unit-area of $N$ grain-size classes of sediment deposited on $M$ spatial grids in the sampling window is given to the input nodes of the NN. Thus, the total number of the NN input nodes is $N \times M$. The number of nodes in all hidden layers is set to 2000 in this study.

The Rectified Linear Unit (ReLU) activation function is adopted for all NN layers \citep{Nair2010, Glorot2011}. The ReLU is the half-wave rectifier $f(z) = \max(z, 0)$. Compared with other smoother non-linearities, such as $\tanh(z)$ or $1/(1+\exp(-z))$, the ReLU typically learns much faster in NN with multiple layers \citep{Glorot2011}, and thus it allows to train a deep supervised network without unsupervised pre-training \citep{LeCun2015}.

The NN is expected to output the model input parameters (i.e., the initial flow conditions and the basin slope), and therefore, the number of nodes in the output layer is equal to the number of input parameters for the forward model, which is seven here (the initial flow length, depth, sediment concentrations and the basin slope).

\subsection{Training the inverse model}
To develop the inverse model, supervised training is conducted using the artificial dataset produced by the forward model calculation. First, the artificial dataset is randomly split into training and validation datasets to detect overfitting during the training process. The ratio of the validation dataset is set to 0.2 so that 80\% of the artificial dataset is used for training. The model input parameters used for producing training and validation sets were regarded as the teacher data to train and evaluate the model.

Methodology applied for training the NN is as follows. The mean squared error (MSE) is adopted as the loss function because the supervised training of NN in this study is classified as a regression problem \citep{Specht1991}, and MSE is a common loss function for regression \citep{Bishop2006,Hastie2009,ShalevShwartz2014}. Before training, all weight coefficients of NN are randomly initialized using the Glorot uniform distribution \citep{Glorot2010}. The backpropagation algorithm \citep{Rumelhart1986} is used to calculate the derivative of this error metric for each connection between the nodes, and the stochastic gradient descent method (SGD) with Nesterov momentum \citep{NESTEROV1983} is used for optimizing the weight coefficients of NN to minimize the difference between the model predictions and the teacher datasets. Other optimization methods, such as AdaGrad \citep{Duchi2011}, RMSprop \citep{Tieleman2012} and AdaDelta \citep{Zeiler2012}, have been tested, but SGD shows the best performance in this case. Dropout regularization \citep{Srivastava2014} is applied for each epoch to reduce overfitting and to improve the generalization ability of the NN. One training epoch, which refers to one cycle through the full training dataset, is repeated until the loss function of the validation dataset converges to a constant value. These methods are all implemented in Python with the library Tensorflow 2.1.0 \citep{Raschka2019}, and the calculations are conducted using GPU NVIDIA GeForce GTX 2080 Super with libraries CUDA 11.0 and CuDNN 7.0.

Several hyperparameters should be specified for the training of NN. Specifically, the dropout rate, the learning rate, the batch size, the number of epochs, and the momentum are adjusted manually after repeated trial and error. To perform an optimization calculation with SGD, the batch size and the learning rate were set to 32 and 0.02, and the value 0.9 was chosen for the momentum. Dropout rate for regularization was 0.5.

\subsection{Testing the inverse model}

The performance of the inverse model is tested using a set of 300 data that are produced independently of the training and validation datasets. The inversion precision for each model input parameter is evaluated by the root mean square error (RMSE) and the mean absolute error (MAE) of the prediction. These error metrics are computed for both raw and normalized values with true values, and used to evaluate the model. Moreover, the bias of prediction (i.e., the mean deviation of the model predictions from the true input parameters) is used describe the accuracy of the inversion.

Three additional tests are conducted for verifying the robustness of the inverse model that is significant for the applicability of the model to field datasets. The results of these tests are evaluated by the average of the normalized RMSE, which is defined as:

\begin{equation}
  \mathrm{RMSE} = \sqrt{ \frac{1}{JK} \sum_J \sum_K {\left( \frac{I_{\mathrm{p}jk} - I_{jk}}{I_{jk}} \right)^2} }
  \label{eq:RMSE}
\end{equation}

\noindent where $I_{\mathrm{p}jk}$ and $I_{jk}$ denote the predicted and the original values of the $j$th model input parameter for the $k$th test dataset, respectively. $J$ and $K$ are the numbers of the model input parameters and the test data sets.

First, noise is artificially added to the test data to evaluate the robustness of the inversion results against the measurement error. Under natural conditions, measurement errors in the thickness and grain size analysis of turbidites as well as the local topography  affect these results. If the results of the inverse analysis change significantly due to such errors, it means that our method is not suitable for application to field data. To investigate this, we apply normal random numbers to the volume per unit area at each grid point in the training data at various rates, and we observe how much influence the noise has on the inverse analysis results.

The second test on the inverse model is to perform a subsampling of the grid points in the training data. Outcrops are not continuous over tens of kilometers, so that the thickness and the grain size distribution of a turbidite in the interval between outcrops can only be obtained by interpolation. To simulate this situation, the grid points in test datasets are randomly removed in this test, and the volume-per-unit-area at the removed grid points is linearly interpolated. By varying the rate at which grid points are removed, this test also allows us to estimate the average interval of the outcrops that are necessary for conducting the inverse analysis. That is, if 90\% of the grid points set at 5 m intervals are removed, and the inverse analysis is conducted on the remaining 10\%, the average distance between the grid points is 50 m. Estimating the outcrop spacing requires obtaining reasonable results of inverse analysis before applying it to the actual field.

Finally, the influence of the length of the upstream slope was examined. In this study, it is assumed that a steep slope (10 \%) of a submarine canyon with a length of 5 km exists upstream, and a basin plain with a gentle slope exists downstream of the steep slope. Although the topography and deposits of the upstream slope are not the subject of the inverse model analysis, the length of the slope potentially affect the results of the inverse analysis. As a test, we set a slope of 10 km length instead of 5 km upstream, and deposited a turbidite bed from the turbidity current flowing down from the uppermost part of the slope. The turbidite was then analyzed using a model trained on the assumption of 5 km slope to compare the reconstructed values with the original conditions.


%\documentclass{article}
%\usepackage{amsmath}
%\begin{document}

\section{Turbulence}
{\bf \Large
\begin{tabular}{ccc}
\hline
  Correnspoinding author & : & Seiya Nishizawa\\
\hline
\end{tabular}
}

\def\half{\frac{1}{2}}

\subsection{Spatial filter}

The governing euqations are the followings:
\begin{align}
  \frac{\partial\rho}{\partial t} + \frac{\partial u_i \rho}{\partial x_i}
  &= 0 \\
  \frac{\partial\rho u_i}{\partial t}
  + \frac{\partial u_j \rho u_i}{\partial x_j}
  &= -\frac{\partial p}{\partial x_i} + g \rho \delta_{i3} \\
  \frac{\partial\rho \theta}{\partial t}
  + \frac{\partial u_i \rho \theta}{\partial x_i}
  &= Q
\end{align}

Spatial filtering the continuity equation yields
\begin{equation}
  \frac{\partial \overline{\rho}}{\partial t} + \frac{\partial \overline{u_i \rho}}{\partial x_i} = 0, \label{eq: spatial filtered rho}
\end{equation}
where $\overline{\phi}$ means the spatial filtered quantity of an arbitrary variable $\phi$.
The Favre filtering (Favre 1983) defined by
\begin{equation}
  \widetilde{\phi} = \frac{\overline{\rho \phi}}{\overline{\rho}}
\end{equation}
makes the equation (\ref{eq: spatial filtered rho})
\begin{equation}
  \frac{\partial \overline{\rho}}{\partial t} + \frac{\partial \widetilde{u_i}\overline{\rho}}{\partial x_i} = 0.
\end{equation}

The momentam equations become
\begin{align}
  \frac{\partial \overline{\rho u_i}}{\partial t} + \frac{\partial \overline{u_j\rho u_i}}{\partial x_j} &= -\frac{\partial \overline{p}}{\partial x_i} + \overline{\rho} g\delta_{i3} \\
  \frac{\partial \overline{\rho}\widetilde{u_i}}{\partial t} + \frac{\partial \widetilde{u_j}\:\overline{\rho}\widetilde{u_i}}{\partial x_j} &= -\frac{\partial \overline{p}}{\partial x_i} + g\overline{\rho} \delta_{i3}
    -\frac{\partial}{\partial x_j}\left(\overline{u_i \rho u_j} - \widetilde{u_j}\overline{\rho}\widetilde{u_i}\right) \\
  \frac{\partial \overline{\rho}\widetilde{u_i}}{\partial t} + \frac{\partial \widetilde{u_j}\:\overline{\rho}\widetilde{u_i}}{\partial x_j} &= -\frac{\partial \overline{p}}{\partial x_i} + g\overline{\rho} \delta_{i3}
    -\frac{\partial}{\partial x_j}\overline{\rho}\left(\widetilde{u_i u_j} - \widetilde{u_j}\widetilde{u_i}\right).
\end{align}

As the same matter, the thermal equation becomes
\begin{equation}
  \frac{\partial \overline{\rho}\widetilde{\theta}}{\partial t}
  + \frac{\partial \widetilde{u_i}\overline{\rho}\widetilde{\theta}}{\partial x_i}
  = Q -\frac{\partial}{\partial x_i}\overline{\rho}\left(\widetilde{u_i\theta}-\widetilde{u_i}\widetilde{\theta}\right).
\end{equation}

Then, the govering equations for the prognositic variables
($\overline{\rho}, \overline{\rho}\widetilde{u_i}, $ and $\overline{\rho}\widetilde{\theta}$) are
\begin{align}
  \frac{\partial \overline{\rho}}{\partial t}
  + \frac{\partial \widetilde{u_i}\overline{\rho}}{\partial x_i} &= 0, \\
  \frac{\partial \overline{\rho}\widetilde{u_i}}{\partial t}
  + \frac{\partial \widetilde{u_j}\overline{\rho}\widetilde{u_i}}{\partial x_j}
  &= -\frac{\partial \overline{p}}{\partial x_i} + g\overline{\rho}\delta_{i3}
  -\frac{\partial \overline{\rho}\tau_{ij}}{\partial x_j}, \\
  \frac{\partial \overline{\rho}\widetilde{\theta}}{\partial t}
  + \frac{\partial \widetilde{u_i}\overline{\rho}\widetilde{\theta}}{\partial x_i}
  &= Q -\frac{\partial \overline{\rho}\tau^*_{i}}{\partial x_i},
\end{align}
where
\begin{align}
  \tau_{ij} &= \widetilde{u_iu_j}-\widetilde{u_i}\widetilde{u_j}, \\
  \tau^*_{i} &= \widetilde{u_i\theta}-\widetilde{u_i}\widetilde{\theta}.
\end{align}

\subsection{SGS model}
\subsubsection{Smagorinsky-Lilly model}
\begin{equation}
  \tau_{ij} - \frac{1}{3}\tau_{kk}\delta_{ij} = -2\nu_{SGS}\left(S_{ij}-\frac{1}{3}S_{kk}\delta_{ij}\right),
\end{equation}
where $S_{ij}$ is the strain tensor,
\begin{equation}
  S_{ij} = \frac{1}{2}\left(\frac{\partial \widetilde{u_i}}{\partial x_j} + \frac{\partial \widetilde{u_j}}{\partial x_i}\right),
  \label{eq:strain tensor}
\end{equation}
and
\begin{equation}
  \nu_{SGS} = \left(C_s\Delta\right)^2 \left|S\right|.
\end{equation}
$C_s$ is the Smagorinsky constant,
$\Delta$ is the grid spacing,
and $\left|S\right|$ is scale of the tensor $S$,
\begin{equation}
  \left|S\right| = \sqrt{2S_{ij}S_{ij}}.
  \label{eq:|S|}
\end{equation}
\begin{equation}
  \tau_{ij} = -2\nu_{SGS}\left(S_{ij}-\frac{1}{3}S_{kk}\delta_{ij}\right)
             + \frac{2}{3} TKE\delta_{ij},
  \label{eq:tau}
\end{equation}
where
\begin{equation}
  TKE = \frac{1}{2}\left(\widetilde{u_i^2} - \widetilde{u_i}^2\right)
   = \frac{1}{2}\tau_{ii}
   = \left(C_s\Delta\right)^2\left|S\right|^2.
   \label{eq:tke}
\end{equation}

\begin{equation}
  \tau^*_i = -\nu^*_{SGS} \frac{\partial \widetilde{\theta}}{\partial x_i},
  \label{eq:tau*}
\end{equation}
where
\begin{equation}
  \nu^*_{SGS} = \frac{1}{Pr}\nu_{SGS}.
\end{equation}
$Pr$ is the turbulent Prandtl number.
For the other scalar constants such as water vaper,
$\nu^*_{SGS}$ is also used as their diffusivity.

To include buoyancy effects,
the extension of the basic Smagorinsky developed by Brown et al. (1994)
is used.
\begin{equation}
  \nu_{SGS} = \lambda_r^2 |S| \sqrt{1-Rf},
\end{equation}
where $Rf$ is the flux Richardson number ($Rf = Ri/Pr$),
and $\lambda_r$ is a characteristic subgrid length scale.
$Ri$ is the Richardson number,
\begin{equation}
  Ri = \frac{N^2}{|S|^2},
  \label{eq:Ri}
\end{equation}
and $N^2$ is the Brunt-Visala frequency,
\begin{equation}
  N^2 = \frac{g}{\theta}\frac{\partial\theta}{\partial z}.
  \label{eq:N^2}
\end{equation}
The Prandtl number is an unknow parameter,
and it depends on the Richardson number,
while it is offten assumed a constant value.
For the unstable conditions ($Ri < 0$),
\begin{align}
  \nu_{SGS} &= \left(C_s\Delta\right)^2 |S| \sqrt{1 - c Ri}, \label{eq:nu unstable} \\
  \nu^*_{SGS} &= \frac{1}{Pr_N} \left(C_s\Delta\right)^2 |S| \sqrt{1 - b Ri} \label{eq:nu^* unstable},
\end{align}
where $Pr_N$ is the Prandtl number in neutral condtions.
The values of $c, b, Pr_N$ are set 16, 40, and 0.7, respectively.
Then the Prandtl number is
\begin{equation}
  Pr = Pr_N \sqrt{\frac{1-c Ri}{1-b Ri}}.
\end{equation}
For the stable condtions,
when the Richardson number is smaller than the critical Richardson number, $Ri_c (=0.25)$,
\begin{align}
  \nu_{SGS} &= \left(C_s\Delta\right)^2 |S| \left(1-\frac{Ri}{Ri_c}\right)^4, \label{eq:nu stable} \\
  \nu^*_{SGS} &= \frac{1}{Pr_N}\left(C_s\Delta\right)^2 |S| \left(1-\frac{Ri}{Ri_c}\right)^4\left(1-g Ri\right). \label{eq:nu^* stable}
\end{align}
The constant $g$ is determined as the Prandtl number becomes 1
in the limit of $Ri \to Ri_C$ and then is $(1-Pr_N)/Ri_c$.
The Prandtl number is
\begin{equation}
  Pr = Pr_N \left(1-\frac{1-Pr_N}{Ri_c}Ri\right)^{-1}.
\end{equation}
For the strongly stable condistions ($Ri > Ri_c$),
the eddy viscosity and the diffusivity for scalars are 0;
\begin{align}
  \nu_{SGS} &= 0, \label{eq:nu strong stable} \\
  \nu^*_{SGS} &= 0. \label{eq:nu^* strong stable}
\end{align}

\subsection{descretization}

\subsubsection{Time integration method}
The time integration in terms of the sub-grid scale turburence
is done by the eular scheme:
\begin{align}
  \left(\overline{\rho} \widetilde{u}\right)^{t+\Delta t}
  &= \left(\overline{\rho} \widetilde{u}\right)^t
  - \left(
    \frac{\partial \overline{\rho}\tau_{11}}{\partial x}
  + \frac{\partial \overline{\rho}\tau_{12}}{\partial y}
  + \frac{\partial \overline{\rho}\tau_{13}}{\partial z}
  \right)^t
  \Delta t, \\
  \left(\overline{\rho} \widetilde{v}\right)^{t+\Delta t}
  &= \left(\overline{\rho} \widetilde{v}\right)^t
  - \left(
    \frac{\partial \overline{\rho}\tau_{21}}{\partial x}
  + \frac{\partial \overline{\rho}\tau_{22}}{\partial y}
  + \frac{\partial \overline{\rho}\tau_{23}}{\partial z}
  \right)^t
  \Delta t, \\
  \left(\overline{\rho} \widetilde{w}\right)^{t+\Delta t}
  &= \left(\overline{\rho} \widetilde{w}\right)^t
  - \left(
    \frac{\partial \overline{\rho}\tau_{31}}{\partial x}
  + \frac{\partial \overline{\rho}\tau_{32}}{\partial y}
  + \frac{\partial \overline{\rho}\tau_{33}}{\partial z}
  \right)^t
  \Delta t, \\
  \left(\overline{\rho} \widetilde{\theta}\right)^{t+\Delta t}
  &= \left(\overline{\rho} \widetilde{\theta}\right)^t
  - \left(
    \frac{\partial \overline{\rho}\tau^*_1}{\partial x}
  + \frac{\partial \overline{\rho}\tau^*_2}{\partial y}
  + \frac{\partial \overline{\rho}\tau^*_3}{\partial z}
  \right)^t
  \Delta t.
\end{align}

\subsubsection{Spatial descretization}
We use the 4th order differnce scheme for the advection term as mentioned
in the chapter \ref{chap:descretization dynamics}.
The $\tau_{ij}$ and $\tau^*_{i}$ are propotional to
the square of the grid spacing ($\Delta^2$).
Due to the consistency with the advection term
in terms of order for spatial difference,
the second order central difference scheme
is used for the sub-grid scale turburence.
In the following part in this sub-section,
overline and tilde are ommited,
and overline, and $i,j,k$ mean
as they are in the chapter \ref{chap:descretization dynamics}.

\paragraph{Momentam equation}
The tendencies in the momentam equation related to the sub-grid scale mode are
\begin{align}
  \frac{\partial \rho u}{\partial t}_{i+\half,j,k} &=
    \frac{\rho_{i+1,j,k}\tau_{11,i+1,j,k}-\rho_{i,j,k}\tau_{11,i,j,k}}{\Delta x}
  + \frac{\overline{\rho}_{i+\half,j+\half,k}\tau_{12,i+\half,j+\half,k}-\overline{\rho}_{i+\half,j-\half,k}\tau_{12,i+\half,j-\half,k}}{\Delta y} \nonumber \\
  &+ \frac{\overline{\rho}_{i+\half,j,k+\half}\tau_{13,i+\half,j,k+\half}-\overline{\rho}_{i+\half,j,k-\half}\tau_{13,i+\half,j,k-\half}}{\Delta z}, \\
  \frac{\partial \rho v}{\partial t}_{i,j+\half,k} &=
    \frac{\overline{\rho}_{i+\half,j+\half,k}\tau_{21,i+\half,j+\half,k}-\overline{\rho}_{i-\half,j+\half,k}\tau_{21,i-\half,j+\half,k}}{\Delta x}
  + \frac{\rho_{i,j+1,k}\tau_{22,i,j+1,k}-\rho_{i,j,k}\tau_{22,i,j,k}}{\Delta y} \nonumber \\
 &+ \frac{\overline{\rho}_{i,j+\half,k+\half}\tau_{23,i,j+\half,k+\half}-\overline{\rho}_{i,j+\half,k-\half}\tau_{23,i,j+\half,k-\half}}{\Delta z}, \\
  \frac{\partial \rho w}{\partial t}_{i,j,k+\half} &=
    \frac{\overline{\rho}_{i+\half,j,k+\half}\tau_{31,i+\half,j,k+\half}-\overline{\rho}_{i-\half,j,k+\half}\tau_{31,i-\half,j,k+\half}}{\Delta x}
  + \frac{\overline{\rho}_{i,j+\half,k+\half}\tau_{32,i,j+\half,k+\half}-\overline{\rho}_{i,j-\half,k+\half}\tau_{32,i,j-\half,k+\half}}{\Delta y} \nonumber \\
 &+ \frac{\rho_{i,j,k+1}\tau_{33,i,j,k+1}-\rho_{i,j,k}\tau_{33,i,j,k}}{\Delta z}.
\end{align}
The $\overline{\rho}$ is
\begin{align}
  \overline{\rho}_{i,j+\half,k+\half} &=
  \frac{\rho_{i,j+1,k+1} + \rho_{i,j+1,k} + \rho_{i,j,k+1} + \rho_{i,j,k}}{4}, \\
  \overline{\rho}_{i+\half,j,k+\half} &=
  \frac{\rho_{i+1,j,k+1} + \rho_{i+1,j,k} + \rho_{i,j,k+1} + \rho_{i,j,k}}{4}, \\
  \overline{\rho}_{i+\half,j+\half,k} &=
  \frac{\rho_{i+1,j+1,k} + \rho_{i+1,j,k} + \rho_{i,j+1,k} + \rho_{i,j,k}}{4}.
\end{align}
The $\tau$ is calculated from the strain tensor and $N^2$ at each point,
from eq.(\ref{eq:tau}),(\ref{eq:tke}), (\ref{eq:nu unstable}), (\ref{eq:nu stable}), (\ref{eq:nu strong stable}), (\ref{eq:|S|}), and (\ref{eq:Ri}).

\paragraph{Thermal equation}
The tendency in the thermal equation related to the sub-grid scale model is
\begin{align}
  \frac{\partial \rho \theta}{\partial t}_{i,j,k} &=
  - \frac{\overline{\rho}_{i+\half,j,k}\tau^*_{1,i+\half,j,k}-\overline{\rho}_{i-\half,j,k}\tau^*_{1,i-\half,j,k}}{\Delta x}
  - \frac{\overline{\rho}_{i,j+\half,k}\tau^*_{2,i,j+\half,k}-\overline{\rho}_{i,j-\half,k}\tau^*_{2,i,j-\half,k}}{\Delta y} \nonumber \\
  &- \frac{\overline{\rho}_{i,j,k+\half}\tau^*_{2,i,j,k+\half}-\overline{\rho}_{i,j,k-\half}\tau^*_{3,i,j,k-\half}}{\Delta z}
\end{align}
The $\overline{\rho}$ at half-level is eq.(\ref{eq:rho half i})-(\ref{eq:rho half k}),
the $\nu^*_{SGS}$ is calculated from the strain tensor and the $N^2$ with eq.(\ref{eq:nu^* unstable}),(\ref{eq:nu^* stable}), (\ref{eq:nu^* strong stable}), (\ref{eq:|S|}, and (\ref{eq:Ri}).
The $\tau^*$ at half-level
is proportional to spatial difference of potential temperature
(eq.\ref{eq:tau*}),
which requires potential temperature at full-level (eq.\ref{eq:theta full}):
\begin{align}
  \tau^*_{1,i+\half,j,k} &= -\nu^*_{SGS} \frac{\theta_{i+1,j,k}-\theta_{i,j,k}}{\Delta x}, \\
  \tau^*_{2,i,j+\half,k} &= -\nu^*_{SGS} \frac{\theta_{i,j+1,k}-\theta_{i,j,k}}{\Delta y}, \\
  \tau^*_{3,i,j,k+\half} &= -\nu^*_{SGS} \frac{\theta_{i,j,k+1}-\theta_{i,j,k}}{\Delta x}. \\
\end{align}

\paragraph{Strain tensor}
The strain tensor, eq.(\ref{eq:strain tensor}), have to be calculated
at full-level (grid cell center) and edge center for the momentum equations,
and at hafl-level (plane center) for the thermal equation;

\begin{itemize}
  \item cell center ($i,j,k$)
    \begin{align}
      S_{11,i,j,k} &= \left\{
      \frac{\overline{u}_{i+\frac{1}{2},j,k}-\overline{u}_{i-\frac{1}{2},j,k}}{\Delta x}
      \right\}, \\
      S_{22,i,j,k} &= \left\{
      \frac{\overline{v}_{i,j+\frac{1}{2},k}-\overline{v}_{i,j-\frac{1}{2},k}}{\Delta y}
      \right\}, \\
      S_{33,i,j,k} &= \left\{
      \frac{\overline{w}_{i,j,k+\frac{1}{2}}-\overline{v}_{i,j,k-\frac{1}{2}}}{\Delta z}
      \right\}, \\
      S_{12,i,j,k} = S_{21,i,j,k} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i,j+\frac{1}{2},k}-\overline{u}_{i,j-\frac{1}{2},k}}{\Delta y}
     +\frac{\overline{v}_{i+\frac{1}{2},j,k}-\overline{v}_{i-\frac{1}{2},j,k}}{\Delta x}
      \right\}, \\
      S_{23,i,j,k} = S_{32,i,j,k} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i,j,k+\frac{1}{2}}-\overline{v}_{i,j,k-\frac{1}{2}}}{\Delta z}
     +\frac{\overline{w}_{i,j+\frac{1}{2},k}-\overline{w}_{i,j-\frac{1}{2},k}}{\Delta y}
      \right\}, \\
      S_{31,i,j,k} = S_{13,i,j,k} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+\frac{1}{2},j,k}-\overline{w}_{i-\frac{1}{2},j,k}}{\Delta x}
     +\frac{\overline{u}_{i,j,k+\frac{1}{2}}-\overline{u}_{i,j,k-\frac{1}{2}}}{\Delta z}
      \right\}.
    \end{align}

  \item $x$-$y$ plane center ($i,j,k+\frac{1}{2}$)
    \begin{align}
      S_{11,i,j,k+\frac{1}{2}} &= \left\{
      \frac{\overline{u}_{i+\frac{1}{2},j,k+\frac{1}{2}}-\overline{u}_{i-\frac{1}{2},j,k+\frac{1}{2}}}{\Delta x}
      \right\}, \\
      S_{22,i,j,k+\frac{1}{2}} &= \left\{
      \frac{\overline{v}_{i,j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i,j-\frac{1}{2},k+\frac{1}{2}}}{\Delta y}
      \right\}, \\
      S_{33,i,j,k+\frac{1}{2}} &= \left\{
      \frac{\overline{w}_{i,j,k+1}-\overline{v}_{i,j,k}}{\Delta z}
      \right\}, \\
      S_{12,i,j,k+\frac{1}{2}} = S_{21,i,j,k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i,j+\frac{1}{2},k+\frac{1}{2}}-\overline{u}_{i,j-\frac{1}{2},k+\frac{1}{2}}}{\Delta y}
     +\frac{\overline{v}_{i+\frac{1}{2},j,k+\frac{1}{2}}-\overline{v}_{i-\frac{1}{2},j,k+\frac{1}{2}}}{\Delta x}
      \right\}, \\
      S_{23,i,j,k+\frac{1}{2}} = S_{32,i,j,k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i,j,k+1}-\overline{v}_{i,j,k}}{\Delta z}
     +\frac{\overline{w}_{i,j+\frac{1}{2},k+\frac{1}{2}}-\overline{w}_{i,j-\frac{1}{2},k+\frac{1}{2}}}{\Delta y}
      \right\}, \\
      S_{31,i,j,k+\frac{1}{2}} = S_{13,i,j,k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+\frac{1}{2},j,k+\frac{1}{2}}-\overline{w}_{i-\frac{1}{2},j,k+\frac{1}{2}}}{\Delta x}
     +\frac{\overline{u}_{i,j,k+1}-\overline{u}_{i,j,k}}{\Delta z}
      \right\}.
    \end{align}

    \item $y$-$z$ plane center ($i+\frac{1}{2},j,k$)
    \begin{align}
      S_{11,i+\frac{1}{2},j,k} &= \left\{
      \frac{\overline{u}_{i+1,j,k}-\overline{u}_{i,j,k}}{\Delta x}
      \right\}, \\
      S_{22,i+\frac{1}{2},j,k} &= \left\{
      \frac{\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k}-\overline{v}_{i+\frac{1}{2},j-\frac{1}{2},k}}{\Delta y}
      \right\}, \\
      S_{33,i+\frac{1}{2},j,k} &= \left\{
      \frac{\overline{w}_{i+\frac{1}{2},j,k+\frac{1}{2}}-\overline{v}_{i+\frac{1}{2},j,k-\frac{1}{2}}}{\Delta z}
      \right\}, \\
      S_{12,i+\frac{1}{2},j,k} = S_{21,i+\frac{1}{2},j,k} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i+\frac{1}{2},j+\frac{1}{2},k}-\overline{u}_{i+\frac{1}{2},j-\frac{1}{2},k}}{\Delta y}
     +\frac{\overline{v}_{i+1,j,k}-\overline{v}_{i,j,k}}{\Delta x}
      \right\}, \\
      S_{23,i+\frac{1}{2},j,k} = S_{32,i+\frac{1}{2},j,k} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i+\frac{1}{2},j,k+\frac{1}{2}}-\overline{v}_{i+\frac{1}{2},j,k-\frac{1}{2}}}{\Delta z}
     +\frac{\overline{w}_{i+\frac{1}{2},j+\frac{1}{2},k}-\overline{w}_{i+\frac{1}{2},j-\frac{1}{2},k}}{\Delta y}
      \right\}, \\
      S_{31,i+\frac{1}{2},j,k} = S_{13,i+\frac{1}{2},j,k} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+1,j,k}-\overline{w}_{i,j,k}}{\Delta x}
     +\frac{\overline{u}_{i+\frac{1}{2},j,k+\frac{1}{2}}-\overline{u}_{i+\frac{1}{2},j,k-\frac{1}{2}}}{\Delta z}
      \right\}.
    \end{align}

  \item $z$-$x$ plane center ($i,j+\frac{1}{2},k$)
    \begin{align}
      S_{11,i,j+\frac{1}{2},k} &= \left\{
      \frac{\overline{u}_{i+\frac{1}{2},j+\frac{1}{2},k}-\overline{u}_{i-\frac{1}{2},j+\frac{1}{2},k}}{\Delta x}
      \right\}, \\
      S_{22,i,j+\frac{1}{2},k} &= \left\{
      \frac{\overline{v}_{i,j+1,k}-\overline{v}_{i,j,k}}{\Delta y}
      \right\}, \\
      S_{33,i,j+\frac{1}{2},k} &= \left\{
      \frac{\overline{w}_{i,j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i,j+\frac{1}{2},k-\frac{1}{2}}}{\Delta z}
      \right\}, \\
      S_{12,i,j+\frac{1}{2},k} = S_{21,i,j+\frac{1}{2},k} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i,j+1,k}-\overline{u}_{i,j,k}}{\Delta y}
     +\frac{\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k}-\overline{v}_{i-\frac{1}{2},j+\frac{1}{2},k}}{\Delta x}
      \right\}, \\
      S_{23,i,j+\frac{1}{2},k} = S_{32,i,j+\frac{1}{2},k} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i,j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i,j+\frac{1}{2},k-\frac{1}{2}}}{\Delta z}
     +\frac{\overline{w}_{i,j+1,k}-\overline{w}_{i,j,k}}{\Delta y}
      \right\}, \\
      S_{31,i,j+\frac{1}{2},k} = S_{13,i,j+\frac{1}{2},k} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+\frac{1}{2},j+\frac{1}{2},k}-\overline{w}_{i-\frac{1}{2},j+\frac{1}{2},k}}{\Delta x}
     +\frac{\overline{u}_{i,j+\frac{1}{2},k+\frac{1}{2}}-\overline{u}_{i,j+\frac{1}{2},k-\frac{1}{2}}}{\Delta z}
      \right\}.
    \end{align}

  \item $x$ edge center ($i,j+\frac{1}{2},k+\frac{1}{2}$)
    \begin{align}
      S_{11,i,j+\frac{1}{2},k+\frac{1}{2}} &= \left\{
      \frac{\overline{u}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{u}_{i-\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}}{\Delta x}
      \right\}, \\
      S_{22,i,j+\frac{1}{2},k+\frac{1}{2}} &= \left\{
      \frac{\overline{v}_{i,j+1,k+\frac{1}{2}}-\overline{v}_{i,j,k+\frac{1}{2}}}{\Delta y}
      \right\}, \\
      S_{33,i,j+\frac{1}{2},k+\frac{1}{2}} &= \left\{
      \frac{\overline{w}_{i,j+\frac{1}{2},k+1}-\overline{v}_{i,j+\frac{1}{2},k}}{\Delta z}
      \right\}, \\
      S_{12,i,j+\frac{1}{2},k+\frac{1}{2}} = S_{21,i,j+\frac{1}{2},k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i,j+1,k+\frac{1}{2}}-\overline{u}_{i,j,k+\frac{1}{2}}}{\Delta y}
     +\frac{\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i-\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}}{\Delta x}
      \right\}, \\
      S_{23,i,j+\frac{1}{2},k+\frac{1}{2}} = S_{32,i,j+\frac{1}{2},k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i,j+\frac{1}{2},k+1}-\overline{v}_{i,j+\frac{1}{2},k}}{\Delta z}
     +\frac{\overline{w}_{i,j+1,k+\frac{1}{2}}-\overline{w}_{i,j,k+\frac{1}{2}}}{\Delta y}
      \right\}, \\
      S_{31,i,j+\frac{1}{2},k+\frac{1}{2}} = S_{13,i,j+\frac{1}{2},k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{w}_{i-\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}}{\Delta x}
     +\frac{\overline{u}_{i,j+\frac{1}{2},k+1}-\overline{u}_{i,j+\frac{1}{2},k}}{\Delta z}
      \right\}.
    \end{align}

  \item $y$ edge center ($i+\frac{1}{2},j,k+\frac{1}{2}$)
    \begin{align}
      S_{11,i+\frac{1}{2},j,k+\frac{1}{2}} &= \left\{
      \frac{\overline{u}_{i+1,j,k+\frac{1}{2}}-\overline{u}_{i,j,k+\frac{1}{2}}}{\Delta x}
      \right\}, \\
      S_{22,i+\frac{1}{2},j,k+\frac{1}{2}} &= \left\{
      \frac{\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i+\frac{1}{2},j-\frac{1}{2},k+\frac{1}{2}}}{\Delta y}
      \right\}, \\
      S_{33,i+\frac{1}{2},j,k+\frac{1}{2}} &= \left\{
      \frac{\overline{w}_{i+\frac{1}{2},j,k+1}-\overline{v}_{i+\frac{1}{2},j,k}}{\Delta z}
      \right\}, \\
      S_{12,i+\frac{1}{2},j,k+\frac{1}{2}} = S_{21,i+\frac{1}{2},j,k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{u}_{i+\frac{1}{2},j-\frac{1}{2},k+\frac{1}{2}}}{\Delta y}
     +\frac{\overline{v}_{i+1,j,k+\frac{1}{2}}-\overline{v}_{i,j,k+\frac{1}{2}}}{\Delta x}
      \right\}, \\
      S_{23,i+\frac{1}{2},j,k+\frac{1}{2}} = S_{32,i+\frac{1}{2},j,k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i+\frac{1}{2},j,k+1}-\overline{v}_{i+\frac{1}{2},j,k}}{\Delta z}
     +\frac{\overline{w}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{w}_{i+\frac{1}{2},j-\frac{1}{2},k+\frac{1}{2}}}{\Delta y}
      \right\}, \\
      S_{31,i+\frac{1}{2},j,k+\frac{1}{2}} = S_{13,i+\frac{1}{2},j,k+\frac{1}{2}} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+1,j,k+\frac{1}{2}}-\overline{w}_{i,j,k+\frac{1}{2}}}{\Delta x}
     +\frac{\overline{u}_{i+\frac{1}{2},j,k+1}-\overline{u}_{i+\frac{1}{2},j,k}}{\Delta z}
      \right\}.
    \end{align}

  \item $z$ edge center ($i+\frac{1}{2},j+\frac{1}{2},k$)
    \begin{align}
      S_{11,i+\frac{1}{2},j+\frac{1}{2},k} &= \left\{
      \frac{\overline{u}_{i+1,j+\frac{1}{2},k}-\overline{u}_{i,j+\frac{1}{2},k}}{\Delta x}
      \right\}, \\
      S_{22,i+\frac{1}{2},j+\frac{1}{2},k} &= \left\{
      \frac{\overline{v}_{i+\frac{1}{2},j+1,k}-\overline{v}_{i+\frac{1}{2},j,k}}{\Delta y}
      \right\}, \\
      S_{33,i+\frac{1}{2},j+\frac{1}{2},k} &= \left\{
      \frac{\overline{w}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k-\frac{1}{2}}}{\Delta z}
      \right\}, \\
      S_{12,i+\frac{1}{2},j+\frac{1}{2},k} = S_{21,i+\frac{1}{2},j+\frac{1}{2},k} &= \frac{1}{2}\left\{
      \frac{\overline{u}_{i+\frac{1}{2},j+1,k}-\overline{u}_{i+\frac{1}{2},j,k}}{\Delta y}
     +\frac{\overline{v}_{i+1,j+\frac{1}{2},k}-\overline{v}_{i,j+\frac{1}{2},k}}{\Delta x}
      \right\}, \\
      S_{23,i+\frac{1}{2},j+\frac{1}{2},k} = S_{32,i+\frac{1}{2},j+\frac{1}{2},k} &= \frac{1}{2}\left\{
      \frac{\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{v}_{i+\frac{1}{2},j+\frac{1}{2},k-\frac{1}{2}}}{\Delta z}
     +\frac{\overline{w}_{i+\frac{1}{2},j+1,k}-\overline{w}_{i+\frac{1}{2},j,k}}{\Delta y}
      \right\}, \\
      S_{31,i+\frac{1}{2},j+\frac{1}{2},k} = S_{13,i+\frac{1}{2},j+\frac{1}{2},k} &= \frac{1}{2}\left\{
      \frac{\overline{w}_{i+1,j+\frac{1}{2},k}-\overline{w}_{i,j+\frac{1}{2},k}}{\Delta x}
     +\frac{\overline{u}_{i+\frac{1}{2},j+\frac{1}{2},k+\frac{1}{2}}-\overline{u}_{i+\frac{1}{2},j+\frac{1}{2},k-\frac{1}{2}}}{\Delta z}
      \right\}.
    \end{align}
\end{itemize}

\paragraph{velocity}
Calculattion of the strain tensor
requires value of velocity
at cell center, plane center, edge center, and vertex.
The velocities at cell center (full-level) are eq.(\ref{eq:u full}-\ref{eq:w full}).

\begin{itemize}
  \item $x$-$y$ plane center ($i,j,k+\frac{1}{2}$)
    \begin{align}
      \overline{u}_{i,j,k+\frac{1}{2}} &=
      \frac{\overline{u}_{i,j,k+1}+\overline{u}_{i,j,k}}{2}, \\
      \overline{v}_{i,j,k+\frac{1}{2}} &=
      \frac{\overline{v}_{i,j,k+1}+\overline{v}_{i,j,k}}{2}, \\
      \overline{w}_{i,j,k+\frac{1}{2}} &=
      \frac{(\rho w)_{i,j,k+\frac{1}{2}}}{\overline{\rho}_{i,j,k+\frac{1}{2}}}.
    \end{align}

  \item $y$-$z$ plane center ($i+\frac{1}{2},j,k$)
    \begin{align}
      \overline{u}_{i+\frac{1}{2},j,k} &=
      \frac{(\rho u)_{i+\frac{1}{2},j,k}}{\overline{\rho}_{i+\frac{1}{2},j,k}}, \\
      \overline{v}_{i+\frac{1}{2},j,k} &=
      \frac{\overline{v}_{i+1,j,k}+\overline{v}_{i,j,k}}{2}, \\
      \overline{w}_{i+\frac{1}{2},j,k} &=
      \frac{\overline{w}_{i+1,j,k}+\overline{w}_{i,j,k}}{2}.
    \end{align}

  \item $z$-$x$ plane center ($i,j+\frac{1}{2},k$)
    \begin{align}
      \overline{u}_{i,j+\frac{1}{2},k} &=
      \frac{\overline{u}_{i,j+1,k}+\overline{u}_{i,j,k}}{2}, \\
      \overline{v}_{i,j+\frac{1}{2},k} &=
      \frac{(\rho v)_{i,j+\frac{1}{2},k}}{\overline{\rho}_{i,j+\frac{1}{2},k}}, \\
      \overline{w}_{i,j+\frac{1}{2},k} &=
      \frac{\overline{w}_{i,j+1,k}+\overline{w}_{i,j,k}}{2}.
    \end{align}

  \item $x$ edge center ($i,j+\frac{1}{2},k+\half$)
    \begin{align}
      \overline{u}_{i,j+\half,k+\half} &=
      \frac{\overline{u}_{i,j+1,k+1}+\overline{u}_{i,j+1,k}+\overline{u}_{i,j,k+1}+\overline{u}_{i,j,k}}{4}, \\
      \overline{v}_{i,j+\half,k+\half} &=
      \frac{\overline{v}_{i,j+\half,k+1}+\overline{v}_{i,j+\half,k}}{2}, \\
      \overline{w}_{i,j+\half,k+\half} &=
      \frac{\overline{w}_{i,j+1,k+\half}+\overline{w}_{i,j,k+\half}}{2}.
    \end{align}

  \item $y$ edge center ($i+\half,j,k+\half$)
    \begin{align}
      \overline{u}_{i+\half,j,k+\half} &=
      \frac{\overline{u}_{i+\half,j,k+1}+\overline{u}_{i+\half,j,k}}{2}, \\
      \overline{v}_{i+\half,j,k+\half} &=
      \frac{\overline{v}_{i+1,j,k+1}+\overline{v}_{i+1,j,k}+\overline{v}_{i,j,k+1}+\overline{v}_{i,j,k}}{4}, \\
      \overline{w}_{i+\half,j,k+\half} &=
      \frac{\overline{w}_{i+1,j,k+\half}+\overline{w}_{i,j,k+\half}}{2}.
    \end{align}

  \item $z$ edge center ($i+\half,j+\half,k$)
    \begin{align}
      \overline{u}_{i+\half,j+\half,k} &=
      \frac{\overline{u}_{i+\half,j+1,k}+\overline{u}_{i+\half,j,k}}{2}, \\
      \overline{v}_{i+\half,j+\half,k} &=
      \frac{\overline{v}_{i+1,j+\half,k}+\overline{v}_{i,j+\half,k}}{2}, \\
      \overline{w}_{i+\half,j+\half,k} &=
      \frac{\overline{w}_{i+1,j+1,k}+\overline{w}_{i+1,j,k}+\overline{w}_{i,j+1,k}+\overline{w}_{i,j,k}}{4}.
    \end{align}

  \item vertex ($i+\half,j+\half,k+\half$)
    \begin{align}
      \overline{u}_{i+\half,j+\half,k+\half} &=
      \frac{\overline{u}_{i+\half,j+1,k+1}+\overline{u}_{i+\half,j+1,k}+\overline{u}_{i+\half,j,k+1}+\overline{u}_{i+\half,j,k}}{4}, \\
      \overline{v}_{i+\half,j+\half,k+\half} &=
      \frac{\overline{v}_{i+1,j+\half,k+1}+\overline{v}_{i+1,j+\half,k}+\overline{v}_{i,j+\half,k+1}+\overline{v}_{i,j+\half,k}}{4}, \\
      \overline{w}_{i+\half,j+\half,k+\half} &=
      \frac{\overline{w}_{i+1,j+1,k+\half}+\overline{w}_{i+1,j,k+\half}+\overline{w}_{i,j+1,k+\half}+\overline{w}_{i,j,k+\half}}{4}.
    \end{align}
\end{itemize}

\paragraph{Brunt-Visala frequency}
The Brunt-Visala frequency, $N^2$ is required to calculate the Richardson number
at cell center and edge center for the momentum equations,
and plane center for the thermal equation.

\begin{itemize}
  \item cell center ($i,j,k$)
    \begin{equation}
      N^2_{i,j,k} = \frac{g}{\theta_{i,j,k}}
      \frac{\theta_{i,j,k+1}-\theta_{i,j,k-1}}{2\Delta z}.
    \end{equation}

  \item $x$-$y$ plane center ($i,j,k+\half$)
    \begin{equation}
      N^2_{i,j,k+\half} = \frac{2g}{\theta_{i,j,k+1}+\theta_{i,j,k}}
      \frac{\theta_{i,j,k+1}-\theta_{i,j,k}}{\Delta z}.
    \end{equation}

  \item $y$-$z$ plane center ($i+\half,j,k$)
    \begin{equation}
      N^2_{i+\half,j,k} = \frac{2g}{\theta_{i+1,j,k}+\theta_{i,j,k}}
      \frac{(\theta_{i+1,j,k+1}+\theta_{i,j,k+1})-(\theta_{i+1,j,k-1}+\theta_{i,j,k-1})}{4\Delta z}.
    \end{equation}

  \item $z$-$x$ plane center ($i,j+\half,k$)
    \begin{equation}
      N^2_{i,j+\half,k} = \frac{2g}{\theta_{i,j+1,k}+\theta_{i,j,k}}
      \frac{(\theta_{i,j+1,k+1}+\theta_{i,j,k+1})-(\theta_{i,j+1,k-1}+\theta_{i,j,k-1})}{4\Delta z}.
    \end{equation}

  \item $x$ edge center ($i,j+\half,k+\half$)
    \begin{equation}
      N^2_{i,j+\half,k+\half} = \frac{4g}{\theta_{i,j+1,k+1}+\theta_{i,j+1,k}+\theta_{i,j,k+1}+\theta_{i,j,k}}
      \frac{(\theta_{i,j+1,k+1}+\theta_{i,j,k+1})-(\theta_{i,j+1,k}+\theta_{i,j,k})}{2\Delta z}.
    \end{equation}

  \item $y$ edge center ($i+\half,j,k+\half$)
    \begin{equation}
      N^2_{i+\half,j,k+\half} = \frac{4g}{\theta_{i+1,j,k+1}+\theta_{i+1,j,k}+\theta_{i,j,k+1}+\theta_{i,j,k}}
      \frac{(\theta_{i+1,j,k+1}+\theta_{i,j,k+1})-(\theta_{i+1,j,k}+\theta_{i,j,k})}{2\Delta z}.
    \end{equation}

  \item $z$ edge center ($i+\half,j+\half,k$)
    \begin{equation}
      N^2_{i+\half,j+\half,k} = \frac{4g}{\theta_{i+1,j+1,k}+\theta_{i+1,j,k}+\theta_{i,j+1,k}+\theta_{i,j,k}}
      \frac{(\theta_{i+1,j+1,k+1}+\theta_{i+1,j,k+1}+\theta_{i,j+1,k+1}+\theta_{i,j,k+1})-(\theta_{i+1,j+1,k-1}+\theta_{i+1,j,k-1}+\theta_{i,j+1,k-1}+\theta_{i,j,k-1})}{8\Delta z}.
    \end{equation}

\end{itemize}

%\end{document}