Quant Memo
Advanced

RANSAC and Robust Model Fitting

RANSAC fits a model by repeatedly trying small random subsets of the data and keeping whichever fit most of the remaining points agree with, letting it ignore outliers that would otherwise distort a standard fit.

Ordinary least-squares regression fits a line by minimizing the total squared distance to every data point, which means a single extreme outlier — a bad tick, a data entry error, a one-off market glitch — can pull the whole fitted line noticeably off course, since squaring the error makes large deviations count disproportionately. When a dataset is expected to contain a meaningful fraction of such outliers, a fit that treats every point equally isn't reliable.

RANSAC (Random Sample Consensus) works around this differently: it repeatedly picks a small random subset of points just big enough to define a candidate model (two points for a line, for example), fits that candidate, and then counts how many of the remaining points fall close enough to it to be called "inliers." After many random trials, it keeps whichever candidate model attracted the largest consensus of inliers, and refits the final model using only that inlier set — so a handful of outliers can never outvote the majority of well-behaved points, because they simply won't cluster near any one candidate line.

RANSAC fits a model by trying many random minimal subsets of the data, keeping whichever candidate the most other points agree with, and discarding the rest as outliers — making it robust to contamination that would derail an ordinary least-squares fit.

Worked example. A dataset of 100 (price, volume) pairs is mostly a clean linear relationship, but 15 points are corrupted by a bad data feed and scattered randomly. An ordinary least-squares fit is visibly tilted by those 15 points. Running RANSAC — trying, say, 500 random pairs of points, fitting a line through each, and counting inliers within a small tolerance — finds one candidate line that roughly 83 of the 100 points agree with, correctly identifying the 15 corrupted points as outliers and producing a final fit essentially unaffected by them.

Practice in interviews

Further reading

  • Fischler & Bolles, Random Sample Consensus (1981)
ShareTwitterLinkedIn