\documentclass[a4paper, 10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath, amsfonts, amssymb}
\usepackage{here}

\title{\itshape \ttfamily Tonbandfetzen}
\author{Jan Berges}

\begin{document}
	\maketitle

	\section*{Outline}

	The perl module {\it Tonbandfetzen} helps create audio data from plain text input, modify and arrange it and handle the {\it Audio Interchange File Format}.

	\section*{Global variables}

	There are several global variables ($\rightarrow$ table \ref{variables}) reserved for the information which is necessary to make the module collaborate with the main script.
	\begin{table}[H]
		\centering
		\begin{tabular}
		    { r            c              c                  l }
			{\bf variable} & {\bf value}  & {\bf initial --} & {\bf meaning}            \\
			\verb|$B|      & $8, 16, 32$  & 16               & {\it sampleSize}         \\
			\verb|$C|      & $1, 2$       & 2                & {\it numChannels}        \\
			\verb|$s|      &              & 44100            & second/{\it sampleRate}  \\
			\verb|$T|      & $> 0$        & \verb|$s|/2      & beat duration            \\
			\verb|$A4|     &              & 440/\verb|$s|    & standard pitch           \\
			\verb|$N|      & $1, 2\hdots$ & 12               & notes per octave         \\
			\verb|$pi|     & $\pi$        & $\pi$            & $\pi$                    \\
			\verb|@p|      & (mono        & (cubic           & single wave              \\
			\verb|@a|      & audio        & curves)          & attack envelope          \\
			\verb|@z|      & data)        &                  & reverse release envelope \\
		\end{tabular}
		\caption{global variables}
		\label{variables}
	\end{table}

	\section*{Audio data}

	Audio data is stored in one-dimensional arrays which are sorted chronologically and subordinately by channel according to the {\it Audio Interchange File Format}. Except for \verb|@p|, \verb|@a| and \verb|@z|, data transfer works via references.

	\section*{Subroutines}

	\subsection*{make}

	\begin{verbatim}
		make 'name.aif', fix $data
	\end{verbatim}
	\verb|make| saves data with respect to \verb|$B|, \verb|$C| and \verb|$s|.

	\subsection*{take}

	\begin{verbatim}
		$data = take 'name.aif'
	\end{verbatim}
	\verb|take| reads data and sets \verb|$B|, \verb|$C| and \verb|$s| accordingly.

	\subsection*{mel}

	\begin{verbatim}
		$theme = mel 'F#2 1 = 1 = 1 -4 3:4 +3 1:4 + 1 -4 3:4 +3 1:4 + 2'
	\end{verbatim}
	\verb|mel| creates audio data from a textual command sequence using the samples \verb|@p|, \verb|@a| and \verb|@z| and with respect to \verb|$C|, \verb|$s|, \verb|$T|, \verb|$A4| and \verb|$N|. The commands are separated by white space and usually feature a special sign ($\rightarrow$ table \ref{signs}), that not only defines the meaning but also the mathematical sign of the accompanying number, which may contain the decimal point and one dividing colon and be omitted if zero. The processing of a defined tone is initiated by the duration command, which lacks a sign, unless it indicates a pause. Initial and per \verb|$T| values hold until overwritten. The frequency may also be declared via scientific pitch notation, e.g. \verb|B3|, \verb|C4| or \verb|C#4|. If it is zero, \verb|@p| is not scaled temporally.
	\begin{table}[H]
		\centering
		\begin{tabular}
			{ l            l                                    l                c               c               c }
			{\bf quantity} & {\bf formula}                      & {\bf unit}     & {\bf initial} & {\bf per $t$} & {\bf per \verb|$T|} \\
			duration       & $t$                                & \verb|$T|      & (\verb|*|)    &               &                     \\
			frequency      & $f = f_0$                          & 1/\verb|$s|    &  \verb|~|     &               &                     \\
			none           &                                    &                &  \verb|=|     &               &                     \\
			interval       & $\operatorname{lb} \frac f {f_0}$  & oct./\verb|$N| & \verb|- +|    & \verb|\ /|    & \verb|_ ^|          \\
			level          & $\lg \frac{\sqrt{L^2 + R^2}}{y_0}$ & dB             & \verb|? !|    & \verb|> <|    & \verb|, ;|          \\
			-- difference  & $\lg \frac R L$                    & --             & \verb|[ ]|    & \verb|( )|    & \verb|{ }|
		\end{tabular}
		\caption{signs}
		\label{signs}
	\end{table}

	\subsection*{stack}

	\begin{verbatim}
		$band = stack $vocals, $guitar, $bass, $drums
	\end{verbatim}
	\verb|stack| superposes data, whereby shorter data is repeated.

	\subsection*{stick}

	\begin{verbatim}
		$song = stick $intro, $verse1, $chorus, $verse2, $chorus, $outro
	\end{verbatim}
	\verb|stick| juxtaposes data.

	\subsection*{fit}

	\verb|fit($a, $t)| returns data \verb|$a| scaled to duration \verb|$t| or \verb|$T| with respect to \verb|$C|.

	\subsection*{fix}

	\verb|fix($a, $y)| returns data \verb|$a| scaled to amplitude \verb|$y| or maximizes it for \verb|out| with respect to \verb|$B|. The original data is changed.

	\subsection*{cut}

	\verb|cut($a)| returns data \verb|$a| without leading or trailing zero blocks of length \verb|$C|.

	\subsection*{in}

	\begin{verbatim}
		@p = map { sin($_)      } in 0, $pi * 2;
		@a = map { sin($_) ** 2 } in 0, $pi / 2, $s / 20;
		@z = @a
	\end{verbatim}
	\verb|in($a, $b, $n)| returns an array of \verb|$n| or \verb|$s| equidistant numbers from \verb|$a| exclusively to \verb|$b| inclusively.

	\subsection*{noise}

	\verb|noise($f, $i, $n, $t)| returns noise data of duration \verb|$t| or \verb|$s| that contains \verb|$n| or a hundred frequencies lying inside a range over \verb|$i| half tones or one octave centered geometrically around a given \verb|$f| or the standard pitch.

	\subsection*{ext/rex}

	\verb|ext($n)| returns the {\it 80 bit IEEE Standard 754} representation of the number \verb|$n|, \verb|rex| vice versa.
\end{document}


\part{Expressions}
\label{part:Expressions}
Expressions are the difference between a hard coded package and one that gracefully responds to change. Most every object
in the SSIS world supports the concept of expressions. An expression is just that, it is code that, when evaluated, expresses a new value. It can be important to note that expressions are evaluated each time they are used. This might be a good behaviour if you want to capture the exact moment a row is inserted into a table. It might not be desirable if your output file name changes
every second.

\chapter{Date and time}
\label{chap:Date and time}
We often need to generate a date string in various formats. A common challenge encountered is getting that format just right.
Unless you have reasons to the contrary, using a format of year, month and date, all numeric, results in a sortable date which
makes it easier to find specific files in large collections.

\paragraph{Getting a date}
\label{par:Getting a date}
There are different mechanisms for acquiring a date. A popular approach is to use \verb+GETDATE()+ \href{http://msdn.microsoft.com/en-us/library/ms139875.aspx}{GETDATE(SSIS Expression)}

It's built into the SSIS Expression language but I find that the System scoped variable, \verb+StartTime+ provides a better reference point as it is evaluated only when the package begins. This is in contrast to GETDATE which is evaluated each time it is inspected. For a long running package, the drift in values may not be acceptable. It might also be \begin{em}exactly\end{em} what is desired. Assume we have a Derived Column transformation in a package which adds a column named InsertDate into our buffer.

Consider the following two queries, which would more correctly describe the data loaded from today's load?

Do you look for all the rows loaded at a specific point in time?
\begin{lstlisting}[language=SQL]
SELECT
    T.*
FROM
    MyTable AS T
WHERE
    T.InsertDate = '2015-01-17T12:24:48.123';
\end{lstlisting}

Or do you look for all the rows loaded between an interval?
\lstinputlisting[language=SQL]{Expressions/InsertDate.Scan.sql}

The ``right'' answer is neither---we should instead track data loads through an alternate means that supports tracking of concurrent loads.

\section{YYYY}
\label{sec:YYYY}
This expression defines how to get a 4 digit year. This is sometimes referred to as CCYY (century, century, year, year). The expression simply applies the \verb|YEAR()| function to a variable which returns an integer value.

\lstinputlisting[language=XML]{Expressions/YYYY.exp}

To make use of it, it's been my experience that we will be using this value for string concatenation instead of mathematical operations: addition, subtraction, etc. Therefore, we explicitly cast the result of the \verb|YEAR()| as a unicode string, \verb|DT_WSTR| of length 4

The following snipped expresses the Biml required to generate an SSIS variable which uses the above expression.
\lstinputlisting[language=XML]{Expressions/YYYY.biml}

\section{YY}
\label{sec:YY}
This expression defines how to get a 2 digit year. We apply the \verb|RIGHT()| function and get the last 2 digits. There is no need to explicitly cast to a string as it is handled by the function call.

\verbatiminput{Expressions/YY.exp}

Biml
\verbatiminput{Expressions/YY.biml}

\section{MM}
\label{sec:MM}
This expression defines how to get a 2 digit month such that March is 03 instead of 3. The leading zero is important for character based sorting. We will use the \verb|MONTH()| function which returns an integer from 1 to 12. We convert that to a string, prepend a zero to it and then shear off the last two characters using the \verb|RIGHT()| function. The trimming is only required for October, November, and December but the logic is cleaner to unconditionally apply \verb|RIGHT()|.

\verbatiminput{Expressions/MM.exp}

Biml
\verbatiminput{Expressions/MM.biml}

\section{DD}
\label{sec:DD}
This expression defines how to get a 2 digit day such that instead of 9 we get 09. It is the same process used in MM except with a call to  \verb|DAY()|. We continue to prepend a zero to our value and shear off the last two digits.

\verbatiminput{Expressions/DD.exp}

Biml
\verbatiminput{Expressions/DD.biml}

\section{YYYY-MM-DD}
\label{sec:YYYY-MM-DD}
The most common format is a year, month and day separated by a dash. This recipe uses what we've built in sections \ref{sec:YYYY}, \ref{sec:MM} and \ref{sec:DD}.

verbatimimport

\verbatiminput{Expressions/YYYY-MM-DD.exp}

Biml
\verbatiminput{Expressions/YYYY-MM-DD.biml}

\section{YYYYMMDD}
\label{sec:YYYYMMDD}
A more compact version of the year, month, day string is represented below.

\verbatiminput{Expressions/YYYYMMDD.exp}

Biml
\verbatiminput{Expressions/YYYYMMDD.biml}

\chapter{File names}
\label{chap:File names}
File name operations are generally better served by using the .NET System.IO library as it offers robust error handling, file system translations, etc. For general use however, an expression can get the job done.

The following expressions assume we have a variable named @[User::CurrentFileName] define and populated with C:\textbackslash SSISData\textbackslash Client\textbackslash Input\textbackslash DailySales.csv

\section{Get file extension}
\label{sec:Get file extension}
To get the file extension, we'll three functions: \verb|RIGHT()|, \verb|FINDSTRING()|, and \verb|REVERSE()|. Working from the innermost parenthesis, we'll reverse the value for our string. We do this so that the first period we find {\em should} be the position where the file extension begins. This clearly doesn't work for cases where the file has no extension or where the extension is not the last portion of the filename {\em e.g. MyFile.txt.2015-02-01}.

Knowing the position where the file extension begins, we then pass that starting point for the call to \verb|RIGHT()|.

\verbatiminput{Expressions/FileExtension.exp}

This expression will return \verb|.csv|. If you want only \verb|csv|, then you will need to subtract one from the position passed to the \verb|RIGHT()| call. Thus, the final three characters in the above expression \verb|1))| become \verb|1) -1)|.

