Computes the variance of the values in this vector, which is #standardDeviation() 2 @return the variance
()
| 524 | * @return the variance |
| 525 | */ |
| 526 | public double variance() |
| 527 | { |
| 528 | double mu = mean(); |
| 529 | double variance = 0; |
| 530 | |
| 531 | double N = length(); |
| 532 | |
| 533 | |
| 534 | int used = 0; |
| 535 | for(IndexValue x : this) |
| 536 | { |
| 537 | used++; |
| 538 | variance += Math.pow(x.getValue()-mu, 2)/N; |
| 539 | } |
| 540 | //Now add all the zeros we skipped into it |
| 541 | variance += (length()-used) * Math.pow(0-mu, 2)/N; |
| 542 | |
| 543 | return variance; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Returns the median value in this vector |