\documentclass[letterpaper,10pt]{article}
\usepackage[margin=2cm]{geometry}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[colorlinks]{hyperref}

\newcommand{\panhline}{\begin{center}\rule{\textwidth}{1pt}\end{center}}

\title{\textbf{Parametric Models: from data to models}}
\author{Pradeep Ravikumar (Instructor), HMW-Alexander (Noter)}

\begin{document}

\maketitle

\panhline
\href{../index.html}{Back to Index}

\panhline
\tableofcontents

\section*{Resources}

\begin{itemize}
	\item \href{../../Lectures/02_ParametricModels.pdf}{Lecture}
\end{itemize}

\panhline

\section{Recall Model-based ML}

\href{../01_Introduction/document.html}{Model-based ML}

\section{Model Learning: Data to Model}

Questiongs:
\begin{itemize}
	\item What are the principles in going from data to model?
	\item What are the guarantees of these methods?
\end{itemize}

\subsection{Bernoulli Distribution Example}

\begin{itemize}
	\item Bernoulli distribution model
	\begin{itemize}
		\item $X$ is a random variable with Bernoulli distribution when:
		\begin{itemize}
			\item $X$ takes values in $\{0,1\}$
			\item $P(X=1)=\theta,~P(X=0)=1-\theta$
			\item Where $\theta\in[0,1]$
		\end{itemize}
	\end{itemize}
	\item Draw \textbf{independent} samples that are \textbf{identically distributed} from same distribution model, Bernoulli distribution.
	\begin{itemize}
		\item If we observe an event $X\in\{0,1\}$, its probability $P(X)$ is $\theta^X(1-\theta)^{1-X}$
		\item Then the probability of data:
		\begin{equation}
		\begin{array}{rcl}
			\mathbb{P}(X_1,X_2,...,X_n;\theta) & = & \prod_{i=1}^{n}{P(X_i)} \\
											   & = & \prod_{i=1}^{n}{p^{X_i}(1-p)^{1-X_i}} \\
											   & = & p^{\sum_{i=1}^{n}{X_i}}(1-p)^{n-\sum_{i=1}^{n}{X_i}} \\
											   & = & p^{n_1}(1-p)^{n-n_1}
		\end{array}
		\end{equation}
	\end{itemize}
	\item Maximum Likelihood ($p(D|\theta)$) Estimator (MLE)
	\begin{itemize}
		\item Choose $\theta$ that maximizes the probability of observed data.
		\begin{equation}
		\begin{array}{rcl}
		\hat{\theta} & = & \arg\max_\theta{\mathbb{P}(X_1,\dots,X_n;\theta)} \\
					 & = & \arg\max_\theta{\theta^{n_1}(1-\theta)^{n-n_1}} \\
					 & = & \arg\max_\theta{n_1\log\theta+(n-n_1)\log(1-\theta)}\\
					 & \Rightarrow & \frac{n_1}{\hat{\theta}}-\frac{n-n_1}{1-\hat{\theta}} = 0 \\
					 & \Rightarrow & \hat{\theta}_{MLE} = \frac{n_1}{n}
		\end{array}
		\end{equation}
		\item MLE for parametric models
		\begin{itemize}
			\item Data: $X_1, X_2, \dots, X_n$
			\item Model: $P(X|\theta)$ with parameters $\theta$
			\item Assumption: data drawn \textbf{i.i.d} from distribution $P(X|\theta^*)$ for some unknown $\theta^*$
			\item Mission: recover $\theta^*$ from data $X_1,X_2,\dots,X_n$
			\item Likelihood function: $L(\theta):=\prod_{i=1}^{n}{P(X_i|\theta)}$
			\item Maximum Likelihood Estimator (MLE): find that parameter $\theta$ that would maximize the likelihood of $\theta$.
		\end{itemize}
	\end{itemize}
\end{itemize}

\subsection{How good is this MLE?}

\begin{itemize}
	\item Consistency:
	\begin{itemize}
		\item As we sample more and more times, we want our estimator to converge (in probability) to the true probability.
		\item For Bernoulli distribution example, we get the $\hat{\theta}=\frac{1}{n}\sum_{i=1}^{n}{X_i} \rightarrow \theta$ in probability as $n \rightarrow \infty$ by the \textbf{Law of Large Numbers}\footnote{It does not apply to distributions for whom Expected values do not exist. One example of such a distribution is the Cauchy distribution where the mean and the variance are undefined.}.
		\item An estimator $\hat{\theta}(X_1,\dots,X_n)$ where $X_i\sim P(X;\theta^*)$ is consistent if $\hat{\theta}\rightarrow \theta^*$ in probability as $n\rightarrow \infty$.
	\end{itemize}
	\item Unbiasedness:
	\begin{itemize}
		\item The estimator $\hat{\theta}$ is random: it depends on the samples drawn from a random distribution model with parameter $\theta$. It would be great if the expectation $\mathbb{E}[\hat{\theta}]$ of the estimator $\hat{\theta}$ be equal to the ``true" probability. This property is called unbiasedness.
		\item For Bernoulli example:
		\begin{equation}
		\begin{array}{rcl}
		\mathbb{E}(\hat{\theta}) & = & \mathbb{E}(\frac{n_1}{n}) \\
								 & = & \mathbb{E}(\frac{\sum_{i=1}^{n}X_i}{n}) \\
								 & = & \frac{1}{n}\sum_{i=1}^{n}{\mathbb{E}(X_i)} \\
								 & = & \mathbb{E}(X_1) \\
								 & = & \theta
		\end{array}
		\end{equation}
	\end{itemize}
\end{itemize}

\subsection{Gaussian Distribution Example}

Gaussian Distribution:
$$P(x|\mu,\sigma)=\frac{1}{\sigma\sqrt{2\pi}}\exp(-\frac{(x-\mu)^2}{2\sigma^2})=\mathcal{N}(\mu,\sigma^2)$$
\begin{itemize}
	\item Affine transformation:
	\begin{itemize}
		\item $X \sim \mathcal{N}(\mu,\sigma^2)$
		\item $Y=aX+b \sim \mathcal{N}(a\mu+b,a^2\sigma^2)$
	\end{itemize}
	\item Sum of Gaussians:
	\begin{itemize}
		\item $X \sim \mathcal{N}(\mu_X,\sigma^2_X)$, $Y \sim \mathcal{N}(\mu_Y,\sigma^2_Y)$
		\item $Z=X+Y \sim \mathcal{N}(\mu_X+\mu_Y,\sigma^2_X+\sigma^2_Y)$
	\end{itemize}
\end{itemize}

MLE for Gaussian mean and variance:
\begin{itemize}
	\item $\hat{\mu}_{MLE}=\frac{1}{n}\sum_{i=1}^{n}{x_i}$
	\item $\hat{\sigma}^2_{MLE}=\frac{1}{n}\sum_{i=1}^{n}{(x_i-\hat{\mu})^2}$
\end{itemize}

\subsubsection{The Biased Variance of a Gaussian}

The unbiased variance estimator:
$\hat{\sigma}^2_{unbiased}=\frac{n}{n-1}\hat{\sigma}^2_{MLE}$

Proof:
\begin{equation}
\begin{array}{rcl}
\mathbb{E}(\sigma^2_{MLE}) & = & \mathbb{E}(\frac{1}{n}\sum_{i=1}^{n}{(x_i-\hat{\mu})^2}) \\
						   & = & \frac{1}{n}\mathbb{E}(\sum_{i=1}^{n}{(x_i^2-2x_i\hat{\mu}+\hat{\mu}^2)}) \\
   						   & = & \frac{1}{n}\mathbb{E}(\sum_{i=1}^{n}{x_i^2}-\sum_{i=1}^{n}{2x_i\hat{\mu}}+\sum_{i=1}^{n}{\hat{\mu}^2}) \\
   						   & = & \frac{1}{n}\mathbb{E}(\sum_{i=1}^{n}{x_i^2}-2n\hat{\mu}^2+n\hat{\mu}^2) \\
   						   & = & \frac{1}{n}\mathbb{E}(\sum_{i=1}^{n}{x_i^2}-n\hat{\mu}^2) \\
   						   & = & \frac{1}{n}\sum_{i=1}^{n}{\mathbb{E}(x_i^2)-\mathbb{E}(\hat{\mu}^2)}\\
						   & = & \mathbb{E}(x_i^2)-\mathbb{E}(\hat{\mu}^2) \\
						   & = & (\sigma^2(x_i)+\mathbb{E}(x_i)^2)-(\sigma^2(\hat{\mu})+\mathbb{E}(\hat{\mu})^2) \\
						   & = & \sigma^2(x_i) - \sigma^2(\hat{\mu}) \\
						   & = & \sigma^2(x_i) - \sigma^2(\frac{1}{n}\sum_{i=1}^{n}{x_i}) \\
						   & = & \sigma^2(x_i) - \frac{1}{n^2}\sigma^2(\sum_{i=1}^{n}{x_i}) \\
						   & = & \sigma^2(x_i) - \frac{1}{n^2}n\sigma^2(x_i) \\
						   & = & \frac{n-1}{n}\sigma^2(x_i)
\end{array}
\end{equation}

\section{Convergence Rates of Estimator}

