A straight-line trend can be pulled sharply by one unusual observation when it is fitted by squared error. Theil–Sen regression takes a different route: calculate the slope between many pairs of points and use their median. An isolated extreme response then affects only the pairwise slopes that touch it, while many ordinary pairs still support the central trend. Mathematics matters because medians, combinations, order statistics and geometry produce an interpretable robust estimator. Robust does not mean immune to bad data, causal, nonlinear-proof or assumption-free. The plot, sampling process, repeated x values and uncertainty still need attention.
The practical search question is not simply “what is this named method?” but “which mathematical structure makes the method work, what does it guarantee, and what evidence would show that it is a poor choice?” This article keeps those questions together so students can connect mathematics education with computing, engineering, data reasoning and careful everyday decisions without turning one technique into a career promise.
Quick navigation: Why it matters · Mechanism · Worked example · Deeper ideas · Investigations · Limits · Learning route · Parent guidance · FAQs · Sources
Why this mathematics matters
Mathematics is important here because it turns an apparently visual or computational trick into a chain of reasons. The representation tells us what information is kept. The equation or invariant explains each legal step. The complexity analysis describes the work. The boundary conditions say when a conclusion should not be trusted. That combination is a practical form of problem-solving literacy.
A strong learner should be able to move in both directions: from a real task to a mathematical model, and from a formal guarantee back to its real meaning. Naming an algorithm is not enough. The useful achievement is to explain why the next operation is valid, check a small instance independently, and notice which assumptions the implementation has added.
Source: SciPy reference for Theil–Sen slope and intercept conventions.
Primary or implementation reference: Sen’s 1968 paper on slope estimates based on Kendall’s rank test.
How the mechanism works
For observations (x_i,y_i), form every pair with x_j different from x_i and calculate s_ij=(y_j-y_i)/(x_j-x_i). Sort the valid pairwise slopes and take their median as the fitted slope. Then choose and state an intercept convention. One common separate-median convention uses median(y)-slope×median(x); a joint convention uses median(y-slope×x). Statistical software may provide either or both. Confidence limits for the slope can be obtained from the ordered slopes using rank-based theory. Ties, missing values and repeated x coordinates must be handled according to the implementation rather than silently divided by zero.
With n distinct x values there are n(n-1)/2 pairwise slopes, which explains both the estimator’s information and the naive quadratic computational cost. The median minimises absolute deviation in one dimension and is resistant to a minority of extreme slope values. One outlying point participates in only n-1 of all pairs, while the ordinary points contribute many mutual slopes. This combinatorial balance gives the estimator its robustness. However, leverage patterns, clustered contamination and dependence can still bias it. Theil–Sen estimates a monotone linear trend in the chosen coordinates; changing scales or applying transformations changes the scientific question.
Keep a four-column notebook labelled object, rule, guarantee and caveat. In the first column name the inputs and stored state. In the second write one update precisely. In the third state what remains true after the update. In the fourth record a tie, approximation, domain restriction or hardware assumption. This routine prevents symbols from drifting away from their meaning.
Worked example and core calculations
Use five points: (1,2), (2,4), (3,6), (4,8) and (5,100). The six slopes among the first four points are all 2. The four slopes involving the outlier are 98/4=24.5, 96/3=32, 94/2=47 and 92. Sorted, the ten slopes are 2,2,2,2,2,2,24.5,32,47,92. The median is the average of the fifth and sixth values, so the Theil–Sen slope is 2. Under the joint intercept convention, the residual intercept values y-2x are 0,0,0,0 and 90, whose median is 0. The fitted robust line is y=2x for this illustrative dataset.
How to audit the calculation
- Recompute every intermediate value directly from the definition.
- Keep labels, indices, signs, units and normalisation visible.
- Test a case with a known exact answer.
- State tie rules, random seeds, tolerances and boundary conventions.
- Compare with a slower transparent baseline or trusted implementation.
- Separate agreement of numbers from suitability of the real-world model.
Small examples are not childish; they are diagnostic instruments. A five-line trace can reveal a reversed inequality, a missing scale factor or an inconsistent label convention before a large dataset hides the same error behind plausible output.
Four deeper ideas
Robustness comes from combinations
Ordinary points create many mutually consistent slopes, while one outlier contaminates only the pairs that include it.
To transfer this idea, ask what would remain true if labels, scale or input order changed. Then identify the step whose justification would fail if the stated assumption were removed. This turns the paragraph into a reusable reasoning pattern rather than a fact to memorise.
The median changes the loss geometry
Extreme slope magnitudes do not pull the median as they pull an arithmetic mean.
To transfer this idea, ask what would remain true if labels, scale or input order changed. Then identify the step whose justification would fail if the stated assumption were removed. This turns the paragraph into a reusable reasoning pattern rather than a fact to memorise.
Did you know? Intercept definitions can differ
Two implementations can agree on slope but report different intercepts because their conventions are not identical.
To transfer this idea, ask what would remain true if labels, scale or input order changed. Then identify the step whose justification would fail if the stated assumption were removed. This turns the paragraph into a reusable reasoning pattern rather than a fact to memorise.
A robust line is still a model
Curvature, dependence, measurement error and selection bias do not disappear when the estimator changes.
To transfer this idea, ask what would remain true if labels, scale or input order changed. Then identify the step whose justification would fail if the stated assumption were removed. This turns the paragraph into a reusable reasoning pattern rather than a fact to memorise.
Twelve practical investigations
1. One vertical outlier
Use the worked example. List all ten slopes.
What the mathematics reveals: The median preserves the ordinary trend. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Still investigate the point. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
2. No outliers
Use points near one line. Compare with least squares.
What the mathematics reveals: Both can be informative. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Efficiency may differ. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
3. Repeated x values
Include two responses at the same x. Skip undefined pair slopes per method.
What the mathematics reveals: Division by zero is not a slope. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Document tie handling. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
4. Even slope count
Use four or five points. Apply the median convention.
What the mathematics reveals: Middle slopes may be averaged. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Match the software. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
5. Different intercept rules
Fit separate and joint forms. Compare intercepts.
What the mathematics reveals: Slope agreement does not imply identical lines. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Name the method. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
6. Curved relation
Generate a quadratic pattern. Plot residual structure.
What the mathematics reveals: One line is inadequate. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Robustness is not flexibility. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
7. Clustered outliers
Place several bad points together. Recalculate slopes.
What the mathematics reveals: Contamination can dominate combinations. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Do sensitivity analysis. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
8. Time dependence
Use serial observations. Inspect autocorrelation.
What the mathematics reveals: Slope estimation does not ensure independent errors. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Use suitable inference. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
9. Unit conversion
Change x from years to months. Transform the slope.
What the mathematics reveals: Numerical value follows units. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Interpret dimensions. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
10. Confidence interval
Order pairwise slopes. Use trusted software.
What the mathematics reveals: Uncertainty belongs with the estimate. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Do not report false precision. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
11. Large dataset
Estimate pair count. Consider efficient algorithms or sampling.
What the mathematics reveals: Quadratic work can matter. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: Validate approximations. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
12. Prediction
Predict inside and outside the x range. Separate interpolation and extrapolation.
What the mathematics reveals: Robust fitting does not make distant forecasts safe. Evidence to collect: write the input, predict the next state, carry out the calculation, and compare the result with an independently produced reference. Explain the exact invariant, equation, inequality or counting rule that supports the conclusion. Then vary one assumption and record whether the result changes smoothly, discontinuously or not at all.
Limit to keep visible: State the observed range. A useful report places this boundary beside the result rather than hiding it in a final disclaimer.
Limits and misconceptions
The naive calculation stores or sorts O(n squared) slopes, which can be expensive for large datasets. Repeated x values yield undefined pair slopes and require defined handling. The estimator describes a line; strong curvature or changing regimes should be modelled rather than hidden. Robustness weakens when contamination is structured or numerous, and high-leverage x patterns deserve inspection. Ordinary confidence procedures may be unsuitable for dependent time series or clustered data. Neither a positive slope nor statistical significance establishes causation. Extrapolation beyond the observed x range remains risky.
Five misconceptions to challenge
- Robust means outliers have no effect: They can still influence many pairwise slopes.
- Theil–Sen deletes unusual points: It keeps observations and changes how slope is summarised.
- The intercept is uniquely defined: Common software conventions differ.
- A robust slope proves a causal relationship: It estimates association under a line model.
- It always beats least squares: Efficiency, assumptions and the task determine the better estimator.
Another broad misconception is that advanced mathematics automatically creates intelligence, admission, income or employment. It does not. Deliberate study can strengthen modelling, abstraction, calculation and explanation, while real pathways also depend on interests, communication, domain knowledge, opportunities and circumstances outside one topic.
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 software returned a number.
Step 1: Calculate pairwise slopes
Begin with the smallest non-trivial instance that lets you calculate pairwise slopes. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 2: Count observation pairs
Begin with the smallest non-trivial instance that lets you count observation pairs. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 3: Take an ordered median
Begin with the smallest non-trivial instance that lets you take an ordered median. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 4: Choose an intercept convention
Begin with the smallest non-trivial instance that lets you choose an intercept convention. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 5: Handle repeated x values
Begin with the smallest non-trivial instance that lets you handle repeated x values. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 6: Compare robust and least-squares fits
Begin with the smallest non-trivial instance that lets you compare robust and least-squares fits. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 7: Plot data and residuals
Begin with the smallest non-trivial instance that lets you plot data and residuals. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 8: Interpret slope units
Begin with the smallest non-trivial instance that lets you interpret slope units. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 9: Report uncertainty and sensitivity
Begin with the smallest non-trivial instance that lets you report uncertainty and sensitivity. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
Step 10: Separate association from causation
Begin with the smallest non-trivial instance that lets you separate association from causation. Predict the result before calculating. Write each state change clearly enough that another student can reproduce it. Compare with a trusted implementation only after the hand trace is complete.
Next, change one feature—size, ordering, sign, tolerance, labelling or data distribution—and explain which part of the reasoning changes. Finish by teaching the step without code and by naming one input for which the method is unhelpful. This predict–calculate–vary–explain cycle is a strong test of transferable understanding.
A four-week practice plan
- Week 1 — vocabulary and diagrams: define each object, redraw the 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 method to a new context, report limits and identify when a 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, estimate the result 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 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. Repairing a model after a surprising result is valuable mathematical progress.
Six evidence checks for a finished project
Check 1: Calculate pairwise slopes
Ask the student to demonstrate how to calculate pairwise slopes without relying on a hidden software step. The evidence should include a labelled input, the exact rule used, one intermediate state and a result that can be checked independently. If the answer changes after labels or order change, the student should say whether that reflects the mathematics or only a convention.
Then request one deliberately awkward example. The learner should predict the difficulty before running the method, explain what the output does and does not establish, and name a reasonable alternative or safeguard. This check joins correctness with judgement: a technically valid calculation is useful only when its assumptions and interpretation remain visible.
Check 2: Count observation pairs
Ask the student to demonstrate how to count observation pairs without relying on a hidden software step. The evidence should include a labelled input, the exact rule used, one intermediate state and a result that can be checked independently. If the answer changes after labels or order change, the student should say whether that reflects the mathematics or only a convention.
Then request one deliberately awkward example. The learner should predict the difficulty before running the method, explain what the output does and does not establish, and name a reasonable alternative or safeguard. This check joins correctness with judgement: a technically valid calculation is useful only when its assumptions and interpretation remain visible.
Check 3: Take an ordered median
Ask the student to demonstrate how to take an ordered median without relying on a hidden software step. The evidence should include a labelled input, the exact rule used, one intermediate state and a result that can be checked independently. If the answer changes after labels or order change, the student should say whether that reflects the mathematics or only a convention.
Then request one deliberately awkward example. The learner should predict the difficulty before running the method, explain what the output does and does not establish, and name a reasonable alternative or safeguard. This check joins correctness with judgement: a technically valid calculation is useful only when its assumptions and interpretation remain visible.
Check 4: Choose an intercept convention
Ask the student to demonstrate how to choose an intercept convention without relying on a hidden software step. The evidence should include a labelled input, the exact rule used, one intermediate state and a result that can be checked independently. If the answer changes after labels or order change, the student should say whether that reflects the mathematics or only a convention.
Then request one deliberately awkward example. The learner should predict the difficulty before running the method, explain what the output does and does not establish, and name a reasonable alternative or safeguard. This check joins correctness with judgement: a technically valid calculation is useful only when its assumptions and interpretation remain visible.
Check 5: Handle repeated x values
Ask the student to demonstrate how to handle repeated x values without relying on a hidden software step. The evidence should include a labelled input, the exact rule used, one intermediate state and a result that can be checked independently. If the answer changes after labels or order change, the student should say whether that reflects the mathematics or only a convention.
Then request one deliberately awkward example. The learner should predict the difficulty before running the method, explain what the output does and does not establish, and name a reasonable alternative or safeguard. This check joins correctness with judgement: a technically valid calculation is useful only when its assumptions and interpretation remain visible.
Check 6: Compare robust and least-squares fits
Ask the student to demonstrate how to compare robust and least-squares fits without relying on a hidden software step. The evidence should include a labelled input, the exact rule used, one intermediate state and a result that can be checked independently. If the answer changes after labels or order change, the student should say whether that reflects the mathematics or only a convention.
Then request one deliberately awkward example. The learner should predict the difficulty before running the method, explain what the output does and does not establish, and name a reasonable alternative or safeguard. This check joins correctness with judgement: a technically valid calculation is useful only when its assumptions and interpretation remain visible.
Frequently asked questions
What is the Theil–Sen slope?
The median of valid pairwise slopes.
Why is it robust?
A minority of extreme pairwise slopes does not determine the median.
Does it remove outliers?
No.
What happens when x values repeat?
Pairs with zero x difference have undefined slopes and need explicit handling.
How many slopes are there?
Up to n(n-1)/2 for n observations with distinct x values.
How is the intercept calculated?
Conventions differ; state which one is used.
Is it always better than least squares?
No.
Can it model curves?
Not by itself; it estimates a linear trend.
Does a slope prove causation?
No.
What should students know first?
Coordinate geometry, slope, medians, combinations, plotting and statistical uncertainty.
Useful next reading and sources
Source: SciPy reference for Theil–Sen slope and intercept conventions.
Source: Sen’s 1968 paper on slope estimates based on Kendall’s rank test.
Related eduKateSG reading: PAVA isotonic regression.
Related eduKateSG reading: CUSUM change detection.
Related eduKateSG reading: Comparing percentages fairly.
Read sources with a question in mind: which definition, theorem, default or limitation is being supported? An authoritative link is useful evidence, but it does not replace a worked calculation or a clear statement of how the source applies to the example.
A cheerful final thought
Mathematics becomes friendlier when every symbol has a job and every claim has a boundary. Start small, make the state visible, test the awkward case, and celebrate the moment when a pattern becomes explainable. That is the practical benefit of learning mathematics: not certainty about every future, but a growing ability to ask better questions and build trustworthy answers.
