You are here

function Vector::variance in Recommender API 6.2

Same name and namespace in other branches
  1. 7.6 classes/Matrix.php \Vector::variance()

Calculate the variance. This works for SparseMatrix too.

Parameters

$may_cache:

Return value

mean value

1 call to Vector::variance()
Vector::std in ./Matrix.php

File

./Matrix.php, line 223

Class

Vector
This is the Vector superclass. @author danithaca

Code

function variance($may_cache = FALSE) {
  if (!$may_cache || $this->variance === NULL) {

    // force calculation
    $count = $this
      ->count($may_cache);
    $mean = $this
      ->mean($may_cache);
    $variance = 0;
    foreach ($this->values as $value) {
      $variance += pow($value - $mean, 2);
    }
    $this->variance = $count == 0 ? NAN : $variance / $count;
  }
  return $this->variance;
}