@param array|float[] $a
@param bool          $sample

@return float

@throws InvalidArgumentException
549409
public static function population(array $a, $sample = true)
    {
        if (empty($a)) {
            throw InvalidArgumentException::arrayCantBeEmpty();
        }

        $n = count($a);

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

        $mean = Mean::arithmetic($a);
        $carry = 0.0;
        foreach ($a as $val) {
            $d = $val - $mean;
            $carry += $d * $d;
        }

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

        return sqrt((float) ($carry / $n));
    }
818141
public function transform(array &$samples) : void
    {
        foreach ($samples as &$sample) {
            $norm = 0.;

            foreach ($sample as &$feature) {
                $norm += $feature ** 2;
            }

            $norm = sqrt($norm ?: EPSILON);

            foreach ($sample as &$feature) {
                $feature /= $norm;
            }
        }
    }
250043
public function distance(array $a, array $b): float
    {
        if (count($a) !== count($b)) {
            throw InvalidArgumentException::arraySizeNotMatch();
        }

        $differences = [];
        $count = count($a);

        for ($i = 0; $i < $count; ++$i) {
            $differences[] = abs($a[$i] - $b[$i]);
        }

        return max($differences);
    }
386141
public static function skewness(array $values, ?float $mean = null, ?int $n = null) : float
    {
        $n = $n ?? count($values);

        if ($n === 0) {
            throw new InvalidArgumentException('Skewness is undefined for'
                . ' empty set.');
        }

        $mean = $mean ?? self::mean($values, $n);

        $numerator = self::centralMoment($values, 3, $mean);
        $denominator = self::centralMoment($values, 2, $mean) ** 1.5;

        return $numerator / ($denominator ?: EPSILON);
    }
940934
private function frequency(array $sample): int
    {
        return count(array_filter($this->samples, function ($entry) use ($sample) {
            return $this->subset($entry, $sample);
        }));
    }
1039025
public function transform(array $sample)
    {
        if (!$this->fit) {
            throw new \Exception("LDA has not been fitted with respect to original dataset, please run LDA::fit() first");
        }

        if (!is_array($sample[0])) {
            $sample = [$sample];
        }

        return $this->reduce($sample);
    }
207749
protected function calculate_sample($sampleid, $sampleorigin, $starttime = false, $endtime = false) {

        if (!$starttime || !$endtime) {
            return null;
        }

        $select = '';
        $params = array();

        if ($user = $this->retrieve('user', $sampleid)) {
            $select .= "userid = :userid AND ";
            $params = $params + array('userid' => $user->id);
        }

        if (!$logstore = \core_analytics\manager::get_analytics_logstore()) {
            throw new \coding_exception('No available log stores');
        }

        // Filter by context to use the logstore_standard_log db table index.
        $context = $this->retrieve('context', $sampleid);
        $select .= "contextlevel = :contextlevel AND contextinstanceid = :contextinstanceid AND " .
            "crud = 'r' AND timecreated > :starttime AND timecreated <= :endtime";
        $params = $params + array('contextlevel' => $context->contextlevel,
            'contextinstanceid' => $context->instanceid, 'starttime' => $starttime, 'endtime' => $endtime);
        $nrecords = $logstore->get_events_select_count($select, $params);
        // We define a list of ranges to fit $nrecords into it
        // # Done absolutely nothing
        // # Not much really, just accessing the course once a week
        // # More than just accessing the course, some interaction
        // # Significant contribution, will depend on the course anyway.

        // We need to adapt the limits to the time range duration.
        $nweeks = $this->get_time_range_weeks_number($starttime, $endtime);

        // Careful with this, depends on the course.
        $limit = $nweeks * 3 * 10;
        $ranges = array(
            array('eq', 0),
            // 1 course access per week (3 records are easily generated).
            array('le', $nweeks * 3),
            // 3 course accesses per week doing some stuff.
            array('le', $limit),
            array('gt', $limit)
        );
        return $this->classify_value($nrecords, $ranges);
    }
442112
public function setStartSample($startSample)
	{
		if ( ! is_numeric($startSample))
		{
			throw new \InvalidArgumentException('The start sample value must be a number.');
		}
		
		$this->startSample = (int) $startSample;
		
		return $this;
	}
