%
% personal commentary:
%        DRAFT DRAFT DRAFT
%        - KFALL
% RNG section reviewed and revised 11-Sep-98, johnh.
%
% RNG section revised 28-Aug-02 by Tim Buchheim to include material
% from Michelle Weigle which describes the new implementation of RNG

\chapter{Mathematical Support}
\label{chap:math}

The simulator includes a small collection of mathematical
functions used to implement random variate generation and integration.
This area of the simulator is currently undergoing some
changes.

The procedures and functions described in this chapter can be found in
\nsf{tools/rng.\{cc, h\}},
\nsf{tools/random.\{cc, h\}},
\nsf{tools/ranvar.\{cc, h\}},
\nsf{tools/pareto.\{cc, h\}},
\nsf{tools/expoo.\{cc, h\}},
\nsf{tools/integrator.\{cc, h\}}, and
\nsf{tcl/lib/ns-random.tcl}.

\section{Random Number Generation}
\label{sec:random}

The RNG class contains an implementation of the combined multiple
recursive generator MRG32k3a proposed by L'Ecuyer
\cite{lecuyer99}. The C++ code was adapted from \cite{lecuyer01}.
This replaces the previous implementation of \code{RNG}, which used
the minimal standard multiplicative linear congruential generator of
Park and Miller~\cite{Park88:Random}.  The newer (MRG32k3a) \code{RNG} is
used in ns versions 2.1b9 and later.

The MRG32k3a generator provides $1.8$x$10^{19}$ independent
streams of random numbers, each of which consists of
$2.3$x$10^{15}$ substreams. Each substream has a period
(\emph{i.e.}, the number of random numbers before overlap) of
$7.6$x$10^{22}$. The period of the entire generator is
$3.1$x$10^{57}$. Figure \ref{streams} provides a graphical idea of
how the streams and substreams fit together.
\begin{figure}[ht]
\centering
\includegraphics[angle=270,width=6 in]{rng-streams.eps}
\caption{Overall arrangement of streams and substreams.
\cite{lecuyer01}} \label{streams}
\end{figure}

A default RNG ({\tt defaultRNG}), created at simulator initialization
time, is provided. If multiple random variables are used in a
simulation, each random variable should use a separate RNG object.
When a new RNG object is created, it is automatically seeded to
the beginning of the next independent stream of random numbers.
Used in this manner, the implementation allows for a maximum of
$1.8$x$10^{19}$ random variables.

Often, multiple independent replications of a simulation are
needed (\emph{i.e.}, to perform statistical analysis given
multiple runs with fixed parameters).  For each replication, a
different substream should be used to ensure that the random
number streams are independent. (This process is given as an OTcl
example later.) This implementation allows for a maximum of
$2.3$x$10^{15}$ independent replications. Each random variable in
a single replication can produce up to $7.6$x$10^{22}$ random
numbers before overlapping.

{\bf Note:} Only the most common functions are described here. For
more information, see \cite{lecuyer01} and the source code found
in {\tt tools/rng.h} and {\tt tools/rng.cc}.  For a comparison of this RNG to
the more common LCG16807 RNG (and why LCG16807 is not a good RNG),
see \cite{lecuyer-wsc}.
\subsection{Seeding The RNG}
Due to the nature of the RNG and its implementation, it is not
necessary to set a seed (the default is 12345).  If you wish to
change the seed, functions are available. You should only set the
seed of the default RNG.  Any other RNGs you create are
automatically seeded such that they produce independent streams.
The range of valid seeds is 1 to {\tt MAXINT}.

To get non-deterministic behavior, set the seed of the default RNG
to 0.  This will set the seed based on the current time of day and
a counter.  {\bf This method should not be used to set seeds for
independent replications.}  There is no guarantee that the streams
produced by two random seeds will not overlap.  The only way to
guarantee that two streams do not overlap is to use the substream
capability provided by the RNG implementation.

\subsubsection{Example}
\begin{verbatim}
 # Usage: ns rng-test.tcl [replication number]

 if {$argc > 1} {
    puts "Usage: ns rng-test.tcl \[replication number\]"
    exit
 }
 set run 1
 if {$argc == 1} {
    set run [lindex $argv 0]
 }
 if {$run < 1} {
    set run 1
 }

 # seed the default RNG
 global defaultRNG
 $defaultRNG seed 9999

 # create the RNGs and set them to the correct substream
 set arrivalRNG [new RNG]
 set sizeRNG [new RNG]
 for {set j 1} {$j < $run} {incr j} {
    $arrivalRNG next-substream
    $sizeRNG next-substream
 }

 # arrival_ is a exponential random variable describing the time
 # between consecutive packet arrivals
 set arrival_ [new RandomVariable/Exponential]
 $arrival_ set avg_ 5
 $arrival_ use-rng $arrivalRNG

 # size_ is a uniform random variable describing packet sizes
 set size_ [new RandomVariable/Uniform]
 $size_ set min_ 100
 $size_ set max_ 5000
 $size_ use-rng $sizeRNG

 # print the first 5 arrival times and sizes
 for {set j 0} {$j < 5} {incr j} {
    puts [format "%-8.3f  %-4d" [$arrival_ value] \
            [expr round([$size_ value])]]
 }
\end{verbatim}

\subsubsection{Output}

\begin{verbatim}
 % ns rng-test.tcl 1
 6.358     4783
 5.828     1732
 1.469     2188
 0.732     3076
 4.002     626

 % ns rng-test.tcl 5
 0.691     1187
 0.204     4924
 8.849     857
 2.111     4505
 3.200     1143
\end{verbatim}

\subsection{OTcl Support}

\subsubsection{Commands}

The following commands on the RNG class can be accessed from OTcl
and are found in {\tt tools/rng.cc}:
\begin{description}
    \item[{\tt seed $n$}] -- seed the RNG to $n$, if $n == 0$, the seed
    is set according to the current time and a counter
    \item[{\tt next-random}] -- return the next random number
    \item[{\tt seed}] -- return the current value of the seed
    \item[{\tt next-substream}] -- advance to the next substream
    \item[{\tt reset-start-substream}] -- reset the stream to the beginning
    of the current substream
    \item[{\tt normal $avg$ $std$}] -- return a number sampled from a normal
    distribution with the given average and standard deviation
    \item[{\tt lognormal $avg$ $std$}] -- return a number sampled from a
      lognormal distribution with the given average and standard deviation
\end{description}

The following commands on the RNG class can be accessed from OTcl
and are found in {\tt tcl/lib/ns-random.tcl}:
\begin{description}
    \item[{\tt exponential $mu$}] -- return a number sampled from an
      exponential distribution with mean $mu$
    \item[{\tt uniform $min$ $max$}] -- return an integer sampled from a
    uniform distribution on [$min$, $max$]
    \item[{\tt integer $k$}] -- return an integer sampled from a uniform
    distribution on [0, $k$-1]
\end{description}

\subsubsection{Example}

\begin{verbatim}
 # Usage: ns rng-test2.tcl [replication number]

 if {$argc > 1} {
    puts "Usage: ns rng-test2.tcl \[replication number\]"
    exit
 }
 set run 1
 if {$argc == 1} {
    set run [lindex $argv 0]
 }
 if {$run < 1} {
    set run 1
 }

 # the default RNG is seeded with 12345

 # create the RNGs and set them to the correct substream
 set arrivalRNG [new RNG]
 set sizeRNG [new RNG]
 for {set j 1} {$j < $run} {incr j} {
    $arrivalRNG next-substream
    $sizeRNG next-substream
 }

 # print the first 5 arrival times and sizes
 for {set j 0} {$j < 5} {incr j} {
    puts [format "%-8.3f  %-4d" [$arrivalRNG lognormal 5 0.1] \
            [expr round([$sizeRNG normal 5000 100])]]
 }
\end{verbatim}

\subsubsection{Output}

\begin{verbatim}
 % ns rng-test2.tcl 1
 142.776   5038
 174.365   5024
 147.160   4984
 169.693   4981
 187.972   4982

 % ns rng-test2.tcl 5
 160.993   4907
 119.895   4956
 149.468   5131
 137.678   4985
 158.936   4871
\end{verbatim}

\subsection{C++ Support}

\subsubsection{Member Functions}
The random number generator is implemented by the RNG class and is
defined in {\tt tools/rng.h}.

{\bf Note:} The Random class in {\tt tools/random.h} is an older
interface to the standard random number stream.

Member functions provide the following operations:

\begin{description}
    \item[{\tt void set\_seed (long seed)}] -- set the seed of the RNG, if
    $seed == 0$, the seed is set according to the current time and a
    counter
    \item[{\tt long seed (void)}] -- return the current seed
    \item[{\tt long next (void)}] -- return the next random number as an
    integer on [0, {\tt MAXINT}]
    \item[{\tt double next\_double (void)}] -- return the next random number
    on [0, 1]
    \item[{\tt void reset\_start\_substream (void)}] -- reset the stream
    to the beginning of the current substream
    \item[{\tt void reset\_next\_substream (void)}] -- advance to the next
    substream
    \item[{\tt int uniform (int k)}] -- return an integer sampled from a
      uniform distribution on [0, k-1]
    \item[{\tt double uniform (double r)}] -- return a number sampled from a
      uniform distribution on [0, r]
    \item[{\tt double uniform (double a, double b)}] -- return a number
      sampled from a uniform distribution on [a, b]
    \item[{\tt double exponential (void)}] -- return a number sampled from an
    exponential distribution with mean 1.0
    \item[{\tt double exponential (double k)}] -- return a number sampled from
      an exponential distribution with mean k
    \item[{\tt double normal (double avg, double std)}] -- return a number
      sampled from a normal distribution with the given average and standard
    deviation
    \item[{\tt double lognormal (double avg, double std)}] -- return a number
      sampled from a lognormal distribution with the given average and
      standard deviation
\end{description}

\subsubsection{Example}

\begin{verbatim}
 /* create new RNGs */
 RNG arrival (23456);
 RNG size;

 /* set the RNGs to the appropriate substream */
 for (int i = 1; i < 3; i++) {
   arrival.reset_next_substream();
   size.reset_next_substream();
 }

 /* print the first 5 arrival times and sizes */
 for (int j = 0; j < 5; j++) {
   printf ("%-8.3f  %-4d\n", arrival.lognormal(5, 0.1),
             int(size.normal(500, 10)));
 }
\end{verbatim}

\subsubsection{Output}
\begin{verbatim}
 161.826   506
 160.591   503
 157.145   509
 137.715   507
 118.573   496
\end{verbatim}

\section{Random Variables}
\label{sec:ranvar}

The \clsref{RandomVariable}{../ns-2/ranvar.h}
provides a thin layer of functionality on top
of the base random number generator and the default random number stream.
It is defined in \nsf{ranvar.h}:

\begin{program}
  class RandomVariable : public TclObject \{
  public:
        virtual double value() = 0;
        int command(int argc, const char*const* argv);
        RandomVariable();
  protected:
        RNG* rng_;
  \};
\end{program}

Classes derived from this abstract class implement specific
distributions.  Each distribution is parameterized with the values of
appropriate parameters.  The value method is used to return a value
from the distribution.

The currently defined distributions, and their associated parameters are:

\begin{tabular}{rl}
\clsref{UniformRandomVariable}{tools/ranvar.h} & \code{min_}, \code{max_} \\
\clsref{ExponentialRandomVariable}{tools/ranvar.h} & \code{avg_} \\
\clsref{ParetoRandomVariable}{tools/ranvar.h} & \code{avg_}, \code{shape_}\\
\clsref{ParetoIIRandomVariable}{tools/ranvar.h} & \code{avg_}, \code{shape_}\\
\clsref{ConstantRandomVariable}{tools/ranvar.h} & \code{val_}\\
\clsref{HyperExponentialRandomVariable}{tools/ranvar.h} & \code{avg_}, \code{cov_}\\
\clsref{NormalRandomVariable}{tools/ranvar.h} & \code{avg_}, \code{std_}\\
\clsref{LogNormalRandomVariable}{tools/ranvar.h} & \code{avg_}, \code{std_}\\
\end{tabular}

The RandomVariable class is available in OTcl.  For instance, to
create a random variable that generates number uniformly on [10, 20]:
\begin{program}
        set u [new RandomVariable/Uniform]
        $u set min_ 10
        $u set max_ 20
        $u value
\end{program}
By default, RandomVariable objects use the default random number
generator described in the previous section.  The use-rng method can
be used to associate a RandomVariable with a non-default RNG:
\begin{program}
        set rng [new RNG]
        $rng seed 0

        set e [new RandomVariable/Exponential]
        $e use-rng $rng
