library(ChangeAnomalyDetection)
library(ggplot2)
library(RFinanceYJ)
library(reshape2)
library(forecast)
library(xtable)
stock <- quoteStockXtsData("2432.t", since = "2011-01-01")
stock <- as.data.frame(stock)
stock$date <- as.POSIXct(rownames(stock))
stock.tail <- tail(stock, n = 20)
stock.tail$date <- as.character(stock.tail$date)
print(xtable::xtable(stock.tail), type = "html")
| Open | High | Low | Close | Volume | AdjClose | date | |
|---|---|---|---|---|---|---|---|
| 2013-01-08 | 3030.00 | 3150.00 | 3025.00 | 3100.00 | 5259900.00 | 3100.00 | 2013-01-08 |
| 2013-01-09 | 3105.00 | 3165.00 | 3050.00 | 3115.00 | 3265100.00 | 3115.00 | 2013-01-09 |
| 2013-01-10 | 3135.00 | 3135.00 | 3020.00 | 3060.00 | 2927300.00 | 3060.00 | 2013-01-10 |
| 2013-01-11 | 3085.00 | 3230.00 | 3060.00 | 3215.00 | 4721300.00 | 3215.00 | 2013-01-11 |
| 2013-01-15 | 3250.00 | 3430.00 | 3245.00 | 3410.00 | 6931200.00 | 3410.00 | 2013-01-15 |
| 2013-01-16 | 3285.00 | 3325.00 | 3210.00 | 3255.00 | 4985300.00 | 3255.00 | 2013-01-16 |
| 2013-01-17 | 3255.00 | 3315.00 | 3130.00 | 3170.00 | 4311300.00 | 3170.00 | 2013-01-17 |
| 2013-01-18 | 3190.00 | 3245.00 | 3155.00 | 3190.00 | 4032900.00 | 3190.00 | 2013-01-18 |
| 2013-01-21 | 3210.00 | 3230.00 | 3105.00 | 3120.00 | 2895500.00 | 3120.00 | 2013-01-21 |
| 2013-01-22 | 3120.00 | 3150.00 | 3100.00 | 3140.00 | 1698800.00 | 3140.00 | 2013-01-22 |
| 2013-01-23 | 3155.00 | 3205.00 | 3025.00 | 3050.00 | 3664600.00 | 3050.00 | 2013-01-23 |
| 2013-01-24 | 3030.00 | 3045.00 | 2890.00 | 2903.00 | 9152000.00 | 2903.00 | 2013-01-24 |
| 2013-01-25 | 2884.00 | 2972.00 | 2831.00 | 2896.00 | 6687600.00 | 2896.00 | 2013-01-25 |
| 2013-01-28 | 2905.00 | 2957.00 | 2839.00 | 2840.00 | 6355300.00 | 2840.00 | 2013-01-28 |
| 2013-01-29 | 2852.00 | 2890.00 | 2820.00 | 2829.00 | 4316300.00 | 2829.00 | 2013-01-29 |
| 2013-01-30 | 2833.00 | 2919.00 | 2833.00 | 2917.00 | 5678800.00 | 2917.00 | 2013-01-30 |
| 2013-01-31 | 2920.00 | 2930.00 | 2845.00 | 2892.00 | 5913600.00 | 2892.00 | 2013-01-31 |
| 2013-02-01 | 2901.00 | 2929.00 | 2847.00 | 2856.00 | 4342300.00 | 2856.00 | 2013-02-01 |
| 2013-02-04 | 2889.00 | 2903.00 | 2854.00 | 2875.00 | 4431300.00 | 2875.00 | 2013-02-04 |
| 2013-02-05 | 2876.00 | 2877.00 | 2834.00 | 2835.00 | 3888200.00 | 2835.00 | 2013-02-05 |
ggplot(stock, aes(x = date, y = Close, col = "Stock")) +
geom_line() + geom_point() + xlab("")
fit <- auto.arima(stock$Close[1:30])
summary(fit)
## Series: stock$Close[1:30]
## ARIMA(2,0,2) with non-zero mean
##
## Coefficients:
## ar1 ar2 ma1 ma2 intercept
## 1.623 -0.921 -1.203 0.573 2936.20
## s.e. 0.084 0.098 0.283 0.352 11.97
##
## sigma^2 estimated as 2527: log likelihood=-160.9
## AIC=333.9 AICc=337.5 BIC=342.3
##
## In-sample error measures:
## ME RMSE MAE MPE MAPE
## -1.11566 50.27022 40.17162 -0.07063 1.37003
change.score <- changeAnomalyDetection(x = stock$Close,
term = 30, order = c(2, 0, 2))
stock$change.score <- change.score
stock.tail <- tail(stock, n = 20)[, c("date",
"Close", "change.score")]
stock.tail$date <- as.character(stock.tail$date)
print(xtable::xtable(stock.tail), type = "html")
| date | Close | change.score | |
|---|---|---|---|
| 2013-01-08 | 2013-01-08 | 3100.00 | 3.49 |
| 2013-01-09 | 2013-01-09 | 3115.00 | 3.48 |
| 2013-01-10 | 2013-01-10 | 3060.00 | 3.47 |
| 2013-01-11 | 2013-01-11 | 3215.00 | 3.46 |
| 2013-01-15 | 2013-01-15 | 3410.00 | 3.44 |
| 2013-01-16 | 2013-01-16 | 3255.00 | 3.42 |
| 2013-01-17 | 2013-01-17 | 3170.00 | 3.40 |
| 2013-01-18 | 2013-01-18 | 3190.00 | 3.40 |
| 2013-01-21 | 2013-01-21 | 3120.00 | 3.40 |
| 2013-01-22 | 2013-01-22 | 3140.00 | 3.40 |
| 2013-01-23 | 2013-01-23 | 3050.00 | 3.41 |
| 2013-01-24 | 2013-01-24 | 2903.00 | 3.39 |
| 2013-01-25 | 2013-01-25 | 2896.00 | 3.36 |
| 2013-01-28 | 2013-01-28 | 2840.00 | 3.28 |
| 2013-01-29 | 2013-01-29 | 2829.00 | 3.16 |
| 2013-01-30 | 2013-01-30 | 2917.00 | 2.99 |
| 2013-01-31 | 2013-01-31 | 2892.00 | 2.64 |
| 2013-02-01 | 2013-02-01 | 2856.00 | 2.11 |
| 2013-02-04 | 2013-02-04 | 2875.00 | 1.64 |
| 2013-02-05 | 2013-02-05 | 2835.00 | 1.22 |
stock$Close.normalized <- (stock$Close/mean(stock$Close)) *
100
stock.melt <- melt(stock, id.vars = "date", measure.vars = c("Close.normalized",
"change.score"))
ggplot(stock.melt, aes(x = date, y = value, col = variable)) +
geom_line() + xlab("") + ylab("Anomaly Score")