Understanding Correlation vs Causation
Correlation describes an association in observed data; causation describes what would change under an intervention. Suppose branches with larger advertising budgets also record more sales. The correlation answers whether high budgets and high sales tend to appear together. The causal question is different: would increasing a particular branchβs budget increase its sales, compared with leaving that budget unchanged?
Pearsonβs correlation coefficient, usually written r, summarizes linear association between two numerical variables. It ranges from β1 to +1 when both variables have nonzero variation. Positive values indicate that larger values tend to occur together; negative values indicate that larger values of one variable accompany smaller values of the other. Values near zero mean little linear association, not necessarily no relationship: a curved pattern can have zero Pearson correlation.
Even perfect correlation cannot identify a causal direction. Advertising might increase sales, but managers might also allocate larger budgets to branches already selling more: reverse causation. Alternatively, branch size might influence both budget and sales. Such a common cause is a confounder. Combining branches of different sizes can therefore produce an association that does not represent what changing advertising would accomplish.
Causal reasoning compares outcomes under alternative actions. We cannot observe the same branch, during the same period, both receiving and not receiving an extra budget. A well-designed randomized experiment addresses this problem by assigning treatment independently of preexisting characteristics. Randomization balances confounders on average, not necessarily exactly in a small sample. Attrition, spillovers between branches, and inconsistent treatment delivery can still undermine interpretation.
When experiments are impractical or unethical, observational causal analysis requires explicit assumptions. Comparing otherwise similar branches may help, but only if relevant confounders are measured and handled appropriately. More rows or a more flexible prediction model cannot automatically repair unmeasured confounding. Prediction asks what outcome is likely; causal inference asks what an action would change. The supplied pandas guide organizes tools for descriptive statistics and grouping, while scikit-learn separates prediction, evaluation, and preprocessing topics. Those tools support analysis, but a computed coefficient or accurate prediction is not, by itself, a causal argument. Study design and subject knowledge remain essential to interpreting either result responsibly.
Practical walkthrough
Use the example below to investigate how aggregation changes an association. Prerequisites are basic Python lists, dictionaries, and function calls, plus an installed pandas environment. The code uses APIs available in pandas 2.x and 3.x; use a Python version supported by your installed pandas release. It creates all data in memory and needs no external files.
First, identify the observational unit: each row represents one fictional branch during one period. Advertising is measured in arbitrary budget units, and sales in arbitrary sales units. The six rows are deliberately constructed teaching data, not evidence about actual advertising effectiveness. Branch size is recorded before the hypothetical budget decision, making it a plausible candidate confounder rather than a consequence of advertising.
Next, calculate the pooled Pearson correlation using the advertising and sales columns. This ignores branch size. Then split the rows by size and repeat exactly the same calculation inside each group. Explicit grouping follows the split-and-combine approach covered by the pandas guide. Formatting each result to three decimal places makes the printed output predictable.
Finally, compare the signs, not just the magnitudes. The pooled association is positive, but both within-size associations are negative. This reversal warns that mixing unlike groups can obscure their individual patterns. It does not establish that advertising reduces sales. Each group contains only three observations, and additional causes remain unspecified.
A responsible next step would be to define a specific budget intervention, choose a sales measurement window, and assess whether random assignment across comparable branches is feasible before making a causal recommendation.
Worked example
import pandas as pd
branches = pd.DataFrame({
"size": ["small", "small", "small", "large", "large", "large"],
"advertising": [1, 2, 3, 7, 8, 9],
"sales": [3, 2, 1, 9, 8, 7],
})
pooled = branches["advertising"].corr(branches["sales"])
print(f"All branches: r = {pooled:.3f}")
for size in ("small", "large"):
group = branches.loc[branches["size"] == size]
correlation = group["advertising"].corr(group["sales"])
print(f"{size.capitalize()} branches: r = {correlation:.3f}")
Expected output
All branches: r = 0.862
Small branches: r = -1.000
Large branches: r = -1.000
Within small branches, advertising increases from 1 to 3 while sales decrease from 3 to 1. Large branches follow the same descending straight-line pattern, shifted upward in both variables. Consequently, each within-group correlation is exactly β1. Across all six branches, the separation between groups dominates: large branches have both higher budgets and higher sales. The pooled coefficient is 25/29, printed as 0.862. This is an aggregation reversal often called Simpsonβs paradox. The arithmetic demonstrates that a pooled association can differ from conditional associations; neither calculation reveals the effect of intervening on advertising. No model training or random sampling is involved.
Common mistakes and how to avoid them
Reading r = 0.862 as an 86.2% sales increase caused by advertising.
Treat r as a unitless measure of linear association. Estimating a causal sales change requires a defined intervention, outcome scale, and defensible study design.
Assuming the negative within-group correlations prove advertising is harmful.
Report them as conditional associations. Investigate budget allocation, timing, and other common causes before interpreting the direction causally.
Controlling for every available column to remove bias.
Choose adjustment variables using causal reasoning. Adjusting for consequences of treatment can remove part of its effect; conditioning on shared consequences can introduce bias.
Treating strong predictive performance as causal validation.
Separate forecasting from intervention questions. For prediction, split data before fitting preprocessing and fit it only on training data; leakage-free accuracy still does not establish causation.
Review questions
What does a positive Pearson correlation tell you, and what does it leave unresolved?
Larger values tend to occur together in a linear pattern. It does not identify causal direction, rule out common causes, or quantify the outcome change from an intervention.
Why does grouping by branch size reverse the sign in this example?
Between-group differences raise both advertising and sales together. Within each size group, the constructed observations instead follow a descending line. Pooling combines these distinct sources of variation.
Why is randomized budget assignment stronger evidence than observing existing budgets?
Assignment independent of preexisting characteristics breaks systematic links between treatment and confounders on average. Credible interpretation still requires appropriate outcome measurement and attention to attrition, compliance, and spillovers.
Would collecting thousands more branches resolve the causal question?
More observations can improve precision, but systematic confounding can remain. A causal conclusion needs a suitable design and justified assumptions, not merely a more precisely estimated association.