\subsection{Simple Bound (Hoeffding's Inequality)}

In probability theory, Hoeffding's inequality provides an upper bound on the probability that the sum of random variables deviates from its expected value. It can be applied to the important special case of identically distributed Bernoulli random variables.
\begin{itemize}
	\item Let $X_1,\dots,X_n$ be independent random variables bounded by the interval $[0,1]:0\leq X_i \leq 1$.
	\item and $\hat{\theta}=\bar{X}=\frac{1}{n}\sum_{i=1}^{n}{X_i}$
	\item then $\forall \epsilon>0,~P(|\hat{\theta}-\mathbb{E}(\hat{\theta})| \geq \tau) \leq \exp(-2n\epsilon^2)$
\end{itemize}

\subsection{PAC (Probably Approximate Correct) Learning}

PAC is a learning framework. Its initials stand for: Probably Approximately Correct. PAC learning aims to provide bounds (worst case estimates) on the size of the dataset.

The terminology 'Probably Approximately Correct' comes from the requirement that with high probability (greater than 1-delta) the error rate(epsilon) will be small.

PAC bounds are very conservative, i.e they strongly over-estimate the size of the dataset required to give good generalization.

A more detailed version about PAC theory include origin, introduction, framework, etc. can be found at:
\url{http://web.cs.iastate.edu/~honavar/pac.pdf}

Besides, one very interesting comment I have seen is that
"PAC is the bridge between statistic and machine learning."

\section{Computational Issues of MLE}

When number of parameters, or number of samples n is large, computing the MLE is a large-scale optimization problem.

\end{document}


\section{Search}
\label{sec:search}

As the search space of possible DSP program is extremely large, our search procedures must be exceptionally efficient.
As a first foray into DSP-PBE, we restrict ourselves to only synthesizing low-pass and high-pass filters, and global volume adjustment.
These two filters have the key property that they are quasi-commutative -- when the thresholds of these filters do not overlap, applying a low-pass and then a high-pass is the same as applying a high-pass and then a low-pass.
Although our approach has no theoretical basis for being applicable to non-commutative filters (for example, delay lines or ring filters), we do attempt to use our approach on such filters in Sec~\ref{sec:future}.
We leave a more thorough exploration of non-commutative filters to future work.

\subsection{Gradient Descent}

Gradient descent is a technique commonly used in modelling and machine learning.
Given a cost function, which represents the disagreement between a proposed model and the actual data, gradient descent can be used efficiently to minimize the cost and generate the model of best fit.
Gradient descent is only guaranteed to terminate with the globally minimal cost if the cost function being optimized is convex -- this is because gradient descent will ``descend'' along the surface of the cost function, in each step following the steepest gradient.
While we were not able to design our aural distance function from Section~\ref{sec:distance} to be convex, our cost function does demonstrate some properties of convexity that allow gradient descent to produce useful results, even if the result is not guaranteed to be the global minimum.
We will describe here some properties of our distance metric that were helpful in minimizing the cost of the synthesized filter, as well as the shortcomings of our design, and how we try to overcome them by adjusting our implementation of gradient descent.

\begin{figure}[!h]
%\includegraphics[width=\columnwidth]{figs/distCurves}
\include{figs/distCurves}
\caption{The distance curves showing the convex-like shape of the aural distance function. Each curve is the distance between an input file, and a filter applied to that file - $dist(I, \synthFilter(I))$.}
\label{fig:distCurves}
\end{figure}

In order to visualize the rough shape of our distance metric, we plot the distance between pairs of examples, and various possible DSP filters in Figure~\ref{fig:distCurves}.
Here we only visualize the distance curves in the dimension of the low-pass filter.
Notice that the curves exhibit a clear ``saddle'', which represents the minimum cost.
In the ideal case, gradient descent will find these points.
Note that we do not have these graphs available during synthesis -- producing the entire graph as in Figure~\ref{fig:distCurves} is prohibitively expensive.

In Figure~\ref{fig:distCurves}, the last curve we plot is the distance between \texttt{cartoon-spring.wav} and \texttt{cartoon-spring-hpf1500.wav}, the same file with a high pass filter applied with a threshold of 1500 Hz.
Notice that as the threshold of the low-pass filter applied to the input example (\texttt{cartoon-spring.wav} increases, the distance to the output example decreases.
This is because as a low-pass filter's threshold increases, it allows more and more frequencies to pass into the output -- thereby having less of an effect.
Whereas in the case of the \texttt{cartoon-spring-hpf1500.wav}, the true filter is a high-pass filter, so the less we apply a low-pass filter, the closer we get to the correct filter.

\begin{figure}
%\includegraphics[width=\columnwidth]{figs/distCurveZoom}
\include{figs/distCurveZoom}
\caption{Zooming in (1000 to 1500 Hz) on a portion of a curve from Figure~\ref{fig:distCurves}, we see the aural distance function is not perfectly convex on the micro scale.}
\label{fig:microDist}
\end{figure}

Although Fig.~\ref{fig:distCurves} depicts on one dimension of the search space (low-pass filter threshold), the actual space we need to search has many more dimensions.
In our implementation, we only explore a space of two DSP filters and volume adjustment, but this already results in 5 dimensional space (each filter requires both a threshold value and an amplitude value for how much of the filter to apply).
In general, this space becomes even larger for DSP-PBE as more DSP primitives (ring filter, white noise, delay etc) are added.
To speed up gradient descent, we use stochastic gradient descent, so that in each step, we only move in $d<5$ number of dimensions.

\subsection{Dealing with Non-convexity}
There are a number challenges with working with gradient descent in the aural DSP domain because our distance metric is not convex.
On the micro scale, the distance function is susceptible to noise and not entirely smooth, as shown in Figure~\ref{fig:microDist}.
In order to handle the micro scale variations, we use a periodic restart of the gradient descent.
This means that every $n$ rounds, as defined by the user, the gradient descent will backtrack to the best solution it has found so far.
Intuitively, the choice of $n$ represents how far gradient descent is allowed to explore a path of optimization before it is forced to give-up on that direction if it has not found any benefit to this direction.
The best value for $n$ then must be determined based on the trade-off of potential time wasted on poor choices, and the potential benefit of these choices.
In our implementation we use $n=4$ after a holistic evaluation of the convexity of the aural distance function.
The stochastic gradient descent will then continue, selecting dimensions to explore in each round using a new random seed.

\begin{figure}
%\includegraphics[width=\columnwidth]{figs/distCurveMacro}
\include{figs/distCurveMacro}
\caption{Looking at the portion of a curve from Figure~\ref{fig:distCurves} between 8k Hz and 20k Hz, we see the aural distance function is not perfectly convex on the macro scale. In this case, that is because the sample has very few frequencies above the 8k Hz range.}
\label{fig:macroDist}
\end{figure}

On the macro scale, we face the challenge that the distance function is again not convex -- there are many local minima and long plateaus, as shown in Figure~\ref{fig:macroDist}.
In order to overcome this, we must carefully pick the initial value for gradient descent.
If we pick a value in the middle of a plateau, the gradient descent algorithm will not find any significant gradient, and conclude we have reached the convergence condition.
In our current implementation, we iterate at large intervals (1000 Hz) of possible threshold values for both low and high pass filters.
We choose possible DSP programs that use only low pass, only high pass, and both low and high pass filters.
After evaluating these, we take the lowest cost initial DSP program, and start gradient descent from that point.

Finally, one of the key parts of a good application of gradient descent is the choice of the parameters such as the learning rate and the convergence goal.
These parameters must be adjusted based on the values observed from the cost (in our case, distance) function.
While the details of tuning gradient descent are outside the scope of this paper, it suffices to note that any change in the distance metric will likely also require a readjustment of these parameters.


\chapter{Pointfree funcoids as a generalization of frames}

I define an injection from the set of frames to the set of pointfree endo-funcoids.

This article is a rough partial draft of a future longer writing.

\section{Definitions}

\subsection{Pointfree funcoid induced by a co-frame}

Let $\mathfrak{L}$ is a co-frame.

We will define pointfree funcoid $\Uparrow \mathfrak{L}$.

Let $\mathcal{B} (\mathfrak{L})$ is a boolean lattice whose co-subframe
$\mathfrak{L}$ is. (That this mapping exists follows from
{\cite{stone-spaces}}, page 53.) There may be probably more than one such
mapping, but we just choose one $\mathcal{B}$ arbitrarily.

Define $\cl (A) = \bigsqcap \left\{ X \in \mathfrak{L} \hspace{1em} |
\hspace{1em} X \sqsupseteq A \right\}$.

Here $\bigsqcap$ can be taken on either $\mathfrak{L}$ or $\mathcal{B}
(\mathfrak{L})$ as they are the same.

\begin{obvious}
  $\cl \in \mathfrak{L}^{\mathcal{B} (\mathfrak{L})}$.
\end{obvious}

$\cl (A \sqcup B) = \bigsqcap \left\{ X \in \mathfrak{L} \hspace{1em} |
\hspace{1em} X \sqsupseteq A \sqcup B \right\} = \bigsqcap \left\{ X \in
\mathfrak{L} \hspace{1em} | \hspace{1em} X \sqsupseteq A, X \sqsupseteq B
\right\} = \bigsqcap \left\{ X_1 \sqcup X_2 \hspace{1em} | \hspace{1em} X_1
\sqsupseteq A, X_2 \sqsupseteq B \right\} = \bigsqcap \left\{ X_1 \hspace{1em}
| \hspace{1em} X_1 \sqsupseteq A \right\} \sqcup \bigsqcap \left\{ X_2
\hspace{1em} | \hspace{1em} X_2 \sqsupseteq B \right\} = \cl A \sqcup
\cl B$.

$\cl 0 = 0$ is obvious.

Hence we are under conditions of the theorem 14.26 in my book.

So there exists a unique pointfree endo-funcoid $\Uparrow \mathfrak{L} \in
\mathsf{FCD} (\mathfrak{F} (\mathcal{B} (\mathfrak{L})) , \mathfrak{F}
(\mathcal{B} (\mathfrak{L})))$ such that
\[ \langle \Uparrow \mathfrak{L} \rangle \mathcal{X} = \bigsqcap^{\mathfrak{F}
   (\mathcal{B} (\mathfrak{L}))} \langle \cl \rangle
   \up^{\text{$(\mathfrak{F} (\mathcal{B} (\mathfrak{L})) , \mathfrak{P}
   (\mathcal{B} (\mathfrak{L})))$}}  \mathcal{X} \]
for every filter $\mathcal{X} \in \mathfrak{F} (\mathcal{B} (\mathfrak{L}))$.

\subsection{Co-frame induced by a pointfree funcoid}

The co-frame $\Downarrow f$ for some pointfree endo-funcoids $f$ will be
defined to be the reverse of $\Uparrow$. See below for exact meaning of being
reverse.

Let restore the co-frame $\mathfrak{L}$ from the pointfree funcoid $\Uparrow
\mathfrak{L}$.

Let poset $\Downarrow f$ for every pointfree funcoid $f$ is defined by the
formula:
\[ \Downarrow f = \left\{ X \in Z (\Ob f) \hspace{1em} | \hspace{1em}
   \supfun{f} X = X \right\} . \]
\begin{rem}
  It seems that $\Downarrow$ is \emph{not} a monovalued function from
  $\mathsf{pFCD}$ to $\Ob (\mathbf{Frm})$.
\end{rem}

\subsection{Isomorphism of co-frames through pointfree funcoids}

\begin{rem}
  $\mathfrak{P} (\mathcal{B} (\mathfrak{L})) = Z (\mathfrak{F} (\mathcal{B}
  (\mathfrak{L})))$ (theorem 4.137 in {\cite{volume-1}}).
\end{rem}

\begin{thm}
  $\mathfrak{L} \mapsto \Downarrow \Uparrow \mathfrak{L}$ (where
  $\mathfrak{L}$ ranges all small frames) is an order isomorphism.
\end{thm}

\begin{proof}
  Let $A' \in \Downarrow \Uparrow \mathfrak{L}$. Then there exists $A \in
  \mathcal{B} (\mathfrak{L})$ such that $A' = \uparrow^{\mathcal{B}
  (\mathfrak{L})} A$.

  $\supfun{f} A' = \uparrow^{\mathcal{B} (\mathfrak{L})} \cl A$.

  $\supfun{f} A' = A'$ that is $\uparrow^{\mathcal{B} (\mathfrak{L})}
  \cl A = A' = \uparrow^{\mathcal{B} (\mathfrak{L})} A$. So $\cl A
  = A$ and thus $A \in \mathfrak{L}$.

  Let now $A \in \mathfrak{L}$. Then take $A' = \uparrow^{\mathcal{B}
  (\mathfrak{L})} A$. We have $\supfun{f} A' = \cl A =
  \uparrow^{\mathcal{B} (\mathfrak{L})} A = A'$. So $A' \in \Downarrow
  \Uparrow \mathfrak{L}$.

  We have proved that it is a bijection.

  Because $A$ and $A'$ are related by the equation $A' = \uparrow^{\mathcal{B}
  (\mathfrak{L})} A$ it is obvious that this is an order embedding.
\end{proof}

\section{Postface}

Pointfree funcoids are a \textbf{massive} generalization of locales and
frames: They don't only require the lattice of filters to be boolean but these
can be even not lattices of filters at all but just arbitrary posets. I think
a new era in pointfree topology starts.

Much work is yet needed to relate different properties of frames and locales
with corresponding properties of pointfree funcoids.


The authors simulate planetesimal and binary interactions using the parallelized gravitational N-body code PKDGRAV in the University of Bristol’s Advanced Computing Research
Centre. But as the simulations can get computationally expensive, they limit the resolution of the planetesimal disk to N = $10^6$ particles so as to be able to get statistically confident conclusions in a practical time frame

\subsubsection{Initial Conditions}
 The authors develop three simulations; two of which are of a circumbinary disk representative of the Kepler-34 system(ie., inner and outer disk boundaries include the current location of Kepler-34(AB)b) but vary in the masses of planetesimals by a factor of 1000 in order to determine the effect of inter-planetesimal gravity on the collision outcome and the third is of a control simulation around a single star using the same time step and disk parameters to set a benchmark for conditions known to sustain planetesimal accretion.
\\
The simulations are run with short time steps for over thousnads of orbits as the binary system has a high eccentricity and low orbital period. The simulations are started with unperturbed planetesimal disks. The disk around the binary perturbs from the initial eccentricity distribution into a eccentricity wave structure within 25 orbits. After over 1000 orbits it reaches a quasi steady state with low and high eccentricity planetesimals on crossing orbits
It is at this point that they turn on collisions and allow the planetesimals to collisionally evolve.

\subsubsection{Collision Model}
For this the authors use the collision model EDACM which has been integrated into PKDGRAV and can handle a variety of collision outcomes. Analytic determination of the outcome provides substantial improvements in computational efficiency and outcome accuracy and is done using a series of scaling laws which require only the collider impact velocity, impact parameter, mass ratio, and two fixed material property parameters.
\\
After collisions, fragments with mass less than $m_0 = 1.7 \times 10^{21}$ g are not resolved and are put into one of 10 radial bins depending on the colliders location, in order to maintain a practical value of N. These unresolved debris do not offer friction to the resolved planetesimals, although the total momentum of the system is conserved in the accretion process.

computational-physics/N-BODY SIMULATIONS OF KEPLER-34/Kepler_method.tex


%%
%% /docs/report/content/chapters/comparison.tex
%%
%% Created by Paul Warkentin <paul@warkentin.email> on 24/07/2018.
%% Updated by Paul Warkentin <paul@warkentin.email> on 25/07/2018.
%%

\section{Comparison}
\label{section:comparison}

Next to the SSD network are many other object detection algorithms published. In this section, we will take a look at two other networks.

\subsection{Faster R-CNN}

Faster R-CNN \cite{fasterrcnn2015} is a faster version of the Region-based Convolutional Neural Network (in short R-CNN). R-CNN uses an algorithm called Selective Search to propose objects and reduces the number of region proposals to around 2000. For each regional proposal, CNN features are calculated and finally classified. Faster R-CNN replaces that Selective Search with a convolutional network as the Selective Search is very slow. The new convolutional network is called Region Proposal Network (in short RPN) that generates regions of interests. \\

With Faster R-CNN, the idea of anchor boxes comes to life. For each location in a feature map extracted by convolutional layers from the image, anchor boxes with 3 different scales and 3 different aspect ratios are computed, resulting in 9 anchor boxes per location. A sliding window is then run spatially on these feature maps which are then fed to another network for classification and regression. The regression network predicts a bounding box, the classification network outputs a probability indicating whether the predicted box contains an object or background.

\subsection{YOLO}

You Ony Look Once (in short YOLO) \cite{yolo2016} is a Regression-based object detector. We will look into YOLOv2 here. \\

YOLO takes an image of size $448 \times 448$ as input and feeds it to a convolutional neural network with a tensor of $(7, 7, 1024)$ as output. This tensor is then fed into two fully connected layers that performs linear regression. The final output is then a tensor of size $(7, 7, 30)$ . Each cell in the $7 \times 7$ grid predicts two bounding boxes and its confidence scores. The score measures how accurate the bounding box is and how likely the box contains an object or background.


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\subsection*{checkgrad.m}

\begin{par}
\textbf{Summary:} checkgrad checks the derivatives in a function, by comparing them to finite differences approximations. The partial derivatives and the approximation are printed and the norm of the difference divided by the norm of the sum is returned as an indication of accuracy.
\end{par} \vspace{1em}

\begin{verbatim}  function [d dy dh] = checkgrad(f, X, e, varargin)\end{verbatim}
    \begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}

\begin{lstlisting}
%		f          function handle to function that needs to be checked.
%              The function f should be of the type [fX, dfX] = f(X, varargin)
%              where fX is the function value and dfX is the gradient of fX
%              with respect to the parameters X
%   X          parameters (can be a vector or a struct)
%   e          small perturbation used for finite differences (1e-4 is good)
%   varargin   other arguments that are passed on to the function f
%
%
% *Output arguments:*
%
%   d          relative error of analytical vs. finite difference gradient
%   dy         analytical gradient
%   dh         finite difference gradient
%
%
% Copyright (C) 2008-2013 by
% Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
%
% Last modified: 2013-03-21
%
\end{lstlisting}

\subsection*{High-Level Steps}

\begin{enumerate}
\setlength{\itemsep}{-1ex}
   \item Analytical gradient
   \item Numerical gradient via finite differences
   \item Relative error
\end{enumerate}

\begin{lstlisting}
function [d dy dh] = checkgrad(f, X, e, varargin)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
% 1. Analytical gradient
Z = unwrap(X); NZ = length(Z);                 % number of input variables
[y dy] = feval(f, X, varargin{:});             % get the partial derivatives dy
[D E] = size(y); y = y(:); Ny = length(y);     % number of output variables
if iscell(dy) || isstruct(dy); dy = unwrap(dy); end;
dy = reshape(dy,Ny,NZ);

% 2. Finite difference approximation
dh = zeros(Ny,NZ);
for j = 1:NZ
  dx = zeros(length(Z),1);
  dx(j) = dx(j) + e;                               % perturb a single dimension
  y2 = feval(f, rewrap(X,Z+dx), varargin{:});
  y1 = feval(f, rewrap(X,Z-dx), varargin{:});
  dh(:,j) = (y2(:) - y1(:))/(2*e);
end

% 3. Compute error
% norm of diff divided by norm of sum
d = sqrt(sum((dh-dy).^2,2)./sum((dh+dy).^2,2));
small = max(abs([dy dh]),[],2) < 1e-5; % small derivatives are poorly tested ...
d(d > 1e-3 & small) = NaN;             % ... by finite differences
d = reshape(d,D,E);

disp('   Analytic  Numerical');
for i=1:Ny;
    disp([dy(i,:)' dh(i,:)']);                           % print the two vectors
    fprintf('d = %e\n\n',d(i))
end

if Ny > 1; disp('For all outputs, d = '); disp(d); end; fprintf('\n');
\end{lstlisting}


\epigraph{``Since Newton, mankind has come to realise that the law of physics are always expressed in the language of differential equations"}{Steven Strogatz}

\section{RNN}
\subsection{Introduction}
Weather forecasting has traditionally been done by physical models of the atmosphere, which are unstable to perturbations, and thus are inaccurate for large periods of time\cite{why_rnn}. Since machine learning techniques are more sensitive to perturbations, it would be logical to combine a neural network with a physical model. Weather forecasting is a sequential data problem, therefore, a recurrent neural network is the most suitable option for this task.

\begin{definition}
A recurrent neural network is a class of artificial neural networks where connections between nodes form a directed graph along a temporal sequence.
\end{definition}

Before, we delve into the specific example of using a recurrent neural network to predict the future state of the atmosphere, it is necessary to review what a recurrent neural network is. Recurrent Neural Networks (RNNs) are neural networks that are used in situations where data is presented in a sequence. For example, let's say you want to predict the future position of a fast-moving ball. Without information on the previous position of the ball, it is only possible to make an inaccurate guess. If you had, however, a large number of snapshots of the previous position, you are then able to predict the future position of the ball with some certainty. RNNs excel at modelling sequential data such as this. This is due to sequential memory.

In order to intuitively understand sequential memory, the prime example would be the alphabet. While it is easy to say the alphabet from A-Z, it is much harder to go from Z-A. There is a logical reason why this is difficult. As a child, you learn the alphabet in a sequence. Sequential memory is a mechanism that makes it easier for your brain to recognise sequence patterns.

In a traditional neural network, there is an input layer, hidden layer, and an output layer. In a recurrent neural network, a loop is added to pass information forward as seen in the diagram below (provided by Towards Data Science)\cite{intro_rnn}:

\begin{figure}[H]
    \centering
    \includegraphics[width=.2\linewidth]{Images/rnn.png}
    \caption{Visualisation of a Recurrent Neural Network}
\end{figure}

The information that is forwarded is the hidden layer, which is a representation of previous inputs. How this works in practise is that you initialise your network layers and the initial hidden state. The shape and dimension of the hidden state will be dependent on the shape and dimension of your recurrent neural network. Then you loop through your inputs, pass the relevant parameter and hidden state into the RNN. The RNN returns the output and a modified hidden state. Last you pass the output of the hidden state to the output layer of the model, and it returns a prediction.

There is, however, a major problem known as short-term memory. Short-term memory is caused by something known as the vanishing gradient problem, which is also prevalent in other neural network architectures. As the RNN processes more steps, it has troubles retaining information from previous steps. Short-Term memory and the vanishing gradient is due to the nature of back-propagation. This can be comprehended through understanding how a neural network is trained\cite{intro_rnn}.

\begin{definition}
Back-propagation is an algorithm used to train and optimise neural networks.
\end{definition}

To train a recurrent neural network, you use an application of back-propagation called back-propagation through time. Training a neural network has three major steps. First, the relevant data vector is normalised between 0 and 1, the vector is fed into the RNN, and it goes through an activation function. The activation function utilised in the software is the rectified linear activation function\cite{lstm_rnn}.

\begin{definition}
The rectified linear activation function is a piece-wise linear function that will output the input directly if is positive, otherwise, it will output zero.
\end{definition}

The function is linear for values greater than zero, meaning it has a lot of the desirable properties of a linear activation function when training a neural network using back-propagation. Yet, it is a nonlinear function as negative values are always output as zero. As a result, the rectified function is linear for half of the input domain and nonlinear for the other half, it is referred to as a piece-wise linear function\cite{relu}. This nonlinear element is extremely important if the system has a nonlinear component, for example in predicting the evolution of the future state of the atmosphere.

\begin{figure}[H]
    \centering
    \includegraphics[width=.65\linewidth]{Images/relu.png}
    \caption{Sketch of the Rectified Linear Activation Function}
\end{figure}

Second, it outputs the results. Third, it compares the prediction to the observed state using a loss function.

\begin{definition}
A loss function outputs an error value which is an estimate of how poorly the network is performing.
\end{definition}

The lost function that will be utilised in the software will be the function for mean squared error. The reason for choosing this particular function is that it heavily penalises large errors, as it squares the difference between the predicted and actual value. A large error in a weather forecast is highly undesirable, hence, the use of this function. The function is represented below:

\begin{equation}
    MSE = \frac{1}{n}\sum_{i=1}^n(Y_i-\hat{Y_i})^2
\end{equation}

If  a vector of $n$ predictions is generated from a sample of $n$ data points on all variables, and $Y$ is the vector of observed values of the variable being predicted, with $\hat{Y_i}$ being the predicted values.

\begin{definition}
Mean squared error is the average squared difference between the estimated values and the actual value.
\end{definition}

Returning to the training of the RNN, it uses that error value from the loss function to do back propagation which calculates the gradients for each time step in the network. The gradient is the value used to adjust the networks internal weights, allowing the network to learn. The bigger the gradient, the bigger the adjustments and vice versa. Here is where the problem lies. When doing back propagation, the gradient of the current time step is calculated with respect to the effects of the gradients, in the time step before it. So if the adjustments to the time step before it is small, then adjustments to the current time step will be even smaller.  The gradient values will exponentially shrink as it propagates through each time step. That causes gradients to exponentially shrink as it back propagates down. The earlier layers fail to do any learning as the internal weights are barely being adjusted due to extremely small gradients.

Because of vanishing gradients, the RNN doesn’t learn the long-range dependencies across time steps. So not being able to learn on earlier time steps causes the network to have a short-term memory. In order to combat this, a long short-term memory is used\cite{intro_rnn}.

\subsection{LSTM}
LSTM's were created as a solution to the short-term memory problem. They have internal mechanisms called gates that can regulate the flow of information. These gates can learn which data in a sequence is important to keep or throw away. By doing that, it can pass relevant information down the long chain of sequences to make predictions. For example, if you were interested in buying a particular product, you might read a review in order to determine if the purchase of the product is a good decision. When you read a review, your brain subconsciously only remembers important keywords. You pick up words like ``amazing", ``superb", or ``awful", you don't remember words such as "the", "as", or "because". This is what an LSTM does, it learns to keep only the relevant information to make predictions.

An LSTM has a similar control flow as a recurrent neural network. It processes data passing on information as it propagates forward. The differences are the operations within the LSTM’s cells. The core concept of LSTM’s are the cell state, and it’s various gates. The cell state is the method by which information is transferred down the sequence chain. The cell state, in theory, can carry relevant information throughout the processing of the sequence. So even information from the earlier time steps can make its way to later time steps, reducing the effects of short-term memory. As the cell state goes on its journey, information gets added or removed to the cell state via gates\cite{lstm_rnn}.

\begin{definition}
A gate is an electric circuit with an output which depends on the combination of several inputs.
\end{definition}

Gates contain the sigmoid activation function. The sigmoid activation function squishes values between 0 and 1. That is helpful to update or forget data because any number getting multiplied by 0 is 0, causing values to disappears or be ``forgotten". Any number multiplied by 1 is the same value therefore that value stays the same or is ``kept".

\begin{figure}[H]
    \centering
    \includegraphics[width=.65\linewidth]{Images/sigmoid.png}
    \caption{Sketch of the Sigmoid Activation Function}
\end{figure}

There are three types of gates utilised within a neural network: a forget gate, an input gate, and an output gate. A forget gate decides what information should be thrown away or kept. Information from the previous hidden state and information from the current input is passed through the sigmoid function. An input gate is where the previous hidden state and current input are passed into a sigmoid function. The output gate decides what the next hidden state should be. The hidden state is also used for predictions. First, we pass the previous hidden state and the current input into a sigmoid function. Then we pass the newly modified cell state to the rectified linear activation function. We multiply the rectified linear activation function output with the sigmoid output to decide what information the hidden state should carry. The output is the hidden state. The new cell state and the new hidden state is then carried over to the next time step\cite{lstm_rnn}.

\subsection{Convolutional LSTM Network}
The formulation of a numerical weather prediction model is a spatiotemporal sequence forecasting problem that can be solved under a general sequence-to-sequence learning framework.

\begin{definition}
A spatiotemporal sequence is a sequence that contains both spatial and temporal information.
\end{definition}

In order to better model the spatiotemporal relationships, this project will utilise ConvLSTM layers; which was proposed by Xingjian Shi et. al\cite{convlstm}. This spatiotemporal sequence forecasting problem is different from the one-step time series forecasting problem because the prediction target of the problem is a sequence which contains both spatial and temporal structures. Although a LSTM layer has proven powerful for handling temporal correlation, it contains too much redundancy for spatial data. To address this problem, this project will use a ConvLSTM layer which has convolutional structures in both the input-to-state and state-to-state transitions\cite{convlstm}.

The major drawback of LSTMs is in the handling spatiotemporal data due to its usage of full connections in input-to-state and state-to-state transitions in which no spatial information is encoded. To overcome this problem, a distinguishing feature of a ConvLSTM cell is that all the inputs and gates of the ConvLSTM layer are 3D tensors whose last two dimensions are spatial dimensions. To get a better picture of the inputs and states, we may imagine them as vectors standing on a spatial grid. The ConvLSTM determines the future state of a certain cell in the grid by the inputs and past states of its local neighbour. This can easily be achieved by using a convolution operator in the state-to-state and input-to-state transitions\cite{convlstm}. This is what the ConvLSTM layer does.

\begin{figure}[H]
    \centering
    \includegraphics[width=.8\linewidth]{Images/convlstm.png}
    \caption{The architecture of a ConvLSTM cell.}
\end{figure}

\section{Dataset}
\subsection{ERA5 Atmospheric Reanalysis Dataset}\label{era5_dataset}
ERA5 provides hourly estimates of a large number of atmospheric, land and oceanic climate variables. The data covers the Earth on a 30km grid and resolves the atmosphere using 137 levels from the surface up to a height of 80km. ERA5 includes information about uncertainties for all variables at reduced spatial and temporal resolutions. Quality-assured monthly updates of ERA5 are published within 3 months of real time. Preliminary daily updates of the dataset are available to users within 5 days of real time. ERA5 combines vast amounts of historical observations into global estimates using advanced modelling and data assimilation systems\cite{era5}.

The ERA5 reanalysis dataset was used for training, validating and testing the performance of the neural network architecture. Reanalysis datasets provide the best guess of the atmospheric state at any point in time by combining a forecast model with the available observations. The raw data is available hourly for 40 years from 1979 to 2019 on a $0.25^{\circ}$ latitude-longitude grid ($721 \times 1440$ grid points) with 37 vertical levels. Since this raw dataset is quite significant, it is necessary to regrid the dataset to a lower resolution and use a smaller fraction of the available dataset\cite{rasp2020weatherbench}.  The poles were excluded from the dataset in order to avoid a singularity, and the potential negative impact that could have on predictive ability of the neural network.

It was ultimately decided to use a spatial resolution of $1^{\circ}$ ($179 \times 360$ grid points) and a temporal resolution of 2 hours. The data is split into yearly NetCDF files for each variable. The entire dataset at $0.25^{\circ}$ resolution has a size of 400GB, before the dataset was interpolated to a grid of a lower resolution. The prognostic variables of interest are temperature and geopotential, and were chosen based on meteorological considerations. Geopotential, and temperature are prognostic state variables in most physical numerical weather prediction and climate models\cite{rasp2020weatherbench}.

\subsection{Integrated Forecasting System}\label{ifs_section}
The Integrated Forecast System is a global numerical weather prediction system developed and maintained by the European Centre for Medium-Range Weather Forecasts organisation. The version of the IFS run at ECMWF is often referred to as the ``ECMWF" or the ``European model" in North America, to distinguish it from the American GFS. It comprises of a spectral atmospheric model with a terrain-following vertical coordinate system coupled to a 4D variational data assimilation system. In 1997 the IFS became the first operational forecasting system to use a 4D variational, data assimilation system

\begin{definition}
4D dimensional variational data assimilation system adjusts a short-range forecast, called the background, in space and time to bring it into closer agreement with meteorological observations.
\end{definition}

\begin{figure}[H]
    \centering
    \includegraphics[width=.8\linewidth]{Images/ifs.jpg}
    \caption{ECMWF Integrated Forecast System}
\end{figure}

It is one of the predominant global medium-range models in general use worldwide; its most prominent rival in the 6–10 day medium range include the American Global Forecast System, the Canadian Global Environmental Multiscale Model and the UK Met Office Unified Model. For context, Met Éireann utilises the IFS for medium-range forecasts; however for short-range forecasts, Met Éireann uses its own HARMONIE-AROME NWP model. The operational configuration consists of $1000 \times 900$ grid points in the horizontal at 2.5km resolution, with 65 levels in the vertical. A 54-hour forecast is produced four times a day, at 00Z, 06Z, 12Z and 18Z\cite{harmonie_arome_nwp}.

The IFS is the gold standard of medium-range numerical weather prediction. The current IFS deterministic forecast is computed on a cluster with 11,664 cores. One 10 day forecast at 10 km resolution takes around 1 hour of real time to compute\cite{rasp2020weatherbench}. The Integrated Forecasting System will be used as a comparison against the neural network.

To provide physical baselines more in line with the current resolution of the neural network, it was compared against the IFS model at two coarser horizontal resolutions, T42 (approximately $2.8^{\circ}$) with 62 vertical levels and T63 (approximately $1.9^{\circ}$) with 137 vertical levels. It must be noted that I personally did not generate such forecasts, or perform the analysis on said forecasts. It acquired the results from Weather Bench, a benchmark dataset for data-driven weather forecasting. According to said source; computationally, a single forecast takes 270 seconds for the T42 model and 503 seconds for the T64 model on a single XC40 node with 36 cores.

\section{Implementation}\label{implement_rnn}
Drawing from knowledge of current numerical weather prediction model frameworks, it may seem intuitive to train a machine learning model to produce the best possible single‐step forecast from a given atmospheric state. In practice, this can yield a model that performs well for short‐range forecasts but diverges from reality for longer range predictions. This is because there are no constraints on the CNN, physical or mathematical, that would prevent it from diverging from reality when its prediction fed back in as inputs no longer resemble an atmospheric state in the training data. In order to nudge the numerical weather prediction model toward learning to predict longer‐term weather and improve its long‐term stability, the model will be trained on multiple iterated predictive steps.

Initially, the model inputs included two time steps and it is tasked with predicting two output time steps. This resulted in the first couple of time step predictions producing relatively decent result, however, it quickly diverged from reality after this point. Hence, it was necessary to increase both the time steps and the output time steps to six. This increased the overall numerical stability of the model.

The dataset consists of three features: air temperature at a pressure surface of 850 hPa, geopotential at a pressure surface of 500 hPa, and air temperature at 2 metres above the surface. For a single day, there is twelve observations. The goal for this project will be to predict the relevant atmospheric parameter in 12 hours time given the last twelve hours of data. In order to make such predictions, it is necessary to create a window of the last 6 ($\frac{12}{2}$) observations to train the model\cite{time_series}. The neural network was trained on observational data from 2009 to 2015. The remainder of the dataset, 2016 to 2019, was preserved for validation, testing and benchmarking the neural network against physics-based models. The model was built using the open‐source Keras library for Python with Google's TensorFlow backend\cite{numerical_stability}.

\begin{minted}[mathescape,linenos,frame=lines]{python}
# Optimiser.
opt = Adam(lr=1e-3, decay=1e-5)
\end{minted}

Adam optimisation was chosen as the most appropriate optimiser for this particular model. This is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments. This method is computationally efficient, has little memory requirements, invariant to diagonal rescaling of gradients, and is well suited for problems that are large in terms of parameters.

\begin{minted}[mathescape,linenos,frame=lines]{python}
# First layer of model.
model.add(
    ConvLSTM2D(
        filters=64,
        kernel_size=(7, 7),
        input_shape=(6, 179, 360, 3),
        padding='same',
        return_sequences=True,
        activation='tanh',
        recurrent_activation='hard_sigmoid',
        kernel_initializer='glorot_uniform',
        unit_forget_bias=True,
        dropout=0.3,
        recurrent_dropout=0.3,
        go_backwards=True
    )
)
\end{minted}

The above code is the first layer of the model. The activation functions utilised in this layer are predefined within Tensorflow, and have been described at great length previously. The model consists of four ConvLSTM layers in total, with similar parameters as the aforementioned layer with a few variations.

\begin{minted}[mathescape,linenos,frame=lines]{python}
# Batch normalisation.
model.add(BatchNormalization())
\end{minted}

A consistent challenge in machine learning is that the model is updated layer-by-layer backward from the output to the input using an estimate of error that assumes the weights in the layers prior to the current layer are fixed. Because all layers are changed during an update, the update procedure is forever chasing a moving target. A batch normalisation layer occurs after each ConvLSTM layer to resolve this problem.

\begin{definition}
Batch normalization is a technique to help coordinate the update of multiple layers in the model.
\end{definition}

It does this by scaling the output of the layer, specifically by standardising the activation of each input variable per mini-batch, such as the activation of a node from the previous layer. Standardising the activation of the prior layer means that assumptions the subsequent layer makes about the spread and distribution of inputs during the weight update will not change. This has the effect of stabilising and speeding-up the training process of neural networks\cite{batch_normalization}.

\begin{minted}[mathescape,linenos,frame=lines]{python}
# Dropout.
model.add(Dropout(0.1))
\end{minted}

Each batch normalisation layer is then followed by a dropout. Dropout is a regularisation method that approximates training a large number of neural networks with different architectures in parallel. During training, some number of layer outputs are randomly ignored or “dropped out.” This has the effect of making the layer look-like and be treated-like a layer with a different number of nodes and connectivity to the prior layer. In effect, each update to a layer during training is performed with a different “view” of the configured layer. Dropout has the effect of making the training process noisy, forcing nodes within a layer to probabilistically take on more or less responsibility for the inputs. This conceptualisation suggests that perhaps dropout breaks-up situations where network layers co-adapt to correct mistakes from prior layers, in turn making the model more robust\cite{dropout}.

\begin{minted}[mathescape,linenos,frame=lines]{python}
# Add dense layer.
model.add(Dense(3))
\end{minted}

Following the final ConvLSTM layer, a dense layer is implemented. The dense layer is a neural network layer that is connected deeply, which means each neuron in the dense layer receives input from all neurons of its previous layer\cite{dense}. In our case, it results in the model outputting the expected shape by reducing the filters down to the three previously mentioned features. The code for the model in its entirety can be found in appendix \ref{model_code}.


\documentclass{article}

\title{\sc\LARGE CSCA67 Tutorial, Week 8\\
{\Large Nov. 2nd-Nov. 6th, 2015}}
\date{}
\author{\sc Compiled by {\em G. Singh Cadieux}\\[1ex]
\sc Adapted from\\
D. Cunningham. \textit{A logical introduction to proof}. Springer, 2012. \&\\
A. Bretscher, \href{http://www.utsc.utoronto.ca/~bretscher/a67/lectures/predicates.pdf}{\em CSCA67 Week 8 Lecture Notes}}

\usepackage{fullpage}
\usepackage{amsmath,amssymb}
\usepackage{color}
%\usepackage{multirow}
\usepackage{tikz}
\usepackage{hyperref}
%\usepackage{array}

\setlength{\parindent}{0pt}

\begin{document}
\maketitle

\section{\sc More formal logic problems}

\subsection*{Q: {\em Show that $(a\vee b)\wedge\neg(a\wedge b)$ is logically equivalent to $a\leftrightarrow \neg b$ using truth tables.}}
Using the method discussed last week, we construct a truth table for each of the statements.\\
(The truth table shown below merges the two truth tables into one.)
\begin{center}
\begin{tabular}{cc|c|c|c|c||c|c}
$a$&$b$&$a\wedge b$&$\neg(a\wedge b)$&$a\vee b$&$(a\vee b)\wedge\neg(a\wedge b)$&$\neg b$&$a\leftrightarrow \neg b$\\\hline
T&T&T&F&T&F&F&F\\
T&F&F&T&T&T&T&T\\
F&T&F&T&T&T&F&T\\
F&F&F&T&F&F&T&F
\end{tabular}
\end{center}
We have shown that the truth table for $(a\vee b)\wedge\neg(a\wedge b)$ and the truth table for $a\leftrightarrow \neg b$ are identical (the truth value of two statements is the same, given the same combination of truth values of $a$ and $b$). Thus, the two statements are logically equivalent.

\subsection*{Q: {\em Construct a truth table for $(p\vee r)\wedge(r\wedge q)$.}}
\begin{center}
\begin{tabular}{ccc|c|c|c}
$p$&$q$&$r$&$p\vee r$&$r\wedge q$&$(p\vee r)\wedge(r\wedge q)$\\\hline
T&T&T&T&T&T\\
T&T&F&T&F&F\\
T&F&T&T&F&F\\
T&F&F&T&F&F\\
F&T&T&T&T&T\\
F&T&F&F&F&F\\
F&F&T&T&F&F\\
F&F&F&F&F&F
\end{tabular}
\end{center}

\subsection*{Q: {\em Shade regions of a Venn diagram where $(p\vee r)\wedge(r\wedge q)$ is true.}}
Let $p$ be the statement ``$x\in P$", $q$ be the statement ``$x\in Q$", and $r$ be the statement ``$x\in R$", where $P,Q,R$ are sets.\\
Then $(p\vee r)\wedge(r\wedge q)$ represents the region(s) of the Venn diagram where $x\in P$ and $x\in Q$, or where $x\in R$ and $x\in Q$.\\[1ex]
Let us first shade in the region where $x\in P$ or $x\in R$ (red). Then let us shade in the region where $x\in R$ and $x\in Q$ (blue). The intersection between these regions represents our statement, since they are joined by an ``and."
\begin{center}
\begin{tikzpicture}
\filldraw[red!70] (0,0) circle[radius=0.5in];
\draw (0.75in,0) circle[radius=0.5in];
\filldraw[red!70] (0.375in,0.6in) circle[radius=0.5in];
\begin{scope}
\path[clip] (0.375in,0.6in) circle[radius=0.5in];
\draw[fill=blue!70,fill opacity=0.5] (0.75in,0) circle[radius=0.5in];
\end{scope}
\draw (-.65in,-.65in) rectangle (1.4in,1.25in);
\draw (0,0) circle[radius=0.5in];
\draw (0.375in,0.6in) circle[radius=0.5in];
\node at (0,0) {$P$};
\node at (0.75in,0) {$Q$};
\node at (0.375in,0.6in) {$R$};
\node at (-.5in,1.1in) {$\mathcal{U}$};
\end{tikzpicture}\qquad
\begin{tikzpicture}[a/.style={pin={[pin distance=.7in]above right:${\color{red}p,\,q,\,r}$}}]
\draw (0,0) circle[radius=0.5in];
\draw (0.75in,0) circle[radius=0.5in];
\draw (0.375in,0.6in) circle[radius=0.5in];
\draw (-.65in,-.65in) rectangle (1.4in,1.25in);
\node at (-.1in,-.1in) {${\color{red}p},\,q,\,r$};
\node at (0.85in,-.1in) {$p,\,{\color{red}q},\,r$};
\node at (0.375in,0.7in) {$p,\,q,\,{\color{red}r}$};
\node at (-.3in,1in) {$p,\,q,\,r$};
\node[pin={above right:$p,\,{\color{red}q,\,r}$}] at (.7in,.3in) {};
\node[pin={above left:${\color{red}p},\,q,\,{\color{red}r}$}] at (.05in,.3in) {};
\node[pin={below:${\color{red}p,\,q},\,r$}] at (.375in,-.2in) {};
\node[a] at (.3in,.2in) {};
\end{tikzpicture}
\end{center}
\textsc{Notice} that the Venn diagram and the truth table for this statement are equivalent. The regions that are shaded in the diagram correspond to the rows of the truth table where the statement is true, and the regions that are not shaded correspond to the rows where the statement is false.

\subsection*{{\normalsize Let $d$ represent ``David likes baseball."\\
Let $j$ represent ``Jaime likes gymnastics."\\
Let $a$ represent ``Anna likes hockey."}\\
Q: {\em Translate each of the following propositions into English.}}
\subsubsection*{a) $\neg d\wedge j\wedge a$}
We start with the most specific statement: $\neg d$ is the negation of ``David likes baseball", which we express in English as ``David does not like baseball".\\[1ex]
Then, $\neg d\wedge j\wedge a$ is the 3-way conjunction (``and") of ``David does not like baseball", ``Jaime likes gymnastics", and ``Anna likes hockey".\\[1ex]
Thus, in English, our statement is ``David does not like baseball and Jaime likes gymnastics and Anna likes hockey".
We might also express this as ``David does not like baseball \textit{but} Jaime likes gymnastics and Anna likes hockey", as well as a number of other sentence structures.

\subsubsection*{b) $\neg (d\wedge j)$}
Again, we start with the most specific statement: $d\wedge j$ is the conjunction of ``David likes baseball" and ``Jaime likes gymnastics", which we express as ``David likes baseball and Jaime likes gymnastics".\\[1ex]
Then, $\neg (d\wedge j)$ is the negation of ``David likes baseball and Jaime likes gymnastics".\\[1ex]
Thus, in English, our statement is ``It is not the case that David likes baseball and Jaime likes gymnastics".
We might express this more naturally as ``David does not like baseball or Jaime does not like gymnastics" (which is really the English translation of an equivalent but different logic statement).

\subsubsection*{c) $\neg d\vee\neg a$}
As above, we translate $\neg d$ to ``David does not like baseball", and similarly, $\neg a$ becomes ``Anna does not like hockey".\\[1ex]
Then, $\neg d\vee\neg a$ is the disjunction (``or") of ``David does not like baseball" and ``Anna does not like hockey".\\[1ex]
Thus, in English, our statement is ``David does not like baseball or Anna does not like hockey".

\subsubsection*{d) $(d\wedge a)\vee j$}
We start with the most specific statement: $d\wedge a$ is the conjunction of ``David likes baseball" and ``Anna likes hockey", which we express in English as ``David likes baseball and Anna likes hockey".\\[1ex]
Then, $(d\wedge a)\vee j$ is the disjunction of ``David likes baseball and Anna likes hockey", and ``Jaime likes gymnastics".\\[1ex]
Thus, in English, our statement is ``David likes baseball and Anna likes hockey, or Jaime likes gymnastics".\\
The comma separating ``David likes [\ldots]" and ``or Jaime likes gymnastics" is meant to indicate that these are two separate clauses of the sentence, so that it is not interpreted as a conjunction of ``David likes baseball" and ``Anna likes hockey, or Jaime likes gymnastics".

\subsection*{Q: {\em Translate each of the following English sentences into formal logic.}}
\subsubsection*{a) ``Jaime doesn't like gymnastics but David likes baseball."}

We start with the most general statement: from the language ``\ldots but\ldots", we know that the statement is a conjunction (``and").\\[1ex]
Then the first part of the conjunction ``Jaime doesn't like gymnastics" is the negation of ``Jaime likes gynmastics".\\[1ex]
Thus, in formal logic, our statement is $\neg j\wedge d$.

\subsubsection*{b) ``Either David likes baseball or Anna likes hockey, and Jaime likes gymnastics."}

Again, we start with the most general statement: from the language ``\ldots and\ldots", we know that the statement is a conjunction.\\[1ex]
Then the first part of the conjunction ``Either David likes baseball or Anna likes hockey" is the disjunction (``or") of ``David likes baseball" and ``Anna likes hockey".\\
However, we must be careful: in English, the use of the word ``either" implies exclusion. The ``$x$ or $y$" we have previously seen is true if $x$ or $y$ or \textit{both} are true (known as ``inclusive or"); this ``either $x$ or $y$" is true if $x$ or $y$ are true, but \textit{not} if both are true.\\
We can express this more clearly as ``David likes baseball and Anna doesn't like hockey, or David doesn't like baseball and Anna likes hockey".\\[1ex]
Thus, in formal logic, our statement is $((d\wedge\neg a)\vee(\neg d\wedge a))\wedge j$.\\[1ex]
Alternatively, because of the ambiguity of the English language, we may consider this statement to be a disjunction of ``David likes baseball" and ``Anna likes hockey, and Jaime likes gymnastics". The comma separating ``Either David likes baseball or Anna likes hockey" and `` and Jaime likes gymnastics" suggests that this is not the case, but it is possible.\\
Then our statement would be $(d\wedge\neg(a\wedge j))\vee(\neg d\wedge(a\wedge j))$.

\subsubsection*{c) ``If Anna likes hockey then David likes baseball."}

From the language ``if\ldots then\ldots", we know that the statement is an implication, with ``Anna likes hockey" as the antecedent (first half) and ``David likes baseball" as the consequent (second half).\\[1ex]
Thus, in formal logic, our statement is $a\to d$.

\section{\sc Predicate Logic}
So far, we have discussed something that we have referred to as ``formal logic." However, we have only studied one branch of formal logic, which we will now properly call \textsc{sentential logic} (also known as ``propositional logic").\\[1ex]
Here, we introduce another branch of formal logic: \textsc{predicate logic}.\\[1em]
Predicate logic addresses some of the limitations of sentential logic:
\begin{itemize}
\item Sentential logic cannot express quantitities/categories, eg. ``\textit{Every} person has a mother," ``\textit{Some} horses are white"
\item Sentential logic cannot express relationships between properties, eg. ``If X is married to Y, then Y is married to X"
\end{itemize}

Predicate logic uses \textsc{predicates} instead of statements/propositions. Predicates are sentences containing variables which can be assigned values. For example, ``$x>5$" is a predicate, where $x$ is the variable.\\[1ex]
Predicates are \textit{not} statements themselves because they have no inherent truth value.\\
\textsc{Consider}: can we say definitively that ``$x>5$" is true or false?\\[1ex]
However, once the variable(s) in the predicate have been assigned values, then the sentence becomes a statement. For example, if we let $x=3$, then ``$x>5$" is a false statement, and if we let $x=6$, then ``$x>5$" is a true statement.

\subsection{\em Syntax}

The language of sentential logic is composed of: sentence symbols, operators/connectives, and parentheses.\\
Since predicate logic uses predicates rather than statements, it uses predicate symbols rather than sentence symbols.\\ Predicate symbols typically follow mathematical function notation, eg. $\overbrace{P(\underbrace{x}_{\text{variable}})}^{\text{predicate}}$.\\
The name of the predicate is typically a single uppercase letter, and the variable is denoted as a single lowercase letter.\\[1ex]
\textsc{Note} that, as with a mathematical function, the letter chosen to represent the variable is simply a placeholder. $P(x)$ is identical to $P(y),\,P(z)$, etc.\\[1em]
We can then use the same connectives as in sentential logic to combine predicates and create compound predicates, eg.
\begin{itemize}
\item $P(x):\,2x=6$
	\begin{itemize}
	\item $P(3)$ is true
	\item $P(10)$ is false
	\end{itemize}
\item $Q(x,y):\,``x\text{ is larger than }y"$
	\begin{itemize}
	\item $Q(\text{Earth},\text{moon})$ is true
	\item $Q(\text{apple},\text{car})$ is false
	\end{itemize}
\item $R(x):\,x<10$
\item $P(x)\wedge R(x)\equiv ``x\text{ is between 5 and 10}"$
	\begin{itemize}
	\item $P(6)\wedge R(6)$ is true
	\item $P(14)\wedge R(14)$ is false
	\end{itemize}
\end{itemize}
\textsc{Note} that, although $x$ is a placeholder, $x$ represents the same value everywhere it is used within a predicate. For example, if we create the compound predicate $P(x)\wedge R(x)$, we cannot assign values such that we have $P(6)\wedge R(14)$.\\[1ex]
Conversely, different variables may represent different values. For example, if we create the compound predicate $P(x)\wedge R(y)$, we \textit{can} assign $x=6$ and $y=14$ such that we have $P(6)\wedge R(14)$.
\begin{itemize}
\item $S(x):\,x<20$
\item $\neg S(x)\equiv ``x$ is greater than or equal to 20"
\item $R(x)\to S(x)\equiv$ ``if $x$ is less than 10, then it is also less than 20"
	\begin{itemize}
	\item $R(3)\to S(3)$ is true
	\item $R(25)\to S(25)$ is (vacuously) true
	\end{itemize}
\end{itemize}
Predicate logic also adds operators called \textsc{quantifiers}, which allow us to describe ranges of values for variables, rather than assigning them individual values.\\[1em]
The \textsc{existential} quantifier is denoted $\exists$ and means ``(there) exists."\\[1ex]
For example, if $P(x):\,``x^2=5"$, then $\exists x(P(x))$ means ``There exists (at least) one value of $x$ for which $x^2=5$". The statement $\exists x(P(x))$ is true, since $P(\sqrt{5})$ and $P(-\sqrt{5})$ are both true.\\[1ex]
If $Q(x):\,``x^2=-5"$, then $\exists x(Q(x))$ means ``There exists (at least) one value of $x$ for which $x^2=-5$", which is false (assuming that $x$ is restricted to the set of reals).\\[1em]
The \textsc{universal} quantifier is denoted $\forall$ and means ``for all/every."\\[1ex]
For example, if $P(x):\,``x^2=5"$, then $\forall x(P(x))$ means ``For every possible value of $x$, $x^2=5$". The statement $\forall x(P(x))$ is false, since only $P(\sqrt{5})$ and $P(-\sqrt{5})$ are true, and $P(x)$ is false for any other value of $x$.\\[1ex]
If $R(x):\,x>5$ and $S(x):\,x>7$, then $\forall x(S(x)\to R(x))$ means ``For every possible value of $x$, if $x$ is greater than 7, $x$ is also greater than 5". We know that this statement is true, since $5<7$ and $7<x$.\\[1em]
As with sentential logic, we may use parentheses to group predicates and operators. When parentheses are omitted, the operators are applied according to the following precedence rules:
\begin{center}
\begin{tikzpicture}
\draw[<->] (0,0) node[left,align=center,font=\small] {highest\\precedence}
-- (.5,0) node[above] {$\neg$}
-- (1.5,0) node[above] {$\wedge$}
-- (2.5,0) node[above] {$\vee$}
-- (3.5,0) node[align=center] {$\exists$\\$\forall$}
-- (4.5,0) node[align=center] {$\to$\\$\leftrightarrow$}
-- (5,0) node[align=center,right,font=\small] {lowest\\precedence};
\end{tikzpicture}
\end{center}

\subsection{\em Ordering of quantifiers}

When we have a statement containing multiple, different quantifiers, such as $\forall x\exists y(P(x,y))$, the quantified variables are read left to right. For example, if $P(x,y):\,x\cdot y=5$, then $\forall x\exists y(P(x,y))$ means ``For every number $x$, there exists a number $y$ such that $x\cdot y=5$".\\[1ex]
\textsc{Does the} meaning of the sentence change if we reorder the quantifiers?\\
It depends upon the predicate.\\[1ex]
For example, if $P(x,y):\,x>y$, then $\forall x\exists y(P(x,y))$ means ``For every number $x$, there exists a number $y$ such that $x$ is greater than $y$".\\
We know that this statement is true because the set of integers (or reals, depending upon the allowed values of $x$) is infinite. Thus, by definition, there is always a number larger than any number we choose from the set.\\[1ex]
But $\exists y\forall x(P(x,y))$ means ``There exists a number $y$ such that, for every number $x$, $x$ is greater than $y$".\\
We know that this statement is false because it is saying that there is some minimum integer (since every other integer is larger than it). Since the integers are infinite, this is impossible.\\[1ex]
Here, the order of the quantifiers is significant.

\subsection{\em Negation of quantifiers}

In addition to negating predicates using the negation operator, we want to be able to negate quantifiers - that is, we want to be able to say ``There does \textit{not} exist\ldots" and ``\textit{It is not the case that} for all\ldots".\\[1ex]
For some predicate $P(x)$, ``There does \textit{not} exist an $x$ such that $P(x)$ is true" is equivalent to saying ``$P(x)$ is false for every value of $x$", which we can write formally as $\forall x(\neg P(x))$.\\[1ex]
For some predicate $P(x)$, ``\textit{It is not the case that}, for every value of $x$, $P(x)$ is true" is equivalent to saying ``There is some value of $x$ for which $P(x)$ is false", which we can write formally as $\exists x(\neg P(x))$.\\[1ex]
Thus, the negation of $\exists x(P(x))$ is $\forall x(\neg P(x))$ and the negation of $\forall x(P(x))$ is $\exists x(\neg P(x))$.\\[1em]
For example, if $P(x)$: ``$x$ is tall", where $x$ is a person, then
\begin{itemize}
\item $\exists x(P(x))$ means ``There exists a person who is tall"
\item $\forall x(\neg(P(x))$ means ``Every person is not tall", or ``There does not exist a person who is tall"
\item $\forall x(P(x))$ means ``All people are tall"
\item $\exists x(\neg P(x))$ means ``There exists a person who is not tall", or ``Not every person is tall"
\end{itemize}

\section{\sc Additional practice problems}
Let $P(x,y)$ be the predicate ``$x\cdot y=12$", where $x,y$ are integers.\\
{\bf Q: Which of the following statements is true?}
\begin{itemize}
\item $P(3,4)$
\item $P(3,5)$
\item $P(2,6)\vee P(3,7)$
\item $\forall x,\forall y(P(x,y)\to P(y,x))$
\item $\forall x,\exists y(P(x,y))$
\end{itemize}
\vspace{1ex}Given the predicates\\
$L(x)=$``$x$ is a lion."\\
$F(x)=$``$x$ is fuzzy."\\
where $x$ is a mammal,\\
{\bf Q: Translate ``All lions are fuzzy" into predicate logic.}\\[1ex]
{\bf Q: Translate ``Some lions are fuzzy" into predicate logic.}\\[1em]
Consider the three predicates\\
$P(x)$ symbolizes the statement ``$x$ is a prime number"\\
$E(x)$ symbolizes the statement ``$x$ is even"\\
$D(x, y)$ symbolizes the statement ``$x$ evenly divides $y$"\\
where $x$ and $y$ represent integers.\\
{\bf Q: Find some values for the variables that make the following logical formulas true, and others making them false.}
\begin{itemize}
\item $P(x) \wedge E(x)$
\item $E(x) \vee D(x, y)$
\item $\neg P(x) \wedge D(x, y)$
\item $D(x, y) \to\neg P(x)$
\end{itemize}
\vspace{1ex}\textit{Tarski's World} is a computer program that is meant to be an introduction to predicate logic.\\
You can build two-dimensional worlds of shapes, describe them using predicates, and test whether your predicates are true or false.\\
Experiment with Tarski's World using the following implementation: \href{http://courses.cs.washington.edu/courses/cse590d/03sp/tarski/tarski.html}{Tarski's World (Java Applet)}

\end{document}


\vsssub
\subsubsection{~$S_{in} + S_{ds}$: \wam\ cycle 4 (ECWAM)} \label{sec:ST3}
\vsssub

\opthead{ST3}{\wam\ model}{F. Ardhuin}

\noindent
The wind-wave interaction source terms described here are based on the wave
growth theory of \cite{art:Miles57}, modified by \cite{art:Jan82}. The
pressure-slope correlations that give rise to part of the wave generation are
parameterized following \cite{art:Jan91}.
%A wave dissipation term due to shear
%stresses variations in phase with the orbital velocity is added for the swell
%part of the spectrum, based on the swell decay observations of
%\cite{art:ACC09}.

This parameterization was further extended by \cite{rep:AB02} to take into
account a stronger gustiness in unstable atmospheric conditions. This effect is
included in the present parameterization and is activated with the optional
{\code STAB3} switch. The formula used in {\code STAB3} is not described herein.
For that, the reader is referred to \cite{rep:AB02}. If {\code STAB3} is used, the air-sea
temperature differences should be provided by the user, e.g. using {\file ww3\_prep}.

Efforts have been made to make the present implementation as
close as possible to the one in the ECWAM model \citep{rep:Bea05}, in
particular the stress lookup tables were verified to be identical.
Later modifications include the addition of a negative part in the wind input
to represent swell dissipation.

The source term reads \citep{bk:Jan04}
\begin{equation}
\cS_{in}(k,\theta) =
\frac{\rho_a}{\rho_w}\frac{\beta_{\mathrm{max}}}{\kappa^2}{\mathrm e}^{Z}Z^4
\left(\frac{u_\star}{C}+z_\alpha\right)^2 \cos^{p_{in}}(\theta - \theta_u) \sigma N
\left(k,\theta\right) + S_{out}(k,\theta),\label{eq:SinWAM4}
\end{equation}

\noindent where $\rho_a$ and $\rho_w$ are the air and water densities,
$\beta_{\mathrm{max}}$ is a non-dimensional growth parameter (constant),
$\kappa$ is von K\'{a}rm\'{a}n' constant, and $p_{in}$ is a constant that
controls the directional distribution of $\cS_{in}$. In the present
implementation the air/water density ratio ${\rho_a}/{\rho_w}$ is constant. We
define $Z=\log(\mu)$ where $\mu$ is given by \cite{art:Jan91}  Eq.~(16), and
corrected for intermediate water depths, so that

\begin{equation}
Z=\log(k z_1)+\kappa/\left[\cos\left(\theta - \theta_u\right)
\left(u_\star/C + z_\alpha \right)\right],
\end{equation}

\noindent
where $z_1$ is a roughness length modified by the wave-supported stress
$\tau_w$, and $z_\alpha$ is a wave age tuning parameter\footnote{Although this
tuning parameter $z_\alpha$ is not well described in WAM-Cycle4 documentation,
it has an important effect on wave growth. Essentially it shifts the wave age
of the long waves, which typically increases the growth, and even generates
waves that travel faster than the wind. This accounts for some gustiness in
the wind and should possibly be resolution-dependent. For reference, this
parameter was not properly set in early versions of the SWAN model, as
discovered by R. Lalbeharry.}.  The roughness $z_1$ is defined as,

\begin{eqnarray}
U_{10}&=&\frac{u_\star}{\kappa} \log\left(\frac{z_u}{z_1}\right) \\
z_1&=&\alpha_0 \frac{\tau}{ \sqrt{1-\tau_w/\tau}},
\end{eqnarray}

\noindent
where $\tau=u_\star^2$, and $z_u$ is the height at which the wind is
specified. These two equations provide an implicit functional dependence of
$u_\star$ on $U_{10}$ and $\tau_w/\tau$. This relationship is then tabulated
\citep{art:Jan91, rep:Bea07}.

An important part of the parameterization is the calculation of the
wave-supported stress $\tau_w$,

\begin{equation}
\tau_w=\left|\int_0^{k_{\max}} \int_0^{2 \pi} \frac{\cS_{in}(k',\theta)}{C}
\left(\cos \theta, \sin \theta \right)  {\mathrm d} k' \mathrm d \theta +
\tau_{\mathrm{hf}}(u_\star,\alpha) \left(\cos \theta_u, \sin \theta_u \right)
\right|,\label{eq:tauwint}
\end{equation}

\noindent
which includes the resolved part of the spectrum, up to $k_{\max}$, as well as
the stress supported by shorter waves, $\tau_{\mathrm{hf}}$. Assuming a
$f^{-X}$ diagnostic tail beyond the highest frequency, $\tau_{\mathrm{hf}}$ is
given by

\begin{eqnarray}
\tau_{\mathrm{hf}}(u_\star,\alpha)&= &\frac{u_{\star}^2}{g^2}
\frac{\sigma_{\max}^X 2 \pi \sigma }{2 \pi C_g(k_{\max})} \int_0^{2 \pi} N
\left(k_{\max},\theta \right)
\max\left\{0,\cos\left(\theta-\theta_u\right)\right\}^3 d \theta \nonumber \\
& & \times \frac{\beta_{\mathrm{max}}}{\kappa^2}
\int_{\sigma_{\max}}^{0.05*g/u_\star} \frac{{\mathrm
e}^{Z_{\mathrm{hf}}}Z_{\mathrm{hf}}^4}{\sigma^{X-4}} {\mathrm d} \sigma
\label{eq:tauhfint}
\end{eqnarray}

\noindent
where the second integral is a function of $u_\star$ and the Charnock
coefficient $\alpha$ only, which is easily tabulated. In practice the
calculation is coded with $X=5$, and the variable $Z_{\mathrm{hf}}$ is defined
by,

\begin{equation}
Z_{\mathrm{hf}}(\sigma)=\log(k z_1)+\min\left\{\kappa/\left(u_\star/C +
z_\alpha \right),20\right\}.
\end{equation}

\noindent
This parameterization is sensitive to the spectral level at $k_{\max}$.
A higher spectral level will lead to a larger value of $u_\star$ and thus
positive feedback on the wind input via $z_1$. This sensitivity is exacerbated
by the sensitivity of the high-frequency spectral level to the presence of
swell via the dissipation term.

\begin{table}[htb]
\begin{center}
\begin{tabular}{|l|c|c|c|c|c|} \hline \hline
Par.         &  WWATCH var.           & namelist & WAM4 & BJA   & Bidlot 2012 \\
\hline
  $z_u$ &  ZWND                       & SIN3 & 10.0    & 10.0   & 10.0   \\
  $\alpha_0$ &  ALPHA0                & SIN3 & 0.01    & 0.0095 &  0.0095 \\
  $\beta_{\mathrm{max}}$ & BETAMAX    & SIN3 & 1.2     & 1.2    & 1.2  \\
  $p_{\mathrm{in}}$ &  SINTHP         & SIN3 & 2       & 2      & 2  \\
  $z_\alpha$ &  ZALP                  & SIN3 & 0.0110  & 0.0110 &  0.0080 \\
  $s_1$ &  SWELLF                     & SIN3 & 0.0     & 0.0    & 1.0   \\
\hline
\end{tabular} \end{center}
\caption{Parameter values for WAM4, BJA and the 2012 update in the ECWAM model. Source term
  parameterizations that can be reset via the {\F SIN3} and {\F SDS3} namelist. BJA is
  generally better than WAM4. The default parameters in ST3 corresponds to BJA. Please
  note that the names of the variables only apply to the namelists. In the source
  term module the names are slightly different, with a doubled first letter, in
  order to differentiate the variables from the pointers to these variables.} \label{tab:WAM4_parSIN}
\botline
\end{table}

\begin{table}[htb]
\begin{center}
\begin{tabular}{|l|c|c|c|c|c|} \hline \hline
Par.                               &  WWATCH var.         & namelist & WAM4 & BJA   & Bidlot 2012 \\
\hline
  $C_{\mathrm{ds}}$                 &  SDSC1          & SDS3 & -4.5 & -2.1& -1.33       \\
  $p$                               &  WNMEANP        & SDS3 & -0.5 & 0.5 &  0.5        \\
  $p_{\mathrm{tail}}$               &  WNMEANPTAIL    & SDS3 & -0.5 & 0.5 &  0.5        \\
  $\delta_1$                        &  SDSDELTA1      & SDS3 & 0.5  & 0.4 &  0.5        \\
  $\delta_2$                        &  SDSDELTA2      & SDS3 & 0.5  & 0.6 &  0.5 \\
  \hline \hline
\end{tabular} \end{center}
\caption{Parameter values for WAM4, BJA and the update by \cite{pro:Bid12}. Source term
parameterizations that can be reset via the {\F SDS3} namelist. BJA is generally
better than WAM4. Please note that the
names of the variables only apply to the namelists. In the source term module
the names are slightly different, with a doubled first letter, in order to
differentiate the variables from the pointers to these variables.} \label{tab:WAM4_parSDS}
\botline
\end{table}

A linear damping of swells was introduced in the operational ECWAM model in September 2009. It takes
the form given by \cite{bk:Jan04}

\begin{equation}
S_{out}(k,\theta)= 2 s_1  \kappa \frac{\rho_a }{\rho_w} \left(\frac{u_\star}{C}\right)^2
\left[\cos \left(\theta - \theta_u\right) - \frac{\kappa C}{u_\star \log(k z_0)}\right]
\end{equation}

\noindent where $s_1$ is set to 1 when this damping is used and 0 otherwise. For $s_1=0$
the parameterization is the WAM4 or BJA parameterization (see Table \ref{tab:WAM4_parSIN}).

Due to the increase in high-frequency input compared to WAM3, the dissipation
function was adapted by Janssen (1994) from the WAM3 dissipation, and later
reshaped by \cite{rep:Bea05}. That later modification is referred to as "BJA" for Bidlot,
Janssen and Abdallah. A more recent modification, strongly improved the model results for
Pacific swells, at the price of an underestimation of the highest sea states. This
corresponds to the ECMWF WAM model contained in the IFS version CY38R1 \citep{pro:Bid12}. Note that these parameters were optimized for use of neutral winds from the
operational ECMWF analysis. Using these with other wind products may require a re-tuning of these coefficients. For example, with NCEP or CFSRR winds, the value of BETAMAX
should probably be reduced or ZWND increased.

The generic form of the WAM4 dissipation term is,

\begin{equation}
S_{ds}\left(k,\theta\right)^{\mathrm{WAM}} = C_{ds} \overline{\alpha}^2
 \overline{\sigma} \left[\delta_1 \frac{k}{\overline{k}} + \delta_2
\left(\frac{k}{\overline{k}}\right)^2\right]\label{eq:SdsWAM4}
N\left(k,\theta\right)
\end{equation}

\noindent
where $C_{ds}$ is a non-dimensional constant $\delta_1$ and $\delta_2$ are
weight parameters,

\begin{equation}
\overline{k}=\left[\frac{\int k^p N\left(k,\theta\right) {\mathrm d}
\theta}{\int N\left(k,\theta\right) {\mathrm d} \theta}\right]^{1/p}
\end{equation}

\noindent
with $p$ a constant power. Similarly, the mean frequency is defined as

\begin{equation}
\overline{\sigma}=\left[\frac{\int \sigma^p N\left(k,\theta\right) {\mathrm d}
\theta}{ \int N\left(k,\theta\right) {\mathrm d} \theta}\right]^{1/p},
\end{equation}

\noindent
so that the mean steepness is $\overline{\alpha}=E \overline{k}^2$.

The mean frequency also occurs in the definition of the maximum frequency of
prognostic integration of the source terms. Since the definition of that
frequency may be different from that of the source term it is defined with
another exponent $p_{\mathrm{tail}}$.

Unfortunately these parameterizations are sensitive to swell. An increase in
swell height typically reduces dissipation at the windsea peak because the mean wavenumber $\overline{k}$ and
thus the mean steepness $\overline{\alpha}$ are reduced. For $p< 2$, as in the WAM-Cycle 4 and BJA
parameterizations, this sensitivity is much larger and opposite to the expected effect of
short wave modulation by long waves.

The source term code was generalized to
allow the use of WAM4, BJA or others ECWAM parameterization, via a simple change of
the parameters in the namelists {\F SIN3} and {\F SDS3}, see Tables \ref{tab:WAM4_parSIN} and \ref{tab:WAM4_parSDS}. At present, the default values of the namelist
parameters correspond to BJA \citep{rep:Bea05}.


\documentclass{report}
\usepackage{amsfonts, amsmath, amssymb, hyperref}
\renewcommand{\chaptername}{}
\setlength{\parindent}{0pt}

\title{Solutions to exercises in SICP (2e)}
\author{Paul Tan (\href{http://pauljxtan.com}{website}, \href{https://github.com/pauljxtan}{github})}

\begin{document}

\maketitle
\tableofcontents

\chapter{Exercise 1.10}

This is a pretty fun exercise in rendering Scheme procedures in mathematical notation. Personally, I find it helps with visualizing the recursive process. \\

Ackermann's function is defined in the question as

\begin{equation}
  A(x, y) = \begin{cases}
    0 & \text{if } y = 0 \\
    2y & \text{if } x = 0 \\
    2 & \text{if } y = 1 \\
    A(x - 1, A(x, y - 1)) & \text{otherwise}
  \end{cases}
\end{equation}

For each of the given expressions, we have

\begin{align*}
  A(1, 10) &= A(0, A(1, 9)) \\
           &= A(0, A(0, A(1, 8))) \\
           &= A(0, A(0, A(0, A(1, 7)))) \\
           &= A(0, A(0, A(0, A(0, A(1, 6))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(1, 5)))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(1, 4))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 3)))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 2))))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(1, 1)))))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 2))))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(0, A(0, 2\cdot2)))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, A(0, 2\cdot2\cdot2))))))) \\
           &= A(0, A(0, A(0, A(0, A(0, A(0, 2\cdot2\cdot2\cdot2)))))) \\
           &= A(0, A(0, A(0, A(0, A(0, 2\cdot2\cdot2\cdot2\cdot2))))) \\
           &= A(0, A(0, A(0, A(0, 2\cdot2\cdot2\cdot2\cdot2\cdot2)))) \\
           &= A(0, A(0, A(0, 2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2))) \\
           &= A(0, A(0, 2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2)) \\
           &= A(0, 2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2) \\
           &= 2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2\cdot2 \\
           &= 2^{10}
