Docsity

Questions on Statistical Computing - Assignment 5 | STAT 534, Assignments of Statistics

Toggle sidebar
bg1
Hil Lyons
STAT 534
HW 5
1) Charts and output from the timewarp functions
a. Costs of the optimal warping paths (alignment error)
Note: cost is determined via the sum of squared distance between points of post-
warped curves at the same indices. Thus, the cost result is in square units.
Warped functions Cost
fun1 and fun2 0.02407648
fun1.noisy and fun2.noisy 0.126304
b. Charts
Fun1 and fun2
Before and after alignment
Note: fun2 was warped to fun1.
0 10 20 30 40 50
0.2 0.4 0.6 0.8
fun1 (black) and fun2 (blue ) before alignment
Index
Y
0 10 20 30 40 50
0.2 0.4 0.6 0.8
fun1 (black) and fun2 (blue ) after alignm ent
Index
Y
Hil Lyons Page 1 5/15/2006
pf3
pf4
pf5

Partial preview of the text

Download Questions on Statistical Computing - Assignment 5 | STAT 534 and more Assignments Statistics in PDF only on Docsity!

Hil Lyons

STAT 534

HW 5

1) Charts and output from the timewarp functions

a. Costs of the optimal warping paths (alignment error)

Note: cost is determined via the sum of squared distance between points of post-

warped curves at the same indices. Thus, the cost result is in square units.

Warped functions Cost

fun1 and fun2 0.

fun1.noisy and fun2.noisy 0.

b. Charts

Fun1 and fun

Before and after alignment

Note: fun2 was warped to fun1.

0 10 20 30 40 50

fun1 (black) and fun2 (blue) before alignment Index Y 0 10 20 30 40 50

fun1 (black) and fun2 (blue) after alignm ent Index Y

Optimal warping path

0 10 20 30 40 50 0 10 20 30 40 50 Optim al w arping path, fun1, fun fun fun

Fun1.noisy and fun2.noisy

Before and after alignment

Note: fun2.noisy was warped to fun1noisy.

0 10 20 30 40 50

fun1.noisy (black) and fun2.noisy (blue) before alignment Index Y 0 10 20 30 40 50

fun1.noisy (black) and fun2.noisy (blue) after alignm ent Index Y

2) R function specification

The timewarp function:

Returns warping path between two time series.

################################################################### timewarp <- function(fun1, fun2){ m <- length(fun1) n <- length(fun2) if (m == 0 || n == 0) return("Error: array of 0 length") cost.matrix <- matrix(-1, ncol = m, nrow = n)

cost.of.warp computes the cost of the optimal path

using memoization to the cost.matrix. Cost is

computed basically using sum of square distance between

post-warped elements of the two time series at the same

index.

cost.of.warp <- function(func1, func2){ m2 <- length(func1); n2 <- length(func2) if (cost.matrix[n2, m2] != -1) return(cost.matrix[n2, m2]) point.distance <- (func1[m2] - func2[n2])^ if ((m2 + n2) == 2) tot.cost <- point.distance else { if (m2 != 1 && n2 != 1){ option.cost <- c(cost.of.warp(func1[1:(m2-1)], func2[1:(n2 - 1)]), cost.of.warp(func1[1:(m2-1)], func2[1:n2]), cost.of.warp(func1[1:m2], func2[1:(n2 - 1)])) tot.cost <- point.distance + min(option.cost)} else { if (m2 == 1){ tot.cost <- point.distance + cost.of.warp(func1, func2[1:(n2 - 1)])} else { tot.cost <- point.distance + cost.of.warp(func1[1:(m2 - 1)], func2)}}} return(cost.matrix[n2, m2] <<- tot.cost) } cost = cost.of.warp(fun1, fun2)

The cost.matrix that was used for memoization is used to

trace the optimal path

