finmlkit.sampling.filters module¶
- finmlkit.sampling.filters.cusum_filter(raw_time_series: ndarray[tuple[int, ...], dtype[float64]], threshold: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[int64]][source]¶
Apply the CUSUM filter to detect events based on the cumulative sum of log returns.
- Parameters:
raw_time_series – Array of price series.
threshold – Threshold values for event detection. - If array has 1 element, a constant threshold is used. - If multiple elements, it must be of the same length as raw_time_series.
- Returns:
Indices where events occurred. These indices correspond to positions in raw_time_series.
Note
This function implements the Symmetric CUSUM Filter, which is designed to detect a shift in the mean value of a measured quantity away from a target value. It identifies events when the cumulative sum of log returns exceeds a specified threshold.
This implementation follows the methodology outlined in:
Lopez de Prado, Marcos. “Advances in Financial Machine Learning.” Wiley, 2018. Snippet 2.4, page 39.
- finmlkit.sampling.filters.z_score_peak_filter(y: ndarray[tuple[int, ...], dtype[float64]], window: int, threshold: float = 3) ndarray[tuple[int, ...], dtype[int64]][source]¶
Implement a z-score peak detection filter.
- Parameters:
y – The input time series data of at least length window + 2.
window – The window parameter for the moving window (number of observations to use for mean and standard deviation calculations).
threshold – The z-score threshold for detecting peaks.
- Returns:
The indices of the events (peaks) in the input time series data y.
Note
This function implements a z-score based peak detection algorithm suitable for real-time data. It is optimized for performance using Numba’s JIT compilation.