\documentclass[twoside,11pt]{article}

% Any additional packages needed should be included after jmlr2e.
% Note that jmlr2e.sty includes epsfig, amssymb, natbib and graphicx,
% and defines many common macros, such as 'proof' and 'example'.
%
% It also sets the bibliographystyle to plainnat; for more information on
% natbib citation styles, see the natbib documentation, a copy of which
% is archived at http://www.jmlr.org/format/natbib.pdf

\usepackage{jmlr2e}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{bm}
\usepackage[noend]{algpseudocode}
\usepackage{wrapfig}
\usepackage{float}

% Definitions of handy macros can go here

\newcommand{\dataset}{{\cal D}}
\newcommand{\fracpartial}[2]{\frac{\partial #1}{\partial  #2}}

% Heading arguments are {volume}{year}{pages}{submitted}{published}{author-full-names}

% \jmlrheading{1}{2017}{1-48}{4/00}{10/00}{Taylor G. Smith and Jason M. White}

% Short headings should be running head and authors last names

\ShortHeadings{Correcting Class Imbalance with Variational Auto-Encoders}{Smith and White}
\firstpageno{1}

\begin{document}

\title{On the Synthetic Generation of Minority Class Observations Using Variational Auto-Encoders}

\author{\name Taylor G.\ Smith \email taylor.smith@alkaline-ml.com
       \AND
       \name Jason M.\ White \email jason.m.white5@gmail.com}

\editor{Michael Bernico}
\maketitle

\begin{abstract}%   <- trailing '%' for backward compatibility of .sty file
This document describes a methodology by which to remedy imbalanced datasets for the purpose of classification. A dataset may be considered imbalanced if its classification labels are disproportionately represented across classes. In many real-world domains, class imbalance---or the presence of extremely rare events which may be costly to misclassify---is quite prevalent. Commonly cited problem domains include fraud detection, medical imaging, anomaly detection and countless more.  Our procedure augments the Synthetic Minority Over-sampling Technique \citep*{chawla2002smote}---or SMOTE---by training generative variational auto-encoders on the minority class(es) for the purpose of creating synthetic examples. This ensures a set of synthetic observations that are more true to the most archetypal observed points in the minority class.
\end{abstract}

\begin{keywords}
  classification, class-imbalance, cost-sensitive learning, SMOTE, variational auto-encoders, skewed distributions
\end{keywords}

\section{Introduction}

A dataset may be considered imbalanced if its classification labels are disproportionately represented across classes. While class imbalance is very common and manifests itself in varying degrees, of particular interest are the cases in which one class---the majority class---is significantly more present than one or more minority classes, which are represented at a much smaller ratio. This can detrimentally impact a learning algorithm's ability to estimate a generalizable decision boundary. Consider, for instance, a medical test to determine whether a patient suffers a rare disease. The dataset may be 99.8\% composed of negative observations with only 0.2\% positive examples. Even the most na\"ive classifier can achieve 99.8\% classification accuracy in this case by simply learning to always predict the negative class \citep{lewis1994heterogeneous}. However, the utility of this test is completely absent, since it will never accurately predict the condition of value. This is by no means an isolated case, either; many machine learning domains---such as fraud detection, network security, spam filtration---frequently face some level of class imbalance. This paper presents some of the pitfalls of training machine learning models on such dataset, and presents a remedying class-balancing technique.

Throughout this paper, we focus on inducing classification algorithms on a given training set, $X \in \mathbb{R}^{m \times n}$, with a corresponding set of class labels, $y \in \{0, 1, ..., c\}$ in which one or more of the minority class labels is/are represented at a significantly smaller proportion than that of one or more majority class labels. As noted by countless studies and the aforementioned medical test example, classifier efficacy often cannot meaningfully be measured via conventional, cost-insensitive metrics such as accuracy (or the percentage of testing observations properly identified by the learner). This greatly complicates the classification task since such metrics will offer misleadingly optimistic scores on an otherwise ineffective classifier. Therefore, what makes class imbalance a particularly interesting and relevant problem is the frequent tangible cost with which misclassification of rare events is typically associated. The real-world impact of such errors can be especially perilous in the medical domain, where diagnostic datasets are especially susceptible to class disparity, as high risk examples of interest (e.g., instances of rare diseases) tend to constitute the minority class \citep{rahman2013addressing}.

Section 2 presents previous work to which our approach may be compared. Section 3 introduces generative models, and more specifically, variational auto-encoders. Section 4 outlines the details of our technique. Section 5 details the specifics of our experiments and the performance of our technique compared with other common class imbalance solutions. \\

\section{Previous Work}

The problem of class imbalance is not a new one; for a long time, the machine learning community has attempted to address the problem of class imbalance in several ways: class weighting---such as the development of cost-sensitive classification metrics like the ROC convex hull \citep{provost2001robust} or the F-measure \citep{lewis1994training}---which assigns a heavier cost to misclassified events in the positive class, and data preparation tasks that either resample or manipulate the input data before building a model. Our approach can be considered one of the latter, and seeks to synthetically augment the training set irrespective of the selected scoring metric. We present existing research for each of the strategies in the following subsections.

\subsection{Over-sampling minority class samples}

Research on the efficacy of resampling as a preprocessing technique for imbalanced datasets has been extensively conducted. One common approach for resampling a training set is over-sampling the minority class examples with replacement \citep{japkowicz2000class}. She proposed several variants of over-sampling, with varying degrees of success. Her random resampling approach drew minority samples with replacement until the minority class was represented at the same magnitude as the majority class. She also proposed ``focused resampling," which only drew minority samples from along the decision boundary between the majority and minority classes.

Of note is the fact that she observed no clear advantage between generalized over-sampling and her ``focused" over-sampling \citep{japkowicz2000class}. Using a decision tree classifier, \citet{chawla2002smote} showed that duplicating minority observations actually further narrowed the (theoretically, already very small) decision region, causing more splits in the tree and leading to overfitting.

\subsection{Under-sampling majority class samples}

In conjunction with her research on over-sampling methods, \citet*{japkowicz2000learning} showed that under-sampling the majority class can be effective---especially in tasks where $c > 2$---presenting the added benefit of reducing the training set size, and consequently the complexity of the training task. However, in cases of extreme class disparity, the amount of down-sampling required to balance the class labels may so significantly diminish the size of the dataset that it could become unusable.

\citet*{kubat1997addressing} proposed a more sophisticated form of under-sampling by which the majority class samples are segmented into one of four categories: those suffering \emph{class-label noise}, unreliable \emph{borderline} samples that sit near the decision surface, \emph{redundant} examples, and \emph{safe} examples (archetypal to the majority class representation, and necessary for training). They showed that the noisy and border-point majority class examples could easily be recognized in the training set by identifying and removing majority class samples that form \emph{Tomek links} \citep{tomek1976two} with minority class samples.

\subsection{Synthesizing minority class samples}

Rather than simply sampling the minority class with replacement, \cite{chawla2002smote} proposed the over-sampling of synthetic minority class observations.  As previously mentioned, they implemented the SMOTE algorithm which uses randomly sampled minority class observations' \emph{k}-nearest neighbors to generalize minority class decision region neighborhoods: for each of $p$ randomly sampled minority observations, $\mathbf{x}^{(i)} \in \mathbb{R}^{n}$, compute the \emph{k}-nearest neighbors to $\mathbf{x}^{(i)}$, denoted as $\mathbf{J} \in \mathbb{R}^{k \times n}$, and generate a synthetic example, $\mathbf{x}^{(i)\prime}$, between $\mathbf{x}^{(i)}$ and each nearest neighbor, $\mathbf{j}^{(l)}$:
\[
    \mathbf{x}^{(i)\prime} = \mathbf{x}^{(i)} + gap * (\mathbf{j}^{(l)} - \mathbf{x}^{(i)})
\]
where $gap$ is a random scalar between 0 and 1. The application of SMOTE has been shown to effectively aid in generalizing the decision region for the minority class(es) by synthetically creating minority class observations \citep*{chawla2002smote}. However, it runs the risk of ``\emph{over}-generalized'' minority decision regions in cases of significant class overlap.

Figure 1 shows an unbalanced dataset with significant overlap between classes. The decision regions for the minority class are narrow with respect to the total decision space, and classification performance is poor. Figure 2 shows the same dataset after being balanced by SMOTE. The decision region for the minority class, though significantly generalized, has bled so far into the majority class space as to diminish the utility of the classifier.

%\newpage
%\centerline{
%  \includegraphics[scale=.6]{no-balance.png}
%}

%\newpage
\begin{figure}[H]
    \centering
    \includegraphics[scale=.5]{no-balance.png}
    \caption[width=40mm]{An SVM classifier exhibits poor performance on a highly unbalanced dataset with significant class overlap \& a narrow decision region for the positive class.}
    \label{fig:label}
\end{figure}
\begin{figure}[H]
    \centering
    \includegraphics[scale=.5]{smote-balance.png}
    \caption[width=40mm]{The same dataset, after being balanced with SMOTE. The decision region has been over-generalized to the point of bleeding so far into the positive decision space that the usefulness of the classifier is diminished.}
    \label{fig:label}
\end{figure}

\section{Generative Models}

At a high level, generative methods are a family of unsupervised learning techniques that can be trained to generate synthetic data. Easy to conceptualize is the case of image datasets, where a model can be thought of as not simply memorizing input samples for some classification task, but learning features that can be used to create a stylistically canonical image that is completely fake, yet resembles a sample that \emph{could} have been presented to the inducer for training. Thus, the advent of generative algorithms is that they do not simply learn the classification boundaries between classes---as do discriminative algorithms---but model the distribution by which the data itself was generated.

\subsection{Variational Auto-Encoders}

SMRT is built atop variational auto-encoders. An auto-encoder is a special case of unsupervised multilayer perceptron (MLP) that estimates its own input vector as its output. In a more formal sense, the auto-encoder takes some input vector $\mathbf{x} \in  \mathbb{R}^{n}$ and maps it deterministically to some hidden representation (typically a compressed space) $\mathbf{y} \in  \mathbb{R}^{h}$ via $\mathbf{y} = f_{\theta}(\mathbf{x}) = g(\mathbf{Wx} + b)$, where $\mathbf{W}$ is a $n \times h$ matrix of weights parameterized by $\theta = \{\mathbf{W, b}\}$ \citep{meyer2015introduction}. $b \in \mathbb{R}^{h}$ is a vector of biases generally initialized as $\vec{1}$, and $g$ is the activation function---commonly either the \emph{sigmoid} or \emph{relu}:
\[
    \mathrm{sigmoid(x)} = \frac{1}{1 + e^{-x}}
\]
\[
    \mathrm{relu(x)} = \mathrm{max}(0,x)
\]
The goal, then, is that the mapping of $\mathbf{x} \rightarrow \mathbf{y}$ reveals some non-linear  transformation of $\mathbf{x}$, thus extracting new features derived by otherwise unobvious relationships between input features. This offers the ability to effectively reconstruct inputs with minimal error, while identifying high-reconstruction-error examples as anomalous, or non-conformant to the underlying learned distribution. The general architecture of an auto-encoder is dual: the encoding task either compresses or expands the input signal into the hidden layer space, while the decode task projects the hidden layer-transformed values back to the input space.

Whereas general auto-encoders are deterministic MLPs, the \emph{variational} auto-encoder (VAE) is a generative adaptation that consists of two parts: a \emph{probabilistic} encoder, $q_{\phi}(\mathbf{z}|\mathbf{x}^{(i)})$, that approximates the (intractable) posterior distribution, $p(\mathbf{z}|\mathbf{x}^{(i)})$; and a \emph{generative} decoder, $p_{\theta}(\mathbf{x}^{(i)}|\mathbf{z})$ which does not depend on any input \citep{shiffman2016}. VAEs learn latent vectors, which approximate a unit Gaussian posterior, $\mathbf{z}$, with a diagonal covariance structure \citep{kingma2013auto}:
\[
    \log q_{\phi}(\mathbf{z}|\mathbf{x}^{(i)}) = \log \mathcal{N}(\mathbf{z}; \bm{\mu}^{(i)}, \bm{\sigma}^{2(i)}\mathbf{I})
\]
where the mean and standard deviation of $\mathbf{z}$ (the approximate posterior), $\bm{\mu}^{(i)}$ and $\bm{\sigma}^{(i)}$, are the output of the auto-encoder's encode task \citep{kingma2013auto}.

The VAE parameters are found by minimizing a dual loss-function: the combination of the reconstructive loss---the MSE of the input vector and the reconstructed vector---and the latent loss---the Kullback-Leibler (KL) divergence---which measures how well the latent vectors approximate the unit Gaussian:
\[
    D_{KL}(q_{\phi}(\mathbf{z}|\mathbf{x}^{(i)})\parallel p_{\theta}(\mathbf{z}))
\]
In the decode stage, synthetic examples are generated by sampling the posterior:
\[
    \mathbf{z}^{(i,l)} \sim q_{\phi}(\mathbf{z}|\mathbf{x}^{(i)})
\]
and finally decoding $\mathbf{z}^{(i,l)}$ with either a Bernoulli or Gaussian multilayer perceptron \citep{kingma2013auto}. The utility of using VAEs for class balancing is that the approximated posterior distribution will generate synthetic examples that are more probabilistically archetypal to the minority class samples, in similar fashion to the idea of ``safe examples'' proposed by \citet{kubat1997addressing}.

\section{SMRT}

We present a generative over-sampling approach similar to SMOTE, by which the observations in each minority class are used to fit a variational auto-encoder, and synthetic examples are generated until the class is represented at the user-specified ratio. \\

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

\begin{algorithm}
\caption{SMRT}\label{smrt}
  \begin{algorithmic}[1]
    \Procedure{Balance}{$X, y, ratio$}\Comment{Balance $X, y$ subject to $ratio$}
      \State $labels \gets \text{distinct } \textit{y}$
      \State $majority \gets argmax(count(y))$
      \State $nlabels \gets \text{length of } labels$
      \State $nreq \gets int(ratio \times \text{number of majority samples in } \textit{y})$
      \BState \emph{loop}:
      \For{i := 1 to \textit{nlabels}}
        \State $label \gets labels[i]$
        \If {$label \neq majority$}\Comment{Skip the majority class}
          \State $Xsub \gets X \text{where } y = label$
          \State $p \gets nreq - \text{length of } Xsub$\Comment{\textit{n} needed for this class}
          \State \text{Fit a variational auto-encoder with $Xsub$, call it $vae$}
          \State \text{Generate $p$ synthetic examples from $vae$, update $X, y$}
        \EndIf
      \EndFor
      \Return{$X, y$}\Comment{Optionally shuffle}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\section{Experiments}

We used induced three different learners on each dataset: TODO

\subsection{Datasets}

\newpage
\bibliography{references}

\end{document}


A type classes are often compared to interfaces. It contains the function declarations. A type becomes an instance of a type class when it defines all required functions of the type class. When a type is an instance of a type class, we can make certain assumptions about behavior of the type. In contrary to interfaces, type classes aren't types. A value can have the type of an interface but not of a type class. Another difference to interfaces is that type classes describe functions and the instance types are part of the function signature. The instance type can be the return type. Interfaces define methods of the instance type.

The concept of a type class is explained by example with the function \verb|show| from the \verb|Prelude|-Library (\verb|Prelude| is a module and part of the standard). \verb|show| converts a given value of a type \verb|a| into a character string. The type of \verb|show| is

\begin{verbatim}
show :: Show a => a -> String
\end{verbatim}

The \verb|Show a| before the \verb|=>| is a type class constraint. \verb|a| is a type variable. A type that has one more type variables is called polymorphic \cite{hutton}. The signature means that \verb|show| takes something that implements the type class \verb|Show| and returns a string. \verb|Show| is a type class. It's possible to call \verb|show| with different types (e.g \verb|show 1|, \verb|show "hello"|). The compiler will look up the correct definition for us as long the type of the first parameter is an instance of the type class \verb|Show|.
Any type that implements \verb|Show| can be converted to a character string. Types in this class are \verb|Bool|, \verb|Char|, \verb|Int|, \verb|Float|, \verb|Double| etc.

The type class \verb|Show| is defined as follows:
\begin{verbatim}
class Show a where
    show :: a -> String
\end{verbatim}
The keyword \verb|class| defines a new type class. \verb|a| is the type variable. It represents the type that implements the type class (e.g. \verb|Int| or \verb|Bool|).

Once we have a type class we are able to create instances of that class. The following listing defines a type \verb|Person|. It has fields for name and email address. \todo{loesung fuer schoene listings mit labels und caption finden und ueberall gleich machen}
\begin{verbatim}
data Person = Person { name :: String
                       email :: String
                     }
\end{verbatim}

To make \verb|Person| an instance of \verb|Show| we provide a function with the following type:
\begin{verbatim}
show :: Person -> String|
\end{verbatim}

\begin{verbatim}
instance Show Person where
    show (Person name _) = name
\end{verbatim}

There many other useful type classes in the standard library.

\begin{description}
\item[Ord] Types with an order relation implement \verb|Ord|.
\item[Eq] For types that can be equated.
\item[Read] Types that can be converted from a string.
\end{description}

It will explain the relation to polymorphism and describe type classes \verb|Functor|, \verb|Applicative| and \verb|Monoid| in more detail as these type classes are important for the example in section \ref{sec:example}.

Type classes allow programmers to write general definitions in a typesafe manner by use constrain the paramaters.

Polymorphic functions can be applied to values with different types.

If a type can be converted to a string, it can be given the type class \verb|Show|. The type \verb|Person| has to provide an implementation for the function \verb|show|. Applying \verb|show| to \verb|Person| results in a different behavior then applying \verb|show| to an \verb|Int|.

\subsection{Monoid}

Some types, let's say \verb|a|, have a binary function with the type declaration.
\begin{verbatim}
f :: a -> a -> a
\end{verbatim}

The type \verb|a| has a value that serves as identity for the given function. For example the number 1 is the identity for the multiplication. Multiplication of the identity and any other number $x$ results always in $x$.

Several value of type \verb|a| can always be reduces to a single value. It doesn't matter in which order we apply the function, the result is always the same. This is called associativity.

If type \verb|a| has this behavior, it's a monoid and it can be an instance of the \verb|Monoid| type class.

The \verb|Monoid| type class is define as follows \cite{monoid}:
\begin{verbatim}
class Monoid m where
    mempty :: m
    mappend :: m -> m -> m
    mconcat :: [m] -> m
    mconcat = foldr mappend mempty
\end{verbatim}
\verb|mconcat| takes a list of monoids and reduces them \verb|mappend| to a single value, applying mappend. It has a default implementation.

and \verb|mappend| must be associative. There are three monoid laws \cite{monoid}. This article deals only with the first law:

\begin{enumerate}
\item \verb|mappend mempty x = x|
\item \verb|mappend x mempty = x|
\item \verb|(x `mappend y) `mappend z = x `mappend (y `mappend z)|
\end{enumerate}

The following Haskell types are \verb|Monoid| instances
\begin{description}
\item[List] The empty list \verb|[]| and \verb|++| (concatenation) form a monoid.
\item[Product and Sum] Numbers can be monoid with respect to multiplication or addition. There are two monoid instances for \verb|Num|. One for multiplication an the other for addition.
\item[Maybe] Can also be an instance of \verb|Monoid|. \todo{instance beschreiben}
\end{description}

Some types, let's say \verb|a|, have a binary function with the type declaration.
\begin{verbatim}
f :: a -> a -> a
\end{verbatim}

The type \verb|a| has a value that serves as identity for the given function. For example the number 1 is the identity for the multiplication. Multiplication of the identity and any other number $x$ results always in $x$.

Several value of type \verb|a| can always be reduces to a single value. It doesn't matter in which order we apply the function, the result is always the same. This is called associativity.

If type \verb|a| has this behavior, it's a monoid and it can be an instance of the \verb|Monoid| type class.

The \verb|Monoid| type class is define as follows \cite{monoid}:
\begin{verbatim}
class Monoid m where
    mempty :: m
    mappend :: m -> m -> m
    mconcat :: [m] -> m
    mconcat = foldr mappend mempty
\end{verbatim}
\verb|mconcat| takes a list of monoids and reduces them \verb|mappend| to a single value, applying mappend. It has a default implementation.

and \verb|mappend| must be associative. There are three monoid laws \cite{monoid}. This article deals only with the first law:

\begin{enumerate}
\item \verb|mappend mempty x = x|
\item \verb|mappend x mempty = x|
\item \verb|(x `mappend y) `mappend z = x `mappend (y `mappend z)|
\end{enumerate}

The following Haskell types are \verb|Monoid| instances
\begin{description}
\item[List] The empty list \verb|[]| and \verb|++| (concatenation) form a monoid.
\item[Product and Sum] Numbers can be monoid with respect to multiplication or addition. There are two monoid instances for \verb|Num|. One for multiplication an the other for addition.
\item[Maybe] Can also be an instance of \verb|Monoid|. \todo{instance beschreiben}
\end{description}

All members of the collection  with the property that certain functions are defined over the type.

 With equational reasoning we are able to show that certain algebraic properties hold.

This article will only describe proofs with programs, which halts. An evaluation can go on forever.
\begin{verbatim}
f x = 1 + f x
\end{verbatim}

The value of the expression is undefined. The proofs in this article hold only for defined expressions and finite lists..

Equational reasoning is use-full because
\begin{itemize}
\item it allows to verify properties of a program.
\item it can be used to eliminate expensive function call while preserving behavior
\end{itemize}

\subsection{Pipes: A real world example}
\label{sec:pipes}
\todo{Abschnitt neu formulieren oder wegglassen}
Pipes is a streaming library for Haskell. It was build with the following requirements \cite{gonzales13}.
\begin{description}
\item[Effects] Streams has to be effectful
\item[Streaming] Processing in constant memory
\item[Composability] Modules have to be composable
\end{description}

Pipes uses the type class \verb|Category| too keep the API simple and easy to use.

This article will descipe a part of the library with an example.

First we need a input stream, that emits values. Input streams are have the type
\verb|Producer a m ()|.
Listing \ref{lst:simpleproducer} show a simple Producer. It emits the integers 1,2 and 3. It is also possible to create producers for effectful streams. We use the simple producer from Listing \ref{lst:simpleproducer} for simplicity reasons.

\begin{program}
\begin{verbatim}
produceints123 :: Producer Int IO ()
produceints123 = each [1,2,3]
\end{verbatim}
\caption{Simple Producer}
\label{lst:simpleproducer}
\end{program}

Pipes provide the function \verb|for| to consume a producer.
\verb|for| \verb|producer| \verb|body| loops over the \verb|producer| and applies a the transformation defined in \verb|body| to every element yielded by the producer. If the body is of type \verb|Effect| it return an \verb|Effect|. If body is of type \verb|Producer| a \verb|Producer| is returned. The type declaration of \verb|for|
\begin{program}
\begin{verbatim}
for::Monad m=>
Producer b m r
-> (b -> Producer c m ())
-> Producer c m r
\end{verbatim}
\caption{type of for}
\end{program}

A value for the body, could be function, that takes \verb|Int| and returns producer \verb|Producer a IO ()|. Hence the type declarion is \verb|Int -> Producer a IO ()|. We implement a body, that prints the integers to the standard input (strictly speaking this value is an effect of type \verb|Effect Int ()|. But \verb|Effect Int ()| is of type \verb|Producer Int IO () |).

\begin{program}
\begin{verbatim}
print2stdout:: Int -> Effect IO ()
print2stdout x = (lift . putStrLn . show ) x
\end{verbatim}
\caption{Definition of an effect, that prints to stdout}
\label{lst:stdouteffect}
\end{program}

With the producer and the body we can define a \verb|Effect IO ()|
\begin{verbatim}
effect :: Effect IO ()
effect = for produceints123 print2stdout
\end{verbatim}

Because the \verb|body| and of \verb|for| can be of type \verb|a -> Producer|, and the type of the return value is \verb|Producer|, it's possible to interleave several Producers.

If we want to duplicate all element we write another body
\begin{verbatim}
duplicate :: Int -> Producer Int IO ()
duplicate x = yield x >> yield x
\end{verbatim}

We use \verb|for| to create a another producer. \verb|composed_producer| is a composition of \verb|produceints123| and \verb|duplicate|.
\begin{verbatim}
composed_producer :: Producer Int IO ()
composed_producer = for produceints123 duplicate
\end{verbatim}

To compose \verb|composed_producer| with our \verb|print2stdout| effect, we apply the \verb|for| function

\begin{verbatim}
effect2 :: Effect IO ()
effect2 = for composed_producer print2stdout
\end{verbatim}

In order to compose producers, the pipe library provides the \verb|~>| function. \verb|~>| is defined as follows:
\begin{verbatim}
(f ~> g) x = for (f x) g
\end{verbatim}

Hence we can write compose a body for \verb|for| like this:
\begin{verbatim}
composed_with_yield :: Int -> Effect IO ()
composed_with_yield = duplicate ~> print2stdout
\end{verbatim}

Gabriel Gonzales proved that the \verb|~>| operator is associativ. It has the associativity property.

\begin{verbatim}
 -- Associativity
 (f ~> g) ~> h = f ~> (g ~> h)
\end{verbatim}

Because the \verb|~>| operator is associativ, it doesnt matter in witch order the body is composed. The expected behavior is defined in term of cateory laws. It's possible to prove that the specified laws hold.

\subsection{Function composition}

\label{sec:functioncomposition}

Function composition in mathematics is defined as

\begin{equation}
  \label{eq:functioncomposition}
  (f \circ g)(x) = f(g(x))
\end{equation}

Haskell functions can be compose with the \verb|.| function.

\begin{figure}
  \centering
\begin{verbatim}
(.) :: (b -> c) -> (a -> b) -> a -> c
f . g = \x -> f (g x)
\end{verbatim}
  \caption{. function}
  \label{fig:compositionfunction}
\end{figure}

Function composition allows us to create a function by composing it with many small functions.

Functions in Haskell form a category in category theory. They satisfy the category laws.

\subsection{garbage}
 There are several methods to verify software.
being able to replace equals by equals.
In this article we will look at technique of equational reasoning to prove type class laws. If we can rely on known properties of your program, we minimize unexpected behavior.

 In addition, they exhibit certain properties. These properties are called laws.

For example the type class \verb|Functor| makes sure that we can apply the function \verb|fmap| with a type that is part of the \verb|Functor| type class. The type class dictates the behavior. All functors are expected to exhibit certain kinds of properties. These properties guarantee that the function \verb|fmap| only applies a function to all values inside the functor and doesn't change the structure or the context. The expression
\begin{verbatim}
dontchangecontext = do line <- fmap reverse getLine
                       putStrLn $ " "
\end{verbatim}
applies \verb|reverse| to the result of the IO function \verb|getLine| without changing the the input stream.

Type class instances from the standard library obey the type class laws. If we write new instances, it's our responsibility to check if the type class laws hold.
It's in the responsibility of the programmer to check if the program behaves correctly. For most programmers it's hard to write a correct program at the first attempt. This leaves us with the question of how to verify the type class laws.

Properties are specified in form of equations. The source code must satisfy this equations.

The monoid laws are specified by the type class monoid.

\begin{figure}
  \centering
\begin{verbatim}
fmap id = id
fmap (g . h) = fmap g . fmap h
\end{verbatim}
  \caption{The Functor laws}
  \label{fig:functorlaws}
\end{figure}

\begin{figure}
  \centering
     \includegraphics[width=0.9\textwidth]{functor_applicative}
  \caption{Interrelationship of Functor and Applicative }
  \label{fig:functor_applicative}
\end{figure}

\subsubsection{Applicative function definition}
 The applicative instance is defined as follows:
\begin{verbatim}
instance Monoid b => Monoid (a -> b) where
    mempty = pure mempty

    mappend = liftA2 mappend
\end{verbatim}

\begin{description}
\item[Associativity law]
\item[Left/Right Identity law]
\end{description}

In comparison to testing equational reasoning covers not just one possible case of input but all cases.

Of course listing \ref{lst:compose} only works if \verb|mappend| is implemented for the type \verb|IO ( Char -> IO ())|. Hence we have to provide a monoid instance implementation for the type \verb|IO ( Char -> IO ())|.

 requirements:
\begin{itemize}
\item We want to be able to add an arbitrary number of plugins.
\item The plugins should be composable.
\item The order we add the plugins must not matter.
\end{itemize}

. We could use the \verb|mappend| operator to compose several plugins.

The following listing shows the use of a plugin. The \verb|logto| function is a plugin. The program will read a character \verb|c| from the command line and apply the given plugin to \verb|c|.

\begin{lstlisting}

\end{lstlisting}

To append additional plugins, e.g. \verb|print2stdout|, we compose a new monoid with the \verb|mappend| function.
\begin{verbatim}
handleChar <- mappend logto print2stdout
\end{verbatim}
The definition of \verb|logTo| is shown in the following listing:

\begin{verbatim}
logTo :: IO ( Char -> IO ())
logTo = do
    handle <- openFile "log.txt" WriteMode
    return (hPutChar handle)
\end{verbatim}

The Haskell type \verb|IO|, used for I/O actions, is part of the \verb|Applicative| type class. Listing \ref{lst:monoidinstance1} shows the implementation of a \verb|Monoid| instance for the the type \verb|IO m| where the type variable \verb|m| is a \verb|Monoid|.
\bibitem{gonzales}
Gabriel Gonzales,
Equational reasoning at scale,
\url{http://www.haskellforall.com/2014/07/equational-reasoning-at-scale.html},
2014.

\begin{verbatim}
y = f x
g = h y
\end{verbatim}
We can replace the definition of \verb|g| with
\begin{verbatim}
g = h (f x)
\end{verbatim}
and get the same result.

We can replace the definition of \verb|g| with
\begin{verbatim}
g = h (f x)
\end{verbatim}
and get the same result.

\begin{frame}
 \frametitle{Why are functional programming languages great for equational reasoning?}
Referential transparancy
\end{frame}

\begin{frame}[fragile]

  \begin{description}
  \item[Testing] Code is compiled and executed. Can expose error. Cannot proof absence of errors.

  \end{description}
\end{frame}

\begin{frame}

\begin{Verbatim}

\end{Verbatim}

\end{frame}

\begin{frame}[fragile]
\frametitle{Associativity}
The order of evaluation must not matter.

\end{frame}


%        File: ml-notes.tex
%     Created: Mon Jan 14 08:00 PM 2019 E
% Last Change: Mon Jan 14 08:00 PM 2019

% Documentclass
\documentclass[12pt]{article}
% margin
\setlength{\topmargin}{0mm} \setlength{\oddsidemargin}{0mm}
\setlength{\textwidth}{160mm} \setlength{\textheight}{220mm}
\font\bbc=msbm10 scaled 1200

% amsmath:\text{} inside math mode; amssymb:\lesssim, amsthm:\newtheorem; amsfonts:\mathbb{}
\usepackage{amsmath, amssymb, amsthm, amsfonts}
%to use color
\usepackage{color}
\usepackage{hyperref}

% User defined fun
\newcommand{\F}{\mathcal{F}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\br}[1]{\color{red} (#1) \color{black}}
\newcommand{\bb}[1]{\color{blue} (#1) \color{black}}
\newcommand{\what}{\bb{??}}
\newcommand{\half}{\frac{1}{2}}
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
\newcommand{\abs}[1]{\left\lvert#1\right\rvert}
\newcommand{\jap}[1]{\left\langle #1 \right\rangle}
\newcommand{\inn}[1]{\left\langle #1 \right\rangle}
\newtheorem{thm}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{cor}{Corollary}
\newtheorem{defn}{Definition}
\newtheorem{rem}{Remark}
\newtheorem{result}{Result}
\newtheorem*{notation}{Notation}
\newtheorem{prob}{Problem}
\newtheorem{propn}{Proposition}
% newtheorem{proof}{Proof} is not needed, already available with \begin{proof} .. \end{proof} with nice italic fonts

\begin{document}
\title{Notes on Machine learning}
\author{Debdeep Bhattacharya}
\maketitle

\section{Documentaion}
\begin{itemize}
  \item \href{https://scikit-learn.org/stable/tutorial/statistical_inference/supervised_learning.html}{scikit-learn tutorial}
\end{itemize}

\section{numpy matrix operations}
import numpy as np
Create a new vector with np.array([1, 2, 3])
Create a new matrix with np.c\_[1, 2, 3]
Transpose with .T
Create a random matrix with entries from a normal distribution with size 2x1 using np.random.normal(size = (2,1))

\section{p}<++>

\section{k-Nearest Neighbor (k-NN)}
If the number of features (dependent variables) is $p$ and the maximum distance of any two points in a cluster is less than $d$, then the required data points (sample size) need to be $n \sim \frac{1}{d^p}$.

\section{Python}
Install scikit-learn on linux using pip3 (not pip) and run with python3, not python.

\end{document}


\documentclass[a4paper, 12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{indentfirst}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{esvect}
\usepackage{graphicx}
\usepackage{hyperref}

\newcommand{\veca}{\[ \vv{a} \]}
\setlength{\parindent}{1cm}
\DeclareMathOperator{\MyFunction}{Fun}

\title{Tutorial}
\author{Gustave Li}
\date{July 2021}

\begin{document}

\maketitle

\section{Change text sizes} \label{sec: text size}

I want to try out different font sizes in this paragraph, this is {\scriptsize smaller than} the original text, this one is {\Large larger}, this one is {\huge even larger}.

\section{Font styles}

\setlength{\parindent}{1cm}
\noindent This is a normal paragraph.

\noindent \textbf{This is a paragraph with bold texts}

\noindent \textit{This is a paragraph with italics texts}

\noindent \underline{This is a paragraph with underlined texts}

\section{Text emphasis}
Text emphasis in {\LaTeX} is \emph{context driven}, \textbf{which means that the font choice for emphasis text is based on the \emph{current font}}.

\section{Font families}
The default font for {\LaTeX} is Roman, but we can change to \textsf{the sans serif font}.

When typing codes, we may choose typewriter font:
\begin{center}
    \texttt{print('Hello world!')}

    \texttt{a = 2}
\end{center}

\section{The tabular environment}
\begin{center}
    \begin{tabular}{|c|c|c|}
    \hline
    text & text & text \\
    \hline
    1    &    2 & 3 \\
    4 & 5 & 6 \\
    \hline
    \end{tabular}
\end{center}

\section{The math environment}
You can enter math formulae in math mode
\[
F = m \times a
\]
\subsection{Display style math}
The formulae are independent from the texts in this style, they are centered separately.

To align several formulae:
\begin{align*}
    f(x) & = a_2 x^2 + a_1 x + a_0 \\
         & = x^2 + 4x -5
\end{align*}

The formulae can also be numbered:
\begin{align}
    f(x) & = a_2 x^2 + a_1 x + a_0 \\
         & = x^2 + 4x -5
\end{align}

Or can only number the last formula:
\begin{align}
    f(x) & = a_2 x^2 + a_1 x + a_0 \nonumber \\
         & = x^2 + 4x -5
\end{align}

\subsection{Inline style math}
The equation \( F = m a\) is called Newton's Second Law.

\section{Basic math notations}

\subsection{Arithmetic}
Multiplication and division signs can by typed in math mode: \(a \cdot b\), \(a \times b\), \(a \div b\).
\subsection{Parentheses}
By specifying the parentheses pairs, {\LaTeX} can automatically adjust the size of parentheses:
\[
\left( \sum_{n=0}^N \left( \frac{1}{a + b } \right)^2 \right)^2
\]

\subsection{Greek letters}
\noindent You can type Greek letters under math mode: \( \alpha \beta \gamma \delta\) etc. \\

\noindent The complete alphabet with the commands are shown below

\includegraphics[width=\linewidth]{Greek_letters.png}

\subsection{Array}
\noindent \textbf{Warning: arrays must be generated in math mode}
\[
\begin{array}{ccc}
    \label{array: example}
    a_{11} & a_{12} & a_{13}  \\
    a_{21} & a_{22} & a_{23}  \\
    a_{31} & a_{32} & a_{33}
\end{array}
\]
The '\&' tells Latex where to align, while the slash tells where to start a new row.

\subsection{Calculus}
\begin{itemize}
    \item Limit
    \[
    \lim_{x \to \infty} \, \frac{1}{x}
    \]
    \item Sum
    \[
    \sum_{x=0}^{\infty} \, x^2
    \]
    \item Integral
    \[
    \int_{- \infty}^\infty x^2
    \]
    \begin{itemize}
        \item Double integral
        \[
        \iint_{- \infty}^\infty x^2
        \]
        \item Vertical bar for integral evaluation
        \[
        \int_0^4 x^2 \, = \frac{x^3}{3} \, \bigg\vert_0^4
        \]
    \end{itemize}
    \item Derivative
    \begin{itemize}
        \item Prime notation
        \[
        f'(x), f''(x), f'''(x), \ldots, f^{(n)}(x)
        \]
        \item Dot notation
        \[
        \dot{x}(t), \ddot{x}(t)
        \]
    \end{itemize}
    \item Vector
    \begin{itemize}
        \item Bold notation
        \[
        \bm{a}, \bm {b}, \bm{a} \cdot \bm {b}
        \]
        \item Arrow notation
        \[
        \vv{a}, \vv{b}, \vv{a} \times \vv{b}
        \]
    \end{itemize}
\end{itemize}
\subsection{Others}
\begin{itemize}
    \item Fraction
    \[
    \frac{3}{5}, \; \frac{x^2}{x+1}
    \]
    \item Square root
    \[ \sqrt{5} \]
    Can also specify the order:
    \[ \sqrt[3]{5} \]
    \item Comparison symbols
    Greater than or equal to: \( a \geq b\)

    Less than or equal to: \( a \leq b\)

    Approximately equal to: \(a \approx b\)
\end{itemize}

\section{Customization}
\subsection{Page breaks}
\begin{itemize}
    \item Soft break
    The 'pagebreak' command tries to extend the line spacing so that the text can occupy the whole page
    \item Hard break
    The 'newpage' command just starts a new page
\end{itemize}
\subsection{User defined commands}
\noindent \textbf{(Should be defined in preamble)} \\
First define a new command: I want the command 'veca' be the arrow-formed vector a.

Then try it out: \veca

\subsection{User defined functions}
\noindent \textbf{(Should be defined in preamble)}

\noindent I want to define 'myfunction' as a Fun. (It should be displayed without italics in math mode)

\noindent Try it out:

\[\MyFunction(x)\]

\subsection{Labels and references}
We have discussed how to change the text size in Section~\ref{sec: text size}, if you are unclear, please check them out.

The demonstration for generating an array is located in Section~\ref{array: example}, feel free to check them out.

\section{Ending remarks}
This is basically all for this tutorial, for more detailed information, please reference \href{https://www.youtube.com/watch?v=fCzF5gDy60g&list=WL&index=1}{this Youtube video}

\end{document}


\documentclass[Physics.tex]{subfiles}
\begin{document}
\chapter{Gravitational Field}
Newton's \sldef{law of universal gravitation} states that every particle in the universe attracts every other particle with a force directly proportional to the product of their masses and inversely proportional to the square of the distance between them i.e. \begin{equation}\left|\mathbf{F}\right| = G\frac{m_{1}m_{2}}{r^{2}}\end{equation} where \(G\) is the gravitational constant.

The law of gravitation is an inverse-square law i.e. the magnitude of force is inversely proportional to the square of something -- in this case the square of the separation between the particles.

This equation holds only for point masses, but large masses at a large distance can be assumed, without losing too much accuracy, to be a point mass, so this equation is still reasonably valid.

The region in space where an object exerts a gravitational force on another object is the \sldef{gravitational field} of the first object. A gravitational field can be represented by field lines representing the resultant direction of gravitational force acting on a mass placed at any point in the field. Where the field lines are closer, the field is stronger, and vice versa.

The \sldef{gravitational field strength} \textbf{g} at a point in a gravitational field is the gravitational force per unit mass acting on a body placed at that point. \begin{equation}\left|\mathbf{g}\right| = G\frac{Mm}{mr^{2}} = G\frac{M}{r^{2}}\end{equation}

Near the surface of the Earth, \(\mathbf{g}\) is approximately constant, as \(r\) does not vary very appreciably. However, \(\mathbf{g}\) varies over different points on Earth's surface, as the Earth is not a perfect sphere -- it bulges at the equator, the density of the Earth is not uniform, and the Earth is rotating about an axis through its poles -- so for an object not at the poles, gravity also has to provide for centripetal acceleration.

An object is only truly weightless when it experiences no gravitational force at all. Otherwise, it experiences apparent weightlessness -- it simply does not feel its weight as it experiences no normal force.

The \sldef{gravitational potential energy} \(U\) of a mass at a point in a gravitational field is the work done by an external force in bringing the mass from infinity to that point without acceleration. \begin{equation}U = -G\frac{Mm}{r}\end{equation} At infinity, \(U = 0\). The negative sign arises from the fact that the gravitational force is attractive in nature.

The \sldef{gravitational potential} \(\Phi\) at a point in a gravitational field is the work done per unit mass by an external force in bringing a test mass from infinity to that point without acceleration.\begin{equation}\Phi = -G\frac{M}{r}\end{equation}

The \sldef{escape speed} is the minimum speed with which a mass should be launched from a planet's surface to escape the planet's gravitational field. It is simply the speed that gives the mass kinetic energy that makes the mass's total energy exactly equal what its total energy would be if it were stationary at infinity.

An \sldef{equipotential} line or surface is formed by all the points that have the same potential. Objects moving along an equipotential do not lose or gain any energy.

\sldef{Kepler's 3rd law} states that the square of an object's orbit period is proportional to the cube of its orbit radius.

A \sldef{geostationary satellite} of a planet is a satellite that orbits the planet such that it will always be above the same point on the planet. This means that the satellite must be orbiting the planet's equator in the direction of the planet's axial rotation, and the period of the orbit must be equal to the period of the rotation.

Geostationary satellites are often used for communication. They are useful because they always stay over the same point, so there is no need to adjust satellite dishes; they are also high above the Earth and so can see large areas of the Earth. However, also due to their high altitude, images taken tend to be of low spatial resolution, and because they must be over the equator, they are of limited use for latitudes more than about \SI{70}{\degree} N or S.
\end{document}


\chapter{Proofs in Quantified Logic}
\markright{Chap \ref{chap:proofsinQL}: Proofs in QL}
\label{chap:proofsinQL}
\setlength{\parindent}{1em}

% ******************************************
%  *                       6.1 Rules for Quantifiers             *
%******************************************

\section{Rules for Quantifiers}

For proofs in QL, we use all of the basic rules of SL plus four new basic rules: both introduction and elimination rules for each of the quantifiers.

Since all of the derived rules of SL are derived from the basic rules, they will also hold in QL. We will add another derived rule, a replacement rule called quantifier negation.

\subsection{Universal elimination}

If you have $\forall x Ax$, it is legitimate to infer that anything is an $A$. You can infer $Aa$, $Ab$, $Ac$, $Ad_3$--- in short, you can infer $A\script{c}$ for any constant \script{c}. This is the general form of the universal elimination rule ($\forall$E):

\begin{proof}
	\have[m]{a}{\forall \script{x}\script{A}}
	\have[\ ]{c}{\script{A}[\script{c}|\script{x}]} \Ae{a}
\end{proof}

$\script{A}[\script{c}|\script{x}]$ is a substitution instance of $\forall\script{x}\script{A}$. The symbols for a substitution instance are not symbols of QL, so you cannot write them in a proof. Instead, you write the subsituted sentence with the constant \script{c} replacing all occurrences of the variable \script{x} in \script{A}. For example:

\begin{proof}
	\hypo{a}{\forall x(Mx \eif Rxd)}
	\have{c}{Ma \eif Rad} \Ae{a}
	\have{d}{Md \eif Rdd} \Ae{a}
\end{proof}

\subsection{Existential introduction}

When is it legitimate to infer $\exists x Ax$? If you know that something is an $A$--- for instance, if you have $Aa$ available in the proof.

This is the existential introduction rule ($\exists$I):

\begin{proof}
	\have[m]{a}{\script{A}}
	\have[\ ]{c}{\exists \script{x}\script{A}[\script{x}||\script{c}]} \Ei{a}
\end{proof}

It is important to notice that $\script{A}[\script{x}||\script{c}]$ is not the same as a substitution instance. We write it with two bars to show that the variable \script{x} does not need to replace all occurrences of the constant \script{c}. You can decide which occurrences to replace and which to leave in place. For example:

\begin{proof}
	\hypo{a}{Ma \eif Rad}
	\have{b}{\exists x(Ma \eif Rax)} \Ei{a}
	\have{c}{\exists x(Mx \eif Rxd)} \Ei{a}
	\have{d}{\exists x(Mx \eif Rad)} \Ei{a}
	\have{e}{\exists y\exists x(Mx \eif Ryd)} \Ei{d}
	\have{f}{\exists z\exists y\exists x(Mx \eif Ryz)} \Ei{e}
\end{proof}

\subsection{Universal introduction}
A universal claim like $\forall x Px$ would be proven if {every} substitution instance of it had been proven, if every sentence $Pa$, $Pb$, $\ldots$ were available in a proof. Alas, there is no hope of proving \emph{every} substitution instance. That would require proving $Pa$, $Pb$, $\ldots$, $Pj_2$, $\ldots$, $Ps_7$, $\ldots$, and so on to infinity. There are infinitely many constants in QL, and so this process would never come to an end.

Consider a simple argument: $\forall x Mx$, \therefore\ $\forall y My$

It makes no difference to the meaning of the sentence whether we use the variable $x$ or the variable $y$, so this argument is obviously valid. Suppose we begin in this way:

\begin{proof}
	\hypo{x}{\forall x Mx} \by{want $\forall y My$}{}
	\have{a}{Ma} \Ae{x}
\end{proof}

We have derived $Ma$. Nothing stops us from using the same justification to derive $Mb$, $\ldots$, $Mj_2$, $\ldots$, $Ms_7$, $\ldots$, and so on until we run out of space or patience. We have effectively shown the way to prove $M\script{c}$ for any constant \script{c}. From this, $\forall x Mx$ follows.

\begin{proof}
	\hypo{x}{\forall x Mx}
	\have{a}{Ma} \Ae{x}
	\have{y}{\forall y My} \Ai{a}
\end{proof}

It is important here that $a$ was just some arbitrary constant. We had not made any special assumptions about it. If $Ma$ were a premise of the argument, then this would not show anything about \emph{all} $y$. For example:

\begin{proof}
	\hypo{x}{\forall x Rxa}
	\have{a}{Raa} \Ae{x}
	\have{y}{\forall y Ryy} \by{not allowed!}{}
\end{proof}

This is the schematic form of the universal introduction rule ($\forall$I):

\begin{proof}
	\have[m]{a}{\script{A}}
	\have[\ ]{c}{\forall \script{x}\script{A}[\script{x}|\script{c}]^\ast} \Ai{a}
\end{proof}
$^\ast$ \script{c} must not occur in any undischarged assumptions.

Note that we can do this for any constant that does not occur in an undischarged assumption and for any variable.

Note also that the constant may not occur in any \emph{undischarged} assumption, but it may occur as the assumption of a subproof that we have already closed. For example, we can prove $\forall z(Dz \eif Dz)$ without any premises.

\begin{proof}
	\open
		\hypo{f1}{Df}\by{want $Df$}{}
		\have{f2}{Df}\by{R}{f1}
	\close
	\have{ff}{Df \eif Df}\ci{f1-f2}
	\have{zz}{\forall z(Dz \eif Dz)}\Ai{ff}
\end{proof}

\subsection{Existential elimination}
A sentence with an existential quantifier tells us that there is \emph{some} member of the UD that satisfies a formula. For example, $\exists x Sx$ tells us (roughly) that there is at least one $S$. It does not tell us \emph{which} member of the UD satisfies $S$, however. We cannot immediately conclude $Sa$, $Sf_{23}$, or any other substitution instance of the sentence. What can we do?

Suppose that we knew both $\exists x Sx$ and $\forall x(Sx \eif Tx)$. We could reason in this way:
\begin{quote}
Since $\exists x Sx$, there is something that is an $S$. We do not know which constants refer to this thing, if any do, so call this thing $\Omega$. From $\forall x(Sx \eif Tx)$, it follows that if $\Omega$ is an $S$, then it is a $T$. Therefore $\Omega$ is a $T$.  Because $\Omega$ is a $T$, we know that $\exists x Tx$.
\end{quote}
In this paragraph, we introduced a name for the thing that is an $S$. We called it $\Omega$, so that we could reason about it and derive some consequences from there being an $S$. Since $\Omega$ is just a bogus name introduced for the purpose of the proof and not a genuine constant, we could not mention it in the conclusion. Yet we could derive a sentence that does not mention $\Omega$; namely, $\exists x Tx$. This sentence does follow from the two premises.

We want the existential elimination rule to work in a similar way. Yet since Greek letters like $\Omega$ are not symbols of QL, we cannot use them in formal proofs. Instead, we will use constants of QL which do not otherwise appear in the proof. A constant that is used to stand in for whatever it is that satisfies an existential claim is called a \define{proxy}. Reasoning with the proxy must all occur inside a subproof, and the proxy cannot be a constant that is doing work elsewhere in the proof.

This is the schematic form of the existential elimination rule ($\exists$E):

\begin{proof}
	\have[m]{a}{\exists \script{x}\script{A}}
	\open
		\hypo[n]{b}{\script{A}[\script{c}|\script{x}]^\ast}
		\have[p]{c}{\script{B}}
	\close
	\have[\ ]{d}{\script{B}} \Ee{a,b-c}
\end{proof}
$^\ast$ The constant \script{c} must not appear in $\exists\script{x}\script{A}$, in \script{B}, or in any undischarged assumption.

With this rule, we can give a formal proof that $\exists x Sx$ and $\forall x(Sx \eif Tx)$ together entail $\exists x Tx$. The structure of the proof is effectively the same as the English-language argument with which we began, except that the subproof uses the constant ``$a$'' rather than the bogus name $\Omega$.

\begin{proof}
	\hypo{es}{\exists x Sx}
	\hypo{ast}{\forall x(Sx \eif Tx)}\by{want $\exists x Tx$}{}
	\open
		\hypo{s}{Sa}
		\have{st}{Sa \eif Ta}\Ae{ast}
		\have{t}{Ta} \ce{s,st}
		\have{et1}{\exists x Tx}\Ei{t}
	\close
	\have{et2}{\exists x Tx}\Ee{es,s-et1}
\end{proof}

\subsection{Quantifier negation}

When translating from English to QL, we noted that $\enot\exists x\enot\script{A}$ is logically equivalent to $\forall x\script{A}$. In QL, they are provably equivalent. We can prove one half of the equivalence with a rather gruesome proof:

\begin{proof}
	\hypo{Aa}{\forall x Ax} \by{want $\enot\exists x\enot Ax$}{}
	\open
		\hypo{Ena}{\exists x \enot Ax}\by{for reductio}{}
		\open
			\hypo{nc}{\enot Ac}\by{for $\exists$E}{}
			\open
				\hypo{Aa2}{\forall x Ax}\by{for reductio}{}
				\have{c2}{Ac}\Ae{Aa}
				\have{nc2}{\enot Ac}\by{R}{nc}
			\close
			\have{nAa}{\enot\forall x Ax}\ni{Aa2-nc2}
		\close
		\have{nAa3}{\enot\forall x Ax}\Ee{nc-nAa}
		\have{Aa3}{\forall x Ax}\by{R}{Aa}
	\close
	\have{nEna}{\enot\exists x \enot Ax}\ni{Ena-nAa3}
\end{proof}

In order to show that the two sentences are genuinely equivalent, we need a second proof that assumes $\enot\exists x\enot\script{A}$ and derives $\forall x\script{A}$. We leave that proof as an exercise for the reader.

It will often be useful to translate between quantifiers by adding or subtracting negations in this way, so we add two derived rules for this purpose. These rules are called quantifier negation (QN):
\begin{center}
\begin{tabular}{rl}
$\enot\forall\script{x}\script{A} \nsststile{}{} \hspace{.5em} \sststile{}{} \exists\script{x}\enot\script{A}$\\
$\enot\exists\script{x}\script{A} \nsststile{}{}  \hspace{.5em} \sststile{}{} \forall\script{x}\enot\script{A}$
& QN
\end{tabular}
\end{center}
Since QN is a replacement rule, it can be used on whole sentences or on subformulae.

%%%%%%%%%   		Practice problems for Section 6.1                %%%%

% rob: I put all the quantification problems from the original chapter 6 that don't involve identity or models in this section.

\practiceproblems

\setlength{\parindent}{0pt}

\problempart
\label{pr.justifyQLproof}
Provide a justification (rule and line numbers) for each line of proof that requires one.

\begin{enumerate}[label=\arabic*)]
\begin{multicols}{2}
\begin{minipage}{\linewidth}
\item \textcolor{white}{.} \\  %$\vdash \exists x Mx \eor \forall x\enot Mx$
\vspace{-24pt}
\begin{proof}
	\open
		\hypo{p1}{\enot (\exists x Mx \eor \forall x\enot Mx)}
		\have{p2}{\enot \exists x Mx \eand \enot \forall x\enot Mx}{}
		\have{p3}{\enot \exists x Mx}{}
		\have{p4}{\forall x\enot Mx}{}
		\have{p5}{\enot \forall x\enot Mx}{}
	\close
\have{n}{\exists x Mx \eor \forall x\enot Mx} {}
\end{proof}
\end{minipage}

\pagebreak[4]
\item \textcolor{white}{.} \\ %$\{\forall x(\exists y)(Rxy \eor Ryx),\forall x\enot Rmx\}\vdash\exists xRxm$
\vspace{-16pt}
\begin{proof}
\hypo{p1}{\forall x\exists y(Rxy \eor Ryx)}
\hypo{p2}{\forall x\enot Rmx}
\have{3}{\exists y(Rmy \eor Rym)}{}
	\open
		\hypo{a1}{Rma \eor Ram}
		\have{a2}{\enot Rma}{}
		\have{a3}{Ram}{}
		\have{a4}{\exists x Rxm}{}
	\close
\have{n}{\exists x Rxm} {}
\end{proof}

\vspace{2ex}

\item \textcolor{white}{.} \\ %$\{\forall x(\exists yLxy \eif \forall zLzx), Lab\} \vdash \forall xLxx$
\vspace{-16pt}
\begin{proof}
\hypo{1}{\forall x(\exists yLxy \eif \forall zLzx)}
\hypo{2}{Lab}
\have{3}{\exists y Lay \eif \forall zLza}{}
\have{4}{\exists y Lay} {}
\have{5}{\forall z Lza} {}
\have{6}{Lca}{}
\have{7}{\exists y Lcy \eif \forall zLzc}{}
\have{8}{\exists y Lcy}{}
\have{9}{\forall z Lzc}{}
\have{10}{Lcc}{}
\have{11}{\forall x Lxx}{}
\end{proof}

\item \textcolor{white}{.} \\ % $\{\forall x(Jx \eif Kx), \exists x\forall y Lxy, \forall x Jx\} \vdash \exists x(Kx \eand Lxx)$
\vspace{-16pt}
\begin{proof}
\hypo{a}{\forall x(Jx \eif Kx)}
\hypo{b}{\exists x\forall y Lxy}
\hypo{c}{\forall x Jx}
\have{d}{Ja}{}
\have{e}{Ja \eif Ka}{}
\have{f}{Ka}{}
\open
	\hypo{2}{\forall y Lay}
	\have{3}{Laa}{}
	\have{4}{Ka \eand Laa}{}
	\have{5}{\exists x(Kx \eand Lxx)}{}
\close
\have{j}{\exists x(Kx \eand Lxx)}{}
\end{proof}
\end{multicols}
\end{enumerate}

\problempart Without using the QN rule, prove $\enot\exists x\enot\script{A} \sststile{}{} \forall x\script{A}$

\problempart
\label{pr.someQLproofs}
Provide a proof of each claim.
\begin{earg}
\item $\sststile{}{}  \forall x Fx \eor \enot \forall x Fx$
\item $\{\forall x(Mx \eiff Nx), Ma\eand\exists x Rxa\}\sststile{}{}  \exists x Nx$
\item $\{\forall x(\enot Mx \eor Ljx), \forall x(Bx\eif Ljx), \forall x(Mx\eor Bx)\}\sststile{}{}  \forall xLjx$
\item $\forall x(Cx \eand Dt)\sststile{}{}  \forall xCx \eand Dt$
\item $\exists x(Cx \eor Dt)\sststile{}{}  \exists x Cx \eor Dt$
\end{earg}

\problempart
%Provide a proof of the argument about Billy on p.~\pageref{surgeon2}.

In the previous chapter (p.~\pageref{surgeon2}), we gave the following example

	\begin{quote}

	The hospital will only hire a skilled surgeon. All surgeons are greedy. Billy is a surgeon, but is not skilled. Therefore, Billy is greedy, but the hospital will not hire him.

	\begin{ekey}
	\item[UD:] people
	\item[Gx:] $x$ is greedy.
	\item[Hx:] The hospital will hire $x$.
	\item[Rx:] $x$ is a surgeon.
	\item[Kx:] $x$ is skilled.
	\item[b:] Billy
	\end{ekey}

	\begin{earg}
	\label{surgeon2}
	\item[] $\forall x\bigl[\enot (Rx \eand Kx) \eif \enot Hx\bigr]$
	\item[] $\forall x(Rx \eif Gx)$
	\item[] $Rb \eand \enot Kb$
	\item[\therefore] $Gb \eand \enot Hb$
	\end{earg}

	\end{quote}

Prove the symbolized argument.

\problempart \label{pr.BarbaraEtc.proof1} \iflabelexists{chap:catstatements}{On page \pageref{table:full_twentyfour} you were introduced to the twenty-four valid Aristotelian syllogisms, and on page \pageref{venn_proofs} you were able to show 15 of these valid using Venn diagrams.  Now that we have translated them into QL (see page \pageref{pr.BarbaraEtc}) we can actually prove all of them valid. In this section, you will prove the unconditional forms. I have omitted Datisi and Ferio because their proofs are trivial variations on Darii and Ferison.}{On page \pageref{pr.BarbaraEtc} you translated the basic categorical syllogisms studied by Aristotle and his followers into QL. Now you need to provide derivations for some of them.}

\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item \textbf{Barbara:} All $B$s are $C$s. All $A$s are $B$s.
	\therefore\  All $A$s are $C$s.
\item \textbf{Baroco:} All $C$s are $B$s. Some $A$ is not $B$.
	\therefore\  Some $A$ is not $C$.
\item \textbf{Bocardo:} Some $B$ is not $C$. All $B$s are $A$s.
	\therefore\  Some $A$ is not $C$.
\item\textbf{Celantes:} No $B$s are $C$s. All $A$s are $B$s.
	\therefore\  No $C$s are $A$s.
\item\textbf{Celarent:} No $B$s are $C$s. All $A$s are $B$s.
	\therefore\  No $A$s are $C$s.
\item\textbf{Campestres:} All $C$s are $B$s. No $A$s are $B$s.
	\therefore\  No $A$s are $C$s.
\item\textbf{Cesare:} No $C$s are $B$s. All $A$s are $B$s.
	\therefore\  No $A$s are $C$s.
\item\textbf{Dabitis:} All $B$s are $C$s. Some $A$ is $B$.
	\therefore\  Some $C$ is $A$.
\item\textbf{Darii:} All $B$s are $C$s. Some $A$ is $B$.
	\therefore\  Some $A$ is $C$.
\item\textbf{Disamis:} Some $B$ is $C$. All $B$s are $A$s.
	\therefore\  Some $A$ is $C$.
\item\textbf{Ferison:} No $B$s are $C$s. Some $B$ is $A$.
	\therefore\  Some $A$ is not $C$.
\item\textbf{Festino:} No $C$s are $B$s. Some $A$ is $B$.
	\therefore\  Some $A$ is not $C$.
\item\textbf{Frisesomorum:} Some $B$ is $C$. No $A$s are $B$s.
	\therefore\  Some $C$ is not $A$.
\end{enumerate}

\problempart
\label{pr.BarbaraEtc.proof2}
Now prove the conditionally valid syllogisms using QL. Symbolize each of the following and add the additional assumptions ``There is an $A$'' and ``There is a $B$.'' Then prove that the supplemented arguments forms are valid in QL. Calemos and Cesaro have been skipped because they are trivial variations of Camestros and Celaront.

\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]

\item\textbf{Barbari:} All $B$s are $C$s. All $A$s are $B$s.
	\therefore\  Some $A$ is $C$.
\item\textbf{Celaront:} No $B$s are $C$s. All $A$s are $B$s.
	\therefore\  Some $A$ is not $C$.
\item\textbf{Camestros:} All $C$s are $B$s. No $A$s are $B$s.
	\therefore\  Some $A$ is not $C$.
\item\textbf{Darapti:} All $A$s are $B$s. All $A$s are $C$s.
	\therefore\  Some $B$ is $C$.
\item\textbf{Felapton:} No $B$s are $C$s. All $A$s are $B$s.
	\therefore\  Some $A$ is not $C$.
\item\textbf{Baralipton:} All $B$s are $C$s. All $A$s are $B$s.
	\therefore\  Some $C$ is $A$.
\item\textbf{Fapesmo:} All $B$s are $C$s. No $A$s are $B$s.
	\therefore\  Some $C$ is not $A$.
\end{enumerate}

\problempart
Provide a proof of each claim.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item $\forall x \forall y Gxy \sststile{}{} \exists x Gxx$
\item $\forall x \forall y (Gxy \eif Gyx) \sststile{}{} \forall x\forall y (Gxy \eiff Gyx)$
\item $\{\forall x(Ax\eif Bx), \exists x Ax\} \sststile{}{} \exists x Bx$
\item $\{Na \eif \forall x(Mx \eiff Ma), Ma, \enot Mb\}\sststile{}{} \enot Na$
\item $\sststile{}{}\forall z (Pz \eor \enot Pz)$
\item $\sststile{}{}\forall x Rxx\eif \exists x \exists y Rxy$
\item $\sststile{}{}\forall y \exists x (Qy \eif Qx)$
\end{enumerate}

\problempart
Show that each pair of sentences is provably equivalent.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item $\forall x (Ax\eif \enot Bx) \nsststile{}{} \hspace{.5em} \sststile{}{}\enot\exists x(Ax \eand Bx)$
\item $\forall x (\enot Ax\eif Bd) \nsststile{}{} \hspace{.5em} \sststile{}{} \forall x Ax \eor Bd$
\item $\exists x Px \eif Qc \nsststile{}{} \hspace{.5em} \sststile{}{}\forall x (Px \eif Qc)$
%\item $Rca \eiff \forall x Rxa$, $\forall x(Rca \eiff Rxa)$  rob: I'm embarassed to say I can't solve this one.
\end{enumerate}

\problempart
Show that each of the following is provably inconsistent.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item \{$Sa\eif Tm$, $Tm \eif Sa$, $Tm \eand \enot Sa$\}
\item \{$\enot\exists x \exists y Lxy$, $Laa$\}
\item \{$\forall x(Px \eif Qx)$, $\forall z(Pz \eif Rz)$, $\forall y Py$, $\enot Qa \eand \enot Rb$\}
\end{enumerate}

\problempart
\label{pr.likes}
Write a symbolization key for the following argument, translate it, and prove it:
\begin{quote}
There is someone who likes everyone who likes everyone that he likes. Therefore, there is someone who likes himself.
\end{quote}

%\problempart
%Look back at Part \ref{pr.QLarguments} on p.~\pageref{pr.QLarguments}. For each argument: If it is valid in QL, give a proof. If it is invalid, construct a model to show that it is invalid.

\problempart
\label{pr.QLequivornot}
For each of the following pairs of sentences: If they are logically equivalent in QL, give proofs to show this. If they are not, construct a model to show this.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item $\forall x Px \eif Qc \nsststile{}{} \hspace{.5em} \sststile{}{}\forall x (Px \eif Qc)$
\item $\forall x Px \eand Qc\nsststile{}{} \hspace{.5em} \sststile{}{}\forall x (Px \eand Qc)$
\item $Qc \eor \exists x Qx\nsststile{}{} \hspace{.5em} \sststile{}{}\exists x (Qc \eor Qx)$
\item $\forall x\forall y \forall z Bxyz\nsststile{}{} \hspace{.5em} \sststile{}{}\forall x Bxxx$
\item $\forall x\forall y Dxy\nsststile{}{} \hspace{.5em} \sststile{}{}\forall y\forall x Dxy$
\item $\exists x\forall y Dxy\nsststile{}{} \hspace{.5em} \sststile{}{}\forall y\exists x Dxy$
\end{enumerate}

\problempart
\label{pr.QLvalidornot}
For each of the following arguments: If it is valid in QL, give a proof. If it is invalid, construct a model to show that it is invalid.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item $\forall x\exists y Rxy \sststile{}{} \exists y\forall x Rxy$
\item $\exists y\forall x Rxy \sststile{}{} \forall x\exists y Rxy$
\item $\exists x(Px \eand \enot Qx) \sststile{}{} \forall x(Px \eif \enot Qx)$
\item $\{\forall x(Sx \eif Ta)$, $Sd\} \sststile{}{}Ta$
\item $\{\forall x(Ax\eif Bx)$, $\forall x(Bx \eif Cx)\} \sststile{}{} \forall x(Ax \eif Cx)$
\item $\{\exists x(Dx \eor Ex)$, $\forall x(Dx \eif Fx)\} \sststile{}{} \exists x(Dx \eand Fx)$
\item $\forall x\forall y(Rxy \eor Ryx)\sststile{}{} Rjj$
\item $\exists x\exists y(Rxy \eor Ryx)\sststile{}{}Rjj$
\item $\{\forall x Px \eif \forall x Qx$, $\exists x \enot Px\}\sststile{}{}\exists x \enot Qx$
\item $\{\exists x Mx \eif \exists x Nx$, $\enot \exists x Nx\}\sststile{}{} \forall x \enot Mx$
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                         6.2 Rules for Identity %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Rules for Identity}
The identity predicate is not part of QL, but we add it when we need to symbolize certain sentences. For proofs involving identity, we add two rules of proof.

Suppose you know that many things that are true of $a$ are also true of $b$. For example: $Aa\eand Ab$, $Ba\eand Bb$, $\enot Ca\eand\enot Cb$, $Da\eand Db$, $\enot Ea\eand\enot Eb$, and so on. This would not be enough to justify the conclusion $a=b$. (See p.~\pageref{model.nonidentity}.) In general, there are no sentences that do not already contain the identity predicate that could justify the conclusion $a=b$. This means that the identity introduction rule will not justify $a=b$ or any other identity claim containing two different constants.

However, it is always true that $a=a$. In general, no premises are required in order to conclude that something is identical to itself. So this will be the identity introduction rule, abbreviated {=}I:

\begin{proof}
	\have[\ \,\,\,]{x}{\script{c}=\script{c}} \by{=I}{}
\end{proof}

Notice that the {=}I rule does not require referring to any prior lines of the proof. For any constant \script{c}, you can write $\script{c}=\script{c}$ on any point with only the {=}I rule as justification.

If you have shown that $a=b$, then anything that is true of $a$ must also be true of $b$. For any sentence with $a$ in it, you can replace some or all of the occurrences of $a$ with $b$ and produce an equivalent sentence. For example, if you already know $Raa$, then you are justified in concluding $Rab$, $Rba$, $Rbb$. Recall that $\script{A}[\script{a}||\script{b}]$ is the sentence produced by replacing \script{a} in \script{A} with \script{b}. This is not the same as a substitution instance, because \script{b} may replace some or all occurrences of \script{a}. The identity elimination rule ({=}E) justifies replacing terms with other terms that are identical to it:
\begin{proof}
	\have[m]{e}{\script{a}=\script{b}}
	\have[n]{a}{\script{A}}
	\have[\ ]{ea1}{\script{A}[\script{a}||\script{b}]} \by{=E}{e,a}
	\have[\ ]{ea2}{\script{A}[\script{b}||\script{a}]} \by{=E}{e,a}
\end{proof}

%The basic rules for conjunction can be valuable in a proof even if there are no conjunctions in any of the assumptions; the basic rules for disjunction can be used even if there are no disjunctions in any assumptions; and similarly for the other basic rules. The rules for identity are different, in that there must be an identity claim in some assumption in order for the rules to do any work. Other than the trivial identity that we can introduce with the {=}I rule

%do not apply we can now prove that identity is \emph{transitive}: If $a=b$ and $b=c$, then $a=c$. The proof proceeds in this way:
%\begin{proof}
%	\open
%		\hypo{p}{a=b \eand b=c}\by{want $a=c$}{}
%		\have{ab}{a=b}\ae{p}
%		\have{bc}{b=c}\ae{p}
%		\have{ac}{a=c}\by{{=}E}{ab,bc}
%	\close
%	\have{conc}{(a=b \eand b=c)\eif a=c} \ci{p-ac}
%\end{proof}

%As an example, consider this argument:
%\begin{quote}
%There is only one button in my pocket. There is a blue button in my pocket. Therefore, there is no button in my pocket that is not blue.
%\end{quote}
%We begin by defining a symbolization key:
%\begin{ekey}
%\item{UD:} buttons in my pocket
%\item{Bx:} $x$ is blue.
%\end{ekey}
%\begin{proof}
%	\hypo{one}{\forall x\forall y\ x=y}
%	\hypo{eb}{\exists x Bx} \by{want $\enot\exists x \enot Bx$}{}
%	\open
%		\hypo{be1}{Be}
%		\have{ef1}{e=f}\Ae{one}
%		\have{bf1}{Bf}\by{{=}E}{ef1,be1}
%	\close
%	\have{bf}{Bf}\Ee{eb,be1-bf1}
%	\have{ab}{\forall x Bx}\Ai{bf}
%	\have{nnab}{\enot\enot\forall x Bx}\by{DN}{ab}
%	\have{nenb}{\enot\exists x\enot Bx}\by{QN}{nnab}
%\end{proof}

To see the rules in action, consider this proof:
\begin{proof}
	\hypo{one}{\forall x\forall y\ x=y}
	\hypo{eb}{\exists x Bx}
	\hypo{Abnc}{\forall x(Bx \eif \enot Cx)}
		\by{want $\enot\exists x Cx$}{}
	\open
		\hypo{be1}{Be}
		\have{ef1}{\forall y\ e=y}\Ae{one}
		\have{ef2}{e=f}\Ae{ef1}
		\have{bf1}{Bf}\by{{=}E}{ef2,be1}
		\have{bnc1}{Bf\eif\enot Cf}\Ae{Abnc}
		\have{ncf1}{\enot Cf}\ce{bnc1,bf1}
	\close
	\have{cf}{\enot Cf}\Ee{eb,be1-ncf1}
	\have{Anc}{\forall x \enot Cx}\Ai{cf}
	\have{nEc}{\enot\exists x Cx}\by{QN}{Anc}
\end{proof}

%\section*{Summary of definitions}
%\begin{itemize}
%\item A sentence \script{A} is a \define{theorem} if and only if $\vdash\script{A}$.
%
%\item Two sentences \script{A} and \script{B} are \define{provably equivalent} if and only if $\script{A}\vdash\script{B}$ and $\script{B}\vdash\script{A}$.
%
%\item $\{\script{A}_1,\script{A}_2,\ldots\}$ is \define{provably inconsistent} if and only if, for some sentence \script{B}, $\{\script{A}_1,\script{A}_2,\ldots\}\vdash(\script{B} \eand \enot \script{B})$.
%\end{itemize}

\practiceproblems

\problempart
\label{pr.identity}
Provide a proof of each claim.
\begin{enumerate}[label=\arabic*), topsep=0pt, parsep=0pt, itemsep=3pt]
\item $\{Pa \eor Qb, Qb \eif b=c, \enot Pa\}\sststile{}{} Qc$
\item $\{m=n \eor n=o, An\}\sststile{}{} Am \eor Ao$
\item $\{\forall x x=m, Rma\}\sststile{}{} \exists x Rxx$
\item $\enot \exists x x \neq m \sststile{}{} \forall x\forall y (Px \eif Py)$
\item $\forall x\forall y(Rxy \eif x=y)\sststile{}{} Rab \eif Rba$
\item $\{\exists x Jx, \exists x \enot Jx\}\sststile{}{} \exists x \exists y x\neq y$
%\item $\{\forall x(x=n \eiff Mx), \forall x(Ox \eand \eor Mx)\}\sststile{}{} On$
\item $\{\exists x Dx, \forall x(x=p \eiff Dx)\}\sststile{}{} Dp$
\item $\{\exists x [(Kx \eand Bx) \eand \forall y(Ky \eif x=y)], Kd\}\sststile{}{} Bd$
\item $\sststile{}{} Pa \eif \forall x(Px \eor x \neq a)$
\end{enumerate}


\documentclass[]{aiaa-tc}% insert '[draft]' option to show overfull boxes
\usepackage{amsmath}

 \title{A Rigorous Specification for a Medium Scale Sequential Quadratic Programming (SQP) Algorithm\\DRAFT}

 \author{
  Steven P. Hughes
 }

\newcommand{\st}[1]{\begin{ttfamily}#1\end{ttfamily}}
 % Define commands to assure consistent treatment throughout document
 \newcommand{\eqnref}[1]{(\ref{#1})}
 \newcommand{\class}[1]{\texttt{#1}}
 \newcommand{\package}[1]{\texttt{#1}}
 \newcommand{\file}[1]{\texttt{#1}}

 \renewcommand{\thefigure}{\arabic{figure}}
\begin{document}

\maketitle

\begin{abstract}
   This document contains a rigorous mathematical and algorithmic specification for
   a medium scale SQP code.  The Quadratic Programming (QP) subproblem is solved using
   an active set method and the KKT system is solved using the null space method.  Matrix
   factorizations are performed using QR factorization.
\end{abstract}

\section{Quadratic Programming Subproblem}

\begin{eqnarray}
   \mbox{min} & & \frac{1}{2}\mathbf{x}^T\mathbf{G}\mathbf{x} + \mathbf{d}^T \mathbf{x}\\
   \mbox{subject to} & & a_i \geq b_i \hspace{.05 in} (i \in \mathcal{I})\\
     & & a_i = b_i \hspace{.05 in} (i \in \mathcal{E})
\end{eqnarray}

\begin{equation}
    \nabla_x f - \sum_i \lambda_i a_i = 0
\end{equation}

\section{Line Search Algorithm}

The merit function is given by
%
\begin{equation}
     \mathcal{L}(\mathbf{x},\boldsymbol{\lambda},\mathbf{s},\boldsymbol{\rho}) = f(\mathbf{x}) - \boldsymbol{\lambda}^T\left(\mathbf{c}(\mathbf{x}) - \mathbf{s}\right)
      + \frac{1}{2} \left(\mathbf{c}(\mathbf{x}) - \mathbf{s}\right)^T \mathbf{D}(\boldsymbol{\rho}) \left(\mathbf{c}(\mathbf{x}) - \mathbf{s}\right)
\end{equation}
%
where
%
\begin{equation}
     \mathbf{D}(\boldsymbol{\rho}) = \mbox{diag}(\boldsymbol{\rho})
\end{equation}
%
and
%
\begin{equation}
 s_i = \left\{
    \begin{array}{llcc}
        0                               & \mbox{if $i \in \mathcal{E}$}\\
        \mbox{max(0,$c_i(\mathbf{x})$)} & \mbox{if $i \in \mathcal{I}$ and $\rho = 0$}\\
        \mbox{max(0,$c_i(\mathbf{x}) - \lambda_i/\rho$)} & \mbox{otherwise}\\
    \end{array}
 \right.
\end{equation}
%
Define the search direction for the slack variables to be $q$ where
%
\begin{equation}
     q_k \equiv J_k p_k + c_k - s_k
\end{equation}
%
and the search direction for the Lagrange multiplier estimates to be
%
\begin{equation}
     \xi_k = \mu_k - \lambda_k
\end{equation}
%
The step in terms of $\alpha$ is then,
%
\begin{equation}
   \left(
   \begin{array}{ccc}
       x_{k+1}\\
       s_{k+1}\\
       \lambda_{k+1}
   \end{array}
   \right) =
   %
    \left(
    \begin{array}{ccc}
       x_{k}\\
       s_{k}\\
       \lambda_{k}
   \end{array}
   \right) +
      %
    \alpha  \left(
    \begin{array}{ccc}
       p_{k}\\
       q_{k}\\
       xi_{k}
   \end{array}
   \right)
\end{equation}
%
\begin{equation}
     \nabla \mathcal{L}\left( \mathbf{x}, \boldsymbol{\lambda}, \mathbf{s} \right) = \left(
     \begin{array}{c}
          \mathbf{g}  -  \mathbf{J}(\mathbf{x})^T \boldsymbol{\lambda} + \left(\mathbf{c}(\mathbf{x}) - \mathbf{s} \right)^T \mathbf{D}(\boldsymbol{\rho}) \mathbf{J}(\mathbf{x})\\
           -\left(\mathbf{c}(\mathbf{x}) - \mathbf{s} \right) \\
           \boldsymbol{\lambda} - \left(\mathbf{c}(\mathbf{x}) - \mathbf{s} \right)\mathbf{D}(\boldsymbol{\rho})
     \end{array}
     \right)
\end{equation}
%
\begin{equation}
     \phi'(\alpha) = \left[\mathbf{p}^T  \hspace{.05 in} \boldsymbol{\xi}^T \hspace{.05 in} \mathbf{q}^T \right]
      \nabla \mathcal{L}\left( \mathbf{x}+\alpha \mathbf{p}, \boldsymbol{\lambda}+\alpha \mathbf{q}, \mathbf{s} +\alpha \boldsymbol{\xi}\right)
\end{equation}
%
Define $\mathbf{r}$ such that $ \mathbf{r} \equiv \left( r_1,
r_2,...,r_m\right)$ where $r_i = (c_i - s_i)^2$.  We chan choose the
penalty parameters to minimize ...
%
\begin{equation}
    \boldsymbol{\rho}* = \frac{\theta}{\mathbf{r}^T \mathbf{r}} \mathbf{r}
\end{equation}
%
\begin{equation}
    \theta = \mathbf{g}^T\mathbf{p} + (2 \boldsymbol{\lambda} - \boldsymbol{\mu})^T(\mathbf{c} - \mathbf{s}) +
    \displaystyle\frac{1}{2}\mathbf{p}^T\mathbf{H}\mathbf{p}
\end{equation}
%
\begin{equation}
    \Delta_\rho
\end{equation}
%
\begin{equation}
 \bar{\rho}_i = \mbox{max}(\rho^*_i, \hat{\rho_i} ),  \hspace{.1 in} \mbox{where} \hspace{.1 in} \hat{\rho_i} = \left\{
    \begin{array}{llcc}
        \rho_i                                & \rho_i < 4(\rho_i^* + \Delta_\rho)  \\
        (\rho_i(\rho_i^* + \Delta \rho)^{1/2} & \mbox{otherwise}  \\
    \end{array}
 \right.
\end{equation}
%
The sufficient decrease conditions are
%
\begin{eqnarray}
    \phi(\alpha) - \phi(0) &\leq& \sigma \alpha \phi'(0)\\
    %
    | \phi'(\alpha) | & \leq & -\eta \phi'(0)
\end{eqnarray}
%
where $0 \leq \sigma \leq \eta \leq \frac{1}{2}$.

\end{document}


%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Problem Set/Assignment Template to be used by the
%% Food and Resource Economics Department - IFAS
%% University of Florida's graduates.
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Version 1.0 - November 2019
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Ariel Soto-Caro
%%  - asotocaro@ufl.edu
%%  - arielsotocaro@gmail.com
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[12pt]{article}
\usepackage{design_ASC}

\theoremstyle{definition}
\usepackage{longtable}
\newtheorem{exmp}{Example}[section]
\newtheorem{slo}{Definition}[section]
\newcommand*{\Perm}[2]{{}^{#1}\!P_{#2}}%
\newcommand*{\Comb}[2]{{}^{#1}C_{#2}}%
\setlength\parindent{0pt} %% Do not touch this
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
%% -----------------------------
%% TITLE
%% -----------------------------
\title{\textbf{Discrete Random variable}} %% Assignment Title

\author{\textbf{Ibrahim Abou Elenein}}

\date{\today} %% Change "\today" by another date manually
%% -----------------------------
%% -----------------------------

%% %%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\setlength{\droptitle}{-5em}
%% %%%%%%%%%%%%%%%%%%%%%%%%%
\maketitle

% --------------------------
% Start here
% --------------------------

% %%%%%%%%%%%%%%%%%%%
\section{Random Variable}
 A random variable is a quantity “X ” resulting from an experiment, by chance,
 that can assume different values.\\

 A random variable is a variable “X” that
 has a single numerical value determined by chance, for each outcome of a
 procedure.\\

 If a sample space S is discrete, then every R.V.  defined on S is
 also discrete, i.e., its range is countable (think of random counts for
 examples).
\section{Discrete Random Variable}
 A Discrete Random Variable is a variable that can assume only certain clearly
 separated values. \\

 A Discrete Random Variable has either a finite or countable
 number of values, where “countable” refers to the fact that there might be
 infinitely many values, but they can be associated with a counting process.
 \subsection{Examples of Discrete Random Variables}
 \begin{itemize}
     \item The outcome of rolling a single die.
     \item The number of boys in a family with three children.
     \item The number of heads that appear when a coin is flipped nine times.
     \item The sum of the numbers on the dice, when k dice are rolled.
     \item The number of bits received in error when n bits are received.
     \item The number of bits received until the r-th error.
 \end{itemize}
 \section{Discrete Probability Distributions}
A discrete probability distribution is a listing of
all possible values of a random variable along
with their probabilities. \\
\begin{equation}
    \displaystyle \frac{X}{P(X)} \frac{|x_1|}{|p_1|} \frac{|x_2|}{|p_2|} \frac{|x_2|}{|p_2|}
    \frac{|\dots|}{|\dots|} \frac{|x_k|}{|p_k|}; \  \ \  \sum _{k \geq 1} p_k = 1.
\end{equation}
\begin{enumerate}
    \item The sum of all probabilities must be 1in any probability distribution
    \item All probability values must be in [0,1]
\end{enumerate}
\begin{exmp}
    x $\Rightarrow$ The number of heads appearing when a coin is flipped three times.

\end{exmp}

% Please add the following required packages to your document preamble:
% Note: It may be necessary to compile the document several times to get a multi-page table to line up properly
\begin{longtable}[c]{lllll}
X    & 0   & 1   & 2   & 3   \\
\endfirsthead
%
\endhead
%
P(X) & 1/8 & 3/8 & 3/8 & 1/8
\end{longtable}

\subsection{Bernoulli}
A Bernoulli trail is an experiment with only two outcomes Success and Failure
\begin{enumerate}
    \item $P(S) = p = $ Probability of a success
    \item $P(F) = q = 1 - p = $ Probability of a failure
\end{enumerate}
If a Bernoulli random variable, X, denotes No. of successes, then
\begin{enumerate}
    \item $ X = 1$ if the outcome is success
    \item $ X = 0$ if the outcome is failure
\end{enumerate}
\begin{exmp}
A bit is transmitted, it is received in error with probability “0.1”.\\
Assume that 8 bits are transmitted independently.
\begin{enumerate}
    \item How many bits can be received in error?
        \begin{center}
            $\Perm{8}{2}$
        \end{center}
    \item What is the probability that 2 bits are received in error
        \begin{center}
            $P(X=2) = \Perm{8}{2} \times (0.1)^2 (0.9)^6  $
        \end{center}
\end{enumerate}
\end{exmp}
\subsection{Binomial Distribution}
In general, let X stand for the number of bits received in error, when $n$ bits are transmitted,
with the probability of a single bit in error being $p$
\begin{equation}
    P (X = k) = \Comb{n}{k} \times p^k \times (1-p)^{n - k}
\end{equation}

\begin{equation}
    \sum_{k = 0}^{n} p_k  = \Comb{n}{k} \times p^k \times (1-p)^{n - k} = [(p)+(1-p)]^n = 1 ^ n = 1
\end{equation}
\begin{exmp}
A fair coin is tossed 10 times, what is the probability of getting:
\begin{enumerate}
    \item  Exactly 6 heads.
        \begin{center}
            $ n = 10; \ \ p = 0.5; q = 0.5 $
        \end{center}
        \begin{center}
            $P(X = 6) = \Comb{10}{6} \times (0.5)^6 \times (0.5)^4   $
        \end{center}
    \item  At least 6 heads.
        \begin{center}
            $P(X \geq 6) = P(X = 6) + P(X = 7) + P(X = 8) + P(X = 9) + P(X = 10)  $
        \end{center}
\end{enumerate}
\end{exmp}

\subsection{Geometric Distribution}

A bit is transmitted, it is received in error with probability “0.1”.

Assume that bits are transmitted independently, until the first bit is received
in error.
\begin{enumerate}

    \item How many bits can be received?
        \begin{center}
            Infinity many many
        \end{center}
    \item What is the probability that the 5-th bit is received in error?
        \begin{center}
            $  P(X = 5) = 0.1 \times 0.9^4 $
        \end{center}
    \item What is the probability that at least 5 (i.e. 5 or more) bits are received until the first error
        \begin{center}
            $  P(X \geq 5) = P(X = 5) + P(X = 6) + \dots $  =
        \end{center}
        \begin{center}
            $  \displaystyle \sum_{k = 5}^{\infty} P(X = K) = \sum_{k = 5}^{\infty}(0.1)(0.9)^{k-1} = 0.656$
        \end{center}

\end{enumerate}
So The General case is
\begin{equation}
    P(X = k) = p \times (1 - p)^{k-1} ; \ \ \ k \geq 1
\end{equation}

\begin{equation}
    \displaystyle \sum_{k = 1}^{\infty}   P(X = k) = \sum_{k = 1}^{\infty}p \times (1 - p)^{k-1}
    = p\sum_{k = 1}^{\infty}(1 - p)^{k-1} = p \times \frac{1}{p} = 1
\end{equation}
\begin{exmp}
    Given that the first $k$ trials were Failures. Find the probability that $(k+1)$-th trial will be a Success. \\

    we need to find that $P(X = k + 1 | X > k)$
    \begin{center}
        $ \displaystyle  P(X = k + 1 | X > k) = \frac{P(X=k+1 \cap X>k)}{P(X>k)}
        = \frac{p \times (1-p)^k}{(1-p)^k} = p = p(X = 1)$  this called lack of memory where the probability
        of k + 1 is the same as the first
\end{center}
\end{exmp}
\subsection{Negative Binomial}
When a bit is transmitted, it is received in error with probability “0.1”.
Assume that bits are transmitted independently, until FOUR bits are received in error.
\begin{enumerate}
    \item At least, how many bits can be received ?
        \begin{center}
            at least 4 bits.
        \end{center}
    \item What is the probability that exactly 10 bits will be received
        \begin{center}
            $  P(X= 10) = (0.1)^9 \times \Comb{9}{3} \times (0.9)^6 \times (0.1)^3 $
        \end{center}
        In General, $P(X = k) = \Comb{k-1}{3} \times (1-p)^{k-4} \times p^4$

\end{enumerate}
\begin{slo}
 In general, let X stand for the number of bits
received until r bits are received in error, with
the probability of a single bit in error being p.
\end{slo}
\begin{equation}
    P(X = k) =  \underbrace{p^r}_{\text{last trial}}
    \ \ \underbrace{\Comb{k-1}{r-1} (1-p)^{k-r}}_{\text{$(r - 1)$success in$(k - 1)$trial}}; \ \ k \geq r.
\end{equation}
\begin{equation}
    \displaystyle \sum_{k=r}^{\infty} \Comb{k-1}{r-1} p^r(1-p)^{k-r} =
    \sum_{k' = 0}^{\infty} \Comb{k' + r -1}{k'} \ p^r (1-p)^{k'}
\end{equation}
given that $ \displaystyle \Comb{n}{k} = \Comb{n}{n- k}$

\begin{equation}
    \displaystyle \sum_{k' = 0}^{\infty} \Comb{k' + r -1}{k'} \ p^r (1-p)^{k'} =
    p^r \sum_{k'= 0}^{\infty}  \Comb{k' + r-1}{r-1}(1-p)^{k'}
\end{equation}
given that $ \displaystyle \frac{1}{(1-x)^r} = \sum_{k=0}^{\infty} \Comb{k+r-1}{r-1}x^r $
\begin{equation}
    \displaystyle p^r \sum_{k'= 0}^{\infty}  \Comb{k' + r-1}{r-1}(1-p)^{k'} = p^r * \frac{1}{(1-(1-p))^r} = 1
\end{equation}
\section{Notes}
\begin{enumerate}
    \item Recall that a binomial random variable is a count of the number
    of successes in n Bernoulli trials. That is, the number of trials n is
    predetermined, and the number of successes represents the random variable X.
    \item  A negative binomial random variable is a count of the number of
trials required to obtain r successes. That is, the number of
successes r is predetermined, and the number of trials (n or k)
represents the random variable X.
\end{enumerate}
\end{document}