Winsorization
Winsorization
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.
Percentile Interpolation
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⌋])Examples
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.
Hint 1
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)].
Hint 2
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.
Requirements
- Compute the lower and upper bounds using linear interpolation on the sorted array
- Clip values below the lower bound up to the lower bound
- Clip values above the upper bound down to the upper bound
- Return a list of floats with the same length as the input
Constraints
- values has at least 1 element
- 0 <= lower_pct <= upper_pct <= 100
- Return a list of floats
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number
Winsorization
Winsorization
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.
Percentile Interpolation
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⌋])Examples
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.
Hint 1
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)].
Hint 2
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.
Requirements
- Compute the lower and upper bounds using linear interpolation on the sorted array
- Clip values below the lower bound up to the lower bound
- Clip values above the upper bound down to the upper bound
- Return a list of floats with the same length as the input
Constraints
- values has at least 1 element
- 0 <= lower_pct <= upper_pct <= 100
- Return a list of floats
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: number
Accepts: number