\section{Get file name}
\label{sec:Get file name}
To get the file name without the path, \verb|DailySales.csv| we'll apply the same concept as in section \ref{sec:Get file extension}. However, instead of searching for \symbol{42}, this time we'll look for the \textbackslash.

\verbatiminput{Expressions/FileName.exp}

The double backslash, \textbackslash \textbackslash , in the above formula is not a typo. The backslash character is used to escape control codes in strings and in this case, it escapes itself.

\section{Get file path}
\label{sec:Get file path}
To get the path \verb|C:\SSISData\Client\Input\|, we need to keep everything to the left of the final path separator, the \textbackslash. The 2005 release of SSIS, did not include a \verb|LEFT()| expression. We had \verb|RIGHT()| and we had \verb|SUBSTRING()|. It was argued that \verb|LEFT| was really just a case of SUBSTRING with a starting position of $1$, but the convenience and inconsistent behaviour when compared to other languages lead to the introduction of \verb|LEFT| with 2008

TODO: verify release introduction

Our expression becomes

\verbatiminput{Expressions/FilePath.exp}

The 2008 and forward releases of SSIS allow for the use of the slightly more compact expression

\verbatiminput{Expressions/FilePath2008.exp}

The similar caveat exists here as in \ref{sec:Get file extension} -- this generates the file path with the trailing backslash intact, {\em i.e.} C:\textbackslash SSISData\textbackslash Client\textbackslash Input\textbackslash . If you need C:\textbackslash SSISData\textbackslash Client\textbackslash Input, then you will need to subtract one from the final argument to SUBSTRING/LEFT {\em i.e.} \verb|)-1)|

\section{Get file name without extension}
\label{sec:Get file name without extension}
There are different recipes you can use to get the base file name, \verb|DailySales|, but they generally take the form of eiter perform substring operations to trim the path and extension or you can use the \verb|REPLACE()| function to strip out the known elements.

\chapter{Padding strings}
\label{chap:Padding strings}
A pad operation fills out a string with a given pattern. The reverse of padding a string would be trimming.

\section{Left pad}
\label{sec:Left pad}
In the U.S., a postal code is a five digit string. Tools like Excel interpret that data as numeric and thus 00532 becomes 532. Calling a left pad function, we could get our leading zeros back.

\section{Right pad}
\label{sec:Right pad}
A right pad operation may be needed if you were providing text input for printing a check. Assume the issued amount is \$100.30. The trailing zero is not needed from a mathematics perspective so the numeric value written would be \$100.3 That's going to look odd.

\section{Trim to null}
\label{sec:Trim to null}
This expression only works in the context of a Data Flow. An SSIS Variable cannot be set to NULL. If you're dealing with fixed width files, that file transport mechanism does not have the ability to express that something lacks a value. There is no NULL in that column, it will simply be the space character. A Derived Column can be used with the following expression to ensure we make an string filled with whitespace is turned into a NULL for storage.


\documentclass{report}
\input{../preamble}

\begin{document}

\thispagestyle{FirstPage}
\begin{center}
\textbf{\large Extra problems to save for review/backup}
\end{center}
\vspace{1cm}

{\huge More relevant}

\section*{Problem}

A reactor is operating for a long time at some known power density $P_0$. Then, it instantaneously changes power to some power density $P_1$. One fission product of interest is $^{135}$Xe, though it has a neglible yield from the initial fission reaction. $^{135}$Xe precursors $^{135}$Te and $^{135}$I are produced with a combined yield of approximately 6\%, before decaying via $\beta^{-}$ decay to $^{135}$I and $^{135}$Xe respectively. Find the number density of $^{135}$Xe as a function of time after the power change. (Your solution may be left as variables)

\begin{table}[htbp]
	\centering
	\begin{tabular}{|c|c|c|}
			\hline
			Nucleus		&	Half-life 	& Thermal $\sigma_{\text{a}}$ \\
			\hline
			$^{135}$Te	&  $19.0$ s 	& $\sim 0$\\
			$^{135}$I	&  $6.6$ hr 	& $\sim 0$\\
			$^{135}$Xe	&  $9.2$ hr 	& $2.6 \times 10^6$ barns \\
			\hline
	\end{tabular}
	\label{tab:design-specs}
\end{table}

\textbf{Walkthrough of neutron slowing down}\\
(1) Generate a neutron from fission-neutron energy spectrum\\
(2) Find cross section at that point\\
(3) Determine if more likely to scatter/absorb; choose higher prob ((hopefully scatter))\\
(4) Decrease by average energy loss\\
(5) Repeat steps 2/3 until absorbed\\
Identify interaction points on fission cross section plot... show "skipped resonances"

\vspace{4cm}
{\huge Less relevant}

\section*{Problem}

Recall from mechanics that centripetal force is $F_{\text{cent}} = -\frac{mv^2}{r}$ and recall from E\&M that the Coulombic force is $F_{\text{coul}} = -\frac{Ze^2}{r^2}$. Solve for the Bohr radius of the orbit of an electron on hydrogen, assuming the angular momentum $L = mvr$ is quantized multiples of $\hbar$ ($1\hbar, \; 2\hbar,\; 3\hbar$, etc). Compare this to the measured value of $5.2917721067(12)\times10^{11} \text{\AA}$ , the most probable distance between an electron in the ground state and the nucleus of a hydrogen atom.

\end{document}


\documentclass[11pt]{article}

