%-----------------------------------------------------------------------------%
%                                                                             %
%    K A P I T E L   3                                                      %
%                                                                             %
%-----------------------------------------------------------------------------%

\chapter{Contact Stability Constrained DDP}\label{c3}
This chapter presents a generic method for integrating contact stability constraints into DDP-like solvers. The key idea is to define inequality constraints for unilaterality, friction and the \gls{CoP} of each contact surface with the goal of generating inherently balanced motions.

\section{The Idea}\label{sec:StabilityIdea}
Stability of the contacts is an essential objective of motion planning since prevents the robot from sliding and falling down. In \cref{sec:TheoryStability} we have explored two different criteria for ensuring contact stability for dynamic systems, namely the \gls{ZMP} and \gls{CoP}. As outlined, the application of the \gls{ZMP} is limited due to the assumptions of sufficiently high friction and the existence of one planar contact surface. Since we want to provide a \textit{generic} method that can also be used for e.g. walking up stairs, these simplifying assumptions do not hold anymore.

Consequently, we decide to model a 6D surface contact, as introduced in \cref{eqn:EoMLeggedRobotSurfaceContact} with dedicated constraints for (i) unilaterality of the contact forces (ii) Coulomb friction on the resultant force, and (iii) \gls{CoP} inside the support area. For the sake of simplicity, we model a rectangular contact area. Nevertheless, this concept can be extended  to arbitrary feet designs. This approach can be compared to the concept of contact wrench cone \cite{caron2015stability}, without additionally enforcing the yaw torque constraint. These inequality constraints for surface contacts can compactly be summarized as
\begin{subequations}\label{eqn:contractWrenchConeReduced}
\begin{align}
f_i^z &> 0 \label{subeqn:stabilityUnilaterality},\\
\mid f_i^x\mid &\leq \mu f_i^z \label{subeqn:stabilityFrictionX},\\
\mid f_i^y\mid &\leq \mu f_i^z \label{subeqn:stabilityFrictionY},\\
\mid X\mid & \geq C_x \label{subeqn:stabilityCoPPitch},\\
\mid Y\mid & \geq C_y \label{subeqn:stabilityCoPRoll}.
\end{align}
\end{subequations}
%Original CoP constraints from Caron paper for horizontal floor
%\mid \tau_i^x\mid & \leq Yf_i^z \label{subeqn:stabilityCoPPitch},\\
%\mid \tau_i^y\mid & \leq Xf_i^z \label{subeqn:stabilityCoPRoll}.

Let us now detail each line of the approach.
The first inequality \cref{subeqn:stabilityUnilaterality} accounts for the unilaterality of the contact force. By nature, contact forces always have to be positive since the robot can only \textit{push} from the ground, not \textit{pull} to the ground (\cref{img:simple_contact}).
Inequality \cref{subeqn:stabilityFrictionX,subeqn:stabilityFrictionY} corresponds to the Coulomb friction, where $\mu$ denotes the static coefficient of friction. From a modeling perspective, this can be interpreted via the concept of spatial friction cones \cite{kao2016contact}. If, and only if the distributed contact forces lie inside their respective friction cones, these constraints are satisfied.
Finally, inequality \cref{subeqn:stabilityCoPPitch,subeqn:stabilityCoPRoll} constrain the \gls{CoP} to lie inside the rectangular contact area of each foot (see \cref{img:contact_surface}). $C_x$ and $C_y$ denote the x and y position of $\bp_{CoP}$, respectively. These \gls{CoP} constraints prevent the robot from tilting around the edges of the rectangular surface contact. In particular, \cref{subeqn:stabilityCoPPitch} corresponds to a constraint of tilting around the pitch axis and \cref{subeqn:stabilityCoPRoll} prevents tilting around the roll axis.

Both, the unilaterality of the contact forces and the friction cone constraints, are already implemented inside Crocoddyl. However, the central component, bounding the \gls{CoP} to lie inside the support area of each contact foot, is missing. Therefore, the rest of this chapter deals with the derivation of a set of implementable \gls{CoP} constraints and describes the integration of these constraints as a cost function into the Crocoddyl framework.
\begin{figure}[t]
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=.95\linewidth]{img/simple_contact}
		\caption{}
		\label{img:simple_contact}
	\end{subfigure}%
	\begin{subfigure}{.5\textwidth}
		\centering
		\includegraphics[width=.77\linewidth]{img/contact_surface}
		\caption{}
		\label{img:contact_surface}
	\end{subfigure}
	\caption[Simplified contact situation and CoP notation]{(a) Visualization of acting forces on a simple rigid body and (b) notation used for the \gls{CoP} definition in the contact surface plane \cite{caron2015stability}.}
	\label{fig:natural2robot}
\end{figure}

\section{Center of Pressure (CoP) Constraints}\label{sec:StabilityCoP}
In this section we will derive a universal set of implementable constraints that bound the \gls{CoP} to lie inside the rectangular contact area of each foot.

\subsection{CoP Stability Conditions}
Recapitulate the constraints from inequality \crefrange{subeqn:stabilityCoPPitch}{subeqn:stabilityCoPRoll}. Instead of using the absolute value of $X$ and $Y$, one can also formulate the constraints as
\begin{align}
\begin{split}
-X &\leq C_x \leq X,\\
-Y &\leq C_y \leq Y.
\end{split}
\end{align}
Based on this formulation it becomes evident that the \gls{CoP} is constrained to lie inside the foot geometry visualized in \cref{img:contact_surface}. In fact, these conditions can be represented via four single inequality equations as
\begin{align}\label{eqn:CoPInequalities}
\begin{split}
X + C_x \geq 0, \\
X - C_x \geq 0, \\
Y + C_y \geq 0, \\
Y - C_y \geq 0. \\
\end{split}
\end{align}
These four inequality equations will be used in the following to formulate the \gls{CoP} constraints.

\subsection{CoP Computation}
Our goal is to determine explicit expressions for $C_x$ and $C_y$ for arbitrary floor orientations, including inclined ground. To this end, consider the computation routine for the \gls{CoP} from \cref{eqn:CoPComputation}
\begin{equation*}
\bp_{CoP}=\dfrac{\bn\times\myM{\btau_O^c}}{\bfun^c\cdot\bn}.
\end{equation*}
For arbitrary orientations of the contact normal vector $\bn$, we obtain
\begin{equation}\label{eqn:CoPComputationDetailed}
\bp_{CoP}=\dfrac{\bn\times\myM{\btau_O^c}}{\bfun^c\cdot\bn} = \dfrac{\begin{bmatrix} n_x \\ n_y \\ n_z \end{bmatrix} \times \begin{bmatrix} t_x \\ t_y \\ t_z \end{bmatrix}}{\begin{bmatrix} f_x \\ f_y \\ f_z \end{bmatrix} \cdot \begin{bmatrix} n_x \\ n_y \\ n_z \end{bmatrix}} =
\begin{bmatrix} n_yt_z - n_zt_y \\ n_zt_x-n_xt_z \\ n_xt_y-n_yt_x \end{bmatrix}\cdot \dfrac{1}{f_xn_x+f_yn_y+f_zn_y},
\end{equation}
and solve for the desired position $C_x$ and $C_y$ of the \gls{CoP} as
\begin{subequations}
\begin{align}
C_x&=\dfrac{n_yt_z - n_zt_y}{f_xn_x+f_yn_y+f_zn_y}, \label{subeqn:Cx}\\
C_y&=\dfrac{n_zt_x-n_xt_z}{f_xn_x+f_yn_y+f_zn_y} \label{subeqn:Cy}.
\end{align}
\end{subequations}

\subsection{CoP Inequality Constraints}
Now that we have found explicit expressions for computing the \gls{CoP} (\crefrange{subeqn:Cx}{subeqn:Cy}), we can insert them into \cref{eqn:CoPInequalities}, which gives a set of four \gls{CoP} constraints as
\begin{align}\label{eqn:CoPInequalityEqs}
\begin{split}
X + \dfrac{n_yt_z - n_zt_y}{f_xn_x+f_yn_y+f_zn_y} \geq 0, \\
X - \dfrac{n_yt_z - n_zt_y}{f_xn_x+f_yn_y+f_zn_y} \geq 0, \\
Y + \dfrac{n_zt_x-n_xt_z}{f_xn_x+f_yn_y+f_zn_y} \geq 0, \\
Y - \dfrac{n_zt_x-n_xt_z}{f_xn_x+f_yn_y+f_zn_y} \geq 0. \\
\end{split}
\end{align}
These conditions can be written in matrix form as:
\begin{equation}\label{eqn:CoPInequalityMatrix}
\begin{bmatrix}
Xn_0 & Xn_1 & Xn_2 & 0 & -n_2 & n_1 \\
Xn_0 & Xn_1 & Xn_2 & 0 & n_2 & -n_1 \\
Yn_0 & Yn_1 & Yn_2 & n_2 & 0 & -n_0 \\
Yn_0 & Yn_1 & Yn_2 & -n_2 & 0 & n_0 \\ \end{bmatrix}
\begin{bmatrix} f^x \\ f^y \\ f^z \\ \tau^x \\ \tau^y \\ \tau^z \end{bmatrix} \geq
\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix},
\end{equation}
and finally yield an implementable set of inequality equations for constraining the \gls{CoP} to lie inside the rectangular contact area of each foot.

\section{Integration Into the Crocoddyl Framework}\label{sec:StabilityIntegration}
This section presents the integration of the derived CoP inequality constraints from \cref{eqn:CoPInequalityMatrix} into the Crocoddyl framework.

\subsection{Inequality Constraints by Penalization}
In \cref{sec:TheoryConstrainedDDP} we have discussed possible ways of incorporating inequality constraints into DDP-like solvers. Crocoddyl handles inequality constraints, such as joint limits or friction cone, via penalization.
In numerical optimization, the goal is to minimize a given cost function. In Crocoddyl, an \textit{action model} combines dynamics and cost model for each knot of the discretized \gls{OC} problem from \cref{eqn:totalCost}. The cost function for an action model at knot $n$ can be written as:
\begin{equation}\label{eqn:costSum}
l_n=\sum_{c=1}^{C}\alpha_c\Phi_c(\bq,\dot{\bq}, \btau),
\end{equation}
where $C$ different costs $\Phi_c$ are weighted by a respective coefficient $\alpha_c\in R$. The goal of the following two parts is to demonstrate how the inequality \cref{eqn:CoPInequalityMatrix} is implemented inside a novel cost function into the framework.

\subsection{Computation of Residual and Cost}
%\subsection{Computation of the Residual}
In numerical analysis, the term \textit{residual} corresponds to the error of a result \cite{shewchuk1994introduction}. For the sake of compactness, we abbreviate \cref{eqn:CoPInequalityMatrix} as
\begin{equation} \label{eqn:costPositive}
\myM{A} \myM{w} \geq \myM{0},
\end{equation}
where $\myM{A}$ corresponds to a matrix of \gls{CoP} inequality constraints and $\myM{w}$ is the contact wrench acting on the according foot. The residual $\myM{r}\in \myM{R}_{4\times 1}$ of the cost is retrieved by a simple matrix-vector multiplication:
\begin{equation}\label{eqn:costResidual}
\myM{r} = \myM{A} \myM{w}.
\end{equation}

%\subsection{Computation of the Cost}
The residual vector depicted in \cref{eqn:costResidual} typically contains non-zero numbers. The resulting scalar \gls{CoP} cost value $\Phi_{CoP}$ is computed via a bounded quadratic activation as
\begin{equation}\label{eqn:CoPCostComputation}
\Phi_{CoP}=
\begin{cases}
\quad\dfrac{1}{2}\myM{r}^T\myM{r} &\mid \text{lb} > \myM{r} > \text{ub} \\[10pt]
\quad 0 &\mid \text{lb} \leq \myM{r} \leq \text{ub}.
\end{cases}
\end{equation}

