% math.tex
%
% math topics
%
% Samuel Rohrer

\subsection{Prerequisites}
  \subsubsection{Linear Algebra}
  \subsubsection{Calculus}
  \subsubsection{Statistics}

\subsection{Eigenvalues, Singular Value Decomposition}

\subsection{Moore Penrose Psuedoinverse}

\subsection{Expectation, Variance, Covariance}

\subsection{Conditional Probability, Bayes Rule}

\subsection{A/B testing, hypothesis testing}

\subsection{Statistical measures: p-values, t-tests, ANOVA tests, correlation
              coefficients}

\subsection{Probability Distributions}

\subsection{Overflow, underflow, floating point numbers}

\subsection{Matrix trace, Poor conditioning of matrices}

\subsection{Gradient Based Optimization}
  \subsubsection{Gradient Descent}
  \subsubsection{Nesterov Accelerated Gradient Descent}
  \subsubsection{Choosing a gradient based optimizer}

\subsection{Constrained optimization}

\subsection{Stochastic gradient descent}

\subsection{Overfitting}

\subsection{Regularization}

\subsection{Preconditioner}

\subsection{Quantization, Pruning}


\chapter{ODE group}


%----------------------------------------------------------------------------------------
% Conclusion
%----------------------------------------------------------------------------------------

\section{Conclusion}

Throughout this paper, we've discussed the importance of randomization-based inference in statistics, the situations that this method is best attuned to, and most importantly, why this method works for so many statistical hypothesis tests. We've also briefly touched on Monte Carlo Simulations and the importance of these methods in performing accurate randomization-based inference. Randomization-based inference is a powerful non-parametric tool which specifies that we only check one condition: randomness. This creates an extremely versatile process which can be implemented in situations where other model conditions cannot be met in some cases. As with many topics in statistics, we learned that the utility for randomization-based inference derived from the Central Limit Theorem.

{\color{white}\cite{paradoxPaper}\cite{bayesianAnalysis}\cite{hesterbergTextbook}\cite{howDoPermTestsWork}\cite{monteCarloIntro}\cite{randomizationProcedures}\cite{simpleMonteCarlo}\cite{whatTeachersShouldKnow}\cite{wildBootstrap}}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{hyperref}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}

