\chapter{$Z\to\tauh\taul$ tag and probe study}\label{chap4}
This chapter describes the analysis methodology of using $Z\to\tau\tau$ events to measure Monte Carlo correction factors for tau identification algorithms in the high-$p_T$ region.

As we saw in the previous section different working points are defined for RNN score relative to the efficiency of selecting true $\tauh$ candidates. When the efficiency of the working points is measured in data and simulation, a correction factor is derived and then applied to the simulation in order for the signal efficiency to agree between data and simulation \cite{ATLAS:2017mpa}. Because of the top quark mass, $t\bar{t}$ events are used as a source of high momentum taus for measuring correction factors on the high-$p_T$ bins. But as we already saw in section \ref{chap2sec2}, Lepton universality may not hold in W decays. For that reason our study is aimed to use $Z\to\tau\tau$ events for deriving and cross checking simulation correction factors in the high-$p_T$ region.
\section{Signal events}\label{chap4sec1}
The type of $Z\to\tau\tau$ events we consider as signal for this study are when one the taus decays hadronically and the other leptonically, either into an electron or a muon ($Z\to\tau\tau\to\tau_h +l=\mu,e$). Thus, our final states will include a $\tauh$ candidate and a lepton $l=e,\mu$. The presence of this lepton will be used as our tag.
Generally, in $Z\to\tau\tau$ events, the taus are produced back to back and their $\pt$ spectrum falls sharply. One way to select events where the taus are boosted in the transverse plane is to look for events where the opening angle in the transverse plane between the taus ($\Delta\phi(\tauh,\taul)$) is more acute. A depiction of the topologies of our signal events is shown in Fig.\ref{Fig7}. For these events, the missing transverse momentum ($\ptmiss$) is assumed to come from the neutrinos produced in the decays of the tau leptons. Due to the fact that two neutrinos are produced in the leptonic decay mode we expect our events to have a larger $\ptmiss$ component along the $\taul$ direction.

We classify our events in two types of topologies. First, events where the $\ptmiss$ is inside the opening angle between the visible objects. In this kind of events we assume the missing energy is due to a pair of neutrinos flying in the same direction as the visible objects. This is shown in Fig.\ref{Fig7a}. In this case, we solve the following equation to obtain the momentum of the neutrinos:
\begin{equation}
	\vec{p}_{T_{\nu_l}}+\vec{p}_{\nu_h}=\vec{\ptmiss},
\end{equation}
given the following set of constraints (\textit{collinear approximation}):
\begin{align}
	\phi(\nu_l)&=\phi(l),
	\\
	\phi(\nu_h)&=\phi(\tauh),
	\\
	\eta(\nu_l)&=\eta(l),
	\\
	\eta(\nu_h)&=\eta(\tauh).
\end{align}
The second case is when the $\ptmiss$ is outside the angle formed by the visible objects, as is shown in Fig.\ref{Fig7b}. In this case the assumption is that only one neutrino is responsible for the majority of the $\ptmiss$, a neutrino flying in the direction of the visible object that is closest to the $\ptmiss$. We use the following equations to obtain the neutrino momentum:
\begin{align}
p_{T_{\nu}}&=\ptmiss \cos(\Delta\phi(\tau_{\text{closer}},\ptmiss)),
\\
\phi(\nu)&=\phi(\tau_{\text{closer}}),
\\
\eta(\nu)&=\eta(\tau_{\text{closer}}),
\end{align}
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig7a}{\includegraphics[width=0.49\textwidth]{figures/Fig7a}}}\hfill
	\subfloat[]{\label{Fig7b}{\includegraphics[width=0.49\textwidth]{figures/Fig7b}}}
	\caption{The two different types of topologies that define signal events. On the left, when the missing energy is between the visible objects two neutrinos are assumed to be responsible for all the missing energy. On the right, only one neutrino is assumed to be flying on the direction of the visible object closest to the missing energy.}
	\label{Fig7}
\end{figure}
where $\tau_{closer}$ stands for the visible object closest to the direction of the missing energy.

We define a variable called $\Omega$ in order to classify our events in the different topologies already described. First we define:
\begin{equation}
	\omega=\frac{\Delta\phi(\tau_{\text{closer}},\ptmiss)}{\Delta\phi(\tauh,\taul)},
\end{equation}
then,
\begin{itemize}
	\item when $\ptmiss$ is inside the opening angle between the visible objects but closer to $\tauh$:
	\begin{equation}
		\Omega=\omega,
	\end{equation}
	\item when $\ptmiss$ is still inside, but closer to $\taul$:
	\begin{equation}
		\Omega=1-\omega.
	\end{equation}
	\item If $\ptmiss$ is outside and closer to $\tauh$:
	\begin{equation}
		\Omega=-\omega,
	\end{equation}
	\item and finally, if $\ptmiss$ is outside and closer to $\taul$:
	\begin{equation}
		\Omega=\omega+1.
	\end{equation}
\end{itemize}
Thus, $\Omega$ is a continuous variable that give us information on the topology of the event. When $\ptmiss$ is inside the visible system it has values in the interval [0,1]. It is exactly 0 when $\ptmiss$ is in the $\tauh$ direction and 1 when $\ptmiss$ is in the $\taul$ direction. $\Omega$ has negative values when $\ptmiss$ is outside and closer to the $\tauh$ candidate and has positive and values greater than 1 when is outside and closer to the $\taul$. A diagram describing the $\Omega$ values is shown in Fig.\ref{Fig8}.
\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.5\textwidth]{figures/Fig8}
	\caption{Graphical representation of the different values of $\Omega$ depending on the region where the $\ptmiss$ is located in the event.}
	\label{Fig8}
\end{figure}
As we will see later the event classification into these different types of topologies will allow us to reconstruct and exploit kinematical variables as the invariant mass of the di-tau system, the Z boson transverse momentum ($Z_{p_T}$) and the angular distribution of the objects in the event.
\section{Monte Carlo and Data Samples}
The data used for this study has been recorded during the Run-II of the LHC. It corresponds to the 2015-2018 data taking period, the total integrated luminosity corresponds to 139.2 $\text{fb}^{-1}$ of proton-proton collisions at a centre-of-mass energy of $\sqrt{s}=13$ TeV.

Monte Carlo (MC) samples are used for signal and background simulation. For $Z\to\tau\tau$ events two MC samples are used, Powheg+Pythia8 and Sherpa. For the rest of the samples Powheg+Pythia8 is used. Table \ref{Table3} shows the MC generators used for each process.

\begin{table}[htbp]
	\centering
	\begin{tabular}{cc}
		\hline
		\multicolumn{1}{|c|}{Process}  & \multicolumn{1}{c|}{Event Generator} \\ \hline
		$Z\to\tau\tau$                 & Powheg+Pythia8 and Sherpa            \\
		$Z\to ee$                      & Powheg+Pythia8                       \\
		$Z\to\mu\mu$                   & Powheg+Pythia8                       \\
		$W\to l\nu_l$				   & Powheg+Pythia8                       \\
		$t\bar{t}$                     & Powheg+Pythia8                       \\
		Single $t$                     & Powheg+Pythia8                       \\
		Diboson                        & Powheg+Pythia8                       \\ \hline
	\end{tabular}
	\caption{List of MC event generators used.}
	\label{Table3}
\end{table}
\section{Event Selection}\label{chap4sec3}
In order to select $Z\to\tauh\taul$ events, our basic selection includes events with exactly one $\tauh$ candidate and one lepton, a muon or an electron. The $\tauh$ and lepton pair need to have opposite charge. As we said, the presence of the lepton will be used as our tag, thus, a lepton trigger is required to be fired with an online requirement on the $p_{T}(\mu)\geq 20$ ($p_{T}(e)\geq 24$) GeV for 2015 data taking period. For 2016-2018 the online requirement is $p_{T}(\mu,e)\geq 26$ GeV. The muons are required to pass a \textit{medium} ID requirement and the electron has to pass a \textit{tight} ID filter. Additionally, both muons and electrons have to pass an offline $p_T$ requirement to be greater than 27 GeV. Finally, the opening angle between the two visible objects $\Delta\phi(\tauh,l)\leq 2\pi/3$.

Events containing b-jets are vetoed in order to reject $t\bar{t}$ events. Also an isolation criterion is required to be passed by the leptons: for the muon (electron), the scalar sum of the $p_T$ of the tracks within a cone of $\Delta R=0.3 (0.2)$ of the muon (electron) must be less than 0.06 times the muon (electron) $p_T$. Additionally, for the electron, the sum of the calorimeter cluster energy in a cone of size $\Delta R=0.2$ must be 0.06 times smaller than the electron $p_T$. As we said in section \ref{chap4sec1}, we expect signal events to peak around $\Omega=1$, thus we select events where $\Omega\in (0,1.4)$. Fig.\ref{Fig9} shows the $\Omega$ distribution for $Z\to\tauh\mu$ final state.
\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.6\textwidth]{figures/Fig9}
	\caption{Distribution of $\Omega$ for $Z\to\tauh\mu$ final state. All the other cuts have been applied apart from the one being plotted.The ATLAS data are shown as points with error bars, showing the statistical uncertainties. The detector-level predictions are shown as the shaded histograms, with the colour code as given on the plots.The region between the vertical dashed red lines is included in the final final candidate event sample by the selection criterion on the quantity plotted. The lower section of each plot shows the ratio of data to prediction. The uncertainties on the predictions represent the MC statistical uncertainties. These uncertainties are shown as blue shaded regions.}
	\label{Fig9}
\end{figure}
Additionally, a cut on the reconstructed invariant mass ($\mreco$) of the event is made, the cut aims to pick events where the invariant mass is around the Z boson mass ($70\leq\mreco\leq 110$ GeV). The invariant mass of the final states is calculated depending on the event topology. For events where $\Omega\in [0,1]$ (\textit{in-between events}),
\begin{equation}
	\mreco^{2}=(q_l+q_{\tauh}+q_{\nu_l}+q_{\nu_{\tauh}})^2.
	\label{eq35}
\end{equation}
When the $\ptmiss$ is outside (\textit{outside events}),
\begin{equation}
	(\mreco-5)^{2}=(q_l+q_{\tauh}+q_\nu)^2.
	\label{eq36}
\end{equation}
The RHS of \eqref{eq35} and \eqref{eq36} are vector sums, where $q$ represents the four-momentum of each particle. In the outside events case we manually add 5 GeV to maintain our cuts consistent, since in this region we find empirically that the di-tau mass is underestimated and we wish to apply the same selection in both regions. Fig.\ref{Fig10} shows the distribution of $\mreco$ for the in-between and outside $Z\to\tauh\mu$ events.
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig10a}{\includegraphics[width=0.50\textwidth]{figures/Fig10a}}}\hfill
	\subfloat[]{\label{Fig10b}{\includegraphics[width=0.50\textwidth]{figures/Fig10b}}}
	\caption{$\mreco$ distribution for the in-between region (a) and the outside region (b). All the other cuts have been applied apart from the one being plotted.}
	\label{Fig10}
\end{figure}

In addition, for the $Z\to\tauh e$ final state, we add two cuts aimed to reject events where the $\tauh$ candidate is faked by an electron. First to reject $Z\to ee$ events, a requirement on the invariant mass of the electron and $\tauh$ candidate is made, $m(e,\tauh)<80$ GeV. Then, we also make use of an ID algorithm trained to discriminate electrons from real $\tauh$. We require, eBDTScore$\geq 0.06$. The distributions for these variables are shown in Fig.\ref{Fig11}.
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig11a}{\includegraphics[width=0.50\textwidth]{figures/Fig11a}}}\hfill
	\subfloat[]{\label{Fig11b}{\includegraphics[width=0.50\textwidth]{figures/Fig11b}}}
	\caption{Distribution of the variables used for rejecting $\tauh$ fakes coming from electrons. On the right $m(e,\tauh)$ distribution (a) and on the left the eBDTScore distribution (b). All the other cuts have been applied apart from the one being plotted.}
	\label{Fig11}
