RecSys & Search

Matrix Factorization & ALS

Decomposing high-dimensional sparse user-item interaction matrices into low-rank dense embedding vectors.

🟡 intermediate5 min readrecsys
Matrix Factorization (MF) is the foundational collaborative filtering algorithm in recommendation systems. It factorizes a sparse user-item rating matrix R (size U × I) into two low-rank dense matrices: User Embeddings P (U × k) and Item Embeddings Q (I × k). Predicted user rating for item i is computed via dot product: R̂_ui = p_u · q_i. Optimization techniques include Alternating Least Squares (ALS - ideal for parallel implicit feedback data) and Stochastic Gradient Descent (SGD - with L2 regularization).

Low-Rank Matrix Decomposition

Given sparse interaction matrix $R \in \mathbb{R}^{U \times I}$ where most entries are un-observed:

$$R \approx P \cdot Q^T$$

  Sparse Rating Matrix R (U × I)               User Embeddings P (U × k)    Item Embeddings Q^T (k × I)
  ┌───┬───┬───┬───┐                            ┌───┬───┐                    ┌───┬───┬───┬───┐
  │ 5 │ ? │ 1 │ ? │                            │.1 │.8 │                    │.4 │.2 │.9 │.1 │
  ├───┼───┼───┼───┤            ≈               ├───┼───┤         ×          ├───┼───┼───┼───┤
  │ ? │ 4 │ ? │ 2 │                            │.9 │.3 │                    │.7 │.6 │.1 │.8 │
  └───┴───┴───┴───┘                            └───┴───┘                    └───┴───┴───┴───┘

Predicted Rating with Biases:

$$\hat{R}_{ui} = \mu + b_u + b_i + p_u^T q_i$$

Alternating Least Squares (ALS) vs SGD

Objective Loss with L2 Regularization $\lambda$:

$$\mathcal{L} = \sum_{(u,i) \in \text{Observed}} \left( R_{ui} - (\mu + b_u + b_i + p_u^T q_i) \right)^2 + \lambda \left( |p_u|_2^2 + |q_i|_2^2 + b_u^2 + b_i^2 \right)$$

Because $p_u^T q_i$ multiplies two unknown matrices, the loss is non-convex jointly.

ALS Optimization Loop:

  1. Fix $Q$: Loss becomes strictly quadratic in $P$. Solve for each user $p_u$ in parallel via closed-form linear algebra:

$$p_u = \left( Q^T Q + \lambda I \right)^{-1} Q^T R_{u,*}$$

  1. Fix $P$: Loss becomes strictly quadratic in $Q$. Solve for each item $q_i$ in parallel:

$$q_i = \left( P^T P + \lambda I \right)^{-1} P^T R_{*,i}$$

  1. Repeat until convergence!

Say this out loud

"Matrix Factorization decomposes sparse user-item interaction matrices R into dense low-rank user embeddings P and item embeddings Q. Preference predictions are computed via dot product R̂_ui = μ + b_u + b_i + p_u^T q_i. Alternating Least Squares (ALS) optimizes MF efficiently on distributed Spark clusters by alternating between solving P and Q in closed-form."

Follow-ups to expect

Check yourself

Question 1 of 3

How does Matrix Factorization predict user u's preference rating for an un-observed item i?

More in RecSys & Search

See all →
Collaborative Filtering5 minThe Cold Start Problem4 minTwo-Stage: Retrieval then Ranking5 min