# Statistics/Numerical Methods/Basic Linear Algebra and Gram-Schmidt Orthogonalization
## Introduction

Basically, all the sections found here can be also found in a linear algebra book. However, the Gram-Schmidt Orthogonalization is used in statistical algorithm and in the solution of statistical problems. Therefore, we briefly jump into the linear algebra theory which is necessary to understand Gram-Schmidt Orthogonalization.
The following subsections also contain examples. It is very important for further understanding that the concepts presented here are not only valid for typical vectors as tuple of real numbers, but also functions that can be considered vectors.

## Fields
### Definition

A set $R$ with two operations $+$ and $*$ on its elements is called a field (or short $(R,+,*)$), if the following conditions hold:
* For all $\alpha ,\beta \in R$ holds $\alpha +\beta \in R$
* For all $\alpha ,\beta \in R$ holds $\alpha +\beta =\beta +\alpha$ (commutativity)
* For all $\alpha ,\beta ,\gamma \in R$ holds $\alpha +(\beta +\gamma )=(\alpha +\beta )+\gamma$ (associativity)
* It exist a unique element $0$, called zero, such that for all $\alpha \in R$ holds $\alpha +0=\alpha$
* For all $\alpha \in R$ a unique element $-\alpha$, such that holds $\alpha +(-\alpha )=0$
* For all $\alpha ,\beta \in R$ holds $\alpha *\beta \in R$
* For all $\alpha ,\beta \in R$ holds $\alpha *\beta =\beta *\alpha$ (commutativity)
* For all $\alpha ,\beta ,\gamma \in R$ holds $\alpha *(\beta *\gamma )=(\alpha *\beta )*\gamma$ (associativity)
* It exist a unique element $1$, called one, such that for all $\alpha \in R$ holds $\alpha *1=\alpha$
* For all non-zero $\alpha \in R$ a unique element $\alpha ^{-1}$, such that holds $\alpha *\alpha ^{-1}=1$
* For all $\alpha ,\beta ,\gamma \in R$ holds $\alpha *(\beta +\gamma )=\alpha *\beta +\alpha *\gamma$ (distributivity)
The elements of $R$ are also called scalars.

### Examples

It can easily be proven that real numbers with the well known addition and multiplication $(IR,+,*)$ are a field. The same holds for complex numbers with the addition and multiplication. Actually, there are not many more sets with two operations which fulfill all of these conditions.
For statistics, only the real and complex numbers with the addition and multiplication are important.

## Vector spaces
### Definition

A set $V$ with two operations $+$ and $*$ on its elements is called a vector space over R, if the following conditions hold:
* For all $x,y\in V$ holds $x+y\in V$
* For all $x,y\in V$ holds $x+y=y+x$ (commutativity)
* For all $x,y,z\in V$ holds $x+(y+z)=(x+y)+z$ (associativity)
* It exist a unique element $\mathbb {O}$, called origin, such that for all $x\in V$ holds $x+\mathbb {O} =x$
* For all $x\in V$ exists a unique element $-v$, such that holds $x+(-x)=\mathbb {O}$
* For all $\alpha \in R$ and $x\in V$ holds $\alpha *x\in V$
* For all $\alpha ,\beta \in R$ and $x\in V$ holds $\alpha *(\beta *x)=(\alpha *\beta )*x$ (associativity)
* For all $x\in V$ and $1\in R$ holds $1*x=x$
* For all $\alpha \in R$ and for all $x,y\in V$holds $\alpha *(x+y)=\alpha *x+\alpha *y$ (distributivity wrt. vector addition)
* For all $\alpha ,\beta \in R$ and for all $x\in V$holds $(\alpha +\beta )*x=\alpha *x+\beta *x$ (distributivity wrt. scalar addition)
Note that we used the same symbols $+$ and $*$ for different operations in $R$ and $V$. The elements of $V$ are also called vectors.
Examples:
* The set $IR^{p}$ with the real-valued vectors $(x_{1},...,x_{p})$ with elementwise addition $x+y=(x_{1}+y_{1},...,x_{p}+y_{p})$ and the elementwise multiplication $\alpha *x=(\alpha x_{1},...,\alpha x_{p})$ is a vector space over $IR$.
* The set of polynomials of degree $p$, $P(x)=b_{0}+b_{1}x+b_{2}x^{2}+...+b_{p}x^{p}$, with usual addition and multiplication is a vector space over $IR$.

