Detect the interval between time points
up vote
-1
down vote
favorite
I want to detect the interval between time points. I want to ignore some anomalies if there are. The idea is choose some random samples and try to detect what is the interval between points in most cases.
import java.util.Arrays;
import java.util.Random;
public class Testing {
private static int RANDOM_POINT_COUNT = 5;
static private long getAverageInterval(long times, boolean useSampling) {
long diffs;
if (useSampling && times.length > 2 * RANDOM_POINT_COUNT) {
diffs = calculateAverageRandomly(times, RANDOM_POINT_COUNT);
} else {
diffs = calculateAverage(times);
}
long sortedDiffs = Arrays.copyOf(diffs, diffs.length);
Arrays.sort(sortedDiffs);
final long interval = sortedDiffs[sortedDiffs.length / 2];
return interval;
}
static private long calculateAverage(long times) {
long diffs = new long[times.length - 1];
for (int i = times.length - 2; i >= 0; --i) {
diffs[i] = Math.abs(times[i] - times[i + 1]);
}
return diffs;
}
static private long calculateAverageRandomly(long times, int count) {
assert times.length > 1;
if (times.length == 2) {
return new long {times[1] - times[0]};
}
long diffs = new long[2*count];
int partCount = (int) Math.round(times.length*1.0/count);
Random rn = new Random();
int currentIndex = 0;
for(int i = times.length - partCount-1; i >= 0 && currentIndex < diffs.length; i-= partCount) {
int index = rn.nextInt(partCount) +i;
if(index == 0) {
index = 1;
}
diffs[currentIndex] = times[index+1] - times[index];
++currentIndex;
diffs[currentIndex] = times[index] - times[index - 1];
++currentIndex;
}
return diffs;
}
public static void main(String args) {
final long t = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 61/*anomaly*/ };
final long result = Testing.getAverageInterval(t, true);
System.out.println(result);
if(result != 5) {
new RuntimeException("Excepted result is 5");
}
final long t1 = { 1, 2, 3, /*4,*/ 5, 6 /*7,*/, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, /*18,*/ 19 };
final long result1 = Testing.getAverageInterval(t1, true);
System.out.println(result1);
if(result1 != 1) {
new RuntimeException("Excepted result is 1");
}
}
}
java floating-point interval
add a comment |
up vote
-1
down vote
favorite
I want to detect the interval between time points. I want to ignore some anomalies if there are. The idea is choose some random samples and try to detect what is the interval between points in most cases.
import java.util.Arrays;
import java.util.Random;
public class Testing {
private static int RANDOM_POINT_COUNT = 5;
static private long getAverageInterval(long times, boolean useSampling) {
long diffs;
if (useSampling && times.length > 2 * RANDOM_POINT_COUNT) {
diffs = calculateAverageRandomly(times, RANDOM_POINT_COUNT);
} else {
diffs = calculateAverage(times);
}
long sortedDiffs = Arrays.copyOf(diffs, diffs.length);
Arrays.sort(sortedDiffs);
final long interval = sortedDiffs[sortedDiffs.length / 2];
return interval;
}
static private long calculateAverage(long times) {
long diffs = new long[times.length - 1];
for (int i = times.length - 2; i >= 0; --i) {
diffs[i] = Math.abs(times[i] - times[i + 1]);
}
return diffs;
}
static private long calculateAverageRandomly(long times, int count) {
assert times.length > 1;
if (times.length == 2) {
return new long {times[1] - times[0]};
}
long diffs = new long[2*count];
int partCount = (int) Math.round(times.length*1.0/count);
Random rn = new Random();
int currentIndex = 0;
for(int i = times.length - partCount-1; i >= 0 && currentIndex < diffs.length; i-= partCount) {
int index = rn.nextInt(partCount) +i;
if(index == 0) {
index = 1;
}
diffs[currentIndex] = times[index+1] - times[index];
++currentIndex;
diffs[currentIndex] = times[index] - times[index - 1];
++currentIndex;
}
return diffs;
}
public static void main(String args) {
final long t = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 61/*anomaly*/ };
final long result = Testing.getAverageInterval(t, true);
System.out.println(result);
if(result != 5) {
new RuntimeException("Excepted result is 5");
}
final long t1 = { 1, 2, 3, /*4,*/ 5, 6 /*7,*/, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, /*18,*/ 19 };
final long result1 = Testing.getAverageInterval(t1, true);
System.out.println(result1);
if(result1 != 1) {
new RuntimeException("Excepted result is 1");
}
}
}
java floating-point interval
1
Welcome on Code Review. Can you provide some examples of inputs and outputs (expected and unwanted)? And also, it sound like your code isn't actually working. In this case, I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Finally, use the{}
to format your code.
– Calak
37 mins ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I want to detect the interval between time points. I want to ignore some anomalies if there are. The idea is choose some random samples and try to detect what is the interval between points in most cases.
import java.util.Arrays;
import java.util.Random;
public class Testing {
private static int RANDOM_POINT_COUNT = 5;
static private long getAverageInterval(long times, boolean useSampling) {
long diffs;
if (useSampling && times.length > 2 * RANDOM_POINT_COUNT) {
diffs = calculateAverageRandomly(times, RANDOM_POINT_COUNT);
} else {
diffs = calculateAverage(times);
}
long sortedDiffs = Arrays.copyOf(diffs, diffs.length);
Arrays.sort(sortedDiffs);
final long interval = sortedDiffs[sortedDiffs.length / 2];
return interval;
}
static private long calculateAverage(long times) {
long diffs = new long[times.length - 1];
for (int i = times.length - 2; i >= 0; --i) {
diffs[i] = Math.abs(times[i] - times[i + 1]);
}
return diffs;
}
static private long calculateAverageRandomly(long times, int count) {
assert times.length > 1;
if (times.length == 2) {
return new long {times[1] - times[0]};
}
long diffs = new long[2*count];
int partCount = (int) Math.round(times.length*1.0/count);
Random rn = new Random();
int currentIndex = 0;
for(int i = times.length - partCount-1; i >= 0 && currentIndex < diffs.length; i-= partCount) {
int index = rn.nextInt(partCount) +i;
if(index == 0) {
index = 1;
}
diffs[currentIndex] = times[index+1] - times[index];
++currentIndex;
diffs[currentIndex] = times[index] - times[index - 1];
++currentIndex;
}
return diffs;
}
public static void main(String args) {
final long t = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 61/*anomaly*/ };
final long result = Testing.getAverageInterval(t, true);
System.out.println(result);
if(result != 5) {
new RuntimeException("Excepted result is 5");
}
final long t1 = { 1, 2, 3, /*4,*/ 5, 6 /*7,*/, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, /*18,*/ 19 };
final long result1 = Testing.getAverageInterval(t1, true);
System.out.println(result1);
if(result1 != 1) {
new RuntimeException("Excepted result is 1");
}
}
}
java floating-point interval
I want to detect the interval between time points. I want to ignore some anomalies if there are. The idea is choose some random samples and try to detect what is the interval between points in most cases.
import java.util.Arrays;
import java.util.Random;
public class Testing {
private static int RANDOM_POINT_COUNT = 5;
static private long getAverageInterval(long times, boolean useSampling) {
long diffs;
if (useSampling && times.length > 2 * RANDOM_POINT_COUNT) {
diffs = calculateAverageRandomly(times, RANDOM_POINT_COUNT);
} else {
diffs = calculateAverage(times);
}
long sortedDiffs = Arrays.copyOf(diffs, diffs.length);
Arrays.sort(sortedDiffs);
final long interval = sortedDiffs[sortedDiffs.length / 2];
return interval;
}
static private long calculateAverage(long times) {
long diffs = new long[times.length - 1];
for (int i = times.length - 2; i >= 0; --i) {
diffs[i] = Math.abs(times[i] - times[i + 1]);
}
return diffs;
}
static private long calculateAverageRandomly(long times, int count) {
assert times.length > 1;
if (times.length == 2) {
return new long {times[1] - times[0]};
}
long diffs = new long[2*count];
int partCount = (int) Math.round(times.length*1.0/count);
Random rn = new Random();
int currentIndex = 0;
for(int i = times.length - partCount-1; i >= 0 && currentIndex < diffs.length; i-= partCount) {
int index = rn.nextInt(partCount) +i;
if(index == 0) {
index = 1;
}
diffs[currentIndex] = times[index+1] - times[index];
++currentIndex;
diffs[currentIndex] = times[index] - times[index - 1];
++currentIndex;
}
return diffs;
}
public static void main(String args) {
final long t = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 61/*anomaly*/ };
final long result = Testing.getAverageInterval(t, true);
System.out.println(result);
if(result != 5) {
new RuntimeException("Excepted result is 5");
}
final long t1 = { 1, 2, 3, /*4,*/ 5, 6 /*7,*/, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, /*18,*/ 19 };
final long result1 = Testing.getAverageInterval(t1, true);
System.out.println(result1);
if(result1 != 1) {
new RuntimeException("Excepted result is 1");
}
}
}
java floating-point interval
java floating-point interval
edited 13 mins ago
asked 48 mins ago
Vardan Hovhannisyan
1414
1414
1
Welcome on Code Review. Can you provide some examples of inputs and outputs (expected and unwanted)? And also, it sound like your code isn't actually working. In this case, I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Finally, use the{}
to format your code.
– Calak
37 mins ago
add a comment |
1
Welcome on Code Review. Can you provide some examples of inputs and outputs (expected and unwanted)? And also, it sound like your code isn't actually working. In this case, I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Finally, use the{}
to format your code.
– Calak
37 mins ago
1
1
Welcome on Code Review. Can you provide some examples of inputs and outputs (expected and unwanted)? And also, it sound like your code isn't actually working. In this case, I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Finally, use the
{}
to format your code.– Calak
37 mins ago
Welcome on Code Review. Can you provide some examples of inputs and outputs (expected and unwanted)? And also, it sound like your code isn't actually working. In this case, I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Finally, use the
{}
to format your code.– Calak
37 mins ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209135%2fdetect-the-interval-between-time-points%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome on Code Review. Can you provide some examples of inputs and outputs (expected and unwanted)? And also, it sound like your code isn't actually working. In this case, I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. Finally, use the
{}
to format your code.– Calak
37 mins ago