cswork / libraryOpen practice workspace

Articles

Correlation Interview Questions: Pearson, Spearman, Outliers, and Nonlinear Dependence

Practice correlation interviews with Pearson and Spearman counterexamples, an outlier sensitivity calculation, nonlinear dependence, and inference limits.

“Which correlation should I use?” is a good interview question only after you ask what pattern you want to detect. Pearson measures linear association. Spearman applies Pearson's idea to ranks and captures monotonic association. Neither coefficient is a general detector of every dependency, and neither establishes a causal effect. A five-point U-shape can be perfectly determined by x while both coefficients are zero.

The datasets and worked calculations here are original teaching examples, not employer-specific interview reports. SciPy's pearsonr documentation defines Pearson's linear coefficient and its inference output; SciPy's spearmanr documentation defines rank-order correlation. The distinction between official method behavior and our suggested answer sequence matters: a library contract can establish what a statistic computes, while an interview strategy is editorial inference.

Pearson or Spearman: what question are you asking?

Pearson's sample coefficient r compares centered x and y values. One readable form is r = sum((x - mean_x) * (y - mean_y)) / sqrt(sum((x - mean_x)^2) * sum((y - mean_y)^2)), with sums over paired observations. The result lies between -1 and 1 when both variances are nonzero. Near +1 means a strong positive linear pattern in this sample; near -1 means a strong negative one. Zero means the centered cross-product sum cancels, not that the variables are independent. SciPy documents the coefficient as a measure of linear relationship.

Spearman's coefficient is the Pearson correlation of rank-transformed values, with appropriate handling of ties. It asks whether larger x values tend to pair with larger or smaller y values, even if the numerical steps are not linear. A strictly increasing curved relationship can have Spearman equal to one while Pearson is lower than one. A symmetric U-shape is different: as x moves from negative values toward zero, y falls, then rises again. That pattern is nonmonotonic, so a near-zero Spearman result is possible despite deterministic dependence.

Choose a coefficient from the data question, not from a superstition that rank correlation is always “safer.” If you care about change in a measured amount per unit of x, preserve the values and examine a scatterplot, a model, and Pearson or regression as appropriate. If the values are ordinal or a monotonic ordering is the main question, Spearman may match the estimand better. Ranking changes the question: it discards the size of gaps between observations. Outliers, ties, missingness, and sample selection still require inspection.

Interview calculation 1: How can a perfect curve have r = 0?

Take five paired observations: x = [-2, -1, 0, 1, 2] and y = [4, 1, 0, 1, 4]. Every y equals x², so knowing x determines y exactly. Yet mean_x = 0. The numerator of Pearson's coefficient is the sum of x * (y - mean_y). Because the pairs at -2 and 2 have equal y values, their products cancel; the same happens for -1 and 1; the zero term contributes nothing. Thus the numerator is zero while both denominator sums are positive, and Pearson r = 0.

Spearman is zero here too. Ranking x yields [1, 2, 3, 4, 5]; ranking y with average ranks for ties yields [4.5, 2.5, 1, 2.5, 4.5]. The rank deviations from their means cancel symmetrically. This is a stronger interview counterexample than the vague phrase “correlation is not causation”: it shows that even a deterministic relationship can be invisible to both linear and monotonic coefficients.

The conclusion is narrow. Zero Pearson and Spearman do not prove no relationship; the paired scatterplot reveals the U. NIST's scatter-plot guidance explicitly uses plots to inspect linear and nonlinear patterns, outliers, and changing variation. If the shape is plausible in the domain, consider transformations, a nonlinear model, or another dependence measure chosen for that question. Do not automatically fit a quadratic and then announce causality. The values may still be confounded, selected, or collected with measurement error; the curve is an association in this constructed dataset.

Paired patternPearson answerSpearman answerWhat a plot adds
y = x on nonconstant values+1+1An exact increasing line.
Symmetric y = x² on [-2,-1,0,1,2]00A deterministic U-shape.
Increasing values with one extreme endpointBelow +1+1 if order is unchangedThe leverage of one numeric magnitude.

The table is an original comparison, and its third row needs numbers before it becomes a claim. The next calculation supplies them. All three examples assume complete, correctly paired observations. Pairing errors can make any coefficient meaningless even when the code returns a number.

Interview calculation 2: What does one outlier change?

