public function __construct(array $samples, array $stats = []) { if (count($samples) < 1) { throw new \LogicException('Cannot create a distribution with zero samples.'); } $this->samples = $samples; $this->closures = ['min' => function () { return min($this->samples); }, 'max' => function () { return max($this->samples); }, 'sum' => function () { return array_sum($this->samples); }, 'stdev' => function () { return Statistics::stdev($this->samples); }, 'mean' => function () { return Statistics::mean($this->samples); }, 'mode' => function () { return Statistics::kdeMode($this->samples); }, 'variance' => function () { return Statistics::variance($this->samples); }, 'rstdev' => function () { $mean = $this->getMean(); return $mean ? $this->getStdev() / $mean * 100 : 0; }]; if ($diff = array_diff(array_keys($stats), array_keys($this->closures))) { throw new \RuntimeException(sprintf('Unknown pre-computed stat(s) encountered: "%s"', implode('", "', $diff))); } $this->stats = $stats; }
/** * Computes the covariance matrix for each Gaussian kernel using * coVarianceFactor(). */ private function computeCovariance() { $factorCallable = $this->coVarianceFactor; $this->factor = $factorCallable(); // Cache covariance and inverse covariance of the data if (null === $this->_dataInvCov) { // original used the numpy.cov function. $this->_dataCovariance = pow(Statistics::stdev($this->dataset, true), 2); //$this->_dataInvCov = 1/ linalg.inv($this->_dataCovariance) $this->_dataInvCov = 1 / $this->_dataCovariance; } $this->covariance = $this->_dataCovariance * pow($this->factor, 2); $this->invCov = $this->_dataInvCov / pow($this->factor, 2); $this->normFactor = sqrt(2 * M_PI * $this->covariance) * count($this->dataset); }