← Machine Learning

Box-Cox Transformation

In linear regression, transforming a variable YY can help with:

  • Non-linearity.
  • Non-normality of the errors.
  • Non-constant variance of the errors.

However, it is often difficult to determine from residual diagnostic plots which transformation of YY is most appropriate.
The Box-Cox procedure automatically identifies a transformation from the family of power transformations on YY.

Consider the family of regression models:

Yi(λ)=β0+β1xi1+β2xi2+⋯+βp−1xi,p−1+ϵi,i=1,…,nY_i^{(\lambda)} = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \cdots + \beta_{p-1} x_{i,p-1} + \epsilon_i, \quad i = 1, \ldots, n ϵi∼iidN(0,σ2)\epsilon_i \overset{iid}{\sim} N(0, \sigma^2)
  • The Box-Cox procedure automatically identifies λ\lambda.

Note that this family encompasses the following simple transformations:

λ=2Y′=Y2λ=0.5Y′=Yλ=0Y′=log⁡(Y)λ=−0.5Y′=1Yλ=−1Y′=1Y\begin{aligned} \lambda &= 2 \quad & Y' &= Y^2 \\ \lambda &= 0.5 \quad & Y' &= \sqrt{Y} \\ \lambda &= 0 \quad & Y' &= \log(Y) \\ \lambda &= -0.5 \quad & Y' &= \frac{1}{\sqrt{Y}} \\ \lambda &= -1 \quad & Y' &= \frac{1}{Y} \end{aligned}

Note: λ\lambda is a parameter and must be estimated.
qquad\\qquad e.g. if λ^=2.173→\hat{\lambda} = 2.173 \rightarrow then choose λ=2\lambda = 2
Note: Estimation of λ\lambda must be done numerically, i.e., via maximum likelihood estimation.

# For single variable import numpy as np import matplotlib.pyplot as plt from scipy.stats import boxcox # Example data (All y value must be positive) y = np.random.exponential(scale=2, size=100) # Box-Cox transformation y_transformed, lambda_opt = boxcox(y) print(f"Optimal lambda: {lambda_opt:.4f}") # Visualize plt.figure(figsize=(10, 4)) plt.subplot(1, 2, 1) plt.hist(y, bins=20) plt.title("Original Data") plt.subplot(1, 2, 2) plt.hist(y_transformed, bins=20) plt.title("Box-Cox Transformed Data") plt.show()
Optimal lambda: 0.3251
boxcox1
# For multiple variables import numpy as np import pandas as pd from sklearn.preprocessing import PowerTransformer import matplotlib.pyplot as plt import seaborn as sns # Example data: 3 variables (All value must be positive) np.random.seed(0) X = np.column_stack([ np.random.exponential(scale=2, size=100), # feature 1 np.random.gamma(shape=2., scale=1., size=100),# feature 2 np.random.lognormal(mean=0., sigma=0.5, size=100) # feature 3 ]) # Box-Cox transformation pt = PowerTransformer(method='box-cox', standardize=False) X_trans = pt.fit_transform(X) # Note: PowerTransformer requires 2D array input (n_samples, n_features) and all values must be positive # `standardize = False` doesn't standardize ((mean=0, std=1) the feature # If negative value is included, use 'yeo-johnson' instead. print("Estimated λ (lambda) values for each variable:", pt.lambdas_) # Visualize features = ['Feature 1', 'Feature 2', 'Feature 3'] df_original = pd.DataFrame(X, columns=features) df_transformed = pd.DataFrame(X_trans, columns=features) fig, axes = plt.subplots(3, 2, figsize=(10, 9)) for i, col in enumerate(features): sns.histplot(df_original[col], kde=True, ax=axes[i, 0]) axes[i, 0].set_title(f"Original {col}") sns.histplot(df_transformed[col], kde=True, ax=axes[i, 1]) axes[i, 1].set_title(f"Box-Cox Transformed {col}") plt.tight_layout() plt.show()
Estimated λ (lambda) values for each variable: [ 0.24399399 0.22962182 -0.18959628]
boxcox2