\section{Example Application: Fuzzy Extension Principle}
\label{sec:55fuzzy}

\minitoc[-1mm]{84mm}{4}

\noindent
To conclude this chapter, we consider the fuzzy extension principle
as an example application of optimization of B-spline sparse grid surrogates.

\paragraph{Aleatoric and epistemic uncertainties}

Classical uncertainty quantification (UQ) distinguishes between
aleatoric and epistemic uncertainties \cite{Walz16Fuzzy}.
Aleatoric uncertainties result from the variability of inputs or
model components and from the ``intrinsic randomness''
of quantities.
They are best described by probability theory, giving exact probabilities.
Epistemic uncertainties arise from subjectivity,
simplifying modeling assumptions, and incomplete knowledge.
These uncertainties are better captured by fuzzy theory,
which is more imprecise than the ``exact'' stochastic assumptions
of probabilities \cite{Walz16Fuzzy}.

\paragraph{Uncertainty quantification with fuzzy uncertainties}

In uncertainty quantification, the key question is as follows:
Given a model and uncertain input parameters for the model,
how uncertain is the model output?
While there are many approaches available
for probabilistic uncertainties,
it is not straightforward to solve this task
for fuzzy uncertainties.
Fortunately, Zadeh proposed in 1975
the \term{fuzzy extension principle} \cite{Zadeh75Concept},
which addresses this very question.

\paragraph{Sparse grids and B-splines for fuzzy uncertainties}

As we explain in this section,
the fuzzy extension principle requires the solution of numerous
optimization problems that involve the original objective function
$\objfun$.
This predestines the replacement of $\objfun$ with sparse grid surrogates,
as explained in the beginning of the chapter.
Previous work by Klimke \cite{Klimke06Uncertainty} already
studied this approach for piecewise linear functions on uniform sparse grids
and for global polynomials on sparse Clenshaw--Curtis grids.
We assess the suitability of interpolation with higher-order
hierarchical B-splines on sparse grids for the fuzzy extension principle.
It should be mentioned that there is also work
directly incorporating (non-hierarchical) B-splines
into the framework of fuzzy theory for modeling uncertain surfaces
\multicite{Anile00Modeling,Zakaria14Fuzzy}.

\subsection{Fuzzy Sets and Fuzzy Intervals}
\label{sec:551fuzzySets}

In the following, we repeat very briefly the necessary
definitions of basic fuzzy theory.
Examples for the definitions are shown in \cref{fig:fuzzySet}.
A more in-depth introduction can be found in
\multicite{Hanss05Applied,Klimke06Uncertainty,Walz16Fuzzy}.

\begin{figure}
  \subcaptionbox{%
    Non-convex fuzzy set \emph{\textcolor{C0}{(blue)}}
    and $\alpha$-cut \emph{\textcolor{C1}{(red)}.}%
  }[46mm]{%
    \includegraphics{fuzzySet_1}%
  }%
  \hfill%
  \subcaptionbox{%
    Fuzzy interval.%
  }[43mm]{%
    \includegraphics{fuzzySet_2}%
  }%
  \hfill%
  \subcaptionbox{%
    Common types of fuzzy numbers and intervals (see text).%
  }[56mm]{%
    \includegraphics{fuzzySet_3}%
  }%
  \caption[%
    Examples of fuzzy sets and $\alpha$-cuts%
  ]{%
    Examples of membership functions of fuzzy sets and $\alpha$-cuts.%
  }%
  \label{fig:fuzzySet}%
\end{figure}

\paragraph{Fuzzy sets}

