\chapter{Statistics}
Data are collections of information made through observations. Statistics is the device through which data is cranked through to make interpretations of what the data actually says.

\section{OUTSTANDING ITEMS}
\begin{itemize}
	\item Uniformize notation, use $w$ as a vector, etc
	\item Sort sections
	\item Fill out TODOs
\end{itemize}

\subsection{Mean}
Given a set of data, one important feature one might wish to know is what this data looks like on average. This average is typically synonymous with the arithmetic mean defined as

\begin{align}
\E[x] \equiv \bar{x} = \frac{1}{n}\sum_{i=1}^n x_i
\end{align}
Where $n$ is the total amount of points in the data set.
\subsection{Population vs Sample}
The \textbf{population} is the full body of data points that exist for a given study. For instance if you wanted to measure the variation in height of humans, the population variance would necessarily require that you measure all humans for which this study pertains to. Obviously in real life, this is a tough thing to do, so most often a \textbf{sample} is used. A sample is a subset of the full population.

TODO Derive how you get different variances

\subsection{Variance}
TODO
\subsection{Covariance}
The covariance between two variables tells us how closely they track with each other. The \textbf{population covariance} is defined as
\begin{align}
\Cov[x,y] = \frac{1}{n}\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})
\end{align}
Whereas the \textbf{sample covariance} is defined as
\begin{align}
	\Cov[x,y] = \frac{1}{n-1}\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})
\end{align}
TODO: Difference between sample and population.

If the covariance is 0, both variables are independent. If the covariance is large and positive, a larger $x$ value suggests a larger $y$ value (and vice versa). If the coviarance is negative, a larger $x$ value suggests a smaller $y$ value (and vice versa).

\subsubsection{Covariance Matrix}\label{covmat}
With many variables (say $n$ of them), it is often helpful to define the sample Covariance matrix which calculates the covariance between each of the variables $x_i$ with $i = 1,\dots, n$

\begin{align}
\textbf{K}_{x_i,x_j} = \begin{pmatrix}
                             \Cov[x_0,x_0]&\Cov[x_1,x_0]&\Cov[x_2,x_0]&\cdots &\Cov[x_n,x_0]\\
                             \Cov[x_0,x_1]&\Cov[x_1,x_1]&\Cov[x_2,x_1]&\cdots &\Cov[x_n,x_1]\\
                             \Cov[x_0,x_2]&\Cov[x_1,x_2]&\Cov[x_2,x_2]&\cdots &\Cov[x_n,x_2]\\
                             \vdots&\vdots&\vdots&\ddots&\vdots\\
                             \Cov[x_0,x_n]&\Cov[x_1,x_n]&\Cov[x_2,x_n]&\cdots &\Cov[x_n,x_n] \end{pmatrix}
\end{align}

\label{ch:statistics}

\section{Markov Chains}
TODO

\subsection{Gibbs Sampling}
Gibbs sampling is used to approximate a multivariate probability distribution when directly sampling it is difficult.
TODO

