使用滚动窗口的小波相关 [英] Wavelet correlation using rolling window
问题描述
我有3个时间序列,可以将小波变换应用于滚动窗口.滚动窗口采用单个长度为200的时间序列,并对前30个样本对其应用waveslim::modwt
函数.这将输出5个我只感兴趣的列表(d1,d2,d3,d4),每个列表的长度为30.可以在此处找到一个简单的示例:
I have 3 time series which I can apply the wavelet transform to using a rolling window. The rolling window takes a single time series of length 200 and applies the waveslim::modwt
function to it over the first 30 samples. This outputs 5 lists of which I am only interested in (d1,d2,d3,d4) and these each have a length of 30. A simple example can be found here:
library(waveslim)
J <- 4 #no. of levels in decomposition
data(ar1)
ar1.modwt <- modwt(ar1, "la8", J)
@G. Grothendieck为单个时间序列的滚动窗口方法提供了简洁的代码
@G. Grothendieck has kindly provided a neat piece of code for the rolling window approach for a single time series here.
滚动窗口增加1,然后我们再次进行操作,生成另外5个列表,我只关心d1-> d4,依此类推,直到时间序列的整个长度都经过滚动.
The rolling window increments by 1 and we go again, producing another 5 lists of which I only care for d1->d4 and so on and so on until the full length of the time series had been rolled over.
下一步是将waveslim::brick.wall
函数应用于滚动窗口列表的输出. brick.wall
函数在4个级别的第一个窗口中查看modwt
的输出,并将某些值替换为NA
s.
The next step is to apply the waveslim::brick.wall
function to the output of the rolling window lists. The brick.wall
function looks at the output of modwt
for the first window across the 4 levels and replaces some of the values with NA
s.
我相信我已经通过修改@G来解决了这个问题. Grothendieck使用以下方法回答,希望我是对的:
I believe I have covered this by modifying @G. Grothendieck answer using the following approach, I hope I am right:
modwt2 <- function(...) unlist(head(brick.wall(modwt(...)), 4))
rollr <- rollapplyr(ar1, 30, FUN = modwt2, wf = "la8", n.levels = 4, boundary = "periodic")
L <- lapply(1:nrow(rollr), function(i) matrix(rollr[i,], , 4))
最后一步是为brick.wall
函数的输出构造相关矩阵,该函数在4个感兴趣的层次上均高于L
.
The final piece is to construct correlation matrices for the outputs of the brick.wall
function which is L
above over the 4 levels of interest.
有一个名为waveslim::wave.correlation
的函数,该函数接收两个brick.wall
输出X和Y,并在各个级别上计算wave.correlation
.
There is a function called waveslim::wave.correlation
which takes two brick.wall
outputs X and Y and computes the wave.correlation
over the various levels.
library(waveslim)
data(exchange)
returns <- diff(log(as.matrix(exchange)))
returns <- ts(returns, start=1970, freq=12)
wf <- "la8"
J <- 4
demusd.modwt <- modwt(returns[,"DEM.USD"], wf, J)
demusd.modwt.bw <- brick.wall(demusd.modwt, wf)
jpyusd.modwt <- modwt(returns[,"JPY.USD"], wf, J)
jpyusd.modwt.bw <- brick.wall(jpyusd.modwt, wf)
returns.modwt.cor <- wave.correlation(demusd.modwt.bw, jpyusd.modwt.bw,
N = dim(returns)[1])
我希望对此进行扩展,并为我的3个时间序列计算完整的相关矩阵.请注意,上面的汇率示例没有使用滚动窗口方法,因为它使用了我现在想要的整个时间序列的长度,并且还为两个时间序列之间的相关性生成了一个单一值.它并没有构建我需要的完整相关矩阵,因为我对这些相关矩阵随时间的特征值很感兴趣.
I wish to expand on this and compute the full correlation matrix for my 3 time series. Please note that the example above with exchange rates does not use the rolling window approach as it uses the full length of the time series which I would like to now do and it also produces a single value for the correlation between two time series. It does not construct the full correlation matrix which I need as I am interested in the eigenvalues of these correlation matrices over time.
总而言之:
- 采取3个时间序列
- 使用滚动窗口应用
modwt
功能 - 对上面2中滚动窗口的每个输出应用
brick.wall
函数 - 使用以上3个输出随时间推移为4个级别创建完整的3x3相关矩阵
- Take 3 time series
- Apply
modwt
function using rolling window - Apply
brick.wall
function to each output of the rolling window in 2 above - Create full 3x3 correlation matrix for the 4 levels using outputs of 3 above over time
推荐答案
将您在问题中给出的内容放在一起:
Putting together the pieces you give in your question:
1)创建3个时间序列
set.seed(1)
s <- replicate(3, rnorm(200), simplify = FALSE)
2)& 3)在滚动窗口中应用modwt
和brick.wall
2) & 3) Apply modwt
and brick.wall
with rolling window
modwt2 <- function(...) unlist(head(brick.wall(modwt(...), wf = "la8"), 4))
rollr <- lapply(s, function(x) rollapplyr(x, 30, FUN = modwt2, wf = "la8",
n.levels = 4, boundary = "periodic"))
L <- lapply(rollr, function(x) lapply(1:nrow(x), function(i) matrix(x[i,], , 4)))
res <- lapply(L, function(y) lapply(y, function(x) as.list(as.data.frame(x))))
4)创建相关矩阵
create_4mat <- function(w) {
# create four 3*3 correlation matrices (one for each level) for window w
M <- replicate(4, matrix(0, nrow = 3, ncol = 3), simplify = FALSE)
for (k in 1:4) {
for (i in 1:3) {
for (j in (i:3)[-1]) {
M[[k]][i, j] = wave.correlation(res[[i]][[w]], res[[j]][[w]], N=30)[k, 1]
}
}
M[[k]] <- M[[k]] + t(M[[k]]) + diag(1, 3, 3)
}
M
}
output <- lapply(1:171, create_4mat)
output
是171个包含4个相关矩阵的列表的列表.
output
is a list of 171 lists of 4 correlations matrices.
例如,output[[28]][[2]]
是第28个窗口中d2
的相关矩阵:
For instance, output[[28]][[2]]
is the correlation matrix for d2
in the 28th window:
output[[28]][[2]]
# [,1] [,2] [,3]
# [1,] 1.0000000 -0.1740320 0.2292872
# [2,] -0.1740320 1.0000000 0.6046918
# [3,] 0.2292872 0.6046918 1.0000000
特征值(按评论要求)
对于d1
:
eigenvalues1 <- lapply(output, function(x) eigen(x[[1]], symmetric = TRUE,
only.values = TRUE)$values)
与d2
类似.请注意,对于d3
和d4
,所有相关矩阵都填充有缺失值.
Similarly for d2
. Note that for d3
and d4
all correlation matrices are filled with missing values.
这篇关于使用滚动窗口的小波相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!