\subsection{Even and odd functions}

\subsubsection{Defining odd and even functions}

An even function is one where:

\(f(x)=f(-x)\)

An odd function is one where:

\(f(x)=-f(-x)\)

\subsubsection{Functions which are even and odd}

If a function is even and odd:

\(f(x)=f(-x)=-f(-x)\)

\(f(x)=-f(x)\)

Then \(f(x)=0\).
\subsubsection{Scaling odd and even functions}

Scaling an even function provides an even function.

\(h(x)=c.f(x)\)

\(h(-x)=c.f(-x)\)

\(h(-x)=c.f(x)\)

\(h(-x)=h(x)\)

Scaling an odd function provides an odd function.

\(h(x)=c.f(x)\)

\(-h(-x)=-c.f(-x)\)

\(-h(-x)=c.f(x)\)

\(-h(-x)=h(x)\)

\subsubsection{Adding odd and even functions}

Note than \(2\) even functions added together makes an even function.

\(h(x)=f(x)+g(x)\)

\(h(x)=f(-x)+g(-x)\)

\(h(-x)=f(x)+g(x)\)

\(h(x)=h(-x)\)

And adding \(2\) odd functions together makes an odd function.

\(h(x)=f(x)+g(x)\)

\(h(x)=-f(-x)-g(-x)\)

\(-h(-x)=f(x)+g(x)\)

\(-h(-x)=h(x)\)

\subsubsection{Multiplying odd and even functions}

Multiplying \(2\) even functions together makes an even function.

\(h(x)=f(x)g(x)\)

\(h(-x)=f(-x)g(-x)\)

\(h(-x)=f(x)g(x)\)

\(h(-x)=h(x)\)

Multiplying \(2\) odd functions together makes an even function.

\(h(x)=f(x)g(x)\)

\(h(-x)=f(-x)g(-x)\)

\(h(-x)=(-1).(-1.)f(x)g(x)\)

\(h(-x)=h(x)\)


\section{The Beginning}
\subsection{The First Law of Thermodynamics and the Stefan-Boltzmann Equation}
The beginning of CLAuDE is based upon one of the most important laws of physics: "Energy is neither created nor destroyed, only changed from one form to another." In otherwords, if energy goes into an object it must
equal the outflowing energy plus the change of internal energy. This is captured in Stefan-Boltzmann's law (\autoref{eq:stefan-boltzmann}) \cite{stefan-boltzmann}.

Here we assume that the planet is a black body, i.e. it absorbs all radiation (energy waves, some waves are visible like light, others are invisible like radio signals) on all wavelengths.
In \autoref{eq:stefan-boltzmann} the symbols are:

\begin{itemize}
    \item $S$: The energy that reaches the top of the atmosphere, coming from the sun or a similar star, per meters squared $Jm^{-2}$. This is also called the insolation.
    \item $\sigma$: The Stefan-Boltzmann constant, $5.670373 \cdot 10^-8 \ (Wm^{-2}K^{-4})$ \cite{stefan-boltzmann}.
    \item $T$: The temperature of the planet ($K$).
\end{itemize}

Technically speaking \autoref{eq:stefan-boltzmann} is incorrect, as there is a mismatch in units. However, that is corrected in \autoref{eq:basis sphere final} so there is no need to worry about
it just yet. The energy difference between the energu that reaches the atmosphere and the temperature of the planet must be equal to the change in temperature of the planet, which is written in
\autoref{eq:sb rewritten}. The symbols on the right hand side are:

\begin{itemize}
    \item $\Delta U$: The change of internal energy ($J$) \cite{thermo1}.
    \item $C$: The specific heat capacity of the object, i.e. how much energy is required to heat the object by one degree Kelvin ($\frac{J}{K}$).
    \item $\Delta T$: The change in temperature ($K$).
\end{itemize}

We want to know the change of temperature $\Delta T$, so we rewrite the equation into \autoref{eq:sb rewritten2}. Here we added the $\delta t$ term to account for the time difference (or time step). This is needed as
we need an interval to calculate the difference in temperature over. Also we needed to make the units match, and by adding this time step the units all match up perfectly.

\begin{subequations}
    \begin{equation}
        S = SB = \sigma T^4
        \label{eq:stefan-boltzmann}
    \end{equation}
    \begin{equation}
        S - \sigma T^4 = \Delta U = C \Delta T
        \label{eq:sb rewritten}
    \end{equation}
    \begin{equation}
        \Delta T = \frac{\delta t(S - \sigma T^4)}{C}
        \label{eq:sb rewritten2}
    \end{equation}
    \label{eq:basis}
\end{subequations}

The set of equations in \autoref{eq:basis} form the basis of the temperature exchange of the planet. However two crucial aspects are missing. Only half of the planet will be receiving light from
the sun at once, and the planet is a sphere. So we need to account for both in our equation. We do that in \autoref{eq:basis sphere correction}. We view the energy reacing the atmosphere as a
circular area of energy, with the equation for the are of a circle being \autoref{eq:basis circle} \cite{areaCircle}. The area of a sphere is in \autoref{eq:basis sphere} \cite{areaSphere}. In
both equations, $r$ is the radius of the circle/sphere. By using \autoref{eq:basis circle} and \autoref{eq:basis sphere} in \autoref{eq:sb rewritten2} we get \autoref{eq:basis sphere2} where
$r$ is replaced by $R$. It is common in physics literature to use capitals for large objects like planets. However we are not done yet since we can divide some stuff out. We end up with
\autoref{eq:basis sphere final} as the final equation we are going to use.

\begin{subequations}
    \begin{equation}
        \pi r^2
        \label{eq:basis circle}
    \end{equation}
    \begin{equation}
        4 \pi r^2
        \label{eq:basis sphere}
    \end{equation}
    \begin{equation}
        \Delta T = \frac{\delta t (\pi R^2S - 4\pi R^2\sigma T^4)}{4\pi CR^2}
        \label{eq:basis sphere2}
    \end{equation}
    \begin{equation}
        \Delta T = \frac{\delta t (S - 4\sigma T^4)}{4C}
        \label{eq:basis sphere final}
    \end{equation}
    \label{eq:basis sphere correction}
\end{subequations}

\subsection{Insolation}

With the current equation we calculate the global average surface temperature of the planet itself. However, this planet does not have an atmosphere just yet. Basically we modelled the
temperature of a rock floating in space, let's change that with \autoref{eq:atmos}. Here we assume that the area of the atmosphere is equal to the area of the planet surface. Obviously
this assumption is false, as the atmosphere is a sphere that is larger in radius than the planet, however the difference is not significant enough to account for it. We also define the
atmosphere as a single layer. This is due to the accessibility of the model, we want to make it accessible, not university simulation grade. One thing to take into account for the
atmosphere is that it only partially absorbs energy. The sun (or a similar star) is relatively hot and sends out energy waves (radiation) with relatively low wavelengths. The planet is
relatively cold and sends out energy at long wavelengths. As a side note, all objects radiate energy. You can verify this by leaving something in the sun on a hot day for a while and
almost touch it later. You can feel the heat radiating from the object. The planet is no exception and radiates heat as well, though at a different wavelength than the sun. The
atmosphere absorbs longer wavelengths better than short wavelengths. For simplicity's sake we say that all of the sun's energy does not get absorbed by the atmosphere. The planet's
radiation will be absorbed partially by the atmosphere. Some of the energy that the atmosphere absorbs is radiated into space and some of that energy is radiated back onto the planet's
surface. We need to adjust \autoref{eq:basis sphere final} to account for the energy being radiated from the atmosphere back at the planet surface.

So let us denote the temperature of the planet surface as $T_p$ and the temperature of the atmosphere as $T_a$. Let us also write the specific heat capacity of the planet surface as $C_p$
instead of $C$. We add the term in \autoref{eq:atmos on surface improved} to \autoref{eq:basis sphere final} in \autoref{eq:surface change}. In \autoref{eq:atmos on surface}, $\epsilon$ is the
absorbtivity of the atmosphere, the fraction of energy that the atmosphere absorbs. We divided \autoref{eq:atmos on surface} by $\pi R^2$ as we did that with \autoref{eq:basis sphere final} as
well, so we needed to make it match that division.

\begin{subequations}
    \begin{equation}
        4\pi R^2 \epsilon \sigma T_a^4
        \label{eq:atmos on surface}
    \end{equation}
    \begin{equation}
        4\epsilon \sigma T_a^4
        \label{eq:atmos on surface improved}
    \end{equation}
    \begin{equation}
        \Delta T_p = \frac{\delta t (S + 4\epsilon \sigma T_a^4 - 4\sigma T_p^4)}{4C_p}
        \label{eq:surface change}
    \end{equation}
    \label{eq:atmos}
\end{subequations}

As you probably expected, the atmosphere can change in temperature as well. This is modelled by \autoref{eq:atmos change}, which is very similar to \autoref{eq:surface change}. There are
some key differences though. Instead of subtracting the radiated heat of the atmosphere once we do it twice. This is because the atmosphere radiates heat into space and towards the
surface of the planet, which are two outgoing streams of energy instead of one for the planet (as the planet obviously cannot radiate energy anywhere else than into the atmosphere).
$C_a$ is the specific heat capacity of the atmosphere.

\begin{equation}
    \Delta T_a = \frac{\delta t (\sigma T_p^4 - 2\epsilon\sigma T_a^4)}{C_a}
    \label{eq:atmos change}
\end{equation}

\subsection{The Latitude Longitude Grid}
With the current model, we only calculate the global average temperature. To calculate the temperature change along the surface and atmosphere at different points, we are going to use a grid.
Fortunately the world has already defined such a grid for us, the latitude longitude grid \cite{latlong}. The latitude is the coordinate running from the south pole to the north pole, with -90
being the south pole and 90 being the north pole. The longitude runs parallel to the equator and runs from 0 to 360 which is the amount of degrees that an angle can take when calculating the
angle of a circle. So 0 degrees longitude is the same place as 360 degrees longitude. To do this however we need to move on from mathematical formulae to code (or in this case pseudocode).

Pseudocode is a representation of real code. It is meant to be an abstraction of code such that it does not matter how you present it, but every coder should be able to read it and implement
it in their language of preference. This is usually easier to read than normal code, but more difficult to read than mathematical formulae. If you are unfamiliar with code or coding, look up
a tutorial online as there are numerous great ones.

The pseudocode in \autoref{alg:stream1v1} defines the main loop of the model. All values are initialised beforehand, based on either estimations, trial and error or because they are what they
are (like the Stefan-Boltzmann constant $\sigma$). The total time $t$ starts at 0 and increases by $\delta t$ after every update of the temperature. This is to account for the total time that
the model has simulated (and it is also used later). What you may notice is the $T_p[lan, lon]$ notation. This is to indicate that $T_p$ saves a value for each $lan$ and $lon$ combination.
It is initialised as all zeroes for each index pair, and the values is changed based on the calculations. You can view $T_p$ like the whole latitude longitude grid, where $T_p[lat, lon]$ is an
individual cell of that grid indexed by a specific latitude longitude combination.

