%\documentclass[12pt]{article}
\documentclass[12pt,landscape]{article}

\include{preamble}

\newcommand{\instr}{\small Your answer will consist of a lowercase string (e.g. \texttt{aebgd}) where the order of the letters does not matter. \normalsize}

\title{Math 369 / 650 Fall \the\year{} \\ Midterm Examination Two}
\author{Professor Adam Kapelner}

\date{Wednesday, November 11, \the\year{}}

\begin{document}
\maketitle

%\noindent Full Name \line(1,0){410}

\thispagestyle{empty}

\section*{Code of Academic Integrity}

\footnotesize
Since the college is an academic community, its fundamental purpose is the pursuit of knowledge. Essential to the success of this educational mission is a commitment to the principles of academic integrity. Every member of the college community is responsible for upholding the highest standards of honesty at all times. Students, as members of the community, are also responsible for adhering to the principles and spirit of the following Code of Academic Integrity.

Activities that have the effect or intention of interfering with education, pursuit of knowledge, or fair evaluation of a student's performance are prohibited. Examples of such activities include but are not limited to the following definitions:

\paragraph{Cheating} Using or attempting to use unauthorized assistance, material, or study aids in examinations or other academic work or preventing, or attempting to prevent, another from using authorized assistance, material, or study aids. Example: using an unauthorized cheat sheet in a quiz or exam, altering a graded exam and resubmitting it for a better grade, etc.
\\

\noindent By taking this exam, you acknowledge and agree to uphold this Code of Academic Integrity. \\

%\begin{center}
%\line(1,0){250} ~~~ \line(1,0){100}\\
%~~~~~~~~~~~~~~~~~~~~~signature~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date
%\end{center}

\normalsize

\section*{Instructions}

This exam is 75 minutes (variable time per question) and closed-book. You are allowed \textbf{one} page (front and back) of a \qu{cheat sheet}, blank scrap paper and a graphing calculator. Please read the questions carefully. No food is allowed, only drinks. %If the question reads \qu{compute,} this means the solution will be a number otherwise you can leave the answer in \textit{any} widely accepted mathematical notation which could be resolved to an exact or approximate number with the use of a computer. I advise you to skip problems marked \qu{[Extra Credit]} until you have finished the other questions on the exam, then loop back and plug in all the holes. I also advise you to use pencil. The exam is 100 points total plus extra credit. Partial credit will be granted for incomplete answers on most of the questions. \fbox{Box} in your final answers. Good luck!

\pagebreak

\problem\timedsection{5} These are questions on method of moments estimators.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{11}

\begin{enumerate}[(a)]
\item For any DGP, $\thetahatmm$ will be a function of the data.
\item For any DGP, $\thetahatmm$ will be a function of the data and of $\theta$.
\item For any DGP, its fifth moment is $\expe{X^5}$ if it exists.
\item For any iid DGP with a finite expectation, $\thetahat = \Xbar := \oneover{n}\sum_{i=1}^n X_i$ is an MM estimator for the DGP's expectation.
\item For any iid DGP with a finite variance, $\thetahat = \sigsqhat := \oneover{n}\sum_{i=1}^n (X_i - \Xbar)^2$ is an MM estimator for the DGP's variance.
\item Let $\thetahatmm$ be an estimator for $\theta$. Its estimate $\thetahathatmm$ must be a legal value in the parameter space of $\theta$.
\item For an iid DGP with a finite variance where $\theta := \expe{X} / \sd{X}$, then $\thetahatmm = \Xbar / \sqrt{\sigsqhat}$ where $\Xbar$ and $\sigsqhat$ are defined in (d) and (e).\\

Consider a DPG $\Xoneton \iid \text{ShiftedParetoI}(1, \theta) := \theta (x + 1)^{-\theta - 1}$ which has support in positive numbers only. Using calculus you can show that $\expe{X} = \frac{1}{\theta - 1}$.
\item $\thetahatmm = \Xbar$
\item $\thetahatmm = 1 / \Xbar$
\item $\thetahatmm = 1 / \Xbar + 1$
\item $\thetahatmm = (\Xbar + 1) / \Xbar$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{13} Consider the DPG from the previous problem, $\iid \text{ShiftedParetoI}(1, \theta) := \theta (x + 1)^{-\theta - 1}$ which has support in positive numbers only and consider a dataset $\xoneton$ to be realizations from this DGP.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{19}

\begin{enumerate}[(a)]
\item The likelihood for all the data is $=\theta (\xoneton + 1)^{-\theta - 1}$.
\item The likelihood for all the data is $=\prod_{i=1}^n \theta (x_i + 1)^{-\theta - 1}$.
\item The log likelihood for all the data is $=\prod_{i=1}^n \theta (\natlog{x_i} + 1)^{-\theta - 1}$.
\item The log likelihood for all the data is $=\natlog{\prod_{i=1}^n \theta (x_i + 1)^{-\theta - 1}}$.
\item The log likelihood for all the data is $=n\natlog{\theta} + \natlog{(-\theta - 1)\prod_{i=1}^n  (x_i + 1)}$.
\item The log likelihood for all the data is $=n\natlog{\theta} + (-\theta - 1) \sum_{i=1}^n  \natlog{x_i + 1}$.
\item The score function for all the data is the derivative of the log likelihood function.
\item The deriviative of the log likelihood for all the data is $=\frac{n}{\theta} - \sum_{i=1}^n  \natlog{x_i + 1}$.
\item Assuming (h) is true, $\thetahathatmle = n \Big/ \sum_{i=1}^n  \natlog{x_i + 1}$.
\item The Fisher information for this DGP is $I(\theta) = 1$.
\item The Fisher information for this DGP is $I(\theta) = n / \theta^2$.
\item The Fisher information for this DGP is $I(\theta) = -1 / \theta^2$.
\item The Fisher information for this DGP is $I(\theta) = 1 / \theta^2$.
\item The variance of any unbiased estimator for $\theta$ for this DGP must be at least $1 / n$.
\item The variance of any unbiased estimator for $\theta$ for this DGP must be at least $\theta / n$.
\item The variance of any unbiased estimator for $\theta$ for this DGP must be at least $\theta^2 / n$.
\item To show that $\thetahatmle$ is/isn't the UMVUE for $\theta$ involves simple algebra / calculus on information available to you on this page.
\item To show that $\thetahatmle$ is/isn't the UMVUE for $\theta$ involves a lot of algebra / calculus but the information you require is available to you on this page.
\item To show that $\thetahatmle$ is/isn't the UMVUE for $\theta$ is impossible given the information available to you on this page.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{6} \ingray{Consider the DPG from the previous problem, $\iid \text{ShiftedParetoI}(1, \theta) := \theta (x + 1)^{-\theta - 1}$ which has support in positive numbers only and consider a dataset $\xoneton$ to be realizations from this DGP.} Let $\sigsq$ denote the variance of this DGP model. You can show that $\thetahathatmle = n \Big/ \sum_{i=1}^n  \natlog{x_i + 1}$ and $I(\theta) = 1 / \theta^2$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{13}

\begin{enumerate}[(a)]
\item $\var{\thetahatmle} = \theta^2 / n$.
\item $\thetahatmle$ is normally distributed.
\item $\thetahatmle$ is distributed as a Student's $t$ distribution.
\item $\thetahatmle$ is an asymptotically normally estimator.
\item $\thetahatmle \approxdist \normnot{0}{1}$.
\item $\thetahatmle \approxdist \normnot{\theta}{\sigsq / n}$.
\item $\thetahatmle \approxdist \normnot{\theta}{\oneover{n\theta^2}}$.
\item You can use the fact in (g) to create a confidence interval for $\theta$ (i.e. a function of $\xoneton$ and constants).
\item $\thetahatmle \approxdist \normnot{\theta}{\squared{\oversqrtn{\theta}}}$.
\item You can use the fact in (i) to create a confidence interval for $\theta$ (i.e. a function of $\xoneton$ and constants).
\item $\thetahatmle \approxdist \normnot{\thetahathatmle}{\squared{\oversqrtn{\thetahathatmle}}}$.
\item You can use the fact in (k) to create a confidence interval for $\theta$ (i.e. a function of $\xoneton$ and constants).
\item $\thetahatmle$ can provide arbitrary precision to measure $\theta$ given a higher sample size $n$.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

