/** * Calculate the regression parameters using the Theil-Sen method * * Procedure: * Calculate the slopes of all pairs of points and select the median value * Calculate the intercept using the slope, and the medians of the X and Y values. * b = Ymedian - (m * Xmedian) */ public function calculate() { // The slopes array will be a list of slopes between all pairs of points $slopes = []; $n = count($this->points); for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { $pointi = $this->points[$i]; $pointj = $this->points[$j]; $slopes[] = ($pointj[1] - $pointi[1]) / ($pointj[0] - $pointi[0]); } } $this->m = Average::median($slopes); $this->b = Average::median($this->ys) - $this->m * Average::median($this->xs); $this->parameters = [$this->b, $this->m]; }
/** * Five number summary * A descriptive statistic that provides information about a set of observations. * It consists of the five most important sample percentiles: * 1) the sample minimum (smallest observation) * 2) the lower quartile or first quartile * 3) the median (middle value) * 4) the upper quartile or third quartile * 5) the sample maximum (largest observation) * * https://en.wikipedia.org/wiki/Five-number_summary * * @param array $numbers * * @return array [min, Q1, median, Q3, max] */ public static function fiveNumberSummary(array $numbers) { $quartiles = self::quartiles($numbers); return ['min' => min($numbers), 'Q1' => $quartiles['Q1'], 'median' => Average::median($numbers), 'Q3' => $quartiles['Q3'], 'max' => max($numbers)]; }