\end{align*}

In general, it appears that for $n > 0$ we have

\begin{equation}
  A(1, n) = 2^n
\end{equation}

(Recall that $A(1, 0) = 0$ by definition.) \\

Next expression:

\begin{align*}
  A(2, 4) &= A(1, A(2, 3)) \\
          &= A(1, A(1, A(2, 2))) \\
          &= A(1, A(1, A(1, A(2, 1)))) \\
          &= A(1, A(1, A(1, 2))) \\
          &= A(1, A(1, 2^2)) \quad &\text{using the result found above} \\
          &= A(1, 2^{2^2}) \quad &\text{ditto} \\
          &= 2^{2^{2^2}} \quad &\text{and again} \\
          &= 2^{2^4} \\
          &= 2^{16} \\
          &= 65536
\end{align*}

And one more:

\begin{align*}
  A(3, 3) &= A(2, A(3, 2)) \\
          &= A(2, A(2, A(3, 1))) \\
          &= A(2, A(2, 2)) \\
          &= A(2, A(1, A(2, 1)) \\
          &= A(2, A(1, 2)) \\
          &= A(2, A(0, A(1, 1))) \\
          &= A(2, A(0, 2)) \\
          &= A(2, 4) \\
          &= A(1, A(2, 3)) \\
          &= A(1, A(1, A(2, 2))) \\
          &= A(1, A(1, A(1, A(2, 1)))) \\
          &= A(1, A(1, A(1, 2))) \\
          &= A(1, A(1, A(0, A(1, 1)))) \\
          &= A(1, A(1, A(0, 2))) \\
          &= A(1, A(1, 4)) & \text{familiar pattern emerging\dots} \\
          &= A(1, A(0, A(1, 3))) \\
          &= A(1, A(0, A(0, A(1, 2)))) \\
          &= A(1, A(0, A(0, A(0, A(1, 1))))) \\
          &= A(1, A(0, A(0, A(0, 2)))) \\
          &= A(1, A(0, A(0, 4))) \\
          &= A(1, A(0, 8)) \\
          &= A(1, 16) \\
          &= 2^{16} \\
          &= 65536 \\
\end{align*}

(Gotta say, the way the equations ``fan'' in and out like that is pretty neat. First it fans out 3 times, then 2, then just once on the final expansion.) \\

Just to check, I computed these values in \textbf{sicp\_1-10\_check.scm} and they are indeed correct. Phew! \\

The last part is essentially the same idea, but with a variable parameter $n$. Let's see\dots

\begin{align*}
  f(n) &= A(0, n) \\
       &= 2n
\end{align*}

\begin{align*}
  g(n) &= A(1, n) \\
       &= A(0, A(1, n - 1)) \\
       &= 2 \cdot A(1, n - 1) \\
       &= 2 \cdot A(0, A(1, n - 2)) \\
       &= 2 \cdot 2 \cdot A(1, n - 2) \\
       &= \dots & \text{follow the pattern\dots} \\
       &= 2^{n - 1} \cdot A(1, 1) \\
       &= 2^{n - 1} \cdot 2 \\
       &= 2^n & \text{as found earlier!}
\end{align*}

\begin{align*}
  h(n) &= A(2, n) \\
       &= A(1, A(2, n - 1)) \\
       &= A(1, A(1, A(2, n - 2))) \\
       &= A(1, A(1, A(1, A(2, n - 3)))) \\
\end{align*}

At this point, note that the levels of nesting is equal to the number $m$ in $n - m$ at the end\dots

\begin{align*}
  \Rightarrow h(n)
       &= A(1, A(1, A(1, \dots A(1, A(2, 1)) \dots ))) \\
       &= A(1, A(1, A(1, \dots A(1, 2) \dots ))) & \text{We know what $A(1, n)$ is from earlier!} \\
       &= A(1, A(1, A(1, \dots 2^2 \dots ))) & \text{Now we "fold" it back in $n - 1$ times\dots} \\
       &= 2^{2^{2^{\dots}}} & \text{with $n$ "2"s} \\
\end{align*}

That is,

\begin{align*}
  A(2, 1) &= 2 \\
  A(2, 2) &= 2^2 = 4 \\
  A(2, 3) &= 2^{2^2} = 16 \\
  A(2, 4) &= 2^{2^{2^2}} = 65536 \quad \text{(as we found earlier)}
\end{align*}

and so on. To be honest, I have no idea how to formulate this as a ``concise mathematical definition''. Any suggestions welcome\dots

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{bussproofs}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage[a4paper, total={7in, 10in}]{geometry}

\newenvironment{scprooftree}[1]%
  {\gdef\scalefactor{#1}\begin{center}\proofSkipAmount \leavevmode}%
  {\scalebox{\scalefactor}{\DisplayProof}\proofSkipAmount \end{center} }

\begin{document}

{\Huge Natural Deduction on Predicate Logic} \\

All this work is based on the book \textit{Logic and Structure} by Dirk Van Dalen.

\section*{Strategy to build correct derivations in Predicate Logic}

\noindent\fbox{%
    \parbox{\textwidth}{%
\textbf{From an university test:} \\ \\

Let $\Gamma = \{(\exists x)P_1(x), (\exists x)(\exists y)(\neg P_2 (x, y)), (\forall x)(P_1 (x) \rightarrow (\exists y)P_2 (x,y)) \}$ \\

Build a derivation that proves the following: \\
$$\Gamma \cup \{ (\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y)) \} \vdash (\exists x)(\exists y)(\neg P_3 (x, y))$$
    }%
}
\newline
\newline\newline
\noindent\fbox{%
    \parbox{\textwidth}{%
First of all build the derivation without canceling any hypotheses:\\
    }%
}
\begin{scprooftree}{0.8}
  \AxiomC{$(\exists x)(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$\neg P_2 (x, y)$}
  \AxiomC{$(\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *6}$}
  \UnaryInfC{$(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *5}$}
  \UnaryInfC{$P_3 (x,y) \leftrightarrow P_2 (x,y)$}
  \AxiomC{$P_3 (x, y)$}
  \RightLabel{$E_{\leftrightarrow 1}$}
  \BinaryInfC{$P_2 (x, y)$}
  \RightLabel{$E_{\neg}$}
  \BinaryInfC{$\bot$}
  \LeftLabel{(3)}
  \RightLabel{$I_{\neg}$}
  \UnaryInfC{$\neg P_3 (x, y)$}
  \RightLabel{$I_{\exists *4}$}
  \UnaryInfC{$(\exists y)(\neg P_3 (x, y))$}
  \RightLabel{$I_{\exists *3}$}
  \UnaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(2)}
  \RightLabel{$E_{\exists *2}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(1)}
  \RightLabel{$E_{\exists *1}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
\end{scprooftree}

During the construction the following hypotheses were generated:
\begin{enumerate}
\item $(\exists y)(\neg P_2 (x, y))$
\item $\neg P_2 (x, y)$
\item $P_3 (x, y)$
\end{enumerate}

\noindent\fbox{%
    \parbox{\textwidth}{%
Next, start checking every atomic step top-down. In each atomic step, if the rule needs proofs we write them down. If the rule generated hypotheses we cancel them. If you can give a correct proof for all rules that need it, the derivation is correct. \\
    }%
}
\begin{itemize}
  \item [$E_{\forall *6}$]:
  \item This rule doesn't generate hypotheses.
  \item \textbf{*6 justification:} $x$ is free for $x$ in any formula (in particular, in: $(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$).
\end{itemize}

\begin{itemize}
  \item [$E_{\forall *5}$:]
  \item This rule doesn't generate hypotheses.
  \item \textbf{*5 justification:} $y$ is free for $y$ in any formula (in particular, in $(P_3 (x,y) \leftrightarrow P_2 (x,y))$).
\end{itemize}

\begin{itemize}
  \item [$E_{\leftrightarrow 1}$:]
  \item This rule doesn't generate hypotheses and doesn't need justification.
\end{itemize}

\begin{itemize}
  \item [$E_{\neg 1}$:]
  \item This rule doesn't generate hypotheses doesn't need justification.
\end{itemize}
\newpage

\begin{itemize}
  \item [$I_{\neg 1}$:]
  \item This rule generates the hypotheses ($P_3 (x, y)$), se let's cancel it.
  \item This rule doesn't need justification.\\
\end{itemize}

The derivation at this step is: \\

\begin{scprooftree}{0.8}
  \AxiomC{$(\exists x)(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$\neg P_2 (x, y)$}
  \AxiomC{$(\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *6}$}
  \UnaryInfC{$(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *5}$}
  \UnaryInfC{$P_3 (x,y) \leftrightarrow P_2 (x,y)$}
  \AxiomC{$[P_3 (x, y)]^3$}
  \RightLabel{$E_{\leftrightarrow 1}$}
  \BinaryInfC{$P_2 (x, y)$}
  \RightLabel{$E_{\neg}$}
  \BinaryInfC{$\bot$}
  \LeftLabel{(3)}
  \RightLabel{$I_{\neg}$}
  \UnaryInfC{$\neg P_3 (x, y)$}
  \RightLabel{$I_{\exists *4}$}
  \UnaryInfC{$(\exists y)(\neg P_3 (x, y))$}
  \RightLabel{$I_{\exists *3}$}
  \UnaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(2)}
  \RightLabel{$E_{\exists *2}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(1)}
  \RightLabel{$E_{\exists *1}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
\end{scprooftree}

\begin{itemize}
  \item [$I_{\exists *4}$:]
  \item This rule doesn't generate hypotheses.
  \item \textbf{*4 justification:} $y$ is free for $y $ in any formula (in particular, in $\neg P_3 (x,y)$).
\end{itemize}

\begin{itemize}
  \item [$I_{\exists *3}$:]
  \item This rule doesn't generate hypotheses.
  \item \textbf{*3 justification:} $x$ is free for $x$ in any formula (in particular, in $(\exists y)(\neg P_3 (x,y))$).
\end{itemize}

\begin{itemize}
  \item [$E_{\exists *2}$:]
  \item This rule generates the hypotheses $\neg P_2 (x, y)$, let's cancel it.
  \item \textbf{*2 justification:} $y \not\in$ $FV((\exists x)(\exists y)(\neg P_3 (x, y)))$, $y \not\in$ $FV((\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y)))$. \\
  \textbf{Note that there's no need to check if other hypotheses contain $y$ free: ($P_3 (x,y)$ ni en $\neg P_2 (x,y)$) as in this step they are canceled.}\\
\end{itemize}

The derivation at this step is: \\
\begin{scprooftree}{0.8}
  \AxiomC{$(\exists x)(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$[\neg P_2 (x, y)]^2$}
  \AxiomC{$(\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *6}$}
  \UnaryInfC{$(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *5}$}
  \UnaryInfC{$P_3 (x,y) \leftrightarrow P_2 (x,y)$}
  \AxiomC{$[P_3 (x, y)]^3$}
  \RightLabel{$E_{\leftrightarrow 1}$}
  \BinaryInfC{$P_2 (x, y)$}
  \RightLabel{$E_{\neg}$}
  \BinaryInfC{$\bot$}
  \LeftLabel{(3)}
  \RightLabel{$I_{\neg}$}
  \UnaryInfC{$\neg P_3 (x, y)$}
  \RightLabel{$I_{\exists *4}$}
  \UnaryInfC{$(\exists y)(\neg P_3 (x, y))$}
  \RightLabel{$I_{\exists *3}$}
  \UnaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(2)}
  \RightLabel{$E_{\exists *2}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(1)}
  \RightLabel{$E_{\exists *1}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
\end{scprooftree}

\newpage

\begin{itemize}
\item [$E_{\exists *1}$:]
\item This rule generates the hypotheses: $(\exists y)(\neg P_2 (x, y))$, let's cancel it.
\item \textbf{*1 justification:} $x \not\in$ $FV((\exists x)(\exists y)(\neg P_3 (x, y)))$, $x \not\in$ $FV((\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y)))$.\\
\textbf{Let's note that for this case also there's no need to check if any other hypotheses contain $x$ free: ($P_3 (x,y)$ ni en $\neg P_2 (x,y)$) because they are all canceled.}\\
\end{itemize}

The derivation at this step is: \\

\begin{scprooftree}{0.8}
  \AxiomC{$(\exists x)(\exists y)(\neg P_2 (x, y))$}
  \AxiomC{$[(\exists y)(\neg P_2 (x, y))]^1$}
  \AxiomC{$[\neg P_2 (x, y)]]^2$}
  \AxiomC{$(\forall x)(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *6}$}
  \UnaryInfC{$(\forall y)(P_3 (x,y) \leftrightarrow P_2 (x,y))$}
  \RightLabel{$E_{\forall *5}$}
  \UnaryInfC{$P_3 (x,y) \leftrightarrow P_2 (x,y)$}
  \AxiomC{$[P_3 (x, y)]^3$}
  \RightLabel{$E_{\leftrightarrow 1}$}
  \BinaryInfC{$P_2 (x, y)$}
  \RightLabel{$E_{\neg}$}
  \BinaryInfC{$\bot$}
  \LeftLabel{(3)}
  \RightLabel{$I_{\neg}$}
  \UnaryInfC{$\neg P_3 (x, y)$}
  \RightLabel{$I_{\exists *4}$}
  \UnaryInfC{$(\exists y)(\neg P_3 (x, y))$}
  \RightLabel{$I_{\exists *3}$}
  \UnaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(2)}
  \RightLabel{$E_{\exists *2}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
  \LeftLabel{(1)}
  \RightLabel{$E_{\exists *1}$}
  \BinaryInfC{$(\exists x)(\exists y)(\neg P_3 (x, y))$}
\end{scprooftree}

And this, together with the justifications is the complete finished derivation.

\newpage

\section{How to use identity rules.}

Special thanks to Prof. Luis Sierra (FIng UdelaR) for the corrections. \\

\noindent\fbox{%
    \parbox{\textwidth}{%
\textbf{From an university test:} \newline \newline
Build a derivation that proves:
$$(\forall x)P(x, f(x)), (\exists y)f(y) =' y \vdash (\exists z)P(f(z), z)$$
    }%
}
\newline \newline

First of all, looking at the premises we can figure out that to reach the conclusion, using substition in the application of the rule $E_{\forall}$ won't lead us anywhere. For example, if we wanted to reach $(\exists z)P(f(z), z)$ with the premise $(\forall x)P(x, f(x))$, we should apply the rule $E_{\forall}$. If we wanted to obtain $f(z)$ instead of $x$, when substituting, we would get $P(f(z), f(f(z)))$. The only way to swawp the arguments of $P$ is by using the rule $RI4'$. \\

Let's build the derivation: \\

All we can do with the premises we have is an existential elimination:

\begin{prooftree}
\AxiomC{$(\exists y)f(y) =' y$}
\AxiomC{$(\exists z)P(f(z), z)$}
\LeftLabel{(1)}
\RightLabel{$E_{\exists *1}$}
\BinaryInfC{$(\exists z)P(f(z), z)$}
\end{prooftree}

Here, we obtained the hypotheses $f(y) =' y$. Checking the premises again we can see that with $(\forall x)P(x, f(x))$, by applying the rule $E_{\forall}$ and substituting adequately we obtain $P(y, f(y))$. If we manage to substitute in this formula in a way that we get $P(f(y), y)$, applying the rule $I_{\exists}$ and substituting adequately we can obtain $(\exists z)P(f(z), z)$. We can do this by using the identity rule $RI4'$.
Doing this, the resulting derivation is:

\begin{prooftree}
  \AxiomC{$(\exists y)f(y) =' y$}
  \AxiomC{$(\forall x)P(f(x), x)$}
  \UnaryInfC{$P(f(y), y)$}
  \AxiomC{$f(y) =' y$}
  \AxiomC{$f(y) =' y$}
  \UnaryInfC{$y =' f(y)$}
  \TrinaryInfC{$P(f(y), y)$}
  \UnaryInfC{$(\exists z)P(f(z), z)$}
  \LeftLabel{(1)}
  \RightLabel{$E_{\exists *1}$}
  \BinaryInfC{$(\exists z)P(f(z), z)$}
\end{prooftree}

By adding the rules in each step and using the strategy of the previous example, we have built the derivation: \\

\noindent\fbox{%
    \parbox{\textwidth}{%
 \textbf{*4}: $y$ free for $x$ in $P(f(x), x).$ \\
 \textbf{*3}: $y$ free for $z_1$; $f(y)$ free for $z_2$ en $P(z_1,z_2)$. \\
 $f(y)$ free for $z_1$; $y$ free for $z_2$ en $P(z_1,z_2)$.\\
 \textbf{*2}: $y$ free for $z$ en $P(f(z), z)$.\\
 \textbf{*1}: $y$ $\not \in$ $FV(\{(\exists z)P(f(z), z), (\forall x)P(f(x), x)\}).$
    }%
}

\begin{prooftree}
  \AxiomC{$(\exists y)f(y) =' y$}
  \AxiomC{$[f(y) =' y]^1$}
  \RightLabel{$RI2$}
  \UnaryInfC{$y =' f(y)$}
  \AxiomC{$[f(y) =' y]^1$}
  \AxiomC{$(\forall x)P(x, f(x))$}
  \RightLabel{$E_{\forall *4}$}
  \UnaryInfC{$P(z_1,z_2)[y/z_1,f(y)/z_2])$}
  \RightLabel{$RI4'_{*3}$}
  \TrinaryInfC{$P(z_1,z_2)[f(y)/z_1,y/z_2)]$}
  \RightLabel{$I_{\exists *2}$}
  \UnaryInfC{$(\exists z)P(f(z), z)$}
  \LeftLabel{(1)}
  \RightLabel{$E_{\exists *1}$}
  \BinaryInfC{$(\exists z)P(f(z), z)$}
\end{prooftree}

\end{document}