In order to account for the positiveness of $\myM{r}$ (see \cref{eqn:costPositive}), the bounds are set to $\text{lb}=\myM{0}$ and $\text{ub}=\infty$, respectively. Finally, this bounded quadratic activation of the residual vector has the following implications:
\begin{itemize}
\item The \gls{CoP} cost is zero, whenever $\bp_{CoP}$ lies inside or on the border of the foot area spanned by $X$ and $Y$.
\item The \gls{CoP} cost increases in a quadratic manner, when $\bp_{CoP}$ exceeds the foot area spanned by $X$ and $Y$.
\end{itemize}
Besides this formulation of the cost function also other designs are conceivable for the future. For example, the Euclidean distance from the CoP to the coordinate origin could be considered when computing the residual (see  \cref{eqn:CoPCostComputation}).

\subsection{Basic Usage and Contributions}
%\subsection{Basic Usage of the CoP Cost}
The cost function is implemented in C++, but can be accessed via Python bindings for versatile and fast prototyping. In the following, a basic example is provided to demonstrate the interface of the \gls{CoP} cost function to the interested reader.
\begin{verbatim}
# 1. Creating the cost model container
costModel = crocoddyl.CostModelSum(state, actuation.nu)
# 2. Defining the CoP cost
footGeometry = np.array([0.2, 0.08]) # dim [m] of the foot area
CoPCost = crocoddyl.CostModelContactCoPPosition(state,
crocoddyl.FrameCoPSupport(footId, footGeometry), actuation.nu)
# 3. Adding the CoP cost term with assigned weight to the cost model
costModel.addCost("LF_CoPCost", CoPCost, 1e3)
\end{verbatim}

%\subsection{List of Contributions}
The contributions of this thesis to the open-source framework Crocoddyl are summarized in two main pull requests. The first one, \href{https://github.com/loco-3d/crocoddyl/pull/792}{\#792} contains the basic formulation of the \gls{CoP} cost function for contact dynamics action models (see \cref{app:ContactCoP}). With \href{https://github.com/loco-3d/crocoddyl/pull/830}{\#830}, an additional version of the cost function is implemented for impulse dynamics action models (see \cref{app:ImpulseCoP}).
A functional unit test that checks the cost against numerical differentiation as well as accessible python bindings can be found in the according directory of \cite{crocoddylweb}.

%\subsection{Backup: Friction Cone constraints}
%\begin{align}
%\begin{split}
%\mid\mid f^x\mid\mid &\leq \mu f^z \\
%\mid\mid f^y\mid\mid &\leq \mu f^z \\
%f^z &> 0
%\end{split}
%\end{align}
%For the case of four edges of the linear approximation of the friction cone, the equations become:
%\begin{equation}
%\begin{bmatrix} 1 & 0 & -\mu \\
%-1 & 0 & -\mu \\
%0 & 1 & -\mu \\
%0 & -1 & -\mu \\
%0 & 0 & -\mu \\ \end{bmatrix} \cdot
%\begin{bmatrix} f^x \\ f^y \\ f^z \end{bmatrix} \leq
%\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}
%\end{equation}

%\subsection{CoP Constraints: Horizontal Floor}
%For the special case of horizontal floor, with the according normal vector $\myM{n}=[0,0,1]$, the \gls{CoP} can be computed with the help of \cref{eqn:CoPComputationDetailed} to
%\begin{equation}
%\myM{p}_{CoP} = \begin{bmatrix} -t_y/f_z \\ t_x/f_z \\ 0 \end{bmatrix},
%\end{equation}
%which in turn can be represented by four inequality conditions:
%\begin{align}
%\begin{split}
%X-\dfrac{\tau_y}{f_z} &\geq 0, \\
%X+\dfrac{\tau_y}{f_z} &\geq 0, \\
%Y+\dfrac{\tau_x}{f_z} &\geq 0, \\
%Y-\dfrac{\tau_x}{f_z} &\geq 0. \\
%\end{split}
%\end{align}
%These conditions can be transformed into matrix form for the purpose of implementation as
%\begin{equation}
%\begin{bmatrix}
%0 & 0 & X & 0 & -1 & 0 \\
%0 & 0 & X & 0 & 1 & 0 \\
%0 & 0 & Y & 1 & 0 & 0 \\
%0 & 0 & Y & -1 & 0 & 0 \end{bmatrix} \cdot
%\begin{bmatrix} f^x \\ f^y \\ f^z \\ \tau^x \\ \tau^y \\ \tau^z \end{bmatrix} \geq
%\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}.
%\end{equation}


\section{Task E}
\label{sec:task-e}

The given boundary value problem has been solved using Matlab script
\texttt{plate\_with\_hole.m} (see section \ref{app:matlab-code}).
Figure \ref{fig:bvp} shows total reaction force plotted against
displacement of the top nodes and the shape of the plate before
and after the deformation.
\begin{figure}[th]
  \centering
  % Reaction force
  \begin{subfigure}[t]{\textwidth}
    \begin{tikzpicture}
      \begin{axis}[
        width = 0.95\textwidth,
        height=\axisdefaultheight,
        minor tick num=1,
        grid=both,
        xlabel = {\(u_{x}\), [mm]},
        ylabel = {Reaction force, [N]},
        xmin=0, xmax=30
        ]
        \addplot table[skip first n=1] {data/force_displacement.dat};
      \end{axis}
    \end{tikzpicture}
    \caption{}
  \end{subfigure}

  % Geometry
  \begin{subfigure}[t]{\textwidth}
    \begin{tikzpicture}
      \begin{axis}[
        width = 0.95\textwidth,
        try min ticks=6,
        enlargelimits=false,
        axis equal image,
        axis on top,
        xlabel={\(x\), [mm]},
        ylabel={\(y\), [mm]}
        ]
        \addplot graphics[xmin=0,xmax=50,ymin=0,ymax=20] {mesh};
      \end{axis}
    \end{tikzpicture}
    \caption{}
  \end{subfigure}
  \caption{(a) Reaction force versus displacement and (b) deformed mesh.}
  \label{fig:bvp}
\end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../main"
%%% End:


\documentclass[11pt]{article}
\newcommand\tab[1][1cm]{\hspace*{#1}}
\usepackage{graphicx}
\graphicspath{ {C:/Users/yedkk/Desktop/CS465/hw4} }
\begin{document}
\section{Homework 9}
Name: Kangdong Yuan

\subsection{problem1}
a).I did not work in a group.
\\b).I did not consult without anyone my group members
\\c).I did not consult any non-class materials.

\subsection{problem2}
We prove by contradiction.\\
Given $e^*$ is the heaviest edge in some cycle C of undirected graph G.\\
We assume the claim is Flase, so T is a minimal spanning forest and $e^* \in T$. Let $S, \ V-S$ be the two connected components in $T \setminus \{e\}$. Since C is a cycle, there are two edges in C that cut $S, \ V-S$. We define another edge is r. The $e^*$ has more cost than edge r, which is $w(e)>w(r)$. So, the new minimal spanning forest with edge r cost less than minimal spanning forest with edge $e^*$. It is a contradiction to the minimal cost principle of minimal spanning forest.\\
So, $e^*$ cannot in minimal spanning forest, which cannot appear in any MST of G.

\subsection{problem3}
a). $f_a>f_b \ and \ f_a>f_c$, so $f_a=10, f_b=5, f_c=5$\\
b). This encoding is not possible, because the code for $a$ is $(0)$, is a prefix of the code for $c$ is $(00)$. \\
c). This encoding is not optimal, because the $\{0,10,11\}$ cost less space. And, the Huffman tree of this encoding is not complete, which is not optimal.

\subsection{problem4}
we can construct Huffman tree to find prefix-free encoding of minimal total cost.
As a common convention, bit '0' represents following the left child and bit '1' represents following the right child.\\
\\
First, using a priority queue $Q$ to store all the words as key and $f_i*c_i$ as value. In Q, lowest value is given highest priority. \\
\\
1. Create a leaf node for each symbol and add it to the	Q.\\
2. While there is more than one node in the queue:\\
\tab Remove the two nodes from Q (lowest $f_i*c_i$)\\
\tab Create a new internal node with these two nodes as children and with value ($f_i*c_i$) equal to the sum of the two nodes' value ($f_i*c_i$).\\
\tab Add the new node to the queue.\\
3. The remaining node is the root node and the tree is complete.\\

Then the prefix-free encoding of each word is path of each node. for example\\
\includegraphics[scale=0.7]{huff}\\
Time complexity: $O(nlogn)$, because each iteration requires $O(logn)$ time to determine the lowest frequencies and insert the new word in priority queue. There are $O(n)$ iterations.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
%\usepackage{color}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathabx}
\usepackage{commath}

\geometry{textwidth=6.5in, textheight=9.0in,
    marginparsep=7pt, marginparwidth=.6in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.08in}

