devnotes.

What is Correlation Analysis?

What is Correlation Analysis?

Most things in the world are connected: study time and marks, price and demand, rainfall and crop yield. Correlation analysis is the statistical technique that measures how strongly two variables are linked and in which direction.

It helps us to:

  • detect patterns and trends in data,
  • choose useful features for machine learning,
  • and make rough forecasts.

Key ideas

  • Strength: how tightly the points follow a pattern. Larger absolute value = stronger link.
  • Direction: the sign of the coefficient - positive, negative or zero.
  • Range: the correlation coefficient always lies between -1 and +1.
r valueMeaning
+1perfect positive correlation
0no linear correlation
-1perfect negative correlation

Types of correlation

Positive correlation - both variables move in the same direction. Example: height and weight - taller people tend to weigh more.

Negative correlation - variables move in opposite directions. Example: price and demand - when the price of a product rises, demand tends to fall.

Zero correlation - no relationship. Example: shoe size and intelligence.

import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(0)
x = rng.normal(size=200)

fig, ax = plt.subplots(1, 3, figsize=(14, 4))
ax[0].scatter(x, x + rng.normal(scale=0.4, size=200)); ax[0].set_title("Positive")
ax[1].scatter(x, -x + rng.normal(scale=0.4, size=200)); ax[1].set_title("Negative")
ax[2].scatter(x, rng.normal(size=200)); ax[2].set_title("Zero")
plt.show()

Correlation is NOT causation

Two variables can be correlated because:

  1. A causes B (smoking and lung disease),
  2. B causes A (reverse causation),
  3. A third variable C causes both (heat causes both ice-cream sales and swimming accidents - a confounder), or
  4. pure coincidence (spurious correlations, especially in small data).

Only carefully designed experiments (such as randomised controlled trials) can establish causation.

Correlation only sees straight lines

If y = x squared, the relationship is perfect, but Pearson's r for symmetric x values is about 0 because the pattern is a curve, not a line. Always plot the data - do not trust r alone.

x = np.linspace(-3, 3, 200)
y = x ** 2
print(np.corrcoef(x, y)[0, 1])   # about 0.0 although y depends completely on x
What is Correlation Analysis? | Exploratory Data Analysis | DevNotes