MathPHP\Statistics\Correlation::populationCovariance PHP Method

populationCovariance() public static method

Average product of their deviations from their respective means. The population covariance is defined in terms of the population means μx, μy https://en.wikipedia.org/wiki/Covariance cov(X, Y) = σxy = E[⟮X - μx⟯⟮Y - μy⟯] ∑⟮xᵢ - μₓ⟯⟮yᵢ - μy⟯ cov(X, Y) = σxy = ----------------- N
public static populationCovariance ( array $X, array $Y ) : number
$X array values for random variable X
$Y array values for random variable Y
return number
    public static function populationCovariance(array $X, array $Y)
    {
        if (count($X) !== count($Y)) {
            throw new Exception\BadDataException('X and Y must have the same number of elements.');
        }
        $μₓ = Average::mean($X);
        $μy = Average::mean($Y);
        $∑⟮xᵢ − μₓ⟯⟮yᵢ − μy⟯ = array_sum(array_map(function ($xᵢ, $yᵢ) use($μₓ, $μy) {
            return ($xᵢ - $μₓ) * ($yᵢ - $μy);
        }, $X, $Y));
        $N = count($X);
        return $∑⟮xᵢ − μₓ⟯⟮yᵢ − μy⟯ / $N;
    }