\end{figure}
Finally, in order to measure the tau ID scale factors on the high-$p_T$ region we select events where $p_T(\tauh)\geq 45$ GeV. The $p_T$ distributions for $\tauh$ candidates are shown in Fig.\ref{Fig12} for the in-between and outside regions.
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig12a}{\includegraphics[width=0.50\textwidth]{figures/Fig12a}}}\hfill
	\subfloat[]{\label{Fig12b}{\includegraphics[width=0.50\textwidth]{figures/Fig12b}}}
	\caption{$p_T(\tauh)$ distribution for the in-between (a) and outside (b) regions. All the other cuts have been applied apart from the one being plotted.}
	\label{Fig12}
\end{figure}
\section{Multi Jet Background Estimation}
As we said previously, $\tauh$ candidates are seeded by jets, thus, QCD events represent a source of background. Nonetheless, we do not have MC simulations for this processes as compared with the EW interactions shown in Table \ref{Table3}. For this reason, we use a \textit{data driven method} to estimate multi jet (MJ) background. For that purpose, we choose a variable that in principle we suppose is uncorrelated with the shape of the MJ background. For our study, this variable is the relative sign between the charges of the $\tauh$ and the lepton. Thus, this defines two regions: the same sign (SS) region where $q(\tauh)=q(l)$ and the opposite sign region where $q(\tauh)=-q(l)$. Our estimate of MJ background for the SS region is obtained subtracting signal and electroweak backgrounds (EWBG) simulated contributions, basically:
\begin{equation}
\text{MJBG}_{\text{SS}}=\text{Data}_{\text{SS}}-\text{Signal}_{\text{SS}}-\text{EWBG}_{\text{SS}},
\end{equation}
 To study the residual charge correlation, we define a control region (CR) where the MJ background is enhanced. This region is defined by events that fail either the lepton isolation criteria or the $\tauh$ ID requirement of tauRNNScore$>0.4\, (0.55)$ for 1-prong (3-prong) $\tauh$ candidates and have the same other kinematic features of the signal region (SR) described in section \ref{chap4sec3}. A diagram showing the four regions just defined is shown in Fig.\ref{Fig13}. Now, if charge correlation are the same in SR and CR, we have:
 \begin{equation}
 \frac{\text{MJBG}_{\text{SROS}}}{\text{MJBG}_{\text{SRSS}}}=\frac{\text{MJBG}_{\text{CROS}}}{\text{MJBG}_{\text{CRSS}}},
 \end{equation}
then,
 \begin{equation}
\text{MJBG}_{\text{SROS}}=\text{MJBG}_{\text{SRSS}}\times \text{RQCD}\,
\label{eq36}
\end{equation}
where $\text{RQCD}\equiv\frac{\text{MJBG}_{\text{CROS}}}{\text{MJBG}_{\text{CRSS}}}$. So, \eqref{eq36} gives the estimation of MJ background on the signal region (SROS).
\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.5\textwidth]{figures/Fig13}
	\caption{Diagram showing the regions defined to estimate MJ background contribution in the SROS region. This data-driven method is also known as the ABCD method.}
	\label{Fig13}
\end{figure}
Table \ref{tab:MJ} summarises inputs into the calculation of the MJ background in the three candidate event samples.
\begin{table}[htbp]
	\centering
	\begin{tabular}{rrr}
		Sample &             $\mu \tauh$\ & $e \tauh$\  \\
		\toprule
		CR OS Data &       7396    & 6255   \\
		MC          &       6057    & 5031.9   \\
		CR SS Data &       708    & 687    \\
		MC      &       191.3      & 279.2     \\
		\toprule
		RQCD      &      2.591   & 2.999  \\
		\toprule
		SR SS     &      212    & 219   \\
		MC     &       121.5    &  142.3   \\
		\toprule
		MJ Background        &      $231.1 \pm 41.8$    & $230 \pm 49.1$    \\

	\end{tabular}
	\caption{Inputs into the calculation of the MJ background in the two final state candidate event samples  $\mu\tauh$ and $e\tauh$.
		The numbers of data events in the CR OS and CR SS samples are given, together with the total numbers of events in these
		categories expected from MC.
		The excess of data with respect to MC is assumed to arise from MJ and is used to calculate the value of RQCD.
		The quoted uncertainties are statistical only.
	}
	\label{tab:MJ}
\end{table}


\documentclass[]{beamer}
%\usepackage[utf8]{inputenc}
%\usepackage{amsmath}
\usepackage{pgfplots}
\usetheme{Singapore}
\usefonttheme{serif}
\usecolortheme{lily}
\title{Introduction to Derivative Pricing:}
\subtitle{An intro to Baxter \& Rennie}
\author{guoxiaoq@gmail.com}
\date{}
\begin{document}
\section{Betting Game}
\begin{frame}
  \titlepage
\end{frame}
\frame{\frametitle{Horse Gambling Example}
  \begin{itemize}
  \item Horse A has a 25\% chance of winning the race, B has 75\% \pause
  \item On the market there are \$5000 betting for A, and \$10000 betting for B. \pause
  \item How should the book maker set the odds for A and B assuming no margin? \pause
  \item Using the market supply and demand, his profit is not dependent on the actual outcome. \pause
  \item Using the actual probability, his short-term PnL will be dependent on the race outcome \pause
  \item so how should he decide on the odds?
  \end{itemize}
}
\frame{\frametitle{Power of Arbitrage}
  As seen from the house gambling example, arbitrage is a better pricer than expectation. Financial instrument pricing is determined in the same way. \pause
  \begin{itemize}
  \item Arbitrage opportunity would be a (self-financing) trading strategy which started with zero value and terminated at some definite date T with a positive value.
  \item How do you price the share of Apple?
    \pause
  \item How do you price the call option on Apple share?
    \pause
  \item How to price 1 million Apple shares?
  \end{itemize}
}

\frame{\frametitle{No Arbitrage Pricing}
  \begin{itemize}
  \item No-arbitragee implies no way of making riskfree profits. \pause
  \item Arbitrage is a stronger pricing argument than expectation. \pause
  \item Arbitrage activity in the market will drive price to no-arb pricing.
  \end{itemize}
}

\frame{\frametitle{ Betting/Trading strategy}
  What are the elements that goes into a betting/trading strategy?
  \begin{itemize}
  \item instrument/Rule: what to bet. What are the possible outcomes that would be known later.
  \item quantity: how much to bet.
  \item odds/market price:  What are the payoffs for the pay for each possible outcome?
  \item Rule:pre-visibility: Bets off before the outcome is known. No cheating
  \end{itemize}
}

\frame{\frametitle{Martingale: double down stragetgy}
  For a fair coin toss ( odds at 1:1 ) bet, bet \$1 for head, and double the bet each time tails shows until the 1st head shows. This will give you a sure profit of \$1, as long as you don't go broke before the 1st heads shows.
  \begin{itemize}
  \item Can it be carried out in Marina Bay Sands?
  \item Why martingale is not an arbitrage? \pause --- not self-financing
  \end{itemize}
}

\section{Binonmial}
\frame{\frametitle{Binomial representation theorem (I)}
  \begin{itemize}
  \item A random process is described using binomial tree nodes for all possible future values.
  \item A probability set for the above tree, called measure.
  \item Filtration $\mathcal{F}_i$ is the historical path upto $i$. This can be seen as information set available up to time $i$.
  \item Claim $X$ is a function depends on $\mathcal{F}_T$.
  \item $\mathbb{E_Q}(X|\mathcal{F}_i)$, converts a claim into a process, i.e. price of the claim at each $t$.
  \item A previsible process/trading stragegy
  \item A martingale measure. ( $\mathbb{E_P}(S_j|\mathcal{F}_i)=S_i$, for all $i \leq j$. )
  \end{itemize}
}