\usepackage{alltt,fullpage,graphics,color,epsfig,amsmath, amssymb}
\usepackage{hyperref}
\usepackage{boxedminipage}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{amsmath}
\usepackage{commath}
\usepackage{graphicx}
\graphicspath{ {.} }
\newcommand{\floor}[1]{\lfloor #1 \rfloor}
\newcommand{\ceil}[1]{\lceil #1 \rceil}

\title{CS 512 Assignment 2}
\author{Daniel Campos}
\date{April 25th,2021}
\begin{document}
\maketitle
\section{Problem 1}
\subsection{Short Answers}
\subsubsection{What is overfitting? What are two techniques that can mitigate this issue and explain why they handle overfitting.}
Over fitting is when a machine learn method trains to emulate the training data too closely. In other words the model has overfit its learned solution to the training data to such a degree where the performance seen on the training data is far beyond what is seen on a sample that has not been trained on. \\
One way to get around this is to always keep a portion of the dataset for validation and evaluation which you do not train on. The validation corpus can be used to tweak model performance and the evaluation portion is used as a final blind verification of model performance. This handles overfitting because it keeps a machine learning engineer honest. The validation provides a observable insight into how the model is performing on data that has not been trained on and the evaluation portion provides a complete blind evaluation which is an indication of how well this model might perform on a broader unseen data.\\
Another mechanism that can be used to mitigate overfitting is dropout. Dropout is a mechanism which each neuron in the network has some probability that it will be turned off in during the forward pass of training. This makes the network become an ensemble of sort with multiple networks working withing the larger neural network. This prevents overfitting because having ensembles of neural networks provide a broader estimation function for the training data and thus less likely to overfit.
\subsubsection{Compare LSTM and GRU, what do they have in common and what are their differences}
LSTM's and GRU's are two different Recurrent Neural Network (RNN) architectures. RNNs are designed to process inputs in series (like time series or language data) and do so by passing a hidden state from one state to the next. GRU and LSTM have learned gates which allow them to learn what information should be encoded and what information should not be encoded in the hidden state. They are similar because they both provide methods of updating and modifying what is remembered at each iteration but they differ in their structure. GRU only feature update and reset gate while the LSTM features a update, forget and input gate. As a result LSTMs can perform better on more complicated inputs but are computationally more expensive to train/predict and they require more data to train. In general GRU's will train and infer quicker and out perform LSTM with small datasets but as the dataset grows LSTMs will outperform GRUs.
\subsection{Back Propagation}
\subsubsection{Prove the error equation for unit $U_6$ and the weight update function for the same neuron}
To calculate the degree which unit six effected the loss is equivalent to asking what effect did the output of unit 6 have on the loss. This in turn can be viewed as what effect did the out of unit 6 multiplied by its connections to units 8 and 9 and their in turn connection to unit 10, the output unit. Via the chain rule we have $\frac{\partial L}{\partial_6} = \sum_k (\frac{\partial L}{\partial_k} * \frac{\partial_k}{\partial a_6} * \frac{\partial a_6}{\partial O_6} \frac{\partial O_6}{\partial w_{i,j}})$ where $a_6$ is the output of unit six and $z_6$ is the input of unit six. Now, we address each term separately: \\
$\frac{\partial L}{\partial_k} = - \partial_k$ via the definition of $\partial$ \\
$\frac{\partial_k}{\partial a_6} = \frac{\partial}{\partial a_6} (\sum_s a_s w_{s,k}) = w_{j,k}$ as the only term in the sum that involves $a_6$ is $a_6 w_{j,k}$\\
$\frac{\partial a_6}{\partial z_6} = \frac{\partial}{\partial O_6} g(O_6)= g'(0_6) = g(O_6)(1-g(0_6)) = 0_6(1-O_6)$ where g is the sigmoid function.\\
$\frac{\partial O_6}{\partial w_{6,j}} = a_j$ since the only term that involves $w_{6,j}$ is $a_i w_{i,j}$ \\ \\
Thus combining it all $\frac{\partial L}{\partial_6} = \sum_k (-\partial_k w_{6,k} a(1-a_j) O_6)$
which we simplify to $\partial_6 = O-6(1-O_6)(\sum_k (\partial_k w_{6,k}))$ and since unit six is only connected to unit 8 and 9 we further simplify to $\partial_6 = O-6(1-O_6)(\partial_8 w_{6,8}) + \partial_9 w_{6,9})$. \\
The update is derived from $\frac{\partial L}{\partial w_{i,j}} = -\partial_j O_i$ which because of the gradient descent technique we add  the learning rate and get $\delta w_{i,j} = n \partial_j O_i$
\subsubsection{Derive the equation which computes the error for output unit $U_10$ for cross entropy function}
Derive the equation which computes the error for the output unit $U_10$
First, We know that with the cross entropy error function where D is the sample being evaluated $L= -\sum_k [t_k *log(o_k) + (1-t_k)log(1-o_k)]$. This becomes \\
$\frac{\partial L}{\partial O} = \frac{\partial}{\partial O} \sum_k [t_k log(O_k) + (1-t_k)log(1-o_k)$ . \\
$\frac{\partial L}{\partial O} = \frac{\partial}{\partial O} t log(O) - \frac{\partial}{\partial O} (1-t)log(1-O)$ . \\
$\frac{\partial L}{\partial O} = \frac{t}{O} + \frac{1-t}{1-O}$. \\
This means in turn that the $\partial_{10} = (\frac{t}{O} + \frac{1-t}{1-O})$
\subsection{2D Convolution}
\subsubsection{Compute the feature maps by applying kernel $K_1$, $K_2$ (stride = 1)}
First its important to note that because there is no specification about what form of padding to use I am not using any padding. This means that our 5x5 input becomes a 4x4 output. The feature map after applying $K_1$ can be found in table \ref{tab:conv1-k1} and the feature map after applying $K_2$ can be found in table \ref{tab:conv1-k2}.
\begin{table}[]
\begin{tabular}{|l|l|l|l|} \hline
2 & 0 & 0 & 2 \\ \hline
0 & 0 & 2 & 0 \\ \hline
1 & 1 & 0 & 2 \\ \hline
1 & 0 & 2 & 0 \\ \hline
\end{tabular}
\caption{Resulting feature map after application of K1 on Input}
\label{tab:conv1-k1}
\end{table}
\begin{table}[]
\begin{tabular}{|l|l|l|l|} \hline
1 & 1 & 1 & 1 \\ \hline
1 & 1 & 1 & 2 \\ \hline
0 & 1 & 2 & 1 \\ \hline
1 & 1 & 1 & 2 \\ \hline
\end{tabular}
\caption{Resulting feature map after application of K2 on Input}
\label{tab:conv1-k2}
\end{table}
\subsubsection{Compute the new feature map after we apply average pooling with 2x2 filter from previous work}
Average pooling for the feature map extracted by using kernel $K_1$ can be found in table \ref{tab:conv-k1-average}, and table \ref{tab:conv1-k2-average} for the average pooling on the feature map extracted by $K_2$.
\begin{table}[]
\begin{tabular}{|l|l|} \hline
0.5 & 1 \\ \hline
0.75 & 1 \\ \hline
\end{tabular}
\caption{Average pooling for feature map using K1 on input}
\label{tab:conv-k1-average}
\end{table}
\begin{table}[]
\begin{tabular}{|l|l|} \hline
1 & 1.25 \\ \hline
0.75 & 1.5 \\ \hline
\end{tabular}
\caption{Average pooling for feature map using K2 on input}
\label{tab:conv1-k2-average}
\end{table}
\subsection{Graph Convolutional Networks}
\subsubsection{Is this task inductive learning or transductive learning}
This is inductive learning because we only see a portion of the dataset while training (the train portion: 60\%) and then we apply the learned model on dataset it has never seen before (validation/eval data portions)
The main difference is that during transductive learning, you have already encountered both the training and testing datasets when training the model. However, inductive learning encounters only the training data when training the model and applies the learned model on a dataset which it has never seen before.
\subsubsection{Implement this model and evaluate and report accuracy}
Final Loss on eval: 0.8361107707023621 and final accuracy:0.7458563535911602
\subsection{Vary the optimizer and learning rate strategies and report numbers}
First, using ADAM I vary the learning rate and training length as seen in table \ref{tab:1d}. Next, I vary the best LR (1e-1 ) and experiment with ADAM across SGD, and RMSProp finding ADAM to be the best as shown in table \ref{tab:1de}
\begin{table}[]
\begin{tabular}{|l|l|l|l|} \hline
Learning Rate & Updates & Loss & Accuracy \\ \hline
1e-1 & 100  & 0.7455894351005554 & 0.7495395948434622 \\ \hline
1e-1 & 1000  & 0.6971593499183655 & 0.7569060773480664 \\ \hline
1e-1 & 10000  & \textbf{0.6468987464904785} & \textbf{0.7992633517495397}  \\ \hline
1e-2 & 100 & 1.1029239892959595 & 0.6611418047882136 \\ \hline
1e-2 & 1000  & 0.6627088189125061 & 0.7808471454880295 \\ \hline
1e-2 & 10000  & 0.6523106694221497 & 0.7974217311233887 \\ \hline
1e-3 & 100  & 1.832948923110962 & 0.3020257826887661 \\ \hline
1e-3 & 1000  & 1.0243993997573853 & 0.6758747697974218 \\ \hline
1e-3 & 10000  & 0.6788013577461243 & 0.7808471454880295 \\ \hline
1e-4 & 100  & 1.9703178405761719 & 0.0865561694290976 \\ \hline
1e-4 & 1000  & 1.8301498889923096 & 0.3020257826887661 \\ \hline
1e-4 & 10000  & 1.0697520971298218 & 0.6666666666666667 \\ \hline
1e-5 & 100  & 1.9364348649978638 & 0.18968692449355434 \\ \hline
1e-5 & 1000  & 1.9022825956344604 & 0.3001841620626151 \\ \hline
1e-5 & 10000  & 1.8908064365386963 & 0.27808471454880296 \\ \hline
\end{tabular}
\caption{Effect of varying learning rate and training duration for GCN}
\label{tab:1d}
\end{table}

\begin{table}[]
\begin{tabular}{|l|l|l|l|} \hline
Optimizer & Updates & Loss & Accuracy \\ \hline
ADAM & 100   & 0.7455894351005554 & 0.7495395948434622 \\ \hline
ADAM & 1000   & 0.6971593499183655 & 0.7569060773480664 \\ \hline
ADAM & 10000 & \textbf{0.6468987464904785} & \textbf{0.7992633517495397} \\ \hline
SGD & 100  & 1.822108507156372 & 0.3020257826887661 \\ \hline
SGD & 1000   & 1.05472731590271 & 0.6261510128913444 \\ \hline
SGD & 10000   & 0.8426060676574707 & 0.7679558011049724 \\ \hline
RMSProp & 100  & 1.5735245943069458 & 0.40699815837937386 \\ \hline
RMSProp & 1000  & 1.5078521966934204 & 0.3830570902394107 \\ \hline
RMSProp & 10000  & 1.0892350673675537 & 0.5948434622467772 \\ \hline
\end{tabular}
\caption{Effect of varying optimizer and training duration for GCN}
\label{tab:1de}
\end{table}
\subsubsection{If the number of GCN layers increase how does the model performance change in terms of quality and efficiency}
If we increase the layers of the model we can model more complex data but do so with a computational overhead. In other words, a deeper network (more layers) is more computationally expensive to run and has a greater ability to fit the data. A deeper network can produce better accuracy but can also more easily overfit to the training data. When we add a third layer to our model our performance goes from 0.7569 to 0.784530 but the time to train goes from 1m13.765s to 1m38.955s and while the accuracy is higher for the deeper model its loss on eval is higher (0.7600502967834473 vs. 0.6616357564926147) indicating the model is overfitting.
\section{Outlier Detection}
\subsection{Short Answers}
\subsubsection{From the perspectives of outlier detection methods give two possible definitions of outliers}
One definition of an outlier would be a data point that differs so much from the expected data distribution as to make it clearly not part of the regular distribution. Another definition would be a data point which has a significant effect on the conditional probabilities learned while studying data as models will over compensate in order to fit this out of distribution sample.
\subsection{What is the disadvantage of describing outliers as any point that is larger than the majority of values? How do we modify our definition of outliers to avoid this problem? Use examples }
This method will miss any value that is an outlier on small scale (both close to 0 and negative) and also will fail to take into account natural distribution of large values. One way to avoid this problem is to build a notion of outliers based on the distribution of the data like Grubbs test. Instead of calling extremely large values outliers we refer to values which our outside of a Gaussian distribution as outliers.
\subsection{Implementation using $PyOD^3$}
The three methods I ran were KNN, PCA, and VAE results for runs on vowels.mat can be found in table \ref{tab:vowels} and results for runs on satimage-2 can be found in table \ref{tab:satimage}. At a broad level we see that more simple data like that of vowels, KNN performs best(and trained fastest) but as data gets harder Neural (VAE) and more complex methods (PCA) out perform. That being said neither PCA or VAE achieves the highest of scores on the satellite data as the feature space is small.
\begin{table}[]
\begin{tabular}{|l|l|l|l|} \hline
Metric        & KNN & PCA     & VAE  \\ \hline
Precision @10 & 0.00/0.00       & 0.00/0.0    &  0.00/0.0   \\ \hline
Precision @20 & 0.00/0.00       & 0.00/0.0    &  0.00/0.0     \\ \hline
Precision @50 & \textbf{0.4/0.3571}      & 0.1429/0.105 & 0.1538/0.1136 \\ \hline
Precision @100 & \textbf{0.4/0.3571}      & 0.1429/0.1053 & 0.1538/0.1136 \\ \hline
ROC AUC Score & \textbf{0.8868/0.8955}   & 0.6336/0.6267 & 0.6372/0.6625 \\ \hline
Recall Score  & \textbf{0.8235/0.8333}   & 0.3529/0.3333 & 0.3529/0.4167 \\ \hline
\end{tabular}
\caption{Model performance on vowel dataset vowels.mat}
\label{tab:vowels}
\end{table}
\begin{table}[]
\begin{tabular}{|l|l|l|l|}\hline
Metric        & KNN & PCA     & VAE  \\ \hline
Precision @10 & 0.00/0.00       & 0.00/0.0     &  0.00/0.0   \\ \hline
Precision @20 & 0.00/0.00       & 0.00/0.0     &  0.00/0.0   \\ \hline
Precision @50 & 0.00/0.00       & 0.00/0.0     &  0.00/0.0   \\ \hline
Precision @100 & 0.00/0.00      & 0.00/0.0     &  0.00/0.0   \\ \hline
Precision @1000 & 0.0914/0.0983 & \textbf{0.1024/0.1264} & 0.0994/0.1223  \\ \hline
ROC AUC Score & 0.8666/0.8691   & 0.9331/0.9304& \textbf{0.9546/0.9252} \\ \hline
Recall Score  & 0.8182/0.8421   & 0.9545/0.9474& \textbf{1.0/0.9474} \\ \hline
\end{tabular}
\caption{Model performance on Satellite images dataset satimage-2.mat}
\label{tab:satimage}
\end{table}
\section{Gaussian Mixture Model}
\subsection{Compared with K-Means, what are the advantages and disadvantages of Gaussian mixture model}
The advantages of GMM over KNN are related to how a Gaussian models a cluster. First off, since a GMM is a probability distribution we can get confidence scores for each of these clusters which can help us decide if a data point may belong to more than one cluster. Additionally GMM can outperform KNN as they can man model more oblong clusters of data while KNN is limited to modeling circular clusters while Gaussian can become oblong shapes. This works well for data that does not have a circular distribution.  \\

The disadvantages of GMM are mostly related to the model size. There are a lot of parameters to fit which means that there are many iterations and large data is required for training. If there are few samples in the train dataset KNN may still have good performance while GMM will likely not.
\subsection{E-Step:Prove that the posterior probability of the k-th component $\gamma_{i,j}$}
First from bayes theorem $p(z_i = K| x) = \frac{p(z_i = k)p(x|z_i = k)}{\sum_{j=1}^k p(z_j = k)p(x| z_j = k)}$. \\
which since the prior probability $p(Z_i=k) = \pi_k$ we can rewrite $\gamma_{i,k} = p(z_i = K| x_i) = \frac{\pi_k  N(x_i | \mu_k , \sigma_k)}{\sum_k N(x_i| \mu_k, \sigma_k)}$
\subsection{M-Step:Prove that the new estimations of $\mu_k$, $\sigma_k$, $\pi_k$}
The approach we use to prove all three estimates is the same: take the derivative with respect to the variable set to 0.
First for $u_k$: $\frac{\partial}{\partial \mu_k}= \sum_{i=1}^n \frac{\pi_k N(x_i| \mu_k, \sigma_k)}{\sum_{k=1}^K \pi_k N(x_i| \mu_k, \sigma_k)} \frac{x_i - \mu_k}{\sigma_k^2} = 0$ which given the E step can be rewritten as $\sum_{i=1}^n \gamma_{i,k}\frac{x_i - \mu_k}{\sigma_k^2} = 0 $ which can be simplified to $\frac{\sum_{i=1}^n \gamma_{i,k}x_i}{\sigma_k^2}  - \frac{\sum_{i=1}^n \gamma_{i,k}\mu_k}{\sigma_k^2} = 0 $ which then becomes $\frac{\sum_{i=1}^n \gamma_{i,k}x_i}{\sigma_k^2}  = \frac{\sum_{i=1}^n \gamma_{i,k}\mu_k}{\sigma_k^2}$, then $\frac{\sum_{i=1}^n \gamma_{i,k}x_i}{1}  = \frac{\sum_{i=1}^n \gamma_{i,k}\mu_k}{1}$, $\frac{\sum_{i=1}^n \gamma_{i,k}x_i}{\sum_{i=1}^n \gamma_{i,k}}  = \mu_k$ which is our target. \\\\
Following the same logic for $\sigma_k^2$ we first take the derivative and set to 0 we get $\frac{\partial }{\partial \sigma_k} =  (\sum_{n=1}^n \gamma_{n,k}(x_n-u_k)(x_n-u_k)) - (\sum_i \gamma_{i,k} \sigma_k^2)$ which leads to $(\sum_{n=1}^n \gamma_{n,k}(x_n-u_k)(x_n-u_k)) = (\sum_i \gamma_{i,k} \sigma_k^2)$, then $\frac{\sum_{n=1}^n \gamma_{n,k}(x_n-u_k)^2}{\sum_i \gamma_{i,k}} =  \sigma_k^2$. \\
Finally, to find the value of $\pi_k$ we must take into account mixing coefficients must sum to one. We find the maximum by using the Lagrangian multiplier and taking its derivative and setting to one. this gives $\ln p(x_i| \pi_k, \mu_k, \sigmoid_k) + \lambda (\sum_k \pi_k -1)$ which the derivative with respect to $\pi_k$ is $\frac{1}{N}*\sum_i \gamma_{i,k}$ which is our desired output
\section{Graph Connectivity and SIS model}
\subsection{Implement the Susceptible-Infected-Susceptible and graph number of people in each group}
For the initial implementation there are an average of 6876.03 infected nodes
after the convergence(200-300 iterations). The plot for susceptible and infected individuals can be found  in figure \ref{fig:sis}.
\begin{figure}
    \centering
    \includegraphics{Assignments/Assignment2/sis.png}
    \caption{Effect of Random Vaccination in virus propagation}
    \label{fig:sis}
\end{figure}
\subsection{Vaccination}
For random vaccination of 200 nodes the average infected nodes after convergence is 6645.46 and the plot for susceptible and infected individuals can be found  in figure \ref{fig:vacrand}.
For those with highest degree there is an average we achieve a slightly lower rate of 6215.99 and the plot for infection growth over time can be found in figure \ref{fig:highdegree}.
\begin{figure}
    \centering
    \includegraphics{Assignments/Assignment2/random-vac.png}
    \caption{Effect of Random Vaccination in virus propagation}
    \label{fig:vacrand}
\end{figure}
\begin{figure}
    \centering
    \includegraphics{Assignments/Assignment2/high-degree.png}
    \caption{Effect of High Degree Vaccination in virus propagation}
    \label{fig:highdegree}
\end{figure}
\subsection{More Vaccination}
I explore three strategies for vaccination: proximity to infected nodes at time step 0, highest connection via 2 hops, random walk. \\
For proximity to infected nodes I generate a ranking by how heavily nodes are connected to infected node. The 200 susceptible nodes with highest connection to infected nodes become vaccinated. This averages to 6342.96 infected individuals after convergence and results over time can be seen in figure \ref{fig:infection}. This method is more effective than any random method but not as effective at vaccinating the highest degree nodes. \\
For highest connection via two hop we build on the notion of highest degree nodes but take the highest number of nodes available within 2 hops (Node-Edge-Node). This averages to 6335.06 infected individuals after convergence and results over time can be seen in figure \ref{fig:2hop}. \\
For Random walk we start at an random node and vaccinate it and then walk to one of its edges and make that our new vaccination node. We continue this process until we have used all of our vaccination. This averages to 6620.56 infected individuals after convergence and results over time can be seen in figure \ref{fig:randwalk}. We can see this method is more effective than random but not by much.
\begin{figure}
    \centering
    \includegraphics{Assignments/Assignment2/two_hop.png}
    \caption{Effect of Two Hop Vaccination in virus propagation}
    \label{fig:2hop}
\end{figure}
\begin{figure}
    \centering
    \includegraphics{Assignments/Assignment2/infection-proximity.png}
    \caption{Effect of Infection proximity Vaccination in virus propagation}
    \label{fig:infection}
\end{figure}
\begin{figure}
    \centering
    \includegraphics{Assignments/Assignment2/random-walk.png}
    \caption{Effect of Random Walk Vaccination in virus propagation}
    \label{fig:randwalk}
\end{figure}
\end{document}


\begin{appendices}
    \section{Accuracy Benchmarking Results}
    \subsection{AMSIMP}
    \subsubsection{Mean Absolute Error}
    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/temperature/mean_absolute_error.png}
        \caption{AMSIMP Air Temperature Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/virtual_temperature/mean_absolute_error.png}
        \caption{AMSIMP Virtual Temperature Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/geopotential_height/mean_absolute_error.png}
        \caption{AMSIMP Atmospheric Pressure Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/zonal_wind/mean_absolute_error.png}
        \caption{AMSIMP Zonal Wind Mean Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/meridional_wind/mean_absolute_error.png}
        \caption{AMSIMP Meridional Wind Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/relative_humidity/mean_absolute_error.png}
        \caption{AMSIMP Relative Humidity Mean Absolute Error}
    \end{figure}

    \subsubsection{Root Mean Squared Error}
    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/temperature/root_mean_squared_error.png}
        \caption{AMSIMP Air Temperature Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/virtual_temperature/root_mean_squared_error.png}
        \caption{AMSIMP Virtual Temperature Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/geopotential_height/root_mean_squared_error.png}
        \caption{AMSIMP Atmospheric Pressure Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/zonal_wind/root_mean_squared_error.png}
        \caption{AMSIMP Zonal Wind Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/meridional_wind/root_mean_squared_error.png}
        \caption{AMSIMP Meridional Wind Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/relative_humidity/root_mean_squared_error.png}
        \caption{AMSIMP Relative Humidity Root Mean Squared Error}
    \end{figure}

    \subsubsection{Mean Absolute Scaled Error}
    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/temperature/mean_absolute_scaled_error.png}
        \caption{AMSIMP Air Temperature Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/virtual_temperature/mean_absolute_scaled_error.png}
        \caption{AMSIMP Virtual Temperature Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/geopotential_height/mean_absolute_scaled_error.png}
        \caption{AMSIMP Atmospheric Pressure Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/zonal_wind/mean_absolute_scaled_error.png}
        \caption{AMSIMP Zonal Wind Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/amsimp/meridional_wind/mean_absolute_scaled_error.png}
        \caption{AMSIMP Meridional Wind Mean Absolute Scaled Error}
    \end{figure}

    \subsection{OpenWeatherAPI}
    \subsubsection{Mean Absolute Error}
    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/air_temperature/mean_absolute_error.png}
        \caption{OpenWeatherAPI Air Temperature Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/virtual_temperature/mean_absolute_error.png}
        \caption{OpenWeatherAPI Virtual Temperature Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/atmospheric_pressure/mean_absolute_error.png}
        \caption{OpenWeatherAPI Atmospheric Pressure Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/zonal_wind/mean_absolute_error.png}
        \caption{OpenWeatherAPI Zonal Wind Mean Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/meridional_wind/mean_absolute_error.png}
        \caption{OpenWeatherAPI Meridional Wind Mean Absolute Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/relative_humidity/mean_absolute_error.png}
        \caption{OpenWeatherAPI Relative Humidity Mean Absolute Error}
    \end{figure}

    \subsubsection{Root Mean Squared Error}
    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/air_temperature/root_mean_squared_error.png}
        \caption{OpenWeatherAPI Air Temperature Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/virtual_temperature/root_mean_squared_error.png}
        \caption{OpenWeatherAPI Virtual Temperature Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/atmospheric_pressure/root_mean_squared_error.png}
        \caption{OpenWeatherAPI Atmospheric Pressure Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/zonal_wind/root_mean_squared_error.png}
        \caption{OpenWeatherAPI Zonal Wind Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/meridional_wind/root_mean_squared_error.png}
        \caption{OpenWeatherAPI Meridional Wind Root Mean Squared Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/relative_humidity/root_mean_squared_error.png}
        \caption{OpenWeatherAPI Relative Humidity Root Mean Squared Error}
    \end{figure}

    \subsubsection{Mean Absolute Scaled Error}
    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/air_temperature/mean_absolute_scaled_error.png}
        \caption{OpenWeatherAPI Air Temperature Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/virtual_temperature/mean_absolute_scaled_error.png}
        \caption{OpenWeatherAPI Virtual Temperature Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/atmospheric_pressure/mean_absolute_scaled_error.png}
        \caption{OpenWeatherAPI Atmospheric Pressure Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/zonal_wind/mean_absolute_scaled_error.png}
        \caption{OpenWeatherAPI Zonal Wind Mean Absolute Scaled Error}
    \end{figure}

    \begin{figure}[H]
        \centering
        \includegraphics[width=.7\linewidth]{Graphs/accuracy/appendices/openweatherapi/meridional_wind/mean_absolute_scaled_error.png}
        \caption{OpenWeatherAPI Meridional Wind Mean Absolute Scaled Error}
    \end{figure}

    \clearpage

