private function analyzeRoutine(Routine $routine, PerformanceStatistic $statistic, $target) { $sum = 0; $min = 30; $max = 0; $scores = $routine->getPerformanceScores()->count(); $values = []; foreach ($routine->getPerformanceScores() as $score) { $method = 'get' . $target; $val = $score->{$method}(); $min = min($min, $val); $max = max($max, $val); $sum += $val; $values[] = $val; } $avg = $sum / $scores; $statistic->setAverage($avg); $statistic->setMin($min); $statistic->setMax($max); $statistic->setRange(abs($max - $min)); // median sort($values); if ($scores % 2 == 0) { $lower = floor($scores / 2); $upper = ceil($scores / 2); $md = ($values[$lower] + $values[$upper]) / 2; } else { $md = $values[ceil($scores / 2)]; } $statistic->setMedian($md); // sd + variance $sum = 0; foreach ($routine->getPerformanceScores() as $score) { $method = 'get' . $target; $sum += pow($val - $avg, 2); } $v = 1 / ($scores - 1) * $sum; $sd = sqrt($v); $statistic->setVariance($v); $statistic->setStandardDeviation($sd); $statistic->setVariabilityCoefficient($sd / $avg * 100); $statistic->save(); return $statistic; }