\section{Probability}
The mathematics of probability translate chances into dice rolls. For a given event, a value $P\in[0,1]$ is assigned to the fraction of times it is expected to happen. The theoretical underpinnings of probability have remained unresolved since it's birth, leaving two primary camps of practitioners; Bayesians and Frequentists.
TODO Bayesian vs Frequentist
"In particle physics, with its strong tradition of frequentist coverage, prior pdfs are often chosen to provide intervals (in particular upper limits for Poisson means) with good frequentist coverage [4]. In such cases, our use of Bayesian computational machinery for interval estimation is not so much a change in paradigm as it is a technical device for frequentist inference." - Bob Cousins \url{https://ep-news.web.cern.ch/node/3213}
\subsubsection{Bayesian Probability}
TODO
\subsubsection{Frequentists Probability}
TODO

\subsection{Random Variables}
A random variable is one whose values have an associated probability distribution \cite{grus}. Random variables can come in two different flavors: \B{discrete} and \B{continuous}.

Discrete random variables can only take a countable number of distinct values e.g. ($1,~2, ~5,\ldots$) or (red, blue, green). These values get pulled from something called a \B{probability mass function}.

Conversely, continuous random variables represent a continuous spectrum of values e.g. all the real numbers. These values get pulled from something called a \B{probability density function}.

\subsection{Marginalizing A Variable}
TODO

\subsection{Neyman Pearson Lemma}
The main idea is that you want to maximize your detection probability $P_D$ under then constraint that your Type I error (false alarm) probability $P_{FA}$ is equal to a set value $\alpha$. This is done looking for a maximum with a Lagrange multiplier. TODO

\begin{equation}
	\textrm{max}[P_D - \gamma(P_{FA}-\alpha)]
\end{equation}

Given two simple hypotheses $\theta_0$ (the null hypothesis) and $\theta$, (simple hypotheses specify uniquely the probability distributions), we pick

\subsection{Hoeffding's Inequality}\label{hoeffding}
Given $X_1, X_2, \ldots, X_n$ independent random variables each within $0 \leq X_i \leq 1$, Hoeffding's inequality states that
\begin{align}
	P(\bar{X} - E[\bar{X}] \geq t) \leq e^{-2nt^2}
\end{align}
with
\begin{align}
	\bar{X} = \frac{1}{n}\Big(X_1+X_2+\ldots+X_n\Big)
\end{align}
this gives us an easily computable way to find edge's of cumulative distributions.

\subsection{Cross Entropy}
TODO
If we are maximizing for probabilities, using cross-entropy as a loss function represents the negative log-likelihood of the observed data\cite{grus}.

\subsection{Characteristic Function}
The characteristic function is the Fourier transform of a distributions PDF $p(x)$. Defined as
\begin{align}
\phi_x(k) = E[e^{ikx}] = \int_{-\infty}^\infty e^{ikx}p(x)dx
\end{align}
This function has a one to one relationship with the PDF, and is useful in proving things. It turns out that when adding a whole bunch of random numbers $x_1, x_2, ..., x_n$, all pulled from different PDFs $p_1(x_1), p_2(x_2), ..., p_n(x_n)$
\begin{align}
s = x_1 + x_2 + ... + x_n
\end{align}
the characteristic function has the property that
\begin{align}
\phi_s(k) =\phi_1(k)\phi_2(k)...\phi_n(k)
\end{align}

\subsection{Central Limit Theorem}
States that if you have a bunch of random variables $x_1, x_2, ... x_n$, each coming from an arbitrary pdf with mean $\mu_1, \mu_2, ... \mu_n$, and variance $\sigma_1^2, \sigma_2^2, ...\sigma_n^2$, if you add them all up in the limit of large $n$, it should approach a Gaussian distribution with mean $\mu = \sum_i \mu_i$ and variance $\sigma^2 = \sum_i\sigma_i^2$

TODO

Jeffery's prior allows two people to come up with an equal parameter(?) model that looks different, but have the statistics for the two come out the same, when they otherwise wouldn't (Jacobian, etc.)

\subsection{Laplace's Rule of Succession}
Consider the problem of flipping a coin $N$ times and getting $A$ heads. If each flip has a probability $\rho$ to come up heads, the probability for any amount of heads is given by the Binomial distribution
\begin{align}
p(A) = {N\choose A}\rho^A(1-\rho)^{N-A}
\end{align}
If we want to figure out how fair the coin is, i.e. the value of $\rho$, we know that the probability of a given $\rho$ should be proportional to this function as we
\begin{align}
	p(\rho) \propto {N\choose A}\rho^A(1-\rho)^{N-A}
\end{align}
We also know that $\rho$ must be in $[0,1]$. Knowing that the sum of probabilities over $\rho$ must equal 1, the normalization constant must be
\begin{align}
	p(\rho) =\ffrac{ {N\choose A}\rho^A(1-\rho)^{N-A}}{\int_0^1{N\choose A}\rho^A(1-\rho)^{N-A} d\rho}
\end{align}
Since ${N\choose A}$ is constant and common in both numerator and denominator, we have
\begin{align}
	p(\rho) =\ffrac{ \rho^A(1-\rho)^{N-A}}{\int_0^1\rho^A(1-\rho)^{N-A} d\rho}
\end{align}
It so happens the integral is Euler's beta function, which evaluates to
\begin{align}
	\int_0^1\rho^A(1-\rho)^{N-A} d\rho = \frac{A!(N-A)!}{(N+1)!}
\end{align}
This then simplifies our expression with
\begin{align}
	p(\rho) =(N+1){N\choose A}\rho^A(1-\rho)^{N-A}
\end{align}
This then literally tells us what the chances are of a given $\rho$ given any number of flips $N$ and heads $A$. The question answered by Laplace's rule of succession answers: If I flip a coin $N$ times with $A$ heads, what is the probability of getting heads on the next flip $N+1$?

This is the same thing as asking, what is $\rho$, which is the probability of getting a heads on any single flip. Therefore, all we need to do is take the expectation value of $\rho$
\begin{align}
	\langle \rho\rangle &= \int_0^1\rho ~p(\rho) d\rho\\
	&= (N+1){N\choose A}\int_0^1 ~\rho^{A+1}(1-\rho)^{N-A} d\rho
\end{align}
Using the same beta function relation (or alternatively using the chain rule) we get
\begin{align}
	\langle \rho\rangle &= \int_0^1\rho ~p(\rho) d\rho\\
	&= (N+1)\frac{N!}{A!(N-A)!}\frac{(A+1)!(N-A)!}{(N+2)!}
\end{align}
Therefore, the chance that we get a heads on the next flip, is given by
\begin{align}
	\langle\rho\rangle = \frac{A+1}{N+2}
\end{align}

%\subsection{Uncertainties?}
%Study email from Jay "CLCT output to Track Finder" to understand why it makes sense to add errors in quadrature, and write it up here.

\subsection{Bayes Theorem}
The conditional probability, written as $P(A|B)$ is understood as the chance that $A$ happens, given that $B$ has already happened. The chance that both $A$ and $B$ happen $P(A \cap B)$ is the same as the chance $B$ happens at all $P(B)$, multiplied by $P(A|B)$.
\begin{align}
P(A\cap B) = P(A|B)P(B)
\end{align}
This can of course be looked at the other way, with
\begin{align}
P(A\cap B) = P(B|A)P(A)
\end{align}
Therefore
\begin{align}
P(A|B) = \frac{P(B|A)P(A)}{P(B)}
\end{align}

\section{Likelihood Function}

The likelihood function is an interpretation of the probability distribution which takes the observed values $\textbf{x}$ as \textit{fixed} and allows the parameters $\boldsymbol{\mu}$ to vary.

\begin{align}
	P(\textbf{x}|\boldsymbol{\mu}) = \mathcal{L}(\boldsymbol{\mu}|\textbf{x})
\end{align}

\subsection{Maximum Likelihood Estimation}
The most foolproof means of solving an optimization problem is writing down the corresponding likelihood function, then solving for the parameters $\boldsymbol{\mu}$. If doing so analytically, this corresponds to solving the series of equations given by
\begin{align}
\frac{\partial \mathcal{L}}{\partial \boldsymbol{\mu}} = 0
\end{align}
In general this usually isn't possible, so one resorts to other means such as gradient descent. When doing this optimization on a computer, typically one optimizes for
\begin{align}
	\ln\mathcal{L}
\end{align}
instead of $\mathcal{L}$. This is kosher since $\ln$ is a strictly increasing function, so maximizing it means maximizing its arguments \cite{burkov}. There are also some nice properties since
\begin{align}
-2\ln\mathcal{L} \simeq \chi^2
\end{align}

% TODO Maximum Likelihood Estimation

This tells us that if the model had the parameter $\mu_{ML}$ the observation we made would be the most likely, out of the space of all possible $\mu$'s it could have been. To find the \textbf{confidence interval}, we use

\begin{align}
-2\ln\mathcal{L} \simeq \chi^2
\end{align}
This is because (TODO: Learn more about this) most things tend towards Gaussians in the limit of large numbers and in the product of many Likelihood functions one gets
\begin{align}
	-2\ln\prod_i\mathcal{L}_i = -2\ln\prod_i e^{(x_i-\mu)^2/2\sigma^2} = \sum_i\frac{(x_i-\mu)^2}{\sigma^2} = \chi^2
\end{align}
(TODO, $i$'s are correct?). Now using Wilk's Theorem (TODO), one can look at the change in $\chi^2$ as you change the value of $\mu$. Once you increase $\mu$ in one direction from $\mu_{ML}$, the $\chi^2$ will grow. $\chi^2$ will of course also grow if you decrease it from $\mu_{ML}$. You can quantify how much you can change $\mu$ to some limit of $\chi^2$ given by (SOME THEORY). Depending on what kind of confidence interval you are looking for, or the amount of parameters you still have, this change in $\chi^2$ will be different.

This defines your confidence interval, which tells you, given whatever data you have, what the values of $\mu$,  [$\mu_1, \mu_2$] would be such that what you saw was on the tail end of its distribution (e.g. outside of 95\% of the entire distribution)

\subsection{Asimov Dataset}
An Asimov dataset is an artificial dataset such that when it is used to evaluate the estimators for all parameters in the context of maximum likelihood estimation, one obtains the (assumed) true parameter values. This is typically done to derive $\sigma$ of the parameter of interest $\mu$, which is then used in asymptotic formulae. Some notes on notation
\begin{itemize}
	\item $\hat{\mu}$: Best estimate on the parameter of interest $\mu$
	\item $\mu'$: Assumed true mean of the Gaussian used in Asymptotic formula approximations
\end{itemize}
This method is used to derive the approximate Gaussian width when calculating Asymptotic formulas for limits in particle physics \cite{cowan}. It was named after author, Isaac Asimov, whose 1955 short story, Franchise, envisaged the 2008 US Presidential Election decided by one voter representative of the entire electorate.

\section{Hypothesis Testing}

\subsection{Confidence Intervals}
TODO
Also add calculation of confidence intervals of fit parameters.
\url{https://online.stat.psu.edu/stat415/lesson/7/7.5}
Add confidence interval of a Gaussian

\subsection{P-Value}
The $p$-value is defined as the probability under the null hypothesis of obtaining a result equal to or more extreme than what was actually observed. The smaller the $p$-value, the more it tells investigators that the null hypothesis may not adequately explain the observation.

\subsection{A/B Testing}
See book \cite{grus}

\subsection{F-Test}
TODO

\section{Kalman Filter}
The Kalman filter\cite{fruwirth} uses a series of measurements taken in a time series to make an estimate of the underlying parameters. It works as an iterative $\chi^2$ method, and is useful when there is noise introduced between measurements.

Let us assume the state vector of interest is $\textbf{x}$. If we assume the evolution of the state vector is linear, we have
\begin{align}
    \textbf{x}_k = \textbf{F}_{k-1}\textbf{x}_{k-1} + w_{k-1}
\end{align}
Where $k$ is the current state, $w$ is random noise that is introduced, and $\textbf{F}$ is how the state vector propagates itself (here it is assumed to be linear). Measurements $\textbf{m}$ in each detector $k$ can be written as
\begin{align}
    \textbf{m}_k = \textbf{H}_k\textbf{x}_k + \epsilon_k
\end{align}
We assume both $w_k$ and $\epsilon_k$ are independent and have a mean of zero. The Kalman algorithm performs three different analyses:
\begin{enumerate}
    \item \textbf{Filtering}: estimation of present state vector based off of all previous measurements.
    \item \textbf{Prediction}: estimation of future state vector.
    \item \textbf{Smoothing}: estimation of the state vector at some time in the past using all information known in the present.
\end{enumerate}
The Kalman filter minimizes the mean squared error and is in that sense the optimum solution to these problems. We can denote our estimate $\hat{\textbf{x}}_k^i$ for each of these three cases:
\begin{itemize}
    \item $i < k$: prediction, since we are using fewer points than the state we are looking at $k$
    \item $i=k$: filtering, since we are using all previous states and the measurement at $k$ itself
    \item $i > k$: smoothing, since we already had a measurement at this point, but used additional later values to get a better estimate
\end{itemize}
One can then define the covariance matrix as the difference between the ...

\section{Bootstrapping}
TODO


\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\title{Eigenvalues and Eigenvectors}
\begin{document}
  \pagenumbering{gobble}
  \maketitle
  \newpage
  \pagenumbering{arabic}

\section*{The power method}

Let us have a square matrix A. It's size is $nxn$. \\
It has a number of independet real eigenvalues: $\lambda_1, \lambda_2, \dots, \lambda_n$. \\
Eigenvectors corresponding to the eigenvalues are: $v_1, v_2, \dots, v_n$. \\
One condition: $|\lambda_1| > |\lambda_2|$.

The eigenvectors are independent, which means that they are as well basis vectors. This implies that any vector in the same space can be written as a linear combination of the eigenvectors.

$$x_0 = c_1v_1+c_2v_2+\dots+c_nv_n$$

where $c_1\ne0$

After multypling both sides by matrix A:

$$Ax_0 = c_1Av_1+c_2Av_2+\dots+c_nAv_n$$

From the defintion $Av_i = \lambda{v_i}$, so:

$$Ax_0 = c_1\lambda_1v_1+c_2\lambda_2v_2+\dots+c_n\lambda_nv_n$$

$$Ax_0 = c_1\lambda_1[v_1+\frac{c_2}{c_1}\frac{\lambda_2}{\lambda_1}v_2+\dots+\frac{c_n}{c_1}\frac{\lambda_n}{\lambda_1}v_n]= c_1\lambda_1x_1$$

This was the first iteration. To begin the second iteration, we multiply $A$ by $x_1$:

$$Ax_1 = \lambda_1{v_1}+\frac{c_2}{c_1}\frac{\lambda_2^2}{\lambda_1}v_2+\dots+\frac{c_n}{c_1}\frac{\lambda_n^2}{\lambda_1}v_n$$

$$Ax_1 = \lambda_1[v_1+\frac{c_2}{c_1}\frac{\lambda_2^2}{\lambda_1^2}v_2+\dots+\frac{c_n}{c_1}\frac{\lambda_n^2}{\lambda_1^2}v_n] = \lambda_1x_2$$

After k iterations we have:

$$Ax_{k-1} = \lambda_1[v_1+\frac{c_2}{c_1}\frac{\lambda_2^k}{\lambda_1^k}v_2+\dots+\frac{c_n}{c_1}\frac{\lambda_n^k}{\lambda_1^k}v_n] = \lambda_1x_k$$

For large k, $(\frac{\lambda_n}{\lambda_1})^{k} = 0$

We have now the largest eigenvalue and its corresponding eigenvector:

$$Ax_{k-1} = {\lambda_1}v_1$$

\section*{The inverse power method}
The reciprocals of the eigenvalues of A are the eigenvalues of it's inverse matrix  $A^{-1}$. \\
This will help us to find the smallest eigenvalue of A. \\
Instead of multiplying A as in power method, we multiply it's inverse to find it's largest value.

\section*{QR method}

The QR method is used to find all eigenvalues of a matrix, without finding the eigenvectors at the same time.\\

1. The eigenvalues and corresponding eigenvectors of similar matrices are the same.

Two square matrices A and B are similar if:

$$A = C^{-1}BC$$

where C is an invertible matrix.\\

2. You can present any matrix as a product of two other matrices.

\begin{align}
A &= QR
\end{align}

Here we want to get an orthogonal matrix Q and an upper triangular matrix R.\\

A matrix M is an orthogonal matrix if: $M^{-1} = M^T$. Thus $M^*M = I$. \\

Let us rewrite equation (1):

$$RQ = Q^*AQ$$

$$RQ = Q^{-1}AQ$$

RQ has the same eigenvalues as A.\\

Compute a QR factorization and reverse the order of multiplcation of Q and R.

$A_0 = A$

$$A_k = R_kQ_k = Q^{-1}_kA_kQ_k$$

$$A_{k-1}=Q_kR_k$$

We will finally converge to an upper triangular matrix form as the iteration progresses:

\begin{align*}
\begin{split} A_k = R_kQ_k = \begin{bmatrix}
\lambda_1 & X & \dots & X\\
0 & \lambda_2 & \dots & X\\
& &\dots &\\
0 & 0 & \dots & \lambda_n\\
\end{bmatrix}\end{split}
\end{align*}

\end{document}


\section*{Classification}
\subsection*{0/1 loss - "NP-Hard"}
0/1 loss is not convex and not differentiable.\\
$l_{0/1} (w;y_i,x_i) =
\begin{cases}
    1 \text{ , if } y_i \neq sign(w^Tx_i)\\
		0 \text{ , otherwise}
\end{cases}$

\subsection*{Perceptron loss}
Perceptron loss is convex and not differentiable, but gradient is informative.\\
$l_{P} (w;y_i,x_i) = max\{0, -y_i w^T x_i \}$\\
$\nabla_w l_p(w;y_i,x_i) = \begin{cases}
    0 &\text{ , if } -y_i w^T x_i \leq 0\\
    -y_i x_i &\text{ , if } -y_i w^T x_i > 0
\end{cases}\\
w^* = \underset{w}{\operatorname{argmin}} \sum_{i=1}^n l_p (w;y_i,x_i)$

\subsection*{Stochastic Gradient Descent (SGD)}
1. Start at an arbitrary $w_0 \in \mathbb{R}^d$\\
2. For $t = 1, 2,  ...$ do: \\
	Pick data point $(x',y') \in_{u.a.r.} D$\\
	$w_{t+1} = w_t - \eta_t \nabla_w l(w_t;x',y')$\\
Perceptron Algo: SGD with Perceptron loss

%\subsection*{Perceptron Algorithm}
%Stoch. Gradient Descent with Perceptron loss\\
%\emph{Theorem:} If $D$ is linearly separable $\Rightarrow$ Perceptron will obtain a linear separator.

%\subsection*{Hinge loss}
%Loss for Support Vector Machine.\\
%$l_H(w;x,y) = max \{0,1-y w^T x\}$

\subsection*{Support Vector Machine - "Max Margin"}
Hinge loss: $l_H(w;x,y) = max \{0,1-y w^T x\}$\\
Goal: max. the margin around the separator.\\
$w^* = \underset{w}{\operatorname{argmin}} \sum_{i=1}^n  max \{0,1-y_i w^T x_i\} + \lambda ||w||_2^2\\
g_i(w) = max \{0,1-y_i w^T x_i\} + \lambda ||w||_2^2\\
\nabla_w g_i(w) = \begin{cases}
    -y_i x_i + 2\lambda w &\text{ , if $y_i w^T x_i<1$}\\
		2\lambda w &\text{ , if $y_i w^T x_i \geq 1$}
\end{cases}$

\subsection*{L1-SVM}
$\underset{w}{\operatorname{min}} \lambda ||w||_1 + \sum_{i=1}^n max(0,1-y_i w^T x_i)$
$\rightarrow$ enourages coefficients to be zero (only linear models).

%\subsection*{Matrix-Vector Gradient}
%multiply transposed matrix to the same side as its occurance w.r.t. derivate variable: $\beta \in \mathbb{R}^d$
%$\nabla_\beta ( ||y-X\beta||_2^2 + \lambda ||\beta||_2^2 ) = 2X^T (y-X\beta) + 2\lambda \beta$\\


\subsection{Quotient groups}

We have a group \(G\) and a normal subgroup \(N\).

We define a quotient group from this as \(G/N\). This is the set of cosets from \(N\).


\chapter*{Introduction}
When you want to develop a selfdriving car, you have to plan which path
it should take. A reasonable choice for the representation of
paths are cubic splines. You also have to be able to calculate
how to steer to get or to remain on a path. A way to do this
is by applying the \href{https://en.wikipedia.org/wiki/PID_algorithm}{PID algorithm}.
This algorithm needs to know the signed current error. So you need to
be able to get the minimal distance of a point (the position of the car)
to a cubic spline (the prefered path)
combined with sign (which represents the steering direction).
As one steering direction might be prefered, it is not only necessary to
get the minimal absolute distance, but might also help to get all points
on the spline with minimal distance.

In this paper, I want to discuss how to find all points on a cubic
function with minimal distance to a given point.
As other representations of paths might be easier to understand and
to implement, I will also cover the problem of finding the minimal
distance of a point to a polynomial of degree 0, 1 and 2.

While I analyzed this problem, I've got interested in variations
of the underlying PID-related problem. So I will try to give
robust and easy-to-implement algorithms to calculate the distance
of a point to a (piecewise or global) defined polynomial function
of degree $\leq 3$.

When you're able to calculate the distance to a polynomial which is
defined on a closed invervall, you can calculate the distance from
a point to a spline by calculating the distance to the pieces of the
spline.


% !TEX root = ../main.tex

\section{Spectrum}

\subsection{Eigenvalues and eigenvectors}

$$
Ax = \lambda x
$$

\begin{itemize}
  \item An eigenvector $x$ is a non-zero vector belonging to eigenvalue $\lambda$
  \item An $n \times n$ matrix $A$ has $n$ eigenvalues (possibly not all distinct)
  \item The eigenvalue problem for the transposed matrix leads us to talk about
  right-eigenvectors ($A$ and $A^T$ have the same eigenvalues, but not necessarily the same
  eigenvectors)
  \item The eigenvectors are linearly independent
  \item Mostly, we normalized eigenvectors such that $x^Tx = 1$
  \item If $P^{-1}$ exists, then $PAP^{-1}$ has the same eigenvalues as $A$, but each
  eigenvector is $y = Px$, where $x$ is an eigenvector of $A$
\end{itemize}

\begin{align*}
  A(rx) = \lambda (rx) \quad r \ne 1
\end{align*}

\begin{gather*}
  A
  \begin{bmatrix}
    x_1 & x_2 & \cdots & x_N
  \end{bmatrix} =
  \begin{bmatrix}
    x_1 & x_2 & \cdots & x_N
  \end{bmatrix}
  \begin{bmatrix}
    \lambda_1 & & & \\
    & \lambda_2 & & \\
    & & \ddots & \\
    & & & \lambda_N
  \end{bmatrix} \\
  AX = X\Lambda \implies A = X\Lambda X^{-1} \\
  (A - \lambda I)x = 0 \implies \det(A - \lambda I) = 0
\end{gather*}

\subsubsection{Basic theorem for symmetric matrices}
\textbf{Any real symmetric matrix $S$} can be written as $S = X \Lambda X^T$,
where $X$ is the orthogonal matrix with real eigenvectors in the columns and
$\Lambda = diag(\lambda_1, ..., \lambda_N)$, where $\lambda_j$ is the $j$-th real eigenvalue.

The real eigenvalues can be ordered as

\begin{gather*}
  \lambda_N \le \lambda_{N-1} \le \cdots \lambda_2 \le \lambda_1
\end{gather*}

If symmetric: $A = A^T = X \Lambda X^T = \sum_{k=1}^{N} \lambda_k x_k x^T_k$

\subsubsection{The orthogonal matrix X of a symmetric matrix A}

$$
A^T = \sum_{k=1}^N \lambda_k^m x_k x^T_k
$$

Orthogonality of eigenvectors: $x_k^T x_m = \delta_{km} \implies X^T X = I \implies X^{-1} = X^T$

A matrix and its inverse commute: $X^{-1}X = X X^{-1} \implies X^TX = XX^T = I$ \textbf{Double
orthogonality}. Both column vectors (=eigenvectors of A) and row vectors of $X$ are orthogonal.

\subsubsection{Gerschgorin's theorem}

It explains why we use spectral radious..

\begin{quotation}
  Each eigenvalue of an $n\times n$ matrix $A$ lies in at least one of the circular discs
  with a center $a_{jj}$ and radii $R_j = \sum_{k=1;k \ne j}^n |a_{jk}|$
\end{quotation}

\textbf{Consequence for Adjacency matrix $A$: } $|\lambda| \le \sum_{k=1}^n a_{rk} = d_r$
$$
\lambda_1 \le d_{max}
$$

The largest eigenvalue is always smaller thant the maximum degree

\textbf{Properties}:
\begin{itemize}
  \item Spectrum of $A$:
  \begin{itemize}
    \item All eigenvalues lie in the interval $(-d_{max}, d_{max}]$
    \item $\sum_{j=1}^N \lambda_j = 0 \qquad \sum_{j=1}^N \lambda_j^2 = 2L \qquad
    \sum_{j=1}^N \lambda_j^k = Trace(A^k)$
    \item Perron-Frobenius Theorem: $\lambda_1$ non-negative and components eigenvector $x_1$ are
    non-negative. (irreducible = connected: positive)
  \end{itemize}
  \item Spectrum of $Q = \Delta - A = BB^T$:
  \begin{itemize}
    \item Any eigenvalue $\mu_k$ is non-negative and the smallest $\mu_N = 0$
    \item Complexity (number of spanning trees is) $\epsilon(G) = \frac{1}{N} \prod_{k=1}^{N-1} \mu_k$
    \item The second smallest eigenvalue of the Laplacian $Q$, called \textbf{the algebraic
    connectivity} $a(G) = \mu_{N-1}$, quantifies how strongly a graph is
  \end{itemize}
\end{itemize}

\subsection{Largest eigenvalue of a symmetric matrix}

If $Ax = \lambda x$ then $A^k x = \lambda^k x$ for non-negative integers $k$.

\textbf{Power method} (eigenvector):
$$
A^k w = \alpha_1 \lambda_1^k x_1 \left(1 + \left(\left|\frac{\lambda_2}{\lambda_1}
\right|^k\right)\right)
$$

\textbf{Gerschgorin's theorem}: $\lambda_1 \le d_{max}$

\textbf{Rayleigh's principle}: $\lambda_1 \ge \frac{w^T Aw}{w^Tw}$ with equality only if $w = x_1$

\subsubsection{Spectral radius adjacency matrix}
Rayleigh principle: $\lambda_1 \ge \frac{w^T Aw}{w^TW}$ with equality only if $w = x_1$

Choose $w = u$, then $u^Tu = N$ and $u^TAu = 2L$

Hence:
$$
d_{max} \ge \lambda_1 (A) \ge \frac{2L}{N} = E[D]
$$

Equality holds if $u$ is an eigenvector of $A$. In general, $A u = d$ if $d = r u$, then
$u$ is an eigenvector.

The eigenvalue equation $A x = \lambda x$ implies that $A^m x = \lambda^m x$

Rayleigh principle

Choose $w = u$ and $m = 2$ then $u^Tu = N$ and
$u^T A^2 u= (Au)^T (Au) = d^Td = \sum_{j = 1}^N d^2_j$

$$
\lambda_1 \ge E[D]\sqrt{1 + \frac{Var[D]}{(E[D])^2}} \ge E[D]
$$

Tighter bound thant the previous one

\subsubsection{Spectral radius and subgraph structure}
Let's have a graph $G$ that has two subgraphs $G_1$ and $G_2$

$$
A =
\begin{bmatrix}
  A_1 & C \\
  C^T & A_2
\end{bmatrix}
\implies \lambda_1(G) \ge \lambda_1(G_1)
$$

\subsection{Graph metric: Eigenvector centrality}

\begin{itemize}
  \item The $i$-th component $(x_j)_i$ of the eigenvector $x_j$ belonging to the $j$-th
  largest eigenvalue $\lambda_j$ of the adjacency matrix $A$ reflects a property of node
  $i$ in the graph at ``eigenfrequency'' $\lambda_j$
  \begin{itemize}
    \item Hence $(x_j)_i^2$ is non-zero and can be interpreted as a centrality metrics for node $i$
  \end{itemize}
\end{itemize}


\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% Main maths packages
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathrsfs}

% Declare a differential command in order to not display the "d" in italic
\newcommand*{\dd}[1][x]{\,\mathrm{d}#1}

\begin{document}

\section*{Integrals}

\subsection*{Simple integrals}

\[\int x^2 \dd\]

% The bounds are written at the right of the integral
\[\int_{0}^{\infty} x^2 \dd\]

% The bounds are written at the top and bottom of the integral by specifying \limits.
%Refers styling.tex for more information about the command \limits.
\[\int\limits_{0}^{\infty} x^2 \dd\]

\[\oint x^2 \dd\]

\subsection*{Double integrals}

\[\iint x^2 \dd \]

\[\int\int x^2 \dd\]

\[\int_{0}^{4}\int_{1}^{3} x^2 \dd\]

\subsection*{Triple integrals}

\[\iiint x^2 \dd\]

\[\int_{x=0}^{x=5}\int_{y=0}^{y=4}\int_{z=1}^{z=3} x^2+y-z \dd\]

\[\int\limits_{x=0}^{x=5}\int\limits_{y=0}^{y=4}\int\limits_{z=1}^{z=3} x^2+y-z \dd\]

\end{document}


\subsection{Exponential discounting}

\subsubsection{Introduction}

We have:

\(U_T=\sum_[t=T]^{\infty }d_tU(x_t)\)

\subsubsection{Exponential discounting}

\(d_t=(1+\delta )^t\)

\(U_T=\sum_[t=T]^{\infty }(1+\delta )^tU(x_t)\)

\(\delta \) is the discount rate.


\subsection{Exponential separation of QMDD from ADD}

Algebraic Decision Diagrams (\textsf{ADD}) are a precursor to \textsf{QMDD}s.
The \textsf{QMDD} data structure groups together nodes representing subfunctions which differ only by a multiplicative constant.
By labelling the edges with appropriate weights, no information about the state is lost.
In this Section, we show that this leads to an exponential decrease in the size of the diagram.
Put another way, we have $\textsf{ADD}\subsetneq \textsf{QMDD}$.
In the literature, \textsf{ADD} is sometimes called \textsf{QuiDD}.

\begin{theorem}
	There is an infinite family of quantum states $\{\ket{\phi_n}_n\}_{n}$ such that every reduced, ordered \textsf{ADD} needs $\Theta(2^n)$ nodes to store $\ket{\phi_n}$, but every \textsf{QMDD} needs only $\Theta(n)$ nodes.
\end{theorem}
\begin{proof}
	The state is the Fourier-transformed state
	\begin{align}
		\ket{\phi_n}=(\ket{0}+e^{i\pi})\otimes (\ket{0}+e^{i\pi2^{-1}})\otimes \cdots \otimes (\ket{0}+e^{i\pi2^{-n+1}})
	\end{align}
	Since the state is a product state, its QMDD only has $n$ nodes, not counting the \textsf{leaf} node.

	However, the amplitudes of each basis vector is different, so the \textsf{ADD} has an exponential number of leaves, each containing a different amplitude. Namely, the amplitude in $\ket{\phi}_n$ of basis vector $\ket{x}_n$ is $\frac{1}{\sqrt{2^n}}e^{i\pi x2^{-n}}$.
\end{proof}


\section{Probabilistic Information Retrieval}
The complexity of text analysis is such that we can't have
a notion of correctness of an answer to a query, we therefore switch
idea and change \emph{true} to \emph{probable}.

\subsection{Review of probability theory}

\paragraph{Random variable}
A variable $A$ represents an events, so a subset of the space of possible
outcomes. Another representation of $A$ is through a random variable,
that maps outcomes to real numbers.

\paragraph{Joint and conditional probabilities}
We denote $P(A|B)$ the probability of $A$ occurring given the occurrence of $B$.

\paragraph{Chain rule}
The following twos hold
$$P(A,B) = P(A \cap B) = P(A|B)P(B) = P(B|A)P(A)$$
$$P(\neg A, B) = P(\neg A|B)P(\neg A)$$

\paragraph{Partition rule}
If an event $B$ can be divided into an exhaustive set of disjoint sub-cases,
the probability of $B$ is the sum of the sub-cases
$$P(B) = P(A,B) + P(\neg A, B)$$

\paragraph{Bayes rule} The following rule holds
$$P(A|B) = \frac{P(B|A)P(A)}{P(B)}$$

This rule is a powerful tool to invert the conditional probability.
The first step is to estimate the likelihood of the event $A$, thus $P(A)$,
then we can derive a posterior probability $P(A|B)$ after having seen the
event $B$, based on the likelihood of $B$ occurring when $A$ does or not hold.

The left member of the equation $P(A|B)$ is what we are trying to estimate,
this could be for instance \emph{probability of a document to be relevant
given that it contains a word X}.

$P(B|A)$ is on the other end the probability of observing the new evidence
given the initial hypothesis. This could mean finding the \emph{probability
of observing the word X given that a document is relevant}.

$P(A)$ is called the prior. For instance \emph{probability of any document to
be relevant}.

$P(B)$ is the marginal likelihood, for instance \emph{probability of observing
the word X in any document}.

\paragraph{Odds of an event}
It is often useful to talk about the odds of an event
$$O(A) = \frac{P(A)}{P(\neg A)} = \frac{P(A)}{1 - P(A)}$$

\subsection{Binary indipendence model}
\label{binary-ind}

In information retrieval we represent the relevant of a document
given an information need as a random variable $R$, Given a document $d$
and a query $q$ we want to estimate the probability $$P(R = 1 |q, d)$$

Given a dictionary of terms $T = \left\langle t_1, \dots, t_n \right\rangle $
we model each document as a binary vector $\vec{x} = (x_1, \dots, x_n)$
where $x_i$ is 1 if the $i$th term is in the document.

The goal is to calculate
$$O(R=1 | q, \vec{x}) = \frac{P(R=1|q, \vec{x})}{P(R=0|q, \vec{x})}$$

\paragraph{Simplifying the goal function}
Applying the Bayes rule leads to
\begin{equation}
    \begin{aligned}
        O(R=1 | q, \vec{x}) &= \frac{P(R=1|q, \vec{x})}{P(R=0|q, \vec{x})}
        = \frac{\frac{P(\vec{x}|R=1,q)P(R=1|q)}{P(q, \vec{x})}}{\frac{P(\vec{x}|R=0,q)P(R=0|q)}{P(q, \vec{x})}}\\
        &= \frac{P(\vec{x}| R = 1, q)}{P(\vec{x}| R = 0, q)}\cdot\frac{P(R=1|q)}{P(R=0|q)}
    \end{aligned}
\end{equation}

The left part of the result represent the odds of having that document
given that it is relevant and given that query. On the right part we
have the probability of the query producing relevant documents or not.

Assuming that the terms are independent, note that this is a strong assumptions,
we can write
$$O(R=1| q, \vec{x}) = \frac{P(R=1|q)}{P(R=0|q)} \prod_{i=1}^{n}
\frac{P(x_i|R=1, q)}{P(x_i|R=0, q)}$$
This product can be split according to the presence of terms in the document
$$O(R=1| q, \vec{x}) = \frac{P(R=1|q)}{P(R=0|q)} \prod_{i=1}^{n}
\frac{P(x_i = 1|R=1, q)}{P(x_i = 1|R=0, q)} \prod_{i=1}^{n}
\frac{P(x_i = 0|R=1, q)}{P(x_i = 0|R=0, q)}$$

To simplify the formula we introduce this values
$$p_i = P(x_i = 1|R=1, q), \;\; q_i = P(x_i = 1|R=0, q)$$
\begin{center}
    \begin{tabular}{c | c |c | c}
        & Document & $R=1$ & $R=0$\\
        \hline
        We observe the term & $x_i = 1$ & $p_i$ & $q_i$\\
        \hline
        We do not observe the term & $x_i = 0$ & $1- p_i$ & $1- q_i$
    \end{tabular}
\end{center}
Thus the previous equation becomes, where the first product iterates
over the words that appear in the document and the query,
and the second product goes for words that do not appear in the document,
but do on the queries.
$$O(R=1| q, \vec{x}) = \frac{P(R=1|q)}{P(R=0|q)} \prod_{x=1, q=1}
\frac{p_i}{q_i} \prod_{x=0, q=1}
\frac{1- p_i}{1- q_i}$$
This assumes that $p_i = q_i$ for words that
don't appear in a query, the assumption is motivated by the fact that a document
is relevant or not given a query, not by itself.

We now extend the right product to cover all the query terms,
formally
$$O(R=1| q, \vec{x}) = \frac{P(R=1|q)}{P(R=0|q)} \prod_{x=1, q=1}
\frac{\frac{p_i}{q_i}}{\frac{1 - p_i}{1-q_i}} \prod_{q=1}
\frac{1- p_i}{1- q_i}$$

We not observe that the quantity
$\frac{P(R=1|q)}{P(R=0|q)}$ represents the odds of the query and does not
depend on the document.
Also, the quantity $\prod_{q_i = 1} \frac{1- p_i}{1-q_i}$ depends on the terms
that are in the query, it's so query dependent but it does not change
for different documents.
This leads to a simplification of the previous formula, with some rewriting
$$O(R=1|q, \vec{x}) \approx \prod_{x_i=1, q_i = 1}
\frac{p_i(1-q_i)}{q_i(1-p_i)}$$

\paragraph{Retrieval Status Value}
After the last simplification we can calculate the Retrieval Status Value
for a document given a query as the quantity
$$\mathit{RSV}_{q,d} = \log \prod_{x_i=1, q_i = 1}
\frac{p_i(1-q_i)}{q_i(1-p_i)} = \sum_{x_i=1, q_i = 1} \log
\frac{p_i(1-q_i)}{q_i(1-p_i)}$$

\paragraph{Estimating probabilities}
We now just need to estimate $p_i$ and $q_i$ and we are ready to go.
If a dataset has informations about relevant documents given a query,
we can easily estimate them, given $R(t)$ and $N(t)$ occurrences of term $t$ in
relevant and non relevant documents
$$p_i = \frac{R(t_i) + 0.5}{R + 1.0}, \;\; q_i = \frac{N(t_i) + 0.5}{N + 1.0}$$

Of course we don't always have the relevant and non relevant documents
for queries, so we need some assumptions:
\begin{itemize}
    \item $p_i = q_i\;\mathit{if}\; t_i \notin q$, as discussed above
    \item $p_i = 0.5\;\mathit{if}\; t_i \in q$, we have the same probability of
    observing or not a term in a randomly-picked relevant document
    \item $q_i \approx \frac{ |\{ d | t_i \in d\}| }{ |D| }$,
    the set of non-relevant documents is approximated by the whole collection
    of documents $D$
\end{itemize}
By applying the previous assumptions we obtain
\begin{equation}
    \begin{aligned}
        \mathit{RSV}_{q,d} &= \sum_{x_i=1, q_i = 1} \log \frac{p_i(1-q_i)}{q_i(1-p_i)}\\
        &= \sum_{x_i=1, q_i = 1} \log \frac{(1-q_i)}{q_i}\\
        &= \sum_{x_i=1, q_i = 1} \log \frac{N - N_i + 0.5}{N_i + 0.5}
    \end{aligned}
\end{equation}
By having $n$ and $N$ as the number of docs with $t$ and the total
number of docs, respectively
$$\log \frac{(1-q_i)}{q_i} \approx \log \frac{N-n}{n} \approx \log \frac{N-n}{n} = \mathit{IDF}$$

This means that in practice, we are intersecting a document and a query terms,
summing the \emph{IDF} of the words in the intersection, of course, by
considering the initial assumptions.

\subsection{Non binary models}
The non binary models takes into consideration the frequency of the terms
and not simply a binary vector. They remove the binary assumption
of the model presented in \vref{binary-ind}.

\paragraph{Okapi BM25}
In this non binary model, the RSV is estimated as follows
$$\mathit{RSV}_{q,v} = \sum_{t \in q}\log \frac{N}{\mathit{df}_t}
\cdot \frac{(k+1)\mathit{tf}_{t,d}}{k((1-b) + b (\frac{L_d}{L_{\mathit{avg}}}))
+ \mathit{tf}_{t,d}}$$
$k$ regulates the term frequency, while $b$ scales the importance of
the document length.

\subsection{Probabilistic language modelling}
\label{problangmodel}
The goal is now to remove the independence assumption made in the model
at \vref{binary-ind}.

In order to accomplish this, we need a model that can compute the probability
of a sentence, i.e. a query, where the order of the words is important:
$$P(S) = P(w_1, \dots, w_n)$$
Another request is to have a generative model for sentences, so a model
that estimates the probability of a word, given the previous terms in the
sequence:
$$P(w_k | w_{k-1}, \dots, w_1)$$

The idea is to build a model on top of each document and calculate
the probability $P(q|M_d)$ that a query $q$ has been generated by the model
$M_d$.

\paragraph{Computing a sentence probability}
Given a sentence, for instance \emph{Mario plays a lot with Maria},
we need to compute the joint probability:
$$P(\mathit{Mario}, \mathit{plays}, \mathit{a}, \mathit{lot}, \mathit{with}, \mathit{Maria})$$
By applying the chain rule we can calculate the probability of the sequence
$$P(x_1, x_2, \dots, x_n) = P(x_1)P(x_2|x_1)P(x_3|x_1,x_2)\dots = \prod_i^nP(w_i|w_1, \dots, w_{i-1})$$

Calculating the quantity on real world documents could be impossible,
as we might not be able to count long sequences of words to calculate the probability.
An approximation to the sequence probability is given by restricting the sequence length:
$$P(w_1, \dots, w_n) \approx \prod_i^nP(w_i | w_{i-k}, \dots, w_{i-1})$$
$k$ is a parameter, it should be
small if the dataset is really small, or higher if there is enough data. For instance,
if we take $k=2$ we are estimating the sequence by bi-grams
$$P(w_1, \dots, w_n) \approx \prod_i^nP(w_i | w_{i-1})$$
Note that to compute the probability of a bi-gram, we just count occurrences of a given bi-gram
over all the possible ones.

\paragraph{Query likelihood model}
After building a model $M_d$ for each documents, following the idea presented on the previous paragraph,
we can estimate relevance of documents given a query.

So the probability that a query was generated by a document becomes:
$$P(q|M_d) = K_q\prod_iP(w_i|M_d)^{\mathit{tf}_{w_i, d}}\;\;\;
K_q = \frac{\mathit{Len}(K_q)!}{\mathit{tf}_{t_1, q}! \dots \mathit{tf}_{t_N, q}!}$$
The $K_q$ is query dependent, so we can ignore it, it would take into consideration
the query permutations.

We can also estimate the probability with this quantity
$$P(q|M_d) \approx \prod_i\frac{\mathit{tf}_{t_i, d}}{L_d}$$

Note that $\mathit{tf}_{t_i, d}$ can be changed to the probability of the
k-grams computed at the previous paragraph, it depends on how we choose to
estimate the sentence probability.

\paragraph{Smoothing}
Note that all zero and non frequent term probabilities are a problem,
for instance a long sequence could be interrupted by only one bi-gram not appearing in
the document, the solution
is to smooth the numbers, for instance with the \emph{Laplace smoothing} that adds
one to each bi-gram, the \emph{Bayesian smoothing} or the \emph{Linear interpolation} that
takes into consideration the global document model.

\subsection{Other models}
\paragraph{Kullback-Leibler divergence model}
In this model the relevance term is computed as the quantity
$$R(d, q) = \sum_i P(w_i|M_q)\log\frac{P(w_i|M_q)}{P(w_i|M_d)}$$
where $M_q$ is the model computed on the query.

\paragraph{Translation model}
The idea here is to use a generative model taking into consideration
the possible translation of a query term.


\section*{Exercises}

\begin{ex}
  Determine by direct calculation (i.e., without calculating the
  characteristic polynomial or the eigenvalues) which of the following
  matrices are positive definite, positive semidefinite, or neither.
  \begin{equation*}
    A = \begin{mymatrix}{rr}
      1 & 1 \\
      1 & 1 \\
    \end{mymatrix},\quad
    B = \begin{mymatrix}{rr}
      1  & -2 \\
      -2 &  5 \\
    \end{mymatrix},\quad
    C = \begin{mymatrix}{rr}
      2 & 3 \\
      1 & 3 \\
    \end{mymatrix},\quad
    D = \begin{mymatrix}{rrr}
      2 & 1 & 1 \\
      1 & 1 & 0 \\
      1 & 0 & 1 \\
    \end{mymatrix},\quad
    E = \begin{mymatrix}{rrr}
      1 & 0 &  0 \\
      0 & 2 &  0 \\
      0 & 0 & -1 \\
    \end{mymatrix}.
  \end{equation*}
  \begin{sol}
    (a) Positive semidefinite. (b) Positive definite. (c) Not
    symmetric (therefore neither). (d) Positive semidefinite. (e)
    Neither.
  \end{sol}
\end{ex}

\begin{ex}
  Calculate the eigenvalues of each symmetric matrix, then determine
  for each matrix whether it is positive definite, positive
  semidefinite, or neither.
  \begin{equation*}
    A = \begin{mymatrix}{rr}
      2 & 2 \\
      2 & 5 \\
    \end{mymatrix},\quad
    B = \begin{mymatrix}{rr}
      4  & -6 \\
      -6 &  9 \\
    \end{mymatrix},\quad
    C = \begin{mymatrix}{rrr}
      2 &  1 &  0 \\
      1 &  1 & -1 \\
      0 & -1 &  2 \\
    \end{mymatrix},\quad
    D = \begin{mymatrix}{rrr}
      3 & -4 &  2 \\
     -4 &  4 &  0 \\
      2 &  0 &  4 \\
    \end{mymatrix}.
  \end{equation*}
  is positive definite, positive semidefinite, or neither.
  \begin{sol}
    (a) Eigenvalues: $\set{1,6}$. Positive definite.
    (b) Eigenvalues: $\set{0,13}$. Positive semidefinite.
    (c) Eigenvalues: $\set{0,2,3}$. Positive semidefinite.
    (d) Eigenvalues: $\set{-1,4,8}$. Neither.
  \end{sol}
\end{ex}

\begin{ex}
  Which of the following formulas define an inner product on $\R^3$?
  Here, $\vect{u}=\mat{u_1,u_2,u_3}^T$ and
  $\vect{v}=\mat{v_1,v_2,v_3}^T$.
  \begin{enumerate}
  \item $\iprod{\vect{u},\vect{v}} = u_1v_1 + 2u_1v_2 + 2u_2v_1 + 3u_2v_2
    + 3u_3v_3$.
  \item $\iprod{\vect{u},\vect{v}} = u_1v_1 - u_1v_2 - u_2v_1 + 2u_2v_2
    + u_3v_3$.
  \item $\iprod{\vect{u},\vect{v}} = u_1v_1 - u_1v_2 - u_2v_1 + u_2v_2
    + 4u_3v_3$.
  \item $\iprod{\vect{u},\vect{v}} = u_1v_1 - u_1v_2 - u_2v_1 + 3u_2v_2
    - u_2v_3 - u_3v_2 + u_3v_3$.
  \end{enumerate}
  \begin{sol}
    (a) No (neither positive definite nor semidefinite).
    (b) Yes.
    (c) No (positive semidefinite but not definite).
    (d) Yes.
  \end{sol}
\end{ex}

\begin{ex}
  Use Descartes' rule of signs to determine which of the following
  matrices are positive definite and/or positive semidefinite.
  \begin{equation*}
    A = \begin{mymatrix}{cc}
      2 & 2 \\
      2 & 1 \\
    \end{mymatrix},
    \quad
    B = \begin{mymatrix}{ccc}
      2  & -1 & 0 \\
      -1 &  1 & 0 \\
      0  &  0 & 1 \\
    \end{mymatrix},
    \quad
    C = \begin{mymatrix}{cccc}
      2  & 1 & -1 &  1 \\
      1  & 1 &  0 &  0 \\
      -1 & 0 &  2 & -1 \\
      1  & 0 & -1 &  1 \\
    \end{mymatrix}.
  \end{equation*}
  \begin{sol}
    The characteristic polynomials are:
    \begin{equation*}
      \begin{array}{lcl}
        \det(A-\eigenvar I) &=& \eigenvar^2 - 3\eigenvar - 2, \\
        \det(B-\eigenvar I) &=& -\eigenvar^3 + 4\eigenvar^2 - 4\eigenvar + 1, \\
        \det(C-\eigenvar I) &=& \eigenvar^4 - 6\eigenvar^3 + 9\eigenvar^2 - 3\eigenvar + 0.
      \end{array}
    \end{equation*}
    For $A$, the coefficients are not weakly alternating, so $A$ is
    not positive semidefinite. For $B$, the coefficients are strongly
    alternating, so $B$ is positive definite. For $C$, the
    coefficients are weakly, but not strongly alternating, so $C$ is
    positive semidefinite, but not positive definite.
  \end{sol}
\end{ex}