100.0) { throw new InvalidArgumentException('Percentile must be' . " between 0 and 100, $p given."); } $this->p = $p; } /** * Return the data type the strategy handles. * * @return DataType */ public function type() : DataType { return DataType::continuous(); } /** * Has the strategy been fitted? * * @internal * * @return bool */ public function fitted() : bool { return isset($this->percentile); } /** * Fit the guessing strategy to a set of values. * * @internal * * @param list $values * @throws InvalidArgumentException */ public function fit(array $values) : void { if (empty($values)) { throw new InvalidArgumentException('Strategy must be fitted' . ' to at least 1 value.'); } $this->percentile = Stats::quantile($values, $this->p / 100.0); } /** * Make a guess. * * @internal * * @throws RuntimeException * @return float */ public function guess() : float { if ($this->percentile === null) { throw new RuntimeException('Strategy has not been fitted.'); } return $this->percentile; } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return "Percentile (p: {$this->p})"; } }