grid1.warped <- m grid2.warped <- n cur.fun1 <- m; cur.fun2 <- n while (cur.fun1 > 1 || cur.fun2 > 1){ if (cur.fun1 == 1) cur.fun2 <- cur.fun2 - 1 else { if (cur.fun2 == 1) cur.fun1 <- cur.fun1 - 1 else { step.options <- c(cost.matrix[cur.fun2 - 1, cur.fun1 - 1], cost.matrix[cur.fun2, cur.fun1 - 1], cost.matrix[cur.fun2 - 1, cur.fun1]) next.step <- which.min(step.options) if (next.step == 3) cur.fun2 <- cur.fun2 - 1 else { if (next.step == 2) cur.fun1 <- cur.fun1 - 1 else { cur.fun2 <- cur.fun2 - 1 cur.fun1 <- cur.fun1 - 1}}}} grid1.warped <- c(cur.fun1, grid1.warped) grid2.warped <- c(cur.fun2, grid2.warped)}

return(list(grid1.warped = grid1.warped, grid2.warped = grid2.warped, cost = cost)) } ###################################################################

Print functions

print.timewarp and print.path provide a print mechanism

for the b/a alignmnent and optimal warping path plots.

################################################################### print.timewarp <- function(fun1, fun2, warp1, warp2){ windows() par(mfrow = c(1,2)) plot(fun1, type = "l", main = "fun1 (black) and fun2 (blue) before alignment", ylim = c(min(c(fun1, fun2)), max(c(fun1, fun2))), ylab = "Y", xlab = "Index", cex.main = .7, cex.lab = .7, cex.axis = .7) lines(fun2, type = "l",col = "blue") plot(fun1, type = "l", main = "fun1 (black) and fun2 (blue) after alignment", ylim = c(min(c(fun1, fun2)), max(c(fun1, fun2))), ylab = "Y", xlab = "Index", cex.main = .7, cex.lab = .7, cex.axis = .7) for (i in 1:(length(warp1)-1)){ y.val = c(fun2[warp2[i]],fun2[warp2[i+1]]) x.val = c(warp1[i],warp1[i+1]) lines(x.val, y.val, type = "l", col = "blue") #points(x.val, y.val, type = "p", col = "blue", cex = .7) } return() } print.path = function(warp1, warp2){ windows() ylims = c(0, max(warp2)) xlims = c(0, max(warp1)) x.points = seq(0, max(warp1), floor(max(warp1)/10)) y.points = seq(0, max(warp2), floor(max(warp2)/10)) plot(rep(x.points, rep(length(y.points), length(x.points))), rep(y.points, length(x.points)), cex = .7, ylim = ylims, xlim = xlims, main = "Optimal warping path, fun1, fun2", cex.main = .7, ylab = "fun2", xlab = "fun1", cex.lab = .7, cex.axis = .7) abline(h = y.points, col = "lightgray") abline(v = x.points, col = "lightgray") lines(warp1, warp2) return() } print.timewarp2 <- function(fun1, fun2, warp1, warp2){ windows() par(mfrow = c(1,2)) plot(fun1, type = "l", main = "fun1.noisy (black) and fun2.noisy (blue) before alignment", ylim = c(min(c(fun1, fun2)), max(c(fun1, fun2))), ylab = "Y", xlab = "Index", cex.main = .7, cex.lab = .7, cex.axis = .7) lines(fun2, type = "l",col = "blue") plot(fun1, type = "l", main = "fun1.noisy (black) and fun2.noisy (blue) after alignment", ylim = c(min(c(fun1, fun2)), max(c(fun1, fun2))), ylab = "Y", xlab = "Index", cex.main = .7, cex.lab = .7, cex.axis = .7) for (i in 1:(length(warp1)-1)){ y.val = c(fun2[warp2[i]],fun2[warp2[i+1]]) x.val = c(warp1[i],warp1[i+1]) lines(x.val, y.val, type = "l", col = "blue") #points(x.val, y.val, type = "p", col = "blue", cex = .7)