Skip to content

Class TripleExponentialMovingFeature

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

Import

from NitroFE import TripleExponentialMovingFeature

TripleExponentialMovingFeature

The Triple exponential moving average (TEMA) was designed to smooth value fluctuations, thereby making it easier to identify trends without the lag associated with traditional moving averages (MA). It does this by taking multiple exponential moving averages (EMA) of the original EMA and subtracting out some of the lag.

TEMA is calculated as

\[ EMA_1 = Exponential \ moving \ average \ of \ dataframe \]
\[ EMA_2 = Exponential \ moving \ average \ of \ EMA_1 \]
\[ EMA_3 = Exponential \ moving \ average \ of \ EMA_2 \]
\[ TEMA = 3*EMA_1 - 3*EMA_2 + EMA_3 \]

Methods

Provided dataframe must be in ascending order.

__init__(self, alpha=None, operation='mean', initialize_using_operation=False, initialize_span=None, com=None, span=None, halflife=None, min_periods=0, ignore_na=False, axis=0, times=None) special

Parameters:

Name Type Description Default
alpha float

Specify smoothing factor directly, by default None

None
operation str

operation to be performed for the moving feature,available operations are 'mean','var','std', by default 'mean'

'mean'
initialize_using_operation bool

If True, then specified 'operation' is performed on the first 'initialize_span' values, and then the exponential moving average is calculated, by default False

False
initialize_span int

the span over which 'operation' would be performed for initialization, by default None

None
com float

Specify decay in terms of center of mass, by default None

None
span float

pecify decay in terms of span , by default None

None
halflife float

Specify decay in terms of half-life, by default None

None
min_periods int

Minimum number of observations in window required to have a value (otherwise result is NA)., by default 0

0
ignore_na bool

Ignore missing values when calculating weights; specify True to reproduce pre-0.15.0 behavior, by default False

False
axis int

The axis to use. The value 0 identifies the rows, and 1 identifies the columns, by default 0

0
times str

Times corresponding to the observations. Must be monotonically increasing and datetime64[ns] dtype, by default None

None
Source code in nitrofe\time_based_features\moving_average_features\moving_average_features.py
def __init__(
    self,
    alpha: float = None,
    operation: str = "mean",
    initialize_using_operation: bool = False,
    initialize_span: int = None,
    com: float = None,
    span: float = None,
    halflife: float = None,
    min_periods: int = 0,
    ignore_na: bool = False,
    axis: int = 0,
    times: str = None,
):
    """
    Parameters
    ----------
    alpha : float, optional
        Specify smoothing factor  directly, by default None
    operation : str, {'mean','var','std'}
        operation to be performed for the moving feature,available operations are 'mean','var','std', by default 'mean'
    initialize_using_operation : bool, optional
        If True, then specified 'operation' is performed on the first 'initialize_span' values, and then the exponential moving average is calculated, by default False
    initialize_span : int, optional
        the span over which 'operation' would be performed for initialization, by default None
    com : float, optional
        Specify decay in terms of center of mass, by default None
    span : float, optional
        pecify decay in terms of span , by default None
    halflife : float, optional
        Specify decay in terms of half-life, by default None
    min_periods : int, optional
        Minimum number of observations in window required to have a value (otherwise result is NA)., by default 0
    ignore_na : bool, optional
        Ignore missing values when calculating weights; specify True to reproduce pre-0.15.0 behavior, by default False
    axis : int, optional
        The axis to use. The value 0 identifies the rows, and 1 identifies the columns, by default 0
    times : str, optional
        Times corresponding to the observations. Must be monotonically increasing and datetime64[ns] dtype, by default None
    """

    self.com = com
    self.span = span
    self.halflife = halflife
    self.alpha = alpha
    self.min_periods = min_periods
    self.ignore_na = ignore_na
    self.axis = axis
    self.times = times
    self.operation = operation
    self.initialize_using_operation = initialize_using_operation
    self.initialize_span = initialize_span

fit(self, dataframe, first_fit=True)

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

Parameters:

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

dataframe containing column values to create feature over

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 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/subsequent implementation pass fit_first=False

    Parameters
    ----------
    dataframe : Union[pd.DataFrame, pd.Series]
        dataframe containing column values to create feature over
    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 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._first_exponential_average_object = ExponentialMovingFeature(
            initialize_using_operation=self.initialize_using_operation,
            initialize_span=self.initialize_span,
            com=self.com,
            span=self.span,
            halflife=self.halflife,
            alpha=self.alpha,
            min_periods=self.min_periods,
            ignore_na=self.ignore_na,
            axis=self.axis,
            times=self.times,
            operation=self.operation,
        )
        self._second_exponential_average_object = ExponentialMovingFeature(
            initialize_using_operation=self.initialize_using_operation,
            initialize_span=self.initialize_span,
            com=self.com,
            span=self.span,
            halflife=self.halflife,
            alpha=self.alpha,
            min_periods=self.min_periods,
            ignore_na=self.ignore_na,
            axis=self.axis,
            times=self.times,
            operation=self.operation,
        )
        self._third_exponential_average_object = ExponentialMovingFeature(
            initialize_using_operation=self.initialize_using_operation,
            initialize_span=self.initialize_span,
            com=self.com,
            span=self.span,
            halflife=self.halflife,
            alpha=self.alpha,
            min_periods=self.min_periods,
            ignore_na=self.ignore_na,
            axis=self.axis,
            times=self.times,
            operation=self.operation,
        )

    first_exponential_average = self._first_exponential_average_object.fit(
        dataframe=dataframe, first_fit=first_fit
    )
    second_exponential_average = self._second_exponential_average_object.fit(
        dataframe=first_exponential_average, first_fit=first_fit
    )
    third_exponential_average = self._third_exponential_average_object.fit(
        dataframe=second_exponential_average, first_fit=first_fit
    )
    triple_exponential_average = (
        3 * first_exponential_average
        - 3 * second_exponential_average
        + third_exponential_average
    )
    return triple_exponential_average

References