You are here

function Vector::variance in Recommender API 7.6

Same name and namespace in other branches
  1. 6.2 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 classes/Matrix.php

File

classes/Matrix.php, line 218

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