Let $X \subset \real$ be a closed interval on the real line
and $\memfun{x}\colon X \to \clint{0, 1}$ be a function.
We call the graph $\fuzzy{x} \ceq \{(x, \memfun{x}(x) \mid x \in X\}$
of $\memfun{x}$ a \term{fuzzy set} with
\term{membership function} $\memfun{x}$.
Fuzzy sets generalize ordinary subsets of $X$,
which can be obtained by requiring $\memfun{x}(X) \subset \{0, 1\}$.
In this case, the fuzzy set is called \term{crisp} and
$\fuzzy{x}$ can be identified with the ordinary set
$\{x \in X \mid \memfun{x}(x) = 1\}$.
A fuzzy set $\fuzzy{x}$ is \term{normalized}
if $\max_{x \in X} \memfun{x}(x) = 1$.
A \term{convex} fuzzy set $\fuzzy{x}$ satisfies
$\min(\memfun{x}(a), \memfun{x}(c)) \le \memfun{x}(b)$ for all $a, b, c \in X$
with $a \le b \le c$.

\paragraph{Fuzzy intervals and $\alpha$-cuts}

A convex and normalized fuzzy set $\fuzzy{x}$ with
piecewise continuous membership function $\memfun{x}$ is called
\term{fuzzy interval.}
If $\{x \in X \mid \memfun{x}(x) = 1\} = \{a\}$ for some $a \in X$,
then the fuzzy interval $\fuzzy{x}$ is called \term{fuzzy number.}

For $\alpha \in \clint{0, 1}$, the $\alpha$-cut of $\fuzzy{x}$ is
defined as $\acut{x}{\alpha} \ceq \{x \in X \mid \memfun{x}(x) \ge \alpha\}$
for $\alpha > 0$ and $\acut{x}{0} \ceq \supp \memfun{x}$ for $\alpha = 0$.
The $\alpha$-cuts of fuzzy intervals $\fuzzy{x}$ are always
nested closed intervals, i.e.,
$\acut{x}{\alpha} = [a, b]$ for some $a \le b$ and
$\acut{x}{\alpha_1} \supset \acut{x}{\alpha_2}$ for $\alpha_1 \le \alpha_2$.

\paragraph{Common types of fuzzy numbers and intervals}

There are various types of fuzzy numbers and intervals
\cite{Klimke06Uncertainty}.
Most common are
\term{triangular fuzzy numbers} (TFNs, i.e., linear B-splines),
\term{trapezoidal fuzzy intervals}
(TFIs, where a plateau of height one is inserted at the peak, i.e.,
sums of two neighboring linear B-splines), and
\term{Gaussian fuzzy numbers} (GFNs) with membership function
$\memfun{x}(x) = \exp(-\frac{(x - \mu)^2}{(2\sigma)^2})$.
As the support of Gaussian fuzzy numbers is unbounded,
\term{quasi-Gaussian fuzzy numbers} (QGFNs) truncate the support
to a fixed multiple of the standard deviation $\sigma$
\cite{Klimke06Uncertainty}.
However, it would be more natural to directly employ B-splines of
degree $p > 1$ (normalized adequately), since they generalize
triangular fuzzy numbers and their limit with respect to $p$
is a Gaussian fuzzy number.

\subsection{Fuzzy Extension Principle}
\label{sec:552fuzzyExtensionPrinciple}

Let $\objfun\colon \clint{\*0, \*1} \to \real$ be an objective function,
whose values $y = \objfun(\*x)$ represent the results of the
simulation of a model with input parameters $(x_1, \dotsc, x_d) = \*x$.
If the input parameters are uncertain and
given as fuzzy sets $\fuzzy{x}_1, \dotsc, \fuzzy{x}_d$,
what is the resulting uncertain outcome
``$\fuzzy{y} \ceq \objfun(\fuzzy{x}_1, \dotsc, \fuzzy{x}_d)$''?
Note that there is no definite answer to this question,
as ``$\objfun(\fuzzy{x}_1, \dotsc, \fuzzy{x}_d)$'' is not well-defined.
The fuzzy extension principle, suggested by Zadeh \cite{Zadeh75Concept},
provides one possible definition.

\paragraph{Alternative fuzzy extension principle}

We use an alternative formulation of the fuzzy extension principle,
which is stated in \cite{Klimke06Uncertainty}.
The original formulation is computationally more complex,
as it requires the solution of equality-constrained optimization problems
and one needs to know the range of $\objfun$, which might not be given.
The two formulations are equivalent,
if $\fuzzy{x}_1, \dotsc, \fuzzy{x}_d$ are (compactly supported)
fuzzy intervals and $\objfun$ is continuous \cite{Buckley90Using},
which we assume in the following.

The alternative fuzzy extension principle defines
``$\fuzzy{y} = \objfun(\fuzzy{x}_1, \dotsc, \fuzzy{x}_d)$'' as the fuzzy set
$\fuzzy{y}$ with
\begin{subequations}
  \label{eq:alternativeFuzzyExtensionPrinciple}
  \begin{alignat}{2}
    \memfun{y}(y)
    &\ceq \sup\{\alpha \in \clint{0, 1} \mid y \in \acut{y}{\alpha}\},\quad
    &&y \in \real,\\
    \acut{y}{\alpha}
    &\ceq \bracket*{
      \min_{\*x \in \Omega_\alpha} \objfun(\*x),\;
      \max_{\*x \in \Omega_\alpha} \objfun(\*x)
    },\quad
    &&\alpha \in \clint{0, 1},\\
    \Omega_\alpha
    &\ceq \acut[1]{x}{\alpha} \times \dotsb \times \acut[d]{x}{\alpha},\quad
    &&\alpha \in \clint{0, 1}.
  \end{alignat}
\end{subequations}
This definition is visualized in \cref{fig:fuzzyExtensionPrinciple}.
The first equation defines $\fuzzy{y}$ via its $\alpha$-cuts,
which are given in the second equation as the closed interval
between the minimal and the maximal value of $\objfun$ on some
hyper-rectangular domain $\Omega_\alpha$.
The third equation specifies this domain $\Omega_\alpha$ as the
Cartesian product of the univariate $\alpha$-cuts.
Hence, we only have to solve box-constrained optimization problems,
as opposed to the general equality-constrained problems
in the original formulation of the fuzzy extension principle.

\begin{figure}
  \includegraphics{fuzzyExtensionPrinciple_1}%
  \caption[%
    Alternative fuzzy extension principle%
  ]{%
    Example of the application of the
    alternative fuzzy extension principle to the bivariate objective function
    $\objfun(\*x) = 6.4 x_1 x_2$ \emph{(bottom)}
    and triangular fuzzy input intervals
    $\fuzzy{x}_1$ and $\fuzzy{x}_2$ \emph{(top and left)}
    to obtain the fuzzy output interval $\fuzzy{y}$ \emph{(right).}
    Adapted from \cite{Klimke06Uncertainty}.%
  }%
  \label{fig:fuzzyExtensionPrinciple}%
\end{figure}

\paragraph{Implementation}

The implementation of the alternative fuzzy extension principle
is straightforward and shown in \cref{alg:alternativeFuzzyExtensionPrinciple}
\cite{Klimke06Uncertainty}.
The range $\clint{0, 1}$ of $\alpha$
is discretized into $m + 1$ uniformly spaced values $\alpha_j$
(where $m \in \nat$).
For each of these values $\alpha_j$, we compute the corresponding
$\alpha_j$-cut of $\fuzzy{y}$ by solving the two box-constrained
optimization problems of \cref{eq:alternativeFuzzyExtensionPrinciple}.
The fuzzy output interval $\fuzzy{y}$ can then be approximated by
interpolating the interval bounds of the $\alpha_j$-cuts of $\fuzzy{y}$.

%\begin{enumerate}
%  \item
%  Discretize $\clint{0, 1}$ with discrete values
%  $\alpha_j \in \clint{0, 1}$ with $m$ segments
%  (e.g., $\alpha_j \ceq j/m$ for $j = 0, \dotsc, m$)
%  and compute $\acut[t]{x}{\alpha_j}$ for all $t = 1, \dotsc, d$
%  and $j = 0, \dotsc, m$ to obtain $\Omega_{\alpha_j}$.
%
%  \item
%  Compute $\acut{y}{\alpha_j}$ by solving
%  $\min_{\*x \in \Omega_{\alpha_j}} \objfun(\*x)$ and
%  $\max_{\*x \in \Omega_{\alpha_j}} \objfun(\*x)$ ($j = 0, \dotsc, m$).
%
%  \item
%  Approximate $\memfun{y}(y)$ by interpolating the data
%  $\{(\min(\acut{y}{\alpha_j}), \alpha_j) \mid j = 0, 1, \dotsc, m\} \cup
%  \{(\max(\acut{y}{\alpha_j}), \alpha_j) \mid j = m, m - 1, \dotsc, 0\}$.
%\end{enumerate}

\begin{algorithm}
  \begin{algorithmic}[1]
    \Function{$\fuzzy{y} = \texttt{alternativeFuzzyExtensionPrinciple}$}{%
      $m$, $\fuzzy{x}_1$, \dots, $\fuzzy{x}_d$%
    }
      \For{$j = 0, \dotsc, m$}
        \State{$\alpha_j \gets j/m$}
        \ForOneLine{$t = 1, \dotsc, d$}{Compute $\acut[t]{x}{\alpha_j} = \clint{a_{j,t}, b_{j,t}}$}
        \State{%
          $\Omega_{\alpha_j} \gets \acut[1]{x}{\alpha_j} \times \dotsb \times
          \acut[d]{x}{\alpha_j} = \clint{\*a_j, \*b_j}$%
        }
        \State{%
          Solve $\min_{\*x \in \Omega_{\alpha_j}} \objfun(\*x)$ and
          $\max_{\*x \in \Omega_{\alpha_j}} \objfun(\*x)$%
        }
        \State{%
          $\acut{y}{\alpha_j} = [c_j, d_j] \gets \clint{
            \min_{\*x \in \Omega_{\alpha_j}} \objfun(\*x),
            \max_{\*x \in \Omega_{\alpha_j}} \objfun(\*x)
          }$
        }
      \EndFor{}\vspace{-2mm}
      \State{%
        $D \gets
        \{(c_j, \alpha_j) \mid j = 0,\, 1,\, \dotsc,\, m\} \cup
        \{(d_j, \alpha_j) \mid j = m,\, m - 1,\, \dotsc,\, 0\}$%
      }
      \State{%
        $\memfun{y} \gets \text{Piecewise linear interpolant of $D$}$%
      }%
      \Comment{extend to $X$ by zero}%
    \EndFunction{}
  \end{algorithmic}
  \caption[Alternative fuzzy extension principle]{%
    Alternative fuzzy extension principle.
    Inputs are the number of $\alpha$ segments to use as discretization and
    the $d$ fuzzy intervals $\fuzzy{x}_1, \dotsc, \fuzzy{x}_d$
    (we have to be able to determine $\alpha$-cuts
    of these fuzzy input intervals).
    The output is an approximation to the output $\fuzzy{y}$
    of the alternative fuzzy extension principle
    (given by an approximation of its membership function $\memfun{y}$).%
  }%
  \label{alg:alternativeFuzzyExtensionPrinciple}%
\end{algorithm}

\subsection{Using B-Splines on Sparse Grids to Propagate Fuzzy Uncertainties}
\label{sec:553fuzzyBSplines}

Following Klimke's approach \cite{Klimke06Uncertainty},
we replace the objective function $\objfun$ in
\cref{alg:alternativeFuzzyExtensionPrinciple}
with a sparse grid surrogate $\sgintp$.
The solution of the optimization problems
$\min_{\*x \in \Omega_{\alpha_j}} \sgintp(\*x)$ and
$\max_{\*x \in \Omega_{\alpha_j}} \sgintp(\*x)$ with respect to the
surrogate $\sgintp$ instead of the true objective function $\objfun$
takes significantly less time, if evaluations of the objective function
are expensive.

However, Klimke used piecewise linear functions as the hierarchical basis on
uniform sparse grids and global polynomials on sparse Clenshaw--Curtis grids.
The drawbacks of each of the bases are evident:
First, piecewise linear surrogates are not continuously differentiable and
can thus not be optimized well with gradient-based optimization methods.
Second, global polynomials are only suitable for
Clenshaw--Curtis grids (Chebyshev-distributed points)
due to Runge's phenomenon,
unnecessarily restricting the choice of grid points.
Hierarchical B-splines of degree $p$ are $(p - 1)$ times
continuously differentiable and defined for arbitrary point
distributions, eliminating both drawbacks simultaneously.

\paragraph{Methodology}

Given a sparse grid $\sgset$, which may be regular or spatially adaptive,
we compute three solutions of the alternative fuzzy extension principle
as follows:

\begin{itemize}
  \item
  First,
  we replace $\objfun$ in \cref{alg:alternativeFuzzyExtensionPrinciple}
  with the sparse grid interpolant $\sgintp[p]$
  on $\sgset$ using modified hierarchical not-a-knot B-splines
  $\bspl[\nak,\modified]{l,i}{p}$ of cubic degree ($p = 3$).
  For solving the optimization problems over $\sgintp[p]$ in
  \cref{alg:alternativeFuzzyExtensionPrinciple},
  we use the globalized version of the method of gradient descent
  as described in \cref{sec:522method} using 100 initial points.
  %and \num{50000} allowed function evaluations of $\sgintp[p]$.
  The resulting fuzzy output interval is denoted by $\fuzzy[\sparse,p]{y}$.

  \item
  Second,
  we replace $\objfun$ in \cref{alg:alternativeFuzzyExtensionPrinciple}
  with the sparse grid interpolant $\sgintp[1]$
  on $\sgset$ using modified piecewise linear basis functions.
  For solving the optimization problems over $\sgintp[1]$ in
  \cref{alg:alternativeFuzzyExtensionPrinciple},
  we use a multi-start version of the Nelder--Mead method
  as described in \cref{sec:522method}
  using 100 initial simplices%
  \footnote{%
    The Nelder--Mead method does not require an initial point,
    but an initial simplex.
    %(given by $d + 1$ points in $\real^d$).
    The method is a hybrid between global and local optimization.
    If the initial simplex is chosen badly, Nelder--Mead may get stuck
    in local minima.
    Hence, we restart the algorithm for different initial simplices.%
  }.
  %and \num{50000} allowed function evaluations of $\sgintp[1]$.
  The resulting fuzzy output interval corresponds to Klimke's method and
  is denoted by $\fuzzy[\sparse,1]{y}$.

  \item
  Third,
  for comparison, we solve
  \cref{alg:alternativeFuzzyExtensionPrinciple} for the
  actual objective function $\objfun$.
  For solving the optimization problems over $\objfun$,
  we use a multi-start version of the Nelder--Mead method
  as described in \cref{sec:522method}
  using 1000 initial simplices
  and \num{2000000} allowed evaluations of $\objfun$.
  %\usenotation{Ëref}
  The resulting fuzzy output interval is denoted by $\fuzzy[\reference]{y}$
  \term{(reference solution).}
\end{itemize}

\noindent
In the following, we fix the number of $\alpha$ segments
in \cref{alg:alternativeFuzzyExtensionPrinciple} as $m = 100$.
As fuzzy input intervals $\fuzzy{x}_t$, $t = 1, \dotsc, d$, we use
the trapezoidal fuzzy interval with $0$-cut $\clint{0.125, 0.625}$
and $1$-cut $\clint{0.25, 0.375}$ if $t$ is odd and
the quasi-Gaussian fuzzy number with mean $0.5$, standard deviation $0.125$,
and $0$-cut $\clint{0.125, 0.875}$ if $t$ is even.

\paragraph{Convergence of fuzzy intervals on regular sparse grids}

As an example,
\cref{fig:resultsFuzzyPropagation} shows the convergence of the
fuzzy output intervals $\fuzzy[\sparse,p]{y}$ and $\fuzzy[\sparse,1]{y}$
obtained by the interpolation of the
bivariate Alp02 function on regular sparse grids $\sgset = \regsgset{n}{d}$
to the reference solution $\fuzzy[\reference]{y}$.
Already for $n = 4$, the B-spline approximation is better than the
piecewise linear approximation.
For $n = 5$, no difference is visible anymore between $\fuzzy[\sparse,p]{y}$
and $\fuzzy[\reference]{y}$, while $\fuzzy[\sparse,1]{y}$ still clearly
deviates from $\fuzzy[\reference]{y}$.

\begin{SCfigure}
  \includegraphics{resultsFuzzyPropagation_1}%
  \caption[Convergence of fuzzy output intervals]{%
    Convergence of the membership functions of the fuzzy output intervals
    $\fuzzy[\sparse,p]{y}$
    (\emph{solid lines,} modified hierarchical cubic not-a-knot B-splines)
    and $\fuzzy[\sparse,1]{y}$
    (\emph{dashed,} modified hierarchical hat functions)
    to the reference solution $\fuzzy[\reference]{y}$
    \emph{\textcolor{C1}{(red)}} for the bivariate Alp02 function using
    regular sparse grids of level $n = 2, \dotsc, 5$.%
  }%
  \label{fig:resultsFuzzyPropagation}%
\end{SCfigure}

In \cref{fig:resultsFuzzyRegular}, we study the convergence of the
relative $\Ltwo$ errors
\begin{equation}
  e^{\sparse,\ast}
  \ceq \frac{
    \normLtwo{\memfun[\reference]{y} - \memfun[\sparse,\ast]{y}}
  }{
    \normLtwo{\memfun[\reference]{y}}
  },\quad
  \ast \in \{1, p\},
\end{equation}
of the membership functions (``fuzzy errors'').
The Alp02 ($d = 2$) errors
that correspond to \cref{fig:resultsFuzzyPropagation}
are shown in green in the left-most plot of \cref{fig:resultsFuzzyRegular}.
For the Bra02, GoP\punctfix{,} and Alp02 functions,
B-spline surrogates achieve
dramatic improvements over the hat function surrogates in the bivariate case.
For the bivariate Ack function, B-splines yield an error that
is still an order of magnitude smaller than the error of hat functions.
Just little or even no improvement can be seen
for the functions Sch06 and Sch22 with discontinuous derivatives or
higher dimensionalities $d \ge 4$.

\begin{figure}
  \includegraphics{resultsFuzzyLegend_1}\\[2mm]%
  \includegraphics{resultsFuzzyRegular_1}%
  \hfill%
  \includegraphics{resultsFuzzyRegular_2}%
  \hfill%
  \includegraphics{resultsFuzzyRegular_3}%
  \hfill%
  \includegraphics{resultsFuzzyRegular_4}%
  \caption[Fuzzy errors for regular sparse grids]{%
    Fuzzy errors
    $e^{\sparse,\ast}
    \ceq \normLtwo{\memfun[\reference]{y} - \memfun[\sparse,\ast]{y}}/
    \normLtwo{\memfun[\reference]{y}}$
    for regular sparse grids $\regsgset{n}{d}$
    and different objective functions $\objfun$ \emph{(colors)}
    over the level $n$ of the sparse grid.%
  }%
  \label{fig:resultsFuzzyRegular}%
\end{figure}

\paragraph{Fuzzy Novak--Ritter method}

We want to employ spatial adaptivity to improve the results
of regular sparse grids.
To this end, we modify the Novak--Ritter criterion
to create a grid generation method that is tailored for the
fuzzy extension principle, resulting in \cref{alg:fuzzyNovakRitterMethod}.
Its main idea is to generate more points near the optima
of the fuzzy extension principle
(\cref{alg:alternativeFuzzyExtensionPrinciple}) than in other regions
of $\clint{\*0, \*1}$.
Therefore, we apply the Novak--Ritter criterion twice to
every $\alpha$ level $\alpha_j = \tfrac{j}{m}$ ($j = 0, \dotsc, m$),
once for the minimum and once for the maximum.
For all $\alpha_j$, the points to be refined are collected in a set.
If a point is selected multiple times for different $\alpha_j$,
it is refined only once.
In addition, we enlarge the search domain $\Omega_{\alpha_j}$
by \SI{10}{\percent}, since the minimum or the maximum might be
near the boundary $\Omega_{\alpha_j}$ and
since the points to be inserted might not be close to
the points to be refined.
We ensure that the size of $\Omega_{\alpha_j}$ is at least $0.05$
in every coordinate direction.
The remaining experiments use $\gamma = 0.1$ as adaptivity.

\begin{algorithm}
  \begin{algorithmic}[1]
    \Function{$\liset = \texttt{fuzzyNovakRitterMethod}$}{%
      $\objfun$, $\gamma$, $m$, $\liset$, $\fuzzy{x}_1$, \dots, $\fuzzy{x}_d$%
    }
      \ForOneLine{$(\*l, \*i) \in \liset$}{$d_{\*l,\*i} \gets 0$}
      \Comment{degrees (number of refinements)}%
      \While{$\setsize{\liset} < \ngpMax$}
        \State{$R \gets \emptyset$}
        \For{$j = 0, \dotsc, m$}
          \State{$\alpha_j \gets j/m$}
          \For{$t = 1, \dotsc, d$}
            \State{$\clint{a_{j,t}, b_{j,t}} \gets \acut[t]{x}{\alpha_j}$}
            \Comment{determine $\alpha_j$-cut}%
            \If{$b_{j,t} - a_{j,t} < 0.05$}
            \Comment{ensure minimal size of $0.05$}%
              \State{%
                $(a_{j,t}, b_{j,t}) \gets
                ((a_{j,t} + b_{j,t})/2 - 0.025,
                (a_{j,t} + b_{j,t})/2 + 0.025)$%
              }\vspace{-1mm}
            \EndIf{}
            \State{%
              $(a_{j,t}, b_{j,t}) \gets
              (a_{j,t} - 0.05 (b_{j,t} - a_{j,t}),
              b_{j,t} + 0.05 (b_{j,t} - a_{j,t}))$%
            }
            \Comment{enlarge by \SI{10}{\percent}}%
            \vspace{-1mm}
          \EndFor{}
          \State{%
            $\liset_j \gets \{(\*l, \*i) \in \liset \mid
            \gp{\*l,\*i} \in \clint{\*a_j, \*b_j} \cap \clint{\*0, \*1}\}$%
          }
          \Comment{%
            set of feasible points
            ($\clint{\*a_j, \*b_j} = \Omega_{\alpha_j}$)%
          }%
          \ForOneLine{$(\*l, \*i) \in \liset_j$}{%
            $r_{\*l,\*i} \gets \setsize{
              \{(\*l', \*i') \in \liset_j \mid
              \objfun(\gp{\*l',\*i'}) \le \objfun(\gp{\*l,\*i})\}
            }$%
          }
          \Comment{ranks}%
          \State{%
            $(\*l^\ast, \*i^\ast) \gets
            \vecargmin_{(\*l,\*i) \in \liset_j} \bracket*{
              (r_{\*l,\*i} + 1)^\gamma
              (\normone{\*l} + d_{\*l,\*i} + 1)^{1 - \gamma}
            }$%
          }
          \Comment{for minimum}%
          \vspace{-0.7mm}
          \State{%
            $(\*l^{\ast\ast}, \*i^{\ast\ast}) \gets
            \vecargmin_{(\*l,\*i) \in \liset_j} \bracket*{
              (\setsize{\liset_j} - r_{\*l,\*i} + 2)^\gamma
              (\normone{\*l} + d_{\*l,\*i} + 1)^{1 - \gamma}
            }$%
          }
          \Comment{for maximum}%
          \State{%
            $R \gets R \cup \{(\*l^\ast, \*i^\ast),
            (\*l^{\ast\ast}, \*i^{\ast\ast})\}$%
          }
        \EndFor{}
        \State{Refine all points in $\liset$ that are in $R$}
        \ForOneLine{$(\*l, \*i) \in R$}{$d_{\*l,\*i} \gets d_{\*l,\*i} + 1$}
      \EndWhile{}
    \EndFunction{}
  \end{algorithmic}
  \caption[Fuzzy Novak--Ritter method]{%
    Fuzzy Novak--Ritter method to generate spatially adaptive sparse grids
    for the fuzzy extension principle.
    Inputs are
    the objective function $\objfun$,
    the adaptivity parameter $\gamma \in \clint{0, 1}$,
    the number of $\alpha$ segments,
    the initial sparse grid $\liset$ as a set of level-index pairs, and
    the $d$ fuzzy intervals $\fuzzy{x}_1, \dotsc, \fuzzy{x}_d$.
    The output is the spatially adaptive sparse grid $\liset$.%
  }%
  \label{alg:fuzzyNovakRitterMethod}%
\end{algorithm}

\paragraph{Convergence of fuzzy intervals on spatially adaptive sparse grids}

As we can see in \cref{fig:resultsFuzzyAdaptive},
the spatially adaptive sparse grids generated by the fuzzy Novak--Ritter
method improve results significantly
for both cubic B-spline and piecewise linear surrogates.
However, the performance of the B-spline surrogates benefits more
from the spatial adaptivity.
Even for higher-dimensional settings such as $d = 6$,
the spatial adaptivity helps to decrease the errors by one order of magnitude.
For instance, for the Ack function in six variables,
we can achieve an error of \SI{2.6}{\percent}
with a budget of \num{10000} objective function evaluations (grid points)
on regular sparse grids.
With the same budget and with spatial adaptivity, the error drops below
\SI{0.25}{\percent}.
Conversely, to achieve the same error as in the regular case
(\SI{2.6}{\percent}),
only \SI{1600} evaluations are needed for spatially adaptive grids.

\begin{figure}
  \includegraphics{resultsFuzzyLegend_2}\\[2mm]%
  \includegraphics{resultsFuzzyAdaptive_1}%
  \hfill%
  \includegraphics{resultsFuzzyAdaptive_2}%
  \hfill%
  \includegraphics{resultsFuzzyAdaptive_3}%
  \caption[Fuzzy errors for spatially adaptive sparse grids]{%
    Fuzzy errors
    $e^{\sparse,\ast}
    \ceq \normLtwo{\memfun[\reference]{y} - \memfun[\sparse,\ast]{y}}/
    \normLtwo{\memfun[\reference]{y}}$
    for spatially adaptive sparse grids $\sgset$ \emph{(solid markers)}
    and different objective functions $\objfun$ \emph{(colors)}
    over the number $\ngpMax$ of objective function evaluations.
    For comparison, the results of \cref{fig:resultsFuzzyRegular}
    for regular sparse grids are repeated \emph{(hollow markers).}%
  }%
  \label{fig:resultsFuzzyAdaptive}%
\end{figure}


The Julia language features a rich ecosystem for scientific computing.
%
LazySets uses \href{https://github.com/JuliaPackaging/Requires.jl}{Requires.jl} to add further functionality depending on whether some external packages are loaded.
%
Here we quickly list various examples, but the list is not exhaustive.
%
From three-dimensional visualizations to reachability analysis using non-convex set representations, there is a broad spectrum of features that are available loading optional dependencies.

\smallskip

Some LazySets functionality prints instructive error messages when the corresponding packages are not loaded but are required by the code. For example, the conversion between constraint and vertex representation of general polytopes in dimension higher than two requires the optional package \href{https://github.com/JuliaPolyhedra/Polyhedra.jl}{Polyhedra.jl}:

\begin{minipage}{\linewidth}
\vspace{-\abovedisplayskip}
\begin{lstlisting}
julia> X = rand(VPolytope, dim=4)

julia> constraints_list(X)
ERROR: AssertionError: package 'Polyhedra' not
  loaded (it is required for executing
  `default_polyhedra_backend`)

# fix the error by loading the optional package
julia> import Polyhedra

julia> constraints_list(X)
12-element Vector{HalfSpace{Float64, ...}}}:
...
	\end{lstlisting}
\end{minipage}

\subsection{Plotting three-dimensional sets and projections}

While writing this article, we received the following question:

\begin{quote}
We need to plot polyhedra given in a form like this:

\begin{center}
	\texttt{2 * x1 >= 0 \& 3 * x2 + 1.7 * x3 >= 0}
\end{center}

Is there a way to plot a 2D projection with LazySets?
\end{quote}

Yes, there is!
We replied with the script below.
The script nicely illustrates the interaction of LazySets with the Julia ecosystem.
We use \href{https://github.com/JuliaSymbolics/Symbolics.jl}{Symbolics.jl} for reading the polyhedron in symbolic form, Polyhedra.jl and \href{https://github.com/JuliaPolyhedra/CDDLib.jl}{CDDLib.jl} for projecting the polyhedron, and \href{https://github.com/JuliaPlots/Plots.jl}{Plots.jl} to plot the two-dimensional projection.

\begin{minipage}{\linewidth}
\vspace{-\abovedisplayskip}
\begin{lstlisting}
using Symbolics
import Polyhedra, CDDLib, Plots

# projected polyhedron from symbolic constraints
vars = @variables x1, x2, x3  # create symbols
P = HPolyhedron([2*x1>=0, 3*x2+1.7*x3>=0], vars)
Q = project(P, [2, 3])  # 2D projection (x2 and x3)

Plots.plot(Q)  # plot the 2D projection
\end{lstlisting}
\end{minipage}

We can also plot the three-dimensional set with \href{https://github.com/JuliaPlots/Makie.jl}{Makie.jl}.
The Makie plot and the two-dimensional projection are shown in Fig.~\ref{fig:polyhedra}.

\begin{minipage}{\linewidth}
\vspace{-\abovedisplayskip}
\begin{lstlisting}
import GLMakie

# intersection with a bounding box
B = BallInf(zeros(3), 5.0)
R = intersection(P, B)

plot3d(R)  # plot in three dimensions
\end{lstlisting}
\end{minipage}

\begin{figure}
	\centering
	\hfill
	\includegraphics[width=0.49\linewidth, height=3cm, keepaspectratio,clip,trim=40mm 14mm 62mm 35mm]{img/polyhedron3D}
	\hfill
	\includegraphics[width=0.49\linewidth, height=3cm, keepaspectratio]{img/polyhedron2D}
	\hfill\,
	\vspace*{1mm}
	\caption{Three-dimensional polyhedron plotted using Makie (left) and a two-dimensional projection obtained by Polyhedra with CDDLib (right). Edges of a triangulation of the polyhedron are plotted in dashed lines (red).}
	\label{fig:polyhedra}
\end{figure}

\subsection{Generic numbers and automatic differentiation}\label{sec:numbertypes}

LazySets types are parametric in the number type. Hence it is simple to use custom number types.
%
Besides rationals and arbitrary-precision floating-point numbers, it is possible to make rigorous floating-point calculations using \href{https://github.com/JuliaIntervals/IntervalArithmetic.jl}{IntervalArithmetic.jl}.
%
Moreover, LazySets features a mechanism to globally tune the numeric tolerances used in floating-point operations.
%
To do so, use \code{set\_atol}, \code{set\_rtol}, and \code{set\_ztol} (for absolute, relative, and comparison-with-zero tolerance) respectively.
%
All set functions have been carefully designed to consistently use the specified tolerance and preserve it during operations.

\smallskip

Questions from users, bug reports, and feature requests are available in the \href{https://github.com/JuliaReach/LazySets.jl/issues/}{issue tracker}.
%
One question we received was from a user of \href{https://github.com/JuliaDiff/ForwardDiff.jl}{ForwardDiff.jl}, which is a package to compute the gradient:

\begin{quote}
	Using ForwardDiff.jl to get the gradient [...] throws the following error:

\texttt{ERROR: default tolerance for numeric type ForwardDiff.Dual\{...\} is not defined}
\end{quote}

The solution was surprisingly simple: extending the LazySets tolerance mechanism to work with dual numbers fixed the error.

\begin{minipage}{\linewidth}
	\vspace{-\abovedisplayskip}
	\begin{lstlisting}
import ForwardDiff
import LazySets.default_tolerance

default_tolerance(::Type{<:ForwardDiff.Dual}) = default_tolerance(Float64)
	\end{lstlisting}
\end{minipage}
With this code, the user could use automatic differentiation (AD) in the formula \code{area(intersection(X, Y))}, which computes the area of the intersection of sets $\X$ and $\Y$. More broadly, AD allows providing information to numerical methods around computational geometry, which is often used in robotics and machine learning~\cite{featherstone2014rigid,BaydinPRS17}.

\subsection{Taylor models as an example of non-convex sets}\label{sec:taylormodels}

LazySet is not limited to convex set representations. Apart from operating with set unions (\code{UnionSet}), which are generally non-convex, the library offers functionality to handle intrinsically non-convex set representations.
%
Examples include star sets (\code{AbstractStar}), polynomial zonotopes (\code{PolynomialZonotope}), and Taylor models. The latter representation is available in the package \href{https://github.com/JuliaIntervals/TaylorModels.jl}{TaylorModels.jl} \cite{TaylorModels.jl,BenetFSS19}, which LazySets interacts with.

\smallskip

Informally, a Taylor model can be thought of as an interval tube around a polynomial.
See for example the red set in Fig.~\ref{fig:taylormodelsconversion}.
%
To illustrate, we use LazySets to convert, or evaluate the range, of a Taylor model (the conversion is formally explained in~\cite{schilling2021verification}).
%
It is easy to operate with Taylor models in LazySets (the definition of \code{vTM} for the linear and nonlinear case is given in Appendix~\ref{sec:taylormodels_appendix}):

\begin{minipage}{\linewidth}
	\vspace{-\abovedisplayskip}
	\begin{lstlisting}
using TaylorModels

# approximate a vector of Taylor models
# with a zonotope
Z = overapproximate(vTM, Zonotope)

# approximate a vector of Taylor models
# with a hyperrectangle
H = overapproximate(vTM, Hyperrectangle)
	\end{lstlisting}
\end{minipage}

\begin{figure}
	\centering
	\includegraphics[height=3.5cm, keepaspectratio]{img/taylormodel_linear}
	\includegraphics[height=3.5cm, keepaspectratio]{img/taylormodel_nonlinear}
	\caption{Left: Exact conversion of a linear Taylor model (red) to a zonotope $Z$ (green) against an inexact evaluation $H$ using interval arithmetic (blue, dashed). Right: Approximate conversion of a non-linear Taylor model (red) using a zonotope (green).}
	\label{fig:taylormodelsconversion}
\end{figure}

In the case when the Taylor model is linear, the conversion is exact, because the zonotope stores linear dependencies between variables.
%
Hence, as is shown in Fig.~\ref{fig:taylormodelsconversion} (left), the zonotope approximation (\code{Z}) is better than directly using interval arithmetic to evaluate the range (\code{H}), and queries about the zonotope provide quick information about the exact set, more accurate than the box approximation.

% \begin{minipage}{\linewidth}
% 	\vspace{-\abovedisplayskip}
% 	\begin{lstlisting}
% julia> d = [-0.35, 0.93]
%
% julia> ρ(d, Z)
% 3.217
%
% julia> ρ(d, H)
% 4.617000000000001
% \end{lstlisting}
% \end{minipage}

\smallskip

If the Taylor model contains nonlinear terms, the zonotope provides only an enclosure but can still be more precise than the box approximation; a comparison is shown in Fig.~\ref{fig:taylormodelsconversion} (right); we obtain a more precise approximation of the Taylor model by partitioning its domain and evaluating each resulting block with a smaller box.

\smallskip

We also mention that LazySets can be combined with an interval-constraint-programming approach by simply loading the optional dependency \href{https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl}{IntervalConstraintProgramming.jl}.

\subsection{Reachability applications}\label{sec:reachability}

LazySets is the core library of JuliaReach, a Julia ecosystem to perform reachability analysis of dynamical systems of the form $\dot{x}(t) = f(x(t), u(t))$.
%
JuliaReach builds on sound scientific approaches such as \cite{BogomolovFFVPS18} and was, in two occasions (2018 and 2020), the winner of the annual friendly competition on Applied Verification for Continuous and Hybrid Systems (\href{https://cps-vo.org/group/ARCH}{ARCH-COMP}).\footnote{JuliaReach also participated in the 2021 edition, but the report is not published yet.}

\smallskip

Reachability techniques are implemented in the JuliaReach package \href{https://github.com/JuliaReach/ReachabilityAnalysis.jl}{ReachabilityAnalysis.jl}, which uses LazySets at its core for dealing with sets, including the computation of reachable states. The union of reachable states for consecutive time intervals is often called a \emph{flowpipe}.
%
We include two examples. In Fig.~\ref{fig:flowpipe_heli}, a flowpipe for the vertical velocity of an 8-dimensional helicopter model with a 20-dimensional controller from \cite{skogestad2007multivariable} is shown.
%
The model has an uncertain initial condition $x(0)$ originating from a set $\X_0$, as well as a non-deterministic input signal $u(t) \in \U$ (i.e., the input signal $u(t)$ can vary arbitrarily within the bounds specified by the set $\U$).
%
The computation terminates in nearly $50$\,ms, illustrating the precision and speed of LazySets.
%
It should be noted that the set of initial states has $2^{28}$ corner cases, thus even in the simpler setting where the inputs are held constant, exhaustive evaluation using simulations is computationally intractable, since it would require $268$ million runs.

\smallskip

As a second example, a three-dimensional flowpipe for the well-known Lorenz system is shown in Fig.~\ref{fig:flowpipe_lorenz}.
%
The initial condition is taken from a flat hyperrectangle of radius $0.1$ along the $x$ coordinate.
%
% \begin{minipage}{\linewidth}
% 	\vspace{-\abovedisplayskip}
% 	\begin{lstlisting}
% X0 = Hyperrectangle(low=[0.9, 0.0, 0.0],
% 				    high=[1.1, 0.0, 0.0])
% 	\end{lstlisting}
% \end{minipage}
%
The reachable states are represented using Taylor models, which are then approximated with zonotopes for further computations.
%
In this case we have exported the LazySets objects to a VTK file using the \href{https://github.com/jipolanco/WriteVTK.jl}{WriteVTK.jl} optional dependency, and rendered the picture with the open-source visualization tool \href{https://www.paraview.org/}{Paraview}.

\begin{figure}[tb]
	\centering
	\includegraphics[width=\linewidth,keepaspectratio]{img/heli_08_simu}
	\vspace*{.5mm}
	\caption{Reachable states for the vertical velocity of the helicopter model.
	We consider two cases for the input signals $u(t)$: constant and non-deterministic. Five hundred trajectories drawn randomly from the set of initial states and with constant inputs are shown on top.}
	\label{fig:flowpipe_heli}
\end{figure}

\begin{figure}[tb]
	\centering
	\includegraphics[width=\linewidth,keepaspectratio,height=4.5cm]{img/lorenz}
	\vspace*{.5mm}
	\caption{Zonotope overapproximation of a Taylor-model flowpipe for the Lorenz equations. The plot is rendered with Paraview.}
	\label{fig:flowpipe_lorenz}
\end{figure}

\subsection{Parametrization and custom array types} \label{sec:custom_arrays}

For set types that contain array fields, we use type parameters. Hence it is possible to instantiate LazySets types with any custom array. Typically, such special arrays (e.g. dense, sparse, static) are used in applications that require high performance.

\smallskip

For example, static arrays (where the size of the array can be determined from the type) are preferable for efficient set computations with ``small'' arrays and are available upon loading \href{https://github.com/JuliaArrays/StaticArrays.jl}{StaticArrays.jl}:

\begin{minipage}{\linewidth}
	\vspace{-\abovedisplayskip}
	\begin{lstlisting}
julia> using StaticArrays

# random ten-dimensional zonotope
julia> Z = rand(Zonotope, dim=10, num_generators=30)

# convert to static arrays
julia> Zs = Zonotope(SVector{10, Float64}(Z.center),
SMatrix{10, 30, Float64}(Z.generators))
julia> d = ones(10)
julia> ds = SVector{10, Float64}(d)

# using normal arrays
julia> @btime ρ($d, $Z)
145.290 ns (0 allocations: 0 bytes)
72.68047192978314

# using static arrays
julia> @btime ρ($ds, $Zs)
44.899 ns (0 allocations: 0 bytes)
72.68047192978314
	\end{lstlisting}
\end{minipage}

\smallskip

Custom arrays can also be used to express knowledge about the \emph{structure} of the set, using only type information. For instance, an axis-aligned half-space can be defined such that its normal vector is a \code{LazySets.SingleEntryVector}, i.e., a vector with a single non-zero element.
%
Then, the concrete intersection with a hyperrectangular set can be computed very efficiently:

\begin{minipage}{\linewidth}
\vspace{-\abovedisplayskip}
\begin{lstlisting}
# half-space with a normal array
julia> @btime intersection($X, $Hvec)
419.424 μs (1799 allocations: 177.83 KiB)

# half-space with a specialized array
julia> @btime intersection($X, $Hsev)
376.059 ns (13 allocations: 1.08 KiB)
\end{lstlisting}
\end{minipage}
This example shows that a 100x speedup is obtained by exploiting structure encoded in the half-space's type.

\subsection{Interoperability with other languages}\label{sec:python}

It is not uncommon that scientists beginning to use Julia are familiar with other languages, specially with the Python programming language.
%
Below we show how to use \href{https://github.com/JuliaPy/pyjulia}{pyjulia} for working with LazySets types \emph{from Python}.
%
We can see that it is not necessary to use Julia objects everywhere; NumPy arrays can also be used, making the interoperability between Julia and Python effortless.

\begin{minipage}{\linewidth}
	\vspace{-\abovedisplayskip}
	\begin{lstlisting}[language=python]
$ python3 -m pip install --user julia

$ python3

>>> import julia
>>> julia.install()  # only once

>>> from julia import Base, LazySets
>>> from julia.LazySets import BallInf, volume

>>> B = BallInf(Base.zeros(3), 1.0)
>>> volume(B)
8.0

>>> import numpy as np
>>> c = np.array([0.0, 0.0, 0.0])
>>> B = BallInf(c, 1.0)
>>> volume(B)
8.0
	\end{lstlisting}
\end{minipage}


\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{a4wide}
%\usepackage{enumerate}

\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Z}{\mathbb{Z}}
\DeclareMathOperator{\Spa}{Spa}
\DeclareMathOperator{\Spv}{Spv}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{proposition}[theorem]{Proposition}
\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newtheorem*{remarkn}{Remark}

\title{Overview of definition of a perfectoid space.}
\begin{document}

\maketitle

This document was written on and shortly after 17th Sept 2018.

\section{Huber rings.}

A \emph{Huber ring} is a topological ring $A$ satisfying the following additional axiom: there exists an open subring $A_0$ containing a finitely-generated ideal $J$ such that the induced topology on $A_0$ is the $J$-adic one. Note that $A_0$ is not part of the data.

Note: this terminology is due to Scholze. The older terminology for this notion is an ``$f$-adic ring''.

A basic example is the $p$-adic numbers $\Q_p$ with its usual topology, with subring $\Z_p$ having topology generated by the ideal $(p)$. Note that the subring $\Q_p$ won't work -- the topology on $\Q_p$ is ``$p$-adic'' in some sense, but it is not the topology generated by the ideal $(p)$ of $\Q_p$, because this ideal is all of $\Q_p$.

Equivalent definitions of a Huber ring, and basic theorems about these rings, can be found in Proposition/Definition 6.1 of \cite{wedhorn} and the lemmas following it.

The notion of a Huber ring has been formalised in the perfectoid project, in the file {\tt Huber\_ring.lean}.

\section{Huber pairs.}

A \emph{Huber pair} is a pair $(A,A^+)$ consisting of a Huber ring $A$ and an open integrally-closed subring of power-bounded elements. Again this is Scholze's terminology -- these used to be called ``affinoid rings''.

Details of what these words mean, and some basic properties of Huber pairs, can be found in Definition~7.14 of~\cite{wedhorn}.

An example of a Huber pair is the pair $(\Q_p,\Z_p)$. Another example is $(\Q_p[T],\Z_p[T])$ with $\Z_p[T]$ given the $p$-adic topology (that is, a polynomial is small if and only if all its coefficents are $p$-adically small -- we don't mind if the degree is big or if the constant coefficient is non-zero).

The notion of a Huber pair has been formalised in the perfectoid project, in the file {\tt Huber\_pair.lean}.

\section{The adic spectrum of a Huber pair.}

The \emph{adic spectrum} $\Spa(A)$ of a Huber pair $(A,A^+)$ (note abuse of notation; $A^+$ is often not mentioned even though it is not uniquely determined by $A$) is quite an elaborate object.

As a set (or a type, if you prefer), it is just the equivalence classes of continuous valuations on $A$ which are bounded by 1 on $A^+$. Valuations are defined in Definition~1.22 of Wedhorn, the equivalence relation is 1.27, continuous valuations are definition 7.7. All of these notions have been formalised in the perfectoid project, and the type {\tt Spa A} for $A$ a Huber pair is defined in {\tt Spa.lean}. An implementation remark: because, formally, the valuations equivalent to a given valuation form a proper class (for trivial reasons -- one can just enlarge the target space of the valuation to be as large as one likes as there is no surjectivity condition) there are universe issues. The design decision made is that if a ring {\tt R} lives in universe {\tt u} then a valuation on {\tt R} can live in another universe {\tt v}, however when forming {\tt Spa A} we restrict to valuations taking values in groups which live in the same universe as {\tt A}, and prove a lemma saying that for any valuation on {\tt A} there is an equivalent valuation taking values in a group which lives in the same universe as {\tt A}.

This set comes with a bunch of extra structures, not all of which are currently in Lean.

1) It has the structure of a topological space, and amongst the open subsets are a special kind of open subsets called \emph{rational subsets}. The definition of the topological space structure on $\Spa(A)$ is that it is a subset of the space $\Spv(A)$ of all valuations on $A$, which has a topology defined in Definition 4.1 of~\cite{wedhorn}; $\Spa(A)$ gets the subspace topology. The definition of a rational subset is Definition 7.29 of~\cite{wedhorn}. Apparently we still have not proved that rational subsets are open -- this is sorried in the perfectoid project.

2) The topological space $\Spa(A)$ has a presheaf of complete topological rings on it. The presheaf is first defined on the rational subsets. To make the definition on the rational subsets one has to have localisations of rings (which mathlib has), and completions of topological rings, which it does not yet quite have (but we are nearly there, thanks to Patrick Massot's valiant efforts). The definition of this presheaf in full is in section 8.1 of~\cite{wedhorn}. To extend from rational subsets to all open subsets we need to take a projective limit; the definition of the presheaf on an arbitrary open is the projective limit of the presheaf on all the rational subsets contained in the open. If the open is itself a rational subset it's hence trivial that we didn't just change the definition (up to canonical isomorphism).

3) Furthermore, the stalk of (the underlying presheaf of rings associated to) this presheaf of topological rings at a point $x\in\Spa(A)$, can be shown to be a local ring, and furthermore the equivalence class of valuations associated to $x$ gives rise to an equivalence class of valuations on this loal ring, whose support is the maximal ideal; this is remark and definition 8.5 of~\cite{wedhorn}. This stuff is not yet in Lean but should not be too hard (famous last words).

{\bf Current status}: We need to be able to complete a topological ring and prove that the completion has the universal property stated in 5.32 of \cite{wedhorn}. This is blocking (2). After that I am hoping (3) should not be too hard, as we have a bunch of stuff on valuations, but as far as I know nobody has thought about this.

\section{Adic spaces}

An adic space is a topological space with a \emph{sheaf} of topological rings, such that the stalks (in the underlying sheaf of rings) are local, and each of these is equipped with a valuation whose support is the maximal ideal, and which has an open cover by subsets each isomorphic to $Spa(A)$ for some Huber pair $A$. Just o be completely clear -- the isomorphism takes place in a category called $V^{pre}$ by Wedhorn, but every mathematician knows what we mean here. Key definitions in \cite{wedhorn}: $V^{pre}$ is on p76, $\mathcal{V}$ on p80, adic space is definition 8.21.

\section{Perfectoid spaces}

A Huber ring is a \emph{Tate ring} if it has a topologically nilpotent unit. This is definition~6.10 of~\cite{wedhorn}. It's also in Scholze in section 3 I guess.

A perfectoid space is just an adic space with some extra property. All this is already in Lean.

\section{Refs}

Wedhorn adic spaces
Scholze etale cohomology of diamonds
\end{document}


\documentclass[11pt]{article}
\usepackage{cctbx_preamble}
\usepackage{amscd}

\title{Least-squares minimisation}
\author{\lucjbourhisatdurham}
\date{\today}

\begin{document}
\maketitle

\section{Introduction}

These notes aim at precisely documenting the math used in the implementation of L.S. minimisation in the \code{cctbx} \cite{cctbx}. As a result, they are fairly technical in nature and the reader is referred to \cite{compcommageconcern:2009} for a presentation with a broader view.

\section{Notations}

Since the both of least-squares on $F$ and on $F^2$ are used in practice, we will use the letter $y$ to refer to either of them. For each Miller indices $h$, we have therefore the observed data $y_o(h)$ and the corresponding calculated $y_c(h)$ and the least-square targets with weights $w(h)$ reads
\begin{equation}
L = \sum_h w(h) (y_o(h) - K y_c(h))^2
\label{eqn:L:def}
\end{equation}
where $K$ is an unknown scale factor. The parameters (atomic sites, ADP's, sof's, etc) that each $y_c(h)$ depends upon will be denoted as $x_1, \cdots, x_n$. Our problem is the minimisation of $L$ with respect to all of $K, x_1, \cdots, x_n$. We will often denotes that dependency as $L(x, K)$ to keep the notations more compact, where $x$ is therefore the vector of parameters $(x_1, \cdots, x_n)$.

\section{Minimisation with respect to the scale factor}

\subsection{Introduction}

There are several methods to find the value of $K$ which minimises $L$ at each minimisation step:
\begin{description}
\item[1-step] $L$ is minimised as a function of $K$ and all $x_i$'s in a single step;
\label{eqn:onestepmini}
\item[2-step] $L$ is first be minimised with respect to $K$ only and that minimum is then function of the $x_i$'s and a second step minimisation is then be performed for those parameters \footnote{In the field of numerical optimisation, one would see this procedure as a trivial application of a much more general technique, known as separable L.S., see \cite{Nielsen:2000fr} for a good introduction with many references therein.};
\item[1$+$1-step] $L$ is first be minimised with respect to $K$ only and the value of $K$ realising that minimum is then use as starting point for a second step minimisation over all parameters (i.e. it starts like the 2-step method but finishes like the 1-step one).
\label{eqn:twostepmini}
\end{description}
The software Crystals~\cite{Crystals:v12} relies on the 1-step method whereas ShelXL~\cite{SHELX:man97} uses the 1$+$1-step. As for the \code{cctbx}, it features two different L.S. minimisation paradigms: one based on the LBFGS algorithm \cite{Nocedal:1980} that is used by the \code{mmtbx} and eventually by \code{PHENIX} \cite{phenix} for macromolecular refinement and a full-matrix L.S. solver, a.k.a. the Gauss-Newton method in mathematical circles, implemented in the \code{smtbx}, that is made available to practising small molecule crystallographers in the \code{Olex 2} software. In the rest of this document, we will focus on those two \code{cctbx} algorithms which both rely on the 2-step method.

\subsection{Optimised scale factor}

Let us expound the math for the minimisation of the L.S. target with respect to the scale factor. We introduce the notations,
\begin{align}
\|y\|^2 &= \sum_h w(h)y(h)^2,\\
y \cdot y' &= \sum_h w(h) y(h) y'(h),
\end{align}
which come from the well-known geometrical interpretation of least-squares\footnote{Mathematically minded people will have recognised scalar products and norms here.}. Then $L$ can be rewritten as follow,
\begin{align}
L &= \| K y_c - y_o \|^2
\label{eqn:L:def:geom}\\
\intertext{which can then be expanded as}
L &= K^2 \|y_c\|^2 -2K y_c \cdot y_o + \|y_o\|^2
\label{eqn:L:def:geom:expanded}
\\
\intertext{and then partially factored as}
L &= \left(K \|y_c\| - \frac{y_c \cdot y_o}{\|y_c\|}\right)^2
+  \|y_o\|^2 \left(1 - \left(\frac{y_c \cdot y_o}{\|y_c\| \|y_o\|}\right)^2 \right) \nonumber
\end{align}
Thus, the value of $K$ which minimises $L$ is the value which zeroes the first parenthesis, i.e.
\begin{align}
\tilde{K} &= \frac{y_c \cdot y_o}{\|y_c\|^2},
\label{eqn:min:L:wrt:K}\\
\intertext{whereas that minimum is the second parenthesis, which can be rewritten in several ways, first by introducing the correlation $C$ between $y_o$ and $y_c$,}
C &= \left(\frac{y_c \cdot y_o}{\|y_c\| \|y_o\|}\right)^2 \\
\left. L \right|_{K=\tilde{K}} &= \|y_o\|^2 ( 1 - C^2 ) \\
\intertext{and then also as,}
\left. L \right|_{K=\tilde{K}} &= \|y_o\|^2 - \tilde{K}^2 \|y_c\|^2
\label{eqn:min:L:wrt:K:val}
\\
\intertext{or, as it is coded in ShelXL for the computation of the so-called weighted $R^2$}
&= \tilde{K}^2 \left(\frac{\|y_o\|^2}{\tilde{K}^2} - \frac{y_c \cdot y_o}{\tilde{K}} \right).
\end{align}
The last parenthesis is what gets assigned to the memory location \code{A(128)} in function \code{SX3I} in file \code{xl.f} in the block of code starting at line 9250. The accumulation of $\|y_c\|^2, \|y_o\|^2, y_c \cdot y_o$ is done in function \code{SXFC} in file \code{xlv.f} starting from line 687.

\subsection{Normal equations }

We need the derivatives with respect to each $x_i$. But let us not forget that $y_c$ is either $|F_c|^2$ or $|F_c|$ and that it is more convenient to write the derivatives of $L$ with the derivatives of $F_c$ for the latter are only dependent on the model without the need to know whether they will be used in a refinement against $F$ or $F^2$, and of course without the knowledge that we minimise least-squares eventually. Writing everything with the derivatives of $F_c$ leads to more modular code and we should thrive for that.

First let's introduce the residuals
\begin{align}
r(h) &= y_o(h) - K y_c(h),
\label{eqn:def:residual}\\
\intertext{which are such that}
L &= \sum_h w(h) r(h)^2 = \|r\|^2 \cdot
\label{eqn:L:vector}
\end{align}
with the geometrical notation we used before.

\subsubsection{First-order derivatives}
\label{lsq:mini:all:firstorder:derivatives}

Mostly straightforward except for two subtleties. The first one is related to the 2-step minimisation procedure. Indeed after $L$ has been minimised over $K$, one wishes to further minimise $L_{K=|\tilde{K}}$ which is a function of the $x_1, \cdots, x_n$ only whose derivatives with respect to each $x_i$ a priori require differentiating $\tilde{K}$ with respect to each $y_c(h)$ -- c.f. \eqnref{min:L:wrt:K:val}. It is actually not necessary to compute those. Indeed
\begin{equation}
\partialder{\left. L \right|_{K=\tilde{K}}}{x_i} = \underbrace{ \left.\partialder{L}{K}\right|_{K=\tilde{K}} }_{\displaystyle = 0} \partialder{\tilde{K}}{x_i} + \left.\partialder{L}{x_i}\right|_{K=\tilde{K}}
\label{eqn:twostep:firstorder:der}
\end{equation}
because $L$ reaches its minimum at $K=\tilde{K}$. Thus we only need the derivatives with respect to $x_i$ keeping $K$ constant -- but with $K=\tilde{K}$ of course.

The second subtlety comes from $F_c$ being a complex quantity, which shall be taken into account when applying the chain rule. The clearest method is to take $F_c$ and it's complex conjugate $F_c^*$ as independent variable and write
\begin{equation}
\partialder{L}{x_i} = \sum_h \partialder{L}{F_c(h)} \partialder{F_c(h)}{x_i} + \partialder{L}{F_c(h)^*}\partialder{F_c(h)^*}{x_i} \cdot \nonumber
\end{equation}
This simplifies because
\begin{align}
\partialder{F_c(h)^*}{x_i} = \left( \partialder{F_c(h)}{x_i} \right)\\
\intertext{which trivially results from $x_i$ being a real variable and}
\partialder{L}{F_c(h)^*} = \left( \partialder{L}{F_c(h)} \right)^*
\end{align}
which comes from $L$ depending on $F_c(h)$ only through $y_c(h)$ which is itself a function of $|F_c(h)|^2$. Before we expound that point, let us conclude with the key formula for the derivatives of $L$,
\begin{equation}
\partialder{L}{x_i} = \sum_h 2 \Re \left( \partialder{L}{F_c(h)} \partialder{F_c(h)}{x_i} \right)
\label{eqn:der:L:wrt:xi}
\end{equation}
which is used in one form or another in all crystallographic refinement codes -- $\Re$ denotes the real part.

Going back in more details to the derivative of $L$ with respect to $F_c(h)$,
\begin{align}
\partialder{L}{F_c(h)} &=
\partialder{L}{y_c(h)}
\partialder{y_c(h)}{|F_c(h)|^2}
\underbrace{ \partialder{|F_c(h)|^2}{F_c(h)} }_{=F_c(h)^*},
&
\partialder{L}{F_c(h)^*} &=
\partialder{L}{y_c(h)}
\partialder{y_c(h)}{|F_c(h)|^2}
\underbrace{ \partialder{|F_c(h)|^2}{F_c(h)^*} }_{=F_c(h)}
\nonumber
\end{align}
which proves the result we announced and gives the useful formulae
\begin{equation}
\partialder{L}{F_c(h)} = \partialder{L}{y_c(h)} \times
\begin{cases}
F_c(h)^* & \text{for $F^2$ refinement},\\
\frac{F_c(h)^*}{2|F_c(h)|} & \text{for $F$ refinement.}
\end{cases}
\label{eqn:der:L:Fc}
\end{equation}
As for the LBFGS method implemented in the \code{cctbx}, all those computations are performed in namespace \code{cctbx::xray}. The computations related to minimisation targets are done in namespace \code{targets} in either class \code{ls\_with\_scale} or \code{least\_squares\_residual} as far as least-squares are concerned. Those uses a variation of the scheme described here: they actually compute $\partialder{L}{F_c(h)^*}$ which forces the classes in charge of the crystallographic models to compute $\partialder{F_c(h)^*}{x_i}$ instead of $\partialder{F_c(h)}{x_i}$. That trivial substitution results in changing a few signs. Those model derivative computations are done in namespace \code{structure\_factors} in classes \code{gradients\_direct} and \code{fast\_gradients}. The former does the sum over $h$ in \eqnref{der:L:wrt:xi} directly whereas the latter uses FFT.

As for full-matrix L.S., structure factor computations are implemented in name space \code{smtbx::structure\_factors::direct} (the moniker direct means here that we directly perform the sum over the reflections in \eqnref{der:L:wrt:xi}). This is also where \eqnref{der:L:wrt:xi} is taken advantage of. Actually \eqnref{der:L:wrt:xi,der:L:Fc} are handled together by two C++ classes \code{modulus} and \code{modulus\_squared}. The computation of the optimised scale factor has been abstracted into a generic tool that can be reused for any L.S. problem, and it is implemented in a L.S. toolbox living in namespace \code{scitbx::lstbx}.

It should be noted that all the formulae written so far in this section \ref{lsq:mini:all:firstorder:derivatives}, except \eqnref{twostep:firstorder:der}, are actually correct for any minimisation target, not just least-squares since we have not specialised $\partialder{L}{F_c(h)}$ or $\partialder{L}{y_c(h)}$. In the \code{cctbx}, it is therefore used for the log-likelihood target too. We will now write those derivatives for the least-squares only.

The key formula is
\begin{align}
\partialder{L}{y_c(h)} &= \sum_h - w(h) 2K y_c(h) r(h)\\
\intertext{or in vector form}
\grad{L}{y_c} &= -2K y_c \cdot r
\end{align}
which are valid whether $L$ has been minimised over $K$ or not, as discussed in the introduction of this section.

\subsubsection{Second-order derivatives}

Quasi-Newton methods such as LBFGS only need the first-order derivatives but Newton methods require the second-order ones too. In the case of least-squares, those can conveniently be expressed with the first-order derivatives only, in the limit of small residuals, leading to the well-known Gauss-Newton method.

The crux of the Gauss-Newton method is to remark that, starting from \eqnref{L:vector},
\begin{align}
\partialderxy{L}{x_i}{x_j} &= \partialder{r}{x_i} \cdot \partialder{r}{x_j} + \partialderxy{r}{x_i}{x_j} \cdot r, \nonumber \\
\intertext{and then that for a well behaved fit, the residual $r$ and the curvature in the second term of the right hand side are small enough that they can safely be neglected, leading to}
\partialderxy{L}{x_i}{x_j} &= \partialder{r}{x_i} \cdot \partialder{r}{x_j}.
\label{eqn:gaussnewtoncrux}
\end{align}
This formula is completely general and does not depend on whether there is a scale factor and therefore not on whether one has already optimised it. We will assume this approximation holds good in the remaining of this document and derive the extra amount of mathematics necessary to handle our optimised scale factor.

From the definition of the residual $r$ in \eqnref{def:residual} and from the expression of the optimised scale factor \eqnref{min:L:wrt:K}, one easily gets the derivatives,
\begin{align}
\frac{\partial}{\partial x_i} r(x, \tilde{K}(x)) &= - \left( \tilde{K}\partialder{y_c}{x_i} + \partialder{\tilde{K}}{x_i} y_c \right), \\
\partialder{\tilde{K}}{x_i} &= \frac{1}{\|y_c\|^2}\partialder{y_c}{x_i} \cdot \left( y_o - 2 \tilde{K} y_c \right).
\end{align}

We can now build the normal equations, which are just the Newton equations for the shifts $s$ of the parameter vector $x$,
\begin{equation}
B s = c
\end{equation}
using the above approximation \eqnref{gaussnewtoncrux} for the Hessian $B$. The normal matrix $B$ reads
\begin{equation}
 \begin{split}
B = \frac{\partial}{x_i} r(x, \tilde{K}(x)) \frac{\partial}{x_j} r(x, \tilde{K}(x))
= & \tilde{K}^2 \partialder{y_c}{x_i} \cdot \partialder{y_c}{x_j}
+ \tilde{K} \left(  \partialder{\tilde{K}}{x_j} y_c \cdot \partialder{y_c}{x_i}
                        + \partialder{\tilde{K}}{x_i} y_c \cdot \partialder{y_c}{x_j} \right)
\\ & + \partialder{\tilde{K}}{x_i} \partialder{\tilde{K}}{x_j} \|y_c\|^2
\end{split}
\end{equation}

whereas the right-hand side of the normal equations reads

\begin{equation}
c = -r(x, \tilde{K}(x)) \cdot \frac{\partial}{\partial x_i} r(x, \tilde{K}(x)) = (y_o - \tilde{K} y_c)
\cdot \left(\tilde{K} \partialder{y_c}{x_i} + \partialder{\tilde{K}}{x_i} y_c \right)
\end{equation}

The last two equations which are implemented in the L.S. generic toolbox living in namespace \code{scitbx::lstbx}.

\section{Structure factors and their derivatives}

As per \eqnref{der:L:wrt:xi,der:L:Fc}, the last component one needs is the computation of the complex structure factors $F_c$ and of their derivatives. This section is dedicated to present the necessary formulae.

\newcommand{\Fuc}{F_{\text{uc}}}
\newcommand{\Fasu}{F_{\text{asu}}}
Since the structure factor of the unit cell content is the sum over the contribution of each scatterer, we focus on one such contribution only. Thus we consider a scatterer with occupancy $o$, $u_{\text{iso}}$ or ADP $U^*$, at position $x$ (the both of $U^*$ and $x$ are with respect to fractional coordinates). For a triplet of Miller indices $h$, we consider its contribution $\Fuc(h)$ to the entire unit cell and its contribution $\Fasu(h)$ to the asymmetric unit only. In the presence of non-trivial crystallographic symmetries, we have the relation
\begin{equation}
\Fuc(h) = \sum_{(R \mid t) \in \cal{O}} \Fasu^{(R \mid t)}(h),
\end{equation}
where $\cal{O}$ is the subset of symmetry operators $(R \mid t)$ of rotational part $R$ and translational part $t$ that generate the orbit of the position $x$ under the application of the entire set of symmetries in the space group of the structure.

The standard crystallographic model is defined by
\begin{equation}
\Fasu(h) = o f(h^2) e^{-2\pi^2 h^2} u_{\text{iso}} \underbrace{e^{h U^* h^T} e^{i 2\pi h x}}_{G(h)}.
\end{equation}
In this formula and the following, we consider $h$ to be a row vector of Miller indices. We have also written the formula with both isotropic and anisotropic displacement factors: in practice one of them is zero but handling the both of those cases together simplifies the mathematical reasoning.

Since only $G(h)$ is altered by symmetry, i.e. the coefficient in front of $G(h)$ is isotropic a function of $h$, we can factor it out of the sum over the symmetries,
\begin{align}
\Fuc(h) &= o f(h^2) e^{-2\pi^2 h^2} u_{\text{iso}} \sum_{(R \mid t) \in \cal{O}} G^{(R \mid t)}(h) \\
\intertext{where}
G^{(R \mid t)}(h) &= G(hR) e^{i 2\pi ht}.
\end{align}

Let us consider the case of centred space group first. The sum can be broken as follow
\begin{align}
 \sum_{(R \mid t) \in \cal{O}} G^{(R \mid t)}(h) &=  \sum_{(R \mid t) \in \cal{O'}} \sum_{\tau \in \cal{T}} G^{(R \mid t+\tau)}(h), \nonumber\\
\intertext{where $\cal{T}$ is the set of all centring translations, including zero, whereas $\cal{O'}$ is the set of ``primitive'' symmetries. Then}
G^{(R \mid t + \tau)} &= G(hR) e^{i2\pi ht} e^{i2\pi h\tau}, \nonumber \\
\intertext{but $h\tau=0$ for any Miller indices $h$ by definition, and therefore}
\sum_{(R \mid t) \in \cal{O}} G^{(R \mid t)}(h) &= n_{\text{ltr}} \sum_{(R \mid t) \in \cal{O'}}
\end{align}
which shows that the centred case and the primitive case only differ by an overall multiplicative factor, the number of centring translations $n_{\text{ltr}}$.

Thus we can now come back to the primitive case, for which three cases are to be considered.
\begin{enumerate}
\item The Miller index $h$ is non-centric. Then there is no further simplification.
\item The Miller index $h$ is centric. Then the sum over the symmetries may be split in two,
\begin{align}
\sum_{(R \mid t) \in \cal{O}} G^{(R \mid t)} &=
\underbrace{\sum_{(R \mid t) \in \cal{O^+}} G^{(R \mid t)} }_{\Sigma}
+ \sum_{(R \mid t) \in \cal{O}^+} G^{\left(\bar{1} \mid t_{\bar{1}}\right)(R \mid t)}, \nonumber \\
\intertext{where $\cal{O}^+$ is the subset of $\cal{O}$ containing only the symmetries not involving the inversion. However}
 G^{(\bar{1} \mid t_{\bar{1}})(R \mid t)}(h) &= G^{\left(-R \mid -t + t_{\bar{1}}\right)}(h) = G(-hR) e^{-2\pi hRx}
 e^{-i2\pi ht} e^{i2pi h t_{\bar{1}}}, \nonumber \\
 \intertext{but then from the very definition of $G(h)$,}
 G(-hR) &= G(h)^*, \\
 \intertext{and therefore}
 \sum_{(R \mid t) \in \cal{O}} G^{(R \mid t)} &= \Sigma + \Sigma^* e^{i2\pi h t_{\bar{1}}}.
\end{align}

\item The Miller index $h$ is centric and the space group is centro-symmetric. In which case, $t_{\bar{1}}=0$ and therefore
\begin{align}
 \sum_{(R \mid t) \in \cal{O}} G^{(R \mid t)} &= 2 \Re{\Sigma} = 2 \sum_{(R \mid t) \in \cal{O}^+}  e^{h R U^* (hR)^T} \cos(hRx + t).
\end{align}
\end{enumerate}

In the 3rd case, the structure factor contribution is therefore real, and its derivative will be so too, the former involving a cosine and the latter involving a sine for the derivatives with respect to $x$. It is thus important to handle this case separately for the sake of efficiency. This is the rationale for the two C++ classes \code{in\_generic\_space\_group} and \code{in\_origin\_centric\_space\_group} living in namespace \code{smtbx::structure\_factors::direct}. The former handles the first two cases whereas the latter handles the 3rd case.

As for the computation of the subset of symmetries $\cal{O}$ or $\cal{O^+}$, it is devolved to a nifty little tool featured by the \code{cctbx}, the class \code{hr\_ht\_cache} that lives in namespace \code{cctbx::xray::structure\_factors}. As the name indicates, it computes and then caches all the $hR$ and $e^{i2\pi ht}$ for a given $h$, those quantities being then reused for each scatterer in the asu, as detailed above. In turn, \code{hr\_ht\_cache} relies on the class \code{sgtbx::space\_group} to iterate over the symmetry operations necessary to complete the sums expounded above.

Finally the derivatives of the structure factor contribution of one scatterer to the unit cell, $\Fuc$, whose computation we have just explained in great details, is straightforward since it involves only exponentials of the real and complex kind (or sines and cosines in the centrosymmetric case). The only difficulty here is in the efficient coding of the chain of operations: the source code itself is the best place to look into. The relevant C++ classes are all in the header file \code{standard\_xray.h}.

\bibliography{cctbx_references}

\end{document}


% Created 2017-02-14 mar 14:21
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage[margin=18mm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{parskip}
\usepackage{standalone}
\usepackage{tikz,pgf,pgfplots}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks,decorations.markings,datavisualization,datavisualization.formats.functions,intersections,external}
\usetikzlibrary{decorations.pathmorphing,patterns}
\pgfplotsset{compat=1.9}
\newcommand*{\mexp}[1]{\ensuremath{\mathrm{e}^{#1}}}
\newcommand*{\laplace}[1]{\ensuremath{\mathcal{L} \{#1\}}}
\newcommand*{\laplaceinv}[1]{\ensuremath{\mathcal{L}^{-1} \{#1\}}}
\newcommand*{\ztrf}[1]{\ensuremath{\mathcal{Z} \{#1\}}}
\newcommand*{\ztrfinv}[1]{\ensuremath{\mathcal{Z}^{-1} \{#1\}}}
\newcommand*{\realpart}[1]{\ensuremath{\operatorname{Re}(#1)}}
\newcommand*{\impart}[1]{\ensuremath{\operatorname{Im}(#1)}}
\newcommand*{\vsp}[1]{\rule{0pt}{#1}}
\newcommand*{\tderiv}[1]{\ensuremath{\frac{d^{#1}}{dt^{n}}}}
\newcommand*{\bbm}{\begin{bmatrix}}
\newcommand*{\ebm}{\end{bmatrix}}
\newcommand*{\obsmatrix}{\mathcal{O}}
\newcommand*{\contrmatrix}{\mathcal{C}}
\newcommand*{\cwh}{\ensuremath{\cos \omega h}}
\newcommand*{\swh}{\ensuremath{\sin \omega h}}
\newcommand*{\numc}{(2z-1)\big(1 - \mexp{-2h}\big)}
\newcommand*{\denc}{(z-1)\big(z - \mexp{-2h}\big)}
\author{Kjartan Halvorsen}
\date{2015-09-18}
\title{Computerized control - partial exam 1}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 24.5.1 (Org mode 8.2.10)}}
\begin{document}

\maketitle

\section*{Problem 1}
\label{sec-1}
Consider the continuous-time system with the transfer function
\[ G(s) = \frac{2}{s+2} \]
The system is sampled with sampling interval $h$ using zero-order hold. \textbf{Show that the pulse-transfer function for the sampled system is}
\[ H(z) = \frac{1-\mexp{-2h}}{z-\mexp{-2h}}. \]

\section*{Problem 2}
\label{sec-2}
The sampled system in Problem 1 is controlled using the discrete-time controller
\[ F(z) = 1 + \frac{1}{2(z-1)} = \frac{2z-1}{z-1}. \]
\begin{center}
\includestandalone[width=0.5\linewidth]{feedbackc}
\end{center}

\begin{enumerate}
\item \textbf{Calculate the pulse-transfer function} of the closed-loop system from $u_c(kh)$ to $y(kh)$.
\item Let $h=\frac{\ln 2}{2} \approx 0.35$. \textbf{Is the closed-loop system stable?}
\end{enumerate}
% Emacs 24.5.1 (Org mode 8.2.10)
\end{document}


In this section the quantum counterpart of the previous classical system will be
studied. In the following the process of \emph{canonical quantization} is used
to go from the classical world to the quantum one and a brief summary is given
in the Appendix~\ref{appendix:canonical_quantization}. This procedures will be
used to derive the Quantum Pais-Uhlenbeck (QPU) oscillator starting
from~\eqref{eq:lagrangian_PU}. The quantum version of the Ostrogradsky theorem
will be proved and lastly more examples of ghost-ridden theories are given.

\subsection{Ghosts}\label{section:ghosts}
As said previously in Section~\ref{subsection:
linear_ostrogradskian_instability}, in quantum mechanics \emph{linear
Ostrogradskian instability} goes under the name ``\emph{ghost}''. Just like
classical HD theories, quantum HD theories have the spectrum of the Hamiltonian
unbounded. Theorem~\ref{th:ostrogradsky_classical} has its quantum counterpart:

\begin{theorem}\label{th:ostrogradsky_quantum}~\cite{Smilga17}
  The quantum counterpart of Hamiltonian~\eqref{eq:general_hamiltonian_1dim}
  for a non-degenerate higher-derivative system has no ground state.
\end{theorem}
\begin{proof}
  The proof is given for a one-dimensional system ($\mu=1$) describe by a second
  order Lagrangian ($n=2$); the generalization for a multi-dimensional HD system
  is straightforward. The result Hamiltonian is
  \begin{equation*}
    H = h(Q_1, Q_2, P_2) P_2 + Q_2 P_1 - L(Q_1, Q_2, h)
  \end{equation*}
  Using the canonical quantization the function H is converted to a quantum
  observable $\hat{H}$ which eigenvalue are the allowed energies of the system.
  \begin{align*}
    & \begin{cases}
      Q_1 \xrightarrow{\wedge} \hat{Q_1} \equiv \hat{x}
      \quad &\mid \quad \hat{x}\psi(x, v) = x \psi(x, v) \\
      P_1 \xrightarrow{\wedge} \hat{P_1} \equiv \hat{p}_x
      \quad &\mid \quad \hat{p}_x\psi(x, v) = -i\partial_x \psi(x, v) \\
    \end{cases} \\
    & \begin{cases}
      Q_2 \xrightarrow{\wedge} \hat{Q_2} \equiv \hat{v}
      \quad &\mid \quad \hat{v}\psi(x, v) = v \psi(x, v) \\
      P_2 \xrightarrow{\wedge} \hat{P_2} \equiv \hat{p}_v
      \quad &\mid \quad \hat{p}_v\psi(x, v) = -i\partial_v \psi(x, v) \\
    \end{cases}
  \end{align*}
  \begin{equation} \label{eq:ham_operator_2nd_order}
    \hat{H} = h(\hat{x}, \hat{v}, \hat{p}_v) \hat{p}_v + \hat{v} \hat{p}_x -
    L (\hat{x}, \hat{v}, h)
  \end{equation}

  Moreover suppose that the spectrum of the Hamiltonian is discrete. If it were
  continuous, we can put the system in a box so the range of $x$ and $v$ is
  finite which makes the spectrum discrete. At the end we can remove the
  regularization by sending the box size to infinity. Let $\psi_0(x, v)$ be the
  normalized ground state of the system with the associated eigenvalue $E_0$,
  i.e. $\hat{H}\psi_0(x, v) = E_0 \psi_0(x, v)$. To seek for an energy value
  close to $E_0$ we use the \emph{variational method}~\footnote{
    Variational methods must not be confused with variational calculus, they are
    totally different things.
  } with the following variational ansatz:
  \begin{equation}
    \psi_c(x, v) = e^{icx}\psi_0(x,v)
  \end{equation}

  A generic linear combination of state $\psi_c$ is
  \begin{equation}
    \psi_f = \int f(c) \psi_c \, dc = \psi_0 \int f(c) e^{icx} \, dc =
    \psi_0 \tilde{f}(x)
  \end{equation}
  where $\tilde{f}(x)$ is the Fourier transform of $f(c)$.

  Notice that neither $\psi_c$ nor $\psi_f$ are eigenfunction of the
  Hamiltonian~\eqref{eq:ham_operator_2nd_order}
  \begin{align*}
    &\hat{H} \psi_c(x,v) = E_0\psi_c(x, v) + cv \psi_c(x,v)
    \neq \lambda \psi_c(x,v) \\
    &\hat{H} \psi_f(x,v) = E_0\psi_f(x, v) -
    iv \frac{\partial \tilde{f}(x)}{\partial x} \psi_c(x,v)
    \neq \lambda \psi_c(x,v)
  \end{align*}

  Since $\psi_c$ and $\psi_f$ do not vanish their expectation value
  must not be lower than $E_0$ and can be obtain by direct calculation.

  \begin{equation*}
    E_0 \leq E_c = \int \psi_c^* \hat{H} \psi_c \, dx \, dv =
    E_0 + c \int v \left|\psi_0 \right| ^2 \, dx \, dv
  \end{equation*}

  If the integral $\int v \left|\psi_0 \right| ^2 \, dx \, dv$ does not vanish
  then arbitrarily negative energies can be reach by an appropriate choice of $c$.
  Otherwise if the integral vanish, $E_c$ does not depend on $c$ and does not
  grow. Thus, the system does not have a non-degenerate ground state. \\
\end{proof}

\subsection{Quantum Pais-Uhlenbeck oscillator}
The first study to be publish about a quantum HD theory was the \emph{Quantum
Pais-Uhlenbeck oscillator} (QPU)~\cite{PU50}. In order to obtain the Hamiltonian
(operator) $\hat{H}_{QPU}$ of the QPU system,
the $L_{PU}$~\eqref{eq:lagrangian_PU} had to be converted in the corresponding
Hamiltonian $H_{PU}$ and then canonically quantize it, i.e.
$L_{PU} \rightarrow H_{PU} \xrightarrow{\wedge} \hat{H}_{QPU}$
\footnote{
  Notation for quantum operators is
  the same used in the proof of Theorem~\ref{th:ostrogradsky_quantum}
}.

\begin{equation} \label{eq:hamiltonian_QPU}
  \hat{H}_{QPU} = \hat{p}_x \hat{v} + \frac{\hat{p}_v^2}{2}
  + \left(\omega_1^2 + \omega_2^2\right)\frac{\hat{v}^2}{2}
  - \omega_1^2\omega_2^2 \frac{\hat{x}^2}{2}
\end{equation}

\subsubsection{Non-degenerate frequencies: $\omega_1 \neq \omega_2$}

Consider the following canonical transformation and then canonically quantize
the new canonical variables $\tilde{Q}$ and $\tilde{P}$
\footnote{
  The proposed transformation is canonical because the canonical commutation
  relations are still valid, i.e.
  $\left\{ \tilde{Q_i} \, , \, \tilde{Q_j}\right\} =
  \left\{ \tilde{P_i} \, , \, \tilde{P_j}\right\} = 0$ and
  $\left\{ \tilde{Q_i} \, , \, \tilde{P_j}\right\} = \delta_{ij}$
}~\cite{Mannheim05}.

\begin{align*}
  \tilde{Q_1} &= \frac{1}{\omega_1}
    \frac{P_1 \, + \,  \omega_1^2 Q_2} {\sqrt{\omega_1^2-\omega_2^2}}
              &\xrightarrow{\wedge}& &\hat{\tilde{Q}}_1 \equiv
  \hat{X}_1 &= \frac{1}{\omega_1}
    \frac{\hat{p}_x \, + \,  \omega_1^2 v} {\sqrt{\omega_1^2-\omega_2^2}}
    \\
  \tilde{P_1} &= \omega_1
    \frac{P_2 \, + \, \omega_2^2 Q_1} {\sqrt{\omega_1^2-\omega_2^2}}
              &\xrightarrow{\wedge}& &\hat{\tilde{P}}_1 \equiv
    \hat{P}_1 &= \omega_1
    \frac{\hat{p}_v \, + \,  \omega_2^2 x} {\sqrt{\omega_1^2-\omega_2^2}}
    \\
  \tilde{Q_2} &=
    \frac{P_2 \, + \,  \omega_1^2 Q_1} {\sqrt{\omega_1^2-\omega_2^2}}
              &\xrightarrow{\wedge}& &\hat{\tilde{Q}}_2 \equiv
  \hat{X}_2 &=
    \frac{\hat{p}_v \, + \,  \omega_1^2 x} {\sqrt{\omega_1^2-\omega_2^2}}
    \\
  \tilde{P_2} &=
    \frac{P_1 \, + \, \omega_2^2 Q_2} {\sqrt{\omega_1^2-\omega_2^2}}
              &\xrightarrow{\wedge}& &\hat{\tilde{P}}_2 \equiv
  \hat{P}_2 &=
    \frac{\hat{p}_x \, + \,  \omega_2^2 v} {\sqrt{\omega_1^2-\omega_2^2}}
\end{align*}

Using $\hat{X}$ and $\hat{P}$, the Hamiltonian~\eqref{eq:hamiltonian_QPU}
becomes the difference of two independent quantum harmonic oscillators.

\begin{equation} \label{eq:Ham-QPU-HO}
  \hat{H} =
  \frac{\hat{P}_1^2 \, + \, \omega_1^2 \hat{X}_1^2}{2} -
  \frac{\hat{P}_2^2 \, + \, \omega_2^2 \hat{X}_2^2}{2} \equiv
  \hat{H}_1 - \hat{H}_2
\end{equation}

The spectrum of $\hat{H}$ follows from the solution of the equation
$\hat{H} \psi = E \psi$. Let $\psi$ be the product of the eigenstates of two
independent simple one-dimensional quantum harmonic oscillator $\psi_n$ and
$\psi_m$, i.e.

\begin{equation*}
  \psi(X_1, X_2) = \psi_n(X_1) \psi_m(X_2)
  \quad \text{where} \quad
  \begin{cases}
    &\hat{H}_1\psi_n = \omega_1 \left( \frac{1}{2} + n \right)\psi_n \\
    &\hat{H}_2\psi_m = \omega_2 \left( \frac{1}{2} + m \right)\psi_m
  \end{cases}
\end{equation*}

\begin{align*}
  \hat{H} \psi
  &= \left(\hat{H}_1 - \hat{H}_2\right) \left(\psi_n \psi_m\right) \\
  &= \left(\hat{H}_1\psi_n\right)\psi_m - \left(\hat{H}_2\psi_m\right)\psi_n \\
  &= \left(
    \omega_1\left(\frac{1}{2} + n\right) -
    \omega_2\left(\frac{1}{2} + m\right)
     \right) \left(\psi_n\psi_m\right)
\end{align*}

So the spectrum is

\begin{equation}
  E_{mn} =
  \omega_1 \left( \frac{1}{2} + n \right) -
  \omega_2 \left( \frac{1}{2} + m \right)
  \qquad m,n = 0,1,2,\ldots
\end{equation}

Consider the situation where ratio between $\omega_2$ and $\omega_1$ is a
rational number $k$; the spectrum can be written as

\begin{equation*}
  E_{mn} = \omega_1 \left( \frac{1-k}{2} + n + mk \right)
\end{equation*}
so the spectrum is discrete. On the other hand if the frequencies are
incommensurable the every real number can be hit by an appropriate choice of
$n$ and $m$. But the wave function of all eigenstate are normalizable, so the
spectrum is not continuous but rather pure point. \\

In theoretical physics, \emph{ghosts} are quantum states with negatives norms.
Negative norm means ``negative probability'' which does not make sense, so
these states does not have a physical meaning. Nevertheless they appear in a
well established theories (e.g. QED) during the quantization procedure and in
such contests they turn out to be harmless~\cite{Smilga01}.

Ghosts appear in the quantum Pais-Uhlenbeck oscillator when we try to get rid
of negatives energies (i.e.\ make the energy spectrum bounded from below).
First we define the usual creation and annihilation operators for the
quantum harmonic oscillators
\begin{equation} \label{eq:a_operators}
  \hat{a}_j := \frac{1}{\sqrt{2\omega_j}}
    \left(\partial_{X_j} + \omega_j X_j\right) \quad
  \hat{a}_j^{\dagger} := \frac{1}{\sqrt{2\omega_j}}
    \left(-\partial_{X_j} + \omega_j X_j\right)
\end{equation}
where their commutator is
\begin{equation} \label{eq:a_commutator}
  \left[ \hat{a}_j, \hat{a}_j^{\dagger} \right] =
  \hat{a}_j \ \hat{a}_j^{\dagger} - \hat{a}_j^{\dagger} \ \hat{a}_j = 1
\end{equation}

Using~\eqref{eq:a_operators} can rewrite the Hamiltonian~\eqref{eq:Ham-QPU-HO} as
\begin{align} \label{eq:Ham_a_operators}
  \hat{H} &=
    \omega_1 \left( \hat{a}_1 \hat{a}_1^{\dagger} - \frac{1}{2} \right) -
    \omega_2 \left( \hat{a}_2 \hat{a}_2^{\dagger} - \frac{1}{2} \right) \notag
    \\    &=
    \omega_1 \hat{a}_1 \hat{a}_1^{\dagger} -
    \omega_2 \hat{a}_2 \hat{a}_2^{\dagger} +
    \frac{\omega_2 - \omega_1}{2}
\end{align}

Notice that the second term in~\eqref{eq:Ham_a_operators} produces exited states
with decreasing negatives energies. We can stop this behavior by \emph{formally}
defining a state $\phi(X_2)$ such that
\begin{equation*}
  \hat{a}_2^{\dagger} \ \phi(X_2) = 0
\end{equation*}

The wave function of such state is~\cite{Ilhan13}
\begin{equation*}
  \phi(X_2) \propto \exp \left\{ \frac{\omega_2}{2} X_2^2\right\}
\end{equation*}
indeed
\begin{align*}
  \hat{a}_2^{\dagger} \ \phi(X_2)
  &= \frac{1}{\sqrt{2\omega_2}}
    \left(-\partial_{X_2} + \omega_2 X_2\right)
    \exp \left\{ \frac{\omega_2}{2} X_2^2\right\} \\
  &= \frac{1}{\sqrt{2\omega_2}}
    \left(-\frac{\omega_2}{2} \, 2 \, X_2 + \omega_2 X_2\right)
    \exp \left\{ \frac{\omega_2}{2} X_2^2\right\}
  = 0
\end{align*}

The $\phi$ state has positive norm and have the lowest energy by definition.
The operator $\hat{a}_2$ acts on it as creation operator while
$\hat{a}_2^{\dagger}$ acts on it as annihilation operator (this behavior it's
the opposite of the usual one). The consequences of defining such state $\phi$
is that \emph{exited states of $\phi$ have negatives norms (i.e.\ are ghosts)}.
Starting from $\phi$ we can obtain exited states with the action of the creation
operator $\hat{a}_2$
\begin{equation*}
  {\Big| \hat{a}_2 \ket{\phi} \Big|}^2 =
  \bra{\phi} \hat{a}_2^{\dagger} \hat{a}_2 \ket{\phi} =
  \underbrace{
    \bra{\phi} \hat{a}_2 \hat{a}_2^{\dagger} \ket{\phi}
  }_{0} - \bra{\phi} \ket{\phi} < 0
\end{equation*}
where in the last steps was used equation~\eqref{eq:a_commutator}
Negative norm entails a violation of unitary which
leads to the wrong conclusion that every HD-theory is intrinsically sick.

\subsubsection{Degenerate frequencies: $\omega_1 = \omega_2$}
If frequencies $\omega_1$ and $\omega_2$ are the same, the previous
transformation can not be performed due to the vanishing denominator.
Nevertheless a solution can be obtain following the procedure described
in~\cite{PU50, Bolonek06}. Let $\psi$ be written as

\begin{equation*}
  \psi(x, v) = e^{-i\omega^2xv}\phi(x,v)
\end{equation*}

So $\hat{H}$ acts on such $\psi$ as

\begin{equation*}
  \hat{H} \psi = e^{-i\omega^2xv}
  \left[\frac{\hat{p}_v^2}{2} + v\hat{p}_x - \omega^2x\hat{p}_v \right] \phi
  \quad \Rightarrow \quad
  \hat{H}_{\phi} :=
  \frac{\hat{p}_v^2}{2} + v\hat{p}_x - \omega^2x\hat{p}_v
\end{equation*}

Now, on $\hat{H}_{\phi}$ we can apply a scale transformation and then a unitary
transformation\footnote{
  During the calculation of $\hat{H}'_{\phi}$ was useful swapping
  two non-commuting operators (e.g. $v$ and $e^{\lambda \hat{p}_v}$)
  so that their commutator appears. This identity was used:
  $\left[e^{\lambda \, \hat{A}}, \, \hat{B}\right] =
  \lambda \, e^{\lambda \, \hat{A}} \left[\hat{A} , \, \hat{B} \right]$
}.

\begin{equation*}
  \text{scale} \
  \begin{cases}
    &x         \rightarrow x/\omega \\
    &\hat{p}_x \rightarrow \omega\hat{p}_x
  \end{cases}
  \qquad \text{unitary} \;
  \U = \exp\left\{ - \frac{\hat{p}_x \hat{p}_v}{4}\right\}
\end{equation*}
to get

\begin{equation*}
  \hat{H}'_{\phi} = \, \U^{\dagger}
  \hat{H}_{\phi} \left(x/\omega,\ v,\ \omega \hat{p}_x,\ \hat{p}_v\right) \ \U
  \quad \text{with} \quad
  \phi'(x, v) = \U \phi\left(x/\omega, v\right)
\end{equation*}
so that

\begin{equation} \label{eq:ham_phi_PU}
  \hat{H}'_{\phi} = \frac{\hat{p}^2_x \, + \, \hat{p}^2_{v}}{4}
  + \omega \left(v \hat{p}_x - x \hat{p}_v \right)
\end{equation}

Hamiltonian~\eqref{eq:ham_phi_PU} can be interpreted as the Hamiltonian of a
free particle plus the term $\omega \left(v \hat{p}_x - x \hat{p}_v \right)$
which resemble the orbital angular momentum in the $(\omega x, \, v)$ plane.
Going in polar coordinates and reverting back to the original $\psi$, we obtain

\begin{align} \label{eq:sol_ham_degen_PU}
  \psi_{l, \, k}(x, v \,; t) \ &\propto \
  e^{-i\omega^2xv}
  \exp\left\{
    \frac{i}{4\omega^2} \frac{\partial^2}{\partial x \partial v}
  \right\} \cdot \nonumber \\
  &\cdot \left[
    J_l \left(k\sqrt{v^2 + \omega^2 x^2}\right)
    {\left( \frac{wx - iv}{wx +iv} \right)}^{l/2}
  \right]
  e^{-it (l\omega + k^2/4)}
\end{align}

Wave functions~\eqref{eq:sol_ham_degen_PU} are not normalizable hence the
spectrum is continuous. Moreover each energy level is infinitely degenerate:
$\psi_{l+1, \, k}$ and $\psi_{l, \, \sqrt{k^2 + 4 \omega}}$ have same
eigenvalue.