\subsection{Design of system}
\label{sec: system}

\subsubsection{Problem Setting}
\label{sec: problem_settings}

Online Portfolio Selection is the process that one distributes his/her wealth into $m$ different financial market assets sequentially in a period of time $t$, aiming to achieve certain goals such as maximum return. The movements of an asset price usually includes open, high, low, close, volume (OHLCV) and closing price is mostly used in OLPS~\cite{li2014online}. In this work, price movement at time $t$ is defined in \emph{price relative vector}, $\vec X_{t}$. For example, the $i\mathrm{th}$ element of $\vec{X}_t$, $\vec{X}_{i,t}$ is the closing price of the $i\mathrm{th}$ asset in the $t$th period. If the market is continuous, $\vec{X}_t$ is also the opening prices at Period $t+1$. Mathematically, the \emph{price relative vector} is the ratio of $t$th closing price to previous $(t-1)$th closing price:
\begin{equation}
	\vec{x}_{t} := \vec{x}_{t} \oslash \vec{x}_{t-1} =
\left(\frac{ x_{1, t}}{x_{1, t-1}}, \frac{x_{2, t}}{x_{2, t-1}}, ..., \frac{x_{m, t}}{x_{m, t-1}} \right)^\intercal.
\label{eq:x}
\end{equation}

The portfolio value $P$ at the end of Period $t$ without considering transaction cost is thus calculated as the product of market sequence $X$ and portfolio weight vector $\vec w$,
\begin{equation} \label{eq:portfolio_value_t_no_cost}
    P_{t} = \prod_{t-1}^{i=0} \vec{x}_{i+1} \bcdot \vec{w}_{i},
\end{equation}

The sum of $\vec{w}_t$ is always 1 when leverage and short are not considered.
$\forall t, \sum\limits_i  w_{t,i} =1$. Then we can calculate the \emph{rate of return}(RoR) as,
\begin{equation} \label{eq:rho_t_no_cost}
	\rho_{t} := \frac{P_{t}}{P_{t-1}} = \vec{x}_{t} \bcdot \vec{w}_{t-1}
\end{equation}

The \emph{gross logarithmic rate of return} is simply apply logarithmic function to RoR, mathematically,
\begin{equation}
    \mathrm{log}r_{t} := \ln \frac{P_{t}}{P_{t-1}} = \ln \vec{x}_{t} \bcdot \vec{w}_{t-1}.
	\label{eq:log_r_no_cost}
\end{equation}
Therefore, the final portfolio value will be
\begin{equation}\label{eq:final_P_no_cost}
	P_\mathrm{f} = P_0 \exp\left( \sum\limits_{t=1}^{t+1} r_{t} \right) = P_0 \prod_{t=1}^{t+1} \vec{x}_{t} \bcdot \vec{w}_{t-1},
\end{equation}
where $P_0$ is the initial investment amount.

By maximizing rate of return of each period, the final accumulative return is expected to be maximized as well. The general OLPS framework is as following:

\begin{algorithm}[H]
\SetAlgoLined
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\Input{$\vec{x}_t^0$ Historic price data sequences}
\Output{$p_\mathrm{f}$ Final Portfolio Value}
{Initialize $\vec{w}_0 = (\frac{1}{m},\ldots,\frac{1}{m})$}

 \For{$t=1,2,\ldots,n$}{
  Portfolio weight vector $\vec{w}_t$ is calculated by the algorithm.\;
  Price data $x_t$ is revealed.\;
  Portfolio value vector at Period $t$ is computed as Eq~\ref{eq:portfolio_value_t_no_cost} indicates.\;
 }
 \caption{Online Portfolio Selection Framework Overview}
\end{algorithm}

There are three assumptions widely used in previous works~\cite{jiang2017deep, li2014online} and are also adopted in this work,
\begin{enumerate}
\label{num:assumptions}
    \item Transaction cost: it is assumed that no transaction cost/slippages incurred during online trading.
    \item Market liquidity: it is assumed that market liquidity is high enough, that the agents are able to buy/sell assets according to their algorithms.
    \item Market impact: it is assumed that the investments made by agents have zero influence on the price movement of the market.
\end{enumerate}

\subsubsection{Deep Reinforcement Learning Algorithm}
\label{subsub: drl}

This section presents a deep reinforcement learning algorithm to OLPS problem as Section~\ref{sec: problem_settings} describes, specifically a deep deterministic policy gradient algorithm. At the context of reinforcement learning, an agent interacts with its environment. For OLPS, the agent is the software or trading robot and the corresponding environment is certain financial market~\cite{jiang2017deep}. The agent makes an action which is buying/selling one or more assets. Mathematically,
\begin{equation}  \label{eq:action}
	\vec a_t = \vec{w_t}.
\end{equation}
where $w_t$ is the \emph{portfolio weight vector} at Period $t$. The action once made will influence the current situation. While in this work, $\vec{w}_{t-1}$ will include the influence as a part of policy making, the state therefore is represented as the pair of $X_t$ and $\vec{w}_{t-1}$,
\begin{equation} \label{eq:state}
	\vec s_t = (\bm X_t, \vec w_{t-1}),
\end{equation}
where $\vec w_0$ is predetermined by the algorithm. A policy is a mapping from state to action, $\pi:\mathcal{S}\rightarrow\mathcal{A}$. With gradient ascent, the optimal policy is made from a set of parameters $\vec{\theta}$ and the action will be $\vec a_t = \pi_{\vec \theta}(\bm s_t)$. The optimal policy will maximize the reward function from time interval $[0,t_\mathrm{f}]$, and performance metrics $J$ of $\pi_{\vec \theta}$ is the reward function from $t=0$ to $t=\mathrm{f}$:
\begin{equation}
	J(\pi_{\vec \theta}) = R\left( \vec s_1,\pi_{\vec \theta}(s_1),\cdots,
		\vec s_{t},\pi_{\vec \theta}(s_{t}),\vec s_{t+1} \right).
	\label{eq:policy_value}
\end{equation}
Then the parameters are updated by gradient descent method, towards the optimal direction,
\begin{equation}
	\vec\theta \longrightarrow \vec\theta + \lambda\nabla_{\vec\theta}J(\pi_{\vec \theta}).
	\label{eq:gradient_ascent}
\end{equation}
where $\lambda$ is the learning rate.

The reward function $R$ is the average logarithmic cumulated return for $n$ periods, expressly,
\begin{align}
	R &= \frac{1}{n} \sum_n \ln \vec x_{t} \bcdot \vec w_{t-1} \\
	  &= \frac{1}{n} \sum_n \mathrm{log}r_t
\end{align}

\subsubsection{Neural Networks and Policies}

Different neural networks are used to construct the policy $\pi_{\vec \theta}$, including Convolutional Neural Networks~\cite{lecun1998gradient}, Long-Short term Memory Networks~\cite{hochreiter1997long} and Recurrent Networks~\cite{jordan1986attractor}. Besides, several novel designs such as Identical Independent Evaluators (IIE), Ensemble of IIE (EIIE), Portfolio-Vector Memory (PVM), Online Stochastic Batch Learning (OSBL) are applied inside the network~\cite{jiang2017deep}.

EIIE topology means the neural networks output directly to the \emph{portfolio weight vector} $\vec{w}_t$ via softmax function. PVM saves the previous portfolio weights and takes them as input for next period of training. OSBL selects the mini-batch by a geometric distribution, simply the more recent price data will be selected more frequently.

\subsubsection{Design of the Python Package}

