kthSmallest() public static method

if $a = [1,2,3,4,6,7] kthSmallest($a, 4) = 6 Algorithm: 1) If n is small, just sort and return 2) Otherwise, group into 5-element subsets and mind the median 3) Find the median of the medians 4) Find L and U sets - L is numbers lower than the median of medians - U is numbers higher than the median of medians 5) Recursive step - if k is the median of medians, return that - Otherwise, recusively search in smaller group.
public static kthSmallest ( array $numbers, integer $k ) : number
$numbers array
$k integer zero indexed
return number
Ejemplo n.º 1
0
 /**
  * Evaluate for x
  * Use the smoothness parameter α to determine the subset of data to consider for
  * local regression. Perform a weighted least squares regression and evaluate x.
  *
  * @param  number $x
  *
  * @return number
  */
 public function evaluate($x)
 {
     $α = $this->α;
     $λ = $this->λ;
     $n = $this->n;
     // The number of points considered in the local regression
     $Δx = Single::abs(Single::subtract($this->xs, $x));
     $αᵗʰΔx = Average::kthSmallest($Δx, $this->number_of_points - 1);
     $arg = Single::min(Single::divide($Δx, $αᵗʰΔx * max($α, 1)), 1);
     // Kernel function: tricube = (1-arg³)³
     $tricube = Single::cube(Single::multiply(Single::subtract(Single::cube($arg), 1), -1));
     $weights = $tricube;
     // Local Regression Parameters
     $parameters = $this->leastSquares($this->ys, $this->xs, $weights, $λ);
     $X = new VandermondeMatrix([$x], $λ + 1);
     return $X->multiply($parameters)[0][0];
 }