Practical RANSAC investigation lesson. Can you defend a fitted line when some observations are unreliable? This companion turns RANSAC into a sequence of small, reproducible tests: compare competing hypotheses, measure threshold sensitivity, calculate a sampling budget and expose a convincing-looking failure. Start with the introductory guide to RANSAC and consensus sets if the mechanism is new. Here, the goal is an investigation notebook with data, calculations and defensible limits. The datasets below are constructed classroom examples, not measurements or claims about real-world performance.
Quick navigation: Evidence checks · Final takeaway · Why this mathematics matters · How the mechanism works · Worked example and core equations · Four deeper ideas · Twelve practical investigations · Limits and misconceptions · A learning route for students · Guidance for parents and educators · Frequently asked questions · Useful next reading and sources
Why this mathematics matters
Choose a route: for a first paper-and-pencil session, complete the worked example and investigations 1–4. For a spreadsheet or coding project, add investigations 6–10. Investigations 5, 11 and 12 are extension tasks about geometry and responsible interpretation. You need coordinates, straight-line equations and absolute values for the first route; logarithms and least squares can come later.
Prepare columns for point ID, x, y, prediction, signed residual, absolute residual and inlier decision. Keep a separate trial log for the sampled IDs, candidate equation, consensus size and rejection reason. Your final submission should show one successful fit, one sensitivity test and one failure that more random trials cannot repair.
Source boundary: Fischler and Bolles’ original paper introduces the sample-and-consensus idea. The official RANSACRegressor documentation specifies that implementation’s parameters. The classroom conventions and worked numbers below are stated explicitly so they can be checked independently of a library.
How the mechanism works
Freeze the rules before experimenting. For the line activities use y=mx+b, samples of two distinct point IDs with unequal x, and vertical absolute residual r=|y−(mx+b)|. An inlier satisfies r≤τ, including equality. Rank candidates by inlier count, then by the sum of squared residuals on their inliers; keep the earlier trial on an exact tie. After choosing the winner, fit ordinary least squares to its consensus once. Record that original consensus and recompute residuals after refitting without silently relabelling the training set. These are our teaching rules, not a claim that every implementation uses them.
For a clean-sample probability q per independent trial, the chance of at least one clean sample in N trials is 1−(1−q)^N. Thus N=ceil(log(1−p)/log(1−q)) for 0<q<1 and 0<p<1. The familiar approximation q=w^s uses inlier fraction w and sample size s. For a finite dataset with I inliers among M points, choosing s distinct IDs uniformly gives q=C(I,s)/C(M,s). Independent trials may repeat an earlier subset. A clean sample still need not produce an accurate or geometrically valid model.
Notebook contract: write the dataset version, model family, residual and units, threshold, sample rule, tie-breaker, refit rule and stopping rule before running. Keep classroom results separate from library output. In scikit-learn, equal-size consensuses use the estimator’s score, and the final estimator is fitted to the winning inliers; consult its linked documentation rather than assuming our tie-breaker is built in.
Worked example and core equations
Label the constructed points A=(0,1), B=(1,3), C=(2,5), D=(3,7), E=(2,20). Use τ=0.2. The deliberately chosen trial AD gives m=(7−1)/(3−0)=2 and b=1. Its residuals in A–E order are 0,0,0,0,15, so its consensus is ABCD. Trial BE gives y=17x−14, residuals 15,0,15,30,0 and consensus BE. Trial CE is invalid for y=mx+b because the x values coincide. This three-trial trace is prescribed for checking, not presented as a random run.
How to audit the calculation
- Recompute each intermediate value from the definition before relying on a shortcut.
- Keep indices, coordinate order, units and normalisation visible.
- Test one case with a known exact answer.
- Name any random choice, tolerance or boundary convention.
- Compare with a slower reference calculation whenever possible.
- Separate numerical agreement from proof that the real-world model is appropriate.
Baseline and refit: least squares on all five points uses Σx=8, Σy=36, Σx²=18 and Σxy=74. Therefore m=(5×74−8×36)/(5×18−8²)=41/13 and b=(36−8m)/5=28/13. Refitting ABCD instead returns m=2, b=1. At the separate check point (4,9), the all-data line predicts 192/13≈14.769, an absolute error of 75/13≈5.769; the consensus refit predicts 9. This exact construction demonstrates sensitivity to E, not a universal superiority claim.
Four deeper ideas
Minimal samples create hypotheses cheaply
A line needs two distinct points, while a planar homography generally needs four nondegenerate correspondences. Using the smallest valid set reduces each proposal’s cost and raises the chance that every sampled item is an inlier, but makes degeneracy tests vital.
Consensus replaces one global quadratic loss
Least squares lets every residual influence the fit quadratically. RANSAC first makes a discrete inlier decision, so extreme outliers outside the threshold do not drag a candidate’s refit. The gain in robustness comes with threshold sensitivity and a discontinuous objective.
Probability budgets depend strongly on inlier fraction and sample size
Because w is raised to s, modest changes in either can multiply the required trials. A four-point model in a low-inlier dataset is far harder to sample cleanly than a two-point line. Reporting s, w estimate, p and maximum trials makes the trade-off visible.
Verification remains separate from random search
The best consensus can still support the wrong model family or a repeated structure. Hold-out checks, physical constraints, uncertainty analysis and comparison with alternative models are needed. Randomness helps find a hypothesis; it does not grant semantic truth.
Twelve practical investigations
1. Line With One Outlier
Do: reproduce the two fits above. Then replace E=(2,20) with E=(2,t), first t=5 and then t=10, keeping the other four points unchanged. Calculate the all-data slope from m=(21+t)/13 and intercept b=(8+t)/13.
Check: t=5 gives m=2, b=1; t=10 gives m=31/13, b=18/13. For the fixed candidate y=2x+1, E has residual |t−5|. It changes membership precisely when that residual crosses τ=0.2.
Explain: draw slope against t and compare continuous movement of least squares with the threshold decision. Do not claim the full random search always returns the same line merely because this one candidate’s consensus is stable.
2. Threshold Sweep
Data: use F=(0,1), G=(1,3.1), H=(2,4.8), J=(3,7.4), K=(4,9). First hold the candidate y=2x+1 fixed. Its absolute residuals are 0,0.1,0.2,0.4,0. Treat these decimals as exact classroom values; document any numerical tolerance in code.
Do and check: thresholds 0.05, 0.15, 0.25 and 0.45 yield consensus sizes 2, 3, 4 and 5. At τ=0.25 the selected set is FGHK. Its least-squares refit is y=(139/70)x+1, approximately 1.985714x+1. Its residual at the excluded J is 31/70≈0.442857.
Investigate: after completing this fixed-candidate test, rerun the full search at each threshold and record any changes in the winning equation. A larger threshold must not shrink a fixed candidate’s consensus, but it need not improve a newly selected model. Evaluate on separately declared check points, not just the observations used to choose τ.
3. Trial Calculation
Do: plan for p=0.99 and w=0.5. With q=w^s, s=2 gives q=0.25 and N=17; s=4 gives q=0.0625 and N=72. Use the ceiling, not ordinary rounding, and verify that N−1 falls short.
Finite-data check: for exactly five inliers among ten points, a uniform pair without replacement has q=C(5,2)/C(10,2)=2/9, requiring 19 independent trials. A four-point subset has q=C(5,4)/C(10,4)=1/42, requiring 192. The w^s approximation is noticeably optimistic for this small population.
Report: distinguish sampling without replacement within a trial from independent repetition across trials. These budgets target drawing a clean subset, not recovering the intended structure with 99% accuracy. Degeneracy and noisy minimal fits can weaken the practical outcome.
4. Degenerate Pair
Do: test the pairs (2,5),(2,5) and (2,5),(2,20). Both fail the unequal-x rule for y=mx+b. Record a skipped trial rather than dividing by zero or assigning an arbitrary slope.
Compare: the second pair identifies the geometric vertical line x=2, while duplicate coordinates do not identify a unique geometric line. This separates a limitation of the slope-intercept representation from a lack of information.
Stress test: replace the second x by 2.000001 while retaining y=20. The slope is 15,000,000. Perturb y by 0.01 and observe a slope change of 10,000. Choose any near-degeneracy tolerance from the measurement scale before running; an unexplained machine epsilon is not a measurement model.
5. Homography
Construct: map the four corners (0,0),(1,0),(1,1),(0,1) to (2,3),(3,3),(3,4),(2,4). The known transformation is the translation (x,y)↦(x+2,y+3), a special case of a planar homography. Hold out (0.5,0.5)↦(2.5,3.5) for checking.
Test: add the false correspondence (0.25,0.75)↦(9,9). Under the known transformation its Euclidean transfer error is √73.125≈8.551 coordinate units, whereas the five correct pairs have zero error. Set a threshold before fitting; report fitting pairs separately from the held-out pair.
Boundary: repeat with source points all on one line and explain why four correspondences alone are insufficient. For an implementation extension, use OpenCV’s findHomography documentation for reprojection-error and degeneracy conventions. A planar mapping is not a general explanation of an arbitrary 3D scene.
6. Two Lines
Construct: use (0,0),(1,1),(2,2),(3,3),(4,4) and (0,4),(1,3),(3,1),(4,0). These nine points lie on y=x or y=4−x, sharing (2,2). Use τ=0.1 and score both known candidates.
Check: each line has five inliers with zero residual. Our exact-tie rule keeps whichever was encountered first. If you remove its five inliers, the other line is left with four observations; the intersection has been assigned to the first model.
Investigate: reverse the candidate order, then compare hard removal with retaining the shared point for both models. Report membership overlap and how the rule changes the result. Two plausible structures are not evidence that one of them is a measurement error.
7. Heteroscedastic Noise
Construct: relative to y=2x+1, observation U=(1,3.3) has stated y-uncertainty σ=0.1 and V=(2,5.6) has σ=0.5. Treat x as exact for this exercise. The absolute residuals are 0.3 and 0.6.
Compare: a common raw threshold 0.4 accepts U and rejects V. Standardised residuals r/σ are 3 and 1.2; a declared cutoff of 2 rejects U and accepts V. This is a worked comparison of two rules, not proof that either observation is wrong.
Challenge: halve both stated uncertainties and recompute: standardised residuals become 6 and 2.4, so both fail. Explain why unjustified uncertainty estimates can dominate the classification. If x is uncertain too, dividing only by y-uncertainty no longer captures the full error model.
8. Random-State Audit
Protocol: use the five-point A–E dataset, τ=0.2 and the notebook rules. For each seed 0 through 19, draw three pairs uniformly from the ten distinct unordered pairs, independently across trials; invalid CE consumes a trial. Save the actual pair sequence, not just the seed, so another tool can replay it.
Record: seed, three sampled pairs, skipped count, winner, consensus size, refitted m and b, and absolute error at (4,9). If every sampled pair is invalid, record “no valid model”, set coefficients and check-point error to N/A, and count the run as a recovery failure. Declare recovery as consensus ABCD before inspecting the runs. The six pairs within ABCD all recover the clean line, so the exact probability of sampling at least one of them in three trials is 1−(4/10)^3=0.936.
Interpret: compare your observed recovery fraction with 0.936 without expecting twenty runs to match it exactly. Report failures and all seeds; do not select the seed with the best check-point error. Results across different software generators may differ even when the numeric seed matches.
9. Adaptive Stopping
Paper trace: use the w^s planning approximation with s=2 and p=0.99. An estimated inlier fraction 0.4 implies 27 trials; a later estimate 0.8 implies 5. If the stronger consensus first appears on trial 8, that revised target has already been exceeded.
Test: compare a fixed-budget run with a run that recomputes the target whenever its best consensus grows. Log the trial at each update, the consensus fraction and the revised total target. Never reinterpret a target of 5 as five additional trials.
Audit: a permissive threshold can inflate the apparent inlier fraction and shorten search. Keep the threshold fixed for this comparison, report the maximum-trial cap and check the returned model separately. An estimated fraction is not knowledge of which observations are genuinely valid.
10. Wrong Model Family
Construct: take x=−2,−1,0,1,2 with y=x². Ordinary least squares over these five symmetric points gives the horizontal line y=2; signed residuals are 2,−1,−2,−1,2.
Test: at τ=0.1, the candidate y=1 accepts just x=−1 and x=1. Other sampled lines can select other pairs, but a line intersects this exact parabola at at most two points. The quadratic y=x² fits all five exactly.
Explain: inspect residual shape and compare models on the unused points x=−1.5 and x=1.5, whose constructed y values are both 2.25. More trials cannot make a line represent this entire curve. With noisy data, a more flexible model also needs independent validation; the exact example is not an instruction to maximise complexity.
11. Image Localisation
Build a validation card: in a synthetic pinhole camera with unit focal length and zero principal point, projection is (X,Y,Z)↦(X/Z,Y/Z). The points (1,1,2) and (−1,−1,−2) both project to (0.5,0.5).
Check: both predictions can have zero image residual for that image coordinate, but only the first has positive depth in this camera convention. This toy ambiguity shows why reprojection agreement alone is insufficient. It is not a full pose-estimation experiment.
Extension: for a camera-pose project, hold out correspondences or a separate view, record calibration and coordinate conventions, and test positive depth as well as error. Consult OpenCV’s camera geometry documentation before choosing a pose solver; the required minimal sample depends on the solver and available information.
12. Medical Measurement
Use fictional data only: reuse A–E as anonymous instrument readings, without clinical units, patient labels or diagnostic interpretation. Ask two groups to consider the same E=(2,20): one is told it may be a transcription error; the other that it may represent a real unusual event.
Evidence task: both groups must calculate the same residual 15 against y=2x+1. Each must then list what information is missing, such as the original record, instrument checks or repeat measurement. A residual cannot distinguish the two explanations.
Boundary: retain the raw value and mark the algorithm’s classification separately from any decision to exclude it. This is an exercise in limits of inference, not clinical guidance. Do not apply the classroom exclusion rule to patient care or real health records.
Limits and misconceptions
The investigations expose different failure boundaries. A threshold can reject valid noisy measurements; a large threshold can accept unrelated points. A clean sample can be degenerate or unstable. Two structures can tie, and a curved dataset can defeat the line family. A controlled seed makes a run reproducible but does not establish reliability. In each conclusion, name the particular boundary your evidence tested rather than saying that RANSAC “removes outliers” without qualification.
Five misconceptions to challenge
- RANSAC removes every outlier: It proposes a consensus under a threshold; difficult outliers may fit and difficult inliers may fail.
- The default threshold is universal: Residual scale, units and noise distribution determine a meaningful threshold.
- More trials always solve the problem: No number of trials repairs a wrong model family, invalid residual or systematic bias.
- A fixed random seed proves robustness: It aids reproducibility; robustness needs results across data variations and appropriate probability analysis.
- The largest consensus must be the desired structure: Repeated clutter or a dominant irrelevant plane can have the most inliers.
Another broad misconception is that advanced mathematics automatically creates intelligence, admission, income or employment. It does not. Learning it can strengthen modelling, abstraction, calculation and explanation when practice is deliberate, but opportunities also depend on interests, communication, domain knowledge, education pathways and many circumstances outside one topic. Keep claims specific and options open.
A learning route for students
Move from a visible trace to a symbolic rule, then to code and critique. For every step below, prepare three pieces of evidence: a correct hand example, a boundary or failure case, and a short explanation using the relevant invariant. Do not advance merely because a library returned a number.
Step 1: Define a model and minimal sample
Write y=mx+b and list what x and y represent. Show why two unequal x values determine m and b. Add x=2 as a geometric line that this model family cannot express. Your output is a model contract, not just the phrase “two points”.
Step 2: Fit a line from two points
Calculate AD and BE from the labelled dataset without software. Substitute both sampled points back into each equation. Keep the slope calculation and intercept calculation separate so a sign error is visible.
Step 3: Choose a residual with units
For E against y=2x+1, calculate vertical residual 15. If both axes use the same Euclidean units, perpendicular distance is 15/√5≈6.708. Explain why reusing the numerical threshold after changing the residual changes the experiment.
Step 4: Classify inliers under a threshold
Use the F–K residuals and classify every row at τ=0.2. F,G,H,K pass, including H on the boundary; J fails. Distinguish exact-decimal arithmetic from any rounding tolerance your spreadsheet or code uses.
Step 5: Reject degenerate samples
Write a rejection test for equal x and record the CE failure before attempting a fit. Then use the near-vertical pair in investigation 4 to show why exact nonzero separation is not the same as numerical stability.
Step 6: Score and refit a consensus
Keep both the sampled candidate and its refitted model in your notebook. For FGHK, compare y=2x+1 with y=(139/70)x+1. Recompute all five residuals, including the excluded point, instead of presenting only the refit coefficients.
Step 7: Derive the approximate trial formula
Start with failure in one trial, 1−q, and multiply across N independent trials. Taking logarithms reverses the inequality when dividing by log(1−q), which is negative. Check both N and N−1 for one budget in investigation 3.
Step 8: Control and report random state
Run the twenty-seed protocol only after freezing its recovery criterion. Archive data order, seed, sampled pairs and software version. Repeat one recorded pair sequence manually to check whether a discrepancy comes from sampling or scoring.
Step 9: Compare ransac with least squares
Put the all-data least-squares line and consensus-refitted line on the same axes. Report predictions at (4,9), then repeat after changing E to (2,5). Explain why the two methods agree in that uncorrupted construction.
Step 10: Separate robust estimation from semantic validation
Use the two-line or camera-depth example to separate “fits the data under our rule” from “answers our intended question”. End with one additional observation that could distinguish the competing explanations and one claim your existing evidence cannot justify.
A four-week practice plan
- Week 1 — vocabulary and diagrams: define each object, redraw the core mechanism and reproduce the worked example.
- Week 2 — equations and manufactured tests: derive the key relation and test inputs with known answers.
- Week 3 — implementation and evidence: use a trusted library, record parameters and compare with a transparent baseline.
- Week 4 — transfer and critique: apply the mechanism to a new context, report limits and identify when a simpler or different method is better.
Guidance for parents and educators
Ask ‘what stays true?’ and ‘what could make this conclusion fail?’ before asking for speed. Invite the student to draw the input and output, label units, estimate the answer and locate the first step that disagrees with a reference. These prompts reveal conceptual understanding more clearly than memorised terminology.
Advanced enrichment should sit beside secure school foundations in algebra, functions, geometry, statistics and computational thinking. It need not accelerate a student into a fixed career identity. A healthy goal is curiosity with discipline: the learner becomes comfortable reading unfamiliar notation, checking an example, revising a model and communicating uncertainty.
Evidence prompts for a useful conversation
- Which quantity is observed and which is inferred?
- Which step saves work, and why is it allowed?
- Is the claim exact, approximate, probabilistic or empirical?
- What units and conventions are in use?
- Which input makes the method struggle?
- What simple baseline could check the answer?
- What would count as evidence that the chosen parameters are poor?
Mini-project assessment
A strong mini-project contains the original question, a small reproducible dataset, a hand-worked example, code or a spreadsheet trace, at least one boundary test, a comparison with a baseline, and a paragraph on limits. Assess the reasoning trail as well as the final output. If a student can explain why a surprising result appeared and repair the model, that is valuable mathematical progress even when the first attempt was wrong.
Ten evidence checks
Check 1: Define a model and minimal sample
Pass if the student states the model family and validity condition, and correctly explains both duplicate coordinates and distinct points with equal x. A two-point count without those distinctions is incomplete.
Check 2: Fit a line from two points
Pass if AD gives m=2,b=1 and BE gives m=17,b=−14, with substitution into both sampled points. A correct plotted line without recoverable arithmetic is not enough.
Check 3: Choose a residual with units
Pass if residual type and units are named and E’s vertical value 15 is distinguished from its perpendicular value 15/√5 when Euclidean distance is meaningful. Ask what happens if only one axis is rescaled.
Check 4: Classify inliers under a threshold
Pass if the student produces the full F–K membership list at τ=0.2 and explains why the equality case passes. Test a point just outside the boundary to detect accidental rounding before comparison.
Check 5: Reject degenerate samples
Pass if CE is logged as invalid for this representation and no infinite or arbitrary slope is scored. Ask for the near-degenerate example and a measurement-based reason for any extra tolerance.
Check 6: Score and refit a consensus
Pass if the original consensus, tie-break rule and refitted coefficients are all visible. Use FGHK to check m=139/70,b=1 and verify that residuals are recomputed rather than copied from the sampled line.
Check 7: Derive the approximate trial formula
Pass if s=4,w=0.5,p=0.99 yields 72 under w^s, while five inliers among ten distinct IDs yield 192 under the exact finite-subset rule. The student must identify which sampling design each calculation describes.
Check 8: Control and report random state
Pass if every planned seed, failure and skipped sample is reported. Ask a peer to replay one logged pair sequence. An especially successful seed alone cannot support a reliability claim.
Check 9: Compare ransac with least squares
Pass if the five-point least-squares calculation and the error 75/13 at the separate check point are recoverable. Ask the student to repeat with E=(2,5), where both approaches recover y=2x+1.
Check 10: Separate robust estimation from semantic validation
Pass if the conclusion names a counterexample: tied structures, curved data, negative camera depth or an unexplained unusual measurement. Require an explicit statement of what extra evidence is needed before interpreting the consensus.
Frequently asked questions
What does RANSAC stand for?
Random Sample Consensus.
Why use minimal samples?
They make proposals cheap and maximise the chance that every sampled observation is an inlier.
What is a consensus set?
The observations whose residuals to a candidate model fall within the declared threshold.
How is the best model chosen?
Usually by consensus size plus a residual-based tie-breaker, followed by refitting on inliers.
How many trials are needed?
A common approximation uses inlier fraction, sample size and target success probability; implementation assumptions matter.
Why test degeneracy?
A sample can have enough points but still fail to identify a stable model.
Is RANSAC deterministic?
Only when sampling and all implementation choices are controlled; otherwise results can vary.
Does it estimate several structures?
Standard RANSAC usually targets one dominant structure; multi-model methods or sequential strategies add assumptions.
What should students know first?
Lines, residuals, probability, logarithms, random sampling and least squares.
What broader lesson does it teach?
Robustness can come from testing many small hypotheses against explicit evidence rather than trusting every observation equally.
Useful next reading and sources
Begin with Fischler and Bolles’ 1981 RANSAC paper for the historical result and official scikit-learn RANSACRegressor documentation for a maintained modern interface or reference. Then connect the mechanism to these verified eduKateSG articles:
- Hough transforms and parameter-space voting
- Kabsch alignment and best-fit rotations
- Douglas–Peucker simplification and tolerance
For the broader series, continue at eduKateSG Mathematics. Use primary sources for definitions and results, official documentation for present software behaviour, and experiments for performance on the actual task. Those evidence types support different claims and should not be merged casually.
Final takeaway
A finished investigation lets another learner replay the evidence: the data and rules are explicit, the arithmetic has a check, the experiment changes one factor at a time, and the conclusion stops where the evidence stops. Keep the successful fit alongside its failure case.