\frame{\frametitle{Binomial representation theorem (II)}
  \begin{theorem}
    Suppose the measure Q is such that the binomial price process S is a Q-martingale. If N is any other Q-martingale, then there exists a previsible process h such that \[ N_i = N_0 + \sum_{k=1}^i h_k \Delta S_k ,\] where $\Delta S_k := S_i - S_{i-1}$ is the change in $S$ from time $i-1$ to $i$, and $h_i$ is the value of $h$ at the appropriate node at time $i$.
  \end{theorem}
}\frame{\frametitle{Binomial representation theorem (III)}
  \begin{itemize}
  \item As a result, all claimes can be fully replicated using simple trading strategies and therefore strongly priced by no-arbitrage argument. This is also called market completeness.
  \item arbitrage free = market complete = existence of unique Equivalence of Martingale Measure
  \item The theorem can be extended to continuous-time version.
  \end{itemize}
}
\section{Martingale}
\frame{\frametitle{Brownian motion}
  \begin{definition}
    The process $W = (W_t : t \geq 0)$ is a $\mathbb{P}$-Brownian motion if and only if:
    \begin{enumerate}
    \item $W_t$ is continuous, and $W_0$ = 0,
    \item the value of $W_t$ is distributed, under $\mathbb{P}$, as a normal random variable $N(0,t)$
    \item the increment $W_{s+t} - W_s$ is distributed as a normal $N(0,t)$, under $\mathbb{P}$, and is independent of $\mathcal{F}_s$, the history of what the process did up to time $s$.
    \end{enumerate}
  \end{definition}
}
\frame{\frametitle{Stochastic process}
  \begin{definition}
    A stochastic process $X$ is a continous process ( $X_t:t\geq0$) such that $X_t$ can be written as \[ X_t = X_0 + \int_0^t \sigma_s dW_s + \int_0^t\mu_sds,\] where $\sigma$ and $\mu$ are random $\mathcal{F}$-previsible process such that $\int_0^t(\sigma_s^2+|\mu_s|)ds$ is finite for all times $t$ (with probability 1 ). The differential form of this equation can be written \[dX_t=\sigma_tdW_t+\mu_tdt.\]
  \end{definition}
}
\frame{\frametitle{It$\hat{\textrm{o}}$'s formula}
  If $X$ is a stochastic process, satisfying $dX_t=\sigma_tdW+\mu_tdt,$ and $f$ is a deterministic twice continously differentiable function, then $Y_t:=f(X_t)$ is also a stochastic process and is given by \[dY_t=\left(\sigma_tf'(X_t)\right)dW_t+\left(\mu_tf'(X_t)+\frac{1}{2}
  \sigma_t^2f''(X_t)\right)dt.\]
  \begin{example}
    If $X_t = \exp(\sigma W_t)$, then what is $dX_t$?
    \pause
    \[dX_t=\sigma X_tdW_t+\frac{1}{2}\sigma^2X_tdt.\]
  \end{example}
}
\frame{\frametitle{Radon-Nikodym derivative}
  \begin{definition}
    \emph{Equivalence} \\
    Two measures P and Q are equivalent if they operate on the same sample space and agree on what is possible. \[P(A)>0 \Leftrightarrow Q(A) > 0.\]
  \end{definition}
  \begin{definition}
    Given $\mathbb{P}$ and $\mathbb{Q}$ equivalent measures and a time horizon T, we can define a random variable $\frac{d\mathbb{Q}}{d\mathbb{P}}$ defined on $\mathbb{P}$-possible paths, taking positive real values, such that
    \begin{enumerate}
    \item $\mathbb{E_Q}(X_T)=\mathbb{E_P}(\frac{d\mathbb{Q}}{d\mathbb{P}}X_T),$ for all claims $X_T$ knowable by time $T$.
    \item $\mathbb{E_Q}(X_t| \mathcal{F}_s) = \xi_s^{-1}\mathbb{E_P}(\xi_t X_t|\mathcal{F}_s), \ s\leq t \leq T,$
    \end{enumerate}
    where $\xi_t$ is the process $\mathbb{E_P}(\frac{d\mathbb{Q}}{d\mathbb{P}}|\mathcal{F}_t).$
  \end{definition}
}
\frame{\frametitle{Cameron-Martin-Girsanov theorem}
  \begin{theorem}
    If $W_t$ is a $\mathbb{P}$-Brownian motion and $\gamma_t$ is a $\mathcal{F}$-previsible process satisfying the boundedness condition $\mathbb{E_P}\exp(\frac{1}{2}\int_0^T\gamma^2_tdt)<\infty$, then there exists a measure $\mathbb{Q}$ such that
    \begin{enumerate}
    \item $\mathbb{Q}$ is equivalent to $\mathbb{P}$
    \item $\frac{d\mathbb{Q}}{d\mathbb{P}}=\exp\left( - \int_0^T \gamma_tdW_t - \frac{1}{2}\int_0^T\gamma^2_tdt\right)$
    \item $\widetilde{W}_t = W_t + \int_0^t\gamma_sds$ is a $\mathbb{Q}$-Brownian motion.
    \end{enumerate}
    In other words, $W_t$ is a drifting $\mathbb{Q}$-Brownian motion with drift $-\gamma_t$ at time $t$.
  \end{theorem}
}
\frame{\frametitle{Martingale representation theorem}
  \begin{definition}
    A stochastic process $M_t$ is a \emph{martingale} with respect to a measure $\mathbb{P}$ if and only if
    \begin{enumerate}
    \item $\mathbb{E_P}(|M_t|)<\infty, \forall t$
    \item $\mathbb{E_P}(M_t|\mathcal{F}_s)= M_s, \forall s \leq t.$
    \end{enumerate}
  \end{definition}
  \begin{theorem}
    Suppose that $M_t$ is a $\mathbb{Q}$-martingale process, whose volatility $\sigma_t$ satisfies the additional condition that it is (with probability one) always non-zero. Then if $N_t$ is any other $\mathbb{Q}$-martingale, there exists an $\mathcal{F}$-previsible process $\phi$ such that $\int_0^T\phi_t^2\sigma_t^2dt < \infty, a.s.$, and $N$ can be written as \[N_t=N_0+\int_0^t\phi_sdM_s.\] Further $\phi$ is (essentially) unique.
  \end{theorem}
}
\section{Application}
\frame{\frametitle{Construction of Dynamic Hedging Strategy}
  Under no-arb pricing, we can fully hedge any contigent claim using following steps:
  \begin{itemize}
  \item The Portfolio $(\phi,\psi)$, where $\phi_t$ and $\psi_t$ is the number of units of security and bond we hold at $t$. $\phi$ is $\mathcal{F}$-\emph{previsible} (in other words, left continuous).
  \item Self-financing SDE $dV_t = \phi_t dS_t + \psi_t dB_t.$
  \item Suppose we are in a market of a riskless bond $B$ and a risky security $S$ with volatility $\sigma_t$, and a claim $X$ on events up to time $T$. A \emph{replication strategy} for $X$ is a self-financing portfolio $(\phi, \psi)$ such that $\int_0^T \sigma_t^2 \phi_t^2 dt < \infty$ and $V_T = \phi_T S_T + \psi_T B_T = X.$
  \end{itemize}
}
\frame{\frametitle{Black Scholes Formula}
  We assume a bond price $B_t$ and a stock price $S_t$ that follow
  \begin{align*}
    S_t &= S_0 \exp( \sigma W_t + \mu t) \\
    B_t &= \exp(rt)
  \end{align*}
  \begin{enumerate}
  \item Find a measure Q under which $Z_t := B_t^{-1}S_t$ is a martingale. $B_t$ is therefore the \emph{numeraire}.
  \item Find the process $E_t=\mathbb{E_Q}(B_T^{-1}X|\mathcal{F}_t).$
  \item Find a previsible process $\phi$, such that $dE_t = \phi_tdZ_t$
  \item We then have
    \begin{align*}
      V_t &= B_t \mathbb{E_Q}(B_T^{-1}X | \mathcal{F}_t) \\
      V_0 &= e^{-rT} \mathbb{E_Q}(X)
    \end{align*}
  \end{enumerate}
  which can be easily solved analytically.
}

\frame{\frametitle{Application to FX: assumptions}
  We assume constant interest rate for domestic and foreign currency, and the exchange rate expressed in units of domestic currency per 1 foreign currency follows GBM.
  \begin{align*}
    B_t&=e^{rt}\\
    D_t&=e^{ut}\\
    S_t&=S_0 \exp(\sigma W_t + \mu t)
  \end{align*}
  for some $W_t$ a $\mathbb{P}$-Brownian motion and constants $r$, $u$,$\sigma$, and $\mu$, where $D_t$ is the foreign cash bond.
}
\frame{\frametitle{Application to FX: approach }
  \begin{itemize}
  \item Choose the numeraire: $B_t$.
  \item Make all tradables in this numeraire a martigale at the same time.
    \begin{itemize}
    \item demostic bond becomes 1.
    \item foreign bond is not tradable in domestic ccy
    \item fx rate itself is not a tradable
    \item foreign bond multiply by the fx spot is. $Z_t = B_t^{-1}S_t D_t$ needs to be a martingale.
    \end{itemize}
  \item form the martigale process of the derivative value $E_t = \mathbb{E_Q}(B_T^{-1}X|\mathcal{F}_t)$.
  \item find the hedging strategy $\phi_t$, s.t. $dE_t=\phi_tdZ_t$.
  \end{itemize}
}
\frame{\frametitle{Application to FX: general formula}
  \begin{itemize}
  \item original converted foreign bond process in numeraire $B_t$: $$Z_t = S_0 \exp ( \sigma W_t + ( \mu + u - r ) t )$$
  \item we need to make it into a martingale by changing the drift:
    $$Z_t = S_0 \exp ( \sigma \tilde{W}_t - \frac{1}{2}\sigma^2t )$$
  \item therefore the fx process becomes via the same drift change:
    $$S_t = S_0 \exp ( \sigma \tilde{W}_t + ( r - u - \frac{1}{2}\sigma^2 ) t )$$
  \item so we have the derivative pricing formula:
    $$V_t = B_t\mathbb{E_Q}(B_T^{-1}X|\mathcal{F}_t)$$
  \end{itemize}
}
\frame{\frametitle{Application to FX: forward }
  Let's price a forward $V_T = S_T - K$ using the formula.
  \begin{align*}
    V_t &= B_t\mathbb{E_Q}(B_T^{-1}X|\mathcal{F}_t) \\
    V_0 &= e^{-rT}(\mathbb{E_Q}(S_T) - K) \\
    V_0 &= e^{-rT}(S_0 e^{(r-u)T} - K ) \\
  \end{align*}
}
\frame{\frametitle{Application to FX: Call option}
  Let's price a call option $V_T = (S_T - K )^+$ using the formula.
  \begin{align*}
    V_t &= B_t\mathbb{E_Q}(B_T^{-1}X|\mathcal{F}_t) \\
    V_0 &= e^{-rT}(\mathbb{E_Q}(S_T - K)^+ \\
    V_0 &= e^{-rT}(F_0 N(d_1) - KN(d_2) ) \\
  \end{align*}
  Illustration of expectation of lognormal distribution: $F_T = F_0 e^{\sigma \sqrt{T} \tilde{W}_T - \frac{1}{2} \sigma^2 T}$:
  \begin{align*}
    \mathbb{E_Q}(1_{F_T>K}) &= P(F_T>K) \\
    &= P( F_0 e^{\sigma \sqrt{T} \tilde{W}_T - \frac{1}{2} \sigma^2 T} > K ) \\
    &= P( \tilde{W}_T > \frac{\ln{ \frac{K}{F_0}}+\frac{1}{2} \sigma^2 T}{\sigma\sqrt{T}} ) \\
    &= P( \tilde{W}_T < \frac{\ln{ \frac{F_0}{K}}-\frac{1}{2} \sigma^2 T}{\sigma\sqrt{T}} )  = N(d_2)
  \end{align*}
}
\frame{\frametitle{Application to FX: view from foreign side }
  Let's price a put option from the foreign investor point of view. A call on 1 EUR for 1.25 USD should be the same as 1.25 put on 1 USD for 0.8 EUR. Assuming spot and forward are all 1, and interest rates are all 0. We have
  \begin{align*}
    C &= F N(d_1) - K N(d_2) = N(d_1) - 1.25 N( d_2) \\
    1.25 \tilde{P} &= 1.25 (\tilde{K} N(-\tilde{d_2}) - \tilde{F}N(-\tilde{d_1})) = N(-\tilde{d_2}) - 1.25 N(-\tilde{d_1})\\
    d_1 &= \frac{\ln{\frac{F}{K}}+\frac{1}{2}\sigma^2T}{\sigma \sqrt T} = \frac{\ln{0.8}+\frac{1}{2}\sigma^2T}{\sigma \sqrt T} = -\frac{\ln{1.25}-\frac{1}{2}\sigma^2T}{\sigma \sqrt T} = - \tilde{d_2}\\
  \end{align*}
  Whether we use domestic measure or foreign one, the pricing and hedging is consistent.
}
\frame{\frametitle{Application to Quantos: Assumptions}
  Assuming a foreign asset, and fx process both follow lognormal process, with constant interest rates.
  \begin{align*}
    dS_t &= \mu S_t d_t+ \sigma_1 S_t dW_1(t) \\
    dC_t &= v C_t d_t + \sigma_2 C_t dW_2(t)
  \end{align*}
  $dW_1$ and $dW_2$ is correlated at $\rho$.
}
\frame{\frametitle{Application to Quantos: Numeraire and tradables}
  \begin{itemize}
  \item $B_t$, numeraire.
  \item $C_t D_t$, dollar tradable of foreign bond
  \item $C_t S_t$, dollar tradable of foreign asset
  \end{itemize}
  We need to do a change of measure so that all of the tradables are martingales
  \begin{align*}
    Y_t &= B_t^{-1}C_tD_t \\
    Z_t &= B_t^{-1}C_tS_t
  \end{align*}
}
\frame{\frametitle{Application to Quantos: formula }
  The result of the change is expected: fx process same as the fx option case, quantoed asset process just change the drift, due to correlation to fx process.
  \begin{align*}
    dS_t &= ( u - \rho\sigma_1\sigma_2 ) S_t d_t+ \sigma_1 S_t d\tilde{W}_1(t) \\
    dC_t &= ( r - u ) C_t d_t + \sigma_2 C_t d\tilde{W}_2(t)
  \end{align*}
  The pricing formula is as usual: $$V_t = B_t\mathbb{E_Q}(B_T^{-1}X|\mathcal{F}_t),$$ where $X$ is a function of $S_T$ like a forward or call.
}
\frame{\frametitle{Application to Composite}
  \begin{itemize}
  \item What is the tradable?
  \item What is the process?
  \item Using a single composite process with a simple payoff generates the same answer as using the quanto processes with a composite payoff
  \end{itemize}
}
\section{Term Structure}
\frame{\frametitle{Application to Interest Rate}
  \begin{enumerate}
  \item [$P(t,T)$] discount bond price with maturity $T$ at $t$
  \item [$R(t,T)$] bond yield ( continuous compounding ). $R(t,T) = -\frac{\ln P(t,T)}{T-t}$

    \begin{tikzpicture}[scale = 0.5]
      \begin{axis}[
	  scale only axis,
	  axis y line=left,
	  ymin=.4,
	  ymax = 1.1,
	  axis y discontinuity=crunch,
	  xlabel = Bond Maturity ( $T$ ),
	  ylabel = Bond Price ($P$ )]
        \addplot+[smooth, no marks]
	plot coordinates{
	  (0,1)
	  ( 5, .8 )
	  ( 10, .7 )
	};
      \end{axis}
      \begin{axis}[
	  scale only axis,
	  axis y line=right,
	  ymin=3,
	  ymax = 6,
	  axis y discontinuity=crunch,
	  ylabel = Bond Yield \% ($R$ )
	]
        \addplot+[red, smooth, no marks]
	plot coordinates{
	  (0, 4 )
	  ( 5, 4.5 )
	  ( 10, 5.5 )
	};
      \end{axis}
    \end{tikzpicture}
  \end{enumerate}
}
\frame{\frametitle{Additional IR basics}
  A bond price can be computed from today's (instantaneous) forward rates or the spot rate.
  \begin{itemize}
  \item $f(t,T) = - \frac{\partial}{\partial T}\ln P(t,T)$
  \item $r(t) = f(t,t)$
  \item $P(t,T) = \exp ( - \int_t^T f(t,u) du )$
  \item $R(t,T) = \frac{\ln P(t,T)}{T-t}$
  \item $P(t,T) = \exp ( - (T-t)R(t,T))$
  \end{itemize}
  Is it correct to say $P(t,T) = \exp ( - \int_t^T r(u) du )$?
}
\frame{\frametitle{HJM 1F}
  Assuming all forward rates are driving by the same random source, but can have different drift and vol.
  \begin{itemize}
  \item $df(t,T) = \sigma(t,T)dW_t + \alpha(t,T)dt$
  \item $f(t,T)=f(0,T)+\int_0^t \sigma(s,T)dW_s + \int_0^t \alpha(s,T)ds, 0\leq t \leq T$
  \end{itemize}
  We skip the technical conditions here, but basically things should be finite.
}
\begin{frame}
  \frametitle{Short Rate, Bonds, and Numeraire}
  We can choose any tradable asset as numeraire. For convenience we normally use either the cash bond/money market account  $B_t$, or some discount bond $P(t,T)$.
  \begin{itemize}
  \item $r(t)=f(0,t)+\int_0^t \sigma(s,t)dW_s + \int_0^t \alpha(s,t)ds$
  \item $B_t = \exp(\int_0^t r_s ds) = e^{\left( \int_0^t ( \int_s^t \sigma(s,u)du)dW_s + \int_0^t f(0,u)du + \int_0^t\int_s^t \alpha(s,u)du\, ds \right)}$
  \item $P(t,T)=\exp (- \int_t^Tf(t,u)dt)=e^{-\left(\int_0^t ( \int_t^T \sigma(s,u)du)dW_s + \int_t^T f(0,u)du + \int_0^t\int_t^T \alpha(s,u)du\, ds \right)}$
  \item $Z(t,T) = \frac{P(t,T)}{B_t} = e^{\int_0^t \Sigma(s,T)dW_s - \int_0^Tf(0,u)du - \int_0^t \int_s^T \alpha(s,u)du\,ds }$, where $\Sigma(t,T) = - \int_t^T \sigma(t,u)du$.
  \end{itemize}
  \begin{tikzpicture}[scale = 0.3]
    \begin{axis}[title = {$\int\int\alpha(t,T)$ for $B_1$}, stack plots = y, area style, xlabel = Time, ylabel = Maturity, xmax=2, ymax=2  ]
      \addplot[green] coordinates{(0,0) (1,1)};
      \addplot+[green] coordinates{(0,1) (1,0)} \closedcycle;
    \end{axis}
  \end{tikzpicture}
  \begin{tikzpicture}[scale = 0.3]
    \begin{axis}[title = {$\int\int\alpha(t,T)$ for $P(1,2)$}, stack plots = y, area style, xlabel = Time, ylabel = Maturity, xmax=2 ]
      \addplot[white] coordinates{(0,0) (1,1) };
      \addplot[white] coordinates{(0,1) (1,0) };
      \addplot+[red] coordinates{(0,1) (1,1) } \closedcycle;
    \end{axis}
  \end{tikzpicture}
  \begin{tikzpicture}[scale = 0.3]
    \begin{axis}[title = {$\int\int\alpha(t,T)$ for $Z(1,2)$}, stack plots = y, area style, xlabel = Time, ylabel = Maturity, xmax=2 ]
      \addplot[red] coordinates{(0,0) (1,1) };
      \addplot+[red] coordinates{(0,2) (1,1) } \closedcycle;
    \end{axis}
  \end{tikzpicture}
\end{frame}
\begin{frame}
  \frametitle{Change of Numeraire}
  Using Ito lemma, we have $dZ(t,T) = Z(t,T) \left( \Sigma(t,T)dW_t + (\frac{1}{2}\Sigma^2(t,T) - \int_t^T \alpha(t,u)du)dt\right)$. Using Girsanov theorem, we have
  \begin{align*}
    dZ(t,T) &=Z(t,T)\Sigma(t,T)d\tilde{W}_t \\
    \tilde{W}_t &= W_t + \int_0^t \gamma_s ds \\
    \gamma_t &= \frac{1}{2} \Sigma(t,T) - \frac{1}{\Sigma(t,T)}\int_t^T \alpha(t,u) du
  \end{align*}
  Note the $\gamma_s$ is computed from $\alpha(t,T)$ and $\sigma(t,T)$, but cannot not depend on $T$, so this puts restrictions on the functional form of real world drift and vol.
\end{frame}
\begin{frame}
  \frametitle{Rates under $\mathbf{Q}$}
  We should have 0 by differentiate $\gamma$ against $T$, so
  \begin{align*}
    \alpha(t,T) &= \sigma(t,T)( \gamma_t - \Sigma(t,T)) \\
    df(t,T) &= \sigma(t,T)d\tilde{W}_t - \sigma(t,T) \Sigma(t,T)dt \\
    r(t) &= f(0,t) + \int_0^t\sigma(s,t)d\tilde{W}_s - \int_0^t \sigma(s,t) \Sigma(s,t)ds \\
  \end{align*}
  \begin{itemize}
  \item All bonds have to be made into martingale at the same time, so the market is arbitrage free.
  \item Real world drift does not matter, and the vol remains the same. Drift of the bonds goes to 0 not the rates.
  \item Short rate models can be expressed in HJM format ( HW, CIR, BK ) and can have closed form solution.
  \end{itemize}
\end{frame}
\begin{frame}
  \frametitle{Application to Commodity}
  In commodity, we need to model the forward/futures curve like rates. We know all futures have zero drift.
  \begin{itemize}
  \item Future price equals to forward price, when correlation to margin interest is assumed to be zero.
  \item Forward price process has zero drift.
  \item Forward contract $B_t(F_t-K)$ and futures contract $M_t(F_t-K)$ are both tradables. $B_t$ is usual zcb, while $M_t$ is a money market account that earns risk free interest on the balance.
  \item Forward contract value with zcb as the numeraire $B_t^{-1} B_t F_t$ is a martingale.
  \item Similarly future contract with either $B_t$ or $M_t$ as numeraire $M_t^{-1}M_tF_t$ is also martingale. In fact, ratio of any two tradables' value is a martingale under complete market no arbitrage.
  \end{itemize}
\end{frame}
\begin{frame}
  Assuming a lognormal model for com future, we have
  $$\frac{dF(t,T)}{F(t,T)}=\sigma(t,T)d\tilde{W}(t)$$
  It is easy to generalize it to 2-factor model:
  $$\frac{dF(t,T)} { F(t,T)}=\sigma_1(t,T)d\tilde{W}_1(t) + \sigma_2(t,T)d\tilde{W}_2(t)$$
  But good models should be able to both fit/generate realistic scenarios and have parameters that can be controlled with some interpretation. People tends to prefer the model to be specified in a certain way that relates to real world observables (e.g. below Gabillon model), then solve by transform back to a general problem (e.g. Anderson ).
  $$dF(t,T) = F(t,T)(\sigma_S(t) e^{- \lambda (T - t )} d\tilde{W}_1(t) + \sigma_L d\tilde{W}_2(t))$$
\end{frame}

\end{document}


\documentclass[twoside,10pt]{article}
%\setlength{\oddsidemargin}{0in}
%\setlength{\evensidemargin}{0in}
%\setlength{\textwidth}{16.0cm}
%\setlength{\textheight}{22cm}
%\pagestyle{myheadings}
\usepackage{xparse}
\usepackage[margin=0.9in]{geometry}
\usepackage{tcolorbox}
\setlength\parindent{0pt}
% \setlength{\parskip}{.5em}
\definecolor{bubblegum}{rgb}{0.99, 0.76, 0.8}
\usepackage[normalem]{ulem}
\usepackage{xcolor,colortbl}
% \usepackage{multirow}
\usepackage{framed}
\usepackage{caption}
\usepackage{subcaption}
\captionsetup{font={small}}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{listings}

\begin{document}

% \thispagestyle{empty}
\begin{center}
    %Title:
    {\Large \bf CS 682 – Artificial Intelligence}

    \vspace{.5cm}

    {\Large \bf Project 1 – 3×3 Tic-Tac-Toe}

    \vspace{0.5cm}
    {\large \bf Monikrishna Roy}
    \vspace{0.5cm}

    {\large \today}

\end{center}

\section*{Task Description}
Write a program to play 3×3 tic–tac–toe. The program should use minimax search. You do not need to play the entire game, you just need to make one move. For example, the program should be executed like:

\begin{verbatim}
    ./tictactoe <infile> <outfile>
    infile: file to be loaded with a board state
    output: file to be saved with board state after a move is made
\end{verbatim}

Your program should make a single move (x,y) which will be written to a file $<outfile>$, and exit. If there is an immediate win in the moves available, make that move. Otherwise, do minimax on all available moves.

\section*{Purpose}

The purpose of this assignment is to provide a basic programming experience with an algorithm commonly-used in AI. You should be able to demonstrate the ability to utilize a recursive algorithm in a game-playing scenario.

\section*{Algorithms}

This program uses the minimax algorithm with alpha-beta pruning to find the best available moves.

\subsection*{Algorithm Details}

A minimax algorithm is a recursive algorithm for choosing the next move in an n-player game, usually a two-player game. A value is associated with each position or state of the game. This value is computed by means of a position evaluation function and it indicates how good it would be for a player to reach that position. The player then makes the move that maximizes the minimum value of the position resulting from the opponent's possible following moves. If it is A's turn to move, A gives a value to each of their legal moves.
\vspace{0.3cm}

Alpha–beta pruning is a search algorithm that seeks to decrease the number of nodes that are evaluated by the minimax algorithm in its search tree. It is an adversarial search algorithm used commonly for machine playing of two-player games (Tic-tac-toe, Chess, Go, etc.). It stops evaluating a move when at least one possibility has been found that proves the move to be worse than a previously examined move. Such moves need not be evaluated further. When applied to a standard minimax tree, it returns the same move as minimax would, but prunes away branches that cannot possibly influence the final decision.

\newpage

\subsection*{Pseudocode}

Pseudocode for the minimax algorithm with alpha-beta pruning.

\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
function minimax_optimization(node, alpha, beta, maximizingPlayer) is
    if node is a terminal node or don't have legal moves then
        return the heuristic value of node
    if maximizingPlayer then
        value := -INF
        for each child of node do
            value := max(value, minimax_optimization(child, alpha, beta, FALSE))
            alpha := max(alpha, value)
            if value >= beta then
                break
        return value
    else
        value := +INF
        for each child of node do
            value := min(value, minimax_optimization(child, alpha, beta, TRUE))
            beta := min(beta, value)
            if value <= alpha then
                break
        return value
    \end{verbatim}
\end{tcolorbox}
\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
<Initial call>
minimax_optimization(origin, -INF, +INF, TRUE)
\end{verbatim}
\end{tcolorbox}

\section*{Pre-requisities for run locally}

Some pre-requisities to run the program.

\begin{itemize}
    \item cmake $>=$ 2.6
    \item make
    \item gcc/g++ $>=$ 5.4
\end{itemize}

\section*{Compilation Instructions}

Instructions to compile the project. First, need to create a build directory, then need to run make to build the program.

\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
<this section should only need to be done once>
$ mkdir build && cd build
$ cmake ..

<each time you want to compile (from the build directory)>
$ make
(if successful, the executable will be in the ’build’ directory)

\end{verbatim}
\end{tcolorbox}

\section*{Test/Play Instructions}

Details description of how to play the game. Options are not mandatory to play the game. However, if an input file is provided, then an output file must need to be provided.

\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
Usage
    ./tictactoe [inputFile] [outputFile] [--play]

DESCRIPTIONS
    A program to play the 3×3 tic–tac–toe game interactively.

    The program used a minimax search algorithm with alpha-beta
    pruning to get the best moves if no immediate win move is available.

    If the initial state is in losing position,
    it will return the first available move.

OPTIONS
    inputFile
        Name of the input file. It contains the initial board state.

        The board value should be -1, 0, or 1,
        where -1, 0, and 1 means O, empty, and X, respectively.

        If you give the the inputFile, you must give the outFile as well.

    outputFile
        Name of the output file. Programs will save the final state of the board.
        If file is not exit, it will create one.

    --play
        If you want to play interactively with AI.
        Otherwise, the program will make only one move.
\end{verbatim}
\end{tcolorbox}

\section*{Run Commands}

The commands are to run the program or conduct the entire game.

\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
<need to run from buld directory, skip if already in there>
$ cd build

<command to make one possible best move>
$ ./tictactoe test.board out.board

<command to play interactively with AI>
$ ./tictactoe test.board out.board --play

<command to play from the ZERO state>
$ ./tictactoe
\end{verbatim}
\end{tcolorbox}

\section*{Results and Discussion}

Here is the output for the provided test board without any immediate winning move. The program made the move (0, 0) which is one of the best moves.
\vspace{0.3cm}

\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
$ ./tictactoe test.board out.board

<initial state>
  |   |
----------
X | O |
----------
  |   |

Best move by AI (User X): 0 0

<state after best move>
X |   |
----------
X | O |
----------
  |   |
\end{verbatim}
\end{tcolorbox}

\vspace{0.3cm}
Here is the example for a test board with any immediate winning move. The program made the move (1, 2) as it's the immediate winning move.
\vspace{0.3cm}

\begin{tcolorbox}[boxrule=0pt]
    \begin{verbatim}
$ ./tictactoe test.board out.board

<initial state>
O |   |
----------
X | X |
----------
O |   |

Winning move by AI (User X): 1 2

<state after best move>
O |   |
----------
X | X | X
----------
O |   |
<winning results>
You Lost!! (User O).
\end{verbatim}
\end{tcolorbox}

\newpage

The below example is to conduct the full with AI interactively. In this example, User X is AI, and User O is human. The game was played to lose intentionally.
\vspace{0.3cm}

\begin{tcolorbox}[boxrule=0pt]
\begin{verbatim}
$ ./tictactoe test.board out.board --play

|   |
----------
X | O |
----------
  |   |
Do you want to start? (y/n): n
Best move by AI (User X): 0 0

X |   |
----------
X | O |
----------
  |   |

Enter the x, y (0 - indexed) (User O) : 2 0

X |   |
----------
X | O |
----------
O |   |

Best move by AI (User X): 0 2

X |   | X
----------
X | O |
----------
O |   |

Enter the x, y (0 - indexed) (User O) : 2 2

X |   | X
----------
X | O |
----------
O |   | O

Best move by AI (User X): 0 1

X | X | X
----------
X | O |
----------
O |   | O

You Lost!! (User O).
\end{verbatim}
\end{tcolorbox}

\end{document}


\documentclass{article}
\usepackage{graphicx}

\begin{document}

\title{JupyterLab \LaTeX}
\date{}
\maketitle

\section{Introduction}
This is a sample document demonstrating the ability to live-edit
\LaTeX documents in JupyterLab.
\\
\\
Right-click on this document, and select "Show LaTeX Preview".
A new panel should open up on the right with the PDF that is generated
by this document. If there are any errors in the document, an
error panel should open below.
\\
\\
We can write equations:
\begin{equation}
    \rho \left( \frac{\partial \mathbf{u}}{\partial t} + \mathbf{u} \cdot \nabla \mathbf{u} \right) =
    -\nabla P + \eta \nabla^2 \mathbf{u} + \rho \mathbf{g}.
\end{equation}
\\
\\
And we can write tables:
\begin{center}
  \begin{tabular}{ | l | c | r| }
    \hline
    1 & 2 & 3 \\
    4 & 5 & 6 \\
    7 & 8 & 99 \\
    \hline
  \end{tabular}
\end{center}

\subsection{We can add new subsections}
And we can include images, such as the Jupyter logo:
\begin{center}
  \includegraphics[width=0.65\textwidth]{images/jupyter_logo.png}
\end{center}

\pagebreak

You can also use SyncTeX by typing ⌘(CMD)/CTRL+⇧(SHIFT)+X.

\end{document}


\documentclass[letter,12pt]{article}
\usepackage{epsfig,latexsym,amsmath,amssymb,epic,eepic,psfrag,subfigure,float,euscript,array}
\usepackage[latin1]{inputenc}
\usepackage{standalone}
\usepackage{tikz,pgf,pgfplots}
\usepackage[margin=2.5cm]{geometry}

\newenvironment{exercise}[1][Uppgift]{\begin{trivlist} \item[\hskip
    \labelsep {\stepcounter{exerctr}\bfseries #1
      \arabic{exerctr}}]}{\end{trivlist}\vspace{10mm}}

\newcounter{exerctr}
\newcounter{abcctr}[exerctr]

\newcommand{\abc}{\noindent\vspace{1mm}\\ {\bf
    \stepcounter{abcctr}(\alph{abcctr})\ }}
\newcommand{\bbm}{\begin{bmatrix}}
\newcommand{\ebm}{\end{bmatrix}}
\newcommand{\point}[1]{\hfill {\bf (#1p)}\\ \vspace{-5mm}}
\newcommand{\ctrb}{\EuScript{S}}
\newcommand{\Lap}{\mathcal{L}}
\newcommand{\obsv}{\EuScript{O}}
\newcommand{\realdel}[1]{\text{Re}\left\{#1\right\}}
\newcommand{\imagdel}{\text{Im}}
\newcommand{\bC}{\mathbb{C}}
\newcommand{\bR}{\mathbb{R}}
\newcommand{\bmpv}{\begin{minipage}[t]}
\newcommand{\bmps}{\begin{minipage}[t]{45mm}}
\newcommand{\bmpm}{\begin{minipage}[t]{90mm}}
\newcommand{\bmpl}{\begin{minipage}[t]{\textwidth}}
\newcommand{\emp}{\end{minipage}}
\newcommand*{\zethree}{\big(z - \mexp{-3h}\big)}
\newcommand*{\mexp}[1]{\ensuremath{\mathrm{e}^{#1}}}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

\addtolength{\topmargin}{-1cm}
\textheight 23.5cm
%\oddsidemargin 0.61cm
%\evensidemargin 0.61cm

\title{Computerized control final exam (26\%)}
\author{Kjartan Halvorsen}

\begin{document}

\maketitle

\begin{description}
\item[Time] 2016-11-25 17:35 - 19:00
\item[Place] 5211
\item[Permitted aids] The single colored page with your own notes, table of transforms, calculator
\end{description}

All answers should be readable and well motivated (if nothing else is written). Solutions/motivations should be written on the provided spaces in this exam. Use the last page if more space is needed.

\begin{center}
{\Large Good luck!} \\
\end{center}

\noindent
\fbox{
\bmpl
{\bf Matricula and name:}\\
\vspace*{30mm}
\emp}

\clearpage

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

\subsection*{Problem 1 (50p)}
% Cancelling zero of double integrator - See example 3.15
The double-integrator
\begin{center}
\begin{tikzpicture}[block/.style={rectangle, draw, minimum width=15mm},]
  \node[block] (plant) {$\frac{1}{s^2}$};
  \draw[->] (plant) ++ (-2cm,0) -- node [above] {$u(t)$} (plant);
  \draw[<-] (plant) ++ (2cm,0) -- node [above] {$y(t)$} (plant);
\end{tikzpicture}
\end{center}
is a common model in physical systems where force (or torque) is the input and the position (or angle) is the output, and where the load is purely inertial (no friction, damping or elastic elements).

Zero-order-hold sampling of the double integrator with sampling period $h=1$ gives the discrete-time model
\begin{equation}
 y(k) = \frac{(q+1)}{2(q-1)^2} u(k)
\label{eq:doubleint}
\end{equation}

\subsubsection*{(a)}
Write the system in \eqref{eq:doubleint} as a \emph{difference equation}.

\noindent
\fbox{
\bmpl
{\bf Solution:}\\
\vspace*{60mm}
\emp}

\subsubsection*{(b)}
The plant is to be controlled by the two-degrees-of-freedom controller illustrated in figure~\ref{fig:feedback}.
\begin{figure}
\begin{center}
     \begin{tikzpicture}[scale = 0.8, node distance=25mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

     \node[sumnode, ] (sumerr) {\tiny $\sum$};
     \node[block, left of=sumerr] (ffw) {$\frac{2z}{z+1}$};
     \node[coordinate, left of=ffw] (refinput) {};
     %\node[above of=controller, node distance=6mm] {controller};
     \node[block, right of=sumerr, node distance=30mm] (plant) {$\frac{z+1}{2(z-1)^2}$};
     \node[block, below of=plant, node distance=20mm] (ffb) {$\frac{2(2z-1)}{z+1}$};

     %\node[sumnode, right of=plant, node distance=24mm] (sum) {\tiny $\sum$};
     %\node[above of=tank, node distance=6mm] {motor};
     \node[coordinate, right of=plant, node distance=20mm] (measurement) {};
     \node[coordinate, right of=measurement, node distance=20mm] (output) {};
     %\node[coordinate, above of=sum, node distance=12mm] (disturbance) {};

     \draw[->] (refinput) -- node[above, near start] {$u_c(k)$} (ffw);
     \draw[->] (ffw) -- node[above] {} (sumerr);
     \draw[->] (sumerr) -- node[above] {$u(k)$} (plant);
     \draw[->] (plant) -- node[above, near end] {$y(k)$} (output);
     \draw[->] (measurement) |- (ffb);
     \draw[->] (ffb) -| (sumerr) node [left, pos=0.9] {$-$};

     \end{tikzpicture}
     \caption{Closed-loop system, problem 1 (b).}
     \label{fig:feedback}
   \end{center}
 \end{figure}
Show that the closed-loop system from the command signal $u_c(k)$ to the output $y(k)$ can be written
\[ Y(z) = \frac{z(z+1)}{z^2(z+1)} U_c(z) = z^{-1} U_c(z). \]

\noindent
\fbox{
\bmpl
{\bf Solution:}\\
\vspace*{140mm}
\emp}

\subsubsection*{(c)}
The controller from (b) gives the closed-loop discrete-time response
\[ y(k) = u_c(k-1) \]
which appears to be a system with very good performance, since the output follows the input perfectly with just a small delay of a single sampling period. Too good to be true? Yes, there is an important flaw with this controller, as shown in the simulation below. This is an example of hidden oscillations (oscillations in the continuous-time system that are invisible at the sampling instants).
\begin{center}
\includegraphics[width=0.7\linewidth]{p1_closed_loop_step-crop}
\end{center}

Consider the feedback part of the controller
\begin{equation}
 F_{fb}(z) = \frac{2(2z-1)}{z+1}.
 \label{eq:feedback}
\end{equation}
Figure~\ref{fig:step} shows four step-responses. Which of these responses corresponds to the step-response from the system \eqref{eq:feedback}? Motivate!

\noindent
\fbox{
\bmpl
{\bf Answer and motivation:}\\
\vspace*{100mm}
\emp}

\begin{figure}[tp]
\begin{center}
\begin{tabular}{cc}
A & B\\
\includegraphics[width=0.4\linewidth]{step-plot-1-crop}
&\includegraphics[width=0.4\linewidth]{step-plot-2-crop}\\
C & D\\
\includegraphics[width=0.4\linewidth]{step-plot-3-crop}
&\includegraphics[width=0.4\linewidth]{step-plot-4-crop}

\end{tabular}
\caption{Step responses, problem 1 (c).}
\label{fig:step}
\end{center}
\end{figure}

\cleardoublepage

\subsection*{Problem 2 (50p)}

The double-integrator can be represented on state-space form as
\begin{equation}
\begin{split}
x(k+1) &= \bbm 1 & 1\\0 & 1\ebm x(k) + \bbm 0.5\\1\ebm u(k)\\
y(k) &= \bbm 1 & 0 \ebm u(k)
\end{split}
\end{equation}

\subsubsection*{(a)}

Show that state-space model indeed corresponds to the pulse transfer function
\[ H(z) = \frac{z+1}{2(z-1)^2}. \]

\noindent
\fbox{
\bmpl
{\bf Solution:}\\
\vspace*{120mm}
\emp}

\subsubsection*{(b)}

Determine a state feedback
\[ u(k) = u_c(k) - k_1 x_1(k) - k_2 x_2(k) \]
which gives a closed-loop system with poles in the origin, i.e.~deadbeat control.

\noindent
\fbox{
\bmpl
{\bf Controller design:}\\
\vspace*{160mm}
\emp}

\cleardoublepage

\noindent
{\bf If necessary,} you can continue your solutions on this sheet. Mark clearly which problem the solution corresponds to.

%\end{document}

\section*{Solutions}
\subsection*{Problem 1}

\subsubsection*{(a)}

\begin{equation*}
\begin{split}
 y(k) &= \frac{(q+1)}{2(q-1)^2} u(k) \\
2(q-1)^2y(k) &= (q+1)u(k)\\
2(q^2 - 2q +1)y(k) &= (q+1)u(k)\\
2y(k+2) - 4y(k+1) + 2y(k) &= u(k+1) + u(k).
\end{split}
\end{equation*}

\subsubsection*{(b)}
Using Mason's rule (``gain of direct path to output divided by one plus the loop gain'') we get
\begin{equation}
\begin{split}
Y(z) &= \frac{ \frac{z+1}{2(z-1)^2} \cdot \frac{2z}{z+1}}{ 1 + \frac{z+1}{2(z-1)^2} \cdot \frac{2(2z-1)}{z+1}}U_c(z) \\
     &= \frac{ \frac{z}{(z-1)^2}}{1 + \frac{2z-1}{(z-1)^2} }U_c(z)\\
     &= \frac{ z }{(z-1)^2 + (2z-1)} = \frac{z}{z^2} = z^{-1} U_c(z).
   \end{split}
 \end{equation}

\subsubsection*{(c)}

The system (the feedback controller)
\[ F_{fb}(z) = \frac{2(2z-1)}{z+1}\]
has a pole in \(z=-1\). Such a system will give time-responses with alternating positive and negative values and without decay since the pole is on the unit circle. This corresponds to step response \textbf{B}. One can also argue that all step-responses except B show stable, decaying responses.

\subsection*{Problem 2}

\subsubsection*{(a)}
Take the z-transform of the state-space system
\begin{equation}
\begin{split}
x(k+1) &= \underbrace{\bbm 1 & 1\\0 & 1\ebm}_{\Phi} x(k) + \underbrace{\bbm 0.5\\1\ebm}_{\Gamma} u(k)\\
y(k) &= \underbrace{\bbm 1 & 0 \ebm}_{C} u(k)
\end{split}
\end{equation}

and assume the initial states to be zero to obtain
\begin{equation}
\begin{split}
zX &= \Phi X + \Gamma U\\
(zI - \Phi) X &= \Gamma U\\
X &= (zI-\Phi)^{-1}\Gamma U.
\end{split}
\end{equation}
Insert into the measurement equation to get
\[ Y = C(zI-\Phi)^{-1}\Gamma U,\]
so the pulse transfer function is given by
\begin{equation}
\begin{split}
H(z) &= C(zI-\Phi)^{-1}\Gamma = C \bbm z-1 & -1\\ 0 & z-1 \ebm^{-1}\Gamma\\
     &= C \frac{1}{(z-1)^2} \bbm z-1 & 1\\ 0 z-1 \ebm \bbm 0.5\\1\ebm
      = \frac{1}{(z-1)^2} \bbm 1 & 0 \ebm  \bbm 0.5(z-1)+ 1\\ z-1 \ebm\\
     &= \frac{0.5z + 0.5}{(z-1)^2} = \frac{z+1}{2(z-1)^2}
   \end{split}
 \end{equation}

\subsubsection*{(b)}

With the feedback \[u(k) = u_c(k) - k_1 x_1(k) - k_2 x_2(k) = u_c(k) - Kx(k) \]
inserted into the state-space model we get the closed-loop state space model
\begin{equation*}
\begin{split}
x(k+1) = \left(\Phi - \Gamma K\right)x(k) + \Gamma u_c(k)
\end{split}
\end{equation*}
which have poles given by the characteristic equation of $\Phi - \Gamma K$
\[ \det \left(zI - (\Phi - \Gamma K)\right) = 0. \] We have
\[ \Gamma K = \bbm 0.5 k_1 & 0.5 k_2\\ k_1 & k_2 \ebm \] and
\[ \Phi - \Gamma K = \bbm 1 - 0.5k_1 & 1-0.5k_2\\-k_1 & 1 - k_2 \ebm \] which gives
\begin{equation*}
\begin{split}
\det \left(zI - (\Phi - \Gamma K)\right)  &= \det \bbm z - (1 - 0.5k_1) & -(1-0.5k_2)\\-(-k_1) & z-(1 - k_2) \ebm  = \det \bbm z - 1 + 0.5k_1 & -1+0.5k_2\\k_1 & z- 1 + k_2 \ebm\\
  &= (z - 1 + 0.5k_1)(z- 1 + k_2) - (-1+0.5k_2)k_1\\
  &= z^2 + (0.5k_1 +k_2 - 2)z + (0.5k_1-1)(k_2-1) + k_1 - 0.5k_1k_2\\
  &= z^2 + (0.5k_1 + k_2 - 2)z -k_2 -0.5k_1 + 1 + k_1.
\end{split}
\end{equation*}
We want this characteristic polynomial to equal \(z^2\) which gives two poles in the origin. This gives the system of equations
\begin{align*}
0.5k_1 + k_2 &= 2\\
0.5k_1 - k_2 &= -1\\
\end{align*}
or
\[ \bbm 0.5 & 1\\0.5 & -1\ebm K = \bbm 2\\-1\ebm \]
with solution
\[ K = \frac{1}{-1} \bbm -1 & -1\\-0.5 & 0.5 \ebm \bbm 2\\-1\ebm = \bbm 1\\1.5 \ebm. \]

\end{document}


\addtocontents{toc}{\protect\enlargethispage{\baselineskip}}\chapter{Language and structure} \label{ch:language and structure}
In \chapref{ch:information and agents}, I described informational spaces as well as agents and their interactions. Among other things, I introduced the infon space $({\cal I}, \odot)$ which will supply the semantic contents of utterances in Equilibrium Semantics. Assessing utterances requires certain operations on words and parse trees analogous to the operation $\odot$ on infons and so I now define two systems similar to $({\cal I}, \odot)$. One serves as the language to be analyzed and the other provides the syntactic contents.

\section{Language} \label{sec:language definition}
Assume a finite set of words $\cal W$. For example, $\cal W$ might include $\{\Expression{Bill}, \Expression{ran}\}$. A string on $\cal W$ of length $n$ is a function from $\{0, 1, 2, \ldots, n\}$ to $\cal W$. We usually write just a list of words such as $\Expression{Bill}$, $\Expression{Bill ran}$, or $\Expression{ran Bill}$. The unique string of length $0$, the empty string, is denoted by $e$. The unique zero string $0$ can be thought of as an illegitimate string. Both $e$ and $0$ are always members of $\cal W$. The concatenation $\cdot$ of two nonzero strings is defined in the usual way\footnote{See \citet[164--166]{wall:ml} for example.} and $w \cdot w'$ is written $ww'$. For example, $\Expression{Bill} \cdot \Expression{Bill} = \Expression{Bill Bill}$ and $\Expression{ran} \cdot \Expression{Bill} = \Expression{ran Bill}$. The concatenation of any string with $0$ is $0$. ${\cal W}^{\ast}$ is the set of all strings on ${\cal W}$ and is called the free monoid on $\cal W$. It is an infinite set and has an identity $e$ and a zero $0$.

\is{grammar}

Assume $G$ is a context-free grammar (CFG).\footnote{A context-free grammar is a grammar whose rules all have the form $\mathrm{A} \to w$. That is, there is no (linguistic) context surrounding $\mathrm{A}$ and $w$ so that the rule can be freely applied. See \citet[Chapter~9]{wall:ml}.}  Here is a simple example:
\[\mathrm{S} \to  \hbox{NP VP};\ \ \ \ \hbox{NP} \to \hbox{N};\ \ \ \ \hbox{VP} \to \hbox{V};\ \ \ \ \hbox{N} \to \hbox{Bill};\ \ \ \  \hbox{V} \to \hbox{ran}\]
Here, ``S'' stands for sentence, ``NP'' for noun phrase, ``VP'' for verb phrase, ``N'' for noun, and ``V'' for verb. The only sentence generated by this CFG is $\Expression{Bill ran}$. I will use ``parse'' and ``parse tree'' to refer also to the subtrees of phrases with the root node being any relevant nonterminal symbol. A tree such as $[_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}]]$ would count as a parse or parse tree of the noun phrase \Expression{Bill}.

I now use concatenation to define an operation that yields exactly the subsentential expressions and sentences of the language
$\cal L$ by forming an appropriate proper subset of ${\cal W}^{\ast}$. Define the following modification of concatenation: $w \circ_G w' = w \cdot w' = ww'$ when $ww'$ is a substring of some string in ${\cal W}^{\ast}$ that has at least one parse by $G$ and $w \circ_G w' = 0$ otherwise. The set formed by freely generating all strings from ${\cal W}$ by this special concatenation operation is the language $\cal L$. In our little example, ${\cal L} = \{e, 0, \Expression{Bill}, \Expression{ran}, \Expression{Bill ran}\}$. For instance, the string \Expression{ran Bill} has been dropped from ${\cal W}^{\ast}$ because it is not a substring of any string parseable by $G$.\footnote{Consider the sentence \Expression{I handed you the salt}. Then the string \Expression{you the} is a member of the corresponding $\cal L$ even though it is not a legitimate phrase because it is a substring of the whole sentence which would be parseable by the corresponding $G$. I owe this example to Tom Wasow.\ia{Wasow, Thomas}} This operation is called \emph{grammatical concatenation} and is abbreviated to $\circ$. It is associative but not commutative and has $e$ as its identity and the zero element $0$. In other words, $({\cal L}, \circ)$ is a monoid with a zero. A sentence $\varphi$ of $\cal L$ is made up of individual words $\varphi_1 \circ \varphi_2 \circ \ldots \circ \varphi_n = \varphi_1 \varphi_2 \ldots \varphi_n$ for some natural number $n$. For the sentence $\varphi = \Expression{Bill ran}$, $\varphi_1 = \Expression{Bill}$, $\varphi_2 = \Expression{ran}$, and $\varphi = \varphi_1 \circ \varphi_2 = \varphi_1\varphi_2$.

\section{Algebraic system of trees} \label{sec:algebraic system of trees}
So far, I have defined two algebraic systems $({\cal I}, \odot)$ and $({\cal L}, \circ)$ that capture the structure of infons and linguistic expressions. One describes the world and the other language. The third system involves a new way to describe the grammar $G$ as a system of trees with a product operation.

Each of the five rules of the CFG above can be re-described as a tree. For example, the tree corresponding to the first rule is $[_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]]$ and the tree corresponding to the fourth rule is $[_{\mathrm{N}}\, \mathrm{Bill}]$. Thus, $G$ can be expressed either as a set of rules or as a set of trees. However, we cannot define the desired operation on these trees directly and a little work is required to get them into the appropriate form.

The product operation is defined in two stages. First, an intuitive substitution or merging operation on parse trees is specified as follows. A tree such as $t' = [_{\mathrm{X}}\, \ldots ]$ can be substituted into $t = [_{\mathrm{Z}} [_{\mathrm{X}}\, ] \ldots ]$ to form $t'' = [_{\mathrm{Z}} [_{\mathrm{X}}\, \ldots ] \ldots ]$ where the $\ldots$ from $t'$ have been entered into $t$ because the outer category X of $t'$ matched an inner category X of $t$. If there is more than one X in $t$ that matches the X in $t'$, then the leftmost one is substituted into. This operation is denoted $\lhd$ and we write $t \lhd t' = t''$. It is neither associative nor commutative and is identical to the substitution operation defined in \citet{joshi:tag} and \citet{joshi:tag2}.

Consider now the following merging of the second and fourth tree above: $[_{\mathrm{NP}} [_{\mathrm{N}}\, ]] \lhd [_{\mathrm{N}}\, \mathrm{Bill}] = [_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}]]$. This can be informally described by saying that we have merged the simple tree $[_{\mathrm{N}}\, \mathrm{Bill}]$ as far as it could go up the parse tree of the whole sentence without encountering another branch. It cannot go any further because the tree encountered is $[_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]]$ which has another branch involving the verb phrase. I call this procedure \emph{chaining}, so we say that simple lexical trees are chained as far up as possible. It is possible to chain the third and fifth trees in the same way to get $[_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]]$. Thus, we are left with just two maximally chained trees from the original five trees and we also have the first tree -- $[_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]]$. These are:
\[[_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]];\ \ \ \ [_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}];\ \ \ \ [_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]]\]
%After we have obtained such maximally chained trees and unchained trees, we divide them into two groups of chained and unchained trees. In our example, the two groups contain $[_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}]$ and $[_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]]$ on the one hand and $[_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]]$ on the other.
The chained trees, also called elementary trees, are collected and given names as follows:
\[t_1 = [_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}]]\]
\[t_2 = [_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]]\]
The subscripts of $t_1$ and $t_2$ are determined by the sentence being considered, namely, \Expression{Bill ran}, so that the tree involving the first word \Expression{Bill} is indexed by 1 and the tree involving the second word \Expression{ran} is indexed by 2. In more complex sentences, there will be more than nine elementary trees and then the indexes will be written (10), (11), (12), and so on. Keeping track of the indexes in this way makes it easier to describe the operation below. The unchained trees -- just $[_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]]$ in our case -- are left in $G$.

