Skip to content

[12.x] Add percentile method to Collection #56122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

nathanwuiske
Copy link

@nathanwuiske nathanwuiske commented Jun 24, 2025

Summary

This PR introduces a percentile() helper to Illuminate\Support\Collection, filling the gap between the existing median() and mode() statistics helpers.

By computing percentiles, we can quickly understand the distribution of a dataset - whether it’s latency measurements, order totals, or any numeric series—and make decisions based on relative standing.


Why?

  • Dashboards, alerts, and reports frequently rely on P95 latency, P90 response time, or top-10% spenders - all straightforward percentile queries.
  • Collections already ship with mean, median, and mode. Percentiles are the logical missing metric in the basic descriptive-statistics toolkit.

Usage

// 1) P90 of students’ average grades:
$students = collect([
    ['name' => 'Alice', 'grades' => [88, 92, 85]],
    ['name' => 'Bob',   'grades' => [75, 78, 82]],
    ['name' => 'Cara',  'grades' => [95, 98, 100]],
]);

$p90Avg = $students
    ->map(fn($s) => collect($s['grades'])->avg())
    ->percentile(0.9);  // ≈ 95.9

// 2) Top-20% order totals:
$orderTotals = collect([45.00, 79.99, 120.00, 55.50, 65.00]);
$threshold   = $orderTotals->percentile(0.8); // e.g. 75.00

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@nathanwuiske nathanwuiske marked this pull request as ready for review June 24, 2025 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants