utilities.winsorize()

Winsorize a numeric vector at symmetric quantiles.

Usage

Source

utilities.winsorize(
    x,
    cut,
)

Replaces values below the lower ‘cut’ quantile and above the upper ‘1 - cut’ quantile with the corresponding quantile boundaries. The length of the vector is preserved.

Parameters

x: numpy.ndarray or polars.Series

A numeric vector to winsorize. Inputs that are not already arrays are coerced via ‘numpy.asarray’.

cut: float
Proportion of observations replaced at each tail. For example, ‘cut=0.05’ clips the lowest and highest five percent. Must lie in ‘[0, 0.5]’.

Returns

numpy.ndarray
Vector with the same length as ‘x’ in which extreme values have been replaced by the lower and upper quantile cutoffs.

Examples

import numpy as np
from tidyfinance import winsorize
rng = np.random.default_rng(123)
data = rng.standard_normal(100)
winsorized = winsorize(data, 0.05)