【问题标题】:Recursive time series segmentation algorithm递归时间序列分割算法
【发布时间】:2012-02-21 03:31:31
【问题描述】:

我正在对股市数据进行时间序列分析,并尝试实现一种分段线性分割算法,如下所示:

    split(T [ta, tb ]) – split a time series T of length
    n from time ta to time tb where 0 ≤ a < b ≤ n
    1: Ttemp = ∅
    2: εmin = ∞;
    3: εtotal = 0;
    4: for i = a to b do
            5:εi = (pi − pi )^2 ;
            6:if εmin > εi then
                7:  εmin = εi ;
                8:  tk = ti ;
            9:end if
        10:εtotal = εtotal + εi ;
    11: end for
    12: ε = εtotal /(tb − ta );
    13: if t-test.reject(ε) then
            14:Ttemp = Ttemp ∪ split(T [ta , tk ]);
            15:Ttemp = Ttemp ∪ split(T [tk , tb ]);
        16: end if
    17: return Ttemp ;

我的时间序列类如下:

class MySeries{
      ArrayList<Date> time;
      Double[] value;
}

在上述算法中,Ttemp 是时间序列的另一个实例。第 4-12 行的计算用于计算误差。
问题是我无法实现上面的递归和联合部分(第 14 和 15 行)。我不清楚如何递归和合并 MySeries 对象。

************ 编辑*********** *******

class Segmentation{
    static MySeries series1 = new MySeries();    //contains the complete time series
    static HashSet<MySeries> series_set = new HashSet<MySeries>();    

    public static MySeries split(MySeries series, int start, int limit) throws ParseException{      
        if(limit-start < 3){     //get min of 3 readings atleast
        return null;
        }

    tTemp = MySeries.createSegment(series1, start, limit);

    double emin = 999999999, e,etotal=0, p, pcap;
    DescriptiveStatistics errors = new DescriptiveStatistics();

    for(int i=start;i<limit;i++){
        p = series1.y[i];
        pcap = series1.regress.predict(series1.x[i]);
        e = (p-pcap)*(p-pcap);
        errors.addValue(e);
        if(emin > e){
            emin = e;
            splitPoint = i;
        }
        etotal = etotal + e;
    }
    e = etotal/(limit-start);

    double std_dev_error = errors.getStandardDeviation();
    double tTstatistic = e/(std_dev_error/Math.sqrt(errors.getN()));

        if(ttest.tTest(tTstatistic, errors, 0.10)){
            union(split(series1, start, splitPoint));
            union(split(series1, splitPoint+1, limit));
        }
    return tTemp;
}

    static void union(MySeries ms){
        series_set.add(ms);    
    }
}

我已经为给定的算法编写了上面的代码..但我不知道为什么它会陷入无限循环.. 如果有人可以向我提供代码的任何其他设计或修改,我将不胜感激。

【问题讨论】:

  • (pi - pi)^2 -- 不就是0吗?
  • 实际上没有(pi -pi_cap)^2..数学术语..不要打扰。
  • 我们是split函数的代码?当你得到它时,在我看来你只需要做一个集合的并集(u)(相当于hashSet.addAll
  • 抱歉这个错误..算法的名字本身是分裂的..所以在第 14 行和第 15 行它递归地调用自己。
  • @Perception 的意思是,当你实现你的方法时,你可以使用HashSet 作为 Ttemp 的类型,并使用像Ttemp.addAll(split(timeseries)); 这样的行来执行递归调用返回的数据的连接。

标签: java algorithm recursion time-series linear-regression


【解决方案1】:

我不知道为什么会陷入无限循环

很容易找出原因。只需插入一些打印语句即可查看发生了什么(或使用调试器)。例如,

    if(ttest.tTest(tTstatistic, errors, 0.10)){
        System.out.printf("About to split %d .. %d .. %d%n", start, splitPoint, limit);
        union(split(series1, start, splitPoint));
        union(split(series1, splitPoint+1, limit));
    }
    else
        System.out.printf("Not splitting %d .. %d%n", start, limit);

【讨论】:

    【解决方案2】:

    您的 εi 始终为零!因此,您在 εi = (pi - pi )^2 之后的 if 语句将始终为真!

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2012-04-02
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      相关资源
      最近更新 更多