Chapter 4: Model Fitting and Optimization
Techniques for fitting models to data, handling outliers with RANSAC, and using regularization methods like Total Variation for robust computer vision.
Chapter Overview
Computer vision often requires finding mathematical models that explain observed data—whether it's a line through detected edge points, a transformation between images, or a surface representing an object. But real-world data is messy: sensors are noisy, features are misdetected, and outliers corrupt our measurements.
What is this chapter about? We study model fitting: given noisy, imperfect observations, how do we find the underlying model? This is fundamental to nearly every computer vision pipeline.
Why does this matter? Without robust fitting techniques, a single bad measurement can completely ruin your results. Imagine fitting a line to detected lane markers—one misdetection shouldn't send your self-driving car off the road.
How the topics connect: We start with Least Squares—the classic approach that works great when data is clean. Then we learn RANSAC, which elegantly handles outliers by finding consensus among the data. Finally, Total Variation shows how regularization can guide solutions toward realistic, edge-preserving results.
Chapter Roadmap
Click any topic to jump in
Least Squares
Linear regression, normal equations, and residuals — fitting models to data by minimizing error.
Outlier robustness and regularization
RANSAC
Random sampling for robust fitting — handling outliers that break least squares.
Regularization
Total variation, ROF model, and edge preservation — adding priors to prevent overfitting.
We begin with the most fundamental fitting technique: least squares. When your data is mostly clean, this elegant approach gives you the mathematically optimal solution in closed form. Understanding least squares is essential because it forms the foundation for more advanced methods.
In this topic
Least Squares Criterion
The least squares criterion minimizes the sum of squared residuals—the vertical distances from data points to the fitted curve, squared and summed. Here:
- is the observed value at point
- is the model's prediction at that point
- The squaring penalizes large errors more heavily than small ones
This is the optimal solution when errors are normally distributed (Gaussian noise). However, it's sensitive to outliers because squaring amplifies large deviations. A single outlier can dramatically shift the fitted line.
The least squares objective minimizes the squared norm of the residual vector. Setting the gradient yields the normal equations. The quadratic form defines a paraboloid in parameter space with a unique minimum when is positive definite. Squaring penalizes large residuals disproportionately — a single outlier at distance contributes to the cost, giving it leverage proportional to on the solution.
Points (1,2), (2,3), (3,5) are fit with line y=x+1. Calculate the sum of squared errors.
Normal Equations
The normal equations provide a closed-form solution to the least squares problem:
- is the design matrix, where each row contains features for one data point (e.g., for linear regression)
- is the vector of parameters we're solving for (e.g., slope and intercept)
- is the vector of observed outputs (y values)
- is the transpose of
The solution is , which can be computed directly using matrix operations. This is efficient for small to medium datasets but can be numerically unstable for ill-conditioned problems.
The normal equations project the observation vector onto the column space of . Geometrically, is the point in closest to , and the residual is orthogonal to every column of — hence the name "normal" equations. The condition number means numerical errors are amplified quadratically; for ill-conditioned problems, the SVD-based pseudoinverse or QR decomposition is preferred.
For points (0,1), (1,3), write the design matrix A for fitting y=ax+b.
Residuals
Residuals are the differences between observed values () and predictions (). Analyzing residuals helps validate your model:
- Random scatter around zero: Good fit, model captures the pattern
- Curved pattern: Model is missing nonlinear structure
- Increasing spread: Heteroscedasticity—variance changes with
- Clustering: Possible outliers or model inadequacy
The sum of residuals in linear regression always equals zero, but the sum of squared residuals (RSS) measures overall fit quality.
The residual vector lies in the left null space of (orthogonal complement of the column space). For a correctly specified linear model with i.i.d. Gaussian noise , the residuals follow where is the hat matrix. The unbiased estimator uses degrees of freedom because the projection consumes of the original dimensions.
Fitted line predicts [10, 20, 30] but actual values are [12, 19, 32]. Compute residuals and RSS.
Theory Exercise
Problem:
Given 3 data points: (1, 2), (2, 4), (3, 5), fit a line y = ax + b using least squares. Calculate the optimal values of a and b.
Hints:
- Set up the sums: Σx, Σy, Σxy, Σx²
- Use the formulas: a = (nΣxy - ΣxΣy)/(nΣx² - (Σx)²)
- Then: b = (Σy - aΣx)/n