\end{appendices}


\documentclass[11pt]{article}
\date{}

\usepackage[margin=0.8in]{geometry}
\usepackage{amsmath,amsthm,amssymb,bm,graphicx,pdfpages,enumerate,enumitem,breqn,blkarray}
\newcommand*\tageq{\refstepcounter{equation}\tag{\theequation}}
\usepackage{graphicx, bbm, color, parskip}
\usepackage{multirow}
\newcommand{\indep}{\rotatebox[origin=c]{90}{$\models$}}
\setcounter{MaxMatrixCols}{20}

\usepackage{physics}
\usepackage[makeroom]{cancel}
\usepackage{listings}

\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\allowdisplaybreaks
\makeatletter
\newcommand{\colvect}{\rcvector}
\newcommand{\rowvect}{\rcvector}
\newcommand{\E}{\textrm{E}}
\newcommand{\Var}{\textrm{Var}}
\newcommand{\Cov}{\textrm{Cov}}
\newcommand{\Corr}{\textrm{Corr}}
\newcommand{\pf}{\begin{flushright}$\square$\end{flushright}}
\newcommand{\asreq}{\begin{flushright}...as required\end{flushright}}

\begin{document}

\title{\vspace{-0.7cm}The \texttt{stmpy} package}
\date{\today}
\author{Hoffman lab}
\maketitle

