\section{Error Analysis}

\begin{slide}{Fitting Strategies in IFEFFIT }

    The general idea is to find a {\RedEmph{Model}} that best matches the {\BlueEmph{Data}}.

    \vmm

    We use $\bchi^2$ (don't confuse with EXAFS $\bchi$!!) to describe our
    fit:

    \[ \displaystyle{\mathbf{
        \chi^2  =   \sum_i^{N_{\rm fit}} \frac{[\chi_i^{\rm data} - \chi_i^{\rm
            model}({x})]^2}{\epsilon^2}
      }} \]

    \vmm $\mathbf{N_{\rm fit}} = $ number of points to fit,
    $\mathbf{x} = $  the set of variables, and $\mathbf{\epsilon} =$ the estimated noise
    level in the data.

    \vmm \hrule \vmm

    But we usually oversample our data (that is, $\mathbf{N_{\rm fit} >
      N_{\rm idp} } $), so we renormalize this to $ \mathbf{N_{\rm idp}}$,
    and assert that $\epsilon$ is a constant:

    \begin{center} \highlightbox{
        $ \displaystyle{\mathbf{
            \chi^2  =  \frac{ N_{\rm idp}}{\epsilon^2 N_{\rm fit}}
            \sum_i^{N_{\rm fit}} [\chi_i^{\rm data} - \chi_i^{\rm model}({x})]^2
          }}
        $
      }
    \end{center}

    \vmm

    \begin{center}
       {\RedEmph{ The Best Fit is that with lowest $\bchi^2$. }}
    \end{center}

    \vmm \hrule \vmm

    \vmm

    The fitting algorithm finds the set of variables $\mathbf{x} $ which
    minimizes $[\mathbf{\chi_i^{\rm data} - \chi_i^{\rm model}({x})}]$ in
    this ``least-squares'' sense.

\vfill
\end{slide}

\begin{slide}{Other Fitting Statistics}
\small

    Other ``goodness-of-fit statistics'':
    \begin{description}
    \item[{\Red{chi-square}}] As before: \vmm

      $ \displaystyle{\mathbf{
          \chi^2  =  \frac{ N_{\rm idp}}{\epsilon^2 N_{\rm fit}}
          \sum_i^{N_{\rm fit}} [\chi_i^{\rm data} - \chi_i^{\rm model}({x})]^2
        }}
      $

    \item[{\Red{reduced chi-square}}] scale $\mathbf{N_{\rm varys}}$ by the
      ``degrees of freedom'' : \vmm

      $ \chi^2_\nu =  \chi^2 / (N_{\rm idp}-N_{\rm varys}) $

      \vmm

      For a ``Good Fit'', $\chi^2_\nu$ should be $\sim$ 1 (This
      {\RedEmph{never}} happens!!).

    \item[{\Red{R-factor}}] $\mathbf{\cal{R}}$  gives a ``fractional misfit'': \vmm

      $ \displaystyle{\mathbf{
          {\cal{R}} =
          {\sum_i^{N_{\rm fit}}[\chi_i^{\rm data} - \chi_i^{\rm model}({x})]^2 }
          /
           { \displaystyle{\sum_i^{N_{\rm fit}} [{\chi_i^{\rm data}}]^2}}
        }}
      $

      \vmm $\mathbf{\cal{R}}$ is useful because it is not scaled to the data
    uncertainty $\epsilon$.

    \end{description}
\vfill
\end{slide}

\begin{slide}{Fitting in $R$-space, $k$-space, etc.}

    The $\bchi^2$ definition didn't say anything about what our data
    {\Blue{$\chi_i^{\rm data}$}} is.

    \vmm  We usually fit in $R$-space, because
    we can throw out unwanted ``shells'':

    \vmm \vmm \vmm

    {
      \begin{tabular}{ll}
        \hspace{-10mm} \begin{minipage}{54mm}{\wgraph{53mm}{reduction/chik}}  \end{minipage} &
        \hspace{-5mm}  \begin{minipage}{54mm}{\wgraph{53mm}{reduction/chir_win}}\end{minipage}
    \end{tabular}
    }

    \vmm
    Fitting in $R$-space gives more meaningful fit statistics when we know that
    we're not fitting all the spectral features.

    \vmm
    {\RedEmph{AND:}} We can also have the data {\Blue{$\chi_i^{\rm data}$}}
    extend over {\Red{multiple data sets}}, {\Red{multiple $k$-weightings}},
    etc, as long as we generate {\Red{$\mathbf{\chi_i^{\rm model}(x)}$}} to
    match these data.

\vfill
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}{$\mathbf\epsilon$: The Noise Levels in Data }

    Here are some typical EXAFS spectra (both transmission, 1 sec/point),
    and their estimated noise $\epsilon$ in $\bchi(k)$:

    \vmm

      \begin{tabular}{lcl}\setlength{\baselineskip}{2pt}
        0.2 mM Zn nitrate solution & & Cu foil Room Temperature   \\
        \hspace{-7mm} \wgraph{48mm}{errors/noise_data01} & \hspace{2mm} &
        \hspace{-5mm} \wgraph{48mm}{errors/noise_data02} \\
        $\epsilon \approx 6.6 \times 10^{-4}$ ``Normal Data''& &
        $\epsilon \approx 1.6 \times 10^{-4}$ ``Good Data''\\
      \end{tabular}

      \vmm

      For the Zn solution, this noise level is consistent with counting
      statistics for ``Number of Photons in Ion Chambers''!

\vfill
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}{$\epsilon$: Noise Levels in Data}
\small
\hspace{-.1mm}
\begin{center}
  \begin{minipage}{95mm}\setlength{\baselineskip}{10pt}

    $\epsilon$ is estimated from the high-$R$ (15
    to 25 \AA) components of the data:

    \vmm
      \begin{tabular}{lcl}
        $|\chi(R)|$ &    &  $ \hspace{4mm} \log_{10}(|\chi(R)|)$ \\
        \vspace{-3mm}\hspace{-10mm} \wgraph{50mm}{errors/znnoise01} &      &
        \hspace{-5mm}  \wgraph{50mm}{errors/znnoise02} \\
      \end{tabular}

      \begin{itemize}
        \item The Zn data really shows ``white noise''.

        \item The Cu data has signal well above the noise level past 10\AA!

      \item Using the range $R=[15,25]\rm\, \AA$ may
        {\RedEmph{overestimate}} $\epsilon$ for good data.
      \end{itemize}

      A general property of Fourier transforms (Parseval's Theorem) translates
      the noise in $\bchi(R)$ to the noise in $\bchi(k)$.

      \begin{center}
        \highlightbox{ \begin{minipage}{70mm} When fitting in $R$-space, we want
            $\epsilon_R$, the noise in $\bchi(R)$, but $\epsilon_k$, the noise
            in $\bchi(k)$, is easier to interpret.
      \end{minipage}}\end{center}

  \end{minipage}
\end{center}
\vspace{1mm}
\vfill
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}{Error Bars: the uncertainities in the fit variables}
\small
\hspace{-.1mm}
\begin{center}
  \begin{minipage}{95mm}\setlength{\baselineskip}{10pt}

    A fit finds $\Blue{\mathbf{x_0}}$, the ``best fit values'' of the variables
    $\Blue{\mathbf{x}}$ by minimizing $\bchi^2$.

    \vmm \vmm The uncertaintes in $\Blue{\mathbf{x}}$ are found by increasing
    the best $\bchi^2$ by 1:

    \vmm
    \begin{center} \wgraph{60mm}{errors/ellipse}
    \end{center}

    \vmm

    {\RedEmph{increase by 1}} implies that the fit is ``Good'' ($\chi^2_\nu
    \approx 1$).

    \vmm Sadly, we typically get {\highlightbox{$\chi^2_\nu \gtrsim 50$!! }}
    even for very good fits.
    So, we increase the best $\bchi^2$ by $\bchi^2_\nu$.

    \vmm
    \begin{center}{\highlightbox{
          The ``$\bchi^2 + 1$'' error bars $\delta x$ are multiplies by
          $\mathbf{\sqrt{\bchi^2_\nu}}$.
        }}
    \end{center}

  \end{minipage}
\end{center}
\vspace{1mm} \vfill
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}{Error Bars: correlations between fit variables}
\small
\hspace{-.1mm}
\begin{center}
  \begin{minipage}{95mm}\setlength{\baselineskip}{10pt}

    Pairs of variables can be {\RedEmph{correlated}}: changing one variable away
    from its optimum value can be compensated by changing another variable away
    from its best value.  The uncertainties needs to take correlations into
    account.

    \vmm
    \begin{center} \wgraph{60mm}{errors/ellipse2}   \end{center}

    \vmm
    The uncertainty in $x$ is $\delta x$, NOT  $\delta x'$!

    \vmm

    The correlations values are useful statistics, measuring the ellipse shape.

    \vmm

    In EXAFS, ($R$, $E_0$) and ($N$, $\sigma^2$) are usually very highly
    correlated ($>0.85$).

  \end{minipage}
\end{center}
\vspace{1mm}
\vfill
\end{slide}

\begin{slide}
  \small
  \begin{cenpage}{80mm}\setlength{\baselineskip}{10pt}
    \vfill
    \begin{center}
      {\Huge\BlueEmph{ Some Fit Examples}   }

    \end{center}
    \vfill
  \end{cenpage}
\end{slide}

%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}{Room Temperature Cu Fit }
\small
\hspace{-.1mm}
\begin{center}
  \begin{minipage}{100mm}\setlength{\baselineskip}{10pt}

    Simple fit to first shell of Cu foil (300K): $k = [2,16] \rm\,
    \AA^{-1}$, $R = [1.7,2.6] \rm\, \AA$, $k$-weight=2, $N_{\rm idp} = 8.4
    $.  Fit results and statistics:

    {
      \hspace{0.1mm}\begin{tabular}{lll}
        $R = 2.548(0.007) \, \rm\AA$
        &
        $\Delta E_0 = 4.5(0.6)$
        &
        $C_3      = 9(9) \times10^{-5} \rm\, \AA^3$
        \\
        $\epsilon_k = 1.6 \times 10^{-4}$
        &
        $S_0^2 = 0.96(0.04)$
        &
        $\sigma^2 = 8.5(0.3) \times10^{-3} \rm\, \AA^2$
        \\
        $\chi^2 = 678$ &
        $\chi^2_\nu = 196.7$   & ${\cal{R}} = 0.00107 $\\
      \end{tabular}
    }

    \vmm
      \begin{tabular}{lcl}
        \hspace{-10mm} \wgraph{49mm}{errors/cufit02} & \hspace{2mm} &
        \hspace{-3mm}  \wgraph{49mm}{errors/cufit01} \\
      \end{tabular}

      \begin{itemize}
      \item ${\cal{R}} = 0.1\% $ -- a good fit!  But like $\chi^2_\nu$,
        ${\cal{R}}$ is larger than the $\epsilon_k$ suggests.
      \item These error bars account for correlations.  They increase
        $\chi^2$ by $\chi^2_\nu$ (not 1), which scales them by
        $\sqrt{\chi^2_\nu}\approx 14$ over ``increase $\chi^2$ by 1''.
      \end{itemize}

      \vmm

  \end{minipage}
\end{center}
\vspace{1mm}
\vfill
\end{slide}


\subsection{Bayesian classifier}

\subsubsection{Classification risk}

We can measure the risk of a classifier. This is the chance of misclassification.

\(R(C)=P(C(X)\ne Y)\)

\subsubsection{The Bayesian classifier}

This is the classifer \(C(X)\) which minimises the chance of misclassification.

It takes the output of the soft classifier and chooses the one with the highest chance.


\documentclass[simplex.tex]{subfiles}
% NO NEED TO INPUT PREAMBLES HERE
% packages are inherited; you can compile this on its own
\begin{document}
\subsection{Non-Parametric Shape Clustering}
%
We have been mostly focused on developing
non-parametric clustering methods.
To this purpose, we are exploring ideas from energy statistics,
which is non-parametric,
robust, and rotational invariant, thus it incorporates the main ingredients
that we are looking for. The main difficulty is to formulate an
algorithm based on this, i.e. to identify the correct test
statistic, or to formulate it as a feasible optimization problem.
Consider $K$-Means clustering problem which is
$
\min_{\{\mathcal{C}_k\}}
\sum_{k=1}^K \sum_{x \in \mathcal{C}_k} \| x - \mu_k  \|^2,
$
where $\mathcal{C}_k$ is the $k$th cluster and $\mu_k$ the mean of its points.
We showed that this problem is equivalent to
\begin{equation}
\max_{G} \textnormal{Tr}\left( G^T K G \right) \qquad
\mbox{s.t. \, $G \ge 0$, $G^T G=I$, $G G^T e_1 = e_1$}.
\end{equation}
where $e_1 = (1,1,\dotsc,1)^T$.
This is a Quadratically Constrained Quadratic Problem (QCQP), which is usually
NP-hard.
Analogously, consider the energy function
$\mathcal{E}(F,G) = 2\mathbb{E} \| X - Y\| - \mathbb{E}\| X - X'\| - \mathbb{E}
\| Y - Y'\|$ between $X,X' \sim F$ and $Y,Y' \sim G$. We showed that this can
be written as $\mathcal{E}(A,B) = e_1^T \Delta e_1 $, where $\Delta$ is a
dissimilarity
matrix between the two sets of data points
$A \stackrel{iid}{\sim} F$
and $B \stackrel{iid}{\sim} G$.
Consequently, a simple two-class clustering problem would be
\begin{equation}
\max_{x,z\in \mathbb{R}^N} x^T \Delta z \qquad
\mbox{s.t. $x_i^2=1$, $x+z = 0$},
\end{equation}
which is also a QCQP problem. We are currently investigating this problem and
trying to generalize it correctly for more classes.
A simple check of the energy function as a test statistic
is shown in Fig.~\ref{fig:nonpar}.
Under the null $F = G$,
$T$ converges to a quadratic form of normally distributed random variables. This
seems to be the case in the first (blue) histogram, while it is definitely not
the case in the other (red and green) histograms.
For the blue histogram a single test gives
$T \approx 0.32$ (small), for the red
histogram $T \approx 4000 $ (large), and for the green
histogram $T \approx 105$ (large), with
only a few points. Thus, energy statistics based approach is
able to distinguish between
different distributions, even when the clusters have the same mean, which is a
property that $K$-Means cannot resolve.

%
\begin{figure}[h!]
\begin{cframed}
\centering
%\begin{subfigure}[t]{0.45\textwidth}
\includegraphics[width=\textwidth]{../../figs/energy_hists.pdf}
%\label{fig:nonpar57}
\caption{
Distribution of test statistic
$T\equiv\tfrac{n m }{n+m}\mathcal{E}(A,B)$
for an ensamble obtained from two distributions:
$A \stackrel{iid}{\sim} \mathcal{N}(\mu_A,\sigma_A^2)$ and
$B \stackrel{iid}{\sim} \mathcal{N}(\mu_B, \sigma_B^2)$, where
$|A|=n$ and $|B|=m$.
Blue histogram: $\mu_A = \mu_B = 0$ and $\sigma_A = \sigma_B = 1$;
Red histogram: $\mu_A = - \mu_B = 1$ and $\sigma_A = \sigma_B = 1$;
Green histogram: $\mu_A = \mu_B = 0$, $\sigma_A = 1$ and $\sigma_B = 1.5$.
}
%\end{subfigure}
%\begin{subfigure}[t]{0.45\textwidth}
%\includegraphics[width=\textwidth]{../../figs/nonPar357.png}
%\label{fig:nonpar357}
%\caption{
%Classification error against the size of each
%cluster (the three classes have the same number of points) is
%shown in blue. The red line is standard
%K-means with Euclidean distance for comparison.}
%\end{subfigure}
%\caption{
%  MNIST handwritten digits and classification error results.
%}
\label{fig:nonpar}
\end{cframed}
\end{figure}
%
\clearpage
\end{document}