\newcommand{\red}[1]{\textcolor{red}{#1}}
\newcommand{\blue}[1]{\textcolor{blue}{#1}}
\newcommand*{\annot}[1]{\tag*{\footnotesize{\textcolor{gray}{#1}}}}

\let\Re\undefined
\DeclareMathOperator{\Re}{Re}
\let\Im\undefined
\DeclareMathOperator{\Im}{Im}

\title{Color Gradients Notes}
\author{Josh Meyers}
\date{October 2015}

\begin{document}

\section{Introduction}

The goal is to create a \textsc{GalSim} ChromaticObject with realistic color gradients using high
resolution multiband images (most likely from HST) as input.  This is done by modeling the
preconvolution chromatic surface brightness profile $f(\vec{x}, \lambda)$ as a sum of two or more
separable chromatic surface brightness profiles, each with a particular asserted SED.  I.e.,

\begin{equation}
    \label{eqn:sbprof}
    f(\vec{x}, \lambda) = \sum_j S_j(\lambda) a_j(\vec{x}),
\end{equation}

where $S_j(\lambda)$ is the $j$th SED asserted as part of the decomposition, and $a_j(\vec{x})$ is
the spatial component of the $j$th separable chromatic profile.

The required set of inputs is:

\begin{itemize}

\item Two or more HST images of the same galaxy in different filters, $I_i(\vec{x})$, where $i$
labels the different filters.

\item The 2D noise covariance function for each image, $\xi_i(\Delta\vec{x})$.  If the noise is
uncorrelated, then this could be simply the noise variance ($\xi_i(\Delta\vec{x}=0) = \sigma^2_i$, $\xi_i(\Delta\vec{x}\ne0)=0$).

\item The chromatic HST PSF, $\Pi(\vec{x}, \lambda)$.

\item The HST throughput for each image filter, $T_i(\lambda)$.

\item Two or more SEDs, $S_j(\lambda)$, to use in the decomposition in Equation \ref{eqn:sbprof}.

\end{itemize}

The model for the observed images is:

\begin{align}
    I_i(\vec{x})
    &= \int T_i(\lambda) \left[\Pi(\vec{x}, \lambda) \Asterisk f(\vec{x}, \lambda)\right] \dif{\lambda} + \eta_i(\vec{x}) \\
    &= \int T_i(\lambda) \sum_j S_j(\lambda) \left[\Pi(\vec{x}, \lambda) \Asterisk a_j(\vec{x})\right] \dif{\lambda} + \eta_i(\vec{x})
\end{align}

where $\eta_i(\vec{x})$ indicates (potentially spatially correlated) Gaussian noise in image $i$ and
the $\Asterisk$ symbol indicates (spatial) convolution.  The noise $\eta_i(\vec{x})$ is related to
the noise covariance function via $\langle\eta_i(\vec{x}_l) \eta_i(\vec{x}_m)\rangle =
\xi_i(\vec{x}_l - \vec{x}_m)$, where angle brackets indicate averaging over realizations of the
noise.  Note that we also assume that $\xi_i$ is even: $\xi_i(\Delta\vec{x}) =
\xi_i(-\Delta\vec{x})$.

The convolution is easier to work with in Fourier space where it becomes a mode-by-mode product.
Indicating the Fourier transform of the real-space quantity $g(\vec{x})$ with $\tilde{g}(\vec{k})$,
the model in Fourier space is

\begin{align}
    \tilde{I}_i(\vec{k})
    &= \int T_i(\lambda) \sum_j S_j(\lambda) \tilde{\Pi}(\vec{k}, \lambda) \tilde{a}_j(\vec{k}) \dif{\lambda} + \tilde{\eta}(\vec{k}) \\
    &= \sum_j \left[\int T_i(\lambda) S_j(\lambda) \tilde{\Pi}(\vec{k}, \lambda) \dif{\lambda}\right] \tilde{a}_j(\vec{k}) + \tilde{\eta}(\vec{k}) \\
    \label{eqn:solveme}
    &=  \sum_j \tilde{\Pi}^\mathrm{eff}_{ij}(\vec{k}) \tilde{a}_j(\vec{k}) + \tilde{\eta}(\vec{k}),
\end{align}

where the effective PSF for the $i$th filter and $j$th SED is

\begin{equation}
  \tilde{\Pi}^\mathrm{eff}_{ij}(\vec{k}) = \int T_i(\lambda) S_j(\lambda) \tilde{\Pi}(\vec{k}, \lambda) \dif{\lambda}.
\end{equation}

The crux of the problem is to solve for the (complex-valued) $\tilde{a}_j(\vec{k})$ and to propagate
the statistics of the noise.

\section{Solving for $\tilde{a}_j(\vec{k})$}

There are several possible ways to estimate the required $\tilde{a}_j(\vec{k})$.

\subsection{No noise}

We'll look first at the case where one ignores noise entirely ($\eta(\vec{x}) =
\tilde{\eta}(\vec{k})= 0$).

If the number of asserted SEDs $N_j$ is larger than the number of input images $N_i$, then the
system of equations represented by Equation \ref{eqn:solveme} is underdetermined.  While in
principle this could still be solvable using some prior constraints, for now, we'll just ignore this
possibility.

If $N_j = N_i$, then we can exactly solve Equation \ref{eqn:solveme} using matrix inversion for each
Fourier mode $\vec{k}$:

\begin{equation}
  \tilde{a}_j(\vec{k}) = \sum_i [(\tilde{\Pi}^\mathrm{eff}(\vec{k}))^{-1}]_{ij} \tilde{I}_i(\vec{k}).
\end{equation}

If there are more images than spectra ($N_i > N_j$), then the system of equations is overdetermined
and no longer has an exact solution.  Instead, there is a (usually) unique ``least-squares''
solution for each $\vec{k}$ obtained by minimizing

\begin{equation}
  \label{eqn:lstsq}
  \sum_i\left|\tilde{I}_i(\vec{k}) - \sum_j \tilde{\Pi}^\mathrm{eff}_{ij}(\vec{k}) \tilde{a}_j(\vec{k})\right|^2.
\end{equation}

\subsection{Uncorrelated noise}

Stationary, uncorrelated Gaussian pixel noise in the $i$th image is completely described by its
variance $\sigma^2_i$.  In Fourier space, the variance of each mode is independent of $\vec{k}$ and
proportional to $\sigma^2_i$, where the constant of proportionality depends on the particular
Fourier conventions employed.  We can therefore write down a likelihood for $\tilde{a}_j(\vec{k})$
as

\begin{equation}
    \label{eqn:like}
    \chi^2(\vec{k}) = -2 \log \mathcal{L}(\vec{k}) = \sum_i\frac{1}{\sigma_i^2}\left|\tilde{I}_i(\vec{k}) - \sum_j \tilde{\Pi}^\mathrm{eff}_{ij}(\vec{k}) \tilde{a}_j(\vec{k})\right|^2.
\end{equation}

This is a \textit{weighted} least-squares problem.  Note that the weights only matter for
determining the Fourier coefficients $\tilde{a}_j(\vec{k})$ if the number of input images is greater
than the number of asserted spectra.  When these quantities are equal, it is always possible to find
$\tilde{a}_j(\vec{k})$ such that $\tilde{I}_i(\vec{k}) = \sum_j
\tilde{\Pi}^\mathrm{eff}_{ij}(\vec{k}) \tilde{a}_j(\vec{k})$.  The weights do still matter in this
case if we're interested in propagating the noise, however.

\subsection{Correlated noise}

If the noise covariance function for a particular image $\xi_i(\vec{\Delta x})$ is non-zero away
from the origin, then the variance of different Fourier modes is not constant, but proportional to
the noise power spectrum, which is the Fourier transform of the noise covariance function:

\begin{equation}
  P_i(\vec{k}) = \int \xi_i(\Delta \vec{x}) e^{-2 \pi i \vec{k}\cdot\Delta\vec{x}}.
\end{equation}

Due to assumed translation invariance of the noise (though note that we do not assume isotropy), the
covariance of different Fourier modes vanishes.  More explicitly, if the noise in image $i$ and mode
$\vec{k_l}$ is $\tilde\eta_i(\vec{k_l})$, then

\begin{equation}
  \langle\tilde\eta_i^*(\vec{k_l})\tilde\eta_i(\vec{k_m})\rangle = \delta(\vec{k_l} - \vec{k_m})P_i(\vec{k_l}).
\end{equation}

Once the noise power spectrum has been computed, therefore, the only change to the likelihood in
Equation \ref{eqn:like} is that the mode variance now depends on the particular Fourier mode in
question:

\begin{equation}
    \label{eqn:likecorr}
    -2 \log \mathcal{L}(\vec{k}) = \sum_i\frac{1}{P_i(\vec{k})}\left|\tilde{I}_i(\vec{k}) - \sum_j \tilde{\Pi}^\mathrm{eff}_{ij}(\vec{k}) \tilde{a}_j(\vec{k})\right|^2.
\end{equation}

This is essentially the same weighted least squares problem as in the uncorrelated noise case -- the
only difference being that the weights now depend on $\vec{k}$.  Written in matrix notation, the
solution for a particular $\vec{k}$ mode is:

\begin{equation}
    \tilde{a} = \left(\tilde{\Pi}^{\mathrm{eff}, \dagger} W \tilde{\Pi}^\mathrm{eff} \right)^{-1} \tilde{\Pi}^{\mathrm{eff}, \dagger} W \tilde{I}
\end{equation}

where
\begin{equation}
    W_{ij} = \delta_{ij}/\sqrt{P_i}.
\end{equation}

The covariance matrix for the elements of $\tilde{a}$ is given by
\begin{equation}
    \Sigma = \left(\tilde{\Pi}^{\mathrm{eff}, \dagger} W \tilde{\Pi}^\mathrm{eff} \right)^{-1}.
\end{equation}

\section{Propagating the noise covariance}

At this point, we have the $\tilde{a}_j(\vec{k})$ necessary to represent a chromatic surface
brightness profile in Fourier space as

\begin{equation}
  \tilde{f}(\vec{k}, \lambda) = \sum_j S_j(\lambda) \tilde{a}_j(\vec{k}).
\end{equation}

The uncertainty in each $\tilde{a}_j(\vec{k})$ is uncorrelated from one $\vec{k}$ to the next (since
we defined independent likelihoods for each $\vec{k}$), but does, in general, possess correlations
between the different SED components $j$ and $j^\prime$ for a given $\vec{k}$.  Using
$\Sigma_{jj^\prime}(\vec{k})$ to represent these covariances, we have

\begin{equation}
    \langle\delta\tilde{a}^*_j(\vec{k}_l)\delta\tilde{a}_{j^\prime}(\vec{k}_m)\rangle = \delta(\vec{k}_l - \vec{k}_m)\Sigma_{jj^\prime}(\vec{k_l}).
\end{equation}

Fortunately, $\Sigma_{jj^\prime}(\vec{k})$ is analytically computable for weighted least squares
problems.

This $\Sigma_{jj^\prime}(\vec{k})$ can then be transformed alongside the $\tilde{a}_j(\vec{k})$ to
effect shears, rotations, dilations, etc., the same way that noise is transformed in existing
\textsc{GalSim} routines.

Finally, the model for creating a (Fourier-domain) output image $\tilde{I}^\mathrm{out}(\vec{k})$,
convolving by an output chromatic PSF $\tilde{\Pi}^\mathrm{out}(\vec{k}, \lambda)$ and drawing
through an output filter with transmission $T^\mathrm{out}(\lambda)$, is

\begin{align}
    \tilde{I}^\mathrm{out}(\vec{k})
    &= \int T^\mathrm{out}(\lambda) \tilde{\Pi}^\mathrm{out}(\vec{k}, \lambda) \tilde{f}(\vec{k}, \lambda) \dif{\lambda} \\
    &= \int T^\mathrm{out}(\lambda) \tilde{\Pi}^\mathrm{out}(\vec{k}, \lambda) \sum_j S_j(\lambda) \tilde{a}_j(\vec{k}) \dif{\lambda} \\
    &= \sum_j \tilde{\Pi}_j^\mathrm{out, eff}(\vec{k}) \tilde{a}_j(\vec{k})
\end{align}

where the effective PSF for the $j$th component of the output image is

\begin{equation}
  \tilde{\Pi}_j^\mathrm{out, eff}(\vec{k}) = \int \tilde{\Pi}^\mathrm{out}(\vec{k}, \lambda) T^\mathrm{out}(\lambda) S_j(\lambda)\dif{\lambda}.
\end{equation}

Propagating the (potentially transformed) covariance spectrum $\Sigma_{jj^\prime}(\vec{k})$ into the
final output noise power spectrum $P^\mathrm{out}(\vec{k})$ then follows the normal propagation of
errors formula:

\begin{equation}
  P^\mathrm{out}(\vec{k}) = \sum_{jj^\prime} \tilde{\Pi}_j^\mathrm{out, eff, *}(\vec{k}) \Sigma_{jj^\prime}(\vec{k}) \tilde{\Pi}_{j^\prime}^\mathrm{out, eff}(\vec{k}).
\end{equation}

This power spectrum can then be used in the existing correlated noise whitening and symmetrizing
GalSim routines.

\section{Implementation notes}

\textsc{GalSim} uses the methodology developed by Bernstein \& Gruen (2013) to interpolate
discretely sampled surface brightness profiles in real and Fourier space.  Here we investigate the
impact of this interpolation on the Wiener-Khinchin theorem, which relates the real-space
autocorrelation function to the Fourier-space power spectrum.

The discrete version of the Wiener-Khinchin theorem is derived as follows.  We are interested in
the (co)-variance of the Discrete Fourier Transform amplitudes of some discrete real space samples
with discrete auto-covariance function $\xi\left[r\right]$ (we follow the convention of
Rowe++14 in using square brackets to indicate the arguments of a discretly sampled function, and
reserve parentheses to indicate arguments of continuous objects).  Note that since we're using DFTs,
$\xi$ is implicitly periodic: $\xi\left[r\right] = \xi\left[r+N\right]$.

The 1D DFT of an N-point sampled function is:

\begin{equation}
    \tilde{b}_k = \sum_{j=-N/2}^{N/2-1} b_j e^{-2 \pi i j k / N}.
\end{equation}

% with inverse transform
%
% \begin{equation}
%     b_j = \frac{1}{N}\sum_{k=-N/2}^{N/2-1} \tilde{b}_k e^{2 \pi i j k / N}.
% \end{equation}

We are interested in the quantity $\left\langle\tilde{b}^*_k \tilde{b}_{k^\prime}\right\rangle$
where the angle brackets indicate averaging over noise realizations.

\begin{align}
    \left\langle\tilde{b}^*_k \tilde{b}_{k^\prime}\right\rangle
    \annot{Sub in FT definition}
    &= \left\langle\sum_{j=-N/2}^{N/2-1} b_j e^{+2 \pi i j k / N} \sum_{j^\prime=-N/2}^{N/2-1} b_{j^\prime} e^{-2 \pi i j^\prime k^\prime / N}\right\rangle \\
    \annot{Interchange expectation and summation}
    &= \sum_{j=-N/2}^{N/2-1} \sum_{j^\prime=-N/2}^{N/2-1} \left\langle b_j b_{j^\prime} \right\rangle e^{2 \pi i (jk - j^\prime k^\prime)/N} \\
    \annot{$\xi\left[r\right]$ definition}
    &= \sum_{j=-N/2}^{N/2-1} \sum_{j^\prime=-N/2}^{N/2-1} \xi\left[j-j^\prime\right] e^{2 \pi i (jk - j^\prime k^\prime)/N} \\
    \annot{change variables $r=j^\prime-j$}
    &= \sum_{j=-N/2}^{N/2-1} \sum_{r=-N/2-j}^{N/2-1-j} \xi\left[r\right] e^{2 \pi i (jk - (r+j) k^\prime)/N} \\
    \annot{simplify using periodicty of $\xi$ and $\exp$}
    &= \sum_{j=-N/2}^{N/2-1} \sum_{r=-N/2}^{N/2-1} \xi\left[r\right] e^{-2 \pi i r k^\prime/N} e^{2 \pi i j (k-k^\prime)/N} \\
    \annot{definition of $P\left[k\right]$}
    &= P[k^\prime] \sum_{j=-N/2}^{N/2-1} e^{2 \pi i j (k-k^\prime)/N} \\
    \annot{$e^{2 \pi i j (k-k^\prime)/N}$ evenly samples unit circle unless $k-k^\prime \in N\mathbb{Z}$...}
    &= N P[k^\prime] \sum_{N=0}^\infty\delta^{k+N}_{k^\prime} \\
    \annot{...but we really only care about $k \in \left[-N/2, N/2-1 \right]$ }
    &= N P[k^\prime] \delta^{k}_{k^\prime}.
\end{align}

Now we try the same thing using the Bernstein \& Gruen continuously interpolated Fourier transform.
Recall their result:

\begin{align}
    \tilde{F}(u)
    &= \int{F(x) e^{-2 \pi i u x} \dif{x}} \\
    &\approx \tilde{K}_x(u) \sum_{k=-N/2}^{N/2-1}\tilde{b}_k K_u(u-k/N)
\end{align}

where $K_x$ is an asserted real-space interpolant, $\tilde{K}_x$ is its Fourier transform,
and $K_u$ is an asserted Fourier-space interpolant.  Note that the final equality is exact if and
only if

\begin{equation}
    K_u(v) = e^{i \pi v} \frac{\mathrm{sinc}\, N v}{\mathrm{sinc}\, v}.
\end{equation}

We are interested in the (co)-variance of Fourier amplitudes:

\begin{align}
    \left\langle \tilde{F}^*(u) \tilde{F}(u^\prime)\right\rangle
    \annot{Sub in GalSim approximate FT definition}
    &= \left\langle \tilde{K}_x^*(u) \sum_{k=-N/2}^{N/2-1}\tilde{b}_k^* K_u^*(u-k/N) \tilde{K}_x(u^\prime) \sum_{k^\prime=-N/2}^{N/2-1}\tilde{b}_{k^\prime} K_u(u^\prime-k^\prime/N)  \right\rangle \\
    \annot{Rearrange}
    &= \tilde{K}_x^*(u) \tilde{K}_x(u^\prime) \sum_{k=-N/2}^{N/2-1} \sum_{k^\prime=-N/2}^{N/2-1} K_u^*(u-k/N) K_u(u^\prime-k^\prime/N) \left\langle \tilde{b}_k^* \tilde{b}_{k^\prime} \right\rangle \\
    \annot{definition of $P\left[k\right]$}
    &= \tilde{K}_x^*(u) \tilde{K}_x(u^\prime) \sum_{k=-N/2}^{N/2-1} \sum_{k^\prime=-N/2}^{N/2-1} K_u^*(u-k/N) K_u(u^\prime-k^\prime/N) N P[k^\prime]\delta_{k^\prime}^k \\
    \annot{sum over Kronecker $\delta$}
    &= N \tilde{K}_x^*(u) \tilde{K}_x(u^\prime) \sum_{k=-N/2}^{N/2-1} K_u^*(u-k/N) K_u(u^\prime-k/N) P[k]
\end{align}

This is as far as I've been able to push (ran out of $\delta$ functions!).  Assuming there are no
further simplifications, one implication is that Fourier amplitudes of an interpolated noise image
are not uncorrelated.  Equivalently, the continuous auto-covariance function of interpolated
discretely stationary noise is not itself stationary.  This makes some intuitive sense, I think.
For example, the variance of interpolated points seems like it should always be less than the
variance of the input samples, implying that the continuously regarded noise is not stationary.

\end{document}


\chapter{Creating Spacecraft}

Epoch, State, Ballistic and Mass Properties, Input Coordinate
System,

\noindent\begin{ScriptType} \noindent
Create Spacecraft Sat1\\
Sat1.Epoch.TAIGregorian  = 01 Jan 2000\\
12:00:00.000; \\
Sat1.SMA        = 8000; \\
Sat1.ECC        = .01;\\
Sat1.INC        = 28.5;\\
Sat1.AOP        = 0;\\
Sat1.RAAN = 90;\\
Sat1.TA         = 180;\\     %  Use M for mean anomaly, and E for eccentric anomaly
Sat1.Cd         = 2.0;\\
Sat1.Cr         = 1.4;\\
Sat1.DragArea   = 1;\\
Sat1.SRPArea    = 1;\\
Sat1.DryMass    = 100;
\end{ScriptType}

\section{Spacecraft Epoch}

Alternative ways of Defining a Spacecraft's Epoch

Assuming a reference reference epoch for modified JD as MJD = JD -
2430000, the following epochs are all equivalent.

\noindent \% TAI in Modified Julian Date Format\\
 \begin{ScriptType}
Sat1.Epoch.TAIModJulian = 21545.0003703704 \end{ScriptType}

\noindent \% TAI in Gregorian Date Format\\
\begin{ScriptType} Sat1.Epoch.TAIGregorian  = 01 Jan
2000 12:00:32.0000;\end{ScriptType}

\noindent \% UTC in Modified Julian Date Format\\
  \begin{ScriptType}
 Sat1.Epoch.UTCModJulian  =
21545.00000; \end{ScriptType}

\noindent \% UTC in Gregorian Date Format\\
\begin{ScriptType} Sat1.Epoch.UTCGregorian  = 01 Jan
2000 12:00:00.000;  \end{ScriptType}

\section{Spacecraft Orbit State}

\%  Example: Creating a spacecraft using  the
Cartesian State\\
\begin{ScriptType}
Create Spacecraft Sat1;\\\
Sat1.X = 7082.960306079;\\
Sat1.Y = 7.052885901083;\\
Sat1.Z = -48.94363747128;\\
Sat1.VX = 0.05237087208446;\\
Sat1.VY = -1.069925309259;\\
Sat1.VZ = 7.424767278548;
\end{ScriptType}

\%  Example: Cartesian State: Method 2 \\
\begin{ScriptType}
Create Spacecraft Sat1;\\
Sat1.StateType = Cartesian;\\
Sat1.Element1 = 7000; \\   % X
Sat1.Element2 = 0.0;  \\  % Y
Sat1.Element3 = 0.0;  \\   % Z
Sat1.Element4 = 0.0;  \\   % VX
Sat1.Element5 = 8.0;  \\   % VY
Sat1.Element6 = 0.0;  \\   % VZ
\end{ScriptType}

\noindent \%  Example: Creating a spacecraft using  the
Keplerian Elements\\
\begin{ScriptType}
Create Spacecraft Sat1;\\
Sat1.SMA        = 8000;\\
Sat1.ECC        = .01;\\
Sat1.INC        = 28.5;\\
Sat1.AOP = 0; \\
Sat1.RAAN = 90;\\
Sat1.TA         = 180; \\    %  Use M for mean anomaly, and E for eccentric anomaly
\end{ScriptType}

\noindent \%  Keplerian Elements: Method 2\\
\begin{ScriptType}
Create Spacecraft Sat1;\\
Sat1.StateType   = Keplerian;\\
Sat1.AnomalyType = TA; \\   %  Alternatives:  MA, EA
Sat1.Element1 = 8000;   \\  % SMA
Sat1.Element2 = .01;   \\  % ECC
Sat1.Element3 = 28.5;  \\   % INC
Sat1.Element4 = 0;     \\   % RAAN
Sat1.Element5 = 90;    \\   % AOP
Sat1.Element6 = 180;   \\   % Anomaly
\end{ScriptType}


\section{Fit}%
\label{period.detailed}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fit folder}%
\label{period.folder}

\begin{figure}[h]
$$\image{0cm;0cm}{PFolder.eps}$$%
\caption{The frequency folder}%
\label{period.folder.dialog}
\end{figure}

This folder shows almost all the information regarding the current fit.
(In some rare circumstances the
\helpref{log folder}{log.detailed}
may provide more detailed information.)

{\bf Active frequencies}:
Gives the total number of active frequencies.
These are the frequencies for which in the frequency table,
the check box is selected near the frequency number.

{\bf Use weights}:
use the time string in a weighted form for the
least square fit calculation, if it has been selected.

{\bf Zero point}:
Shows the calculated zero point for the last calculated fit.

{\bf Residuals}:
Shows the residuals for the last calculated fit.

{\bf Calculations based on:} dialog item indicates which data set
will be used for the next calculation. Either the {\bf original}
or the {\bf adjusted} data.

With the four buttons:
{\bf Prev X},
{\bf Prev},
{\bf Next},
{\bf Next X}
scrolling in the frequency list is possible.
(Prev is short for previous.)
(X depends on the size of the screen,
but can also be specified with a
command line switch of {\tt -r$ <$rows$> $} when invoking the
program.)

The next view lines give information on some of the frequencies themselves.

The first column gives the {\bf number of the frequency}
with the possibility to activate or deactivate it by
selecting the check box.

The next column gives the value of the {\bf frequency} itself.
In case of a {\bf harmonic} or {\bf frequency combination}
the contents will be preceded by {\tt``=''} and may be
of one of the following formats:

frequency combination:
\myitemize{
        \item =fx+fy
        \item =nfx+mfy or =n*fx+m*fy
}

harmonic:
\myitemize{
        \item =hfx or =h*fx
}

x,y may be values from 1 to 255, but not be the number
of the frequency itself or a reference to another frequency
which is itself a combination. If one of the composing
frequencies is not active, the combination is not active either!
h may be an integer greater than 1, while m,n can be any integer number.

When entering or editing a frequency value, another feature may make life easy:
if an arbitrary number of ``+'' and ``-'' are trailing the number, then
the value of the
\helpref{alias gap value}{period.aliasgap} is added to or subtracted from
the value, as often as ``+'' or ``-'' are given.

The following two columns give the value of the {\bf amplitude}
and {\bf phase}.

The last column, which is by default not visible, tells
the current numerical value of a composite frequency or harmonic.

The next 3 buttons start a calculation:
\myitemize{
\item \button{Calculate}
takes the current settings and improves
the amplitudes and phases of all active frequencies.

\item \button{Improve all}
takes the current settings and improves
not only amplitudes and phases, but also the frequencies.

\item \button{Improve special}
gives full power to the user for calculations, as it allows to
decide for every active frequency, amplitude or phase whether it
should be a fixed or free parameter.
This option also allows the calculation of
\helpref {amplitude and/or phase variations}{period.ampvar}.
}

{\bf Import}
reads in a set of predefined frequencies.

{\bf Export}
writes out the whole table of frequencies.

{\bf Phase plot}
allows to display the selected time string as a phase plot to
a selectable frequency.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Alias gap}%
\label{period.aliasgap}
As already mentioned in the
\helpref{Fit folder}{period.folder},
a frequency may be shifted up or down by some arbitrary value by placing
``+'' or ``-'', after the frequency value itself.
This value is closely connected to the
spectral window function of the currently selected time string.
For astronomical reasons, the default is set to 1/365.
This value is a good estimate for a time string
with large gaps (of approximately a year) between
densely packed data.

The \helpfigref{alias gap dialog}{period.aliasgap.dialog},
which can be reached from the \menu{Fit} by selecting the
\menuentry{Alias gap}, allows to change this value.
\begin{figure}[h]
$$\image{0cm;0cm}{PAliasgap.eps}$$%
\caption{The ``alias gap'' dialog}%
\label{period.aliasgap.dialog}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Improve Special}%
\label{period.special}
As already mentioned in the description of the
\helpref{Fit folder}{period.folder}, {\bf Improve special} gives
full control over the calculation to make.

\note{This option should only be used with great care
and only after a stable solution has already been reached with the
\button{Calculate} or \button{Improve}.}

\note{Improve automatically
does a calculate before trying to improve the frequencies as well!
This safeguard has not been implemented for {\bf Improve Special},
as this might effect the desired result in unwanted ways.}

The \helpfigref{improve special dialog}{period.special.dialog}
basically contains 3 list boxes.
These list the frequency, amplitude, and phase for every active frequency.
For ease of recognition, in front of each value the number of the
frequency itself is written.
As for the frequency list, frequencies that are combinations or
harmonics are not listed.

\begin{figure}[h]
$$\image{0cm;0cm}{PSpecial.eps}$$%
\caption{The ``improve special'' dialog}%
\label{period.special.dialog}
\end{figure}

As an example, a list of predefined frequencies (f1, f2 and f3) is already
known, and should stay fixed. But other additional frequencies (f4 to f10)
should be improved.
Then in the list box with the frequencies all frequencies
but the first three should be selected, as well as all the amplitudes
and phases.

The three buttons in the bottom allow different calculation modes.
\myitemize{
\item \button{Calculate} gives the requested calculation
\item \button{Calculate amplitude/phase variations} opens up another dialog,
        giving access to an even more specialised calculation mode.
        See \helpref{Amplitude/phase variations}{period.ampvar}
        for details.
\item \button{Cancel} stops the calculation.
}

\note{Interpretation of the log output of the {\bf Fit} module:\\
It will output a before/after scenario with all the active Frequencies listed.
Values that have leading and trailing asterisk are free parameters to the fit.
The others are fixed. }

\note{And a final note: the {\bf zero point} is {\it always} a free parameter!}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Calculate amplitude/phase variations}%
\label{period.ampvar}

The last and most sophisticated calculation mode provided by the
{\bf fit module} is the {\bf Amplitude/Phase variation mode}.
This allows to analyze data where
amplitudes and/or phases have different values, for different subsets
of the currently selected time string.

An example for this is the observation of daily
high and low temperature averages.
Or a star that changes intrinsically its amplitude for some frequency.
Also measuring an object in two filters generally gives different amplitudes
and phases for each filter.

Why is this calculation mode necessary?
Fitting different subsets is possible
and should give the same results.

This is not fully true, as precision of the final result
gets lost. Not only because of the limited number of points, but also
because the frequency for each of these subsets will yield a slightly
different result when using {\bf improve} for calculation.
Thus the results are not fully comparable, especially when large datasets
with huge gaps (say maybe 10 years) are used.

But will {\bf calculate} or {\bf improve special} not do the magic to
some extent? True again, but again a certain frequency has to be assumed
from the start for all subsets, which will still not represent the
best fit.

But a blind eye shot with this new tool will not give necessarily the
wanted results either. Because, when the degrees of freedom are increased
for the calculations (and that is what amplitude/phase variation does!),
the numerical stability may be compromised.

So this tool should only be used with great caution and the
number of frequencies,
which are tested for amplitude and/or phase variations, should be kept
to a minimum. Other techniques should be used first to find such variations
and only {\it then} this tool should be used.

The other techniques are: calculating values for different subsets
and the more optical approach of using the
\helpref{phase plot}{period.phase} feature of the {\bf fit module}.

After the \button{Calculate amplitude/phase variations} has been pressed
in the \helpref{Improve special dialog}{period.special}, the
\helpfigref{amplitude/phase variation dialog}{period.ampvar.dialog} shows up.

\begin{figure}[h]
$$\image{0cm;0cm}{PAmpvar.eps}$$%
\caption{The ``amplitude/phase variation'' dialog}%
\label{period.ampvar.dialog}
\end{figure}

First an {\bf attribute} has to be chosen, for which its selected labels
define subsets of the time string.

Then the {\bf calculation mode} has to be selected.
Possibilities are:
\myitemize{
\item amplitude variation
\item phase variation
\item amplitude and phase variation
}

And finally the {\bf frequencies} for which amplitude/phase
variation should be done, have to be selected. The ones not selected
are assumed to have the same amplitude and phase for all points.

The \button{Calculate} starts the calculation.
As the \helpref{fit folder}{period.folder} does not offer
the possibility to show the complex results in its window,
a new dialog shows up, which tabulates the result.

The \helpref{fit folder}{period.folder} will only reflect
changes to the values, for which separate amplitude and phase variation
has not been done.

The residuals are calculated correctly and individual residuals for
the subsets can be examined with the
\helpref{Adjust selection}{timestring.adjust} tool in the
\helpref{Time string module}{timestring.folder}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Calculate message}%
\label{period.calculate}
When calculating a fit the main window will change its appearance
to the following:
\begin{figure}[h]
$$\image{0cm;0cm}{PCalculate.eps}$$%
\caption{The ``calculate status'' window}%
\label{period.calculate.message}
\end{figure}

It informs about the current state of calculations,
where the first number is the number of iterations already done.
The second number tells at what number of iterations the program will stop
iterating and ask for advice.

With the \button{Cancel} the calculation can be stopped prematurely.
In this case, the result may not be a stable solution.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Predict signal}%
\label{period.predict}
With the \menuentry{Predict} in the \menu{Fit} it is possible to
predict an amplitude at a certain time from the current fit.
The \helpfigref{predict amplitude dialog}{period.predict.dialog} only
allows to enter a {\bf time} and the \button{Calculate}
will update the display accordingly.
\begin{figure}[h]
$$\image{0cm;0cm}{PPredict.eps}$$%
\caption{The ``predict amplitude'' dialog}%
\label{period.predict.dialog}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Create artificial data}%
\label{period.artificial}
With the
\helpfigref{create artificial data dialog}{period.artificial.dialog},
which can be activated by selecting the
\menuentry {Create artificial data} from the \menu{Fit},
it is possible to create an equally spaced artificial time string.
\begin{figure}[h]
$$\image{0cm;0cm}{PArtificial.eps}$$%
\caption{The ``create artificial data'' dialog}%
\label{period.artificial.dialog}
\end{figure}