\end{program}

\section{Integrals}
\label{sec:integral}

The  \clsref{Integrator}{../ns-2/integrator.h}
supports the approximation of (continuous) integration by (discrete)
sums; it is defined in \nsf{integrator.h} as
\begin{program}
{\rm From integrator.h:}
        class Integrator : public TclObject \{
        public:
                Integrator();
                void set(double x, double y);
                void newPoint(double x, double y);
                int command(int argc, const char*const* argv);
        protected:
                double lastx_;
                double lasty_;
                double sum_;
        \};

{\rm From integrator.cc:}
        Integrator::Integrator() : lastx_(0.), lasty_(0.), sum_(0.)
        \{
                bind("lastx_", &lastx_);
                bind("lasty_", &lasty_);
                bind("sum_", &sum_);
        \}

        void Integrator::set(double x, double y)
        \{
                lastx_ = x;
                lasty_ = y;
                sum_ = 0.;
        \}

        void Integrator::newPoint(double x, double y)
        \{
                sum_ += (x - lastx_) * lasty_;
                lastx_ = x;
                lasty_ = y;
        \}

        int Integrator::command(int argc, const char*const* argv)
        \{
                if (argc == 4) \{
                        if (strcmp(argv[1], "newpoint") == 0) \{
                                double x = atof(argv[2]);
                                double y = atof(argv[3]);
                                newPoint(x, y);
                                return (TCL_OK);
                        \}
                \}
                return (TclObject::command(argc, argv));
        \}
\end{program}
This class provides a base class used by other classes such
as \code{QueueMonitor} that keep running sums.
Each new element of the running sum is added by
the \fcn[x, y]{newPoint} function.
After the $k$th execution of \code{newPoint}, the running sum
is equal to $\sum_{i=1}^{k}y_{i-1}(x_i - x_{i-1})$ where
$x_0 = y_0 = 0$ unless \code{lastx\_}, \code{lasty\_}, or \code{sum\_}
are reset via OTcl.
Note that a new point in the sum can be added either by the
C++ member \code{newPoint} or the OTcl member \code{newpoint}.
The use of integrals to compute certain types of averages
(e.g. mean queue lengths) is given in (pp. 429--430, \cite{Jain91:Art}).

\section{\code{ns-random}}

{\bf \code{ns-random} is an obsolete way to generate random numbers.
This information is provided only for backward compatibility.}

\code{ns-random} is implemented in \nsf{misc.\{cc,h\}}.
When called with no argument, it generates a random number with
uniform distribution between 0 and \code{MAXINT}.
When an integer argument is provided, it seeds the random generater
with the given number.
A special case is when \code{ns-random 0} is called, it randomly seeds
the generator based on current time.
This feature is useful to produce non-deterministic results across
runs.

\section{Some mathematical-support related objects}
\label{sec:mathobjects}

\textsc{Integrator objects}
Integrator Objects support the approximate computation of continuous
integrals using discrete sums. The running sum(integral) is computed as:
\code{sum_ += [lasty_ * (x lastx_)]} where (x, y) is the last element
entered and
(lastx\_, lasty\_) was the element previous to that added to the sum.
lastx\_ and lasty\_ are updated as new elements are added. The first
sample point defaults to (0,0) that can be changed by changing the values
of (lastx\_,lasty\_).
\code{$integrator newpoint <x> <y>}\\ %$
Add the point (x,y) to the sum. Note that it does not make sense for x to
be less than lastx\_.

There are no configuration parameters specific to this object. \\

State Variables are:
\begin{description}
\item[lastx\_]
x-coordinate of the last sample point.

\item[lasty\_]
y-coordinate of the last sample point.

\item[sum\_] Running sum (i.e. the integral) of the sample points.
\end{description}

\textsc{Samples Object}
Samples Objects support the computation of mean and variance statistics
for a given sample.

\code{$samples mean}\\
Returns mean of the sample.

\code{$samples variance}\\
Returns variance of the sample.

\code{$samples cnt}\\
Returns a count of the sample points considered.

\code{$samples reset }\\
Reset the Samples object to monitor a fresh set of samples.

There are no configuration parameters or state variables specific to this
object.

\section{Commands at a glance}
\label{sec:mathcommand}

Following is a list of mathematical support related commands commonly used
in simulation scripts:
\begin{flushleft}
\code{set rng [new RNG]}\\
This creates a new random number generator.

\code{$rng seed <0 or n>}\\
This command seeds the RNG. If 0 is specified, the RNG is seeded
heuristically. Otherwise the RNG is seeded with the value <n>.

\code{$rng next-random}\\
This returns the next random number from RNG.

\code{$rng uniform <a> <b>}\\
This returns a number uniformly distributed on <a> and <b>.

\code{$rng integer <k>}\\
This returns an integer uniformly distributed on 0 and k-1.

\code{$rng exponential}\\
This returns a number that has exponential distribution with average 1.

\code{set rv [new Randomvariable/<type of random-variable>]}\\
This creates an instance of a random variable object that generates random
variables with specific distribution. The different types of random
variables derived from the base class are:\\
RandomVariable/Uniform, RandomVariable/Exponential, RandomVariable/Pareto,
RandomVariable/Constant, RandomVariable/HyperExponential.
Each of these distribution types are parameterized with values of
appropriate parameters. For details see section \ref{sec:ranvar} of this
chapter.

\code{$rv use-rng <rng>}\\
This method is used to associated a random variable object with a
non-default RNG. Otherwise by default, the random variable object is
associated with the default random number generator.

\end{flushleft}

% LocalWords:  ranvar pareto expoo tcl lib ns cc integrator RNG rng TclObject
% LocalWords:  enum RNGSources PREDEF int MAXINT rX edv prob edp setbit iph ECN
% LocalWords:  ip ecn eed OURCE OTcl RandomVariable argc const argv min avg val
% LocalWords:  UniformRandomVariable ExponentialRandomVariable cov newPoint pp
% LocalWords:  ParetoRandomVariable ConstantRandomVariable lastx lasty strcmp
% LocalWords:  HyperExponentialRandomVariable newpoint atof QueueMonitor


\documentclass[11pt,twoside,a5paper]{article}

\usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.2cm,right=2.2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{verbatim}
\usepackage[normalem]{ulem} % for striking out with \sout
\usepackage{amsfonts,amsmath,amssymb} % for more math support
\usepackage{times}
\usepackage[italic]{mathastext} % use normal text font (times) in equations
\usepackage{enumitem}
%\usepackage[draft]{hyperref}
\usepackage[colorlinks,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
\usepackage{color}
\usepackage{listings}

\newcommand{\dc}{\mathcal{D} }

\begin{document}
\title{3x2 Covariance emulator}
%\author{Tom McClintock}
\maketitle

\section{Emulator in the big picture}
Our covariance emulator will be quite simple: it will only take in a cosmology and then spit out a matrix to the user. The function signature for making predictions will look something like
\begin{lstlisting}[language=Python]
def predict_matrix(cosmology):
    """
    Inputs:
    cosmology - a container of the cosmological parameters

    Outputs:
    C_inverse - an NxN array containing the inverse covariance matrix.
    The dimensions will be specified by the training data,
    which we have to assume will never change, unless we
    want to get real fancy.
    """
    #do things
    return C_inverse.
\end{lstlisting}
Internally, the algorithm used to make a prediction with the emulator will look like the following:
\begin{enumerate}
\item Predict some small number of important quantities, $X$, that depend on cosmology $\Omega$ such as PCA components of some matrix.
\item Use $X$ to reconstruct the diagonal components $D$ and the off-diagonal components $T$ of the GCD components.
\item Reassemble the GCD components into the inverse covariance matrix $C^{-1}$.
\end{enumerate}
This algorithm is thus the reverse of section 3 from Morrison \& Schneider (2013), where they describe how the emulator is designed. The prediction could be made with Gaussian processes, but doesn't have to be. If, for instance, we observed that some PCA components depended very simply on a certain cosmological parameter then we could model that directly.

Thus, to train our emulator we would apply this prediction code in reverse: take all the input inverse covariance matrices, reduce them to only a few numbers, find a good interpolation of those numbers as a function of cosmology. The following sections will describe how the emulator should be trained.

\section{Matrix decomposition: GCD}
Following Morrison \& Schneider, we should probably use GCD, but this isn't a necessity. The inverse covariance matrix, which is $N\times N$ gets decomposed like (I'm using slightly different notation than in their paper, for convenience)
\begin{equation}
  \label{eq:gcd}
  C^{-1} = L^TDL
\end{equation}
where $L$ is a lower triangular matrix (and therefore has $N(N-1)/2$ indepenent elements) and $D$ is a diagonal semi-positive definite matrix (with $N$ independent elements). Emulating $N(N-1)/2+N$ independent elements isn't feasible, so we have to reduce this further with PCA.

Before moving on, I should note that elements of $L$ can take on any numerical value, but since $D$ is semi-positive then we will work with $\ln D = d$ instead. In other words, we will work with the logarithm of the elements of $D$. This is important because the Gaussian process wouldn't have knowledge that $D$ is semi-positive, and could yield unphysical predictions.

We also subtract of the mean of all $d$ values and rescale the values of $d$ so that they have variance 1. This looks like
\begin{equation}
  \label{eq:rescale}
  d \rightarrow \frac{d - \bar{d}}/\sigma_d.
\end{equation}
This makes PCA described next slightly cleaner. When making the prediction, once we have $d$ from the Gaussian processes, we can transform it back to its original form by multiplying on $\sigma_d$ and adding $\bar{d}$. The same treatment goes for the avlues of $L$.