\subsection{Direct Optimization}
Time is free so we have a new unknowns. we use $t_f$ as a new state and change some parameter that will describe here.
$$J = t_f^2, \qquad 0\leq t\leq t_f,\qquad \tau = \dfrac{t}{t_f}, \qquad 0\leq\tau\leq 1$$
$$
\vec{a}_N(\vec{x}(t),\vec{u}(t),t_f, t) = t_f\vec{a}(\vec{x}(t),\vec{u}(t), t)
$$
$$
g_N(\vec{x}(t),\vec{u}(t),t_f, t) = t_fg(\vec{x}(t),\vec{u}(t), t)
$$
$$
\mathcal{H} = g_N(\vec{x}(t),\vec{u}(t),t_f, t) + P^T\vec{a}_N(\vec{x}(t),\vec{u}(t),t_f, t)
$$
\begin{align*}
	G_1(u) = &
	\begin{cases}
		-\dfrac{1}{g_1(u)}&  g_1(u) \leq \epsilon \\[1em]
		-\dfrac{1}{\epsilon}\left(3 - \dfrac{3g_1(u)}{\epsilon} + \left(\dfrac{g_1(u)}{\epsilon}\right)^2\right) &  g_1(u) > \epsilon
	\end{cases} \\
	G_1^\prime(u) = &
	\begin{cases}
		\dfrac{1}{(u - )^2}&  g_1(u) \leq \epsilon \\[1em]
		-\dfrac{1}{\epsilon}\left(-\dfrac{3}{\epsilon} + \dfrac{2u-2}{\epsilon^2} \right) &  g_1(u) > \epsilon
	\end{cases}
\end{align*}
\begin{align*}
	G_2(x_2) = &
	\begin{cases}
		-\dfrac{1}{g_2(u)}&  g_2(u) \leq \epsilon \\[1em]
		-\dfrac{1}{\epsilon}\left(3 - \dfrac{3g_2(u)}{\epsilon} + \left(\dfrac{g_2(u)}{\epsilon}\right)^2\right) &  g_2(u) > \epsilon
	\end{cases} \\
	G_2^\prime(u) = &
	\begin{cases}
		\dfrac{1}{(u + 1)^2}&  g_2(u) \leq \epsilon \\[1em]
		-\dfrac{1}{\epsilon}\left(\dfrac{3}{\epsilon} + \dfrac{2u+2}{\epsilon^2} \right) &  g_2(u) > \epsilon
	\end{cases}
\end{align*}
$$
g_N(\vec{x}(t),\vec{u}(t),t_f, t) = t_f^2 + r_kG(u)
$$
$$
\dfrac{\partial J}{\partial t_f} = \dfrac{\partial h}{\partial t_f} + \int_{0}^{1}
\dfrac{\partial \mathcal{H}}{\partial t_f}
$$
$$
\dfrac{\partial J}{\partial \vec{X}} =
\begin{bmatrix}
	\left.\dfrac{\mathcal{H}}{\partial u}\right\vert_{\tau_0}\\
	\left.\dfrac{\mathcal{H}}{\partial u}\right\vert_{\tau_1}\\
	\left.\dfrac{\mathcal{H}}{\partial u}\right\vert_{\tau_2}\\
	\vdots\\
	\left.\dfrac{\mathcal{H}}{\partial u}\right\vert_{\tau_f}\\[20pt]
	\dfrac{\partial h}{\partial t_f} + \int_{0}^{1}
	\dfrac{\partial \mathcal{H}}{\partial t_f}\\
\end{bmatrix}
$$
\begin{itemize}
	\item Steepest Descent
	\begin{itemize}
		\item Quadratic Interpolation
		\begin{figure}[H]
			\caption{Steepest Descent and Quadratic Interpolation}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q3/Steepest Descent + Quadratic Interpolation.png}
		\end{figure}
	\newpage
		\item Golden Section
		\begin{figure}[H]
			\caption{Steepest Descent and Golden Section}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q3/Steepest Descent + Golden Section.png}
		\end{figure}
	\end{itemize}
\newpage
	\item BFGS
	\begin{itemize}
		\item Quadratic Interpolation
		\begin{figure}[H]
			\caption{BFGS and Quadratic Interpolation}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q3/BFGS + Quadratic Interpolation.png}
		\end{figure}
		\item Golden Section
		\begin{figure}[H]
			\caption{BFGS and Golden Section}
			\centering
			\includegraphics[width=11.5cm]{../Figure/Q3/BFGS + Golden Section.png}
		\end{figure}
	\end{itemize}
\end{itemize}


\section{Constrained Problems}
\label{sec:a22constrained}

\paragraph{G08}

This problem originates from \cite{Schoenauer93Constrained}.
We changed the domain from $\clint{0, 10}^2$
to $\clint{0.5, 2.5} \times \clint{3, 6}$
to increase the size of feasible region.
In addition, we use different frequencies for the sine terms
as in \cite{Gavana13Global}.
\vspace{-1.6em}

\begin{subequations}
  \begin{gather}
    \centertestfunline{
      \testobjfunscaled{G08}(\xscaled)
      \ceq -\frac{
        \sin^3(2\pi\xse{1}) \sin(2\pi\xse{2})
      }{
        \xse{1}^3 (\xse{1} + \xse{2})
      },\quad
      \testineqconfunscaled{G08}(\xscaled)
      \ceq \begin{pmatrix}
        \xse{1}^2 - \xse{2} + 1\\
        1 - \xse{1} + (\xse{2} - 4)^2
      \end{pmatrix},
    }\\
    \centertestfunline{
      \xscaled \in \clint{0.5, 2.5} \times \clint{3, 6},\quad
      \xoptscaled = (1.227971358337, 4.245373366474),
    }\\
    \centertestfunline{
      \testobjfunscaled{G08}(\xoptscaled) = -0.09582504141804
    }
  \end{gather}
\end{subequations}

\paragraph{G04Squared}

This problem is based on a problem from
\cite{Colville68Comparative} with the objective function
$\testobjfunscaled{G04}(\xscaled)
\ceq 5.3578547 \xse{3}^2 + 0.8356891 \xse{1} \xse{5} +
37.293239 \xse{1} - 40792.141$ and the same constraints
$\testineqconfunscaled{G04}(\xscaled) \ceq
\testineqconfunscaled{G04Sq}(\xscaled)$.
However, hierarchical cubic not-a-knot B-splines are able to exactly
represent the polynomial $\testobjfunscaled{G04}$ of coordinate degree two
on the whole domain $\clint{\*0, \*1}$,
if the level of the sparse grids is high enough,
see \thmref{cor:sparseGridRegularNAKPolynomials}.
Therefore, we modified the original G04 problem by squaring the
objective function.
To ensure that this does not change the location of the global minimum,
we added a constant before squaring such that the shifted function
is non-negative on $\clint{\*0, \*1}$.
\vspace{-1.6em}

\begin{subequations}
  \allowdisplaybreaks
  \begin{gather}
  \centertestfunline{
    \testobjfunscaled{G04Sq}(\xscaled)
    \ceq (5.3578547 \xse{3}^2 + 0.8356891 \xse{1} \xse{5} +
    37.293239 \xse{1} - 10120)^2,
  }\\
  \centertestfunline{
    \testineqconfunscaled{G04Sq}(\xscaled)
    \ceq 10^{-3} \scalebox{0.92}{$
      \begin{pmatrix}
        85334.407 + 5.6858 \xse{2} \xse{5} +
        0.6262 \xse{1} \xse{4} -
        2.2053 \xse{3} \xse{5} - 92000\\
        -85334.407 - 5.6858 \xse{2} \xse{5} -
        0.6262 \xse{1} \xse{4} +
        2.2053 \xse{3} \xse{5}\\
        80512.49 + 7.1317 \xse{2} \xse{5} +
        2.9955 \xse{1} \xse{2} +
        2.1813 \xse{3}^2 - 110000\\
        -80512.49 - 7.1317 \xse{2} \xse{5} -
        2.9955 \xse{1} \xse{2} -
        2.1813 \xse{3}^2 + 90000\\
        9300.961 + 4.7026 \xse{3} \xse{5} +
        1.2547 \xse{1} \xse{3} +
        1.9085 \xse{3} \xse{4} - 25000\\
        -9300.961 - 4.7026 \xse{3} \xse{5} -
        1.2547 \xse{1} \xse{3} -
        1.9085 \xse{3} \xse{4} + 20000
      \end{pmatrix},
    $}
  }\\
  \centertestfunline{
    \xscaled \in \clint{78, 102} \times \clint{33, 45} \times
    \clint{27, 45}^3,
  }\\
  \centertestfunline{
    \xoptscaled = (78, 33, 29.995256025682, 45, 36.775812905788),
  }\\
  \centertestfunline{
    \testobjfunscaled{G04Sq}(\xoptscaled) = 43.590737882363
  }
  \end{gather}
\end{subequations}


\subsection {ATE abd Selection Bias}
When we observe a group of subjects, some of whom have been treated and some not, we calculate
\begin {equation} \begin {split}
& \EE(Y_i(1) \g  W_i = 1) - \EE(Y_i(0) \g  W_i = 0) \iff \\
& [ \EE(Y_i(1) \g  W_i = 1) -  \EE(Y_i(0) \g  W_i = 1) ] +  \\
& \qquad \EE(Y_i(0) \g  W_i = 1) -  \EE(Y_i(0) \g  W_i = 0) ] \iff  \\
& \text {Average Treatment Effect} + \text { Selection Bias} \\
\end {split} \end {equation}

\subsection {Fisher exact test in R}
\begin{lstlisting}
library(perm)
perms <- chooseMatrix(6, 3)
A <- matrix(c(38.2, 37.1, 37.6, 36.4, 37.3, 36), nrow=6, ncol=1, byrow=TRUE)
is.treatment <- c(1, 1, 1, 0, 0, 0)

n_treatment <- sum(is.treatment)
n_control <- length(A) - sum(is.treatment)
treatment_avg <- (1/n_treatment) * perms %*% A
control_avg <- (1/3) * (1-perms) %*% A
test_statistic <- abs(treatment_avg - control_avg)

rownumber <- apply(apply(perms, 1,
    function(x) (x == is.treatment)), 2, sum)
rownumber <- (rownumber == length(A))
observed_test <- test_statistic[rownumber == TRUE]

larger_than_observed <- (test_statistic >= observed_test)
sum(larger_than_observed) / length(test_statistic)
\end{lstlisting}

\subsection {Fixed effects model} \label {r: fem}
G(factor(admin)) creates a dummy variable, one per region, and includes the dummy variable
in the regression
\begin{lstlisting}[language=R]
library("lfe")
model2 <- felm(sex ~ teasown + post + teapost +
G(factor(admin)), data = qiandata)
summary(model2)
\end{lstlisting}

% \subsection {Instrument variable regression}
% To use the ivreg() function directly one needs to setup the call as
% \begin{lstlisting}[language=R]
% ivreg (y ~ exogenous_vars + endogenous_var |
% exogenous_vars + instrument_var)
% \end{lstlisting}

\subsection{Neyman analysis}
\begin{lstlisting}
data <- read.csv("data_myData.csv")
treatment <- data$T == 1

treatment_mean <- mean(data$Y[treatment], na.rm=TRUE)
control_mean <- mean(data$Y[!treatment], na.rm=TRUE)
ate <- treatment_mean - control_mean

Nc <- sum(!treatment)
Nt <- sum(treatment)
Sc2 <- (1/(Nc-1)) * sum( (data$Y[!treatment] - control_mean)^2 )
St2 <- (1/(Nt-1)) * sum( (data$Y[treatment] - treatment_mean)^2 )

Vneyman <- (Sc2/Nc + St2/Nt)

# N > 30
ub <- ate + 1.96 * sqrt(Vneyman)
lb <- ate - 1.96 * sqrt(Vneyman)

# t-distribution analysis
g <- ( Vneyman^2 / ( (St2/Nt)^2/(Nt+1) + (Sc2/Nc)^2/(Nc+1) ) ) - 2
ub <- ate + qt(0.975, g) * sqrt(Vneyman)
lb <- ate - qt(0.975, g) * sqrt(Vneyman)
\end{lstlisting}

\subsection{Regression commands}
\begin{lstlisting}
hprices <- read.csv("data_House_Prices_and_Crime_1.csv")

str(hprices)
summary(hprices)
subset(hprices, index_nsa == 54.29)

model1 <- lm(index_nsa ~ Homicides + Robberies + Assaults, data=hprices)
summary(model1)
confint(model1)
\end{lstlisting}

\subsection{F-test, restricted model}
\begin{lstlisting}
model_unrest <- lm(index_nsa ~ Homicides + Robberies + Assaults, data=hprices)
anova_unrest <- anova(model_unrest)
model_rest <- lm(index_nsa ~ I(Homicides-Assaults) + I(Robberies-Assaults), data=hprices)
anova_rest <- anova(model_rest)

# F statistic
r <- 1
k <- 3
ssr_u <- anova_unrest$`Sum Sq`[4]
ssr_r <- anova_rest$`Sum Sq`[length(anova_rest$`Sum Sq`)]
statistic_test <- (((ssr_r - ssr_u)/r) / ((ssr_u) / anova_unrest$Df[4]))
pvalue <- df(statistic_test, r, anova_unrest$Df[4])
\end{lstlisting}

\subsection{QQ-plots}
The R base functions \lstinline{qqnorm()} and \lstinline{qqline()} can be used to produce quantile-quantile plots:
\begin{lstlisting}
data <- runif(1000)
qqnorm(data)
qqline(data)
\end{lstlisting}

\subsection{Density plots}
\begin{lstlisting}
data <- runif(1000)
d <- density(data, bw = 0.01)
plot(d)
\end{lstlisting}

\subsection{Difference in difference model}
\begin{lstlisting}
manufacturing <- read.csv("data_manufacturing.csv")

# Cleaning
library(tidyverse)
manu <- manufacturing %>%
filter((year == 1987 | year == 1988) & !is.na(scrap)) %>%
select(year, fcode, scrap, grant)

treated <- manu %>%
    filter(grant == 1) %>%
    select(fcode)
treated_firms <- treated$fcode

# Add column 'treated'
manu <- manu %>%
    mutate(treated = 1*(fcode %in% treated_firms))

# Average treatment and control
did_results <- manu %>%
    group_by(year, treated) %>%
    summarize(promedio = mean(scrap), n = n())