To do so, \period has to know about the {\bf start} and {\bf end time}
for the selected time span
and as well the {\bf steps} in between
defaults for this values are: first and last time of the
currently selected time string and for the step 1/(20*Fmax) to give a good
sampling for the highest frequency as well.

{\bf Leading/trailing} allows to extend the time span defined by start
and end in both directions further by a common value.

After pressing the \button{Append to file} or the \button{Create new file}
a file needs to be selected. For {\it append} the data will be appended
to the file, and with {\it create} a new file will be created, and
the old file destroyed.

To create artificial data with times from the currently selected time string,
the ``wished'' frequencies should be entered in the
\helpref{Fit folder}{period.folder} and then the
\menuentry{Recalculate residuals} in the \menu{Fit} should be used to
\helpref{recalculate the residuals}{period.recalculate}.
Then the calculated values can be exported with
\helpref{Time string export}{timestring.export}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Recalculate residuals}%
\label{period.recalculate}
With the \menuentry{Recalculate residuals} in the \menu{Fit}, it is
possible to update the residuals using the values of the current fit.
In addition, the zero point will be asked in the
\helpfigref{zero point dialog}{period.residuals.dialog}
before the calculations are done.
\begin{figure}[h]
$$\image{0cm;0cm}{PZeropoint.eps}$$%
\caption{The ``zero point'' dialog}%
\label{period.residuals.dialog}
\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Epochs}%
\label{period.epochs}
The time of maximum light closest to a certain time (epoch) according
to the current fit can be calculated by selecting the
\menuentry{Epoch} in the \menu{Fit}.

