| name | Correlation Analysis |
| description | Measure relationships between variables using correlation coefficients, correlation matrices, and association tests for correlation measurement, relationship analysis, and multicollinearity detection |
Correlation Analysis
Overview
Correlation analysis measures the strength and direction of relationships between variables, helping identify which features are related and detect multicollinearity.
When to Use
- Identifying relationships between numerical variables
- Detecting multicollinearity before regression modeling
- Exploratory data analysis to understand feature dependencies
- Feature selection and dimensionality reduction
- Validating assumptions about variable relationships
- Comparing linear and non-linear associations
Correlation Types
- Pearson: Linear correlation (continuous variables)
- Spearman: Rank-based correlation (ordinal/non-linear)
- Kendall: Rank correlation (robust alternative)
- Cramér's V: Association for categorical variables
- Mutual Information: Non-linear dependencies
Key Concepts
- Correlation Coefficient: Ranges from -1 to +1
- Positive Correlation: Variables move together
- Negative Correlation: Variables move oppositely
- Multicollinearity: High correlations between predictors
Implementation with Python
import pandas as pd
import numpy as np
import matplotlib.pyplot plt
seaborn sns
scipy.stats pearsonr, spearmanr, kendalltau
np.random.seed()
n =
age = np.random.uniform(, , n)
income = age * + np.random.normal(, , n)
education_years = age / + np.random.normal(, , n)
satisfaction = income / + np.random.normal(, , n)
df = pd.DataFrame({
: age,
: income,
: education_years,
: satisfaction,
: age - education_years -
})
corr_matrix = df.corr(method=)
()
(corr_matrix)
corr_coef, p_value = pearsonr(df[], df[])
()
spearman_matrix = df.corr(method=)
()
(spearman_matrix)
spearman_coef, p_value = spearmanr(df[], df[])
()
kendall_coef, p_value = kendalltau(df[], df[])
()
fig, axes = plt.subplots(, , figsize=(, ))
sns.heatmap(corr_matrix, annot=, cmap=, center=,
square=, ax=axes[], vmin=-, vmax=)
axes[].set_title()
sns.heatmap(spearman_matrix, annot=, cmap=, center=,
square=, ax=axes[], vmin=-, vmax=)
axes[].set_title()
plt.tight_layout()
plt.show()
():
rows, cols = [], []
col1 df.columns:
col2 df.columns:
col1 < col2:
r, p = pearsonr(df[col1], df[col2])
rows.append({
: col1,
: col2,
: r,
: p,
: p <
})
pd.DataFrame(rows)
corr_table = correlation_with_pvalue(df)
()
(corr_table)
fig, axes = plt.subplots(, , figsize=(, ))
pairs = [(, ), (, ),
(, ), (, )]
idx, (var1, var2) (pairs):
ax = axes[idx // , idx % ]
ax.scatter(df[var1], df[var2], alpha=)
z = np.polyfit(df[var1], df[var2], )
p = np.poly1d(z)
x_line = np.linspace(df[var1].(), df[var1].(), )
ax.plot(x_line, p(x_line), , linewidth=)
r, p_val = pearsonr(df[var1], df[var2])
ax.set_title()
ax.set_xlabel(var1)
ax.set_ylabel(var2)
ax.grid(, alpha=)
plt.tight_layout()
plt.show()
statsmodels.stats.outliers_influence variance_inflation_factor
X = df[[, , ]]
vif_data = pd.DataFrame()
vif_data[] = X.columns
vif_data[] = [variance_inflation_factor(X.values, i) i (X.shape[])]
()
(vif_data)
()
()
():
scipy.stats linregress
x_residuals = df[x] - np.poly1d(
np.polyfit(df[control_vars].values, df[x], deg=)
)(df[control_vars].values)
y_residuals = df[y] - np.poly1d(
np.polyfit(df[control_vars].values, df[y], deg=)
)(df[control_vars].values)
pearsonr(x_residuals, y_residuals)[]
partial_corr = partial_correlation(df, , , [])
()
:
dcor distance_correlation
dist_corr = distance_correlation(df[], df[])
()
ImportError:
()
fig, ax = plt.subplots(figsize=(, ))
rolling_corr = df[].rolling(window=).corr(df[])
ax.plot(rolling_corr.index, rolling_corr.values)
ax.set_title()
ax.set_ylabel()
ax.grid(, alpha=)
plt.show()