%----------------------------------------------------------

\section*{Methods in \texttt{stmpy.tools}:}

Descriptions are shown in the order functions appear in {\tt stmpy.tools}, which is vaguely chronologically.

\begin{enumerate}
\item {\tt saturate} - Designed to make it easy to set color limits on images. Adjusts color axis of current image handle by calculating a probability density function for the data in the current axis.  Uses upper and lower thresholds on the PDF to find sensible c-axis limits.

\item {\tt azimuthalAverage} - Given a point $\mathbf{p}=(x_0,y_0)$ in a 2D data set $F(x,y)$, computes the azimuthal average of the $F$ as a function of $r$ away from $\mathbf{p}$. Uses 2D interpolation on $F$ to get evenly spaced $r$ values.

\item {\tt azimuthalAverageRaw} - Computes a raw azimuthal average on 2D data $F(x,y)$.  This is similar to {\tt \small azimuthalAverage} but does not interpolate the data.  Instead the returned $r$ values are not linearly spaced, but follow the sequence: $1,\sqrt{2}, 2, \sqrt{5}...$

\item {\tt binData} - Puts non-linearly sampled data into linear bins.

\item {\tt linecut} - Simple algorithm for taking a line-cut on a 2D data set $F(x,y)$.  Uses interpolation to sample $F$ along a line from $(x_1,y_1)$ to $(x_2,y_2)$ with $n$ evenly spaced points.

\item {\tt squareCrop} - Crops a 2D image to be $m\times m$.

\item {\tt lineCrop} - Takes 1D data $y(x)$ and removes arbitrarily many sections to return $y(x(t_0:t_1, t_2:t_3, ...))$.

\item {\tt removePolynomial1d} - Removes an $n$ degree polynomial fit to 1D data $y(x)$. Optional: can specify the sections of $y(x)$ to use when fitting the background polynomial.

\item {\tt lineSubtract} - Acts on 3D or 2D data $A(\mathbf{r})$ to remove an $n$ degree polynomial from each line in $A(\mathbf{r})$.  Specifically, it iterates over the first index of $A(\mathbf{r})$ until to get 1D data $y(x)$  and implements {\small \tt removePolynomial1d} to remove the background.

\item {\tt fitGaussian2d} - Fit a 2D gaussian of the form $$f(x,y) = A \exp (-a(x-x_0)^2 +2b(x-x_0)(y-y_0) - c(y-y_0)^2 ) + B$$
to 2D data $F(x,y)$ given specified initial parameters: $A, B, x_0, y_0, \sigma_x,  \sigma_y, \theta.$

\item {\tt findOtherBraggPeaks} - For a Fourier transformed lattice, the Bragg peaks come in pairs at $\pm \mathbf{Q}_{B}$, with harmonics at  $\pm n\mathbf{Q}_{B}$ with $n\in \mathbb{N}$.  This function takes on Bragg peak and returns the $2n-1$ other Bragg peak locations for 2D data $F(x,y)$.

\item {\tt findPeaks} - Simple peak detection algorithm that returns the location of the $n$ highest peaks, $x^*$, in 1D data $y(x)$ by checking where the derivative crosses zeros, $y'(x^*)=0$.

\item {\tt fitGaussian1d} - Fits $N$ gaussians to 1D data $y(x)$ of the form $$f(x) = \sum_{n=0}^N A_n \exp (-\frac{(x-\mu_n)^2}{2\sigma_n^2}).$$

\item {\tt foldLayerImage} - Takes 3D data $A(\mathbf{r})$ and returns a $n$-fold symmetric 3D image $\tilde{A}(\mathbf{r})$, by iterating through the first index of $A(\mathbf{r})$ and symmetrizing the $i^{th}$ 2D layer, $A(E_i,x,y)$, about a specified fold direction.  The intended use is to symmetrize an FT-DOS map along the direction of a Bragg peak. Currently implemented for $n=1,2,4$ and all but replaced by {\tt \small symmetrize}

\item {\tt quickFT} - Computes a 2D Fourier transform of 2D or 3D data $A(\mathbf{r})$, with the option to $n$-fold symmetrize the result.  If 3D data is used the 2D Fourier transforms will be computed by iterating along the first index.

\item {\tt symmetrize} - Similar to {\tt \small foldLayerImage}, returns $n$-fold symmetric 2D or 3D data $\tilde{A}(\mathbf{r})$ by rotating clockwise and anti-clockwise by an angle $2\pi/n$, then applying a mirror line.  Works on 2D and 3D data sets, in the case of 3D each layer is symmetrized.

\item {\tt ngauss1d} - More general version of {\tt fitGaussian1d}, which allows any fit parameter to be fixed. Also returns information about the quality of fit.

\item {\tt track_peak} - Generalizes {\tt \small ngauss1d} to work on 2D data $F(x,y)$ by iterating the first index of $F(x,y)$. Only retains information about the position of the gaussian peaks, which track features that disperse in the $y$ direction.

\item {\tt shearcorr} - ... to be updated to include local drift correction.