Then the \helpfigref{epoch dialog}{period.epochs.dialog} will show up,
which basically lists the table of frequencies along with the epoch.
{\bf Time of epoch} gives the time, for which the epochs should be calculated.
And {\bf Data is in intensity} allows for correct interpretation of
maximum light. By default magnitudes are assumed!

\begin{figure}[h]
$$\image{0cm;0cm}{PEpochs.eps}$$%
\caption{The ``epoch'' dialog}%
\label{period.epochs.dialog}
\end{figure}

The \button{Calculate} will update these results.
The \button{Print} will print the results and
the \button{Save} will save the results.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Import frequencies}%
\label{period.import}
To import a table of frequencies, all that is needed to do is
to press the \button{Import} in the \helpref{Fit folder}{period.folder}%
. Then a file selector dialog will show up and a file to
read in may be selected.

The table of frequencies will be erased and the information will be filled
in.

The file format is the following for {\it each} line:
\myitemize{
\item Frequency identifier: in the format: F$<$num$>$ or f$<$num$>$
\item if a bracket is the next character, then the frequency is
        assumed to be inactive
\item frequency
\item amplitude (optional)
\item phase (optional)
\item additional data ignored
}

Lines starting with ``/'', ``;'', ``\#'', ''\%''
are assumed to be comment lines.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Export frequencies}%
\label{period.export}
To export the table of frequencies, all that is needed to do is
to press the \button{Export} in the \helpref{Fit folder}{period.folder}.
Then a file selector dialog will show up and a filename to save
may be selected.

For the format of the output file please see
\helpref{Import frequencies}{period.import}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Clean all frequencies}%
\label{period.cleanfrequencies}
To clean the tabel of frequencies, select in the \menu{Fit} by selecting the
\menuentry{Clean all frequencies}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Phase plot}%
\label{period.phase}
As a last tool, the {\bf Fit module} offers the possibility of phase plotting
the selected time string. This tool has been in use for a long time
and has had its prime time in the early days of asteroseismology.
Nowadays this tool is scarcely used, even though
it can prove to be a powerful visual diagnostic tool.
It allows to view the shape of a light curve not in numbers
but visually. And this possibility can help finding a solution to some,
otherwise possibly overlooked, properties in the data.

The plot basically uses the the reminder of time of a point multiplied by a
certain frequency, which can be edited by the user,
as abscissa, and as Y-Axis most frequently residuals
(of any kind - Original or Adjusted) to plot a point.

By default, \period tries to find a frequency in the list of frequencies,
that has not been activated and chooses the first one found.
This frequency is displayed in the top part of the graph.
If none is found, then a frequency of 1.0 is chosen.

The point itself is plotted as a filled circles with a color that
corresponds to the color of one of its attributes labels.
To change the the attribute to use for to select the color,
the \menu{Color} is used.
(See \helpref{Edit name properties}{timestring.edit} in the
\helpref{Time string folder}{timestring.folder}) for how to
change the color for a label.)

With the \menu{Data} the data to be displayed on the Y-axis can be selected.

For explanation of the {\bf Color, Data and Zoom menus} please see
the documentation on \helpref{Time string graph}{timestring.graph}.

Some of the possible uses of this graph are:
\myitemize{
\item finding systematic changes of amplitudes
        in different subsets of the time string.
\item finding systematic changes of phases
        in different subsets of the time string.
\item detecting ``bad'' data, which do not follow the average
        ``light curve shape'' for whatever reason.
\item reveals the ``true'' ``light curve shape'', as the light curve
        does not have to be sinusoidal, but still periodic.
}

\note{Note: with {\bf harmonic frequencies}, that can be entered
in the frequency table, these light shapes may be ``approximated''
quite accurately and  thus be removed from the residuals.}

So, how does it work? Bringing up the
\helpfigref{Phase plot}{period.phase.window}
is as easy as pressing the \button{Phase plot} in the
\helpref{Fit folder}{period.folder}.
\begin{figure}[h]
$$\image{0cm;0cm}{PPhase.eps}$$%
\caption{The ``phase plot'' window}%
\label{period.phase.window}
\end{figure}

The \menu{Frequency} gives all the necessary possibilities that have to do
with the graph, like changing the frequency used for calculating
the phase of a point,
or to activate the {\bf binning feature}, which allows to extract the
light shape ``corresponding''to this frequency.
For closer detail please see \helpref{Binning}{period.phase.binning}.