Now, I describe a more complex product operation $\star_{G, u}$ on these trees. It is parametrized by $G$ as $\circ_G$ was and by $u$ as $\odot_{u}$ was. The utterance situation is needed because the sentence being parsed enters via $u$ and it is on the basis of the sentence that the trees can be properly indexed as explained above.

Because the chosen CFG is so simple, there is just one nontrivial product:
\begin{eqnarray*}
t_1 \star_{G, u} t_2 & = & [_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}]] \star_{G, u} [_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]] \\
& = & ([_{\mathrm{S}}[_{\mathrm{NP}}\, ][_{\mathrm{VP}}\, ]] \lhd [_{\mathrm{NP}} [_{\mathrm{N}}\, \mathrm{Bill}]]) \lhd
[_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]] \\
& = & [_{\mathrm{S}}[_{\mathrm{NP}}\, \mathrm{Bill}][_{\mathrm{VP}}\, ]] \lhd [_{\mathrm{VP}}[_{\mathrm{V}}\, \mathrm{ran}]] \\
& = & [_{\mathrm{S}}[_{\mathrm{NP}}\, \mathrm{Bill}][_{\mathrm{VP}}\, \mathrm{ran}]] \\
& = & t_{12}
\end{eqnarray*}

In other words, the tree product draws upon relevant trees in $G$ such as $[_{\mathrm{S}}[_{\mathrm{NP}}\, ]\allowbreak\relax[_{\mathrm{VP}}\, ]]$ to enable them to be merged or substituted. In this product, only one such tree in $G$ was introduced but in general there could be more, both to the left of $t_1$ or $t_2$. This is why the operation is parametrized by $G$.

