2012-01-28 224 views
3

Im做對股市數據時間序列分析,並試圖按照以下步驟實施分段線性分割的算法,它是:遞歸時間序列分割算法

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);  
    } 
} 

我寫上面的代碼爲給定algorithm..but我硝基甲苯知道爲什麼它運行到無限循環.. 我應該心存感激,如果有人能請我提供其他任何設計或修改代碼。

+1

'(pi-pi)^ 2' - 是不是隻是'0'? – 2012-01-28 12:30:48

+0

沒有它的實際(pi-pi_cap)^ 2..mathematcial術語..不打擾與那。 – gks 2012-01-28 12:37:30

+0

我們是'split'函數的代碼嗎?當你覺得它看起來像我只是需要做一個聯合(u)的集合(相當於'hashSet.addAll'。 – Perception 2012-01-28 12:44:37

回答

0

我硝基甲苯知道爲什麼它運行到無限循環

很容易找出原因。只需插入一些打印語句即可查看正在發生的事情(或使用調試器)。例如,

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); 
0

您的εi始終爲零!因此你的if語句在εi=(pi-pi)^ 2之後總是成立的!