<jupyter_start><jupyter_text>EWMA: Exponential Weighted Moving Average<jupyter_code>import pandas as pd
import numpy as np
%matplotlib inline

airline = pd.read_csv('../Data/airline_passengers.csv',index_col='Month',parse_dates=True)
airline['6-month-SMA'] = airline['Thousands of Passengers'].rolling(window=6).mean()
airline['12-month-SMA'] = airline['Thousands of Passengers'].rolling(window=12).mean()
airline.plot();<jupyter_output><empty_output><jupyter_text>EWMA is better than basic SMA since:   SMA's smaller windows leads to greater noise  SMA can never reach full peak or actual value  SMA can not inform about the possible future, but just describes trends  SMA can highly be skewed   In contrast, EWMA pays more weight, i.e. significance to the recent values  The formula for EWMA is: $y_t =   \frac{\sum\limits_{i=0}^t w_i x_{t-i}}{\sum\limits_{i=0}^t w_i}$  In EWMA α refers to smoothing factor\begin{split}\alpha = \begin{cases}     \frac{2}{s + 1},               & \text{for span}\ s \geq 1\\     \frac{1}{1 + c},               & \text{for center of mass}\ c \geq 0\\     1 - \exp^{\frac{\log 0.5}{h}}, & \text{for half-life}\ h > 0 \end{cases}\end{split} * Span corresponds to what is commonly called an “N-day EW moving average”.* Center of mass has a more physical interpretation and can be thought of in terms of span: $c=(s−1)/2$* Half-life is the period of time for the exponential weight to reduce to one half.* Alpha specifies the smoothing factor directly.We have to pass precisely one of the above into the .ewm() function. For our data we'll use span=12.<jupyter_code>airline['EWMA12'] = airline['Thousands of Passengers'].ewm(span=12,adjust=False).mean()
airline[['Thousands of Passengers','EWMA12']].plot();<jupyter_output><empty_output><jupyter_text>Comparing SMA to EWMA<jupyter_code>airline[['Thousands of Passengers','EWMA12','12-month-SMA']].plot(figsize=(12,8)).autoscale(axis='x',tight=True);<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup