【问题标题】:How to identify all possible permutations of a time series according to order of permutation如何根据排列顺序识别时间序列的所有可能排列
【发布时间】:2016-12-04 20:58:19
【问题描述】:

我正在尝试找出一种方法,将金融时间序列转换为符号时间序列,根据给定的顺序(在 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 是多余的,因为相等是通过模式 1 1 解释的)

如果 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])

我正在寻找的是一种可扩展(就 Order 为 2、3、4、5 等而言)和自动化(功能方面)的方式来做到这一点。

我正在努力使用诸如“permute”、“gtools”、“combinat”之类的软件包,但无济于事。我认为我所寻求的是一种特殊的排列情况。谁能帮我解决这个问题?

我的任务是从阅读有关“排列熵”的论文开始的,谷歌学术搜索将为您提供相关的参考书目以供任何进一步感兴趣的人使用。

【问题讨论】:

    标签: r algorithm time-series permutation combinatorics


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      时间序列的排列在下面的函数中计算,专门用于排列熵 (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)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-22
        • 2013-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多