( 참고 : Fast Campus 금융공학/퀀트 강의 )

5. TS prediction with NN

(1) Neuralnet AR (Autoregressive)

nnetar

  • input : AR 변수 ( \(y_{t-1},...,y_{t-p}\) )
  • \(p\)는 직접 지정 가능
    • 몇 step의 과거까지 input으로 사용할 지


R 코드

(1) data

library(forecast)
library(tseries)

ms_t1 = sales_s1$$total[1:1099]
ts_t1 = sales_s1$$total[1100:1127]

ms_t2 = sales_s1$$total[1:1069]
ts_t2 = sales_s1$$total[1070:1097]

ms_t3 = sales_s1$$total[1:1039]
ts_t3 = sales_s1$$total[1040:1067]

ms_t1 = ts(ms_t1, frequency = 365) # single seasonality
ms_t2 = ts(ms_t2, frequency = 365) # single seasonality
ms_t3 = ts(ms_t3, frequency = 365) # single seasonality


(2) model & prediction

t1_nnetar = nnetar(ms_t1) 
t2_nnetar = nnetar(ms_t2) 
t3_nnetar = nnetar(ms_t3) 


nnetar_fc1 = forecast(t1_nnetar, h=28) 
nnetar_fc2 = forecast(t2_nnetar, h=28)  
nnetar_fc3 = forecast(t3_nnetar, h=28)  


(2) Prophet ( feat FB )

Prophet의 특징

  1. 비선형추세 산출(Non-linear trend)
  • 시계열 Automatic Chang-Point Detection
  • 베이지안 방법(prior for trend)
  1. 일별 및 연도별 계절성 계산
  • :Fourier terms 이용하여 multi-seasonality 적용

    (daily : freq=7, yearly : freq=365.25)

  1. 휴일 효과 적용가능

  2. Saturating Forecast ( 예측치의 범위 및 limit을 설정 )


R code

library(prophet)
library(dplyr)

(1) Basic

model <- prophet(df)

future <- make_future_dataframe(model, periods = 365) 
forecast <- predict(model, future) 

#-----------------------------------------------#

plot(model, forecast)
prophet_plot_components(m, forecast)

(2) Saturating Forecast in Prophet

df$$cap <- 8.5

model <- prophet(df, growth = 'logistic')

future <- make_future_dataframe(model, periods = 1826)
future$$cap <- 8.5
forecast <- predict(model, future)

#---------------------------------------------------#
plot(m, forecast)
prophet_plot_components(model, forecast)

(3) Adding Monthly Seasonality

model <- prophet(weekly.seasonality=FALSE)
model <- add_seasonality(model, name='monthly', period=30.5, fourier.order=5)
model <- fit.prophet(model, df)

future <- make_future_dataframe(model, periods = 365)
forecast <- predict(model, future)

#---------------------------------------------------#
plot(m, fcst)
prophet_plot_components(model, forecast)

(4) Adding Holiday Effect

playoffs <- data_frame(
    holiday = 'playoff',
    ds = as.Date(c('2008-01-13', '2009-01-03', '2010-01-16',
                   '2010-01-24', '2010-02-07', '2011-01-08',
                   '2013-01-12', '2014-01-12', '2014-01-19',
                   '2014-02-02', '2015-01-11', '2016-01-17',
                   '2016-01-24', '2016-02-07')),
    lower_window = 0,
    upper_window = 1
)

superbowls <- data_frame(
    holiday = 'superbowl',
    ds = as.Date(c('2010-02-07', '2014-02-02', '2016-02-07')),
    lower_window = 0,
    upper_window = 1
)

holidays_df <- bind_rows(playoffs, superbowls)
m <- prophet(df, holidays = holidays_df)
forecast <- predict(m, future)

#---------------------------------------------------#
forecast %>%
select(ds, playoff, superbowl) %>% 
filter(abs(playoff + superbowl) > 0) %>%
tail(10)

prophet_plot_components(m, forecast)