Skip to content

Class HullMovingFeature

  • Jump right in for a hands-on Open In Colab

Import

from NitroFE import HullMovingFeature

HullMovingFeature

The Hull Moving Average (HMA), developed by Alan Hull, is an extremely fast and smooth moving average.

In fact, the HMA almost eliminates lag altogether and manages to improve smoothing at the same time.

The Hull Moving Average is caluclated as

\[ \operatorname{WMA_(\frac{window}{2})} = Weighted \ Moving \ Average \ of \ length \ \frac{window}{2} \ over \ dataframe \]
\[ \operatorname{WMA_(window)} = Weighted \ Moving \ Average \ of \ length \ window \ over \ dataframe \]
\[ \operatorname{Raw \ Hull \ Moving \ Average} = 2*\operatorname{WMA_(\frac{window}{2})} - \operatorname{WMA_(window)} \]
\[ \operatorname{Hull \ Moving \ Average} = Weighted \ Moving \ Average \ of \ length \ \sqrt(window) over \operatorname{Raw \ Hull \ Moving \ Average} \]

Methods

Provided dataframe must be in ascending order.

__init__(self, window=4, min_periods=1, operation=None) special

Parameters:

Name Type Description Default
window int

Size of the rolling window, by default 3

4
min_periods int

Minimum number of observations in window required to have a value, by default 1

1
operation Callable

operation to perform over the weighted rolling window values, when None is passed, np.mean is used

None
Source code in nitrofe\time_based_features\moving_average_features\moving_average_features.py
def __init__(
    self, window: int = 4, min_periods: int = 1, operation: Callable = None
):
    """
    Parameters
    ----------
    window : int, optional
        Size of the rolling window, by default 3
    min_periods : int, optional
        Minimum number of observations in window required to have a value, by default 1
    operation : Callable, optional
        operation to perform over the weighted rolling window values, when None is passed, np.mean is used
    """

    self.window = window
    self.min_periods = min_periods

    operation = np.mean if operation == None else operation
    self.operation = operation

    if self.window <= 1:
        raise ValueError(f"window size less than equal to 1 not supported")

    self.window_by_two, self.window_square_root = int(
        np.ceil(self.window / 2)
    ), int(np.ceil(np.sqrt(self.window)))

fit(self, dataframe, first_fit=True)

For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False

Parameters:

Name Type Description Default
dataframe Union[pandas.core.frame.DataFrame, pandas.core.series.Series]

dataframe/series over which feature is to be constructed

required
first_fit bool

Moving features require past values for calculation. Use True, when calculating for training data (very first fit) Use False, when calculating for any subsequent testing/production data { in which case the values, which were saved during the last phase, will be utilized for calculation }, by default True

True
Source code in nitrofe\time_based_features\moving_average_features\moving_average_features.py
def fit(self, dataframe: Union[pd.DataFrame, pd.Series], first_fit: bool = True):
    """
    For your training/initial fit phase (very first fit) use fit_first=True, and for any production/test implementation pass fit_first=False

    Parameters
    ----------
    dataframe : Union[pd.DataFrame,pd.Series]
        dataframe/series over which feature is to be constructed
    first_fit : bool, optional
        Moving features require past values for calculation.
        Use True, when calculating for training data (very first fit)
        Use False, when calculating for any subsequent testing/production data { in which case the values, which
        were saved during the last phase, will be utilized for calculation }, by default True

    """
    if first_fit:
        self._window_size_weighted_moving_average_object = (
            weighted_window_features()
        )
        self._window_by_two_size_weighted_moving_average_object = (
            weighted_window_features()
        )
        self._hma_object = weighted_window_features()

    window_size_weighted_moving_average = self._window_size_weighted_moving_average_object.caluclate_weighted_moving_window_feature(
        dataframe=dataframe,
        first_fit=first_fit,
        window=self.window,
        min_periods=self.min_periods,
        operation=self.operation,
    )

    window_by_two_size_weighted_moving_average = self._window_by_two_size_weighted_moving_average_object.caluclate_weighted_moving_window_feature(
        dataframe=dataframe,
        first_fit=first_fit,
        window=self.window_by_two,
        min_periods=self.min_periods,
        operation=self.operation,
    )

    raw_hma = (
        2 * window_by_two_size_weighted_moving_average
        - window_size_weighted_moving_average
    )
    hma = self._hma_object.caluclate_weighted_moving_window_feature(
        dataframe=raw_hma,
        first_fit=first_fit,
        window=self.window_square_root,
        min_periods=self.min_periods,
        operation=self.operation,
    )

    return hma

References