\item {\tt planeSubtract} - Removes a 2D polynomial plane $P(x,y)$  from 2D data $F(x,y)$.  The polynomial is of the form:
$$ P(x,y) = a_0 + \sum_{k=1}^N a_{2k-1}\ x^k + a_{2k}\ y^k,$$
where $a_0,a_1,a_2, ...$ are the polynomial coefficients.

\item {\tt butter_lowpass_filter} - Implements a Butterworth filter for 1D data $y(x)$ or for each spectrum $g(E)$ in a 3D data set $g(E,x,y)$.

\item {\tt gradfilter} -  Applies a minimum gradient filter to extract dispersive features in a 2D data set $F(x,y)$ (Ref: arXiv:1612.07880).  Returns filtered data with optional gradient components for pseudo-vector-field and gradient modulus maps.

\end{enumerate}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%	1. Motivation
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Motivation}
\label{s:mot}

The \textit{interwar period} in the United States (1919-1939) remains one of the most discussed topics in the field of quantitative economic history. Enormous volatility in currency exchange rates, hyperinflation in Germany and the failing gold standard constitute some characteristics of this turbulent period. This seminar paper analyzes the \textit{interwar period} in the US using a dataset from \cite{reichsamt}. The relative importance of supply and demand shocks are examined applying a Blanchard-Quah decomposition\footnote{an application of a structural VAR model developped by \cite{blanchard}}. Subsequently the paper features a review of the obtained results and tries to assess the Blanchard-Quah decomposition regarding its suitability for such analysis.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%	2. Data
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Data}
\label{s:data}

The dataset includes monthly observations of Industrial Production (\textit{IP}) and Consumer Price Inflation (\textit{CPI}) figures for the years 1919-1939.\footnote{The obtained time series stems from \cite{reichsamt}.} Figure (\ref{plot}) shows a plot of the series. Three distinct sub-samples are created two examine the properties for each economic cycle in the \textit{Interwar Period} (\cite{feinstein}). Sub-sample (1) aims to reflect the \textit{Roaring Twenties} and includes observations from January 1920 until December 1928. Sub-sample (2) accounts for the \textit{Great Depression} and features figures for the timespan between October 1929 and February 1933. Sub-sample (3) includes observations reflecting the \textit{New Deal} programs in between April 1933 and June 1937.

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/data_plot.pdf}
  \vspace{3mm}
  \caption[US Output Growth \textit{(Industrial Production)} and Inflation \textit{(Consumer Price Index)}]{\textbf{US Output Growth \textit{(Industrial Production)} and Inflation \textit{(Consumer Price Index)}} (Log First Differences)}
  \label{plot}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%	3. Methodology
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\clearpage
\section{Methodology}
\label{s:methodology}

\subsection{Structural vector autoregression}
\label{s:structural}

In structural models (see equation (\ref{e:var})) there often are no possibilities to observe shocks that are of structural nature. Autoregressive models (VAR) are then estimated as alternative (see equation (\ref{eq:var2})).
\begin{equation}
{\bf Gx_t}={\bf Dx_{t-1}}+{\bf \epsilon_t}
		\label{e:var}
\end{equation}
where $
{\bf x_t}
=
\begin{bmatrix}
\Delta p_t \\
\Delta y_t \\
\end{bmatrix}
,
\quad
{\bf x_{t-1}}
=
\begin{bmatrix}
\Delta y_{t-1} \\
\Delta p_{t-1} \\
\end{bmatrix}
\quad
\textrm{and}
\quad
{\bf \epsilon_t}
=
\begin{bmatrix}
\epsilon_{y,t} \\
\epsilon_{p,t} \\
\end{bmatrix}
$

${\bf x_t}$ is a 2$\times$1 vector featuring output growth and inflation.
\begin{equation}
	{\bf x_t} = {\bf A(L)x_{t-1}} + {\bf u_t}
\label{eq:var2}
\end{equation}
where
$
{\bf A(L)}
=
{\bf G^{-1}D}
\quad
\textrm{and}
\quad
{\bf u_t}
=
{\bf G^{-1}}
{\bf \epsilon_t}
$

Estimates for ${\bf A(L)}$, the residuals ${\bf u_t}$ and the covariance matrix ${\bf \sum}$ are obtained by OLS. Equation (\ref{eq:ma}) shows the $MA(\infty)$ representation of such VAR model.
\begin{equation}
{\bf x_t}
=
{\bf B(L)u_t}
\label{eq:ma}
\end{equation}
Rearranging equation (\ref{eq:ma}) yields:
\begin{equation}
{\bf u_t}
=
{\bf G^{-1}\epsilon_t}
=
{\bf S\epsilon_t}
\label{eq:response}
\end{equation}
Equation (\ref{eq:res}) depicts the covariance matrix of ${\bf u_t}$:
\begin{equation}
{\bf \sum} = E \big[ {\bf u_t u'_t} \big] = {\bf S}E \big[ {\bf \epsilon_t \epsilon'_t} \big] {\bf S'} = {\bf SS'}
\label{eq:res}
\end{equation}
${\bf \epsilon_t}$ is assumed to be mutually uncorrelated with a normalized variance of 1. Therefore $var({\bf \epsilon_t})={\bf I}$.
Resolving equation (\ref{eq:res}) yields the identification matrix ${\bf S}$ (impact matrix)\footnote{The impact matrix maps structural shocks into residuals}.

\subsection{Blanchard-Quah decomposition}
\label{s:bq}
Implications of the traditional AS-AD model (\cite{favero}) assume supply shocks to have permanent and demand shocks to have temporary effects, on output whereas price effects are permanent for both, supply and demand shocks.

Blanchard-Quah decomposition (\cite{blanchard}) requires the implications of the traditional AS-AD model to be fulfilled (\cite{favero}). These implications are then sufficient to compute the long-run structural coefficient matrix:

\begin{equation}
\begin{bmatrix}
\Delta p_t \\
\Delta y_t \\
\end{bmatrix}
=
\begin{bmatrix}
(+) & 0 \\
(-) & (+) \\
\end{bmatrix}
\begin{bmatrix}
\epsilon^S  \\
\epsilon^S \\
\end{bmatrix}
\label{eq:lr}
\end{equation}
where: $
\begin{bmatrix}
(+) & 0 \\
(-) & (+) \\
\end{bmatrix}
=
{\bf C(1)}
\quad
\textrm{and}
\quad
\begin{bmatrix}
\epsilon^S  \\
\epsilon^S \\
\end{bmatrix}
=
{\bf \epsilon}$

${\bf C(1)}$ is lower triangular by restriction from the AS-AS model\footnote{as demand shocks have no long-run effects on output}. The long-run multiplier of the reduced form is denoted by:
\begin{equation}
{\bf B(1)}={\bf C(1)S^{-1}}
\label{eq:lrm}
\end{equation}
Choleski decomposition of (\ref{eq:chole}) leads to the long-run coefficient {\bf C(1)} which in turn facilitates the calculation of the identification matrix ${\bf S}={\bf B(1)^{-1}C(1)}$ and the structural moving average representation ${\bf x_t}={\bf C(L)\epsilon_t}={\bf B(L)S\epsilon_t}$.

\begin{equation}
  \begin{split}
		{\bf B}(1)\bs{\Sigma}{\bf B}(1)'=&{\bf C}(1){\bf S}^{-1}\bs{\Sigma}({\bf S}')^{-1}{\bf C}(1)'=\\=&{\bf C}(1){\bf S}^{-1}{\bf S}{\bf S}'({\bf S}')^{-1}{\bf C}(1)^		{\prime}=\\=&{\bf C}(1){\bf C}(1)'.
   \end{split}
\label{eq:chole}
\end{equation}

\clearpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%	4. Results
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Results}
\subsection{Impulse response functions}
\label{s:ir}
Figure (\ref{irt}) shows the impulse response functions \textit{(IR)} calculated on the basis off the entire dataset (years 1919-1939) . Figure (\ref{irr}) features impulse response functions on the \textit{Roaring Twenties} sub-sample (1920-1982). Figures (\ref{irg}) and (\ref{irn}) cover the \textit{Great Depression} (1929-1933) and the \textit{New Deal} era (1933-1937) respectively.

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/impulse_response.pdf}
  \vspace{3mm}
  \caption[Impulse response functions - entire dataset]{\textbf{Impulse response functions - entire dataset} (January 1919 - December 1939)}
  \label{irt}
\end{figure}

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/impulse_response_RT.pdf}
  \vspace{3mm}
  \caption[Impulse response functions - Roaring Twenties]{\textbf{Impulse response functions - Roaring Twenties} (January 1920 - December 1928)}
  \label{irr}
\end{figure}

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/impulse_response_GD.pdf}
  \vspace{3mm}
  \caption[Impulse response functions - Great Depression]{\textbf{Impulse response functions - Great Depression} (October 1929 - February 1933)}
  \label{irg}
\end{figure}

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/impulse_response_ND.pdf}
  \vspace{3mm}
  \caption[Impulse response functions - New Deal]{\textbf{Impulse response functions - New Deal} (April 1933 - June 1937)}
  \label{irn}
\end{figure}

The impulse response function indicate positive short-run output changes in response to a demand shock for the \textit{Great Depression} and \textit{New Deal} sub-samples. These reactions converge to zero in the long-run (which is in line with the restrictions imposed on the Blanchard-Quah decomposition (\cite{blanchard}). Several dependencies in line with the traditional AS-AD model can be observed. Prices tend to increase after a positive demand shock, whereas output tends to rise after positive supply shocks. Figure (\ref{irt}) shows negative short-run effects on output to positive demand shocks during the \textit{Roaring Twenties}. This result collides with the relationship assumed by the traditional AS-AD model.

\clearpage
\subsection{Forecast Error Variance Decomposition}
Figure (\ref{ft}) shows the forecast error variance decompositions of the impulse response functions covering the entire dataset, whereas figures (\ref{fr}), (\ref{fg}), (\ref{fg}) and (\ref{fn}) show the forecast error variance decompositions for the \textit{Roaring Twenties}, \textit{Great Depression} and \textit{New Deal} sub-sets respectively. The results indicate that variation in output is mainly driven by supply shocks whereas variation in prices is mainly driven by shocks in demand. As require by Blanchard-Quah specifications, long-run variation in output is entirely driven by supply shocks. In the short-run there seems to be no impact on output from demand shocks.

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/fevd.pdf}
  \vspace{3mm}
  \caption[Forecast error variance decomposition - Full data]{\textbf{Forecast error variance decomposition - entire dataset} (January 1919 - December 1939)}
  \label{ft}