The values from which the graph has been created, can be written out in a
similar manner as the normal
\helpref{time string export}{timestring.export}%
. The only difference is that instead of time, phase is written out.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Binning}%
\label{period.phase.binning}
As already mentioned, the ``Phase plot'' also offers the possibility of
binning, which is averaging data for discrete phase ranges
and displaying these values along with error bars.
Additionally, a spline like curve is fitted to the data resulting from this
procedure.

The size of the bin box can be changed by selecting the
\menuentry{binning spacing} in the \menu{Frequency}.
Then the \helpfigref{Binning spacing dialog}{period.phase.binning.dialog}
will open up and ask for a new value. This value should be in the
range of {\bf 0} and {\bf 1} exclusively!
\begin{figure}[h]
$$\image{0cm;0cm}{PPhaseBinning.eps}$$%
\caption{The ``binning'' dialog}%
\label{period.phase.binning.dialog}%
\end{figure}

The binned values can also be exported to a file by selecting the
\menuentry{Export binned} in the \menu{File}. This file contains
3 columns: phase, mean amplitude and sigma of mean.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection{Frequency choice}%
\label{period.phase.frequency}
If the frequency used in the graph needs to be changed, then the
\menuentry{Change Frequency} in the \menu{Frequency} should be selected.
This opens up the
\helpfigref{frequency choice dialog}{period.phase.frequency.dialog}.
\begin{figure}[h]
$$\image{0cm;0cm}{PPhaseFrequency.eps}$$%
\caption{The ``frequency choice'' dialog}%
\label{period.phase.frequency.dialog}
\end{figure}

This Dialogs contains a list box, that
lists all deactivated frequencies, as \period assumes
that activated frequencies already have been prewhitened.
Selecting one of these and pressing the \button{OK} selects
this frequency and updates the graph and binning accordingly.

If a totally different frequency needs to be selected, then there is also
the ``Choose different value'' in the list box.
When this has been selected the
\helpfigref{custom frequency dialog}{period.phase.frequency.other.dialog}
will show up, and a new frequency can be entered.
\begin{figure}[h]
$$\image{0cm;0cm}{PPhaseFrequencyother.eps}$$%
\caption{The ``custom frequency'' dialog}%
\label{period.phase.frequency.other.dialog}
\end{figure}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "period98"
%%% End:


%In this section we provide details of the three datasets that we evaluate in this paper, as well as the methodology used.
%\subsection{Dataset description}

%\subsubsection{Twitter example (Detection of natural disasters)}
The scenario we have chosen to evaluate our algorithms (in both offline evaluation and a user study) is related to finding natural disasters discussed in a collection of tweets.  We started with a corpus of approximately 1 billion tweets crawled from the Twitter streaming API during 2013 and 2014 with the following restrictions:
%the Twitter data described in~\cite{Iman2017}:
%, which was curated by four students as follows:
(1) the dataset was restricted to users located within the US, (2) non-English tweets were filtered out, (3) only the tweets related to 12 natural disasters were kept -- tweets related to other natural disasters were removed. These natural disasters are temporally, and geographically disjoint -- a storm, a hurricane,  a drought, two floods, two earthquakes, two tornadoes,  and three blizzards. Finally, (4) false positive tweets were intentionally included -- tweets mentioning natural disasters specific-keywords but are not related to a particular natural disaster. The final dataset contains 39,486 tweets with 5,075 marked as relevant tweets.

In addition to comparing our greedy algorithm to the optimal solution, we also propose to use X-Means \cite{Pelleg2000} as a baseline method for clustering. X-Means is an extension of K-Means which tries to  automatically determine the number of clusters. %Starting with only one cluster, the X-Means algorithm goes into action after each run of K-Means, making local decisions about which subset of the current centroids should split themselves in order to better fit the data.
The distance metric we have used for X-Means is defined as follows:
\begin{equation}
d(i,j) = \alpha\times\textrm{[time dist.]}+\beta\times \textrm{[location dist.]}+\gamma\times \textrm{[text cosine dist.]}
\end{equation}
\noindent where $\alpha$, $\beta$, and $\gamma$ are weights that sum to 1, set all to 1 in the off-line evaluation and set respectively to 0.1, 0.8,  and 0.1 in the user study -- values manually tuned by our four students. Finally, we used EF1 to rank clusters returned by X-Means.

%We assume having an agent monitoring tweets, a topical tweets classifier (e.g., \cite{Iman2017}), and a display showing the locations of the tweets (See Figure \ref{Fig:TwitterData}).  We used the  2.5 TB of Twitter data described in  \cite{Iman2017}, for which we restricted our analysis to the 9M tweets of January 2014.

%\begin{figure}[t]
%\begin{centering}
%\subfigure[Twitter Networks.]{\includegraphics[width=2.8cm]{imgs/twitter_example_3}\label{Fig:TwitterData}}\subfigure[Enron Networks.]{\includegraphics[width=2.8cm]{imgs/enron_net_4}\label{Fig:EnronData}}\subfigure[Reddit Networks.]{\includegraphics[width=2.8cm]{imgs/srforum3}\label{Fig:RedditData}}
%\par\end{centering}
%\caption{Layouts used for the different datasets.}
%\end{figure}


\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}	% Para caracteres en español
\usepackage{amsmath,amsthm,amsfonts,amssymb,amscd}
\usepackage{multirow,booktabs}
\usepackage[table]{xcolor}
\usepackage{fullpage}
\usepackage{lastpage}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{mathrsfs}
\usepackage{wrapfig}
\usepackage{setspace}
\usepackage{url}
\usepackage{calc}
\usepackage{multicol}
\usepackage{cancel}
\usepackage[retainorgcmds]{IEEEtrantools}
\usepackage[margin=3cm]{geometry}
\usepackage{amsmath}
\newlength{\tabcont}
\setlength{\parindent}{0.0in}
\setlength{\parskip}{0.05in}
\usepackage{empheq}
\usepackage{framed}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\colorlet{shadecolor}{orange!15}
\parindent 0in
\parskip 12pt
\geometry{margin=1in, headsep=0.25in}
\theoremstyle{definition}
\newtheorem{defn}{Definition}
\newtheorem{reg}{Rule}
\newtheorem{exer}{Exercise}
\newtheorem{note}{Note}

