MathPHP\Statistics\Correlation::sampleCovariance PHP Метод

sampleCovariance() публичный статический Метод

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