Python/Time Series with Python
TimeSeries with Python _ EWMA & Holt-Winters _ Python
MINSU KANG
2019. 12. 28. 13:25
이번 포스팅에서는 지난번 포스팅에서 언급한 EWMA 및 홀트-윈털스을 모형들을 파이썬으로 작성하는 방법의 대해서 언급한다
파이썬의 큰 장점이지만, 데이터를 어느정도 정제하고 그리고 모듈을 쓰는 방법만 안다면 쉽게 구현할 수 있기에, 사용하는 방법의 대해서만 포스팅 한다
df = pd.read_csv(path, index_col = 'Month', parse_dates=True)
df.dropna(inplace=True)
df.index #DatetimeIndex의 freq=None임 -> 지금 datetimeIndex가 freq가 없기에 설정을 해줌
df.index.freq= 'MS'
df.index
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
span = 12 #데이터의 freq가 월별 1년 기준이기 때문에 12로 설정
alpha = 2/(span+1) #여기서 alpha는 아래 그림 설명대로 설정해 준 것(span이 1을 넘기 때문에)
df['EWMA'] = df['Thousands of Passengers'].ewm(alpha = alpha, adjust=False).mean()
df['SES12'] = SimpleExpSmoothing(df['Thousands of Passengers']).fit(smoothing_level=alpha,optimized=False).fittedvalues.shift(-1)
df.head() #여기서 참고로 'EWMA'와 'SES'로 구한 결과값은 같다
#%% Double Exponential Smoothing
from statsmodels.tsa.holtwinters import ExponentialSmoothing
df['DESadd12'] = ExponentialSmoothing(df['Thousands of Passengers'], trend='add').fit().fittedvalues.shift(-1)
#여기서 Trend변수에 'add'를 설정할 경우 트랜드가 직선으로 증가 혹은 감소한다는 것을 나타냄
df['DESmul12'] = ExponentialSmoothing(df['Thousands of Passengers'], trend='mul').fit().fittedvalues.shift(-1)
#여기서 Trend변수에 'mul'를 설정할 경우 트랜드가 지수적으로 증가 혹은 감소한다는 것을 나타냄
df[['Thousands of Passengers', 'SES12', 'DESadd12','DESmul12']].plot(figsize=(12,6)).autoscale(axis='x',tight=True)
df[['Thousands of Passengers', 'SES12', 'DESadd12','DESmul12']].iloc[:24].plot(figsize=(12,6)).autoscale(axis='x',tight=True)
#%% Triple Exponential Smoothing
df['TESadd12'] = ExponentialSmoothing(df['Thousands of Passengers'],trend='add',seasonal='add',seasonal_periods=12).fit().fittedvalues
df['TESmul12'] = ExponentialSmoothing(df['Thousands of Passengers'],trend='mul',seasonal='mul',seasonal_periods=12).fit().fittedvalues
df[['Thousands of Passengers','TESadd12','TESmul12']].iloc[:24].plot(figsize=(12,6)).autoscale(axis='x',tight=True)