We now add two special trees. The first is the empty tree, $t_e$, and the second is a  tree $t_0$. The former serves as the identity of the set of trees obtained via this product and the latter as the zero of the set. That is, $t \star_{G,u} t_e = t_e \star_{G,u} t = t$ for all $t$ and $t \star_{G,u} t_0 = t_0 \star_{G,u} t = t_0$ for all $t$, the latter being true by definition. When two incompatible trees are multiplied, for example $t_1 \star_{G,u} t_{12}$, the result is stipulated to be $t_0$.

Note the special vector index ``12'' of the product $t_{12}$. First, this index should not be confused with an elementary tree index such as (12) which would be expressed as $t_{(12)}$. In the case of $t_{12}$, the vector index has two components; in the case of $t_{(12)}$ the vector index has just one component. Second, it may seem at first sight that we could have obtained the product $t_2 \star_{G,u} t_1 = t_{12}$ in the same way, by first substituting the verb and then the noun. But instead the product $t_2 \star_{G,u} t_1 = t_0$ by stipulation. The basic rule is that the subscript of the first multiplicand must be strictly lower than the subscript of the second multiplicand to potentially yield a nonzero tree. When higher-level trees are multiplied in a more complex setting, the rule is that the first component of the first vector index must be strictly less than the first component of the second vector index. For example, a tree labeled $t_{34}$ would have a lower vector index than one labeled $t_{5}$ because $3 < 5$. So $t_{34} \star_{G,u} t_5$ could potentially be nonzero. In the reverse order, the product is always $t_0$.

