Jason091
Jason091

Reputation: 11

Calculating the variance I get infinity

I have create a method to calculate the variance but, well lets just say that I have a logic error. Instead of getting the variance I get output as Infinity.

See output:

============ DataSet Test ================================
10 Data Items
[5.55, 47.9, 21.27, 6.59, 42.96, 74.3, 99.17, 50.99, 77.36, 62.76]
Minimum: 5.55 at index 0
Maximum: 99.17 at index 6
Data Range: 93.62
Median    : 56.875
Mean      : 48.885000000000005
Variance  : Infinity
Std Dev   : 0.0

And here is the code:

  //Return the variance of the points in this DataSet   
   public double variance()
   {
  double[] meanVar = this.data;

  for (int k = 0; k < meanVar.length; k++)
  {
  meanVar[k] = meanVar[k] - mean();
  meanVar[k] = Math.pow(meanVar[k],2.0);      
  }

  double variance = 0.0;

  for (int k = 0; k < meanVar.length; k++)
  variance = variance + meanVar[k];

  variance = variance / size();

  return variance;
 }

Upvotes: 0

Views: 175

Answers (1)

skiwi
skiwi

Reputation: 69349

What is size() right before the return in the variance() method?

Perhaps you mean meanVar.length there.

Upvotes: 1

Related Questions