\usepackage{graphicx}

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}m{#1}}

\usepackage{float}

\setlength{\parindent}{0cm}

\title{Udacity Reacher Project}
\author{Thomas Lecat}
\date{April 2021}

\begin{document}

    \maketitle

    \section{Environment}\label{sec:environment}

    The environment is described as Markov Decision Process $(S, A, R, p, \gamma)$ with unknown dynamics.
    The goal is to approximate the optimal policy $\pi^*$ that maximizes the upcoming cumulative reward in every state.

    The action space $A$ is continuous and consists in 4 actions in the $[-1, 1]$ range.

    20 agents run independently in parallel on the environment.

    \section{Agent}\label{sec:agent}

    \subsection{DDPG}\label{subsec:ddpg}

    A \href{https://arxiv.org/abs/1509.02971}{DDPG} agent
    is implemented as the base solution.

    DDPG uses two distinct neural networks: \\

    A "critic" network parameterized by $\theta^Q$ seek to approximate the optimal $Q$ value $Q^*$, defined as:
    \[
        Q^*(s, a) = \max_{\pi} \mathbb{E}_{\pi} (\sum_{t} \gamma^t r_t | S_0=s, A_0=a),  \forall (a, s) \in A \times S
    \]
    The network takes the current observation and action as input and outputs a single value.\\

    A deterministic "actor" network parameterized by $\theta^\mu$ that approximates the function $\argmax_a{Q(\cdot|s)}$.
    It takes the current observation as input.\\

    The loss function of the critic is defined as the mean squared TD error of the Bellman optimality equation:
    \[
        \mathcal{L}_{critic}(\theta^Q) = \mathbb{E}_{s, a, r, s'} \big[((r + \gamma Q(s', \mu'(s'| \theta^{\mu'})| \theta^{Q'}) - Q(s, a| \theta^Q))^2\big]
    \]
    where $\theta^{Q'}$ and $\theta^{\mu'}$ are the parameters of target networks which are slowly updated towards their respective local networks at every training step.
    The target networks makes the TD target $r + \gamma Q(s', \mu'(s'| \theta^{\mu'})| \theta^{Q'})$ independent from the parameters $\theta^Q$,
    which stabilizes the training.\\

    The loss function of the actor strives to maximise the $Q$ value:
    \[
        \mathcal{L}_{actor}(\theta^{\mu}) = - \mathbb{E}_{s} Q(s, \mu(s | \theta^\mu) | \theta^Q)
    \]\\

    The losses are minimized by Adam optimizers, a variant of the Stochastic Gradient Descent algorithm.
    At each step, the gradient is approximated from a minibatch of experiences ${(s, a, r, s', d)_i}$.
    To avoid a strong correlation between the steps of the minibatch, transitions are stored in a large replay buffer
    during rollouts and then sampled uniformly.

    \section{Hyperparemeters}\label{sec:hyperparemeters}

    The actor network is composed of 3 fully connected layers, with respectively 128, 128 and 64 neurons each,
    with a batch normalisation layer between the first two fully connected.
    The actor optimizer is Adam with a learning rate of $10^{-4}$.

    The critic network is composed of 3 fully connected layers, with respectively 128, 128 and 64 neurons each,
    with a batch normalisation layer between the first two fully connected.
    The output of the first layer and the action are concatenated before flowing into the second layer.
    The critic optimizer is Adam with a learning rate of $10^{-3}$.\\

    To increase exploration, random noise created by an Ornstein-Uhlenbeck process is added to the actions during rollouts.\\

    The table below summarizes the main hyperparameters:

    \begin{tabular}{ |p{5cm} C{1cm}|p{3cm}| }
        \hline
        \multicolumn{3}{|c|}{Hyperparameters} \\
        \hline
        actor learning rate                & $\alpha^\mu$ & 1e-04     \\
        \hline
        critic learning rate               & $\alpha^Q$   & 1e-03     \\
        \hline
        discount factor                    & $\gamma$     & 0.99      \\
        \hline
        target networks update coefficient & $\tau$       & 0.001     \\
        \hline
        buffer size                        &              & 1,000,000 \\
        \hline
        batch size                         &              & 128       \\
        \hline
        critic weight decay                &              & 1e-04     \\
        \hline
        Update frequency (number of steps) &              & 20        \\
        \hline
        Number of Adam steps per update    &              & 10        \\
        \hline

    \end{tabular}

    \section{Results}\label{sec:results}

    We plot the mean reward per agent per episode during training:

    \begin{figure}[H]
        \centering
        \includegraphics[scale=0.5]{results/mean_reward_per_episode.png}\label{fig:figure}
    \end{figure}

    The environment is solves in less than 400 iterations.

    \section{Next steps}\label{sec:next-steps}

    The next steps for the project consist in implemented other RL agents for continuous control, such as
    \href{https://openai.com/blog/openai-baselines-ppo/}{PPO} or \href{https://arxiv.org/abs/1801.01290}{SAC}.

    It would also be interesting to discretize the action space and compare a vanilla DQN agent
    with continuous control agents.

    Hyperparemeters could also be further tuned using strategies such as
    \href{https://github.com/hyperopt/hyperopt}{HyperOpt} or
    \href{https://deepmind.com/blog/article/population-based-training-neural-networks}{Population Based Training}.

\end{document}


\documentclass[main.tex]{subfiles}
\begin{document}

\section*{Thu Nov 21 2019}

Let's start the show.

The effective potential equation is
%
\begin{align}
  \frac{1}{2} \qty(\dv{r}{t})^2 + V _{\text{eff}} (r)
  = E = \frac{e^2-1}{2}
\,,
\end{align}
%
where
%
\begin{align}
  V _{\text{eff}}(r)
  = - \frac{GM}{r} + \frac{l^2}{2r^2} - \frac{GMl^2}{r^3}
\,,
\end{align}
%
and the timelike Killing vector gives us \(e\), while the azimuthal Killing vector gives us \(l\).

We can satisfy the equation with \(V _{\text{eff}} \equiv E\), and \(\dv*{V _{\text{eff}}}{r} = 0\). These are the equations characterizing circular orbits; they are, explicitly,
%
\begin{align} \label{eq:effective-potential}
  -\frac{GM}{l} + \frac{l^2}{2r^2} - \frac{GMl^2}{r^3} = \frac{e^2-1}{2}
\,
\end{align}
%
and
%
\begin{align} \label{eq:stationary-potential}
  \frac{GM}{r^2} - \frac{l^2}{r^3} + \frac{3GMl^2}{r^{4}}=0
\,.
\end{align}
%

Let us compute \(\text{eq}\)\eqref{eq:effective-potential} \( + r \qty(1 - r/2GM) \text{eq}\)\eqref{eq:stationary-potential}: after some algebra, we get
%
\begin{align}
  \frac{l}{e} = \sqrt{GMr} \qty(1 - \frac{2GM}{r})^{-1}
\,.
\end{align}

The angular velocity \(\Omega \) is defined as a derivative with respect to coordinate time:
%
\begin{align}
  \Omega = \dv{\varphi }{t}
\,,
\end{align}
%
so
%
\begin{align} \label{eq:omega-geodesic}
  \Omega = \frac{\dv*{\varphi }{\tau }}{\dv*{t}{\tau }}
  = \frac{l/r^2}{e \qty(1 - \frac{2GM}{r})^{-1}}
  = \frac{\sqrt{GMr} \qty(1- \frac{2GM}{r})^{-1} \frac{1}{r^2}}{\qty(1 - \frac{2GM}{r})^{-1}} = \frac{\sqrt{GMr}}{r^2} = \sqrt{\frac{GM}{r^3}}
\,,
\end{align}
%
so \(\Omega^2= GM/ r^3\) or \(\Omega^2 r = GM / r^2\), a relation which is the same as in Newtonian physics.

Now, review of the equation for \(u = r^{-1}\). When we perturbed it, we got a harmonic oscillator. Our equation then is solved by cosinusoidal waves \(u = u_c (1+\cos(\omega \varphi ))\), with \(\omega = \sqrt{1 - 6GMu_c}\):
%
\begin{align}
  r(\varphi) = \frac{r_C}{1 + A \cos(\sqrt{1 - \frac{6GM}{r_c}}\varphi )}
\,,
\end{align}
%
so \(\Delta \varphi \) in one orbit is such that the cosine's argument progresses by \(2 \pi \):
%
\begin{align}
2 \pi = \Delta \varphi \times \sqrt{1 - \frac{6GM}{r_c}}
\,,
\end{align}
%
which means
%
\begin{align}
  \Delta \varphi = \frac{2 \pi}{\sqrt{1 - \frac{6GM}{r_c}}}
  \approx
  2 \pi \qty(1 + \frac{3GM}{r_c})
\,,
\end{align}
%
and then \(\delta \varphi  = 6 \pi GM / r_c\). But \(r_c = l^2/ GM\): so we get
%
\begin{align}
  \delta \varphi = 6 \pi \qty(\frac{GM}{l})^2
\,.
\end{align}
%

We can plug numbers into this: we know that \(G = \SI{6.67408e-11}{m^3 kg^{-1} s^{-2}}\), and our \(l\) has units of \(\SI{}{m^2s^{-1}}\), so we need to insert a \(c\): the calculation then is
%
\begin{align}
  \delta \varphi = 6 \pi \times \frac{180 \times 3600}{\pi }  \qty(\frac{G M_{\odot}}{r _{\text{merc}} v _{\text{merc}} c}) \times \frac{\SI{100}{yr}}{\SI{.241}{yr}} \approx 43''
\,,
\end{align}
%
where the radius and velocity of the orbit of Mercury must be \emph{both} calculated at the perihelion, or at the aphelion, or one can take the average velocity and the semimajor axis of the ellipse.
The factor \(\SI{100}{yr} / \SI{.241}{yr}\) was inserted to account for the number of orbits of Mercury in a century.
The angular momentum is calculated as \(r^2 \dot{\varphi} = r (r \dot{\varphi}) = rv_{\perp}\) at a certain point.

\begin{lstlisting}[language=Python]
from scipy.constants import G, c, pi

sun_mass = 2e30
mercury_orbital_velocity = 4.7e4
mercury_semimajor_axis = 57.9e9
mercury_angular_momentum = mercury_orbital_velocity * mercury_semimajor_axis
mercury_period_yr = .241
rad2arcsec = 3600*180/pi

delta_phi = 6*pi*(G*sun_mass/mercury_angular_momentum/c)**2

print(delta_phi * rad2arcsec * 100/mercury_period_yr)
\end{lstlisting}

\subsection{Radial infall}

We treat motion with \(l=0\) of an object which is at rest at infinity.

The equation becomes
%
\begin{align}
  \frac{1}{2} \qty(\dv{r}{\tau })^2 - \frac{GM}{r} = \frac{e^2 - 1}{2}
\,,
\end{align}
%
and if the object is at rest at infinity we can calculate \(e\) for \(r \rightarrow \infty\):
%
\begin{align}
  e = \qty(1 - \frac{2GM}{r}) \dv{t}{\tau } = 1
\,.
\end{align}

Then, our equation is
%
\begin{align}
\frac{1}{2} \qty(\dv{r}{\tau })^2 - \frac{GM}{r} =0
\,.
\end{align}
%

The time component of the velocity is
%
\begin{align}
  u^{t} = \dv{t}{\tau } = \frac{e}{1 - \frac{2GM}{r}} = \frac{1}{1 - \frac{2GM}{r}}
\,,
\end{align}
%
the radial component is given by our equation of motion:
%
\begin{align}
  \dv{r}{\tau } = \pm \sqrt{\frac{2GM}{r}} = - \sqrt{\frac{2GM}{r}}
\,,
\end{align}
%
where we choose the minus sign since we are falling in. The components \(v^{\theta }\) and \(v^{\varphi }\) are zero since the motion is radial.

Let us compute \(\vec{u} \cdot \vec{u}\): we get
%
\begin{subequations}
\begin{align}
  g_{00} \qty(u^{0})^2 + g_{11} \qty(u^{1})^2
  &= - \qty(1 - \frac{2GM}{r})\qty(1 - \frac{2GM}{r})^{-2}
  + \qty(1 - \frac{2GM}{r})^{-1} \frac{2GM}{r}  \\
  &= \qty(1 - \frac{2GM}{r})^{-1} \qty(-1 + \frac{2GM}{r})= -1
\,.
\end{align}
\end{subequations}
%

We can integrate
%
\begin{align}
  \dv{r}{\tau } = - \sqrt{\frac{2GM}{r}}
\,,
\end{align}
%
we get
%
\begin{align}
  \int _{0}^{r} \sqrt{r} \dd{r} =- \int _{0}^{\tau } \sqrt{2GM} \dd{\tau }
\,,
\end{align}
%
so
%
\begin{align}
  r(\tau ) = \qty(\frac{3}{2})^{2/3} \sqrt[3]{2GM} \sqrt[3]{- \tau }
\,.
\end{align}

Do keep in mind that we set \(\tau =0\) at \(r=0\).
It seems like nothing bad really happens at \(r=2GM\).
This solution only makes sense for negative \(\tau \).

At \(r = 2GM\), a finite \(\Delta \tau \) corresponds to an infinite \(\Delta t\) since \(\dv*{t}{\tau }\) diverges  there.

So, the coordinates \(t\) and \(r\) seem like a bad choice at the horizon: they vary infinitely for a finite time as measured in the frame of the infalling particle.

The horizon is a \emph{coordinate singularity}.

The 4-velocity for a particle going \emph{outward} is:
%
\begin{subequations}
\begin{align}
  u^{\alpha } = \left[\begin{array}{c}
    \qty(1 - \frac{2GM}{r})^{-1} \\
  + \sqrt{\frac{2GM}{r}} \\
  0 \\
  0
  \end{array}\right]
\,,
\end{align}
\end{subequations}
%
\emph{if it reaches radial infinity at rest}.

What is the escape velocity? What is  the three-velocity as measured by an observer at rest at constant \(r\)?

The 4-momentum of the particle is \(p^{\alpha } = m u^{\alpha }\), while the observer's 4-velocity will be
%
\begin{subequations}
\begin{align}
  u _{\text{obs}}^{\alpha } = \left[\begin{array}{c}
  \frac{1}{\sqrt{-g_{00} }} \\
  0 \\
  0 \\
  0
  \end{array}\right]
\,.
\end{align}
\end{subequations}

The energy measured by the observer is \(E = - \vec{p} \cdot \vec{u}_{\text{obs}}\). It is
%
\begin{align}
  E = - g_{00} u^{0} _{\text{obs}} p^{0}
  = \qty(1 - \frac{2GM}{r}) \qty(1 - \frac{2GM}{r})^{-1/2}
  m \qty(1 - \frac{2GM}{r})^{-1}
  = \frac{m}{\sqrt{1 - \frac{2GM}{r}}}
\,.
\end{align}
%

However, any observer sees \(E = m \gamma \), so they identify \(\gamma = 1 / \sqrt{1 - \frac{2GM}{r}}\).

Then, the escape velocity is \(\sqrt{\frac{2GM}{r}}\), just like the Newtonian case.

\subsubsection{Gravitational light deflection}

Now, we will look at the motion of light: by how much is it deflected?

We still have the Killing vector \(\xi^{\mu } = (1, \vec{0})\): so
%
\begin{align}
  - \xi \cdot u = e = \qty(1 - \frac{2GM}{r}) \dv{t}{\lambda }
\,
\end{align}
%
is constant, and from \(\xi^{\mu }= (\vec{0}, 1)\) we have
%
\begin{align}
  \xi \cdot u =  l =
  r^2 \dv{\varphi }{\lambda }
\,,
\end{align}
%
where \(\lambda \) is the parameter of the light trajectory, and we set \(\theta = \pi /2\).

The square modulus of the light's velocity is zero, so
%
\begin{align}
  0 =
  - \qty(1 - \frac{2GM}{r}) \qty(\frac{e}{1-\frac{2GM}{r}})^2
  + \qty(1 - \frac{2GM}{r})^{-1} \qty(\dv{r}{\lambda })^2 + r^2 \qty(\frac{l}{r^2})
\,.
\end{align}

This can be rewritten as
%
\begin{align}
  0= - \frac{e^2}{l^2} + \frac{1}{l^2} \qty(\dv{r}{\lambda })^2 + \frac{1}{r^2} \qty(1 - \frac{2GM}{r})
\,,
\end{align}
%
or
%
\begin{align}
  \frac{1}{l^2} \qty(\dv{r}{\lambda })^2 + W _{\text{eff}}(r) = \frac{1}{b^2}
\,,
\end{align}
%
where \(b^2 = l^2 / e^2\) and
%
\begin{align}
  W _{\text{eff}} (r) = \frac{1}{r^2} \qty(1 - \frac{2GM}{r})
\,.
\end{align}

A note: \(\lambda \) is a parameter, but we can do affine transformations to it: \(\lambda \rightarrow a \lambda + b\). Actually we can do any \(\lambda' = \lambda' (\lambda )\) which is monotonic.
We can check that all physical quantities which appear in the equation are invariant, since all the factors \(\dv*{\lambda ' }{\lambda }\) cancel.

Also, the equation is even in \(l\), since it must be invariant under \(\varphi \rightarrow - \varphi \) (which corresponds to \(P\) symmetry).
Then, we choose \(l\) to be positive as our gauge.

At \(r = 3GM\) we have a maximum of \(W _{\text{eff}}\), which attains the value \(1 / 27 G^2M^2\) there.
Since it is a maximum, the orbit is unstable.

If a photon approaches the BH with energy \(1/ b^2\) larger than \(W _{\text{eff}}(3GM)\) then it ``bounces back'': \(r\) decreases, there is a minimum \(r\) and then \(r\) increases.

The critical equation is \(b^{-2} > (27 G^2M^2)\), which means \(l > \sqrt{27} GM e\). It is actually a critical \emph{angular momentum}.

\end{document}


\subsection{Tabling}
You can also use tabling in inference to speed up the computation and/or avoid loops, see
the \href{http://www.swi-prolog.org/pldoc/man?section=tabling}{SWI-Prolog manual}.

To do so you have to use the \verb|tabling| library module and declare some of the predicates
as tabled. The tabling declarations go after the \verb|:-pita.| or \verb|:- mc.| directives.

For example, to compute the probability of paths in undirected graphs you can use
the program (\href{http://cplint.eu/e/path_tabling.swinb}{\texttt{path\_tabling.swinb}})
\begin{verbatim}
:- use_module(library(pita)).
:- use_module(library(tabling)).
:- pita.
:- table path/2.
:- begin_lpad.
path(X,X).
path(X,Y):-
  path(X,Z),edge(Z,Y).
edge(X,Y):-arc(X,Y).
edge(X,Y):-arc(Y,X).
arc(a,b):0.2.
arc(b,e):0.5.
arc(a,c):0.3.
arc(c,d):0.4.
arc(d,e):0.4.
arc(a,e):0.1.
:- end_lpad.
\end{verbatim}
Then you can compute the probability that \verb|a| and \verb|e| are connected with
\begin{verbatim}
prob(path(a,e),Prob).
\end{verbatim}
This programs has loops so if you run the above query without tabling \verb|pita| would loop forever.

You can use tabling with both \verb|pita| and \verb|mcintyre|.


\chapter{Logic II}

\epigraph{Contrariwise, if it was so, it might be; and if it were so, it would be; but as it isn't, it ain't. That's logic.}{Lewis Carroll}

Okay, for people who like videos. \href{https://www.youtube.com/watch?v=cDRqud3q3Fg}{Bian McLogan} has an explination of converse, inverse, and contrapositive. \href{https://www.youtube.com/watch?v=tKnS3s8fOu4}{shaunteaches} has an overview of De Morgan's Laws. \href{https://www.youtube.com/watch?v=YBNrkXEqCiI}{Alicia Gray} has a video on logical arguments. \href{https://www.youtube.com/watch?v=N6_Q1kcXiIg}{Math and Stats Help} has a video on Euler digrams.

Last time we established the basicis of logic using truth tables. We know that basic logical operators, and we can resolve statements using truth tables. This time we will look at how we can apply logical laws to statements, and see their application in circuits.

\section{Converse, Inverse, and Contrapositive of implication}

For a few seconds in class, we ``learned'' in the inverse of implication (incorrectly called ``inverse of a statement''), but I want to go into that in some more detail. Given an implication (if $P$ then $Q$), we can form the {\bf converse}, {\bf inverse}, and {\bf contrapositive}.

\begin{boxdefine}{Converse, Inverse, and Contrapositive of Implication}{}
	Given the statement $P \implies Q$ (if $P$, then $Q$), the following can be defined.
	\begin{itemize}
		\item The {\bf converse} is $Q \implies P$ (if $Q$, then $P$).
		\item The {\bf inverse} is $\lnot P \implies \lnot Q$ (if not $P$, then not $Q$).
		\item The {\bf contrapositive} is $\lnot Q \implies \lnot P$ (if not $Q$, then not $P$).
	\end{itemize}
\end{boxdefine}

Generally I think its better to think about logic in English rather then symbolically. Even if the symbols above look confusing to you, take a look at the examples below in English.

\begin{boxexample}{}{}
	Suppose our implication is ``if you write fanfic, then you're cool.'' Then we have

	\begin{itemize}
		\item The converse is ``if you're cool, you write fanfic.''
		\item The inverse is ``if you don't write fanfic, then you're not cool.''
		\item The contrapositive is ``if you're not cool, you don't write fanfic.''
	\end{itemize}
\end{boxexample}

Keep in mind that the converse and inverse aren't nessesarily correct. However, if the statement is correct then the contrapositive is equivalent.

\begin{boxproposition}{Contrapositive of inverse}{}
	Given an implication is true, then the contrapositive is also true, denoted by
	\[
		(A \implies B) \iff (\lnot B \implies \lnot A)
	\]
\end{boxproposition}

We spent 7 seconds on this in class, so I assume its not too important. At the very least, make sure you know how to do inverse of implication. I'm not sure if we'll need to do converse and contrapositive, though they are typically taught togeather and closely related.

\section{De Morgan's Laws}

De Morgan's laws allows the experession of conjunctons and disjunctions purely in terms of each other via negation. Phew, thats a lot of words. Basically, you can use the following laws to simplify negations.

\begin{boxproposition}{De Morgan's Laws}{}
	\[
		\lnot (A \lor B) \iff \lnot A \land \lnot B
	\]
	\[
		\lnot (A \land B) \iff \lnot A \lor \lnot B
	\]
\end{boxproposition}

Again, since this is logic, I recommend considering this in English to understand whats going on. For example,

\begin{boxexample}{}{}
	\begin{itemize}
		\item What is the negation of ``gaming or studying'' Answer: ``Not gaming and not studying.''
		\item What is the negation of ``drinking and driving?'' Answer: ``Not driving or not drinking.''
	\end{itemize}
\end{boxexample}

\section{Arguments in Classical Logic}

Instead of using truth tables to determine that valididity of statements, we can use {\bf arguments}. When we make an argument, we assume some premise and reach a conclusion.

\begin{boxdefine}{Laws of Detachment, Contraposition, Syllogism, and Disjunctive Syllogism}{}
	Here is a list of some true arguments.

	\begin{itemize}
		\item {\bf Law of Detachment}: If $A$ implies $B$, and $A$ is true, then $B$ must be true. Denoted by $(A \implies B) \land A \implies B.$
		\item {\bf Law of Contraposition}: If $A$ implies $B$, and $B$ is false, then $A$ must be false. Denoted by $(A \implies B) \land \lnot B \implies \lnot A.$
		\item {\bf Law of Syllogism}: If $A$ implies $B$, and $B$ implies $C$, then $A$ implies $C$. Denoted by $(A \implies B) \land (B \implies C) \implies (A \implies C).$
		\item {\bf Law of Disjunctive Syllogism}: If $A$ or $B$ are true, but $A$ is false, then $B$ is true. Denoted by $(A \lor B) \land \lnot A \implies B$.
	\end{itemize}
\end{boxdefine}

The notes go onto prove these with truth tables. I think the proofs are too easy though, and that some examples of these in \cancel{English} Minecraft are more useful to you.

\begin{boxexample}{}{}
	\begin{itemize}
		\item {\bf Law of Detachment}: If a creeper destroys my base, then I will build a new base. A Creeper destoyed my base. Therefor, I will build a new base.
		\item {\bf Law of Contraposition}: If a creeper destroys my base, then I will build a new base. I did not build a new base. Therefor, a creeper has not destroyed my base.
		\item {\bf Law of Syllogism}: If a creeper destorys my base, then I will build a new base. If I build a new base, I will collect stone. Therefor, if a creeper destroys my base, I will collect stone.
		\item {\bf Law of Disjunctive Syllogism}: I will build underwater or underground. I am not building underwater. Therefor, I am building underground.
	\end{itemize}
\end{boxexample}

An invalid agrument is called a {\bf fallacy}. It seems for this course we're only going to be using two fallacies.

\begin{boxdefine}{Fallacy of Converse and Inverse}{}
	\begin{itemize}
		\item {\bf Fallacy of Converse}: If $A$ implies $B$, and $B$ is true, then $A$ is true. Denoted by $(A \implies B) \land B \implies A$.
		\item {\bf Fallacy of Inverse}: If $A$ implies $B$, and $A$ is fase, then $A$ is false. Denoted by $(A \implies B) \land \lnot A \implies \lnot B$.
	\end{itemize}
\end{boxdefine}

Tired of this yet? Here's a few more examples of these.

\begin{boxexample}{}{}
	Some examples of fallacies (or invalid arguments) are

	\begin{itemize}
		\item {\bf Fallacy of Converse}: If I am in a cave, I will use a tourch. I am using a torch. Therefor, I am in a cave.
		\item {\bf Fallacy of Inverse}: If I am in a cave, I will use a torch. I am not in a cave. Therefor, I am not using a tourch.
	\end{itemize}
\end{boxexample}

\begin{boxremark*}{}{}
	The logic we're currently doing is called {\bf Propositional Logic}. But there are other systems of logic. For example {\bf Predicate Logic} (also called first-order logic) is the most popular logic system for mathematics. Predicate logic defines two more symbols: ``$\forall$'' meaning ``for all'' and ``$\exists$'' meaning ``there exists.'' For example,
	\[
		\forall x \in \mathbb{R}, \exists y \in \mathbb{R} \ni xy = 1
	\]
	reads as ``for all real numbers $x$, there exists real number $y$, such that $ x\cdot y=1.$ There is one other important thing that you might want to know about logic, especially if you're interested in Artificial Intellegence. The systems presented to you in this course are languages. Languages alone aren't too useful. What mathematitions do to make logic useful is define {\bf axioms} (arguments defined to be true) in order to make {\bf deductions}. The laws above are a very weak form of axiom system. A more complete system would be the {\bf Hilbert system}. The first {\bf G\"odel's incompleteness theorem} places restrictions on how complete a consistent system may be.
\end{boxremark*}

\section{Euler Diagrams}

Euler diagrams can be used to express relations between things. For example, if you ever wondered what how the British Isles are classified, look at this interesting CC0 digram I found on Wikipedia.

\includegraphics[scale=0.5]{euler_diagram}

Euler diagrams can also be used to explain logical relations. These are sort of hard to explain, but take a look at the examples below.

\begin{boxexample}{}{}
	Determine if this logic is valid. All students drink alcohol. Scott drinks alcohol. Therefor, Scott is a student.

	Solution: We know that Students drink Alcohol, so we put students inside of an Alcohol circle. We know that Scott drinks alcohol, so we put Scott inside of the alcohol circle along side the students. Thus our diagram looks like this:

	\begin{tikzpicture}
		\draw (0,0) ellipse (3 and 2);
		\node at (-.5,1.5) {Alcohol};
		\draw (1.5,0) circle (1);
		\node at (1.5,0) {Students};
		\draw (-2, 0) circle (0.5);
		\node at (-2,0) {Scott};
	\end{tikzpicture}

	This logic is invalid.
\end{boxexample}

\begin{boxexample}{}{}
	Determine if this logic is valid. All salespeople are annoying. Kelly is a salesperson. Therefor, Kelly is annoying.

	Solution: We know that salespeople are annoying, so we put salepeople inside of the annoying circle. We know that Kelly is a salesperson, so we put Kelly inside of the Salespeople circle. So we have:

	\begin{tikzpicture}
		\draw (0,0) ellipse (3 and 2);
		\node at (-.5,1.5) {Annoying};
		\draw (1,0) circle (1.25);
		\node at (1,0.75) {Salespeople};
		\draw (1,-.5) circle (0.5);
		\node at (1,-.5) {Kelly};
	\end{tikzpicture}

	This logic is valid.
\end{boxexample}

\begin{boxexample}{}{}
	Determine if this logic is valid. All teachers are nice. John is not nice. Therefor, John is not a teacher.

	Solution: We know that teacher are nice, so we put teachers in the nice cirlce. John is not nice, so we put him outside of the nice circle. So we have:

	\begin{tikzpicture}
		\draw (0,0) ellipse (3 and 2);
		\node at (-.5,1.5) {Nice};
		\draw (1,0) circle (1.25);
		\node at (1,0.75) {Teachers};
		\draw (5,0) circle (0.5);
		\node at (5,0) {John};
	\end{tikzpicture}

	This logic is valid.
\end{boxexample}

\section{Circuits}

Logic can be used to build circuits. Actually, you can build an entire computer with logic! Such a computer can be built using real-life logic gates, or even built with Redstone in Minecraft! If you're interested in this, I strongly recommend the book \emph{Digital Design and Computer Architecture} by David Money Harris and Sarah L. Harris. In this class, we will look at very simple and/or circuits using switches.

Basically, when we have two switches in parallel, current will run if either of the switches are on. Thus, two switches in parallal are OR ($\lor$).

\begin{boxdefine}{OR Circuit}{}
	$A \lor B$ in circuitry can be defined as

	\begin{circuitikz}
		\draw (0,0)
		to [short, *-] (1,0)
		to [short] (1,1)
		to [short] (2,1)
		to [nos, l=A] (3,1)
		to [short] (4,1)
		to [short] (4,0)
		to [short, -*] (5,0)
		(1,0)
		to [short] (1,-1)
		to [short] (2,-1)
		to [nos, l=B] (3,-1)
		to [short] (4,-1)
		to [short] (4,0);
	\end{circuitikz}
\end{boxdefine}

Then, when we have two switches in series, we need both of them to be on in order for current to flow. So switches in series are AND ($\land$).

\begin{boxdefine}{AND Circuit}{}
	$A \land B$ in circuity can be defined as

	\begin{circuitikz}
		\draw (0,0)
		to [short, *-] (1,0)
		to [nos, l=A] (2,0)
		to [short] (3,0)
		to [nos, l=B] (4,0)
		to [short, -*] (5,0);
	\end{circuitikz}
\end{boxdefine}

Using these building blocks, its possible to write some logical statements as circuits.

\begin{boxexample}{}{}
	The following circuit can be written as $((P \land Q) \lor R) \land S$.

	\begin{circuitikz}
		\draw (0,0)
		to [short, *-] (1,0)
		to [short] (1,1)
		to [short] (2,1)
		to [nos,l=P] (3,1)
		to [short] (4,1)
		to [nos,l=Q] (5,1)
		to [short] (6,1)
		to [short] (6,0)
		to [short] (7,0)
		to [nos,l=S] (8,0)
		to [short,-*] (9,0)
		(1,0)
		to [short] (1,-1)
		to [short] (2,-1)
		to [nos,l=R] (3,-1)
		to [short] (6,-1)
		to [short] (6,0);
	\end{circuitikz}
\end{boxexample}


\documentclass[a4paper]{article}
\usepackage[margin=20mm]{geometry}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage{slashbox}
\usepackage{booktabs}
\usepackage[table,x11names]{xcolor}
\usepackage{pgfplots}

\setlength{\intextsep}{1pt}

\begin{document}
    \pagenumbering{gobble}
    \section*{Cumulative distribution function (CDF) of the normal distribution}

\pgfmathdeclarefunction{gauss}{3}{%
  \pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}

\begin{figure}[!h]
\centering
\begin{tikzpicture}
\begin{axis}[
  no markers,
  domain=0:6,
  samples=100,
  ymin=0,
  axis lines*=left,
  xlabel=$x$,
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=4cm,
  width=12cm,
  xtick=\empty,
  ytick=\empty,
  enlargelimits=false,
  clip=false,
  axis on top,
  grid = major,
  hide y axis
  ]

 \addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};

\pgfmathsetmacro\valueA{gauss(1,3,1)}
\pgfmathsetmacro\valueB{gauss(2,3,1)}
\draw [gray] (axis cs:1,0) -- (axis cs:1,\valueA)
    (axis cs:5,0) -- (axis cs:5,\valueA);
\draw [gray] (axis cs:2,0) -- (axis cs:2,\valueB)
    (axis cs:4,0) -- (axis cs:4,\valueB);
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);

\node[below] at (axis cs:1, 0)  {$\mu - 2\sigma$};
\node[below] at (axis cs:2, 0)  {$\mu - \sigma$};
\node[below] at (axis cs:3, 0)  {$\mu$};
\end{axis}
\end{tikzpicture}
\end{figure}

\begin{table}[!h]
    \centering
    \begin{tabular}{c|ccccc|ccccc}
    \toprule
    \backslashbox{$x$}{$\Delta x$}  & \textbf{0.00} & \textbf{0.01} & \textbf{0.02} & \textbf{0.03} & \textbf{0.04} & \textbf{0.05} & \textbf{0.06} & \textbf{0.07} & \textbf{0.08} & \textbf{0.09} \\\midrule
\rowcolor{lightgray}\textbf{0.0} & 0.5000 & 0.5040 & 0.5080 & 0.5120 & 0.5160 & 0.5199 & 0.5239 & 0.5279 & 0.5319 & 0.5359\\
\textbf{0.1} & 0.5398 & 0.5438 & 0.5478 & 0.5517 & 0.5557 & 0.5596 & 0.5636 & 0.5675 & 0.5714 & 0.5753\\
\textbf{0.2} & 0.5793 & 0.5832 & 0.5871 & 0.5910 & 0.5948 & 0.5987 & 0.6026 & 0.6064 & 0.6103 & 0.6141\\
\textbf{0.3} & 0.6179 & 0.6217 & 0.6255 & 0.6293 & 0.6331 & 0.6368 & 0.6406 & 0.6443 & 0.6480 & 0.6517\\
\textbf{0.4} & 0.6554 & 0.6591 & 0.6628 & 0.6664 & 0.6700 & 0.6736 & 0.6772 & 0.6808 & 0.6844 & 0.6879\\
\rowcolor{lightgray}\textbf{0.5} & 0.6915 & 0.6950 & 0.6985 & 0.7019 & 0.7054 & 0.7088 & 0.7123 & 0.7157 & 0.7190 & 0.7224\\
\textbf{0.6} & 0.7257 & 0.7291 & 0.7324 & 0.7357 & 0.7389 & 0.7422 & 0.7454 & 0.7486 & 0.7517 & 0.7549\\
\textbf{0.7} & 0.7580 & 0.7611 & 0.7642 & 0.7673 & 0.7704 & 0.7734 & 0.7764 & 0.7794 & 0.7823 & 0.7852\\
\textbf{0.8} & 0.7881 & 0.7910 & 0.7939 & 0.7967 & 0.7995 & 0.8023 & 0.8051 & 0.8078 & 0.8106 & 0.8133\\
\textbf{0.9} & 0.8159 & 0.8186 & 0.8212 & 0.8238 & 0.8264 & 0.8289 & 0.8315 & 0.8340 & 0.8365 & 0.8389\\
\rowcolor{lightgray}\textbf{1.0} & 0.8413 & 0.8438 & 0.8461 & 0.8485 & 0.8508 & 0.8531 & 0.8554 & 0.8577 & 0.8599 & 0.8621\\
\textbf{1.1} & 0.8643 & 0.8665 & 0.8686 & 0.8708 & 0.8729 & 0.8749 & 0.8770 & 0.8790 & 0.8810 & 0.8830\\
\textbf{1.2} & 0.8849 & 0.8869 & 0.8888 & 0.8907 & 0.8925 & 0.8944 & 0.8962 & 0.8980 & 0.8997 & 0.9015\\
\textbf{1.3} & 0.9032 & 0.9049 & 0.9066 & 0.9082 & 0.9099 & 0.9115 & 0.9131 & 0.9147 & 0.9162 & 0.9177\\
\textbf{1.4} & 0.9192 & 0.9207 & 0.9222 & 0.9236 & 0.9251 & 0.9265 & 0.9279 & 0.9292 & 0.9306 & 0.9319\\
\rowcolor{lightgray}\textbf{1.5} & 0.9332 & 0.9345 & 0.9357 & 0.9370 & 0.9382 & 0.9394 & 0.9406 & 0.9418 & 0.9429 & 0.9441\\
\textbf{1.6} & 0.9452 & 0.9463 & 0.9474 & 0.9484 & 0.9495 & 0.9505 & 0.9515 & 0.9525 & 0.9535 & 0.9545\\
\textbf{1.7} & 0.9554 & 0.9564 & 0.9573 & 0.9582 & 0.9591 & 0.9599 & 0.9608 & 0.9616 & 0.9625 & 0.9633\\
\textbf{1.8} & 0.9641 & 0.9649 & 0.9656 & 0.9664 & 0.9671 & 0.9678 & 0.9686 & 0.9693 & 0.9699 & 0.9706\\
\textbf{1.9} & 0.9713 & 0.9719 & 0.9726 & 0.9732 & 0.9738 & 0.9744 & 0.9750 & 0.9756 & 0.9761 & 0.9767\\
\rowcolor{lightgray}\textbf{2.0} & 0.9772 & 0.9778 & 0.9783 & 0.9788 & 0.9793 & 0.9798 & 0.9803 & 0.9808 & 0.9812 & 0.9817\\
\textbf{2.1} & 0.9821 & 0.9826 & 0.9830 & 0.9834 & 0.9838 & 0.9842 & 0.9846 & 0.9850 & 0.9854 & 0.9857\\
\textbf{2.2} & 0.9861 & 0.9864 & 0.9868 & 0.9871 & 0.9875 & 0.9878 & 0.9881 & 0.9884 & 0.9887 & 0.9890\\
\textbf{2.3} & 0.9893 & 0.9896 & 0.9898 & 0.9901 & 0.9904 & 0.9906 & 0.9909 & 0.9911 & 0.9913 & 0.9916\\
\textbf{2.4} & 0.9918 & 0.9920 & 0.9922 & 0.9925 & 0.9927 & 0.9929 & 0.9931 & 0.9932 & 0.9934 & 0.9936\\
\rowcolor{lightgray}\textbf{2.5} & 0.9938 & 0.9940 & 0.9941 & 0.9943 & 0.9945 & 0.9946 & 0.9948 & 0.9949 & 0.9951 & 0.9952\\
\textbf{2.6} & 0.9953 & 0.9955 & 0.9956 & 0.9957 & 0.9959 & 0.9960 & 0.9961 & 0.9962 & 0.9963 & 0.9964\\
\textbf{2.7} & 0.9965 & 0.9966 & 0.9967 & 0.9968 & 0.9969 & 0.9970 & 0.9971 & 0.9972 & 0.9973 & 0.9974\\
\textbf{2.8} & 0.9974 & 0.9975 & 0.9976 & 0.9977 & 0.9977 & 0.9978 & 0.9979 & 0.9979 & 0.9980 & 0.9981\\
\textbf{2.9} & 0.9981 & 0.9982 & 0.9982 & 0.9983 & 0.9984 & 0.9984 & 0.9985 & 0.9985 & 0.9986 & 0.9986\\
\rowcolor{lightgray}\textbf{3.0} & 0.9987 & 0.9987 & 0.9987 & 0.9988 & 0.9988 & 0.9989 & 0.9989 & 0.9989 & 0.9990 & 0.9990\\
\textbf{3.1} & 0.9990 & 0.9991 & 0.9991 & 0.9991 & 0.9992 & 0.9992 & 0.9992 & 0.9992 & 0.9993 & 0.9993\\
\textbf{3.2} & 0.9993 & 0.9993 & 0.9994 & 0.9994 & 0.9994 & 0.9994 & 0.9994 & 0.9995 & 0.9995 & 0.9995\\
\textbf{3.3} & 0.9995 & 0.9995 & 0.9995 & 0.9996 & 0.9996 & 0.9996 & 0.9996 & 0.9996 & 0.9996 & 0.9997\\
\textbf{3.4} & 0.9997 & 0.9997 & 0.9997 & 0.9997 & 0.9997 & 0.9997 & 0.9997 & 0.9997 & 0.9997 & 0.9998\\
\rowcolor{lightgray}\textbf{3.5} & 0.9998 & 0.9998 & 0.9998 & 0.9998 & 0.9998 & 0.9998 & 0.9998 & 0.9998 & 0.9998 & 0.9998\\
\textbf{3.6} & 0.9998 & 0.9998 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999\\
\textbf{3.7} & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999\\
\textbf{3.8} & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999 & 0.9999\\
\textbf{3.9} & 1.0000 & 1.0000 & 1.0000 & 1.0000 & 1.0000 & 1.0000 & 1.0000 & 1.0000 & 1.0000 & 1.0000\\
        \bottomrule
        \end{tabular}
        \caption{Approximations of $\Phi_{0;1}(x + \Delta x)$}
    \end{table}
    \begin{align*}
        \Phi_{0;1}(x) &= \frac{1}{\sqrt{2 \pi}} \int_{-\infty}^{x} e^{- t^2 / 2} \mathrm{d} t &
        \Phi_{0;1}(1.65) &\approx 0.9505\\
        \Phi_{\mu; \sigma^2}(x) &= \Phi_{0;1} \left (\frac{x-\mu}{\sigma} \right ) &
        \Phi_{0;1}(-x) &= 1 - \Phi_{0;1}(x)
    \end{align*}
\end{document}


%********************************************
% Lab 02: Converting Boolean Logic to a Circuit
%********************************************
\chapter{Boolean Logic}

\section{Purpose}

This lab has three goals:

\begin{itemize}
	\item Design circuits when given a Boolean expression.
	\item Create subcircuits.
	\item Create and exercise a test of the subcircuits.
\end{itemize}

\LE permits designers to work with a main circuit and any number of subcircuits. Students who have studied programming languages are familiar with ``functions'' or ``classes'' that can be designed and built one time and then reused many times whenever they are needed. \LE permits that same type of modular design by using subcircuits.

The \LE starter for this lab includes a \lstinline[columns=fixed]|main| circuit and one subcircuit, named \lstinline[columns=fixed]|Equation_1|. The starter subcircuit is used to practice creating a circuit from a Boolean expression and then a new subcircuit is added and a second Boolean expression is used to build that circuit.

\section{Procedure}

\subsection{Subcircuit: Equation 1}

\marginpar{A magnifying glass icon is used to indicate which circuit is active on the drawing canvas.}The starter circuit includes a subcircuit named \lstinline[columns=fixed]|Equation_1|. Double-click that circuit in the Explorer Pane to activate it. The drawing canvas for this subcircuit is mostly blank except for a Boolean expression: $ (A'BC')+(AB'C')+(ABC) $. Before starting to design a circuit, it is helpful to take a minute to analyze the expression.

\begin{itemize}
	\item There are only three variables used in the entire expression: \textit{A}, \textit{B}, and \textit{C}. Therefore, there would be three inputs into the circuit.
	\item There are three groups of variables and within each group the variables are joined with an \texttt{AND}. Therefore, the circuit must include three \texttt{AND} gates with three inputs for each gate.
	\item The three groups of variables are joined with an \texttt{OR}. Therefore, the circuit must include an \texttt{OR} gate with three inputs.
	\item While the expression does not name an output variable, it is reasonable to assume that the circuit would output a logic 1 or 0. Therefore, a one-bit output variable must be specified.
\end{itemize}

\marginpar{Do not be concerned with the exact placement of components on the drawing canvas. They can be rearranged as the build progresses.}Start by placing three inputs and an output on the drawing canvas. Inputs are indicated by a green icon with \textit{I->} on the tool bar above the drawing canvas. Click that tool and place three input pins named \textit{In1A}, \textit{In1B}, and \textit{In1C} \textemdash that means ``Input for Equation One, variable A'' and so forth.

Outputs are indicated by a white icon with \textit{->O} found on the tool bar above the drawing canvas. Click that tool and place an output named \textit{Out1}. The circuit should look like Figure \ref{fig:02-01}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-01}
	\caption{Equation 1 Inputs-Outputs}
	\label{fig:02-01}
\end{figure}

\marginpar{The gates in this manual are all ``narrow'' size. The size does not change the gate behavior but makes it easier to wire the complex circuits in later labs.}Next, the gates should be added. The \texttt{AND} gate tool can be found on the tool bar. Click that tool and place three \texttt{AND} gates on the circuit. Click each gate and in its properties panel set the \textit{Number of Inputs} to 3.

The \texttt{OR} gate tool can be found on the tool bar. Click that tool and place one \texttt{OR} gate on the circuit. Click that gate and in its properties panel set the \textit{Number of Inputs} to 3.

The circuit should look like Figure \ref{fig:02-02}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-02}
	\caption{Equation 1 And-Or Gates}
	\label{fig:02-02}
\end{figure}

Next, the inputs for the \texttt{AND} gates should be set to match the Boolean expression. The top \texttt{AND} gate will match the first group of inputs, $ (A'BC') $, so inputs \textit{A} and \textit{C} should be negated. To negate those two inputs, click the \texttt{AND} gate and in the properties panel set the \textit{Negate} item for the top and bottom input to ``Yes.'' When that is done, the two inputs on the \texttt{AND} gate should include a small ``negate'' circle.

In the same way, the middle and bottom input for the second \texttt{AND} gate should also be negated. The circuit should look like Figure \ref{fig:02-03}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-03}
	\caption{Equation 1 And Gate Inputs Set}
	\label{fig:02-03}
\end{figure}

Finally, connect all gates with wires, like Figure \ref{fig:02-04}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-04}
	\caption{Equation 1 Circuit Completed}
	\label{fig:02-04}
\end{figure}

Test the circuit by selecting the \textit{poke} tool in the tool bar (it looks like a pointing finger) and setting various combinations of 1 and 0 on the three inputs. The output pin should go high only when the inputs are set to $ (A'BC') $, $ (AB'C') $, or $ (ABC) $.

\subsection{Subcircuit: Equation 2}

A new subcircuit can be added to a circuit by clicking \textsc{Project -> Add Circuit}. Name the new circuit \lstinline[columns=fixed]|Equation_2|. Open the new subcircuit by double-clicking its name in the Explorer Pane.

Because this is a new subcircuit, the drawing canvas is blank. To start this subcircuit, write the equation for the circuit near the top of the drawing canvas by clicking the ``A'' button on the Toolbar and then clicking near the top of the drawing canvas and typing the following:

\[ (A'B'CD')+(A'BCD)+(AB'CD')+(ABCD') \]

It will save time to take a few minutes and analyze the expression.

\begin{itemize}
	\item There are only four variables used in the entire expression: \textit{A}, \textit{B}, \textit{C}, and \textit{D}. Therefore, there would be four inputs into the circuit.
	\item There are four groups of variables and within each group the variables are joined with an \texttt{AND}. Therefore, the circuit must include four \texttt{AND} gates with four inputs for each gate.
	\item The four groups of variables are joined with an \texttt{OR}. Therefore, the circuit must include an \texttt{OR} gate with four inputs.
	\item While the expression does not name an output variable, it is reasonable to assume that the circuit would output a logic 1 or 0. Therefore, a one-bit output variable must be specified.
\end{itemize}

Design the subcircuit using these names for the inputs: \textit{In2A}, \textit{In2B}, \textit{In2C}, and \textit{In2D}. Also include an output named \textit{Out2}. Set the \texttt{AND} gates so the their inputs are negated properly and then wire the entire subcircuit. Finally, test the circuit to ensure the output goes high only when the four specified combinations of inputs are present.

\subsection{Main Circuit}

Make the \lstinline[columns=fixed]|main| circuit active by double-clicking its name in the Explorer Panel. Click once on the \lstinline[columns=fixed]|Equation_1| circuit and the cursor will change into an image of that circuit as it will appear on the drawing canvas. Click on the drawing canvas to drop that subcircuit. The circuit can later be moved by clicking it and dragging it to a new location. Wire the three inputs and output as shown in Figure \ref{fig:02-05}. Notice that the input/output pins do not need to be named the same as in the subcircuit; for example, the output for \lstinline[columns=fixed]|Equation_1| is labeled \textit{Out1} but it is connected to an output pin labeled \textit{True1}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-05}
	\caption{Main Circuit}
	\label{fig:02-05}
\end{figure}

Add the \lstinline[columns=fixed]|Equation_2| circuit in the same way and wire four inputs and one output to that circuit. The inputs should be labeled \textit{A2}, \textit{B2}, \textit{C2}, and \textit{D2} and the output labeled \textit{True2}.

\subsection{Testing the Circuit}

One way to test this circuit is to use the \textit{poke} tool and click various input combinations for both subcircuits. If the subcircuits are correct then the output will only go high when the correct combination is set on the inputs. However, as digital logic circuits become more complex it is important to automate the testing process so no input combinations are overlooked. \LE includes a \textsc{Simulate -> Test Vector} feature that is used for automating circuit testing.

The first step in using automatic testing is to create a \textit{Test Vector} file. This is a simple \textit{.txt} file that can be created in any text processor, like \textit{Notepad}. \marginpar{Do not use a word processor to create the Test Vector since that would add unneeded codes for things like fonts and margins.} The format for a test vector is fairly simple.

\begin{itemize}
	\item Every line is a single test of the circuit, except the first line.
	\item The first line defines the various inputs and outputs being tested.
	\item Any line that starts with a hash mark (\#) is a comment and is ignored.
\end{itemize}

Following is the test vector file used to test the \lstinline[columns=fixed]|Equation_1| subcircuit.

\begin{Verbatim}[frame=lines,
								 numbers=left,
								 xleftmargin=10mm,
								 xrightmargin=10mm]
# Test vector for Lab 2
# Equation 1
A1 B1 C1 True1
0   0  0     0
0   0  1     0
0   1  0     1
0   1  1     0
1   0  0     1
1   0  1     0
1   1  0     0
1   1  1     1
\end{Verbatim}

Following is an explanation for the \textit{Test vector for Lab 2} file.

\begin{description}
	\item[Line 1] This is just the title of the file. Because this line starts with a hash (\#) it is a comment and will be ignored by \LE.
	\item[Line 2] This is another descriptor line and is ignored by \LE.
	\item[Line 3] This line lists all of the inputs and outputs in the circuit under test. In this case, there are three inputs, \textit{A1}, \textit{B1}, and \textit{C1}, along with one output, \textit{True1}. \LE is able to determine whether the pin is an input or output from its properties. NOTE: each of the inputs and outputs in this circuit are single bits. If an input or output has more than one bit then that must be specified on this line. For example, if \textit{True1} was actually a four-bit output then that pin would be listed as \textit{True1[4]}.
	\item[Line 4] This line contains the first test for the circuit. This line specifies that \LE make \textit{A1}, \textit{B1}, and \textit{C1} equal to zero and then check to be certain that \textit{True1} is also zero.
	\item[Other Lines] All other lines set the three input bits and specify the expected response in the output bit.
\end{description}

The test vector for Equation 2 would look like this:

\begin{Verbatim}[frame=lines,
								 numbers=left,
								 xleftmargin=10mm,
								 xrightmargin=10mm]
# Test vector for Lab 2
# Equation 2
A2 B2 C2 D2 True2
0   0  0  0     0
0   0  0  1     0
0   0  1  0     1
0   0  1  1     0
0   1  0  0     0
0   1  0  1     0
0   1  1  0     0
0   1  1  1     1
1   0  0  0     0
1   0  0  1     0
1   0  1  0     1
1   0  1  1     0
1   1  0  0     0
1   1  0  1     0
1   1  1  0     1
1   1  1  1     0
\end{Verbatim}

In practice, a circuit designer would usually not create two different test vectors but would, instead, create just one file to test all parts of the circuit. Combining the \textit{Equation 1} test and the \textit{Equation 2} test is not quite as easy as appending one after the other since all input and output pins for both circuits must be specified at the top of the file. Following is the test vector for a circuit that combines \textit{Equation 1} and \textit{Equation 2}. Notice that all input and output pins are defined on line three then each line beginning with line four tests both of the equation circuits. Because only eight tests are needed to fully exercise \textit{Equation 1} but 16 are needed for Equation 2, the \textit{Equation 1} tests are repeated starting on Line 12.

\begin{Verbatim}[frame=lines,
								 numbers=left,
								 xleftmargin=10mm,
								 xrightmargin=10mm]
# Test vector for Lab 2
# Equation 1   - Equation 2
A1 B1 C1 True1   A2 B2 C2 D2 True2
0   0  0     0    0  0  0  0     0
0   0  1     0    0  0  0  1     0
0   1  0     1    0  0  1  0     1
0   1  1     0    0  0  1  1     0
1   0  0     1    0  1  0  0     0
1   0  1     0    0  1  0  1     0
1   1  0     0    0  1  1  0     0
1   1  1     1    0  1  1  1     1
0   0  0     0    1  0  0  0     0
0   0  1     0    1  0  0  1     0
0   1  0     1    1  0  1  0     1
0   1  1     0    1  0  1  1     0
1   0  0     1    1  1  0  0     0
1   0  1     0    1  1  0  1     0
1   1  0     0    1  1  1  0     1
1   1  1     1    1  1  1  1     0
\end{Verbatim}

To start a test, click \textsc{Simulate -> Test Vector}. The window illustrated in Figure \ref{fig:02-06} opens.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-06}
	\caption{Test Vector Window}
	\label{fig:02-06}
\end{figure}

Click the \textit{Load Vector} button at the bottom of the window and load the test vector file. The test will automatically start and Logisim-evolution will report the results, like in Figure \ref{fig:02-07}.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-07}
	\caption{Test Completed}
	\label{fig:02-07}
\end{figure}

The test indicates all 16 lines passed and zero failed so it could be reasonably concluded that the circuits are functioning properly. Figure \ref{fig:02-08} illustrates a failed test. The circuit designer would then need to troubleshoot to determine what went wrong with the circuit.

\begin{figure}[H]
	\centering
	\includegraphics[width=\maxwidth{.95\linewidth}]{gfx/02-08}
	\caption{Test Failure}
	\label{fig:02-08}
\end{figure}

\section{Deliverable}

\marginpar{It is important to name all inputs and outputs as specified in the lab since they are checked with a Test Vector file that depends on those names.}To receive a grade for this lab, complete the \lstinline[columns=fixed]|main| circuit and both subcircuits. Be sure the standard identifying information is at the top left of the \lstinline[columns=fixed]|main| circuit, similar to:

\bigskip
% The minipage environment keeps the three lines together - no page break.
\begin{minipage}{\linewidth}
	\begin{verbatim}
	George Self
	Lab 02: Boolean Equations
	February 18, 2018
	\end{verbatim}
\end{minipage}
\bigskip

Save the file with this name: \emph{\texttt{Lab02\_Bool}} and submit that file for grading.


Many algorithms and tools in robotic autonomy leverage models of the world that are often based on first-principles: physics-based kinematic models are used to design controllers, sensor models are used in localization algorithms, and geometric principles are used in understanding stereo vision. However, there are also many scenarios in robotics where these techniques may fail to capture the complexity of unstructured real-world environments. For example, how can a stop-sign be reliably detected in camera images when it could be rainy, foggy, or dark out, or when the stop-sign is partially occluded? Are there first-principles models that can accurately predict the behavior of a human driver, and distinguish between aggressive and defensive driving behavior? How can a robot be programmed to pick up objects with an infinite number of variations in size, shape, color, and texture? In the last few decades, advancements in \textit{machine learning}\cite{HastieTibshiraniEtAl2017} have led to start-of-the-art approaches for many of these challenging problems\footnote{Of course in many settings it is beneficial to use first-principles and machine learning techniques \textit{in concert}.}. This chapter presents an introduction to machine learning to provide a knowledge of the fundamental tools that are used in learning-based algorithms for robotics, including computer vision, reinforcement learning, and more.

\notessection{Machine Learning}
At their most fundamental level, machine learning techniques seek to extract useful patterns from \textit{data}\footnote{In many cases the data will come from real world experiments, but in other cases may come from simulation.}, and are typically classified as either \textit{supervised} or \textit{unsupervised}.

\begin{definition}[Supervised Learning]
Given a collection of $n$ data points:
\begin{equation*}
\{(x_1, y_1), \dots, (x_n, y_n)\},
\end{equation*}
where $x_i$ is an input variable and $y_i$ is an output, the \textit{supervised learning} problem is to find a function $y = f(x)$ that fits the data and can be used to predict outputs $y$ for new inputs $x$.
\end{definition}

\begin{definition}[Unsupervised Learning]
Given a collection of $n$ data points $\{x_1, \dots, x_n\}$, the \textit{unsupervised learning} problem is to find patterns in the data.
\end{definition}

Supervised learning problems, such as regression and classification\footnote{In regression problems the output $y$ is continuous and in classification problems the output $y$ is discrete (categorical).}, are generally more common in robotics applications and will be the focus of this chapter. For example, robotic imitation learning-based controllers\footnote{Imitation learning refers to the process of learning to mimic a policy (e.g. from an expert) through example decisions.} can be expressed as a regression problem where the input $x$ is the state of the robot and $y$ is the action the robot should take. Classification problems also arise frequently in robotic computer vision, for example to identify whether the image $x$ belongs to a particular class $y$ (e.g. a dog or cat).

In both regression and classification problems, the learned function $f$ is categorized as either \textit{parametric} or \textit{non-parametric}. Parametric functions are generally more structured and can be written down in an analytical form\footnote{The most basic parametric function would be a linear function $f(x) = Wx$, parameterized by the ``weight'' matrix $W$.}, while non-parametric functions are generally defined by the data points themselves\footnote{In the non-parametric k-nearest neighbors method, the value $f(x)$ is defined by the value of the data points $y_i$ corresponding to the $k$ closest points, $x_i$, to $x$.}. The best choice between parametric or non-parametric functions is generally dependent on the particular problem and the type of data available. However, some of the most popular choices are parametric, such as polynomials and \textit{neural networks}.

\subsection{Loss Functions}
In supervised learning problems, a metric known as a \textit{loss function} is used to evaluate and compare candidate models $f(x)$ that could be used to fit the data. Many loss functions for supervised learning problems exist, but some of the most common examples include the $l_2$ and $l_1$ loss (for regression) and the $0 - 1$ and cross entropy loss (for classification).

\begin{enumerate}
\item The $l_2$ loss function is defined by:
\begin{equation} \label{eq:l2loss}
L = \frac{1}{n}\sum_{i=1}^n ( f(x_i) - y_i )^2,
\end{equation}
where the summation is over some set of data points $(x^i, y^i)$. From this loss function it can be seen that a penalty arises from the function $f$ not perfectly matching the data at the sampled data points, but most importantly that the penalty is \textit{quadratic} with respect to this residual. This loss function will therefore favor more small residuals over a few large residuals, which tends to make the model perform better ``on average''. However this also makes the $l_2$ loss sensitive to outliers in the data, making the training less robust.
\item The $l_1$ loss function is defined by:
\begin{equation}
L = \frac{1}{n}\sum_{i=1}^n \lvert f(x_i) - y_i \rvert.
\end{equation}
Unlike the $l_2$ loss, this loss function only penalizes the \textit{absolute value} of the residual. Therefore this loss function will favor all residuals on a more equal footing and generally leads to a more robust training procedure that is less sensitive to outliers in the data.
\item The $0-1$ loss function is defined by:
\begin{equation}
L = \frac{1}{n}\sum_{i=1}^n \mathbf{1}\{f(x_i) \neq y_i \},
\end{equation}
where $\mathbf{1}\{\cdot\}$ is the indicator function.
This loss function can be used in classification problems and provides a loss of $1$ whenever the classification is incorrect, and $0$ otherwise. However, the use of this loss function introduces practical issues when training with gradient-based optimization, since this function is either flat or not differentiable at all points in the domain.
\item The cross entropy loss function\footnote{Cross entropy loss is more practical than $0-1$ loss since it is a differentiable function.} is defined by:
\begin{equation}
L = -\frac{1}{n}\sum_{i=1}^n y^\top_i \log f(x_i),
\end{equation}
and is a common loss function in classification problems. To get an intuitive feeling for how the cross entropy loss works, consider a classification problem where the classes are $c \in \{1, 2, \dots, C\}$ and where the function $f(x_i)$ outputs a \textit{vector} of the probabilities of each class (which is normalized to sum to $1$)\footnote{This can be accomplished by using the \textit{softmax} function.}. Additionally, for each data point the vector $y_i$ is a ``one-hot'' vector\footnote{A one-hot vector is a vector with all zeros and a single $1$.} specified by the class associated with $x_i$. Therefore the loss for a particular data point can be written as:
\begin{equation*}
-y^\top_i \log f(x_i) = -\begin{bmatrix}
0, \dots, 1, \dots, 0
\end{bmatrix}\begin{bmatrix}
\log f_1(x_i) \\ \vdots \\ \log f_C(x_i)
\end{bmatrix} = -\log f_c(x_i),
\end{equation*}
where $C$ is the number of classes, the $1$ element in $y_i$ is in the position corresponding to the correct class, and $f_c(x_i)$ is the probability of the correct class output by the model. Thus, to minimize the loss for this particular data point it is good to make $f_c(x_i) = 1$ (in fact as $f_c(x_i)\xrightarrow{} 0$ the loss approaches infinity!).
Cross entropy loss can also be derived from a statistical perspective, where it can be shown to be the same as maximizing the log-likelihood over all data points.
\end{enumerate}

\subsection{Model Training}
In supervised learning problems with a predetermined parametric model (e.g. linear model or neural network), the \textit{values} of the parameters can be optimized to best fit the data (i.e. minimize the specified loss function). This process of parameter optimization is referred to as \textit{model training}. While in some special cases the optimal set of parameters can be computed analytically, it is more common to search for a good set of parameters in an iterative fashion using numerical optimization techniques.

\begin{example}[Linear Least Squares]
One of the most fundamental regression problems, linear least squares, can be solved analytically. In this problem, the parametric model is a linear model\footnote{This approach can also be extended to nonlinear settings through the use of basis functions. In particular the model becomes $f(x) = \theta^\top \phi(x)$, where $\phi(x)$ are nonlinear basis functions (sometimes referred to as \textit{features}.}:
\begin{equation*}
    f(x) = \theta^\top x,
\end{equation*}
where $x \in \R^p$ is the input and $\theta \in R^p$ is the set of model parameters, and the loss function is the $l_2$ loss \eqref{eq:l2loss}. Given $n$ data points $(x_i, y_i)$, the loss function can be expressed in matrix form as:
\begin{equation*}
L(\theta) = \frac{1}{n}\lVert Y - X\theta \rVert_2^2,
\end{equation*}
where the matrix $Y \in \R^n$ and $X \in \R^{n \times p}$ are defined by the data as:
\begin{equation*}
Y = \begin{bmatrix}
y_1 \\ \vdots \\ y_n
\end{bmatrix}, \quad X =
\begin{bmatrix}
x_1^\top \\ \vdots \\ x^\top_n
\end{bmatrix}.
\end{equation*}
The parameters $\theta$ are then chosen to minimize the loss function by taking the derivative:
\begin{equation*}
\frac{dL}{d\theta} = \frac{2}{n}X^\top X\theta - \frac{2}{n}X^\top Y,
\end{equation*}
and setting it equal to zero, which gives $\theta^* = (X^\top X)^{-1}X^\top Y$.\footnote{Note that directly computing the inverse of $X^\top X$ may be challenging, but alternative numerical methods exist to compute the value of $\theta^*$ that satisfies the necessary condition of optimality.}
\end{example}

\subsubsection{Numerical Optimization} \label{subsubsec:MLnumopt}
In many cases parameter optimization cannot be performed analytically and therefore numerical optimization algorithms are used. Two of the most fundamental algorithms for numerical optimization-based training of parametric models are \textit{gradient descent} and \textit{stochastic gradient descent}\footnote{Gradient descent is referred to as a \textit{first-order} method.}.

In gradient descent, the parameters $\theta \in \R^p$  of a model $f_\theta(x)$ are iteratively updated by:
\begin{equation*}
\theta \xleftarrow{} \theta - \eta \nabla_\theta L(\theta),
\end{equation*}
where $\nabla_\theta L(\theta)$ is the gradient of the loss function with respect to the parameters and the hyperparameter $\eta$ is referred to as the \textit{learning rate} or \textit{step-size}. By leveraging the gradient, this update rule seeks to iteratively improve the parameters to incrementally decrease the loss.

Notice that the gradient of the loss can be written as:
\begin{equation*}
\nabla_\theta L(\theta) = \frac{1}{n}\sum_{i=1}^n \nabla_\theta L_i(\theta),
\end{equation*}
where $L_i$ is the term of the loss function associated with the $i$-th data point.
Therefore computing the gradient of the loss function could be computationally intensive if the number of data points is very large. To address this issue, stochastic gradient descent uses an approximation of the gradient computed by randomly sampling the gradients over a smaller \textit{batch} of data points $S$\footnote{The batch $S$ is resampled at every iteration of the algorithm.}:
\begin{equation*}
\nabla_\theta L(\theta) \approx \frac{1}{\lvert S \rvert} \sum_{i \in S \subset \{1, \dots, n\}} \nabla_\theta L_i(\theta),
\end{equation*}
where $\lvert S \rvert$ is the number of data points in the batch.

Beyond gradient descent approaches lie a broad set of additional numerical optimization algorithms that are commonly used in practice\cite{KochenderferWheeler2019}. Often times these advanced methods may lead to faster learning rates or more robust learning, and some algorithms may also be more applicable to problems with larger amounts of data or larger numbers of model parameters.

\subsubsection{Training and Test Sets + Regularization} \label{subsubsec:MLreg}
In supervised learning with parametric models, the goal is to train a model $f(x)$ that accurately predicts the output $y$ for inputs $x$ that are not seen in the data set. In other words, the goal is to find a model that \textit{generalizes} to unseen data. It is important to note however that simply optimizing the loss function over a dataset \textit{does not} guarantee that the model generalizes well, since it is possible to \textit{overfit} the model to the data.

A model is overfit to a set of data if it predicts the set of data well (i.e. has a low loss) but fails to accurately predict new data. To counter this issue, one very common practice in machine learning is to split the full dataset into two parts: a training set and a test set\footnote{There isn't an optimal way to split the data, but common splits range from $80/20$ training/test to $50/50$ training/test.}. As the names suggest, the model can be trained with the training data and then the test set can be used to verify whether overfitting has occured. To test for overfitting, the loss function can be evaluated over both sets of data. Overfitting has occured if the training loss is significantly lower than the test loss.

While splitting the data into training and test sets provides a good way to \textit{verify} whether the learned model generalizes well, there are also techniques that can be employed in during the training process to avoid overfitting. In particular, the most common technique is known as \textit{regularization}. One form of regularization is implemented by adding terms to the loss function to penalize ``model complexity''. For example, with a model $f_\theta(x)$ parameterized by the vector $\theta$, two common forms of regularization include:
\begin{enumerate}
    \item $l_2$ regularization, which consists of the addition of the term $\lVert \theta \rVert_2$ to the loss function,
    \item $l_1$ regularization, which which consists of the addition of the term $\lVert \theta \rVert_1$ to the loss function.
\end{enumerate}

\subsection{Neural Networks}
One very common parametric model used in machine learning is the \textit{neural network}\footnote{Also known as the multi-layer perceptron.}. Neural networks are models with very specific structures, consisting of a hierarchical sequence of linear and nonlinear functions, which makes them very powerful function approximators. Mathematically, neural networks are typically described as a sequence of functions:
\begin{equation} \label{eq:neuralnet}
\begin{split}
h_1 &= f_1(W_1x + b_1), \\
h_2 &= f_2(W_2h_1 + b_2), \\
& \vdots \\
\hat{y} &= f_K(W_Kh_{K-1} + b_K), \\
\end{split}
\end{equation}
which is an easier notation than writing the equivalent composite function:
\begin{equation*}
\hat{y} =  f_K(W_K f_{K-1}(\dots ) + b_K).
\end{equation*}
In this model, the parameters are the weights $W_1, \dots, W_K$ and biases $b_1, \dots, b_K$, and the structure of the model is predefined by the choice of the \textit{activation functions} $f_1, \dots, f_K$ and the number of \textit{layers} $K$. The intermediate variables $h_1, \dots, h_{K-1}$ are the outputs of the \textit{hidden layers}, aptly named since they are not the input or the output of the model.

To fully specify the structure of the model, a practitioner needs to specify the number of hidden layers\footnote{Neural networks with many layers are referred to as \textit{deep neural networks}.}, the dimensionality of each of the intermediate variables $h_i$ (usually chosen to be the same for all hidden layers), and the activation functions $f_i$.

\subsubsection{Activation Functions}
Commonly used activation functions $f_1, \dots, f_K$ in neural networks include sigmoid functions, hyperbolic tangent functions, rectified linear units (ReLU), and leaky ReLU functions\footnote[][-2\baselineskip]{It is typical for the same activation function to be used for all layers of the network.}.

\begin{enumerate}
\item Sigmoid function (also denoted as $\sigma(x)$):
\begin{equation*}
f(x) = \frac{1}{(1 + e^{-x})},
\end{equation*}
\item Hyperbolic tangent function:
\begin{equation*}
f(x) = \text{tanh}(x),
\end{equation*}
\item ReLU function:
\begin{equation*}
f(x) = \max\{0, x\},
\end{equation*}
\item Leaky ReLU function:
\begin{equation*}
f(x) = \max\{0.1x, x\},
\end{equation*}
\end{enumerate}

\begin{figure*}[ht]
\begin{center}
\includegraphics[width=0.95\textwidth]{tex/figs/app01_figs/activations.png}
\caption{Common activation functions used in neural networks.}
\label{fig:activations}
\end{center}
\end{figure*}

It is important to note that each of these activation functions share two important characteristics: they are \textit{nonlinear} and they are easy to differentiate. It is critical that the activation function be nonlinear since a composition of linear functions will remain linear, and therefore no additional benefit is gained in modeling capability by adding more than a single layer to the network. Differentiability is also critical because the gradients must be easily computable during training\footnote{While ReLU and leaky ReLU are not strictly differentiable, this issue is easily mitigated in practice.}.

\subsubsection{Training Neural Networks}
Neural networks are trained with gradient-based numerical optimization techniques, such as those mentioned in Section \ref{subsubsec:MLnumopt} (e.g. stochastic gradient descent). Therefore once a particular loss function $L$ has been chosen, the gradients $\frac{\partial L}{\partial \theta}$ must be computed for each parameter. Since neural networks can contain a large number of parameters, this gradient computation must be accomplished in a computationally efficient way. In particular, the gradients are computed using an algorithm referred to as \textit{backpropagation}, which leverages the chain rule of differentiation and the layered structure of the network.

As with other parametric models, it is very important to avoid overfitting when training neural networks\footnote{It is quite easy to overfit when training neural networks since they have such a large number of parameters.}. This can partially be accomplished using the division of the dataset into training and test sets, as well as by using regularization techniques as mentioned in Section \ref{subsubsec:MLreg}. Another technique for avoiding overfitting in neural networks is referred to as \textit{dropout}, where some ``connections'' in the network are occasionally removed during the training process. This essentially forces the network to learn more redundant representations, which has been shown to improve generalization. Of course another useful technique to avoid overfitting is just to have an extremely large dataset, but in many cases this may not be very practical.

\subsection{Backpropagation and Computational Graphs}
From a theoretical standpoint, computing the gradients $\frac{d L}{d \theta}$ of the loss function with respect to the parameters is relatively straightforward. However, from a practical standpoint computing these gradients can be computationally expensive, especially for complex models such as neural networks.
\textit{Backpropagation}\footnote{Sometimes also referred to as auto-differentiation.} is an algorithm that addresses this issue by computing all required gradients in an efficient way\marginnote{Many software tools, such as PyTorch (\texttt{https://pytorch.org/}) and TensorFlow (\texttt{https://www.tensorflow.org/}) will automatically be able to perform backpropagation for a large class of functions.}.

Backpropagation computes gradients by cleverly choosing the order in which operations required to compute the gradient are performed. By doing so it seeks to avoid redundant computations, and can in fact be viewed as an example of dynamic programming. While in some simple cases the backpropagation algorithm may provide only a small advantage, in many cases (and in particular for neural network training) backprop can be orders of magnitude faster than naive approaches.

A \textit{computational graph} is another practical tool that is useful when using the backpropagation algorithm to compute gradients. A computational graph provides a way to express a mathematical function using representations from graph theory. In particular the function is expressed as a directed graph where the nodes represent mathematical operations or function inputs and the edges represent intermediate quantities. Using a computational graph, a forward pass through the graph (starting at the root nodes, which are function inputs) is equivalent to evaluating the function.

This representation makes it very easy to see the structure of the mathematical operation that can be exploited by the backpropagation algorithm.
As an example, consider the function $L(x,y) = g(f(x,y))$ and its associated computational graph shown in Figure \ref{fig:backprop} (which includes the intermediate variable $z$). Using the chain rule, the gradient of $L$ with respect to $x$ is $\frac{\partial L}{\partial x} = \frac{dL}{dz}\frac{\partial z}{\partial x}$. The backpropagation algorithm uses this structure to convert the computation of the gradient $\frac{\partial L}{\partial x}$ into a sequence of \textit{local} gradient computations $\frac{dL}{dz}$ and $\frac{\partial z}{\partial x}$, corresponding to each computation node in the graph. With this structure redundant computation can be be avoided. For example, when computing $\frac{\partial L}{\partial y}$ the partial gradient $\frac{dL}{dz}$ can be reused.

\begin{figure}[ht]
\begin{center}
\includegraphics[width=0.55\textwidth]{tex/figs/app01_figs/backprop.png}
\caption{Example computational graph for a function $L(x,y) = g(f(x,y))$.}
\label{fig:backprop}
\end{center}
\end{figure}

To summarize, the backpropagation algorithm follows the following basic steps:
\begin{enumerate}
    \item Perform a forward pass through the computational graph to compute any intermediate variables that may be needed for computing local gradients\footnote{For example if $g(z) = z^2$ the gradient $\frac{dg}{dz} = 2z$ depends on the current value of $z$}.
    \item Starting from the graph output, perform a backwards pass over the graph where at each computation node the local gradient of the node with respect to its inputs and outputs is computed. Then, compute the gradient of the graph's output with respect to the inputs of the local computation node, leveraging the chain rule and previously calculated gradients. In Figure \ref{fig:backprop}, the first step of backprop would be to compute $\frac{\partial L}{\partial z} = \frac{d g}{d z}$, and the second step would use $\frac{\partial L}{\partial z}$ to compute the remaining gradients $\frac{\partial L}{\partial x}$ and $\frac{\partial L}{\partial y}$.
\end{enumerate}

\begin{example}[Training a Simple Model] \label{ex:backprop}
Consider a supervised learning problem with a parametric model defined as:
\begin{equation*}
f(x) = (x+a)(x+b),
\end{equation*}
where $a$ and $b$ are parameters of the model, and a $l_2$ loss function \eqref{eq:l2loss} is used for training. A computational graph for computing the loss from a single data point with this model is shown in Figure \ref{fig:graph}.
\begin{marginfigure}
\begin{center}
\includegraphics[width=0.95\textwidth]{tex/figs/app01_figs/graph.png}
\caption{Computational graph for computing the loss for a single data point for the model $f(x) = (x+a)(x+b)$ with $l_2$ loss (see Example \ref{ex:backprop}). The values $(x_i, y_i)$ are the data point, the model output is $\hat{y}$, and $z_1$, $z_2$, $z_3$ are intermediate variables. The quantities $a$ and $b$ are parameters of the model.}
\label{fig:graph}
\end{center}
\end{marginfigure}

For this model and loss function the gradients required for training can be computed analytically as:
\begin{equation*}
\begin{split}
\frac{\partial L_i}{\partial a} &= -2(y - (x+a)(x+b))(x+b), \\
\frac{\partial L_i}{\partial b} &= -2(y - (x+a)(x+b))(x+a).\\
\end{split}
\end{equation*}
Computing the gradients in this way (the naive approach) would require $7$ operations each (4 sums and 3 multiplications), for a total of $14$ operations.

Alternatively the gradients can be computed in a more efficient way using backpropagation, which avoids redundant computations. This approach can be viewed as taking a \textit{backward pass} over the computation graph. Starting at the output of the graph:
\begin{equation*}
\begin{split}
\frac{\partial L_i}{\partial z_1} = 2z_1.
\end{split}
\end{equation*}
Then moving on through the next operations and using the chain rule (and reusing the previous computations):
\begin{equation*}
\begin{split}
\frac{\partial L_i}{\partial \hat{y}} = \frac{\partial L_i}{\partial z_1}\frac{\partial z_1}{\partial \hat{y}} = -\frac{\partial L_i}{\partial z_1},
\end{split}
\end{equation*}
and:
\begin{equation*}
\begin{split}
\frac{\partial L_i}{\partial z_2} &= \frac{\partial L_i}{\partial \hat{y}}\frac{\partial \hat{y}}{\partial z_2} = \frac{\partial L_i}{\partial \hat{y}}z_3, \\
\frac{\partial L_i}{\partial z_3} &= \frac{\partial L_i}{\partial \hat{y}}\frac{\partial \hat{y}}{\partial z_3} = \frac{\partial L_i}{\partial \hat{y}}z_2. \\
\end{split}
\end{equation*}
Finally, the next step backward reaches the parameters $a$ and $b$:
\begin{equation*}
\begin{split}
\frac{\partial L_i}{\partial a} &= \frac{\partial L_i}{\partial z_2}\frac{\partial z_2}{\partial a} = \frac{\partial L_i}{\partial z_2}, \\
\frac{\partial L_i}{\partial b} &= \frac{\partial L_i}{\partial z_3}\frac{\partial z_3}{\partial b} = \frac{\partial L_i}{\partial z_3}. \\
\end{split}
\end{equation*}
To actually compute the numerical values of these gradients:
\begin{enumerate}
    \item First perform a forward pass through the network to compute the values $z_1$, $z_2$, and $z_3$ (5 operations).
    \item Then perform the backward pass computations to compute $\frac{\partial L_i}{\partial z_1}$, $\frac{\partial L_i}{\partial \hat{y}}$, $\frac{\partial L_i}{\partial z_2}$, $\frac{\partial L_i}{\partial z_3}$, $\frac{\partial L_i}{\partial a}$, and $\frac{\partial L_i}{\partial b}$ (4 operations).
\end{enumerate}
Using backpropagation, only 9 operations are required to compute the gradients $\frac{\partial L_i}{\partial a}$, and $\frac{\partial L_i}{\partial b}$, which is a non-negligible reduction over the naive approach!

\end{example}


\chapter{Basic Filtering}

This chapter introduces the most commonly used filters found in OTB.
Most of these filters are intended to process images. They will accept one or
more images as input and will produce one or more images as output. OTB is
based ITK's data pipeline architecture in which the output of one filter is
passed as input to another filter. (See Section
\ref{sec:DataProcessingPipeline} on page \pageref{sec:DataProcessingPipeline}
for more information.)

\section{Thresholding}
\ifitkFullVersion
\label{sec:ThresholdingFiltering}
\fi

The thresholding operation is used to change or identify pixel values based
on specifying one or more values (called the \emph{threshold} value). The
following sections describe how to perform thresholding operations using
OTB.

\subsection{Binary Thresholding}
\label{sec:BinaryThresholdingImageFilter}

\ifitkFullVersion
\input{BinaryThresholdImageFilter.tex}
\fi

\subsection{General Thresholding}
\label{sec:ThresholdingImageFilter}

\ifitkFullVersion
\input{ThresholdImageFilter.tex}
\fi

\subsection{Threshold to Point Set}
\label{sec:ThresholdImageToPointSetFilter}

\ifitkFullVersion
\input{ThresholdToPointSetExample.tex}
\fi

%% \section{Casting and Intensity Mapping}
%% \label{sec:CastingImageFilters}

%% The filters discussed in this section perform pixel-wise intensity mappings.
%% Casting is used to convert one pixel type to another, while intensity mappings
%% also take into account the different intensity ranges of the pixel types.

%% \subsection{Linear Mappings}
%% \label{sec:IntensityLinearMapping}

%% \ifitkFullVersion
%% %\input{CastingImageFilters.tex}
%% \fi

%% \subsection{Non Linear Mappings}
%% \label{sec:IntensityNonLinearMapping}

%% The following filter can be seen as a variant of the casting filters. Its main
%% difference is the use of a smooth and continuous transition function of
%% non-linear form.

%% \ifitkFullVersion
%% %\input{SigmoidImageFilter.tex}
%% \fi

\section{Mathematical operations on images}
OTB and ITK provide a lot of filters allowing to perform basic operations on image layers (thresholding, ratio, layers combinations...).
It allows to create a processing chain defining at each step operations and to combine them in the data pipeline.
But the library offers also the possibility to perform more generic complex mathematical operation on images in a single filter: the
\doxygen{otb}{BandMathImageFilter} and more recently the \doxygen{otb}{BandMathImageFilterX}.

\subsection{BandMath filter}
\label{sec:BandMathImageFilter}

\ifitkFullVersion
\input{BandMathFilterExample.tex}
\fi

\subsection{BandMathX filter}
\label{sec:BandMathImageFilterX}
A new version of the BandMath filter is now available; among the new functionalities, variables representing multi-band pixels were introduced, as well as variables representing neighborhoods of pixels. The class name is \doxygen{otb}{BandMathImageFilterX}.

\ifitkFullVersion
\input{BandMathXImageFilterExample.tex}
\fi

\section{Gradients}
\label{sec:GradientFiltering}

Computation of gradients is a fairly common operation in image processing. The
term ``gradient'' may refer in some contexts to the gradient vectors and in
others to the magnitude of the gradient vectors. ITK filters attempt to
reduce this ambiguity by including the \emph{magnitude} term when
appropriate. ITK provides filters for computing both the image of gradient
vectors and the image of magnitudes.

\subsection{Gradient Magnitude}
\label{sec:GradientMagnitudeImageFilter}

\ifitkFullVersion
\input{GradientMagnitudeImageFilter.tex}
\fi

\subsection{Gradient Magnitude With Smoothing}
\label{sec:GradientMagnitudeRecursiveGaussianImageFilter}

\ifitkFullVersion
\input{GradientMagnitudeRecursiveGaussianImageFilter.tex}
\fi

\subsection{Derivative Without Smoothing}
\label{sec:DerivativeImageFilter}

\ifitkFullVersion
\input{DerivativeImageFilter.tex}
\fi

\section{Second Order Derivatives}
\label{sec:SecondOrderDerivatives}

%% \subsection{Second Order Recursive Gaussian}
%% \label{sec:SecondDerivativeRecursiveGaussian}

%% \ifitkFullVersion
%% \input{SecondDerivativeRecursiveGaussianImageFilter.tex}
%% \fi

\subsection{Laplacian Filters}
\label{sec:LaplacianFilters}

%\subsubsection{Laplacian Filter Finite Difference}
\subsubsection{Laplacian Filter Recursive Gaussian}
\ifitkFullVersion
\input{LaplacianRecursiveGaussianImageFilter1.tex}
\input{LaplacianRecursiveGaussianImageFilter2.tex}
\fi

\section{Edge Detection}

\subsection{Canny Edge Detection}
\ifitkFullVersion
\input{CannyEdgeDetectionImageFilter.tex}
\fi

\subsection{Ratio of Means Detector}
\input{TouziEdgeDetectorExample}

\section{Neighborhood Filters}
\label{sec:NeighborhoodFilters}

The concept of locality is frequently encountered in image processing in the
form of filters that compute every output pixel using information from a small
region in the neighborhood of the input pixel.  The classical form of
these filters are the $3 \times 3$ filters in 2D images. Convolution masks
based on these neighborhoods can perform diverse tasks ranging from noise
reduction, to differential operations, to mathematical morphology.

The Insight toolkit implements an elegant approach to neighborhood-based image
filtering.  The input image is processed using a special iterator called the
\doxygen{itk}{NeighborhoodIterator}. This iterator is capable of moving over all the
pixels in an image and, for each position, it can address the pixels in a local
neighborhood. Operators are defined that apply an algorithmic operation in the
neighborhood of the input pixel to produce a value for the output pixel.  The
following section describes some of the more commonly used filters that take
advantage of this construction. (See Chapter
\ref{sec:ImageIteratorsChapter} on page
\pageref{sec:ImageIteratorsChapter} for more information about iterators.)

\subsection{Mean Filter}
\label{sec:MeanFilter}

\ifitkFullVersion
\input{MeanImageFilter.tex}
\fi

\subsection{Median Filter}
\label{sec:MedianFilter}

\ifitkFullVersion
\input{MedianImageFilter.tex}
\fi

\subsection{Mathematical Morphology}
\label{sec:MathematicalMorphology}

Mathematical morphology has proved to be a powerful resource for image
processing and analysis \cite{Serra1982}. ITK implements mathematical
morphology filters using NeighborhoodIterators and
\doxygen{itk}{NeighborhoodOperator}s.  The toolkit contains two types of image
morphology algorithms, filters that operate on binary images and filters that
operate on grayscale images.

\subsubsection{Binary Filters}
\label{sec:MathematicalMorphologyBinaryFilters}

\ifitkFullVersion
\input{MathematicalMorphologyBinaryFilters.tex}
\fi

\subsubsection{Grayscale Filters}
\label{sec:MathematicalMorphologyGrayscaleFilters}

\ifitkFullVersion
\input{MathematicalMorphologyGrayscaleFilters.tex}
\fi

%% \subsection{Voting Filters}
%% \label{sec:VotingFilters}

%% Voting filters are quite a generic family of filters. In fact, both the Dilate
%% and Erode filters from Mathematical Morphology are very particular cases of the
%% broader family of voting filters. In a voting filter, the outcome of a pixel is
%% decided by counting the number of pixels in its neighborhood and applying a
%% rule to the result of that counting.For example, the typical implementation of
%% Erosion in terms of a voting filter will be to say that a foreground pixel will
%% become background if the numbers of background neighbors is greater or equal
%% than 1. In this context, you could imagine variations of Erosion in which the
%% count could be changed to require at least 3 foreground.

%% \subsubsection{Binary Median Filter}

%% One of the particular cases of Voting filters is the BinaryMedianImageFilter.
%% This filter is equivalent to applying a Median filter over a binary image. The
%% fact of having a binary image as input makes possible to optimize the execution
%% of the filter since there is no real need for sorting the pixels according to
%% their frequency in the neighborhood.

%% \ifitkFullVersion
%% %\input{BinaryMedianImageFilter.tex}
%% \fi

%% The typical effect of median filtration on a noisy digital image is a dramatic reduction in impulse noise spikes. The filter also tends to preserve brightness differences across signal steps, resulting in reduced blurring of regional boundaries. The filter also tends to preserve the positions of boundaries in an image.

%% Figure \ref{fig:BinaryMedianImageFilterOutputMultipleIterations} below shows the effect of running the median filter with a 3x3 classical window size
%% 1, 10 and 50 times. There is a tradeoff in noise reduction and the sharpness of the image when the window size is increased\begin{figure}
%%   \center
%%   \includegraphics[width=0.44\textwidth]{BinaryMedianImageFilterOutput1.eps}
%%   \includegraphics[width=0.44\textwidth]{BinaryMedianImageFilterOutput10.eps}
%%   \includegraphics[width=0.44\textwidth]{BinaryMedianImageFilterOutput50.eps}
%%   \itkcaption[Effect of many iterations on the BinaryMedian filter.]{Effect of 1, 10 and 50 iterations of the
%%   BinaryMedianImageFilter using a 3x3 window.}
%%   \label{fig:BinaryMedianImageFilterOutputMultipleIterations}
%% \end{figure}.

%% \subsubsection{Hole Filling Filter}

%% Another variation of Voting filters is the Hole Filling filter. This filter
%% converts background pixels into foreground only when the number of foreground
%% pixels is a majority of the neighbors. By selecting the size of the majority,
%% this filter can be tuned to fill-in holes of different size. To be more
%% precise, the effect of the filter is actually related to the curvature of the
%% edge in which the pixel is located.

%% \ifitkFullVersion
%% %\input{VotingBinaryHoleFillingImageFilter.tex}
%% \fi

%% \subsubsection{Iterative Hole Filling Filter}

%% The Hole Filling filter can be used in an iterative way, by applying it
%% repeatedly until no pixel changes. In this context, the filter can be seen as a
%% binary variation of a Level Set filter.

%% \ifitkFullVersion
%% %\input{VotingBinaryIterativeHoleFillingImageFilter.tex}
%% \fi

\section{Smoothing Filters}
\label{sec:SmoothingFilters}

Real image data has a level of uncertainty that is manifested in the
variability of measures assigned to pixels. This uncertainty is usually
interpreted as noise and considered an undesirable component of the image
data. This section describes several methods that can be applied to reduce
noise on images.

\subsection{Blurring}
\label{sec:BlurringFilters}

Blurring is the traditional approach for removing noise from images. It is
usually implemented in the form of a convolution with a kernel. The effect of
blurring on the image spectrum is to attenuate high spatial
frequencies.  Different kernels attenuate frequencies in different ways. One
of the most commonly used kernels is the Gaussian. Two implementations of
Gaussian smoothing are available in the toolkit. The first one is based on a
traditional convolution while the other is based on the application of IIR
filters that approximate the convolution with a Gaussian
\cite{Deriche1990,Deriche1993}.

\subsubsection{Discrete Gaussian}
\label{sec:DiscreteGaussianImageFilter}

\ifitkFullVersion
\input{DiscreteGaussianImageFilter.tex}
\fi

%% \subsubsection{Binomial Blurring}
%% \label{sec:BinomialBlurImageFilter}

%% \ifitkFullVersion
%% %\input{BinomialBlurImageFilter.tex}
%% \fi

%% \subsubsection{Recursive Gaussian IIR}
%% \label{sec:RecursiveGaussianImageFilter}

%% \ifitkFullVersion
%% %\input{SmoothingRecursiveGaussianImageFilter.tex}
%% \fi

%% \subsection{Local Blurring}
%% \label{sec:BlurringFunctions}

%% In some cases it is desirable to compute smoothing in restricted regions of the
%% image, or to do it using different parameters that are computed locally.  The
%% following sections describe options for applying local smoothing in images.

%% \subsubsection{Gaussian Blur Image Function}
%% \label{sec:GaussianBlurImageFunction}

%% \ifitkFullVersion
%% %\input{GaussianBlurImageFunction.tex}
%% \fi

\subsection{Edge Preserving Smoothing}
\label{sec:EdgePreservingSmoothingFilters}

\subsubsection{Introduction to Anisotropic Diffusion}
\label{sec:IntroductionAnisotropicDiffusion}
\ifitkFullVersion
\input{AnisotropicDiffusionFiltering.tex}
\fi

\subsubsection{Gradient Anisotropic Diffusion}
\label{sec:GradientAnisotropicDiffusionImageFilter}

\ifitkFullVersion
\input{GradientAnisotropicDiffusionImageFilter.tex}
\fi

\subsubsection{Mean Shift filtering and clustering}
\label{sec:MeanShift}

\ifitkFullVersion
\input{MeanShiftSegmentationFilterExample.tex}
\fi

%% \subsubsection{Curvature Anisotropic Diffusion}
%% \label{sec:CurvatureAnisotropicDiffusionImageFilter}

%% \ifitkFullVersion
%% %\input{CurvatureAnisotropicDiffusionImageFilter.tex}
%% \fi

%% \subsubsection{Curvature Flow}
%% \label{sec:CurvatureFlowImageFilter}

%% \ifitkFullVersion
%% %\input{CurvatureFlowImageFilter.tex}
%% \fi

%% \subsubsection{MinMaxCurvature Flow}
%% \label{sec:MinMaxCurvatureFlowImageFilter}

%% \ifitkFullVersion
%% %\input{MinMaxCurvatureFlowImageFilter.tex}
%% \fi

%% \subsubsection{Bilateral Filter}
%% \label{sec:BilateralImageFilter}

%% \ifitkFullVersion
%% %\input{BilateralImageFilter.tex}
%% \fi

%% \subsection{Edge Preserving Smoothing in Vector Images}
%% \label{sec:VectorAnisotropicDiffusion}

%% Anisotropic diffusion can also be applied to images whose pixels are vectors.
%% In this case the diffusion is computed independently for each vector
%% component.  The following classes implement versions of anisotropic diffusion
%% on vector images.

%% \subsubsection{Vector Gradient Anisotropic Diffusion}
%% \label{sec:VectorGradientAnisotropicDiffusionImageFilter}

%% \ifitkFullVersion
%% %\input{VectorGradientAnisotropicDiffusionImageFilter.tex}
%% \fi

%% \subsubsection{Vector Curvature Anisotropic Diffusion}
%% \label{sec:VectorCurvatureAnisotropicDiffusionImageFilter}

%% \ifitkFullVersion
%% %\input{VectorCurvatureAnisotropicDiffusionImageFilter.tex}
%% \fi

%% \subsection{Edge Preserving Smoothing in Color Images}
%% \label{sec:ColorAnisotropicDiffusion}

%% \subsubsection{Gradient Anisotropic Diffusion}
%% \label{sec:ColorGradientAnisotropicDiffusion}

%% \ifitkFullVersion
%% %\input{RGBGradientAnisotropicDiffusionImageFilter.tex}
%% \fi

%% \subsubsection{Curvature Anisotropic Diffusion}
%% \label{sec:ColorCurvatureAnisotropicDiffusion}

%% \ifitkFullVersion
%% %\input{RGBCurvatureAnisotropicDiffusionImageFilter.tex}
%% \fi

\subsection{Edge Preserving Speckle Reduction Filters}
\label{sec:SpeckleFilters}
\ifitkFullVersion
\input{LeeImageFilter.tex}
\input{FrostImageFilter.tex}
\fi

\subsection{Edge preserving Markov Random Field}

The Markov Random Field framework for OTB is more detailed in \ref{sec:MarkovRandomFieldOTB} (p. \pageref{sec:MarkovRandomFieldOTB}).

\index{Markov!Filtering}
\index{Markov!Restoration}
\ifitkFullVersion
\input{MarkovRestorationExample.tex}
\fi

\section{Distance Map}
\label{sec:DistanceMap}

\index{Distance map}
\ifitkFullVersion
\input{DanielssonDistanceMapImageFilter.tex}
\fi

%\section{Rasterization}

%Rasterization is the process of rendering vectorial data on a raster
%grid. This rasterization can be either binary or more complete,
%including different styles and labels for the vectorial features.For
%rasterization purposes, OTB uses the Mapnik library through the
%\doxygen{otb}{VectorDataToImageFilter}. Hence, rasterization will be
%only available to users compiling OTB with the Mapnik CMake option
%set to ON, or using a binary package with Mapnik activated.

%\input{RasterizationExample.tex}

%% \ifitkFullVersion
%% %\input{SignedDanielssonDistanceMapImageFilter.tex}
%% \fi

%% \section{Geometric Transformations}
%% \label{sec:GeometricalTransformationFilters}

%% \subsection{Filters You Should be Afraid to Use}

%% \label{sec:ScaryImageFilters}
%% \subsection{Change Information Image Filter}

%% This one is the scariest and more dangerous filter in the entire toolkit. You
%% should not use this filter unless you are entirely certain that you know what
%% you are doing. In fact if you decide to use this filter, you should write your
%% code, then go for a long walk, get more coffee and ask yourself if you really
%% needed to use this filter. If the answer is yes, then you should discuss this
%% issue with someone you trust and get his/her opinion in writing.  In general,
%% if you need to use this filter, it means that you have a poor image provider
%% that is putting your career at risk along with the life of any potential
%% patient whose images you may end up processing.

%% \subsection{Flip Image Filter}

%% \ifitkFullVersion
%% %\input{FlipImageFilter.tex}
%% \fi

%% \subsection{Resample Image Filter}
%% \label{sec:ResampleImageFilter}

%% \subsubsection{Introduction}

%% \ifitkFullVersion
%% %\input{ResampleImageFilter.tex}
%% \fi

%% \subsubsection{Importance of Spacing and Origin}
%% \ifitkFullVersion
%% %\input{ResampleImageFilter2.tex}
%% \fi

%% \subsubsection{A Complete Example}
%% \ifitkFullVersion
%% %\input{ResampleImageFilter3.tex}
%% \fi

%% \subsubsection{Rotating an Image}
%% \ifitkFullVersion
%% %\input{ResampleImageFilter4.tex}
%% \fi

%% \subsubsection{Rotating and Scaling an Image}
%% \ifitkFullVersion
%% %\input{ResampleImageFilter5.tex}
%% \fi

%% \subsubsection{Resampling using a deformation field}
%% \ifitkFullVersion
%% %\input{WarpImageFilter1.tex}
%% \fi

%% \subsubsection{Subsampling and image in the same space}
%% \label{SubsampleVolume}

%% \ifitkFullVersion
%% %\input{SubsampleVolume.tex}
%% \fi

%% \subsubsection{Resampling an Anisotropic image to make it Isotropic}
%% \label{ResampleVolumesToBeIsotropic}

%% \ifitkFullVersion
%% %\input{ResampleVolumesToBeIsotropic.tex}
%% \fi

%% \section{Frequency Domain}
%% \label{sec:FrequencyDomain}

%% \subsection{Computing a Fast Fourier Transform (FFT)}
%% \label{FFTImageFilter}

%% \ifitkFullVersion
%% %\input{FFTImageFilter.tex}
%% \fi

%% \subsection{Filtering on the Frequency Domain}
%% \label{FFTImageFilterFourierDomainFiltering}

%% \ifitkFullVersion
%% %\input{FFTImageFilterFourierDomainFiltering.tex}
%% \fi

%% \section{Extracting Surfaces}
%% \label{sec:ExtractingSurfaces}

%% \subsection{Surface extraction}
%% \label{sec:SufaceExtraction}
%% \index{Surface Extraction}

%% \ifitkFullVersion
%% %\input{SurfaceExtraction.tex}
%% \fi