示例#1
0
 /**
  * Evaluate the covariance amongst 2 sets.
  *
  * @param array $array1 set 1 of values.
  * @param array $array2 set 2 of values.
  * @return number covariance.
  * @throws \Exception
  */
 public static function covariance($array1, $array2)
 {
     if (count($array1) != count($array2)) {
         throw new \Exception('Array mismatch');
     }
     $arrayLength = count($array1);
     $sum1 = Basic::sum($array1);
     $sum2 = Basic::sum($array2);
     $total = 0;
     for ($i = 0; $i < $arrayLength; ++$i) {
         $total += $array1[$i] * $array2[$i];
     }
     return ($total - $sum1 * $sum2 / $arrayLength) / $arrayLength;
 }