\section{PCA on the GCD parts}
Let's pretend that we have run $M$ different simulations (so for us, $M\sim16$). Let's think just about $d$ first. If we take all the $d$s obtained for each $M$ simulation and ``stack'' them on top of each other into a new matrix $\dc$ which is now $N\times M$ (row/column order doesn't matter). Performing PCA on this gives us
\begin{equation}
  \label{eq:PCA}
  \dc = UBV^T
\end{equation}
where if $p=\min(N,M)$ then $U$ is a $N\times p$ matrix with $U^TU=I_p$, $V$ is a $M\times p$ with $V^TV=I_p$ and $VV^T=I_M$, and $B$ is $p\times p$ diagonal matrix of singular values. Basis vectors are formed by $\Phi = \frac{1}{\sqrt{M}}UB$ which is $N\times p$, with weights $w=\sqrt{M}V^T$ normalized by $\frac{1}{\sqrt{M}}w^Tw=I_M$. The columns of $\Phi$, which are of length $N$, represent descreasingly important principle components of the original matrix $\dc$. This means we only need to use the first few of them. In the Coyote papers they found that using the first five was sufficient. This will be something to test.

The $i$th column of $\Phi$ corresponds to the $i$th row of $w$, or $w_i$. This $w_i$ has mean zero, and (according to the papers...) contains all of the cosmologial dependence, and is also a function of the domain.

In the coyote papers, the domain was $k$, whereas in our paper we have this very strange behaviour where the domain is not monotonic, since the ``full'' data vector is a combination of a bunch of data vectors. In other words, looking at Figure 5 of Elizabeth's paper, the data vectors are $\xi_+$, $\xi_-$, $\gamma_t$, and $w$. The domain is not monotonically changing along the edge, which isn't viable for design of the emulator. We will have to think of something, like using just bin number or something. The downside of doing so would mean that the number of bins would have to remain fixed forever.

\section{Gaussian Process}
In this design if we keep five columns then we would utilize five interpolators. We can do this because the weights are all independent of each other, so the prediction by one interpolator is independent of any other interpolator. The challenge we have is to figure out the best way to interpolate $w_i$ across cosmologies and domain. Gaussian processes (GPs) were used in the past because they provide an estimate of the expectation value $\langle w_i(\Omega,x)\rangle$ as well as an estimate of the variance ${\rm Var}(w_i)$. Note that we could achieve the same thing if we defined a model (a line, a quadratic, something else) for $w_i$ and knew the posterior distributions of the parameters in our model. Using GPs or such a model means that we can propogate the uncertainty in our estimate forward in a Bayesian way. On the other hand this could be computational expensive and end up being not adding much to a cosomological analysis.

I'll fill the rest of this out later, but the important thing that we care about is picking kernels with hyperparameters, writing a likelihood, and then finding the best hyperparameters.

\end{document}


%Appendix -- January 2015
\appendix
\renewcommand{\thechapter}{B}
\renewcommand{\chaptername}{Appendix}

\chapter{New Experiments}

The understanding of nonlinear processes in optical fibers is crucial towards
extending the capabilities of modern optical communication systems based on
wavelength division multiplexing (WDM), where each communication channel is
represented by a unique wavelength. One of the nonlinear processes that
limits the information carrying capacity of a WDM system is four-wave mixing
(FWM), which causes cross-talk between neighboring channels. This places a
lower limit on the wavelength separation between adjacent channels and an
upper limit on the input power in each channel. In this study, we describe
a process by which the evolution of FWM processes in an optical fiber can be
used to estimate the inhomogeneities in the fiber core material, in particular
the fluctuations in the linear refractive index of the fiber core.

Experiments measuring the evolution of FWM processes along a length of fiber
were carried out by Hart {\it et al.}\ \cite{hart1} and are described in detail in
Sec.\ 2.2. In this experiment, two input pump waves at frequencies
$\omega_1$ and $\omega_2$, interacted with each other through the third-order
nonlinearity of the fiber material to generate first-order sidebands at frequencies
$\omega_3 = 2\omega_1 - \omega_2$ and $\omega_4 = 2\omega_2 - \omega_1$.
These waves further interacted to produce second-order sidebands at
$\omega_5 = 2\omega_3 - \omega_4$ and $\omega_6 = 2\omega_4 - \omega_3$.
Higher-order sidebands were also generated. The normalized power in the
sideband at frequency $\omega_m$ was represented by $\rho_m$. The
evolution of the FWM processes was characterized by the evolution of
$\rho_m$(z) as a function of fiber length z.

\section{Background}

In the present work, we make a quantitative comparison between these
experimental results and our numerical results based on efficient algorithms
\cite{Agrawal2} to solve the nonlinear Schr\"odinger equation (NLSE) that
governs the system. The numerical model, its underlying assumptions and
the results are described in Sec.\ B.3. A realistic description of a
standard single mode optical fiber must take into account the random phase
perturbations a light wave undergoes while propagating through it, without
disturbing the underlying conservative properties of the system. The NLSE
needs to be suitably modified in order to incorporate the stochastic nature
of the propagation. In order to preserve the conservative properties of the
system, the stochastic terms in the NLSE must necessarily be multiplicative in
nature as an additive term acts as a source or a sink. An algorithm that
achieves this with linear, Gaussian, $\delta$-correlated noise is outlined in
Sec.\ B.3. This algorithm preserves the unconditional stability of the
system. At the same time, care is taken to transform the stochastic NLSE from
its original Ito representation \cite{ito} to the computationally feasible Stratanovich
representation \cite{stratanovich} by compensating for the
spurious linear drift that results from integrating such stochastic
differential equations \cite{risken,werner2,drummond1,carter3}. The dominant
sources of phase noise are discussed in Sec.\ B.4.

Conclusions on the relevance of the experiments of Hart {\it et al.}\ \cite{hart1}
and the stochastic modeling presented here are summarized in Sec.\ B.5.

\section{Experimental and Computational Background}

In this work, we focus on tracing the evolution of the sidebands, generated
through FWM, along a length of optical fiber. The FWM spectral evolution along
50\,m of fiber for two input pump power regimes (2.1\,W and 5.5\,W) was
investigated \cite{hart1}. In the 2.1\,W case, the sideband evolution followed a damped
sinusoid along the length of the fiber. The experiments also found that the
two first-order sidebands ($\rho_3$-blueshifted and $\rho_4$-redshifted from
the two pumps) had different evolutions along the fiber (with different
spatial wavelengths). For the 5.5\,W case, the evolution of both first- and
second-order sidebands was measured. The damping in the first-order sidebands
($\rho_3$ and $\rho_4$) occured faster than in the 2.1\,W case. Experiments
probing the dependence of the sideband power on the input power (ranging from
2\,W to 17\,W) were also performed at a fixed output length of 50\,m of the fiber.
At the same fiber length, the optical spectra for input powers ranging from
2\,W to 17\,W were also recorded \cite{hart1}. The spectral envelopes were observed to fit
well to a hyperbolic secant function and the fit parameters were recorded.
Measurements with a high-resolution wavemeter showed that one of the two pumps
consisted of two very closely spaced longitudinal modes
($\Delta\nu\sim$ 0.5\,GHz) which were not resolved by the spectrometer used to
record the FWM spectra. Inclusion of this multimode nature of the pump input
in their model was found to alter the sideband dynamics dramatically and
partly explained the asymmetry between the blue-shifted and red-shifted
sidebands though it did not account for the damping in the sidebands. This
was accounted for by adding weak phase fluctuations to the waves as they
propagated along the fiber \cite{hart1}. The physical source of these phase fluctuations
was not known at that time. However, the inclusion of the phase fluctuations
into the model gave excellent qualitative and quantitative agreement with
experiment. Their model involved integration of a system of coupled ODEs
derived from the NLSE \cite{thompson1} by a process of truncation that
retained only the leading frequency components (the pumps and the first- and
second-order sidebands), a process justified by the fact that the input pump
waves are well approximated by a combination of monochromatic waves. Their
final numerical results are based on simulations using the truncated-ODE model
with Langevin noise terms representing phase fluctuations in the fiber.
Another physical source of stochasticity in their experiment was the inherent
power fluctuation in the lasers used as the input pumps. The level of
fluctuations (5-20\%) was measured and incorporated appropriately into their
model through stochastic initial conditions. This explained the evolution of
the level of observed fluctuations in the sideband trajectories although it
was found to be inadequate by itself, to account for the damping of the
trajectories. They found that all three physical characteristics mentioned
above, namely the multimode nature of the pump input, the stochastic phase
fluctuations along the length of the fiber, and the stochastic initial power
fluctuations were crucial to explaining the different features of the
experimental measurements \cite{hart1}.

\section{Stochastic NLSE Model}

In the present work, we have developed and implemented an unconditionally
stable scheme for integrating the NLSE that successfully incorporates phase
noise into the SSFM. Thus, we are now in a position to harness the high
frequency / time resolution of the SSFM together with its efficient
convergence properties. Due to these advances, we are now able to do
simulations with much higher frequency resolution (60\,MHz as compared to
300\,GHz in the ODE model). This high resolution, coupled with an appropriate
convolution scheme, enables us to compare these simulated spectra with the
composite spectra observed by the spectrometers which had a resolution of
$\sim$ 60\,GHz. This was not possible with the truncated ODE model as the
resolution of the simulated spectra in that case was $\sim$ 300\,GHz. For
exactly the same levels of phase fluctuations, and initial condition
fluctuations as used in Ref.\ \cite{hart1}, comparisons for the present NLSE
model with the experimental sideband evolution functions $\rho_i(z)$ show
excellent quantitative agreement. These results, along with the algorithms
employed, are described in detail in this section. We have identified linear
refractive index fluctuations along the fiber length to be a strong candidate
for a physical source of the stochastic phase fluctuations. A comparison
between the various possible sources is given in Sec.\ B.4.

Under the assumption that the electric field of the light in the fiber has a
slowly varying envelope $A(z,\tau)$, and that the fiber medium has an
instantaneous nonlinear response, the system is well described by the
nonlinear Schr\"{o}dinger equation (NLSE) with a linear multiplicative
stochastic term
%B.1
\begin{equation}
{\partial U \over \partial z} + {i\beta^{(2)} \over 2T_0^2}
{\partial^2 U \over \partial\tau^2} + {\alpha U \over 2}
 + i\Gamma(z,\tau)U-i\gamma P_0 |U|^2 U = 0.
\end{equation}
$Z$ is distance along the length of the fiber,
$U(z,\tau)=A(z,\tau)/\sqrt{P_0}$ is the complex electric field envelope
$A(z,\tau)$ normalized to the absolute amplitude of the field $\sqrt{P_0}$,
$P_0$ is the total power in the fiber, $\tau$ is time normalized to a
convenient time scale $T_0(\sim 1\ ns)$ measured in a reference frame
moving with the group velocity of the pulse [$\tau=(t-z/v_g)/T_0$]. The
simulations are carried out for exactly the same physical parameters as the
experiments and simulations reported by Hart {\it et al}.\ \cite{hart1}, i.e.,
$\beta^{(2)}=55\,(ps)^2/km$, is the group velocity dispersion of the fiber at
the operating wavelength $\lambda_{0}\sim$ 632\,nm
($k_0\sim 10^7\,m^{-1}$). A loss of $\sim$ 6\,dB/km gives $\alpha$ = 0.0014\,m$^{-1}$ as the
loss in the fiber at this wavelength. The nonlinearity coefficient
$\gamma=0.019\,W^{-1}m^{-1}$ is given by
%B.2
\begin{equation}
\gamma = {\omega_{ave}n_2^I \over cA_{eff}},
\end{equation}
where $A_{eff}$ is the effective core area of the fiber,
$n_2^I$ is the Kerr coefficient for the intensity-dependent refractive index, and
$\omega_{ave}$ is the average angular frequency of the wave envelope.
$\Gamma(z,\tau)$ is a linear multiplicative phase noise field. In this study
the noise field is assumed to be $\delta$-correlated in both space and time.
The evolution of the FWM dynamics is found to be sensitive to the strength of
this noise field. It can be physically interpreted as phase noise arising due
to fluctuations in the linear refractive index of the fiber medium. A detailed
discussion of its physical origin is given in Sec.\ B.4.

The system was simulated using the Split-Step Fourier Method (SSFM)
\cite{Agrawal2}. An algorithm for appropriately incorporating stochastic
phase fluctuations along the length of the fiber in the SSFM was developed
and is summarized below.

The NLSE is composed of linear and nonlinear terms, and can be written in operator form as
%B.3
\begin{eqnarray}
{\partial U \over \partial z} & = & (\hat{D}+\hat{S}+\hat{N})U \nonumber \\
\hat{D}& = & {-i\beta^{(2)} \over 2T_0^2}
{\partial^2 \over \partial\tau^2} - {\alpha \over 2} \nonumber \\
\hat{S} & = & i\Gamma(z,\tau) \nonumber \\
\hat{N} & = & i\gamma P_0|U|^2,
\end{eqnarray}
where $\hat{D}$, $\hat{S}$ and $\hat{N}$ are linear
(dispersive), nonlinear
and stochastic operators, respectively. It has an exact solution for
infinitesimal $\Delta z$ given by -
%B.4
\begin{equation}
U(z + \Delta z,\tau) = exp[\Delta z(\hat{D} + \hat{S} + \hat{N})]U(z,\tau) ,
\end{equation}
which can be approximated by
%B.5
\begin{equation}
U(z + \Delta z,\tau) \approx exp[\Delta z \hat{D}]exp[\Delta z \hat{S}]exp[\Delta z \hat{N}]U(z,\tau) .
\end{equation}

The execution of $exp[\Delta z \hat{N}]$ is carried out in $\tau$-space:
%B.6
\begin{equation}
B_1(z,\tau)=exp[\Delta z \hat{N}]U(z,\tau) .
\end{equation}

The execution of $exp[\Delta z \hat{S}]$ and $exp[\Delta z \hat{D}]$ is
carried out in $\omega$-space.

In particular, the stochastic phase fluctuations are introduced by modifying
the phase $\phi_j$ of each frequency component $\omega_j$ of the complex
field according to
%B.7
\begin{eqnarray}
B_2(z,\omega) & = & {\cal{F}}[B_1(z,\tau)] \nonumber \\
B_3(z,\omega_{j}) & = & exp[i \delta\phi(z,\omega_j)]B_2(z,\omega_j) ,
\end{eqnarray}
where $\cal{F}$ represents the Fourier transform operation.

This process only modifies the phase of each complex frequency component,
leaving its absolute value unchanged. Thus the algorithm conserves the total
power and the unconditional stability of the system.

The stochastic phase fluctuations $\delta\phi(z,\omega_j)$ are taken to be
$\delta$-correlated in frequency as well as spatially along the fiber length.
The Box-Muller algorithm \cite{boxmuller} was used to generate Gaussian random
deviates from computer-generated uniform random deviates $r_{1j}$ and $r_{2j}$
at each spatial step and for each frequency component $\omega_j$. The
fluctuations are given by
%B.8
\begin{equation}
\delta\phi(z,\omega_{j}) = \sqrt{-2\sigma_{\phi}^2 \Delta z ln(r_{1j})}cos(2 \pi r_{2j}) .
\end{equation}

This is followed by the execution of $exp[\Delta z \hat{D}]$, which is also
carried out in Fourier space, followed by the inverse transform
%B.9
\begin{equation}
U(z + \Delta z,\tau) = {\cal{F}}^{-1}[exp[\Delta z \hat{D}(i\omega)]B_{3}(z,\omega)] .
\end{equation}

$\hat{D}(i\omega)$ is obtained by replacing $(\partial / \partial \tau)$
by $i \omega$.

%Figure B.1\renewcommand{\baselinestretch}{1}

\begin{figure}
\begin{center}
\includegraphics[0in,0in][3.25in,4.266in]{nlsetime.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption{Multimode pulse input to the NLSE: (a) input pulse in time
domain and (b) input spectrum.}
\label{figB.1}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

The basic form of the initial complex wave envelope function is
%B.10
\begin {equation}
U(0,\tau) = exp \left( - {\tau^2 \over 2\tau_p^2} \right)
\left\{
\begin{array}{l}
exp\left( {i\Omega\tau \over 2} \right) + \\
exp\left( - {i\Omega\tau \over 2} \right)
\end{array}
\right\} ,
\end{equation}
where $\tau_p$ is the pulse width T$_p$ =5\,ns FWHM, normalized to the time scale
T$_0$, $\Omega$=366\,GHz is the frequency detuning between the two laser
sources normalized to a frequency scale $\Omega_0$ = 62.5\,MHz.  Figure B.1(a)
shows a plot of this pulse $|U(0,\tau)|^2$. The overall Gaussian envelope
has an FWHM of 5\,ns, the closely spaced dark lines are due to the 366\,GHz
($\sim$3\,ps) beating between the two input pump frequencies. The 2\,ns
modulations on the pulse are due to the 0.5\,GHz mode-structure in the
blue-shifted pump wave. Figure 2.1(b) shows the input spectrum of this pulse
which consists of two highly monochromatic pump waves with a detuning of
$\Omega$=366\,GHz. The spectrum of the blue-shifted pump, upon magnification,
is seen to be composed of two very closely spaced peaks, with a separation of
$\Delta\nu$=0.5\,GHz. Hart {\it et al}.\ \cite{hart1} did not use pulsed
wave functions in their NLSE simulations as the size of the FFT required to do
so made it computationally prohibitive at that time. The size of the FFT was
chosen such that it would accommodate a time span of 16\,ns in order to go
sufficiently far into the wings on the Gaussian pulse; and a frequency span of
16\,THz in order to accommodate all the sidebands generated and prevent
spurious effects due to the reflection boundary conditions implicit in the
SSFM algorithm. These considerations dictated the size of the FFT to be
$\geq$(16 THz)$\cdot$(16 ns) = 256000. The nearest power of 2 is
2$^{18} = 262144$, which has been used throughout the present work. The
incorporation of the pulsed nature of the light was found to be necessary in
explaining the dynamics. From the perspective of the coupled amplitude
equations used by Hart {\it et al}.\ \cite{hart1}, the present model is equivalent
to a coupled-ODE model with $2^{18}$ coupled ODEs.

%Figure B.2
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{modestruc21ornot.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption[Short caption for Figure B.2.]
{Effects of inclusion of the multimode nature ($\Delta\nu = 0.5$\,GHz) of the blue-shifted input pump laser on the 1st order sideband evolution as a function of fiber length for P$_0 = 2.1$\,W. Dashed curves represent simulations without the multimode nature and solid curves represent simulations with the multimode nature. $\Omega = 366$\,GHz, $\gamma = 0.019$\,W$^{-1}$\,m$^{-1}$, and $\beta^{(2)} = 55$\,ps$^2$/km (a) power in the blue-shifted sideband, (b) power in the red-shifted sideband.}
\label{figB.2}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

%Figure B.3
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{modestruc55ornot.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption[Effects of inclusion of the multimode nature]
{Effects of inclusion of the multimode nature ($\Delta\nu = 0.5$\,GHz) of the blue-shifted input pump laser on the 1st order sideband evolution as a function of fiber length for P$_0 = 5.5$\,W. Dashed curves represent simulations without the multimode nature and solid curves represent simulations with the multimode nature. $\Omega = 366$\,GHz, $\gamma = 0.019$\,W$^{-1}$\,m$^{-1}$, and $\beta^{(2)} = 55$\,ps$^2$/km (a) power in the first-order blue-shifted sideband, (b) power in the first-order red-shifted sideband, (c) power in the second-order blue-shifted sideband, (d) power in the second-order red-shifted sideband.}
\label{figB.3}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

%Figure B.4
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{nlsez21cwpulse.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption[Effects of inclusion of the pulsed nature]
{Effects of inclusion of the pulsed nature (5\,ns FWHM) of the input pump laser light on the first-order sideband evolution as a function of fiber length for P$_0 = 2.1$\,W. Dashed curves represent cw simulations and solid curves represent pulsed simulations. $\Omega = 366$\,GHz, $\Delta\nu = 0.5$, $\gamma = 0.019$\,W$^{-1}$m$^{-1}$, and $\beta^{(2)} = 55$\,ps$^2$/,km (a) power in the blue-shifted sideband, (b) power in the red-shifted sideband.}
\label{fig24}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

%Figure B.5
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{nlsez55cwpulse.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption
[Other effects of inclusion of the pulsed nature]
{Effects of inclusion of the pulsed nature (5\,ns FWHM) of the input pump laser on the first- and second-order sideband evolution as a function of fiber length for P$_0 = 5.5$\,W. Dashed curves represent cw simulations and solid curves represent pulsed simulations. $\Omega = 366$\,GHz, $\Delta\nu = 0.5$, $\gamma = 0.019$\,W$^{-1}$m$^{-1}$, and $\beta^{(2)} = 55$\,ps$^2$/,km (a) power in the first-order blue-shifted sideband, (b) power in the first-order red-shifted sideband, (c) power in the second-order blue-shifted sideband, (d) power in the second-order red-shifted sideband.}
\label{fig25}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

Upon incorporation of the multimode nature of the blue input pump laser source
and the stochastic fluctuations in the initial power in the lasers, the
initial wave function takes the form
%B.11
\begin {equation}
U(0,\tau) = exp\left( - {\tau^2 \over 2\tau_p^2} \right)
\left\{
\begin{array}{l}
\sqrt{{1 + \delta\rho_1 \over 2}}
\left[ \begin{array}{l}
exp \left( {i(\Omega+\Delta\nu)\tau \over 2} \right) + \\
exp \left( {i(\Omega-\Delta\nu)\tau \over 2} \right)
\end{array} \right]\\
+ \sqrt{1 + \delta\rho_2} exp\left( - {i\Omega\tau \over 2} \right)
\end{array}
\right\}.
\end{equation}

\

\noindent $\Delta\nu = 0.5$\,GHz is the frequency separation between the two longitudinal
modes in the blue-shifted pump. $\delta\rho_1$ and $\delta\rho_2$ are
Gaussian random deviates (generated using the Box-Muller algorithm
\cite{boxmuller}) that represent the initial power fluctuations in each of the
pump laser sources. Their standard deviations were taken to be,
$\sigma_{\rho_1} = 0.2$, $\sigma_{\rho_2} = 0.11$ for simulations from 0\,m to
20\,m, $\sigma_{\rho_1} = 0.12$, $\sigma_{\rho_2} = 0.05$ for simulations from
20\,m to 50\,m along the length of the fiber. This is exactly the same
prescription used by Hart {\it et al}.\ \cite{hart1} in their simulations and is
dictated by their experimental measurements of the fluctuations in the pump
laser intensities.

At this point it is worth noting the effects of the inclusion of two attributes of
the input laser light, namely, the multimode nature of the blue-shifted pump, and
the pulsed nature of the input light (assumed to be cw in the simulations reported by
Hart {\it et al}.\ \cite{hart1}).

Figure B.2 shows a comparison between simulations with (solid curves) and without (dashed curves) the multimode nature for an input pump power of 2.1 Watts. The simulations with the mode structure show the asymmetry between the blue- and red-shifted sideband evolution, in particular, the difference in spatial wavelength between the two, and a non-return to zero nature of the evolution, as observed in the experimental data (black dots with error bars). These features are absent in the simulations without mode-structure. $\rho_3$ and $\rho_4$ stands for the first order blue- and red-shifted sidebands respectively.  Figure 2.3 shows the corresponding comparison for the case of 5.5 Watts of input pump power.  Here, too, the simulations incorporating the multimode nature of the blue-shifted pump (solid curves) are seen to be an improvement over those not incorporating it (dashed curves). A feature of the experimental data (black dots with errorbars) is that for the $\rho_3$ sideband, the initial part of the evolution involves a peak followed by a shoulder, while for the $\rho_4$ sideband, the initial part of the evolution involves a shoulder followed by a peak. This feature, too, is seen to occur as a result of the inclusion of the multimode nature of the blue-shifted pump.

The effect of inclusion of the pulsed nature of the input beam is seen in Fig.\ B.4 (for the 2.1 Watt case) and Fig.\ B.5 (for the 5.5 Watt case). The solid dashes represent simulations for a cw input beam and the solid curves represent those for a pulsed input beam. The incorporation of the pulsed nature clearly results in damping of the sideband trajectories which are seen to come closer to the experimental data \cite{hart1} (black dots with error bars).

Use of the FFT algorithm makes evaluation relatively fast compared to other
finite-difference schemes. The computational error is $O(\Delta z^2)$, thus
the solution converges with decreasing spatial step-size $\Delta z$.

The simulations were tested for the conservation of total power along the
fiber length (by setting the loss $\alpha$ to zero) and for the conservation
of asymmetry \cite{thompson1,hart1} given by
%B.12
\begin{equation}
C(Z) = \sum_{i=1}^{\infty}(2i-1)[\rho_{2i-1}(Z)-\rho_{2i}(Z)] .
\end{equation}

A clearer picture of the evolution of the sidebands is obtained by plotting both the
power in the sidebands and their standard deviations as a function of length along the fiber. Figures B.6(a) and B.6(b) show a comparison between simulation and experiment of the evolution
of the first-order blue-shifted ($\rho_3$) and red-shifted ($\rho_4$) sidebands,
respectively, for an input power of 2.1 W. The dashed curves represent NLSE simulations
which include the stochastic nature of the input powers of the pump lasers but exclude
the stochastic phase fluctuations added along the length of the fiber, an attribute
which is included in the simulations represented by the solid curves. The black dots
with error bars represent the experimental data. The measured sideband
power, normalized to the total power in the fiber, is periodic in length but
appears to be damping to a constant value. The measured data also show a clear
difference between the spatial wavelengths of oscillation of the blue-shifted ($\rho_3$) and red-shifted ($\rho_4$) sidebands trajectories, respectively. Both these features are captured well by both the simulations. Figures B.6(c) and B.6(d) compare experimental and simulated
measures of the evolution of the standard deviation in the sideband power
along the fiber length. It is clearly observed that simulations with phase noise
added to the light field along the length of the fiber (solid curves) are closer to the
experimental data as compared to those that exclude this feature (dashed curves). This indicates
the instrumental nature of the phase fluctuations in explaining key features of the dynamics.

%Figure B.6
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{nlsez21phaseornot.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption
[Comparison between experiments measurements]
{Comparison between the experimental measurements \cite{hart1}(black), the random initial condition NLSE model excluding phase noise (dashed curves) and the stochastic phase noise NLSE model (solid curves) showing the first-order sideband evolution as a function of fiber length for P$_{0} = 2.1$\,W, $\Omega = 366$\,GHz, $\Delta\nu = 0.5$\,GHz,$\gamma = 0.019$\,W$^{-1}$m$^{-1}$, and $\beta^{(2)} = 55$ps$^2$/km: dynamical evolution of the: (a) power in the blue-shifted sideband, (b) power in the red-shifted sideband, (c) fluctuations in the blue-shifted sideband, (d) fluctuations in the red-shifted sideband.}
\label{figB.6}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

The apparent damping of the periodic sideband trajectory is seen more
dramatically in Figs.\ B.7(a) and B.7(b), which show the evolution of the
first-order sideband power along the fiber for an input power of 5.5\,W.
The two first-order sidebands evolve differently. They appear to
damp to a constant value at a faster rate than for the case with an input pump
power of 2.1\,W. Here again, NLSE simulations that incorporate phase noise along the length
of the fiber (solid curves) are much more successful in accurately capturing the dynamical features of the system than NLSE simulations that do not take this feature into account (dashed curves).  Figures B.7(c) and B.7(d) show a comparison between the simulated and measured standard deviations. Comparisons for the second-order blue-shifted ($\rho_5$) and red-shifted ($\rho_6$) sidebands, respectively, are shown in Figs.\ B.7(e) and B.7(f).

%Figure B.7
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{nlsez55phaseornot.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption
[This figure caption is indented and single-spaced.]
{This figure caption is indented and single-spaced.  Comparison between the experimental measurements \cite{hart1} (black), the random initial condition NLSE model excluding phase noise (dashed curves) and the stochastic phase noise NLSE model (solid curves) showing the first- and second-order sideband evolution as a function of fiber length for P$_{0} = 5.5$\,W, $\Omega = 366$\,GHz, $\Delta\nu = 0.5$\,GHz, $\gamma = 0.019$\,W$^{-1}$m$^{-1}$, and $\beta^{(2)} = 55$\,ps$^2$/km: dynamical evolution of the: (a) power in the first-order blue-shifted sideband, (b) power in the first-order red-shifted sideband, (c) fluctuations in the first-order blue-shifted sideband, (d) fluctuations in the first-order red-shifted sideband, (e) power in the second-order blue-shifted sideband, (f) power in the second-order red-shifted sideband.}
\label{figB.7}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

The observed dynamical evolution of the sidebands is found to depend
sensitively on the strength of the stochastic phase fluctuations. Yet, best
agreement with the experimental results of Hart {\it et al}.\ \cite{hart1} is
achieved with exactly the same noise strength $\sigma^2_\phi$ as used in
their truncated ODE model, namely, $\sigma^2_\phi = 0.0067$\,m$^{-1}$. They
report that including phase noise in their FWM calculations resulted in a
spurious linear drift in the trajectories for the sideband power with length.
To remove this artifact of the computations, they added a linear loss to their
coupled ODEs. They set the loss coefficient $\alpha = 0.0046$\,m$^{-1}$ by
finding the value that removed this increasing slope. We have observed exactly
the same secular growth phenomenon for a wide range of the noise strength
$\sigma^2_\phi$ and have arrived at an empirical prescription for $\alpha$
namely, $\alpha\sim\sigma^2_\phi$, where $\sigma^2_\phi$ is the
variance of the added phase noise. This indicates the general nature of
dynamics resulting from the addition of stochastic, $\delta$-correlated phase
fluctuations to systems governed by nonlinear partial differential equations
\cite{risken}.

It is remarkable that the strength of the phase noise required is the same in
both the 2.1\,W and the 5.5\,W cases. Further, it is worth noting that exactly
the same noise strength was used by Hart {\it et al}.\ \cite{hart1}, the difference
being that they introduced phase noise only in the pump frequencies, whereas
we have introduced it in all the Fourier modes ($\sim2^{18}$). As a
confirmation of this result, they also performed experiments and numerical
simulations examining the sideband power dependence on the input power at a
fixed length of 50.4\,m of the same fiber. We have repeated these simulations
with the stochastic NLSE model and the results are shown in Figs.\ B.8(a)
(blue-shifted sideband) and B.8(b) (red-shifted sideband). The experimental
measurements of the sideband powers are represented by filled squares and the
results of numerical simulations are represented by triangles (without phase
noise) and by circles (with phase noise). The simulations are seen to follow
the general trend seen in the experiments. As the pump power is increased, the
triangles (without phase noise) start to disagree with experiment, whereas the
circles (with phase noise) are much closer to experiment. The phase noise
strength used in these simulations was exactly the same as that used in the
simulations depicted in Figs.\ B.6 and B.7. The agreement between the phase noise
simulations and the experimental data was (once again) highly sensitive to the
noise strength. Since this experiment (unlike those shown in Figs.\ B.2 - B.7)
is non-destructive, it can be used to deduce the strength of phase noise
processes in a given optical fiber. It will be shown in Sec.\ B.4 that a
likely cause of the phase noise is fluctuation in the linear refractive index
of the fiber. The noise strength deduced from the present computational study
corresponds to a refractive index inhomogeneity of
$\langle \Delta n^{2} \rangle \sim 10^{-16}$.

%Figure B.8
\begin{figure}
\hspace{1.25in}
\includegraphics[0in,0in][6in,4.572in]{nlsefinal.eps}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption
[Comparison between the experiments measurements (filled squares]
{Comparison between the experimental measurements (filled squares), simulations without stochastic phase fluctuations (open triangles) and with stochastic phase fluctuations (open circles) of the first-order sideband power versus pump input power for L=50.39\,m, and $\Omega = 366$\,GHz: power in the (a) blue-shifted sideband and (b) red-shifted sideband.}
\label{figB.8}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

%Figure B.9
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.572in]{fig292.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption
[Evolution of the FWM spectrum]
{Evolution of the FWM spectrum along the fiber (a) P=2.1\,W, experiment,
(b) P=5.5\,W, experiment, (c) P=2.1\,W, stochastic-NLSE model, (d) P=5.5\,W, stochastic-NLSE model.}
\label{figB.9}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

Till now the comparisons between our simulations of the full NLSE and the
truncated ODE model give basically the same results, although with much better
agreement with experiment. However, the full NLSE can also provide a detailed
comparison with the experimental spectra. This was not available from the
truncated ODE model. The simulations reported in this work were carried out
with a very high frequency and time resolution in order to incorporate the
fact that the input light was not cw, but was composed of $\sim$ 5\,ns long
pulses; and that the number of sidebands generated required the frequency
spread of the FFT to be $\sim$ 16\,THz, while resolving a longitudinal
mode-structure of $\Delta\nu$ $\sim 0.5$\,GHz. The spectral resolution used was
$\sim$ 0.05\,GHz, whereas the spectrometer used to observe the spectra had a
resolution 1000 times larger ($\sim$ 50\,GHz). To account for this difference,
the simulated spectra were first convolved with a Gaussian of unit peak and
62\,GHz FWHM, before they were compared with the observed spectra.

Figures B.9(a) and B.9(b) show three-dimensional plots of the average experimental
FWM output spectrum along the length of the fiber for input pump powers of 2.1\,W and 5.5\,W,
 respectively (courtesy Hart {\it et al}.\ \cite{hart1}). The vertical
axis represents the intensity, normalized to the peak power in one of the
input pumps, plotted on a logarithmic scale. The pump frequencies are centered
on $+/-\Omega/2$ and the fiber length is increasing into the page. Figures B.9(c)
and B.9(d) show the corresponding comparisons based on simulations using
the stochastic-NLSE model. The basic features of the spectral evolution are
captured by the simulations.

%Figure B.10
\begin{figure}
\begin{center}
\includegraphics[0in,0in][6in,4.573in]{nlsespec.eps}
\end{center}
\renewcommand{\baselinestretch}{1}
\small\normalsize
\begin{quote}
\caption
[Experimental FWM output spectrum]
{Experimental FWM output spectrum (solid line), convolved spectra from simulations of the stochastic NLSE model (dashed line), and hyperbolic secant envelope fit (dotted line) for pump input powers P$_0$ of (a) 2.1\,W, (b) 5.5\,W, (c) 6.7\,W, (d) 8.3\,W, (e) 12.7\,W, (f) 17.4\,W, fiber length L$= 50.39$\,m, $\Omega = 366$\,GHz, $\Delta\nu = 0.5$\,GHz, $\gamma = 0.019$\,W$^{-1}$m$^{-1}$, and $\beta^{(2)} = 55$\,ps$^2$/km.}
\label{figB.10}
\end{quote}
\end{figure}
\renewcommand{\baselinestretch}{2}
\small\normalsize

Hart {\it et al}.\ \cite{hart1} also documented the experimentally observed FWM
output spectra for a fixed fiber length of 50.39 meters for 6 different input
pump powers. They state the coefficients A and B of the hyperbolic secant
envelopes that best fit the output spectra which are given by
%B.13
\begin{equation}
f(\omega) = Asech(B\omega) ,
\end{equation}
where A and B are the experimental fit parameters.

The hyperbolic secant parameters A and B, that best fit the simulated spectra
are exactly the same as those that best fit the experimental spectra
\cite{hart1} for all the 6 cases of input power considered. Figure 2.10 shows an
overlap of the simulated spectra (dashed line), with the experimental spectra
(solid line) and the experimental hyperbolic secant envelope (dotted line) for
6 different pump powers, namely, (a) 2.1\,W, (b) 5.5\,W, (c) 6.7\,W, (d) 8.3\,W, (e)
12.7\,W, (f) 17.4\,W. The hyperbolic secant parameters for each of these pump
powers are (a) A=3.85 and B=0.36, (b) A=2.26 and B=0.27, (c) A=1.81, B=0.25,
(d) A=1.56 and B=0.23, (e) A=0.98,B=0.20, and (f) A=0.81 and B=0.20. The exact
shapes of the simulated spectra match very well with the experimental spectra
for low input pump powers (2.1\,W and 5.5\,W), but tend to lack the "filled-in"
character of the experimental spectra at higher powers (6.7\,W, 8.3\,W, 12.7\,W and
17.4\,W).

\section{Discussion}

Hart {\it et al}.\ \cite{hart1} postulated that strong candidates for the possible
physical sources of the phase fluctuations are stimulated Brillouin
scattering, stimulated Raman scattering and fiber medium inhomogeneities.
Brillouin scattering was eliminated as a source, since a backward propagating
wave, which is a signature of Brillouin scattering in optical fibers, was not
observed in the experiments. We have  modeled stimulated Raman scattering
\cite{Agrawal8, headley} for our system and have found no evidence to
support the hypothesis that it could be a possible source of the stochastic phase
fluctuations for fiber lengths up to 50 meters and pump power levels up to 5.5 Watts.
A more detailed discussion of the Raman scattering simulations performed is given in Chap.\ 3.
Apart from these, quantum phase fluctuations are another well
known, though extremely weak, source of phase noise in optical fibers
\cite{Agrawal2,perlmutter1}.

Fiber medium inhomogeneities were identified as the major cause of the
stochastic phase fluctuations. These inhomogeneities can manifest themselves
through spatial and/or temporal fluctuations in the fiber parameters, namely,
the linear refractive index $n_0$, the group velocity $v_g$, the group
velocity dispersion $\beta^{(2)}$ and the nonlinearity
$\gamma$ \cite{abdullaev}. Of these, the fluctuation in the linear refractive
index was found to be the only source of phase fluctuation that had a
significant effect on the dynamics. A relationship between the level of
refractive index fluctuations and the  corresponding level of phase
fluctuations has been arrived at. It is found that refractive index
fluctuations as small as $\sigma_n^2 \sim 10^{-17}$\,m$^{-1}$ can cause the
desired phase fluctuations. Possible sources of these refractive index
fluctuations are discussed below.

Consider the modified nonlinear Schr\"odinger equation (NLSE) which is
stated below, with the linear multiplicative noise term represented in terms of
spatial and temporal fluctuations in the refractive index of the fiber.
%B.14
\begin{equation}
{\partial U \over \partial z} + {i\beta^{(2)} \over 2T_0^2} {\partial^2U \over \partial\tau^2} + {\alpha U \over 2} + ik_0 \delta n(z,\tau)U - i\gamma P_{0}|U|^2 U = 0 ,
\end{equation}
where $\delta n(z,\tau)$ is the spatial and temporal variation of the refractive
index along the fiber. It can be caused by temperature and density
fluctuations in the fiber \cite{glenn}.

The thermodynamic estimate for $\Delta n$ is given by \cite{glenn}
%B.15
\begin{equation}
\langle \Delta n^{2} \rangle = {-kT\rho^2 \over V^2}
\left( {\partial V \over \partial P} \right)_{T}
\left( {\partial n \over \partial \rho} \right)_{T}^{2}
 + {kT^2 \over \rho VC_v} \left( {\partial n \over \partial T} \right)_{\rho}^2 .
\end{equation}

This gives the mean-square index fluctuation in terms of the properties of
the material. It can be rewritten as
%B.16
\begin{equation}
\langle \Delta n^{2} \rangle = {V_{\rho}+V_T \over V} = \langle \Delta n^{2} \rangle_{\rho}+\langle \Delta n^{2} \rangle_{T} .
\end{equation}

For a fiber of length z=1\,m and radius r=2.82\,$\mu$m
(Volume V=2.5 $\times 10^{-12}$\,m$^3$), these have been calculated to be
%B.17
\begin{eqnarray}
\langle \Delta n^2 \rangle_{\rho} \sim 10^{-21} & \equiv & \langle \Delta \rho^2 \rangle \sim 10^{-14}
{kg^2 \over m^6}, \nonumber \\
\langle \Delta n^2 \rangle_T \sim 10^{-23} & \equiv & \langle \Delta T^2 \rangle \sim 10^{-12}~{^\circ}C^2 .
\end{eqnarray}

It should be noted that $\langle \Delta n^2 \rangle \propto (1/z) \Rightarrow \delta n \propto  (1 / \sqrt{z})$. The corresponding phase fluctuation that this would lead to in the NLSE is given by $\delta \phi=k_{0} \delta n z \propto \sqrt {z}$, which is equivalent to the prescription for incorporating phase fluctuations into the stochastic NLSE model described in Sec.\ 2.3, namely,  $\langle \Delta \phi^2 \rangle = 6.7 \times 10^{-3}z$. Hart {\it et al}.\ \cite{hart1} used the same prescription and the same noise strength in their truncated-ODE model. From this we can estimate the level of refractive index fluctuation that corresponds to the noise strength used in the simulations described in Sec.\ 2.3
%B.18
\begin{eqnarray}
\langle \Delta n^2 \rangle = {6.7 \times 10^{-3} \over k_0^2} = 6.78 \times 10^{-17} \nonumber\\
\equiv \langle \Delta T^{2} \rangle \sim 10^{-6}~{^\circ}C^2 \equiv \Delta T \sim 10^{-3}~{^\circ}C
\end{eqnarray}

The temperature coefficient of the refractive index of silica \cite{glenn},
$(\partial n / \partial T)_{\rho} \sim 10^{-5} ~{^\circ}C^{-1}$. Thus even small spatio-temporal temperature fluctuations of $\sim 10^{-3} ~{^\circ}C$ are enough to cause the inferred level of refractive index fluctuations.

The refractive index fluctuations could also be due to inhomogeneities in the
density of the fiber material, frozen in at the time of manufacture of the
fiber. The simulations were averaged over $\sim$ 600 iterations to get a good
estimate of the power fluctuations in the sidebands. Initially, simulations
were performed with a different phase noise distribution for each iteration.
Later, a particular (arbitrary) phase noise distribution was selected and
frozen for all the iterations.
This did not reduce the level of damping observed in the sideband trajectories
provided that the strength of the phase noise was kept the same, thus
indicating that density fluctuations induced during fiber manufacture could be
a possible source. The phase noise was modeled as $\delta$-correlated in
both space and time. A more realistic approach would be to use correlated
noise. Numerical methods to incorporate linear multiplicative correlated noise
into the NLSE have been developed by M.J. Werner {\it et al}.\ \cite{werner2}.

\section{Conclusions}

The role of stochasticity in the dynamical evolution of four-wave-mixing
processes in an optical fiber has been investigated. This research consisted
of theoretical and numerical computations. It focuses on tracing the evolution
of the sidebands, generated through FWM, along a length of optical fiber.
Detailed comparisons were made with the experimental results of
Hart {\it et al}.\ \cite{hart1} and the agreement was excellent. The present work
uses numerical techniques that have much higher resolution and better
efficiency, and it presents a theoretical basis for the role of the
stochasticity in the dynamics. The system is known to be governed by the
nonlinear Schr\"odinger equation (NLSE) to a very good
approximation \cite{Agrawal2}.

A powerful technique that can be used for simulations of the stochastic NLSE
is the Split-step Fourier Method (SSFM) \cite{Agrawal2}. An algorithm for the
direct implementation of stochastic processes along the length of the fiber in
the SSFM has been developed. The advantages of this approach with respect to
the coupled-ODE approach are that we can carry out simulations with much
higher frequency and time resolution without sacrificing computational
efficiency.

The physical sources of these stochastic phase fluctuations are investigated
quantitatively and are identified to be due to fluctuations in the linear
refractive index of the fiber. Strong candidates for the causes of these
refractive index fluctuations are temperature fluctuations in the fiber medium
caused by the fluctuating temperature of the fiber environment, density
fluctuations in the fiber medium frozen into the fiber during manufacture, and
intrinsic thermodynamic fluctuations in the temperature and density of the
fiber.

The experiments performed by Hart {\it et al}.\ \cite{hart1} can be used to
determine the level of these refractive index fluctuations in commercial
fibers. Results described in Figs.\ 2 and 3 represent a destructive
experiment that measures the sideband evolution with fiber length for a fixed
input pump power, necessarily requiring the fiber to be cut repeatedly. The
level of refractive index fluctuations can be used as a parameter in the
simulations to best fit the experimental results. Alternatively, Fig.\ 4
represents a non-destructive experiment that measures the sideband evolution
with input pump power for a fixed fiber length. These experiments are found to
be effective for estimating the refractive index fluctuations, as the dynamics
is observed to be sensitively dependent on the strength of the phase
fluctuations.


\documentclass{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsfonts}
\usepackage{syntax}
\usepackage{stmaryrd}
\usepackage{mathpartir}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\chead{Tuple Unboxing}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{30pt}

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Zt}{$\Z$}

% Create a relational rule
% [#1] - Additional mathpartir arguments
% {#2} - Name of the rule
% {#3} - Premises for the rule
% {#4} - Conclusions for the rule
\newcommand{\relationRule}[4][]{\inferrule*[lab={\sc #2},#1]{#3}{#4}}

\newcommand{\rel}[1]{\ensuremath{\llbracket {#1} \rrbracket}}
\newcommand{\ttt}{\texttt}
\newcommand{\transform}{\rightsquigarrow}
\newcommand{\proj}{\pi}
\newcommand{\ttuple}{\tau_1 * \ldots * \tau_n}
\newcommand{\etuple}{(e_1,\ldots,e_n)}

\begin{document}
This document defines a transformation $\transform$, such that every
expression with a tuple type is a tuple expression. The following invariant
should hold after this transformation:
$$ \Gamma \vdash e : \ttuple \rightarrow e = \etuple$$

For defining tuple unboxing we add a tuple projection construct
in the language $\proj_i$.

\section*{Values}

\begin{mathpar}
	\relationRule{identifer - tuple}{
		\null
	}{
		\Gamma \vdash x : \ttuple \transform (\proj_1 x, \ldots, \proj_n x)
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{identifer}{
		\tau \neq \ttuple
	}{
		\Gamma \vdash x : \tau \transform x
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{literal}{
		\tau \in \{\texttt{bool}, \texttt{int}\}
	}{
          \Gamma \vdash n : \tau \transform n
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Function}{
          e \transforms e'
	}{
		\Gamma \vdash \texttt{fun}\ x \to e : \tau_1 \to \tau_2 \transforms \ttt{fun}\ x \to e'
	}
\end{mathpar}

\section*{Expressions}

\begin{mathpar}
	\relationRule{Tuple}{
		\null
	}{
          \Gamma \vdash \etuple : \tau \transform \etuple
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Not}{
          e \transforms e'
	}{
		\Gamma \vdash !e : \tau \transforms !e'
	}
\end{mathpar}

\begin{mathpar}
\relationRule{Binop}{
	 \oplus \mbox{ is a binary operator} \\ e_1 \transforms e_1' \\ e_2 \transforms e_2'
   }{
   	\Gamma \vdash e_1 \oplus e_2 : \tau \transforms e_1' \oplus e_2'
   }
\end{mathpar}

\begin{mathpar}
	\relationRule{Function application}{
		e_1 \transforms e_1' \\ e_2 \transforms e_2'
	}{
		\Gamma \vdash e_1 e_2 : \tau \transforms e_1' e_2'
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Let-binding-tuple}{
          e_1 \transforms e_1' \\ e_2 \transforms (e_{21},\ldots,e_{2n})
	}{
		\Gamma \vdash \ttt{let}\ p = e_1\ \ttt{in}\ e_2 : \ttuple \transforms
                (\ttt{let}\ p = \ e_1' \ttt{in}\ e_{21},\ldots,\ttt{let}\ p = \ e_1' \ttt{in}\ e_{2n})
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Let-binding}{
          \tau \neq \ttuple \\ e_1 \transforms e_1' \\ e_2 \transforms e_2'
	}{
		\Gamma \vdash \ttt{let}\ p = e_1\ \ttt{in}\ e_2 : \tau \transforms \ttt{let}\ p = \ e_1' \ttt{in}\ e_2'
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Match-Tuple}{
          e \transforms e' \\ e_1 \transforms (e_{11},\ldots,e_{1n}) \\ \ldots \\ (e_{k1},\ldots,e_{kn})
	}{
		\Gamma \vdash \ttt{match}\ e\ \ttt{with}\ |\ p_1 \to e_1 \dots |\ p_k \to e_k : \ttuple \transforms
		(\ttt{match}\ e'\ \ttt{with}\ |\ p_1 \to e_{11} \dots |\ p_k \to e_{k1}, \ldots,
                \ttt{match}\ e'\ \ttt{with}\ |\ p_1 \to e_{1n} \dots |\ p_k \to e_{kn})
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Match}{
          \tau \neq \ttuple \\ e \transforms e' \\ e_1
	}{
          \Gamma \vdash \ttt{match}\ e\ \ttt{with}\ |\ p_1 \to e_1 \dots |\ p_k \to e_k : \tau =
		\ttt{match}\ e'\ \ttt{with}\ |\ p_1 \to e_1' \dots |\ p_k \to \rel{e_k}
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{If-else}{
		\null
	}{
		\rel{\ttt{if}\ e_1\ \ttt{then}\ e_2\ \ttt{else}\ e_3} =
		\ttt{if}\ \rel{e_1}\ \ttt{then}\ \rel{e_2}\ \ttt{else}\ \rel{e_3}
	}
\end{mathpar}

\section*{Declarations}

\begin{mathpar}
	\relationRule{Let declaration}{
		\null
	}{
		\rel{\ttt{let}\ x = e} = \ttt{let}\ x = \rel{e}
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Attribute type - not $\tau_m$}{
		\tau \neq \tau_m
	}{
		\rel{\ttt{type attribute} = \tau} = \ttt{type attribute} = \tau
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Attribute type - $\tau_m$}{
		\null
	}{
		\rel{\ttt{type attribute} = \tau_m} = \ttt{type attribute} = \tau_t
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Symbolic type - not $\tau_m$}{
		\tau \neq \tau_m
	}{
		\rel{\ttt{symbolic}\ x : \tau} = \ttt{symbolic}\ x : \tau
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Symbolic type - $\tau_m$}{
		\null
	}{
		\rel{\ttt{symbolic}\ x : \tau_m} = \ttt{symbolic}\ x : \tau_t
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Symbolic value}{
		\null
	}{
		\rel{\ttt{symbolic}\ x = e} = \ttt{symbolic}\ x = \rel{e}
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Require}{
		\null
	}{
		\rel{\ttt{require}\ e} = \ttt{require}\ \rel{e}
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Node declaration}{
		\null
	}{
		\rel{\ttt{let nodes} = n} = \ttt{let nodes}\ = n
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Edge declaration}{
		\null
	}{
		\rel{\ttt{let edges} = lst} = \ttt{let edges}\ = lst
	}
\end{mathpar}

\section*{Map expressions}
Each of the following expressions have at least one subexpression which must be a map. The following rules apply if and only if those subexpressions have type $\tau_m$. If not, we recurse as usual but don't otherwise change the expression.

I'm noting that some of these rules involve a lot of duplication, so we might want to look into translations
which e.g. store duplicated expressions in a local variable first.

They also involve a lot of extra unpacking since we don't have syntax for getting/setting with tuples. It
would probably be good to extend the map syntax to work on tuples as well, so long as that doesn't mess
anything up; we could also create a new syntax for tuples.

\begin{mathpar}
	\relationRule{CreateDict}{
		e' = \rel{e}
	}{
		\rel{\ttt{createDict}\ e} = (e', \dots, e')
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Get}{
		k = k_i \\
		x_i \mbox{ fresh}
	}{
		\rel{m[k_i]} =
		\ttt{let}\ (\_, \dots, x_i, \dots, \_) = \rel{m}\ \ttt{in}\
		x_i
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Set - known key}{
		k = k_i \\
		x_1, \dots, x_n \mbox{ fresh}
	}{
		\rel{m[k := e]} =
		\ttt{let}\ (x_1, \dots, x_n) = \rel{m}\ \ttt{in}\
		(x_1, \dots, \rel{e}, \dots, x_n)
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Set - unknown key}{
		\forall i.k \neq k_i \\
		x_1, \dots, x_n \mbox{ fresh}
	}{
		\rel{m[k := e]} = \rel{m}
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Map}{
		f' = \rel{f} \\
		x_1, \dots, x_n \mbox{ fresh}
	}{
		\rel{\ttt{map}\ f\ m} =
		\ttt{let}\ (x_1, \dots, x_n) = \rel{m}\ \ttt{in}\
		(f'\ x_1, \dots, f'\ x_n)
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{MapIf}{
		f' = \ttt{fun}\ (k, v)\ \to \ttt{if}\ p\ k\ \ttt{then}\ \rel{f}\ v\ \ttt{else}\ v \\
		x_1, \dots, x_n \mbox{ fresh}
	}{
		\rel{\ttt{mapIf}\ p\ f\ m} =
		\ttt{let}\ (x_1, \dots, x_n) = \rel{m}\ \ttt{in}\
		(f'\ (k_1, x_1), \dots, f'\ (k_n, x_n))
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Filter}{
		p' = \ttt{fun}\ (k, v)\ \to \ttt{if}\ p\ k\ \ttt{then}\ v\ \ttt{else}\ \ttt{false} \\
		x_1, \dots, x_n \mbox{ fresh}
	}{
		\rel{\ttt{filter}\ p\ m} =
		\ttt{let}\ (x_1, \dots, x_n) = \rel{m}\ \ttt{in}\
		(p'\ (k_1, x_1), \dots, p'\ (k_n, x_n))
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Combine}{
		f' = \rel{f} \\
		x_1, \dots, x_n, y_1, \dots y_n \mbox{ fresh}
	}{
		\rel{\ttt{combine}\ f\ m_1\ m_2} =
		\ttt{let}\ ( x_1, \dots, x_n) = \rel{m_1}\ \ttt{in}\
		\ttt {let}\ (y_1, \dots,  y_n) = \rel{m_2}\ \ttt{in}\ \\
		(f'\ x_1\ y_1, \dots, f'\ x_n\ y_n))
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Union}{
		f' = \rel{f} \\
		x_1, \dots, x_n, y_1, \dots y_n \mbox{ fresh}
	}{
		\rel{m_1\ \ttt{union}\ m_2} =
		\ttt{let}\ (x_1, \dots, x_n) = \rel{m_1}\ \ttt{in}\
		\ttt{let}\ (y_1, \dots, y_n) = \rel{m_2}\ \ttt{in}\
		(x_1\ \|\ y_1, \dots, x_n\ \|\ y_n)
	}
\end{mathpar}

\begin{mathpar}
	\relationRule{Intersection}{
		f' = \rel{f} \\
		x_1, \dots, x_n, y_1, \dots y_n \mbox{ fresh}
	}{
		\rel{m_1\ \ttt{inter}\ m_2} =
		\ttt{let}\ (x_1, \dots, x_n) = \rel{m_1}\ \ttt{in}\
		\ttt{let}\ (y_1, \dots, y_n) = \rel{m_2}\ \ttt{in}\
		(x_1\ \&\&\ y_1, \dots, x_n\ \&\&\ y_n)
	}
\end{mathpar}

\end{document}


%************************************************
\chapter{Protocol Design}\label{ch:protocol}
%************************************************

% What is the goals of an adversary?
% - Be able to forge authentication
% - Be able to link different identities to the same user.
% - Be able to use/hijack an active session

In the previous chapter, we present a design for a \gls{cta} authentication scheme. In this chapter we will present a protocol, which is simply a sequence of well-chosen messages, that can support this scheme. In this chapter, we will only highlight a few important properties from the previous chapter in regards to certain choices, and will then later evaluate which properties the proposed protocol achieves.

\section{Definition of Authentication}

So far we have not clearly defined what `authentication' actually entails. In his well-renowned article ``A Hierarchy of Authentication Specifications'' \citet{lowe1997hierarchy} puts forward the following definition:

\begin{definition}[Injective Agreement]
We say that a protocol guarantees to an initiator $A$ agreement with a responder $B$ on a set of data items $ds$ if, whenever $A$ (acting as initiator) completes a run of the protocol, apparently with responder $B$ , then $B$ has previously been running the protocol, apparently with $A$, and $B$ was acting as responder in his run, and the two agents agreed on the data values corresponding to all the variables in $ds$, and each such run of $A$ corresponds to a unique run of $B$.
\end{definition}

This definition infer that if two parties agree on a set of data (or evidence of identity), and if there is a one-to-one relationship between the protocol run at the responder and verifier, then they achieve (injective) agreement, and thus the responder authenticates with the verifier.

\section{Design Rationale}
Our design inspires from this definition, and effectively, the \gls{server} $S$, will on request from the \gls{client} $C$, issue a fresh challenge. If the client can reply with a proper response to the challenge then it should be authenticated for a given period of time. Let us consider a simple example in which the \gls{server} encrypts a random nonce with the public-key of the \gls{client}, and sends the cipher to the client. The client then decrypts the cipher and responds to the challenge with the recovered nonce.
{\setlength{\mathindent}{0cm}
\begin{align}
    \tag{message 1} && S \rightarrow C &: enc(n,pk_c)\\
    \tag{message 2} && C \rightarrow S &: n
\end{align}}

Only an actor in possession of $sk_c$ would be able to recover the correct answer, and thus presenting $n$, serves as evidence of knowing $sk_c$. This can be used to authenticate if the server links the public key to a user-identity. This will serve as a starting point for this protocol. However, a few design features from the previous chapter needs to be considered:
\begin{itemize}
    \item The \gls{client} should ask the \gls{authenticator} for `permission' to authenticate.
    \item The \gls{authenticator} should only give permission if unlocked; thus in range of the \gls{sibling}.
    \item The \gls{client} should not be exposed to any piece of information that could be used in a later successful round of authentication.
    \item  Hijacking an active session or token should not give an \gls{adversary} unauthorized access for longer than the session is actively kept alive by the \gls{server} and \gls{authenticator}.
\end{itemize}

In the design, we mention that the \gls{authenticator} can be in a state of either unlocked, locked or lockdown. In practice, we take a different approach to implementing these states. As we envision using general-purpose commodity platforms such as e.g. iOS and Android devices, compromised devices are a concern that should be inherently designed for, and solely trusting a single device to uphold these states is therefore not an option.

\subsection{Distributed Authentication}

To ensure that \gls{authenticator} and \gls{sibling} are always involved in an authentication run, the unlocked state will in practice be implemented by forcing the \gls{authenticator} and \gls{sibling} to collaborate, in computing the response to an authentication challenge. If communication between \gls{authenticator}, \gls{sibling} and \gls{client} are forced onto a local channel (such as Bluetooth), then authentication is only possible when all three devices are in each others proximity.\\

In comparison, Pico~\cite{stajano2011pico} functions by having a main device, the Pico, that is the users key-store. The key-store is encrypted with a \textit{$k$-out-of-$n$} threshold encryption system. This means that at least $k$ siblings (small wearable tokens) must be in its vicinity for it to unlock. On request from the Pico, the siblings send their key-share, allowing the Pico to unlock its key-store. An assumption is made on the Pico, that it periodically `forgets' the key, thus forcing it to continuously interact with its siblings.

We see this as a problem because if the Pico is compromised in the unlocked state, then \textit{all} of the user's services are compromised. In the original paper, it is assumed that an adversary is not capable of compromising the Pico in the unlocked state. This is an unrealistic assumption when using general-purpose devices.

The rationale behind having a central unit holding all keys, and having peripheral devices unlocking, instead of having all devices actively participate, was taken with consideration to the technical limitations of wearables~\cite{stannard2012good}. However, since the Pico was proposed in 2011, these limitations are diminishing, and modern wearables are now fully capable of making cryptographic calculations without draining their battery.\\

The collaboration between \gls{authenticator} and \gls{sibling} can be achieved with `secure multiparty computations' (MPC). MPC entails a group of agents jointly computing a function, such as decrypting a cipher, without revealing anything about their individual input to the function. Furthermore, only with full participation from all actors will the output of the function be meaningful. This means that both devices would have to be compromised, for an adversary to be able to obtain the secrets needed to compromise the user's services. In practice, MPC can be implemented by utilizing a partial crypto system.\\

\begin{comment}
\paragraph{Security Goals}
\begin{itemize}

    \item An adversary should not be able to forge evidence of identity without compromising the secrets of both \gls{authenticator} and \gls{sibling}.

    \item Hijacking an active session or token should not give the \gls{adversary} unauthorized access for longer than the session is actively kept alive by the \gls{server} and \gls{authenticator}.

\end{itemize}
\end{comment}

\section{Partial Crypto Systems}

A partial crypto systems is a system in which multiple actors have to collaborate to encrypt and decrypt messages. Such systems are useful because they ensure that even if one of the actors is compromised, then the system is not compromised. Many such systems exists, but in this section we will present Distributed ElGamal, which is a partial crypto system~\cite{brandt2005efficient}.

\paragraph{ElGamal Crypto System}

ElGamal is a probabilistic and homomorphic public-key crypto system based on the Diffie-Hellman assumption. ElGamal is secure against \Glspl{cpa} (\acrshort{cpa}), if the Decisional Diffie–Hellman (DDH) problem is hard~\cite[page 400]{katz2014introduction}.

Using the cyclic prime order groups, as defined in section~\ref{par:cyclic}, we can define ElGamal in the following way:

\begin{itemize}
    \item \textbf{Gen:} on input $1^n$ run $\mathcal{G}(1^n)$ to obtain a cyclic group $\langle \mathbb{G},q,g \rangle$. Then choose a uniform $x \in \mathbb{Z}_q$ and compute $y := g^x$. Then output the public-key $\langle \mathbb{G},q,g,y \rangle$ and the private-key $\langle \mathbb{G},q,g,x \rangle$

    \item \textbf{Enc:} on input of a public-key and a message $m \in \mathbb{G}$, choose a uniform $r \in \mathbb{Z}_q$ and output the ciphertext $c := ( my^r, g^r )$.

    %\begin{align*}
    %    && \langle m\cdot y^r, g^r \rangle
    %\end{align*}

    \item \textbf{Dec:} on input of a private-key and a ciphertext $c = ( \alpha, \beta )$, output the message $m := \alpha / \beta^x$.

    %\begin{align*}
    %    && m := \alpha / \beta^x
    %\end{align*}
\end{itemize}

\paragraph{Distributed ElGamal}\label{sec:deg}

The distributed variant of ElGamal leverages that the original crypto system is homomorphic. Although the system is homomorphic over both message and keys for both encryption and descryption, the following is focused on homomorphism over the keys for decryption\footnote{For some set of operators `$+$' and `$\times$'}.
{\setlength{\mathindent}{0cm}
\begin{align*}
&&    Dec(c,sk_1) \times Dec(c,sk_2) = Dec(c,sk_1 + sk_2)
\end{align*}}\vspace{-1em}

Let each participating actor $i$ in the distributed system generate an ElGamal key-pair $( pk_i, sk_i )$, using the same cyclic group $\langle \mathbb{G}, q, g \rangle$, by choosing an uniform $x_i \in \mathbb{Z}_q$ and calculating $y_i = g^{x_i}$~\cite{brandt2005efficient}. The joint public and private-key is now given as:
{\setlength{\mathindent}{0cm}
\begin{align*}
&& y = \prod^n_{i=1} y_i && x = \sum^n_{i=1} x_i
\end{align*}}

An encrypted message $Enc(m,pk) \rightarrow ( \alpha, \beta )$ can be jointly decrypted by each participant calculating $\beta^{x_i}$. The message can then be recovered as:
{\setlength{\mathindent}{0cm}
\begin{align*}
&&    m := \frac{\alpha}{\prod^n_{i=1} \beta^{x_i}} = \frac{\alpha}{\beta^{x}}
\end{align*}}

The advantage of this is that the computation and sharing of $\beta^{x_i}$, following the DDH assumption, does not leak any information about the private-keys. Neither does the shares leak any information about the encrypted message before all shares are combined. We define the new operations as:
\begin{itemize}

    \item \textbf{Gen':} on input of a cyclic group $\langle \mathbb{G}, q, g \rangle$, choose a uniform $x \in \mathbb{Z}_q$ and calculate $y = g^{x}$. Then output the public-key $\langle \mathbb{G},q,g,y \rangle$ and the private-key $\langle \mathbb{G},q,g,x \rangle$

    \item \textbf{Dec':} on input of a private-key and a ciphertext $c = ( \alpha, \beta )$, output the partial decryption $c' := ( \alpha, \beta^{x_i} )$

    \item \textbf{Combine:} on input of two partially decrypted ciphertexts $c'_1 = ( \alpha, \beta^{x_1} )$ and $c'_2 = ( \alpha, \beta^{x_2} )$\marginpar{notice that the $\alpha$'s must match}, output
    {\setlength{\mathindent}{0cm}
    \begin{align*}
    && c' := \left( \alpha, \beta^{x_1} \cdot \beta^{x_2} \right) = \left( \alpha, \beta^{{x_1}+{x_2}} \right)
    \end{align*}}\vspace{-2em}

    \item \textbf{Recover:} on input of a partially decrypted ciphertext $c' = ( \alpha, \beta^x )$, output the message $m := \alpha / \beta^x$

\end{itemize}

We denote the recovery of a message from combining partially decrypted ciphers as $m := Dec'(\cdot) \times Dec'(\cdot)$. Furthermore, we denote the product of public-keys as $pk := pk_1 \times pk_2$.

\section{The Protocol}

Two steps of the protocol has to be defined. A \gls{registration} and \gls{authentication} step. The purpose of the registration step is to establish a set of shared knowledge. The purpose of the authentication step is to provide the \gls{server} with evidence, and for the \gls{server} to be able to verify the authenticity of the evidence based on the knowledge acquired during the registration. The proposed protocol builds on the Distributed ElGamal crypto system as presented in the previous section.

\subsection{Registration}

The registration is initiated by the \gls{client} (and thus the end-user) by sending a message to the \gls{authenticator} with a universally unique user-id (such as a uuid\footnote{See \url{https://en.wikipedia.org/wiki/Universally_unique_identifier}}). The \gls{client} might have asked the \gls{server} in advance to issue this id based on some data such as a username. The \gls{authenticator} then forwards the message to the \gls{sibling} and starts computing a new Distributed ElGamal key-pair. The \gls{sibling} also computes a new Distributed ElGamal key-pair and sends its public-key to the \gls{authenticator}. The \gls{authenticator} now combines the keys to a joint public-key and sends it back to the \gls{client}. Lastly the \gls{client} sends the new public-key to the server along with the user-id. This is shown in figure~\ref{msc:register}.

\begin{figure}[bth]
\centering
\resizebox{\linewidth}{!}{
\begin{msc}{Registration}

\setlength{\instdist}{1.5cm}
\setlength{\actionwidth}{3cm}

\declinst{as}{}{$As$}
\declinst{a}{}{$A$}
\declinst{c}{}{$C$}
\declinst{s}{}{$S$}

\nextlevel
\mess{$username$}{c}{s}
\nextlevel[2]
\mess{$id$}{s}{c}
\nextlevel
\mess{$id$}{c}{a}
\nextlevel
\mess{$id$}{a}{as}
\nextlevel
\action{Generate $( pk_{A}, sk_{A} )$}{a}
\action{Generate $( pk_{As}, sk_{As} )$}{as}
\nextlevel[4]
\mess{$pk_{As}$}{as}{a}
\nextlevel
\action{$pk = pk_A \times pk_{As}$}{a}
\nextlevel[4]
\mess{$id, pk$}{a}{c}
\nextlevel
\mess{$id, pk$}{c}{s}
\nextlevel

\end{msc}}
\caption[Registration sequence diagram]{The sequence of messages involved in a successful registration.}
\label{msc:register}
\end{figure}

\subsection{Authentication}
Authentication is initiated by the \gls{client} by sending an authentication request with a user-id to the \gls{server}. The \gls{server} responds with a challenge $c \leftarrow enc(n,pk)$, where $pk$ is the public-key corresponding to the user-id, and $n$ is an arbitrary nonce in $\mathbb{G}$. After sending the challenge the \gls{server} starts a timer $T$. The challenge is forwarded to the \gls{authenticator} and \gls{sibling} which both partly decrypts the challenge. The \gls{sibling} sends its partial decryption to the \gls{authenticator} which combines and recovers the nonce and sends it back to the \gls{server}. If the received nonce is correct then the \gls{server} issues a token to the client, valid for a given duration (in regards to the timer T). Before the token expires, the process is repeated to continuously keep the session active. This is shown in figure~\ref{msc:auth}.

\begin{figure}[bh]
\centering
\resizebox{\linewidth}{!}{
\begin{msc}{Authentication}

\setlength{\instdist}{1.5cm}
\setlength{\actionwidth}{3cm}

\declinst{as}{$\left(sk_{As}\right)$}{$As$}
\declinst{a}{$\left(sk_{A}\right)$}{$A$}
\declinst{c}{}{$C$}
\declinst{s}{$\left(pk\right)$}{$S$}

\nextlevel
\mess{$id$}{c}{s}
\nextlevel
\inlinestart[1.75cm][1.75cm]{exp1}{loop}{as}{s}
\nextlevel
\action{$c \leftarrow enc(n,pk)$}{s}
\nextlevel[3]
\settimer[r]{T}{s}
\nextlevel
\mess{$c$}{s}{c}
\nextlevel
\mess{$c, id $}{c}{a}
\nextlevel
\mess{$c, id$}{a}{as}
\nextlevel
\action{$c'_{As} = Dec'(c,sk_{As})$}{as}
\action{$c'_{A} = Dec'(c,sk_{A})$}{a}
\nextlevel[5]
\mess{$c'_{As}$}{as}{a}
\nextlevel
\action{$n' = c'_A \times c'_{As}$}{a}
\nextlevel[3]
\mess{$n'$}{a}{c}
\nextlevel
\mess{$n'$}{c}{s}
\nextlevel
\action{if $n \stackrel{?}{=} n'$ then proceed}{s}
\nextlevel[5]
\mess{$token$}{s}{c}
\nextlevel
\inlinestart[2.75cm][1.5cm]{exp2}{while $T < x$}{c}{s}
\nextlevel[2]
\mess*{$request, token$}{c}{s}
\nextlevel[2]
\mess*{$data$}{s}{c}
\nextlevel
\inlineend{exp2}
\nextlevel
\stoptimer[r]{T}{s}
\nextlevel
\inlineend{exp1}
\end{msc}}
\caption[Authentication sequence diagram]{The sequence of messages involved in a successful authentication.}
\label{msc:auth}
\end{figure}

%*****************************************
%*****************************************
%*****************************************
%*****************************************


\input{../templates/assignment.tex}

\usepackage{mathtools}
\usepackage{relsize}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10}  % for normal

% Custom colors
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{lbcolor}{rgb}{0.99,0.99,0.99}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
numbers=left,
otherkeywords={self},
keywordstyle=\ttb\color{deepblue},
stringstyle=\color{deepgreen},
backgroundcolor=\color{lbcolor},
frame=single,
showstringspaces=false
}}

% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

\title{
\normalfont \normalsize
\textsc{Norwegian University of Science and Technology\\TDT4137 -- Cognitive Architectures}
\horrule{0.5pt} \\[0.4cm]
\huge Assignment 4:\\ Fuzzy Reasoning\\
\horrule{2pt} \\[0.5cm]
}

\author{Per Magnus Veierland\\permve@stud.ntnu.no}

\date{\normalsize\today}

\newacro{COG}{Centre of Gravity}

\begin{document}

\maketitle

\section*{Fuzzy sets and rules}

\begin{enumerate}[label=\alph*)]
\item

The distance between the robot car and the car in front is 3.6. This value intersects with the linguistic sets \textsc{Small} and \textsc{Perfect}:

\begin{displaymath}
\mu_{\textsc{Small}} = \frac{4.5 - 3.6}{4.5 - 3.0} = 0.6
\end{displaymath}

\begin{displaymath}
\mu_{\textsc{Perfect}} = \frac{3.6 - 3.5}{5.0 - 3.5} \approx 0.07
\end{displaymath}

The gradient of the distance is the delta variable which has the value 1.1 and belongs to the linguistic sets \textsc{Stable} and \textsc{Growing}:

\begin{displaymath}
\mu_{\textsc{Stable}} = \frac{1.5 - 1.1}{1.5 - 0} \approx 0.27
\end{displaymath}

\begin{displaymath}
\mu_{\textsc{Growing}} = \frac{1.1 - 0.5}{2 - 0.5} = 0.4
\end{displaymath}

\begin{enumerate}[label=\textsc{Rule \arabic*}:]
\item \textbf{If} \textsc{Distance} \textbf{is} \textsc{Small} \textbf{and} \textsc{Delta} \textbf{is} \textsc{Growing} \textbf{then} \textsc{Action} \textbf{is} \textsc{None}

\begin{displaymath}
\mu_{\textsc{None}} = \min(\mu_{\textsc{Small}}, \mu_{\textsc{Growing}}) = \min(0.6, 0.4) = 0.4
\end{displaymath}

\item \textbf{If} \textsc{Distance} \textbf{is} \textsc{Small} \textbf{and} \textsc{Delta} \textbf{is} \textsc{Stable} \textbf{then} \textsc{Action} \textbf{is} \textsc{SlowDown}

\begin{displaymath}
\mu_{\textsc{SlowDown}} = \min(\mu_{\textsc{Small}}, \mu_{\textsc{Stable}}) = \min(0.6, 0.27) = 0.27
\end{displaymath}

\item \textbf{If} \textsc{Distance} \textbf{is} \textsc{Perfect} \textbf{and} \textsc{Delta} \textbf{is} \textsc{Growing} \textbf{then} \textsc{Action} \textbf{is} \textsc{SpeedUp}

\begin{displaymath}
\mu_{\textsc{SpeedUp}} = \min(\mu_{\textsc{Perfect}}, \mu_{\textsc{Growing}}) = \min(0.07, 0.4) = 0.07
\end{displaymath}

\item \textbf{If} \textsc{Distance} \textbf{is} \textsc{VeryBig} \textbf{and} \big(\textsc{Delta} \textbf{is not} \textsc{Growing} \textbf{or} \textsc{Delta} \textbf{is not} \textsc{GrowingFast}\big) \textbf{then} \textsc{Action} \textbf{is} \textsc{FloorIt}

\begin{align*}
\mu_{\textsc{FloorIt}} &= \min\big(\mu_{\textsc{VeryBig}}, \max(\neg\mu_{\textsc{Growing}}, \neg\mu_{\textsc{GrowingFast}})\big)\\
&= \min\big(0, \max(1 - 0.4, 1 - 0)\big)\\
&= \min\big(0, \max(0.6, 1)\big)\\
&= \min(0, 1)\\
&= 0
\end{align*}

\item \textbf{If} \textsc{Distance} \textbf{is} \textsc{VerySmall} \textbf{then} \textsc{Action} \textbf{is} \textsc{BrakeHard}

\begin{displaymath}
\mu_{\textsc{BrakeHard}} = \mu_{\textsc{VerySmall}} = 0
\end{displaymath}

\end{enumerate}

The resulting defuzzified output can be calculated as the \ac{COG} of the aggregated fuzzy set $A$. Rule outputs are clipped before they are aggregated.

%Raw data:
%-10 = 0
%-9  = 0
%-8  = 0
%-7  = 0
%-6  = 0.2
%-5  = 0.2
%-4  = 0.2
%-3  = 0.2
%-2  = 0.3
%-1  = 0.4
%0   = 0.4
%1   = 0.4
%2   = 0.3
%3   = 0.07
%4   = 0.07
%5   = 0.07
%6   = 0.07
%7   = 0
%8   = 0
%9   = 0
%10  = 0

\begin{align*}
\text{COG} &= \frac{\mathlarger{\int_{-10}^{10} x \cdot \mu_A(x)~dx}}{\mathlarger{\int_{-10}^{10} \mu_A(x)~dx}}\\[0.2cm]
&\approx \frac{\sum\limits_{n=-10}^{-7}(n) \cdot 0 + \sum\limits_{n=-6}^{-3}(n) \cdot 0.2 + (-2) \cdot 0.3 + \sum\limits_{n=-1}^{1}(n) \cdot 0.4 + (2) \cdot 0.3 + \sum\limits_{n=3}^{6}(n) \cdot 0.07 + \sum\limits_{n=7}^{10}(n) \cdot 0}{4 \cdot 0 + 4 \cdot 0.2 + 0.3 + 3 \cdot 0.4 + 0.3 + 4 \cdot 0.07 + 4 \cdot 0}\\[0.2cm]
&= \frac{0 + -3.6 + -0.6 + 0 + 0.6 + 1.26 + 0}{2.88} = \frac{-2.34}{2.88} = -0.8125
\end{align*}

\item
Performing \textit{Mamdani} reasoning with \ac{COG} defuzzification using segmentation steps yielded an output of -1.1066.

\newpage

\pythonexternal{fuzzy.py}

\end{enumerate}

\end{document}


\subsection{Ramsey pricing}