%\newif\ifnotes
%\newcommand{\h}[1]{\ifnotes{\textcolor{red}{#1}}\fi}
\newcommand{\h}[1]{\textcolor{red}{#1}}

\begin{document}
\setcounter{section}{8}
\title{Chapter 9 Review Notes}

\thispagestyle{empty}

\begin{center}
{\LARGE \bf Convexity of Differentiable Function Approximations}\\
\end{center}
\section{Convexity Reference}

\subsection{Definitions}

There are several definitions and properties of convexity which are useful for determining where functions are convex.

\textbf{Definition.} A function $f(x)$ is \textit{convex} on interval $I$ if $\forall x_1, x_2 \in I, \forall \in [0, 1]: f(t x_1 + (1-t)x_2) \leq tf(x_1) + (1-t)f(x_2)$.

\textbf{First Derivative} Using the first derivative this becomes: $\forall x_1, x_2 \in I: f(x_1) \geq f(x_2) + f'(x_2)*(x_1 - x_2)$. In other words, the derivative cannot decrease on $I$.

\textbf{Second Derivative} This also means that $\forall x \in I: f''(x) \geq 0$

\subsection{Compositions}

\textbf{Summation and Multiplication} Sums of convex functions are convex. Multiplication by a constant is convex, provided the constant is greater than 0.

\textbf{Composition} If $f$ and $g$ are convex, $g \circ f$ is convex on intervals where $g' \geq 0$, (ie where $g$ is monotonically increasing).

\begin{figure}[h!]
  \centering
  %\includegraphics[width=\linewidth]{figs/mlp2.png}
  %\includegraphics[height=6cm]{figs/mlp2.png}
  \includegraphics[width=\linewidth]{figs/convex_examples.png}
  \caption{Some examples of convex and concave functions. (from \protect\url{http://www.math.cmu.edu/~lohp/docs/math/mop2013/convexity-soln.pdf})}
  %\label{fig:mlp2}
\end{figure}

\section{Convexity of Integer Operations}
\subsection{Arithmetic Operations}

\textbf{Addition}.
\begin{enumerate}
\item \textbf{Function:} $g(x) = x + c$ is trivially convex, since $g'(x) = 1$. ,
\item \textbf{Composition:}  $g \circ f$ is also convex given convex $f$, since $\forall x: g'(x) \geq 1$.
\item \textbf{Binary Composition:} For addition given two convex input functions $f_1$ and $f_2$: $g(f_1(x), f_2(x)) = f_1(x) + f_2(x)$. This is still convex, since $\frac{dg}{df_1} = \frac{dg}{df_2} = 1$.
\item \textbf{Step size:} Step size does not affect convexity for additon ($Ax + b$ is an affine tranformation).
\end{enumerate}

\textbf{Subtraction}.
\begin{enumerate}
\item \textbf{Function:} $g(x) = x - c$ is both trivially convex, since $g'(x) = 1$. ,
\item \textbf{Composition:}  $g \circ f$ is also convex given convex $f$, since $\forall x: g'(x) \geq 1$.
\item \textbf{Binary Composition:} This is still convex, since $\frac{dg}{df_1} = \frac{dg}{df_2} = 1$. For subtraction $g(f_1(x), f_2(x)) = f_1(x) - f_2(x)$, $g''(f_1(x), f_2(x)) = f_1''(x) - f_2''(x)$, this will be convex where $f_1''(x) \geq f_2''(x)$. For example, for $f_1(x) = x^2$, $f_2(x) = x^3$, $g(x)$ will be convex where the 2nd derivative $2 - 6x \geq 0$, ($x \leq \frac{1}{3}$).
\item \textbf{Binary Composition with bounds:} Given a particular interval $x \in I$ in which $f_1''(x) \in [d_{1low}, d_{1high}]$ and $f_2''(x) \in [d_{2low}, d_{2high}]$, $g(x)$ will be convex if $d_{1low} - d_{2high} \geq 0$, since this means $g''(x) \geq 0$ for $x \in I$.
\item \textbf{Step size:} Does not effect single function composition, for multiple function composition $x$ becomes $\frac{x}{s}$ (for step size $s$), which may effect the regions where $g(x)$ is convex depending on the input functions.
\end{enumerate}

\textbf{Multiplication}
\begin{enumerate}
\item \textbf{Function:} $g(x) = cx$  is trivially convex, since $g''(x) = 0$. ,
\item \textbf{Composition:}  $g \circ f$ is convex for convex $f$ when $c \geq 0$, since $g'(x) = c$. For concave $f$, the inverse holds. If $f''(x)$ is bounded, this simply means the lower bound must greater than 0 if $c>0$, or the upper bound must be less than 0 if $c<0$.
\item \textbf{Binary Composition:} For multiplication given two convex input functions $f_1$ and $f_2$: $g(f_1(x), f_2(x)) = f_1(x) * f_2(x)$. This is not convex in general, counterexample $g(-x, x) = -x^2$, but can be convex or convex in regions.
\item \textbf{Binary Composition with Bounds:} \h{Bounds don't seem to help much here, it seems you need more information about the functions. Example $g(x^2, -(x-2))$ is convex for $x<\frac{2}{3}$, how do you predict that from bounds on derivatives?}

\item \textbf{Step size:} $x = \frac{x}{s}$, does not effect single function composition.
\end{enumerate}

\textbf{Division}
\begin{enumerate}
\item \textbf{Function:} $g(x) = x/c$  is trivially convex, since $g''(x) = 0$. For $g(x) = c/x$, $g''(x) = 2cx^{-3}$, this is convex for $x \in [0, \inf), c \geq 0$, and $x \in (-\inf, 0), c < 0$. (note, may need to adjust this for integer division)
\item \textbf{Composition:} For $g(x) = x/c$, $g \circ f$ is convex when $c \geq 0$, since $g'(x) = c^{-1}$.  For $g(x) = c/x$, $g'(x) = -cx^{-2}$, then $\forall c < 0: g'(x) \geq 0$, and it is convex under composition for all $f(x)<0$.
\item \textbf{Binary Composition:} Given two convex input functions $f_1$ and $f_2$: $g(f_1(x), f_2(x)) = f_1(x) / f_2(x)$. This is not convex in general, counterexample $g(x, x^2) = 1/x$, inputs are convex but output is concave, however it can still be convex or convex in regions.
\item \textbf{Binary Composition with Bounds:} \h{Similar issues to multiplication here.}
\item \textbf{Step size:} $x = \frac{x}{s}$, does not effect single function composition.
\end{enumerate}

\textbf{Exponentiation}
\begin{enumerate}
\item \textbf{Function:} $g(x) = x^c$  is convex if $c \mod 2 = 0$, since $g''(x) = (c-1)(c-2)x^{c-2} \geq 0$ if $c$ is even. If $c$ is odd, $g''(x) \geq 0$ for $x > 0$, so the function will be convex for $x>0$. ($x^1$ is the exception is convex everywhere).  For $g(x) = c^x$, it is convex for $c>0$ since $g''(x) = c^x \log^2(x)$. For $c<0$, the sign alternates so the function is convex over every interval $(x, x+2)$.

\item \textbf{Composition:} For $g(x) = x^c$, $g \circ f$ has second derivative $g''(x) = c(c-1) (f(x))^{c-2}f'(x) + cf(x)^{c-1}f''(x)'$.
\item \textbf{Binary Composition:} For given two convex input functions $f_1$ and $f_2$: $g(f_1(x), f_2(x)) = f_1(x) ^ f_2(x)$. This does not preserve convexity in general, counterexample $g(-x, x) = -x^2$, inputs are convex but output is concave.
\item \textbf{Step size:}  $x = \frac{x}{s}$, does not effect single function composition, where $g(x) = f(x)^c$, but renders analysis for $g(x) = c^{f(x)}$ invalid.
\end{enumerate}

\subsection{Bitwise Operations}

%\textbf{And}
\subsubsection*{And}
\begin{enumerate}
\item \textbf{Function:} For $g(x) = x \& c$, $g(x)$ will be convex in the following regions: let $k$ denote the number of contiguous leading bits in $c$ that share the same value, either 0 or 1. Then $g(x)$ will be convex where $x \in [x_k, x_k + 2^k - 1], x_k \mod 2^k = 0$.

{\it Proof.}

{\it Lemma 1} Only the first $k$ bits of $x$ change in the region $x \in [x_k, x_k + 2^k - 1] x_k \mod 2^k = 0$. Pf. The first $k$ bits of $x_k$ are 0 b/c $x_k \mod 2^k = 0$. Since the maximum in the region $x \in [x_k, x_k + 2^k - 1] x_k \mod 2^k = 0$ from $x_k$ is $2^k-1$, which is also represented in $k$ 1s, all changes in the region from $x_k$ only effect the first $k$ bits.

There are two cases, either the first $k$ bits are 0s or they are 1s. If they are 0s, then $g(x)$ will not change in the region $[x_k, x_k = 2^k-1]$ because from \textbf{lemma 1} only the first $k$ bits will change and the first $k$ bits are being bitwise $\&$ with $k$ 0s. Then $g(x) = g(x_k)$ in the region, and the definition of convexity applies. If the first $k$ bits are 1s, then from \textbf{lemma 1} any change in $x$ is anded with 1s, and $g(x) = g(x_k) + x - x_k$, which is linear and therefore a convex function.

Also note that for leading 1s, in some places the convexity region can be extended to include $x_k-1$: $x \in [x_k - 1, x_k + 2^k - 1] x_k \mod 2^k = 0$, while for the leading 0s, in some places the upper bound could be extended to $x_k + 2^k$. However these cases do not apply to all regions.

\item \textbf{Composition:} For $g \circ f$, the previous analysis holds when the leading bits are 0s for $f(x) \in [f(x)_k, f(x)_k + 2^k - 1] f(x)_k \mod 2^k = 0$, since $g(x)$ will still be constant in those regions. If the leading bits are 1s, then $g(x)$ is equivalent to multiplication by $1$ of $-1$, and convexity will be preserved if $f''(x) > 0$, or inverted if $f''(x) < 0$.
\item \textbf{Output bounds:} For leading 1s, bounds are limited to bounds in the convex region defined by the bitwise operation, or could remain the same if evaluating the input 2nd derivative in the region for tighter bounds is not feasible. For leading 0s, the 2nd derivative is set to 0 within the flat (convex) intervals.
\item \textbf{Step size:} If $s < 2^k$, then step size shrinks the convexity bounds to $\frac{2^k}{s}$, for $s > 2^k$, the input will jump between convex regions on changes in $x$.
\end{enumerate}

\textbf{Composition} If $f$ and $g$ are convex, $g \circ f$ is convex on intervals where $g' \geq 0$, (ie where $g$ is monotonically increasing).

\begin{figure}[h!]
  \centering
  \includegraphics[width=\linewidth]{figs/and.png}
  \caption{Convex regions for bitwise \textbf{and}}
\end{figure}

%\textbf{Or}
\subsubsection*{Or}
\begin{enumerate}
\item \textbf{Function:}  For $g(x) = x | c$, $g(x)$ will be convex in the following regions: let $k$ denote the number of contiguous leading bits in $c$ that share the same value, either 0 or 1. Then $g(x)$ will be convex where $x \in [x_k, x_k + 2^k - 1], x_k \mod 2^k = 0$.

{\it Proof.} There are two cases, either the first $k$ bits are 0s or they are 1s. If they are 1s, then $g(x)$ will not change in the region $[x_k, x_k = 2^k-1]$ because from \textbf{lemma 1} only the first $k$ bits will change and the first $k$ bits are being bitwise $|$ with $k$ 1s, setting them all to 1. Then $g(x) = g(x_k)$ in the region, and the definition of convexity applies. If the first $k$ bits are 0s, then from \textbf{lemma 1} any change in $x$ is bitwise \textbf{or}ed with 1s, and $g(x) = g(x_k) + x - x_k$, which is linear and therefore a convex function.

\item \textbf{Composition:} For $g \circ f$, the previous analysis holds when the leading bits are 1s for $f(x) \in [f(x)_k, f(x)_k + 2^k - 1] f(x)_k \mod 2^k = 0$, since $g(x)$ will still be constant in those regions. If the leading bits are 0s, then $g(x)$ is equivalent to multiplication by $1$ of $-1$, and convexity will be preserved if $c > 0$, or inverted if $c < 0$.
\item \textbf{Step size:} If $s < 2^k$, then step size shrinks the convexity bounds to $\frac{2^k}{s}$, for $s > 2^k$, the input will jump between convex regions on changes in $x$.
\item \textbf{Output bounds:} For leading 0s, bounds are limited to bounds in the convex region defined by the bitwise operation, or could remain the same if evaluating the input 2nd derivative in the region for tighter bounds is not feasible. For leading 1s, the 2nd derivative is set to 0 within the flat (convex) intervals.
\end{enumerate}

\begin{figure}[h!]
  \centering
  \includegraphics[width=\linewidth]{figs/or.png}
  \caption{Convex regions for bitwise \textbf{or}}
\end{figure}

%\textbf{Not}
\subsubsection*{Not}
\begin{enumerate}
\item \textbf{Function:} For $g(x) = ~x$, this is equivalent to $g(x) = -x - 1$, and as a linear function is convex.
\item \textbf{Composition:} For $g \circ f$, $f$ will be inverted so that concave regions become convex.
\end{enumerate}

%\textbf{Xor}
\subsubsection*{Xor}
\begin{enumerate}
\item \textbf{Function:} For $g(x) = x \oplus c$, $g(x)$ will be convex in the following regions: let $k$ denote the number of contiguous leading bits in $c$ that share the same value, either 0 or 1. Then $g(x)$ will be convex where $x \in [x_k, x_k + 2^k - 1], x_k \mod 2^k = 0$.

{\it Proof.} There are two cases, either the first $k$ bits are 0s or they are 1s. If they are 0s, then $g(x) = g(x_k) + x - x_k$  in the region $[x_k, x_k = 2^k-1]$ because from \textbf{lemma 1} only the first $k$ bits will change and the first $k$ bits are being bitwise $\oplus$ with $k$ 0s, leaving them unchanged, and the definition of convexity applies. If the first $k$ bits are 1s, then from \textbf{lemma 1} any change in $x$ is bitwise $\oplus$ed with 1s, and $g(x) = g(x_k) - x + x_k$, which is also linear and therefore a convex function.
\item \textbf{Composition:} For $g \circ f$, $f$ will be unchanged in the convex regions if there are leading 0s, preserving convexity in these regions, or inverted if there are leading 1s, making any concave parts of the function convex.
\item \textbf{Step size:} If $s < 2^k$, then step size shrinks the convexity bounds to $\frac{2^k}{s}$, for $s > 2^k$, the input will jump between convex regions on changes in $x$.
\item \textbf{Output bounds:} For leading 1s, bounds are inverted in the convex region defined by the bitwise operation. For leading 1s, bounds remain the same, or could be tightened to the range within the convex region of the xor.
\end{enumerate}

\begin{figure}[h!]
  \centering
  \includegraphics[width=\linewidth]{figs/xor.png}
  \caption{Convex regions for bitwise \textbf{xor}}
\end{figure}
%\begin{figure}
  %\centering
%\begin{subfigure}{.5\textwidth}
  %\centering
  %%\includegraphics[width=\linewidth]{figs/mlp2.png}
  %\includegraphics[height=6cm]{figs/mlp2.png}
  %\caption{Multilayer Perceptron with 2 hidden layers.}
%\end{subfigure}%
%\begin{subfigure}{.5\textwidth}
  %\centering
  %\includegraphics[height=6.5cm]{figs/mlp3.png}
  %\caption{Multilayer Perceptron with 2 hidden layers and residual connections.}
%\end{subfigure}
  %\caption{Two Layer MLPs.}
  %\label{fig:mlp2}
%\end{figure}

\end{document}


\documentclass[../thesis.tex]{subfiles}

%!TeX spellcheck = en-GB

% chktex-file 18

\begin{document}

\chapter{Results}
\label{chap:results}

For all values of the control parameter,\(B\), we observed that the
nearest neighbour distributions deviate from both the Poisson and Wigner distributions.
We propose the following hypothesis: the obtained nearest neighbour distributions
can be seen as a linear superposition of the
Poisson and Wigner distributions:
\[
  P(s) = \alpha P_P(s) + (1-\alpha) P_W(s).
\]

This hypothesis is rooted in the fact that the classical counterpart of our system
has an interplay between regular and chaotic motion clearly illustrated by the Poincaré
sections in figures~\ref{fig:ponicare-sections}~\cite{Baran1998}.
In this way we consider that for a range of energies of the classical system,
the phase space ratio of the volumes of chaotic and regular trajectories is
reflected in the the superposition of Wigner and Poisson distributions.

Thus, a system with an integrable classical counterpart will have \({\alpha=1}\),
corresponding to the Poisson distribution, while a fully chaotic
system will have \(\alpha=0\) corresponding to the Wigner distribution. For
a system which has balanced contributions to the phase space volume both from
the regular and chaotic trajectories, we expect the Poisson and Wigner distributions
to equally contribute to \(P(s)\).

For the fixed value of the non-integrability parameter of \(B=0.55\) we observe that
with the increase of energy, the phase space fills with tori up to a energy of
around \(E=600\) (in units of harmonic oscillator energy) and after that the chaotic trajectories begin to fill
the phase space. We can say that globally, for an energy range \(\Delta E \leq 600\),
the phase space is characterised by an increase of regular trajectories as the
energy increases. If we now consider the quantum counterpart of the system, we
can use \(\alpha \) as a measure of the closeness to the Poisson distribution.
As we can see in figures~\ref{fig:fit-b0.55n260-me100-120} and~\ref{fig:fit-b0.55n260-me150-180.3}
with the increase of the energy interval \(\alpha \) increases from \(0.493\) at
\(\Delta E \approx 100 \) to \(0.876\) at \(\Delta E \approx 180 \).

Thus, at least for a fixed value of $B$, we can observe a correlation between
the tori volume in phase space and \(\alpha \), the superposition coefficient.

\begin{figure}
\centering
\begin{subfigure}{0.49\textwidth}
  \includegraphics{ponicare-sections-e_30}  % chktex 36
\label{fig:ponicare-sections-e_30}
\end{subfigure}
\begin{subfigure}{0.49\textwidth}
  \includegraphics{ponicare-sections-e_120}  % chktex 36
\label{fig:ponicare-sections-e_120}
\end{subfigure}

\begin{subfigure}{0.49\textwidth}
  \includegraphics{ponicare-sections-e_240}  % chktex 36
\label{fig:ponicare-sections-e_240}
\end{subfigure}
\begin{subfigure}{0.49\textwidth}
  \includegraphics{ponicare-sections-e_400}  % chktex 36
\label{fig:ponicare-sections-e_400}
\end{subfigure}

\begin{subfigure}{0.49\textwidth}
  \includegraphics{ponicare-sections-e_600}  % chktex 36
\label{fig:ponicare-sections-e_600}
\end{subfigure}
\begin{subfigure}{0.49\textwidth}
  \includegraphics{ponicare-sections-e_1000}  % chktex 36
\label{fig:ponicare-sections-e_1000}
\end{subfigure}
\caption{Poincaré sections for \(B=0.55\) and \(E=30A (\text{MeV})\) (b), \({E=120A (\text{MeV})}\) (c),
\(E=240A (\text{MeV})\) (d), \(E=400A (\text{MeV})\) (e), \(E=600A (\text{MeV})\) (f), \({E=1000A (\text{MeV})}\) (g).}
\label{fig:ponicare-sections}  % chktex 24
\end{figure}

\begin{figure}
\centering
\begin{subfigure}{\textwidth}
  \includegraphics{"B0.55 D0.4 N260"/{P(s)_fit_1e-09_eps_1e-08_max_e_100.0}.pdf}  % chktex 36
\label{fig:P(s)-fit-b0.55n260-me100}
\end{subfigure}

\begin{subfigure}{\textwidth}
  \includegraphics{"B0.55 D0.4 N260"/{P(s)_fit_1e-09_eps_1e-08_max_e_120.0}.pdf}  % chktex 36
\label{fig:P(s)-fit-b0.55n260-me120}
\end{subfigure}
\caption{\(P(s)\) for \(B=0.55, D=0.4, N=260\) and \(\Delta E_{max}=100, 120\)}
\label{fig:fit-b0.55n260-me100-120}  % chktex 24
\end{figure}

\begin{figure}
\centering
\begin{subfigure}{\textwidth}
  \includegraphics{"B0.55 D0.4 N260"/{P(s)_fit_1e-09_eps_1e-08_max_e_150.0}.pdf}  % chktex 36
\label{fig:P(s)-fit-b0.55n260-me150}
\end{subfigure}

\begin{subfigure}{\textwidth}
  \includegraphics{"B0.55 D0.4 N260"/P(s)_fit_1e-09_eps_1e-08}  % chktex 36
  \label{fig:P(s)-fit-b0.55n260}  % chktex 24
\end{subfigure}
\caption{\(P(s)\) for \(B=0.55, D=0.4, N=260\) and \(\Delta E_{max}=150, 180.3\)}
\label{fig:fit-b0.55n260-me150-180.3}  % chktex 24
\end{figure}

\clearpage

Looking at other values for \(B\) such as \(B=0.2\) and \(B=0.63\), we observe
that \(P(s)\) gets closer to a Poisson distribution with the increase of energy
(see~\cref{fig:fit-b0.2n260,fig:fit-b0.63n260,fig:fit-b0.2n260-me100,fig:fit-b0.63n260-me100}).
This global feature of the system can be observed on the entire interval from
\(B=0.01\) to \(B=0.63\). In order to emphasise this, we plot \(\alpha \)
as a function of \(\Delta E\)
(see~\cref{fig:alpha-e-b0.1-0.15-0.2,fig:alpha-e-b0.25-0.3-0.35,fig:alpha-e-b0.4-0.45-0.5,fig:alpha-e-b0.55-0.6-0.63}).

This remarkable behaviour can be viewed as consequence of the interplay of the
third and fourth order terms in the Hamiltonian. The third order terms, which
consist in the non-integrable part of the Hamiltonian and can be considered as
contributing to the apparition of chaotic trajectories.

\begin{figure}
\centering
  \begin{subfigure}{\textwidth}
    \includegraphics{"B0.2 D0.4 N260"/P(s)_fit_1e-09_eps_1e-08}  % chktex 36
    \label{fig:P(s)-fit-b0.2n260}  % chktex 24
  \end{subfigure}

  \begin{subfigure}{\textwidth}
    \includegraphics{"B0.2 D0.4 N260"/I(s)_fit}  % chktex 36
  \label{fig:I(s)-fit-b0.2n260}  % chktex 24
  \end{subfigure}
  \caption{\(P(s), I(s)\) for \(B=0.2, D=0.4, N=260\)}
  \label{fig:fit-b0.2n260}  % chktex 24
\end{figure}

\begin{figure}
\centering
  \begin{subfigure}{\textwidth}
    \includegraphics{"B0.63 D0.4 N260"/P(s)_fit_1e-09_eps_1e-08}  % chktex 36
    \label{fig:P(s)-fit-b0.63n260}  % chktex 24
  \end{subfigure}

  \begin{subfigure}{\textwidth}
    \includegraphics{"B0.63 D0.4 N260"/I(s)_fit}  % chktex 36
  \label{fig:I(s)-fit-b0.63n260}  % chktex 24
  \end{subfigure}
  \caption{\(P(s), I(s)\) for \(B=0.63, D=0.4, N=260\)}
  \label{fig:fit-b0.63n260}  % chktex 24
\end{figure}

\begin{figure}
\centering
\begin{subfigure}{\textwidth}
  \includegraphics{"B0.2 D0.4 N260"/{P(s)_fit_1e-09_eps_1e-08_max_e_100.0}.pdf}  % chktex 36
\label{fig:P(s)-fit-b0.2n260-me100}
\end{subfigure}

\begin{subfigure}{\textwidth}
  \includegraphics{"B0.2 D0.4 N260"/{I(s)_fit_max_e_100.0}.pdf}  % chktex 36
  \label{fig:I(s)-fit-b0.2n260-me100}  % chktex 24
\end{subfigure}
\caption{\(B=0.2, D=0.4, N=260, \Delta E_{max}=100\)}
\label{fig:fit-b0.2n260-me100}
\end{figure}

\begin{figure}
\centering
\begin{subfigure}{\textwidth}
  \includegraphics{"B0.63 D0.4 N260"/{P(s)_fit_1e-09_eps_1e-08_max_e_100.0}.pdf}  % chktex 36
\label{fig:P(s)-fit-b0.63n260-me100}
\end{subfigure}

\begin{subfigure}{\textwidth}
  \includegraphics{"B0.63 D0.4 N260"/{I(s)_fit_max_e_100.0}.pdf}  % chktex 36
  \label{fig:I(s)-fit-b0.63n260-me100}  % chktex 24
\end{subfigure}
\caption{\(B=0.63, D=0.4, N=260, \Delta E_{max}=100\)}
\label{fig:fit-b0.63n260-me100}
\end{figure}

% alpha(deltaE)

\begin{figure}
  \includegraphics{{"alpha_e_B[0.1, 0.15, 0.2]"_N[260]}.pdf}  % chktex 36
  \caption{\(B=0.1, 0.15, 0.2,\; N=260\)}
\label{fig:alpha-e-b0.1-0.15-0.2}
\end{figure}

\begin{figure}
  \includegraphics{{"alpha_e_B[0.25, 0.3, 0.35]"_N[260]}.pdf}  % chktex 36
  \caption{\(B=0.25, 0.3, 0.35,\; N=260\)}  % replace
\label{fig:alpha-e-b0.25-0.3-0.35}
\end{figure}

\begin{figure}
  \includegraphics{{"alpha_e_B[0.4, 0.45, 0.5]"_N[260]}.pdf}  % chktex 36
  \caption{\(B=0.4, 0.45, 0.5,\; N=260\)}  % replace
\label{fig:alpha-e-b0.4-0.45-0.5}
\end{figure}

\begin{figure}
  \includegraphics{{"alpha_e_B[0.55, 0.6, 0.63]"_N[260]}.pdf}  % chktex 36
  \caption{\(B=0.55, 0.6, 0.63,\; N=260\)}   % replace
\label{fig:alpha-e-b0.55-0.6-0.63}
\end{figure}

An other global characteristic of the system can be revealed by studying the
dependence of \(\alpha \) on the non-integrability parameter $B$.
In figure~\ref{fig:alpha-n220-240-260} \(\alpha(B)\) is plotted for
different values of $N$. The value of $N$ determines both the stability
of the values and the maximum energy interval. Indeed, since our method implies the
truncation of the Hilbert space, we restrict the number of considered energy
levels with the stability criterion.

As with the above case of \(\alpha(\Delta E)\), we can reduce the energy interval
by considering only the energy levels up to a given value. In figures~\ref{fig:alpha-n260-me100-120-140}
and~\ref{fig:alpha-n260-me140-160-0} we can see that the shape of \(\alpha(B)\)
for \(N=260\) remains qualitatively the same when we consider different
values for the maximum energy interval. We can observe that as we increase
the energy the values of \(\alpha \) globally rise, as expected from the
previous plots.

An other qualitative test of our data implies checking how the values of
\(\alpha \) change when we consider different values for \(N\), in this
case \(N=220, 240, 260\) and different energy intervals. By considering
multiple values for $N$ we can check the stability of the energy levels
within a given interval provided that the respective interval is less than
the maximum energy interval for all $N$.
In~\cref{fig:alpha-n220-240-260-me100,fig:alpha-n220-240-260-me120,fig:alpha-n220-240-260-me140}
we can see that indeed the energy levels
are stable as the values of \(\alpha \) barely change.

% alpha(B)

\begin{figure}
  \includegraphics{"alpha_N[220, 240, 260]".pdf}  % chktex 36
  \caption{\(N=220, 240, 260\)}
\label{fig:alpha-n220-240-260}
\end{figure}

\begin{figure}
  \includegraphics{{alpha_N[260]_max_e_"[100.0, 120.0, 140.0]"}.pdf}  % chktex 36
  \caption{\(N=260,\; \Delta E_{max} \approx 100, 120, 140\)}
\label{fig:alpha-n260-me100-120-140}
\end{figure}

\begin{figure}
  \includegraphics{{alpha_N[260]_max_e_"[140.0, 160.0, 0.0]"}.pdf}  % chktex 36
  \caption{\(N=260,\; \Delta E_{max} \approx 140, 160, 190.305\)}
\label{fig:alpha-n260-me140-160-0}
\end{figure}

\begin{figure}
  \includegraphics{{"alpha_N[220, 240, 260]"_max_e_[100.0]}.pdf}  % chktex 36
  \caption{\(N=220, 240, 260,\; \Delta E_{max} \approx 100\)}
\label{fig:alpha-n220-240-260-me100}
\end{figure}

\begin{figure}
  \includegraphics{{"alpha_N[220, 240, 260]"_max_e_[120.0]}.pdf}  % chktex 36
  \caption{\(N=220, 240, 260,\; \Delta E_{max} \approx 120\)}
\label{fig:alpha-n220-240-260-me120}
\end{figure}

\begin{figure}
  \includegraphics{{"alpha_N[220, 240, 260]"_max_e_[140.0]}.pdf}  % chktex 36
  \caption{\(N=220, 240, 260,\; \Delta E_{max} \approx 140\)}
\label{fig:alpha-n220-240-260-me140}
\end{figure}

\end{document}