private static int SNHT_change_point_detection(double[] data) {
double avg = Arrays.stream(data).average().getAsDouble();
double sigma= 0;
for (int i = 0; i < data.length; i++) {
sigma+= Math.pow(data[i] - avg, 2);
}
sigma = Math.sqrt(sigma/(data.length-1));
int n=data.length;
double max=0;
int index = 0;
for (int x = 1; x <= n; x++) {
double sum=0;
for (int x1 = 0; x1 < x; x1++) {
sum += ((data[x1] - avg) / sigma);
}
if (sum == 0) {
continue;
}
sum = x * Math.pow(sum / x, 2);
double sum1=0;
for (int x1 = x; x1 < n; x1++) {
sum1 += ((data[x1] - avg) / sigma);
}
if (sum1 == 0) {
continue;
}
sum1 = (n - x) * Math.pow(sum1 / (n - x), 2);
sum += sum1;
if (max < sum) {
max = sum;
index = x;
}
}
return index;
}
|