Data analysis: PCA and Fourier

Principal component analysis on the iris data, and sampling, aliasing and reconstruction with the discrete Fourier transform.

Two workhorses of data analysis, from the exercises in Theoretical Neuroscience. Everything on this page runs in your browser; nothing to install. Change a slider while a simulation is running and it reacts immediately.

Principal component analysis

Fisher’s iris data: 150 flowers, four measurements each. PCA finds the directions of largest variance, the eigenvectors of the covariance matrix.

\[C = \frac{1}{n} X^{\mathsf T} X, \qquad C\, e_k = \lambda_k e_k\]

  • \(X\): the data matrix, 150 rows (flowers) and 4 columns (sepal length, sepal width, petal length, petal width, in cm), with the column means removed. With standardisation on, each column is also divided by its standard deviation.
  • \(C\): the \(4 \times 4\) covariance matrix.
  • \(e_k\): the \(k\)-th principal component, a direction in the four-dimensional measurement space; \(\lambda_k\) its eigenvalue, the variance of the data along it.
  • Explained variance: \(\lambda_k / \sum_j \lambda_j\).

What you see. Left: each flower as a dot in the chosen coordinates. With species colouring on, blue is setosa, orange versicolor, green virginica; off, every dot is grey, which is what PCA itself sees, since it never uses the labels. When both axes are raw measurements, the first two principal components are drawn as lines through the origin, PC 1 in red and PC 2 in purple, each scaled to two standard deviations of the data along it. Right, top: the share of the total variance carried by each component, as blue bars. Right, bottom: the loadings, how much of each measurement goes into each component.

Try: switch standardisation off and PC 1 is dominated by petal length, simply because it has the largest spread in centimetres. Look at the raw-variable planes to see where PC 1 points. From the dimensionality-reduction exercise in Theoretical Neuroscience.

Sampling and the discrete Fourier transform

A signal made of two sine waves is sampled at \(N\) points spaced \(\Delta x\) apart over an interval of length \(T\). The discrete Fourier transform (DFT) decomposes the samples into frequencies \(f_m = m / T\):

\[y(x) = a_1 \sin(2\pi f_1 x) + a_2 \sin(2\pi f_2 x), \qquad Y_m = \sum_{n=0}^{N-1} y_n\, e^{-2\pi i\, m n / N}\]

  • \(f_1, f_2\): frequencies of the two components in cycles per unit length; \(a_1, a_2\) their amplitudes.
  • \(\Delta x\): sampling interval. \(T\): interval length, so \(N = T / \Delta x\).
  • \(|Y_m| \cdot 2/N\): the amplitude the DFT assigns to frequency \(f_m = m/T\).
  • Nyquist frequency \(f_{\text{Ny}} = 1 / (2 \Delta x)\): the highest frequency the samples can represent. Anything above it is aliased to a lower frequency.
  • \(K\): how many of the strongest DFT components are used to rebuild the signal.

What you see. Top: the true continuous signal in grey, its samples as blue dots, and in orange the signal rebuilt from the \(K\) strongest DFT components. Bottom: the amplitude spectrum, one stem per DFT frequency \(f_m = m/T\), up to the Nyquist frequency marked by the red dashed line. Stems used in the reconstruction are orange, the rest blue. The green dashed lines mark where the two true frequencies \(f_1\) and \(f_2\) land in the spectrum; when a frequency lies above Nyquist, its green line sits at the aliased frequency instead.

Try: set \(f_2\) above the Nyquist frequency and watch its energy appear at the wrong place. Make \(T\) a non-integer multiple of the period and the peak smears over neighbouring frequencies (leakage). With \(K = 2\) and well-resolved peaks, the orange reconstruction sits exactly on the grey signal. From the Fourier exercises in Theoretical Neuroscience.