我对股市数据进行时间序列分析,并尝试实现一种分段线性分割算法,如下所示:
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);
}
}
我已经为给定的算法编写了上面的代码..但我不知道它为什么会陷入无限循环..如果有人可以向我提供代码的任何其他设计或修改,我将不胜感激。