You are here

abstract class Vector in Recommender API 6.2

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

This is the Vector superclass. @author danithaca

Hierarchy

Expanded class hierarchy of Vector

File

./Matrix.php, line 127

View source
abstract class Vector {

  // an array of values
  protected $values = NULL;

  // cached
  protected $count = NULL;
  protected $mean = NULL;
  protected $variance = NULL;
  protected $std = NULL;

  // users can only use the factory method.
  protected function __construct() {
  }

  /**
   * Factory method to create a vector.
   * Note, no parameter checking.
   * Array index has to be [0..n), otherwise program unstable
   * @param $type Create a sparse vector or a real vector. Could be 'SparseVector' or 'RealVector'
   * @param $size
   * @param $value
   * @return unknown_type
   */
  static function create($type, $size = 0, $value = 0) {
    if ($type == 'SparseVector') {
      $vector = new SparseVector();
      $vector->values = array();
    }
    elseif ($type == 'RealVector') {
      $vector = new RealVector();
      $vector->count = $size;
      $vector->values = array_fill(0, $size, $value);
    }
    else {
      trigger_error('Vector type not recognized', E_USER_ERROR);
    }
    return $vector;
  }

  /**
   * Factory method.
   * Wrap the array of numbers into the vector. Note: passing by reference!
   * @param $type Create a sparse vector or a real vector. Could be 'SparseVector' or 'RealVector'
   * @param $values the array of numbers. index staring at 0, or the program will be unexpected.
   * @return Vector
   */
  static function wrap($type, &$values) {

    // & required, or it will make a copy when passing.
    if ($type == 'SparseVector') {
      $vector = new SparseVector();
    }
    elseif ($type == 'RealVector') {
      $vector = new RealVector();
    }
    else {
      trigger_error('Vector type not recognized', E_USER_ERROR);
    }
    $vector->values =& $values;

    // & required, or it will make a copy here too.
    return $vector;
  }

  // Note: PHP doesn't throw OutOfIndexException.
  // assume $dim is not OutOfIndex, otherwise the array will just grow in size w/o throwing an error.
  function set($dim, $value) {
    $this->values[$dim] = $value;
  }
  function get($dim) {
    return array_key_exists($dim, $this->values) ? $this->values[$dim] : NAN;
  }

  /**
   * Count the number of vectors. This works for SparseVector too.
   * It only counts valid numbers, not the size the vector is supposed to be.
   * @param $may_cache
   * @return the count number
   */
  function count($may_cache = FALSE) {
    if (!$may_cache || $this->count === NULL) {

      // triggers counting
      $this->count = count($this->values);
    }
    return $this->count;
  }

  /**
   * Calculate the mean. This works for SparseMatrix too.
   * @param $may_cache
   * @return mean value
   */
  function mean($may_cache = FALSE) {
    if (!$may_cache || $this->mean === NULL) {

      // force calculation
      $count = $this
        ->count($may_cache);
      $this->mean = $count == 0 ? NAN : array_sum($this->values) / $count;
    }
    return $this->mean;
  }

  /**
   * Calculate the variance. This works for SparseMatrix too.
   * @param $may_cache
   * @return mean value
   */
  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;
  }
  function std($may_cache = FALSE) {
    if (!$may_cache || $this->std === NULL) {

      // force calculation
      $variance = $this
        ->variance($may_cache);
      $this->std = is_nan($variance) ? NAN : sqrt($variance);
    }
    return $this->std;
  }

  /**
   * Compute covariance with $vector. No caching option.
   * Works for RealVector. SparseVector needs additional handling.
   * @param $vector it has to be the same type (either SparseVector or RealVector) as $this
   * @return covariance value
   */
  function covariance(&$vector) {

    // $arary_a and $array_b just pass by reference
    $array_a =& $this->values;
    $array_b =& $vector->values;
    $mean_a = $this
      ->mean(TRUE);
    $mean_b = $vector
      ->mean(TRUE);
    $count = $this
      ->count(TRUE);

    // if the vector doesn't have any elements, covariance would be NAN.
    if ($count == 0) {
      return NAN;
    }
    $covariance = 0;
    for ($i = 0; $i < $count; $i++) {
      $covariance += ($array_a[$i] - $mean_a) * ($array_b[$i] - $mean_b);
    }
    return $covariance / $count;
  }

  /**
   * Compute correlation with $vector. No caching option.
   * Works for RealVector. SparseVector needs additional handling.
   * @param $vector it has to be the same type (either SparseVector or RealVector) as $this
   * @return correlation value
   */
  function correlation(&$vector) {
    $covariance = $this
      ->covariance($vector);
    if (is_nan($covariance)) {
      return NAN;
    }

    // might use cached std.
    $std_a = $this
      ->std(TRUE);
    $std_b = $vector
      ->std(TRUE);
    return $std_a == 0 || $std_b == 0 ? NAN : $covariance / ($std_a * $std_b);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Vector::$count protected property
Vector::$mean protected property
Vector::$std protected property
Vector::$values protected property
Vector::$variance protected property
Vector::correlation function Compute correlation with $vector. No caching option. Works for RealVector. SparseVector needs additional handling. 1
Vector::count function Count the number of vectors. This works for SparseVector too. It only counts valid numbers, not the size the vector is supposed to be.
Vector::covariance function Compute covariance with $vector. No caching option. Works for RealVector. SparseVector needs additional handling. 1
Vector::create static function Factory method to create a vector. Note, no parameter checking. Array index has to be [0..n), otherwise program unstable
Vector::get function
Vector::mean function Calculate the mean. This works for SparseMatrix too.
Vector::set function
Vector::std function
Vector::variance function Calculate the variance. This works for SparseMatrix too.
Vector::wrap static function Factory method. Wrap the array of numbers into the vector. Note: passing by reference!
Vector::__construct protected function