There are thus five trees in the operation table for this sentence with respect to this CFG, the three above and $t_e$ and $t_0$. All combinations of these five trees yield one of these five trees. This gives us closure for the operation. The  multiplication table for $\star_{G,u}$ is shown in Figure~\ref{fig:operation table}. Observe that $t_1 \star_{G,u} t_2 = t _{12} \neq t_2 \star_{G,u} t_1$.

\begin{figure}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{c|c|c|c|c|c|c}
\multicolumn{1}{c}{$\star_{G,u}$} & \multicolumn{1}{c}{$t_e$} & \multicolumn{1}{c}{$t_1$} & \multicolumn{1}{c}{$t_2$} & \multicolumn{1}{c}{$t_{12}$} & \multicolumn{1}{c}{$t_0$} \\[.5ex] \cline{2-6}
$t_e$ & $t_e$ & $t_1$ & $t_2$ & $t_{12}$ & $t_0$ & \qquad \\[.5ex] \cline{2-6}
$t_1$ & $t_1$ & $t_0$ & $t_{12}$ & $t_0$ & $t_0$ & \qquad \\[.5ex] \cline{2-6}
$t_2$ & $t_2$ & $t_0$ & $t_0$ & $t_0$ & $t_0$ & \qquad \\[.5ex] \cline{2-6}
$t_{12}$ & $t_{12}$ & $t_0$ & $t_0$ & $t_0$ & $t_0$ & \qquad \\[.5ex] \cline{2-6}
$t_0$ & $t_0$ & $t_0$ & $t_0$ & $t_0$ & $t_0$ & \qquad \\[.5ex] \cline{2-6}
\end{tabular}
\caption{The operation table for $\star_{G,u}$} \label{fig:operation table}
\end{figure}

