Linear Algebra·Unidad 4

LA-04 — Systems, Gaussian elimination, rank

Resolver $A\mathbf{x}=\mathbf{b}$ es el esqueleto de la regresión lineal (las normal equations son un sistema). El rank te dice si tus features son redundantes o si un modelo tiene solución única. Los cuatro subespacios fundamentales (column space, null space) explican cuándo un problema de ML tiene solución y cuántas.

Why this matters for ML

Resolver Ax=bA\mathbf{x}=\mathbf{b} es el esqueleto de la regresión lineal (las normal equations son un sistema). El rank te dice si tus features son redundantes o si un modelo tiene solución única. Los cuatro subespacios fundamentales (column space, null space) explican cuándo un problema de ML tiene solución y cuántas.

Concepts covered

  • Sistema lineal Ax=bA\mathbf{x}=\mathbf{b}
  • Gaussian elimination, row echelon form (REF) y reduced (RREF)
  • Pivots
  • Rank
  • Column space y null space (kernel)
  • Existencia y unicidad de soluciones

Intuition first

🎬 3Blue1Brown E.o.L.A. cap. 7 ("Inverse matrices, column space and null space"). Piensa en Ax=bA\mathbf{x}=\mathbf{b} como "¿qué input x\mathbf{x} aterriza en b\mathbf{b} tras la transformación AA?".

Theory & key results

Sistema: mm ecuaciones, nn incógnitas ↔ Ax=bA\mathbf{x}=\mathbf{b} con ARm×nA\in\mathbb{R}^{m\times n}.

Gaussian elimination: con tres row operations (intercambiar filas, escalar una fila, sumar múltiplo de una fila a otra) llevas AA a forma triangular (REF) y luego a RREF. No cambian el conjunto solución.

Pivots: las primeras entradas no nulas de cada fila en REF. El número de pivots = rank.

Rank rr: número de filas (o columnas) linealmente independientes = dimensión del column space. Intuición ML: cuántas "direcciones de información" reales hay.

Column space C(A)C(A): span de las columnas de AA = todos los b\mathbf{b} alcanzables. Ax=bA\mathbf{x}=\mathbf{b} tiene solución ⟺ bC(A)\mathbf{b}\in C(A).

Null space N(A)N(A): todos los x\mathbf{x} con Ax=0A\mathbf{x}=\mathbf{0}. Su dimensión es nrn-r (rank–nullity theorem). Si N(A){0}N(A)\neq{\mathbf{0}}, hay infinitas soluciones (o ninguna).

Existencia y unicidad (para AA cuadrada n×nn\times n):

  • r=nr=n (full rank): solución única para todo b\mathbf{b}; AA invertible.
  • r<nr<n: null space no trivial → o infinitas soluciones o ninguna, según b\mathbf{b}.

Worked example

