gpt4 book ai didi

java - 递归时间序列分割算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:25:18 26 4
gpt4 key购买 nike

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

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

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

最佳答案

i dnt know why it runs into infinite loop

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

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

关于java - 递归时间序列分割算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9045232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com