public static function fromXYArrays(array $x, array $y, $sample = true, float $meanX = null, float $meanY = null)
    {
        if (empty($x) || empty($y)) {
            throw InvalidArgumentException::arrayCantBeEmpty();
        }

        $n = count($x);
        if ($sample && $n === 1) {
            throw InvalidArgumentException::arraySizeToSmall(2);
        }

        if ($meanX === null) {
            $meanX = Mean::arithmetic($x);
        }

        if ($meanY === null) {
            $meanY = Mean::arithmetic($y);
        }

        $sum = 0.0;
        foreach ($x as $index => $xi) {
            $yi = $y[$index];
            $sum += ($xi - $meanX) * ($yi - $meanY);
        }

        if ($sample) {
            --$n;
        }

        return $sum / $n;
    }