Winsorization is a technique for handling outliers by capping extreme values at specified percentiles rather than removing them. Unlike trimming (which deletes outliers), winsorization replaces them with the boundary values, preserving the dataset size. This is important when the number of data points matters (e.g., for variance estimation or when each observation corresponds to a unique entity).
Given a list of values, a lower percentile, and an upper percentile, clip values below the lower percentile to the lower bound and values above the upper percentile to the upper bound. Use linear interpolation for percentile computation.
For a sorted array of length n, the percentile at p% corresponds to index k=(n−1)⋅p/100. Interpolate between the floor and ceiling indices:
value=arr[⌊k⌋]+(k−⌊k⌋)⋅(arr[⌈k⌉]−arr[⌊k⌋])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]
10th percentile = 1.9, 90th percentile = 9.1. Value 1 is below 1.9 so it is capped to 1.9. Value 10 is above 9.1 so it is capped to 9.1. All other values are unchanged.
Input:
values = [1, 2, 3, 4, 5], lower_pct = 0, upper_pct = 100
Output:
[1, 2, 3, 4, 5]
0th percentile = minimum, 100th percentile = maximum. No clipping occurs.
First sort the values to compute percentiles. Use the formula k = (n-1) * p / 100 to find the interpolation index. Then linearly interpolate between arr[floor(k)] and arr[ceil(k)].
After computing the lower and upper bounds, clip each original value: result[i] = max(lower, min(upper, values[i])). Remember to preserve the original order.
Sign in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number
Winsorization is a technique for handling outliers by capping extreme values at specified percentiles rather than removing them. Unlike trimming (which deletes outliers), winsorization replaces them with the boundary values, preserving the dataset size. This is important when the number of data points matters (e.g., for variance estimation or when each observation corresponds to a unique entity).
Given a list of values, a lower percentile, and an upper percentile, clip values below the lower percentile to the lower bound and values above the upper percentile to the upper bound. Use linear interpolation for percentile computation.
For a sorted array of length n, the percentile at p% corresponds to index k=(n−1)⋅p/100. Interpolate between the floor and ceiling indices:
value=arr[⌊k⌋]+(k−⌊k⌋)⋅(arr[⌈k⌉]−arr[⌊k⌋])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]
10th percentile = 1.9, 90th percentile = 9.1. Value 1 is below 1.9 so it is capped to 1.9. Value 10 is above 9.1 so it is capped to 9.1. All other values are unchanged.
Input:
values = [1, 2, 3, 4, 5], lower_pct = 0, upper_pct = 100
Output:
[1, 2, 3, 4, 5]
0th percentile = minimum, 100th percentile = maximum. No clipping occurs.
First sort the values to compute percentiles. Use the formula k = (n-1) * p / 100 to find the interpolation index. Then linearly interpolate between arr[floor(k)] and arr[ceil(k)].
After computing the lower and upper bounds, clip each original value: result[i] = max(lower, min(upper, values[i])). Remember to preserve the original order.
Sign in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number