如何根据排列顺序识别时间序列的所有可能排列

时间:2016-12-04 20:58:19

标签: r algorithm time-series permutation combinatorics

我试图找出一种方法将金融时间序列转换为符号时间序列,根据给定的顺序(在R中)解释所有“有意义”的排列:

示例:

给定时间序列:ts= c(1,2,3,4,5)

如果Order = 2,我想提取以下模式:
1)1 1 (ts[i]==ts[i+1])
2)1 2 (ts[i]<ts[i+1])
3)2 1 (ts[i]>ts[i+1])

(模式2 2是多余的,因为通过模式11来说明相等)

如果Order = 3,我想提取以下模式:
1)1 2 3 (ts[i]<ts[i+1]<ts[i+2])
2)1 2 2 (ts[i]<ts[i+1]==ts[i+2])
3)1 2 1 (ts[i]<ts[i+1]>ts[i+2])
4)2 2 3 (ts[i]==ts[i+1]<ts[i+2])
5)2 2 2 (ts[i]==ts[i+1]==ts[i+2])
6)2 2 1 (ts[i]==ts[i+1]>ts[i+2])
7)3 2 1 (ts[i]>ts[i+1]>ts[i+2])
8)3 2 2 (ts[i]>ts[i+1]==ts[i+2])
9)3 2 3 (ts[i]>ts[i+1]<ts[i+2])

我正在寻找的是可扩展的(按照订单2,3,4,5等)和自动(功能方式)方式来实现这一点。

我正在努力使用诸如“permute”,“gtools”,“combinat”等软件包,但无济于事。我认为我所寻求的是一种特殊的排列。任何人都可以帮我解决这个问题吗?

我的任务从阅读“排列熵”论文开始,谷歌学者搜索将为任何对此感兴趣的人提供相关的参考书目。

2 个答案:

答案 0 :(得分:1)

试试这个:

library(zoo)
ts <- c(1,3,2,4,5,4,3,3,2)
rollapply(ts, 2, rank, ties='min')

     [,1] [,2]
[1,]    1    2
[2,]    2    1
[3,]    1    2
[4,]    1    2
[5,]    2    1
[6,]    2    1
[7,]    1    1
[8,]    2    1

当订单= 3时:

rollapply(ts, 3, rank, ties='min')

     [,1] [,2] [,3]
[1,]    1    3    2
[2,]    2    1    3
[3,]    1    2    3
[4,]    1    3    1
[5,]    3    2    1
[6,]    3    1    1
[7,]    2    2    1

这不是你想要的,但它很接近。主要问题出现在前两行中,当两者都高于或低于中间观察时,您不希望区分第一和第三值的等级。这是一个修复。

z <- rollapply(ts, 3, rank, ties='min')
lohilo <- z[,1] < z[,2] & z[,3] < z[,2]
hilohi <- z[,1] > z[,2] & z[,3] > z[,2]
z[lohilo,] <- rep(c(1,2,1),rep(sum(lohilo),3))
z[hilohi,] <- rep(c(2,1,2),rep(sum(hilohi),3))
z
     [,1] [,2] [,3]
[1,]    1    2    1
[2,]    2    1    2
[3,]    1    2    3
[4,]    1    2    1
[5,]    3    2    1
[6,]    3    1    1
[7,]    2    2    1

答案 1 :(得分:0)

时间序列的排列是在下面的函数中计算的,专门针对排列熵(Source):

# Function to compute the ordinal patterns for a given time series.
 # Input (2 arguments. Null arguments are not vaild)
   # x = Given time series (type=numeric vector)
   # dim = Embedding dimension (type=numeric)
   # Commonly used value of dim ranges from 3 to 7 
 # Output is a numeric vector of size=(dim)!

ordinal_pattern<-function(x,dim){ 

# Generate ordinal numbers to assign. For example if dim =3, then 
# ordinal number=0,1,2  
ordinal_numbers<-seq(0,(dim-1),by=1)

# Compute all possible permutations of the ordinal numbers. 
# Maximum size of possible_pattern=dim!
possible_pattern<-(combinat::permn(ordinal_numbers))

# Initialize result. Result is the output. 
result<-0
result[1:length(possible_pattern)]<-0

# Loop for computation of ordinal pattern
for(i in 1:(length(x)-(dim-1))){
    temp<-x[i:(i+(dim-1))]
    tempseq<-seq(0,dim-1,by=1)
    tempdata<-data.frame(temp,tempseq)
    tempdata<-tempdata[order(temp),]
  
    for(j in 1: length(possible_pattern)){
        if (all(possible_pattern[[j]]==tempdata$tempseq)){
            result[j]<-result[j]+1
            }
    
       }
  
    }

return(result)

}