### Linear combinations

A vector $x$ can be written as a linear combination of vectors $x_{1},...x_{n}$, if
$x=\sum _{i=1}^{n}\alpha _{i}x_{i}$
with $\alpha _{i}\in R$.
Examples:
* $(1,2,3)$ is a linear combination of $(1,0,0),\,(0,1,0),\,(0,0,1)$ since $(1,2,3)=1*(1,0,0)+2*(0,1,0)+3*(0,0,1)$
* $1+2*x+3*x^{2}$ is a linear combination of $1+x+x^{2},\,x+x^{2},\,x^{2}$ since $1+2*x+3*x^{2}=1*(1+x+x^{2})+1*(x+x^{2})+1*(x^{2})$

### Basis of a vector space

A set of vectors $x_{1},...,x_{n}$ is called a basis of the vector space $V$, if
1. for each vector $x\in V$ exist scalars $\alpha _{1},...,\alpha _{n}\in R$ such that $x=\sum _{i}\alpha _{i}x_{i}$ 2. there is no subset of $\{x_{1},...,x_{n}\}$ such that 1. is fulfilled.
Note, that a vector space can have several bases.
Examples:
* Each vector $(\alpha _{1},\alpha _{2},\alpha _{3})\in IR^{3}$ can be written as $\alpha _{1}*(1,0,0)+\alpha _{2}*(0,1,0)+\alpha _{3}*(0,0,1)$. Therefore is $\{(1,0,0),(0,1,0),(0,0,1)\}$ a basis of $IR^{3}$.
* Each polynomial of degree $p$ can be written as linear combination of $\{1,x,x^{2},...,x^{p}\}$ and therefore forms a basis for this vector space.
Actually, for both examples we would have to prove condition 2., but it is clear that it holds.

### Dimension of a vector space

A dimension of a vector space is the number of vectors which are necessary for a basis. A vector space has infinitely many number of basis, but the dimension is uniquely determined. Note that the vector space may have a dimension of infinity, e.g. consider the space of continuous functions.
Examples:
* The dimension of $IR^{3}$ is three, the dimension of $IR^{p}$ is $p$ .
* The dimension of the polynomials of degree $p$ is $p+1$.

### Scalar products

A mapping $<.,.>:V\times V\rightarrow R$ is called a scalar product if the following holds for all $x,x_{1},x_{2},y,y_{1},y_{2}\in V$ and $\alpha _{1},\alpha _{2}\in R$ :
* $<\alpha _{1}x_{1}+\alpha _{2}x_{2},y>=\alpha _{1}<x_{1},y>+\alpha _{2}<x_{2},y>$
* $<x,\alpha _{1}y_{1}+\alpha _{2}y_{2}>=\alpha _{1}<x,y_{1}>+\alpha _{2}<x,y_{2}>$
* $<x,y>={\overline {<y,x>}}$ with ${\overline {\alpha +\imath \beta }}=\alpha -\imath \beta$
* $<x,x>\geq 0$ with $<x,x>=0\Leftrightarrow x=\mathbb {O}$
Examples:
* The typical scalar product in $IR^{p}$ is $<x,y>=\sum _{i}x_{i}y_{i}$.
* $<f,g>=\int _{a}^{b}f(x)*g(x)dx$ is a scalar product on the vector space of polynomials of degree $p$.

### Norm