Resolver {x+2y=53x+4y=6,A=[1234].\begin{cases} x + 2y = 5\ 3x + 4y = 6 \end{cases}, \quad A=\begin{bmatrix}1&2\3&4\end{bmatrix}. Fila2 ← Fila2 − 3·Fila1: [1202]\begin{bmatrix}1&2\0&-2\end{bmatrix}, lado derecho (5,9)(5, -9). Dos pivots → rank 2 → solución única. Back-substitution: 2y=9y=4.5-2y=-9\Rightarrow y=4.5; x=52(4.5)=4x=5-2(4.5)=-4. Solución (4,4.5)(-4, 4.5).

Notebook exercises (by hand)

  1. Lleva a RREF [112224]\begin{bmatrix}1&1&2\2&2&4\end{bmatrix} y da su rank. ¿Qué notas de la fila 2?
  2. Para AA del ej. anterior, describe C(A)C(A) y N(A)N(A).
  3. ¿Cuántas soluciones tiene Ax=bA\mathbf{x}=\mathbf{b} si AA es 3×33\times3 con rank 2? Depende de b\mathbf{b}: explica ambos casos.
  4. Verifica el rank–nullity theorem (r+dimN(A)=nr + \dim N(A) = n) en un ejemplo 3×33\times3 de rank 2.
  5. Resuelve por eliminación: x+y+z=6, 2y+5z=4, 2x+5yz=27x+y+z=6,\ 2y+5z=-4,\ 2x+5y-z=27.
  6. Si dos feature columns de tu dataset son idénticas, ¿qué le pasa al rank y por qué eso complica ajustar un modelo lineal?

Python lab

import numpy as np
from numpy.linalg import matrix_rank, solve

A = np.array([[1.,2.],[3.,4.]]); b = np.array([5.,6.])
print("rank:", matrix_rank(A))       # 2 -> full rank
print("solución:", solve(A, b))      # [-4. , 4.5]

# sistema singular (columnas dependientes)
S = np.array([[1.,1.,2.],[2.,2.,4.],[0.,1.,1.]])
print("rank singular:", matrix_rank(S))   # < 3

# null space vía SVD (pequeño adelanto de LA-09)
U, s, Vt = np.linalg.svd(S)
null = Vt[np.isclose(s, 0)].T if np.any(np.isclose(s,0)) else "trivial"
print("null space:\n", null)

Implementa tu propia Gaussian elimination sobre una matriz aumentada y compárala con np.linalg.solve — es el mejor ejercicio del módulo.

Examen final 📝

Intenta cada nivel antes de abrir las soluciones.

🟡 Medio

  1. Resuelve por eliminación: x+2y=4, 3xy=5x+2y=4,\ 3x-y=5.
  2. Da el rank de [123246111]\begin{bmatrix}1&2&3\2&4&6\1&1&1\end{bmatrix} llevándola a REF.

🟠 Medio-difícil

  1. Para A=[121242]A=\begin{bmatrix}1&2&1\2&4&2\end{bmatrix}: describe C(A)C(A) (column space) y encuentra una base de N(A)N(A) (null space).
  2. Un sistema Ax=bA\mathbf x=\mathbf b con AA de 3×33\times3 tiene rank(A)=2\text{rank}(A)=2. Da un b\mathbf b para el que haya infinitas soluciones y explica cuándo no habría ninguna.

🔴 Difícil

  1. Demuestra el rank–nullity theorem en un caso concreto 3×33\times3 de rank 2 (construye la matriz, calcula rank y dimN(A)\dim N(A), verifica r+dimN(A)=nr+\dim N(A)=n) y explícalo conceptualmente.
  2. En regresión lineal, w^=(XX)1Xy\hat{\mathbf w}=(X^\top X)^{-1}X^\top\mathbf y requiere que XXX^\top X sea invertible. Muestra que rank(XX)=rank(X)\text{rank}(X^\top X)=\text{rank}(X) y concluye qué condición sobre las feature columns garantiza solución única.
✅ Soluciones
  1. De la 2ª: y=3x5y=3x-5. Sustituye: x+2(3x5)=47x=14x=2, y=1x+2(3x-5)=4\Rightarrow 7x=14\Rightarrow x=2,\ y=1.
  2. Fila2−2·Fila1 → fila nula; Fila3−Fila1=(0,1,2)(0,-1,-2). Quedan 2 pivots ⇒ rank 2.
  3. La 2ª fila es 2× la 1ª: C(A)=span{(1,2)}C(A)=\text{span}{(1,2)^\top} (una recta en R2\mathbb{R}^2, dim 1). N(A)N(A): x1+2x2+x3=0x_1+2x_2+x_3=0 ⇒ base p. ej. {(2,1,0),(1,0,1)}{(-2,1,0),(-1,0,1)} (dim 2). Check rank-nullity: 1+2=3=n1+2=3=n. ✓
  4. Con rank 2 en 3×33\times3, dimN(A)=1\dim N(A)=1. Si bC(A)\mathbf b\in C(A) (p. ej. una columna de AA), hay infinitas soluciones (solución particular + todo el null space). Si bC(A)\mathbf b\notin C(A), ninguna.
  5. Ej.: A=[101011112]A=\begin{bmatrix}1&0&1\0&1&1\1&1&2\end{bmatrix} (fila3=fila1+fila2). rank 2, N(A)=span{(1,1,1)}N(A)=\text{span}{(1,1,-1)} dim 1. 2+1=3=n2+1=3=n. Conceptual: cada dimensión de input o "sobrevive" (contribuye al rank/output) o "colapsa" (cae en el null space); la suma es la dimensión total del input.
  6. XXX^\top X y XX tienen el mismo null space (Xv=0XXv=0X\mathbf v=0\Leftrightarrow X^\top X\mathbf v=0, porque vXXv=Xv2\mathbf v^\top X^\top X\mathbf v=|X\mathbf v|^2), luego misma nullity y mismo rank. XXX^\top X (n×nn\times n) es invertible ⟺ rank nn ⟺ las nn feature columns son linealmente independientes (sin colinealidad).