MediumFeature Engineering

Winsorization

Feature Engineering · Data Processing

Medium

Problem

Winsorization limits extreme observations without removing them. Compute lower and upper percentile bounds by linearly interpolating within the sorted values, then clip every original value to those bounds.

For a percentile p and n sorted values, compute its fractional index:

k = \frac{(n-1)p}{100}

Interpolate between the surrounding sorted entries:

q_p = a_{\lfloor k \rfloor} + (k-\lfloor k \rfloor)(a_{\lceil k \rceil}-a_{\lfloor k \rfloor})

Here, a is the sorted copy of values and q_p is the percentile bound. Clip values below the lower bound upward and values above the upper bound downward. Return the clipped values in their original order.

Theory

Winsorization is a technique that limits extreme values by capping them at specified percentiles. Rather than removing outliers, winsorization replaces them with the nearest non-extreme value. Named after biostatistician Charles P. Winsor, this approach preserves sample size while reducing the influence of extreme observations.


Why Winsorize?

Preserve sample size: Unlike trimming (which removes data), winsorization keeps all observations, maintaining statistical power.

Reduce outlier influence: Extreme values are capped, preventing them from dominating means, variances, and model parameters.

Maintain data structure: The number of data points remains unchanged; only extreme values are modified.

Robust statistics: Winsorized means and variances are more resistant to outliers than standard statistics.


The Winsorization Process

For winsorization at the p-th percentile on both tails:

Step 1: Compute the lower bound (p-th percentile) and upper bound ((100-p)-th percentile)

Step 2: Replace values below the lower bound with the lower bound

Step 3: Replace values above the upper bound with the upper bound

Mathematically:

x_{winsorized} = \begin{cases} L & \text{if } x < L \\ x & \text{if } L \leq x \leq U \\ U & \text{if } x > U \end{cases}

Where:


Common Winsorization Levels

5% winsorization:

1% winsorization:

10% winsorization:


Worked Example

Data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 100]

Note: 100 is an extreme outlier.

10% winsorization (lower 10% and upper 10%):

Step 1 - Compute percentiles:

Step 2 - Apply caps:

Winsorized data: [1.9, 2, 3, 4, 5, 6, 7, 8, 9, 18.1]

Effect on mean:


Asymmetric Winsorization

Different percentiles for lower and upper bounds:

Example: 0% lower, 5% upper

Use case: When outliers are expected only in one direction (e.g., response times can be extremely high but not negative)


Winsorization vs Trimming

Winsorization (capping):

Trimming (truncation):

Example with 10% on each tail, n=100:


Winsorized Mean

The mean of winsorized data:

\bar{x}_w = \frac{1}{n} \sum_{i=1}^{n} x_{i,winsorized}

Properties:


Winsorized Standard Deviation

Applied to winsorized data:

s_w = \sqrt{\frac{1}{n-1} \sum_{i=1}^{n} (x_{i,winsorized} - \bar{x}_w)^2}

Note: Some formulations adjust degrees of freedom for winsorization. The standard formula treats winsorized values as real observations.


Choosing Winsorization Level

Factors to consider:

Common choices:

Too aggressive: May distort the true distribution Too conservative: May not adequately address outliers


Column-wise Application

For multi-feature datasets, winsorize each column independently:

Process:

  1. For each column, compute percentile bounds
  2. Apply winsorization using column-specific bounds
  3. Different columns may have different bound values

Result: Each feature is winsorized according to its own distribution


Relationship to IQR-Based Methods

IQR outlier detection:

Percentile-based winsorization:

Equivalence: For normal distributions, 1.5 × IQR roughly corresponds to certain percentiles, but the methods are distinct.


Order Statistics Perspective

Winsorization replaces extreme order statistics:

For sorted data x_{(1)} \leq x_{(2)} \leq ... \leq x_{(n)}:

k-winsorization (k values on each tail):

This is equivalent to percentile-based winsorization with p = 100k/n.


Numerical Considerations

Percentile calculation: Various interpolation methods exist (linear, lower, higher, nearest). Choice affects boundary values slightly.

Ties at boundaries: If many values equal the boundary, they remain unchanged.

Empty tails: If percentile falls within repeated values, cap may not change any data.


Where Winsorization Shows Up

Examples

Example 1

Input
values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], lower_pct = 10, upper_pct = 90
Output
[1.9, 2, 3, 4, 5, 6, 7, 8, 9, 9.1]
Explanation
The interpolated bounds are 1.9 and 9.1, so only the first and last values are clipped.

Example 2

Input
values = [1, 2, 3, 4, 5], lower_pct = 0, upper_pct = 100
Output
[1, 2, 3, 4, 5]

Hints

  1. Use (len(values) - 1) * percentile / 100 for the fractional sorted index.
  2. Clip each original value with max(lower_bound, min(upper_bound, value)).

Requirements

Constraints

Starter Code

def winsorize(values: list, lower_pct: float, upper_pct: float) -> list:
    """
    Returns values clipped to the interpolated percentile bounds.
    """
    # Write code here
    pass

Test Cases

CaseMatches
basicpublic
no clippublic