The basic rules for forming the product of trees are as follows:

\begin{enumerate}
\item Merge or substitute via $\lhd$ if possible.
\item Otherwise, successively introduce trees from $G$ to the left of either or both multiplicands until one or more substitutions via $\lhd$ are possible. For example, if two trees $t'$ and $t''$ have to be introduced in that order to the left of $t_i$ in a product $t_i \star_{G,u} t_j$, it would be as follows: $(t'' \lhd (t' \lhd t_i)) \lhd t_j$.
\item If the above fails, the result is $t_0$.
\end{enumerate}

This procedure always gives a unique result as only trees such as Z $\to$ X Y from $G$ can left multiply a product like $t_i \star_{G,u} t_j$ where $t_i = [_{\mathrm{X}}\, \ldots ]$ and $t_j = [_{\mathrm{Y}}\, \ldots ]$ and we stipulate that there cannot be another rule $\mathrm{Z}' \to$ X Y with $\mathrm{Z}' \neq$ Z in the CFG. Like $\lhd$, the operation $\star_{G,u}$ is neither associative nor commutative.

Full parsing may be done in either of two ways: by successive application of compatible rules in the CFG to yield S or by successive application of the $\star_{G,u}$ operation to yield a tree like $t_{12}$. All other combinations will ultimately result in $t_0$. In more complex CFGs, there will be multiple trees like $t_{12}$ that may be the end result of combining subtrees, which corresponds to multiple parses for the sentence. The word order of the sentence is automatically taken into account by the product operation owing to the indexing procedure so the yield of successive applications of $\star_{G,u}$ is guaranteed to match it.

Consider the algebraic system $({\cal T},\star_{G,u})$ where $\cal T$ is the set of five trees. This captures the relevant subset of the CFG for this sentence and so is an equivalent way to express a context-free grammar sentence by sentence. Each sentence corresponds to a separate algebraic system, all of which can in principle be combined into a larger system but this is unnecessary in practice. If we start with the elementary trees $t_e$, $t_1$, $t_2$, and $t_0$, we can freely generate the whole set $\cal T$.

%In general, there will be an infinite number of trees in $\cal T$ but only a finite number of elementary trees and one may work with just these elements.

I will assume the grammar $G$ for $\cal L$ can be rewritten as a system of trees $({\cal T},\star_{G,u})$ for each sentence in the manner described above. For convenience, I will drop the parameters from the notation and write just $\star$ henceforth and also frequently write $t_it_j$ instead of $t_i \star t_j$.

\section{Summary of assumptions}\label{sec:4.3}

Three algebraic systems have been constructed: $({\cal I}, \odot_{u})$, $({\cal L}, \circ_G)$, $({\cal T}, \star_{G,u})$ or, more simply, $({\cal I}, \odot)$, $({\cal L}, \circ)$, $({\cal T}, \star)$. The second system is a monoid with a zero but the  first and third have just an identity and a zero. As the parameter $u$ is fixed at the outset, each sentence in it is identifiable, and so the corresponding subsystem $({\cal T}, \star)$ is also identifiable.

In \chapref{ch:picture of communication}, I sketched what communication looks like in the small and in the large and briefly mentioned the games of partial information that arise. The interaction between speaker and addressee can be partly described by these partial information games and they lead to two more monoidal systems $({\cal G}, \otimes)$ and $({\cal G}', \otimes')$, the first involving semantic games and the second involving syntactic games. These are introduced in the context of the sentence $\varphi = \Expression{Bill ran}$ that we will consider in detail in \chapref{ch:defining communication games}.

A peculiarity of all these systems is that every element of each system that is not an identity element is what is called a zero divisor, that is, a nonzero element for which another nonzero element exists such that their product is zero. Also, there is just an operation of multiplication and the zeros are stipulated rather than arising as identities of a second operation of addition as happens in rings.


\chapter[Ionisation]{Ionisation}

 The class G4hIonisation calculates the continuous energy loss
  due to ionisation and
 simulates the 'discrete' part of the ionisation ,
the delta ray production by charged hadrons . The formulae used here
 are essentially the same than those in GEANT3 (\cite{hion.geant3}).

\section{Method}

In {\tt GEANT4}, $\delta$-rays are generated generally
only above a threshold energy
$T_{cut}$ which value is calculated from the parameter {\em cut in range}
for all  charged particles. The total cross-section
for the production of a $\delta$-ray electron of kinetic
energy $T > T_{cut}=$ by a particle of kinetic energy $E$
is:
\begin{equation}
\label{hion.a}
\sigma (E,T_{cut}) = \int_{T_{cut}}^{T_{max}} \frac{d \sigma (E,T)}{dT} dT
\end{equation}
where $T_{max}$ is the maximum energy transferable to the free electron:
\begin{equation}
T_{max} =\frac{2m(\gamma^2 -1)}
      {1+2\gamma\frac{m}{M}+
      \left(\frac{m}{M} \right)^2 }
\end{equation}

The energy lost in
ionising collisions producing $\delta$-rays below ${T_{cut}}$
are included in the continuous energy loss. The mean value of the
energy lost due to these {\it soft} collisions is:
\begin{equation}
\label{hion.b}
E_{loss}(E,T_{cut}) = \int_{0}^{T_{cut}}
T \frac{d \sigma (E,T)} {dT} dT
\end{equation}
where $m$ is the electron mass and $M$ is the mass of the incident particle.

\section{Continuous energy loss}
The integration of (\ref{hion.b}) leads to the Bethe-Bloch
stopping power or to the restricted energy
loss formula \cite{hion.pdgb}:
\begin{equation}
\label{hion.c}
 \left(\frac{dE}{dx} \right) =