\begin{algorithm}[hbt]
    \SetAlgoLined
    $\delta t \leftarrow 60 \cdot 5$ \;
    $\sigma \leftarrow 5.67 \cdot 10^{-8}$ \;
    $\epsilon \leftarrow 0.75$ \;
    $C_p \leftarrow 10^7$ \;
    $C_a \leftarrow 10^6$ \;
    $S \leftarrow 1370$ \;
    $R \leftarrow 6.4 \cdot 10^6$ \;
    $t \leftarrow 0$ \;

    \While{\texttt{TRUE}}{
        \For{$lat \in [-90, 90]$}{
            \For{$lon \in [0, 360]$}{
                $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t (S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{C_p}$ \;
                $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{C_a}$ \;
                $t \leftarrow t + \delta t$ \;
            }
        }
    }
    \caption{The main loop of the temperature calculations}
    \label{alg:stream1v1}
\end{algorithm}

\subsection{Day/Night Cycle}
As you can see, the amount of energy that reaches the atmopsphere is constant. However this varies based on the position of the sun relative to the planet. To fix this, we have to assign a function
to $S$ that gives the correct amount of energy that lands on that part of the planet surface. This is done in \autoref{alg:solar}. In this algorithm the term insolation is mentioned, which is $S$
used in the previous formulae if you recall. We use the $\cos$ function here to map the strength of the sun to a number between $0$ and $1$. The strength is dependent on the latitude, but since
that is in degrees and we need it in radians we transform it to radians by multiplying it by $\frac{\pi}{180}$. This function assumes the sun is at the equinox (center of the sun is directly
above the equator) \cite{equinox} at at all times. The second $\cos$ is needed to simulate the longitude that the sun has moved over the longitude of the equator. For that we need the difference
between the longitude of the point we want to calculate the energy for, and the longitude of the sun. The longitude of the sun is of course linked to the current time (as the sun is in a different\
position at 5:00 than at 15:00). So we need to map the current time in seconds to the interval $[0,$ seconds in a day$]$. Therefore we need the mod function. The mod function works like this:
$x$ mod $y$ means subtract all multiples of $y$ from $x$ such that $0 \leq x < y$. So to map the current time to a time within one day, we do $t$ mod $d$ where $t$ is the current time and $d$ is
the amount of seconds in a day. When we did the calculation specified in \autoref{alg:solar} we return the final value (which means that the function call is "replaced" \footnote{Replaced is not
necessarily the right word, it is more like a mathematical function $f(x)$ where $y = f(x)$. You give it an $x$ and the value that correpsonds to that $x$ is saved in $y$. So you can view the
function call in pseudocode as a value that is calculated by a different function which is then used like a regular number.} by the value that the function calculates). If the final value is less
than 0, we need to return 0 as the sun cannot suck energy out of the planet (that it does not radiate itself, which would happen if a negative value is returned).

In the second stream, it was revealed that $t$ mod $d$ in \autoref{alg:solar} should be $-t$ mod $d$ such that the sun moves in the right direction. In the first stream the sun would move to the
right (west to east), however the sun moves to the left (east to west) and so the time must be flipped in order for the model to be correct.

\begin{algorithm}[hbt]
    \SetAlgoLined
    \SetKwInput{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{insolation $ins$, latitude $lat$, longitude $lon$, time $t$, time in a day $d$}
    \Output{Amount of energy $S$ that hits the planet surface at the given latitude-time combination.}
    $longitude \leftarrow 360 \cdot \frac{(-t \text{ mod } d)}{d}$ \;
    $S \leftarrow ins \cdot \cos(lat \frac{\pi}{180}) \cos((lon - longitude) \cdot \frac{\pi}{180})$ \;
    \eIf{$S < 0$}{
        \Return{$0$}
    }{
        \Return{$S$}
    }
    \caption{Calculating the energy from the sun (or similar star) that reaches a part of the planet surface at a given latitude and time}
    \label{alg:solar}
\end{algorithm}

By implementing \autoref{alg:solar}, \autoref{alg:stream1v1} must be changed as well, as $S$ is no longer constant for the whole planet surface. So let us do that in \autoref{alg:stream1v2}. Note
that $S$ is defined as the call to \autoref{alg:solar} (as is showcased by the text \texttt{solar}). In case you are unfamiliar with calls, defining a function is defining how it works and
calling a function is actually using it.

\begin{algorithm}[hbt]
    \SetAlgoLined
    $\delta t \leftarrow 60 \cdot 5$ \;
    $\sigma \leftarrow 5.67 \cdot 10^{-8}$ \;
    $\epsilon \leftarrow 0.75$ \;
    $C_p \leftarrow 10^7$ \;
    $C_a \leftarrow 10^7$ \;
    $I \leftarrow 1370$ \;
    $R \leftarrow 6.4 \cdot 10^6$ \;
    $t \leftarrow 0$ \;
    $day \leftarrow 60 \cdot 60 \cdot 24$ \;
    $S \leftarrow$ \texttt{solar($I$, $lat$, $lon$, $t$, $day$)} \;
    $nlat$ is the amount of latitude points in the interval $[0, 90]$, how you divide them is your own choice. \;
    $nlot$ is the amount of longitude points in the interval $[0, 360]$, how you divide them is your own choice. \;

    \While{\texttt{TRUE}}{
        \For{$lat \in [-nlat, nlat]$}{
            \For{$lon \in [0, nlot]$}{
                $T_p[lat, lon] \leftarrow T_p[lat, lon] + \frac{\delta t (S + 4\epsilon \sigma (T_a[lat, lon])^4 - 4\sigma (T_p[lat, lon])^4)}{C_p}$ \;
                $T_a[lat, lon] \leftarrow T_a[lat, lon] + \frac{\delta t (\sigma (T_p[lat, lon])^4 - 2\epsilon\sigma (T_a[lat, lon])^4)}{C_a}$ \;
                $t \leftarrow t + \delta t$ \;
            }
        }
    }
    \caption{The main loop of the temperature calculations}
    \label{alg:stream1v2}
\end{algorithm}

\autoref{alg:stream1v2} calculates the values that are plotted (which is not discussed here as that is Python specific). Due to the \texttt{WHILE(TRUE)} loop, this calculation never finishes and
allows us to simulate days, weeks, months and even years of heat exchange all conveniently plotted in a graph. In Simon's implementation, the graphs update in realtime, meaning that whenever a
round of calculations has finished, they are immediately processed to be displayed in the graph.

However other forms of looking at the calculated data can be implemented, like writing a table to a txt file, saving the generated grpahs at a certain interval or spewing all the data into a csv
dataset. The possibilities are endless, and the whole goal of the model is for it to be modular. Meaning that if you want to do something with it (like have a multi-layered atmosphere instead of
a single layer atmosphere) you can just write some lines of code and run the model and it should still work. Therefore you can write your own extensions of the model to fit it to your needs and
requirements.


\documentclass[oneside]{tufte-handout}
\usepackage{amsmath}

\newcommand\code[1]{\texttt{#1}}

\title{STL Quadratic Interpolation \\ Implementation Notes}

\author{James A. Crotinger \\ email: \href{mailto:jim.crotinger@servicenow.com}{\tt jim.crotinger@servicenow.com}}

\begin{document}

\maketitle%

\section{Implementation}

\begin{fullwidth}
The algorithm used by \code{stl-decomp-4j} assumes that the input data is regularly spaced with no missing values,
allowing for a very efficient implementation of the underlying Loess smoother. Part of this formulation is explained here in order to extend the formulation to quadratic interpolation.

\section{Local Linear Interpolation}

The local (weighted) linear interpolation in \code{LinearLoessInterpolator} is a straight port of the code in the original Ratfor function \code{stl.r:est}. The input data is a sequence of data points $\{x_i, y_i\}$, where the $x_i$ are the regularly spaced grid points. The Loess interpolation of the data set at an arbitrary point $x$ can be expressed as
\begin{equation}\label{eq:interp-def}
  y(x) = \sum_{i = 1}^m \hat{w}_i(x) y_i
\end{equation}
i.e. the interpolation can be re-cast as a linear operation on the input y-values. The weights $\hat{w}_i(x)$ depend only on the original weights, $w_i$, and on geometric factors.

For linear interpolation, we desire coefficients $\alpha$ and $\beta$ such that the line
\begin{equation}\label{eq:linear}
	y(x) = \alpha + \beta x
\end{equation}
is the best fit to our set of points. The square error from a weighted least-squares fit of this curve to the training data is
\begin{equation}\label{eq:mean-square-error}
E = {\frac{1}{2}} \sum_{i=1}^m (y_i - \alpha - \beta x_i)^2 \cdot w_i
\end{equation}
where $\sum w_i = 1$ are external weights (in Loess these come from the implementation of the locality window).

\subsection{Finding $\alpha$}

The optimal choices of $\alpha$ and $\beta$ are found by differentiating Eq. (\ref{eq:mean-square-error}) with respect to each of these, setting to zero and solving:
\begin{equation}
\frac{\partial E}{\partial \alpha} = - \sum_{i=1}^m w_i (y_i - \alpha - \beta x_i) = 0
\end{equation}
Then
\begin{equation}\label{eq:alpha1}
\sum w_i y_i - \alpha \sum w_i - \beta \sum w_i x_i = 0
\end{equation}
For a given sequence ${z_i}$ we define
\begin{equation}\label{eq:avg-def}
	\langle z \rangle \equiv \sum_i w_i z_i
\end{equation}
Then we can rewrite the Eq. (\ref{eq:alpha1}) as
\begin{equation}\label{eq:alpha}
\alpha = \langle y \rangle - \beta \langle x \rangle
\end{equation}

\subsection{Finding $\beta$}

Repeating this exercise for $\beta$, skipping the intermediate details, gives
\begin{equation}\label{eq:beta}
\beta = \frac{\langle x y \rangle - \langle x \rangle \langle y \rangle}{\langle x^2 \rangle - \langle x \rangle^2}
\end{equation}

\subsection{The Weight}

Given Eqs. (\ref{eq:alpha}) and (\ref{eq:beta}),  Eq. (\ref{eq:linear}) becomes
\begin{align}
y(x) &= \alpha + \beta x \\
       &= \langle y \rangle + \beta ( x - \langle x \rangle) \\
       &= \langle y \rangle + \frac{\langle x y \rangle - \langle x \rangle \langle y \rangle}{\langle x^2 \rangle - \langle x \rangle^2} (x - \langle x \rangle)
\end{align}
Writing out the averages that involve $y$, we have:
\begin{align}
y(x) &= \sum_j w_j y_j + \sum_j \frac{x - \langle x \rangle}{\langle x^2 \rangle - \langle x \rangle^2} w_j (x_j - \langle x \rangle)y_j  \\
       &= \sum_j w_j \left[1 + \frac{x - \langle x \rangle}{\langle x^2 \rangle - \langle x \rangle^2}(x_j - \langle x \rangle) \right] y_j \\
       &= \sum_j \hat{w}_j(x) y_j
\end{align}
where:
\begin{equation}\label{eq:linear-weight}
\hat{w}_j(x) \equiv w_j\left[ 1 + \frac{(x - \langle x \rangle)(x_j - \langle x \rangle)}{\langle x^2 \rangle - \langle x \rangle^2} \right]
\end{equation}

So, given the point $x$ at which we want to perform the interpolation (or extrapolation - $x$ is not limited to being in the range of the set of grid points, $\{x_i\}$), we just calculate a geometric adjustment to the original weights, $w_i$. These various averages can be computed efficiently since the weights $w_i$ are non-zero only in the Loess window near the point $x$.

\section{Local Quadratic Interpolation}
As before, given data points $\{x_i, y_i\}, i = 1, ..., m$, and externally supplied weights $w_i, \sum_i w_i = 1$, we want to find a set of modified weights $\hat{w}_i$ such that the interpolation at a value $x$ can be written as
\begin{equation}
y(x) = \sum_{i=1}^m \hat{w}_i(x) y_i
\end{equation}
Now we model the data as a local quadratic:
\begin{equation}\label{eq:quadratic}
y(x) = a_0 + a_1 x + a_2 x^2
\end{equation}

\subsection{Finding $a_0$}
The square error in the local interpolation of the training data is:
\begin{equation}\label{eq:quad-error}
E = \frac{1}{2} \sum_i w_i (y_i - a_0 - a_1 x_i - a_2 x_i^2)
\end{equation}
Minimizing with respect to $a_0$,
\begin{equation}
\frac{\partial E}{\partial a_0} = - \sum_i w_i (y_i - a_0 - a_1 x_i - a_2 x_i^2) = 0
\end{equation}
This yields the obvious extension of the linear result:
\begin{equation}\label{eq:a0}
a_0 = \langle y \rangle - a_1 \langle x \rangle - a_2 \langle x^2 \rangle
\end{equation}
where the averages $\langle \cdot \rangle$ are defined as before.

\subsection{Finding $a_1$}
Similarly, for $a_1$ we have
\begin{align}
\frac{\partial E}{\partial a_1} &= - \sum_i w_i x_i (y_i - a_0 - a_1 x_i - a_2 x_i^2) = 0 \\
                                &= - \langle x y \rangle + a_0 \langle x \rangle + a_1 \langle x^2 \rangle + a_2 \langle x^3 \rangle = 0
\end{align}
Solving for $\langle x y \rangle$ and substituting $a_0$ from Eq. (\ref{eq:a0}):
\begin{align}
\langle x y \rangle &= \left[\langle y \rangle - a_1 \langle x \rangle - a_2 \langle x^2 \rangle \right] \langle x \rangle + a_1 \langle x^2 \rangle + a_2 \langle x^3 \rangle \\
                    &= \langle x \rangle \langle y \rangle - a_1 \langle x \rangle^2 - a_2 \langle x \rangle \langle x^2 \rangle + a_1 \langle x^2 \rangle + a_2 \langle x^3 \rangle
\end{align}
Gathering terms
\begin{equation}\label{eq:xymxy}
\langle x y \rangle - \langle x \rangle \langle y \rangle = a_1 (\langle x^2 \rangle - \langle x \rangle^2) + a_2 (\langle x^3 \rangle - \langle x^2 \rangle \langle x \rangle)
\end{equation}
We define the following geometric factors $M_2$ and $M_3$, and a correlation factor $C_{xy}$:
\begin{align}
M_2 &= \langle x^2 \rangle - \langle x \rangle^2 \label{eq:m2} \\
M_3 &= \langle x^3 \rangle - \langle x^2 \rangle \langle x \rangle  \label{eq:m3} \\
C_{xy} &= \langle x y \rangle - \langle x \rangle \langle y \rangle \label{eq:cxy}
\end{align}
Then Eq. (\ref{eq:xymxy}) can be rewritten as
\begin{equation}
C_{xy} = a_1 M_2 + a_2 M_3
\end{equation}
Solving for $a_1$
\begin{equation}\label{eq:a1}
a_1 = \frac{C_{xy}}{M_2} - a_2 \frac{M_3}{M_2}
\end{equation}

\subsection{Finding $a_2$}

The result of minimizing Eq. (\ref{eq:quad-error}) with respect to $\alpha_2$ results in an expression similar to Eq.~(\ref{eq:xymxy}), just adding another $x$ in the appropriate averages, leading to
\begin{equation}\label{eq:x2ymx2y}
\langle x^2 y \rangle - \langle x^2 \rangle \langle y \rangle = a_1 (\langle x^3 \rangle - \langle x^2 \rangle \langle x \rangle) + a_2 (\langle x^4 \rangle - \langle x^2 \rangle^2)
\end{equation}
We define the following geometric and correlation factors:
\begin{align}
M_4 &= \langle x^4 \rangle - \langle x^2 \rangle^2 \label{eq:m4} \\
C_{x^2y} &= \langle x^2 y \rangle - \langle x^2 \rangle \langle y \rangle \label{eq:cx2y}
\end{align}
Using Eq. (\ref{eq:m3}) and Eqs. (\ref{eq:m4}-\ref{eq:cx2y}), Eq. (\ref{eq:x2ymx2y}) becomes
\begin{equation*}
C_{x^2y} = a_1 M_3 + a_2 M_4
\end{equation*}
Substituting our expression for $a_1$ from Eq. (\ref{eq:a1}),
\begin{align}
C_{x^2y} &= \left[
     \frac{C_{xy}}{M_2} - a_2 \frac{M_3}{M_2}
     \right] M_3 + a_2 M_4 \\
     &= \frac{M_3}{M_2} C_{xy} + a_2 (M_4 - \frac{M_3^2}{M_2})
\end{align}
Solving for $a_2$ and simplifying
\begin{align}
a_2 &= \frac{
        C_{x^2y} - \frac{M_3}{M_2} C_{xy}
     }{
        M_4 - \frac{M_3^2}{M_2}
     } \\
     &= \frac{
        M_2 C_{x^2y} - M_3 C_{xy}
     }{
        M_2 M_4 - M_3^2
     } \label{eq:a2}
\end{align}
Substituting Eq. (\ref{eq:a2}) back into Eq. (\ref{eq:a1}) gives
\begin{align*}
a_1 &= \frac{C_{xy}}{M_2} - \frac{
        M_2 C_{x^2y} - M_3 C_{xy}
     }{
        M_2 M_4 - M_3^2
     } \frac{M_3}{M_2} \\
     &= \frac{C_{xy}}{M_2}
         - \frac{M_2 C_{x^2y}}{M_2 M_4 - M_3^2} \frac{M_3}{M_2}
         + \frac{M_3^2 }{M_2 M_4 - M_3^2} \frac{C_{xy}}{M_2} \\
     &= \frac{C_{xy}}{M_2} \frac{M_2 M_4 - M_3^2}{M_2 M_4 - M_3^2}
         + \frac{M_3^2 }{M_2 M_4 - M_3^2} \frac{C_{xy}}{M_2}
         - \frac{M_3 C_{x^2y}}{M_2 M_4 - M_3^2}
\end{align*}
So our final expression for $a_1$ is
\begin{equation}\label{eq:a1-final}
	a_1     =  \frac{M_4 C_{xy} - M_3 C_{x^2y}}{M_2 M_4 - M_3^2}
\end{equation}

\subsection{The Weight}
Returning to the interpolation expression, Eq. (\ref{eq:quadratic}), we start by substituting back $a_0$:
\begin{align}
y(x) &= a_0 + a_1 x + a_2 x^2 \\
     &= \langle y \rangle - a_1 \langle x \rangle - a_2 \langle x^2 \rangle + a_1 x + a_2 x^2 \\
     &= \langle y \rangle + a_1 (x - \langle x \rangle) + a_2 (x^2 - \langle x^2 \rangle) \label{eq:ywa1a2}
\end{align}
Defining the following geometric terms
\begin{align}
\beta_2 &= \frac{M_4}{M_2 M_4 - M_3^2} \\
\beta_3 &= \frac{M_3}{M_2 M_4 - M_3^2}\\
\beta_4 &= \frac{M_2}{M_2 M_4 - M_3^2}
\end{align}
We can rewrite Eqs. (\ref{eq:a2}) and (\ref{eq:a1-final}) as
\begin{align}
a_1 &= \beta_2 C_{xy} - \beta_3 C_{x^2 y} \\
a_2 &= \beta_4 C_{x^2y} - \beta_3 C_{xy}
\end{align}
Then Eq. (\ref{eq:ywa1a2}) becomes
\begin{align}
y(x) &= \langle y \rangle + (x - \langle x \rangle)(\beta_2 C_{xy} - \beta_3 C_{x^2 y})
       + (x^2 - \langle x^2 \rangle)(\beta_4 C_{x^2y} - \beta_3 C_{xy}) \\
       &= \langle y \rangle  + [\beta_2(x - \langle x \rangle) - \beta_3 (x^2 - \langle x^2 \rangle)] C_{xy}
       + [\beta_4(x^2 - \langle x^2 \rangle) - \beta_3 (x - \langle x \rangle)] C_{x^2y}
\end{align}
Defining the following functions of $x$
\begin{align}
\hat{a}_1(x) &\equiv \beta_2(x - \langle x \rangle) - \beta_3 (x^2 - \langle x^2 \rangle) \\
\hat{a}_2(x) &\equiv \beta_4(x^2 - \langle x^2 \rangle) - \beta_3 (x - \langle x \rangle)
\end{align}
we can write, using the definitions of $C_{xy}$ and $C_{x^2y}$, Eqs. (\ref{eq:cxy}) and (\ref{eq:cx2y}):
\begin{align}
y(x) &= \langle y \rangle + \hat{a}_1(x) C_{xy} + \hat{a}_2(x) C_{x^2y} \\
     &= \sum_i w_i y_i + \hat{a}_1(x)  \sum_i w_i (x_i - \langle x \rangle) y_i
         + \hat{a}_2(x) \sum_i w_i (x_i^2 - \langle x^2 \rangle) y_i \\
     &= \sum_i w_i y_i \left[1 + \hat{a}_1(x) (x_i - \langle x \rangle)
         + \hat{a}_2(x) (x_i^2 - \langle x^2 \rangle)\right]
\end{align}
So finally we arrive at our goal
\begin{equation}
y(x)     = \sum_i \hat{w}_i(x) y_i
\end{equation}
where
\begin{equation}
    \hat{w}_i(x) \equiv w_i \left[1 + \hat{a}_1(x) (x_i - \langle x \rangle)
         + \hat{a}_2(x) (x_i^2 - \langle x^2 \rangle)\right]
\end{equation}
The code in \code{QuadraticLoessInterpolator.updateWeights} in \code{LoessInterpolator.java} is very close to a literal transcription of the above math.

\end{fullwidth}
\end{document}


\section{$n$-step TD Prediction}
\subsection{Exercise 7.1}
\subsubsection*{Q}
In Chapter 6 we noted that the Monte Carlo error can be written as the sum of TD errors (6.6) if the value estimates don’t change from step to step. Show that the $n$-step error used in (7.2) can also be written as a sum TD errors (again if the value estimates don’t change) generalizing the earlier result.

\subsubsection*{A}
Write
\[
    G_{t:t+n} = \sum_{i=1}^n \gamma^{i-1}R_{t-i} + \gamma^n V(S_{t+n}),
\]
then have the TD error
\[
    \delta_t \doteq R_{t+1} + \gamma V(S_{t+1}) - V(S_t).
\]
So the $n$-step error can be written as
\begin{align*}
    G_{t:t+n} - V(S_t) &= R_{t+1} + \gamma \sum_{i=1}^{n-1} \gamma^{i-1} R_{t+1+i} + \gamma^n V(S_{t+n}) - V(S_t) \\
                       &= \delta_t + \gamma \left( G_{t+1:t+n} - V(S_{t+1}) \right) \\
                       &\vdotswithin{=}\\
                       &= \sum_{k=t}^{t+n-1} \gamma^{k-t} \delta_k
\end{align*}

\subsection{Exercise 7.2 (programming)}
\subsubsection*{Q}
With an $n$-step method, the value estimates \emph{do} change from step to step, so an algorithm that used the sum of TD errors (see previous exercise) in place of the error in (7.2) would actually be a slightly different algorithm. Would it be a better algorithm or a worse one? Devise and program a small experiment to answer this question empirically.

\subsubsection*{A}
...

\subsection{Exercise 7.3}
\subsubsection*{Q}
Why do you think a larger random walk task (19 states instead of 5) was used in the examples of this chapter? Would a smaller walk have shifted the advantage to a different value of $n$? How about the change in left-side outcome from 0 to -1 made in the larger walk? Do you think that made any difference in the best value of $n$?
\subsubsection*{A}
\begin{itemize}
    \item Smaller walk would have shifted advantage to smaller $n$ because when $n \geq \frac{\textrm{\#states}-1}{2}$ (\#states is odd) the algorithm updates all states visited by the terminal reward. This means that the algorithm only makes value changes of size $\alpha$, since the values are no longer bootstrapped or backed up.
    \item The addition of the $-1$ reward on the left favours smaller values of $n$, because in longer episodes the larger values of $n$ will have to update many states by the terminal reward (now $-1$ rather than $0$) thus increasing variance
\end{itemize}

\subsection{Exercise 7.4}
\subsubsection*{Q}
Prove that the n-step return of Sarsa (7.4) can be written exactly in terms of a novel TD error, as
\[
    G_{t:t+n} = Q_{t-1}(S_t, A_t) + \sum_{k=t}^{\textrm{min}(t+n, T) -1} \gamma^{k-1}[R_{k+1} + \gamma Q_k(S_{k+1}, A_{k+1}) - Q_{k-1}(S_k, A_k)].
\]
\subsubsection*{A}
Denote
\[
    G_{t:t+n} \doteq \sum_{i=1}^n \gamma^{i-1}R_{t+i} + \gamma^n Q_{t+n-1}(S_{t+n}, A_{t+n})
\]
for $n \geq 1$ and $0 \leq t < T-n$ and with $G_{t:t+n} = G_t$ if $t+n > T$.\\

Set $\tau = \textrm{min}(t+n, T) - 1$ and observe that
\begin{align*}
   & \sum_{k=t}^{\tau} \gamma^{k-t} [R_{t+1} + \gamma Q_k(S_{k+1}, A_{k+1}) - Q_{k-1}(S_k, A_k)] \\
   &= \sum_{k=t}^{\tau} \gamma^{k-t} R_{t+1} + \gamma \sum_{k=t}^{\tau} \gamma^{k-t} Q_k(S_{k+1}, A_{k+1}) - \sum_{k=t}^{\tau} \gamma^{k-t} Q_{k-1}(S_k, A_k) \\
   &= G_{t:t+n} - \mathds{1}\{t+n < T\}\gamma^n Q_{t+n-1}(S_{t+n}, A_{t+n}) + \gamma^{\tau} Q_{\tau}(S_{\tau}, A_{\tau}) - Q_{t-1(S_t, A_t)} \\
   &= G_{t:t+n} - Q_{t-1}(S_t, A_t).
\end{align*}

\subsection{Exercise 7.5}
\subsubsection*{Q}
Write the pseudocode for the off-policy state-value prediction algorithm described above.
\subsubsection*{A}
The update that we use is the same as the $n$-step TD update, only multiplied by the importance sampling ratio and with the control variate added.
\[
    G_{t:h} \doteq \rho_t (R_{t+1} + \gamma G_{t+1:h}) + (1 - \rho_t) V_{h-1}(S_t)
\]
The algorithm is therefore the same, but with these steps replacing the old returns calculations, using the latest available value function.\\

If you take the recursion relation literally then we should get a control variate for each of the intermediary states $(t+1, \dots, t+n)$, but I don't think that this is what is intended as this would just increase variance further.

\subsection{Exercise 7.6}
\subsubsection*{Q}
Prove that the control variate in the above equations does not change the expected value of the return.
\subsubsection*{A}
In (7.13) we have
\begin{align*}
    \E{}[(1-\rho_t)V_{h-1}(S_t)] &= \E{}_b[(1-\rho_t)V_{h-1}(S_t)] \\
                                 &= \E{}_b[(1-\rho_t)]\E{}_b[V_{h-1}(S_t)] \\
                                 &= 0.
\end{align*}
In the second case (7.14) we have
\begin{align*}
    & \E{}_b [\bar{V}_{h-1}(S_{t+1}) - \rho_{t+1}Q_{h-1}(S_{t+1}, A_{t+1}) \vert S_{t+1}] \\
    &= \sum_a \pi(a \vert S_{t+1}) Q_{h-1}(S_{t+1}, a) - \sum_a b(a \vert S_{t+1}) \frac{\pi(a\vert S_{t+1})}{b(a \vert S_{t+1})} Q_{h-1}(S_{t+1}, a)\\
                        &= 0
\end{align*}

\subsection{*Exercise 7.7}
\subsubsection*{Q}
Write the pseudocode for the off-policy action-value prediction algorithm described immediately above. Pay particular attention to the termination conditions for the recursion upon hitting the horizon or the end of episode.
\subsubsection*{A}
...

\subsection{Exercise 7.8}
\subsubsection*{Q}
Show that the general (off-policy) version of the $n$-step return (7.13) can still be written exactly and compactly as the sum of state-based TD errors (6.5) if the approximate state value function does not change.
\subsubsection*{A}
Update target is
\[
    G_{t:h} = \rho_t (R_{t+1} + \gamma G_{t+1:h}) + (1 - \rho_t) V_{h-1}(S_t).
\]
Assume state-value function does not change and introduce the TD error
\[
    \delta_t \doteq R_{t+1} + \gamma V(S_{t+1}) - V(S_t).
\]
Then
\begin{align*}
    G_{t:h} - V(S_t) &= \rho_t \left( R_{t+1} + \gamma G_{t+1:h} - V(S_t) \right) \\
                     &= \rho_t \left( R_{t+1} + \gamma [G_{t+1:h} - V(S_{t+1})] + \gamma V(S_{t+1})  - V(S_t) \right) \\
                     &= \rho_t \delta_t + \rho_t \gamma[G_{t+1:h} - V(S_{t+1})] \\
                     &\vdotswithin{=}\\
                     &= \sum_{i=t}^{\textrm{min}(h, T)-1}\rho_{t:i}\gamma^{i-t}\delta_i
\end{align*}

\subsection{Exercise 7.9}
\subsubsection*{Q}
Repeat the above exercise for the action version of the off-policy n-step return (7.14) and the Expected Sarsa TD error (the quantity in brackets in Equation 6.9).
\subsubsection*{A}
Action-value update is
\[
    G_{t:h} = R_{t+1} + \gamma \rho_{t+1} \left(G_{t+1:h} - Q_{h-1}(S_{t+1}, A_{t+1}\right) - \gamma \bar{V}_{h-1}(S_h)
\]
where
\[
    \bar{V}_h \doteq \sum_a \pi(a \vert S_h)Q(S_h, a)
\]
and we assume that the action value function does not change between iterations. Define
\[
    \delta_t \doteq R_{t+1} + \gamma \bar{V}(S_{t+1}) - Q(S_t, A_t).
\]
Then
\begin{align*}
    G_{t:h} - Q(S_t, A_t) &= \delta_t + \gamma \rho_{t+1}(G_{t+1:h} - Q(S_{t+1}, A_{t+1})) \\
                          &\vdotswithin{=}\\
                          &=\sum_{i=t}^{\textrm{min}(h, T)-1}\gamma^{i-t}\rho_{t+1:i}\delta_i
\end{align*}
where we enforce the convention that $\rho_{a:b} = 1$ if $a > b$.

\subsection{Exercise 7.10 (programming)}
\subsubsection*{Q}
Devise a small off-policy prediction problem and use it to show that the off-policy learning algorithm using (7.13) and (7.2) is more data efficient than the simpler algorithm using (7.1) and (7.9).
\subsubsection*{A}
...

\subsection{Exercise 7.11}
\subsubsection*{Q}
Show that if the approximate action values are unchanging, then the tree-backup return (7.16) can be written as a sum of expectation-based TD errors:
\[
    G_{t:t+n} = Q(S_t, A_t) + \sum_{k=t}^{\textrm{min}(t+n, T)-1}\delta_k \prod_{i=t+1}^k \gamma \pi(A_i \vert S_i),
\]
where $\delta_t \doteq R_{t+1} + \gamma \bar{V}_t(S_{t+1}) - Q(S_t, A_t)$ and $\bar{V}_t$ is given by (7.8).

\subsubsection*{A}
Assume action-values unchanging. The recursion formula for tree backup is
\[
    G_{t:t+n} = R_{t+1} + \gamma \sum_{a \neq A_{t+1}} \pi(a \vert S_{t+1}) Q(S_{t+1}, a) + \gamma \pi(A_{t+1}, S_{t+1}) G_{t+1:t+n}.
\]
Define
\[
    \delta_t \doteq R_{t+1} + \gamma \bar{V}(S_{t+1}) - Q(S_t, A_t)
\]
where
\[
    \bar{V}_h \doteq \sum_a \pi(a \vert S_h)Q(S_h, a).
\]
Then
\begin{align*}
    G_{t:t+n} - Q(S_t, A_t) &= R_{t+1} + \gamma \bar{V}(S_{t+1}) - \gamma \pi(A_{t+1}, S_{t+1})Q(S_{t+1}, A_{t+1}) \\
                            &\quad - Q(S_t, A_t) + \gamma \pi(A_{t+1}, S_{t+1})G_{t+1:t+n} \\
                            &= \delta_t - \gamma \pi(A_{t+1}, S_{t+1}) [G_{t+1:t+n} - Q(S_{t+1}, A_{t+1})] \\
                            &\vdotswithin{=}\\
                            &=\sum_{i=1}^{\textrm{min}(t+n, T)-1} \delta_i \prod_{j=t+1}^i \gamma \pi(A_j, S_j)
\end{align*}
where we define the product operator to have the behaviour $\prod_a^b[\cdot] = 1$ for $a > b$.


\subsection{$k$-Nearest Neighbors}

The $k$-nearest neighbors algorithm ($k$-NN) is a nonparametric method commonly used for classification, where each object is classified by a vote among its $k$ nearest neighbors.\cite{altman1992introduction} Since the algorithm is substantially a local method, it would be less likely to be influenced by global topology of the attribute space, which suggests an important advantage of applying $k$-NN here.

However, it is also noteworthy that the $k$-NN algorithm suffers from severe curse of dimensionality. What makes it worse is that we have got only 2192 observations here but more than 10 attributes. Therefore some variable selection must be applied before launching the $k$-NN algorithm.

\begin{figure}[h]
\center\includegraphics[width = .7\textwidth]{knncv.png}
\caption{Cross Validation Error Rates of \(k\)-NN}
\label{knncv}
\end{figure}

\begin{table}[h]
\setlength{\belowcaptionskip}{5pt}
\caption{Confusion Matrix and Error Rates of \(k\)-NN}
\label{errknn}
\centering
\renewcommand\arraystretch{1.5}
\begin{tabular}{rrrrr}
\hline
\hline
 & & \multicolumn{2}{c}{True Condition} & \\
\hline
 & & Non-Precipitation & Precipitation & \\
\cline{1-4}
\multirow{2}{*}{Prediction} & {Non-Precipitation} & 308 & 52 & \\
\cline{2-4}
&Precipitation&58&241&\\
\hline
&Error Rate & 0.1585 & 0.1775 & 0.1669\\
\cline{2-5}
& & Type \uppercase\expandafter{\romannumeral1} & Type \uppercase\expandafter{\romannumeral2} & Overall\\
\hline
\end{tabular}
\end{table}

Fortunately, the process of variable selection can be done with the result of the random forest which we have launched before. According to random forest, the most important attributes are PRCP, TDIF, and the four attributes regarding wind.

The data set can be even further reduced as we observe the strong correlation between WSF2 and WSF5 (their correlation turns out to be approximately $0.95$). Because of such stong colinearity, we would only keep 4 attributes, i.e. PRCP, TDIF, WSF5 and WDF5 in the $k$-NN algorithm. (Note that for wind vectors, the distances shall not be Euclidean as they are presented in polar coordinates.)

The cross validation error rates for different values of $k$ are shown above in Figure \ref{knncv}, which suggests that $k=30$ would be the best to proceed with. Then the confusion matrix and the test errors are as follows in Table \ref{errknn}.

As we can see, in general, SVM has the best performance, while $k$-NN the worse. It is also noteworthy that for all of the three methods here, there are always more Type \uppercase\expandafter{\romannumeral2} error than Type \uppercase\expandafter{\romannumeral1} error.


\documentclass[11pt]{article}

\usepackage{amsmath,amsthm}
\usepackage[headings]{fullpage}
\usepackage[utopia]{mathdesign}
\usepackage{color}

\pagestyle{myheadings}
\markboth{MATH426/CISC410}{MATH426/CISC410}

\input{../../fncextra}
\begin{document}

\begin{center}
  \bf A fitting challenge
\end{center}

Some phenomena, like the growth of tumors, can be predicted by theory to follow a \textbf{Gompertz function},
\begin{equation}
  \label{g}
  g(t) = A e^{-b e^{-ct}},
\end{equation}
where $A,b,c$ are positive parameters. As $t$ varies over the real line, $g$ increases from 0 to $A$.

Suppose you have data $(t_i,z_i)$, $i=1,\dots,m$, that you believe follow a Gompertz curve with unknown parameters. Taking the log, we get
\begin{equation}
  \label{logdata}
  \log(z_i) \approx (\log A) - b e^{-c t_i}.
\end{equation}
We let $y_i=\log z_i$ and let $a=\log A$. Define a function $\bff$ whose components are
\begin{equation}
	\label{f}
	f_i(a,b,c) = a - b e^{-c t_i} - y_i.
\end{equation}
In order to fit the data, we seek to minimize $\|\bff\|_2$ as a function of $a$, $b$, and $c$, which together we call the vector $\bfx$. This is a nonlinear least squares problem that can be solved by the Levenberg iteration.

\subsection*{Preparation}

Read section 4.7.

\subsection*{Goals}

You will find a nonlinear least squares fit to given data and use the result to predict the asymptotic value $g(\infty)=A$.

\subsection*{Procedure}

Download the script template and the file \texttt{gompertz\_data}.

\begin{enumerate}
    \item Load the data file, which has two vectors \texttt{t} and \texttt{z}. Make a plot of $z$ versus $t$.

    \item In a separate file, write a function
\begin{verbatim}
function f = gomp(x,t,z)
\end{verbatim}
    that returns the $m$-vector defined by~\eqref{f}, given a value of $\bfx=[a;b;c]$ and the vectors \texttt{t} and \texttt{z}.

    \item In the script file, use \texttt{levenberg} to find best fitting values for $a,b,c$.

    \item Using the result of the fit, calculate the value of $\lim_{t\to\infty} g(t)$.

    \item On top of the data points, make a plot of the fitting function~\eqref{g} over $0\le t \le 40$. (Remember that the function fits the log of the values, not the values themselves.)
\end{enumerate}

\end{document}


\section{Graphs}

\begin{frame}{Graphs}{Paths}
  For a graph {\color{MainA}$G = (V, E)$}:
  \begin{itemize}
    \item
      A path of {\color{MainA}$G$} is a sequence of edges
      \begin{math}
        {\color{MainA}u_1},
        {\color{MainA}u_2},
        \dots,
        {\color{MainA}u_i} \in {\color{MainA}V}
      \end{math}
      with
      \begin{itemize}
        \item
          Undirected graph:
          \begin{math}
            \{{\color{MainA}u_1}, {\color{MainA}u_2}\},
            \{{\color{MainA}u_2}, {\color{MainA}u_3}\},
            \dots,
            \{{\color{MainA}u_{i-1}}, {\color{MainA}u_i}\} \in
            {\color{MainA}E}
          \end{math}
        \item
          Directed graph:
          \begin{math}
            ({\color{MainA}u_1}, {\color{MainA}u_2}),
            ({\color{MainA}u_2}, {\color{MainA}u_3}),
            \dots,
            ({\color{MainA}u_{i-1}}, {\color{MainA}u_i}) \in
            {\color{MainA}E}
          \end{math}
      \end{itemize}
    \item
      The {\color{MainA}length of a path} is
      \begin{itemize}
        \item
          Without weights:
          {\color{MainA}number of edges} taken
        \item
          With weights:
          {\color{MainA}sum of weigths of edges} taken
      \end{itemize}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Graphs}{Paths}
  For a graph {\color{MainA}$G = (V, E)$}:
  \begin{itemize}
    \item
      The {\color{MainA}shortest path} between two vertices
      ${\color{MainA}u}, {\color{MainA}v}$ is the path
      \begin{math}
      {\color{MainA}P}
      = ({\color{MainA}u}, \dots, {\color{MainA}v})
      \end{math}
      with the shortest length ${\color{MainA}d(u,v)}$ or lowest costs
    \item
      The {\color{MainA}diameter} of a graph is the
      {\color{MainA}longest shortest path}
  \end{itemize}
\end{frame}


Given classifier $f : \mathbb{R}^d \to \mathcal{Y}$, construct smoothed classifier $g$ as $\displaystyle g(x) := \argmax_{c \in \mathcal{Y}} \P_\epsilon \left[f(x+\epsilon) = c\right]$ where $\epsilon \sim \mathcal{N}(0, \sigma^2 \mathbf{1})$

\paragraph{Robustness}
If $\displaystyle \P[f(x+\epsilon) = c_A] \ge \underline{p_A} \ge \overline{p_B} \ge \max_{c \neq c_A} \P[f(x+\epsilon) = c]$, then $g(x+\delta) = c_A$ for all $\| \delta \|_2 < R := \frac{\sigma}{2} \left( \Phi^{-1}(\underline{p_A}) - \Phi^{-1}(\overline{p_B}) \right)$

\paragraph{Certification} $\underline{p_A}$ approximate with sampling: if $\underline{p_A} > 0.5$, return radius $\sigma \Phi^{-1}(\underline{p_A})$, otherwise abstain.

\paragraph{Certified Accuracy}
Pick target radius $T$, count \#points in test set with certified radius $R > T$ and where predicted label matches test set label (Standard accuracy: $T=0$)

 \paragraph{Inference}
 Reject null hypothesis (true prob. of $f$ returning $\hat{c_A}$ is $0.5$, i.e., classes are indistinguishable) if estimated $p$-value $\le \alpha$, else abstain.

 $\rightarrow$ returns wrong class $\hat{c_A} \neq c_A$ with prob. $\le \alpha$

 \paragraph{Generalized Smoothing}
 $g(x) = \argmax_{c \in \mathcal{Y}} \P_\epsilon[f(\psi_\epsilon(x)) = c]$, $\epsilon \sim \mathcal{N}(0, \sigma^2\mathbf{1})$, $\psi_\alpha(\psi_\beta) = \psi_{\alpha+\beta}$ (e.g., instantiate $\psi$ with geometric transformations).

\paragraph{Summary}
\begin{itemize}
\item Scales to large networks;
\item Relaxes deterministic guarantees into statistical guarantees on robustness;
\item May need many samples to obtain higher certified radius. Also requires sampling at inference time;
\item Generalizing smoothing to different properties is harder than convex methods.
\end{itemize}


\subsection{Lagrange Multipliers}
\noindent
Lagrange multipliers are a method that allow us to find extrema of a function subject to domain constraints.\\

\input{./differentialMultivariableCalculus/toyExample}

\noindent
In the example above, $f_x = 1$, $g_x = 2x$, $f_y = 1$, and $g_y = 2y$ with constraint $x^2 + y^2 = 1$.\\
Giving us a system
\begin{equation*}
	\begin{cases}
		1 = \lambda 2x \\
		1 = \lambda 2y \\
		x^2 + y^2 = 1
	\end{cases} \implies x = y = \pm \frac{1}{\sqrt{2}}\text{ and } \lambda = \frac{1}{\sqrt{2}}.
\end{equation*}
This means that the max value of $f$ constrained by $g$ is
\begin{equation*}
	f\left(\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}\right) = \sqrt{2}.
\end{equation*}

\input{./differentialMultivariableCalculus/methodLagrangeMultipliers}


\subsection{Scalar potential}

Given a vector field \(\mathbf F\) we may be able to identify a scalar field \(P\) such that:

\(\mathbf F=-\nabla P\)

\subsection{Non-uniqueness of scalar potentials}

Scalar potentials are not unique.

If \(P\) is a scalar potential of \(\mathbf F\), then so is \(P+c\), where \(c\) is a constant.

\subsection{Conservative vector fields}

Not all vector fields have scalar potentials. Those that do are conservative.

For example if a vector field is the gradient of a scalar height function, then the height is a scalar potential.

If a vector field is the rotation of water, there will not be a scalar potential.


\subsection{Matrix Completion via Alternating Minimizations}
\label{matrix_completion}

\noindent{\bf Description}
\smallskip

Low-rank matrix completion is an effective technique for statistical data analysis widely used in the data mining and machine learning applications.
Matrix completion is a variant of low-rank matrix factorization with the goal of recovering a partially observed and potentially noisy matrix from a subset of its revealed entries.
Perhaps the most popular applications in which matrix completion has been successfully applied is in the context of collaborative filtering in recommender systems.
In this setting, the rows in the data matrix correspond to users,
the columns to items such as movies, and entries to feedback provided by users for items.
The goal is to predict missing entries of the rating matrix.
This implementation uses the alternating least-squares (ALS) technique for solving large-scale matrix completion problems.\\

\smallskip
\noindent{\bf Usage}
\smallskip

{\hangindent=\parindent\noindent\it%
	{\tt{}-f }path/\/{\tt{}ALS.dml}
	{\tt{} -nvargs}
	{\tt{} V=}path/file
	{\tt{} L=}path/file
	{\tt{} R=}path/file
%	{\tt{} VO=}path/file
	{\tt{} rank=}int
	{\tt{} reg=}L2$\mid$wL2%regularization
	{\tt{} lambda=}double
	{\tt{} fmt=}format

}

\smallskip
\noindent{\bf Arguments}
\begin{Description}
	\item[{\tt V}:]
	Location (on HDFS) to read the input (user-item) matrix $V$ to be factorized
	\item[{\tt L}:]
	Location (on HDFS) to write the left (user) factor matrix $L$
	\item[{\tt R}:]
	Location (on HDFS) to write the right (item) factor matrix $R$
%	\item[{\tt VO}:]
%	Location (on HDFS) to write the input matrix $VO$ with empty rows and columns removed (if there are any)
	\item[{\tt rank}:] (default:\mbox{ }{\tt 10})
	Rank of the factorization
	\item[{\tt reg}] (default:\mbox{ }{\tt L2})
	Regularization:\\
	{\tt L2} = L2 regularization;\\
 	{\tt wL2} = weighted L2 regularization;\\
 	if {\tt reg} is not provided no regularization will be performed.
 	\item[{\tt lambda}:] (default:\mbox{ }{\tt 0.000001})
 	Regularization parameter
 	\item[{\tt maxi}:] (default:\mbox{ }{\tt 50})
	 Maximum number of iterations
	\item[{\tt check}:] (default:\mbox{ }{\tt FALSE})
	Check for convergence after every iteration, i.e., updating $L$ and $R$ once
	\item[{\tt thr}:] (default:\mbox{ }{\tt 0.0001})
	Assuming {\tt check=TRUE}, the algorithm stops and convergence is declared
	if the decrease in loss in any two consecutive iterations falls below threshold {\tt thr};
	if {\tt check=FALSE} parameter {\tt thr} is ignored.
	\item[{\tt fmt}:] (default:\mbox{ }{\tt "text"})
	Matrix file output format, such as {\tt text}, {\tt mm}, or {\tt csv}
\end{Description}

 \smallskip
 \noindent{\bf Usage: ALS Prediction/Top-K Prediction}
 \smallskip

 {\hangindent=\parindent\noindent\it%
 	{\tt{}-f }path/\/{\tt{}ALS\_predict.dml}
 	{\tt{} -nvargs}
 	{\tt{} X=}path/file
 	{\tt{} Y=}path/file
 	{\tt{} L=}path/file
 	{\tt{} R=}path/file
 	{\tt{} Vrows=}int
 	{\tt{} Vcols=}int
 	{\tt{} fmt=}format

 }\smallskip

  \smallskip
  {\hangindent=\parindent\noindent\it%
  	{\tt{}-f }path/\/{\tt{}ALS\_topk\_predict.dml}
  	{\tt{} -nvargs}
  	{\tt{} X=}path/file
  	{\tt{} Y=}path/file
  	{\tt{} L=}path/file
  	{\tt{} R=}path/file
  	{\tt{} V=}path/file
  	{\tt{} K=}int
  	{\tt{} fmt=}format

  }\smallskip

%   \noindent{\bf Arguments --- Prediction}
%   \begin{Description}
%   	\item[{\tt X}:]
%   	Location (on HDFS) to read the input matrix $X$ containing user-ids (first column) and item-ids (second column)
%   	\item[{\tt L}:]
%   	Location (on HDFS) to read the left (user) factor matrix $L$
%   	\item[{\tt R}:]
%   	Location (on HDFS) to read the right (item) factor matrix $R$
%   	\item[{\tt Y}:]
%   	Location (on HDFS) to write the output matrix $Y$ containing user-ids (first column), item-ids (second column) and predicted ratings (third column)
%   	\item[{\tt Vrows}:]
%   	Number of rows of the user-item matrix $V$
%   	\item[{\tt Vcols}]
%   	Number of columns of the user-item matrix $V$
%   	\item[{\tt fmt}:] (default:\mbox{ }{\tt "text"})
%   	Matrix file output format, such as {\tt text}, {\tt mm}, or {\tt csv}
%   \end{Description}

  \noindent{\bf Arguments --- Prediction/Top-K Prediction}
  \begin{Description}
  	\item[{\tt V}:]
  	Location (on HDFS) to read the user-item matrix $V$
  	\item[{\tt X}:]
  	Location (on HDFS) to read the input matrix $X$ with following format:
  	\begin{itemize}
  		\item for {ALS\_predict.dml}: a 2-column matrix that contains the user-ids (first column) and the item-ids (second column),
  		\item for {ALS\_topk\_predict.dml}: a 1-column matrix that contains the user-ids.
  	\end{itemize}
  	\item[{\tt Y}:]
  	Location (on HDFS) to write the output of prediction with the following format:
  	\begin{itemize}
  		\item for {ALS\_predict.dml}: a 3-column matrix that contains the user-ids (first column), the item-ids (second column) and the predicted ratings (third column),
  		\item for {ALS\_topk\_predict.dml}: a ($K+1$)-column matrix that contains the user-ids in the first column and the top-K item-ids in the remaining $K$ columns will be stored at {\tt Y}.
  		Additionally, a matrix with the same dimensions that contains the corresponding actual top-K ratings will be stored at {\tt Y.ratings}; see below for details.
  	\end{itemize}
%  	Note the following output format in predicting top-K items.
%  	For a user with no available ratings in $V$ no
%  	top-K items will be provided, i.e., the corresponding row in $Y$ will contains 0s.
%  	Moreover, $K'<K$ items with highest predicted ratings will be provided for a user $i$
%  	if the number of missing ratings $K'$ (i.e., those with 0 value in $V$) for $i$ is less than $K$.
  	\item[{\tt L}:]
  	Location (on HDFS) to read the left (user) factor matrix $L$
  	\item[{\tt R}:]
  	Location (on HDFS) to write the right (item) factor matrix $R$
   	\item[{\tt Vrows}:]
   	Number of rows of $V$ (i.e., number of users)
   	\item[{\tt Vcols}]
   	Number of columns of $V$ (i.e., number of items)
  	\item[{\tt K}:] (default:\mbox{ }{\tt 5})
  	Number of top-K items for top-K prediction
  	\item[{\tt fmt}:] (default:\mbox{ }{\tt "text"})
  	Matrix file output format, such as {\tt text}, {\tt mm}, or {\tt csv}
  \end{Description}

 \noindent{\bf Details}
 \smallskip

 Given an $m \times n$ input matrix $V$ and a rank parameter $r \ll \min{(m,n)}$, low-rank matrix factorization seeks to find an $m \times r$ matrix $L$ and an $r \times n$ matrix $R$ such that $V \approx LR$, i.e., we aim to approximate $V$ by the low-rank matrix $LR$.
 The quality of the approximation is determined by an application-dependent loss function $\mathcal{L}$. We aim at finding the loss-minimizing factor matrices, i.e.,
 \begin{equation}\label{eq:problem}
 (L^*, R^*) = \textrm{argmin}_{L,R}{\mathcal{L}(V,L,R)}.
 \end{equation}
 In the context of collaborative filtering in the recommender systems it is often the case that the input matrix $V$ contains several missing entries. Such entries are coded with the 0 value and the loss function is computed only based on the nonzero entries in $V$, i.e.,
 \begin{equation*} %\label{eq:loss}
 \mathcal{L}=\sum_{(i,j)\in\Omega}l(V_{ij},L_{i*},R_{*j}),
 \end{equation*}
 where $L_{i*}$ and $R_{*j}$, respectively, denote the $i$th row of $L$ and the $j$th column of $R$, $\Omega=\{\omega_1,\dots,\omega_N\}$ denotes the training set containing the observed (nonzero) entries in $V$, and $l$ is some local loss function.
 %for some training set $\Omega$ that contains the observed (nonzero) entries in $V$ and some local loss function $l$. In the above formula,

 ALS is an optimization technique that can be used to solve quadratic problems.
 For matrix completion, the algorithm repeatedly keeps one of the unknown matrices ($L$ or $R$) fixed and optimizes the other one. In particular, ALS alternates between recomputing the rows of $L$ in one step and the columns of $R$ in the subsequent step.
 Our implementation of the ALS algorithm supports the loss functions summarized in Table~\ref{tab:loss_functions} commonly used for matrix completion~\cite{ZhouWSP08:als}.
 %
 \begin{table}[t]
 	\centering
 	\label{tab:loss_functions}
 	\begin{tabular}{|ll|} \hline
 		Loss & Definition \\ \hline
% 		$\mathcal{L}_\text{Sl}$ & $\sum_{i,j} (V_{ij} - [LR]_{ij})^2$ \\
% 		$\mathcal{L}_\text{Sl+L2}$ & $\mathcal{L}_\text{Sl} + \lambda \Bigl( \sum_{ik} L_{ik}^2 + \sum_{kj} R_{kj}^2 \Bigr)$ \\
 		$\mathcal{L}_\text{Nzsl}$ & $\sum_{i,j:V_{ij}\neq 0} (V_{ij} - [LR]_{ij})^2$ \\
 		$\mathcal{L}_\text{Nzsl+L2}$ & $\mathcal{L}_\text{Nzsl} + \lambda \Bigl( \sum_{ik} L_{ik}^2 + \sum_{kj} R_{kj}^2 \Bigr)$ \\
 		$\mathcal{L}_\text{Nzsl+wL2}$ & $\mathcal{L}_\text{Nzsl} + \lambda \Bigl(\sum_{ik}N_{i*} L_{ik}^2 + \sum_{kj}N_{*j} R_{kj}^2 \Bigr)$ \\ \hline
 	\end{tabular}
 	\caption{Popular loss functions supported by our ALS implementation; $N_{i*}$ and $N_{*j}$, respectively, denote the number of nonzero entries in row $i$ and column $j$ of $V$.}
 \end{table}

 Note that the matrix completion problem as defined in (\ref{eq:problem}) is a non-convex problem for all loss functions from Table~\ref{tab:loss_functions}.
 However, when fixing one of the matrices $L$ or $R$, we get a least-squares problem with a globally optimal solution.
 For example, for the case of $\mathcal{L}_\text{Nzsl+wL2}$ we have the following closed form solutions
  \begin{align*}
  L^\top_{n+1,i*} &\leftarrow (R^{(i)}_n {[R^{(i)}_n]}^\top + \lambda N_2 I)^{-1} R_n V^\top_{i*}, \\
  R_{n+1,*j} &\leftarrow ({[L^{(j)}_{n+1}]}^\top L^{(j)}_{n+1} + \lambda N_1 I)^{-1} L^\top_{n+1} V_{*j},
  \end{align*}
 where $L_{n+1,i*}$ (resp. $R_{n+1,*j}$) denotes the $i$th row of $L_{n+1}$ (resp. $j$th column of $R_{n+1}$), $\lambda$ denotes
 the regularization parameter, $I$ is the identity matrix of appropriate dimensionality,
 $V_{i*}$ (resp. $V_{*j}$) denotes the revealed entries in row $i$ (column $j$),
 $R^{(i)}_n$ (resp. $L^{(j)}_{n+1}$) refers to the corresponding columns of $R_n$ (rows of $L_{n+1}$),
 and $N_1$ (resp. $N_2$) denotes a diagonal matrix that contains the number of nonzero entries in row $i$ (column $j$) of $V$.

% For example, for the case of $\mathcal{L}_\text{Sl-L2}$ we have the following closed form solutions
% \begin{align*}
% L^\top_{n+1,i*} &\leftarrow (R_n {[R_n]}^\top + \lambda I)^{-1} R_n V^\top_{i*}, \\
% R_{n+1,*j} &\leftarrow ({[L_{n+1}]}^\top L_{n+1} + \lambda I)^{-1} L^\top_{n+1} V_{*j},
% \end{align*}
% where $L_{n+1,i*}$ (resp. $R_{n+1,*j}$) denotes the $i$th row of $L_{n+1}$ (resp. $j$th column of $R_{n+1}$), $\lambda$ denotes
% the regularization parameter and $I$ is the identity matrix of appropriate dimensionality.
% For the case of $\mathcal{L}_\text{Nzsl}$ we need to remove the equation that correspond to zero entries of $V$ from the least-squares problems.
% With wL2 we get the following equations
% \begin{align*}
% L^\top_{n+1,i*} &\leftarrow (R^{(i)}_n {[R^{(i)}_n]}^\top + \lambda N_2 I)^{-1} R_n V^\top_{i*}, \\
% R_{n+1,*j} &\leftarrow ({[L^{(j)}_{n+1}]}^\top L^{(j)}_{n+1} + \lambda N_1 I)^{-1} L^\top_{n+1} V_{*j},
% \end{align*}
% where $V_{i*}$ (resp. $V_{*j}$) denotes the revealed entries in row $i$ (column $j$),
% $R^{(i)}_n$ (resp. $L^{(j)}_{n+1}$) refers to the corresponding columns of $R_n$ (rows of $L_{n+1}$),
% and $N_1$ (resp. $N_2$) denotes a diagonal matrix that contains the number of nonzero entries in row $i$ (column $j$) of $V$.

 \textbf{Prediction.}
 Based on the factor matrices computed by ALS we provide two prediction scripts:
 \begin{Enumerate}
 	\item {\tt ALS\_predict.dml} computes the predicted ratings for a given list of users and items;
 	\item {\tt ALS\_topk\_predict.dml} computes top-K item (where $K$ is given as input) with highest predicted ratings together with their corresponding ratings for a given list of users.
 \end{Enumerate}

 \smallskip
 \noindent{\bf Returns}
 \smallskip

 We output the factor matrices $L$ and $R$ after the algorithm has converged. The algorithm is declared as converged if one of the two criteria is meet:
 (1) the decrease in the value of loss function falls below {\tt thr}
 given as an input parameter (if parameter {\tt check=TRUE}), or (2) the maximum number of iterations (defined as parameter {\tt maxi}) is reached.
 Note that for a given user $i$ prediction is possible only if user $i$ has rated at least one item, i.e., row $i$ in matrix $V$ has at least one nonzero entry.
 In case, some users have not rated any items the corresponding factor in $L$ will be all 0s.
 Similarly if some items have not been rated at all the corresponding factors in $R$  will contain only 0s.
 Our prediction scripts output the predicted ratings for a given list of users and items as well as the top-K items with highest predicted ratings together with the predicted ratings for a given list of users. Note that the predictions will only be provided for the users who have rated at least one item, i.e., the corresponding rows contain at least one nonzero entry.
% Moreover in the case of top-K prediction, if the number of predicted ratings---i.e., missing entries--- for some user $i$ is less than the input parameter $K$, all the predicted ratings for user $i$ will be provided.

 \smallskip
 \noindent{\bf Examples}
 \smallskip

% {\hangindent=\parindent\noindent\tt
% 	\hml -f ALS.dml -nvargs V=/user/biadmin/V L=/user/biadmin/L R=/user/biadmin/R rank=10 reg="L2" lambda=0.0001 fmt=csv
%
% }

 {\hangindent=\parindent\noindent\tt
 	\hml -f ALS.dml -nvargs V=/user/biadmin/V L=/user/biadmin/L R=/user/biadmin/R rank=10 reg="wL2" lambda=0.0001 maxi=50 check=TRUE thr=0.001 fmt=csv

 }

 \noindent To compute predicted ratings for a given list of users and items:

 {\hangindent=\parindent\noindent\tt
  	\hml -f ALS-predict.dml -nvargs X=/user/biadmin/X Y=/user/biadmin/Y L=/user/biadmin/L R=/user/biadmin/R  Vrows=100000 Vcols=10000 fmt=csv

 }

 \noindent To compute top-K items with highest predicted ratings together with the predicted ratings for a given list of users:

 {\hangindent=\parindent\noindent\tt
   	\hml -f ALS-top-predict.dml -nvargs X=/user/biadmin/X Y=/user/biadmin/Y L=/user/biadmin/L R=/user/biadmin/R V=/user/biadmin/V K=10 fmt=csv

 }

%
%\begin{itemize}
%	\item Y. Zhou, D. K. Wilkinson, R. Schreiber, and R. Pan. \newblock{Large-scale parallel collaborative flitering for the Netflix prize}. In Proceedings of the International
%	Conference on Algorithmic Aspects in Information and Management (AAIM), 2008, 337-348.
%\end{itemize}


\section{Clustering Data Streams (20 points) }

\paragraph{Introduction.}
In this problem, we study an approach for clustering massive data streams. We will study a framework for turning an approximate clustering algorithm into one that can work on data streams, \emph{i.e.}, one which needs a small amount of memory and a small number of (actually, just one) passes over the data. As the instance of the clustering problem, we will focus on the $k$-means problem.

\paragraph{Definitions.}
Before going into further details, we need some definitions:
\begin{itemize}
	\item The function $d:\mathbb{R}^{p}\times \mathbb{R}^{p} \rightarrow \mathbb{R}^{+}$ denotes the Euclidean distance:
	\[
		d(x,y) = ||x-y||_2.
	\]
	\item For any $x\in \mathbb{R}^p$ and $T\subset \mathbb{R}^p$, we define:
	\[
		d(x,T)=\min_{z\in T}\{d(x,z)\}.
	\]
	\item Having subsets $S,T\subset \mathbb{R}^{p}$, and a weight function $w:S\rightarrow \mathbb{R}^{+}$, we define:
	\[
		\mathrm{cost}_w(S,T)=\sum_{x\in S} w(x)d(x,T)^2.
	\]
	\item Finally, if for all $x\in S$ we have $w(x)=1$, we simply denote $\mathrm{cost}_w(S,T)$ by $\mathrm{cost}(S,T)$.
\end{itemize}

\paragraph{Reminder: $k$-means clustering.}
The $k$-means clustering problem is as follows: given a subset $S\subset \mathbb{R}^p$, and an integer $k$, find the set $T$ (with $|T|=k$), which minimizes $\mathrm{cost}(S,T)$. If a weight function $w$ is also given, the $k$-means objective would be to minimize $\mathrm{cost}_w(S,T)$, and we call the problem the weighted $k$-means problem.

\paragraph{Strategy for clustering data streams.}
We assume we have an algorithm \textsc{alg} which is an $\alpha$-approximate weighted $k$-means clustering algorithm (for some $\alpha>1$). In other words, given any $S\subset \mathbb{R}^p$, $k\in\mathbb{N}$, and a weight function $w$, \textsc{alg} returns a set $T\subset
\mathbb{R}^p$, $|T|=k$, such that:
\[
	\mathrm{cost}_w(S,T)\leq \alpha \min_{|T'|=k}\{\mathrm{cost}_w(S,T')\}.
\]
\textbf{We will see how we can use {\sc alg} as a building block to make an algorithm for the $k$-means problem on data streams.}

The basic idea here is that of divide and conquer: if $S$ is a huge set that does not fit into main memory, we can read a portion of it that does fit into memory, solve the problem on this subset (\emph{i.e.}, do a clustering on this subset), record the result (\emph{i.e.}, the cluster centers and some corresponding weights, as we will see), and then read a next portion of $S$ which is again small enough to fit into memory, solve the problem on this part, record the result, etc. At the end, we will have to combine the results of the partial problems to construct a solution for the main big problem (\emph{i.e.}, clustering $S$).

To formalize this idea, we consider the following algorithm, which we denote as
\textsc{algstr}:

\begin{itemize}
	\item Partition $S$ into $\ell$ parts $S_1, \ldots, S_{\ell}$.
  \item For each $i=1$ to $\ell$, run \textsc{alg} on $S_i$ to get a set of $k$ centers
  $T_i=\{t_{i1}, t_{i2}, \ldots, t_{ik}\}$, and assume $\{S_{i1},S_{i2}, \ldots,
  S_{ik}\}$ is the corresponding clustering of $S_i$ (\emph{i.e.}, $S_{ij} = \{x\in
  S_i| \, d(x,t_{ij}) < d(x,t_{ij'}) \; \forall j'\neq j, 1\leq j'\leq k\}$).
  \item Let $\widehat{S}= \bigcup_{i=1}^{\ell}T_i$, and define weights $w(t_{ij}) =
  |S_{ij}|$.
  \item Run \textsc{alg} on $\widehat{S}$ with weights $w$, to get $k$ centers $T$.
  \item Return $T$.
\end{itemize}

Now, we analyze this algorithm. Assuming $T^{*}=\{t^{*}_1, \ldots, t^{*}_k\}$ to
be the optimal $k$-means solution for $S$ (that is, $T^{*} =
\mathrm{argmin}_{|T'|=k}\{\mathrm{cost}(S,T')\}$), we would like to compare
$\mathrm{cost}(S,T)$ (where $T$ is returned by \textsc{algstr}) with
$\mathrm{cost}(S,T^{*})$.

A small fact might be useful in the analysis below: for any ${(a,b) \in \mathbb{R}^+}$ we have: \[{(a+b)^2 \leq 2a^2 + 2b^2.}\]

\subquestion{(a) [5pts]} First, we show that the cost of the final clustering can be bounded in terms of the total cost of the intermediate clusterings:

\task{Prove that: \[\mathrm{cost}(S,T) \leq 2\cdot \mathrm{cost}_w(\widehat{S},
T) + 2\sum_{i=1}^{\ell} \mathrm{cost}(S_i, T_i).\]}

\hint{You might want to use Triangle Inequality for Euclidean distance $d$.}

\subquestion{(b) [5pts]} So, to bound the cost of the final clustering, we can
bound the terms on the right hand side of the inequality in part (a).
Intuitively speaking, we expect the second term to be small compared to
$\mathrm{cost}(S,T^{*})$, because $T^{*}$ only uses $k$ centers to represent the
data set ($S$), while the $T_i$'s, in total, use $k\ell$ centers to represent
the same data set (and $k\ell$ is potentially much bigger than $k$). We show this formally:

\task{Prove that: \[{\sum_{i=1}^{\ell} \mathrm{cost}(S_i, T_i) \leq \alpha \cdot \mathrm{cost}(S,T^{*})}.\]}

\subquestion{(c) [10pt]} Prove that \textsc{algstr} is a $(4\alpha^2+6\alpha)$-approximation algorithm for the $k$-means problem.

\task{
Prove that:
\[{\mathrm{cost}(S,T) \leq (4\alpha^2+6\alpha)
\cdot \mathrm{cost}(S,T^{*})}.\]}

\textit{Hint: You might want to first prove two useful facts, which help bound the first term on the right hand side of the inequality in part (a):}
\[{\mathrm{cost}_w(\widehat{S}, T) \leq \alpha \cdot \mathrm{cost}_w(\widehat{S},T^{*})}.\]
\[{\mathrm{cost}_w(\widehat{S},
T^{*}) \leq 2\sum_{i=1}^{\ell} \mathrm{cost}(S_i, T_i) + 2\cdot \mathrm{cost}(S,T^{*})}.\]

\textbf{Additional notes:} We have shown above that \textsc{algstr} is a $(4\alpha^2+6\alpha)$-approximation algorithm for the $k$-means problem. Clearly, $4\alpha^2+6\alpha > \alpha$, so \textsc{algstr} has a somewhat worse approximation guarantee than \textsc{alg} (with which we started).  However, \textsc{algstr} is better suited for the streaming application, as not only it takes just one pass over the data, but also it needs a much smaller amount of memory.

Assuming that \textsc{alg} needs $\Theta(n)$ memory to work on an input set $S$ of size $n$  (note that just representing $S$ in memory will need $\Omega(n)$ space), if we partitioning $S$ into $\sqrt{n/k}$ equal parts, {\sc algstr} can work with only ${O(\sqrt{nk})}$ memory. (Like in the rest of the problem, $k$ represents the number of clusters per partition.)

Note that for typical values of $n$ and $k$, assuming $k \ll n$, we have $\sqrt{nk} \ll n$. For instance, with $n=10^6$, and $k=100$, we have $\sqrt{nk} = 10^4$, which is $100$ times smaller than $n$.

\subsection*{What to submit}
\begin{enumerate}[(a)]
	\item Proof that $\mathrm{cost}(S,T) \leq 2\cdot
	\mathrm{cost}_w(\widehat{S}, T) + 2\sum_{i=1}^{\ell} \mathrm{cost}(S_i, T_i)$.
	\item Proof that $\sum_{i=1}^{\ell} \mathrm{cost}(S_i, T_i) \leq
\alpha \cdot \mathrm{cost}(S,T^{*})$.
	\item Proof that $\mathrm{cost}(S,T) \leq (4\alpha^2+6\alpha)
\cdot \mathrm{cost}(S,T^{*})$.
\end{enumerate}


\chapter{Falling Bodies}

Because of gravity, if you throw a hammer straight up in the air, from
the moment it leaves your hand until it hits the ground, it is
accelerating toward the center of the earth at a constant rate.

\emph{Acceration} is the change in velocity. If the hammer leaves your
hand with a velocity of 12 meters per second upward, one second later
it will be rising, its velocity will have slowed to 2.2 meters per
second. One second after that, the hammer will be falling at a rate of
7.6 meters per second. Every second the hammer's velocity is changing by
9.8 meters per second, and that change is always toward the center of
the earth. When the hammer is going up, gravity is slowing it down by
9.8 meters per second each second.  When the hammer is coming down,
gravity is speeding it up by 9.8 meters per second each second.\index{acceleration}

Acceleration due to gravity on earth is a constant negative 9.8 meters per second per second:
\begin{equation*}
a = -9.8
\end{equation*}
(Why is it negative? We are talking about height, which increases as
you go away from the center of the earth. Acceleration is changing the
velocity in the opposite direction.)

\section{Calculating the Velocity}

Given that the acceleration is constant, it makes sense that the
velocity is a straight line. Assuming once again that the hammer
leaves your hand at 12 meters per second, then the upwards velocity at
time $t$ is given by:
\begin{equation*}
  v = 12 - 9.8t
\end{equation*}

Note that the velocity of the hammer is being given as a function. Here is its graph:

\begin{tikzpicture}
    \begin{axis}[
        xmin=-0.25,xmax=2.75,
        ymin=-13,ymax=13,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$t$},
        ylabel={$v$},
        ]
        \addplot[no marks,sdkblue] expression[domain=0:2.25,samples=100]{x * (-9.8) + 12} node[left] {$12 - 9.8t$};
    \end{axis}
\end{tikzpicture}

\begin{Exercise}[title={When is the apex of flight?}, label=vapex]
  Given the hammer's velocity is given by $12 - 9.8t$, at what time (in seconds)
  does it stop rising and begin to fall?
\end{Exercise}
\begin{Answer}[ref=vapex]
  Solve for when the velocity is zero.

  $t = \frac{12}{9.8} = 1.22$ seconds after release.
\end{Answer}

At this point, we need to say something about air resistance. Gravity
is not the only force on the hammer; as it travels through the air,
the air tries to slow it down. This force is called \emph{air resistance},
and for a large, fast moving object (like an airplane) it is very big force. For a
dense object (like a hammer) moving at a slow speed (what you generate
with your hand), air resistance doesn't significantly affect acceleration.

\section{Calculating Position}

If you let go of the hammer when it is 2 meters
above the ground, the height of the hammer is given by:
\begin{equation*}
  p = -\frac{9.8}{2}t^2 + 12t + 2
\end{equation*}

Here is a graph of this function:

\begin{tikzpicture}
    \begin{axis}[
        xmin=-1.2,xmax=3.5,
        ymin=-13,ymax=13,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$t$},
        ylabel={$p$},
      ]
      \addplot[no marks,sdkblue,dashed,<-] expression [domain=-0.7:0,samples=100] {(-4.9)*(x^2) + 12 * x + 2};
      \addplot[no marks,sdkblue] expression [domain=0:2.58,samples=100] {(-4.9)*(x^2) + 12 * x + 2};
      \addplot[no marks,sdkblue,dashed,->] expression [domain=2.58:3,samples=100] {(-4.9)*(x^2) + 12 * x + 2};
    \end{axis}
\end{tikzpicture}

How did I figure this out? \textbf{The change in position between time
  $0$ and any time $t$ is equal to the area under the velocity graph
  between $x = 0$ and $x = t$.}

Let's use the velocity graph to figure out how much the position has
changed in the first second of the hammer's flight. Here's the
velocity graph with the area under the graph for the first second filled
in:

\usepgfplotslibrary{fillbetween}

\begin{tikzpicture}
    \begin{axis}[
        xmin=-0.25,xmax=2.75,
        ymin=-13,ymax=13,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$t$},
        ylabel={$v$},
      ]
      \addplot[no marks,sdkblue, name path=f] expression[domain=0:2.25,samples=100]{x * (-9.8) + 12} node[left] {$12 - 9.8t$};
      \path[name path=xaxis] (axis cs:0,0) -- (axis cs:1,0);
      \addplot[
        thick,
        color=sdkblue,
        fill=sdkblue,
        fill opacity=0.05
    ]
    fill between[
        of=f and xaxis,
        soft clip={domain=0:1},
    ];
    \addplot[dashed,gray] coordinates {(0,12)(1,12)};
    \addplot[dashed,gray] coordinates {(1,12)(1,0)};
    \end{axis}
\end{tikzpicture}

The blue filled region is the area of the dashed rectangle minus that
empty triangle in its upper left.  The height of the rectangle is
twelve and its width is the amount of time the hammer has been in
flight ($t$).  The triangle is $t$ wide and and $9.8t$ tall. Thus, the
area of the blue region is given by $12t - \frac{1}{2}9.8 t^2$.

That's the change in position. Where was it originally? 2 meter off
the ground.  So the height is given by $p = 2 + 12t - \frac{1}{2}9.8t^2$.
We usually write terms so that the exponent decreases, so:

$$p = - \frac{1}{2}9.8t^2 + 12t + 2$$

Finding the area under the curve like this is called
\textit{integration}.  We say ``To find a function that gives the
change in position, we just integrate the velocity function.''  A lot
of the study of calculus is learning to integrate different sorts of
functions.\index{integration}

One important note about integration: Any time the curve drops under
the $x$-axis, the area is considered negative. (Which makes sense,
right? If the velocity is negative, the hammer's position is
decreasing.)

\begin{tikzpicture}
    \begin{axis}[
        xmin=-0.25,xmax=2.75,
        ymin=-13,ymax=13,
        axis x line=middle,
        axis y line=middle,
        axis line style=<->,
        xlabel={$t$},
        ylabel={$v$},
      ]
      \addplot[no marks,sdkblue, name path=f] expression[domain=0:2.25,samples=100]{x * (-9.8) + 12} node[left] {$12 - 9.8t$};
      \path[name path=xaxis] (axis cs:0,0) -- (axis cs:2.25,0);
      \addplot[
        thick,
        color=sdkblue,
        fill=sdkblue,
        fill opacity=0.05
      ]
      fill between[
        of=f and xaxis,
        soft clip={domain=0:1.2245},
      ];
      \addplot[
        thick,
        color=red,
        fill=red,
        fill opacity=0.07
      ]
      fill between[
        of=f and xaxis,
        soft clip={domain=1.2245:2.1},
      ];
    \end{axis}
\end{tikzpicture}

\section{Quadratic functions}

Functions of the form $f(x) = a x^2 + b x + c$ are called \newterm{quadratic functions}.
If $a > 0$, the ends go up.
If $a < 0$, the ends go down.\index{quadratic functions}

\begin{tikzpicture}
  \begin{axis}[
      xmin=-2.2,xmax=1.2,
      ymin=-2,ymax=3,
      axis x line=middle,
      axis y line=middle,
      axis line style=<->,
    ]
    \addplot[no marks,sdkblue] expression [domain=-2:1,samples=100] {(2)*(x^2) + 2 * x - 1};
  \end{axis}
  \node[right] at (1,4) {$2x^2 + 2x - 1$};
\end{tikzpicture}
\hspace{4mm}
\begin{tikzpicture}
  \begin{axis}[
    xmin=-1.5,xmax=1.5,
    ymin=-2,ymax=1.5,
    axis x line=middle,
    axis y line=middle,
    axis line style=<->,
  ]
  \addplot[no marks,sdkblue] expression [domain=-1.5:1.5,samples=100] {(-1.2)*(x^2) + 0.5 * x + 1};
\end{axis}
\node[right] at (0.5,1) {$-1.2 x^2 + 0.5 x + 1$};
\end{tikzpicture}

The graph of a quadratic function is a \newterm{parabola}.

\section{Simulating a falling body in Python}

Now you are going to write some Python code that simulates the flying hammer. First, we are just going print out the position, speed, and acceleration of the hammer for every 1/100th of a second after it leaves your hand. (Later we will make a graph.)

Create a file called \filename{falling.py} and type this into it:

\begin{Verbatim}
# Acceleration on earth
acceleration = -9.8 # m/s/s

# Size of time step
time_step = 0.01 # seconds

# Initial values
speed = 12  # m/s upward
height = 2  # m above the ground
current_time = 0.0  # seconds after release

# Is the hammer still aloft?
while height > 0.0:

    # Show the values
    print(f"{current_time:.2f} s:")
    print(f"\tacceleration: {acceleration:.2f} m/s/s")
    print(f"\tspeed: {speed:.2f} m/s")
    print(f"\theight: {height:.2f} m")

    # Update height
    height = height + time_step * speed

    # Update speed
    speed = speed + time_step * acceleration

    # Update time
    current_time = current_time + time_step

print(f"Hit the ground: Complete")
\end{Verbatim}

When you run it, you will see something like this:
\begin{Verbatim}
0.00 s:
	acceleration: -9.80 m/s/s
	speed: 12.00 m/s
	height: 2.00 m
0.01 s:
	acceleration: -9.80 m/s/s
	speed: 11.90 m/s
	height: 2.12 m
0.02 s:
	acceleration: -9.80 m/s/s
	speed: 11.80 m/s
	height: 2.24 m
0.03 s:
	acceleration: -9.80 m/s/s
	speed: 11.71 m/s
	height: 2.36 m
...
2.60 s:
	acceleration: -9.80 m/s/s
	speed: -13.48 m/s
	height: 0.20 m
2.61 s:
	acceleration: -9.80 m/s/s
	speed: -13.58 m/s
	height: 0.07 m
Hit the ground: Complete
\end{Verbatim}

Note that the acceleration isn't changing at all, but it is changing
the speed and the speed is changing the height.

We can see that the hammer in our simulation hits the ground just
after 2.61 seconds.

\subsection{Graphs and Lists}

Now, we are going to graph the acceleration, speed, and height using a
library called matplotlib. However, in order to make the graphs, we
need to gather all the data into lists.\index{matplotlib}

For example, we will have a list of speeds, and the first three
entries will be 12.0, 11.9, and 11.8.\index{lists, python}

We create an empty list and assign it to a variable like this:
\begin{Verbatim}
x = []
\end{Verbatim}

Then we can add items like this:
\begin{Verbatim}
x.append(3.14)
\end{Verbatim}

To get the first time back, we can ask for the object at index 0.
\begin{Verbatim}
y = x[0]
\end{Verbatim}
Note that the list starts at 0.  So if you have 32 items in the list,
the first item is at index 0. The last item is at index 31.

Duplicate the file \filename{falling.py} and name the new copy \filename{falling\_graph.py}

We are going to make a plot of the height over time. At the start of the program, you will import the
matplotlib library.  At the end of the program, you will create a plot and show it to the user.

In \filename{falling\_graph.py}, add the bold code:

\begin{Verbatim}[commandchars=\\\{\}]
\textbf{import matplotlib.pyplot as plt}

# Acceleration on earth
acceleration = -9.8 # m/s/s

# Size of time step
time_step = 0.01 # seconds

# Initial values
speed = 12  # m/s upward
height = 2  # m above the ground
current_time = 0.0  # seconds after release

\textbf{# Create empty lists}
\textbf{accelerations = []}
\textbf{speeds = []}
\textbf{heights = []}
\textbf{times = []}

# Is the hammer still aloft?
while height > 0.0:

    \textbf{# Add the data to the lists}
    \textbf{times.append(current_time)}
    \textbf{accelerations.append(acceleration)}
    \textbf{speeds.append(speed)}
    \textbf{heights.append(height)}

    # Update height
    height = height + time_step * speed

    # Update speed
    speed = speed + time_step * acceleration

    # Update time
    current_time = current_time + time_step

\textbf{# Make a plot}
\textbf{fig, ax = plt.subplots()}
fig.suptitle("Falling Hammer")
\textbf{ax.set_xlabel("Time (s)")}
\textbf{ax.set_ylabel("Height (m)")}
\textbf{ax.plot(times, heights)}
\textbf{plt.show()}
\end{Verbatim}

When you run the program, you should see a graph of the height over time.

\includegraphics[width=0.7\linewidth]{heightplot.png}

It is more interesting if we can see all three: acceleration, speed, and height.
So lets make three stacked plots.  Change the plotting code in \filename{falling\_graph.py} to:\index{matplotlib!subplots}

\begin{Verbatim}
# Make a plot with three subplots
fig, ax = plt.subplots(3,1)
fig.suptitle("Falling Hammer")

# The first subplot is acceleration
ax[0].set_ylabel("Acceleration (m/s/s)")
ax[0].plot(times, accelerations)

# Second subplot is speed
ax[1].set_ylabel("Speed (m/s)")
ax[1].plot(times, speeds)

# Third subplot is height
ax[2].set_xlabel("Time (s)")
ax[2].set_ylabel("Height (m)")
ax[2].plot(times, heights)
plt.show()
\end{Verbatim}

Now you will get plots of all three variables:

\includegraphics[width=0.8\linewidth]{stackedplot.png}

This is what we expected, right?  The acceleration is a constant negative number.  The speed is a
straight line with a negative slope.  The height is a parabola.

A natural question at this point is ``When exactly will the hammer hit the
ground?''  That is, when does $height = 0$? The values of $t$ where a function is zero are
are known as its \textit{roots}. Height is given by a quadratic function. In the next
chapter, you will get the trick for finding the roots of any quadratic
function.


\begin{cor}
    \label{cor_bin_exp_and_macaulay_conv}
    (Generalization of Theorem~\ref{thm_odd_power_by_macaulays_convolution} for Binomials.)
    For every $m\in\mathbb{N}, \; x,y\in\mathbb{R}$
    \begin{equation*}
        \sum_{r=0}^{m} \coeffA{m}{r} \convPower{n}{r}{x+y}
        =
        1 + \sum_{r=0}^{2m+1} \binom{2m+1}{r} x^{2m+1-r} y^r, \quad n\geq 0.
    \end{equation*}
\end{cor}
For example, given $m=0,1,2$ the Corollary~\ref{cor_bin_exp_and_macaulay_conv} gives
\begin{equation*}
    \begin{split}
        \sum_{r=0}^{0} \coeffA{0}{r} \convPower{n}{r}{x+y}
        &= 1 + x + y \\
        \sum_{r=0}^{1} \coeffA{1}{r} \convPower{n}{r}{x+y}
        &= 1 + x + y - (x + y) (1 + x + y) (1 - 3 x - 3 y + 2 (x + y)) \\
        &= 1 + x^3 + 3 x^2 y + 3 x y^2 + y^3\\
        \sum_{r=0}^{2} \coeffA{2}{r} \convPower{n}{r}{x+y}
        &=1 + x + y + (x + y) (1 + x + y) \left(-1 + x + 5 x^2 + y + 10 x y + 5 y^2\right. \\
        &-15 x (x + y) + 10 x^2 (x + y) - 15 y (x + y) + 20 x y (x + y) \\
        &+ 10 y^2 (x + y) +9 (x + y)^2 - 15 x (x + y)^2 \\
        &\left.-15 y (x + y)^{2} + 6 {(x + y)}^{3}\right) \\
        &=x^5 + 5 x^4 y + 10 x^3 y^2 + 10 x^2 y^3 + 5 x y^4 + y^5 + 1
    \end{split}
\end{equation*}
Above example could be verified using using the commands
\begin{itemize}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionTest[0, x + y]}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionTest[1, x + y]}
    \item \texttt{Expand[BinomialTheoremAndDiscreteConvolutionTest[1, x + y]]}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionTest[2, x + y]}
    \item \texttt{Expand[BinomialTheoremAndDiscreteConvolutionTest[2, x + y]]}
\end{itemize}
defined in Mathematica package at~\cite{PK22Source}.
\begin{cor}
    \label{cor_bin_exp_and_macaulay_conv_strict}
    (Generalization of Theorem~\ref{thm_odd_power_by_macaulays_convolution_strict} for Binomials.)
    For every $m\in\mathbb{N}, \; x,y\in\mathbb{R}$
    \begin{equation*}
        \sum_{r=0}^{m} \coeffA{m}{r} \convPower{n}{r}{x+y}
        =
        -1 + \sum_{r=0}^{2m+1} \binom{2m+1}{r} x^{2m+1-r} y^r, \quad n > 0.
    \end{equation*}
\end{cor}
For example, given $m=0,1$ the Corollary~\ref{cor_bin_exp_and_macaulay_conv_strict} gives
\begin{equation*}
    \begin{split}
        \sum_{r=0}^{0} \coeffA{0}{r} \convPower{n}{r}{x+y}
        &= x + y - 1 \\
        \sum_{r=0}^{1} \coeffA{1}{r} \convPower{n}{r}{x+y}
        &= -1 + x + y - (-1 + x + y) (x + y) (-1 - 3 x - 3 y + 2 (x + y)) \\
        &= x^3 + 3 x^2 y + 3 x y^2 + y^3 - 1
    \end{split}
\end{equation*}
Above example could be verified using using the commands
\begin{itemize}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionStrictTest[0, x + y]}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionStrictTest[1, x + y]}
    \item \texttt{Expand[BinomialTheoremAndDiscreteConvolutionStrictTest[1, x + y]]}
\end{itemize}
defined in Mathematica package at~\cite{PK22Source}.
From the other prospective, let be a function $f_r(t,k) = (t-k)^r, \; t \geq k$, then following identity holds
\begin{equation}
(x-2a)
    ^{2m+1} + 1 =\sum_{r=0}^{m} \coeffA{m}{r} (f_r(t,k) \ast f_r(t,k))[x]
    \label{eq:parametric-identity}
\end{equation}
Let be a function $g_r(t,k) = (t-k)^r, \; t > k$, then
\begin{equation}
(x-2a)
    ^{2m+1} - 1 =\sum_{r=0}^{m} \coeffA{m}{r} (g_r(t,k) \ast g_r(t,k))[x]
    \label{eq:parametric-identity-strict}
\end{equation}

\subsection{Generalization for Multinomials} \label{subsec:generalization-for-multinomials}
In this subsection we generalize
Theorems~\ref{thm_odd_power_by_macaulays_convolution},~\ref{thm_odd_power_by_macaulays_convolution_strict}
for multinomial cases.
\begin{cor}
    \label{cor_mult_exp_and_macaulay_conv}
    (Generalization of Theorem~\ref{thm_odd_power_by_macaulays_convolution} for Multinomials.)
    For every $x_1, x_2, \ldots, x_t\in\mathbb{R}, \; m\in\mathbb{N}, \; n\geq1\in\mathbb{N}$
    \[
        \sum_{r=0}^{m} \coeffA{m}{r} \convPower{n}{r}{\multifoldSum{t}} =
        1 + \sum_{\multifoldSum[k]{t}=2m+1} \binom{2m+1}{k_1, k_2,\ldots, k_t} \prod_{\ell=1}^{t} x_\ell^{k_\ell}
    \]
\end{cor}
For instance, given $m=1$ the Corollary~\ref{cor_mult_exp_and_macaulay_conv} gives
\begin{equation*}
    \begin{split}
        &\sum_{r=0}^{1} \coeffA{1}{r} \convPower{n}{r}{x+y+z} \\
        &=1 + x + y + z - (x + y + z) (1 + x + y + z) (1 - 3 x - 3 y - 3 z + 2 (x + y + z)) \\
        &=1 + x^3 + 3 x^2 y + 3 x y^2 + y^3 + 3 x^2 z + 6 x y z + 3 y^2 z + 3 x z^2 + 3 y z^2 + z^3.
    \end{split}
\end{equation*}
Above example could be verified using using the commands
\begin{itemize}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionTest[1, x + y + z]}
    \item \texttt{Expand[BinomialTheoremAndDiscreteConvolutionTest[1, x + y + z]]}
\end{itemize}
defined in Mathematica package at~\cite{PK22Source}.
\begin{cor}
    \label{cor_mult_exp_and_macaulay_conv_strict}
    (Generalization of Theorem~\ref{thm_odd_power_by_macaulays_convolution_strict} for Multinomials.)
    For each $\multifoldSum{t} \geq 1, \; x_1,x_2,\ldots,x_t\in\mathbb{R}, \; m\in\mathbb{N}, \; n\geq1\in\mathbb{N}$
    \[
        \sum_{r=0}^{m} \coeffA{m}{r} \convPower{n}{r}{\multifoldSum{t}} =
        -1 + \sum_{\multifoldSum[k]{t}=2m+1} \binom{2m+1}{k_1, k_2,\ldots, k_t} \prod_{\ell=1}^{t} x_\ell^{k_\ell}
    \]
\end{cor}
For example, given $m=1$ the Corollary~\ref{cor_mult_exp_and_macaulay_conv_strict} gives
\begin{equation*}
    \begin{split}
        &\sum_{r=0}^{1} \coeffA{1}{r} \convPower{n}{r}{x+y+z} \\
        &=-1 + x + y + z - (-1 + x + y + z) (x + y + z) (-1 - 3 x - 3 y - 3 z + 2 (x + y + z)) \\
        &=-1 + x^3 + 3 x^2 y + 3 x y^2 + y^3 + 3 x^2 z + 6 x y z + 3 y^2 z + 3 x z^2 + 3 y z^2 + z^3.
    \end{split}
\end{equation*}
Above example could be verified using using the commands
\begin{itemize}
    \item \texttt{BinomialTheoremAndDiscreteConvolutionStrictTest[1, x + y + z]}
    \item \texttt{Expand[BinomialTheoremAndDiscreteConvolutionStrictTest[1, x + y + z]]}
\end{itemize}
defined in Mathematica package at~\cite{PK22Source}.


\section{Signal processing for vibration analysis}
Vibration data is vital for health assessment of a given system and carries out very useful information about its performance (refer to section \ref{sec:data-acquisition} for details about data acquisition), yet these information are usually hard to observe in its raw waveform. Signal processing techniques are used to convert the raw waveform from time domain into frequency or time–frequnecy domains.

\section{Fourier analysis}
Fourier analysis, also called harmonic analysis, of a periodic signal $x(t)$ is the decomposition of the series into summation of sinusoidal components, where each sinusoid has a specific amplitude and phase.

The Fourier transform (FT) of a signal $x(t)$ can be mathematically given by equation \ref{equation:fourier-transform}:

\begin{equation}
    X(w) = \int_{-\infty}^{\infty}x(n)e^{-jwt}dt
    \label{equation:fourier-transform}
\end{equation}

In practical applications of digital signal processing where signals are discrete in time rather than continuous (e.g. vibration analysis) a discretized version called discrete fourier transform (DFT) is used instead, it is expressed mathematically by equation \ref{equation:discrete-fourier-transform}:

\begin{equation}
    X(w) = \sum_{-\infty}^{\infty}x(t)e^{-jwt}dt
    \label{equation:discrete-fourier-transform}
\end{equation}

Fast Fourier transform (FFT) is an effective algorithm used to implement DFT in computers. Figure \ref{figure:fft} shows a signal in its waveform (or time domain) and its corresponding spectrum (frequency domain) obtained using FFT algorithm. The spectrum shows the frequency components present in the signal:

\begin{figure}[H]
    \centering
    \includegraphics{figures/fft.pdf}
    \caption{Signal in the time domain and its fast Fourier transform}
    \label{figure:fft}
\end{figure}

\section{Wavelet transform}
Wavelet transform is also a spectral analysis tool, like Fourier transform. The main difference is that Fourier transform decomposes the signal into sinusoidal components, but wavelet transform decomposes it into a set of oscillatory functions called \textbf{wavelets}. Unlike sinusoids, wavelets are localized in time, thus wavelet transform doesn't only provide information about the frequency present in a signal but also the time of their occurence. Wavelet transform is a much better solution than Fourier transform when studying non-linear non-stationary signals (i.e. its frequency components vary with time).

Figure \ref{fig:time-frequency-plane} shows the difference in time and frequency resolutions between different methods. In the waveform, the signal has absolute resolution in time and zero resolution in frequency. Fourier transform on the contrary transforms the signal totally into the frequency domain, therefore it has absolute resolution in frequency but no resolution in time. Short-time Fourier transform is calculated identically to fourier transform but it is performed on separate segments of the original signal to preserve some resolution in time. Wavelet transform on the other hand exhibits a high time resolution for high frequencies and high frequency resolution for low frequencies:

\begin{figure}[H]
    \centering
    \input{figures/time-frequency-resolution.tex}
    \caption{Time—frequency resolution plane}
    \label{fig:time-frequency-plane}
\end{figure}

There are a wide variety of wavelets that serve different purposes like Morlet wavelet, Daubechies wavelet and many others.

\begin{figure}[H]
    \centering
    \includegraphics{figures/wavelets.pdf}
    \caption{Different types of wavelets}
    \label{fig:wavelets}
\end{figure}

\subsection{Continuous wavelet transform}
Mathematically, continuous wavelet transform is defined by equation \ref{equation:cwt}:

\begin{equation}
    CWT_x^\psi(\tau, s)=\frac{1}{\sqrt{|s|}}\int_{-\infty}^{\infty}x(t)\psi^* \left(\frac{t-\tau}{s}\right)dt
    \label{equation:cwt}
\end{equation}

Where $x(t)$ is the original signal, $\psi^*$ is a function called the \textbf{mother wavelet}; $s$ and $\tau$ are the \textbf{scale} and \textbf{translation} parameters respectively. The original signal is multiplied by the mother wavelet which is scaled using different scales then translated over the signal.

The output of \acrshort{cwt} is a scaleogram like the one in figure \ref{fig:scaleogram} which is a scaleogram (filled contour plot) of vibrations data snapshot of 25ms:

\begin{figure}[H]
    \centering
    \includegraphics{figures/scaleogram.pdf}
    \caption{Scaleogram of vibration data snapshot}
    \label{fig:scaleogram}
\end{figure}

The x and y axes represent time and frequency respectively. Different colors indicate the power (i.e. amplitude) of each frequency (y-axis) during each instant of time (x-axis) which—unlike Fourier transform—provides information about the frequencies present in the signal and also the instances of time when these frequencies are present.

\subsection{Discrete wavelet transform}
In practical applicaions, discrete wavelet transform (DWT) is implemented as a filter bank where the signal is passed therough low- and high-pass filters to obtain \textbf{approximation} and \textbf{decomposition coefficients}. Figure \ref{fig:dwt} shows a DWT with 2 levels of decomposition which yields 2nd order approximation and decomposition coefficients:

\begin{figure}[H]
    \centering
    \input{figures/dwt.tex}
    \caption{Discrete wavelet transform (DWT) as a filter bank}
    \label{fig:dwt}
\end{figure}

\begin{figure}[H]
    \centering
    \includegraphics{figures/dwt_chirp.pdf}
    \caption{Level 3 signal decomposition using DWT}
    \label{fig:dwt-chirp-signal}
\end{figure}

DWT returns two sets of coefficients: \textbf{approximation coefficients} associated with the low pass filter and \textbf{detail coefficients} associated with the high pass filter of the DWT. By applying DWT again on the approximation coefficients the next level of decomposition can be obtained. At each level the original signal is downsampled by a factor of 2, this fact imposes a limitation on the possible number of decomposition levels for a given signal.

\section{Conclusion}
Vibration data are discrete signals sampled at a certain frequency in time. Although they hold so many valuable information about equipment performance, these information are usually not directly observable in the time domain. Digital signal processing techniques offer a way to gain more insights from raw vibration data by converting it to frequency or time–frequency domains where unusual frequency components can indicate development of certain degradation pattern. This chapter introduced several of these techniques like Fourier and Wavelet transforms. A following chapter will present the use of these techniques for extracting features that serve as an input for a neural network that can estimate the remaining useful life.
\end{comment}


\chapter{Theory}

\section{Strongly Correlated Systems}
\subsection{Exercise 1}
\paragraph*{(a)} Let $a_i^\dagger$ be the creation operator on orbital $i$ and $a_i$ the annhilation operator. The canonical anticommutation relations are
\begin{align}
    \{a_i,\, a_j\} &= \{a_i^\dagger,\, a_j^\dagger\} = 0 \\
    \{a_i,\, a_j^\dagger\} &= \delta_{i,\, j} \mathds{1}.
\end{align}

\paragraph*{(b)} One transformation is
\begin{align}
    c_{i,0} &= a_i + a_i^\dagger \\
    c_{i,1} &= \mathrm{i}(a_i - a_i^\dagger),
\end{align}
where $i$ is the orbital index.

\paragraph*{(c)} Majorana fermions satisfy the following anticommutation relation
\begin{equation}
    \{c_{i,\alpha}c_{i,\beta}\} = \delta_{i,j}\delta_{\alpha,\beta} \mathds{1}.
\end{equation}

\subsection{Exercise 2}
\paragraph*{(a)} Using the Jordan-Wigner transformation, a fermionic operator $a_j$ or $a_j^\dagger$ becomes a $j$-local qubit operator, since it acts non trivially on $j$ sites.

\paragraph*{(b)} Thanks to the fact of storing only partial sums of qubits occupation, a fermionic operator translates into a $O(\log(j))$-local qubit operator.


\subsection{Introduction}

page setting out linear stuff to come

OLS, generalised linea rmodels etc are m-estimators, as are gmm

h3 on parametric

With maximum likelihood estimation we maximise a function.

We could choose other functions to maximise or minimise.

\(\sum_i f(x_i, \theta )\)

If \(f(x_i, \theta )\) is differentiable wrt to \(\theta \) this can be solved by finding the stationay point.

This is a \(\phi \) type.

Otherwise it is a \(\rho \) type.

page on influence funcitons there

Generalisation of MLE.

\(m_\theta =m_\theta (x, \theta )\)

Z-estimator is where this is met, through diff

\(\frac{\delta }{\delta \theta }m_\theta =z_\theta (\theta , x)=0\)

M-estimator for mean

\(m_\theta (\theta )=-(x-\theta )^2\)

\(z_\theta (\theta )=x-\theta \)