With the basic understanding of the algorithm, this section describes some functionalities of our python package \textit{mercurius}. The example is performed on futures contract market with data available online\footnote{project url: https://github.com/dexhunter/eco301}. There are 10 assets for selection and we select several traditional algorithms to test. The input price movements is shown as Figure~\ref{fig:input_data}.

\begin{figure}
    \centering
    \includegraphics{img/input.eps}
    \caption{Input Sequence Data from Futures Contract Market, 10 assets are contracts for coking coal, palm oil, iron ore, soybean oil, corn, polypropylene, soybean meal, ethylene glycol, plastic and coke respectively}
    \label{fig:input_data}
\end{figure}

The performances of different algorithms is compared at Figure~\ref{fig:algo} where we select Buy-and-Hold (BAH), Constant Rebalanced Portfolio (CRP) as benchmark. Robust Median Reversion (RMR) performs the best in terms of final APV. The numeric table is demonstrated at Table~\ref{tab:my_label}.

\begin{figure}
    \centering
    \includegraphics{img/algos.eps}
    \caption{Comparison of traditional machine learning techniques on Futures Contract Market}
    \label{fig:algo}
\end{figure}

\begin{table}[]
    \centering
\begin{tabular}{lrrrr}
\toprule
{} &   profit &    sharpe &           APV &  VaR(90\%) \\
\midrule
BAH   &  0.98876 &  -1.07573 & -9.885370e+00 &  -0.00045 \\
BCRP  &  1.03393 &   2.89564 &  7.971866e+01 &  -0.00094 \\
CRP   &  0.98824 &  -1.14052 & -1.063131e+01 &  -0.00046 \\
CWMR  &  1.42910 &   4.71027 &  2.044639e+01 &  -0.00018 \\
EG    &  0.98825 &  -1.13839 & -1.060031e+01 &  -0.00046 \\
OLMAR &  1.54763 &  38.42997 &  3.673105e+06 &  -0.00114 \\
ONS   &  0.98814 &  -1.18334 & -1.485441e+01 &  -0.00064 \\
PAMR  &  1.65176 &  44.91529 &  1.530392e+07 &  -0.00108 \\
RMR   &  1.57637 &  39.98359 &  5.552418e+06 &  -0.00113 \\
\bottomrule
\end{tabular}

    \caption{Traditional Machine Learning Techniques on Futures Contract Market}
    \label{tab:my_label}
\end{table}

\subsection{Design of evaluation}
\label{sec: evaluation}

All the algorithms mentioned at Section~\ref{subsub: background} will be backtested, in other words, historic data will be used to simulate a trading environment for algorithm to trade as if it sees the data points in real time. One caution there is to avoid look-ahead bias since the algorithm should receive the price data in a sequence. During backtesting, multiple metrics including accumulative portfolio value (APV), Sharpe ratio (SR), Maximum drawdown (MDD) are applied to measure the performance of different OLPS algorithms. To fairly compare portfolio values of different algorithms, APV is divided by the corresponding unit of the initial portfolio values:
\begin{equation}
    p_t = p_t / p_0.
\end{equation}
where $p_t$ is the accumulative portfolio value at time $t$ and $p_0$ is the initial portfolio value and will be set to $0$ after this division. APV reflects the profitability of an algorithm, however, it lacks risk information for the algorithm. Sharpe ratio and maximum drawdown, therefore, are taken into consideration for risk measurements. SR~\cite{sharpe1964, sharpe1994} considers the volatility of the portfolio values for both upwards and downwards movements, while MDD~\cite{blanchard1979MaxDD, magdon2003maximum} calculates the largest loss from a peak to a trough. Rigorously, SR is defined as the average of risk-free return by its deviation,
\begin{equation} \label{eq:sharpe}
    S = \frac{\overline{p}_t - p_f}{\sigma_p},
\end{equation}
where $\overline{p}_t$ is the expected portfolio return at time $t$, $p_f$ is the risk free rate of return (0 in this case), and $\sigma_p$ is the standard deviation of the portfolio value. For maximum drawdown, mathematically:
\begin{equation} \label{eq:mdd}
	D = \max_{\substack{\tau>t\\ t}} \frac{p_t-p_\tau}{p_t}.
\end{equation}

While in this work, more recent data from broker markets will be used. For example, forex data from October 2018 to October 2019 will be put into back-testing while training data will be from earliest available data points from broker\footnote{This work uses forex data provided by OANDA APIs}.


\documentclass[12pt]{article}
\usepackage{amsmath} % AMS Math Package
\usepackage{amsthm} % Theorem Formatting
\usepackage{amssymb}    % Math symbols such as \mathbb
\usepackage{graphicx} % Allows for eps images
\usepackage[dvips,letterpaper,margin=1in,bottom=0.7in]{geometry}
\usepackage{tensor}
 % Sets margins and page size
\usepackage{amsmath}

 % Expectation symbol
\DeclareMathOperator*{\E}{\mathbb{E}}
\DeclareMathOperator*{\V}{\mathbb{V}}

\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\definecolor{dgray}{gray}{0.35}

\usepackage{graphicx, subfigure, float} % graphics commands

\usepackage{sgame}

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\usepackage{listings}

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,	                   % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Octave,                 % the language of the code
  otherkeywords={*,...},           % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=2,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,	                   % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

\renewcommand{\labelenumi}{(\alph{enumi})} % Use letters for enumerate
% \DeclareMathOperator{\Sample}{Sample}
\let\vaccent=\v % rename builtin command \v{} to \vaccent{}
\usepackage{enumerate}
\renewcommand{\v}[1]{\ensuremath{\mathbf{#1}}} % for vectors
\newcommand{\gv}[1]{\ensuremath{\mbox{\boldmath$ #1 $}}}
% for vectors of Greek letters
\newcommand{\abs}[1]{\left| #1 \right|} % for absolute value
\newcommand{\avg}[1]{\left< #1 \right>} % for average
\let\underdot=\d % rename builtin command \d{} to \underdot{}
\renewcommand{\d}[2]{\frac{d #1}{d #2}} % for derivatives
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
% for partial derivatives
\providecommand{\fr}{\frac}
\providecommand{\RR}{\mathbb{R}}
\providecommand{\NN}{\mathbb{N}}
\providecommand{\seq}{\subseteq}
\providecommand{\e}{\epsilon}

\newtheorem{prop}{Proposition}
\newtheorem{thm}{Theorem}[section]
\newtheorem{axiom}{Axiom}[section]
\newtheorem{p}{Problem}[section]
\usepackage{cancel}
\newtheorem*{lem}{Lemma}
\theoremstyle{definition}
\newtheorem*{dfn}{Definition}
 \newenvironment{s}{%\small%
        \begin{trivlist} \item \textbf{Solution}. }{%
            \hspace*{\fill} $\blacksquare$\end{trivlist}}%
% ***********************************************************
% ********************** END HEADER *************************
% ***********************************************************

\begin{document}

{\noindent\Huge\bf  \\[0.5\baselineskip] {\fontfamily{cmr}\selectfont  Section 2 notes}         }\\[2\baselineskip] % Title
{ {\bf \fontfamily{cmr}\selectfont \large Gov 2006}\\ {\fontfamily{cmr}\selectfont    \today}}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    {\Large \textsc{sophie hill}} % Author name
\\[0.1\baselineskip]

\section*{Alesina (1988)}

\section*{Dixit, Grossman \& Gul (2000)}

\end{document}


\chapter{Analysis}
\label{chap:analysis}
\section{Word Embedding: Word2Vec}
The main focus for this \gls{dp} is for the author to get some expertise with the Word Embedding [\ref{stoa:word-embedding}] and more specifically the Word2Vec [\ref{stoa:word2vec}, \ref{stoa:word2vec-pros-cons}] algorithm, which is already very complete on features and parameters\cite{article:word2vec-parameters}.

\subsection{Word2Vec Operations}
\subsubsection{Analogies}
\label{analyse:analogies}

Word2Vec is known for being able to handle analogies and more specifically for the famous:\\\\ \textit{Man is to Woman as King is to? \textbf{[Queen]}}\\

Which translated with Gensim [\ref{stoa:gensim}] into the vectorial form and the geometric operation as:
\begin{lstlisting}[language=Python]
model.wv.most_similar(positive=['king','woman'],
                    negative=['man'], topn=1)
\end{lstlisting}

Outputing the following: \textbf{[('queen', 0.6848626732826233)]}

\subsubsection{Geometric Operations}
\label{analyse:geometric-operations}
As each word in Word2Vec is a vector representation in a multi-dimensional space, it implies that standard geometrical operations are applicable. Indeed, as seen with analogies [\ref{analyse:analogies}], some operations are being made, implying \textit{positive} and \textit{negative}. Below are the top 3 operations used in the Word2Vec.

\begin{itemize}
    \setlength\itemsep{0em}
    \item Addition
    \item Soustraction
    \item Cosine
\end{itemize}

\subsubsection{Common Tasks}
\label{analyse:common-tasks}
As seen with analogies [\ref{analyse:analogies}], the \textit{most\_similar\(\)} function is a massively used task for Word2Vec, however it's not the only one. Indeed, the following is the top 3 functions used with Word2Vec space:

\begin{itemize}
    \setlength\itemsep{0em}
    \item most\_similar('king'): outputs the top most similar words to a given word.
    \item similarity('woman','man'): outputs the degree of similarity between two words.
    \item doesnt\_match('dog cat computer bird'.split\(\)): outputs the non similar words.
\end{itemize}

\subsection{Word Length}
\label{analyse:word-length}
As Word2Vec is a multi-dimension space, in which words are positioned, it is important to understand how those vectors are positioned and what information they carry. The word length represents how often a word is used in a context. Indeed, if a word is used a lot in a context, its length will be greater than the same word used at the same frequency but split up in multiple contexts. Meaning that word length, in combination with the term frequency, is useful to measure the word significance in contexts. \cite{article:word2vec-length}

\subsection{Word Angle}
\label{analyse:word-angle}
Word vectors are not only carrying the length [\ref{analyse:word-length}], they also have a direction, popularly described by the Cosine function. The process applied to vectors in Word Embedding is known as the Cosine Similarity, which is the vectors normalized dot product and making it very efficient for evaluation, in particular with sparse vectors such as word vectors.

\subsubsection{Positive Space}
Often, the Word2Vec space is kept into a positive space, implying that the output is between 0 and 1, where 1 is for the angle at 0\textdegree, which implies that vectors are above each other and the vectors are most probably the same.

\subsubsection{Negative Space}
However, it is also possible to go into the negative similarities, which is described by vectors being in opposite directions, with an angle higher than 90\textdegree, which transcribes into the -1 value if the vectors are in the exact opposites, independently to their magnitude.

\subsection{Normalization}
\label{analyse:normalization}
During the similarity calculation, mathematically speaking normalizing vectors are making cosine [\ref{analyse:word-angle}] and dot-product equivalent. In word embedding, it is usually the relations between word vectors that are required, implying that to enhance the similarity function performances, the vector normalization is commonly used as in this case the length does not carry any useful information. However, if the relation to the context is required, the normalization should be avoided.

\subsection{Lemmatization}
\label{analyse:lemmatization}
Expect from its direct implication with the dictionary size and its implication related to the processing power\/time required to compute the model. Lemmatization should be considered in specific cases. Indeed, using it makes the Word2Vec space sparser, which is useful for small datasets; however, for big datasets, the gain is often negligible. \\

However, for the case where the words must carry different information depending on the context, for instance with abbreviations, it is necessary to use the lemmatization feature, for example, \gls{ir} could be mistaken with Infrared (IR).\\

Another case would be the need for the use of tokens for written words such as \textit{New York}.

\subsection{\gls{cbow}}
\label{analyse:cbow}
Continues Bag of Words is the original method presented in the Word2Vec paper \cite{article:word2vec}  and involves \gls{nn} to predict a word vector based on its context. In the case of a unique word vector, it is similar to an encoder-decoder architecture.\\

The concept behind \gls{cbow} is to take, for a given word vector, the \textit{n} neighboring word vectors as the context, then predict the given word vector, as seen on the figure \ref{fig:wikipedia_cbow_skipgram_img}.\\

Concerning the prediction quality, its measure requires to provide the hot encoding of the given word as input, so it calculates the output error and learns the vector representation of the word.

\subsection{Skip-Grams}
\label{analyse:skip-grams}
Introduced by Facebook, Fasttext\cite{article:fasttext} Word Embedding is using a variant of \gls{cbow} that looks like its flipped version, as seen on figure \ref{fig:wikipedia_cbow_skipgram_img}. It also involves a \gls{nn}, but instead of predicting the word based on the context, it predicts the context based on the word.\\

Given the target word vector as input, the model outputs the probability distribution for the given word.

\begin{figure}[ht!]
    \centering
    \includegraphics[width=\textwidth,height=6cm,keepaspectratio=true]{CBOW_eta_Skipgram}
    \caption{
       CBOW and Skip-Gram neural network representations\cite{wikipedia:cbow_skipgram_img}
    }
    \label{fig:wikipedia_cbow_skipgram_img}
\end{figure}

\subsection{Dimensions}
\label{analyse:dimensions}
Typically between 100 and 400, Word2Vec dimensions are defining the accuracy in the similarity between word vectors. It is difficult to find the sweet spot. However, there is some experimental curve for the dimension amount, which takes into account the final accuracy, time, and power consumption.

\paragraph{Low accuracy}
It is recommended to use at least 50 dimensions to avoid loosing too many properties of high dimensions.

\paragraph{Normal accuracy}
Using around 200 dimensions provides acceptable accuracy for the time spent computing.

\paragraph{High accuracy}
Around 300 dimensions, the results are considered very good; however, the time is high.

\paragraph{Beyond high accuracy}
It seems that beyond 300 dimensions, the accuracy gain is not worth anymore the extra time used during training.\\

In other words, the amount of dimensions usually reflects the over and underfitting of the model. Each model is dependent on the dataset, and it is often required to test the different number until the accuracy is acceptable. Plus, as this subject is still debated at the moment in the \gls{ml} community, there is no absolute value, except try and fail.

\subsection{Window}
\label{analyse:window}
As explained for \gls{cbow}\ref{analyse:cbow} and Skip-Grams\ref{analyse:skip-grams}, those algorithms are either taking the context as input, or outputting the context for a given word. However, to capture the context, it is required to define its size, which is in our case called the window.\\

By default, the value of  5 of the window is used and generally works well; however, the accuracy of the model will depend on the dataset used.\\

As a measure, the analogy score could be used. However, this score is often not the solution as the impact of the quality of a poor dataset is higher than the size of the window if using the default value. Plus it is essential to be careful with the sentence sizes of the corpus used; indeed, if the window value is higher to the average sentences length, the algorithm will not capture meaningful relations. \\

In other words, large windows size usually capture more information related to the main topic, and small windows capture the information related to the word itself.

\subsection{Epochs}
\label{analyse:epochs}
As the number of epochs is directly proportional to the amount of computation and time spent training the model, the common question to ask is: \textit{How much epochs will be giving the best accuracy for the time spent?} The answer is as always: it depends on the dataset and the parameters. \\

However, based on various sources, including the author of Gensim\cite{article:word2vec-epochs}, it is suggested that increasing the number of epochs have, in most of cases, an accuracy performance. The default value is usually five epochs.\\

Finally, the quest to find the right parameters is still a matter of experimentations.

\subsection{Gensim API}
It would not make much sense to go in details of each Gensim parameter, as a list of the parameters, and their description is available on their official API website\cite{article:gensim-api}.

However, to initiate the curiosity of the reader, the following list represents the most used parameters during the \gls{dp}:

\begin{itemize}
    \setlength\itemsep{0em}
    \item size: dimensions used [\ref{analyse:dimensions}]
    \item window: window size used [\ref{analyse:window}]
    \item min\_count: minimum frequency a word should have to be considered
    \item workers: cpu cores used
    \item sg: set at 1 to use Skip-Grams [\ref{analyse:skip-grams}] and set at 0 to use \gls{cbow} [\ref{analyse:cbow}]
    \item iter: epochs used [\ref{analyse:epochs}]
\end{itemize}

\subsection{Retrain Model}
\label{analyse:retrain}
In \gls{ml}, models are the resulting artifacts from training processes and provide the rules to generate an output for an input. A Word2Vec model is the representation of the relations between the dictionary of words and their contexts for the corpus it was trained on.\\

As seen in the state of the art section, pre-trained Word2Vec models are available on the internet [\ref{sota:word2vec-models}]. However, to save disk space and probably business knowledge, it is infrequent that the full model is shared. Indeed, the model provided is only a model with frozen \gls{nn} weights, which is perfectly fine for regular usage as it behaves the same as a full model, except that it is not containing the multi-dimensional matrix representing the Word2Vec space.\\

To get the full model, it requires to train it ourselves, with all the side-effects it implies, such as having a high amount of power and time consumption. However, a full model provides the ability to \textit{retrain} the model. One could add words to the dictionary or customize the weights to match a new verbal style or context. For instance, starting from on a Wikipedia model, it would be possible to influence the words to match the style of an author such as \textit{Edgar Allan Poe}.

\subsection{Evaluation}
\label{analyse:evaluation}
Determining if the model works correctly is difficult. A naive solution would be to create a complex supervised protocol to evaluate the success of a model. However, it would not require an enormous amount of work from a human perceptive. A solution is to exploit the analogies capabilities from Word2Vec \ref{analyse:analogies}, which, in theory, should perform well at.\\

The concept is called the \textit{Analogy Evaluation} and it uses a list of pre-made analogies in various domaines, such as the famous: \textbf{Man is to King, as Woman is to?}, and evaluates if the results from the model match the expect answer: \textit{Queen}. In Gensim, the following function does the evaluation:
%[language=Python]
\begin{lstlisting}
evaluate_word_analogies(analogies,
                        restrict_vocab=300000,
                        case_insensitive=True,
                        dummy4unknown=False)
\end{lstlisting}
\vspace{1em}

Another evaluation method would be based on the word similarities itself. Indeed, based on a pre-made list of pairs of words, an input such as \textit{cup} would output \textit{mug}. This evaluation is done in Gensim via the following function:

%[language=Python]
\begin{lstlisting}
evaluate_word_pairs(pairs, delimiter='\t',
                    restrict_vocab=300000,
                    case_insensitive=True,
                    dummy4unknown=False)
\end{lstlisting}

\subsection{CPU VS GPU}
As mentioned in the state of the art section, Word2Vec is performing better than \gls{dl} solutions, in the most cases, because it is not using labeled data [\ref{sota:rnn}], even with the use of GPUs. However, in the context of Word2Vec, an experiment from Gensim has been made to compare the computation made CPU and GPU on the Gensim Framework \cite{article:word2vec-cpu-gpu}. The result is unexpected from a generalization point of view: indeed, it appears that CPUs are performing better than GPUs during the Word2Vec training.


\chapter{Syntax}
{
 \newcommand{\term}      {\mbox{\it term}}
 \newcommand{\IMP}       {\mbox{\tt ==>}}
 \newcommand{\ALL}       {\mbox{\tt !}}
 \newcommand{\EXISTS}    {\mbox{\tt ?}}
 \newcommand{\CHOOSE}    {\mbox{\tt @}}
 \newcommand{\EXISTSONE} {\mbox{\tt ?!}}
 \newcommand{\LET}       {\mbox{\tt let}}
 \newcommand{\und}       {\mbox{\tt and}}
 \newcommand{\IN}        {\mbox{\tt in}}
 \newcommand{\CONS}      {\mbox{\tt CONS}}
 \newcommand{\INSERT}    {\mbox{\tt INSERT}}
 \newcommand{\SUC}       {\mbox{\tt SUC}}
 \newcommand{\vstr}      {\mbox{\it vstr}}
 \newcommand{\numeral}   {\mbox{\it numeral}}
 \newcommand{\charseq}   {\mbox{\it charseq}}

The HOL logic is a classical higher-order predicate calculus. Its
syntax enjoys two main differences from the syntax of standard first
order logic.\footnote{We assume the reader is familiar with first
  order logic.}  First, there is no distinction in HOL between terms
and formulas: HOL has only terms. Second, each term has a type: types
are used in order to build well-formed terms. There are two ways to
construct types and terms in HOL: by use of a parser, or by use of the
programmer's interface. In this chapter, we will focus on the concrete
syntax accepted by the parsers.

\section{Types}

A HOL type can be a variable, a constant, or a compound type, which is
a constant of arity $n$ applied to a list of $n$ types.
\[
\begin{array}{rclr}
  \type & ::= & \mbox{\bf '}\ident & \mbox{(type variable)} \\
  & | &  \verb+bool+ & \mbox{(type of truth values)} \\
  & | &  \verb+ind+ & \mbox{(type of individuals)} \\
  & | &  \type\ \verb+->+\ \type & \mbox{(function arrow)} \\
  & | &  \type\ \ident\ \type\ & \mbox{(binary compound type)}\\
  & | &  \ident & \mbox{(nullary type constant)} \\
  & | & \type\; \ident & \mbox{(unary compound type)} \\
  & | & (\type_1,\ldots,\type_n) \ident & \mbox{(compound type)}
\end{array}
\]
Type constants are also known as type operators. They must be
alphanumeric. Type variables are alphanumerics written with a leading
prime ('). In \HOL{}, the type constants {\tt bool}, {\tt fun}, and
{\tt ind} are primitive. The introduction of new type constants is
described in Section~\ref{type-defs}. {\tt bool} is the two element type
of truth values. The binary operator {\tt fun} is used to denote
function types; it can be written with an infix arrow. The nullary
type constant {\tt ind} denotes an infinite set of individuals; it is
used for a few highly technical developments in the system and can be
ignored by beginners.  Thus
\begin{verbatim}
     'a -> 'b
     (bool -> 'a) -> ind
\end{verbatim}
are both well-formed types. The function arrow is ``right associative'',
which means that ambiguous uses of the arrow in types are resolved by
adding parentheses in a right-to-left sweep: thus the type expression
\begin{verbatim}
     ind -> ind -> ind -> ind
\end{verbatim}
is identical to
\begin{verbatim}
     ind -> (ind -> (ind -> ind)).
\end{verbatim}
The product (\verb+#+) and sum (\verb!+!) are other infix type
operators, also right associative; however, they are not loaded by
default in \HOL{}. How to load in useful logical context is described
in Section~\ref{theoryfns}.

\section{Terms}

Ultimately, a HOL term can only be a variable, a constant, an
application, or a lambda term.
\[
\begin{array}{rclr}
  \term & ::= & \ident & \mbox{(variable or constant)} \\
  & | &  \term\  \term & \mbox{(combination)} \\
  & | &  \bs\ident.\  \term &
  \mbox{(lambda abstraction)}
\end{array}
\]
In the system, the usual logical operators have already been defined,
including truth (\verb+T+), falsity (\verb+F+), negation (\verb+~+),
equality (\verb+=+), conjunction (\verb+/\+), disjunction (\verb+\/+),
implication (\verb+==>+), universal (\verb+!+) and existential
(\verb+?+) quantification, and an indefinite description operator
(\verb+@+). As well, the basis includes conditional, lambda, and `let'
expressions. Thus the set of terms available is, in general, an
extension of the following grammar:
\[
\begin{array}{rclr}
  \mbox{\it term} & ::= & \term : \type & \mbox{(type constraint)} \\
  & | & \term\ \term & \mbox{(application)} \\
  & | & \verb+~+ \term & \mbox{(negation)} \\
  & | & \term\ =\ \term & \mbox{(equality)} \\
  & | & \term\ \IMP\ \term & \mbox{(implication)} \\
  & | & \term\ \verb+\/+\ \term & \mbox{(disjunction)} \\
  & | & \term\ \verb+/\+\ \term & \mbox{(conjunction)} \\
  & | & \texttt{if}\ \term\ \texttt{then}\ \term\ \texttt{else}\ \term &
  \mbox{(conditional)} \\
  & | & \bs\ident_1\ldots\ident_n.\  \term & \mbox{(lambda abstraction)} \\
  & | & \ALL \ident_1\ldots\ident_n.\ \term & \mbox{(forall)} \\
  & | & \EXISTS \ident_1\ldots\ident_n.\ \term & \mbox{(exists)} \\
  & | & \CHOOSE \ident_1\ldots\ident_n.\ \term & \mbox{(choose)} \\
  & | & \EXISTSONE \ident_1\ldots\ident_n.\ \term &\mbox{(exists-unique)} \\
  & | & \LET\; \ident = \term  & \\
  &   & [\und\ \ident = \term]^{*}\ \IN\ \term & \mbox{(let expression)} \\
  & | & \verb+T+ & \mbox{(truth)} \\
  & | & \verb+F+ & \mbox{(falsity)} \\
  & | & \ident & \mbox{(constant or variable)} \\
  & | & \verb+(+ \term \verb+)+ & \mbox{(parenthesized term)}
\end{array}
\]

Some examples may be found in Table \ref{syntaxExamples}. Term
application can be iterated. Application is left associative so that
$\term\ \term\ \term \ldots \term$ is equivalent in the eyes of the
parser to $(\ldots((\term\ \term)\ \term) \ldots)\ \term$.

The lexical structure for term identifiers is much like that for
ML: identifiers can be alphanumeric or symbolic. Variables must be
alphanumeric. A symbolic identifier is any concatenation of the characters
in the following list:
\begin{verbatim}
    #?+*/\=<>&%@!,:;_|~-
\end{verbatim}
with the exception of the keywords \verb+\+, \verb+;+, \verb+=>+,
\verb+|+, and \verb+:+ (colon). Any alphanumeric can be a constant except the
keywords \verb+let+, \verb+in+, \verb+and+, and \verb+of+.

 \begin{table}[h]
\begin{center}
 \begin{tabular}{|r|l|} \hline
 \verb+x = T+ & {\it x is equal to true.} \\
 \verb+!x. Person x ==> Mortal x+ & {\it All persons are mortal.} \\
 \verb+!x y z. (x ==> y) /\ (y ==> z) ==> x ==> z+ & {\it Implication is
 transitive.} \\
 \verb+!x. P x ==> Q x+ & {\it P is a subset of Q} \\
 \verb+S = \f g x. f x (g x)+ & {\it Definition of a famous combinator.} \\ \hline
 \end{tabular}
 \caption{Concrete Syntax Examples}\label{syntaxExamples}
\end{center}
 \end{table}

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


\chapter{Time } \label{Ch:TimeSystems} \index{Time systems}

Time is the primary independent variable in GMAT.  Time is used in
integrating the equations of motion, and calculating planetary
ephemerides, the orientations of planets and moons, and atmospheric
density among others.  GMAT uses three types of time systems
depending on the type of calculations being performed: universal
time systems based on the Earth's rotation with respect to the Sun;
dynamic time systems that are based on the dynamic motion of the
solar system and take into account relativistic effects; and atomic
time systems based on the oscillation of the cesium atom. Each of
these time systems has specific uses and is discussed below.  In
addition,  universal, dynamic, and atomic time systems can be
expressed in different time formats.  The two time formats used in
GMAT are the Modified Julian Date (MJD) format, and the Gregorian
Date (GD) format.  In the next section, we'll take a look at the
time systems used in GMAT, and when GMAT uses each time system. Then
we'll look at the different time formats.

\section{Time Systems}

GMAT uses several different time systems in physical models and
spacecraft dynamics modelling.  The choice of time system for  a
particular calculation is determined by which time system is most
natural and convenient, as well as the  accuracy required.  In
general, for determining Earth's orientation at a given epoch, we
use one of several forms of Universal Time (UT), because universal
time is based on the Earth's rotation with respect to the Sun.
Planetary ephemerides are usually provided with time in a dynamic
time system, because dynamic time is the independent variable in the
dynamic theories and ephemerides.  The independent variable in
spacecraft equations of motion in GMAT is time expressed in an
atomic time system.  Let's look at each of these three systems,
starting with atomic time.

\subsection{Atomic Time:  TAI and A.1} \index{Time systems!TAI}
\index{Time systems!A.1} \label{Sec:AtomicTime}

Atomic time (AT) is a highly accurate time system which is
independent of the rotation of the Earth\cite{vallado2}.
Therefore, AT is a natural system for integrating a spacecraft's
equations of motion.  AT is defined in terms of the oscillations
of the cesium atom at mean sea level. The duration of the SI
second is defined to be 9,192,631,770 oscillations of the cesium
nuclide 133Ce. Two atomic times systems are are used in GMAT: A.1,
and international atomic time (TAI). A.1 is in advance of TAI by
0.0343817 seconds.
%
\begin{equation}
     A.1 = TAI + 0.0343817 sec
\end{equation}
%
where
%
\begin{equation}
   TAI = UTC + \Delta AT
\end{equation}
%
and $\Delta AT$ is the number of leap seconds, added since 1972,
needed to keep $\mid UTC - UT1 \mid \leq 0.9 $sec.  GMAT reads
$\Delta AT$ from the file named \emph{tai-utc.dat}. For times that
appear before the first epoch on the file, GMAT uses the first value
found in the file.  For times that appear after the last epoch, GMAT
uses the last value contained in the file.  Currently, GMAT uses A.1
time as the independent variable in the equations of motion. TAI is
used as a time system for defining spacecraft state information.

Now let's look at the universal time system.

\subsection{Universal Time: UTC and UT1}\index{Time systems!UTC}
\index{Time systems!UT1} \label{Sec:UniversalTime}

All of the universal time (UT) scales are based on the Earth's
rotation with respect to a fixed point (sidereal time) or with
respect to the Sun (solar time). The observed universal time (UTO)
is determined from observations of stellar transits to determine
mean local sidereal time. UT1 is UTO corrected for the Earth's polar
motion and is used when the instantaneous orientation of the Earth
is needed.  UTC is the basis for all civil time standards. It is
also known as Greenwich mean time (GMT) and Zulu time (Z). The UTC
time unit is defined to be an SI second, but UTC is kept within 0.9
seconds of UT1 by occasional leap second adjustments.  The equation
relating UTC and UT1 is
%
\begin{equation}
    UTC = UT1 - \Delta UT1 \label{Eq:UT1ToUTC}
\end{equation}
%
In GMAT,  $\Delta UT1$ is read from the file \emph{eopc04.62-now}
provided by the International Earth Rotation and Reference Systems
Service (IERS). The file containing the latest measurements and
predictions can be found at
\\\emph{http://www.iers.org/}. For times past the last epoch
contained in the file, GMAT uses the last value of $ \Delta UT1$
contained in the file. GMAT uses UTC as a time system to define
spacecraft state information. UT1 is used to determine the Greenwich
hour angle and for the sidereal time portion of FK5 reduction.

\subsection{Dynamic Time: TT, TDB and TCB}\index{Time systems!TDB}
\index{Time systems!TT}\index{Time systems!TCB}
\label{Sec:DynamicTime}

Dynamical time is the independent variable in the dynamical
theories and ephemerides. This class of time scales contains
terrestrial time (TT),  Barycentric Dynamical time (TDB), and
Barycentric Coordinate Time (TCB).    TDB is the independent
variable in the equations of motion referred to the solar system
barycenter. It is also the coordinate time in the theory of
general relativity. Despite the fact that the Jet Propulsion
Laboratory (JPL) J2000.0 ephemerides are referred to in TDB, TT is
frequently used. This is because TT and TDB always differ by less
than 0.002 sec. As higher accuracy or more sensitive missions are
planned, the difference may need to be distinguished.  In this
section we'll discuss how to calculate TT, TDB and TCB, and
discuss where each is used in GMAT.

TT is the independent variable in the equations of motion referred
to the Earth's center. It is also the proper time in the theory of
general relativity. The unit of TT is a day of 86400 SI seconds at
mean sea level.  In GMAT, TT is used in FK5 reduction, and as an
intermediate time system in the calculation of TDB and TCB.  TT
can be calculated from the following equation:
%
\begin{equation}
     TT = TAI + 32.184 \hspace{.05 in}\mbox{sec}
\end{equation}
%

Calculating TDB exactly is a complicated process that involves
iteratively solving a transcendental equation.  For this reason,
it is convenient to use the following approximation
%
\begin{equation}
     TDB \approx TT + \underbrace{0.001658\sin{M_E} +
     0.00001385\sin{2M_E}}_{\mbox{units of seconds}}
\end{equation}
%
Note that the term in the underbrace has units of seconds, and
depending upon the units of $TT$, which is usually in days, a
conversion of the term may be necessary before performing the
addition with $TT$. $M_E$ is the Earth's mean anomaly with respect
to the sun and is given approximately as
%
\begin{equation}
     M_E \approx 357.5277233 + 35,999.05034 T_{TT}
\end{equation}
%
where $T_{TT}$ is the time in TT expressed in the Julian Century
format.  $T_{TT}$ can be calculated from
%
\begin{equation}
     T_{TT} = \frac{JD_{TT} - 2,451,545.0}{36,525} \label{Eq:T_TTComputation}
\end{equation}
%
where $JD_{TT}$  is the time in TT expressed in the Julian Date
format.  For a more complete discussion of the TDB time system, see
Vallado\cite{vallado2} (pp. 195-198) and Seidelmann\cite{seidelmann}
(pp. 41-48).  GMAT uses TDB as the default time system in the JPL
ephemerides files. There is an option to use TT in the ephemerides
using the \st{UseTTForEphemeris} flag.

The last dynamic time system GMAT uses is Barycentic Coordinate
Time.  In 1992, the IAU adopted this system and clarified the
relationships between space-time coordinates\cite{seidelmann}.  In
general, calculating TCB requires a four-dimensional space-time
transformation that is well beyond the scope of this discussion.
However, TCB can be approximated using the following equation:
%
\begin{equation}
 TCB - TDB = L_B  (JD - 2443144.5)  86400
 \end{equation}
%
The present estimate of the value of $L_B$ is $1.550505 x 10^{-8}$
$(+/- 1 x 10^{-14})$ (Fukushima et al., Celestial Mechanics, 38,
215, 1986).  It is important to note that the main difference
between TDB and TCB is a secular drift, and that as of the J2000
Epoch, the difference was approximately 11.25 seconds and growing
\cite{Seidelmann:etal:92}. GMAT uses time in the TCB system to
evaluate the IAU data  for the spin axes and prime meridian
locations of all planets and moons except for
Earth.\cite{Seidelmann:etal:02}  Note that
Seidelmann\cite{Seidelmann:etal:02} mistakenly says that time in TCB
should be used in the equations given for the  pole and meridian
locations of the planets.  The correct time to use is TDB, and GMAT
uses this time system.

\section{Time Formats} \index{Time Formats} \label{Sec:TimeFormats}

There are two time formats that GMAT uses to represent time in the
systems discussed above.  These formats are called the Gregorian
Date (GD), and the Julian Date (JD).  The difference between the GD
and JD formats is how they represent the Year, Month, Day, Hours,
Minutes, and Seconds of a given date.  The GD format is well known,
and the J2000 epoch is expressed as, 01 Jan 2000 12:00:00.000 TT.
The reference epoch for the GD calendar is the beginning of the
Christian Epoch.  The JD format represents an epoch as a continuous
number containing the day and the fraction of day.

The J2000 epoch \index{J2000 Epoch} is commonly used in
astrodynamics as a reference epoch for planetary and other data. The
J2000 epoch occurred at 01 Jan 2000 12:00:00.000 TT.  The time
system, TT, is important for precise applications!  While the J2000
epoch is a specific instant in time, the numerical value changes
depending upon which time system you express it in. We can make an
analogy with vector algebra where we have an abstract quantity that
is a vector, and can't write down a set of numbers representing the
vector until we choose a coordinate system. Similarly, the J2000
epoch can be written in any of the different time systems and
formats. All of the following are equivalent definitions of the
J2000 Epoch:
%
\begin{eqnarray}
    2451545.0 & TT \nonumber \\
    2451544.9996274998411 & TAI \nonumber \\
    2451544.9992571294708 & UTC \nonumber\\
    2451544.9999999990686 & TDB \nonumber
\end{eqnarray}
%
\begin{eqnarray}
 & \mbox{01 Jan }   2000 \hspace{.1 in} 12:00:00.000000000 &\mbox{TT} \nonumber \\
& \mbox{01 Jan }  2000 \hspace{.1 in}   11:59:27.815986276  &\mbox{TAI} \nonumber \\
& \mbox{01 Jan }  2000 \hspace{.1 in} 11:58:55.815986276 &\mbox{UTC} \nonumber\\
 & \mbox{01 Jan }  2000 \hspace{.1 in} 11:59:59.999919534  &\mbox{TDB} \nonumber
\end{eqnarray}

In the next two sections we'll look at how to convert an epoch in a
given time system from the GD format to the JD format, and vice
versa.

\subsection{Julian Date and Modified Julian Date}\index{Time
formats!Julian date} \index{Time formats!modified Julian
date}\label{Sec:JDFormat}

The Julian date is a time format in which we can express a time
known in any of the Atomic, Universal or Dynamic time systems. The
Julian Date is composed of the Julian day number and the decimal
fraction of the current day.  Seidelmann\cite{seidelmann} (pp.
55-56) says ``The Julian day number represents the number of days
that has elapsed, at Greenwich noon on the day designated, since
...the epoch noon Jan 1 4713 B.C. in the Julian proleptic
calendar.  The Julian date (JD) corresponding to any instant is,
by simple extension to this concept, the Julian day number
followed by  the fraction of the day elapsed since the preceding
noon".

The fundamental epoch for most astrodynamic calculations is the
J2000.0 epoch\cite{seidelmann}.  This epoch is GD 01 Jan 2000
12:00:00.000 in the TT time system and is expressed as JD 2451545.0
TT. To convert between Julian Date format and Gregorian Date format,
GMAT uses Algorithm 14 from Vallado\cite{vallado2}
%
\begin{equation}
\begin{split}
    JD = 367Y - Int\left( \displaystyle\frac{ 7\left( Y + Int\left( \displaystyle\frac{M + 9}{12} \right) \right)}{4}
    \right)+ \\Int\left(\displaystyle\frac{275M}{9} \right) + D +
    1,721,013.5 + \frac{ \displaystyle\frac{ \displaystyle\frac{s}{60}+m}{60} + H}{24}
    \end{split}
\end{equation}
%
where $Y$ is the four digit year, $Int$ signifies real truncation,
and $M$, $D$, $H$, $m$ and $s$ are month, day, hour, minutes, and
seconds respectively. This equation is valid for the time period 01
Mar. 1900 to 28 Feb. 2100.

For numerical reasons it is often convenient to work in a Modified
Julian Date (MJD) format to ensure we can capture enough significant
figures using double precision computers.  In GMAT the MJD system is
defined as
%
\begin{equation}
     MJD = JD - 2,430,000.0
\end{equation}
%
where the reference epoch expressed in the GD format is 05 Jan 1941
12:00:00.000.  However, we must be careful in calculating the
Modified Julian Date, or we will lose the precision we are trying to
gain.  GMAT calculates the MJD as follows:
%
\begin{equation}
     \begin{split}
     \mbox{JDay} = & 367Y - Int\left( \displaystyle\frac{ 7\left( Y + Int\left( \displaystyle\frac{M + 9}{12} \right) \right)}{4}
    \right)+ \\ & Int\left(\displaystyle\frac{275M}{9} \right) + D +
    1,721,013.5
    \end{split}
\end{equation}
%
\begin{equation}
     \mbox{PartofDay} = \frac{ \displaystyle\frac{ \displaystyle\frac{s}{60}+m}{60} + H}{24}
\end{equation}
%
\begin{equation}
     \mbox{MJD} = (\mbox{JDay}- 2,430,000.0) + \mbox{PartofDay}
\end{equation}
%
The important subtlety is that we must subtract the MJD reference
from the JD, before we add the fraction of day, to avoid losing
precision in the MJD.

\subsection{Gregorian Date}\index{Time formats!Gregorian
date}\label{Sec:GregorianDateFormat}

The Gregorian Date format is primarily used as a time system in
which to enter state information in GMAT.  GD is not a convenient
time format for most mathematical calculations.  Hence, GMAT often
takes input in the GD format and converts it to a MJD format for
use internally.  The algorithm for converting from GD to JD is
taken from Vallado\cite{vallado2} and reproduced here verbatim.

\begin{equation}
\begin{array}{llll}
      & T_{1900} = \displaystyle\frac{JD - 2,415,019.5}{365.25}\\
      & \\
      & Year = 1900 + \mbox{TRUNC}(T_{1900}) \\
      & \\
      & LeapYrs = \mbox{TRUNC}((Year - 1900 - 1)(0.25))\\
      & \\
      & Days = (JD - 2,415,019.5) - \\
      & \hspace{.5 in}((Year - 1900)(365.0) +
               LeapYrs)\\
      & \\
      & \mbox{IF} Days < 1.0 \mbox{ THEN}
      \end{array}\nonumber
\end{equation}
%
\begin{equation}
     \begin{array}{llll}
      & \\
      & \hspace{.5 in} Year = Year - 1 \\
      &  \\
      & \hspace{.5 in} LeapYrs = \mbox{TRUNC}((Year - 1900 - 1)(0.25))\\
      & \\
      & \hspace{.5 in}Days = (JD - 2,415,019.5) - \\
      & \hspace{1 in}((Year - 1900)(365.0) +
               LeapYrs)\\
      & \\
      & \mbox{If (\emph{Year} Mod 4)} = 0 \mbox{ Then} \\
      & \\
      &  \hspace{.5 in} LMonth[2] = 29 \\
       \end{array}\nonumber
\end{equation}
%
\begin{equation}
     \begin{array}{llll}
      & DayofYr = \mbox{TRUNC}(Days) \\
      & \\
      & \mbox{Sum days in each month until}\\
      & \hspace{.5 in} \mbox{\emph{LMonth} + 1 summation} > \mbox{\emph{DayofYr}} \\
      & \\
      &  Mon = \# \mbox{ of months in summation} \\
      & \\
      &  Day = DayofYr - LMonth \mbox{ summation}\\
      & \\
      &  \tau = (Days - DayofYr)24\\
      &  \\
      &  h = \mbox{TRUNC}(Temp)\\
      &  \\
      &  min = \mbox{TRUNC}((Temp - h)60) \\
      &  \\
      &  s = \left( Temp - h - \displaystyle\frac{min}{60} \right)3600
      %
       \end{array}\nonumber
\end{equation}

\section{Conclusions}

In this chapter we looked at three time systems that GMAT uses to
perform internal calculations:  atomic time, universal time, and
dynamic time.  Atomic time is used to integrate spacecraft
equations of motion, while universal time is used to determine the
sidereal time and greenwhich hour angle for use in FK5 reduction.
Dynamic time systems are used in the JPL ephemerides and in the
IAU planetary orientation data.   Time, in any of these time
systems, are represented in two formats:  the Gregorian Date, and
Julian Date. We looked at how to convert between different time
systems, and between different time formats.


\documentclass{article}
\usepackage{amsmath}

\begin{document}

\title{Bland's Rule Elaboration}
\author{Athan Clark\\ Copyright \copyright \enspace The Grid, 2015}

\maketitle

\begin{abstract}
Bland's rule is the most popular method to deciding the next pivot to take when
refactoring a system of linear inequalities for more-optimized basic-feasible
solutions. Here, we detail Bland's rule, and provide it's dual form for implementation
in the Haskell version of the Cassowary constraint solver.
\end{abstract}

\section{Overview}

Say we have an objective function in the following form:

\begin{flalign*}
  &\omega \enspace = \enspace \sum_{0}^{n} \alpha_{n} x_{n} \enspace + \enspace c_{\omega}
\end{flalign*}

That is, an equation where the unique objective variable \(\omega\) is in basic-normal form, and
the rest of the public \(n\) variables are summed with their coefficients - \(\alpha_{n} x_{n}\),
and the constant value \(c_{\omega}\). We use \(\alpha\) to denote \textit{objective} coefficients.

This objective function is then maximized over a constraint set with the following form:

\begin{flalign*}
  & \left \{
      \sum_{0}^{n} \beta_{(m,n)} x_{n} \enspace + \enspace c_{m}
    \right \}
\end{flalign*}

For each constraint \(m\). We use \(\beta\) to denote \textit{constraint} coefficients. Each
constraint \(m\) can have any variable \(x_{n}\) in basic-feasible form. Also, note that
the slack variables normally introduced in this context are implicit - they are referenced
by the unique identifier \(m\) per-constraint.

\section{Bland's Rule Primal}

The primal version of Bland's rule proceeds as follows: Choose a column \(k\) out of
\(n\), and a row \(j\) out of \(m\) as the next variable to refactor into basic-normal
form over the whole constraint set - this as, \(x_{k}\) will be defined in terms of
the constraint \(j\).

\subsection{Column Selection}

\begin{flalign*}
  & \mathrm{Choose} \enspace \alpha_{k} \enspace := \enspace
    \mathrm{min} \left \{
                    \alpha_{n}
                    \enspace | \enspace \alpha_{n} < 0
                 \right \}
\end{flalign*}

We choose the column \(k\) to be the minimum \textit{negative} objective coefficient.

\subsection{Row Selection}

In the primal version of Bland's rule, we need to first solve for \(k\) before we
can solve for \(j\).

\begin{flalign*}
  & \mathrm{Choose} \enspace \frac{c_{j}}{\beta_{(j,k)}} \enspace := \enspace
    \mathrm{min} \left \{
                    \frac{c_{m}}{\beta_{(m,k)}}
                    \enspace \bigg| \enspace \beta_{(m,k)} > 0
                 \right \}
\end{flalign*}

That is, we choose \(j\) to be the minimum (primal) Bland ratio - the constant
\(c_{m}\) divided by the \textit{strictly positive} coefficient \(\beta_{(m,k)}\).

You can find the proofs that this method of row and column selection does not
cycle, and is strictly maximizing the basic-feasible solution in the literature.

\section{Bland's Rule Dual}

The dual version of Bland's rule is very similar to the primal version, except
for the swapped roles - this would behave the same as the primal version if we
transpose the matrix of the objective function and constraint coefficients.

Here, we will instead need to select the row \(j\) \textbf{first}, and the column
second.

\subsection{Row Selection}

\begin{flalign*}
  & \mathrm{Choose} \enspace c_{j} \enspace := \enspace
    \mathrm{min} \left \{
                    c_{m}
                    \enspace | \enspace c_{m} < 0
                 \right \}
\end{flalign*}

We choose the row \(j\) to be the minimum \textit{negative} constraint constant.

\subsection{Column Selection}

In the dual version of Bland's rule, we need to first solve for \(j\) before we
can solve for \(k\).

\begin{flalign*}
  & \mathrm{Choose} \enspace \frac{\alpha_{k}}{\beta_{(j,k)}} \enspace := \enspace
    \mathrm{min} \left \{
                    \frac{\alpha_{n}}{\beta_{(j,n)}}
                    \enspace \bigg| \enspace \beta_{(j,n)} > 0
                 \right \}
\end{flalign*}

The dual version of Bland's rule will \textit{minimize} the basic-feasible solution
for the constraint set and objective function.

\end{document}


\section{How to add an equation}~\label{sec:equations}

An example Eq.~\eqref{eqn:gru} is as follows.
\begin{equation}
\vspace{0mm}
\begin{aligned}
\label{eqn:gru}
& r_t = \sigma( W_{xr} x_t + W_{hr} h_{t-1}) \\
& z_t = \sigma( W_{xz} x_t + W_{hz} h_{t-1}) \\
& u_t = \tanh( W_{xu} x_t + W_{hu} (  r_t \odot h_{t-1}))\\
& h_t =  z_t h_{t-1} + (1 - z_t) u_t\\
\end{aligned}
\vspace{0mm}
\end{equation}
where $\sigma(\cdot)$ and $\mbox{tanh}(\cdot)$ are the sigmoid and tangent hyperbolic activation functions, respectively, $\odot$ denotes the element-wise multiplication operator,
and $\mathbf{r}_t$,  $\mathbf{z}_t$ are referred to as {\em reset}, {\em update} gates, respectively. $\mathbf{u}_t$ and  $\mathbf{h}_t$ are the candidate activation and the new hidden state of the GRU, respectively.


\section{Nuclear fission cross section.}

\hspace{1.0em}The probability $P_{n}^{fis}$ that fission occurs at any
step of evaporation chain with $n$ evaporated fragments can be defined
as
\begin{equation}
\label{FCS1}P_{n}^{fis} = 1-P_{n},
\end{equation}
where $P_{n}$ is the probability of a transition from an excited state
to the ground state for the nucleus only by evaporation of $n$
fragments. The probability $P_{n}$ can be calculated using equation:
\begin{equation}
\label{FCS2}P_{n}=\prod_{i=1}^{n}[1-W_{fis}(U_i,A_i,Z_i)/W_{tot}(U_i,A_i,Z_i)],
\end{equation}
where $W_{fis}$ fission probability (per unit time) in the Bohr and
Wheeler theory of fission \cite{BW39}. It is assumed to be proportional
to the level density $\rho_{fis}(T)$ at the saddle point:
\begin{equation}
\label{FCS3}W_{fis}=\frac{1}{2\pi h \rho(U)}\int_{0}^{U-B_{fis}}
\rho_{fis}(U-B_{fis}-T)dT.
\end{equation}
In Eq. ($\ref{FCS3}$) $B_{fis}$ is the fission barrier height.  $W_{tot}$
is total decay probability (per unit time) of a nucleus:
\begin{equation}
\label{FCS4} W_{tot}=W_{fis}+\sum_{b=1}^{6}W_{b}
\end{equation}
and $W_{b}$ is the probability to evaporate fragment of type $b$.  In
the Weisskopf and Ewing theory of particle evaporation \cite{WE40}:
\begin{equation}
\label{FCS5}W_{b}=\frac{(2s_b + 1)m_b}{\pi^2 h^3 \rho(U)}
\int_{V_b}^{U-Q_b}
\sigma_b(T_b)\rho(E^{*})T_bdT_b,
\end{equation}
where $V_b$ is the Coulomb barrier for fragment $b$ evaporation,
$\sigma_{b}(T_b)$ is the inverse (absorption of fragment $b$ with its kinetic
energy $T_b$) reaction
cross section, $s_b$ and $m_b$ are fragment spin and mass respectively,
$Q_b$ is the fragment $b$ binding energy.
$\rho(E^{*})$ and $\rho(U)$ are level densities of nucleus after, when
excitation energy of nucleus will be $E^{*}=U-Q_b-T_b$, and before
fragment evaporation, respectively.

Eq. ($\ref{FCS1}$) gives us a possibility to calculate numericaly (MC
-method) the so-called fissility of nucleus $P_{fis} =
\sigma_{fis}/\sigma_{in}$ (see e.g. \cite{ICC80}), where $\sigma_{in}$
is the inelastic nuclear reaction cross section and hence the fission
cross section $\sigma_{fis}$ E.g.
\begin{equation}
\label{FCS2} \sigma_{fis}=\sigma_{in}P_{fis}=\sigma_{in}\frac{1}{N_{ch}}
\sum_{n=1}^{N_{ch}}P^{fis}_{n},
\end{equation}
where $N_{ch}$ is the number of fragment evaporation chains, which is
used for averaging.

As one can see from Eq. ($\ref{FCS3}$) the fission barrier height
$B_{fis}$ and the parameter of the level density of a nucleus $a_{fis}$
at saddle point are the basic ingredients of model, which are necessary
for the calculation of fission cross section.


\vsssub
\subsubsection{~$S_{nl}$: Full Boltzmann Integral (WRT)} \label{sec:NL2}
\vsssub

\opthead{NL2}{Exact-NL model}{G. Ph. van Vledder}

\noindent
The second method for calculating the nonlinear interactions in \ws\ is the
so-called Webb-Resio-Tracy method (\xnl), which is based on the original work
on the six-dimensional Boltzmann integral formulation of
\cite{art:Has62,art:Has63a,art:Has63b}, and additional considerations by
\cite{art:Web78}, \cite{rep:TR82} and \cite{art:RP91}.

The Boltzmann integral describes the rate of change of action density of a
particular wavenumber due to resonant interactions between pairs of four
wavenumbers. To interact, these wavenumbers must satisfy the following
resonance conditions

%--------------------------%
% Resonance conditions WRT %
%--------------------------%
% eq:resonance_2

\begin{equation} \left .
\begin{array}{ccc}
  \bk_1 + \bk_2 & = & \bk_3 + \bk_4          \\
  \sigma_1 + \sigma_2  & = & \sigma_3 + \sigma_4
\end{array} \:\:\: \right \rbrace \:\:\: , \label{eq:resonance_2}
\end{equation}

\noindent
which is a more general version of the resonance conditions
(\ref{eq:resonance}). The rate of change of action density $N_1 $ at
wavenumber $\bk_1$ due to all quadruplet interactions involving
$\bk_1$ is given by

\begin{eqnarray}
\frac{{\p N_1 }}{{\p t}} & = & \int\!\!\int\!\!\int G\left( \bk_1
,\bk_2 ,\bk_3 ,\bk_4 \right ) \: \delta \left( \bk_1  + \bk_2  - \bk_3
- \bk_4 \right)
\: \delta \left( {\sigma _1  + \sigma _2  - \sigma _3  - \sigma _4 }
\right) \nonumber \\  & &
\hspace{7mm}\times \:\:\: \left[ {N_1 N_3 \left( {N_4  - N_2 } \right)
+ N_2 N_4 \left(
{N_3  - N_1 } \right)} \right] \: d\bk_2 \: d\bk_3 \: d\bk_4 \:\:\: ,
\end{eqnarray}

\noindent
where the action density $N$ is defined in terms of the wavenumber
vector $\bk$, $N = N(\bk)$. The term $G$ is a complicated coupling
coefficients for which expressions have been given by \cite{art:HH80}.
In the \xnl\ method a number of transformations are
made to remove the delta functions. A key element in the \xnl\ method
is to consider the integration space for each ($\bk_1 ,\bk_3 $)
combination \citep[see][]{art:RP91}

\begin{equation}
{{\partial N_1 } \over {\partial t}} = 2\int T\left( {\bk_1 ,\bk_3 }
\right) \: d\bk_3  \:\:\: ,
\end{equation}

\noindent
in which the function $T$ is given by

\begin{eqnarray}
T \left( \bk_1 ,\bk_3 \right) & = & \int\!\!\int
G\left( \bk_1 ,\bk_2, \bk_3 ,\bk_4 \right) \: \delta \left( \bk_1  +
\bk_2  - \bk_3  - \bk_4 \right) \nonumber \\
& \times & \delta \left( {\sigma _1  + \sigma _2  - \sigma _3  -
\sigma _4 }
\right) \: \theta \left ( \bk_1 , \bk_3 , \bk_4 \right )
\nonumber \\
& \times & \left [ N_1 N_3 \left ( N_4 - N_2 \right ) + N_2 N_4 \left
(
N_3  - N_1 \right ) \right ] \: d\bk_2 \: d\bk_4 \:\:\: ,
\label{eq:WRT_T}
\end{eqnarray}

\noindent
in which

\begin{equation}
\theta \left( {\bk_1 ,\bk_3 ,\bk_4 } \right) = \left\{ {\begin{array}{*{20}c}
   1 & {\rm{when}} & {\left| {\bk_1  - \bk_3 } \right| \leq \left| {\bk_1  - \bk_4 } \right|}  \\
   0 & {\rm{when}} & {\left| {\bk_1  - \bk_3 } \right| > \left| {\bk_1  - \bk_4 } \right|}  \\
 \end{array} } \right.
\end{equation}

The delta functions in Eq. (\ref{eq:WRT_T}) determine a region in wavenumber
space along which the integration should be carried out.  The function
$\theta$ determines a section of the integral which is not defined due to the
assumption that $\bk_1$ is closer to $\bk_3$ than $\bk_2$. The crux of the
Webb method consists of using a local coordinate system along a so-named
locus, that is, the path in $\bk$ space given by the resonance conditions for
a given combination of $\bk_1$ and $\bk_3$. To that end the $(k_{x},k_{y})$
coordinate system is replaced by a $(s,n)$ coordinate system, where $s$ ($n$)
is the tangential (normal) direction along the locus. After some
transformations, the transfer integral can then be written as a closed line
integral along the closed locus

\begin{eqnarray}
T \left( \bk_1 ,\bk_3 \right) & = & \oint G \:
\left | \frac{\p W(s,n)}{\p n}\right | ^{-1}
\: \theta ( \bk_1 , \bk_3 , \bk_4 ) \nonumber \\
& \times & \left [ N_1 N_3 \left ( N_4 - N_2 \right ) + N_2 N_4 \left
(
N_3  - N_1 \right ) \right ] \: d s  \:\:\: , \label{eq:WRT_T2}
\end{eqnarray}

\noindent
in which $G$ is the coupling coefficient and $\left| {\partial W/\partial n}
\right|$ is the gradient term of a function representing the resonance
conditions \citep[see][]{pro:vVl00}. Numerically, the Boltzmann integral is
computed as the finite sum of many line integrals $T$ for all discrete
combinations of $\bk_1$ and $\bk_3$. The line integral (\ref{eq:WRT_T2}) is
solved by dividing the locus in typically 30 pieces, such that the discretized
version is given as:

\begin{equation}
T\left( {\bk_1 ,\bk_3 } \right) \approx \sum_{i=1}^{n_s } G(s_i)
W(s_i) P(s_i) \: \Delta s_i \:\:\: ,
\end{equation}

\noindent
in which $P(s_i)$ is the product term for a given point on the locus,
$n_s$ is the number of segments, and $s_i$ is the discrete coordinate
along the locus. Finally, the rate of change for a given wavenumber
$\bk_1 $ is given by

\begin{equation}
\frac{\p N(\bk_1)}{\p t} \approx \sum_{i_{k3}  = 1}^{n_k }
{\sum_{i_{\theta 3}  = 1}^{n_\theta  } k_3 T(\bk_1,\bk_3) \: \Delta
k_{i_{k3}} \: \Delta \theta_{i_{\theta 3} } }, \label{eq:WRT_numT}
\end{equation}

\noindent
where $n_k$ and $n_\theta$ are the discrete number of wavenumbers and
directions in the computational grid, respectively. Note that although the
spectrum is defined in terms of the vector wavenumber $\bk$, the computational
grid in a wave model is more conveniently defined in terms of the absolute
wavenumber and wave direction ($k,\theta$) to assure directional isotropy of
the calculations. Taking all wavenumbers $\bk_1 $ into account produces the
complete source term due to nonlinear quadruplet wave-wave
interactions. Details of the efficient computation of a locus for a given
combination of the wavenumbers $\bk_1 $ and $\bk_3 $ can be found in
\cite{pro:vVl00,rep:vVl02a,rep:vVl02b}.

It should be noted that these exact interaction calculations are extremely
expensive, typically requiring $10^3$ to $10^4$ times more computational
effort than the \dia. Presently, these calculations can therefore only be made
for highly-idealized test cases involving a limited spatial grid.

The nonlinear interactions according to the \xnl\ method have been implemented
in \ws\ using the portable subroutines developed by \cite{rep:vVl02b}.  In
this implementation, the computational grid of the \xnl\ method is taken
identical to the discrete spectral grid of \ws.  In addition, the \xnl\
routines inherit the power of the parametric spectral tail as in the
\dia. Choosing a higher resolution than the computational grid of \ws\ for
computing the nonlinear interactions is possible in theory, but this does not
improve the results and is therefore not implemented.

Because nonlinear quadruplet wave-wave interactions at high frequencies are
important, it is recommended to choose the maximum frequency of the wave model
about five times the peak frequency of the spectra that are expected to occur
in a wave model run. Note that this is important as the spectral grid
determines the range of integration in Eq.~(\ref{eq:WRT_numT}). The
recommended number of frequencies is about 40, with a frequency increment
factor 1.07. The recommended directional resolution for computing the
nonlinear interactions is about $10^\circ$. For specific purposes other
resolutions may be used, and some testing with other resolutions may be
needed.

An important feature of most algorithms for the evaluation of the Boltzmann
integral is that the integration space can be pre-computed.  This is also the
case for the subroutine version of the \xnl\ method used in \ws. In the
initialization phase of the wave model the integration space, consisting of
the discretized paths of all loci, together with the interaction coefficients
and gradient terms, are computed and stored in a binary data file. For each
water depth such a data file is generated and stored in the current
directory. The names of these data files consist of a keyword, ``quad",
followed by the keyword ``{\it{xxxx}}", with {\it xxxx} the water depth in
meters, or 9999 for deep water.  The extension of the binary data file is
``bqf" (Binary Quadruplet File, BQF). If a BQF file exists, the program checks if
this BQF file has been generated with the proper spectral grid. If this is not
the case, the existing BQF file is overwritten with the correct BQF
file. During a wave model run with various depths, the optimal BQF is used, by
looking at the nearest water depths for which a valid BQF file has been
generated. In addition, the result is rescaled using the ratio of the depth
scaling factors (\ref{eq:snl_shal}) for the target depth and the depth
corresponding to the BQF file.


%! Author = tstreule

\section{Imaging with X-rays}

In $\log E$ vs. $\log\mu$ plot is Compton linear, but Photo decreasing.
\begin{itemize}
    \item \textbf{Photo effect, char. radiation} (Desired):
    Photon gets completely absorbed by throwing an electron out of atom $\to$ damage.
    \textbf{Probability} $\propto \rho\cdot Z^3/E^3$,
    \textcolor{gray}{$\rho$: tissue density}\\
    \textit{Effective in} contrast agent, lead, bone
    \item \textbf{Compton Scattering} ($\to$ resolution loss):
    Photon only gets deflected.
    \textbf{Probability:} $\propto \rho/E$\\
    \textit{Effective in} water, air, soft tissue, bone\\
    Scattering angle $\phi$: \highlight{$\displaystyle \lambda_p - \lambda_{p'} = \frac{h}{m_c \cdot c} (1 - \cos \phi)$}
\end{itemize}
To only have photo effect $\to$ $E\downarrow$. Compromise with thick targets.

\textbf{Spatial resolution}: MTF is indicated in const vs \textbf{l}ine \textbf{p}airs/mm

\textbf{Temporal resolution}: periodic s.t. $\Delta t<1/\textrm{BW}\ped{heartbeat}$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Attenuation coefficients \hfill\textnormal{efficient for small $E$}}
%
\textbf{Mass absorption}: $\mu' = \frac{A\ped{eff}N\ped{A}}{\textrm{atomic weight}} [\unitfrac{cm^2}{g}]$ \hfill area to collide with\\
\textbf{Linear attenuation}: $\mu = \mu' \cdot \rho [\unit{cm^{-1}}]$

\textbf{Beer-Lambert's law}: $I = \int \limits_0^{E\ped{max}} I_0(E) \eu^{-\int_{-\infty}^{\infty} \mu(E,x)\diff x}\diff E$\\
$\mu$ homog.: \highlight{$\displaystyle I = I_0 \eu^{-\mu x}$} \hfill \textbf{Contrast} $\propto (\mu_1 - \mu_0)d = \ln(I_0/I)$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Detector Technology}
%
Analog photographic film: $Ag^+ + Br^- \underbrace{\longrightarrow}_{photon} AgBr$\\
Use fluorescent screen (spacial res.$\uparrow$) $\longrightarrow$ 5x efficiency

\textbf{Anti-scatter grid}:
No grid: $c\ped{scatter} = \frac{(I+I_S) - (I_B + I_S)}{I_B + I_S} = c \cdot \frac{1}{1 + I_S/I_B}$ where $c = \frac{I - I_B}{I_B}$ is contrast with grid

\textbf{Digital Detector}: photofilm, excited el's $\to$ laser $\to$ PMT

\textbf{DSA}: Contrast Agent: Iodine: increases x-ray absorption.


\documentclass{article}

\include{stddefs}
\include{imodefs}

\begin{document}

\chapterno{6}
\chapter{Convex functions}\label{chapter:convexfunctions}

In this chapter we will dive deeper into convex functions.
The main focus will be convex functions defined on intervals (convex subsets) of the
real numbers i.e. convex functions in just one variable. Along the way, differentiability is
introduced along with several classical results. We will sketch surrounding ideas, but skip
proofs of some of the main results.

In this chapter $\log(x)$ denotes the logarithm with base $e$.

\section{Strictly convex functions}

Below we strengthen Definition \ref{Def:convexfunction} of a convex function.

\begin{definition}[emph]
  Let $C \subseteq \RR^d$ be a convex subset.
    A \emph{strictly convex function} is a convex function $f: C\rightarrow \RR$, such that
    $$
    f((1 - t) x + t y) < (1-t) f(x) + t f(y)
    $$
    for every number $t$ with $0< t < 1$ and $x\neq y$.
  \end{definition}

\begin{frameit}
  \begin{figure}\label{fignonstrict}
    \includegraphics{strconvex.png}

Strictly convex function. The line segment between $(x, f(x))$ and $(y, f(y))$ lies \emph{strictly} ($<$) above
the graph of  $f$.
  \end{figure}
  \end{frameit}

  \begin{example}
    Consider the line (function) $f:\RR\rightarrow \RR$ given by
    $$
    f(x) = a x + b
    $$
    for $a, b\in \RR$.
    This function is convex, since we can formally write for every $t\in \RR$:
    \begin{align}\label{notstrconv}
      f((1-t) x + t y) &= a ((1-t) x + t y) + b \\
                       &= a ((1-t) x) + (1-t)b + a (t y) + t b\\
                       &= (1-t)(a x + b) + t(a y + b)\\
                       &= (1-t) f(x) + t f(y).
    \end{align}
    However, the computation in \eqref{notstrconv} also shows why
    there is no chance that $f(x)$ is strictly convex. Intuitively,
    the graph of convex functions need to bend and curve a bit to
    be strictly convex. No lines should occur in their graphs.
  \end{example}

  \beginshex
  Let $f$ be a convex function. Show $f$ is strictly convex if and only if
  $$
  f((1-t) x + t y) = (1-t) f(x) + t f(y)
  $$
  for $0 < t < 1$ implies that $x=y$.
  \endshex

  \beginshex
  Give an example of a non-constant convex function $f:\RR\rightarrow \RR$, which is not strictly convex. Show in
  details that $f(x) = x^2$ is a strictly convex function.

  \begin{hideinbutton}{Hint}
    Look back to the relevant part of Exercise \ref{exconvfct} for
    dealing with $f(x) = x^2$.
    \end{hideinbutton}
  \endshex

\section{Why are convex functions interesting?}

  We begin this section by giving the following result without proof.

  \begin{theorem}[emph]\label{opencvxcont}
    A convex function defined on an open convex subset is continuous.
  \end{theorem}

  \beginshex
  Give an example showing that Theorem \ref{opencvxcont} is not true if the convex function
  is defined on a closed convex subset.

  \begin{hideinbutton}{Hint}
    Try to come up with an example like $f: [0, 1] \rightarrow \RR$. Look at
    the end point $0$.

    \begin{hideinbutton}{Hint}
      Well, try out
      $$
      f(x) =
      \begin{cases}
        1&\text{if } x = 0\\
        x&\text{if } x > 0
      \end{cases}
      $$
    \end{hideinbutton}
    \end{hideinbutton}
  \endshex

Let us now define precisely what is meant by a local vs a global minimum
for a function.

\begin{definition}[emph]\label{Def:localglobalmin}
Let $f: S\rightarrow \RR$ be a function, where $S\subseteq\RR^d$ is an
arbitrary subset (not necessarily convex, open or closed). Then
$x_0\in S$ is called a \emph{local minimum} for $f$ if
$$
f(x_0)\leq f(x)
$$
for every $x\in S$, which is sufficiently close to $x_0$. Being
sufficiently close to means that $x\in S$ satisfies
$$
| x - x_0 | < \epsilon
$$
for some fixed $\epsilon > 0$.

In a much stronger notion, $x_0\in S$ is called a \emph{global minimum} if
$$
f(x_0) \leq f(x)
$$
for every $x\in S$ (not just locally).
\end{definition}

\begin{frameit}
\begin{figure}
\includegraphics{localglobalmin.png}

Graph of function defined on an interval. This function has a local minimum, which is not
a global minimum.
\end{figure}
\end{frameit}

\beginshex
Give an example of a local minimum that is not a global minimum for a precisely specified function.
Also give
an example of a global minimum, which is not uniquely defined (again for a precisely specified function).
Uniquely defined means that there is precisely one $x_0$, such that $f(x_0)$ is minimal.
\endshex

We might as well have talked about maximum instead of minimum above.

\beginshex
Reformulate Definition \ref{Def:localglobalmin} in order to define
a local and a global maximum.
\endshex

A \emph{local extremum} is a point $x_0\in S$, which is either a
local minimum or a local maximum.

Convex functions $f: C\rightarrow \RR$ are interesting, because of the local
nature of the minimization problem

\begin{align}\label{cminpr}
    &\text{Minimize} &f(x)\\
    &\text{with constraint}\\
    &&x\in C
\end{align}

If you run into a local minimum in \eqref{cminpr},
then you are sure that it also is a global minimum! This is the
content of the result below.

\begin{theorem}[emph]\label{Thm:convexoptnice}
  Let $f: C\rightarrow \RR$ be a convex function defined on a convex subset $C\subseteq \RR^d$. If $x_0\in C$ is a
  local minimum, then $x_0$ is a global minimum. If $f$ is strictly
  convex, then a global minimum for $f$ is unique.
\end{theorem}
\begin{proof}[showhide]
 By the definition of local minimum in Definition \ref{Def:localglobalmin}, there exists $\epsilon > 0$, such that $f(x_0)\leq
  f(x)$, when $x \in C$ and $\abs{ x - x_0 }< \epsilon$. Suppose that
  $x_0$ is not a global minimum. Then there exists $x_1\in C$ with
  $f(x_1) < f(x_0)$. Consider the point
  \begin{equation*}
    x_t = (1 - t) x_0 + t x_1\in C,
  \end{equation*}
  where $0 < t < 1$. Then
  \begin{equation*}
    f(x_t) \leq (1-t) f(x_0) + t f(x_1) < (1-t) f(x_0) + t f(x_0) =
    f(x_0).
  \end{equation*}
  Since $\abs{ x_t - x_0 } = t \abs{ x_1 - x_0 }$, we can choose $t >
  0$ sufficiently small such that $\abs{ x_t - x_0 } < \epsilon$
  implying $f(x_0)\leq f(x_t)$, since $x_0$ is a local minimum. This
  contradicts that $f(x_t) < f(x_0)$ for every $0 < t < 1$.  Let $f$
  be strictly convex and let $x_0$ be a global minimum for $f$. If
  $x_1\in C$, $x_1\neq x_0$ and $f(x_1) = f(x_0)$, then
  \begin{equation*}
    f((1-\lambda) x_0 + \lambda x_1) < (1-\lambda) f(x_0) + \lambda f(x_1)
    = f(x_0)
  \end{equation*}
  for $0 < \lambda < 1$. This would contradict that global minimality
  of $x_0$, since $x_0\neq (1-\lambda)x_0 + \lambda x_1\in C$ for $0<
  \lambda < 1$.
\end{proof}

The following little result turns out to be very useful and
also very intuitive and drawable! It is a key component
in characterizing convex differentiable
functions $f(x)$ in terms of $f''(x)$. We will not give the proof here.

\begin{lemma}[emph]\label{littleconvresult}
  Let $f:[a,b]\rightarrow \RR$ be a convex function. Then
  \begin{equation*}
    \frac{f(x) - f(a)}{x - a} \leq
    \frac{f(b)-f(a)}{b - a} \leq \frac{f(b)-f(x)}{b - x}
  \end{equation*}
  for $a < x < b$.
\end{lemma}

\begin{frameit}
\begin{figure}

\includegraphics{geomlemmaconv.png}

The result in Lemma \ref{littleconvresult} is depicted above. A formal proof can be
given from first principles only using Definition \ref{Def:convexfunction}.
\end{figure}
\end{frameit}

\section{Differentiable functions}

To appreciate the depth of the notion of differentiability, you should
read the story (joke, actually) in the second paragraph of \url{section
  8-2}{http://www.feynmanlectures.caltech.edu/I_08.html} in volume I
of the famous \url{Feynman Lectures on Physics}{http://www.feynmanlectures.caltech.edu/}. Below is a
  photograph of the \url{master explainer}{https://en.wikipedia.org/wiki/Richard_Feynman} in action.

\includegraphics{Feynman.jpeg}

\subsection{Definition}

Let $f:(a, b)\rightarrow \RR$ be a function defined on the open interval $(a, b)\subset \RR$.
The notion of $f$ being differentiable at a point $x_0\in (a, b)$
can be glanced from the drawing below

\includegraphics{limitslope.png}

where we informally let $x$ approach $x_0$ and look at the limiting
value of the slope. Newton used to say many hundred years ago, that
the derivative of $f$ at $x_0$ is the value of this slope just before
$x$ becomes $x_0$. In modern day mathematical (obfuscated?) parlance, this
gets translated into the following.

\begin{quote}
  The function $f$ is differentiable at $x_0$ if there exists a number
$c\in \RR$, such that for every $\epsilon > 0$ there exists $\delta$
with
\begin{equation}\label{defdiff0}
  \left| \frac{f(x) - f(x_0)}{x-x_0} - c \right| < \epsilon
\end{equation}
for every $x$ satisfying $|x-x_0 |<\delta$.
\end{quote}

We will use a somewhat more operational definition below in terms
of continous functions $\epsilon$ defined around $0$ with
$\epsilon(0) = 0$. This looks difficult, but it is
actually a clever way of approaching differentiability (and more
in the spirit of Newton).

\begin{definition}[emph]\label{defdiff}
The function $f$ is differentiable at $x_0$ if
for some $\delta>0$, there exists a function $\epsilon: (-\delta, \delta)
\rightarrow \RR$ continuous at $0$ with $\epsilon(0) = 0$
and a number $c$, such that
\begin{equation}\label{operational}
f(x_0 + h) - f(x_0) = c h + \epsilon(h) h
\end{equation}
for $x_0 + h\in (a, b)$ and $h\in (-\delta, \delta)$. The number
$c$ is denoted $f'(x_0)$ and called \emph{the derivative} of
$f$ at $x_0$; $f$ is called \emph{differentiable} if
its derivative exists at every $x_0\in (a, b)$.
\end{definition}

\begin{remark}[emph]
  If a function $f:(a, b)\rightarrow \RR$ is
  differentiable, we get a new function
  $f':(a, b)\rightarrow \RR$ giving the (first) derivative at
  a point as output. We may ask again if this function
  is differentiable. If this is so, we may define a
  function $f'':(a, b)\rightarrow \RR$ given by
  $f''(x) = (f')'(x)$ called the second derivative. This
  procedure may be continued. We use the notation
  $f^{(n)}$ for the $n$-th derivative.
\end{remark}

\begin{example}
Let us apply Definition \ref{defdiff} to the function $f(x) = x^2$ at
the point $x_0$. Here
$$
(x_0 + h)^2 - x_0^2 = 2 x_0 h + h^2.
$$
Here you immediately see that $f'(x_0) = 2 x_0$ with
$\epsilon(h) = h$.
\end{example}

\beginshex
Use Definition \ref{defdiff} to formally show that $f'(x) = 3 x^2$
if $f(x) = x^3$.
\endshex

\begin{remark}[emph]

In operating with differentiable functions you are supposed to draw on
your previous knowledge. I have summarized some of this knowledge below (even though we will give hints below as how to prove some of the rules).

  \begin{enumerate}
\item
  If $f(x) = a g(x)$, where $a\in \RR$, then
  $$f'(x) = a g'(x)$$.
\item
If $f(x) = x^n$, where $n \in \NN$, then $$f'(x) = n x^{n-1}.$$
\item
If $f(x) = e^x$, then  $$f'(x) = f(x) = e^x.$$
\item
If $f(x) = \log(x)$, then $$f'(x) = 1/x.$$
\item
If $f(x) = \sin(x)$, then $$f'(x) = \cos(x).$$
\item
If $f(x) = \cos(x)$, then $$f'(x) = -\sin(x).$$
\item
  If $f(x)$ and $g(x)$ are differentiable functions, then the derivative of their product is
  $$
  (f g)'(x) = f'(x) g(x) + f(x) g'(x).
  $$
\item
   If $f(x)$ and $g(x)$ are differentiable functions, then the derivative of their quotient is
  $$
  \left(\frac{f(x)}{g(x)}\right)' = \frac{f'(x) g(x) - f(x) g'(x)}{g(x)^2}.
  $$
\item
     If $f(x)$ and $g(x)$ are composable differentiable functions, then the derivative of their composite is
  $$
  (f\circ g)'(x) = f'(g(x)) g'(x).
  $$
\end{enumerate}
\end{remark}

\beginshex
Suppose that $f(x) = \sin(x)$. What is
$$
f^{(17)}(x)?
$$
\endshex

\subsection{Recall the derivative of a product?}

From high school you know that the derivative of a product of two functions $f$ and $g$
is given by the formula

\begin{equation}\label{hsproduct}
(f g)'(x) = f'(x) g(x) + f(x) g'(x).
\end{equation}

We can use the $\epsilon$-definition \eqref{operational} to derive the product rule
in \eqref{hsproduct}. The computation below is a bit cumbersome, but actually quite
doable. We assume to begin with that $f$ and $g$ are differentiable at $x_0$
according to \eqref{operational} i.e.,

\begin{align*}
  f(x_0 + h) &= f(x_0) + f'(x_0)h + \epsilon_f(h) h\\
  g(x_0 + h) &= g(x_0) + g'(x_0)h + \epsilon_g(h) h.\\
\end{align*}
Then we start the computation:
\begin{align}\label{prrulecomp}
&(f g)(x_0 + h) = f(x_0 + h) g(x_0 + h) =\\
  &(f(x_0) + f'(x_0)h + \epsilon_f(h) h)\,\, (g(x_0) + g'(x_0) h + \epsilon_g(h) h) =\\
  &f(x_0) g(x_0) + (f'(x_0) g(x_0)  + f(x_0) g'(x_0)) h + \epsilon(h) h,
\end{align}
where the function
\begin{equation}\label{bigcompdiffproduct}
\epsilon(h) = f(x_0) \epsilon_g(h) + f'(x_0) g'(x_0) h + f'(x_0) \epsilon_g(h) h + \epsilon_f(h) g'(x_0) +
\epsilon_f(h) g'(x_0) h + \epsilon_f(h) \epsilon_g(h) h
\end{equation}
is seen to be continuous at $h=0$ with $\epsilon(0) = 0$.
The end result of this computation shows that $f g$ is differentiable at $x_0$ with
\begin{equation}\label{prrule}
(f g)'(x_0) = f'(x_0) g(x_0) + f(x_0) g'(x_0)
\end{equation}
again according to \eqref{operational}.

\beginshex
Show that the $\epsilon$ function defined in \eqref{bigcompdiffproduct} satisfies the
relevant conditions in Definition \ref{defdiff}.
\endshex

The formula for the derivative of a fraction i.e.,
$$
\left(\frac{f(x)}{g(x)}\right)' = \frac{f'(x) g(x) - f(x) g'(x)}{g(x)^2}
$$
can be derived using a neat little trick. This is the topic of the following exercise.

\beginshex
Show how the product rule may be used to derive the rule for finding the derivative of
a fraction:
$$
\left(\frac{f}{g}\right)'(x_0) = \dfrac{f'(x_0) g(x_0) - f(x_0) g'(x_0)}{g(x_0)^2}.
$$

\begin{hideinbutton}{Hint}
  $$
  f'(x) = \left(g(x) \left(\frac{f(x)}{g(x)}\right)\right)'.
  $$
  \end{hideinbutton}
\endshex
\subsection{Recall the one variable chain rule?}

The formula for the derivative of a composite function is given by
$$
(f\circ g)'(x_0) = f'(g(x_0)) g'(x_0),
$$
where $g(x_0)$ is in the domain of $f$. Let us see how \eqref{operational}
applies in showing this.

Suppose that $f$ is differentiable at $g(x_0)$ and $g$ is
differentiable at $x_0$, then we can mess around a bit
with the $\epsilon$-functions for $f$ and $g$ for
the composite function $f(g(x))$ around $x_0$:
\begin{align*}
f(g(x_0 + h)) &= f(g(x_0) + g'(x_0) h + h \epsilon_g(h))\\
  &= f(g(x_0)) + f'(g(x_0)) g'(x_0) h + \epsilon(h) h,
\end{align*}
where (take a deep breath)
$$
\epsilon(h) = f'(g(x_0)) \epsilon_g(h) + \epsilon_f(g'(x_0) h + \epsilon_g(h)h) g'(x_0).
$$
Here $\epsilon$ is seen to be continuous at $0$ with $\epsilon(0)=0$ i.e.,
the composition $f(g(x))$ is differentiable at $x_0$ with
derivative
\begin{equation}\label{formulacompdiff}
(f\circ g)'(x_0) = f'(g(x_0)) g'(x_0).
\end{equation}

The formula \eqref{formulacompdiff} is extremely important and useful. We give some applications in
the exercises below.

\beginshex
Compute the derivative of the function $f: (0, \pi) \rightarrow \RR$ given by
$$
f(x) = \dfrac{1}{\sqrt{\sin(x)}}
$$
using only paper and pencil! You can check your result afterwards using a computer.
\endshex

\beginshex
Suppose that $g$ and $f$ are inverse functions i.e.,
$$
f(g(x)) = x\qquad \text{and} \qquad g( f(x)) = x.
$$
If you know the derivative of $f$, how can you use the chain rule
to get the derivative of $g$? Illustrate with examples like
$f(x) = x^2$ and $g(x) = \sqrt{x}$, $f(x) = e^x$ and $g(x) = \log(x)$.
\endshex

\beginshex
Suppose that $f:\RR\rightarrow \RR$ is a convex function. We know that
$f$ is continuous, but is $f$ differentiable at every point $x_0\in \RR$?

\begin{hideinbutton}{Hint}
  Nope. This is wrong. Come up with a convex function $f$ and a point $x_0$,
  such that $f$ is not differentiable at $x_0$.
\end{hideinbutton}
\endshex

\subsection{The Newton-Raphson method for finding roots}

We begin this section with a surprising example.

\begin{example}
Suppose that $a> 0$ and we wish to compute $\sqrt{a}$. To do this we may focus on the
quadratic equation $f(x) = x^2 - a = 0$ and attempt to compute an approximate value $x_0\geq 0$,
such that $f(x_0)$ is close to $0$. Let me at this point disclose that there is
a very effective iterative scheme for doing this. You start by putting $x_0 = a$ and then iterate
using the formula
\begin{equation}\label{sqnewton}
x_{i+1} = \frac{1}{2}\left(x_i + \frac{a}{x_i}\right)
\end{equation}
to get better and better approximations $x_0, x_1, x_2, \dots$ to $\sqrt{a}$.

\begin{openeyes}
The formula in \eqref{sqnewton} is derived from
$$
x_{i+1} = x_i - \frac{f(x_i)}{f'(x_i)},
$$
where $f(x) = x^2 - a$.
\end{openeyes}

You can try out \eqref{sqnewton}
below.

\begin{sage}
a = 2.0
# Note a = 2.0 instead of a = 2. We need Sage to output decimal numbers - not fractions.
numberOfIter = 10

def onestep(x):
  return (x + a/x)/2

z = a
for i in range(numberOfIter):
  z = onestep(z)
  print(z)
\end{sage}
\end{example}

I have been in complete awe of the \url{Newton-Raphson method}{https://en.wikipedia.org/wiki/Newton\%27s_method} since my early youth. It is
an algorithm, where the notion of differentiability really shines.

The method
comes from Definition \ref{defdiff} with $h = x - x_0$:
we are assuming that $x_0$ is very close to $x$, where $f(x) = 0$. Then
$$
f(x) - f(x_0) = f'(x_0) (x- x_0) + \text{a very small number}.
$$
Ignoring the very small number and solving this equation for $x$ we get
$$
x = x_0 - \frac{f(x_0)}{f'(x_0)}.
$$

In the Sage window below, I have entered the algorithm starting
in $x_0 = 0$ running ten iterations for finding a zero for
$f(x) = \cos(x) - x$.

\begin{hideinbutton}{Graph}
  \begin{sage}
    plot(cos(x) - x, (x, 0, 2*pi))
  \end{sage}
\end{hideinbutton}

\begin{sage}
x = var('x')
f(x) = cos(x) - x
df = diff(f, x)
NR(x) = x - f(x)/df(x)

x0 = 0 # initial value
n = 10 # number of iterations

for i in range(n):
  print( x0)
  x0 = N(NR(x0), digits = 10)
\end{sage}

\beginshex
Give an example, where the Newton-Raphson method cycles between points
and never finds the desired zero. Perhaps a drawing will help here.
\endshex

The Newton-Raphson converges rapidly in most cases. Of course, it
breaks down violently if it runs into a \emph{critical point} i.e.,
a point $x$, such that $f'(x) = 0$.

William Cook has made some nice Sage code available for
\url{experimentation}{https://billcookmath.com/sage/calculus/Newtons_method.html} with Newton's method.

\begin{example}
  The formula (see button in Example \ref{examplegeomseries})
  for the (monthly) payment $Y$ on a (car) loan over $N$ payments with
  a down payment of $P$ and an interest rate of $r$ (per payment or term) is given by the formula
    $$
    Y = \frac{r P}{1 - \left(\frac{1}{1+r}\right)^N}.
    $$
    There is no explicit formula for calculating $r$ given $Y, P$ and $N$. Here
    the Newton-Raphson method is invaluable for estimating $r$ by approximating a
    zero for the function
    $$
    r(x) = Y - \frac{x P}{1 - \left(\frac{1}{1+x}\right)^N}.
    $$
\end{example}

\beginshex
Your bank promises you a loan of $1.000.000$ DKK with yearly payments
of $45.000$ DKK over $30$ years. At the same time it claims that
its interest rate is very favorable at only $1.0$\%. Here the bank is
wrong! What is the
real interest rate?
How much money do you save (compared to the original offer from the bank) if you insist that the bank offers you
the promised interest rate of $1.0$\%?
\endshex

\subsection{Critical points and extrema}

\begin{definition}[emph]
  A critical point for a differentiable function $f:(a, b)\rightarrow \RR$ is
  a point $x_0\in (a, b)$ with
  $$
  f'(x_0) = 0.
  $$
\end{definition}

The crucial result here is the following. It seems to date back to Fermat (see \url{Fermat's theorem}{https://en.wikipedia.org/wiki/Fermat\%27s_theorem_(stationary_points)}).

\begin{lemma}[emph]\label{lemmacritone}
  Let $f : (a, b)\rightarrow \RR$ be a differentiable function. If
  $x_0$ is a local extremum for $f$, then $x_0$ is critical point i.e., $f'(x_0) = 0$.
\end{lemma}
\begin{proof}[showhide]
 Suppose that $\xi$ is a local maximum and that
  \begin{equation*}
    f(\xi + h) - f(\xi) = f'(\xi) h + \epsilon(h) h
  \end{equation*}
  according to \eqref{operational}. If $f'(\xi)>0$, then we can choose
  $\delta >0$ sufficiently small, such that $\abs{ \epsilon(h) } <
  f'(x_0)$ if $0\leq h < \delta$, since $\epsilon(0) = 0$ and
  $\epsilon $ is continuous in $0$. Therefore
  \begin{equation*}
    f(\xi +  h) - f(\xi) = (f'(\xi) + \epsilon(h)) h > 0,
  \end{equation*}
  contradicting that $\xi$ is a local maximum.  The proof is similar
  for $f'(\xi) < 0$ and if $\xi$ is a local minimum.
\end{proof}

\beginshex
Is the converse of the above lemma true i.e., if $f'(x_0) = 0$ is
$x_0$ a local extremum?
\endshex

Theorem \ref{thmmeanvalue} below is called the mean value theorem. It is a consequence of Lemma \ref{lemmacritone}
and the extremely important Theorem \ref{thmcontcomp} about continuous functions
on compact subsets attaining their maxima and minima!

\begin{frameit}
\begin{theorem}\label{thmmeanvalue}
  Let $f:[a, b]\rightarrow \RR$ be continuous and differentiable
  on $(a, b)$. Then there exists $x_0\in (a, b)$ such that
  \begin{equation*}
    f'(x_0) = \frac{f(b) - f(a)}{b - a}.\label{xiaim}
  \end{equation*}
\end{theorem}
\end{frameit}

\subsection{Increasing functions}

The definition below is much simpler than the definition of differentiability.

\begin{definition}[emph]
A function $f:S\rightarrow \RR$ with $S\subseteq \RR$ is called
  \emph{increasing} if
  \begin{equation*}
    x\leq y\Rightarrow f(x) \leq f(y)
  \end{equation*}
  and \emph{strictly increasing} if \index{strictly increasing function}
  \begin{equation*}
    x< y\Rightarrow f(x) < f(y)
  \end{equation*}
  for $x, y\in S$.
\end{definition}

\beginshex
Give an example of an increasing function.
Give an example of an increasing function that is not strictly increasing.
\endshex

The following very important result is a consequence of Theorem \ref{thmmeanvalue}. You probably already know this result from your previous (danish) education (monotoniforhold!).

\begin{proposition}[emph]
  Let $f : (a, b)\rightarrow \RR$ be a differentiable function.
  Then
  $f$ is increasing if and only if $f'(x)\geq 0$ for every $x\in (a,
  b)$.  If $f'(x)> 0$ for every $x\in (a, b)$, then $f$ is strictly
  increasing.
\end{proposition}

\beginshex
Show that $f(x) = x^3$ is strictly increasing i.e.,
$$
x < y \implies x^3 < y^3.
$$
\begin{hideinbutton}{Hint}
$$
y^3 - x^3 = (y - x) (y^2 + x y + x^2),
$$
but why is $y^2 + x y + x^2$ always $> 0$ except when $x = y = 0$?
\end{hideinbutton}
\endshex

\beginshex
Suppose that $f: [a, b] \rightarrow \RR$ is a continuous function, such that
$f$ is differentiable on the open interval $(a, b)$. Is $f$ increasing
on $[a, b]$ if $f'(x)\geq 0$ for every $x\in (a, b)$?
\endshex

\beginshex
Is it possible for a strictly increasing function $f: \RR \rightarrow \RR$ to be bounded i.e., does
there exist a (positive) number $M$, such that $|f(x)| \leq M$ for every $x\in \RR$?

\begin{hideinbutton}{Hint}
  Have a look at
  $$
  f(x) = \dfrac{1}{1 + e^{-x}}.
  $$

\end{hideinbutton}
\endshex

\section{Taylor polynomials}\label{section:Taylor}

If $x_0$ is a critical point for $f$ we cannot conclude that $x_0$ is a local extremum. We know
that $f'(x_0)=0$ and we can get more information out of $f$ by exploring the signs of
$$
f''(x_0), f'''(x_0), \dots
$$

Suppose that
$$
f(x) = a_0 + a_1 x + a_2 x^2 + \cdots + a_n x^n
$$
is a polynomial, then
\begin{equation}\label{taylor}
f(x) = f(0) + f'(0) x + \frac{f''(0)}{2} x^2 + \cdots + \frac{f^{(n)}(0)}{n!} x^n.
\end{equation}

For nice functions like $f(x) = e^x$ we can play this game ad infinitum. In fact in this
way we get the beautiful infinite series
$$
e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \cdots + \frac{x^n}{n!} + \cdots.
$$

If $f$ is an $n$ times differentiable function defined at $0$, we call
the polynomial in \eqref{taylor} the Taylor polynomial about the point $0$ of degree $n$ associated
with the $f$. Similarly, one may also define the Taylor polynomial of order $n$ about a point $a$ by
$$
f(a) + f'(a) (x-a) + \frac{f''(a)}{2} (x-a)^2 + \cdots + \frac{f^{(n)}(a)}{n!} (x-a)^n.
$$
Taylor polynomials can be used to approximate more complicated functions such as
$\cos(x)$ and $\sin (x)$ with a well defined error term. This is cool classical
mathematics. Unfortunately we do not have time to go deeper into
\url{Taylor's theorem}{https://en.wikipedia.org/wiki/Taylor\%27s_theorem}, which states this
in precise terms.

\beginshex
Compute the Taylor polynomial for $f(x) = \cos(x)$ up to degree $10$.
\endshex

\beginshex
Suppose you have a number $i$ that satisfies
$$
i^2 = -1.
$$
Can you make sense of the formula
$$
e^{i x} = \cos(x) + i \sin(x)
$$
using Taylor polynomials?
\endshex

In the context of optimization, the following
result becomes important. We will not give the proof, but only notice that Theorem
\ref{thmmeanvalue} also here plays an important role.

\begin{theorem}[emph]\label{derconv}
  Let $x_0$ be a critical point of an $n+1$ times differentiable
  function $f:(a, b)\rightarrow \RR$, such that $f^{(n+1)}$
  is a continuous function,
  \begin{align*}
    f''(x_0) &= 0\\
    f'''(x_0) &= 0\\
&\vdots\\
   f^{(n-1)}(x_0) &= 0
  \end{align*}
  and $f^{(n)}(x_0)\neq 0$. If $n$ is even, then $x_0$ is a local
  minimum if $f^{(n)}(x_0) > 0$ and a local maximum if
  $f^{(n)}(x_0)<0$. If $n$ is odd, then $x_0$ is not a local extremum.
\end{theorem}

\begin{example}\label{parabolalegs}
  Let us apply Theorem \ref{derconv} to the function
  $$
  f(x) = a x^2 + b x + c,
  $$
  where $a\neq 0$. Here $f'(x) = 2 a x + b$ and
  $$
  x_0 = - \frac{b}{2 a}
  $$
  is a critical point (why?). Since
  $$
  f''(x_0) = 2 a,
  $$
  we see that $x_0$ is a local minimum if $a > 0$ and a local maximum if
  $a < 0$.
\end{example}

\beginshex
Have you seen Example \ref{parabolalegs} elsewhere, perhaps in a more
geometric setting? What type of curve is the graph of $f(x)$? Here you may consult
your previous mathematical knowledge.

What is the outcome, when you apply Theorem \ref{derconv} to the
function $f(x) = x^3$ at $x_0 = 0$?
\endshex

\beginshex\label{exderconv}
Show that $x_0=0$ is a critical point of the function $f: (-\frac{1}{2}, \infty)\rightarrow \RR$
defined by
$$
f(x) = e^x + \log(1 + 2 x) - 3 x.
$$
Use Theorem \ref{derconv} in deciding if it is a local
maximum or minimum or neither.

\endshex

\section{Differentiable convex functions}

The following theorem is proved using Lemma \ref{littleconvresult} and
Theorem \ref{thmmeanvalue}. It immediately implies Corollary \ref{corfdp},
which is the result mostly used.

\begin{theorem}\label{fdp}
  Let $f:(a, b)\rightarrow \RR$ be a
  differentiable function. Then $f$ is convex if and only if $f'$ is
  increasing.  If $f'$ is strictly increasing, then $f$ is strictly
  convex.
\end{theorem}

Theorem \ref{fdp} leads to the following all important result.

\begin{corollary}[emph]\label{corfdp}
  Let $f:(a, b)\rightarrow \RR$ be a twice differentiable
  function. Then $f$ is convex if and only if $f''(x)\geq 0$ for every
  $x\in (a, b)$. If $f''(x) > 0$ for every $x\in (a, b)$, then $f$ is
  strictly convex.
\end{corollary}

\begin{frameit}
\begin{remark}
Wait! Stop! Why did I not write $f''(x) > 0$ if and only if
$f$ is strictly convex?
\end{remark}
\end{frameit}

\beginshex
Can you deduce from Corollary \ref{corfdp} that the function
$f: \RR \rightarrow \RR$ given by $f(x) = x^4$ is a
strictly convex function?
\endshex

\beginshex
Show that $f(x) = e^x$ is a strictly convex function $f: \RR\rightarrow \RR$.

Show that $f(x) = -\log(x)$ is a strictly convex function
$f: (0, \infty) \rightarrow \RR$.
\endshex

\beginshex
Show that $f: \{x\in \RR \mid x \geq 0\}\rightarrow \RR$ given by
$$
f(x) = -\sqrt{x}
$$
is a strictly convex function.
\endshex

Another nice application of Lemma \ref{littleconvresult} (and Theorem \ref{fdp}) is the following.

\begin{theorem}\label{fp}
  Let $f:(a, b)\rightarrow \RR$ be a differentiable function. Then $f$
  is convex if and only if
  \begin{equation*}
    f(y) \geq f(x) + f'(x)(y-x)
  \end{equation*}
  for every $x, y\in (a, b)$.
\end{theorem}

\beginshex
Suppose that $f:(a, b)\rightarrow \RR$ is a differentiable
convex function and $x_0\in (a, b)$ is a critical point for
$f$. What can you say about $x_0$ using Theorem \ref{fp}?
\endshex

\end{document}


\subsection{Functional integration}

Integrate over possible functions?