mlfinlab.filters.filters

Filters are used to filter events based on some kind of trigger. For example a structural break filter can be used to filter events where a structural break occurs. This event is then used to measure the return from the event to some event horizon, say a day.

Module Contents

Functions

cusum_filter(close_prices, threshold[, time_stamps])

Advances in Financial Machine Learning, Snippet 2.4, page 39.

z_score_filter(raw_time_series, mean_window, std_window)

Filter which implements z_score filter

cusum_filter(close_prices, threshold, time_stamps=True)

Advances in Financial Machine Learning, Snippet 2.4, page 39.

The Symmetric Dynamic/Fixed CUSUM Filter.

Note: As per the book this filter is applied to closing prices but we extended it to also work on other time series such as volatility.

Parameters:
  • close_prices – (pd.Series) Close prices (or other time series, e.g. volatility).

  • threshold – (float/pd.Series) When the abs(change) is larger than the threshold, the function captures it as an event, can be dynamic if threshold is pd.Series.

  • time_stamps – (bool) Default is to return a DateTimeIndex, change to false to have it return a list.

Returns:

(datetime index vector) Vector of datetimes when the events occurred. This is used later to sample.

z_score_filter(raw_time_series, mean_window, std_window, z_score=3, time_stamps=True, influence=1)

Filter which implements z_score filter (https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data)

Parameters:
  • raw_time_series – (pd.Series) Close prices (or other time series, e.g. volatility).

  • mean_window – (int): Rolling mean window.

  • std_window – (int): Rolling std window.

  • z_score – (float): Number of standard deviations to trigger the event.

  • influence – (float) The influence parameter determines to which extent the previous detected events influence the rolling mean and standard deviation. If the value is 0, the event has no influence on the rolling mean and standard deviation. At 0.5, it has a half influence of a normal datapoint. At 1, the method operates as a standard Z-Score filter. This value should be in a range of 0 to 1.

Returns:

(datetime index vector) Vector of datetimes when the events occurred. This is used later to sample.