A norm of a vector is a mapping $\|.\|:V\rightarrow R$, if holds
* $\|x\|\geq 0$ for all $x\in V$ and $\|x\|=0\Leftrightarrow x=\mathbb {O}$ (positive definiteness)
* $\|\alpha v\|=\mid \alpha \mid \|x\|$ for all $x\in V$ and all $\alpha \in R$
* $\|x+y\|\leq \|x\|+\|y\|$ for all $x,y\in V$ (triangle inequality)
Examples:
* The $L_{q}$ norm of a vector in $IR^{p}$ is defined as $\|x\|_{q}={\sqrt[{q}]{\sum _{i=1}^{p}x_{i}^{q}}}$.
* Each scalar product generates a norm by $\|x\|={\sqrt {<x,x>}}$, therefore $\|f\|={\sqrt {\int _{a}^{b}f^{2}(x)dx}}$ is a norm for the polynomials of degree $p$.

### Orthogonality

Two vectors $x$ and $y$ are orthogonal to each other if $<x,y>=0$. In $IR^{p}$ it holds that the cosine of the angle between two vectors can expressed as
$\cos(\angle (x,y))={\frac {<x,y>}{\|x\|\|y\|}}$.
If the angle between $x$ and $y$ is ninety degree (orthogonal) then the cosine is zero and it follows that $<x,y>=0$.
A set of vectors $x_{1},...,x_{p}$ is called orthonormal, if
$<x_{i},x_{j}>={\begin{cases}0&{\mbox{ if }}i\neq j\\1&{\mbox{ if }}i=j\end{cases}}$.
If we consider a basis $e_{1},...,e_{p}$ of a vector space then we would like to have a orthonormal basis. Why ?
Since we have a basis, each vector $x$ and $y$ can be expressed by $x=\alpha _{1}e_{1}+...+\alpha _{p}e_{p}$ and $y=\beta _{1}e_{1}+...+\beta _{p}e_{p}$. Therefore the scalar product of $x$ and $y$ reduces to
* $=<\alpha _{1}e_{1}+...+\alpha _{p}e_{p},\beta _{1}e_{1}+...+\beta _{p}e_{p}>\$
* $=\sum _{i=1}^{p}\sum _{j=1}^{p}\alpha _{i}\beta _{j}<e_{i},e_{j}>$
* $=\sum _{i=1}^{p}\alpha _{i}\beta _{i}<e_{i},e_{i}>$
* $=\alpha _{1}\beta _{1}+...+\alpha _{p}\beta _{p}.\$
Consequently, the computation of a scalar product is reduced to simple multiplication and addition if the coefficients are known. Remember that for our polynomials we would have to solve an integral!

## Gram-Schmidt orthogonalization
### Algorithm

The aim of the Gram-Schmidt orthogonalization is to find for a set of vectors $x_{1},...,x_{p}$ an equivalent set of orthonormal vectors $o_{1},...,o_{p}$ such that any vector which can be expressed as linear combination of $x_{1},...,x_{p}$ can also be expressed as linear combination of $o_{1},...,o_{p}$:
1. Set $b_{1}=x_{1}$ and $o_{1}=b_{1}/\|b_{1}\|$
2. For each $i>1$ set $b_{i}=x_{i}-\sum _{j=1}^{i-1}{\frac {<x_{i},b_{j}>}{<b_{j},b_{j}>}}b_{j}$ and $o_{i}=b_{i}/\|b_{i}\|$, in each step the vector $x_{i}$ is projected on $b_{j}$ and the result is subtracted from $x_{i}$.

### Example

