php教程

stats_skew

(PECL stats >= 1.0.0)

stats_skewComputes the skewness of the data in the array

说明

stats_skew ( array $a ) : float

Returns the skewness of the values in a.

参数

a

The input array

返回值

Returns the skewness of the values in a, or FALSE if a is empty or is not an array.

User Contributed Notes

Angel J. Salinas 11-Dec-2015 08:35
If you don't have the php_stats library, you can use this implementation:

  public static function getSkewness( $values )
  {
    $numValues = count( $values );
    if ( $numValues == 0 ) {
      return 0.0;
    }
   
    // Use function from php_stats library if available
    if ( function_exists( 'stats_skew' ) ) {
      return stats_skew( $values );
    }
   
    $mean = array_sum( $values ) / floatval( $numValues );
   
    $add2 = 0.0;
    $add3 = 0.0;
    foreach ( $values as $value ) {
      if ( ! is_numeric( $value ) ) {
        return false;
      }
   
      $dif = $value - $mean;
      $add2 += ( $dif * $dif );
      $add3 += ( $dif * $dif * $dif );
     
    } // foreach values
   
    $variance = $add2 / floatval( $numValues );
   
    return ( $add3 / floatval( $numValues ) ) / pow( $variance, 3 / 2.0  );
  }

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788