About Principal Component Analysis (PCA)

This simulation shows how Principal Component Analysis finds the directions of greatest spread in a two-dimensional point cloud. From the mean-centred data it builds the 2×2 covariance matrix, then solves the eigenproblem in closed form using λ = trace/2 ± √(trace²/4 − det). The eigenvectors define the principal axes (PC1 and PC2), and their eigenvalues λ₁ and λ₂ give the variance captured along each direction.

You choose a preset dataset (correlated cloud, clusters, circle, diagonal stripe or noise), set the point count n and the correlation ρ, then regenerate. Toggles draw the PC1 and PC2 arrows and the perpendicular projection lines onto PC1, while the lower panel histograms the projected scores. PCA underpins dimensionality reduction, data compression, noise filtering and feature extraction across statistics and machine learning.

Frequently Asked Questions

What is Principal Component Analysis?

PCA is a statistical technique that rotates the coordinate axes to align them with the directions of maximum variance in a dataset. The first principal component (PC1) points along the direction of greatest spread, and the second (PC2) is orthogonal to it. It is widely used to reduce dimensionality while keeping as much information as possible.

How does this simulator compute the principal components?

It mean-centres the points, then forms the 2×2 covariance matrix with entries s₁₁, s₁₂ and s₂₂ using a divisor of n−1. The eigenvalues come from λ = trace/2 ± √(trace²/4 − det), and the eigenvectors give the principal directions. This closed-form solution is exact for two-dimensional data.

What do the blue and red arrows mean?

The blue arrow is PC1, the direction capturing the most variance, and the red arrow is PC2, orthogonal to PC1 and capturing the remainder. Each arrow's length is scaled by the square root of its eigenvalue, so a longer arrow signals a larger spread of data along that axis.

What is explained variance and why does it matter?

Explained variance is each eigenvalue expressed as a percentage of the total, λᵢ / (λ₁ + λ₂). It tells you how much of the dataset's overall variability a component accounts for. If PC1 explains 95%, you can describe the data well using just one dimension, which is the whole point of dimensionality reduction.

What do the n, ρ and preset controls do?

The preset selects the underlying shape of the data, n sets how many points are generated (10 to 300), and ρ sets the target correlation between the two variables for the correlated preset (−0.99 to 0.99). Changing a slider regenerates the cloud, except in custom mode where you click the canvas to place points yourself.

Why are the eigenvectors always at right angles?

The covariance matrix is symmetric, and a fundamental result of linear algebra is that a symmetric matrix has orthogonal eigenvectors. PCA therefore produces an orthonormal set of axes, meaning the principal components are uncorrelated with one another and span the space at perfect right angles.

What are the faint lines connecting points to PC1?

Those are projection lines. Each shows the perpendicular drop from a data point onto the PC1 axis, marking where the point lands when reduced to a single dimension. PCA chooses PC1 precisely to minimise the total squared length of these projection residuals, equivalent to maximising the variance retained.

What does the histogram at the bottom show?

It plots the distribution of the PC1 scores, the one-dimensional coordinates of every point measured along the first principal component. This is what the data looks like after projection onto a single axis, illustrating how a 2D cloud is compressed into a 1D representation while preserving the dominant structure.

Is the calculation physically and mathematically accurate?

Yes. For two-dimensional data the eigenvalues and eigenvectors of the covariance matrix can be solved exactly with the quadratic formula used here, so the results are not approximations. The covariance uses the unbiased n−1 divisor, matching standard sample-statistics conventions found in textbooks and libraries.

Where is PCA used in the real world?

PCA appears in image and signal compression, gene-expression analysis, finance for identifying market factors, face recognition (eigenfaces) and as a preprocessing step before clustering or regression. By discarding low-variance components it removes noise and redundancy, making downstream models faster and often more robust.