Consider the polynomials of degree two in the interval$[-1,1]$ with the scalar product $<f,g>=\int _{-1}^{1}f(x)g(x)dx$ and the norm $\|f\|={\sqrt {<f,f>}}$. We know that $f_{1}(x)=1,f_{2}(x)=x$ and $f_{3}(x)=x^{2}$ are a basis for this vector space. Let us now construct an orthonormal basis:
Step 1a: $b_{1}(x)=f_{1}(x)=1$
Step 1b: $o_{1}(x)={\frac {b_{1}(x)}{\|b_{1}(x)\|}}={\frac {1}{\sqrt {<b_{1}(x),b_{1}(x)>}}}={\frac {1}{\sqrt {\int _{-1}^{1}1dx}}}={\frac {1}{\sqrt {2}}}$
Step 2a: $b_{2}(x)=f_{2}(x)-{\frac {<f_{2}(x),b_{1}(x)>}{<b_{1}(x),b_{1}(x)>}}b_{1}(x)=x-{\frac {\int _{-1}^{1}x\ 1dx}{2}}1=x-{\frac {0}{2}}1=x$
Step 2b: $o_{2}(x)={\frac {b_{2}(x)}{\|b_{2}(x)\|}}={\frac {x}{\sqrt {<b_{2}(x),b_{2}(x)>}}}={\frac {x}{\sqrt {\int _{-1}^{1}x^{2}dx}}}={\frac {x}{\sqrt {2/3}}}=x{\sqrt {3/2}}$
Step 3a: $b_{3}(x)=f_{3}(x)-{\frac {<f_{3}(x),b_{1}(x)>}{<b_{1}(x),b_{1}(x)>}}b_{1}(x)-{\frac {<f_{3}(x),b_{2}(x)>}{<b_{2}(x),b_{2}(x)>}}b_{2}(x)=x^{2}-{\frac {\int _{-1}^{1}x^{2}1\ dx}{2}}1-{\frac {\int _{-1}^{1}x^{2}x\ dx}{2/3}}x=x^{2}-{\frac {2/3}{2}}1-{\frac {0}{2/3}}x=x^{2}-1/3$
Step 3b: $o_{3}(x)={\frac {b_{3}(x)}{\|b_{3}(x)\|}}={\frac {x^{2}-1/3}{\sqrt {<b_{3}(x),b_{3}(x)>}}}={\frac {x^{2}-1/3}{\sqrt {\int _{-1}^{1}(x^{2}-1/3)^{2}dx}}}={\frac {x^{2}-1/3}{\sqrt {\int _{-1}^{1}x^{4}-2/3x^{2}+1/9\ dx}}}={\frac {x^{2}-1/3}{\sqrt {8/45}}}={\sqrt {\frac {5}{8}}}(3x^{2}-1)$
It can be proven that $1/{\sqrt {2}},x{\sqrt {3/2}}$ and ${\sqrt {\frac {5}{8}}}(3x^{2}-1)$ form a orthonormal basis with the above scalarproduct and norm.

### Numerical instability

