-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly_detection.R
220 lines (167 loc) · 5.53 KB
/
anomaly_detection.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#異常検知の対象データ行の指定
target <- 33
#学習データの行の指定
learn_data_start <- target-30
learn_data_end <- target-1
#学習データの設定
learn_data <- ts(data01[learn_data_start:learn_data_end,2])
#異常検知の対象データの設定
y <- as.numeric(data01[target,2])
#学習データの描写
plot(learn_data)
#階差データの生成
#1階差
diff(learn_data)
#2階差
diff(diff(learn_data))
#階差データの描写
plot(diff(learn_data))
#ARモデルの構築
ARmodel <- auto.arima(learn_data,
max.p = 5,
max.q = 0,
max.d = 2,
max.P = 0,
max.Q = 0,
max.D = 0)
#ARモデルの構築結果
ARmodel
#ARモデルの予測値(学習データ)
ARmodel_fitted <-fitted(ARmodel)
#ARモデルの実現値と予測値の描写
plot(learn_data,
col="blue",
type="l",
ylim=c(0,500),
xlab="時系列",
ylab="日販")
lines(ARmodel_fitted,
col="red",
type="l",
lty=3)
#ARモデルの残差
ARmodel_res <- residuals(ARmodel)
#ARモデルの残差の描写
plot(ARmodel_res,ylim=c(-100,100))
#学習データの外れ値処理
learn_data2 <- ifelse(abs(ARmodel_res)>20,
ARmodel_fitted,
learn_data)
#
#外れ値処理後の学習データで再構築
#
#ARモデルの構築
ARmodel <- auto.arima(learn_data2,
max.p = 5,
max.q = 0,
max.d = 2,
max.P = 0,
max.Q = 0,
max.D = 0)
#ARモデルの構築結果
ARmodel
#ARモデルの予測値(学習データ)
ARmodel_fitted <- fitted(ARmodel)
#ARモデルの実現値と予測値の描写
plot(learn_data2,
col="blue",
type="l",
ylim=c(0,500),
xlab="時系列",
ylab="日販")
lines(ARmodel_fitted,col="red",type="l",lty=3)
#ARモデルの残差
ARmodel_res <- residuals(ARmodel)
#ARモデルの残差の標準偏差
ARmodel_res_sd <- sd(ARmodel_res)
#ARモデルの残差の平均
ARmodel_res_mean <- mean(ARmodel_res)
#ARモデルの評価対象の予測
ARmodel_yosoku <- forecast(ARmodel,h=1)$mean
#ARモデルの評価対象の残差
ARmodel_gap <- y - ARmodel_yosoku
#外れ値スコア算出
LOF <- -log(dnorm(ARmodel_gap,ARmodel_res_mean,ARmodel_res_sd))
LOF
########################
#関数
########################
LOF <- function(data01,target,trim) {
#データセット
learn_data_start <- target-30
learn_data_end <- target-1
learn_data <- ts(data01[learn_data_start:learn_data_end,2])
y <- as.numeric(data01[target,2])
#ARモデル構築(外れ値処理前)
ARmodel <- auto.arima(learn_data,
max.p = 5, max.q = 0, max.d = 2,
max.P = 0, max.Q = 0, max.D = 0)
ARmodel_fitted<-fitted(ARmodel)
ARmodel_res<-residuals(ARmodel)
#ARモデル構築(外れ値処理)
learn_data2 <- ifelse(abs(ARmodel_res)>trim,
ARmodel_fitted,
learn_data)
learn_data <- learn_data2
ARmodel <- auto.arima(learn_data,
max.p = 5, max.q = 0, max.d = 2,
max.P = 0, max.Q = 0, max.D = 0)
ARmodel_fitted<-fitted(ARmodel)
ARmodel_res<-residuals(ARmodel)
#残差の標準偏差と平均値
ARmodel_res_sd<-sd(ARmodel_res)
ARmodel_res_mean<-mean(ARmodel_res)
#評価対象の予測と残差
ARmodel_yosoku <- forecast(ARmodel,h=1)$mean
ARmodel_gap <- y-ARmodel_yosoku
#外れ値度
LOF <- -log(dnorm(ARmodel_gap,ARmodel_res_mean,ARmodel_res_sd))
#出力
output_data <- c(LOF,ARmodel_res_mean,ARmodel_res_sd,y,ARmodel_yosoku,ARmodel_gap)
output_name <- c("LOF", "Mean", "SD","Measured value","Predicted value","Gap")
names(output_data) <- output_name
return(output_data)
}
LOF(data01,33,20)
LOF(data01,134,20)
LOF2 <- function(data01,target) {
#データセット
learn_data_start <- target-30
learn_data_end <- target-1
learn_data <- ts(data01[learn_data_start:learn_data_end,2])
y <- as.numeric(data01[target,2])
#ARモデル構築(外れ値処理前)
ARmodel <- auto.arima(learn_data,
max.p = 5, max.q = 0, max.d = 2,
max.P = 0, max.Q = 0, max.D = 0)
ARmodel_fitted<-fitted(ARmodel)
ARmodel_res<-residuals(ARmodel)
#残差の標準偏差と平均値
ARmodel_res_sd<-sd(ARmodel_res)
ARmodel_res_mean<-mean(ARmodel_res)
#ARモデル構築(外れ値処理)
learn_data2 <- ifelse(abs((ARmodel_res-ARmodel_res_mean)/ARmodel_res_sd)>2,
ARmodel_fitted,
learn_data)
learn_data <- learn_data2
ARmodel <- auto.arima(learn_data,
max.p = 5, max.q = 0, max.d = 2,
max.P = 0, max.Q = 0, max.D = 0)
ARmodel_fitted<-fitted(ARmodel)
ARmodel_res<-residuals(ARmodel)
#残差の標準偏差と平均値
ARmodel_res_sd<-sd(ARmodel_res)
ARmodel_res_mean<-mean(ARmodel_res)
#評価対象の予測と残差
ARmodel_yosoku <- forecast(ARmodel,h=1)$mean
ARmodel_gap <- y-ARmodel_yosoku
#外れ値度
LOF <- -log(dnorm(ARmodel_gap,ARmodel_res_mean,ARmodel_res_sd))
#出力
output_data <- c(LOF,ARmodel_res_mean,ARmodel_res_sd,y,ARmodel_yosoku,ARmodel_gap)
output_name <- c("LOF", "Mean", "SD","Measured value","Predicted value","Gap")
names(output_data) <- output_name
return(output_data)
}
LOF2(data01,33)
LOF2(data01,134)