【发布时间】: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