What is ewm() Method in Pandas?

Topics Covered

The Pandas ewm() function is a type of moving average to calculate the exponentially weighted moving average for a certain number of previous periods. It is a statistical measure used to represent or describe a time series. Older values are given less weight here, whereas more recent values are given greater weight.

An exponentially weighted moving average (EWMA) is a type of moving average (MA) that gives more weight to recent observations and less weight to observations from the distant past. This type of moving average is commonly used in finance and economics to smooth out short-term fluctuations and to highlight longer-term trends or cycles.

The formula for the weighted moving average is as follows:

WMAt=i=1nwixti+1i=1nwiWMA_t = \frac{\sum_{i=1}^n w_i \cdot x_{t-i+1}}{\sum_{i=1}^n w_i}

Where,

WMAtWMA_t = Weighted moving average at time tt xtx_t = Value at time tt wiw_i = Weight assigned to each value nn = Number of values used to calculate the moving average

The weights should be chosen such that they sum to 1, and they determine how much each value is weighted in the average. For example, if the weights are 0.3, 0.2, 0.1, and 0.4, then the most recent value will have the highest weight (0.4) and the oldest value will have the lowest weight (0.1) in the calculation of the moving average.

The Pandas ewm() functions can be used with both Series and DataFrame. Let's first look at how to use ewm() function with the pandas Series.

a. Series

Let's first understand what is series. So, the Pandas Series is a one-dimensional data structure that can hold data of any type, like integers, floats, strings, Python objects, etc. The Pandas series can be created using existing datasets like CSV files, excel files, etc. It can also be created using a Python list, dictionary, etc. In python pandas.series() is the constructor that is used for creating series.

Now let's see how to use Pandas ewm() function with pandas series.

Syntax

The syntax of Pandas ewm series functions as follows:

Parameters

Optional Parameters:

  • com:
    • It is an optional parameter and can be float.
    • Decay in terms of center of mass i.e. α=1/(1+com) is represented by this for com ≥0 .
  • The Decay is the Constant Value used to compute this reduction in the weightage of the values.
  • com represents the decay in terms of the center of mass.
  • span:
    • It is an optional parameter and can be float.
    • Decay in terms of span i.e. α=2/(span+1),is represented by this, where span≥1.
  • halflife:
    • It is an optional parameter and can be float.
    • α=1−exp (log(0.5)/halflife), represents the decay in halflife, where for halflife>0.
  • alpha:
    • It can float.
    • This is the smoothing factor α, 0<α≤1.

Required Parameters:

  • min_periods:

    • It can be int and its default value is 0.
    • This indicates the minimum number of observations required to have a value in the window.
  • adjust:

    • It can be bool and its default value is True.
    • To account for the imbalance in the relative weightings, this is divided by the decaying adjustment factor into the starting periods.
  • ignore_na:

    • It can be bool and its default value is False.
    • This specifies the missing data should be ignored when determining weights. False is the default setting.
  • axis:

    • It can be {0 or ‘index’, 1 or ‘columns’} and Default Value: 0.
    • The axis on which the function is to be carried out is specified. If the value is 0, the operation is performed across all rows. If not, the process is performed throughout the columns. The default value is 0.

Returns

  • It returns the Series.
  • A exponentially weighted Window sub-classed for the particular operation.

Example: Use the Series.ewm() Function in Pandas

Code#1:

Output:

Explanation: In the above code example pandas is imported as pd and Dictionary data is used to create Pandas series df. By setting the com parameter 0.2, we use the .ewm() function to calculate the exponential weight of the series components.

Code#2:

Output:

Explanation: Here, we set the com parameter as 0.2 and adjust the parameter as True to calculate the exponential weight of the series components using the .emw() function.

Code#3:

Output:

Explanation: In this Code example, we set the alpha parameter i.e smoothing factor of the .ewm() function as 2/3 to calculate the exponential weight of each element of the series.

Code#4:

Output:

Explanation: Here, In the above example to ignore the missing values in data, we set the ignore_na parameter as True and also the .com parameter as 0.2 to calculate the exponential weight of each element of the series.

b. DataFrame

A Pandas DataFrame is a two-dimensional, size mutable data structure, consisting of labeled axes, i.e., rows and columns. Each column is a pandas series. Pandas DataFrame can be created using existing datasets like CSV files, excel files, etc. It can also be created from python lists, dictionaries, etc. Pandas DataFrame can be created using the pandas.DataFrame() constructor.

Now let's see how the pandas ewm() function can be used with pandas DataFrame.

Syntax

The syntax of Pandas ewm DataFrame function is as follows:

Parameters

Optional Parameters:

  • com:
    • It is an optional parameter and can be float.
    • Decay in terms of center of mass i.e. α=1/(1+com) is represented by this for com ≥0 .
  • span:
    • It is an optional parameter and can be float.
    • Decay in terms of span i.e. α=2/(span+1),is represented by this, where span≥1.
  • halflife:
    • It is an optional parameter and can be float.
    • α=1−exp(log(0.5)/halflife), represents the decay in halflife, where for halflife>0.
  • alpha:
    • It can float.
    • This is the smoothing factor α, 0<α≤1.

Required Parameters:

  • min_periods:

    • It can be int and its default value is 0.
    • This indicates the minimum number of observations required to have a value in the window.
  • adjust:

    • It can be bool and its default value is True.
    • To account for the imbalance in the relative weightings, this is divided by the decaying adjustment factor into the starting periods.
  • ignore_na:

    • It can be bool and its default value is False.
    • This specifies the missing data should be ignored when determining weights. False is the default setting.
  • axis:

    • It can be {0 or ‘index’, 1 or ‘columns’} and Default Value: 0.
    • The axis on which the function is to be carried out is specified. If the value is 0, the operation is performed across all rows. If not, the process is performed throughout the columns. The default value is 0.

Returns

  • It returns the DataFrame.
  • A exponentially weighted Window sub-classed for the particular operation.

Example: Use the DataFrame.ewm() Function in Pandas

Code#1:

Output:

Explanation: In the above code example pandas is imported as pd and Dictionary data of calories and duration is created and used to create Pandas DataFramedf. By setting the .com parameter 0.2, we use the .ewm() function to calculate the exponential weight of the elements of DataFrame df.

Code#2:

Output:

Explanation: In the above example, we calculated the exponential weight of the elements of DataFrame df by setting the alpha parameter as 3/4.

Code#3:

Output:

Explanation: In this example, we calculated the exponential weight of the elements of DataFrame df by setting the com parameter as 0.2 and the adjust parameter as True.

Code#4:

Output:

Explanation: Here, we set the adjust parameter as False and calculate the exponential weight of elements of the DataFrame.

Code#5:

Output:

Explanation: In this code example, we set the ignore_na parameter as True to ignore the missing values in the DataFrame and calculated the exponential weight of elements of the DataFrame.

Conclusion

  • The pandas ewm function is used to calculate the exponentially weighted calculations.
  • The Decay is the Constant Value that is used to compute this reduction in the weightage of the values.
  • We can use the pandas ewm function with pandas series and DataFrame.
  • This function returns the exponential weights of provided series or DataFrame according to the particular operation performed on it.
  • We can ignore the missing values by using the ignore_na parameter of the pandas ewm function.