Start with x = [1, 2, 3, 4, 5, 6] and y = [1, 2, 3, 4, 5, 6]. Both coefficients are +1. Now change only the last y to 1000, yielding [1, 2, 3, 4, 5, 1000]. The ranks of y are still [1, 2, 3, 4, 5, 6], so Spearman remains +1. Pearson drops to about 0.6573 because the final observation changes the means and cross-products disproportionately. We verified these figures using SciPy's pearsonr and spearmanr functions for this exact six-pair dataset.

This does not prove Spearman is immune to outliers. A point that changes rank order can change Spearman, and many tied values reduce the rank information available. Nor does Pearson's lower result mean the first five pairs stopped lying on a line. The right explanation is that the coefficients answer different questions about the whole set of six values. Plot the points, inspect whether the extreme value is a measurement error or a valid rare observation, and report a sensitivity comparison with and without it if that comparison is substantively justified. Never silently delete it simply to improve r.

The interview follow-up is often “Would you winsorize?” Answer with an estimand. Capping or excluding the value changes the population relationship you report. If the business decision concerns extreme amounts, suppressing the tail may erase the very cases that matter. If a sensor is known to have produced an impossible reading, document that quality rule before recalculating. A robust analysis shows the raw data, the rationale for any treatment, and how the decision changes under reasonable alternatives.

Constant inputs, ties, and missing observations

If every x is the same, the Pearson denominator contains zero variance; the coefficient is undefined. Spearman is also undefined when one ranked input is constant. Do not answer zero. SciPy's pearsonr reference says a constant input produces a warning and NaN; it also warns about near-constant input because numerical error can make a reported value inaccurate. The Spearman reference likewise documents constant-input behavior.

With ties, Spearman commonly uses average ranks rather than arbitrarily ordering equal values. This matters for coarse ratings, rounded measurements, and binary indicators. Ask how the library handles ties, and do not apply the familiar no-ties shortcut formula for Spearman to a tied dataset without checking. If two arrays have different lengths, or missing entries were dropped independently rather than pairwise, the computation is not even based on the intended observations. Report the number of complete pairs and the missing-data rule.

For very small samples, any apparent association is unstable. SciPy's docs describe p-value and confidence-interval machinery, but a printed r alone is a descriptive statistic of the observed pairs. The uncertainty attached to a broader population claim depends on the sampling design and assumptions. A sample of six independent people is not equivalent to six repeated records from one person; clustering or temporal dependence can invalidate an off-the-shelf significance interpretation.

Does a significant correlation prove a useful effect?

No. Separate coefficient, p-value, confidence interval, and decision. A coefficient describes strength and direction under a chosen measure. A p-value evaluates a specified null under a reference model; it is not the probability the null is true. A confidence interval gives a range produced by a method under its assumptions. None of these by itself establishes causation or practical importance. SciPy's pearsonr reference explicitly returns both a statistic and a p-value, with choices for alternative hypotheses and confidence intervals.

Before interpreting a low p-value, ask whether the pairs were sampled independently, whether an outlier dominates, whether the relationship is linear enough for the coefficient to answer the question, and whether the test was selected before seeing dozens of candidate variables. A very small coefficient can be statistically detectable in a large dataset yet offer little predictive or business value. Conversely, a wide interval in a small dataset may leave an important effect unresolved. State both the estimate and uncertainty, then connect them to an action.

“Correlation is not causation” should lead to a concrete alternative explanation. Perhaps a promotion causes both site traffic and sales; perhaps the retained customers who use a feature were already more engaged; perhaps a third measurement process creates both columns. Propose a randomized test where feasible or a design with explicit identification assumptions. Do not treat switching Pearson to Spearman as a causal adjustment: ranking alters a statistic, not the assignment mechanism.

A concise interview answer

“I would plot the paired observations and define whether the question is linear association, monotonic ordering, or broader dependence. Pearson uses numerical deviations from the means; Spearman correlates ranks. I would check constants, ties, missing pairs, and influential points before interpreting either. A symmetric U-shape can make both coefficients zero despite deterministic dependence; one extreme endpoint can move Pearson while leaving ranks unchanged. I would report the coefficient separately from its uncertainty and avoid causal language unless the study design supports it.”

Practice by recomputing the two tiny datasets above by hand or with a trusted function, then explain the plot first and the coefficient second. The Statistics & Math question bank offers a separate follow-up on correlation constraints and interpretation.

Sources and Further Reading