Search code examples
phpstatisticscovariancequantitative-finance

How to calculate covariance in PhP for arrays of different sizes?


This function computes the covariance of two arrays of the same size (e.g., $countA=$countB=20).

How could it be expanded for arrays of different sizes? (For instance, to calculate based on the size of smaller array, if $countA=10 and $countB=12? How low the smaller array could be and still would make statistical sense?)

Thanks a million!

M

function getCovariance($valuesA, $valuesB){
  $countA=count($valuesA);
  $countB=count($valuesB);
  if($countA != $countB){
    trigger_error('Arrays with different sizes: countA='. $countA .', countB='. $countB, E_USER_WARNING);
    return false;
  }

  if($countA < 0){
    trigger_error('Empty arrays', E_USER_WARNING);
    return false;
  }

  // Use library function if available
  if(function_exists('stats_covariance')){
    return stats_covariance($valuesA, $valuesB);
  }

  $meanA=array_sum($valuesA) / floatval($countA);
  $meanB=array_sum($valuesB) / floatval($countB);
  $add=0.0;

  for ($pos=0; $pos < $countA; $pos++){
    $valueA=$valuesA[ $pos ];
    if(!is_numeric($valueA)){
      trigger_error('Not numerical value in array A at position '. $pos .', value='. $valueA, E_USER_WARNING);
      return false;
    }

    $valueB=$valuesB[ $pos ];
    if(!is_numeric($valueB)){
      trigger_error('Not numerical value in array B at position '. $pos .', value='. $valueB, E_USER_WARNING);
      return false;
    }

    $difA=$valueA - $meanA;
    $difB=$valueB - $meanB;
    $add += ($difA * $difB);
  }

  return $add / floatval($countA);
}

Solution

  • Use the min() to find which array has the fewest items and slice them both at that number of items.

    function getCovariance($valuesA, $valuesB){
      // sizing both arrays the same, if different sizes, @Andreas @stackoverflow
      $no_keys = min(count($valuesA), count($valuesB));
      $valuesA = array_slice($valuesA, 0, $no_keys);
      $valuesB = array_slice($valuesB, 0, $no_keys);
    
      // if size of arrays is too small
      if($no_keys<2){return 0.0000000000001;}
    
      // Use library function if available
      if(function_exists('stats_covariance')){return stats_covariance($valuesA, $valuesB);}
    
      $meanA=array_sum($valuesA)/$no_keys;
      $meanB=array_sum($valuesB)/$no_keys;
      $add=0.0;
    
      for ($pos=0; $pos < $no_keys; $pos++){
        $valueA=$valuesA[ $pos ];
        if(!is_numeric($valueA)){
          trigger_error('Not numerical value in array A at position '. $pos .', value='. $valueA, E_USER_WARNING);
          return false;
        }
    
        $valueB=$valuesB[ $pos ];
        if(!is_numeric($valueB)){
          trigger_error('Not numerical value in array B at position '. $pos .', value='. $valueB, E_USER_WARNING);
          return false;
        }
    
        $difA=$valueA - $meanA;
        $difB=$valueB - $meanB;
        $add += ($difA * $difB);
      }
    
      return $add/$no_keys;
    }