Gold Price Prediction with Advanced Time Series Approaches by R แบบ งงๆ(Part4: TBATS)

Tour Watcharachai
2 min readSep 27, 2020

--

กลับมาอีกครั้งสำหรับโมเดลสุดท้ายนะครับ นั้นก็คือ TBATS Model (Trigonometric seasonality, Box-Cox transformation, ARMA errors, Trend and Seasonal components) โดย Approach นี้ถูกพัฒนาขึ้นโดย Alysha M. De Livera, Rob J. Hyndman & Ralph D. Snyder (2011)

ก่อนที่จะเริ่ม Coding ขอบอกก่อนว่าหลักการทำงานของ TBATS Model ถูกพัฒนามาจากหลายๆ State space models เช่น Additive Holt-Winter, Box-cox transformation, ARMA i.e. ดังนั้นข้อดีของเค้าก็คือ เค้าเข้ามาช่วยแก้ปัญหา Overpameting ของพวก Smoothing Methods และยังช่วยแก้ปัญหา unobserved pattern ที่ยุ่งยากที่เกิดจากจำนวน State space สูง ด้วย Trigonometric Function

สำหรับผู้ที่สนใจเบื้องหลังของ Logic แนะนำสามารถอ่านได้จาก Paper นี้เลยนะครับ

Coding with R!!

ครั้งนี้ผมต้องขอเปลี่ยนมาใช้ R ก่อนนะครับเนื่องจาก Import Library tbats ลง conda แล้วมีปัญหา

#Library for analysis data
install.packages('forecast', dependencies = TRUE)
library(forecast)
#Import Data
Gold_price <- read.csv(file = 'Gold15-18.csv')
head(Gold_price)
Gold_price
#DataFrame
Gold_price_frame <- data.frame(Date=Gold_price[1], Data = Gold_price[2])
Gold_price_frame[2]
#Since we know that data contains a weekly seasonality and a semi-annual seasonality
#We again do a decomposition analysis
Gold_price_de <- ts(Gold_price_frame[2], start=c(2015,1,02), frequency = 220) #transform to time series object by annual data
plot(Gold_price_de)
Decom_gold<-decompose(Gold_price_de, type = "additive", filter = NULL)
plot(Decom_gold)

ก่อนอื่นเรา Import data(CSV type) ลงมาแล้วเอา Data มาใส่ใน Frame ก่อนที่จะนำมาทำการแยก Decomposition แบบ Additive เนื่องจากต้องการ recheck seasonality pattern และในการทำ Model เราทดลองปรับการใช้ Box-cox transformation ขึ้นมาเพื่อเปรียบเทียบผลที่เกิดขึ้น

#Seperate train and test dataset
tt_v <-sample(1:nrow(Gold_price_de), 0.8 * nrow(Gold_price_de))
train_sample <- Gold_price_de[tt_v,]
test_sample <- Gold_price_de[-tt_v,]
#Tbats fit model No box cox
fit_tbats <- tbats(train_sample,use.box.cox = FALSE)
plot(fit_tbats)
forecast(fit_tbats)
plot(forecast(fit_tbats))
accuracy(fit_tbats)
#Tbats fit model2 with Box Cox
model_tbats <- tbats(train_sample)
model_tbats
plot(model_tbats)
forecast(model_tbats)
plot(forecast(model_tbats))
accuracy(model_tbats)

สำหรับการปรับแต่งโมเดล Library นี้ปรับแต่งให้เองโดยใช้ AIC Criteria ที่ต่ำที่สุดในการเลือก

Result!!

ผลลัพท์ที่ได้จาก model นั้น ถือว่าค่อนข้างผิดหวังเลยครับ แม้ว่า Accuracy ของ Result จะต่ำ แต่เมื่อพิจารณาความสามารถในการทำนายและ Prediction Bound นั้นไม่สามารสู้กับ ANN หรือ ARIMA ได้

Parametor of Box-Cox transformation is 1, 0 orders of ARMA, weekly and annually seasonal adjustment

ทั้งนี้เราไม่ได้ความว่า TBATS เป็นโมเดลที่ไม่ดีนะครับ เพราะหลาย Research นิยมใช้ TBATS ในการทำนายมากกว่า ANN อีก เนื่องจากหลายงานที่เรารีวิวมามีการปรับ Features ให้ดีขึ้นและปรับแต่ง Model ด้วย (งานผมยังไม่ได้ลอง Adjusted ค่าต่างๆเลยย ฮ่าๆ ลองไปทำดูกันได้นะครับ)

Summary!!

Conclusion งานครั้งนี้เราพบประเด็นสำคัญหลักๆที่น่าสนใจ

  • ANN is the winner!! ในทุกกลุ่มข้อมูล
  • ข้อมูลที่มีการกระจายตัวตาม White noise, SARIMA ก็สามารถนำมาใช้คำนวณได้แต่ไม่สามารถจัดการกับปัญหา Multiple seasonality ได้
  • ข้อมูลที่มี Trend เข้ามาเกี่ยวข้อง การใส่ Trend component เข้าไปควรทำอย่างระมัดระวัง เนื่องจากจะทำให้ค่าพยากรณ์เบี่ยงเบนได้
  • การปรับแต่ง Model นั้นสามารถแก้ไขได้ในบางกรณี และต้องปรับแต่งอย่างละเอียด เช่น การใช้ Wavelet จริงๆสามารถพัฒนา ANN ให้มีประสิทธิภาพมากขึ้นได้ จากการ technique ในการคำนวณ Wavelet

ขอบคุณสำหรับผู้อ่านทุกคนที่ติดตามกันมานะครับ จริงๆงานนี้ยังไม่ใช่งานที่ดีที่สุดเพราะยังมีหลายส่วนให้ปรับแต่งได้อีกเยอะ ที่ตั้งใจเขียนงานนี้ขึ้นมาก็เพราะว่าอยากให้ผู้ที่สนใจด้านพยากรณ์ Financial Asset หันมาลอง Time Series Analysis กัน ไว้พบกันครั้งหน้าจะพยายามแชร์งานที่ Practical มากขึ้นนะครับ

Thank you for reading!! See you in a week ^^

--

--