\end{figure}

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/fevd_RT.pdf}
  \vspace{3mm}
  \caption[Forecast error variance decomposition - Roaring Twenties]{\textbf{Forecast error variance decomposition - Roaring Twenties} (January 1920 - December 1928)}
  \label{fr}
\end{figure}

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/fevd_GD.pdf}
  \vspace{3mm}
  \caption[Forecast error variance decomposition - Great Depression]{\textbf{Forecast error variance decomposition - Great Depression} (October 1929 - February 1933)}
  \label{fg}
\end{figure}

\begin{figure}[ht]
  \setstretch{1.0}
  \footnotesize
  \centering
  		\includegraphics[width=1\textwidth]{../../out/figures/fevd_ND.pdf}
  \vspace{3mm}
  \caption[Forecast error variance decomposition - New Deal]{\textbf{Forecast error variance decomposition - New Deal} (April 1933 - June 1937)}
  \label{fn}
\end{figure}

\clearpage

\section{Conclusion}
In general Blanchard-Quah decompositions lead to results further in line with textbook macroeconomic models when applied to post-World War II data (see e.g. \cite{blanchard2}). Applying Blanchard-Quah decomposition on the evaluated dataset may lead to several implications that seem counterfactual to the traditional AS-AD model (see section \ref{s:ir}). Blanchard-Quah decompositions seem to be of limited power when modelling the effects of structural shocks that occurred in the \textit{inter war} period (see e.g. \cite{eggertson} who claim that monopoly protection and union support backed by \textit{New Deal} policies were beneficial to output growth or \cite{comm} who highlight the effects of commodity markets disintegration after 1929).


\documentclass{elsart}
\usepackage{epsfig,amssymb,amsmath}
\begin{document}

\section{Drift velocity and gas gain}

As shown in the Langevin equation \cite{blum}[p.49], the drift velocity is a function of the field (electric, magnetic) and the mobility. The mobility depends on the gas density which is a function of the environment variables as well as the gas composition which can change in time.

$$v_d = v_d(E/N) = v_d(E,B,T,P,C_{CO_2},C_{N_2})$$

$E$ and $B$ are the field values (electric, magnetic), $N$ is the gas density, $P$ is the atmospheric pressure, $T$ is the temperature inside of the TPC and $C_{CO_2}$ and $C_{N_2}$ are two concentration out of three components of the drift gras $Ne,CO_2,N_2$ (90/10/5) within the TPC. We suppose that these parameters, especially the environment variables, will vary in time within a reasonable range. However, according to performed \textsl{Magboltz-2} \cite{magboltz} simulations a first order Taylor expansion of the dependencies around the nominal values is sufficient. More details can be found in section \ref{sec:magbolzSim}.
\begin{equation}
\Delta{v_d}=v_d-v_{d0}=\frac{dv}{dE}\Delta{E}+\frac{dv}{dN}\Delta{N(P,T)}+\frac{dv}{dC_{CO_2}}\Delta{C_{CO_2}}+\frac{dv}{dC_{N_2}}\Delta{C_{N_2}}
\label{equ:taylor}
\end{equation}

Within the TPC volume, the parameters in the expansion are changing with different time constant. A significant change of the drift velocity due to the gas composition changes has a time constant of days. On the other hand the changes due to the pressure and temperature variation have to be corrected on the level of minutes.
In the following we will focus on the influence of the changes of the gas density, temperature and pressure.
\begin{equation}
\frac{\Delta{v_d}}{v_{d0}}= k_t(t)+k_{N}\frac{\Delta{N(P,T)}}{N_0(P,T)}
\end{equation}
\begin{equation}
\frac{\Delta{v_d}}{v_{d0}}= k_t(t)+k_{P/T}\frac{\Delta{(P/T)}}{(P/T)_0}
\end{equation}

The factor of the time dependent offset $k_t(t)$ describes then the influence of the gas composition and possible changes within the field.

The correction factor $v_c=\frac{\Delta{v_d}}{v_{d0}}$ can be measured using different methods:
\begin{itemize}
\item Matching laser tracks with the surveyed mirror position
\item Matching with the ITS tracks
\item Matching of the TPC primary vertices from the two halves of the TPC
\item Using cosmic tracks - matching tracks from two halves of the TPC
\end{itemize}

The unknown parameters $k_t(t)$ and $k_N$ can be than fitted using the Kalman filter as is shown in section \ref{sec:kalman}.

\section{Simulation of drift velocity dependencies }
\label{sec:magbolzSim}

The state-of-the-art program \textsl{Magboltz-2} \cite{magboltz} can be used to calculate different drift properties by means of MonteCarlo (MC) methods as for example the drift velocity within a certain gas mixtures, under certain environment conditions and with any choosen field. Since MC simulations itself are too time consuming, a first order tayler expansion was used in order to fit various simulated data points under different conditions. Upper and lower tresholds for the simulated points were choosen to be within a reasonable range of possible changes within the TPC as shown in table \ref{tab:valRange}.  This approximation was implemented within the class \textsl{AliTPCCalibVDrift}. It allows to estimate the drift velocity, as a function of field and gas properties changes, quickly and with sufficient accuracy, at least for a first order calibration.\\

\begin{table}[htbp]
\caption{Dependency range of simulated drift velocities}
\centering
\begin{tabular}{|l|r|r|r|}
\hline
 & \multicolumn{1}{l|}{std.cond.} & \multicolumn{1}{l|}{MIN} & \multicolumn{1}{l|}{MAX}  \\ \hline
E [V/cm] & 400 & 395 & 405 \\ \hline
T [K]& 293 & 288 & 300  \\ \hline
P [TORR]& 744 & 719 & 759 \\ \hline
Co2 [\%]& 9.52 & 9.02 & 10.02  \\ \hline
N2 [\%]& 4.76 & 4.36 & 5.26  \\ \hline
\end{tabular}
\label{tab:valRange}
\end{table}

The following dependencies were obtained through fitting the simulated $v_d$'s with a linear hyperplane which is equal to the first order taylor expansion from equation (\ref{equ:taylor}).

\begin{eqnarray*}
  \frac{\partial v_d}{\partial E} & = & 0.24 \; [cm/V\mu s] \\
  \frac{\partial v_d}{\partial T} & = & 0.31 \; [cm/K\; \mu s]\\
  \frac{\partial v_d}{\partial P} & = & -0.13 \; [cm/Torr\; \mu s]\\
  \frac{\partial v_d}{\partial C_{CO_2}} & = & -6.60 \; [cm/\%\; \mu s]\\
  \frac{\partial v_d}{\partial C_{N_2}} & = & -1.73 \;[cm/\%\; \mu s]
\end{eqnarray*}

Two example plots are given in figure \ref{figVDdepend} where the drift velocity change is plotted in dependency of pressure and temperature. A residual histogram of the complete taylor expansion with all dependencies is plotted in figure \ref{figResHistoSim}. The sigma of the residual distribution is lower than the claimed relative precision of \textsl{Magboltz-2}, which is 0.05\%. This proves that the first order taylor approximation is valid within the chosen range of possible variations.

\begin{figure}[t]
\centering
\includegraphics[width=0.48\textwidth]{picDCS/NeCO2N2_T-vd.epsi}
\includegraphics[width=0.48\textwidth]{picDCS/NeCO2N2_P-vd.epsi}
\caption{
Change of drift velocity in dependency of temperature (left) and pressure (right). }
\label{figVDdepend}
\end{figure}

\begin{figure}[t]
\centering
\includegraphics[width=90mm]{picDCS/resHist.eps}
\caption{
Fit residuals of drift velocity change in [\%]. }
\label{figResHistoSim}
\end{figure}

\section{Kalman filter for time dependent variables}
\label{sec:kalman}
The drift velocity and the gas gain are changing in time.
The drift velocity and gas gain is a function of many parameters, but not all of
them are known. We assume that the most important parameters are pressure and temperature
and the influence of other parameters (gas composition, and electric field) are only
slowly varying in time and can be expressed by smooth function $x_{off}(t)$:
\begin{equation}
x(t) = x_{off}(t)+k_N\frac{\Delta{P/T}}{P/T}
\label{eq:KalmanTime}
\end{equation}
where x(t) is the parameter which we observe.
\begin{equation}
\begin{split}
x(t)=\frac{\Delta{G}}{G_0}	\\
x(t)=\frac{\Delta{v_d}}{v_{d0}}
\end{split}
\end{equation}

The Kalman filter parameters are:
\begin{itemize}
\item State vector  ($x_{off}(t)$, $k_N$) at given time
\item Covariance matrix
\end{itemize}

The Kalman filter implement the following functions:
\begin{itemize}
\item Prediction - adding covariance element $\sigma_{xoff}$
\item Update state vector with new measurement vector ($x_t,\frac{\Delta{P/T}}{P/T}$)
\end{itemize}

\begin{figure}[t]
\centering
\includegraphics[width=80mm]{picDCS/vdriftraw_time.eps}
\includegraphics[width=80mm]{picDCS/tpraw_tp.eps}
\caption{
	Drift velocity as function of time (upper plot) and as a function of $\Delta(T/P)$ (lower plot)
}
\label{figVDrift}
\end{figure}

\begin{figure}[t]
\centering
\includegraphics[width=80mm]{picDCS/vdriftptcorr_time.eps}
\includegraphics[width=80mm]{picDCS/vdriftfdrift_time.eps}
\caption{
	Drift velocity corrected for T/P variation as function of time (upper plot).
	In the lower plot the correction for time dependent offset is  also applied ($x_{off}(t)$ in formula\ref {eq:KalmanTime})
}
\label{figVDriftCorrected}
\end{figure}

\section{Precision of the correction}