did_results

# DiD model
did_model <- lm(scrap ~ treated + I(year==1988) + I(treated*(year==1988)), data=manu)
\end{lstlisting}

\subsection{R distribution functions}
The functions for the density/mass function, cumulative distribution function, quantile function and random variate generation are named in the form \lstinline{dxxx}, \lstinline{pxxx}, \lstinline{qxxx} and \lstinline{rxxx} respectively.
\begin{itemize}
\item For the beta distribution see \lstinline{dbeta}.
\item For the binomial (including Bernoulli) distribution see \lstinline{dbinom}.
\item For the Cauchy distribution see \lstinline{dcauchy}.
\item For the chi-squared distribution see \lstinline{dchisq}.
\item For the exponential distribution see \lstinline{dexp}.
\item For the F distribution see \lstinline{df}.
\item For the gamma distribution see \lstinline{dgamma}.
\item For the geometric distribution see \lstinline{dgeom}. (This is also a special case of the negative binomial.)
\item For the hypergeometric distribution see \lstinline{dhyper}.
\item For the log-normal distribution see \lstinline{dlnorm}.
\item For the multinomial distribution see \lstinline{dmultinom}.
\item For the negative binomial distribution see \lstinline{dnbinom}.
\item For the normal distribution see \lstinline{dnorm}.
\item For the Poisson distribution see \lstinline{dpois}.
\item For the Student's t distribution see \lstinline{dt}.
\item For the uniform distribution see \lstinline{dunif}.
\item For the Weibull distribution see \lstinline{dweibull}.
\end{itemize}


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

\subsection{Enumeration of Matroids}
\begin{defn}
Let $\mathcal{I}$ be the collection of subsets of $E$ that do not contain all of the edges of any \textit{cycle} of $G.$
We get a matroid on the edge set of every graph $G$ by defining $\mathcal{I}$ in this way. This matroid is called the \textit{cycle matroid} of the graph $G$ and is denoted $M(G).$ We will later prove that this is a matroid.
\end{defn}

\begin{defn}
If $M_i, M_j$ are matroids , then there exists a bijection from the ground set of $M_i$ to the ground set of $M_j$, such that a set is independent in the first matroid if and only if it is independent in the second matroid, then $M_i$ and $M_j$ are said to be isomorphic.
\end{defn}

\begin{note}
A matroid that is isomorphic to the cycle matroid of some graph is called graphic.
And every graphic matroid is binary
\end{note}

The following table from Oxley's text \cite{ox_book} is enlightening in that it helps us see how many ways a matroid can be defined on a set.
\noindent The numbers of non-isomorphic matroids and binary matroids on an n-element set for $0 \leq n \leq  8$
\begin{center}
 \begin{tabular}{| c c c c c c c c c c |}
 \hline
 n & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ [0.5ex]
 \hline\hline
 matroids & 1 & 2 & 4 & 8 & 17 & 38 & 98 & 306 & 1724\\
 \hline
 binary matroids & 1 & 2 & 4 & 8 & 16 & 32 & 68 & 148 & 342\\
 \hline
\end{tabular}
\end{center}
It can be seen from this table, that the number of possible matroids on an $n$-set grows very rapidly.
\end{document}


\subsection{Cantilever beam}
\paragraph{}
A two-dimensional cantilever beam subjected to a parabolic shear load at the free end is examined as shown in Fig.~\ref{iso_fig:cantilever_beam_geo_bc}.
    \begin{figure}[h!]
    \centering
        \scalebox{0.8}{\includegraphics{isogeometric_sbfem/images/cantilever_beam_geo_bc.eps}}
        \caption{ Cantilever beam: Geometry and boundary conditions.}
        \label{iso_fig:cantilever_beam_geo_bc}
    \end{figure}
%
The geometry is: length $ L = \SI{8}{\meter} $, height $ D = \SI{4}{\meter} $.
The material properties are: Young’s modulus $ E = \SI{3e7}{\newton \per \square \meter}$ , Poisson’s ratio $ \nu = 0.25 $.
The parabolic shear force is $P = \SI{250}{\meter} $.
The exact solutions for the displacements are given by \citep{Aug2008}:
    \begin{equation}
        \begin{aligned}
            u(x,y) &=
                \frac{Py}{6 \mean{E} I}
                \left[
                    \left(
                        6L-3x
                    \right)x
                    +\left(
                        2+ \mean{\nu}
                    \right)\left(
                        y^2-\frac{D^2}{4}
                    \right)
                \right]
            \\
            v(x,y) &=
                -\frac{P}{6 \mean{E} I}
                \left[
                    3 \mean{\nu} y^2 \left(
                        L-x
                    \right)+
                    \left(
                        4+5 \mean{\nu}
                    \right)\frac{D^2x}{4}+
                    \left(
                        3L-x
                    \right)x^2
                \right]
        \end{aligned}
    \label{iso_eq:cantilever_beam_displacement_solution}
    \end{equation}
%
where $I=D^3/12$ is the moment of inertia, $\mean{E}=E$, $\mean{\nu}=\nu$ and $\mean{E}=E/(1-\nu^2)$, $\mean{\nu}=\nu/(1-\nu)$ for plane stress and plane strain condition respectively.
The stress $\sigma$ can be expressed as \citep{Aug2008}
    \begin{subequations}
    \begin{align}
        \sigma_{xx} &= \frac{P(L-x)y}{I} \\
        \sigma_{yy} &= 0 \\
        \tau_{xy} &= -\frac{P}{2I} \left[
            \frac{D^2}{4} - y^2
        \right]
    \end{align}
    \label{iso_eq:cantilever_beam_stress_solution}
    \end{subequations}
%
The strain energy can be derived from Eq.~\ref{iso_eq:cantilever_beam_stress_solution} and Eq.~\ref{iso_eq:cantilever_beam_displacement_solution} as
\begin{equation}
\epsilon =
    \frac{1}{2} \left(
        \frac{D^3 L^3 P^2}{36EI^2} +
        \frac{D^5LP^2(1+v)}{60EI^2}
    \right)
    \label{iso_eq:cantilever_beam_energy_solution}
\end{equation}
\paragraph{}
In this example, rigid body motion is constrained by fixing 3 DOF on the left edge of the beam.
$u_x=0$ for points at $(0,-D/2)$ and $(0,D/2)$ and $u_y =0$ for point at $(0,0)$.
Surface tractions determined from the analytical solution of stress in Eq.~\ref{iso_eq:cantilever_beam_stress_solution} are applied on the boundary.

\paragraph{}
From the description in Section~\ref{iso_section:surface_traction}, the expression of the surface tractions must be transformed into NURBS-like representation before they can be applied.
The control points that describe a second order function as surface tractions in this example can be solved mathematically.
Assume the knot vector is evenly spaced and the shape functions is in second order, i.e. knot vector $\Xi=[0,0,0,1,1,1]$.
Weight vector will be uniform because only the straight line is being interpolated, i.e. weight vector $w=[1,1,1]$.
Three basis functions used in B-Spline will be
    \begin{equation}
    \begin{aligned}
        N_1 & = (1-u)^2 \\
        N_2 & = 2u(1-u) \\
        N_3 & = u^2
    \end{aligned}
    \end{equation}
%
With the given targeted parabola as $y=ax^2+bx+c,x \in [0,1]$, the generalized control points for the NURBS curve will be
    \begin{equation}
        P= \begin{bmatrix}
            P_x \\
            P_y
        \end{bmatrix} = \begin{bmatrix}
            0 & m & 1 \\
            c & n & a+b+c
        \end{bmatrix}
    \end{equation}