\left \{
\begin{array}{lcl}
 N_{el} \frac{ Z_{inc}^{2}}{\beta^{2}} \left [
\ln \left (
\frac{2 m_{e} \beta^{2} \gamma^{2} T_{max} }{I^{2}}
\right ) - 2 \beta^{2} -
\delta -
\frac{ 2 C_{e}}{Z} \right ]
\hfill & \mbox{if} & T_{cut} \geq T_{max} \\ [0.5cm]
 N_{el} \frac{ Z_{inc}^{2}}{ \beta^{2}} \left [
\ln \left (
\frac{ 2 m_{e} \beta^{2} \gamma^{2} T_{c} }{I^{2}}
\right ) -
\beta^{2} \left ( 1 + \frac{T_{c}}{T_{max}} \right )
 - \delta
-\frac{2 C_{e}}{Z} \right ] \hfill & \mbox{if} & T_{cut} < T_{max}
\end{array}
\right .
\end{equation}

where,
$ N_{el}$ \mbox{is the electron density in the medium} ,
$T _c = \min(T_{cut},T_{max})$ , $m_e$ \mbox{the electron mass}
and $I$ is the average ionisation potential .
There exists a variety of phenomenological approximations for this.
 At present the simple expression  $I=16\:Z^{0.9}$ eV is used for all the
elements.

$\delta$ is a correction term which takes account of the reduction
in energy loss due to the so-called density effect. This becomes
important at high energy because media have a tendency to become
polarised as the incident particle velocity increases. As a consequence,
the atoms in a medium can no longer be considered as isolated. To correct
for this effect the formulation of Sternheimer~\cite{hion.sternheimer}
is used:

\begin{equation}
\delta = \left \{ \begin{array}{lll}
            0                   &    \mbox{if} & X  < X_0      \\
            4.606X + C + a(X_1-X)^m & \mbox{if} & X_0 \leq X < X_1 \\
            4.606X + C  & \mbox{if}  & X  \geq X_1
            \end{array} \right .
\end{equation}

where the medium-dependent constants are calculated as follows:
\[
\begin{array}{lcllcl}
X & = & \log_{10}(\gamma \beta) = \ln(\gamma^{2} \beta^{2})/4.606 &
\nu_{p} & = & \sqrt{ \frac{N_{el} e^2 }{\pi m} }
\mbox{\hspace{0.25cm}s$^{-1}$ plasma frequency} \\
N_{el} & = & \mbox{electron density in the material}
& C & = & -2 \ln \left ( \frac{I}{h\nu_{p}} \right ) - 1 \\
a & = & \frac{4.606(X_a - X_0)}{(X_1 - X_0)^m} & 4.606 \: X_{a} & = & -C
\end{array}
\]

For condensed media we have:

\[
\begin{array}{ll}
I < 100 \: \mbox{eV} & \left \{
\begin{array}{lcl}
X_{0} & = & \left \{
\begin{array}{lll}
0.2 & \mbox{if} & -C \leq 3.681 \\
-0.326C-1.0 & \mbox{if} & -C > 3.681
\end{array} \right . \\
X_{1} & = & 2.0 \\
m & = & 3.0
\end{array} \right . \\ [1.0cm]
I \geq 100 \: \mbox{eV} & \left \{
\begin{array}{lcl}
X_{0} & = & \left \{
\begin{array}{lll}
0.2 & \mbox{if} & -C \leq 5.215 \\
-0.326C-1.5 & \mbox{if} & -C > 5.215
\end{array} \right . \\
X_{1} & = & 3.0 \\
m & = & 3.0
\end{array} \right .
\end{array}
\]

and in the case of gaseous media $m=3$ and:

\[
\begin{array}{llcrcl}
X_0 = 1.6 & X_1 = 4 & \mbox{for} &       & C & \leq 9.5 \\
X_0 = 1.7 & X_1 = 4 & \mbox{for} & 9.5 < & C & \leq 10 \\
X_0 = 1.8 & X_1 = 4 & \mbox{for} & 10  < & C & \leq 10.5 \\
X_0 = 1.9 & X_1 = 4 & \mbox{for} & 10.5 < & C & \leq 11 \\
X_0 = 2.  & X_1 = 4 & \mbox{for} & 11  < & C & \leq 12.25 \\
X_0 = 2.  & X_1 = 5 & \mbox{for} & 12.25 < & C & \leq 13.804 \\
X_0 = 0.326C-2.5 & X_1 = 5 & \mbox{for} & 13.804 < & C &  \\
\end{array}
\]

$C_e/Z$ is a so-called {\it shell correction term}
which accounts for the fact that, at
low energies for light elements and at all energies for heavy ones, the
probability of collision with the electrons of the inner atomic shells
(K, L, etc.) is negligible. The semi-empirical formula used in
{\tt GEANT4}, applicable to all materials, is due to Barkas \cite{hion.barkas}:
\begin{eqnarray*}
 C_e(I,\eta) & = & (0.42237\eta^{-2}+0.0304\eta^{-4}-0.00038\eta^{-6})
10^{-6} I^2 \\
& +& (3.858\eta^{-2}-0.1668\eta^{-4}+0.00158\eta^{-6})10^{-9}I^3 \\
\eta & = &  \gamma \beta
\end{eqnarray*}
This formula breaks down at low energies, and it only applies for values
of $\eta > 0.13$ (i.e. $T > 7.9$ MeV for a proton). For $\eta \leq
0.13$ the shell correction term is calculated as:

\[
\left . C_{e}(I,\eta) \rule{0mm}{5mm} \right |_{\eta \leq
0.13} = C_{e}(I,\eta=0.13)\frac{\ln \left ( \frac{T}{T_{2l}} \right )}
{\ln \left ( \frac{7.9 \: 10^{-3} \: \rm GeV}{T_{2l}} \right )}
\]

i.e. the correction is {\it switched off} logarithmically from $T=7.9$
MeV to $T=T_{2l}=2$ MeV.

The mean energy loss can be described by the Bethe-Bloch formula
(\ref{hion.c}) only if the projectile velocity is larger than that
of orbital electrons. In the low-energy region where this is not
verified, a different kind of parameterisation should be used.
Presently a simple phenomenological parametrisation has been chosen which
 gives the energy loss as a function of the variable
 $\tau = \frac{T}{M_{proton}}$.
The parametrisation is given by the expression
\begin{equation}
\label{hion.lowen1}
\frac{dE}{dx}  =  N_{el} \cdot (A\sqrt{\tau}+B \tau )
       \mbox{\hspace{0.5cm}for\hspace{0.5cm}}
        \tau \leq \tau _{0}
\end{equation}
\begin{equation}
\label{hion.lowen2}
\frac{dE}{dx}  =  N_{el} \cdot \frac{C}{\sqrt{\tau }}
              \mbox{\hspace{0.5cm}for\hspace{0.5cm}}
        \tau _{0} < \tau \leq \tau _{1}
\end{equation}
 ,the values of the $A,B,C$ and $\tau _{0} ,\tau _{1}$ parameters can
be found in the code, here only the value of $\tau _{1}$ is given:
$\tau _{1}= \frac{2 \cdot MeV}{M_{proton}}$ .
  The parameters $A,B,C$ have been chosen in such a way that
 $\frac{dE}{dx}$ is a continuous function of the kinetic energy $T$
 at $T_0 =\tau _{0} \cdot M_{proton}$ and $T_1 =\tau _{1} \cdot M_{proton}$
 ( above $T_1 $ the Bethe-Bloch formula is used ).

The energy lost due to the soft $\delta$-rays is tabulated during
initialisation as a function of the medium and of the energy .

The energy loss of all charged hadrons
 is obtained from that of protons
by calculating the kinetic energy of a proton with the same
$\beta$, and using this value to interpolate the tables (see the
description for the energy loss process):
\begin{equation}
 T_{proton} =\frac{M_{\rm p}}{M}T
\end{equation}

\subsection{Total cross-section}
The integration of formula (\ref{hion.a}) gives the total cross-section :
\begin{equation}
\label{hion.d}
\sigma (Z,E,T_{cut}) = \left \{
\begin{array}{ll}
2 \pi r^2_0 m  \frac{Z}{\beta^2}
   \frac{1-Y+\beta^2 Y\ln Y}{T_{cut}} & \mbox{for spin-0 particles} \\ [.5cm]
2 \pi r^2_0 m  \frac{Z}{\beta^2} \left (\frac{1-Y+\beta^2 Y\ln Y}{T_{cut}}
+ \frac{T_{max}-T_{cut}} {2E^2} \right) &
\mbox{for spin-}\frac{1}{2}\mbox{~particles}
\end{array} \right .
\end{equation}
 , where  $Y = \frac{T_{cut}}{T_{max}} $ .

The mean free path is tabulated during initialisation
as a function of the medium and of the energy for all the charged hadrons.

\section{Simulation of the delta-ray production}
\subsection{Differential cross-section}
For heavy charged particles the differential cross-section
can be written:
\[
\begin{array}{lcll}
\frac {d\sigma }{dT}&=& 2 \pi Z r^2_ 0 m\frac{1}{\beta^2 }\frac{1}{T^2}
\left[ 1- \beta^2 \frac{T} { T_{max} }\right]
& \mbox{for spin 0 particle} \\ [.3cm]
\frac{d \sigma} {dT}&=& 2 \pi Z r^2_0 m \frac{1}{\beta^2 } \frac{1}{T^2}
\left[1- \beta^ 2 \frac{T}{T_{max} }+ \frac{T^2} {2E^2}
 \right] &  \mbox{for spin 1/2 particle}
\end{array}
\]
where $T_{max}$  is the maximum energy transferable to the free electron:
\[
 T_{max}  = \frac{2m(\gamma^2 -1)} {1+2\gamma (m/M) +
(m/M)^2}
\]
and $T_{cut}$  the energy threshold for the $\delta$-ray emission:
$ T_{cut} \leq T \leq  T_{max} $

\subsection{Sampling}
Apart from the normalisation, the cross-section can be written as

\begin{eqnarray*}
f(T) &=& \left(\frac{1}{T_{cut}} -\frac{1}{T_{max}}\right)
\frac{1}{T} \\
g(T) &=& 1 - \beta^2\frac{T}{T_{max}} +\frac{T^2}{2E^2}
\mbox{\quad(last term for spin-$\frac{1}{2}$ particle only)}
\end{eqnarray*}
The variable $\epsilon$ is sampled by:
\begin{enumerate}
\item sample $\epsilon$ from $f(\epsilon)$
\item calculate the rejection function $g(\epsilon)$ and accept the
sampled $\epsilon$ with a probability of $g(\epsilon)$.
\end{enumerate}

After the successful sampling of $\epsilon$,  the polar
angles of the scattered electron are generated
with respect to the direction of the
incident particle. The azimuthal angle $\phi$ is generated isotropically;
the polar angle
$\theta$ is calculated from the energy momentum conservation.
This information
is used to calculate the energy and momentum of both scattered
particles and to transform them into the global coordinate system.

\section{Status of this document}

  9.10.98  created by L.Urb\'an.

\begin{thebibliography}{99}

\bibitem[GEANT3]{hion.geant3}
  GEANT3 manual ,CERN Program Library Long Writeup W5013 (October 1994).
\bibitem[Mess70]{hion.messel}
  H.Messel and D.F.Crawford. Pergamon Press,Oxford,1970.
\bibitem[Ster71]{hion.sternheimer}
  R.M.Sternheimer. Phys.Rev. B3 (1971) 3681.
\bibitem[PDGB88]{hion.pdgb}
  Particle Data Group . Rev. of Particle Properties.
   Eur. Phys. J. C 3. (1988) 1.
\bibitem[Bark62]{hion.barkas}
  W. H. Barkas. Technical Report 10292,UCRL, August 1962.
\bibitem[Serr67]{hion.serre1}
  C.R.Serre. Techical Report 67 5 , CERN, March 1967.
\bibitem[Serr71]{hion.serre2}
  C.R.Serre. Techical Report 71 18, CERN, September 1971.

\end{thebibliography}