%\problem\timedsection{4} Consider the DGP $\Xoneton \iid U(0, \theta)$. We showed in class that $\thetahatmm = 2\Xbar$ and $\thetahatmle = \max{\Xoneton}$.
%
%\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{5}
%
%\begin{enumerate}[(a)]
%\item $\thetahathatmm > \thetahathatmle$.
%\item $\bias{\thetahatmm} = 0$.
%\item $\var{\thetahatmm} > \var{\thetahatmle}$.
%\item $\mse{\thetahatmm} > \mse{\thetahatmle}$.
%\end{enumerate}
%\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{6} Consider $\xoneton$ to be realizations from an iid DGP, $\thetahat$ to be an unbiased estimator for $\theta$ and assume the conditions needed to prove the Cramer-Rao Lower Bound (CRLB). A quantity primed (e.g. $\ell'$) denotes differentiation with respect to $\theta$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{7}

\begin{enumerate}[(a)]
\item ${\ell'\parens{\thetahat; \xoneton}} = 0$
\item $\expe{\ell\parens{\theta; \Xoneton}} = 0$
\item $\expe{\ell\parens{\theta; \Xoneton}} = n\expe{\ell\parens{\theta; X}}$
\item $\expe{\ell\parens{\theta; \Xoneton}^2} = 0$
\item $\expe{\ell'\parens{\theta; \Xoneton}^2} = 0$
\item $\partialop{\theta}{\expe{\ell\parens{\theta; \Xoneton}}} = 0$
\item $\var{\ell\parens{\theta; \Xoneton}^2} = 0$
\item $\var{\ell'\parens{\theta; \Xoneton}^2} = nI(\theta)$
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

%\problem\timedsection{4} These are some questions about some theorems we discussed in class. Let $X_1, X_2, ... $ be rv's and $a_1, a_2, ...$ be constants.
%
%\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{5}
%
%\begin{enumerate}[(a)]
%\item If $X_1 \convp a_1$, then $\natlog{X_1} \convp \natlog{a_1}$.
%\end{enumerate}
%\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{5} These are some questions about some theorems we discussed in class. Let $X_1, X_2, ... $ be rv's indexed also by $n$, the sample size, and $a_1, a_2, ...$ be positive constants.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{6}

\begin{enumerate}[(a)]
\item If $X_1 \convp a_1$, then $\natlog{X_1} \convp \natlog{a_1}$.
\item If $X_1 \convp a_1$, then $\displaystyle\oneover{X_1} \convp \oneover{a_1}$.
\item If $X_1 \convp a_1$ and $\displaystyle \frac{X_2 - \mu}{a_1} \convd \stdnormnot$ then $\displaystyle\frac{X_2 - \mu}{X_1} \convd \stdnormnot$
\item If $\displaystyle \frac{X_1 - \mu}{a_1} \sim \stdnormnot$ then $\displaystyle\frac{(X_1 - \mu)^2}{a_1^2} \sim \stdnormnot$
\item If $\displaystyle \frac{X_1 - \mu}{a_1} \sim \stdnormnot$ then $\displaystyle\frac{(X_1 - \mu)^2}{a_1^2} \sim \chisq{n}$
\item If you do a one-sample t test with $n=20$ and get a standardized estimate of -2.30. If the square of this estimate is less than 95\% quantile of the $F_{1,19}$ distribution, then you retain $H_0$.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{7} According to Benford's Law in Base 10, in any measurement setting that spans orders of magnitude, the first digit of measurements is more likely to be a one than a two, a two than a three, etc. This phenomenon is ubiquitous and can be used to describe measurements for city populations, surface area of rivers, dollar amounts on tax returns, etc. The distribution is on the first row of the table below.

Here we investigate possible fraud in the 2009 Iranian election, a topic of many academic papers. We examine the vote counts in the 366 districts for one of the five main candidates and count the number of districts who's count had first digit = 1, first digit = 2, ..., first digit = 9. The tally is the second row of the table below.

\begin{table}[htp]
\centering
\begin{tabular}{l|ccccccccc|c}
& \multicolumn{9}{c}{First Digit is $x = $} &\\
 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & Total \\\hline\hline
Benford's Law in Base 10 $p_X(x) = $ & .301 & .176 & .125 & .097 & .079 & .067 & .058 & .051 & .046 & 1.000 \\\hline
Observed Count & 125 & 57 & 44 & 29 & 24 & 16 & 41 & 13 & 17 & 366\\
\ingray{Row 3 Name} & 110.17 & 64.42 & 45.75 & 35.50 & 28.91 & 24.52 & 21.23 & 18.67 & 16.84 & ?\\
\ingray{Row 4 Name}  & 2.00 & 0.85 & 0.07 & 1.19 & 0.83 & 2.96 & 18.42 & 1.72 & 0.00 & ?
\end{tabular}
\end{table}
\FloatBarrier

We wish to test if the voting counts \emph{differ} from Benford's Law at $\alpha = 5\%$. Let $\theta_1$ denote the true probability of a count having a first digit = 1, $\theta_2$ denote the true probability of a count having a first digit = 2, ..., $\theta_9$ denote the true probability of a count having a first digit = 9. Note: $F_{\chisq{8}}(15.51) = F_{\chisq{9}}(16.92) = F_{\chisq{24}}(36.42) = F_{\chisq{27}}(40.11) = 95\%$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{9}

\begin{enumerate}[(a)]
\item This test is a \qu{goodness of fit} test.
\item This test is an \qu{independence} of $\geq 2$ events test.
\item The number of parameters in Benford's Law in Base 10 is 8.
\item $H_0: \theta_1 = \theta_2 = \ldots = \theta_9 = 1/9$.
\item $H_0:$ The $\theta_k$'s are equal to the values in the table's first row.
\item $H_a: \theta_1 = \theta_2 = \ldots = \theta_9 = 1/9$.
\item $H_a:$ at least one $\theta_k$ is not equal to the value in the table's first row.
\item The name of row 3 could be \qu{expected number of first digits in the district counts under Benford's Law in Base 10}.
\item The name of row 4 could be \qu{expected number of first digits in the district counts under Benford's Law in Base 10}.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{7} \textbf{This header is the same as in the previous problem.} According to Benford's Law in Base 10, in any measurement setting that spans orders of magnitude, the first digit of measurements is more likely to be a 1 than a 2, a 2 than a 3, etc. This phenomenon is ubiquitous and can be used to describe measurements for city populations, surface area of rivers, dollar amounts on tax returns, etc. The distribution is on the first row of the table below.

Here we investigate possible fraud in the 2009 Iranian election, a topic of many academic papers. We examine the vote counts in the 366 districts for one of the five main candidates and count the number of districts who's count had first digit = 1, first digit = 2, ..., first digit = 9. The tally is the second row of the table below.

\begin{table}[htp]
\centering
\begin{tabular}{l|ccccccccc|c}
& \multicolumn{9}{c}{First Digit is $x = $} &\\
 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & Total \\\hline\hline
Benford's Law in Base 10 $p_X(x) = $ & .301 & .176 & .125 & .097 & .079 & .067 & .058 & .051 & .046 & 1.000 \\\hline
Observed Count & 125 & 57 & 44 & 29 & 24 & 16 & 41 & 13 & 17 & 366\\
\ingray{Row 3 Name} & 110.17 & 64.42 & 45.75 & 35.50 & 28.91 & 24.52 & 21.23 & 18.67 & 16.84 & ?\\
\ingray{Row 4 Name}  & 2.00 & 0.85 & 0.07 & 1.19 & 0.83 & 2.96 & 18.42 & 1.72 & 0.00 & ?
\end{tabular}
\end{table}
\FloatBarrier

We wish to test if the voting counts \emph{differ} from Benford's Law at $\alpha = 5\%$. Let $\theta_1$ denote the true probability of a count having a first digit = 1, $\theta_2$ denote the true probability of a count having a first digit = 2, ..., $\theta_9$ denote the true probability of a count having a first digit = 9. Note: $F_{\chisq{8}}(15.51) = F_{\chisq{9}}(16.92) = F_{\chisq{24}}(36.42) = F_{\chisq{27}}(40.11) = 95\%$.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{7}

\begin{enumerate}[(a)]

\item The Pearson chisq statistic has an estimate = 366.00.
\item The Pearson chisq statistic has an estimate = 28.04.
\item The Pearson chisq statistic estimator here is asymptotically $\chisq{24}$.
\item The Pearson chisq statistic estimator here is asymptotically $\chisq{8}$.
\item If (d) were to be true, the retainment region for the estimate would be $\bracks{-15.51, +15.51}$.
\item $H_0$ is rejected and we conclude that the counts for this specific candidate is \emph{not} Benford-Law-distributed.
\item Assuming (f), the digit that is most incongruent with Benford's law is $x=9$.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}
\centering
\includegraphics[width = 6in]{windspeeds.pdf}
\end{figure}

\problem\timedsection{10} Above is a histogram of wind speeds at JFK airport measured at midnight for every day in 2013. We fit eight different iid DGPs / models to this data. Below are the models, the maximum likelihood estimates for their parameter(s) and their log likelihood value estimates:

\begin{table}[htp]
\centering
\begin{tabular}{r|cccccccc}
& \multicolumn{8}{c}{Name of Model / DGP}\\
 & Exponential & Normal & Weibull & Gamma & Gumbel & Gompertz & Frechet & Generalized Logistic \\\hline
$\thetahathatmle_1$ & 0.05 & 18.97 & 3.52 & 11.71 & 15.17 & 0.14 & 3.18 & 3.49 \\
$\thetahathatmle_2$ &  & 5.59 & 21.01 & 0.62 & 4.84 & 0.05 & 15.59 & 12.22 \\
$\thetahathatmle_3$ &  &  &  &  &  &  &  & 4.03 \\ \hline
$\ell(\thetahathatmle_1, \ldots, \thetahathatmle_{K_m}; \xoneton)$ & -1435.12 & -1142.72 & -1148.11 & -1129.28 & -1143.31 & -1200.26 & -1174.37 & -1129.65
\end{tabular}
\end{table}
\FloatBarrier

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{9}

\begin{enumerate}[(a)]
\item All of these models have the same number of parameters $K$.
\item The true model will be one of these eight models candidates.
\item According to the log-likelihood estimates, the best fitting model candidate is the Gamma.
\item According to the AICC metric, the best fitting model candidate is the Generalized Logistic.
\item The asymptotic bias on the true log-likelihood for the normal model is the same as the bias for the Gompertz model.
\item The AIC metric for the generalized logistic model candidate is 2263.30. %false
\item The AIC metric for the exponential model candidate is 2872.24. %true
\item Assuming (b), the probability the exponential model is the true model is negligibly small.
\item AICC values will not significantly differ from the AIC values.
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\problem\timedsection{16} In the \qu{Topiramate for the Treatment of Binge Eating Disorder Associated With Obesity: A Placebo-Controlled Study}, the researchers were also interested in adverse effects of the drug Topiramate. One such bad effect is \qu{Upper Respiratory Tract Infection} which we call \emph{infection}. The experimental results are below.

\begin{itemize}
\item In the $n_T = 202$ Topiramate group, there were 75 cases of infection and
\item in the $n_C = 202$ placebo group that did not take Topiramate, there were 40 cases of infection.
\end{itemize}
Let $\theta_T$ be the true proportion of infection in Topiramate-takers and $\theta_C$ be the true proportion of infection among non Topiramate-takers.  All numbers below are rounded to the nearest 3 digits.

\vspace{-0.2cm}\benum\truefalsesubquestionwithpoints{13}

\begin{enumerate}[(a)]
\item When testing $H_a: \theta_T \neq \theta_C$ at $\alpha = 5\%$, a retainment region for $H_0$ is equal to or is approximately $\bracks{-0.086, 0.086}$.%true
\item When testing $H_a: \theta_T \neq \theta_C$ at $\alpha = 5\%$, a retainment region for $H_0$ is equal to or is approximately $\bracks{-0.088, 0.088}$.%true
\item $CI_{\theta_T - \theta_C, 95\%}$ is equal to or is approximately $\bracks{0.087, 0.260}$.%true
\item There is a 95\% chance that the CI estimate in (c) contains the true mean difference $\theta_T - \theta_C$.
\item The estimate of the odds against infection in the Topiramate group is 1.693. %true
\item The estimate of the odds against infection in the Topiramate group is 0.591. %false
\item When testing $H_a: \displaystyle \frac{1 - \theta_T}{\theta_T} \neq \half$ at $\alpha = 5\%$, a retainment region for $H_0$ is equal to or is approximately $\bracks{0.224, 0.776}$.%true
\item The estimate of the risk ratio of infection in the Topiramate group vs the control group is 0.173.%false
\item The estimate of the risk ratio of infection in the Topiramate group vs the control group is 1.875.%true
\item $CI_{\theta_T / \theta_C, 95\%}$ is equal to or is approximately $\bracks{1.255, 2.495}$.%true
\item $CI_{\theta_T / \theta_C, 95\%}$ is equal to or is approximately $\bracks{-0.088, 0.088}$.%false
\item It would be possible to create an exact CI for the risk ratio of infection in the Topiramate group vs the control group given the data here and the concepts learned in class.%false
\item The CI estimator in (j) is more approximate in coverage probability than the CI estimator in (c).
\end{enumerate}
\eenum\instr\pagebreak

%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%


\documentclass{article}
\usepackage{parskip}
\usepackage{amsmath}

\setlength{\parindent}{0cm}

\begin{document}

\title{CPSC 413 \\ Assignment 1 - Analysis of Simple Algorithms}
\author{Andrew Helwer}
\date{February 2011}
\maketitle

\section{Partial Correctness of FindLeaf}

\emph{Precondition:}
\begin{itemize}
\item A nonempty binary tree $T$ is given as input
\end{itemize}

\emph{Postcondition:}
\begin{itemize} \item A leaf of $T$ is returned as output
\item The tree $T$ has not been changed
\end{itemize}

\textbf{Assertion A:} $v:=$ the root of $T$ and $T$ has not been modified

\textbf{Assertion B:} $v:=$ a leaf of $T$ and $T$ has not been modified

\textbf{Assertion C:} $v$ is an interior vertex of $T$

\textbf{Assertion D:} The depth of $v$ has increased by one, and $T$ has not
been modified

Assuming the precondition is satisfied, $T$ is nonempty - that is, it contains
at least one element, the root vertex. $v$ is set equal to this vertex before
execution of the loop, satistfying assertion $A$ as simply changing the
reference $v$ does not modify $T$.

If there is only one vertex in $T$, then $v$ (the root vertex) is a leaf and
the loop invariant $C$ is not satisfied. If there is more than one vertex in
$T$, then $v$ will be an interior vertex of $T$ as the root must then have at
least one child (either left or right) and so $C$ will be satisfied and the
loop entered.

By satisfying $C$ and entering the loop, $v$ is guaranteed to have either a
left or right child, specified $v.left$ and $v.right$ respectively. Once the
loop body has executed, $v$ has been set to either $v.left$ or $v.right$, and so
the number of $v:=v.parent$ operations it would take to reach the root node has
increased by one - thus the depth of $v$ has increased by one, partially
satisfying $D$. As well, the operations $v:=v.left$ and $v:=v.right$ make no
modifications to $T$ - they change only the reference $v$. Thus $T$ is not
modified during the loop body, and $D$ is fully satisfied. Therefore $C
\Rightarrow D$ over execution of the loop body.

$C$ is once again tested for, and if satisfied the loop body again executes. If
$C$ is not satisfied, the loop is exited. Since $C$ is not satisfied, $v$ is not
an interior vertex of $T$. Since $v$ is not an interior vertex, by definition it
must be a leaf. Thus $B$ is partially satisfied, which in turn satisfies one of
the postrequisites as $v$ is returned. The second postrequisite, and second
part of $B$ - that $T$ has not been modified - is ensured by satisfying $D$ at
the end of every execution of the loop body, or $A$ if there is only one vertex
in $T$. Thus $A \Rightarrow B$ over execution of the findLeaf function.

The above is sufficient to prove that the satisfaction of the preconditions
prior to findLeaf execution implies the satisfaction of the postconditions
after findLeaf execution. Since termination has not been proven, findLeaf has
only been shown to be partially correct.

\section{Termination of FindLeaf}

The following assumes that a binary tree consists of a finite number of
vertices.

Assume that findLeaf is executed on valid input whereby the precondition is
satisfied. Observe that instructions 1 and 6 are guaranteed to execute only
once, as they are not contained within the body of a loop. They may therefore
be disregarded when proving termination, and only the loop (instructions 2
through 5) considered.

A fundamental property of binary trees is depth, which is the number of edges
on the longest path from the root to any leaf in $T$. The depth of the root
is therefore zero. For any interior vertex $v$ of $T$, setting $v:=v.left$ or
$v:=v.right$ increases the depth of $v$ by one, as it adds an additional edge
to traverse to reach the root.

In question 1 it was proved that after every execution of the loop
body on valid input, the depth of $v$ was increased by one. Define a new
function $D$, whereby if $D(v)$ is computed for some $v \in T$, $D$ returns the
depth of $v$ in $T$, and if $D(T)$ is computed for some binary tree $T$, $D$
returns the depth of $T$. Thus after every execution of the loop body, where
$v'$ is the value of $v$ before execution, $D(v'):=D(v')+1=D(v)$.

Define another function $B(T,v) = D(T) - D(v)$, called the bound function. Since
$D(v)$ increases by one every time the loop body executes and $D(T)$ remains
constant (due to the condition of $T$ not being modified), $|B|$ decreases in
value with every execution of the loop body. Because the size of $T$ is finite,
$D(T)$ is finite as well, and so the loop body will execute only a finite
number of times before $B(T,v) = 0$ - at this point, $D(v) = D(T)$, and so $v$
must be a leaf node of $T$, as otherwise $v$ possesses a child with depth
greater than the depth of the tree, by definition impossible. Since $v$ is not
an interior vertex of $T$, the loop invariant is not satisfied and so the loop
will terminate when $B(T,v) = 0$ after a finite number of executions.

Thus it has been shown that the loop body will execute only a finite number of
times on valid input, and so findLeaf will terminate on valid input.

\section{Upper Bound on Cost of FindLeaf}

As in question 2, observe that on valid input steps 1 and 6 are executed only
once, for a total constant cost of 2. Also observe that the depth of $T$ is
maximized when every vertex in $T$ counts toward the depth of $T$ - that is,
when $T$ is in a configuration resembling a linked list with each vertex having
exactly one parent and one child except for the root (which lacks a parent) and
the leaf (which lacks a child). It will be in this case then that the bound
function defined in question 2 is initialized to a value furthest from zero.

The configuration described above will result in $T$ having a depth of
$|T|-1$, where $|T|$ is the number of vertices in $T$, also known as the size of
$T$. Since the depth of the root node is zero, $B$ is initialized to the value
of $|T|-1$, which is also the number of times the loop runs as $B$
decreases by exactly one with every execution of the loop body.

Inside the loop, three instructions will be executed every time it is run - the
initial check to see if the loop invariant is satisfied, a check whether the
vertex has a left child, and the assignment of the vertex reference to either
its left or right child depending on the outcome of this check. Thus the number
of instructions executed before the loop terminates will be $3*n$, where $n$ is
the number of times the loop executes.

Assembling these observations, the worst case running time of findLeaf may be
given by $R_U(s) = 3(s-1)+2 = 3s-1$, where $s=|T|$.

\section{Lower Bound on Cost of FindLeaf}

Steps 1 and 6 are executed exactly once, resulting in a cost of 2.
Since the number of other instructions executed has been proven to
be a function of the depth of the input, finding the binary tree
configuration with the least depth per unit size will also result in
a function for the lower bound on instruction executions in findLeaf.

Logically, this configuration results when each depth level of $T$ is completely
filled before putting a vertex on the next level - the binary tree cannot be
compressed any further. The structure described is one that is as close to a
full binary tree as possible. Since the capacity of each level is double the
previous, the function that relates $|T|$ to $D(T)$ is given by
$D(|T|)=fl(\log_2(|T|))$, where $fl$ is the flooring function that transforms
any real number into the next lowest integer value.
Following the same derivations as in question 3, we arrive at the best case
running time of findLeaf given by $R_L(s)=3*fl(\log_2(s))+2$, where $s=|T|$.

\section{The Action of the VertexCover Function}

\textbf{Proof A:}

By the action of vertexCover instruction either 6 or 7, the parent of the root
of $T_L$ in $T$ is $w$. Due to instructions 12 or 13, there is no path leading
from any vertex in $T_U$ to $w$, and thus no path leading from any vertex in
$T_U$ to any vertex in $T_L$. Thus any vertex in $T_U$ is not a vertex in
$T_L$, and vice versa - they are disjoint sets, with the addition that $u$ and
$v$ are in neither.

Since $v$ is defined to be a leaf, it has no children. The children of $w$ are
$v$ and the root of $T_L$, which could be $null$. Thus the removal of $T_L \cup
\{v\} \cup \{w\}$ from $T$ results in no lost vertices - all removed vertices
are in $T_L \cup \{v\} \cup \{w\}$. $T_U$ may thus be written $T \setminus T_L
\cup \{v\} \cup \{w\}$. Therefore $T_U$, $T_L$, $\{w\}$, and $\{v\}$ partition
$T$.

From the above it follows that every vertex of $T$ except $v$ and $w$ is a
vertex of exactly one of $T_U$ or $T_L$, so that $|T| = |T_U| + |T_L| + 2$.

\textbf{Proof B:}

The basis for the proof of disjointness in part A was that after the execution
of instructions 12 or 13, there is no path leading from any vertex in $T_U$ to
$w$, and thus no path leading from any vertex in $T_U$ to any vertex in $T_L$.
This may be modified slightly to prove something similar for edges in $T$.
Observe that of all the edges in $T$, there are only three that join to at
least one element outside of $T_U$ and $T_L$ - the edge from $x$ to $w$ (before
removal), the edge from the root of $T_L$ to $w$, and the edge from $v$ to $w$.
If these edges are disregarded, due to the partitioning nature of $T_U$ and
$T_L$ and how edges by definition must join two vertices, all other edges must
exist within $T_U$ or $T_L$.

Thus it is true that every edge in $T$ is an edge in exactly one of $T_U$ or
$T_L$, except for the edges that touch the vertex $w$.

\section{Correctness of VertexCover}

Prove: For all nonnegative integers $n = |T|$, $vertexCover(T)$ returns the
correct output

Base cases:

If $|T|$ = 0 or $|T|$ = 1, then the vertex cover is the empty set, as
there are no edges in $T$ to connect to the vertices in $T$.

Strong Inductive Hypothesis:

Assume the correct vertex cover is returned for $0...n$.
Show $vertexCover(n+1)$ also holds:

$vertexCover(n+1)$ splits $T$ up such that $|T| = |T_U| + |T_L|
+ 2$. Thus $|T_U| + |T_L| = |T|-2$, and so $|T_U| < |T|-2$ and $|T_U| < |T|-2$,
so by inductive hypothesis $vertexCover(T_U)$ and $vertexCover(T_L)$ return the
correct vertex cover of $T_U$ and $T_L$ respectively. Since $vertexCover(n+1) =
vertexCover(T_U) \cup vertexCover(T_L) \cup \{w\}$, $vertexCover(n+1)$ also
returns the correct vertexCover and so by the strong inductive hypothesis
vertexCover is correct for all nonnevative integers $n = |T|$.

\section{Worst-Case Cost of VertexCover}

The worst case running time will result from the same circumstances as the
worst case for findLeaf - that of a linked list configuration for $T$. Since
this is a recursive function, it is best to consider instruction 15 and work
from there.

Step 15 specifies returning $vertexCover(T_U) \cup vertexCover(T_L) \cup
\{w\}$. In the linked list configuration, $T_L$ will always be $null$, leading
to a cost of 2 for calling $vertexCover(T_L)$ as only instructions 1 and 2 will
be executed. The real cost lies in calling $vertexCover(T_U)$ - observe that by
removing both the leaf node and its parent from $T$ upon every execution of the
body of vertexCover, $|T_U| = |T|-2$. Since the precondition does not specify
whether $|T|$ is even or odd, this necessitates two base cases:

\begin{enumerate}
\item if $s = 0$, $C(s) = 2$
\item if $s = 1$, $C(s) = 2$
\item otherwise $C(s) = R_U(s) + 11 + 2 + C(s-2)$
\end{enumerate}

In the above, $s$ is initialized to $|T|$, $R_U(s)$ is the cost function of the
worst case for findLeaf defined back in question 3, and 11 is then number of
instructions that will be executed during a single level of recursion in
vertexCover, with 2 the number of instructions executed during the
$vertexCover(T_L)$ call.

\section{Upper Bound on Cost of VertexCover}

If we consider the recurrence defined in the previous question, it's clear that
there will be approximately $\frac{|T|}{2}$ levels of recursion. Consider the
quantities $A = C(s)$ and $B = C(s-2)$. As the recurrence is defined, $A$ must
be greater than or equal to $B$. Thus $A' = C(s) - C(s-2)$ is greater than or
equal to $B' = C(s-2) - C(s-4)$, and so $\frac{|T|}{2}(C(s) - C(s-2)) \geq C(s)$
for some large value of $s$, as $\frac{|T|}{2}(C(s) - C(s-2))$ may be viewed as
replacing the number of operations performed by all $\frac{|T|}{2}$ levels of
recursion with $C(s) - C(s-2)$, an increase over what they would have been.

Observing the above, the upper bound of the recurrence may be written
$\frac{|T|}{2}(R_U(s) + 13)$, which is of course linear in the product of the
size of $T$ and the number of steps used by findLeaf in the worst case.

\section{Lower Bound on Cost of VertexCover}

Observing that the best-case running time will occur if $T$ is as close
to a full binary tree as possible, using similar arguments to question 8 it is
ascertained that the lower bound on the running time will be of the order
$\frac{|T|}{2}(R_L(s) + C)$, where $C$ is some positive integer constant and
$R_L(s)$ is the lower bound on the running time of findLeaf defined in question
4. Thus the lower bound is also linear in the product of the size of $T$ and
the number of steps used by findLeaf in the best case.

\section{Bounds of VertexCover in Asymptotic Notation}

The worst-case running time of vertexCover, as a function of the size of the
input tree, $n$, is:

\begin{equation}
f(n) \in O(n^2)
\end{equation}

\end{document}


\chapter{Theory and overview}
\label{chapter:overview}

\section{Terminology and data products}
\label{sec:terminology}
%
As mentioned in the Introduction, the overall topic of this document is the
extraction of geophysical data from observed spectra. This calculation step is
normally denoted as the retrieval or the inversion, where mainly the first term
is used in this document. The objective of \smr\ is to estimate atmospheric
quantities, mainly the concentration of different gases, and these variables
are accordingly the main target of the retrieval, and, together with auxiliary
information, they form the L2 data.

Besides geophysical variables, the retrieval must consider a number of
instrumental aspects to avoid that e.g.\ pointing errors cause unnecessary
large retrieval errors. The result of this part of the retrieval can be seen as
diagnostics data. Some of the quality fields of the L2 data are based on the
diagnostics data, and the data are also stored internally for further analysis
(then denoted as L2I), to e.g.\ detect instrumental drifts.

The retrieval can also include atmospheric state variables that influence the
measurement, but the accuracy of the retrieval is not sufficiently high to
justify inclusion in the L2 data. Regarding atmospheric gases, the ones
included in the L2 data are denoted as target species, while other retrieved
gases are called secondary species.

\section{Radiative transfer basics}
\label{sec:rt}
%
There is no \ATBD\ dedicated to radiative transfer, as a general software is used
to handle this task (Sec.~\ref{sec:setup:forward}). Instead the main
considerations around radiative transfer are commented inside this \ATBD. For a
more detailed discussion, targeting \smr, the reader is referred to
\citet{eriksson:studi:02}.

A strong advantage of using microwaves is that local thermodynamic equilibrium
(LTE) can be assumed, even for radiative transfer in the mesosphere and lower
thermosphere. Further, as long as the lowest altitude of the line-of-sight, the
tangent point, is some kilometres above the tropopause, scattering can safely
be neglected as \smr\ operates at wavelengths $>$\,0.5\,mm. Such a restriction in
tangent altitudes is made for the operational \smr\ retrievals.

The combination of LTE and no scattering makes the basic simulation task
relatively simple. In fact, when it comes to atmospheric radiative transfer,
the required simulations boil down to summing up emission along the propagation
path, weighted with the transmission between \smr\ and the point of emission.
That is, the expression for radiative transfer to handle is \citep[see
e.g.][Eq.~50]{chandrasekhar:60}:
\begin{equation}
  \label{eq:rteq}
     I(\nu) = I_0(\nu)e^{-\int^l_0{k(s,\nu)ds}} +
     \int^l_0{k(s,\nu)B(T(s),\nu) e^{-\int^l_0{k(s',\nu)ds'}} ds},
\end{equation}
where $I$ is radiance, $I_0$ is the radiance along the line-of-sight (\LOS) at
the top of the atmosphere, where the propagation path starts, $l$ is the length
of this propagation path, $k$ is the absorption coefficient, $s$ is the
distance along the propagation path, and $B$ is the Planck function giving the
emission of a blackbody at temperature $T$ and frequency $\nu$.
Eq.~\ref{eq:rteq} assumes that the gaseous absorption is unpolarised. This is a
valid assumption in the microwave region for all species, except for oxygen which
is affected by the Zeeman effect. That is, Earth's magnetic field induces
polarised absorption and emission, and a matrix-vector equivalent to
Eq.~\ref{eq:rteq} is required to handle oxygen \citep[see
e.g.][]{larsson:zeema:14}.

Evaluation of Eq.~\ref{eq:rteq} gives the monochromatic intensity (radiance)
along pencil-beam propagation paths, matching infinite frequency and angular
resolution. Accordingly, Eq.~\ref{eq:rteq} must be solved for a set of
frequencies and propagation paths, to make it possible to incorporate the
antenna pattern, channel frequency response and other characteristics of the
sensor in the simulations \citep[see e.g.][]{eriksson:studi:02,read2006clear}.

\section{Theoretical formalism}
\label{sec:formalism}

\subsection{Forward and inverse models}
%
The retrievals are presented and discussed using the formalism presented in
Chapter~3 of \citet{rodgers:00}. The data to be inverted are appended to create
the measurement vector, \MsrVct. This vector is related to other variables as
\begin{equation}
  \label{eq:fmodel1}
  \MsrVct = \FrwMdl(\SttVct,\FrwMdlVct) + \MsrErrVct_n,
\end{equation}
where \FrwMdl\ is denoted as the forward model, \SttVct\ as the state vector,
\FrwMdlVct\ as the vector of forward model parameters and $\MsrErrVct_n$
represents measurement noise. The distinction between the two arguments of the
forward model is that all variables that we want to retrieve form \SttVct,
while all other quantities required of the forward model are placed in
\FrwMdlVct.

The forward model treated explicitly in this document is the software used to
model atmospheric radiative transfer and sensor responses. This is a model
operating with discrete quantities, while the ``true'', hypothetical, forward
model, \trueFrwMdl, representing the actual physical mechanisms in the atmosphere
and the instrument, must be seen as a continuous function. If \FrwMdl\ is
constructed and used carefully, the discrete representation should not cause
any fundamental problems, and it is here assumed that all deviations between
\trueFrwMdl\ and \FrwMdl\ can be treated as imperfect values in \FrwMdlVct.
That is, $\trueFrwMdl=\FrwMdl(\SttVct,\FrwMdlVct)$, but the exact values of
\FrwMdlVct\ are unknown and all we can do is to use the best possible estimate,
\FrwMdlVctHat. That is:
\begin{equation}
  \label{eq:fmodel2}
  \MsrVct = \FrwMdl(\SttVct,\FrwMdlVctHat) + \MsrErrVct_b + \MsrErrVct_n,
\end{equation}
where
\begin{equation}
  \label{eq:eps_b}
  \MsrErrVct_b = \trueFrwMdl - \FrwMdl(\SttVct,\FrwMdlVctHat).
\end{equation}
This difference ($\MsrErrVct_b$) is below denoted as the forward model
uncertainty.

The retrieval process is formalised in the inverse model, \InvMdl:
\begin{equation}
  \label{eq:imodel}
   \RtrVct = \InvMdl(\MsrVct,\aSttVct{a},\FrwMdlVctHat,\InvMdlVct),
\end{equation}
where \RtrVct\ is the retrieved state vector and \InvMdlVct\ covers all
additional variables introduced by the inverse model. The exact nature
of the vector \aSttVct{a}\ varies between retrieval approaches, but in general it
represents an a priori estimate of \SttVct.

\subsection{Linearisation}
%
From this point it is assumed that the retrieval problem is not strongly
non-linear, and a local linear analysis is possible. Or expressed differently,
that derivatives of the forward and inverse models are approximately valid over
a significant range. The Jacobian, or the weighting function matrix, is the
partial derivative of \FrwMdl\ with respect to \SttVct:
\begin{equation}
  \label{eq:kx}
  \aWfnMtr{\SttVct} = \frac{\PartD\FrwMdl}{\PartD\SttVct}.
\end{equation}
In the same manner, we define \aWfnMtr{\FrwMdlVct} as
\begin{equation}
  \label{eq:kb}
  \aWfnMtr{\FrwMdlVct} = \frac{\PartD\FrwMdl}{\PartD\FrwMdlVct}.
\end{equation}
The contribution function matrix, \CtrFncMtr, is defined as the partial
derivative of the inverse model with respect to the measurement vector:
\begin{equation}
  \label{eq:g}
  \CtrFncMtr = \frac{\PartD\InvMdl}{\PartD\MsrVct}.
\end{equation}
Having these partial derivatives at hand, the retrieval error can be related
to the fundamental uncertainties. As a first step, the forward model is
linearised around (\aSttVct{a},\FrwMdlVctHat):
\begin{equation}
  \label{eq:fmodel3}
  \MsrVct = \FrwMdl(\aSttVct{a},\FrwMdlVctHat) +
  \aWfnMtr{\SttVct}\left(\SttVct-\aSttVct{a}\right) +
  \aWfnMtr{\FrwMdlVct}\left(\FrwMdlVct-\FrwMdlVctHat\right) +
  \MsrErrVct_n
\end{equation}
Under these assumptions, we now have a second way to express the forward
model uncertainty:
\begin{equation}
  \MsrErrVct_b = \aWfnMtr{\FrwMdlVct}\left(\FrwMdlVct-\FrwMdlVctHat\right).
\end{equation}
By combining the equations above and rearranging the terms, we can finally
derive an expression for the total retrieval error, $\RtrErr = \RtrVct - \SttVct$:
\begin{equation}
  \label{eq:delta}
  \RtrErr =  \left(\AvrKrnMtr-\IdnMtr\right)
    \left(\SttVct-\aSttVct{a}\right) +
    \CtrFncMtr\aWfnMtr{\FrwMdlVct}\left(\FrwMdlVct-\FrwMdlVctHat\right) +
    \CtrFncMtr\MsrErrVct_n,
\end{equation}
where \IdnMtr\ is the identity matrix and
\begin{equation}
  \label{eq:A}
  \AvrKrnMtr = \CtrFncMtr\aWfnMtr{\SttVct},
\end{equation}
is the averaging kernel matrix. The terms on the right hand side of
Eq.~\ref{eq:delta} are denoted as smoothing error, forward model retrieval
error and measurement noise retrieval error, respectively. These error
components are discussed further in Chapter~\ref{chapter:characterisation}.

\subsection{Calculation of retrieval error}
%
Despite that Eq.~\ref{eq:delta} gives an expression for the retrieval error it cannot be used in practice, for the simple reason that \SttVct\ and \FrwMdlVct\
are not known. The retrieval error can only evaluated in a statistical sense,
and for this purpose uncertainties and errors are described by
covariance matrices (\CvrMtr). See e.g.\
\url{en.wikipedia.org/wiki/Covariance_matrix} for a description of this type of
matrices, as well as the basic calculation rules needed to understand the error
propagation. The statistical correspondence to Eq.~\ref{eq:delta} is
\begin{equation}
  \label{eq:Sdelta1}
  \aCvrMtr{\RtrErr} = \left(\AvrKrnMtr-\IdnMtr\right)\aCvrMtr{\SttVct}
  \left(\AvrKrnMtr-\IdnMtr\right)^T +
  \CtrFncMtr\aWfnMtr{\FrwMdlVct}\aCvrMtr{\FrwMdlVct}
  \aWfnMtr{\FrwMdlVct}^T\CtrFncMtr^T +
  \CtrFncMtr\aCvrMtr{\MsrErrVct_n}\CtrFncMtr^T.
\end{equation}
The combination of the instrument and the forward model can be seen as
the ``observation system'', then having an uncertainty of
\begin{equation}
  \label{eq:eps_o}
  \MsrErrVct_o = \MsrErrVct_b + \MsrErrVct_n.
\end{equation}
The covariance matrix of the observation system uncertainty is
\begin{equation}
  \label{eq:So}
  \aCvrMtr{\MsrErrVct_o} = \aWfnMtr{\FrwMdlVct}\aCvrMtr{\FrwMdlVct}
  \aWfnMtr{\FrwMdlVct}^T + \aCvrMtr{\MsrErrVct_n}.
\end{equation}
Using the definition of $\aCvrMtr{\MsrErrVct_o}$, Eq.~\ref{eq:Sdelta1}
can be written in a somewhat more compact form:
\begin{equation}
  \label{eq:Sdelta2}
  \aCvrMtr{\RtrErr} = \left(\AvrKrnMtr-\IdnMtr\right)\aCvrMtr{\SttVct}
  \left(\AvrKrnMtr-\IdnMtr\right)^T + \CtrFncMtr\aCvrMtr{\MsrErrVct_o}\CtrFncMtr^T.
\end{equation}

\section{Selected set-up}
\label{sec:setup}

\subsection{Retrieval method}
\label{sec:setup:inverse}
%

\subsubsection{The need for regularisation}
\label{sec:setup:inverse:reg}
%
In short, the task of the retrieval method is to derive \RtrVct\ from \MsrVct.
It turns out that the mapping from \MsrVct\ to \RtrVct\ is not unique. That is,
there exists an infinite set of $\SttVct$-vectors that result in a fit with the
measurement, considering the measurement noise. Instead, some ``optimal'' state
must be selected, among this set of possible solutions.

If we for a moment assume that the retrieval problem can be treated as fully
linear around (\aSttVct{a},\FrwMdlVctHat), we
have that $\aWfnMtr{\SttVct}\left(\SttVct-\aSttVct{a}\right)\approx \MsrVct
-\FrwMdl(\aSttVct{a},\FrwMdlVctHat)$ and the solution can be written as
\begin{equation}
  \label{eq:linretrieval}
  \RtrVct = \aSttVct{a} + \CtrFncMtr(\MsrVct-\FrwMdl(\aSttVct{a},\FrwMdlVctHat)).
\end{equation}
It is clear that \CtrFncMtr, at least roughly, represents an inverse of
\aWfnMtr{\SttVct}, but to use the standard inverse of \aWfnMtr{\SttVct} (only
possible if \aWfnMtr{\SttVct} is square) or to apply the least squares method
($\CtrFncMtr=(\aWfnMtrTrp{\SttVct}\aWfnMtr{\SttVct})^{-1}\aWfnMtr{\SttVct}$)
will result in an extremely high sensitivity to noise. The retrieval problem at
hand is said to be ill-posed, which is the standard situation for passive
atmospheric sounding.

\subsubsection{The optimal estimation method}
\label{sec:setup:inverse:oem}
%
The normal way to tackle ill-posed problems of this type is
``regularisation'', where some constrain on the solution is introduced. In
passive atmospheric sounding, using statistical information as basis for the
regularisation has become the standard approach, and it is also the approach
selected for \smr. The method is probably most widely known as the optimal
estimation method (\OEM), which is the name used in this document. The method
can be seen as an application of Bayes theorem, under certain conditions, and
another possible name of the method the maximum a posteriori solution, see
further \citet{rodgers:00}.

The aspect of ``statistical regularisation'' is probably most clearly
identified by noting that \OEM\ minimises the following ``cost function'':
\begin{equation}
  \label{eq:costfun}
  \CstFnc(\SttVct) = (\MsrVct-\FrwMdl(\SttVct,\FrwMdlVctHat))^T\aCvrMtr{o}^{-1}
  (\MsrVct-\FrwMdl(\SttVct,\FrwMdlVctHat)) +
  (\SttVct-\aSttVct{a})^T\aCvrMtr{a}^{-1}(\SttVct-\aSttVct{a}),
\end{equation}
that is,
\begin{equation}
  \label{eq:mincost}
  \RtrVct = \min\limits_{\SttVct}(\CstFnc(\SttVct)).
\end{equation}
The cost function \CstFnc\ can be seen as the sum of the two penalty terms. The
first term weights how well the solution corresponds to the measurement. If
\CstFnc\ would be set to only contain this term, the standard least squares
solution would be obtained. The regularisation is introduced by the second
term, that evaluates the probability of \SttVct\ based on a priori
information.

If the retrieval problem is linear around (\aSttVct{a},\FrwMdlVctHat), \RtrVct\
can be calculated using Eq.~\ref{eq:linretrieval}, with
\citep[][Eq.~4.5]{rodgers:00}
\begin{equation}
  \label{eq:lindy}
  \CtrFncMtr = (\aWfnMtrTrp{\SttVct}\aCvrMtr{o}^{-1}\aWfnMtr{\SttVct}
  +\aCvrMtr{a}^{-1})^{-1}\aWfnMtrTrp{\SttVct}\aCvrMtr{o}^{-1}.
\end{equation}
However, the inversion of \smr\ measurements result in a (moderately)
non-linear problem and an iterative process is required to find the minimum of
\CstFnc, see Sec.~\ref{sec:ml}.

\subsubsection{Considerations around \SttVct\ and \FrwMdlVct}
\label{sec:setup:inverse:xb}
%
Formally, \aCvrMtr{a}\ shall be set to \aCvrMtr{\SttVct}\ \citep{rodgers:00},
that is, a description of the natural variability (including correlations) of
the elements in \SttVct, but this rule is not always obeyed. First of all,
normally there is no manner in which an exact \aCvrMtr{\SttVct} can be derived,
and this matrix is set in some parametric way, loosely based on measurements
and general knowledge of atmospheric physics. It could also be the case that
\aCvrMtr{a}\ is partly used as a ``tuning parameter''. The most common example
is that the variances in \aCvrMtr{a}\ are set with some margin with respect to
the true values, to maintain measurement information as far as possible, or
expressed differently, not to constrain the solution more than necessary.
Further, it is likely that \aSttVct{a}\ differs from the true mean value of
\SttVct, and this systematic deviation should also be represented in
\aCvrMtr{a}\ \citep{eriksson:analy:00}. For a continued discussion, and
comments on the relationship between \aCvrMtr{a}\ and the corresponding
regularisation matrix used in ``Tikhonov regularisation'', see, for example,
\citet{eriksson:analy:00} and \citet{ungermann2011tomographic}.

Finally regarding \aCvrMtr{a}, the point made in Sec.~2.6 of \citet{rodgers:00}
is stressed, for given values along the diagonal, setting \aCvrMtr{a}\ to be
purely diagonal means a stronger regularisation than including reasonable
off-diagonal elements (to reflect correlations). It is a popular mistake to
think that this relationship is reversed. Here it should be noted that using a
diagonal \aCvrMtr{a}\ equals the common choice in Tikhonov regularisation to
select the ``smallest norm'' ($\MtrStl{L}_0$) solution.

The formally correct choice for \aCvrMtr{o} is \aCvrMtr{\MsrErrVct_o}
\citep{eriksson:analy:00,rodgers:00}, but again this theoretical guideline is
seldomly followed exactly. The normal choice is to set
$\aCvrMtr{o}=\aCvrMtr{\MsrErrVct_n}$. One important practical consideration is
that the inverse of \aCvrMtr{\MsrErrVct_o}\ must be calculated and this is a
highly costly operation in the general case, but \aCvrMtr{\MsrErrVct_n}\ is in
general a pure diagonal matrix and the inverse can be determined with a minimal
calculation cost.

Further, the setting of \aCvrMtr{o}\ is influenced by the exact choice of
variables included in \SttVct. As discussed in Sec.~4.1.2 of \citet{rodgers:00}
there is no strict division line between \SttVct\ and \FrwMdlVct. Obviously,
\SttVct\ must contain a representation of the quantities targeted by the
measurements, but for variables of interfering character (e.g.\ frequency
off-set) there is a choice to make. For a perfectly linear retrieval case, exactly
the same results will be obtained for the target quantities independent of whether
interfering factors are covered by \SttVct\ or \FrwMdlVct. However, inclusion
in \SttVct\ is to prefer as this will result in that values describing the
interfering effects are obtained, which can provide important diagnostic
information (e.g.\ to detect instrumental changes). In addition, in a
non-linear situation a more optimal solution is obtained by placing interfering
factors in \SttVct, as it is likely that \aWfnMtr{\FrwMdlVct}, and then also
\aCvrMtr{\MsrErrVct_o} (cf.\ Eq.~\ref{eq:So}), varies with \SttVct. That is,
the description of observation uncertainties is likely to deteriorate with
the deviation between $\SttVct$ and $\aSttVct{a}$. If interfering species are part
of \SttVct, such non-linear effects will be taken care of by the reevaluation of
\aWfnMtr{\SttVct}.

\subsection{Forward model}
\label{sec:setup:forward}
%
Some basic requirements on the forward model were introduced in
Sec.~\ref{sec:rt}. The simulation problem to be tackled is basically the
simplest possible, Eq.~\ref{eq:rteq} should be handled by all forward models
for passive microwave measurements (but limb sounding requires that refraction
and the spherical shape of the Earth is considered when determining the
propagation path, which excludes all ``plane-parallel'' models). On the other
hand, the analysis of some \smr\ data requires non-standard forward model
features: the 118\,GHz oxygen measurements give rise to a demand for treatment
of Zeeman effects, and Doppler effects due to winds could have a significant
impact on mesospheric spectra.

Beside the atmospheric radiative transfer, the second main task of the forward
model is to incorporate effects of sensor characteristics. For the \smr\
radiometer package the sensor features needing consideration are antenna
angular response, mixer, sideband filtering and frequency response of each
spectrometer channel. Compensation of the Doppler shift due to
the satellite's movement is part of the L1b processing.

A further requirement is that the forward model can deliver the Jacobian
(\aWfnMtr{\SttVct}), a demand that should be clear from Eq.~\ref{eq:lindy}.
This task must be handled for all quantities that can be part of the state
vector. That is, the Jacobian is needed not only for the relevant atmospheric
variables (VMRs, temperature and winds), but also fitting of various sensor
imperfections must be handled (pointing off-set, frequency off-set and baseline
fit), see further Sec.~\ref{sec:x}.

In contrast to many other satellite missions \citep[e.g.][]{read2006clear}, no
\smr\ specific forward model has been developed. Instead, the general forward
model \ARTS\ (\ARTSlong) is used for \smr\ L2 processing. Earlier operational
L2 processing \citep{urban:odins:05-b} made use of the first \ARTS\ version
\citep{buehler:artst:05}, as well as the Moliere forward model
\citep{urban2004moliere}), while the retrievals described in this document are
based on the latest version (version 2.3). At this point it is just noted that
\ARTS\ complies fully with the demands listed above, and the presentation of
\ARTS\ is continued in Chapter~\ref{chapter:arts}.

\section{Non-standard retrievals}
\label{sec:nonstandard}
%
As mentioned already in Sec.~\ref{sec:aim}, the operational retrievals do not
handle all \smr\ measurements. Most importantly, scattering is not activated in
the forward model simulations and special retrievals have been developed to
extract information from spectra containing tropospheric information.
This retrieval scheme was introduced by \citet{rydberg:nonga:09} and the latest
results are found in \citet{eriksson:overw:14}. However, the method of
\citet{rydberg:nonga:09} is only applicable for tangent altitudes below 9\,km
and latitudes between 30$^\circ$S and 30$^\circ$N. This leaves a gap between
the operational and the tropical-tropospheric retrievals, and there are
still \smr\ spectra that are not part of any inversion.

The operational processing is of 1D character (Sec.~\ref{sec:arts:config}), as
the standard limb scanning pattern of \smr\ does not provide a basis for
resolving along-track features. However, Odin has during some shorter periods
made measurements dedicated to polar mesospheric clouds. During these
measurement campaigns, Odin has just scanned over a narrow altitude region in
order to enhance the along-track resolution. Accordingly, to maintain the
measurement information of these special observations, 2D (or tomographic)
retrievals were needed. These retrievals were also made by combing \OEM\ and
\ARTS\ but are presented separately, in \citet{christensen:tomog:15}.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "L2_ATBD"
%%% End:


\chapter{Orbital workflow} \label{workflow}

To appreciate the scope of the design and implementation Skylab, we detail the major work processes that the various user roles need to accomplish during an iteration (one year's cohort) of Orbital.  We describe the workflow for Orbital students first, and circle back to describe the workflows of other supporting roles in the process.

\section{Registration} \label{registration}

To enroll in the Orbital program, students will need to register in Skylab first. When a user first logs into Skylab, a \textit{User} object will be created for user. And since it is the first time the user logs in to the system, there is no existing role assigned to current user in current cohort. Then Skylab assumes that the user is trying to register and therefore a link to registration will be displayed. This workflow works for most users who are students in Orbital program. As for roles like \textit{Adviser}, \textit{Mentor} and \textit{Tutor}, admin users will create those roles manually. This is because there are only a few such roles and they have higher privileges and therefore creation of such roles should definitely be controlled by admin users.

For students' registration, after a user click the registration link, a form consisting of different questions will be presented to the user. Among these questions, several are to get a sense of what sort of interests the user has for the project over the summer. The user is supposed to select interested topics from different categories like programming language, web framework or mobile platform, IDE or text editor, e-commerce, cloud platform, purpose, embedded system, game framework, content management system and audience. These information will be recorded for match-making among students who need to find a teammate in Skylab.

After completing the registration form, if the user already has a teammate in mind, he/she can send an invitation to the potential teammate to form a team. Some users, on the other hand, do not have anyone to partner with for Orbital yet and a match-making algorithm will be run offline by admin user based on the interested topics they have selected during registration. An illustration of the whole workflow is as Figure~\ref{fig:RegistrationWorkflow}.

\begin{figure}[h]
  \centering
  \includegraphics[width=\textwidth]{Images/Skylab_Registration_Workflow.png}
  \caption{Illustration of registration workflow in Skylab}
  \label{fig:RegistrationWorkflow}
\end{figure}

\subsection{Match making algorithm}

For students who do not have potential teammates at the time of registration, we will run a matching making algorithm based on the interested topics recorded in registration form submission. The algorithm composes of mainly two steps:

\begin{itemize}
  \item Calculate inverse document frequency of tags: inverse document frequency is to reflect how rare a term is in the corpus\cite{citationir}. In Skylab's match making's case, a document is a user's selection of interested topics, or tags; the corpus is the collection of users' interested topic sets; a term is a tag in a users' selections. If a term(tag) is rare among registration form results, we will be more confident that if both students choose this term and they will have higher chance of forming a team. The formula of calculation is as Formula~\ref{eq:idf}.

  \begin{equation}
    idf(t,D) = \log \frac{N}{\{ d \in D : t \in d \} + 1} \
    \label{eq:idf}
  \end{equation}

  with \(N\) as total number of documents in the corpus \( N = |D| \) and \(\{ d \in D : t \in d \}\) as number of documents where the term \(t\) appears\cite{citationir}.

  \item Construct a vector space model based on students' selected tags and each tag's weight calculated in previous step and calculate similarity(cosine) of different students. The formula for calculating cosine of two vectors is as Formula~\ref{eq:veccos}\cite{citationvecspacemodel}.

  \begin{equation}
    \cos\theta  = \frac{d_2 \cdot q}{ \norm{d_2} \norm{q} } \
    \label{eq:veccos}
  \end{equation}

  And we represent a user's interested topics with tags' weights and the similarity between 2 users will be the cosine between 2 vectors representing the 2 users. The formula for calculating cosine of two vectors is as Formula~\ref{eq:similarity}\cite{citationvecspacemodel}.

  \begin{equation}
    sim(d_j, q)  = \frac{d_j \cdot q}{ \norm{d_j} \norm{q} } \ = \frac{sum_{i=1}^{N} \omega_{i,j} \omega_{i,q}}{ \sqrt{sum_{i=1}^{N} \omega_{i,j}^2} \sqrt{sum_{i=1}^{N} \omega_{i,q}^2}} \
    \label{eq:similarity}
  \end{equation}

\end{itemize}

As matching making only needs to be run once before Orbital officially starts for those students who do no have a team yet, the algorithm is currently designed to run offline as a rake task. Currently it is only for students but in the future with recoding of mentors' and advisers' interested tags, this algorithm can be easily extended to students and mentors or students and advisers as well.

\section{Submission} \label{submission}

Students in Orbital are supposed to report their progress regarding their project at each milestone via \textit{Submission}. Basically a submission contains 3 parts:

\begin{itemize}
  \item README: highlights what are the changes and new features in the project.
  \item Project Log: a summary of work done during the phase and time used for each task.
  \item Video link: a link to a video introducing the project to evaluators.
\end{itemize}

As the structure of \textit{README} and \textit{Project Log} is free and we should allow creativity of students when it comes to describing their own projects, we decided to support rich text for submissions and there are some issues coming along with this decision such as SQL injection and XSS attacks. What is more, during use of Skylab, many good suggestions regarding user experience were brought up by students and advisers, like uploading of images and auto-expanding textareas during editing.

\subsection{Handling of rich text}

We used TinyMCE to support rich text editing feature in Skylab as it is a very popular WYSIWYG(what-you-see-is-what-you-get) editor with a rich set of features\cite{citationtinymce}. There are some libraries with markdown syntax supported such as EpicEditor, Vue.js and Hallo.js which are more lightweight. However, as Skylab is built for freshmen to get more hands-on experience with coding, we do not expect students to be equipped with much prior knowledge such as markdown syntax. In the contrast, markup based editors such as TinyMCE and CKEditor do not require any learning and are easily to get started with. What is more, the large community using TinyMCE has made various plug-ins available for different features. There are also quite good resources online about integrating TinyMCE editor in a Rails project. Therefore, we decided to use TinyMCE for \textit{Submissions}' rich text support.

One particular disadvantage of using TinyMCE is that the size of this editor is pretty large and loading of the page will be slowed down because of it. Therefore, TinyMCE related resources is only loaded if the page is requiring rich text editing. This is done by configuration to disable auto loading of all JavaScript, which is the default behavior of Rails. In this way, most pages can be loaded within a very short time. What is more, assets whose names start with ``tinymce'' are set to expire in a year to reduce frequent requesting of such resources from clients --- which basically means students only need to load these resources once in one year.

Rails has built-in checking against SQL injection attacks and therefore Skylab is safe from such attacks when storing submissions' contents\cite{citationrorsecurity}. However, there is still currently a known bug in the implementation when it comes to viewing of submissions. As contents like \textit{README} and \textit{Project Log} should be rendered as rich text, it is possible for students to perform XSS attacks by injecting executable JavaScript code in the submission. This sort of vulnerabilities will be fixed in the future.

\subsection{Usability}

Skylab is a software engineering project and therefore improvements in user experience is one key part when it comes to implementation. During the use of Skylab, many suggestions were brought up by students and advisers about usability. And by addressing these issues, Skylab is serving users better with a smoother user experience.

\subsubsection{Target Milestone Selection}

When Skylab was used for the first time, students were expected to choose the target milestone, which the submission is for. However, many students reported that it is just a redundant step as every time they will only submit to the currently active milestone. After hearing this, a quick fix of automatically selecting the current milestone for students using JavaScript functions was done, while the manual selection is still possible. And then during the \textit{Adviser Focus Group Meeting}, some advisers further pointed out that the selection should not even be presented to users as it is of completely no use and therefore the whole selection was completely removed from Skylab after the meeting by moving the task to the backend logic.

\subsubsection{Rich Text Editing}

As quite some students want to insert image to \textit{README} sections, an image uploading feature was soon added to submission page for users' convenience. Behind the scene Skylab is using a third-party API from Imgur. This was done mainly for 2 reasons: using Imgur is relatively easy to implement; we can also avoid heavy server load due to file uploading and possible attacks in uploaded files.

Another improvement over user experience is auto expanding of TinyMCE editing area as feedback from students mentioned that their \textit{README} and \textit{Project Log} are usually quite lengthy. So auto expanding would not require too much scrolling during creating/editing a submission.

\section{Peer Evaluation} \label{peerevaluation}

 After students have submitted to milestones, peer evaluation process can begin. Teams will look through evaluated teams' projects and submissions and then evaluate their performance in \textit{PeerEvaluation}, which is a very important component in determining whether the evaluated teams can pass Orbital or not. Although there are different questions for each peer evaluation, all evaluations contains essentially 2 parts:

 \begin{itemize}
  \item Public: a section with general feedback on how well the evaluated team has done and the response will be viewed by target team with evaluator team name available.
  \item Private: a section with critiques and overall rating. Critiques will only be viewed by target team without any evaluator team information while overall rating is only for grading purpose and not viewable by target teams.
\end{itemize}

\subsection{Loading of Peer Evaluation templates}

When Skylab was first developed, the public part of a \textit{PeerEvaluation} templates will be one HTML form and the private part is another one. This means all questions in the public part are coded in one predefined html template and same goes for questions in private part.

This approach does not seem to have much problem at first and it really served Skylab's purpose well by delivering perfectly workable features in time. However, since questions are different for different milestones, separate templates have to be created for each new milestone. And when user wants to view/submit/edit a \textit{PeerEvaluation}, the corresponding templates will have to be loaded based on properties of the milestone. In this way, the system is not really open to extension and clearly it violates Open-Close principle. After realizing this, a system for dynamically creating questions has been implemented for \textit{Feedback} and migration of \textit{PeerEvaluations} to the survey-template-and-questions based system has been done as well. A more detailed description for how the dynamic loading of questions is in Section~\ref{feedback}.

\section{Feedback} \label{feedback}

\textit{Feedback} is for evaluated teams to evaluate the \textit{PeerEvaluations} they have received and it is also a very important component in determining whether the evaluator team can pass or not. After realizing the lack of extensibility in the design for \textit{PeerEvaluations} and with more time available for implementation of \textit{Feedback}, a survey-template-and-questions system has been set up, which made the system open to extensions for more questions and even question types.

\subsection{Survey-template-and-questions system}

The basic flow when a request to create a \textit{Feedback} is illustrated in Figure~\ref{fig:FeedbackFlow}.

\begin{figure}[h]
  \centering
  \includegraphics[width=0.8\textwidth]{Images/Skylab_Feedback_Flow.png}
  \caption{Flow of a feedback creation process}
  \label{fig:FeedbackFlow}
\end{figure}

So when a student clicks the button to create feedback, \textit{FeedbacksController}'s \textit{new} action will be invoked and inside the method, the corresponding \textit{SurveyTemplate} and all \textit{Questions} belonging to the \textit{SurveyTemplate} will be sent to view. Then instruction for the \textit{Feedback} and all questions will be rendered as response to the student. After the student completed and pressed submit button, responses to all questions will be sent to server and a Feedback instance containing all submitted responses will be created.

There currently 4 types of \textit{Questions} in the system as Figure~\ref{fig:SkylabQuestions}:

\begin{itemize}
  \item \textbf{TextQuestion}: a text question which will be rendered in the format of textarea and text-based responses are expected.
  \item \textbf{RichTextQuestion}: a rich text question which will be rendered in the format of TinyMCE editable area and rich text is expected as response.
  \item \textbf{MultipleChoiceQuestion}: a multiple choice question which will be rendered as radio buttons followed by option descriptions and one option value is expected.
  \item \textbf{MultipleSelectQuestion}: a multiple choice question which will be rendered as a select element with ``multiple'' enabled with each option as one choice of selection.
\end{itemize}

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{Images/Skylab_Questions.png}
    \caption{All types of questions in Skylab}
    \label{fig:SkylabQuestions}
\end{figure}

All types of question will have \textit{title}, \textit{instruction}, \textit{content}, \textit{extras} and \textit{question\_type} and each type of question will have its own way of rendering and validation. In this way, adding new types of questions can simply be done via creating a new question model with its own rendering and validation methods.


% Chapter Template

\chapter{Topical n-grams model} % Main chapter title

\label{topicalngram} % Change X to a consecutive number; for referencing this chapter elsewhere, use \ref{ChapterX}

\lhead{Chapter 5. \emph{Topical n-grams model}} % Change X to a consecutive number; this is for the header on each page - perhaps a shortened title

%----------------------------------------------------------------------------------------
%	Introduction
%----------------------------------------------------------------------------------------

Joint sentiment and topic models have been used to tackle this classification problem. Despite having a hierarchical structure, these generative models have
a bag of words assumption. Due to this fact, they tend to misclassify texts having sentiment in the form of phrases. \textit{LDA} and it's extensions don't
work properly with phrases. To tackle this situation, we propose an unsupervised approach to sentiment analysis using the topical n-grams model which has been
shown to be effective with phrases. We train the topical n-grams model using two topics i.e., positive, and negative, list of positive and negative
words, and rules to detect positive and negative phrases. New documents are then classified using this trained model. The system gives better results than the
existing Joint Sentiment Topic model. We also propose an approach to generate a list of positive and negative words using LDA based on our observations reported in~\cref{experiments}.

\section{Introduction}

In the next section, we will discuss the topical n-gram model in detail.

\section{Topical n-grams model}

n-gram phrases (or collocations) are fundamentally important in many areas of natural language processing (e.g., parsing, machine translation and information retrieval).
Phrase as the whole carries more information than the sum of its individual components, thus it is much more crucial in determining the topics of document collections
than individual words \citep*{wang2005note}. However, most of the topic models assume that words are generated independently to each other, i.e., under the bag of words
assumption. The possible over complicacy caused by introducing phrases makes these topic models completely ignore them. It is true that these models with the bag of words
assumption have enjoyed a big success, and attracted a lot of interests from researchers with different backgrounds. A topic model considering phrases would be more
useful in certain applications. Topical n-grams model is one such generative model. It's generative process is explained as follows,

\begin{enumerate}
 \item Draw multinomial \(\phi_z\) from a Dirichlet prior \(\beta\);
 \item Draw binomial \(\psi_z\) from a Beta prior \(\gamma\);
 \item Draw multinomial \(\sigma_{zw}\) from a Dirichlet prior \(\delta\);
 \item For each document d, draw a multinomial \(θ^{(d)}\) from a Dirichlet prior \(\alpha\); then for each word \({w_i}^{(d)}\) in document \(d\),
    \begin{enumerate}
     \item Draw \({x_i}^{(d)}\) from binomial \(\psi_{{w_{i-1}}^{(d)}}\);
     \item Draw \({z_i}^{(d)}\) from multinomial \(\theta_{(d)}\);
     \item Draw \({w_i}^{(d)}\) from multinomial \(\sigma_{{w_{i-1}}^{(d)}}\) if \({x_i}^{(d)} = 1\); else draw \({w_i}^{d}\) from multinomial \(\phi_{{z_i}^{(d)}}\).
    \end{enumerate}
\end{enumerate}

The main point to infer from this generative process is that the topic assignments for the two terms in a bigram are not required to be identical. In the description of
topic n-grams given in \citep*{wang2005note}, they have used the topic of the last term as the topic of the phrase. But in our experiments, we have used certain rules
as prior information to assign topics to a phrase initially.

\includegraphics[width=\textwidth]{topicalngram.png}
\begin{center}
 Figure 5.1 Topical n-grams model
\end{center}

\subsection{Inferencing}

Gibbs sampling is used to conduct approximate inference in this paper. During Gibbs sampling, we draw the topic assignment \(z_i\) and the bigram status \(x_i\)
iteratively for each word \(w_i\) according to the following conditional probability distribution:

\begin{equation}
p(z_i,x_i | z_{-i},w_{-i},w,\alpha,\beta,\gamma,\delta) \propto
\frac{\gamma_{x_i} + p_{z_{i-1}w_{i-1}x_i}}{\sum_{k=0}^1 {(\gamma_k+p_{z_{i-1}}w_{i-1}k)}} {(\alpha_{z_i} + q_{d_{z_i}})} \times \left\{
  \begin{array}{l l}
    \frac{\beta_{w_i} + n_{z_i w_i}}{\sum_{v=1}^V {(\beta_v+n_{z_i v})}} & \quad \text{if $x_i$ is even}\\
    \frac{\delta_{w_i} + m_{z_i w_{i-1} w_i}}{\sum_{v=1}^V {(\delta_v + m_{z_i w_{i-1} v})} } & \quad \text{if $n$ is odd}
  \end{array} \right.
\end{equation}

where,

\(z_{-i}\) denotes the topic assignments for all word tokens except word \(w_i\), \\
\(x_{-i}\) represents the bigram status for all tokens except word \(w_i\), \\
\(n_{zw}\) represents how many times word \(w\) is assigned into topic \(z\) as a unigram, \\
\(m_{zwv}\) represents how many tmes word \(v\) is assigned as the second term of a bigram given the previous word \(w\), \\
\(p_{zwk}\) denotes how many times the status variable \(x\) equals \(k\) given the previous word \(w\) and previous word's topic \(z\), and \\
\(q_{dz}\) represents how many times a word is assigned to topic \(z\) in document d.

In next section, we will explain the use of topical n-grams model for sentiment analysis

\section{Topical n-grams model for Sentiment Analysis}

To make use of topical n-grams model for sentiment classification, we use an approach similar to using LDA for sentiment classification.

\begin{itemize}
 \itemsep0em
 \item Set number of topics equal to 2.
 \item Remove stop-words.
 \item Remove objective words as they won't affect sentiment. The objective words in this case do not include the negation words like \textit{doesn't,
 won't, no}, etc. This is to ensure that we can catch negation of polarity when they are used with subjective words.
 \item Apply Gibbs Sampling with prior. The prior used in this case is more sophisticated and can handle both words and phrases. In case of words, if
 is not present in a bigram then simply use a list of positive and negative words to assign positive or negative topic. If the word is present in a
 bigram then assign it the topic of the bigram. There are some rules to detect and assign topics to bigrams which are explained next.
 \item Use the trained model to classify a new document as positive or negative.
\end{itemize}

\subsubsection*{Rules for Topic assignment of phrases}

At present, our rules are restricted to bigrams. We plan to extend them as explained in Section~\cref{conclusions}.
In the following rules, we mean topic when we say polarity. The use of polarity makes it easy to understand
the rules as they are concerned with subjectivity.

\begin{enumerate}
 \itemsep0em
 \item If the first word in the bigram is a negation word and the second word is subjective then the polarity
 of the bigram is opposite to the polarity of the second word. \\
 \textbf{Examples:} \textit{won't like, won't regret, etc.}. Here, \textit{won't like} is assigned negative
 polarity and \textit{won't regret} is assigned positive polarity.
 \item If both the words in the bigram are subjective then are two cases. If both words are of the same polarity
 then resultant polarity is the same. But if their polarities are different, then the polarity of the first word
 is assigned to the bigram. \\
 \textbf{Examples:} \textit{beautifully amazing} is positive as both words as positive. \textit{lack respect} is
 assigned negative as per the rules.
\end{enumerate}

One salient feature of this approach is that it is an unsupervised method and can work on any domain.

\section*{SUMMARY}

In this chapter, we dicussed the motivation behind the topical n-grams model. Later on, we discussed the inference equation used by it. At the end,
we proposed an approach to use this model for sentiment analysis. The experiments and results obtained are discussed in ~\cref{experiments}.

In the next chapter, we will show the use of deep semantics for sentiement analysis.


\section{Calibration and detector behavior\label{sec:strategy}}

\subsection{Calibration}

This section describes how different correction factors are obtained:  the energy calibration (MIP, $\pi^{0}$, run by run), the time calibration and the bad channel mask.\

All these correction factors or masks are stored in the OCDB but also the OADB. Since these calibration parameters do not arrive before the full ALICE data reconstructions of the first periods are completed, the parameters are stored not only in the OCDB but also in the OADB so that the clusters can be corrected at the analysis level.  For the moment we do not store the time calibration and run by run correction factors in OCDB just in OADB.

\subsubsection{Energy calibration: MIP calibration before installation - Julien}
First, the calibration is done on cosmic measurements before installing the SuperModules at P2, but the accuracy obtained using MIPs is not good enough.

\subsubsection{Energy calibration: $\pi^{0}$  - Catherine}

The energy calibration relies during data taking on the measurement of the $\pi^{0}$ mass position per cell. Each tower has a calibration coefficient. In what follows, a calibration parameter is equal to the result of the fitted mass over the PDG mass value, where the fitted mass denotes the mass given by a gaussian fit on the $\pi^{0}$ invariant mass peak distribution in a given tower (plus a combinatorial background, fitted by a 2nd degree polynomial).
About 100-200 M events EMCAL (L0) triggered (trigger threshold at 1.5-2 GeV) allow to calibrate a majority of the towers. The towers located on rows 0 and 23 of each super modul (SM ) and those behind the support frame (about 5 columns per SM) have much fewer statistics and would need a minimum of 150 Mevts (probably more). It is to be noted that the run-to-run temperature variations change the towers' response in a non-uniform way, i.e. the width of the $\pi^{0}$ peak increases, and the mean $\pi^{0}$ mass is shifted differently for the various towers. Also the $\pi^{0}$ mass shifts to lower values for the towers with material in front, due to photoconversion close to the EMCAL surface.

A few iterations on the data, obtaining in each iteration improved calibration coefficients, are needed to achieve a good accuracy (1-2\%). Since the online calibration has a strong effect on the trigger efficiency, the voltage gains of the APDs are varied after each running period, to get a uniform trigger performance. Still, some towers are difficult to calibrate because they are behind of a lot of material (TRD support structures). For those MIPs or $J/\Psi$ measurements could help.

\paragraph*{$\pi^{0}$ Calibration Procedure\\}

Since $\pi^{0}$s decay into 2 gammas, their invariant mass is calculated from the energy of 2 clusters (and angle between the clusters). The position of the invariant mass peak of a tower therefore doesn't depend only on its response and calibration coefficient, but also on an average of the responses and calibration coefficients of all the other towers of the SM, weighted by  how often they appear in combination with a cluster in the considered tower. The 2nd effect, of weaker magnitude maybe, originates from the fact that a cluster most often covers more than the considered tower. To simplify the calibration process, the calibration coefficient is calculated as if the whole energy of the cluster was contained in the tower of the cluster which has the largest signal. So the position of the invariant mass peak of a tower also depends on an average of the responses and calib coeffs of its neighbouring towers. For these reasons, the calibration of the calorimeter with the  $\pi^{0}$ is an iterative procedure :
\begin{itemize}
\item Set all calib coeffs to 0 in OCDB.
\item Reconstruct the $\pi^{0}$'s with these OCDB coeffs.
\item Run the analysis code on this data to produce the analysis histograms and a 1st version of the calib coeffs.
\item Look at the fits on the towers invariant mass histograms and discard the value (or set it by hand) of the calib coeff of the towers for which the fit can't be trusted.
\item Create a 1st set of OCDB coeffs.
\item Reconstruct the $\pi^{0}$'s with these OCDB coeffs.
\item Run the analysis code on this data to produce the analysis histograms and a 2nd version of the calib coeffs.
\item Look at the fits on the towers invariant mass histograms and discard the value (or set it by hand) of the calib coeff of the towers for which the fit can't be trusted.
\item Create a 2nd set of OCDB coeffs.
\item Etc..., until the invariant mass is satisfactory in all the towers.
\end{itemize}
When the statistics is enough, 4 iterations should be enough to finalize the calibration (in practice, more are needed, due to outliers or studies that are needed).

There are 3 sets of codes :
\begin{itemize}
\item  Reco code : reads the data, reconstructs the $\pi^{0}$ inv mass distrib in each tower after it applies some cuts on the clusters and $\pi^{0}$ parameters. The output is a root file with invariant mass histograms (per tower, and summed-up per SM, per pT-bin).
\item Analysis code : reads the file produced by the reco code and analyses the histograms to produce the calib coeffs. This code is the one I present in what follows.
\item A code which reads the calib coeffs and writes them into a format that is loadable to OCDB.
\end{itemize}
The code is located in EMCAL/calibPi0/ :
\begin{itemize}
\item macros/ : contains the various macros.
\item input/ : contains the root files produced by the analysis code for the various iterations ("passes"). It has subdirectories "pass0/", "pass1/", etc... with, in each dir, the root file.
\item output/ : contains the various files produced by the analysis code for the various passes. It has subdirectories "pass0/", "pass1/", etc... with, in each dir, the various output files related to the pass.\footnote{Note that it wouldn't necessarily help to set-up a code that automatically reads and writes the pass number to avoid the hardcoded directories in the code, because it happens to do several times the same pass with various parameters (e.g. cuts in the reconstruction, or more statistics, or various masked zones, or hand-customization of a few calib coeffs, etc...).}
\end{itemize}

The cuts which must be put in the reconstruction are :
\begin{itemize}
\item Bad towers masked.
\item Both clusters in the same SM (to avoid misalignment effects).
\item Cut the 1-tower clusters out.
\item 20~ns timing cut.
\item Non-linearity correction (for the cluster energy)-- from beam test AFAIK.
\item No asymetry cut.
\item $E_{cluster} > 0.8$ ~GeV, or 0.7 GeV if there is little statistics. Tests showed that to remove the residual non-linearity (the $pi_{0}$ invariant mass rises with $p_{T}$), tightening the cut on $E_{cluster}$ was more efficient than requiring symetric decays (both gamma's of similar energy) (e.g. $asym < 0.5$ with $E_{gamma} > 0.5$~GeV).
\end{itemize}
It has the possibility to mask some areas. This is useful to disentangle the zones which have more material in front of them from those which don't. In the invariant mass distributions, the $\pi^{0}$ candidates kept are only those for which both clusters belong to the non-masked zones. In 2011, we considered masking the zones behind the support frame (in all the SMs or only in the SMs with TRD modules in front of them, i.e. SM 6-9 that year), plus additionnal problematic zones, to avoid taking clusters in these zones for the calculation of the average invariant mass in the towers with less material. (NB : not used for final calibration results, but for studies).

The analysis code has 3 input files :
\begin{itemize}
\item the root file f05 with inv mass histograms produced by the reconstruction code,
\item a file txtFileIn (output\_calibPi0\_parameters.txt) that contains the values of various parameters of the fit for each tower, at the previous pass,
\item a file txtFilePrevCalib (output\_calibPi0\_coeffs\_clean.txt) that contains the value of the calibration coefficient for each tower, at the previous pass (and after the hand-made corrections).
\end{itemize}
The 2 last files are therefore useless for the "pass0". To run the code for "pass0" (1st iteration), put the name of a valid file (e.g. one of last year) and just ignore the plots (red colour, in the last section -- see below).

There are 4 output files, that are written in the current directory (calibPi0/) : be careful not to overwrite an existing file ! After the code has been run, simply move those files to the relevant passXX directory := output/passXX/=.
\begin{itemize}
\item a postscript file psfile (output\_calibPi0.ps) with the plots described below,
\item a root file rootFileOut (output\_calibPi0.root) that contains the same plots in root format,
\item a file txtFileOut (output\_calibPi0\_parameters.txt) that contains the values of various parameters of the fit for each tower, for the current pass,
\item a file outputFile (output\_calibPi0\_coeffs.txt) that contains the value of the calib coeff for each tower, for the current pass.
Once the code has been run and the output files copied to the relevant output directory, I copy output\_calibPi0\_coeffs.txt to output\_calibPi0\_coeffs\_clean.txt, and modify the latter by hand to put the desired calib coeffs where we estimate that they can't be trusted.
\end{itemize}

9 parameters are defined to qualify the invariant mass distribution in each tower : the distribution is fitted by a gaussian + pol2 for the combinatorial background. The parameters are :
\begin{itemize}
\item amplitude of gaussian fit,
\item mean of the gaussian fit,
\item sigma of the gaussian fit,
\item c, b and a parameters of the combinatorial background fit $ax^2+bx+c$, I (histo integral),
\item I-S, S (integral of the gaussian fit). Minimal and maximal cut values are hardcoded (and to be changed at each iteration) for each parameter.
\end{itemize}
When the value of all the parameters lie between both extremes, the tower (i.e. the fit values, hence the mean, hence the calculated calib coeff) is "trusted". If one or more parameter has a value beyond the max cut value or below the min cut value, the tower is "untrusted".
Because these cut values can't be guessed in advance, the analysis code must be run twice per pass. The 1st time, so as to get the distributions of all 9 parameters, and decide on the basis of those distributions what are the suitable cut values to separate the towers to be trusted and those not to be trusted. The values are plugged in the code, and the code is then run a 2nd time, for real this time.
The macro (currently called DrawJulienFullEMCAL6.C) runs with 1 parameter in argument (set to 10 by default) : choice, which sets the number of SMs that one desires to include in the analysis. The values are either 4 (for the older SMs), or 6 (for only the newer SMs), or 10 (for the whole EMCAL). Here is the code. The macro is run this way :
\begin{lstlisting}
aliroot -b -q 'macros/DrawJulienFullEMCAL6.C++(10)'
\end{lstlisting}
There are various places where things must be customized before running the code ; they can be spotted by searching for this line : //CUSTOMIZE customize :.
\begin{itemize}
\item testChoice : this variable is a flag that allows to shorten the execution time for tests. 0 = not a test ; 1 = runs with only the 2 first columns of each SM ; 2 = runs with only the 2 first columns of the first SM,
\item the root input file f05,
\item the text input file txtFilePrevCalib (in principle not the name, only the path),
\item the text input file txtFileIn (in principle not the name, only the path),
\item if necessary : the min and max range values for the parameter histograms : tabMin and tabMax,
\item the min and max cut values for the parameters cutMin and cutMax,
\item if necessary : the number of bins in pT (for the 1st section, see below) nbPtBins and their range tabPtBins.
\item Text output on the standard output ("printf's") :
\end{itemize}

Finally, the first iteration needs the recalibration factors. This file is made running macros/RecalibrationFactors\_TextToHistoJulien\_mult\_2012.C on the output\_calibPi0\_coeffs.txt file. Once the RecalibrationFactors.root file is created it needs to be linked properly to re-run the reconstruction.

\subsubsection{Energy calibration: Run by run temperature gain variations - Evi, David  }

The SuperModules calibration depends on the temperature dependence of the different towers gains. We observe that from one period to other, where the T changes, the $\pi^{0}$ peak positions also changes. There are 2 ways to correct for this effect : either measure the mean T per run, and get the gain curves per tower a calculate the corresponding correction; or use the calibration LED events to quantify the variation from one reference run. Each of those 2 procedures have problems, poor or lack of knowledge of the gain curves of some towers or bad performance of the LED system in certain regions.
These temperature or time-dependent corrections are still under study: for further, and up-to-date, information, please see the wiki:
https://twiki.cern.ch/twiki/bin/viewauth/ALICE/EMCalTimeDependentCalibrations

\subsubsection{Time calibration - Marie }

The time of the amplitude measured by a given cell is a good candidate to reject noisy towers, identify pile up events when coming from different Bunch Crossing, or even identify heavy hadrons at low energy. The average time is around 580 ns. The aim of the time calibration is to do a relative calibration between cells to align all cells to a mean value of 0 ns, with as small spread as possible (negative values are unavoidable for the moment). The time calibration coefficient for each cell is the result of the average time of the cell when belonging to a cluster with enough energy (>1GeV).
The calibration coefficient have to be subtracted to the cell time.

\paragraph*{Time Calibration Procedure\\}

Since the some variations of mean time have been observed depending on the bunch cross numbers (BC) $\%$ 4 the computation of the time coefficients is done for each bunch cross numbers BC$\%$ 4 scheme.

The time calibration coefficient computing is done in 2 iterations.
\begin{itemize}
\item 1$^{rst}$  iteration:\\
Get Bunch Cross Number for the event.
Loop on all cluster of the event. \\ Loop on all cells in the cluster.
If cell amplitude is > 0.9 GeV and 500ns < cell time < 700ns then
compute average per cell per BC$\%$4 and fill in 1D histogam with calibration coefficients: hAveragesBC$x$ where $x$ stands for the result of BC$\%$4.
\item 2$^{nd}$iteration:\\
Get Bunch Cross Number for the event.\\
Loop on all cluster of the event. \\ Loop on all cells in the cluster.\\
Get Calibration coefficient for BC$\%$4 from  hAveragesBC$x$.  If cell amplitude is > 0.9 GeV and -20ns < (cell time-cell Calibration Coefficient)  < 20ns.\\
Compute average time per cell per BC$\%$4 and fill in 1D histogam with those new calibration coefficients: hAllAveragesBC$x$.
\end{itemize}

\paragraph*{Acces time calibration coefficients\\}

The time calibration coefficient are stored in OADB in TH1D histograms named hAllTimeAvBCx where x stands for the BC$\%$4 value.
They have the usual structure:
runrange/pass/

To use them in your analysis you may do the following:\\
\begin{DDbox}{\linewidth}
\begin{lstlisting}
 AliOADBContainer *contTRF=new AliOADBContainer("");
contTRF->InitFromFile(Form("%s/EMCALTimeCalib.root",fOADBFilePathEMCAL.Data()),"AliEMCALTimeCalib");
TObjArray trecal=(TObjArray)contTRF->GetObject(runnumber);
if(trecal)
{TObjArray trecalpass=(TObjArray)trecal->FindObject(pass);
if(trecalpass)
{printf("AliCalorimeterUtils::SetOADBParameters() - Time Recalibrate EMCAL \n");
for (Int_t ibc = 0; ibc < 4; ++ibc)
{
TH1F *h = GetEMCALChannelTimeRecalibrationFactors(ibc);
if (h)
delete h;
h = (TH1F*)trecalpass->FindObject(Form("hAllTimeAvBC%d",ibc));
}}
\end{lstlisting}
\end{DDbox}

As already  mentioned the time calibration of the cells is not done at the reconstruction level but offline during analysis. The methods to recalibrate cells is implemented in the class
\texttt{\$ALICE\_ROOT/\\EMCAL/AliEMCALRecoUtils}.
The method \texttt{RecalibrateCellTime(absId,bc,time)}: is called by the method \texttt{SwitchOnTimeRecalibration} , modifies the provided time with the calibration parameters. The inputs are the bunch crossing number that can be recovered from the event with InputEvent()->GetBunchCrossNumber(), and the absolute ID of the cell.
The way to pass  the calibration parameters to AliEMCALRecoUtils is the following.\\
\begin{DDbox}{\linewidth}
\begin{lstlisting}
AliCalorimeterUtils *cu = new AliCalorimeterUtils ;
TGeoManager::Import("geometry.root") ; //need file "geometry.root" in local dir!!!!
AliEMCALRecoUtils * reco = cu->GetEMCALRecoUtils();
and then
for(Int_t i =0; i< 4; i++) reco->SetEMCALChannelTimeRecalibrationFactors( i, (TH1F*) file->Get(Form("hAllTimeAvBC%d",i)))
\end{lstlisting}
\end{DDbox}
, where TFile *file is in the file containing the time calibration coefficients.

Some more details on time recalibration can be found in twikis:
https://twiki.cern.ch/twiki/bin/\\viewauth/ALICE/EMCalCode:HowTo\#Energy\_and\_Time\_calibration and in \\
https://twiki.cern.ch/twiki/bin/viewauth/ALICE/EMCALTimeCalibration

\subsection{Alignment - Marco}

CERN provides survey measurements of the position of different EMCAL Supermodules points at the beginning of the running period (and on request?). As soon this information is available, the ideal EMCAL positions used in the reconstruction by default, are corrected with special position matrices calculated from the measurements. Finally, once the data is reconstructed, the accuracy of the alignment is cross checked with track matching and $\pi^{0}$ mass measurements, since those values change depending on variations on the positions of the SuperModules.

\subsection{Bad channel finding - Alexis}

The analysis is done on the output of offline Quality assurance see section \ref{sec:QAOffline} histograms \texttt{TH2F EMCAL\_hAmpId} containing the distribution of amplitudes (energy) of cell versus cell Absolute ID number (AbsId). The idea is to check distributions over the cells of different observables extracted from this histograms. Then each cell is tested regarding to the distribution over all the cells for each obbservable. The different tests are the following:

\begin{enumerate}
\item average energy (average computed for Emin< E  <Emax)
\item average number of hit per event  (average computed for Emin< E  <Emax)
\item Shape criteria : A fit of the cell energy (amplitude) distribution is performed with the function:$A*e^{-B*x}/x^2$ between Emin and Emax.  Then the  $\chi^{2}/ndf$, A  and B which are parameters from the fit of each cell amplitude.
\end{enumerate}

Each criteria is tested at least once (they can be tested also for different energy i nterval). At the end of each test the marked cells are excluded (if above nsigma from mean value, usually nsigma is taken equal 4 or 5) before computing the next distribution.

The typical nsigma used is 4 or 5.
The min energy considered is 0.1 GeV/0.3 GeV.  And the maximum energy depends on the data (minbias or triggered data).

There are different levels of classification for  cells wich will enter the deadMap:
\begin{itemize}
\item kAlive = 0: cell is OK
\item kDead = 1: cell is dead
\item kHot = 2: cell is bad
\item kWarning=3: cell may have problems (warm or miscalibrated)
\end{itemize}

The distinction of the bad/warm status is done by visual check of the energy distribution of the cells detected by the different tests described above.

In the reconstruction pass the only the cells marked as kHot and kDead are not reconstructed.

The macro to compute the BadCellAnalysis can be found on \\ \texttt{\$ALICE\_ROOT/PWGGA/CaloTrackCorrelations/macros/QA/BadChannelAnalysis.C}. This macro allows first to merge all the individual run histograms in order to get the energy distribution over a large statistics. Usually this is done at the end of a data taking period. Then it computes the different tests and produces the text file with the dead and bad cells detected. Finally the macro produce a pdf file containing the individual energy distribution for all the detected cells.

All the results and  OCDB created are updated on the following twiki:\\
https://twiki.cern.ch/twiki/bin/viewauth/ALICE/EMCalQABadChannels.