0
public double variance() {
    if (data.length == 0) {
        return Double.NaN;
    } else {
        for (int i = 0; i < data.length; i++) {
            double temp = average() - data[i];
            Math.pow(temp, 2);
            return temp / data.length;
        }
    }   
}

This is a snippet of code from my program, Stat. I am coding on eclipse and it tells me not only to add a return statement, but that the i++ in the for loop is "dead code" (this is the first time I am encountering that term). basically what I am trying to do is return Double.NaN for an empty array, then for any other array subtract the data at position i from the average (first line under for loop). This value is then squared (second line under for loop). Since variance is the average of all these "temp" values, the return statement underneath returns temp / data.length. I can tell I am doing this wrong, if anyone can give me a couple hints or point me in the right direction thatd be awesome.

1
  • Stylistic/performance quibble: please tell me you're not computing the average on every pass through the loop!? Commented Nov 9, 2013 at 21:48

2 Answers 2

1

You need to move the return out of the loop statement.

public double variance() {
if (data.length == 0) {
    return Double.NaN;
} 

double variance = 0;
for (int i = 0; i < data.length; i++) {
    double temp = average() - data[i];
    variance += Math.pow(temp, 2);
}

return variance/data.length;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I would remove the method call, although a good interpreter could replace the method call, but still, a method call for a simple mult is not efficient, I'd go with variance = temp * temp;
This post was not supposed to improve performance, I prefer a minimum change in the original code to concentrate on the main problem. Even if it was, I reckon it does not worth to reduce code readability just because of one less function call in Java language!. Also, I believe Math.pow will use SHIFT operations for computing square which is more efficient than simply multiplication operation.
Furthermore, I am sure when a function is invoked in the body of a loop statement and the function just got a couple of lines of code, any modern compiler would copy the body of the function into the loop.
0

This is a dead code because if you enter in your for loop, you will return a value at the first iteration each time, and hence the statement i++ is useless because it will never be executed.

Then you else should look like this :

double temp = 0;
for (int i = 0; i < data.length; i++) {
    temp += average() - data[i];
    temp = Math.pow(temp, 2); //Math.pow returns a double so you have to assign it to temp
}
return temp/data.length; 

4 Comments

that makes sense, thank you. However, even before I saw your comment I tried putting the return statement outside of the for loop and I was greeted by another error message from eclipse. Any hints on how to fix this problem?
I think for each data[i] you want to add the square of the difference between the average and data[i] to temp - I don't think you want to square temp on every pass through the loop!
This code is incorrect for computing the variance. see my answer.
Who upped this? you are squaring temp on every pass; Iman's answer is correct. This is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.