%
where $m$ and $n$ are unknowns for the second control point.
B-spline curve $C=[N][P]$ then can be expressed as in parametric form as
    \begin{equation}
        \left\{
        \begin{aligned}
            x &= 2u(1-u)m + u^2 \\
            y &= c(1-u)^2 + 2u(1-u)n + (a+b+c)u^2
        \end{aligned}
        \right.
    \label{iso_eq:parabola_fitting_parametric}
    \end{equation}
%
After substituting Eq.~\ref{iso_eq:parabola_fitting_parametric} into $y=ax^2+bx+c$, we then have the system of equations as
    \begin{equation}
        \begin{bmatrix}
            0 \\
            0 \\
            2c - 2n +a +b \\
            2n - 2c \\
            c
        \end{bmatrix} =
        \begin{bmatrix}
            4am^2-4am+a \\
            -8am^2 + 4am \\
            4am^2 -2bm +b \\
            2bm \\
            c
        \end{bmatrix}
    \end{equation}
%
$m$ and $n$ then can be solved as
    \begin{equation}
        \left\{
        \begin{aligned}
            m &= \frac{1}{2} \\
            n &= \frac{b+2c}{2}
        \end{aligned}
        \right.
    \end{equation}
%
The numerical convergence of the relative error in the displacement norm and the relative error in the energy norm are shown in Fig.~\ref{iso_fig:cantilever_beam_convergence} for various order of NURBS basis functions with refinement.
Fig.~\ref{iso_fig:cantilever_beam_convergence} also shows the error in the displacement norm when quadratic Lagrange shape functions are used along each edge within the scaled boundary formulation.
It can be observed that NURBS basis functions yield superior accuracy when compared to Lagrange basis functions of the same order.
It is seen that as the order of the shape functions is increased, the error decreases while the convergence rate increases.
\begin{figure}
    \begin{subfigure}[b]{1\linewidth}
        \centering
        \scalebox{0.7}{
            \input{isogeometric_sbfem/images/cantilever_beam_displacement_convergence.tikz}
        }
        \label{iso_fig:cantilever_beam_displacement_convergence}
        \caption{the relative error in displacement norm $(L^2)$}
    \end{subfigure}

    \begin{subfigure}[b]{1\linewidth}
        \centering
        \scalebox{0.7}{
            \input{isogeometric_sbfem/images/cantilever_beam_energy_convergence.tikz}
        }
        \label{iso_fig:cantilever_beam_energy_convergence}
        \caption{the relative error in the energy norm}
    \end{subfigure}
\caption{Bending of thick cantilever beam: Convergence results}
\label{iso_fig:cantilever_beam_convergence}
\end{figure}
%


\subsection{Calculating \(\tau \)}

As we note above, \(\sin (\theta )=\cos (\theta )\) at \(\theta =\tau *\dfrac{1}{8}\)

This is also where \(\tan (\theta )=1\).

\(\arctan (k)=\arctan (a)+\int_a^k\dfrac{1}{1+y^2} \delta y\)

We start from \(a=0\).

\(\arctan (k)=\arctan (0)+\int_0^k\dfrac{1}{1+y^2} \delta y\)

We know that one of the results for \(\arctan (0)\) is \(0\).

\(\arctan (k)=\int_0^k\dfrac{1}{1+y^2} \delta y\)

We want \(k=1\)

\(\arctan (1)=\int_0^1\dfrac{1}{1+y^2} \delta y\)

\(\dfrac{\tau }{8}=\int_0^1\dfrac{1}{1+y^2} \delta y\)

\(\tau =8\int_0^1\dfrac{1}{1+y^2} \delta y\)

We know that the \(\cos (\theta )\) and \(\sin (\theta )\) functions cycle with period \(\tau \).

Therefore \(cos (n.\tau )=\cos (0)\)


\input{../common/common.tex}

\title{Math notes - Bridge Crossings}
\author{Uwe Hoffmann}
\hypersetup{colorlinks, pdftitle={Math notes - Bridge Crossings}}

\begin{document}

\setcounter{chapter}{1}
\section*{Bridge Crossings}

\begin{fullwidth}

\vspace{10 mm}
\begin{problem}
Four people begin on the same side of a bridge. You must send them across to the other side in the fastest time possible. It is night. There is one flashlight. A maximum of two people can cross at a time. Any party who crosses, either one or two people, must have the flashlight to see. The flashlight must be walked back and forth, it cannot be thrown, etc. Each person walks at a different speed. A pair must walk together at the rate of the slower person's pace, based on this information: Person $1$ takes $t_1 = 1$ minutes to cross, and the other persons take $t_2 = 2$ minutes, $t_3 = 5$ minutes, and $t_4 = 10$ minutes to cross, respectively.
\end{problem}

\end{fullwidth}

G\"unter Rote\footnote{\bibentry{Rote02}} gives a very elegant solution to this puzzle.

\subsection{How many ways are there to let $n$ people cross the bridge under the rules of the original puzzle ?}

There are $\binom{n}{2}$ ways to send the first pair over to the other side, there are 2 ways to send the flashlight back with somebody from that side.  Now there are $\binom{n -1}{2}$ ways to send the next pair over to the other side from the remaining $n - 1$ people on this side and then there are 3 ways to send the flashlight back with somebody from that side etc.

Using the basic product counting principle from combinatorics we get the number of ways $P$ to let $n$ people cross the bridge

\begin{equation}
\begin{aligned}
P & = \binom{n}{2} 2 \binom{n - 1}{2} 3 \binom{n - 2}{2} 4 \dots (n - 1) \binom{2}{2} \\
    & = (n - 1) ! \prod_{k = 0}^{n - 2} \binom{n - k}{2}
\end{aligned}
\label{mainFormula}
\end{equation}

Taking the product from (\ref{mainFormula}) and using the definition of a binomial coefficient we get:

\begin{equation}
 \prod_{k = 0}^{n - 2} \binom{n - k}{2} =  \prod_{k = 0}^{n - 2} \frac{(n - k) !}{2 ! (n - k - 2)!}
 \label{productPart}
\end{equation}

\noindent With:

\begin{equation}
\begin{aligned}
P_k & = \frac{(n - k) !}{2 ! (n - k - 2)!} \quad \text{and} \\
p_k & = (n - k) !
\end{aligned}
\end{equation}

\noindent we get:

\begin{equation}
P_k  = \frac{p_k}{2 ! p_{k + 2}}
\end{equation}

\noindent The product of these $P_k$ can now be simplified to:

\begin{equation}
\begin{aligned}
 \prod_{k = 0}^{n - 2} P_k & =  \prod_{k = 0}^{n - 2} \frac{(n - k) !}{2 ! (n - k - 2)!} \\
                                              & = \frac{1}{(2!)^{n - 1}} \prod_{k = 0}^{n - 2} \frac{p_k}{p_{k + 2}} \\
                                              & = \frac{1}{2^{n - 1}} \frac{p_0}{p_2} \frac{p_1}{p_3} \frac{p_2}{p_4} \dots \frac{p_{n - 3}}{p_{n - 1}} \frac{p_{n - 2}}{p_n} \\
                                              & = \frac{1}{2^{n - 1}} \frac{p_0 p_1}{p_{n - 1} p_n} \\
                                              & = \frac{1}{2^{n - 1}}  n ! (n - 1) !
\end{aligned}
 \label{simplifiedFormula}
\end{equation}

\noindent Using  (\ref{simplifiedFormula}) we get the solution

\begin{equation}
P = \frac{n ! ((n - 1) !)^2}{2^{n - 1}}
\end{equation}

For four people this comes to an astonishing 108 ways to cross the bridge under the rules of the puzzle.

\subsection{Generating the ways}

This section shows a small Haskell program that generates all the possible ways to cross the bridge.  It has a helper function \emph{pairs} that generates a list of all possible pairs from a set. It then defines two mutually recursive functions \emph{bridgecrossleft} and \emph{bridgecrossright} for crossing the bridge from the left side as pairs and for a flashlight carrier coming back from the right. The functions pass along the states on the left bank \emph{lbs} and the right bank \emph{rbs}.  They generate all possible crossings in their respective direction given the current state. For pairs crossing from the left tuples have the respective pair and for people coming back from the right tuples have the same person in both positions of the tuple. The functions collect the resulting combinations in a list of lists of tuples \emph{rs} (Fig. \ref{bridge_crossing_gen}). \emph{bridgecross} is the main function taking a list and calling \emph{bridgecrossleft} because we start on the left with all possible ways of crossing of the first pair.

\begin{marginfigure}
\includegraphics[width=3in]{fig.pdf}
\caption{Two mutually recursive functions \emph{bridgecrossleft} and \emph{bridgecrossright}.}
	\label{bridge_crossing_gen}
\end{marginfigure}

Calling \emph{bridgecross [1, 2, 3]} we get this result:

\begin{lstlisting}
[
 [(1,2),(1,1),(1,3)],
 [(1,2),(2,2),(2,3)],
 [(1,3),(1,1),(1,2)],
 [(1,3),(3,3),(3,2)],
 [(2,3),(2,2),(2,1)],
 [(2,3),(3,3),(3,1)]
]
\end{lstlisting}

\newpage

\lstinputlisting[language=Haskell, basicstyle=\small, frame=trBL, caption={Haskell code}]{bridge.hs}

\bibliographystyle{plainnat}
\bibliography{../common/math}

\end{document}


\section{Green's Function}
\subsection{Physical Motivation}
Consider the static force on a string which can be caused by gravity acting on the mass of the string.
Let the tension be $T$ and linear mass density be $\mu$.
Suppose the string is suspended between fixed ends $y(0)=y(1)=0$.
The static force is then $-\mu\delta xg$ on the $y$ direction on the piece $\delta x$.
By resolving forces, we get the equation $-y^{\prime\prime}=f(x)$ where $f(x)=-\mu g/T$.
We can solve it via direct integration.
For uniform mass density (i.e. $\mu$ is constant), we have
$$-y=-\frac{\mu g}{2T}x^2+k_1x+k_2\implies y(x)=\left( -\frac{\mu g}{T} \right)\frac{1}{2}x(1-x)$$
by the boundary conditions.
This is a parabolic curve.
\footnote{You might be expecting the catenary curve instead -- but not really, since in that problem we require the string to be non-elastic (i.e. has a fixed length) which makes it a completely different situation.}
Another way to solve this problem is to disassemble the force on the string as the sum of infinitesimal parts, and consider their superposition.
Assume the string is massless but with a point mass $\delta m$ suspended at $x=\xi_i$.
Obviously the solution is simply two line segments meeting at some points $(\xi_i,y_i)$.
Suppose the segment closer to $0$ makes an angle $\theta_1$ with the horizontal and the other segment makes an angle $\theta_2$, then resolving in $y$-direction gives
$$0=T(\sin\theta_1+\sin\theta_2)-\delta mg=T\left( \frac{-y_i}{\xi_i}+\frac{-y_i}{1-\xi_i}-\delta mg \right)$$
Solving this gives us $y_i=-\delta mg\xi_i(1-\xi_i)/T$.
So the solution is
$$y_i(x)=\frac{-\delta mg}{T}\begin{cases}
    x(1-\xi_i)\text{, for $x<\xi_i$}\\
    \xi(1-x)\text{., for $x>\xi_i$}
\end{cases}=f_iG(x,\xi),f_i=\frac{-\delta mg}{T}$$
where $f_i$ is interpreted as the source $f$ around a infinitesimal neighbourhood of $\xi_i$.
Now the superposition of the solution for $N$ point masses $\delta m$ at $x=\{\xi_i\}$ gives
$$y(x)=\sum_{i=1}^Nf_iG(x,\xi_i)$$
If we take the continuum limit
$$f_i=-\frac{\delta mg}{T}=-\frac{\mu\delta x g}{T}=f(x)\,\mathrm dx$$
we have
$$y(x)=\int_0^1f(\xi)G(x,\xi)\,\mathrm d\xi=\left( -\frac{\mu g}{T} \right)\frac{1}{2}x(1-x)$$
\subsection{Definitions of Green's Function}
We wish to solve the inhomogeneous ODE $\mathcal Ly=f(x)$ where $\mathcal L=\alpha y^{\prime\prime}+\beta y^\prime+\gamma y$ on $[a,b]$ subject to boundary conditions $y(a)=y(b)=0$.
We require $\alpha\neq 0$ over $[a,b]$ and $\alpha,\beta,\gamma$ all continuous and bounded.
\begin{definition}
    The Green's function $G$ for the operator $\mathcal L$ is the solution to $\mathcal LG(x,\xi)=\delta(x-\xi)$ subject to homogeneous boundary conditions $G(a,\xi)=G(b,\xi)=0$ for all $\xi$.
\end{definition}
So by linearily, if such $G$ does exist, then we have
$$y(x)=\int_a^bG(x,\xi)f(\xi)\,\mathrm d\xi$$
Indeed,
$$\mathcal Ly=\int_a^b\mathcal LG(x,\xi)f(\xi)\,\mathrm d\xi=\int_a^b\delta(x-\xi)f(\xi)\,\mathrm d\xi=f(x)$$
Loosely speaking, we can write $y=\mathcal L^{-1}f$ where
$$\mathcal L^{-1}=\int_a^b\mathrm d\xi\, G(x,\xi)$$
Now, by our established study of these sort of ODEs, the Green's function splits into two smooth enough parts
$$G(x,\xi)=\begin{cases}
    G_1(x,\xi)\text{, for $x\in [a,\xi)$}\\
    G_2(x,\xi)\text{, for $x\in (\xi,b)$}
\end{cases}$$
such that the following conditions hold:\\
1. $\mathcal LG_1=\mathcal LG_2=0$ at any $x\neq\xi$.\\
2. $G_1(a,\xi)=G_2(b,\xi)=0$ for any $\xi$.\\
3. $G$ is continuous, so $G_1(\xi,\xi)=G_2(\xi,\xi)$.\\
4. The jump condition of $G^\prime$, that is
$$[G^\prime(\cdot,\xi)]^{\xi_+}_{\xi_-}=G_2^\prime(\xi_+,\xi)-G_1^\prime(\xi_-,\xi)=\frac{1}{\alpha(\xi)}$$
How to construct $G$?
Note that $\mathcal L$ is a second order differential opertaor, so for $x\in[a,\xi)$, $G_1(x,\xi)=A(\xi)y_1(x)+B(\xi)y_2(x)$ for linearly independent $y_1,y_2$.
The boundary condition $G_1(a,\xi)=0$ gives $G_1(x,\xi)=C(\xi)y_-(x)$ where $y_-(a)=0$.
Similarly for $x\in(\xi,b]$ we have $G_2(x,\xi)=D(\xi)y_+(x)$ with $y_+(b)=0$.
Then continuity condition gives $C(\xi)y_-(\xi)=D(\xi)y_+(\xi)$ and $D(\xi)y_+^\prime(\xi_+)-C(\xi)y_-^\prime(\xi_-)=\alpha(\xi)^{-1}$.
In other words we have the system
$$\begin{pmatrix}
    y_-(\xi)&y_+(\xi)\\
    -y_-^\prime(\xi_-)&y_+^\prime(\xi_+)
\end{pmatrix}\begin{pmatrix}
    C(\xi)\\
    D(\xi)
\end{pmatrix}=\begin{pmatrix}
    0\\
    1/\alpha(\xi)
\end{pmatrix}$$
which has the solution (after extending everything continuously to $x=\xi$)
$$C(\xi)=\frac{y_+(\xi)}{\alpha(\xi)W(\xi)},D(\xi)=\frac{y_-(\xi)}{\alpha(\xi)W(\xi)}$$
where $W(\xi)=y_-(\xi)y_+^\prime(\xi)-y_+(\xi)y_-^\prime(\xi)$ is the Wronskian which is nonzero if $y_+,y_-$ are linearly independent.
So the final Green's function is
$$G(x,\xi)=\begin{cases}
    y_-(x)y_+(\xi)/(\alpha(\xi)W(\xi))\text{, for $x\in[a,\xi]$}\\
    y_+(x)y_-(\xi)/(\alpha(\xi)W(\xi))\text{, for $x\in[\xi,b]$}
\end{cases}$$
Therefore the solution to the original boundary value problem $\mathcal Ly=f$ for $y=0$ at $a,b$ is
\begin{align*}
    y(x)&=\int_a^bG(x,\xi)f(\xi)\,\mathrm d\xi\\
    &=\int_a^xG_2(x,\xi)f(\xi)\,\mathrm d\xi+\int_x^bG_1(x,\xi)f(\xi)\,\mathrm d\xi\\
    &=y_+(x)\int_a^x\frac{y_-(\xi)f(\xi)}{\alpha(\xi)W(\xi)}\,\mathrm d\xi+y_-(x)\int_x^b\frac{y_+(\xi)f(\xi)}{\alpha(\xi)W(\xi)}\,\mathrm d\xi
\end{align*}
\begin{note}
    1. If $\mathcal L$ is in Sturm-Liouville form, then $\beta=\alpha^\prime$, then $\alpha(\xi)W(\xi)$ is a constant and hence $G$ has to be symmetric.\\
    2. Often we take $\alpha=1$.\\
    3. The indefinite integrals are the particular integrals in the particular integral in the Sturm-Liouville solution.
\end{note}
\begin{example}
    For $y^{\prime\prime}-y=f(x),y(0)=y(1)=0$, the homogeneous solutions are $y_1=e^x$ and $y_2=e^{-x}$.
    Imposing the homogeneous boundary conditions reveals that $y_-(x)=\sinh(x)$ and $y_+(x)=\sinh(1-x)$.
    The countinuity condition of $G$ gives $C=D\sinh(1-\xi)/(\sinh\xi)$.
    The jump condition of $G^\prime$ then gives
    $$D=-\frac{\sinh\xi}{\sinh 1},C=-\frac{\sinh(1-\xi)}{\sinh 1}$$
    So
    $$y(x)=-\frac{\sinh(1-x)}{\sinh 1}\int_0^x\sinh(\xi)f(\xi)\,\mathrm d\xi-\frac{\sinh x}{\sinh 1}\int_x^1\sinh(1-\xi)f(\xi)\,\mathrm d\xi$$
\end{example}
For inhomogenous boundary conditions, we simply need to find a solution to $\mathcal Ly_p=0$ satisfying them and solve for $\mathcal Ly_g=f$ under homogeneous boundary conditions by Green's functions.
Adding them up gives the particular solution $y=y_p+y_g$.
\begin{example}
    For $y^{\prime\prime}-y=f(x)$ with $y(0)=0,y(1)=1$, we have the solution $y_p(x)=\sinh x/\sinh 1$ to $y^{\prime\prime}-y=0$ under the same boundary conditions, so the particular solution would be
    \begin{align*}
        y(x)&=y_p(x)+y_g(x)\\
        &=\frac{\sinh x}{\sinh 1}-\frac{\sinh(1-x)}{\sinh 1}\int_0^x\sinh(\xi)f(\xi)\,\mathrm d\xi-\frac{\sinh x}{\sinh 1}\int_x^1\sinh(1-\xi)f(\xi)\,\mathrm d\xi
    \end{align*}
\end{example}
How about high-order ODEs?
Suppose we have $\mathcal Ly=f(x)$ with the highest order term $\alpha(x)y^{(n)}(x)$ in $\mathcal Ly$ with $\alpha\neq 0$ everywhere, then $\mathcal LG(x,\xi)=\delta(x-\xi)$ has the properties:\\
1. $G_1,G_2$ are solutions to $\mathcal LG=0$.\\
2. $G_1,G_2$ satisfy the homogeneous boundary conditions.\\
3. Continuity condition of $G_1^{(i)}(\xi,\xi)=G_2^{(i)}(\xi,\xi)$ for all $i=1,\ldots,n-2$.\\
4. Jump condition of $[G^{(n-1)}(\cdot,\xi)]_{\xi_-}^{\xi_+}=G_2^{(n-1)}(\xi_+,\xi)-G_1^{(n-1)}(\xi_-,\xi)=1/\alpha(\xi)$.\\
We want to take a look at the eigenfunction expansion of $G$.
Suppose $\mathcal L$ is in Sturm-Liouville form with eigenfunctions $y_n(x)$ with eigenvalues $\lambda_n$.
We seek an expansion
$$G(x,xi)\sum_{n=1}^\infty A_n(\xi)y_n(x)$$
satisfying $\mathcal LG=\delta(x-\xi)$.
Now suppose such an expansion exists, then
\begin{align*}
    \sum_{n=1}^\infty A_n(\xi)\lambda_nw(x)y_n(x)&=\sum_{n=1}^\infty A_n(\xi)\mathcal Ly_n(x)=\mathcal LG\\
    &=\delta(x-\xi)=w(x)\sum_{n=1}^\infty y_n(\xi)\frac{y_n(x)}{N_n}
\end{align*}
where $N_n=\langle y_n,y_n\rangle_w$ is the normalisation constant.
Consequently $A_n(\xi)=y_n(\xi)/(\lambda_nN_n)$, therefore
$$G(x,\xi)=\sum_{n=1}^\infty\frac{y_n(\xi)y_n(x)}{\lambda_nN_n}=\sum_{n=1}^\infty\frac{Y_n(\xi)Y_n(x)}{\lambda_n}$$
\subsection{Construction of Green's Function from Initial Values}
We want to solve $\mathcal Ly(t)=f(t)$ for $t\ge a$ with $y(a)=y^\prime(a)=0$.
The Green's function then should satisfy $\mathcal LG=\delta(t-\tau)$ with $G(a)=G^\prime(a)=0$.
For $t<\tau$, $G=G_1$ satisfies $\mathcal LG_1=0$.
So $G_1=Ay_1+By_2$ for $A,B$ constants and $y_1,y_2$ linearly independent solutions to $\mathcal Ly=0$.
The initial conditions then give
$$\begin{pmatrix}
    y_1(a)&y_2(a)\\
    y_1^\prime(a)&y_2^\prime(a)
\end{pmatrix}\begin{pmatrix}
    A\\
    B
\end{pmatrix}=\begin{pmatrix}
    0\\
    0
\end{pmatrix}$$
But $y_1,y_2$ are independent, so the Wronskian is nowhere zero, hence nonzero at $a$.
Therefore necessarily $A=B=0$, so $G_1(t,\tau)=0$ for $a\le t<\tau$.\\
For $t>\tau$, we have $G=G_2$ for $\mathcal LG_2=0$ and $G_2(\tau,\tau)=0$ by continuity.
So we can choose solution $y_+$ to $\mathcal Ly=0$ such that $G_2(t,\tau)=D(\tau)y_+(t)$.
But we must have (extending everything continuously to $\tau$)
$$\frac{1}{\alpha(\tau)}=G_2^\prime(\tau,\tau)-G_1^\prime(\tau,\tau)$$
which gives $D(\tau)=1/(\alpha(\tau)y_+^\prime(\tau))$, hence
$$G(t,\tau)=\begin{cases}
    0\text{, for $t\le\tau$}\\
    y_+(t)/(\alpha(\tau)y_+^\prime(\tau))\text{, for $t\ge\tau$}
\end{cases}$$
So we get the solution
$$y(t)=\int_a^tG_2(t,\tau)f(\tau)\,\mathrm d\tau=\int_a^t\frac{y_+(t)f(\tau)}{\alpha(\tau)y_+^\prime(\tau)}\,\mathrm d\tau$$
which looks simpler as we built in the causality with the initial conditions.
\begin{example}
    For $y^{\prime\prime}-y=f(t)$ with $y(0)=y^\prime(0)=0$.
    We then obtain $G_2(t,\tau)=\sinh(t-\tau)$.
    Therefore
    $$y(t)=\int_0^tf(\tau)\sinh(t-\tau)\,\mathrm d\tau$$
\end{example}


\section{the Crossing tree} % (fold)
\label{sec:the_crossing_tree}

Brief history in the literature. Cite the original papers \cite{jones2004}
and \cite{jonesshen2005}.

\subsection{Definition} % (fold)
\label{sub:definition}

Consider a path of real-valued continuous process $(X_t)_{t\in T}$ on $T = [0,+\infty)$.
Let $(G_n)_{n\geq0}$ be a collection of increasingly coarser uniformly spaced grids
on $\Real$ centred at $x_0\in \Real$ given by $G_n = x_0 + \delta 2^n \mathbb{Z}$
for any $n\geq 0$ and some base grid spacing $\delta>0$.\footnotemark
Each element of this family of nested grids represents the ``resolution'' through
which sample paths of $X_t$ are studied.
\footnotetext{ The addition $x_0+A$, for $A$ in an affine space such as $\Real$
represents shifting of every element of the set $A$ by a common value $x_0$.}

At the heart of the crossing tree lie crossing times of each particular grid $G_n$
properly aligned by $x_0$. The crossing times $T_k^n$ of process $X_t$ of grid $n\geq 0$
are its first passage times across a line of grid $G_n$ centred at $x_0$ that is
different from a previously crossed line of the same grid. Formally $T_k^n$ are
defined as follows: let $T_0^n=0$ and for $k\geq 0$ put
\[
T_{k+1}^n
= \inf \Bigl\{ t \geq T^n_k : \bigr | X(t) - X(T^n_k) \bigr | \geq \delta 2^n \Bigr\}
\]
The forcing of the zero-th crossing time to $0$ automatically aligns the grid $G_n$
with the process, so in effect, without loss of generality one may consider processes
$X_t$, which start at the origin $X(0) = 0$.

%%%% Discuss subcrossings
%%%% More or less relevant
By construction, for a binary crossing grid, $N^l_k$, the number of subcrossings in
any complete crossing between $T^l_k$ and $T^l_{k+1}$ is always an even number. This
is due to the fact, that each crossing is registered as soon as two unidirectional
subcrossings are encountered, as seen by the following.

Suppose $T^{n+1}_k$ and $T^n_m$ are aligned so that $T^{n+1}_k=T^n_m$ and
$T^{n+1}_{k+1}<+\infty$, i.e. the $k$-th crossing of the $n+1$ grid is complete.
First of all, $T^n_{m+1}, T^n_{m+2} \leq T^{n+1}_{k+1}$, since otherwise the process
would have left the $\pm \delta 2^n$ band before it left the $\pm \delta 2^{n+1}$
band, which is twice as wide.

The process is continuous at $T^n_{m+1}$ which means that almost surely for any
$\epsilon>0$ there is $\eta>0$ such that for any $t$ with $\bigl|t - T^n_{m+1}\bigr| < \eta$
it holds that $\bigl|X_{T^n_{m+1}} - X_t \bigl| < \epsilon$. Also for every $t \in \bigl[ T^n_k, T^n_{k+1} \bigr)$
it cannot be otherwise but $\big | X_t - X_{T^n_k} \big | < \delta 2^n$.  In particular,
for $\epsilon = \frac{1}{2}\delta 2^n$ it means that for a small while after $T^n_{m+1}$
the process is almost surely still within the $\pm \delta 2^{n+1}$ band. Therefore
\[ T^n_{m+1} < T^{n+1}_{k+1} \]
and a crossing of a finer grid occurs almost surely before the crossing of a coarser
grid. Thus it is true that
\[ 1 \leq \biggl| \frac{1}{\delta 2^n} \bigl( X_{T^n_{m+1}} - X_{T^n_m} \bigr) \biggr| < 2 \]

\noindent The next crossing of $\pm\delta 2^n$ takes place at $T^n_{m+2}$ and there
are two possibilities:
\begin{enumerate}
	\item the process crossed a new $\pm\delta 2^{n+1}$ line of $n+1$-st grid:
	\[ \big|X_{T^n_{m+2}} - X_{T^n_m}\big|\geq 2\cdot\delta 2^n\] in which case
	$T^{n+1}_{k+1}\leq T^n_{m+2}$ and there are \emph{two} subcrosings in a crossing
	of grid $n+1$.
	\item the process moved back to the level $X_{T^n_m}$, which does not incur a
	crossing of $\pm\delta 2^{n+1}$ grid line, yet is registered by the $\pm\delta 2^n$
	grid. In this case $T^n_{m+2} < T^{n+1}_{k+1}$ and $X_{T^n_{m+2}} = X_{T^{n+1}_{k+1}}$,
	which brings one back to the beginning of this argument.
\end{enumerate}

Since the crossing is complete, $T^{n+1}_{k+1} < +\infty$ implies that sooner or later
a crossing of $\pm\delta 2^{n+1}$ grid occurs, in which case $T^{n+1}_{k+1} = T^n_{m+2p}$,
meaning that there were exactly $2p$ subcrossings in a crossing of grid $n+1$.

%%%% Given this relationship between the crossing times of neighbouring grid
%%%% contruct a crossing tree.
The exposed relationship between the crossing times enables a natural construction
of a crossing tree: each crossing of grig $n$ is a child of a grander crossing of
grid $n+1$, during which it took place.

%%%% Rewrite, as it is irrelevant
The parameter $\delta$ is the spacing of the finest grid, with respect to which
the leaves of the crossing tree are computed. Parent nodes of these leaves represent
crossings of a coarser grid, namely $2\delta$. The choice of $\delta$ affects the tree
in the following way in the case of a sampled process:
\begin{itemize}
	\item the grid with too low a value of $\delta$ would be crossed by straight
	line segments between each pair of consecutive sample observations. This could
	poison the distribution with some unfavourable yet unknown mixture and leads
	to excessive number of seemingly meaningless crossings.
	\item Too large $\delta$ leads to a very poor and under sampled crossing tree.
\end{itemize}

\noindent \textbf{A model of the offspring distribution} \hfill \\

Let's test the hypothesis that the number of subcrossings follows a distribution
similar to geometric. Recall that $G$ is a geometrically distributed random variable,
$G\sim \text{Geom}(\theta)$, if $\mathbb{P}(G=k) = {(1-\theta)}^{k-1}\theta$ for all
$k\geq1$. Other properties of geometrically distributed random variables include
\begin{itemize}
	\item Complimentary CDF $\mathbb{P}(G\geq k) = (1-\theta)^{k-1}$ for any $k\geq1$;
	\item Expectation $\mathbb{E}(G) = \theta^{-1}$;
	\item Memorylessness: for $m\geq 0$
	\begin{align*}
		\pr\bigl( G \geq n + m \bigr\rvert\bigl. G \geq n \bigr)
		&= \frac{\pr(G \geq n+m)}{\pr(G \geq n)} = \frac{(1-\theta)^{n+m-1}}{(1-\theta)^{n-1}} \\
		& = (1-\theta)^{m-1} = \pr(G \geq m)
	\end{align*}
\end{itemize}

The number of offspring of any $\delta 2^n$-grid crossing is the number of subcrossings
of a finer grid with spacing $\delta 2^{n-1}$ during a typical crossing.

Our hypothesis is that $N$, the number of offspring, follows a geometric distribution
on the even numbers, i.e. $\frac{1}{2}N \sim \text{Geom}(\theta)$. To test it we
utilize a truncated geometric distribution with a fixed threshold $\bar{k}$, because
this approach naturally and easily handles unbounded random variables. Truncating the
data effectively means that two kinds of observations are registered:
\begin{itemize}
	\item a value less than $\bar{k}$;
	\item an event $\big\{N\geq \bar{k}\big\}$, indicating that the value has not
	been less than $\bar{k}$.
\end{itemize}
Basically such truncated random variable behaves like a typical geometric one on
the values $\bigl\{ 2m \bigr\rvert \bigl. 2m < \bar{k}\bigr\}$, but happens to have
an unusual concentration of probability at the upper truncation level $\bar{k}$.

Therefore for even integers $k=2,4,\ldots\bar{k}$ the distribution of the number
of offspring is given by
\[
\mathbb{P}(N=k)
= (1-\theta)^{\frac{k}{2}-1} \theta 1_{k<\bar{k}}
+ (1-\theta)^{\frac{\bar{k}}{2}-1} 1_{k = \bar{k}}
\]
where $1_{(\cdot)}$ -- is the $0-1$ indicator.

\noindent \textbf{Maximum Likelihood Estimation} \hfill \\
Suppose ${(g_i)}_{i=1}^N$ a sample of independent geometric random variables, with
distribution truncated by $\bar{k}$. Due to truncation there is a finite number of
distinct values that are observed in any given sample. Therefore it is convenient to
represent every sample in an equivalent value-frequency form: ${\bigl(j, f_j\bigr)}$
for even $j$ not greater than $\bar{k}$ and
$f_j = \Bigl| \bigl\{ i\bigr\rvert \bigl.g_i = j\bigr\}\Bigr|$ -- the number of
observations with the specified value. Without the loss of generality, $f_j$ can
be set to zero for those $j$ that were not observed.

The log-likelihood function is given by
\[
\ln\mathcal{L}
= \sum_{j\neq \bar{k}} f_j \Bigl(\frac{j}{2}-1\Bigr) \ln(1-\theta)
+ \sum_{j\neq \bar{k}} f_j \ln \theta
+ f_{\bar{k}} \Bigl( \frac{\bar{k}}{2} - 1 \Bigr) \ln(1-\theta)
\]
where the summation is done over the all possible distinct values. The first-order
condition on optimal $\theta$ is given by
\[
\frac{d}{d \theta} \ln\mathcal{L} \,:\quad
-\sum_j f_j \Bigl(\frac{j}{2}-1\Bigr) \frac{1}{1-\theta} + \sum_{j\neq \bar{k}} f_j \frac{1}{\theta} = 0
\]
This is equivalent to $\frac{S-N}{1-\theta} = \frac{N-f_{\bar{k}}}{\theta}$, where
$S = \sum_j \frac{j}{2} f_j$ and $N = \sum_j f_j$ because the frequencies sum up to
the total number of observations. Therefore the desired ML estimator of the probability
parameter $\theta$ is
\[ \hat{\theta} = \frac{N-f_{\bar{k}}}{S-f_{\bar{k}}} \]

I strongly suspect that the MLE of the truncated geometric distribution is biased. If so,
this renders it useless for Monte-Carlo estimation purposes. Clearly the bias should depend
on the truncation threshold, and $\hat{\theta}_T \to \hat{\theta}$ as $T\to \infty$.

%%%%%%%%%%%%

sampled at times $(t_i)_{i=0}^N$ with $t_0=0$ and $t_i<t_{i+1}$.

Denote a sample path of a real-valued continuous process by ${(t_i, x_i)}_{k=0}^N$
anchored $X_0 = 0$ with $t_0 = 0$ and $X_i = X(t_i)$.

% subsection definition (end)

\subsection{Practical construction} % (fold)
\label{sub:practical_construction}

% subsection practical_construction (end)

% section the_crossing_tree (end)

\section{Stochastic self-similarity} % (fold)
\label{sec:stochastic_self_similarity}

This section formally describes what an $H$-SSSI process is. For a broader, thorough
and more exhaustive coverage of the topic the reader is encouraged to refer to
\cite{embrechtsselfsimilar}%pp. 1-3
and \cite{embrechts2000introduction}. %pp. 2-5, 19

(Brief history) The notion of self-similarity has its roots in the works of Mandelbrot,
and describes the phenomenon of scale-invariance observed in nature, seismology and
finance.

\subsection{Hurst self-similar stochastic processes with independent increments} % (fold)
\label{sub:hurst_self_similar_stochastic_processes_with_independent_increments}

A stochastic process $\bigl(X(t)\bigr)_{t\geq0}$ is called \textbf{S}elf-\textbf{S}imilar
if for all $a>0$ there exists a constant $b>0$ such that
\[ X(at) \overset{\Dcal}{=} b X(t) \]
where $\overset{\Dcal}{=}$ is understood as the equality of all finite joint distributions.

A stochastic process $\bigl(X(t)\bigr)_{t\geq0}$ is called \textbf{H}urst \textbf{S}elf
-\textbf{S}imilar process, or $H$-SS, if for all $a>0$ and all $t\geq 0$
\[ X(at) \overset{\Dcal}{=} a^H X(t) \]
or equivalently $X(t)\overset{\Dcal}{=} a^{-H} X(at)$ for all $t\geq 0$ and $a>0$.
Furthermore $H$-SS condition implies $ X(t) \overset{\Dcal}{=} t^H X(1) $.

An $H$-SS process is a special case of an SS process, in that it must necessarily
have $X(0) = 0$ almost surely and must be stochastically continuous. A process
$\bigl(X(t)\bigr)_{t\geq0}$ is stochastically continuous at $t\geq 0$ if $X(t+h) = X(t) + o_p(h)$:
for any $\epsilon>0$
\[
\lim_{h\to 0} \pr\Bigl( \bigl\lvert X(t+h) - X(t)\bigr\rvert > \epsilon \Bigr) = 0
\]
For example, one can say that an $H$-self-similar process is stochastically continuous at $0$.

Indeed, for any $t>0$, $\epsilon>0$ and any sequence $\Delta_n\to 0$ as $n\to \infty$
one has the following chain reasoning: $H$-self-similarity of $X(t)$ implies that
the random variable $\lvert X(t) - X(t+h_n) \rvert$ has the same distribution as
$|X(1)|\bigl\lvert t^H - (t+h_n)^H \bigr\rvert$, whence

is equivalent to $\lvert X(t) - X(t+h_n) \rvert > \epsilon$
\begin{enumerate}
	\item
\end{enumerate}

Indeed, for any $\epsilon>0$ $H$-self-similarity
implies that $\pr\bigl( |X(0)|>\epsilon \bigr) = \pr\bigl( a^{-H} |X(0)|>\epsilon \bigr)$
whence for any $n\geq 0$
\[
\pr\bigl( |X(0)|>\epsilon \bigr) = \pr\bigl( |X(0)| > a^{nH} \epsilon \bigr)
\]
Since the events $\Bigl\{|X(0)| > a^{nH} \epsilon\Bigr\}$ decrease monotonically
toward $\emptyset$, it is therefore true that $\pr\bigl( |X(0)|>\epsilon \bigr) \downarrow 0$
by monotonicity of probability measure $\pr$.

A stochastic process $\bigl(X(t)\bigr)_{t\geq0}$ is said to have \textbf{s}tationary
\textbf{i}ncrements if any finite-dimensional joint distribution of the increments of
$X(t)$ defined as $U_s(t) = X(t+s)-X(t)$ is independent of $s\geq0$, i.e. invariant
under time shifts. (\textbf{CHECK THIS!})

The subject of this study, the $H$-SSSI process is a Hurst self-similar process
$\bigl(X(t)\bigr)_{t\geq0}$ with stationary increments.

For an $H$-self-similar process $\bigl(X(t)\bigr)_{t\geq 0}$ it must necessarily
be true that $X(0) = 0$ almost surely. Hence $X(0) = 0$ almost surely.

A random variable $Z$ is non-trivial if $\pr(|Z| > 0 ) > 0$ and a process $\bigl(X(t)\bigr)_{t\geq0}$
is non-trivial if there exists $t\geq 0$ such that the random variable $X(t)$ is non-trivial.

Consider some non-trivial random variable $Z$ in $\Real^d$. Suppose that for some
$b_1,b_2>0$ it is true that $b_1 Z\overset{\Dcal}{\sim} b_2 Z$. Then for any $\epsilon>0$:
\[
\pr\bigl( |Z| > \epsilon \bigr)
= \pr\bigl( b_1 |Z| > b_1 \epsilon \bigr)
= \Bigl[ b_1 Z\overset{\Dcal}{\sim} b_2 Z \Bigr]
= \pr\bigl( b_2 |Z| > b_1 \epsilon \bigr)
\]
from which it follows that for all $n\geq 0$
\[
\pr\bigl( |Z| > \epsilon \bigr) = \pr\biggl( |Z| > \frac{b_1^n}{b_2^n} \epsilon \biggr)
\]
Since $b_1>b_2$, the family of events $A_n = \bigl\{ |Z| > \sfrac{b_1^n}{b_2^n} \epsilon \bigr\}$
is nested $A_{n+1}\subseteq A_n$ and $A_n \downarrow \emptyset$, which implies that
$\pr(A_n) \downarrow 0$. Therefore $\pr(|Z|> \epsilon) = 0$ for all $\epsilon>0$,
contradicting the non-triviality of $Z$. Therefore for a non-trivial random variable
$Z$ if $b_1,b_2>0$ are such that $b_1 Z\overset{\Dcal}{\sim} b_2 Z$ then $b_1 = b_2$.

Now suppose a process $\bigl(X(t)\bigr)_{t\geq0}$ is non-trivial, stochastically
continuous at $t=0$ and self-similar. Then there is $H>0$ such that

\[ \ldots \]

% subsection hurst_self_similar_stochastic_processes_with_independent_increments (end)

\subsection{Examples of scale invariant processes} % (fold)
\label{sub:examples_of_scale_invariant_processes}

\subsubsection{FBM} % (fold)
\label{ssub:fbm}

One of the most prominent example of an $H$-SSSI process is the Brownian Motion
defined as follows:

-
A stationary \textbf{f}ractional \textbf{G}aussian \textbf{N}oise is a sequence
of random variables $(\xi_0)_{n\geq1}$ such that each $\xi_n$ is identically $\Ncal(0,\sigma^2)$
distributed, but the autocorrelation function is
\[
\ex\Bigl( \xi_0 \xi_n\Bigr) = \frac{\sigma^2}{2} \Bigl( |n+1|^{2H} - 2|n|^{2H} + |n-1|^{2H} \Bigr)
\]
Usually the fGN is defined through first differences of a fractional Brownian motion.

The most prominent example of an $H$\textbf{-SS} process is the famous Brownian Motion.

For BM and FBM refer to \cite{embrechtsselfsimilar} pp. 4-5 and \cite{embrechts2000introduction} pp. 5-8

In fact, fractional Brownian motion is an example of a broader class of self-similar processes known as
the \textbf{Hermite} processes.

A fractional Browninan Motion with can be written in the stochastic integral form
\[ B^H(t) = \int_0^t K^H(t,s) dW_s \]
where $(W_s)_{s\in[0,1]}$ is a standard Wiener process on $[0,1]$, $K^H(t,s)$
-- is the integral kernel given for $t>s$ by
\[
K^H(t,s)
= \bigl( \tfrac{ H(2H-1) }{\beta(2-2H, H-\tfrac{1}{2})} \bigr)^{\tfrac{1}{2}}
\cdot s^{\frac{1}{2}-H} \int_s^t (u-s)^{H-\frac{1}{2}}u^{H-\frac{1}{2}}du \]
with $\beta(,\cdot, \cdot)$ being the Beta function (see \cite{Chronopoulou:1114288}).

% subsubsection fbm (end)

\subsubsection{Hermite} % (fold)
\label{ssub:hermite}

Hermite processes inherit their name from the stochastic integral kernel used
in their definition.

A probabilistic Hermite polynomial of order $k\geq0$ is defined as
\[ H_k(x) = (-1)^k e^{-\frac{x^2}{2}} \frac{d^k}{dx^k} e^{-\frac{x^2}{2}} \]
and is a solution to the following differential equation
\[
 \frac{d}{dx}\biggl( e^{-\frac{x^2}{2}} \frac{d}{dx} f\biggr) + \lambda e^{-\frac{x^2}{2}} f = 0
\]
These polynomials constitute an orthogonal basis of the Hilbert space $\Lcal^2(\Real, \mu)$
with measure $\mu$ being the Lebesgue integral $\int e^{-\frac{x^2}{2}} dx$ and the inner
product given by
\[
\langle f, g\rangle = \int_\Real f g d\mu = \int_\Real f g e^{-\frac{x^2}{2}} dx
\]

As opposed to the case of fBM, a Hermite process is defined though stochastic
integral of a special kernel. The Hermite process of order $m$ with self-similarity
parameter $H\in(\tfrac{1}{2},1)$, denoted by $(Z^{mH}(t))_{t\in[0,1]}$, is defined as
\[
Z^{mH}(t) = \underset{\Real^m}{\int \cdots \int} \Biggl(
\int^t_0 \prod_{k=1}^m (u-x_k)_+^{-\frac{1}{2}-\frac{1-H}{m}} du\Biggr) dW(x_1) \ldots dW(x_q)
\]
The Hermite process of order $1$ is fractional Brownian Motion, whereas higher order
Hermite processes correspond to higher order Wiener-chaos (\cite{Bai20141710})

\noindent\textbf{CHECK THIS}.
The fundamental theorem of Lamperti (\cite{lamperti}) states that $H$-sssi process
is the only limiting law of normalized partial sum of a stationary sequence.
Formally, if $(X_i)_{i\geq1}$ is stationary and for some $a_n\to \infty$
\[ \frac{1}{a_n} \sum_{i=1}^{[nt]} X_i \overset{\Dcal}{\rightarrow} y(t) \]
where convergence is in all finite-dimensional distributions, then the process
then there  exists $H>0$ such that $(Y_t)_{t\geq0}$ is $H$-SSSI and $a_n$ is regularly
varying with exponent $H$.

For example, if $(X_i)_{i\geq 1}$ is an iid sequence, or a \textbf{s}hort-\textbf{r}ange
\textbf{d}ependent, then the limit of normed partial sums is the Brownian Motion,
which is $\frac{1}{2}$-SSSI. In case if the stationary sequence $X_i$ is \textbf{l}ong-
\textbf{r}ange \textbf{d}ependent, the limit $Y(t)$ is often $H$-SSSI with $H>\frac{1}{2}$.

Consider a $(\xi_i)_{i\geq1}$ is a stationary fractional Gaussian Noise with
autocorrelation $r_n = L(n) n^{2\frac{H-1}{m}}$ for some slowly varying function $L(n)$.

It is known (non central limit theorem?) that if $H_m$ is a Hermite polynomial of
order $m$, then for all $t\in [0,1]$
\[ n^{-H} \sum_{i=1}^{[nt]} H_m(\xi_i) \overset{\Dcal}{\rightarrow} Z^{mH}(t) \]

The construction of a Hermite process begins with a stationary Gaussian noise.

A \textbf{Hermite} process borrows it name from the

% subsubsection hermite (end)

\subsubsection{Weierstrass} % (fold)
\label{ssub:weierstrass}

% subsubsection weierstrass (end)

% subsection examples_of_scale_invariant_processes (end)

% section stochastic_self_similarity (end)

\section{Fractional Brownian Motion} % (fold)
\label{sec:fractional_brownian_motion}

\subsection{Definiton} % (fold)
\label{sub:definiton}
Brief history, relation to H-SSSI processes.

Mention that BM is fBM for $H=0.5$.

\subsubsection{Sample paths} % (fold)
\label{ssub:sample_paths}

Reference the generation circulant embedding generation algorithm with complexity.

% subsubsection sample_paths (end)

% subsection definiton (end)

% section fractional_brownian_motion (end)

\section{Brownian Motion} % (fold)
\label{sec:brownian_motion}

\section{Literature review} % (fold)
\label{sec:literature_review}
Reviewed papers \cite{jones2004}, \cite{jonesshen2005} and \cite{decrouez2013}.

% section literature_review (end)

\subsection{Properties of the crossing tree} % (fold)
\label{sub:properties_of_the_crossing_tree}

% subsection properties_of_the_crossing_tree (end)

\subsection{Simulation study} % (fold)
\label{sub:simulation_study_bm}

% subsection simulation_study_bm (end)

% section brownian_motion (end)

\section{Conjecture for FBM with $H\in (\sfrac{1}{2},1)$} % (fold)
\label{sec:conjecture_for_fbm}

\subsection{Statement} % (fold)
\label{sub:statement}

% subsection statement (end)

\subsection{Simulation study} % (fold)
\label{sub:simulation_study_fbm}

See the paper by Owen Jones and Shen (2005) for the outline.

Each MonteCarlo realisation generates a discretized sample path $(t_i, X_i)_{i=0}^N$ of
a particular continuous stochastc process $X(t)$, where $X_i = X(t_i)$ and $(t_i)_{i=0}^N\in [0,1]$
is a uniformly spaced mesh with
\[0 = t_0 \geq \ldots \geq t_i < t_{i+1} \geq \ldots \geq t_N = 1\]

Main plots: let $\Delta X_i = X_i - X_{i-1}$ for $i=1,\ldots, N$.
\begin{itemize}
	\item for $\delta = \text{std}\bigl(\Delta X_i \bigr)$, where $\text{std}(Y^n)$ is
	the square root of the unbiased sample estimator of varaince of $Y$:
	\[ \text{std}(Y^n) = \sqrt{ \frac{1}{n-1} \sum_{i=1}^n \bigl( Y_i - \bar{Y}_n \bigr)^2 }\]
	and $\bar{Y}_n$ is tha sample mean: $\bar{Y}_n = \frac{1}{n}\sum_{i=1}^n Y_i$.

	\item for $\delta = \text{iqr}\bigl(\Delta X_i \bigr)$, where $\text{iqr}(Y^n)$ is
	the \textbf{i}nter\textbf{q}uartile \textbf{r}ange of the sample $Y^n = (Y_i)_{i=1}^n$
	defiend as the difference of the $75\%$ and the $25\%$ sample quartiles of the sample:
	\[\text{iqr} = \hat{F}^{-1}_n\Bigl(\frac{3}{4}\Bigr) - \hat{F}^{-1}_n\Bigl(\frac{1}{4}\Bigr)\]
	where $\hat{F}^{-1}_n(p)$ is the generalized inverse of the empirical CDF of the sample $Y^n$
	given by
	\[\hat{F}_n(y) = \frac{1}{n} \sum_{i=1}^n 1_{(-\infty,y]}(Y_i)\]
	Ratinale for the IQR is that it is robust (this is poor!).
	\item for $\delta = \hat{F}^{-1}_n\Bigl(\frac{3}{4}\Bigr)$ -- the $75\%$ empirical qauntile of
	the process of increments $(\Delta_i)_{i=1}^N$.
\end{itemize}

Each group of plots should have: \begin{itemize}
	\item a plot of subcrossing distribution averaged across the MC realisations with
	the theoretical values;
	\item add histograms of $\pr(Z_n = 2k)$ (separately for $k=1,2,\ldots$) with
	superimposed theoretical $\theta = 2^{1-H^{-1}}$ and averaged values;
	$Z\sim\text{Geom}$ with porbability given by
	\[\pr(Z = 2k) = (1-\theta)^{k-1} \theta\]
	for $k\geq 1$.
	\item a table of excursion distribution averaged across the MC realisations;
	\item add histograms of $\pr(+-|++)$ and $\pr(+-|--)$ with superimposed theoretical
	and averaged values. Let $\mu = \ex Z$, which is $\frac{2}{\theta}$. Then the
	hypothesized probability of an up-down excursion $\delta 2^n$ in an upcrossing
	of resolution $\delta 2^{n+1}$ is $\frac{1}{\sqrt{\mu}}$.
\end{itemize}

% subsection simulation_study_fbm (end)


\textit{Calculus} means ``a method of calculation or reasoning.'' When one computes the sales tax on a purchase, one employs a simple calculus. When one finds the area of a polygonal shape by breaking it up into a set of triangles, one is using another calculus. Proving a theorem in geometry employs yet another calculus.

Despite the wonderful advances in mathematics that had taken place into the first half of the $17^\text{th}$ century, mathematicians and scientists were keenly aware of what they \textit{could not do.} (This is true even today.) In particular, two important concepts eluded mastery by the great thinkers of that time: area and rates of change.

Area seems innocuous enough; areas of circles, rectangles, parallelograms, etc., are standard topics of study for students today just as they were then. However, the areas of \textit{arbitrary} shapes could not be computed, even if the boundary of the shape could be described exactly.

Rates of change were also important. When an object moves at a constant rate of change, then ``distance = rate $\times $ time.'' But what if the rate is not constant -- can distance still be computed? Or, if distance is known, can we discover the rate of change?

It turns out that these two concepts were related. Two mathematicians, Sir Isaac Newton and Gottfried Leibniz, are credited with independently formulating a system of computing that solved the above problems and showed how they were connected. Their system of reasoning was ``a'' calculus. However, as the power and importance of their discovery took hold, it became known to many as ``the'' calculus. Today, we generally shorten this to discuss ``calculus.''

The foundation of ``the calculus'' is the \textit{limit.} It is a tool to describe a particular behavior of a function. This chapter begins our study of the limit by approximating its value graphically and numerically. After a formal definition of the limit, properties are established that make ``finding limits'' tractable. Once the limit is understood, then the problems of area and rates of change can be approached.

\section{The tangent problem}\label{sec:TangentProblem} %
Consider the computer- generated plot of the function $f(x)=\frac{x^2}2$ below. The line drawn in blue in Figure \ref{figTangentIdeaFor} appears to just touch the graph of the function $y=\frac{x^2}2$ at the point $P=(2,f(2))=(2, \frac{2^2}{2}) =(2,2)$. In mathematical language we say that the blue line is tangent to the graph of $ y=\frac{x^2}2$ (``tangent'' comes from Latin, ``to touch''). We shall give a formal definition of a tangent line later in Section \ref{secDefTangent}; until then, we shall refer to a ``tangent line'' informally, relying on the reader's intuition.

\begin{figure}[H]
	\centering
     \begin{tikzpicture}
     \begin{axis}[ %
     clip=false,
     %minor x tick num=1,
     axis y line=middle,
     axis x line=middle,
     ymin=-1,
     ymax=5,
     %extra y tick labels={},
     xmin=-1,
     xmax=3.5,
     name=myplot]
     \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
     \addplot [{\colorone},thick, smooth,domain=0.5:3,samples=20] ({x},{2*x-2});
     \coordinate (P) at (axis cs:2,2);
     %\coordinate (Q) at (axis cs:1.9,1.805);
     \end{axis}
     \node [right] at (myplot.right of origin) { $x$};
     \node [above] at (myplot.above origin) { $f$};
     %\draw [color=red, thick, shorten >= -1.9cm, shorten <=-1.9cm] (P)--(Q);
     \draw[color=\colorone,fill] (P) circle (0.1) node[above] {\scriptsize $P$} node[right] {\scriptsize $(2,2)$};
    % \draw[color=red,fill] (Q) circle (0.1)node[above] {\scriptsize $Q$} node[right] {\scriptsize $(1.9,1.805)$};
     \end{tikzpicture}
    \caption{   \label{figTangentIdeaFor} }
\end{figure}

We are seeking an equation for the tangent through $P=(2,2)=(2, f(2))$ drawn in \ref{figTangentIdeaFor}. We know one point on the tangent line - namely the point $P=(2,2)$. Recall that every non-vertical line has equation
\[
y=mx+c
\]
for some numbers $m$ and $c$, where $m$ is called the slope
of the line and $c$ is called the $y$-intercept of the line. As the tangent line passes through the point $P=(2,2)$, it has equation $y-2=m(x-2)$ for some slope $m$ that we yet need to define.

It is natural to approximate the tangent line using secant lines passing through the point $ P=(2, 2)$ and nearby points $Q=(t,f(t))=(t, \frac{t^2}{2})$ lying on the graph of $f(x)$. The line passing through $P=(2,2) $ and $Q=(t,f(t))$ has slope $m_{PQ}:=\frac{f(t)-2}{t-2}$ and therefore has equation
\[
y-2=m_{PQ}(x-2), \quad\quad \quad\text{where~} m_{PQ}= \left(\frac{f(t)-2}{t-2}\right).
\]
As the equation of the tangent line is $y-2=m(x-2)$, we choose to approximate $m$ by the numbers $m_{PQ}$ as $Q$ gets close to the point $P$. On the other hand, the point $Q=(t,f(t))$ gets closer to $P= (x, f(x))$ as $t$ gets closer to $x$.

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.33\textwidth}
		 \begin{tikzpicture}
		 \begin{axis}[ %
		 width=1.1\textwidth,
		 clip=false,
		 %minor x tick num=1,
		 axis y line=middle,
		 axis x line=middle,
		 ymin=-1,
		 ymax=5,
		 %extra y tick labels={},
		 xmin=-1,
		 xmax=3.5,
		 name=myplot]
		 \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
		 \coordinate (P) at (axis cs:2,2);
		 \coordinate (Q) at (axis cs:1,0.5);
		 \end{axis}
		 \node [right] at (myplot.right of origin) { $x$};
		 \node [above] at (myplot.above origin) { $f$};
		 \draw [color=\colorone, thick, shorten >= -1cm, shorten <=-1cm] (P)--(Q);
		 \draw[color=\colorone,fill] (P) circle (0.1) node[above] {\scriptsize $P$} node[right] {\scriptsize $(2,2)$};
		 \draw[color=\colorone,fill] (Q) circle (0.1)node[above] {\scriptsize $Q$} node[right] {\scriptsize $(1,1.5)$};
		 \end{tikzpicture}
        \label{ }
        \caption{$ t=1 $, $ m_{PQ}=1.5 $}
    \end{subfigure}%
 \begin{subfigure}[t]{0.33\textwidth}
     \begin{tikzpicture}
     \begin{axis}[ %
      width=1.1\textwidth,
     clip=false,
     %minor x tick num=1,
     axis y line=middle,
     axis x line=middle,
     ymin=-1,
     ymax=5,
     %extra y tick labels={},
     xmin=-1,
     xmax=3.5,
     name=myplot]
     \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
     \coordinate (P) at (axis cs:2,2);
     \coordinate (Q) at (axis cs:1.5,1.125);
     \end{axis}
     \node [right] at (myplot.right of origin) { $x$};
     \node [above] at (myplot.above origin) { $f$};
     \draw [color=\colorone, thick, shorten >= -1.5cm, shorten <=-1.5cm] (P)--(Q);
      \draw[color=\colorone,fill] (P) circle (0.1) node[left] {\scriptsize $P$} node[right] {\scriptsize $(2,2)$};
      \draw[color=\colorone,fill] (Q) circle (0.1)node[left] {\scriptsize $Q$} node[right] {\scriptsize $(1.5,1.125)$};
     \end{tikzpicture} %
        \label{ }
        \caption{$ t=1.5 $, $ m_{PQ}=1.75 $}
    \end{subfigure}
 \begin{subfigure}[t]{0.33\textwidth}
     \begin{tikzpicture}
     \begin{axis}[ %
      width=1.1\textwidth,
     clip=false,
     %minor x tick num=1,
     axis y line=middle,
     axis x line=middle,
     ymin=-1,
     ymax=5,
     %extra y tick labels={},
     xmin=-1,
     xmax=3.5,
     name=myplot]
     \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
     \coordinate (P) at (axis cs:2,2);
     \coordinate (Q) at (axis cs:1.9,1.805);
     \end{axis}
     \node [right] at (myplot.right of origin) { $x$};
     \node [above] at (myplot.above origin) { $f$};
     \draw [color=\colorone, thick, shorten >= -1.9cm, shorten <=-1.9cm] (P)--(Q);
     		 \draw[color=\colorone,fill] (P) circle (0.1) node[above] {\scriptsize $P$} node[right] {\scriptsize $(2,2)$};
     		 \draw[color=\colorone,fill] (Q) circle (0.1)node[left] {\scriptsize $Q$} node[below right] {\scriptsize $(1.9,1.805)$};
     \end{tikzpicture}
        \label{ }
      \caption{$ t=1.9 $, $ m_{PQ}=1.95 $}
    \end{subfigure}
    \caption{ Slopes of secant lines as $ Q $ approaches $ P $ from the left.  \label{tangentleft} }
\end{figure}

\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{0.33\textwidth}
		 \begin{tikzpicture}
		 \begin{axis}[ %
		 width=1.1\textwidth,
		 clip=false,
		 %minor x tick num=1,
		 axis y line=middle,
		 axis x line=middle,
		 ymin=-1,
		 ymax=5,
		 %extra y tick labels={},
		 xmin=-1,
		 xmax=3.5,
		 name=myplot]
		 \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
		 \coordinate (P) at (axis cs:2,2);
		 \coordinate (Q) at (axis cs:3,4.5);
		 \end{axis}
		 \node [right] at (myplot.right of origin) { $x$};
		 \node [above] at (myplot.above origin) { $f$};
		 \draw [color=\colorone, thick, shorten >= -1cm, shorten <=-1cm] (P)--(Q);
		 \draw[color=\colorone,fill] (P) circle (0.1) node[left] {\scriptsize $P$} node[below right] {\scriptsize $(2,2)$};
		 \draw[color=\colorone,fill] (Q) circle (0.1)node[above left] {\scriptsize $Q$} node[right] {\scriptsize $(3,4.5)$};
		 \end{tikzpicture}
        \label{ }
        \caption{$ t=3 $, $ m_{PQ}=2.5 $}
    \end{subfigure}%
 \begin{subfigure}[t]{0.33\textwidth}
     \begin{tikzpicture}
     \begin{axis}[ %
      width=1.1\textwidth,
     clip=false,
     %minor x tick num=1,
     axis y line=middle,
     axis x line=middle,
     ymin=-1,
     ymax=5,
     %extra y tick labels={},
     xmin=-1,
     xmax=3.5,
     name=myplot]
     \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
     \coordinate (P) at (axis cs:2,2);
     \coordinate (Q) at (axis cs:2.5,3.125);
     \end{axis}
     \node [right] at (myplot.right of origin) { $x$};
     \node [above] at (myplot.above origin) { $f$};
     \draw [color=\colorone, thick, shorten >= -1.5cm, shorten <=-1.5cm] (P)--(Q);
      \draw[color=\colorone,fill] (P) circle (0.1) node[left] {\scriptsize $P$} node[below right] {\scriptsize $(2,2)$};
      \draw[color=\colorone,fill] (Q) circle (0.1)node[above left] {\scriptsize $Q$} node[right] {\scriptsize $(2.5,3.125)$};
     \end{tikzpicture} %
        \label{ }
        \caption{$ t=2.5 $, $ m_{PQ}=2.25 $}
    \end{subfigure}
 \begin{subfigure}[t]{0.33\textwidth}
     \begin{tikzpicture}
     \begin{axis}[ %
      width=1.1\textwidth,
     clip=false,
     %minor x tick num=1,
     axis y line=middle,
     axis x line=middle,
     ymin=-1,
     ymax=5,
     %extra y tick labels={},
     xmin=-1,
     xmax=3.5,
     name=myplot]
     \addplot [{\colortwo},thick, smooth,domain=-1:3,samples=20] ({x},{x^2/2});
     \coordinate (P) at (axis cs:2,2);
     \coordinate (Q) at (axis cs:2.1,2.205);
     \end{axis}
     \node [right] at (myplot.right of origin) { $x$};
     \node [above] at (myplot.above origin) { $f$};
     \draw [color=\colorone, thick, shorten >= -1.9cm, shorten <=-1.9cm] (P)--(Q);
     		 \draw[color=\colorone,fill] (P) circle (0.1) node[left] {\scriptsize $P$} node[below right] {\scriptsize $(2,2)$};
     		 \draw[color=\colorone,fill] (Q) circle (0.1)node[above left] {\scriptsize $Q$} node[right] {\scriptsize $(2.1,2.205)$};
     \end{tikzpicture}
        \label{ }
      \caption{$ t=2.1 $, $ m_{PQ}=2.05 $}
    \end{subfigure}
    \caption{ Slopes of secant lines as $ Q $ approaches $ P $ from the right.  \label{tangentright} }
\end{figure}

In Table \ref{tableTangentIdeaForTable}, we have computed the values of $f(t)$ and $m_{PQ}=\frac{f(t)-2}{t-2}$ for various values of $t$ close to $x=2$.  In Figures \ref{tangentleft}-\ref{tangentright}, and in Table \ref{tableTangentIdeaForTable} we see that as $t$ gets closer to $2$, $m_{PQ}$ appear to get closer to the number $2$, and indeed, we can say that $m_{PQ}$ ``gets infinitely close to $2$ as $t$ approaches $2$'' and so we can define $m=2$. Then the equation of the equation of the tangent line at $P$ becomes
\[
y-2=2(x-2)\quad ,
\]
which is exactly the equation of the line plotted in blue in Figure \ref{figTangentIdeaFor}.

\mTable{1}{Values of $f(x)$ and slope of line through $P,Q$.}{tableTangentIdeaForTable}{\begin{tabular}{c|c|c|c|c}
$x$& $f(x)=\frac{t^2}2$& $t-2$ & $f(t)-f(2)$ & $m_{PQ}=\frac{f(t)-f(2)}{t-2}$ \\\hline
0& 0& -2& -2& 1 \\
1& 0.5& -1& -1.5& 1.5 \\
1.5& 1.125& -0.5& -0.875& 1.75\\
1.9& 1.805& -0.1& -0.195& 1.95\\
1.99& 1.98005& -0.01& -0.01995& 1.995\\
1.999& 1.998& -0.001& -0.0019995& 1.9995\\\hline
2.001& 2.002& 0.001& 0.0020005& 2.0005\\
2.01& 2.02005& 0.01& 0.02005& 2.005\\
2.1& 2.205& 0.1& 0.205& 2.05\\
2.5& 3.125& 0.5& 1.125& 2.25\\
3& 4.5& 1& 2.5& 2.5\\
4& 8& 2& 6& 3 \\
\end{tabular}}

In order to give a strict definition of tangent, we need to define formally what it means for $m_{PQ}$ to ``get infinitely close'' to the number $2$. The colloquial phrase ``to get infinitely close to,'' corresponds to the mathematical notion of taking limits.

\subsection*{Velocity:}

A similar idea is behind our notion of \textit{velocity}. We can see immediately
how fast we are going, that is we can find the \textit{instantaneous velocity} of a
car we are driving just by looking at the speedometer. But what is that actually
measuring? The \textit{average velocity} makes sense, it's the distance travelled
over a specific interval of time. But the speedometer doesn't measure that, it
has a reading at each instant, and does not calculate an average velocity.

But the idea of instantaneous velocity of a moving car, at a specific time,
has to be interpreted as another limiting process, taking average velocities
over shorter and shorter time intervals:\[
velocity\, \, v:=\lim _{\Delta t\rightarrow 0}\frac{\Delta distance}{\Delta t}\]
where \( \Delta t \) means {}``the change in time{}'', \( \Delta =change\, in \).

If you plot the distance on the vertical axis, and time on the horizontal, then
you have the same picture as before, and the velocity is the slope of that graph.

%In the following exercises, we continue our introduction and approximate the value of limits.\\
%
%\printexercises{exercises/01_01_exercises}

%One-sides:
%
%\printexercises{exercises/01_04_exercises}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for Section \ref{sec:TangentProblem}}

\begin{multicols}{2}

\begin{enumialphparenastyle}

%%%%%%%%%%
% % % % % % % % % % %
\begin{ex}
Estimate the slope of the tangent line to the curve $f(x)=2\sin\frac{x}{2}$ at
the point $(\frac{\pi}{2},\sqrt{2})$.
 \begin{tikzpicture}[line width=1pt,scale=1]
\draw[->] (0,-.3) -- (0,2.5);
\foreach \x in {1,2}
{\draw (-.1,\x) --node[left]{\x} (.1,\x);
}
\draw[->] (-.3,0) -- (5,0);
\foreach \x in {2,4}
{\draw (\x*pi/4,-.1) -- (\x*pi/4,.1);
}
\draw node[below=.1cm] at (pi/2,0) {$\frac{\pi}{2}$};
\draw node[below=.1cm] at (pi,0) {$\pi$};
\draw plot[smooth,mark=*] coordinates{(0,0) (pi/3,1) (pi/2,1.414) (2*pi/3,1.732)
(pi,2) (4*pi/3,1.732) (3*pi/2,1.414)};
\draw node  at (3,.7) {$f(x)=2\sin\frac{x}{2}$};
\end{tikzpicture}
\end{ex}

\begin{ex}
The following graphic shows 5 students and their grades posted compared to the
number of cookies that they made for me. Estimate the G.P.A. of a student who
makes me 17 cookies.
\[
\begin{tikzpicture}[line width=1pt,xscale=.5]
\draw[<->] (0,4.5) -- (0,0) -- (15,0);
\foreach \x in {.5,1,...,4} \draw (-.07,\x) -- (.07,\x);
\foreach \x in {1,2,3,4} \draw node[left] at (0,\x) {$\x$};
\draw node[right] at (0,4) {G.P.A};
\foreach \x in {5,10,15}
{
\draw (\x,-.07) -- (\x,.07);
\draw node[below] at (\x,0) {$\x$};
}
\draw node[above] at (15,0.2) {Cookies};
\draw plot[smooth,mark=*] coordinates{(0,1) (5,2) (10,2) (12,3) (14,3.5)};
\end{tikzpicture}
\]
\end{ex}

\begin{ex}
 Gravity on Earth dictates that a projectile with initial velocity of $v_0$ and starting height of $h_0$ has height (in feet) at time $t$ given by
\[
 h(t)=-16t^2+v_0t+h_0.
\]
A rock dropped off the Twin Falls bridge takes $ 6 $ seconds to reach the river below. Find the average velocity of the rock.
\end{ex}

\begin{ex}
  If a rock is thrown upward on the planet Oz with a velocity of $ 20 $ m/s, its height in meters $t$ seconds later is given by $y=20t-10t^2$. Find the average velocity over the given time intervals:
\begin{itemize}
    \item $[1,2]$
    \item $[1,1.5]$
    \item $[1,1.1]$
    \item $[1,1.01]$
\end{itemize}
  Estimate the instantaneous velocity when $t=1$.
\end{ex}

\end{enumialphparenastyle}

\end{multicols}


  A graph $X$ is pair $(X_0,X_1)$ of vertices and edges.
  The \emph{degree} of a vertex $x \in X_0$ is the number of edges meeting at $x$, where we count self-edges twice.
  We say that $X$ is \emph{connected} if any two vertices $x,y \in X_0$ are connected by a finite path of edges.
  \begin{mdframed}
    We will assume that all our graphs are connected and all the vertices have finite degrees.
    The set of vertices itself can be infinite.
  \end{mdframed}

  We want to think of graphs as 1-dimensional topological spaces.
  If there are no loops, we can define maps between graphs $f:X \rightarrow Y$ to be compatible pairs of maps $f_0:X_0 \rightarrow Y_0$ and $f_1:X_1 \rightarrow Y_1$, but when there are loops we need to specify directions for the loops. The map $f_1$ does not keep track of the "directions".
  More precisely, we want to be able to differentiate between the two maps
  \begin{align*}
    [0,1] &\rightarrow [0,1] &&& [0,1] &\rightarrow [0,1] \\
    x &\mapsto x &&& x&\mapsto1-x
  \end{align*}
  For this, we assign labels to each edge so that if $a$ is one direction then $a^{-1}$ is the reverse direction.
  We can then talk about the source $d_0 a$ and target $d_1 a$ vertex of each label, so that
  \begin{align*}
    d_0 a = d_1 a^{-1} && d_1 a = d_0 a^{-1}.
  \end{align*}
  Denote by $X_{\ell}$ the set of all labels (and their inverses).
  A map between the graphs $f:X \rightarrow Y$ is then a map between the label sets
  \begin{align*}
    f_\ell: X_\ell \rightarrow Y_\ell
  \end{align*}
  such that if $f_\ell(a^{-1}) = f_\ell(a)^{-1}$ and $d_i a = d_j b$ then $d_i f_\ell(a) = d_j f_\ell(b)$.
  It is an \emph{isomorphism} if it is bijective.
  \begin{align*}
    [0,1] &\rightarrow [0,1] &&& [0,1] &\rightarrow [0,1] \\
    x &\mapsto x &&& x&\mapsto1-x \\
    a &\mapsto a &&& a&\mapsto a^{-1} \\
    a^{-1} &\mapsto a^{-1} &&& a^{-1}&\mapsto a
  \end{align*}
  A map between graphs $f: X \rightarrow Y$ naturally induces a map between vertices $f_0 : X_0 \rightarrow Y_0$.
  %
  %
  %
  %
  %
  %
  %
  %
  %
  % \subsection{Covering spaces}
  % \begin{definition}
  %   We say that $\pi:X \rightarrow Y$ is a \emph{cover} or a \emph{covering map} if for every vertex $v$, every vertex $w \in \pi_0^{-1}(v)$ has the same degree as $v$.
  % \end{definition}
  % We call $\pi_0^{-1}(v)$ the fiber over $v$.
  % \begin{ex}
  %   \todo{example of covering space}
  % \end{ex}
  % We can construct covers using group actions.
  % A left group action of $G$ on a graph $X$, denoted $G \groupaction X$, is a collection of graph maps
  % \begin{align*}
  %   g \cdot - : X &\longrightarrow X\\
  %   x &\longmapsto gx
  % \end{align*}
  % satisfying $ex = x$, where $e$ is the identity element in $G$ and $g(hx) = (gh)x$ for all $g,h \in G$.
  % A left $G$-action on $X$ naturally defines left $G$-actions on $X_0$, $X_1$ and $X_\ell$.
  % The quotient graph $G \backslash X$ is then the graph with vertices, edges, and labels being $X_0/G$, $X_1/G$, and $X_\ell/G$ respectively.
  % \begin{definition}
  %   We say that $G \groupaction X$ is \emph{free} if the induced group action on the set of vertices $X_0$ is free.
  % \end{definition}
  % \begin{ex}
  %   \todo{add example of maps between $S^1$: rotation and reflection.}
  % \end{ex}
  % \begin{qbox}
  %   Show that if $G \groupaction X$ is free then $X \rightarrow G \backslash X$ is a cover.
  % \end{qbox}
  % \begin{definition}
  %   We say that a cover $\pi:X \rightarrow Y$ is a \emph{Galois cover} if there exists a left group action $G \groupaction X$ such that $Y \cong G \backslash X$.
  % \end{definition}
  % \begin{qbox}
  %   Come up with a cover that is not-Galois.
  % \end{qbox}
  %
  %
  %


\documentclass[a4paper]{article}

\usepackage{amssymb,amsmath}

\author{Edoardo Pasca}

\title{Iterative Sample Statistic}
\date{\today}

\newcommand{\avg}{\bar{v}}

\begin{document}
\maketitle
Some time ago I was sick with some fever. The only thing I could do was to lay in bed and measure my body temperature. The thermometer took so little time to measure that I tried again and again. Each time the temperature was different.

I thought to measure a few times and then to calculate the average, to get a more stable result.

Doing it without a piece of paper starts to be a bit difficult when you have to deal with more than 5 samples (at least for me). I started thinking that there must be a way to update the last calculated average with the next sample.

\section{Mean}
The average is defined as:
\begin{equation}
\langle v\rangle_N = \frac{1}{N} \sum_{i=1}^N v_i
\label{eq:avg_def}
\end{equation}

Adding one sample $v_{N+1}$, the next evaluation of the average is:
\begin{align}
\avg_{N+1}  & =  \frac{1}{N+1} \sum_{i=1}^{N+1} v_i \nonumber \\
 & =  \frac{1}{N+1} \left( \sum_{i=1}^{N} v_i + v_{N+1} \right) \nonumber\\
 & =  \frac{1}{N+1} \left( N \avg_{N} + v_{N+1} \right) \nonumber\\
 & =  \frac{N}{N+1} \avg_{N} + \frac{v_{N+1}}{N+1}
\label{eq:iterative-avg}
\end{align}

where $\avg_{N+1}$ is the average calculated with $N+1$ samples and $N$ is
the number of samples. This formula says that we can calculate the next
average by keeping in mind only 3 numbers: the previous average, the number
of samplings and the last sample. That's nice!

\section{Standard Deviation}
The variance is defined as the square of the standard deviation $\sigma$:
\begin{equation}
\sigma_N^2 = \frac{1}{N-1} \sum_{i=1}^N \left(v_i - \avg_N\right)^2
\label{eq:std_def}
\end{equation}

Again, adding the $N+1$-th sample the estimation of the variance can be
expressed as:

\begin{equation}
\sigma_{N+1}^2   =  \frac{1}{N-1} \sum_{i=1}^{N+1} \left(v_i -
\avg_{N+1}\right)^2
\end{equation}
we now use the eq. \ref{eq:std_def} to express $\avg_{N+1}$ in terms of
$\avg_N$

\begin{equation}
\sigma_{N+1}^2  = \frac{1}{N} \sum_{i=1}^{N+1} \left(v_i -
\left( \frac{N}{N+1} \avg_{N} + \frac{v_{N+1}}{N+1} \right)
 \right)^2
 \label{eq:var-n+1}
 %& = \frac{1}{N-1}\left( \sum_{i=1}^{N} \left(v_i -
 %\frac{N}{N+1} \avg_{N} + \frac{v_{N+1}}{N+1} \right)^2  +
 %\left(v_{N+1} -
 %\frac{N}{N+1} \avg_{N} + \frac{v_{N+1}}{N+1} \right)^2
 %\right) \nonumber
\end{equation}

Before going further, we better arrange the term in the sum with a nice trick:
\begin{align}
v_i - \left(\frac{N}{N+1} \avg_{N} + \frac{v_{N+1}}{N+1}\right)& =  \nonumber \\
& = v_i - \left( \avg_N - \avg_N + \frac{N}{N+1} \avg_{N}+
\frac{v_{N+1}}{N+1}\right) \nonumber \\
& = v_i - \avg_N - \left( - \avg_N + \frac{N}{N+1} \avg_{N}+
\frac{v_{N+1}}{N+1}\right) \nonumber \\
& = \left(v_i - \avg_N\right) - \left( \frac{N-(N+1)}{N+1} \avg_{N}+
\frac{v_{N+1}}{N+1}\right) \nonumber \\
& = \left(v_i - \avg_N\right) - \left(-\frac{1}{N+1} \avg_{N}+
\frac{v_{N+1}}{N+1}\right)
\label{eq:trick}
\end{align}

The term in the sum is actually expressed by the sum of two terms, the first
depending on the various samplings, $v_i$ and the previous evaluation of the
average, $\avg_N$,
and the second term depending only on the previous evaluation of the average,
$\avg_N$, the number of samplings, $N$, and the last sample, $v_{N+1}$.

\begin{align}
a_i = & v_i - \avg_N \label{eq:a_i} \\
b_{N,\avg_N} = b = & \frac{v_{N+1}-\avg_{N}}{N+1} \label{eq:b}
\end{align}

Now we can express the variance, eq. \ref{eq:var-n+1}, with the help of
these variables $a_i$ and b as,
\begin{align}
\sigma_{N+1}^2  = & \frac{1}{N} \left[\left(\sum_{i=1}^{N} \left(a_i -
b\right)^2 \right)
 + \left(a_{N+1} - b\right)^2\right] \\
 = & \frac{1}{N} \left[\left(\sum_{i=1}^{N} \left(a_i^2 +
b^2 - 2a_ib\right) \right)
 + \left(a_{N+1} - b\right)^2\right] \nonumber \\
 = & \frac{1}{N} \left[\left(\sum_{i=1}^{N} \left(a_i^2 +
b^2 \right) \right)
+ \left(a_{N+1} - b\right)^2\right] =
  \frac{1}{N} \left[\left(\sum_{i=1}^{N} a_i^2 \right) +
N b^2
 + \left(a_{N+1} - b\right)^2\right] \nonumber \\
 = & \frac{1}{N} \left[(N-1)\sigma_N^2  +
N b^2
 + \left(a_{N+1} - b\right)^2\right] \nonumber \\
 = & \frac{1}{N} \left[(N-1)\sigma_N^2  +
 \frac{N}{N+1} \left(v_{N+1} - \avg_N \right)^2
 \right] \nonumber \\
 \sigma_{N+1}^2 = & \frac{N-1}{N} \sigma_N^2  +
 \frac{1}{N+1} \left(v_{N+1} - \avg_N \right)^2
 \label{eq:iterative-var}
\end{align}

Eqs. (\ref{eq:iterative-avg}) and (\ref{eq:iterative-var}) allow to iterative
calculate the next estimation of average and variance using only the previous
estimation, the last sampling and the number of samples.

\section{Higher order moments}
As last algebraic remark,
\begin{align}
a_{N+1} - b = & (v_{N+1} - \avg_N) - \frac{1}{N+1} (v_{N+1} - \avg_N)
\nonumber \\
 = & \frac{N}{N+1} (v_{N+1} - \avg_N) \nonumber \\
 a_{N+1} - b= & N b
\end{align}
We can easily extend this treatment to higher standardized moments of the
distribution of the population:

\begin{equation}
x_k = \frac{\sum_{i=1}^N \left(v_i - \avg_N\right)^k}{\sigma^k_N} =
\frac{M_{N,k}}{\sigma^k_N}
\end{equation}
where $k$ is the moment's order. It is now very easy to calculate the

\begin{align}
M_{N+1,k} = & \sum_i^{N+1} (v_i - \avg_{N+1})^k \nonumber \\
 = & \left[\sum_i^N (a_i - b)^k\right] + (a_{N+1} - b) ^k \nonumber \\
 = & \left[\sum_i^N (a_i - b)^k \right]+ (Nb)^k
 \label{eq:kth-moment}
\end{align}

\subsection{Skewness}
The skewness is a third order moment, $k=3$.

\begin{align}
M_{N+1,3} = & \left[\sum_i^N (a_i - b)^3 \right]+ (Nb)^3 \nonumber \\
 = & \left[\sum_i^N (a_i^3 - b^3 +3a_ib^2 - 3a_i^2b) \right]+ (Nb)^3 \nonumber \\
 = & \left[\sum_i^N (a_i^3 - 3a_i^2b) \right] -Nb^3 + (Nb)^3 \nonumber \\
 = & \left[\sum_i^N (a_i^3 - 3a_i^2b) \right]+ Nb^3 (N^2-1)  \nonumber \\
 = & M_{N,3} - 3b \left[\sum_i^N a_i^2 \right]+ Nb^3 (N^2-1)  \nonumber \\
 = & M_{N,3} - 3 b (N-1)\sigma_N^2 + Nb^3 (N-1)(N+1)  \nonumber \\
\end{align}
\subsection{Kurtosis}
The kurtosis is a fourth order moment, $k=4$.

\begin{align}
M_{N+1,4} = & \left[\sum_i^N (a_i - b)^4 \right]+ (Nb)^4 \nonumber \\
 = & \left[\sum_i^N (a_i^4 +b^4-4a_i^3+5a_i^2) \right]+ (Nb)^4 \nonumber \\
 = & \left[\sum_i^N (a_i^3 - 3a_i^2b) \right] -Nb^3 + (Nb)^4 \nonumber \\
 = & \left[\sum_i^N (a_i^3 - 3a_i^2b) \right]+ Nb^3 (N^2-1)  \nonumber \\
 = & M_{N,3} - 3b \left[\sum_i^N a_i^2 \right]+ Nb^3 (N^2-1)  \nonumber \\
 = & M_{N,3} - 3 b (N-1)\sigma_N^2 + Nb^3 (N-1)(N+1)  \nonumber \\
\end{align}
\end{document}