stats_covariance
(PECL stats >= 1.0.0)
stats_covariance — Computes the covariance of two data sets
Description
float stats_covariance
( array
$a
, array $b
)Warning
This function is currently not documented; only its argument list is available.
Parameters
-
a
-
-
b
-
Return Values
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Математические расширения
- Статистика
- stats_absolute_deviation
- stats_cdf_beta
- stats_cdf_binomial
- stats_cdf_cauchy
- stats_cdf_chisquare
- stats_cdf_exponential
- stats_cdf_f
- stats_cdf_gamma
- stats_cdf_laplace
- stats_cdf_logistic
- stats_cdf_negative_binomial
- stats_cdf_noncentral_chisquare
- stats_cdf_noncentral_f
- stats_cdf_poisson
- stats_cdf_t
- stats_cdf_uniform
- stats_cdf_weibull
- stats_covariance
- stats_den_uniform
- stats_dens_beta
- stats_dens_cauchy
- stats_dens_chisquare
- stats_dens_exponential
- stats_dens_f
- stats_dens_gamma
- stats_dens_laplace
- stats_dens_logistic
- stats_dens_negative_binomial
- stats_dens_normal
- stats_dens_pmf_binomial
- stats_dens_pmf_hypergeometric
- stats_dens_pmf_poisson
- stats_dens_t
- stats_dens_weibull
- stats_harmonic_mean
- stats_kurtosis
- stats_rand_gen_beta
- stats_rand_gen_chisquare
- stats_rand_gen_exponential
- stats_rand_gen_f
- stats_rand_gen_funiform
- stats_rand_gen_gamma
- stats_rand_gen_ibinomial_negative
- stats_rand_gen_ibinomial
- stats_rand_gen_int
- stats_rand_gen_ipoisson
- stats_rand_gen_iuniform
- stats_rand_gen_noncenral_chisquare
- stats_rand_gen_noncentral_f
- stats_rand_gen_noncentral_t
- stats_rand_gen_normal
- stats_rand_gen_t
- stats_rand_get_seeds
- stats_rand_phrase_to_seeds
- stats_rand_ranf
- stats_rand_setall
- stats_skew
- stats_standard_deviation
- stats_stat_binomial_coef
- stats_stat_correlation
- stats_stat_gennch
- stats_stat_independent_t
- stats_stat_innerproduct
- stats_stat_noncentral_t
- stats_stat_paired_t
- stats_stat_percentile
- stats_stat_powersum
- stats_variance
Коментарии
<?php
//Covariance Calculation
function standard_covariance($aValues,$bValues)
{
$a= (array_sum($aValues)*array_sum($bValues))/count($aValues);
$ret = array();
for($i=0;$i<count($aValues);$i++)
{
$ret[$i]=$aValues[$i]*$bValues[$i];
}
$b=(array_sum($ret)-$a)/(count($aValues)-1);
return (float) $b;
}
$aValues=array(3,4,5,7);
$bValues=array(10,11,13,14);
echo standard_covariance($aValues,$bValues);
?>
// kanniprabu's function is wrong.
// You can check this function with COVARIANCE.P Excel function:
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 );
} // for
return $add / floatval( $countA );
}