The precision of the drift velocity correction and gain correction is proportional
to the  precision of the pressure and temperature measurement and to the length of the time
interval
\begin{eqnarray}
    \sigma^2_x=\sigma^2_{xoff}\Delta{t}+k^2_N\sigma^2_{P/T}
\label{eq:sigmaX}
\end{eqnarray}

The typical relative resolution of the pressure and temperature measurement is on the level of $6\times10^{-5}$ and
$1\times10^{-5}$ respectively (see picture \ref{figDCSResol}). For cool gas the  coefficient $k_N$ is close to one. The contribution of the P/T correction to the drift velocity uncertainty is on the level of  $6.1\times10^{-5}$ (150 microns for the full drift length of 250 cm)

The $\sigma_{xoff}$ from equation \ref{eq:sigmaX} was  estimated from plot \ref{figVDriftCorrected} and is on the level of 0.001 in a four day period. This estimate was obtained for the period of largest change in the present data sample. Further investigations should be carried out for extended time periods.

For the TPC drift velocity determination, the requiered relative resolution is on the level of $6\times10^{-5}$.
Entering the observed sigmas into equation (\ref{eq:sigmaX}) the minimal frequncy of the drift velocity updates were estimated (equation \ref{eq:driftUpdateTime}) to be about 1 hour.
\begin{eqnarray}
    \Delta{t}\le\frac{\sigma^2_x}{\sigma^2_{xoff}}\approx\left(\frac{6\times10^{-5}}{0.001/4days}\right)^2=0.05 day.
\label{eq:driftUpdateTime}
\end{eqnarray}

\begin{figure}[t]
\centering
\includegraphics[width=80mm]{picDCS/deltaPoverP.eps}
\includegraphics[width=80mm]{picDCS/deltaToverT.eps}
\caption{
The relative resolution of the pressure and temperature  measurement.
}
\label{figDCSResol}
\end{figure}

\section{ Alice TPC drift calibration using tracks}

In the first approximation there is a linear dependence of the z position on the drift time.
In the Alice TPC the expression on the A side and C side of the chambers have the same drift velocity part $v_d$
with opposite sign. The full drift length $z_{0A}$ and $z_{0C}$ are different. We suppose that
the $t_0$ offset given by trigger arrival time is the same. In reality the $t_0$ equalization is applied before,
using the pad-by-pad calibration pulser measurement. We let the variable $s$ represent the sides A and C with, with respective values $s_A=-1$ and $s_C=+1$.
\begin{equation}
\begin{split}
z_s = z_{s0}+sv_d(t-t_0)
\end{split}
\end{equation}

Let the actual value of drift velocity $v_d$ and the time offset $t_0$ are shifted by some $\Delta$ value.
Our starting drift velocity values and time offset are $\tilde{v}_d$ and $\tilde{t}_0$
\begin{equation}
\begin{split}
v_d=\tilde{v}_d+\Delta v_d \\
t_0=\tilde{t}_0+\Delta t_0 \\
v_c = \frac{\Delta{v_d}}{\tilde{v}_d} \\
\Delta{z}_{t_0} = \Delta{t}_0\tilde{v}_d \\
\end{split}
\end{equation}

Then the actual z position is expressed using the starting z position measurement $\tilde{z}_s$.
\begin{equation}
\begin{split}
z_s = \tilde{z}_s-\frac{\Delta v_d}{v_d}(z_{s0}-\tilde{z}_{s})-s\Delta t_0 \tilde{v}_d\\= \tilde{z}_{s} -v_c(z_{s0}-\tilde{z}_{s})+\Delta{z}\\
\end{split}
\end{equation}

In previous expression  we neglected second order correction
\begin{equation}
\begin{split}
\Delta{v_d}\Delta{t_0}\ll\frac{\Delta v_d}{v_d}(z_{s0}-z_{s}) \\
(t-t_0)\approx \frac{(z_{s0}-\tilde{z}_{s})}{v_d}
\end{split}
\end{equation}

Combining the z measurement the track parameters can be fitted. Let's assume linear track model:
\begin{equation}
\begin{split}
\tilde{z}_s =\tilde{a}_s+\tilde{b}_sx \\
z_s = a_s+ b_sx \\
\end{split}
\end{equation}

The relation between starting  parameters $\tilde{a}, \tilde{b}$ and corrected parameters $a,b$ is linear.
\begin{equation}
\begin{split}
a_s=\tilde{a}_s-v_c(z_{s0}-\tilde{a}_s)-s\Delta{z}\\
b_s=\tilde{b}_s(1+v_c)\\
\end{split}
\end{equation}
The inclination angle correction is the same on the A and C side.

Tracks crossing the central electrode, respectively primary tracks can be used to
monitor correction coefficients $\Delta{z}$ and $v_c$. For tracks crossing the central electrode the a and b parameters at the crossing point fitted form A and C side are the same. In case of primary tracks, the z position at r-$\phi$ DCA are also the same:
\begin{equation}
\begin{split}
a_A-a_C=0 \\
\Delta\tilde{a}(1-v_c)+2\Delta{z}-v_c(z_{0A}-z_{0C})=0 \\
\Delta\tilde{a}=\frac{v_c(z_{0A}-z_{0C})-2\Delta{z}}{1-v_c}
\end{split}
\end{equation}

Combining information from A and C side the correction parameters, drift correction $v_c$ and offset correction $\Delta{z}$ can be fitted.

In case track crossed the central electrode the track parameters of the same track on A side and C side can be fitted.
The actual track parameters $a_A$ and $a_C$  respectivally $b_A$ and $b_C$ are the same.

\begin{thebibliography}{99}

\bibitem{Blum}
W.Blum, W.Riegler, L.Luigi: Particle Detection with drift Chambers; 2nd ed.
%@book{Blum,
%      author       = "Blum, Walter and Riegler, Werner and Rolandi, Luigi",
%      title        = "Particle Detection with drift Chambers; 2nd ed.",
%}

\bibitem{magboltz}
S.Biagi: Magboltz-2, transport of electrons in gas mixtures; http://consult.cern.ch/writeup/magboltz
%@Misc{magboltz,
%  author = {S. Biagi},
%  title = {{Magboltz-2}, Transport of electrons in gas mixtures},
%  year =  {2008},
%  annote = {Version 8.3}

 \end{thebibliography}

\end{document}


Rust allows for a limited form of operator overloading. There are certain operators that are able to be overloaded. To support a particular
operator between types, there's a specific trait that you can implement, which then overloads the operator.

\blank

For example, the \code{+} operator can be overloaded with the \code{Add} trait:

\begin{rustc}
use std::ops::Add;

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
}

impl Add for Point {
    type Output = Point;

    fn add(self, other: Point) -> Point {
        Point { x: self.x + other.x, y: self.y + other.y }
    }
}

fn main() {
    let p1 = Point { x: 1, y: 0 };
    let p2 = Point { x: 2, y: 3 };

    let p3 = p1 + p2;

    println!("{:?}", p3);
}
\end{rustc}

In \code{main}, we can use \code{+} on our two \code{Point}s, since we've implemented \code{Add<Output=Point>} for \code{Point}.

\blank

There are a number of operators that can be overloaded this way, and all of their associated traits live in the
\href{https://doc.rust-lang.org/std/ops/}{std::ops} module. Check out its documentation for the full list.

\blank

Implementing these traits follows a pattern. Let's look at \href{https://doc.rust-lang.org/std/ops/trait.Add.html}{Add} in more detail:

\begin{rustc}
pub trait Add<RHS = Self> {
    type Output;

    fn add(self, rhs: RHS) -> Self::Output;
}
\end{rustc}

There's three types in total involved here: the type you \code{impl Add} for, \code{RHS}, which defaults to \code{Self}, and \code{Output}.
For an expression \code{let z = x + y}, \x\ is the \code{Self} type, \y\ is the \code{RHS}, and \z\ is the \code{Self::Output} type.

\begin{rustc}
impl Add<i32> for Point {
    type Output = f64;

    fn add(self, rhs: i32) -> f64 {
        // add an i32 to a Point and get an f64
    }
}
\end{rustc}

will let you do this:

\begin{rustc}
let p: Point = // ...
let x: f64 = p + 2i32;
\end{rustc}

\subsection*{Using operator traits in generic structs}

Now that we know how operator traits are defined, we can define our \code{HasArea} trait and \code{Square} struct from the
traits chapter (see \nameref{sec:syntax_traits}) more generically:

\begin{rustc}
use std::ops::Mul;

trait HasArea<T> {
    fn area(&self) -> T;
}

struct Square<T> {
    x: T,
    y: T,
    side: T,
}

impl<T> HasArea<T> for Square<T>
        where T: Mul<Output=T> + Copy {
    fn area(&self) -> T {
        self.side * self.side
    }
}

fn main() {
    let s = Square {
        x: 0.0f64,
        y: 0.0f64,
        side: 12.0f64,
    };

    println!("Area of s: {}", s.area());
}
\end{rustc}

For \code{HasArea} and \code{Square}, we declare a type parameter \code{T} and replace \code{f64} with it. The \code{impl} needs more
involved modifications:

\begin{rustc}
impl<T> HasArea<T> for Square<T>
        where T: Mul<Output=T> + Copy { ... }
\end{rustc}

The \code{area} method requires that we can multiply the sides, so we declare that type \code{T} must implement \code{std::ops::Mul}.
Like \code{Add}, mentioned above, \code{Mul} itself takes an \code{Output} parameter: since we know that numbers don't change type when
multiplied, we also set it to \code{T}. \code{T} must also support copying, so Rust doesn't try to move \code{self.side} into the return value.


    \filetitle{covmat}{Covariance matrix prior dummy observations for BVARs}{BVAR/covmat}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
O = BVAR.covmat(C,Rep)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{C} {[} numeric {]} - Prior covariance matrix of residuals; if
  \texttt{C} is a vector it will be converted to a diagonal matrix.
\item
  \texttt{Rep} {[} numeric {]} - The number of times the dummy
  observations will be repeated.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{O} {[} bvarobj {]} - BVAR object that can be passed into the
  \href{VAR/estimate}{\texttt{VAR/estimate}} function.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}