forked from ncornwell/R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTFSmoother.R
292 lines (238 loc) · 6.31 KB
/
UTFSmoother.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
library(ggplot2)
library(xts)
## modified number of columns
ncols <- function (x) ifelse(is.matrix(x), ncol(x), length(x))
## modified number of rows
nrows <- function (x) ifelse(is.matrix(x), nrow(x), length(x))
#
# Determine transformed distribution across non-linear function f(x)
#
#
unscented.transform.aug <- function (
MUx, # mean of state
P, # covariance of state
Nyy, # noise covariance matrix of f(x)
f, # non-linear function f(X, E)
dt, # time increment
alpha = 1e-3, # scaling of points from mean
beta = 2, # distribution parameter
kappa = 1)
{
Nyy <- as.matrix(Nyy)
## constants
Lx <- nrows(MUx)
Ly <- nrows(Nyy)
n <- Lx + Ly
## create augmented mean and covariance
MUx.aug <- c (MUx, rep(0, Ly))
P.aug <- matrix(0, Lx+Ly, Lx+Ly)
P.aug[1:Lx,1:Lx] <- P
P.aug[(Lx+1):(Lx+Ly),(Lx+1):(Ly+Ly)] <- Nyy
## generating sigma points
lambda <- alpha^2 * (n + kappa) - n
A <- t (chol (P.aug))
X <- MUx.aug + sqrt(n + lambda) * cbind (rep(0,n), A, -A)
## generate weights
Wc <- c (
lambda / (n + lambda) + (1 - alpha^2 + beta),
rep (1 / (2 * (n + lambda)), 2*n))
Wm <- c (
lambda / (n + lambda),
rep (1 / (2 * (n + lambda)), 2*n))
## propagate through function
Y <- apply(X, 2, function (v)
{
f (dt, v[1:Lx], v[(Lx+1):(Lx+Ly)])
})
if (is.vector(Y))
Y <- t(as.matrix(Y))
## now calculate moments
MUy <- Y %*% Wm
Pyy <- matrix(0, nrows(Nyy), nrows(Nyy))
Pxy <- matrix(0, nrows(MUx), nrows(Nyy))
for (i in 1:ncols(Y))
{
dy <- (Y[,i] - MUy)
dx <- (X[1:Lx,i] - MUx)
Pyy <- Pyy + Wc[i] * dy %*% t(dy)
Pxy <- Pxy + Wc[i] * dx %*% t(dy)
}
list (mu = MUy, Pyy = Pyy, Pxy = Pxy)
}
#
# Augmented UKF filtered series
# - note that f and g are functions of state X and error vector N f(dt, Xt, E)
# - Nx and Ny state and observation innovation covariance
# - Xo is the initial state
# - dt is the time step
#
ukf.aug <- function (
series,
f,
g,
Nx,
Ny,
Xo = rep(0, nrow(Nx)),
dt = 1,
alpha = 1e-3,
kappa = 1,
beta = 2)
{
data <- as.matrix(coredata(series))
## description of initial distribution of X
oMUx <- Xo
oPx <- diag(rep(1e-4, nrows(Xo)))
Yhat <- NULL
Xhat <- NULL
for (i in 1:nrow(data))
{
Yt <- t(data[i,])
## predict
r <- unscented.transform.aug (oMUx, oPx, Nx, f, dt, alpha=alpha, beta=beta, kappa=kappa)
pMUx <- r$mu
pPx <- r$Pyy
## update
r <- unscented.transform.aug (pMUx, pPx, Ny, g, dt, alpha=alpha, beta=beta, kappa=kappa)
MUy <- r$mu
Pyy <- r$Pyy
Pxy <- r$Pxy
K <- Pxy %*% solve(Pyy)
MUx = pMUx + K %*% (Yt - MUy)
Px <- pPx - K %*% Pyy %*% t(K)
## set for next cycle
oMUx <- MUx
oPx <- Px
## append results
Yhat <- rbind(Yhat, t(MUy))
Xhat <- rbind(Xhat, t(MUx))
}
list (Yhat = Yhat, Xhat = Xhat)
}
ukf.smooth <- function (
series, # series to be filtered
f, # state mapping X[t] = f(X[t-1])
g, # state to measure mapping Y[t] = g(X[t])
Nx, # state innovation error covar
Ny, # measure innovation covar
Xo = rep(0, nrow(Nx)), # initial state vector
dt = 1, # time increment
alpha = 1e-3,
kappa = 1,
beta = 2)
{
data <- as.matrix(coredata(series))
Lx <- nrow(as.matrix(Nx))
Ly <- nrow(as.matrix(Ny))
## description of initial distribution of X
oMUx <- Xo
oPx <- diag(rep(1e-4, nrows(Xo)))
Ey <- rep(0, nrows(Ny))
Ex <- rep(0, nrows(Nx))
Ms <- list()
Ps <- list()
## forward filtering
for (i in 1:nrow(data))
{
Yt <- t(data[i,])
## predict
r <- unscented.transform.aug (oMUx, oPx, Nx, f, dt, alpha=alpha, beta=beta, kappa=kappa)
pMUx <- r$mu
pPx <- r$Pyy
## update
r <- unscented.transform.aug (pMUx, pPx, Ny, g, dt, alpha=alpha, beta=beta, kappa=kappa)
MUy <- r$mu
Pyy <- r$Pyy
Pxy <- r$Pxy
K <- Pxy %*% solve(Pyy)
MUx = pMUx + K %*% (Yt - MUy)
Px <- pPx - K %*% Pyy %*% t(K)
## set for next cycle
oMUx <- MUx
oPx <- Px
## append results
Ms[[i]] <- MUx
Ps[[i]] <- Px
}
## backward filtering, recursively determine N(Ms[t-1],Ps[t-1]) from N(Ms[t],Ps[t])
for (i in rev(1:(nrow(data)-1)))
{
## transform
r <- unscented.transform.aug (Ms[[i]], Ps[[i]], Nx, f, dt, alpha=alpha, beta=beta, kappa=kappa)
MUx <- r$mu
Pxx <- r$Pyy
Pxy <- r$Pxy[1:Lx,]
K <- Pxy %*% solve(Pxx)
Ms[[i]] <- Ms[[i]] + K %*% (Ms[[i+1]] - MUx)
Ps[[i]] <- Ps[[i]] + K %*% (Ps[[i+1]] - Pxx) * t(K)
}
Yhat <- NULL
Xhat <- NULL
for (i in 1:nrow(data))
{
MUy <- g(dt, Ms[[i]], Ey)
MUx <- Ms[[i]]
## append results
Yhat <- rbind(Yhat, t(MUy))
Xhat <- rbind(Xhat, t(MUx))
}
list (Y = data, Yhat = Yhat, Xhat = Xhat)
}
#
# Amplitude varying sine function state function:
#
# Yt = Amp[t] Sin(theta[t]) + Ey
#
# Theta[t] = Theta[t-1] + Omega[t-1] dt + Ex,1
# Omega[t] = Omega[t-1] + Ex,2
# Amp[t] = Amp[t-1] + Accel[t-1] dt + Ex,3
# Accel[t] = Accel[t] + Ex,4
#
sine.f.xx <- function (dt, Xt, Ex)
{
nXt <- c (
Xt[1] + dt*Xt[2] + Ex[1],
Xt[2] + Ex[2],
Xt[3] + Ex[3],
Xt[4] + Ex[4])
nXt
}
#
# Amplitude varying sine function observation function:
#
# Yt = Amp[t] Sin(theta[t]) + Ey
#
# Theta[t] = Theta[t-1] + Omega[t-1] dt + Ex,1
# Omega[t] = Omega[t-1] + Ex,2
# Amp[t] = Amp[t-1] + Accel[t-1] dt + Ex,3
# Accel[t] = Accel[t] + Ex,4
#
sine.f.xy <- function (dt, Xt, Ey)
{
y <- Xt[3] * sin(Xt[1] / pi) + Ey[1]
as.matrix(y)
}
#debug(unscented.transform.aug)
#debug(ukf.aug)
series.r <- sapply (1:500, function(x) (1+x/500) * sin(16 * x/500 * pi)) + rnorm(500, sd=0.25)
## unsmoothed test
u <- ukf.aug (
series.r, sine.f.xx, sine.f.xy,
Nx = 1e-3 * diag(c(1/3, 1, 1/10, 1/10)),
Ny = 1,
Xo = c(0.10, 0.10, 1, 1e-3), alpha=1e-2)
data <- rbind (
data.frame(t = 1:500, y=series.r, type='raw', window='Price'),
data.frame(t = 1:500, y=u$Yhat, type='filtered', window='Price'),
data.frame(t = 1:500, y=u$Xhat[,3], type='amp', window='Params'))
ggplot() + geom_line(aes(x=t,y=y, colour=type), data) + facet_grid(window ~ ., scales="free_y", heights=c(3,1))
## smoothed test
u <- ukf.smooth (
series.r, sine.f.xx, sine.f.xy,
Nx = 1e-3 * diag(c(1/3, 1, 1/10, 1/10)),
Ny = 1,
Xo = c(0.10, 0.10, 1, 1e-3), alpha=1e-2)
data <- rbind (
data.frame(t = 1:500, y=series.r, type='raw', window='Price'),
data.frame(t = 1:500, y=u$Yhat, type='filtered', window='Price'),
data.frame(t = 1:500, y=u$Xhat[,3], type='amp', window='Params'))
ggplot() + geom_line(aes(x=t,y=y, colour=type), data) + facet_grid(window ~ ., scales="free_y", heights=c(3,2))