<jupyter_start><jupyter_text>Forecasting Models : SMA, WMA, SES, ARIMA<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
%matplotlib inline
airline = pd.read_csv('data/airline_passengers.csv', index_col='Month', parse_dates=True)
airline.head()<jupyter_output><empty_output><jupyter_text>Simple Moving Average Moving Average 구하는 방법에 대해서는 앞서 rolling mean 을 통해 알아본 바 있습니다.<jupyter_code>airline['6M SMA'] = airline['Thousands of Passengers'].rolling(window=6).mean()
airline['12M SMA'] = airline['Thousands of Passengers'].rolling(window=12).mean()
airline.head()
airline.plot(figsize=(12,8))<jupyter_output><empty_output><jupyter_text>Weighted Moving Averagem<jupyter_code>airline['WMA12'] = airline['Thousands of Passengers'].ewm(span=12).mean()
airline['WMA6'] = airline['Thousands of Passengers'].ewm(span=6).mean()
airline[['Thousands of Passengers', 'WMA6','WMA12']].plot(figsize=(12,8))<jupyter_output><empty_output><jupyter_text>Simple Exponential Smoothing<jupyter_code>from statsmodels.tsa.api import SimpleExpSmoothing
train = airline[:'1959']
test = airline['1960':]
train['Thousands of Passengers'].plot(figsize=(12,8))
test['Thousands of Passengers'].plot()
ses_model = SimpleExpSmoothing(np.asarray(train['Thousands of Passengers']))
ses_result = ses_model.fit()
y_hat = test.copy()
y_hat['SES'] = ses_result.forecast(len(test))
plt.figure(figsize=(12,8))
plt.plot(train['Thousands of Passengers'], label='Train')
plt.plot(test['Thousands of Passengers'], label='Test')
plt.plot(y_hat['SES'], label='Simple Exp Smoothing')
plt.legend()
rmse = np.sqrt(mean_squared_error(test['Thousands of Passengers'], y_hat['SES']))
rmse<jupyter_output><empty_output><jupyter_text>Holt's Method<jupyter_code>from statsmodels.tsa.api import Holt
holt_model = Holt(np.asarray(train['Thousands of Passengers']))
holt_result = holt_model.fit()
y_hat['HOLT'] = holt_result.forecast(len(test))
plt.figure(figsize=(12,8))
plt.plot(train['Thousands of Passengers'], label='Train')
plt.plot(test['Thousands of Passengers'], label='Test')
plt.plot(y_hat['HOLT'], label='Holts Exp Smoothing')
plt.legend()
rmse = np.sqrt(mean_squared_error(test['Thousands of Passengers'], y_hat['HOLT']))
rmse<jupyter_output><empty_output><jupyter_text>Holt-Winters Method<jupyter_code>from statsmodels.tsa.api import ExponentialSmoothing
winter_model = ExponentialSmoothing(
    np.asarray(train['Thousands of Passengers']),
    seasonal_periods=12, trend='add', seasonal='add'
)
winter_result = winter_model.fit()
y_hat['WINTER'] = winter_result.forecast(len(test))
plt.figure(figsize=(12,8))
plt.plot(train['Thousands of Passengers'], label='Train')
plt.plot(test['Thousands of Passengers'], label='Test')
plt.plot(y_hat['WINTER'], label='Holt-Winters Exp Smoothing')
plt.legend()
rmse = np.sqrt(mean_squared_error(test['Thousands of Passengers'],y_hat['WINTER']))
rmse<jupyter_output><empty_output><jupyter_text>ARIMA<jupyter_code>import statsmodels.api as sm
# ARIMA(p,d,q)(P,D,Q)m

arima = sm.tsa.statespace.SARIMAX(train['Thousands of Passengers'],
                                  order=(2,1,1),
                                  seasonal_order=(0,1,0,12),
                                  enforce_stationarity=False,
                                  enforce_invertibility=False)
arima_result = arima.fit()
y_hat['ARIMA'] = arima_result.predict(start='1960-01-01', end='1960-12-01', dynamic=True)
plt.figure(figsize=(12,8))
plt.plot(train['Thousands of Passengers'], label='Train')
plt.plot(test['Thousands of Passengers'], label='Test')
plt.plot(y_hat['ARIMA'], label='Seasonal ARIMA')
plt.legend()
rmse = np.sqrt(mean_squared_error(test['Thousands of Passengers'],y_hat['ARIMA']))
rmse<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1574490605980418, 'fraction_numerical': 0.018523418893887273, 'mean_word_length': 7.235294117647059, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '115054', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1364, 'n_words': 215, 'path': '09. Forecasting Models.ipynb', 'repo_name': 'monsh-git/time-series-data-analysis-with-pandas', 'size': '3779'}