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









share|improve this question




















  • 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















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









share|improve this question




















  • 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













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python