536800
public static function average( $arr, $key = null ) 
	{
		if( !is_array( $arr ) ) 
		{
			throw new \InvalidArgumentException('CCArr::average - first argunent has to be an array.');
		}

		if ( is_string( $key ) && CCArr::is_multi( $arr ) ) 
		{
			$arr = CCArr::pick( $key, $arr );
		}

		return ( static::sum( $arr ) / count( $arr ) );
	}
130822
public static function variance(array $values, ?float $mean = null, ?int $n = null) : float
    {
        $n = $n ?? count($values);

        if ($n < 1) {
            throw new InvalidArgumentException('Variance is undefined for an'
                . ' empty set.');
        }

        $mean = $mean ?? self::mean($values);

        $ssd = 0.;

        foreach ($values as $value) {
            $ssd += ($value - $mean) ** 2;
        }

        return $ssd / $n;
    }
457342
private function powerSet(array $sample): array
    {
        $results = [[]];
        foreach ($sample as $item) {
            foreach ($results as $combination) {
                $results[] = array_merge([$item], $combination);
            }
        }

        return $results;
    }
202903
public static function expandExamples($examples)
    {
        $result = [];
        $m = null;

        $len = \strlen(', …');
        if (\substr($examples, -$len) === ', …') {
            $examples = \substr($examples, 0, \strlen($examples) - $len);
        }

        foreach (\explode(',', \str_replace(' ', '', $examples)) as $range) {
            if (\preg_match('/^\d+$/', $range)) {
                $result[] = (int) $range;
            } elseif (\preg_match('/^(\d+)~(\d+)$/', $range, $m)) {
                $from = (int) $m[1];
                $to = (int) $m[2];
                $delta = $to - $from;
                $step = (int) \max(1, $delta / 100);
                for ($i = $from; $i < $to; $i += $step) {
                    $result[] = $i;
                }
                $result[] = $to;
            } else {
                throw new \InvalidArgumentException("Unhandled test range '$range' in '$examples'");
            }
        }

        if (empty($result)) {
            throw new \InvalidArgumentException("No test numbers from '$examples'");
        }

        return $result;
    }
706067
protected static function setSampled($sampler)
    {
        if ($sampler === null) {
            static::$sampled = true;
            return null;
        }

        if (is_bool($sampler)) {
            static::$sampled = $sampler;
            return null;
        }

        if (!is_bool($sampler) && !($sampler instanceof Sampler)) {
            throw new \InvalidArgumentException('$sampler must be instance of Sampler or boolean');
        }

        static::$sampled = $sampler->isSampled();
        return null;
    }
253750
static public function mean(array $sample)
    {
        $count = count($sample);
        if ($count === 0) {
            return false;
        }

        $sum = 0;
        foreach ($sample as $s) {
            $sType = gettype($s);

            if ($sType !== 'integer' && $sType !== 'double') {
                // only deal with numeric values.
                return false;
            }

            $sum += $s;
        }

        return $sum / $count;
    }
664496
public function distance(array $a, array $b): float
    {
        if (count($a) !== count($b)) {
            throw InvalidArgumentException::arraySizeNotMatch();
        }

        $distance = 0;
        $count = count($a);

        for ($i = 0; $i < $count; ++$i) {
            $distance += pow(abs($a[$i] - $b[$i]), $this->lambda);
        }

        return (float)pow($distance, 1 / $this->lambda);
    }
881253
public int[] getSample(int sampleSize)
  {
    int[] sample = new int[sampleSize];
    for(int i = 0; i< sampleSize;i++){
      sample[i] = randGenerator.nextInt(this.dataDimension()); //Just generate a random index
    }
    return sample;
  }
826823
public static function floats(float $min, float $max, int $n = 10) : array
    {
        if (($max - $min) < 0.) {
            throw new InvalidArgumentException('Maximum cannot be'
                . ' less than minimum.');
        }

        if ($n < 1) {
            throw new InvalidArgumentException('Cannot generate less'
                . ' than 1 parameter.');
        }

        $min = (int) round($min * self::PHI);
        $max = (int) round($max * self::PHI);

        $distribution = [];

        for ($i = 0; $i < $n; $i++) {
            $distribution[] = rand($min, $max) / self::PHI;
        }

        return $distribution;
    }