Consider the vectors $x_{1}=(1,\epsilon ,0,0),x_{2}=(1,0,\epsilon ,0)$ and $x_{3}=(1,0,0,\epsilon )$. Assume that $\epsilon$ is so small that computing $1+\epsilon =1$ holds on a computer (see http://en.wikipedia.org/wiki/Machine_epsilon). Let compute a orthonormal basis for this vectors in $IR^{4}$ with the standard scalar product $<x,y>=x_{1}y_{1}+x_{2}y_{2}+x_{3}y_{3}+x_{4}y_{4}$ and the norm $\|x\|={\sqrt {x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2}}}$.
Step 1a. $b_{1}=x_{1}=(1,\epsilon ,0,0)$
Step 1b. $o_{1}={\frac {b_{1}}{\|b_{1}\|}}={\frac {b_{1}}{\sqrt {1+\epsilon ^{2}}}}=b_{1}$ with $1+\epsilon ^{2}=1$
Step 2a. $b_{2}=x_{2}-{\frac {<x_{2},b_{1}>}{<b_{1},b_{1}>}}b_{1}=(1,0,\epsilon ,0)-{\frac {1}{1+\epsilon ^{2}}}(1,\epsilon ,0,0)=(0,-\epsilon ,\epsilon ,0)$
Step 2b. $o_{2}={\frac {b_{2}}{\|b_{2}\|}}={\frac {b_{2}}{\sqrt {2\epsilon ^{2}}}}=(0,-{\frac {1}{\sqrt {2}}},{\frac {1}{\sqrt {2}}},0)$
Step 3a. $b_{3}=x_{3}-{\frac {<x_{3},b_{1}>}{<b_{1},b_{1}>}}b_{1}-{\frac {<x_{3},b_{2}>}{<b_{2},b_{2}>}}b_{2}=(1,0,0,\epsilon )-{\frac {1}{1+\epsilon ^{2}}}(1,\epsilon ,0,0)-{\frac {0}{2\epsilon ^{2}}}(0,-\epsilon ,\epsilon ,0)=(0,-\epsilon ,0,\epsilon )$
Step 3b. $o_{3}={\frac {b_{3}}{\|b_{3}\|}}={\frac {b_{3}}{\sqrt {2\epsilon ^{2}}}}=(0,-{\frac {1}{\sqrt {2}}},0,{\frac {1}{\sqrt {2}}})$
It obvious that for the vectors
- $o_{1}=(1,\epsilon ,0,0)\$
- $o_{2}=(0,-{\frac {1}{\sqrt {2}}},{\frac {1}{\sqrt {2}}},0)$
- $o_{3}=(0,-{\frac {1}{\sqrt {2}}},0,{\frac {1}{\sqrt {2}}})$
the scalarproduct $<o_{2},o_{3}>=1/2\neq 0$. All other pairs are also not zero, but they are multiplied with $\epsilon$ such that we get a result near zero.

### Modified Gram-Schmidt

To solve the problem a modified Gram-Schmidt algorithm is used:
* Set $b_{i}=x_{i}$ for all $i$
* for each $i$ from $1$ to $n$ compute
** $o_{i}={\frac {b_{i}}{\|b_{i}\|}}$
** for each $j$ from $i+1$ to $n$ compute $b_{j}=b_{j}-<b_{j},o_{i}>o_{i}\$
The difference is that we compute first our new $b_{i}$ and subtract it from all other $b_{j}$. We apply the wrongly computed vector to all vectors instead of computing each $b_{i}$ separately.

### Example (recomputed)

Step 1. $b_{1}=(1,\epsilon ,0,0)$, $b_{2}=(1,0,\epsilon ,0)$, $b_{3}=(1,0,0,\epsilon )$
Step 2a. $o_{1}={\frac {b_{1}}{\|b_{1}\|}}={\frac {b_{1}}{\sqrt {1+\epsilon ^{2}}}}=b_{1}=(1,\epsilon ,0,0)$ with $1+\epsilon ^{2}=1$
Step 2b. $b_{2}=b_{2}-<b_{2},o_{1}>o_{1}=(1,0,\epsilon ,0)-(1,\epsilon ,0,0)=(0,-\epsilon ,\epsilon ,0)\$
Step 2c. $b_{3}=b_{3}-<b_{3},o_{1}>o_{1}=(1,0,0,\epsilon )-(1,\epsilon ,0,0)=(0,-\epsilon ,0,\epsilon )\$
Step 3a. $o_{2}={\frac {b_{2}}{\|b_{2}\|}}={\frac {b_{2}}{\sqrt {2\epsilon ^{2}}}}=(0,-{\frac {1}{\sqrt {2}}},{\frac {1}{\sqrt {2}}},0)$
Step 3b. $b_{3}=b_{3}-<b_{3},o_{2}>o_{2}=(0,-\epsilon ,0,\epsilon )-{\frac {\epsilon }{\sqrt {2}}}(0,-{\frac {1}{\sqrt {2}}},{\frac {1}{\sqrt {2}}},0)=(0,-\epsilon /2,-\epsilon /2,\epsilon )$
Step 4a. $o_{3}={\frac {b_{3}}{\|b_{3}\|}}={\frac {b_{3}}{\sqrt {3/2\epsilon ^{2}}}}=(0,-{\frac {1}{\sqrt {6}}},-{\frac {1}{\sqrt {6}}},{\frac {2}{\sqrt {6}}})$
We can easily verify that $<o_{2},o_{3}>=0$.

## Application
### Exploratory Project Pursuit

In the analysis of high-dimensional data we usually analyze projections of the data. The approach results from the Theorem of Cramer-Wold that states that the multidimensional distribution is fixed if we know all one-dimensional projections. Another theorem states that most (one-dimensional) projections of multivariate data are looking normal, even if the multivariate distribution of the data is highly non-normal.
Therefore in Exploratory Projection Pursuit we judge the interestingness of a projection by comparison with a (standard) normal distribution. If we assume that the one-dimensional data $x$ are standard normal distributed then after the transformation $z=2\Phi ^{-1}(x)-1$ with $\Phi (x)$ the cumulative distribution function of the standard normal distribution then $z$ is uniformly distributed in the interval $[-1;1]$.
Thus the interesting can measured by $\int _{-1}^{1}(f(z)-1/2)^{2}dx$ with $f(z)$ a density estimated from the data. If the density $f(z)$ is equal to $1/2$ in the interval $[-1;1]$ then the integral becomes zero and we have found that our projected data are normally distributed. An value larger than zero indicates a deviation from the normal distribution of the projected data and hopefully an interesting distribution.

### Expansion with orthonormal polynomials

Let $L_{i}(z)$ a set of orthonormal polynomials with the scalar product $<f,g>=\int _{-1}^{1}f(z)g(z)dz$ and the norm $\|f\|={\sqrt {<f,f>}}$. What can we derive about a densities $f(z)$ in the interval $[-1;1]$ ?
If $f(z)=\sum _{i=0}^{I}a_{i}L_{i}(z)$ for some maximal degree $I$ then it holds
$\int _{-1}^{1}f(z)L_{j}(z)dz=\int _{-1}^{1}\sum _{i=0}^{I}a_{i}L_{i}(z)L_{j}(z)dz=a_{j}\int _{-1}^{1}L_{j}(z)L_{j}(z)dz=a_{j}$
We can also write $\int _{-1}^{1}f(z)L_{j}(z)dz=E(L_{j}(z))$ or empirically we get an estimator ${\hat {a}}_{j}={\frac {1}{n}}\sum _{k=1}^{n}L_{j}(z_{k})$.
We describe the term $1/2=\sum _{i=1}^{I}b_{i}L_{i}(z)$ and get for our integral
$\int _{-1}^{1}(f(z)-1/2)^{2}dz=\int _{-1}^{1}\left(\sum _{i=0}^{I}(a_{i}-b_{i})L_{i}(z)\right)^{2}dz=\sum _{i,j=0}^{I}\int _{-1}^{1}(a_{i}-b_{i})(a_{j}-b_{j})L_{i}(z)L_{j}(z)dz=\sum _{i=0}^{I}(a_{i}-b_{i})^{2}.$
So using a orthonormal function set allows us to reduce the integral to a summation of coefficient which can be estimated from the data by plugging ${\hat {a}}_{j}$ in the formula above. The coefficients $b_{i}$ can be precomputed in advance.

### Normalized Legendre polynomials

The only problem left is to find the set of orthonormal polynomials $L_{i}(z)$ upto degree $I$. We know that $1,x,x^{2},...,x^{I}$ form a basis for this space. We have to apply the Gram-Schmidt orthogonalization to find the orthonormal polynomials. This has been started in the first example.
The resulting polynomials are called normalized Legendre polynomials. Up to a scaling factor the normalized Legendre polynomials are identical to Legendre polynomials. The Legendre polynomials have a recursive expression of the form
$L_{i}(z)={\frac {(2i-1)L_{i-1}(z)-(i-1)L_{i-2}(z)}{i}}$
So computing our integral reduces to computing $L_{0}(z_{k})$ and $L_{1}(z_{k})$ and using the recursive relationship to compute the ${\hat {a}}_{j}$'s. Please note that the recursion can be numerically unstable!

## References

* Halmos, P.R. (1974). Finite-Dimensional Vector Spaces, Springer: New York