Positive Semi-Definite Matrices and Covariance
Positive Semi-Definite Matrices and Covariance
Section titled “Positive Semi-Definite Matrices and Covariance”Positive Semi-Definite (PSD) matrices and covariance matrices play a central role in machine learning. They describe variance, correlation, and structure in data, forming the backbone of PCA, kernel methods, and Gaussian models.
Positive Semi-Definite (PSD) Matrices
Section titled “Positive Semi-Definite (PSD) Matrices”A symmetric matrix is positive semi-definite (PSD) if:
- If for all nonzero , the matrix is positive definite (PD).
- Eigenvalues of a PSD matrix are always non-negative.
::: info Explanation of PSD The quadratic form represents “energy” or variance along direction .
- PSD means the matrix never produces negative variance.
- In ML: covariance matrices and kernel matrices are PSD.
:::
Covariance Matrix
Section titled “Covariance Matrix”For a dataset (m samples, n features), the covariance matrix is:
where is the mean vector of features.
- Diagonal entries = variances of features.
- Off-diagonal entries = covariances between features.
Mini Example:
For data points :
- Feature 1 variance = variance of [1,2,3].
- Feature 2 variance = variance of [2,3,4].
- Covariance = how feature 1 and feature 2 vary together.
Properties of Covariance Matrices
Section titled “Properties of Covariance Matrices”- Symmetric.
- Positive semi-definite.
- Encodes feature relationships (correlation structure).
Applications in ML
Section titled “Applications in ML”- PCA: Eigen-decomposition of covariance matrix finds principal components.
- Gaussian Models: Multivariate normal distribution defined by mean vector and covariance matrix.
- Kernels: Kernel (Gram) matrices are PSD by construction.
Hands-on with Python and Rust
Section titled “Hands-on with Python and Rust”::: code-group
import numpy as np
# Dataset: 3 samples, 2 featuresX = np.array([[1, 2], [2, 3], [3, 4]])
# Covariance matrix (rows as observations, columns as features)cov_matrix = np.cov(X, rowvar=False)
# Check PSD via eigenvalueseigenvalues = np.linalg.eigvals(cov_matrix)
print("Covariance matrix:\n", cov_matrix)print("Eigenvalues (should be >= 0):", eigenvalues)use ndarray::{array, Array2};use ndarray_stats::CorrelationExt;use ndarray_linalg::Eig;
fn main() { // Dataset: 3 samples, 2 features let x: Array2<f64> = array![ [1.0, 2.0], [2.0, 3.0], [3.0, 4.0] ];
// Covariance matrix let cov = x.cov(0.0).unwrap();
// Eigenvalues to check PSD let eigenvalues = cov.eig().unwrap().0;
println!("Covariance matrix:\n{:?}", cov); println!("Eigenvalues (should be >= 0): {:?}", eigenvalues);}:::
Connection to ML
Section titled “Connection to ML”- PSD matrices guarantee valid variance/covariance structures.
- Covariance underlies dimensionality reduction (PCA), Gaussian models, and kernels.
- Understanding covariance helps diagnose multicollinearity in regression.
Next Steps
Section titled “Next Steps”Continue to Linear Transformations and Geometry.